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 | ||
68fe256a PB |
106 | #define SQE_COMMON_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_LINK | \ |
107 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) | |
108 | ||
109 | #define SQE_VALID_FLAGS (SQE_COMMON_FLAGS|IOSQE_BUFFER_SELECT|IOSQE_IO_DRAIN) | |
110 | ||
c854357b PB |
111 | #define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \ |
112 | REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS) | |
b16fed66 | 113 | |
09899b19 PB |
114 | #define IO_TCTX_REFS_CACHE_NR (1U << 10) |
115 | ||
2b188cc1 JA |
116 | struct io_uring { |
117 | u32 head ____cacheline_aligned_in_smp; | |
118 | u32 tail ____cacheline_aligned_in_smp; | |
119 | }; | |
120 | ||
1e84b97b | 121 | /* |
75b28aff HV |
122 | * This data is shared with the application through the mmap at offsets |
123 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
124 | * |
125 | * The offsets to the member fields are published through struct | |
126 | * io_sqring_offsets when calling io_uring_setup. | |
127 | */ | |
75b28aff | 128 | struct io_rings { |
1e84b97b SB |
129 | /* |
130 | * Head and tail offsets into the ring; the offsets need to be | |
131 | * masked to get valid indices. | |
132 | * | |
75b28aff HV |
133 | * The kernel controls head of the sq ring and the tail of the cq ring, |
134 | * and the application controls tail of the sq ring and the head of the | |
135 | * cq ring. | |
1e84b97b | 136 | */ |
75b28aff | 137 | struct io_uring sq, cq; |
1e84b97b | 138 | /* |
75b28aff | 139 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
140 | * ring_entries - 1) |
141 | */ | |
75b28aff HV |
142 | u32 sq_ring_mask, cq_ring_mask; |
143 | /* Ring sizes (constant, power of 2) */ | |
144 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
145 | /* |
146 | * Number of invalid entries dropped by the kernel due to | |
147 | * invalid index stored in array | |
148 | * | |
149 | * Written by the kernel, shouldn't be modified by the | |
150 | * application (i.e. get number of "new events" by comparing to | |
151 | * cached value). | |
152 | * | |
153 | * After a new SQ head value was read by the application this | |
154 | * counter includes all submissions that were dropped reaching | |
155 | * the new SQ head (and possibly more). | |
156 | */ | |
75b28aff | 157 | u32 sq_dropped; |
1e84b97b | 158 | /* |
0d9b5b3a | 159 | * Runtime SQ flags |
1e84b97b SB |
160 | * |
161 | * Written by the kernel, shouldn't be modified by the | |
162 | * application. | |
163 | * | |
164 | * The application needs a full memory barrier before checking | |
165 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
166 | */ | |
75b28aff | 167 | u32 sq_flags; |
0d9b5b3a SG |
168 | /* |
169 | * Runtime CQ flags | |
170 | * | |
171 | * Written by the application, shouldn't be modified by the | |
172 | * kernel. | |
173 | */ | |
fe7e3257 | 174 | u32 cq_flags; |
1e84b97b SB |
175 | /* |
176 | * Number of completion events lost because the queue was full; | |
177 | * this should be avoided by the application by making sure | |
0b4295b5 | 178 | * there are not more requests pending than there is space in |
1e84b97b SB |
179 | * the completion queue. |
180 | * | |
181 | * Written by the kernel, shouldn't be modified by the | |
182 | * application (i.e. get number of "new events" by comparing to | |
183 | * cached value). | |
184 | * | |
185 | * As completion events come in out of order this counter is not | |
186 | * ordered with any other data. | |
187 | */ | |
75b28aff | 188 | u32 cq_overflow; |
1e84b97b SB |
189 | /* |
190 | * Ring buffer of completion events. | |
191 | * | |
192 | * The kernel writes completion events fresh every time they are | |
193 | * produced, so the application is allowed to modify pending | |
194 | * entries. | |
195 | */ | |
75b28aff | 196 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
197 | }; |
198 | ||
45d189c6 PB |
199 | enum io_uring_cmd_flags { |
200 | IO_URING_F_NONBLOCK = 1, | |
889fca73 | 201 | IO_URING_F_COMPLETE_DEFER = 2, |
45d189c6 PB |
202 | }; |
203 | ||
edafccee JA |
204 | struct io_mapped_ubuf { |
205 | u64 ubuf; | |
4751f53d | 206 | u64 ubuf_end; |
edafccee | 207 | unsigned int nr_bvecs; |
de293938 | 208 | unsigned long acct_pages; |
41edf1a5 | 209 | struct bio_vec bvec[]; |
edafccee JA |
210 | }; |
211 | ||
50238531 BM |
212 | struct io_ring_ctx; |
213 | ||
6c2450ae PB |
214 | struct io_overflow_cqe { |
215 | struct io_uring_cqe cqe; | |
216 | struct list_head list; | |
217 | }; | |
218 | ||
a04b0ac0 PB |
219 | struct io_fixed_file { |
220 | /* file * with additional FFS_* flags */ | |
221 | unsigned long file_ptr; | |
222 | }; | |
223 | ||
269bbe5f BM |
224 | struct io_rsrc_put { |
225 | struct list_head list; | |
b60c8dce | 226 | u64 tag; |
50238531 BM |
227 | union { |
228 | void *rsrc; | |
229 | struct file *file; | |
bd54b6fe | 230 | struct io_mapped_ubuf *buf; |
50238531 | 231 | }; |
269bbe5f BM |
232 | }; |
233 | ||
aeca241b | 234 | struct io_file_table { |
042b0d85 | 235 | struct io_fixed_file *files; |
31b51510 JA |
236 | }; |
237 | ||
b895c9a6 | 238 | struct io_rsrc_node { |
05589553 XW |
239 | struct percpu_ref refs; |
240 | struct list_head node; | |
269bbe5f | 241 | struct list_head rsrc_list; |
b895c9a6 | 242 | struct io_rsrc_data *rsrc_data; |
4a38aed2 | 243 | struct llist_node llist; |
e297822b | 244 | bool done; |
05589553 XW |
245 | }; |
246 | ||
40ae0ff7 PB |
247 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
248 | ||
b895c9a6 | 249 | struct io_rsrc_data { |
05f3fb3c JA |
250 | struct io_ring_ctx *ctx; |
251 | ||
2d091d62 PB |
252 | u64 **tags; |
253 | unsigned int nr; | |
40ae0ff7 | 254 | rsrc_put_fn *do_put; |
3e942498 | 255 | atomic_t refs; |
05f3fb3c | 256 | struct completion done; |
8bad28d8 | 257 | bool quiesce; |
05f3fb3c JA |
258 | }; |
259 | ||
5a2e745d JA |
260 | struct io_buffer { |
261 | struct list_head list; | |
262 | __u64 addr; | |
d1f82808 | 263 | __u32 len; |
5a2e745d JA |
264 | __u16 bid; |
265 | }; | |
266 | ||
21b55dbc SG |
267 | struct io_restriction { |
268 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
269 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
270 | u8 sqe_flags_allowed; | |
271 | u8 sqe_flags_required; | |
7e84e1c7 | 272 | bool registered; |
21b55dbc SG |
273 | }; |
274 | ||
37d1e2e3 JA |
275 | enum { |
276 | IO_SQ_THREAD_SHOULD_STOP = 0, | |
277 | IO_SQ_THREAD_SHOULD_PARK, | |
278 | }; | |
279 | ||
534ca6d6 JA |
280 | struct io_sq_data { |
281 | refcount_t refs; | |
9e138a48 | 282 | atomic_t park_pending; |
09a6f4ef | 283 | struct mutex lock; |
69fb2131 JA |
284 | |
285 | /* ctx's that are using this sqd */ | |
286 | struct list_head ctx_list; | |
69fb2131 | 287 | |
534ca6d6 JA |
288 | struct task_struct *thread; |
289 | struct wait_queue_head wait; | |
08369246 XW |
290 | |
291 | unsigned sq_thread_idle; | |
37d1e2e3 JA |
292 | int sq_cpu; |
293 | pid_t task_pid; | |
5c2469e0 | 294 | pid_t task_tgid; |
37d1e2e3 JA |
295 | |
296 | unsigned long state; | |
37d1e2e3 | 297 | struct completion exited; |
534ca6d6 JA |
298 | }; |
299 | ||
6dd0be1e | 300 | #define IO_COMPL_BATCH 32 |
6ff119a6 | 301 | #define IO_REQ_CACHE_SIZE 32 |
bf019da7 | 302 | #define IO_REQ_ALLOC_BATCH 8 |
258b29a9 | 303 | |
a1ab7b35 PB |
304 | struct io_submit_link { |
305 | struct io_kiocb *head; | |
306 | struct io_kiocb *last; | |
307 | }; | |
308 | ||
258b29a9 PB |
309 | struct io_submit_state { |
310 | struct blk_plug plug; | |
a1ab7b35 | 311 | struct io_submit_link link; |
258b29a9 | 312 | |
258b29a9 | 313 | bool plug_started; |
4b628aeb | 314 | bool need_plug; |
258b29a9 PB |
315 | |
316 | /* | |
317 | * Batch completion logic | |
318 | */ | |
6f33b0bc PB |
319 | struct io_wq_work_list compl_reqs; |
320 | ||
cd0ca2e0 | 321 | /* inline/task_work completion list, under ->uring_lock */ |
c2b6c6bc | 322 | struct io_wq_work_node free_list; |
258b29a9 PB |
323 | }; |
324 | ||
2b188cc1 | 325 | struct io_ring_ctx { |
b52ecf8c | 326 | /* const or read-mostly hot data */ |
2b188cc1 JA |
327 | struct { |
328 | struct percpu_ref refs; | |
2b188cc1 | 329 | |
b52ecf8c | 330 | struct io_rings *rings; |
2b188cc1 | 331 | unsigned int flags; |
e1d85334 | 332 | unsigned int compat: 1; |
e1d85334 RD |
333 | unsigned int drain_next: 1; |
334 | unsigned int eventfd_async: 1; | |
21b55dbc | 335 | unsigned int restricted: 1; |
f18ee4cf | 336 | unsigned int off_timeout_used: 1; |
10c66904 | 337 | unsigned int drain_active: 1; |
b52ecf8c | 338 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 339 | |
7f1129d2 | 340 | /* submission data */ |
b52ecf8c | 341 | struct { |
0499e582 PB |
342 | struct mutex uring_lock; |
343 | ||
75b28aff HV |
344 | /* |
345 | * Ring buffer of indices into array of io_uring_sqe, which is | |
346 | * mmapped by the application using the IORING_OFF_SQES offset. | |
347 | * | |
348 | * This indirection could e.g. be used to assign fixed | |
349 | * io_uring_sqe entries to operations and only submit them to | |
350 | * the queue when needed. | |
351 | * | |
352 | * The kernel modifies neither the indices array nor the entries | |
353 | * array. | |
354 | */ | |
355 | u32 *sq_array; | |
c7af47cf | 356 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
357 | unsigned cached_sq_head; |
358 | unsigned sq_entries; | |
de0617e4 | 359 | struct list_head defer_list; |
7f1129d2 PB |
360 | |
361 | /* | |
362 | * Fixed resources fast path, should be accessed only under | |
363 | * uring_lock, and updated through io_uring_register(2) | |
364 | */ | |
365 | struct io_rsrc_node *rsrc_node; | |
366 | struct io_file_table file_table; | |
367 | unsigned nr_user_files; | |
368 | unsigned nr_user_bufs; | |
369 | struct io_mapped_ubuf **user_bufs; | |
370 | ||
371 | struct io_submit_state submit_state; | |
5262f567 | 372 | struct list_head timeout_list; |
ef9dd637 | 373 | struct list_head ltimeout_list; |
1d7bb1d5 | 374 | struct list_head cq_overflow_list; |
7f1129d2 PB |
375 | struct xarray io_buffers; |
376 | struct xarray personalities; | |
377 | u32 pers_next; | |
378 | unsigned sq_thread_idle; | |
2b188cc1 JA |
379 | } ____cacheline_aligned_in_smp; |
380 | ||
d0acdee2 | 381 | /* IRQ completion list, under ->completion_lock */ |
c2b6c6bc | 382 | struct io_wq_work_list locked_free_list; |
d0acdee2 | 383 | unsigned int locked_free_nr; |
3c1a2ead | 384 | |
7c30f36a | 385 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
534ca6d6 JA |
386 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
387 | ||
90554200 | 388 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 389 | struct list_head sqd_list; |
75b28aff | 390 | |
5ed7a37d PB |
391 | unsigned long check_cq_overflow; |
392 | ||
206aefde JA |
393 | struct { |
394 | unsigned cached_cq_tail; | |
395 | unsigned cq_entries; | |
0499e582 | 396 | struct eventfd_ctx *cq_ev_fd; |
0499e582 PB |
397 | struct wait_queue_head cq_wait; |
398 | unsigned cq_extra; | |
399 | atomic_t cq_timeouts; | |
0499e582 | 400 | unsigned cq_last_tm_flush; |
206aefde | 401 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 402 | |
2b188cc1 JA |
403 | struct { |
404 | spinlock_t completion_lock; | |
e94f141b | 405 | |
89850fce JA |
406 | spinlock_t timeout_lock; |
407 | ||
def596e9 | 408 | /* |
540e32a0 | 409 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
410 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
411 | * For SQPOLL, only the single threaded io_sq_thread() will | |
412 | * manipulate the list, hence no extra locking is needed there. | |
413 | */ | |
5eef4e87 | 414 | struct io_wq_work_list iopoll_list; |
78076bb6 JA |
415 | struct hlist_head *cancel_hash; |
416 | unsigned cancel_hash_bits; | |
915b3dde | 417 | bool poll_multi_queue; |
2b188cc1 | 418 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 419 | |
21b55dbc | 420 | struct io_restriction restrictions; |
3c1a2ead | 421 | |
b13a8918 PB |
422 | /* slow path rsrc auxilary data, used by update/register */ |
423 | struct { | |
424 | struct io_rsrc_node *rsrc_backup_node; | |
425 | struct io_mapped_ubuf *dummy_ubuf; | |
426 | struct io_rsrc_data *file_data; | |
427 | struct io_rsrc_data *buf_data; | |
428 | ||
429 | struct delayed_work rsrc_put_work; | |
430 | struct llist_head rsrc_put_llist; | |
431 | struct list_head rsrc_ref_list; | |
432 | spinlock_t rsrc_ref_lock; | |
433 | }; | |
434 | ||
3c1a2ead | 435 | /* Keep this last, we don't need it for the fast path */ |
b986af7e PB |
436 | struct { |
437 | #if defined(CONFIG_UNIX) | |
438 | struct socket *ring_sock; | |
439 | #endif | |
440 | /* hashed buffered write serialization */ | |
441 | struct io_wq_hash *hash_map; | |
442 | ||
443 | /* Only used for accounting purposes */ | |
444 | struct user_struct *user; | |
445 | struct mm_struct *mm_account; | |
446 | ||
447 | /* ctx exit and cancelation */ | |
9011bf9a PB |
448 | struct llist_head fallback_llist; |
449 | struct delayed_work fallback_work; | |
b986af7e PB |
450 | struct work_struct exit_work; |
451 | struct list_head tctx_list; | |
452 | struct completion ref_comp; | |
453 | }; | |
2b188cc1 JA |
454 | }; |
455 | ||
53e043b2 SM |
456 | struct io_uring_task { |
457 | /* submission side */ | |
09899b19 | 458 | int cached_refs; |
53e043b2 SM |
459 | struct xarray xa; |
460 | struct wait_queue_head wait; | |
ee53fb2b SM |
461 | const struct io_ring_ctx *last; |
462 | struct io_wq *io_wq; | |
53e043b2 | 463 | struct percpu_counter inflight; |
b303fe2e | 464 | atomic_t inflight_tracked; |
53e043b2 | 465 | atomic_t in_idle; |
53e043b2 SM |
466 | |
467 | spinlock_t task_lock; | |
468 | struct io_wq_work_list task_list; | |
53e043b2 | 469 | struct callback_head task_work; |
6294f368 | 470 | bool task_running; |
53e043b2 SM |
471 | }; |
472 | ||
09bb8394 JA |
473 | /* |
474 | * First field must be the file pointer in all the | |
475 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
476 | */ | |
221c5eb2 JA |
477 | struct io_poll_iocb { |
478 | struct file *file; | |
018043be | 479 | struct wait_queue_head *head; |
221c5eb2 | 480 | __poll_t events; |
8c838788 | 481 | bool done; |
221c5eb2 | 482 | bool canceled; |
392edb45 | 483 | struct wait_queue_entry wait; |
221c5eb2 JA |
484 | }; |
485 | ||
9d805892 | 486 | struct io_poll_update { |
018043be | 487 | struct file *file; |
9d805892 PB |
488 | u64 old_user_data; |
489 | u64 new_user_data; | |
490 | __poll_t events; | |
b69de288 JA |
491 | bool update_events; |
492 | bool update_user_data; | |
018043be PB |
493 | }; |
494 | ||
b5dba59e JA |
495 | struct io_close { |
496 | struct file *file; | |
b5dba59e | 497 | int fd; |
7df778be | 498 | u32 file_slot; |
b5dba59e JA |
499 | }; |
500 | ||
ad8a48ac JA |
501 | struct io_timeout_data { |
502 | struct io_kiocb *req; | |
503 | struct hrtimer timer; | |
504 | struct timespec64 ts; | |
505 | enum hrtimer_mode mode; | |
50c1df2b | 506 | u32 flags; |
ad8a48ac JA |
507 | }; |
508 | ||
8ed8d3c3 JA |
509 | struct io_accept { |
510 | struct file *file; | |
511 | struct sockaddr __user *addr; | |
512 | int __user *addr_len; | |
513 | int flags; | |
aaa4db12 | 514 | u32 file_slot; |
09952e3e | 515 | unsigned long nofile; |
8ed8d3c3 JA |
516 | }; |
517 | ||
518 | struct io_sync { | |
519 | struct file *file; | |
520 | loff_t len; | |
521 | loff_t off; | |
522 | int flags; | |
d63d1b5e | 523 | int mode; |
8ed8d3c3 JA |
524 | }; |
525 | ||
fbf23849 JA |
526 | struct io_cancel { |
527 | struct file *file; | |
528 | u64 addr; | |
529 | }; | |
530 | ||
b29472ee JA |
531 | struct io_timeout { |
532 | struct file *file; | |
bfe68a22 PB |
533 | u32 off; |
534 | u32 target_seq; | |
135fcde8 | 535 | struct list_head list; |
90cd7e42 PB |
536 | /* head of the link, used by linked timeouts only */ |
537 | struct io_kiocb *head; | |
89b263f6 JA |
538 | /* for linked completions */ |
539 | struct io_kiocb *prev; | |
b29472ee JA |
540 | }; |
541 | ||
0bdf7a2d PB |
542 | struct io_timeout_rem { |
543 | struct file *file; | |
544 | u64 addr; | |
9c8e11b3 PB |
545 | |
546 | /* timeout update */ | |
547 | struct timespec64 ts; | |
548 | u32 flags; | |
f1042b6c | 549 | bool ltimeout; |
0bdf7a2d PB |
550 | }; |
551 | ||
9adbd45d JA |
552 | struct io_rw { |
553 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
554 | struct kiocb kiocb; | |
555 | u64 addr; | |
556 | u64 len; | |
557 | }; | |
558 | ||
3fbb51c1 JA |
559 | struct io_connect { |
560 | struct file *file; | |
561 | struct sockaddr __user *addr; | |
562 | int addr_len; | |
563 | }; | |
564 | ||
e47293fd JA |
565 | struct io_sr_msg { |
566 | struct file *file; | |
fddaface | 567 | union { |
4af3417a PB |
568 | struct compat_msghdr __user *umsg_compat; |
569 | struct user_msghdr __user *umsg; | |
570 | void __user *buf; | |
fddaface | 571 | }; |
e47293fd | 572 | int msg_flags; |
bcda7baa | 573 | int bgid; |
fddaface | 574 | size_t len; |
e47293fd JA |
575 | }; |
576 | ||
15b71abe JA |
577 | struct io_open { |
578 | struct file *file; | |
579 | int dfd; | |
b9445598 | 580 | u32 file_slot; |
15b71abe | 581 | struct filename *filename; |
c12cedf2 | 582 | struct open_how how; |
4022e7af | 583 | unsigned long nofile; |
15b71abe JA |
584 | }; |
585 | ||
269bbe5f | 586 | struct io_rsrc_update { |
05f3fb3c JA |
587 | struct file *file; |
588 | u64 arg; | |
589 | u32 nr_args; | |
590 | u32 offset; | |
591 | }; | |
592 | ||
4840e418 JA |
593 | struct io_fadvise { |
594 | struct file *file; | |
595 | u64 offset; | |
596 | u32 len; | |
597 | u32 advice; | |
598 | }; | |
599 | ||
c1ca757b JA |
600 | struct io_madvise { |
601 | struct file *file; | |
602 | u64 addr; | |
603 | u32 len; | |
604 | u32 advice; | |
605 | }; | |
606 | ||
3e4827b0 JA |
607 | struct io_epoll { |
608 | struct file *file; | |
609 | int epfd; | |
610 | int op; | |
611 | int fd; | |
612 | struct epoll_event event; | |
e47293fd JA |
613 | }; |
614 | ||
7d67af2c PB |
615 | struct io_splice { |
616 | struct file *file_out; | |
617 | struct file *file_in; | |
618 | loff_t off_out; | |
619 | loff_t off_in; | |
620 | u64 len; | |
621 | unsigned int flags; | |
622 | }; | |
623 | ||
ddf0322d JA |
624 | struct io_provide_buf { |
625 | struct file *file; | |
626 | __u64 addr; | |
38134ada | 627 | __u32 len; |
ddf0322d JA |
628 | __u32 bgid; |
629 | __u16 nbufs; | |
630 | __u16 bid; | |
631 | }; | |
632 | ||
1d9e1288 BM |
633 | struct io_statx { |
634 | struct file *file; | |
635 | int dfd; | |
636 | unsigned int mask; | |
637 | unsigned int flags; | |
e62753e4 | 638 | const char __user *filename; |
1d9e1288 BM |
639 | struct statx __user *buffer; |
640 | }; | |
641 | ||
36f4fa68 JA |
642 | struct io_shutdown { |
643 | struct file *file; | |
644 | int how; | |
645 | }; | |
646 | ||
80a261fd JA |
647 | struct io_rename { |
648 | struct file *file; | |
649 | int old_dfd; | |
650 | int new_dfd; | |
651 | struct filename *oldpath; | |
652 | struct filename *newpath; | |
653 | int flags; | |
654 | }; | |
655 | ||
14a1143b JA |
656 | struct io_unlink { |
657 | struct file *file; | |
658 | int dfd; | |
659 | int flags; | |
660 | struct filename *filename; | |
661 | }; | |
662 | ||
e34a02dc DK |
663 | struct io_mkdir { |
664 | struct file *file; | |
665 | int dfd; | |
666 | umode_t mode; | |
667 | struct filename *filename; | |
668 | }; | |
669 | ||
7a8721f8 DK |
670 | struct io_symlink { |
671 | struct file *file; | |
672 | int new_dfd; | |
673 | struct filename *oldpath; | |
674 | struct filename *newpath; | |
675 | }; | |
676 | ||
cf30da90 DK |
677 | struct io_hardlink { |
678 | struct file *file; | |
679 | int old_dfd; | |
680 | int new_dfd; | |
681 | struct filename *oldpath; | |
682 | struct filename *newpath; | |
683 | int flags; | |
684 | }; | |
685 | ||
3ca405eb PB |
686 | struct io_completion { |
687 | struct file *file; | |
8c3f9cd1 | 688 | u32 cflags; |
3ca405eb PB |
689 | }; |
690 | ||
f499a021 JA |
691 | struct io_async_connect { |
692 | struct sockaddr_storage address; | |
693 | }; | |
694 | ||
03b1230c JA |
695 | struct io_async_msghdr { |
696 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
697 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
698 | struct iovec *free_iov; | |
03b1230c JA |
699 | struct sockaddr __user *uaddr; |
700 | struct msghdr msg; | |
b537916c | 701 | struct sockaddr_storage addr; |
03b1230c JA |
702 | }; |
703 | ||
f67676d1 JA |
704 | struct io_async_rw { |
705 | struct iovec fast_iov[UIO_FASTIOV]; | |
ff6165b2 JA |
706 | const struct iovec *free_iovec; |
707 | struct iov_iter iter; | |
cd658695 | 708 | struct iov_iter_state iter_state; |
227c0c96 | 709 | size_t bytes_done; |
bcf5a063 | 710 | struct wait_page_queue wpq; |
f67676d1 JA |
711 | }; |
712 | ||
6b47ee6e PB |
713 | enum { |
714 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
715 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
716 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
717 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
718 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 719 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 720 | |
dddca226 | 721 | /* first byte is taken by user flags, shift it to not overlap */ |
93d2bcd2 | 722 | REQ_F_FAIL_BIT = 8, |
6b47ee6e PB |
723 | REQ_F_INFLIGHT_BIT, |
724 | REQ_F_CUR_POS_BIT, | |
725 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 726 | REQ_F_LINK_TIMEOUT_BIT, |
99bc4c38 | 727 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 728 | REQ_F_POLLED_BIT, |
bcda7baa | 729 | REQ_F_BUFFER_SELECTED_BIT, |
e342c807 | 730 | REQ_F_COMPLETE_INLINE_BIT, |
230d50d4 | 731 | REQ_F_REISSUE_BIT, |
b8e64b53 | 732 | REQ_F_CREDS_BIT, |
20e60a38 | 733 | REQ_F_REFCOUNT_BIT, |
4d13d1a4 | 734 | REQ_F_ARM_LTIMEOUT_BIT, |
7b29f92d | 735 | /* keep async read/write and isreg together and in order */ |
b191e2df PB |
736 | REQ_F_NOWAIT_READ_BIT, |
737 | REQ_F_NOWAIT_WRITE_BIT, | |
7b29f92d | 738 | REQ_F_ISREG_BIT, |
84557871 JA |
739 | |
740 | /* not a real bit, just to check we're not overflowing the space */ | |
741 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
742 | }; |
743 | ||
744 | enum { | |
745 | /* ctx owns file */ | |
746 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
747 | /* drain existing IO first */ | |
748 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
749 | /* linked sqes */ | |
750 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
751 | /* doesn't sever on completion < 0 */ | |
752 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
753 | /* IOSQE_ASYNC */ | |
754 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
755 | /* IOSQE_BUFFER_SELECT */ |
756 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 757 | |
6b47ee6e | 758 | /* fail rest of links */ |
93d2bcd2 | 759 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
b05a1bcd | 760 | /* on inflight list, should be cancelled and waited on exit reliably */ |
6b47ee6e PB |
761 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
762 | /* read/write uses file position */ | |
763 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
764 | /* must not punt to workers */ | |
765 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 766 | /* has or had linked timeout */ |
6b47ee6e | 767 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
99bc4c38 PB |
768 | /* needs cleanup */ |
769 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
770 | /* already went through poll handler */ |
771 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
772 | /* buffer already selected */ |
773 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
e342c807 PB |
774 | /* completion is deferred through io_comp_state */ |
775 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
230d50d4 JA |
776 | /* caller should reissue async */ |
777 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), | |
7b29f92d | 778 | /* supports async reads */ |
b191e2df | 779 | REQ_F_NOWAIT_READ = BIT(REQ_F_NOWAIT_READ_BIT), |
7b29f92d | 780 | /* supports async writes */ |
b191e2df | 781 | REQ_F_NOWAIT_WRITE = BIT(REQ_F_NOWAIT_WRITE_BIT), |
7b29f92d JA |
782 | /* regular file */ |
783 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
b8e64b53 PB |
784 | /* has creds assigned */ |
785 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), | |
20e60a38 PB |
786 | /* skip refcounting if not set */ |
787 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), | |
4d13d1a4 PB |
788 | /* there is a linked timeout that has to be armed */ |
789 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT), | |
d7718a9d JA |
790 | }; |
791 | ||
792 | struct async_poll { | |
793 | struct io_poll_iocb poll; | |
807abcb0 | 794 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
795 | }; |
796 | ||
f237c30a | 797 | typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked); |
5b0a6acc | 798 | |
7cbf1722 | 799 | struct io_task_work { |
5b0a6acc PB |
800 | union { |
801 | struct io_wq_work_node node; | |
802 | struct llist_node fallback_node; | |
803 | }; | |
804 | io_req_tw_func_t func; | |
7cbf1722 JA |
805 | }; |
806 | ||
992da01a PB |
807 | enum { |
808 | IORING_RSRC_FILE = 0, | |
809 | IORING_RSRC_BUFFER = 1, | |
810 | }; | |
811 | ||
09bb8394 JA |
812 | /* |
813 | * NOTE! Each of the iocb union members has the file pointer | |
814 | * as the first entry in their struct definition. So you can | |
815 | * access the file pointer through any of the sub-structs, | |
816 | * or directly as just 'ki_filp' in this struct. | |
817 | */ | |
2b188cc1 | 818 | struct io_kiocb { |
221c5eb2 | 819 | union { |
09bb8394 | 820 | struct file *file; |
9adbd45d | 821 | struct io_rw rw; |
221c5eb2 | 822 | struct io_poll_iocb poll; |
9d805892 | 823 | struct io_poll_update poll_update; |
8ed8d3c3 JA |
824 | struct io_accept accept; |
825 | struct io_sync sync; | |
fbf23849 | 826 | struct io_cancel cancel; |
b29472ee | 827 | struct io_timeout timeout; |
0bdf7a2d | 828 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 829 | struct io_connect connect; |
e47293fd | 830 | struct io_sr_msg sr_msg; |
15b71abe | 831 | struct io_open open; |
b5dba59e | 832 | struct io_close close; |
269bbe5f | 833 | struct io_rsrc_update rsrc_update; |
4840e418 | 834 | struct io_fadvise fadvise; |
c1ca757b | 835 | struct io_madvise madvise; |
3e4827b0 | 836 | struct io_epoll epoll; |
7d67af2c | 837 | struct io_splice splice; |
ddf0322d | 838 | struct io_provide_buf pbuf; |
1d9e1288 | 839 | struct io_statx statx; |
36f4fa68 | 840 | struct io_shutdown shutdown; |
80a261fd | 841 | struct io_rename rename; |
14a1143b | 842 | struct io_unlink unlink; |
e34a02dc | 843 | struct io_mkdir mkdir; |
7a8721f8 | 844 | struct io_symlink symlink; |
cf30da90 | 845 | struct io_hardlink hardlink; |
3ca405eb PB |
846 | /* use only after cleaning per-op data, see io_clean_op() */ |
847 | struct io_completion compl; | |
221c5eb2 | 848 | }; |
2b188cc1 | 849 | |
e8c2bc1f JA |
850 | /* opcode allocated if it needs to store data for async defer */ |
851 | void *async_data; | |
d625c6ee | 852 | u8 opcode; |
65a6543d XW |
853 | /* polled IO has completed */ |
854 | u8 iopoll_completed; | |
2b188cc1 | 855 | |
4f4eeba8 | 856 | u16 buf_index; |
9cf7c104 | 857 | u32 result; |
4f4eeba8 | 858 | |
010e8e6b PB |
859 | struct io_ring_ctx *ctx; |
860 | unsigned int flags; | |
abc54d63 | 861 | atomic_t refs; |
010e8e6b PB |
862 | struct task_struct *task; |
863 | u64 user_data; | |
d7718a9d | 864 | |
269bbe5f | 865 | struct percpu_ref *fixed_rsrc_refs; |
fcb323cc | 866 | |
7e3709d5 | 867 | /* used by request caches, completion batching and iopoll */ |
ef05d9eb | 868 | struct io_wq_work_node comp_list; |
7e3709d5 | 869 | struct io_kiocb *link; |
5b0a6acc | 870 | struct io_task_work io_task_work; |
010e8e6b PB |
871 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
872 | struct hlist_node hash_node; | |
7e3709d5 | 873 | /* internal polling, see IORING_FEAT_FAST_POLL */ |
010e8e6b | 874 | struct async_poll *apoll; |
eae071c9 PB |
875 | /* store used ubuf, so we can prevent reloading */ |
876 | struct io_mapped_ubuf *imu; | |
ef05d9eb | 877 | struct io_wq_work work; |
7e3709d5 | 878 | /* custom credentials, valid IFF REQ_F_CREDS is set */ |
ef05d9eb | 879 | const struct cred *creds; |
7e3709d5 | 880 | /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */ |
30d51dd4 | 881 | struct io_buffer *kbuf; |
2b188cc1 | 882 | }; |
05589553 | 883 | |
13bf43f5 PB |
884 | struct io_tctx_node { |
885 | struct list_head ctx_node; | |
886 | struct task_struct *task; | |
13bf43f5 PB |
887 | struct io_ring_ctx *ctx; |
888 | }; | |
889 | ||
27dc8338 PB |
890 | struct io_defer_entry { |
891 | struct list_head list; | |
892 | struct io_kiocb *req; | |
9cf7c104 | 893 | u32 seq; |
2b188cc1 JA |
894 | }; |
895 | ||
d3656344 | 896 | struct io_op_def { |
d3656344 JA |
897 | /* needs req->file assigned */ |
898 | unsigned needs_file : 1; | |
d3656344 JA |
899 | /* hash wq insertion if file is a regular file */ |
900 | unsigned hash_reg_file : 1; | |
901 | /* unbound wq insertion if file is a non-regular file */ | |
902 | unsigned unbound_nonreg_file : 1; | |
66f4af93 JA |
903 | /* opcode is not supported by this kernel */ |
904 | unsigned not_supported : 1; | |
8a72758c JA |
905 | /* set if opcode supports polled "wait" */ |
906 | unsigned pollin : 1; | |
907 | unsigned pollout : 1; | |
bcda7baa JA |
908 | /* op supports buffer selection */ |
909 | unsigned buffer_select : 1; | |
26f0505a PB |
910 | /* do prep async if is going to be punted */ |
911 | unsigned needs_async_setup : 1; | |
27926b68 JA |
912 | /* should block plug */ |
913 | unsigned plug : 1; | |
e8c2bc1f JA |
914 | /* size of async data needed, if any */ |
915 | unsigned short async_size; | |
d3656344 JA |
916 | }; |
917 | ||
0918682b | 918 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
919 | [IORING_OP_NOP] = {}, |
920 | [IORING_OP_READV] = { | |
d3656344 JA |
921 | .needs_file = 1, |
922 | .unbound_nonreg_file = 1, | |
8a72758c | 923 | .pollin = 1, |
4d954c25 | 924 | .buffer_select = 1, |
26f0505a | 925 | .needs_async_setup = 1, |
27926b68 | 926 | .plug = 1, |
e8c2bc1f | 927 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 928 | }, |
0463b6c5 | 929 | [IORING_OP_WRITEV] = { |
d3656344 JA |
930 | .needs_file = 1, |
931 | .hash_reg_file = 1, | |
932 | .unbound_nonreg_file = 1, | |
8a72758c | 933 | .pollout = 1, |
26f0505a | 934 | .needs_async_setup = 1, |
27926b68 | 935 | .plug = 1, |
e8c2bc1f | 936 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 937 | }, |
0463b6c5 | 938 | [IORING_OP_FSYNC] = { |
d3656344 JA |
939 | .needs_file = 1, |
940 | }, | |
0463b6c5 | 941 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
942 | .needs_file = 1, |
943 | .unbound_nonreg_file = 1, | |
8a72758c | 944 | .pollin = 1, |
27926b68 | 945 | .plug = 1, |
e8c2bc1f | 946 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 947 | }, |
0463b6c5 | 948 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
949 | .needs_file = 1, |
950 | .hash_reg_file = 1, | |
951 | .unbound_nonreg_file = 1, | |
8a72758c | 952 | .pollout = 1, |
27926b68 | 953 | .plug = 1, |
e8c2bc1f | 954 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 955 | }, |
0463b6c5 | 956 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
957 | .needs_file = 1, |
958 | .unbound_nonreg_file = 1, | |
959 | }, | |
0463b6c5 PB |
960 | [IORING_OP_POLL_REMOVE] = {}, |
961 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 JA |
962 | .needs_file = 1, |
963 | }, | |
0463b6c5 | 964 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
965 | .needs_file = 1, |
966 | .unbound_nonreg_file = 1, | |
8a72758c | 967 | .pollout = 1, |
26f0505a | 968 | .needs_async_setup = 1, |
e8c2bc1f | 969 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 970 | }, |
0463b6c5 | 971 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
972 | .needs_file = 1, |
973 | .unbound_nonreg_file = 1, | |
8a72758c | 974 | .pollin = 1, |
52de1fe1 | 975 | .buffer_select = 1, |
26f0505a | 976 | .needs_async_setup = 1, |
e8c2bc1f | 977 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 978 | }, |
0463b6c5 | 979 | [IORING_OP_TIMEOUT] = { |
e8c2bc1f | 980 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 981 | }, |
9c8e11b3 PB |
982 | [IORING_OP_TIMEOUT_REMOVE] = { |
983 | /* used by timeout updates' prep() */ | |
9c8e11b3 | 984 | }, |
0463b6c5 | 985 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
986 | .needs_file = 1, |
987 | .unbound_nonreg_file = 1, | |
8a72758c | 988 | .pollin = 1, |
d3656344 | 989 | }, |
0463b6c5 PB |
990 | [IORING_OP_ASYNC_CANCEL] = {}, |
991 | [IORING_OP_LINK_TIMEOUT] = { | |
e8c2bc1f | 992 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 993 | }, |
0463b6c5 | 994 | [IORING_OP_CONNECT] = { |
d3656344 JA |
995 | .needs_file = 1, |
996 | .unbound_nonreg_file = 1, | |
8a72758c | 997 | .pollout = 1, |
26f0505a | 998 | .needs_async_setup = 1, |
e8c2bc1f | 999 | .async_size = sizeof(struct io_async_connect), |
d3656344 | 1000 | }, |
0463b6c5 | 1001 | [IORING_OP_FALLOCATE] = { |
d3656344 | 1002 | .needs_file = 1, |
d3656344 | 1003 | }, |
44526bed JA |
1004 | [IORING_OP_OPENAT] = {}, |
1005 | [IORING_OP_CLOSE] = {}, | |
1006 | [IORING_OP_FILES_UPDATE] = {}, | |
1007 | [IORING_OP_STATX] = {}, | |
0463b6c5 | 1008 | [IORING_OP_READ] = { |
3a6820f2 JA |
1009 | .needs_file = 1, |
1010 | .unbound_nonreg_file = 1, | |
8a72758c | 1011 | .pollin = 1, |
bcda7baa | 1012 | .buffer_select = 1, |
27926b68 | 1013 | .plug = 1, |
e8c2bc1f | 1014 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1015 | }, |
0463b6c5 | 1016 | [IORING_OP_WRITE] = { |
3a6820f2 | 1017 | .needs_file = 1, |
7b3188e7 | 1018 | .hash_reg_file = 1, |
3a6820f2 | 1019 | .unbound_nonreg_file = 1, |
8a72758c | 1020 | .pollout = 1, |
27926b68 | 1021 | .plug = 1, |
e8c2bc1f | 1022 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1023 | }, |
0463b6c5 | 1024 | [IORING_OP_FADVISE] = { |
4840e418 | 1025 | .needs_file = 1, |
c1ca757b | 1026 | }, |
44526bed | 1027 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 1028 | [IORING_OP_SEND] = { |
fddaface JA |
1029 | .needs_file = 1, |
1030 | .unbound_nonreg_file = 1, | |
8a72758c | 1031 | .pollout = 1, |
fddaface | 1032 | }, |
0463b6c5 | 1033 | [IORING_OP_RECV] = { |
fddaface JA |
1034 | .needs_file = 1, |
1035 | .unbound_nonreg_file = 1, | |
8a72758c | 1036 | .pollin = 1, |
bcda7baa | 1037 | .buffer_select = 1, |
fddaface | 1038 | }, |
0463b6c5 | 1039 | [IORING_OP_OPENAT2] = { |
cebdb986 | 1040 | }, |
3e4827b0 JA |
1041 | [IORING_OP_EPOLL_CTL] = { |
1042 | .unbound_nonreg_file = 1, | |
3e4827b0 | 1043 | }, |
7d67af2c PB |
1044 | [IORING_OP_SPLICE] = { |
1045 | .needs_file = 1, | |
1046 | .hash_reg_file = 1, | |
1047 | .unbound_nonreg_file = 1, | |
ddf0322d JA |
1048 | }, |
1049 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 1050 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
1051 | [IORING_OP_TEE] = { |
1052 | .needs_file = 1, | |
1053 | .hash_reg_file = 1, | |
1054 | .unbound_nonreg_file = 1, | |
1055 | }, | |
36f4fa68 JA |
1056 | [IORING_OP_SHUTDOWN] = { |
1057 | .needs_file = 1, | |
1058 | }, | |
44526bed JA |
1059 | [IORING_OP_RENAMEAT] = {}, |
1060 | [IORING_OP_UNLINKAT] = {}, | |
e34a02dc | 1061 | [IORING_OP_MKDIRAT] = {}, |
7a8721f8 | 1062 | [IORING_OP_SYMLINKAT] = {}, |
cf30da90 | 1063 | [IORING_OP_LINKAT] = {}, |
d3656344 JA |
1064 | }; |
1065 | ||
0756a869 PB |
1066 | /* requests with any of those set should undergo io_disarm_next() */ |
1067 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) | |
1068 | ||
7a612350 | 1069 | static bool io_disarm_next(struct io_kiocb *req); |
eef51daa | 1070 | static void io_uring_del_tctx_node(unsigned long index); |
9936c7c2 PB |
1071 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1072 | struct task_struct *task, | |
3dd0c97a | 1073 | bool cancel_all); |
78cc687b | 1074 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); |
1ffc5422 | 1075 | |
d4d19c19 PB |
1076 | static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
1077 | long res, unsigned int cflags); | |
ec9c02ad | 1078 | static void io_put_req(struct io_kiocb *req); |
91c2f697 | 1079 | static void io_put_req_deferred(struct io_kiocb *req); |
c7dae4ba | 1080 | static void io_dismantle_req(struct io_kiocb *req); |
94ae5e77 | 1081 | static void io_queue_linked_timeout(struct io_kiocb *req); |
fdecb662 | 1082 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 1083 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 | 1084 | unsigned nr_args); |
68fb8979 | 1085 | static void io_clean_op(struct io_kiocb *req); |
ac177053 | 1086 | static struct file *io_file_get(struct io_ring_ctx *ctx, |
8371adf5 | 1087 | struct io_kiocb *req, int fd, bool fixed); |
c5eef2b9 | 1088 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1089 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1090 | |
907d1df3 | 1091 | static void io_req_task_queue(struct io_kiocb *req); |
c450178d | 1092 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx); |
179ae0d1 | 1093 | static int io_req_prep_async(struct io_kiocb *req); |
de0617e4 | 1094 | |
b9445598 PB |
1095 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
1096 | unsigned int issue_flags, u32 slot_index); | |
7df778be PB |
1097 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags); |
1098 | ||
f1042b6c | 1099 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer); |
b9445598 | 1100 | |
2b188cc1 JA |
1101 | static struct kmem_cache *req_cachep; |
1102 | ||
0918682b | 1103 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1104 | |
1105 | struct sock *io_uring_get_socket(struct file *file) | |
1106 | { | |
1107 | #if defined(CONFIG_UNIX) | |
1108 | if (file->f_op == &io_uring_fops) { | |
1109 | struct io_ring_ctx *ctx = file->private_data; | |
1110 | ||
1111 | return ctx->ring_sock->sk; | |
1112 | } | |
1113 | #endif | |
1114 | return NULL; | |
1115 | } | |
1116 | EXPORT_SYMBOL(io_uring_get_socket); | |
1117 | ||
f237c30a PB |
1118 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
1119 | { | |
1120 | if (!*locked) { | |
1121 | mutex_lock(&ctx->uring_lock); | |
1122 | *locked = true; | |
1123 | } | |
1124 | } | |
1125 | ||
f2f87370 PB |
1126 | #define io_for_each_link(pos, head) \ |
1127 | for (pos = (head); pos; pos = pos->link) | |
1128 | ||
21c843d5 PB |
1129 | /* |
1130 | * Shamelessly stolen from the mm implementation of page reference checking, | |
1131 | * see commit f958d7b528b1 for details. | |
1132 | */ | |
1133 | #define req_ref_zero_or_close_to_overflow(req) \ | |
1134 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) | |
1135 | ||
1136 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) | |
1137 | { | |
20e60a38 | 1138 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1139 | return atomic_inc_not_zero(&req->refs); |
1140 | } | |
1141 | ||
21c843d5 PB |
1142 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
1143 | { | |
20e60a38 PB |
1144 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
1145 | return true; | |
1146 | ||
21c843d5 PB |
1147 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1148 | return atomic_dec_and_test(&req->refs); | |
1149 | } | |
1150 | ||
1151 | static inline void req_ref_put(struct io_kiocb *req) | |
1152 | { | |
20e60a38 | 1153 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1154 | WARN_ON_ONCE(req_ref_put_and_test(req)); |
1155 | } | |
1156 | ||
1157 | static inline void req_ref_get(struct io_kiocb *req) | |
1158 | { | |
20e60a38 | 1159 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1160 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1161 | atomic_inc(&req->refs); | |
1162 | } | |
1163 | ||
c450178d PB |
1164 | static inline void io_submit_flush_completions(struct io_ring_ctx *ctx) |
1165 | { | |
6f33b0bc | 1166 | if (!wq_list_empty(&ctx->submit_state.compl_reqs)) |
c450178d PB |
1167 | __io_submit_flush_completions(ctx); |
1168 | } | |
1169 | ||
48dcd38d | 1170 | static inline void __io_req_set_refcount(struct io_kiocb *req, int nr) |
20e60a38 PB |
1171 | { |
1172 | if (!(req->flags & REQ_F_REFCOUNT)) { | |
1173 | req->flags |= REQ_F_REFCOUNT; | |
48dcd38d | 1174 | atomic_set(&req->refs, nr); |
20e60a38 PB |
1175 | } |
1176 | } | |
1177 | ||
48dcd38d PB |
1178 | static inline void io_req_set_refcount(struct io_kiocb *req) |
1179 | { | |
1180 | __io_req_set_refcount(req, 1); | |
1181 | } | |
1182 | ||
b895c9a6 | 1183 | static inline void io_req_set_rsrc_node(struct io_kiocb *req) |
36f72fe2 PB |
1184 | { |
1185 | struct io_ring_ctx *ctx = req->ctx; | |
1186 | ||
269bbe5f | 1187 | if (!req->fixed_rsrc_refs) { |
a7f0ed5a | 1188 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
269bbe5f | 1189 | percpu_ref_get(req->fixed_rsrc_refs); |
36f72fe2 PB |
1190 | } |
1191 | } | |
1192 | ||
f70865db PB |
1193 | static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
1194 | { | |
1195 | bool got = percpu_ref_tryget(ref); | |
1196 | ||
1197 | /* already at zero, wait for ->release() */ | |
1198 | if (!got) | |
1199 | wait_for_completion(compl); | |
1200 | percpu_ref_resurrect(ref); | |
1201 | if (got) | |
1202 | percpu_ref_put(ref); | |
1203 | } | |
1204 | ||
3dd0c97a PB |
1205 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
1206 | bool cancel_all) | |
08d23634 PB |
1207 | { |
1208 | struct io_kiocb *req; | |
1209 | ||
68207680 | 1210 | if (task && head->task != task) |
08d23634 | 1211 | return false; |
3dd0c97a | 1212 | if (cancel_all) |
08d23634 PB |
1213 | return true; |
1214 | ||
1215 | io_for_each_link(req, head) { | |
b05a1bcd | 1216 | if (req->flags & REQ_F_INFLIGHT) |
02a13674 | 1217 | return true; |
08d23634 PB |
1218 | } |
1219 | return false; | |
1220 | } | |
1221 | ||
93d2bcd2 | 1222 | static inline void req_set_fail(struct io_kiocb *req) |
c40f6379 | 1223 | { |
93d2bcd2 | 1224 | req->flags |= REQ_F_FAIL; |
c40f6379 | 1225 | } |
4a38aed2 | 1226 | |
a8295b98 HX |
1227 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
1228 | { | |
1229 | req_set_fail(req); | |
1230 | req->result = res; | |
1231 | } | |
1232 | ||
c072481d | 1233 | static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref) |
2b188cc1 JA |
1234 | { |
1235 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1236 | ||
0f158b4c | 1237 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1238 | } |
1239 | ||
8eb7e2d0 PB |
1240 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1241 | { | |
1242 | return !req->timeout.off; | |
1243 | } | |
1244 | ||
c072481d | 1245 | static __cold void io_fallback_req_func(struct work_struct *work) |
f56165e6 PB |
1246 | { |
1247 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, | |
1248 | fallback_work.work); | |
1249 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); | |
1250 | struct io_kiocb *req, *tmp; | |
f237c30a | 1251 | bool locked = false; |
f56165e6 PB |
1252 | |
1253 | percpu_ref_get(&ctx->refs); | |
1254 | llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node) | |
f237c30a | 1255 | req->io_task_work.func(req, &locked); |
5636c00d | 1256 | |
f237c30a | 1257 | if (locked) { |
c450178d | 1258 | io_submit_flush_completions(ctx); |
f237c30a PB |
1259 | mutex_unlock(&ctx->uring_lock); |
1260 | } | |
f56165e6 | 1261 | percpu_ref_put(&ctx->refs); |
f237c30a | 1262 | |
f56165e6 PB |
1263 | } |
1264 | ||
c072481d | 1265 | static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
2b188cc1 JA |
1266 | { |
1267 | struct io_ring_ctx *ctx; | |
78076bb6 | 1268 | int hash_bits; |
2b188cc1 JA |
1269 | |
1270 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1271 | if (!ctx) | |
1272 | return NULL; | |
1273 | ||
78076bb6 JA |
1274 | /* |
1275 | * Use 5 bits less than the max cq entries, that should give us around | |
1276 | * 32 entries per hash list if totally full and uniformly spread. | |
1277 | */ | |
1278 | hash_bits = ilog2(p->cq_entries); | |
1279 | hash_bits -= 5; | |
1280 | if (hash_bits <= 0) | |
1281 | hash_bits = 1; | |
1282 | ctx->cancel_hash_bits = hash_bits; | |
1283 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1284 | GFP_KERNEL); | |
1285 | if (!ctx->cancel_hash) | |
1286 | goto err; | |
1287 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1288 | ||
6224843d PB |
1289 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
1290 | if (!ctx->dummy_ubuf) | |
1291 | goto err; | |
1292 | /* set invalid range, so io_import_fixed() fails meeting it */ | |
1293 | ctx->dummy_ubuf->ubuf = -1UL; | |
1294 | ||
21482896 | 1295 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1296 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1297 | goto err; | |
2b188cc1 JA |
1298 | |
1299 | ctx->flags = p->flags; | |
90554200 | 1300 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1301 | INIT_LIST_HEAD(&ctx->sqd_list); |
1d7bb1d5 | 1302 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c | 1303 | init_completion(&ctx->ref_comp); |
9e15c3a0 | 1304 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
61cf9370 | 1305 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
2b188cc1 | 1306 | mutex_init(&ctx->uring_lock); |
311997b3 | 1307 | init_waitqueue_head(&ctx->cq_wait); |
2b188cc1 | 1308 | spin_lock_init(&ctx->completion_lock); |
89850fce | 1309 | spin_lock_init(&ctx->timeout_lock); |
5eef4e87 | 1310 | INIT_WQ_LIST(&ctx->iopoll_list); |
de0617e4 | 1311 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1312 | INIT_LIST_HEAD(&ctx->timeout_list); |
ef9dd637 | 1313 | INIT_LIST_HEAD(&ctx->ltimeout_list); |
d67d2263 BM |
1314 | spin_lock_init(&ctx->rsrc_ref_lock); |
1315 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1316 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1317 | init_llist_head(&ctx->rsrc_put_llist); | |
13bf43f5 | 1318 | INIT_LIST_HEAD(&ctx->tctx_list); |
c2b6c6bc PB |
1319 | ctx->submit_state.free_list.next = NULL; |
1320 | INIT_WQ_LIST(&ctx->locked_free_list); | |
9011bf9a | 1321 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
6f33b0bc | 1322 | INIT_WQ_LIST(&ctx->submit_state.compl_reqs); |
2b188cc1 | 1323 | return ctx; |
206aefde | 1324 | err: |
6224843d | 1325 | kfree(ctx->dummy_ubuf); |
78076bb6 | 1326 | kfree(ctx->cancel_hash); |
206aefde JA |
1327 | kfree(ctx); |
1328 | return NULL; | |
2b188cc1 JA |
1329 | } |
1330 | ||
8f6ed49a PB |
1331 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
1332 | { | |
1333 | struct io_rings *r = ctx->rings; | |
1334 | ||
1335 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); | |
1336 | ctx->cq_extra--; | |
1337 | } | |
1338 | ||
9cf7c104 | 1339 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1340 | { |
2bc9930e JA |
1341 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1342 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1343 | |
8f6ed49a | 1344 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
2bc9930e | 1345 | } |
de0617e4 | 1346 | |
9d858b21 | 1347 | return false; |
de0617e4 JA |
1348 | } |
1349 | ||
c97d8a0f PB |
1350 | #define FFS_ASYNC_READ 0x1UL |
1351 | #define FFS_ASYNC_WRITE 0x2UL | |
1352 | #ifdef CONFIG_64BIT | |
1353 | #define FFS_ISREG 0x4UL | |
1354 | #else | |
1355 | #define FFS_ISREG 0x0UL | |
1356 | #endif | |
1357 | #define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG) | |
1358 | ||
1359 | static inline bool io_req_ffs_set(struct io_kiocb *req) | |
1360 | { | |
1361 | return IS_ENABLED(CONFIG_64BIT) && (req->flags & REQ_F_FIXED_FILE); | |
1362 | } | |
1363 | ||
c072481d | 1364 | static inline void io_req_track_inflight(struct io_kiocb *req) |
ce3d5aae | 1365 | { |
ce3d5aae | 1366 | if (!(req->flags & REQ_F_INFLIGHT)) { |
ce3d5aae | 1367 | req->flags |= REQ_F_INFLIGHT; |
b303fe2e | 1368 | atomic_inc(¤t->io_uring->inflight_tracked); |
ce3d5aae PB |
1369 | } |
1370 | } | |
1371 | ||
906c6caa PB |
1372 | static inline void io_unprep_linked_timeout(struct io_kiocb *req) |
1373 | { | |
1374 | req->flags &= ~REQ_F_LINK_TIMEOUT; | |
1375 | } | |
1376 | ||
fd08e530 PB |
1377 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
1378 | { | |
906c6caa PB |
1379 | if (WARN_ON_ONCE(!req->link)) |
1380 | return NULL; | |
1381 | ||
4d13d1a4 PB |
1382 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
1383 | req->flags |= REQ_F_LINK_TIMEOUT; | |
fd08e530 PB |
1384 | |
1385 | /* linked timeouts should have two refs once prep'ed */ | |
48dcd38d | 1386 | io_req_set_refcount(req); |
4d13d1a4 PB |
1387 | __io_req_set_refcount(req->link, 2); |
1388 | return req->link; | |
fd08e530 PB |
1389 | } |
1390 | ||
1391 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) | |
1392 | { | |
4d13d1a4 | 1393 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
fd08e530 PB |
1394 | return NULL; |
1395 | return __io_prep_linked_timeout(req); | |
1396 | } | |
1397 | ||
1e6fa521 JA |
1398 | static void io_prep_async_work(struct io_kiocb *req) |
1399 | { | |
1400 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1401 | struct io_ring_ctx *ctx = req->ctx; |
1402 | ||
b8e64b53 PB |
1403 | if (!(req->flags & REQ_F_CREDS)) { |
1404 | req->flags |= REQ_F_CREDS; | |
c10d1f98 | 1405 | req->creds = get_current_cred(); |
b8e64b53 | 1406 | } |
003e8dcc | 1407 | |
e1d675df PB |
1408 | req->work.list.next = NULL; |
1409 | req->work.flags = 0; | |
feaadc4f PB |
1410 | if (req->flags & REQ_F_FORCE_ASYNC) |
1411 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1412 | ||
1e6fa521 JA |
1413 | if (req->flags & REQ_F_ISREG) { |
1414 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1415 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
4b982bd0 | 1416 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
1e6fa521 JA |
1417 | if (def->unbound_nonreg_file) |
1418 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1419 | } | |
e1d675df PB |
1420 | |
1421 | switch (req->opcode) { | |
1422 | case IORING_OP_SPLICE: | |
1423 | case IORING_OP_TEE: | |
e1d675df PB |
1424 | if (!S_ISREG(file_inode(req->splice.file_in)->i_mode)) |
1425 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1426 | break; | |
1427 | } | |
561fb04a | 1428 | } |
cccf0ee8 | 1429 | |
cbdcb435 | 1430 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1431 | { |
cbdcb435 | 1432 | struct io_kiocb *cur; |
54a91f3b | 1433 | |
44eff40a PB |
1434 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
1435 | struct io_ring_ctx *ctx = req->ctx; | |
1436 | ||
79ebeaee | 1437 | spin_lock(&ctx->completion_lock); |
44eff40a PB |
1438 | io_for_each_link(cur, req) |
1439 | io_prep_async_work(cur); | |
79ebeaee | 1440 | spin_unlock(&ctx->completion_lock); |
44eff40a PB |
1441 | } else { |
1442 | io_for_each_link(cur, req) | |
1443 | io_prep_async_work(cur); | |
1444 | } | |
561fb04a JA |
1445 | } |
1446 | ||
fff4e40e PB |
1447 | static inline void io_req_add_compl_list(struct io_kiocb *req) |
1448 | { | |
1449 | struct io_submit_state *state = &req->ctx->submit_state; | |
1450 | ||
1451 | wq_list_add_tail(&req->comp_list, &state->compl_reqs); | |
1452 | } | |
1453 | ||
f237c30a | 1454 | static void io_queue_async_work(struct io_kiocb *req, bool *locked) |
561fb04a | 1455 | { |
a197f664 | 1456 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1457 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1458 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1459 | |
f237c30a PB |
1460 | /* must not take the lock, NULL it as a precaution */ |
1461 | locked = NULL; | |
1462 | ||
3bfe6106 JA |
1463 | BUG_ON(!tctx); |
1464 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1465 | |
cbdcb435 PB |
1466 | /* init ->work of the whole link before punting */ |
1467 | io_prep_async_link(req); | |
991468dc JA |
1468 | |
1469 | /* | |
1470 | * Not expected to happen, but if we do have a bug where this _can_ | |
1471 | * happen, catch it here and ensure the request is marked as | |
1472 | * canceled. That will make io-wq go through the usual work cancel | |
1473 | * procedure rather than attempt to run this request (or create a new | |
1474 | * worker for it). | |
1475 | */ | |
1476 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) | |
1477 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1478 | ||
d07f1e8a PB |
1479 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1480 | &req->work, req->flags); | |
ebf93667 | 1481 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a JA |
1482 | if (link) |
1483 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1484 | } |
1485 | ||
1ee4160c | 1486 | static void io_kill_timeout(struct io_kiocb *req, int status) |
8c855885 | 1487 | __must_hold(&req->ctx->completion_lock) |
89850fce | 1488 | __must_hold(&req->ctx->timeout_lock) |
5262f567 | 1489 | { |
e8c2bc1f | 1490 | struct io_timeout_data *io = req->async_data; |
5262f567 | 1491 | |
fd9c7bc5 | 1492 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
2ae2eb9d PB |
1493 | if (status) |
1494 | req_set_fail(req); | |
01cec8c1 PB |
1495 | atomic_set(&req->ctx->cq_timeouts, |
1496 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1497 | list_del_init(&req->timeout.list); |
d4d19c19 | 1498 | io_cqring_fill_event(req->ctx, req->user_data, status, 0); |
91c2f697 | 1499 | io_put_req_deferred(req); |
5262f567 JA |
1500 | } |
1501 | } | |
1502 | ||
c072481d | 1503 | static __cold void io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1504 | { |
441b8a78 | 1505 | while (!list_empty(&ctx->defer_list)) { |
27dc8338 PB |
1506 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1507 | struct io_defer_entry, list); | |
de0617e4 | 1508 | |
9cf7c104 | 1509 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1510 | break; |
27dc8338 | 1511 | list_del_init(&de->list); |
907d1df3 | 1512 | io_req_task_queue(de->req); |
27dc8338 | 1513 | kfree(de); |
441b8a78 | 1514 | } |
04518945 PB |
1515 | } |
1516 | ||
c072481d | 1517 | static __cold void io_flush_timeouts(struct io_ring_ctx *ctx) |
89850fce | 1518 | __must_hold(&ctx->completion_lock) |
de0617e4 | 1519 | { |
441b8a78 | 1520 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
f010505b | 1521 | |
79ebeaee | 1522 | spin_lock_irq(&ctx->timeout_lock); |
f18ee4cf | 1523 | while (!list_empty(&ctx->timeout_list)) { |
f010505b | 1524 | u32 events_needed, events_got; |
360428f8 | 1525 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1526 | struct io_kiocb, timeout.list); |
de0617e4 | 1527 | |
8eb7e2d0 | 1528 | if (io_is_timeout_noseq(req)) |
360428f8 | 1529 | break; |
f010505b MDG |
1530 | |
1531 | /* | |
1532 | * Since seq can easily wrap around over time, subtract | |
1533 | * the last seq at which timeouts were flushed before comparing. | |
1534 | * Assuming not more than 2^31-1 events have happened since, | |
1535 | * these subtractions won't have wrapped, so we can check if | |
1536 | * target is in [last_seq, current_seq] by comparing the two. | |
1537 | */ | |
1538 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1539 | events_got = seq - ctx->cq_last_tm_flush; | |
1540 | if (events_got < events_needed) | |
360428f8 | 1541 | break; |
bfe68a22 | 1542 | |
135fcde8 | 1543 | list_del_init(&req->timeout.list); |
1ee4160c | 1544 | io_kill_timeout(req, 0); |
f18ee4cf | 1545 | } |
f010505b | 1546 | ctx->cq_last_tm_flush = seq; |
79ebeaee | 1547 | spin_unlock_irq(&ctx->timeout_lock); |
360428f8 | 1548 | } |
5262f567 | 1549 | |
c072481d | 1550 | static __cold void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
360428f8 | 1551 | { |
2335f6f5 PB |
1552 | if (ctx->off_timeout_used) |
1553 | io_flush_timeouts(ctx); | |
1554 | if (ctx->drain_active) | |
1555 | io_queue_deferred(ctx); | |
1556 | } | |
1557 | ||
1558 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) | |
1559 | { | |
1560 | if (unlikely(ctx->off_timeout_used || ctx->drain_active)) | |
1561 | __io_commit_cqring_flush(ctx); | |
ec30e04b PB |
1562 | /* order cqe stores with ring update */ |
1563 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 JA |
1564 | } |
1565 | ||
90554200 JA |
1566 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1567 | { | |
1568 | struct io_rings *r = ctx->rings; | |
1569 | ||
a566c556 | 1570 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries; |
90554200 JA |
1571 | } |
1572 | ||
888aae2e PB |
1573 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1574 | { | |
1575 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1576 | } | |
1577 | ||
d068b506 | 1578 | static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) |
2b188cc1 | 1579 | { |
75b28aff | 1580 | struct io_rings *rings = ctx->rings; |
ea5ab3b5 | 1581 | unsigned tail, mask = ctx->cq_entries - 1; |
2b188cc1 | 1582 | |
115e12e5 SB |
1583 | /* |
1584 | * writes to the cq entry need to come after reading head; the | |
1585 | * control dependency is enough as we're using WRITE_ONCE to | |
1586 | * fill the cq entry | |
1587 | */ | |
a566c556 | 1588 | if (__io_cqring_events(ctx) == ctx->cq_entries) |
2b188cc1 JA |
1589 | return NULL; |
1590 | ||
888aae2e | 1591 | tail = ctx->cached_cq_tail++; |
ea5ab3b5 | 1592 | return &rings->cqes[tail & mask]; |
2b188cc1 JA |
1593 | } |
1594 | ||
f2842ab5 JA |
1595 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1596 | { | |
44c769de | 1597 | if (likely(!ctx->cq_ev_fd)) |
f0b493e6 | 1598 | return false; |
7e55a19c SG |
1599 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1600 | return false; | |
44c769de | 1601 | return !ctx->eventfd_async || io_wq_current_is_worker(); |
f2842ab5 JA |
1602 | } |
1603 | ||
2c5d763c JA |
1604 | /* |
1605 | * This should only get called when at least one event has been posted. | |
1606 | * Some applications rely on the eventfd notification count only changing | |
1607 | * IFF a new CQE has been added to the CQ ring. There's no depedency on | |
1608 | * 1:1 relationship between how many times this function is called (and | |
1609 | * hence the eventfd count) and number of CQEs posted to the CQ ring. | |
1610 | */ | |
b41e9852 | 1611 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1612 | { |
5fd46178 JA |
1613 | /* |
1614 | * wake_up_all() may seem excessive, but io_wake_function() and | |
1615 | * io_should_wake() handle the termination of the loop and only | |
1616 | * wake as many waiters as we need to. | |
1617 | */ | |
1618 | if (wq_has_sleeper(&ctx->cq_wait)) | |
1619 | wake_up_all(&ctx->cq_wait); | |
b41e9852 | 1620 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 JA |
1621 | eventfd_signal(ctx->cq_ev_fd, 1); |
1622 | } | |
1623 | ||
80c18e4a PB |
1624 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1625 | { | |
c57a91fb PB |
1626 | /* see waitqueue_active() comment */ |
1627 | smp_mb(); | |
1628 | ||
80c18e4a | 1629 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
c57a91fb | 1630 | if (waitqueue_active(&ctx->cq_wait)) |
5fd46178 | 1631 | wake_up_all(&ctx->cq_wait); |
80c18e4a PB |
1632 | } |
1633 | if (io_should_trigger_evfd(ctx)) | |
1634 | eventfd_signal(ctx->cq_ev_fd, 1); | |
1635 | } | |
1636 | ||
c4a2ed72 | 1637 | /* Returns true if there are no backlogged entries after the flush */ |
6c2450ae | 1638 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
1d7bb1d5 | 1639 | { |
b18032bb | 1640 | bool all_flushed, posted; |
1d7bb1d5 | 1641 | |
a566c556 | 1642 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
e23de15f | 1643 | return false; |
1d7bb1d5 | 1644 | |
b18032bb | 1645 | posted = false; |
79ebeaee | 1646 | spin_lock(&ctx->completion_lock); |
6c2450ae | 1647 | while (!list_empty(&ctx->cq_overflow_list)) { |
d068b506 | 1648 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
6c2450ae | 1649 | struct io_overflow_cqe *ocqe; |
e6c8aa9a | 1650 | |
1d7bb1d5 JA |
1651 | if (!cqe && !force) |
1652 | break; | |
6c2450ae PB |
1653 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
1654 | struct io_overflow_cqe, list); | |
1655 | if (cqe) | |
1656 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); | |
1657 | else | |
8f6ed49a PB |
1658 | io_account_cq_overflow(ctx); |
1659 | ||
b18032bb | 1660 | posted = true; |
6c2450ae PB |
1661 | list_del(&ocqe->list); |
1662 | kfree(ocqe); | |
1d7bb1d5 JA |
1663 | } |
1664 | ||
09e88404 PB |
1665 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1666 | if (all_flushed) { | |
5ed7a37d | 1667 | clear_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1668 | WRITE_ONCE(ctx->rings->sq_flags, |
1669 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); | |
09e88404 | 1670 | } |
46930143 | 1671 | |
b18032bb JA |
1672 | if (posted) |
1673 | io_commit_cqring(ctx); | |
79ebeaee | 1674 | spin_unlock(&ctx->completion_lock); |
b18032bb JA |
1675 | if (posted) |
1676 | io_cqring_ev_posted(ctx); | |
09e88404 | 1677 | return all_flushed; |
1d7bb1d5 JA |
1678 | } |
1679 | ||
90f67366 | 1680 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
6c503150 | 1681 | { |
ca0a2651 JA |
1682 | bool ret = true; |
1683 | ||
5ed7a37d | 1684 | if (test_bit(0, &ctx->check_cq_overflow)) { |
6c503150 PB |
1685 | /* iopoll syncs against uring_lock, not completion_lock */ |
1686 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1687 | mutex_lock(&ctx->uring_lock); | |
90f67366 | 1688 | ret = __io_cqring_overflow_flush(ctx, false); |
6c503150 PB |
1689 | if (ctx->flags & IORING_SETUP_IOPOLL) |
1690 | mutex_unlock(&ctx->uring_lock); | |
1691 | } | |
ca0a2651 JA |
1692 | |
1693 | return ret; | |
6c503150 PB |
1694 | } |
1695 | ||
6a290a14 PB |
1696 | /* must to be called somewhat shortly after putting a request */ |
1697 | static inline void io_put_task(struct task_struct *task, int nr) | |
1698 | { | |
1699 | struct io_uring_task *tctx = task->io_uring; | |
1700 | ||
e98e49b2 PB |
1701 | if (likely(task == current)) { |
1702 | tctx->cached_refs += nr; | |
1703 | } else { | |
1704 | percpu_counter_sub(&tctx->inflight, nr); | |
1705 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1706 | wake_up(&tctx->wait); | |
1707 | put_task_struct_many(task, nr); | |
1708 | } | |
6a290a14 PB |
1709 | } |
1710 | ||
9a10867a PB |
1711 | static void io_task_refs_refill(struct io_uring_task *tctx) |
1712 | { | |
1713 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; | |
1714 | ||
1715 | percpu_counter_add(&tctx->inflight, refill); | |
1716 | refcount_add(refill, ¤t->usage); | |
1717 | tctx->cached_refs += refill; | |
1718 | } | |
1719 | ||
1720 | static inline void io_get_task_refs(int nr) | |
1721 | { | |
1722 | struct io_uring_task *tctx = current->io_uring; | |
1723 | ||
1724 | tctx->cached_refs -= nr; | |
1725 | if (unlikely(tctx->cached_refs < 0)) | |
1726 | io_task_refs_refill(tctx); | |
1727 | } | |
1728 | ||
d4d19c19 PB |
1729 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
1730 | long res, unsigned int cflags) | |
2b188cc1 | 1731 | { |
cce4b8b0 | 1732 | struct io_overflow_cqe *ocqe; |
2b188cc1 | 1733 | |
cce4b8b0 PB |
1734 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
1735 | if (!ocqe) { | |
1736 | /* | |
1737 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1738 | * or cannot allocate an overflow entry, then we need to drop it | |
1739 | * on the floor. | |
1740 | */ | |
8f6ed49a | 1741 | io_account_cq_overflow(ctx); |
cce4b8b0 | 1742 | return false; |
2b188cc1 | 1743 | } |
cce4b8b0 | 1744 | if (list_empty(&ctx->cq_overflow_list)) { |
5ed7a37d | 1745 | set_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1746 | WRITE_ONCE(ctx->rings->sq_flags, |
1747 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); | |
1748 | ||
cce4b8b0 | 1749 | } |
d4d19c19 | 1750 | ocqe->cqe.user_data = user_data; |
cce4b8b0 PB |
1751 | ocqe->cqe.res = res; |
1752 | ocqe->cqe.flags = cflags; | |
1753 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); | |
1754 | return true; | |
2b188cc1 JA |
1755 | } |
1756 | ||
d4d19c19 PB |
1757 | static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
1758 | long res, unsigned int cflags) | |
2b188cc1 JA |
1759 | { |
1760 | struct io_uring_cqe *cqe; | |
1761 | ||
d4d19c19 | 1762 | trace_io_uring_complete(ctx, user_data, res, cflags); |
51c3ff62 | 1763 | |
2b188cc1 JA |
1764 | /* |
1765 | * If we can't get a cq entry, userspace overflowed the | |
1766 | * submission (by quite a lot). Increment the overflow count in | |
1767 | * the ring. | |
1768 | */ | |
d068b506 | 1769 | cqe = io_get_cqe(ctx); |
1d7bb1d5 | 1770 | if (likely(cqe)) { |
d4d19c19 | 1771 | WRITE_ONCE(cqe->user_data, user_data); |
2b188cc1 | 1772 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1773 | WRITE_ONCE(cqe->flags, cflags); |
8d13326e | 1774 | return true; |
2b188cc1 | 1775 | } |
d4d19c19 | 1776 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
2b188cc1 JA |
1777 | } |
1778 | ||
8d13326e | 1779 | /* not as hot to bloat with inlining */ |
d4d19c19 PB |
1780 | static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
1781 | long res, unsigned int cflags) | |
bcda7baa | 1782 | { |
d4d19c19 | 1783 | return __io_cqring_fill_event(ctx, user_data, res, cflags); |
bcda7baa JA |
1784 | } |
1785 | ||
7a612350 PB |
1786 | static void io_req_complete_post(struct io_kiocb *req, long res, |
1787 | unsigned int cflags) | |
2b188cc1 | 1788 | { |
78e19bbe | 1789 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 | 1790 | |
79ebeaee | 1791 | spin_lock(&ctx->completion_lock); |
d4d19c19 | 1792 | __io_cqring_fill_event(ctx, req->user_data, res, cflags); |
c7dae4ba JA |
1793 | /* |
1794 | * If we're the last reference to this request, add to our locked | |
1795 | * free_list cache. | |
1796 | */ | |
de9b4cca | 1797 | if (req_ref_put_and_test(req)) { |
7a612350 | 1798 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
0756a869 | 1799 | if (req->flags & IO_DISARM_MASK) |
7a612350 PB |
1800 | io_disarm_next(req); |
1801 | if (req->link) { | |
1802 | io_req_task_queue(req->link); | |
1803 | req->link = NULL; | |
1804 | } | |
1805 | } | |
c7dae4ba JA |
1806 | io_dismantle_req(req); |
1807 | io_put_task(req->task, 1); | |
c2b6c6bc | 1808 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
d0acdee2 | 1809 | ctx->locked_free_nr++; |
180f829f | 1810 | } |
7a612350 | 1811 | io_commit_cqring(ctx); |
79ebeaee | 1812 | spin_unlock(&ctx->completion_lock); |
a3f34907 | 1813 | io_cqring_ev_posted(ctx); |
229a7b63 JA |
1814 | } |
1815 | ||
4e3d9ff9 JA |
1816 | static inline bool io_req_needs_clean(struct io_kiocb *req) |
1817 | { | |
c854357b | 1818 | return req->flags & IO_REQ_CLEAN_FLAGS; |
4e3d9ff9 JA |
1819 | } |
1820 | ||
fff4e40e PB |
1821 | static inline void io_req_complete_state(struct io_kiocb *req, long res, |
1822 | unsigned int cflags) | |
229a7b63 | 1823 | { |
a1cdbb4c | 1824 | /* clean per-opcode space, because req->compl is aliased with it */ |
4e3d9ff9 | 1825 | if (io_req_needs_clean(req)) |
68fb8979 | 1826 | io_clean_op(req); |
a38d68db PB |
1827 | req->result = res; |
1828 | req->compl.cflags = cflags; | |
e342c807 | 1829 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
1830 | } |
1831 | ||
889fca73 PB |
1832 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
1833 | long res, unsigned cflags) | |
bcda7baa | 1834 | { |
889fca73 PB |
1835 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
1836 | io_req_complete_state(req, res, cflags); | |
a38d68db | 1837 | else |
c7dae4ba | 1838 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
1839 | } |
1840 | ||
a38d68db | 1841 | static inline void io_req_complete(struct io_kiocb *req, long res) |
0ddf92e8 | 1842 | { |
889fca73 | 1843 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
1844 | } |
1845 | ||
f41db273 PB |
1846 | static void io_req_complete_failed(struct io_kiocb *req, long res) |
1847 | { | |
93d2bcd2 | 1848 | req_set_fail(req); |
f41db273 PB |
1849 | io_req_complete_post(req, res, 0); |
1850 | } | |
1851 | ||
c6d3d9cb PB |
1852 | static void io_req_complete_fail_submit(struct io_kiocb *req) |
1853 | { | |
1854 | /* | |
1855 | * We don't submit, fail them all, for that replace hardlinks with | |
1856 | * normal links. Extra REQ_F_LINK is tolerated. | |
1857 | */ | |
1858 | req->flags &= ~REQ_F_HARDLINK; | |
1859 | req->flags |= REQ_F_LINK; | |
1860 | io_req_complete_failed(req, req->result); | |
1861 | } | |
1862 | ||
864ea921 PB |
1863 | /* |
1864 | * Don't initialise the fields below on every allocation, but do that in | |
1865 | * advance and keep them valid across allocations. | |
1866 | */ | |
1867 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
1868 | { | |
1869 | req->ctx = ctx; | |
1870 | req->link = NULL; | |
1871 | req->async_data = NULL; | |
1872 | /* not necessary, but safer to zero */ | |
1873 | req->result = 0; | |
1874 | } | |
1875 | ||
dac7a098 | 1876 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
cd0ca2e0 | 1877 | struct io_submit_state *state) |
dac7a098 | 1878 | { |
79ebeaee | 1879 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 1880 | wq_list_splice(&ctx->locked_free_list, &state->free_list); |
d0acdee2 | 1881 | ctx->locked_free_nr = 0; |
79ebeaee | 1882 | spin_unlock(&ctx->completion_lock); |
dac7a098 PB |
1883 | } |
1884 | ||
dd78f492 | 1885 | /* Returns true IFF there are requests in the cache */ |
c7dae4ba | 1886 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 1887 | { |
c7dae4ba | 1888 | struct io_submit_state *state = &ctx->submit_state; |
0ddf92e8 | 1889 | |
c7dae4ba JA |
1890 | /* |
1891 | * If we have more than a batch's worth of requests in our IRQ side | |
1892 | * locked cache, grab the lock and move them over to our submission | |
1893 | * side cache. | |
1894 | */ | |
d0acdee2 | 1895 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
cd0ca2e0 | 1896 | io_flush_cached_locked_reqs(ctx, state); |
c2b6c6bc | 1897 | return !!state->free_list.next; |
0ddf92e8 JA |
1898 | } |
1899 | ||
5d5901a3 PB |
1900 | /* |
1901 | * A request might get retired back into the request caches even before opcode | |
1902 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. | |
1903 | * Because of that, io_alloc_req() should be called only under ->uring_lock | |
1904 | * and with extra caution to not get a request that is still worked on. | |
1905 | */ | |
c072481d | 1906 | static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx) |
5d5901a3 | 1907 | __must_hold(&ctx->uring_lock) |
2b188cc1 | 1908 | { |
e5d1bc0a | 1909 | struct io_submit_state *state = &ctx->submit_state; |
864ea921 | 1910 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
3ab665b7 PB |
1911 | void *reqs[IO_REQ_ALLOC_BATCH]; |
1912 | struct io_kiocb *req; | |
864ea921 | 1913 | int ret, i; |
e5d1bc0a | 1914 | |
c2b6c6bc | 1915 | if (likely(state->free_list.next || io_flush_cached_reqs(ctx))) |
a33ae9ce | 1916 | return true; |
e5d1bc0a | 1917 | |
3ab665b7 | 1918 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs); |
fd6fab2c | 1919 | |
864ea921 PB |
1920 | /* |
1921 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1922 | * retry single alloc to be on the safe side. | |
1923 | */ | |
1924 | if (unlikely(ret <= 0)) { | |
3ab665b7 PB |
1925 | reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
1926 | if (!reqs[0]) | |
a33ae9ce | 1927 | return false; |
864ea921 | 1928 | ret = 1; |
2b188cc1 | 1929 | } |
864ea921 | 1930 | |
37f0e767 | 1931 | percpu_ref_get_many(&ctx->refs, ret); |
3ab665b7 PB |
1932 | for (i = 0; i < ret; i++) { |
1933 | req = reqs[i]; | |
1934 | ||
1935 | io_preinit_req(req, ctx); | |
c2b6c6bc | 1936 | wq_stack_add_head(&req->comp_list, &state->free_list); |
3ab665b7 | 1937 | } |
a33ae9ce PB |
1938 | return true; |
1939 | } | |
1940 | ||
1941 | static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx) | |
1942 | { | |
1943 | if (unlikely(!ctx->submit_state.free_list.next)) | |
1944 | return __io_alloc_req_refill(ctx); | |
1945 | return true; | |
1946 | } | |
1947 | ||
1948 | static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) | |
1949 | { | |
1950 | struct io_wq_work_node *node; | |
1951 | ||
1952 | node = wq_stack_extract(&ctx->submit_state.free_list); | |
c2b6c6bc | 1953 | return container_of(node, struct io_kiocb, comp_list); |
2b188cc1 JA |
1954 | } |
1955 | ||
e1d767f0 | 1956 | static inline void io_put_file(struct file *file) |
8da11c19 | 1957 | { |
e1d767f0 | 1958 | if (file) |
8da11c19 PB |
1959 | fput(file); |
1960 | } | |
1961 | ||
6b639522 | 1962 | static inline void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 1963 | { |
094bae49 | 1964 | unsigned int flags = req->flags; |
929a3af9 | 1965 | |
51d48dab | 1966 | if (unlikely(io_req_needs_clean(req))) |
3a0a6902 | 1967 | io_clean_op(req); |
e1d767f0 PB |
1968 | if (!(flags & REQ_F_FIXED_FILE)) |
1969 | io_put_file(req->file); | |
269bbe5f BM |
1970 | if (req->fixed_rsrc_refs) |
1971 | percpu_ref_put(req->fixed_rsrc_refs); | |
99ebe4ef | 1972 | if (req->async_data) { |
094bae49 | 1973 | kfree(req->async_data); |
99ebe4ef PB |
1974 | req->async_data = NULL; |
1975 | } | |
e65ef56d JA |
1976 | } |
1977 | ||
c072481d | 1978 | static __cold void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 1979 | { |
51a4cc11 | 1980 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 1981 | |
216578e5 | 1982 | io_dismantle_req(req); |
7c660731 | 1983 | io_put_task(req->task, 1); |
c6ca97b3 | 1984 | |
79ebeaee | 1985 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 1986 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
c34b025f | 1987 | ctx->locked_free_nr++; |
79ebeaee | 1988 | spin_unlock(&ctx->completion_lock); |
e65ef56d JA |
1989 | } |
1990 | ||
f2f87370 PB |
1991 | static inline void io_remove_next_linked(struct io_kiocb *req) |
1992 | { | |
1993 | struct io_kiocb *nxt = req->link; | |
1994 | ||
1995 | req->link = nxt->link; | |
1996 | nxt->link = NULL; | |
1997 | } | |
1998 | ||
33cc89a9 PB |
1999 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
2000 | __must_hold(&req->ctx->completion_lock) | |
89b263f6 | 2001 | __must_hold(&req->ctx->timeout_lock) |
2665abfd | 2002 | { |
33cc89a9 | 2003 | struct io_kiocb *link = req->link; |
f2f87370 | 2004 | |
b97e736a | 2005 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
c9abd7ad | 2006 | struct io_timeout_data *io = link->async_data; |
7c86ffee | 2007 | |
f2f87370 | 2008 | io_remove_next_linked(req); |
90cd7e42 | 2009 | link->timeout.head = NULL; |
fd9c7bc5 | 2010 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
ef9dd637 | 2011 | list_del(&link->timeout.list); |
d4d19c19 PB |
2012 | io_cqring_fill_event(link->ctx, link->user_data, |
2013 | -ECANCELED, 0); | |
91c2f697 | 2014 | io_put_req_deferred(link); |
d4729fbd | 2015 | return true; |
c9abd7ad PB |
2016 | } |
2017 | } | |
d4729fbd | 2018 | return false; |
7c86ffee PB |
2019 | } |
2020 | ||
d148ca4b | 2021 | static void io_fail_links(struct io_kiocb *req) |
33cc89a9 | 2022 | __must_hold(&req->ctx->completion_lock) |
9e645e11 | 2023 | { |
33cc89a9 | 2024 | struct io_kiocb *nxt, *link = req->link; |
9e645e11 | 2025 | |
f2f87370 | 2026 | req->link = NULL; |
f2f87370 | 2027 | while (link) { |
a8295b98 HX |
2028 | long res = -ECANCELED; |
2029 | ||
2030 | if (link->flags & REQ_F_FAIL) | |
2031 | res = link->result; | |
2032 | ||
f2f87370 PB |
2033 | nxt = link->link; |
2034 | link->link = NULL; | |
2665abfd | 2035 | |
f2f87370 | 2036 | trace_io_uring_fail_link(req, link); |
a8295b98 | 2037 | io_cqring_fill_event(link->ctx, link->user_data, res, 0); |
91c2f697 | 2038 | io_put_req_deferred(link); |
f2f87370 | 2039 | link = nxt; |
9e645e11 | 2040 | } |
33cc89a9 | 2041 | } |
9e645e11 | 2042 | |
33cc89a9 PB |
2043 | static bool io_disarm_next(struct io_kiocb *req) |
2044 | __must_hold(&req->ctx->completion_lock) | |
2045 | { | |
2046 | bool posted = false; | |
2047 | ||
0756a869 PB |
2048 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
2049 | struct io_kiocb *link = req->link; | |
2050 | ||
906c6caa | 2051 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
0756a869 PB |
2052 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
2053 | io_remove_next_linked(req); | |
2054 | io_cqring_fill_event(link->ctx, link->user_data, | |
2055 | -ECANCELED, 0); | |
2056 | io_put_req_deferred(link); | |
2057 | posted = true; | |
2058 | } | |
2059 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { | |
89b263f6 JA |
2060 | struct io_ring_ctx *ctx = req->ctx; |
2061 | ||
2062 | spin_lock_irq(&ctx->timeout_lock); | |
33cc89a9 | 2063 | posted = io_kill_linked_timeout(req); |
89b263f6 JA |
2064 | spin_unlock_irq(&ctx->timeout_lock); |
2065 | } | |
93d2bcd2 | 2066 | if (unlikely((req->flags & REQ_F_FAIL) && |
e4335ed3 | 2067 | !(req->flags & REQ_F_HARDLINK))) { |
33cc89a9 PB |
2068 | posted |= (req->link != NULL); |
2069 | io_fail_links(req); | |
2070 | } | |
2071 | return posted; | |
9e645e11 JA |
2072 | } |
2073 | ||
d81499bf PB |
2074 | static void __io_req_find_next_prep(struct io_kiocb *req) |
2075 | { | |
2076 | struct io_ring_ctx *ctx = req->ctx; | |
2077 | bool posted; | |
2078 | ||
2079 | spin_lock(&ctx->completion_lock); | |
2080 | posted = io_disarm_next(req); | |
2081 | if (posted) | |
2082 | io_commit_cqring(req->ctx); | |
2083 | spin_unlock(&ctx->completion_lock); | |
2084 | if (posted) | |
2085 | io_cqring_ev_posted(ctx); | |
2086 | } | |
2087 | ||
2088 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) | |
c69f8dbe | 2089 | { |
33cc89a9 | 2090 | struct io_kiocb *nxt; |
944e58bf | 2091 | |
d81499bf PB |
2092 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
2093 | return NULL; | |
9e645e11 JA |
2094 | /* |
2095 | * If LINK is set, we have dependent requests in this chain. If we | |
2096 | * didn't fail this request, queue the first one up, moving any other | |
2097 | * dependencies to the next request. In case of failure, fail the rest | |
2098 | * of the chain. | |
2099 | */ | |
d81499bf PB |
2100 | if (unlikely(req->flags & IO_DISARM_MASK)) |
2101 | __io_req_find_next_prep(req); | |
33cc89a9 PB |
2102 | nxt = req->link; |
2103 | req->link = NULL; | |
2104 | return nxt; | |
4d7dd462 | 2105 | } |
9e645e11 | 2106 | |
f237c30a | 2107 | static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked) |
2c32395d PB |
2108 | { |
2109 | if (!ctx) | |
2110 | return; | |
f237c30a | 2111 | if (*locked) { |
c450178d | 2112 | io_submit_flush_completions(ctx); |
2c32395d | 2113 | mutex_unlock(&ctx->uring_lock); |
f237c30a | 2114 | *locked = false; |
2c32395d PB |
2115 | } |
2116 | percpu_ref_put(&ctx->refs); | |
2117 | } | |
2118 | ||
7cbf1722 | 2119 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 2120 | { |
f237c30a | 2121 | bool locked = false; |
ebd0df2e | 2122 | struct io_ring_ctx *ctx = NULL; |
3f18407d PB |
2123 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
2124 | task_work); | |
c40f6379 | 2125 | |
16f72070 | 2126 | while (1) { |
3f18407d PB |
2127 | struct io_wq_work_node *node; |
2128 | ||
c450178d | 2129 | if (!tctx->task_list.first && locked) |
8d4ad41e PB |
2130 | io_submit_flush_completions(ctx); |
2131 | ||
3f18407d | 2132 | spin_lock_irq(&tctx->task_lock); |
c6538be9 | 2133 | node = tctx->task_list.first; |
3f18407d | 2134 | INIT_WQ_LIST(&tctx->task_list); |
6294f368 PB |
2135 | if (!node) |
2136 | tctx->task_running = false; | |
3f18407d | 2137 | spin_unlock_irq(&tctx->task_lock); |
6294f368 PB |
2138 | if (!node) |
2139 | break; | |
3f18407d | 2140 | |
6294f368 | 2141 | do { |
3f18407d PB |
2142 | struct io_wq_work_node *next = node->next; |
2143 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2144 | io_task_work.node); | |
2145 | ||
2146 | if (req->ctx != ctx) { | |
f237c30a | 2147 | ctx_flush_and_put(ctx, &locked); |
3f18407d | 2148 | ctx = req->ctx; |
126180b9 PB |
2149 | /* if not contended, grab and improve batching */ |
2150 | locked = mutex_trylock(&ctx->uring_lock); | |
3f18407d PB |
2151 | percpu_ref_get(&ctx->refs); |
2152 | } | |
f237c30a | 2153 | req->io_task_work.func(req, &locked); |
3f18407d | 2154 | node = next; |
6294f368 PB |
2155 | } while (node); |
2156 | ||
7cbf1722 | 2157 | cond_resched(); |
3f18407d | 2158 | } |
ebd0df2e | 2159 | |
f237c30a | 2160 | ctx_flush_and_put(ctx, &locked); |
7cbf1722 JA |
2161 | } |
2162 | ||
e09ee510 | 2163 | static void io_req_task_work_add(struct io_kiocb *req) |
7cbf1722 | 2164 | { |
c15b79de | 2165 | struct task_struct *tsk = req->task; |
7cbf1722 | 2166 | struct io_uring_task *tctx = tsk->io_uring; |
c15b79de | 2167 | enum task_work_notify_mode notify; |
e09ee510 | 2168 | struct io_wq_work_node *node; |
0b81e80c | 2169 | unsigned long flags; |
6294f368 | 2170 | bool running; |
7cbf1722 JA |
2171 | |
2172 | WARN_ON_ONCE(!tctx); | |
2173 | ||
0b81e80c | 2174 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 | 2175 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
6294f368 PB |
2176 | running = tctx->task_running; |
2177 | if (!running) | |
2178 | tctx->task_running = true; | |
0b81e80c | 2179 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2180 | |
2181 | /* task_work already pending, we're done */ | |
6294f368 | 2182 | if (running) |
e09ee510 | 2183 | return; |
7cbf1722 | 2184 | |
c15b79de PB |
2185 | /* |
2186 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For | |
2187 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2188 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2189 | * will do the job. | |
2190 | */ | |
2191 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; | |
d97ec623 PB |
2192 | if (likely(!task_work_add(tsk, &tctx->task_work, notify))) { |
2193 | if (notify == TWA_NONE) | |
2194 | wake_up_process(tsk); | |
e09ee510 | 2195 | return; |
c15b79de | 2196 | } |
2215bed9 | 2197 | |
0b81e80c | 2198 | spin_lock_irqsave(&tctx->task_lock, flags); |
6294f368 | 2199 | tctx->task_running = false; |
e09ee510 PB |
2200 | node = tctx->task_list.first; |
2201 | INIT_WQ_LIST(&tctx->task_list); | |
0b81e80c | 2202 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 | 2203 | |
e09ee510 PB |
2204 | while (node) { |
2205 | req = container_of(node, struct io_kiocb, io_task_work.node); | |
2206 | node = node->next; | |
2207 | if (llist_add(&req->io_task_work.fallback_node, | |
2208 | &req->ctx->fallback_llist)) | |
2209 | schedule_delayed_work(&req->ctx->fallback_work, 1); | |
2210 | } | |
eab30c4d PB |
2211 | } |
2212 | ||
f237c30a | 2213 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
c40f6379 | 2214 | { |
87ceb6a6 | 2215 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2216 | |
b18a1a45 | 2217 | /* not needed for normal modes, but SQPOLL depends on it */ |
f237c30a | 2218 | io_tw_lock(ctx, locked); |
2593553a | 2219 | io_req_complete_failed(req, req->result); |
c40f6379 JA |
2220 | } |
2221 | ||
f237c30a | 2222 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
c40f6379 JA |
2223 | { |
2224 | struct io_ring_ctx *ctx = req->ctx; | |
2225 | ||
f237c30a | 2226 | io_tw_lock(ctx, locked); |
316319e8 | 2227 | /* req->task == current here, checking PF_EXITING is safe */ |
af066f31 | 2228 | if (likely(!(req->task->flags & PF_EXITING))) |
c5eef2b9 | 2229 | __io_queue_sqe(req); |
81b6d05c | 2230 | else |
2593553a | 2231 | io_req_complete_failed(req, -EFAULT); |
c40f6379 JA |
2232 | } |
2233 | ||
2c4b8eb6 | 2234 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
c40f6379 | 2235 | { |
2c4b8eb6 | 2236 | req->result = ret; |
5b0a6acc | 2237 | req->io_task_work.func = io_req_task_cancel; |
e09ee510 | 2238 | io_req_task_work_add(req); |
c40f6379 JA |
2239 | } |
2240 | ||
2c4b8eb6 | 2241 | static void io_req_task_queue(struct io_kiocb *req) |
a3df7698 | 2242 | { |
5b0a6acc | 2243 | req->io_task_work.func = io_req_task_submit; |
e09ee510 | 2244 | io_req_task_work_add(req); |
a3df7698 PB |
2245 | } |
2246 | ||
773af691 JA |
2247 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
2248 | { | |
2249 | req->io_task_work.func = io_queue_async_work; | |
2250 | io_req_task_work_add(req); | |
2251 | } | |
2252 | ||
f2f87370 | 2253 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2254 | { |
9b5f7bd9 | 2255 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2256 | |
2257 | if (nxt) | |
906a8c3f | 2258 | io_req_task_queue(nxt); |
c69f8dbe JL |
2259 | } |
2260 | ||
c3524383 | 2261 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2262 | { |
c3524383 PB |
2263 | io_queue_next(req); |
2264 | __io_free_req(req); | |
2265 | } | |
8766dd51 | 2266 | |
f237c30a PB |
2267 | static void io_free_req_work(struct io_kiocb *req, bool *locked) |
2268 | { | |
2269 | io_free_req(req); | |
2270 | } | |
2271 | ||
3aa83bfb | 2272 | static void io_free_batch_list(struct io_ring_ctx *ctx, |
1cce17ac | 2273 | struct io_wq_work_node *node) |
3aa83bfb PB |
2274 | __must_hold(&ctx->uring_lock) |
2275 | { | |
d4b7a5ef | 2276 | struct task_struct *task = NULL; |
37f0e767 | 2277 | int task_refs = 0; |
3aa83bfb | 2278 | |
3aa83bfb PB |
2279 | do { |
2280 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2281 | comp_list); | |
2282 | ||
2283 | node = req->comp_list.next; | |
d4b7a5ef PB |
2284 | if (!req_ref_put_and_test(req)) |
2285 | continue; | |
2286 | ||
2287 | io_queue_next(req); | |
2288 | io_dismantle_req(req); | |
2289 | ||
2290 | if (req->task != task) { | |
2291 | if (task) | |
2292 | io_put_task(task, task_refs); | |
2293 | task = req->task; | |
2294 | task_refs = 0; | |
2295 | } | |
2296 | task_refs++; | |
d4b7a5ef | 2297 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
3aa83bfb | 2298 | } while (node); |
d4b7a5ef | 2299 | |
d4b7a5ef PB |
2300 | if (task) |
2301 | io_put_task(task, task_refs); | |
3aa83bfb PB |
2302 | } |
2303 | ||
c450178d | 2304 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx) |
a141dd89 | 2305 | __must_hold(&ctx->uring_lock) |
905c172f | 2306 | { |
6f33b0bc | 2307 | struct io_wq_work_node *node, *prev; |
cd0ca2e0 | 2308 | struct io_submit_state *state = &ctx->submit_state; |
905c172f | 2309 | |
79ebeaee | 2310 | spin_lock(&ctx->completion_lock); |
6f33b0bc PB |
2311 | wq_list_for_each(node, prev, &state->compl_reqs) { |
2312 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2313 | comp_list); | |
5182ed2e | 2314 | |
d4d19c19 PB |
2315 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
2316 | req->compl.cflags); | |
905c172f PB |
2317 | } |
2318 | io_commit_cqring(ctx); | |
79ebeaee | 2319 | spin_unlock(&ctx->completion_lock); |
905c172f | 2320 | io_cqring_ev_posted(ctx); |
5182ed2e | 2321 | |
1cce17ac | 2322 | io_free_batch_list(ctx, state->compl_reqs.first); |
6f33b0bc | 2323 | INIT_WQ_LIST(&state->compl_reqs); |
7a743e22 PB |
2324 | } |
2325 | ||
ba816ad6 JA |
2326 | /* |
2327 | * Drop reference to request, return next in chain (if there is one) if this | |
2328 | * was the last reference to this request. | |
2329 | */ | |
0d85035a | 2330 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2331 | { |
9b5f7bd9 PB |
2332 | struct io_kiocb *nxt = NULL; |
2333 | ||
de9b4cca | 2334 | if (req_ref_put_and_test(req)) { |
9b5f7bd9 | 2335 | nxt = io_req_find_next(req); |
4d7dd462 | 2336 | __io_free_req(req); |
2a44f467 | 2337 | } |
9b5f7bd9 | 2338 | return nxt; |
2b188cc1 JA |
2339 | } |
2340 | ||
0d85035a | 2341 | static inline void io_put_req(struct io_kiocb *req) |
e65ef56d | 2342 | { |
de9b4cca | 2343 | if (req_ref_put_and_test(req)) |
e65ef56d | 2344 | io_free_req(req); |
2b188cc1 JA |
2345 | } |
2346 | ||
91c2f697 | 2347 | static inline void io_put_req_deferred(struct io_kiocb *req) |
216578e5 | 2348 | { |
91c2f697 | 2349 | if (req_ref_put_and_test(req)) { |
f237c30a | 2350 | req->io_task_work.func = io_free_req_work; |
543af3a1 PB |
2351 | io_req_task_work_add(req); |
2352 | } | |
216578e5 PB |
2353 | } |
2354 | ||
6c503150 | 2355 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2356 | { |
2357 | /* See comment at the top of this file */ | |
2358 | smp_rmb(); | |
e23de15f | 2359 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2360 | } |
2361 | ||
fb5ccc98 PB |
2362 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2363 | { | |
2364 | struct io_rings *rings = ctx->rings; | |
2365 | ||
2366 | /* make sure SQ entry isn't read before tail */ | |
2367 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2368 | } | |
2369 | ||
8ff069bf | 2370 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2371 | { |
8ff069bf | 2372 | unsigned int cflags; |
e94f141b | 2373 | |
bcda7baa JA |
2374 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2375 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2376 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2377 | kfree(kbuf); |
2378 | return cflags; | |
e94f141b JA |
2379 | } |
2380 | ||
8ff069bf | 2381 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2382 | { |
ae421d93 PB |
2383 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
2384 | return 0; | |
30d51dd4 | 2385 | return io_put_kbuf(req, req->kbuf); |
8ff069bf PB |
2386 | } |
2387 | ||
4c6e277c JA |
2388 | static inline bool io_run_task_work(void) |
2389 | { | |
ef98eb04 | 2390 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) { |
4c6e277c | 2391 | __set_current_state(TASK_RUNNING); |
ef98eb04 | 2392 | tracehook_notify_signal(); |
4c6e277c JA |
2393 | return true; |
2394 | } | |
2395 | ||
2396 | return false; | |
bcda7baa JA |
2397 | } |
2398 | ||
5ba3c874 | 2399 | static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) |
def596e9 | 2400 | { |
5eef4e87 | 2401 | struct io_wq_work_node *pos, *start, *prev; |
d729cf9a | 2402 | unsigned int poll_flags = BLK_POLL_NOSLEEP; |
b688f11e | 2403 | DEFINE_IO_COMP_BATCH(iob); |
5ba3c874 | 2404 | int nr_events = 0; |
def596e9 JA |
2405 | |
2406 | /* | |
2407 | * Only spin for completions if we don't have multiple devices hanging | |
87a115fb | 2408 | * off our complete list. |
def596e9 | 2409 | */ |
87a115fb | 2410 | if (ctx->poll_multi_queue || force_nonspin) |
ef99b2d3 | 2411 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2412 | |
5eef4e87 PB |
2413 | wq_list_for_each(pos, start, &ctx->iopoll_list) { |
2414 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
9adbd45d | 2415 | struct kiocb *kiocb = &req->rw.kiocb; |
a2416e1e | 2416 | int ret; |
def596e9 JA |
2417 | |
2418 | /* | |
581f9810 BM |
2419 | * Move completed and retryable entries to our local lists. |
2420 | * If we find a request that requires polling, break out | |
2421 | * and complete those lists first, if we have entries there. | |
def596e9 | 2422 | */ |
e3f721e6 | 2423 | if (READ_ONCE(req->iopoll_completed)) |
def596e9 JA |
2424 | break; |
2425 | ||
b688f11e | 2426 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags); |
a2416e1e PB |
2427 | if (unlikely(ret < 0)) |
2428 | return ret; | |
2429 | else if (ret) | |
ef99b2d3 | 2430 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2431 | |
3aadc23e | 2432 | /* iopoll may have completed current req */ |
b688f11e | 2433 | if (!rq_list_empty(iob.req_list) || |
e3f721e6 PB |
2434 | READ_ONCE(req->iopoll_completed)) |
2435 | break; | |
def596e9 JA |
2436 | } |
2437 | ||
b688f11e JA |
2438 | if (!rq_list_empty(iob.req_list)) |
2439 | iob.complete(&iob); | |
5eef4e87 PB |
2440 | else if (!pos) |
2441 | return 0; | |
2442 | ||
2443 | prev = start; | |
2444 | wq_list_for_each_resume(pos, prev) { | |
2445 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
2446 | ||
b3fa03fd PB |
2447 | /* order with io_complete_rw_iopoll(), e.g. ->result updates */ |
2448 | if (!smp_load_acquire(&req->iopoll_completed)) | |
e3f721e6 | 2449 | break; |
b3fa03fd | 2450 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
f5ed3bcd | 2451 | io_put_rw_kbuf(req)); |
e3f721e6 PB |
2452 | nr_events++; |
2453 | } | |
def596e9 | 2454 | |
f5ed3bcd PB |
2455 | if (unlikely(!nr_events)) |
2456 | return 0; | |
2457 | ||
2458 | io_commit_cqring(ctx); | |
2459 | io_cqring_ev_posted_iopoll(ctx); | |
1cce17ac | 2460 | pos = start ? start->next : ctx->iopoll_list.first; |
5eef4e87 | 2461 | wq_list_cut(&ctx->iopoll_list, prev, start); |
1cce17ac | 2462 | io_free_batch_list(ctx, pos); |
5ba3c874 | 2463 | return nr_events; |
def596e9 JA |
2464 | } |
2465 | ||
def596e9 JA |
2466 | /* |
2467 | * We can't just wait for polled events to come to us, we have to actively | |
2468 | * find and complete them. | |
2469 | */ | |
c072481d | 2470 | static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2471 | { |
2472 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2473 | return; | |
2474 | ||
2475 | mutex_lock(&ctx->uring_lock); | |
5eef4e87 | 2476 | while (!wq_list_empty(&ctx->iopoll_list)) { |
b2edc0a7 | 2477 | /* let it sleep and repeat later if can't complete a request */ |
5ba3c874 | 2478 | if (io_do_iopoll(ctx, true) == 0) |
b2edc0a7 | 2479 | break; |
08f5439f JA |
2480 | /* |
2481 | * Ensure we allow local-to-the-cpu processing to take place, | |
2482 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2483 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2484 | */ |
3fcee5a6 PB |
2485 | if (need_resched()) { |
2486 | mutex_unlock(&ctx->uring_lock); | |
2487 | cond_resched(); | |
2488 | mutex_lock(&ctx->uring_lock); | |
2489 | } | |
def596e9 JA |
2490 | } |
2491 | mutex_unlock(&ctx->uring_lock); | |
2492 | } | |
2493 | ||
7668b92a | 2494 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2495 | { |
7668b92a | 2496 | unsigned int nr_events = 0; |
e9979b36 | 2497 | int ret = 0; |
500f9fba | 2498 | |
c7849be9 XW |
2499 | /* |
2500 | * We disallow the app entering submit/complete with polling, but we | |
2501 | * still need to lock the ring to prevent racing with polled issue | |
2502 | * that got punted to a workqueue. | |
2503 | */ | |
2504 | mutex_lock(&ctx->uring_lock); | |
f39c8a5b PB |
2505 | /* |
2506 | * Don't enter poll loop if we already have events pending. | |
2507 | * If we do, we can potentially be spinning for commands that | |
2508 | * already triggered a CQE (eg in error). | |
2509 | */ | |
5ed7a37d | 2510 | if (test_bit(0, &ctx->check_cq_overflow)) |
f39c8a5b PB |
2511 | __io_cqring_overflow_flush(ctx, false); |
2512 | if (io_cqring_events(ctx)) | |
2513 | goto out; | |
def596e9 | 2514 | do { |
500f9fba JA |
2515 | /* |
2516 | * If a submit got punted to a workqueue, we can have the | |
2517 | * application entering polling for a command before it gets | |
2518 | * issued. That app will hold the uring_lock for the duration | |
2519 | * of the poll right here, so we need to take a breather every | |
2520 | * now and then to ensure that the issue has a chance to add | |
2521 | * the poll to the issued list. Otherwise we can spin here | |
2522 | * forever, while the workqueue is stuck trying to acquire the | |
2523 | * very same mutex. | |
2524 | */ | |
5eef4e87 | 2525 | if (wq_list_empty(&ctx->iopoll_list)) { |
8f487ef2 PB |
2526 | u32 tail = ctx->cached_cq_tail; |
2527 | ||
500f9fba | 2528 | mutex_unlock(&ctx->uring_lock); |
4c6e277c | 2529 | io_run_task_work(); |
500f9fba | 2530 | mutex_lock(&ctx->uring_lock); |
def596e9 | 2531 | |
8f487ef2 PB |
2532 | /* some requests don't go through iopoll_list */ |
2533 | if (tail != ctx->cached_cq_tail || | |
5eef4e87 | 2534 | wq_list_empty(&ctx->iopoll_list)) |
e9979b36 | 2535 | break; |
500f9fba | 2536 | } |
5ba3c874 PB |
2537 | ret = io_do_iopoll(ctx, !min); |
2538 | if (ret < 0) | |
2539 | break; | |
2540 | nr_events += ret; | |
2541 | ret = 0; | |
2542 | } while (nr_events < min && !need_resched()); | |
f39c8a5b | 2543 | out: |
500f9fba | 2544 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2545 | return ret; |
2546 | } | |
2547 | ||
491381ce | 2548 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2549 | { |
491381ce JA |
2550 | /* |
2551 | * Tell lockdep we inherited freeze protection from submission | |
2552 | * thread. | |
2553 | */ | |
2554 | if (req->flags & REQ_F_ISREG) { | |
1c98679d | 2555 | struct super_block *sb = file_inode(req->file)->i_sb; |
2b188cc1 | 2556 | |
1c98679d PB |
2557 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
2558 | sb_end_write(sb); | |
2b188cc1 JA |
2559 | } |
2560 | } | |
2561 | ||
b63534c4 | 2562 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2563 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 | 2564 | { |
ab454438 | 2565 | struct io_async_rw *rw = req->async_data; |
b63534c4 | 2566 | |
ab454438 PB |
2567 | if (!rw) |
2568 | return !io_req_prep_async(req); | |
cd658695 | 2569 | iov_iter_restore(&rw->iter, &rw->iter_state); |
ab454438 | 2570 | return true; |
b63534c4 | 2571 | } |
b63534c4 | 2572 | |
3e6a0d3c | 2573 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 2574 | { |
355afaeb | 2575 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 2576 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 2577 | |
355afaeb JA |
2578 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2579 | return false; | |
3e6a0d3c JA |
2580 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
2581 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 2582 | return false; |
7c977a58 JA |
2583 | /* |
2584 | * If ref is dying, we might be running poll reap from the exit work. | |
2585 | * Don't attempt to reissue from that path, just let it fail with | |
2586 | * -EAGAIN. | |
2587 | */ | |
3e6a0d3c JA |
2588 | if (percpu_ref_is_dying(&ctx->refs)) |
2589 | return false; | |
ef046888 JA |
2590 | /* |
2591 | * Play it safe and assume not safe to re-import and reissue if we're | |
2592 | * not in the original thread group (or in task context). | |
2593 | */ | |
2594 | if (!same_thread_group(req->task, current) || !in_task()) | |
2595 | return false; | |
3e6a0d3c JA |
2596 | return true; |
2597 | } | |
e82ad485 | 2598 | #else |
a1ff1e3f | 2599 | static bool io_resubmit_prep(struct io_kiocb *req) |
e82ad485 JA |
2600 | { |
2601 | return false; | |
2602 | } | |
e82ad485 | 2603 | static bool io_rw_should_reissue(struct io_kiocb *req) |
3e6a0d3c | 2604 | { |
b63534c4 JA |
2605 | return false; |
2606 | } | |
3e6a0d3c | 2607 | #endif |
b63534c4 | 2608 | |
8ef12efe | 2609 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
a1d7c393 | 2610 | { |
b65c128f PB |
2611 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
2612 | kiocb_end_write(req); | |
9532b99b PB |
2613 | if (res != req->result) { |
2614 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && | |
2615 | io_rw_should_reissue(req)) { | |
2616 | req->flags |= REQ_F_REISSUE; | |
8ef12efe | 2617 | return true; |
9532b99b | 2618 | } |
93d2bcd2 | 2619 | req_set_fail(req); |
8ef12efe | 2620 | req->result = res; |
9532b99b | 2621 | } |
8ef12efe JA |
2622 | return false; |
2623 | } | |
2624 | ||
f237c30a | 2625 | static void io_req_task_complete(struct io_kiocb *req, bool *locked) |
8ef12efe | 2626 | { |
126180b9 PB |
2627 | unsigned int cflags = io_put_rw_kbuf(req); |
2628 | long res = req->result; | |
2629 | ||
fff4e40e | 2630 | if (*locked) { |
126180b9 | 2631 | io_req_complete_state(req, res, cflags); |
fff4e40e PB |
2632 | io_req_add_compl_list(req); |
2633 | } else { | |
126180b9 | 2634 | io_req_complete_post(req, res, cflags); |
fff4e40e | 2635 | } |
8ef12efe JA |
2636 | } |
2637 | ||
2638 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, | |
2639 | unsigned int issue_flags) | |
2640 | { | |
2641 | if (__io_complete_rw_common(req, res)) | |
2642 | return; | |
63637853 | 2643 | __io_req_complete(req, issue_flags, req->result, io_put_rw_kbuf(req)); |
ba816ad6 JA |
2644 | } |
2645 | ||
2646 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2647 | { | |
9adbd45d | 2648 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2649 | |
8ef12efe JA |
2650 | if (__io_complete_rw_common(req, res)) |
2651 | return; | |
2652 | req->result = res; | |
2653 | req->io_task_work.func = io_req_task_complete; | |
2654 | io_req_task_work_add(req); | |
2b188cc1 JA |
2655 | } |
2656 | ||
def596e9 JA |
2657 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2658 | { | |
9adbd45d | 2659 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2660 | |
491381ce JA |
2661 | if (kiocb->ki_flags & IOCB_WRITE) |
2662 | kiocb_end_write(req); | |
9532b99b | 2663 | if (unlikely(res != req->result)) { |
b66ceaf3 PB |
2664 | if (res == -EAGAIN && io_rw_should_reissue(req)) { |
2665 | req->flags |= REQ_F_REISSUE; | |
2666 | return; | |
9532b99b | 2667 | } |
8c130827 | 2668 | } |
bbde017a | 2669 | |
b3fa03fd PB |
2670 | req->result = res; |
2671 | /* order with io_iopoll_complete() checking ->iopoll_completed */ | |
2672 | smp_store_release(&req->iopoll_completed, 1); | |
def596e9 JA |
2673 | } |
2674 | ||
2675 | /* | |
2676 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2677 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
f39c8a5b | 2678 | * find it from a io_do_iopoll() thread before the issuer is done |
def596e9 JA |
2679 | * accessing the kiocb cookie. |
2680 | */ | |
cb3d8972 | 2681 | static void io_iopoll_req_issued(struct io_kiocb *req) |
def596e9 JA |
2682 | { |
2683 | struct io_ring_ctx *ctx = req->ctx; | |
cb3d8972 PB |
2684 | const bool in_async = io_wq_current_is_worker(); |
2685 | ||
2686 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
2687 | if (unlikely(in_async)) | |
2688 | mutex_lock(&ctx->uring_lock); | |
def596e9 JA |
2689 | |
2690 | /* | |
2691 | * Track whether we have multiple files in our lists. This will impact | |
2692 | * how we do polling eventually, not spinning if we're on potentially | |
2693 | * different devices. | |
2694 | */ | |
5eef4e87 | 2695 | if (wq_list_empty(&ctx->iopoll_list)) { |
915b3dde HX |
2696 | ctx->poll_multi_queue = false; |
2697 | } else if (!ctx->poll_multi_queue) { | |
def596e9 JA |
2698 | struct io_kiocb *list_req; |
2699 | ||
5eef4e87 PB |
2700 | list_req = container_of(ctx->iopoll_list.first, struct io_kiocb, |
2701 | comp_list); | |
30da1b45 | 2702 | if (list_req->file != req->file) |
915b3dde | 2703 | ctx->poll_multi_queue = true; |
def596e9 JA |
2704 | } |
2705 | ||
2706 | /* | |
2707 | * For fast devices, IO may have already completed. If it has, add | |
2708 | * it to the front so we find it first. | |
2709 | */ | |
65a6543d | 2710 | if (READ_ONCE(req->iopoll_completed)) |
5eef4e87 | 2711 | wq_list_add_head(&req->comp_list, &ctx->iopoll_list); |
def596e9 | 2712 | else |
5eef4e87 | 2713 | wq_list_add_tail(&req->comp_list, &ctx->iopoll_list); |
bdcd3eab | 2714 | |
cb3d8972 PB |
2715 | if (unlikely(in_async)) { |
2716 | /* | |
2717 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle | |
2718 | * in sq thread task context or in io worker task context. If | |
2719 | * current task context is sq thread, we don't need to check | |
2720 | * whether should wake up sq thread. | |
2721 | */ | |
2722 | if ((ctx->flags & IORING_SETUP_SQPOLL) && | |
2723 | wq_has_sleeper(&ctx->sq_data->wait)) | |
2724 | wake_up(&ctx->sq_data->wait); | |
2725 | ||
2726 | mutex_unlock(&ctx->uring_lock); | |
2727 | } | |
def596e9 JA |
2728 | } |
2729 | ||
4503b767 JA |
2730 | static bool io_bdev_nowait(struct block_device *bdev) |
2731 | { | |
9ba0d0c8 | 2732 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2733 | } |
2734 | ||
2b188cc1 JA |
2735 | /* |
2736 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2737 | * any file. For now, just ensure that anything potentially problematic is done | |
2738 | * inline. | |
2739 | */ | |
b191e2df | 2740 | static bool __io_file_supports_nowait(struct file *file, int rw) |
2b188cc1 JA |
2741 | { |
2742 | umode_t mode = file_inode(file)->i_mode; | |
2743 | ||
4503b767 | 2744 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2745 | if (IS_ENABLED(CONFIG_BLOCK) && |
2746 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2747 | return true; |
2748 | return false; | |
2749 | } | |
976517f1 | 2750 | if (S_ISSOCK(mode)) |
2b188cc1 | 2751 | return true; |
4503b767 | 2752 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2753 | if (IS_ENABLED(CONFIG_BLOCK) && |
2754 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2755 | file->f_op != &io_uring_fops) |
2756 | return true; | |
2757 | return false; | |
2758 | } | |
2b188cc1 | 2759 | |
c5b85625 JA |
2760 | /* any ->read/write should understand O_NONBLOCK */ |
2761 | if (file->f_flags & O_NONBLOCK) | |
2762 | return true; | |
2763 | ||
af197f50 JA |
2764 | if (!(file->f_mode & FMODE_NOWAIT)) |
2765 | return false; | |
2766 | ||
2767 | if (rw == READ) | |
2768 | return file->f_op->read_iter != NULL; | |
2769 | ||
2770 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
2771 | } |
2772 | ||
b191e2df | 2773 | static bool io_file_supports_nowait(struct io_kiocb *req, int rw) |
7b29f92d | 2774 | { |
b191e2df | 2775 | if (rw == READ && (req->flags & REQ_F_NOWAIT_READ)) |
7b29f92d | 2776 | return true; |
b191e2df | 2777 | else if (rw == WRITE && (req->flags & REQ_F_NOWAIT_WRITE)) |
7b29f92d JA |
2778 | return true; |
2779 | ||
b191e2df | 2780 | return __io_file_supports_nowait(req->file, rw); |
7b29f92d JA |
2781 | } |
2782 | ||
5d329e12 JA |
2783 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2784 | int rw) | |
2b188cc1 | 2785 | { |
def596e9 | 2786 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2787 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 2788 | struct file *file = req->file; |
09bb8394 JA |
2789 | unsigned ioprio; |
2790 | int ret; | |
2b188cc1 | 2791 | |
c97d8a0f | 2792 | if (!io_req_ffs_set(req) && S_ISREG(file_inode(file)->i_mode)) |
491381ce JA |
2793 | req->flags |= REQ_F_ISREG; |
2794 | ||
2b188cc1 | 2795 | kiocb->ki_pos = READ_ONCE(sqe->off); |
75c668cd | 2796 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
ba04291e | 2797 | req->flags |= REQ_F_CUR_POS; |
75c668cd | 2798 | kiocb->ki_pos = file->f_pos; |
ba04291e | 2799 | } |
2b188cc1 | 2800 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
2801 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
2802 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
2803 | if (unlikely(ret)) | |
2804 | return ret; | |
2b188cc1 | 2805 | |
5d329e12 JA |
2806 | /* |
2807 | * If the file is marked O_NONBLOCK, still allow retry for it if it | |
2808 | * supports async. Otherwise it's impossible to use O_NONBLOCK files | |
2809 | * reliably. If not, or it IOCB_NOWAIT is set, don't retry. | |
2810 | */ | |
2811 | if ((kiocb->ki_flags & IOCB_NOWAIT) || | |
2812 | ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req, rw))) | |
75c668cd PB |
2813 | req->flags |= REQ_F_NOWAIT; |
2814 | ||
2b188cc1 JA |
2815 | ioprio = READ_ONCE(sqe->ioprio); |
2816 | if (ioprio) { | |
2817 | ret = ioprio_check_cap(ioprio); | |
2818 | if (ret) | |
09bb8394 | 2819 | return ret; |
2b188cc1 JA |
2820 | |
2821 | kiocb->ki_ioprio = ioprio; | |
2822 | } else | |
2823 | kiocb->ki_ioprio = get_current_ioprio(); | |
2824 | ||
def596e9 | 2825 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
2826 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
2827 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 2828 | return -EOPNOTSUPP; |
2b188cc1 | 2829 | |
394918eb | 2830 | kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE; |
def596e9 | 2831 | kiocb->ki_complete = io_complete_rw_iopoll; |
65a6543d | 2832 | req->iopoll_completed = 0; |
def596e9 | 2833 | } else { |
09bb8394 JA |
2834 | if (kiocb->ki_flags & IOCB_HIPRI) |
2835 | return -EINVAL; | |
def596e9 JA |
2836 | kiocb->ki_complete = io_complete_rw; |
2837 | } | |
9adbd45d | 2838 | |
eae071c9 PB |
2839 | if (req->opcode == IORING_OP_READ_FIXED || |
2840 | req->opcode == IORING_OP_WRITE_FIXED) { | |
2841 | req->imu = NULL; | |
2842 | io_req_set_rsrc_node(req); | |
2843 | } | |
2844 | ||
3529d8c2 JA |
2845 | req->rw.addr = READ_ONCE(sqe->addr); |
2846 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2847 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2848 | return 0; |
2b188cc1 JA |
2849 | } |
2850 | ||
2851 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2852 | { | |
2853 | switch (ret) { | |
2854 | case -EIOCBQUEUED: | |
2855 | break; | |
2856 | case -ERESTARTSYS: | |
2857 | case -ERESTARTNOINTR: | |
2858 | case -ERESTARTNOHAND: | |
2859 | case -ERESTART_RESTARTBLOCK: | |
2860 | /* | |
2861 | * We can't just restart the syscall, since previously | |
2862 | * submitted sqes may already be in progress. Just fail this | |
2863 | * IO with EINTR. | |
2864 | */ | |
2865 | ret = -EINTR; | |
df561f66 | 2866 | fallthrough; |
2b188cc1 JA |
2867 | default: |
2868 | kiocb->ki_complete(kiocb, ret, 0); | |
2869 | } | |
2870 | } | |
2871 | ||
a1d7c393 | 2872 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
889fca73 | 2873 | unsigned int issue_flags) |
ba816ad6 | 2874 | { |
ba04291e | 2875 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2876 | struct io_async_rw *io = req->async_data; |
ba04291e | 2877 | |
227c0c96 | 2878 | /* add previously done IO, if any */ |
e8c2bc1f | 2879 | if (io && io->bytes_done > 0) { |
227c0c96 | 2880 | if (ret < 0) |
e8c2bc1f | 2881 | ret = io->bytes_done; |
227c0c96 | 2882 | else |
e8c2bc1f | 2883 | ret += io->bytes_done; |
227c0c96 JA |
2884 | } |
2885 | ||
ba04291e JA |
2886 | if (req->flags & REQ_F_CUR_POS) |
2887 | req->file->f_pos = kiocb->ki_pos; | |
b66ceaf3 | 2888 | if (ret >= 0 && (kiocb->ki_complete == io_complete_rw)) |
889fca73 | 2889 | __io_complete_rw(req, ret, 0, issue_flags); |
ba816ad6 JA |
2890 | else |
2891 | io_rw_done(kiocb, ret); | |
97284637 | 2892 | |
b66ceaf3 | 2893 | if (req->flags & REQ_F_REISSUE) { |
97284637 | 2894 | req->flags &= ~REQ_F_REISSUE; |
a7be7c23 | 2895 | if (io_resubmit_prep(req)) { |
773af691 | 2896 | io_req_task_queue_reissue(req); |
8c130827 | 2897 | } else { |
b66ceaf3 PB |
2898 | unsigned int cflags = io_put_rw_kbuf(req); |
2899 | struct io_ring_ctx *ctx = req->ctx; | |
2900 | ||
93d2bcd2 | 2901 | req_set_fail(req); |
14cfbb7a | 2902 | if (!(issue_flags & IO_URING_F_NONBLOCK)) { |
b66ceaf3 PB |
2903 | mutex_lock(&ctx->uring_lock); |
2904 | __io_req_complete(req, issue_flags, ret, cflags); | |
2905 | mutex_unlock(&ctx->uring_lock); | |
2906 | } else { | |
2907 | __io_req_complete(req, issue_flags, ret, cflags); | |
2908 | } | |
97284637 PB |
2909 | } |
2910 | } | |
ba816ad6 JA |
2911 | } |
2912 | ||
eae071c9 PB |
2913 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
2914 | struct io_mapped_ubuf *imu) | |
edafccee | 2915 | { |
9adbd45d | 2916 | size_t len = req->rw.len; |
75769e3f | 2917 | u64 buf_end, buf_addr = req->rw.addr; |
edafccee | 2918 | size_t offset; |
edafccee | 2919 | |
75769e3f | 2920 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
edafccee JA |
2921 | return -EFAULT; |
2922 | /* not inside the mapped region */ | |
4751f53d | 2923 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
edafccee JA |
2924 | return -EFAULT; |
2925 | ||
2926 | /* | |
2927 | * May not be a start of buffer, set size appropriately | |
2928 | * and advance us to the beginning. | |
2929 | */ | |
2930 | offset = buf_addr - imu->ubuf; | |
2931 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
2932 | |
2933 | if (offset) { | |
2934 | /* | |
2935 | * Don't use iov_iter_advance() here, as it's really slow for | |
2936 | * using the latter parts of a big fixed buffer - it iterates | |
2937 | * over each segment manually. We can cheat a bit here, because | |
2938 | * we know that: | |
2939 | * | |
2940 | * 1) it's a BVEC iter, we set it up | |
2941 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
2942 | * first and last bvec | |
2943 | * | |
2944 | * So just find our index, and adjust the iterator afterwards. | |
2945 | * If the offset is within the first bvec (or the whole first | |
2946 | * bvec, just use iov_iter_advance(). This makes it easier | |
2947 | * since we can just skip the first segment, which may not | |
2948 | * be PAGE_SIZE aligned. | |
2949 | */ | |
2950 | const struct bio_vec *bvec = imu->bvec; | |
2951 | ||
2952 | if (offset <= bvec->bv_len) { | |
2953 | iov_iter_advance(iter, offset); | |
2954 | } else { | |
2955 | unsigned long seg_skip; | |
2956 | ||
2957 | /* skip first vec */ | |
2958 | offset -= bvec->bv_len; | |
2959 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
2960 | ||
2961 | iter->bvec = bvec + seg_skip; | |
2962 | iter->nr_segs -= seg_skip; | |
99c79f66 | 2963 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 2964 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
2965 | } |
2966 | } | |
2967 | ||
847595de | 2968 | return 0; |
edafccee JA |
2969 | } |
2970 | ||
eae071c9 PB |
2971 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
2972 | { | |
2973 | struct io_ring_ctx *ctx = req->ctx; | |
2974 | struct io_mapped_ubuf *imu = req->imu; | |
2975 | u16 index, buf_index = req->buf_index; | |
2976 | ||
2977 | if (likely(!imu)) { | |
2978 | if (unlikely(buf_index >= ctx->nr_user_bufs)) | |
2979 | return -EFAULT; | |
2980 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); | |
2981 | imu = READ_ONCE(ctx->user_bufs[index]); | |
2982 | req->imu = imu; | |
2983 | } | |
2984 | return __io_import_fixed(req, rw, iter, imu); | |
2985 | } | |
2986 | ||
bcda7baa JA |
2987 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
2988 | { | |
2989 | if (needs_lock) | |
2990 | mutex_unlock(&ctx->uring_lock); | |
2991 | } | |
2992 | ||
2993 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
2994 | { | |
2995 | /* | |
2996 | * "Normal" inline submissions always hold the uring_lock, since we | |
2997 | * grab it from the system call. Same is true for the SQPOLL offload. | |
2998 | * The only exception is when we've detached the request and issue it | |
2999 | * from an async worker thread, grab the lock for that case. | |
3000 | */ | |
3001 | if (needs_lock) | |
3002 | mutex_lock(&ctx->uring_lock); | |
3003 | } | |
3004 | ||
3005 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
30d51dd4 | 3006 | int bgid, bool needs_lock) |
bcda7baa | 3007 | { |
30d51dd4 | 3008 | struct io_buffer *kbuf = req->kbuf; |
bcda7baa JA |
3009 | struct io_buffer *head; |
3010 | ||
3011 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3012 | return kbuf; | |
3013 | ||
3014 | io_ring_submit_lock(req->ctx, needs_lock); | |
3015 | ||
3016 | lockdep_assert_held(&req->ctx->uring_lock); | |
3017 | ||
9e15c3a0 | 3018 | head = xa_load(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
3019 | if (head) { |
3020 | if (!list_empty(&head->list)) { | |
3021 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
3022 | list); | |
3023 | list_del(&kbuf->list); | |
3024 | } else { | |
3025 | kbuf = head; | |
9e15c3a0 | 3026 | xa_erase(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
3027 | } |
3028 | if (*len > kbuf->len) | |
3029 | *len = kbuf->len; | |
30d51dd4 PB |
3030 | req->flags |= REQ_F_BUFFER_SELECTED; |
3031 | req->kbuf = kbuf; | |
bcda7baa JA |
3032 | } else { |
3033 | kbuf = ERR_PTR(-ENOBUFS); | |
3034 | } | |
3035 | ||
3036 | io_ring_submit_unlock(req->ctx, needs_lock); | |
bcda7baa JA |
3037 | return kbuf; |
3038 | } | |
3039 | ||
4d954c25 JA |
3040 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
3041 | bool needs_lock) | |
3042 | { | |
3043 | struct io_buffer *kbuf; | |
4f4eeba8 | 3044 | u16 bgid; |
4d954c25 | 3045 | |
4f4eeba8 | 3046 | bgid = req->buf_index; |
30d51dd4 | 3047 | kbuf = io_buffer_select(req, len, bgid, needs_lock); |
4d954c25 JA |
3048 | if (IS_ERR(kbuf)) |
3049 | return kbuf; | |
4d954c25 JA |
3050 | return u64_to_user_ptr(kbuf->addr); |
3051 | } | |
3052 | ||
3053 | #ifdef CONFIG_COMPAT | |
3054 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
3055 | bool needs_lock) | |
3056 | { | |
3057 | struct compat_iovec __user *uiov; | |
3058 | compat_ssize_t clen; | |
3059 | void __user *buf; | |
3060 | ssize_t len; | |
3061 | ||
3062 | uiov = u64_to_user_ptr(req->rw.addr); | |
3063 | if (!access_ok(uiov, sizeof(*uiov))) | |
3064 | return -EFAULT; | |
3065 | if (__get_user(clen, &uiov->iov_len)) | |
3066 | return -EFAULT; | |
3067 | if (clen < 0) | |
3068 | return -EINVAL; | |
3069 | ||
3070 | len = clen; | |
3071 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3072 | if (IS_ERR(buf)) | |
3073 | return PTR_ERR(buf); | |
3074 | iov[0].iov_base = buf; | |
3075 | iov[0].iov_len = (compat_size_t) len; | |
3076 | return 0; | |
3077 | } | |
3078 | #endif | |
3079 | ||
3080 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3081 | bool needs_lock) | |
3082 | { | |
3083 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3084 | void __user *buf; | |
3085 | ssize_t len; | |
3086 | ||
3087 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3088 | return -EFAULT; | |
3089 | ||
3090 | len = iov[0].iov_len; | |
3091 | if (len < 0) | |
3092 | return -EINVAL; | |
3093 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3094 | if (IS_ERR(buf)) | |
3095 | return PTR_ERR(buf); | |
3096 | iov[0].iov_base = buf; | |
3097 | iov[0].iov_len = len; | |
3098 | return 0; | |
3099 | } | |
3100 | ||
3101 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3102 | bool needs_lock) | |
3103 | { | |
dddb3e26 | 3104 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
30d51dd4 | 3105 | struct io_buffer *kbuf = req->kbuf; |
dddb3e26 | 3106 | |
dddb3e26 JA |
3107 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
3108 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3109 | return 0; |
dddb3e26 | 3110 | } |
dd201662 | 3111 | if (req->rw.len != 1) |
4d954c25 JA |
3112 | return -EINVAL; |
3113 | ||
3114 | #ifdef CONFIG_COMPAT | |
3115 | if (req->ctx->compat) | |
3116 | return io_compat_import(req, iov, needs_lock); | |
3117 | #endif | |
3118 | ||
3119 | return __io_iov_buffer_select(req, iov, needs_lock); | |
3120 | } | |
3121 | ||
847595de PB |
3122 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
3123 | struct iov_iter *iter, bool needs_lock) | |
2b188cc1 | 3124 | { |
9adbd45d JA |
3125 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
3126 | size_t sqe_len = req->rw.len; | |
847595de | 3127 | u8 opcode = req->opcode; |
4d954c25 | 3128 | ssize_t ret; |
edafccee | 3129 | |
7d009165 | 3130 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 3131 | *iovec = NULL; |
9adbd45d | 3132 | return io_import_fixed(req, rw, iter); |
edafccee | 3133 | } |
2b188cc1 | 3134 | |
bcda7baa | 3135 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 3136 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
3137 | return -EINVAL; |
3138 | ||
3a6820f2 | 3139 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3140 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 3141 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 3142 | if (IS_ERR(buf)) |
4d954c25 | 3143 | return PTR_ERR(buf); |
3f9d6441 | 3144 | req->rw.len = sqe_len; |
bcda7baa JA |
3145 | } |
3146 | ||
3a6820f2 JA |
3147 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
3148 | *iovec = NULL; | |
10fc72e4 | 3149 | return ret; |
3a6820f2 JA |
3150 | } |
3151 | ||
4d954c25 JA |
3152 | if (req->flags & REQ_F_BUFFER_SELECT) { |
3153 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
847595de PB |
3154 | if (!ret) |
3155 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); | |
4d954c25 JA |
3156 | *iovec = NULL; |
3157 | return ret; | |
3158 | } | |
3159 | ||
89cd35c5 CH |
3160 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
3161 | req->ctx->compat); | |
2b188cc1 JA |
3162 | } |
3163 | ||
0fef9483 JA |
3164 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3165 | { | |
5b09e37e | 3166 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3167 | } |
3168 | ||
31b51510 | 3169 | /* |
32960613 JA |
3170 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3171 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3172 | */ |
4017eb91 | 3173 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3174 | { |
4017eb91 JA |
3175 | struct kiocb *kiocb = &req->rw.kiocb; |
3176 | struct file *file = req->file; | |
32960613 JA |
3177 | ssize_t ret = 0; |
3178 | ||
3179 | /* | |
3180 | * Don't support polled IO through this interface, and we can't | |
3181 | * support non-blocking either. For the latter, this just causes | |
3182 | * the kiocb to be handled from an async context. | |
3183 | */ | |
3184 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3185 | return -EOPNOTSUPP; | |
3186 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3187 | return -EAGAIN; | |
3188 | ||
3189 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3190 | struct iovec iovec; |
32960613 JA |
3191 | ssize_t nr; |
3192 | ||
311ae9e1 PB |
3193 | if (!iov_iter_is_bvec(iter)) { |
3194 | iovec = iov_iter_iovec(iter); | |
3195 | } else { | |
4017eb91 JA |
3196 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3197 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3198 | } |
3199 | ||
32960613 JA |
3200 | if (rw == READ) { |
3201 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3202 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3203 | } else { |
3204 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3205 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3206 | } |
3207 | ||
3208 | if (nr < 0) { | |
3209 | if (!ret) | |
3210 | ret = nr; | |
3211 | break; | |
3212 | } | |
16c8d2df JA |
3213 | if (!iov_iter_is_bvec(iter)) { |
3214 | iov_iter_advance(iter, nr); | |
3215 | } else { | |
3216 | req->rw.len -= nr; | |
3217 | req->rw.addr += nr; | |
3218 | } | |
32960613 JA |
3219 | ret += nr; |
3220 | if (nr != iovec.iov_len) | |
3221 | break; | |
32960613 JA |
3222 | } |
3223 | ||
3224 | return ret; | |
3225 | } | |
3226 | ||
ff6165b2 JA |
3227 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3228 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3229 | { |
e8c2bc1f | 3230 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3231 | |
ff6165b2 | 3232 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3233 | rw->free_iovec = iovec; |
227c0c96 | 3234 | rw->bytes_done = 0; |
ff6165b2 | 3235 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3236 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3237 | return; |
b64e3444 | 3238 | if (!iovec) { |
ff6165b2 JA |
3239 | unsigned iov_off = 0; |
3240 | ||
3241 | rw->iter.iov = rw->fast_iov; | |
3242 | if (iter->iov != fast_iov) { | |
3243 | iov_off = iter->iov - fast_iov; | |
3244 | rw->iter.iov += iov_off; | |
3245 | } | |
3246 | if (rw->fast_iov != fast_iov) | |
3247 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3248 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3249 | } else { |
3250 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3251 | } |
3252 | } | |
3253 | ||
8d4af685 | 3254 | static inline bool io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3255 | { |
e8c2bc1f JA |
3256 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3257 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3258 | return req->async_data == NULL; | |
3d9932a8 XW |
3259 | } |
3260 | ||
ff6165b2 JA |
3261 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3262 | const struct iovec *fast_iov, | |
227c0c96 | 3263 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3264 | { |
26f0505a | 3265 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
74566df3 | 3266 | return 0; |
e8c2bc1f | 3267 | if (!req->async_data) { |
cd658695 JA |
3268 | struct io_async_rw *iorw; |
3269 | ||
6cb78689 | 3270 | if (io_alloc_async_data(req)) { |
6bf985dc | 3271 | kfree(iovec); |
5d204bcf | 3272 | return -ENOMEM; |
6bf985dc | 3273 | } |
b7bb4f7d | 3274 | |
ff6165b2 | 3275 | io_req_map_rw(req, iovec, fast_iov, iter); |
cd658695 JA |
3276 | iorw = req->async_data; |
3277 | /* we've copied and mapped the iter, ensure state is saved */ | |
3278 | iov_iter_save_state(&iorw->iter, &iorw->iter_state); | |
5d204bcf | 3279 | } |
b7bb4f7d | 3280 | return 0; |
f67676d1 JA |
3281 | } |
3282 | ||
73debe68 | 3283 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3284 | { |
e8c2bc1f | 3285 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3286 | struct iovec *iov = iorw->fast_iov; |
847595de | 3287 | int ret; |
c3e330a4 | 3288 | |
2846c481 | 3289 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
c3e330a4 PB |
3290 | if (unlikely(ret < 0)) |
3291 | return ret; | |
3292 | ||
ab0b196c PB |
3293 | iorw->bytes_done = 0; |
3294 | iorw->free_iovec = iov; | |
3295 | if (iov) | |
3296 | req->flags |= REQ_F_NEED_CLEANUP; | |
cd658695 | 3297 | iov_iter_save_state(&iorw->iter, &iorw->iter_state); |
c3e330a4 PB |
3298 | return 0; |
3299 | } | |
3300 | ||
73debe68 | 3301 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3302 | { |
3529d8c2 JA |
3303 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3304 | return -EBADF; | |
5d329e12 | 3305 | return io_prep_rw(req, sqe, READ); |
f67676d1 JA |
3306 | } |
3307 | ||
c1dd91d1 JA |
3308 | /* |
3309 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3310 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3311 | * This gets called when the page is unlocked, and we generally expect that to | |
3312 | * happen when the page IO is completed and the page is now uptodate. This will | |
3313 | * queue a task_work based retry of the operation, attempting to copy the data | |
3314 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3315 | * do a thread based blocking retry of the operation. That's the unexpected | |
3316 | * slow path. | |
3317 | */ | |
bcf5a063 JA |
3318 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3319 | int sync, void *arg) | |
3320 | { | |
3321 | struct wait_page_queue *wpq; | |
3322 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3323 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3324 | |
3325 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3326 | ||
cdc8fcb4 LT |
3327 | if (!wake_page_match(wpq, key)) |
3328 | return 0; | |
3329 | ||
c8d317aa | 3330 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 | 3331 | list_del_init(&wait->entry); |
921b9054 | 3332 | io_req_task_queue(req); |
bcf5a063 JA |
3333 | return 1; |
3334 | } | |
3335 | ||
c1dd91d1 JA |
3336 | /* |
3337 | * This controls whether a given IO request should be armed for async page | |
3338 | * based retry. If we return false here, the request is handed to the async | |
3339 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3340 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3341 | * will either succeed because the page is now uptodate and unlocked, or it | |
3342 | * will register a callback when the page is unlocked at IO completion. Through | |
3343 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3344 | * That retry will attempt the buffered read again. The retry will generally | |
3345 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3346 | * async worker threads for a blocking retry. | |
3347 | */ | |
227c0c96 | 3348 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3349 | { |
e8c2bc1f JA |
3350 | struct io_async_rw *rw = req->async_data; |
3351 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3352 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3353 | |
bcf5a063 JA |
3354 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3355 | if (req->flags & REQ_F_NOWAIT) | |
3356 | return false; | |
f67676d1 | 3357 | |
227c0c96 | 3358 | /* Only for buffered IO */ |
3b2a4439 | 3359 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3360 | return false; |
3b2a4439 | 3361 | |
bcf5a063 JA |
3362 | /* |
3363 | * just use poll if we can, and don't attempt if the fs doesn't | |
3364 | * support callback based unlocks | |
3365 | */ | |
3366 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3367 | return false; | |
f67676d1 | 3368 | |
3b2a4439 JA |
3369 | wait->wait.func = io_async_buf_func; |
3370 | wait->wait.private = req; | |
3371 | wait->wait.flags = 0; | |
3372 | INIT_LIST_HEAD(&wait->wait.entry); | |
3373 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3374 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3375 | kiocb->ki_waitq = wait; |
3b2a4439 | 3376 | return true; |
bcf5a063 JA |
3377 | } |
3378 | ||
aeab9506 | 3379 | static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
bcf5a063 JA |
3380 | { |
3381 | if (req->file->f_op->read_iter) | |
3382 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d | 3383 | else if (req->file->f_op->read) |
4017eb91 | 3384 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3385 | else |
3386 | return -EINVAL; | |
f67676d1 JA |
3387 | } |
3388 | ||
7db30437 ML |
3389 | static bool need_read_all(struct io_kiocb *req) |
3390 | { | |
3391 | return req->flags & REQ_F_ISREG || | |
3392 | S_ISBLK(file_inode(req->file)->i_mode); | |
3393 | } | |
3394 | ||
889fca73 | 3395 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3396 | { |
3397 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3398 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3399 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3400 | struct io_async_rw *rw = req->async_data; |
45d189c6 | 3401 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
cd658695 JA |
3402 | struct iov_iter_state __state, *state; |
3403 | ssize_t ret, ret2; | |
ff6165b2 | 3404 | |
2846c481 | 3405 | if (rw) { |
e8c2bc1f | 3406 | iter = &rw->iter; |
cd658695 JA |
3407 | state = &rw->iter_state; |
3408 | /* | |
3409 | * We come here from an earlier attempt, restore our state to | |
3410 | * match in case it doesn't. It's cheap enough that we don't | |
3411 | * need to make this conditional. | |
3412 | */ | |
3413 | iov_iter_restore(iter, state); | |
2846c481 PB |
3414 | iovec = NULL; |
3415 | } else { | |
3416 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); | |
3417 | if (ret < 0) | |
3418 | return ret; | |
cd658695 JA |
3419 | state = &__state; |
3420 | iov_iter_save_state(iter, state); | |
2846c481 | 3421 | } |
cd658695 | 3422 | req->result = iov_iter_count(iter); |
2b188cc1 | 3423 | |
fd6c2e4c JA |
3424 | /* Ensure we clear previously set non-block flag */ |
3425 | if (!force_nonblock) | |
29de5f6a | 3426 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
a88fc400 PB |
3427 | else |
3428 | kiocb->ki_flags |= IOCB_NOWAIT; | |
3429 | ||
24c74678 | 3430 | /* If the file doesn't support async, just async punt */ |
b191e2df | 3431 | if (force_nonblock && !io_file_supports_nowait(req, READ)) { |
6713e7a6 | 3432 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc | 3433 | return ret ?: -EAGAIN; |
6713e7a6 | 3434 | } |
9e645e11 | 3435 | |
cd658695 | 3436 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), req->result); |
5ea5dd45 PB |
3437 | if (unlikely(ret)) { |
3438 | kfree(iovec); | |
3439 | return ret; | |
3440 | } | |
2b188cc1 | 3441 | |
227c0c96 | 3442 | ret = io_iter_do_read(req, iter); |
32960613 | 3443 | |
230d50d4 | 3444 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
6ad7f233 | 3445 | req->flags &= ~REQ_F_REISSUE; |
eefdf30f JA |
3446 | /* IOPOLL retry should happen for io-wq threads */ |
3447 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3448 | goto done; |
75c668cd PB |
3449 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3450 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3451 | goto done; |
f38c7e3a | 3452 | ret = 0; |
230d50d4 JA |
3453 | } else if (ret == -EIOCBQUEUED) { |
3454 | goto out_free; | |
cd658695 | 3455 | } else if (ret <= 0 || ret == req->result || !force_nonblock || |
7db30437 | 3456 | (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) { |
7335e3bf | 3457 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3458 | goto done; |
227c0c96 JA |
3459 | } |
3460 | ||
cd658695 JA |
3461 | /* |
3462 | * Don't depend on the iter state matching what was consumed, or being | |
3463 | * untouched in case of error. Restore it and we'll advance it | |
3464 | * manually if we need to. | |
3465 | */ | |
3466 | iov_iter_restore(iter, state); | |
3467 | ||
227c0c96 | 3468 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc PB |
3469 | if (ret2) |
3470 | return ret2; | |
3471 | ||
fe1cdd55 | 3472 | iovec = NULL; |
e8c2bc1f | 3473 | rw = req->async_data; |
cd658695 JA |
3474 | /* |
3475 | * Now use our persistent iterator and state, if we aren't already. | |
3476 | * We've restored and mapped the iter to match. | |
3477 | */ | |
3478 | if (iter != &rw->iter) { | |
3479 | iter = &rw->iter; | |
3480 | state = &rw->iter_state; | |
3481 | } | |
227c0c96 | 3482 | |
b23df91b | 3483 | do { |
cd658695 JA |
3484 | /* |
3485 | * We end up here because of a partial read, either from | |
3486 | * above or inside this loop. Advance the iter by the bytes | |
3487 | * that were consumed. | |
3488 | */ | |
3489 | iov_iter_advance(iter, ret); | |
3490 | if (!iov_iter_count(iter)) | |
3491 | break; | |
b23df91b | 3492 | rw->bytes_done += ret; |
cd658695 JA |
3493 | iov_iter_save_state(iter, state); |
3494 | ||
b23df91b PB |
3495 | /* if we can retry, do so with the callbacks armed */ |
3496 | if (!io_rw_should_retry(req)) { | |
3497 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3498 | return -EAGAIN; | |
3499 | } | |
3500 | ||
3501 | /* | |
3502 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3503 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3504 | * desired page gets unlocked. We can also get a partial read | |
3505 | * here, and if we do, then just retry at the new offset. | |
3506 | */ | |
3507 | ret = io_iter_do_read(req, iter); | |
3508 | if (ret == -EIOCBQUEUED) | |
3509 | return 0; | |
227c0c96 | 3510 | /* we got some bytes, but not all. retry. */ |
b5b0ecb7 | 3511 | kiocb->ki_flags &= ~IOCB_WAITQ; |
cd658695 JA |
3512 | iov_iter_restore(iter, state); |
3513 | } while (ret > 0); | |
227c0c96 | 3514 | done: |
889fca73 | 3515 | kiocb_done(kiocb, ret, issue_flags); |
fe1cdd55 PB |
3516 | out_free: |
3517 | /* it's faster to check here then delegate to kfree */ | |
3518 | if (iovec) | |
3519 | kfree(iovec); | |
5ea5dd45 | 3520 | return 0; |
2b188cc1 JA |
3521 | } |
3522 | ||
73debe68 | 3523 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3524 | { |
3529d8c2 JA |
3525 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3526 | return -EBADF; | |
5d329e12 | 3527 | return io_prep_rw(req, sqe, WRITE); |
f67676d1 JA |
3528 | } |
3529 | ||
889fca73 | 3530 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3531 | { |
3532 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3533 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3534 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3535 | struct io_async_rw *rw = req->async_data; |
45d189c6 | 3536 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
cd658695 JA |
3537 | struct iov_iter_state __state, *state; |
3538 | ssize_t ret, ret2; | |
2b188cc1 | 3539 | |
2846c481 | 3540 | if (rw) { |
e8c2bc1f | 3541 | iter = &rw->iter; |
cd658695 JA |
3542 | state = &rw->iter_state; |
3543 | iov_iter_restore(iter, state); | |
2846c481 PB |
3544 | iovec = NULL; |
3545 | } else { | |
3546 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
3547 | if (ret < 0) | |
3548 | return ret; | |
cd658695 JA |
3549 | state = &__state; |
3550 | iov_iter_save_state(iter, state); | |
2846c481 | 3551 | } |
cd658695 | 3552 | req->result = iov_iter_count(iter); |
2b188cc1 | 3553 | |
fd6c2e4c JA |
3554 | /* Ensure we clear previously set non-block flag */ |
3555 | if (!force_nonblock) | |
a88fc400 PB |
3556 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3557 | else | |
3558 | kiocb->ki_flags |= IOCB_NOWAIT; | |
fd6c2e4c | 3559 | |
24c74678 | 3560 | /* If the file doesn't support async, just async punt */ |
b191e2df | 3561 | if (force_nonblock && !io_file_supports_nowait(req, WRITE)) |
f67676d1 | 3562 | goto copy_iov; |
31b51510 | 3563 | |
10d59345 JA |
3564 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3565 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3566 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3567 | goto copy_iov; |
31b51510 | 3568 | |
cd658695 | 3569 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), req->result); |
fa15bafb PB |
3570 | if (unlikely(ret)) |
3571 | goto out_free; | |
4ed734b0 | 3572 | |
fa15bafb PB |
3573 | /* |
3574 | * Open-code file_start_write here to grab freeze protection, | |
3575 | * which will be released by another thread in | |
3576 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3577 | * released so that it doesn't complain about the held lock when | |
3578 | * we return to userspace. | |
3579 | */ | |
3580 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3581 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3582 | __sb_writers_release(file_inode(req->file)->i_sb, |
3583 | SB_FREEZE_WRITE); | |
3584 | } | |
3585 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3586 | |
fa15bafb | 3587 | if (req->file->f_op->write_iter) |
ff6165b2 | 3588 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3589 | else if (req->file->f_op->write) |
4017eb91 | 3590 | ret2 = loop_rw_iter(WRITE, req, iter); |
2dd2111d GH |
3591 | else |
3592 | ret2 = -EINVAL; | |
4ed734b0 | 3593 | |
6ad7f233 PB |
3594 | if (req->flags & REQ_F_REISSUE) { |
3595 | req->flags &= ~REQ_F_REISSUE; | |
230d50d4 | 3596 | ret2 = -EAGAIN; |
6ad7f233 | 3597 | } |
230d50d4 | 3598 | |
fa15bafb PB |
3599 | /* |
3600 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3601 | * retry them without IOCB_NOWAIT. | |
3602 | */ | |
3603 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3604 | ret2 = -EAGAIN; | |
75c668cd PB |
3605 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3606 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3607 | goto done; |
fa15bafb | 3608 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3609 | /* IOPOLL retry should happen for io-wq threads */ |
3610 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3611 | goto copy_iov; | |
355afaeb | 3612 | done: |
889fca73 | 3613 | kiocb_done(kiocb, ret2, issue_flags); |
fa15bafb | 3614 | } else { |
f67676d1 | 3615 | copy_iov: |
cd658695 | 3616 | iov_iter_restore(iter, state); |
227c0c96 | 3617 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
6bf985dc | 3618 | return ret ?: -EAGAIN; |
2b188cc1 | 3619 | } |
31b51510 | 3620 | out_free: |
f261c168 | 3621 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3622 | if (iovec) |
6f2cc166 | 3623 | kfree(iovec); |
2b188cc1 JA |
3624 | return ret; |
3625 | } | |
3626 | ||
80a261fd JA |
3627 | static int io_renameat_prep(struct io_kiocb *req, |
3628 | const struct io_uring_sqe *sqe) | |
3629 | { | |
3630 | struct io_rename *ren = &req->rename; | |
3631 | const char __user *oldf, *newf; | |
3632 | ||
ed7eb259 JA |
3633 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3634 | return -EINVAL; | |
26578cda | 3635 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
ed7eb259 | 3636 | return -EINVAL; |
80a261fd JA |
3637 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3638 | return -EBADF; | |
3639 | ||
3640 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3641 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3642 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3643 | ren->new_dfd = READ_ONCE(sqe->len); | |
3644 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3645 | ||
3646 | ren->oldpath = getname(oldf); | |
3647 | if (IS_ERR(ren->oldpath)) | |
3648 | return PTR_ERR(ren->oldpath); | |
3649 | ||
3650 | ren->newpath = getname(newf); | |
3651 | if (IS_ERR(ren->newpath)) { | |
3652 | putname(ren->oldpath); | |
3653 | return PTR_ERR(ren->newpath); | |
3654 | } | |
3655 | ||
3656 | req->flags |= REQ_F_NEED_CLEANUP; | |
3657 | return 0; | |
3658 | } | |
3659 | ||
45d189c6 | 3660 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
3661 | { |
3662 | struct io_rename *ren = &req->rename; | |
3663 | int ret; | |
3664 | ||
45d189c6 | 3665 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
3666 | return -EAGAIN; |
3667 | ||
3668 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3669 | ren->newpath, ren->flags); | |
3670 | ||
3671 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3672 | if (ret < 0) | |
93d2bcd2 | 3673 | req_set_fail(req); |
80a261fd JA |
3674 | io_req_complete(req, ret); |
3675 | return 0; | |
3676 | } | |
3677 | ||
14a1143b JA |
3678 | static int io_unlinkat_prep(struct io_kiocb *req, |
3679 | const struct io_uring_sqe *sqe) | |
3680 | { | |
3681 | struct io_unlink *un = &req->unlink; | |
3682 | const char __user *fname; | |
3683 | ||
22634bc5 JA |
3684 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3685 | return -EINVAL; | |
26578cda PB |
3686 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
3687 | sqe->splice_fd_in) | |
22634bc5 | 3688 | return -EINVAL; |
14a1143b JA |
3689 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3690 | return -EBADF; | |
3691 | ||
3692 | un->dfd = READ_ONCE(sqe->fd); | |
3693 | ||
3694 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3695 | if (un->flags & ~AT_REMOVEDIR) | |
3696 | return -EINVAL; | |
3697 | ||
3698 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3699 | un->filename = getname(fname); | |
3700 | if (IS_ERR(un->filename)) | |
3701 | return PTR_ERR(un->filename); | |
3702 | ||
3703 | req->flags |= REQ_F_NEED_CLEANUP; | |
3704 | return 0; | |
3705 | } | |
3706 | ||
45d189c6 | 3707 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
3708 | { |
3709 | struct io_unlink *un = &req->unlink; | |
3710 | int ret; | |
3711 | ||
45d189c6 | 3712 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
3713 | return -EAGAIN; |
3714 | ||
3715 | if (un->flags & AT_REMOVEDIR) | |
3716 | ret = do_rmdir(un->dfd, un->filename); | |
3717 | else | |
3718 | ret = do_unlinkat(un->dfd, un->filename); | |
3719 | ||
3720 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3721 | if (ret < 0) | |
93d2bcd2 | 3722 | req_set_fail(req); |
14a1143b JA |
3723 | io_req_complete(req, ret); |
3724 | return 0; | |
3725 | } | |
3726 | ||
e34a02dc DK |
3727 | static int io_mkdirat_prep(struct io_kiocb *req, |
3728 | const struct io_uring_sqe *sqe) | |
3729 | { | |
3730 | struct io_mkdir *mkd = &req->mkdir; | |
3731 | const char __user *fname; | |
3732 | ||
3733 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3734 | return -EINVAL; | |
3735 | if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index || | |
3736 | sqe->splice_fd_in) | |
3737 | return -EINVAL; | |
3738 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3739 | return -EBADF; | |
3740 | ||
3741 | mkd->dfd = READ_ONCE(sqe->fd); | |
3742 | mkd->mode = READ_ONCE(sqe->len); | |
3743 | ||
3744 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3745 | mkd->filename = getname(fname); | |
3746 | if (IS_ERR(mkd->filename)) | |
3747 | return PTR_ERR(mkd->filename); | |
3748 | ||
3749 | req->flags |= REQ_F_NEED_CLEANUP; | |
3750 | return 0; | |
3751 | } | |
3752 | ||
3753 | static int io_mkdirat(struct io_kiocb *req, int issue_flags) | |
3754 | { | |
3755 | struct io_mkdir *mkd = &req->mkdir; | |
3756 | int ret; | |
3757 | ||
3758 | if (issue_flags & IO_URING_F_NONBLOCK) | |
3759 | return -EAGAIN; | |
3760 | ||
3761 | ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode); | |
3762 | ||
3763 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3764 | if (ret < 0) | |
3765 | req_set_fail(req); | |
3766 | io_req_complete(req, ret); | |
3767 | return 0; | |
3768 | } | |
3769 | ||
7a8721f8 DK |
3770 | static int io_symlinkat_prep(struct io_kiocb *req, |
3771 | const struct io_uring_sqe *sqe) | |
3772 | { | |
3773 | struct io_symlink *sl = &req->symlink; | |
3774 | const char __user *oldpath, *newpath; | |
3775 | ||
3776 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3777 | return -EINVAL; | |
3778 | if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index || | |
3779 | sqe->splice_fd_in) | |
3780 | return -EINVAL; | |
3781 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3782 | return -EBADF; | |
3783 | ||
3784 | sl->new_dfd = READ_ONCE(sqe->fd); | |
3785 | oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3786 | newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3787 | ||
3788 | sl->oldpath = getname(oldpath); | |
3789 | if (IS_ERR(sl->oldpath)) | |
3790 | return PTR_ERR(sl->oldpath); | |
3791 | ||
3792 | sl->newpath = getname(newpath); | |
3793 | if (IS_ERR(sl->newpath)) { | |
3794 | putname(sl->oldpath); | |
3795 | return PTR_ERR(sl->newpath); | |
3796 | } | |
3797 | ||
3798 | req->flags |= REQ_F_NEED_CLEANUP; | |
3799 | return 0; | |
3800 | } | |
3801 | ||
3802 | static int io_symlinkat(struct io_kiocb *req, int issue_flags) | |
3803 | { | |
3804 | struct io_symlink *sl = &req->symlink; | |
3805 | int ret; | |
3806 | ||
3807 | if (issue_flags & IO_URING_F_NONBLOCK) | |
3808 | return -EAGAIN; | |
3809 | ||
3810 | ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath); | |
3811 | ||
3812 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3813 | if (ret < 0) | |
3814 | req_set_fail(req); | |
3815 | io_req_complete(req, ret); | |
3816 | return 0; | |
3817 | } | |
3818 | ||
cf30da90 DK |
3819 | static int io_linkat_prep(struct io_kiocb *req, |
3820 | const struct io_uring_sqe *sqe) | |
3821 | { | |
3822 | struct io_hardlink *lnk = &req->hardlink; | |
3823 | const char __user *oldf, *newf; | |
3824 | ||
3825 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3826 | return -EINVAL; | |
3827 | if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) | |
3828 | return -EINVAL; | |
3829 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3830 | return -EBADF; | |
3831 | ||
3832 | lnk->old_dfd = READ_ONCE(sqe->fd); | |
3833 | lnk->new_dfd = READ_ONCE(sqe->len); | |
3834 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3835 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3836 | lnk->flags = READ_ONCE(sqe->hardlink_flags); | |
3837 | ||
3838 | lnk->oldpath = getname(oldf); | |
3839 | if (IS_ERR(lnk->oldpath)) | |
3840 | return PTR_ERR(lnk->oldpath); | |
3841 | ||
3842 | lnk->newpath = getname(newf); | |
3843 | if (IS_ERR(lnk->newpath)) { | |
3844 | putname(lnk->oldpath); | |
3845 | return PTR_ERR(lnk->newpath); | |
3846 | } | |
3847 | ||
3848 | req->flags |= REQ_F_NEED_CLEANUP; | |
3849 | return 0; | |
3850 | } | |
3851 | ||
3852 | static int io_linkat(struct io_kiocb *req, int issue_flags) | |
3853 | { | |
3854 | struct io_hardlink *lnk = &req->hardlink; | |
3855 | int ret; | |
3856 | ||
3857 | if (issue_flags & IO_URING_F_NONBLOCK) | |
3858 | return -EAGAIN; | |
3859 | ||
3860 | ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd, | |
3861 | lnk->newpath, lnk->flags); | |
3862 | ||
3863 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3864 | if (ret < 0) | |
3865 | req_set_fail(req); | |
3866 | io_req_complete(req, ret); | |
3867 | return 0; | |
3868 | } | |
3869 | ||
36f4fa68 JA |
3870 | static int io_shutdown_prep(struct io_kiocb *req, |
3871 | const struct io_uring_sqe *sqe) | |
3872 | { | |
3873 | #if defined(CONFIG_NET) | |
3874 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3875 | return -EINVAL; | |
26578cda PB |
3876 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
3877 | sqe->buf_index || sqe->splice_fd_in)) | |
36f4fa68 JA |
3878 | return -EINVAL; |
3879 | ||
3880 | req->shutdown.how = READ_ONCE(sqe->len); | |
3881 | return 0; | |
3882 | #else | |
3883 | return -EOPNOTSUPP; | |
3884 | #endif | |
3885 | } | |
3886 | ||
45d189c6 | 3887 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
3888 | { |
3889 | #if defined(CONFIG_NET) | |
3890 | struct socket *sock; | |
3891 | int ret; | |
3892 | ||
45d189c6 | 3893 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
3894 | return -EAGAIN; |
3895 | ||
48aba79b | 3896 | sock = sock_from_file(req->file); |
36f4fa68 | 3897 | if (unlikely(!sock)) |
48aba79b | 3898 | return -ENOTSOCK; |
36f4fa68 JA |
3899 | |
3900 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d | 3901 | if (ret < 0) |
93d2bcd2 | 3902 | req_set_fail(req); |
36f4fa68 JA |
3903 | io_req_complete(req, ret); |
3904 | return 0; | |
3905 | #else | |
3906 | return -EOPNOTSUPP; | |
3907 | #endif | |
3908 | } | |
3909 | ||
f2a8d5c7 PB |
3910 | static int __io_splice_prep(struct io_kiocb *req, |
3911 | const struct io_uring_sqe *sqe) | |
7d67af2c | 3912 | { |
fe7e3257 | 3913 | struct io_splice *sp = &req->splice; |
7d67af2c | 3914 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
7d67af2c | 3915 | |
3232dd02 PB |
3916 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3917 | return -EINVAL; | |
7d67af2c PB |
3918 | |
3919 | sp->file_in = NULL; | |
7d67af2c PB |
3920 | sp->len = READ_ONCE(sqe->len); |
3921 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3922 | ||
3923 | if (unlikely(sp->flags & ~valid_flags)) | |
3924 | return -EINVAL; | |
3925 | ||
62906e89 | 3926 | sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in), |
8371adf5 PB |
3927 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
3928 | if (!sp->file_in) | |
3929 | return -EBADF; | |
7d67af2c | 3930 | req->flags |= REQ_F_NEED_CLEANUP; |
7d67af2c PB |
3931 | return 0; |
3932 | } | |
3933 | ||
f2a8d5c7 PB |
3934 | static int io_tee_prep(struct io_kiocb *req, |
3935 | const struct io_uring_sqe *sqe) | |
3936 | { | |
3937 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3938 | return -EINVAL; | |
3939 | return __io_splice_prep(req, sqe); | |
3940 | } | |
3941 | ||
45d189c6 | 3942 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
3943 | { |
3944 | struct io_splice *sp = &req->splice; | |
3945 | struct file *in = sp->file_in; | |
3946 | struct file *out = sp->file_out; | |
3947 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3948 | long ret = 0; | |
3949 | ||
45d189c6 | 3950 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
3951 | return -EAGAIN; |
3952 | if (sp->len) | |
3953 | ret = do_tee(in, out, sp->len, flags); | |
3954 | ||
e1d767f0 PB |
3955 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
3956 | io_put_file(in); | |
f2a8d5c7 PB |
3957 | req->flags &= ~REQ_F_NEED_CLEANUP; |
3958 | ||
f2a8d5c7 | 3959 | if (ret != sp->len) |
93d2bcd2 | 3960 | req_set_fail(req); |
e1e16097 | 3961 | io_req_complete(req, ret); |
f2a8d5c7 PB |
3962 | return 0; |
3963 | } | |
3964 | ||
3965 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
3966 | { | |
fe7e3257 | 3967 | struct io_splice *sp = &req->splice; |
f2a8d5c7 PB |
3968 | |
3969 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
3970 | sp->off_out = READ_ONCE(sqe->off); | |
3971 | return __io_splice_prep(req, sqe); | |
3972 | } | |
3973 | ||
45d189c6 | 3974 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
3975 | { |
3976 | struct io_splice *sp = &req->splice; | |
3977 | struct file *in = sp->file_in; | |
3978 | struct file *out = sp->file_out; | |
3979 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3980 | loff_t *poff_in, *poff_out; | |
c9687426 | 3981 | long ret = 0; |
7d67af2c | 3982 | |
45d189c6 | 3983 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 3984 | return -EAGAIN; |
7d67af2c PB |
3985 | |
3986 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
3987 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 3988 | |
948a7749 | 3989 | if (sp->len) |
c9687426 | 3990 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c | 3991 | |
e1d767f0 PB |
3992 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
3993 | io_put_file(in); | |
7d67af2c PB |
3994 | req->flags &= ~REQ_F_NEED_CLEANUP; |
3995 | ||
7d67af2c | 3996 | if (ret != sp->len) |
93d2bcd2 | 3997 | req_set_fail(req); |
e1e16097 | 3998 | io_req_complete(req, ret); |
7d67af2c PB |
3999 | return 0; |
4000 | } | |
4001 | ||
2b188cc1 JA |
4002 | /* |
4003 | * IORING_OP_NOP just posts a completion event, nothing else. | |
4004 | */ | |
889fca73 | 4005 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
4006 | { |
4007 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 4008 | |
def596e9 JA |
4009 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4010 | return -EINVAL; | |
4011 | ||
889fca73 | 4012 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
4013 | return 0; |
4014 | } | |
4015 | ||
1155c76a | 4016 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 4017 | { |
6b06314c | 4018 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 4019 | |
09bb8394 JA |
4020 | if (!req->file) |
4021 | return -EBADF; | |
c992fe29 | 4022 | |
6b06314c | 4023 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 4024 | return -EINVAL; |
26578cda PB |
4025 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4026 | sqe->splice_fd_in)) | |
c992fe29 CH |
4027 | return -EINVAL; |
4028 | ||
8ed8d3c3 JA |
4029 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
4030 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
4031 | return -EINVAL; | |
4032 | ||
4033 | req->sync.off = READ_ONCE(sqe->off); | |
4034 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
4035 | return 0; |
4036 | } | |
4037 | ||
45d189c6 | 4038 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4039 | { |
8ed8d3c3 | 4040 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
4041 | int ret; |
4042 | ||
ac45abc0 | 4043 | /* fsync always requires a blocking context */ |
45d189c6 | 4044 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4045 | return -EAGAIN; |
4046 | ||
9adbd45d | 4047 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
4048 | end > 0 ? end : LLONG_MAX, |
4049 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
4050 | if (ret < 0) | |
93d2bcd2 | 4051 | req_set_fail(req); |
e1e16097 | 4052 | io_req_complete(req, ret); |
c992fe29 CH |
4053 | return 0; |
4054 | } | |
4055 | ||
d63d1b5e JA |
4056 | static int io_fallocate_prep(struct io_kiocb *req, |
4057 | const struct io_uring_sqe *sqe) | |
4058 | { | |
26578cda PB |
4059 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
4060 | sqe->splice_fd_in) | |
d63d1b5e | 4061 | return -EINVAL; |
3232dd02 PB |
4062 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4063 | return -EINVAL; | |
d63d1b5e JA |
4064 | |
4065 | req->sync.off = READ_ONCE(sqe->off); | |
4066 | req->sync.len = READ_ONCE(sqe->addr); | |
4067 | req->sync.mode = READ_ONCE(sqe->len); | |
4068 | return 0; | |
4069 | } | |
4070 | ||
45d189c6 | 4071 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 4072 | { |
ac45abc0 PB |
4073 | int ret; |
4074 | ||
d63d1b5e | 4075 | /* fallocate always requiring blocking context */ |
45d189c6 | 4076 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 4077 | return -EAGAIN; |
ac45abc0 PB |
4078 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
4079 | req->sync.len); | |
ac45abc0 | 4080 | if (ret < 0) |
93d2bcd2 | 4081 | req_set_fail(req); |
e1e16097 | 4082 | io_req_complete(req, ret); |
5d17b4a4 JA |
4083 | return 0; |
4084 | } | |
4085 | ||
ec65fea5 | 4086 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 4087 | { |
f8748881 | 4088 | const char __user *fname; |
15b71abe | 4089 | int ret; |
b7bb4f7d | 4090 | |
d3fddf6d PB |
4091 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4092 | return -EINVAL; | |
b9445598 | 4093 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 4094 | return -EINVAL; |
ec65fea5 | 4095 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 4096 | return -EBADF; |
03b1230c | 4097 | |
ec65fea5 PB |
4098 | /* open.how should be already initialised */ |
4099 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 4100 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 4101 | |
25e72d10 PB |
4102 | req->open.dfd = READ_ONCE(sqe->fd); |
4103 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 4104 | req->open.filename = getname(fname); |
15b71abe JA |
4105 | if (IS_ERR(req->open.filename)) { |
4106 | ret = PTR_ERR(req->open.filename); | |
4107 | req->open.filename = NULL; | |
4108 | return ret; | |
4109 | } | |
b9445598 PB |
4110 | |
4111 | req->open.file_slot = READ_ONCE(sqe->file_index); | |
4112 | if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) | |
4113 | return -EINVAL; | |
4114 | ||
4022e7af | 4115 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 4116 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 4117 | return 0; |
03b1230c JA |
4118 | } |
4119 | ||
ec65fea5 PB |
4120 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4121 | { | |
d3fddf6d PB |
4122 | u64 mode = READ_ONCE(sqe->len); |
4123 | u64 flags = READ_ONCE(sqe->open_flags); | |
ec65fea5 | 4124 | |
ec65fea5 PB |
4125 | req->open.how = build_open_how(flags, mode); |
4126 | return __io_openat_prep(req, sqe); | |
4127 | } | |
4128 | ||
cebdb986 | 4129 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 4130 | { |
cebdb986 | 4131 | struct open_how __user *how; |
cebdb986 | 4132 | size_t len; |
0fa03c62 JA |
4133 | int ret; |
4134 | ||
cebdb986 JA |
4135 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4136 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
4137 | if (len < OPEN_HOW_SIZE_VER0) |
4138 | return -EINVAL; | |
3529d8c2 | 4139 | |
cebdb986 JA |
4140 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
4141 | len); | |
4142 | if (ret) | |
4143 | return ret; | |
3529d8c2 | 4144 | |
ec65fea5 | 4145 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
4146 | } |
4147 | ||
45d189c6 | 4148 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
4149 | { |
4150 | struct open_flags op; | |
15b71abe | 4151 | struct file *file; |
b9445598 PB |
4152 | bool resolve_nonblock, nonblock_set; |
4153 | bool fixed = !!req->open.file_slot; | |
15b71abe JA |
4154 | int ret; |
4155 | ||
cebdb986 | 4156 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
4157 | if (ret) |
4158 | goto err; | |
3a81fd02 JA |
4159 | nonblock_set = op.open_flag & O_NONBLOCK; |
4160 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 4161 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
4162 | /* |
4163 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
4164 | * it'll always -EAGAIN | |
4165 | */ | |
4166 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
4167 | return -EAGAIN; | |
4168 | op.lookup_flags |= LOOKUP_CACHED; | |
4169 | op.open_flag |= O_NONBLOCK; | |
4170 | } | |
15b71abe | 4171 | |
b9445598 PB |
4172 | if (!fixed) { |
4173 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); | |
4174 | if (ret < 0) | |
4175 | goto err; | |
4176 | } | |
15b71abe JA |
4177 | |
4178 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
12dcb58a | 4179 | if (IS_ERR(file)) { |
944d1444 | 4180 | /* |
12dcb58a PB |
4181 | * We could hang on to this 'fd' on retrying, but seems like |
4182 | * marginal gain for something that is now known to be a slower | |
4183 | * path. So just put it, and we'll get a new one when we retry. | |
944d1444 | 4184 | */ |
b9445598 PB |
4185 | if (!fixed) |
4186 | put_unused_fd(ret); | |
3a81fd02 | 4187 | |
15b71abe | 4188 | ret = PTR_ERR(file); |
12dcb58a PB |
4189 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
4190 | if (ret == -EAGAIN && | |
4191 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) | |
4192 | return -EAGAIN; | |
4193 | goto err; | |
15b71abe | 4194 | } |
12dcb58a PB |
4195 | |
4196 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) | |
4197 | file->f_flags &= ~O_NONBLOCK; | |
4198 | fsnotify_open(file); | |
b9445598 PB |
4199 | |
4200 | if (!fixed) | |
4201 | fd_install(ret, file); | |
4202 | else | |
4203 | ret = io_install_fixed_file(req, file, issue_flags, | |
4204 | req->open.file_slot - 1); | |
15b71abe JA |
4205 | err: |
4206 | putname(req->open.filename); | |
8fef80bf | 4207 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe | 4208 | if (ret < 0) |
93d2bcd2 | 4209 | req_set_fail(req); |
0bdf3398 | 4210 | __io_req_complete(req, issue_flags, ret, 0); |
15b71abe JA |
4211 | return 0; |
4212 | } | |
4213 | ||
45d189c6 | 4214 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 4215 | { |
e45cff58 | 4216 | return io_openat2(req, issue_flags); |
cebdb986 JA |
4217 | } |
4218 | ||
067524e9 JA |
4219 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4220 | const struct io_uring_sqe *sqe) | |
4221 | { | |
4222 | struct io_provide_buf *p = &req->pbuf; | |
4223 | u64 tmp; | |
4224 | ||
26578cda PB |
4225 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
4226 | sqe->splice_fd_in) | |
067524e9 JA |
4227 | return -EINVAL; |
4228 | ||
4229 | tmp = READ_ONCE(sqe->fd); | |
4230 | if (!tmp || tmp > USHRT_MAX) | |
4231 | return -EINVAL; | |
4232 | ||
4233 | memset(p, 0, sizeof(*p)); | |
4234 | p->nbufs = tmp; | |
4235 | p->bgid = READ_ONCE(sqe->buf_group); | |
4236 | return 0; | |
4237 | } | |
4238 | ||
4239 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
4240 | int bgid, unsigned nbufs) | |
4241 | { | |
4242 | unsigned i = 0; | |
4243 | ||
4244 | /* shouldn't happen */ | |
4245 | if (!nbufs) | |
4246 | return 0; | |
4247 | ||
4248 | /* the head kbuf is the list itself */ | |
4249 | while (!list_empty(&buf->list)) { | |
4250 | struct io_buffer *nxt; | |
4251 | ||
4252 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
4253 | list_del(&nxt->list); | |
4254 | kfree(nxt); | |
4255 | if (++i == nbufs) | |
4256 | return i; | |
4257 | } | |
4258 | i++; | |
4259 | kfree(buf); | |
9e15c3a0 | 4260 | xa_erase(&ctx->io_buffers, bgid); |
067524e9 JA |
4261 | |
4262 | return i; | |
4263 | } | |
4264 | ||
889fca73 | 4265 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
4266 | { |
4267 | struct io_provide_buf *p = &req->pbuf; | |
4268 | struct io_ring_ctx *ctx = req->ctx; | |
4269 | struct io_buffer *head; | |
4270 | int ret = 0; | |
45d189c6 | 4271 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
067524e9 JA |
4272 | |
4273 | io_ring_submit_lock(ctx, !force_nonblock); | |
4274 | ||
4275 | lockdep_assert_held(&ctx->uring_lock); | |
4276 | ||
4277 | ret = -ENOENT; | |
9e15c3a0 | 4278 | head = xa_load(&ctx->io_buffers, p->bgid); |
067524e9 JA |
4279 | if (head) |
4280 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 | 4281 | if (ret < 0) |
93d2bcd2 | 4282 | req_set_fail(req); |
067524e9 | 4283 | |
9fb8cb49 PB |
4284 | /* complete before unlock, IOPOLL may need the lock */ |
4285 | __io_req_complete(req, issue_flags, ret, 0); | |
4286 | io_ring_submit_unlock(ctx, !force_nonblock); | |
067524e9 JA |
4287 | return 0; |
4288 | } | |
4289 | ||
ddf0322d JA |
4290 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4291 | const struct io_uring_sqe *sqe) | |
4292 | { | |
38134ada | 4293 | unsigned long size, tmp_check; |
ddf0322d JA |
4294 | struct io_provide_buf *p = &req->pbuf; |
4295 | u64 tmp; | |
4296 | ||
26578cda | 4297 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
ddf0322d JA |
4298 | return -EINVAL; |
4299 | ||
4300 | tmp = READ_ONCE(sqe->fd); | |
4301 | if (!tmp || tmp > USHRT_MAX) | |
4302 | return -E2BIG; | |
4303 | p->nbufs = tmp; | |
4304 | p->addr = READ_ONCE(sqe->addr); | |
4305 | p->len = READ_ONCE(sqe->len); | |
4306 | ||
38134ada PB |
4307 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
4308 | &size)) | |
4309 | return -EOVERFLOW; | |
4310 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) | |
4311 | return -EOVERFLOW; | |
4312 | ||
d81269fe PB |
4313 | size = (unsigned long)p->len * p->nbufs; |
4314 | if (!access_ok(u64_to_user_ptr(p->addr), size)) | |
ddf0322d JA |
4315 | return -EFAULT; |
4316 | ||
4317 | p->bgid = READ_ONCE(sqe->buf_group); | |
4318 | tmp = READ_ONCE(sqe->off); | |
4319 | if (tmp > USHRT_MAX) | |
4320 | return -E2BIG; | |
4321 | p->bid = tmp; | |
4322 | return 0; | |
4323 | } | |
4324 | ||
4325 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
4326 | { | |
4327 | struct io_buffer *buf; | |
4328 | u64 addr = pbuf->addr; | |
4329 | int i, bid = pbuf->bid; | |
4330 | ||
4331 | for (i = 0; i < pbuf->nbufs; i++) { | |
9990da93 | 4332 | buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT); |
ddf0322d JA |
4333 | if (!buf) |
4334 | break; | |
4335 | ||
4336 | buf->addr = addr; | |
d1f82808 | 4337 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
ddf0322d JA |
4338 | buf->bid = bid; |
4339 | addr += pbuf->len; | |
4340 | bid++; | |
4341 | if (!*head) { | |
4342 | INIT_LIST_HEAD(&buf->list); | |
4343 | *head = buf; | |
4344 | } else { | |
4345 | list_add_tail(&buf->list, &(*head)->list); | |
4346 | } | |
4347 | } | |
4348 | ||
4349 | return i ? i : -ENOMEM; | |
4350 | } | |
4351 | ||
889fca73 | 4352 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4353 | { |
4354 | struct io_provide_buf *p = &req->pbuf; | |
4355 | struct io_ring_ctx *ctx = req->ctx; | |
4356 | struct io_buffer *head, *list; | |
4357 | int ret = 0; | |
45d189c6 | 4358 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ddf0322d JA |
4359 | |
4360 | io_ring_submit_lock(ctx, !force_nonblock); | |
4361 | ||
4362 | lockdep_assert_held(&ctx->uring_lock); | |
4363 | ||
9e15c3a0 | 4364 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
ddf0322d JA |
4365 | |
4366 | ret = io_add_buffers(p, &head); | |
9e15c3a0 JA |
4367 | if (ret >= 0 && !list) { |
4368 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); | |
4369 | if (ret < 0) | |
067524e9 | 4370 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d | 4371 | } |
ddf0322d | 4372 | if (ret < 0) |
93d2bcd2 | 4373 | req_set_fail(req); |
9fb8cb49 PB |
4374 | /* complete before unlock, IOPOLL may need the lock */ |
4375 | __io_req_complete(req, issue_flags, ret, 0); | |
4376 | io_ring_submit_unlock(ctx, !force_nonblock); | |
ddf0322d | 4377 | return 0; |
cebdb986 JA |
4378 | } |
4379 | ||
3e4827b0 JA |
4380 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4381 | const struct io_uring_sqe *sqe) | |
4382 | { | |
4383 | #if defined(CONFIG_EPOLL) | |
26578cda | 4384 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
3e4827b0 | 4385 | return -EINVAL; |
2d74d042 | 4386 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4387 | return -EINVAL; |
3e4827b0 JA |
4388 | |
4389 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4390 | req->epoll.op = READ_ONCE(sqe->len); | |
4391 | req->epoll.fd = READ_ONCE(sqe->off); | |
4392 | ||
4393 | if (ep_op_has_event(req->epoll.op)) { | |
4394 | struct epoll_event __user *ev; | |
4395 | ||
4396 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4397 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4398 | return -EFAULT; | |
4399 | } | |
4400 | ||
4401 | return 0; | |
4402 | #else | |
4403 | return -EOPNOTSUPP; | |
4404 | #endif | |
4405 | } | |
4406 | ||
889fca73 | 4407 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4408 | { |
4409 | #if defined(CONFIG_EPOLL) | |
4410 | struct io_epoll *ie = &req->epoll; | |
4411 | int ret; | |
45d189c6 | 4412 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4413 | |
4414 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4415 | if (force_nonblock && ret == -EAGAIN) | |
4416 | return -EAGAIN; | |
4417 | ||
4418 | if (ret < 0) | |
93d2bcd2 | 4419 | req_set_fail(req); |
889fca73 | 4420 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4421 | return 0; |
4422 | #else | |
4423 | return -EOPNOTSUPP; | |
4424 | #endif | |
4425 | } | |
4426 | ||
c1ca757b JA |
4427 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4428 | { | |
4429 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
26578cda | 4430 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
c1ca757b | 4431 | return -EINVAL; |
3232dd02 PB |
4432 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4433 | return -EINVAL; | |
c1ca757b JA |
4434 | |
4435 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4436 | req->madvise.len = READ_ONCE(sqe->len); | |
4437 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4438 | return 0; | |
4439 | #else | |
4440 | return -EOPNOTSUPP; | |
4441 | #endif | |
4442 | } | |
4443 | ||
45d189c6 | 4444 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4445 | { |
4446 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4447 | struct io_madvise *ma = &req->madvise; | |
4448 | int ret; | |
4449 | ||
45d189c6 | 4450 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4451 | return -EAGAIN; |
4452 | ||
0726b01e | 4453 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b | 4454 | if (ret < 0) |
93d2bcd2 | 4455 | req_set_fail(req); |
e1e16097 | 4456 | io_req_complete(req, ret); |
c1ca757b JA |
4457 | return 0; |
4458 | #else | |
4459 | return -EOPNOTSUPP; | |
4460 | #endif | |
4461 | } | |
4462 | ||
4840e418 JA |
4463 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4464 | { | |
26578cda | 4465 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
4840e418 | 4466 | return -EINVAL; |
3232dd02 PB |
4467 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4468 | return -EINVAL; | |
4840e418 JA |
4469 | |
4470 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4471 | req->fadvise.len = READ_ONCE(sqe->len); | |
4472 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4473 | return 0; | |
4474 | } | |
4475 | ||
45d189c6 | 4476 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4477 | { |
4478 | struct io_fadvise *fa = &req->fadvise; | |
4479 | int ret; | |
4480 | ||
45d189c6 | 4481 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4482 | switch (fa->advice) { |
4483 | case POSIX_FADV_NORMAL: | |
4484 | case POSIX_FADV_RANDOM: | |
4485 | case POSIX_FADV_SEQUENTIAL: | |
4486 | break; | |
4487 | default: | |
4488 | return -EAGAIN; | |
4489 | } | |
4490 | } | |
4840e418 JA |
4491 | |
4492 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4493 | if (ret < 0) | |
93d2bcd2 | 4494 | req_set_fail(req); |
0bdf3398 | 4495 | __io_req_complete(req, issue_flags, ret, 0); |
4840e418 JA |
4496 | return 0; |
4497 | } | |
4498 | ||
eddc7ef5 JA |
4499 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4500 | { | |
2d74d042 | 4501 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4502 | return -EINVAL; |
26578cda | 4503 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
eddc7ef5 | 4504 | return -EINVAL; |
9c280f90 | 4505 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4506 | return -EBADF; |
eddc7ef5 | 4507 | |
1d9e1288 BM |
4508 | req->statx.dfd = READ_ONCE(sqe->fd); |
4509 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4510 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4511 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4512 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4513 | |
4514 | return 0; | |
4515 | } | |
4516 | ||
45d189c6 | 4517 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4518 | { |
1d9e1288 | 4519 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4520 | int ret; |
4521 | ||
59d70013 | 4522 | if (issue_flags & IO_URING_F_NONBLOCK) |
eddc7ef5 JA |
4523 | return -EAGAIN; |
4524 | ||
e62753e4 BM |
4525 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4526 | ctx->buffer); | |
eddc7ef5 | 4527 | |
eddc7ef5 | 4528 | if (ret < 0) |
93d2bcd2 | 4529 | req_set_fail(req); |
e1e16097 | 4530 | io_req_complete(req, ret); |
eddc7ef5 JA |
4531 | return 0; |
4532 | } | |
4533 | ||
b5dba59e JA |
4534 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4535 | { | |
14587a46 | 4536 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4537 | return -EINVAL; |
b5dba59e | 4538 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
7df778be | 4539 | sqe->rw_flags || sqe->buf_index) |
b5dba59e | 4540 | return -EINVAL; |
9c280f90 | 4541 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4542 | return -EBADF; |
b5dba59e JA |
4543 | |
4544 | req->close.fd = READ_ONCE(sqe->fd); | |
7df778be PB |
4545 | req->close.file_slot = READ_ONCE(sqe->file_index); |
4546 | if (req->close.file_slot && req->close.fd) | |
4547 | return -EINVAL; | |
4548 | ||
b5dba59e | 4549 | return 0; |
b5dba59e JA |
4550 | } |
4551 | ||
889fca73 | 4552 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4553 | { |
9eac1904 | 4554 | struct files_struct *files = current->files; |
3af73b28 | 4555 | struct io_close *close = &req->close; |
9eac1904 | 4556 | struct fdtable *fdt; |
a1fde923 PB |
4557 | struct file *file = NULL; |
4558 | int ret = -EBADF; | |
b5dba59e | 4559 | |
7df778be PB |
4560 | if (req->close.file_slot) { |
4561 | ret = io_close_fixed(req, issue_flags); | |
4562 | goto err; | |
4563 | } | |
4564 | ||
9eac1904 JA |
4565 | spin_lock(&files->file_lock); |
4566 | fdt = files_fdtable(files); | |
4567 | if (close->fd >= fdt->max_fds) { | |
4568 | spin_unlock(&files->file_lock); | |
4569 | goto err; | |
4570 | } | |
4571 | file = fdt->fd[close->fd]; | |
a1fde923 | 4572 | if (!file || file->f_op == &io_uring_fops) { |
9eac1904 JA |
4573 | spin_unlock(&files->file_lock); |
4574 | file = NULL; | |
4575 | goto err; | |
3af73b28 | 4576 | } |
b5dba59e JA |
4577 | |
4578 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 4579 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 4580 | spin_unlock(&files->file_lock); |
0bf0eefd | 4581 | return -EAGAIN; |
a2100672 | 4582 | } |
b5dba59e | 4583 | |
9eac1904 JA |
4584 | ret = __close_fd_get_file(close->fd, &file); |
4585 | spin_unlock(&files->file_lock); | |
4586 | if (ret < 0) { | |
4587 | if (ret == -ENOENT) | |
4588 | ret = -EBADF; | |
4589 | goto err; | |
4590 | } | |
4591 | ||
3af73b28 | 4592 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4593 | ret = filp_close(file, current->files); |
4594 | err: | |
3af73b28 | 4595 | if (ret < 0) |
93d2bcd2 | 4596 | req_set_fail(req); |
9eac1904 JA |
4597 | if (file) |
4598 | fput(file); | |
889fca73 | 4599 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 4600 | return 0; |
b5dba59e JA |
4601 | } |
4602 | ||
1155c76a | 4603 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4604 | { |
4605 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 4606 | |
5d17b4a4 JA |
4607 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4608 | return -EINVAL; | |
26578cda PB |
4609 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4610 | sqe->splice_fd_in)) | |
5d17b4a4 JA |
4611 | return -EINVAL; |
4612 | ||
8ed8d3c3 JA |
4613 | req->sync.off = READ_ONCE(sqe->off); |
4614 | req->sync.len = READ_ONCE(sqe->len); | |
4615 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4616 | return 0; |
4617 | } | |
4618 | ||
45d189c6 | 4619 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4620 | { |
8ed8d3c3 JA |
4621 | int ret; |
4622 | ||
ac45abc0 | 4623 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 4624 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4625 | return -EAGAIN; |
4626 | ||
9adbd45d | 4627 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4628 | req->sync.flags); |
4629 | if (ret < 0) | |
93d2bcd2 | 4630 | req_set_fail(req); |
e1e16097 | 4631 | io_req_complete(req, ret); |
5d17b4a4 JA |
4632 | return 0; |
4633 | } | |
4634 | ||
469956e8 | 4635 | #if defined(CONFIG_NET) |
02d27d89 PB |
4636 | static int io_setup_async_msg(struct io_kiocb *req, |
4637 | struct io_async_msghdr *kmsg) | |
4638 | { | |
e8c2bc1f JA |
4639 | struct io_async_msghdr *async_msg = req->async_data; |
4640 | ||
4641 | if (async_msg) | |
02d27d89 | 4642 | return -EAGAIN; |
e8c2bc1f | 4643 | if (io_alloc_async_data(req)) { |
257e84a5 | 4644 | kfree(kmsg->free_iov); |
02d27d89 PB |
4645 | return -ENOMEM; |
4646 | } | |
e8c2bc1f | 4647 | async_msg = req->async_data; |
02d27d89 | 4648 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4649 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 4650 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
4651 | /* if were using fast_iov, set it to the new one */ |
4652 | if (!async_msg->free_iov) | |
4653 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
4654 | ||
02d27d89 PB |
4655 | return -EAGAIN; |
4656 | } | |
4657 | ||
2ae523ed PB |
4658 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4659 | struct io_async_msghdr *iomsg) | |
4660 | { | |
2ae523ed | 4661 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 4662 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 4663 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 4664 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
4665 | } |
4666 | ||
93642ef8 PB |
4667 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
4668 | { | |
4669 | int ret; | |
4670 | ||
93642ef8 PB |
4671 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
4672 | if (!ret) | |
4673 | req->flags |= REQ_F_NEED_CLEANUP; | |
4674 | return ret; | |
4675 | } | |
4676 | ||
3529d8c2 | 4677 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4678 | { |
e47293fd | 4679 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 4680 | |
d2b6f48b PB |
4681 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4682 | return -EINVAL; | |
4683 | ||
270a5940 | 4684 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4685 | sr->len = READ_ONCE(sqe->len); |
04411806 PB |
4686 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4687 | if (sr->msg_flags & MSG_DONTWAIT) | |
4688 | req->flags |= REQ_F_NOWAIT; | |
3529d8c2 | 4689 | |
d8768362 JA |
4690 | #ifdef CONFIG_COMPAT |
4691 | if (req->ctx->compat) | |
4692 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4693 | #endif | |
93642ef8 | 4694 | return 0; |
03b1230c JA |
4695 | } |
4696 | ||
889fca73 | 4697 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4698 | { |
6b754c8b | 4699 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4700 | struct socket *sock; |
7a7cacba | 4701 | unsigned flags; |
0031275d | 4702 | int min_ret = 0; |
0fa03c62 JA |
4703 | int ret; |
4704 | ||
dba4a925 | 4705 | sock = sock_from_file(req->file); |
7a7cacba | 4706 | if (unlikely(!sock)) |
dba4a925 | 4707 | return -ENOTSOCK; |
3529d8c2 | 4708 | |
257e84a5 PB |
4709 | kmsg = req->async_data; |
4710 | if (!kmsg) { | |
7a7cacba PB |
4711 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
4712 | if (ret) | |
4713 | return ret; | |
4714 | kmsg = &iomsg; | |
0fa03c62 | 4715 | } |
0fa03c62 | 4716 | |
04411806 PB |
4717 | flags = req->sr_msg.msg_flags; |
4718 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4719 | flags |= MSG_DONTWAIT; |
0031275d SM |
4720 | if (flags & MSG_WAITALL) |
4721 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4722 | ||
7a7cacba | 4723 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
45d189c6 | 4724 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4725 | return io_setup_async_msg(req, kmsg); |
4726 | if (ret == -ERESTARTSYS) | |
4727 | ret = -EINTR; | |
0fa03c62 | 4728 | |
257e84a5 PB |
4729 | /* fast path, check for non-NULL to avoid function call */ |
4730 | if (kmsg->free_iov) | |
4731 | kfree(kmsg->free_iov); | |
99bc4c38 | 4732 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 4733 | if (ret < min_ret) |
93d2bcd2 | 4734 | req_set_fail(req); |
889fca73 | 4735 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 4736 | return 0; |
03b1230c | 4737 | } |
aa1fa28f | 4738 | |
889fca73 | 4739 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4740 | { |
7a7cacba PB |
4741 | struct io_sr_msg *sr = &req->sr_msg; |
4742 | struct msghdr msg; | |
4743 | struct iovec iov; | |
fddaface | 4744 | struct socket *sock; |
7a7cacba | 4745 | unsigned flags; |
0031275d | 4746 | int min_ret = 0; |
fddaface JA |
4747 | int ret; |
4748 | ||
dba4a925 | 4749 | sock = sock_from_file(req->file); |
7a7cacba | 4750 | if (unlikely(!sock)) |
dba4a925 | 4751 | return -ENOTSOCK; |
fddaface | 4752 | |
7a7cacba PB |
4753 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4754 | if (unlikely(ret)) | |
14db8411 | 4755 | return ret; |
fddaface | 4756 | |
7a7cacba PB |
4757 | msg.msg_name = NULL; |
4758 | msg.msg_control = NULL; | |
4759 | msg.msg_controllen = 0; | |
4760 | msg.msg_namelen = 0; | |
fddaface | 4761 | |
04411806 PB |
4762 | flags = req->sr_msg.msg_flags; |
4763 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4764 | flags |= MSG_DONTWAIT; |
0031275d SM |
4765 | if (flags & MSG_WAITALL) |
4766 | min_ret = iov_iter_count(&msg.msg_iter); | |
4767 | ||
7a7cacba PB |
4768 | msg.msg_flags = flags; |
4769 | ret = sock_sendmsg(sock, &msg); | |
45d189c6 | 4770 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4771 | return -EAGAIN; |
4772 | if (ret == -ERESTARTSYS) | |
4773 | ret = -EINTR; | |
fddaface | 4774 | |
0031275d | 4775 | if (ret < min_ret) |
93d2bcd2 | 4776 | req_set_fail(req); |
889fca73 | 4777 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 4778 | return 0; |
fddaface JA |
4779 | } |
4780 | ||
1400e697 PB |
4781 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4782 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4783 | { |
4784 | struct io_sr_msg *sr = &req->sr_msg; | |
4785 | struct iovec __user *uiov; | |
4786 | size_t iov_len; | |
4787 | int ret; | |
4788 | ||
1400e697 PB |
4789 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4790 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4791 | if (ret) |
4792 | return ret; | |
4793 | ||
4794 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4795 | if (iov_len > 1) | |
4796 | return -EINVAL; | |
5476dfed | 4797 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4798 | return -EFAULT; |
5476dfed | 4799 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 4800 | iomsg->free_iov = NULL; |
52de1fe1 | 4801 | } else { |
257e84a5 | 4802 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4803 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 4804 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 4805 | false); |
52de1fe1 JA |
4806 | if (ret > 0) |
4807 | ret = 0; | |
4808 | } | |
4809 | ||
4810 | return ret; | |
4811 | } | |
4812 | ||
4813 | #ifdef CONFIG_COMPAT | |
4814 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4815 | struct io_async_msghdr *iomsg) |
52de1fe1 | 4816 | { |
52de1fe1 JA |
4817 | struct io_sr_msg *sr = &req->sr_msg; |
4818 | struct compat_iovec __user *uiov; | |
4819 | compat_uptr_t ptr; | |
4820 | compat_size_t len; | |
4821 | int ret; | |
4822 | ||
4af3417a PB |
4823 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
4824 | &ptr, &len); | |
52de1fe1 JA |
4825 | if (ret) |
4826 | return ret; | |
4827 | ||
4828 | uiov = compat_ptr(ptr); | |
4829 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4830 | compat_ssize_t clen; | |
4831 | ||
4832 | if (len > 1) | |
4833 | return -EINVAL; | |
4834 | if (!access_ok(uiov, sizeof(*uiov))) | |
4835 | return -EFAULT; | |
4836 | if (__get_user(clen, &uiov->iov_len)) | |
4837 | return -EFAULT; | |
4838 | if (clen < 0) | |
4839 | return -EINVAL; | |
2d280bc8 | 4840 | sr->len = clen; |
257e84a5 | 4841 | iomsg->free_iov = NULL; |
52de1fe1 | 4842 | } else { |
257e84a5 | 4843 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4844 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 4845 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 4846 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
4847 | if (ret < 0) |
4848 | return ret; | |
4849 | } | |
4850 | ||
4851 | return 0; | |
4852 | } | |
4853 | #endif | |
4854 | ||
1400e697 PB |
4855 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4856 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4857 | { |
1400e697 | 4858 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
4859 | |
4860 | #ifdef CONFIG_COMPAT | |
4861 | if (req->ctx->compat) | |
1400e697 | 4862 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4863 | #endif |
52de1fe1 | 4864 | |
1400e697 | 4865 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4866 | } |
4867 | ||
bcda7baa | 4868 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4869 | bool needs_lock) |
bcda7baa JA |
4870 | { |
4871 | struct io_sr_msg *sr = &req->sr_msg; | |
bcda7baa | 4872 | |
30d51dd4 | 4873 | return io_buffer_select(req, &sr->len, sr->bgid, needs_lock); |
fddaface JA |
4874 | } |
4875 | ||
7fbb1b54 PB |
4876 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4877 | { | |
30d51dd4 | 4878 | return io_put_kbuf(req, req->kbuf); |
7fbb1b54 PB |
4879 | } |
4880 | ||
93642ef8 | 4881 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 4882 | { |
99bc4c38 | 4883 | int ret; |
3529d8c2 | 4884 | |
93642ef8 PB |
4885 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
4886 | if (!ret) | |
4887 | req->flags |= REQ_F_NEED_CLEANUP; | |
4888 | return ret; | |
4889 | } | |
4890 | ||
4891 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4892 | { | |
4893 | struct io_sr_msg *sr = &req->sr_msg; | |
4894 | ||
d2b6f48b PB |
4895 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4896 | return -EINVAL; | |
4897 | ||
270a5940 | 4898 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4899 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4900 | sr->bgid = READ_ONCE(sqe->buf_group); |
04411806 PB |
4901 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4902 | if (sr->msg_flags & MSG_DONTWAIT) | |
4903 | req->flags |= REQ_F_NOWAIT; | |
06b76d44 | 4904 | |
d8768362 JA |
4905 | #ifdef CONFIG_COMPAT |
4906 | if (req->ctx->compat) | |
4907 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4908 | #endif | |
93642ef8 | 4909 | return 0; |
aa1fa28f JA |
4910 | } |
4911 | ||
889fca73 | 4912 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4913 | { |
6b754c8b | 4914 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4915 | struct socket *sock; |
7fbb1b54 | 4916 | struct io_buffer *kbuf; |
7a7cacba | 4917 | unsigned flags; |
0031275d | 4918 | int min_ret = 0; |
52de1fe1 | 4919 | int ret, cflags = 0; |
45d189c6 | 4920 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 4921 | |
dba4a925 | 4922 | sock = sock_from_file(req->file); |
7a7cacba | 4923 | if (unlikely(!sock)) |
dba4a925 | 4924 | return -ENOTSOCK; |
3529d8c2 | 4925 | |
257e84a5 PB |
4926 | kmsg = req->async_data; |
4927 | if (!kmsg) { | |
7a7cacba PB |
4928 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
4929 | if (ret) | |
681fda8d | 4930 | return ret; |
7a7cacba PB |
4931 | kmsg = &iomsg; |
4932 | } | |
03b1230c | 4933 | |
bc02ef33 | 4934 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4935 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4936 | if (IS_ERR(kbuf)) |
52de1fe1 | 4937 | return PTR_ERR(kbuf); |
7a7cacba | 4938 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
4939 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
4940 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
4941 | 1, req->sr_msg.len); |
4942 | } | |
52de1fe1 | 4943 | |
04411806 PB |
4944 | flags = req->sr_msg.msg_flags; |
4945 | if (force_nonblock) | |
7a7cacba | 4946 | flags |= MSG_DONTWAIT; |
0031275d SM |
4947 | if (flags & MSG_WAITALL) |
4948 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4949 | ||
7a7cacba PB |
4950 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4951 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4952 | if (force_nonblock && ret == -EAGAIN) |
4953 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4954 | if (ret == -ERESTARTSYS) |
4955 | ret = -EINTR; | |
03b1230c | 4956 | |
7fbb1b54 PB |
4957 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4958 | cflags = io_put_recv_kbuf(req); | |
257e84a5 PB |
4959 | /* fast path, check for non-NULL to avoid function call */ |
4960 | if (kmsg->free_iov) | |
4961 | kfree(kmsg->free_iov); | |
99bc4c38 | 4962 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 4963 | if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
93d2bcd2 | 4964 | req_set_fail(req); |
889fca73 | 4965 | __io_req_complete(req, issue_flags, ret, cflags); |
03b1230c | 4966 | return 0; |
0fa03c62 | 4967 | } |
5d17b4a4 | 4968 | |
889fca73 | 4969 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4970 | { |
6b754c8b | 4971 | struct io_buffer *kbuf; |
7a7cacba PB |
4972 | struct io_sr_msg *sr = &req->sr_msg; |
4973 | struct msghdr msg; | |
4974 | void __user *buf = sr->buf; | |
fddaface | 4975 | struct socket *sock; |
7a7cacba PB |
4976 | struct iovec iov; |
4977 | unsigned flags; | |
0031275d | 4978 | int min_ret = 0; |
bcda7baa | 4979 | int ret, cflags = 0; |
45d189c6 | 4980 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 4981 | |
dba4a925 | 4982 | sock = sock_from_file(req->file); |
7a7cacba | 4983 | if (unlikely(!sock)) |
dba4a925 | 4984 | return -ENOTSOCK; |
fddaface | 4985 | |
bc02ef33 | 4986 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4987 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
4988 | if (IS_ERR(kbuf)) |
4989 | return PTR_ERR(kbuf); | |
7a7cacba | 4990 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 4991 | } |
bcda7baa | 4992 | |
7a7cacba | 4993 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
4994 | if (unlikely(ret)) |
4995 | goto out_free; | |
fddaface | 4996 | |
7a7cacba PB |
4997 | msg.msg_name = NULL; |
4998 | msg.msg_control = NULL; | |
4999 | msg.msg_controllen = 0; | |
5000 | msg.msg_namelen = 0; | |
5001 | msg.msg_iocb = NULL; | |
5002 | msg.msg_flags = 0; | |
fddaface | 5003 | |
04411806 PB |
5004 | flags = req->sr_msg.msg_flags; |
5005 | if (force_nonblock) | |
7a7cacba | 5006 | flags |= MSG_DONTWAIT; |
0031275d SM |
5007 | if (flags & MSG_WAITALL) |
5008 | min_ret = iov_iter_count(&msg.msg_iter); | |
5009 | ||
7a7cacba PB |
5010 | ret = sock_recvmsg(sock, &msg, flags); |
5011 | if (force_nonblock && ret == -EAGAIN) | |
5012 | return -EAGAIN; | |
5013 | if (ret == -ERESTARTSYS) | |
5014 | ret = -EINTR; | |
14c32eee | 5015 | out_free: |
7fbb1b54 PB |
5016 | if (req->flags & REQ_F_BUFFER_SELECTED) |
5017 | cflags = io_put_recv_kbuf(req); | |
0031275d | 5018 | if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
93d2bcd2 | 5019 | req_set_fail(req); |
889fca73 | 5020 | __io_req_complete(req, issue_flags, ret, cflags); |
fddaface | 5021 | return 0; |
fddaface JA |
5022 | } |
5023 | ||
3529d8c2 | 5024 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 5025 | { |
8ed8d3c3 JA |
5026 | struct io_accept *accept = &req->accept; |
5027 | ||
14587a46 | 5028 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 5029 | return -EINVAL; |
aaa4db12 | 5030 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
5031 | return -EINVAL; |
5032 | ||
d55e5f5b JA |
5033 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5034 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 5035 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 5036 | accept->nofile = rlimit(RLIMIT_NOFILE); |
a7083ad5 | 5037 | |
aaa4db12 PB |
5038 | accept->file_slot = READ_ONCE(sqe->file_index); |
5039 | if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) || | |
5040 | (accept->flags & SOCK_CLOEXEC))) | |
5041 | return -EINVAL; | |
a7083ad5 PB |
5042 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
5043 | return -EINVAL; | |
5044 | if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) | |
5045 | accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; | |
8ed8d3c3 | 5046 | return 0; |
8ed8d3c3 | 5047 | } |
17f2fe35 | 5048 | |
889fca73 | 5049 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
5050 | { |
5051 | struct io_accept *accept = &req->accept; | |
45d189c6 | 5052 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 5053 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
aaa4db12 | 5054 | bool fixed = !!accept->file_slot; |
a7083ad5 PB |
5055 | struct file *file; |
5056 | int ret, fd; | |
8ed8d3c3 | 5057 | |
e697deed JX |
5058 | if (req->file->f_flags & O_NONBLOCK) |
5059 | req->flags |= REQ_F_NOWAIT; | |
5060 | ||
aaa4db12 PB |
5061 | if (!fixed) { |
5062 | fd = __get_unused_fd_flags(accept->flags, accept->nofile); | |
5063 | if (unlikely(fd < 0)) | |
5064 | return fd; | |
5065 | } | |
a7083ad5 PB |
5066 | file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, |
5067 | accept->flags); | |
5068 | if (IS_ERR(file)) { | |
aaa4db12 PB |
5069 | if (!fixed) |
5070 | put_unused_fd(fd); | |
a7083ad5 PB |
5071 | ret = PTR_ERR(file); |
5072 | if (ret == -EAGAIN && force_nonblock) | |
5073 | return -EAGAIN; | |
ac45abc0 PB |
5074 | if (ret == -ERESTARTSYS) |
5075 | ret = -EINTR; | |
93d2bcd2 | 5076 | req_set_fail(req); |
aaa4db12 | 5077 | } else if (!fixed) { |
a7083ad5 PB |
5078 | fd_install(fd, file); |
5079 | ret = fd; | |
aaa4db12 PB |
5080 | } else { |
5081 | ret = io_install_fixed_file(req, file, issue_flags, | |
5082 | accept->file_slot - 1); | |
ac45abc0 | 5083 | } |
889fca73 | 5084 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 5085 | return 0; |
8ed8d3c3 JA |
5086 | } |
5087 | ||
93642ef8 PB |
5088 | static int io_connect_prep_async(struct io_kiocb *req) |
5089 | { | |
5090 | struct io_async_connect *io = req->async_data; | |
5091 | struct io_connect *conn = &req->connect; | |
5092 | ||
5093 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
5094 | } | |
5095 | ||
3529d8c2 | 5096 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 5097 | { |
3529d8c2 | 5098 | struct io_connect *conn = &req->connect; |
f499a021 | 5099 | |
14587a46 | 5100 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 | 5101 | return -EINVAL; |
26578cda PB |
5102 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
5103 | sqe->splice_fd_in) | |
3fbb51c1 JA |
5104 | return -EINVAL; |
5105 | ||
3529d8c2 JA |
5106 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5107 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 5108 | return 0; |
f499a021 JA |
5109 | } |
5110 | ||
889fca73 | 5111 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 5112 | { |
e8c2bc1f | 5113 | struct io_async_connect __io, *io; |
f8e85cf2 | 5114 | unsigned file_flags; |
3fbb51c1 | 5115 | int ret; |
45d189c6 | 5116 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 5117 | |
e8c2bc1f JA |
5118 | if (req->async_data) { |
5119 | io = req->async_data; | |
f499a021 | 5120 | } else { |
3529d8c2 JA |
5121 | ret = move_addr_to_kernel(req->connect.addr, |
5122 | req->connect.addr_len, | |
e8c2bc1f | 5123 | &__io.address); |
f499a021 JA |
5124 | if (ret) |
5125 | goto out; | |
5126 | io = &__io; | |
5127 | } | |
5128 | ||
3fbb51c1 JA |
5129 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
5130 | ||
e8c2bc1f | 5131 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 5132 | req->connect.addr_len, file_flags); |
87f80d62 | 5133 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 5134 | if (req->async_data) |
b7bb4f7d | 5135 | return -EAGAIN; |
e8c2bc1f | 5136 | if (io_alloc_async_data(req)) { |
f499a021 JA |
5137 | ret = -ENOMEM; |
5138 | goto out; | |
5139 | } | |
e8c2bc1f | 5140 | memcpy(req->async_data, &__io, sizeof(__io)); |
f8e85cf2 | 5141 | return -EAGAIN; |
f499a021 | 5142 | } |
f8e85cf2 JA |
5143 | if (ret == -ERESTARTSYS) |
5144 | ret = -EINTR; | |
f499a021 | 5145 | out: |
4e88d6e7 | 5146 | if (ret < 0) |
93d2bcd2 | 5147 | req_set_fail(req); |
889fca73 | 5148 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 5149 | return 0; |
469956e8 Y |
5150 | } |
5151 | #else /* !CONFIG_NET */ | |
99a10081 JA |
5152 | #define IO_NETOP_FN(op) \ |
5153 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
5154 | { \ | |
5155 | return -EOPNOTSUPP; \ | |
5156 | } | |
5157 | ||
5158 | #define IO_NETOP_PREP(op) \ | |
5159 | IO_NETOP_FN(op) \ | |
5160 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
5161 | { \ | |
5162 | return -EOPNOTSUPP; \ | |
5163 | } \ | |
5164 | ||
5165 | #define IO_NETOP_PREP_ASYNC(op) \ | |
5166 | IO_NETOP_PREP(op) \ | |
5167 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
5168 | { \ | |
5169 | return -EOPNOTSUPP; \ | |
5170 | } | |
5171 | ||
5172 | IO_NETOP_PREP_ASYNC(sendmsg); | |
5173 | IO_NETOP_PREP_ASYNC(recvmsg); | |
5174 | IO_NETOP_PREP_ASYNC(connect); | |
5175 | IO_NETOP_PREP(accept); | |
5176 | IO_NETOP_FN(send); | |
5177 | IO_NETOP_FN(recv); | |
469956e8 | 5178 | #endif /* CONFIG_NET */ |
f8e85cf2 | 5179 | |
d7718a9d JA |
5180 | struct io_poll_table { |
5181 | struct poll_table_struct pt; | |
5182 | struct io_kiocb *req; | |
68b11e8b | 5183 | int nr_entries; |
d7718a9d JA |
5184 | int error; |
5185 | }; | |
ce593a6c | 5186 | |
d7718a9d | 5187 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
5b0a6acc | 5188 | __poll_t mask, io_req_tw_func_t func) |
d7718a9d | 5189 | { |
d7718a9d JA |
5190 | /* for instances that support it check for an event match first: */ |
5191 | if (mask && !(mask & poll->events)) | |
5192 | return 0; | |
5193 | ||
5194 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
5195 | ||
5196 | list_del_init(&poll->wait.entry); | |
5197 | ||
d7718a9d | 5198 | req->result = mask; |
5b0a6acc | 5199 | req->io_task_work.func = func; |
6d816e08 | 5200 | |
d7718a9d | 5201 | /* |
e3aabf95 JA |
5202 | * If this fails, then the task is exiting. When a task exits, the |
5203 | * work gets canceled, so just cancel this request as well instead | |
5204 | * of executing it. We can't safely execute it anyway, as we may not | |
5205 | * have the needed state needed for it anyway. | |
d7718a9d | 5206 | */ |
e09ee510 | 5207 | io_req_task_work_add(req); |
d7718a9d JA |
5208 | return 1; |
5209 | } | |
5210 | ||
74ce6ce4 JA |
5211 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
5212 | __acquires(&req->ctx->completion_lock) | |
5213 | { | |
5214 | struct io_ring_ctx *ctx = req->ctx; | |
5215 | ||
316319e8 | 5216 | /* req->task == current here, checking PF_EXITING is safe */ |
e09ee510 PB |
5217 | if (unlikely(req->task->flags & PF_EXITING)) |
5218 | WRITE_ONCE(poll->canceled, true); | |
5219 | ||
74ce6ce4 JA |
5220 | if (!req->result && !READ_ONCE(poll->canceled)) { |
5221 | struct poll_table_struct pt = { ._key = poll->events }; | |
5222 | ||
5223 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
5224 | } | |
5225 | ||
79ebeaee | 5226 | spin_lock(&ctx->completion_lock); |
74ce6ce4 JA |
5227 | if (!req->result && !READ_ONCE(poll->canceled)) { |
5228 | add_wait_queue(poll->head, &poll->wait); | |
5229 | return true; | |
5230 | } | |
5231 | ||
5232 | return false; | |
5233 | } | |
5234 | ||
d4e7cd36 | 5235 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5236 | { |
e8c2bc1f | 5237 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5238 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5239 | return req->async_data; |
d4e7cd36 JA |
5240 | return req->apoll->double_poll; |
5241 | } | |
5242 | ||
5243 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5244 | { | |
5245 | if (req->opcode == IORING_OP_POLL_ADD) | |
5246 | return &req->poll; | |
5247 | return &req->apoll->poll; | |
5248 | } | |
5249 | ||
5250 | static void io_poll_remove_double(struct io_kiocb *req) | |
e07785b0 | 5251 | __must_hold(&req->ctx->completion_lock) |
d4e7cd36 JA |
5252 | { |
5253 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
5254 | |
5255 | lockdep_assert_held(&req->ctx->completion_lock); | |
5256 | ||
5257 | if (poll && poll->head) { | |
5258 | struct wait_queue_head *head = poll->head; | |
5259 | ||
79ebeaee | 5260 | spin_lock_irq(&head->lock); |
18bceab1 JA |
5261 | list_del_init(&poll->wait.entry); |
5262 | if (poll->wait.private) | |
de9b4cca | 5263 | req_ref_put(req); |
18bceab1 | 5264 | poll->head = NULL; |
79ebeaee | 5265 | spin_unlock_irq(&head->lock); |
18bceab1 JA |
5266 | } |
5267 | } | |
5268 | ||
31efe48e | 5269 | static bool __io_poll_complete(struct io_kiocb *req, __poll_t mask) |
e07785b0 | 5270 | __must_hold(&req->ctx->completion_lock) |
18bceab1 JA |
5271 | { |
5272 | struct io_ring_ctx *ctx = req->ctx; | |
88e41cf9 | 5273 | unsigned flags = IORING_CQE_F_MORE; |
e27414be | 5274 | int error; |
18bceab1 | 5275 | |
e27414be | 5276 | if (READ_ONCE(req->poll.canceled)) { |
45ab03b1 | 5277 | error = -ECANCELED; |
88e41cf9 | 5278 | req->poll.events |= EPOLLONESHOT; |
e27414be | 5279 | } else { |
5082620f | 5280 | error = mangle_poll(mask); |
e27414be | 5281 | } |
b69de288 JA |
5282 | if (req->poll.events & EPOLLONESHOT) |
5283 | flags = 0; | |
a62682f9 HX |
5284 | if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) { |
5285 | req->poll.events |= EPOLLONESHOT; | |
88e41cf9 | 5286 | flags = 0; |
a62682f9 | 5287 | } |
7b289c38 HX |
5288 | if (flags & IORING_CQE_F_MORE) |
5289 | ctx->cq_extra++; | |
18bceab1 | 5290 | |
88e41cf9 | 5291 | return !(flags & IORING_CQE_F_MORE); |
18bceab1 JA |
5292 | } |
5293 | ||
31efe48e XW |
5294 | static inline bool io_poll_complete(struct io_kiocb *req, __poll_t mask) |
5295 | __must_hold(&req->ctx->completion_lock) | |
5296 | { | |
5297 | bool done; | |
5298 | ||
5299 | done = __io_poll_complete(req, mask); | |
5300 | io_commit_cqring(req->ctx); | |
5301 | return done; | |
5302 | } | |
5303 | ||
f237c30a | 5304 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 JA |
5305 | { |
5306 | struct io_ring_ctx *ctx = req->ctx; | |
dd221f46 | 5307 | struct io_kiocb *nxt; |
18bceab1 JA |
5308 | |
5309 | if (io_poll_rewait(req, &req->poll)) { | |
79ebeaee | 5310 | spin_unlock(&ctx->completion_lock); |
dd221f46 | 5311 | } else { |
f40b964a | 5312 | bool done; |
18bceab1 | 5313 | |
5b7aa38d HX |
5314 | if (req->poll.done) { |
5315 | spin_unlock(&ctx->completion_lock); | |
5316 | return; | |
5317 | } | |
31efe48e | 5318 | done = __io_poll_complete(req, req->result); |
88e41cf9 | 5319 | if (done) { |
a890d01e | 5320 | io_poll_remove_double(req); |
88e41cf9 | 5321 | hash_del(&req->hash_node); |
bd99c71b | 5322 | req->poll.done = true; |
f40b964a | 5323 | } else { |
88e41cf9 JA |
5324 | req->result = 0; |
5325 | add_wait_queue(req->poll.head, &req->poll.wait); | |
5326 | } | |
31efe48e | 5327 | io_commit_cqring(ctx); |
79ebeaee | 5328 | spin_unlock(&ctx->completion_lock); |
dd221f46 | 5329 | io_cqring_ev_posted(ctx); |
18bceab1 | 5330 | |
88e41cf9 JA |
5331 | if (done) { |
5332 | nxt = io_put_req_find_next(req); | |
5333 | if (nxt) | |
f237c30a | 5334 | io_req_task_submit(nxt, locked); |
88e41cf9 | 5335 | } |
dd221f46 | 5336 | } |
18bceab1 JA |
5337 | } |
5338 | ||
5339 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
5340 | int sync, void *key) | |
5341 | { | |
5342 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 5343 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 | 5344 | __poll_t mask = key_to_poll(key); |
79ebeaee | 5345 | unsigned long flags; |
18bceab1 JA |
5346 | |
5347 | /* for instances that support it check for an event match first: */ | |
5348 | if (mask && !(mask & poll->events)) | |
5349 | return 0; | |
88e41cf9 JA |
5350 | if (!(poll->events & EPOLLONESHOT)) |
5351 | return poll->wait.func(&poll->wait, mode, sync, key); | |
18bceab1 | 5352 | |
8706e04e JA |
5353 | list_del_init(&wait->entry); |
5354 | ||
9ce85ef2 | 5355 | if (poll->head) { |
18bceab1 JA |
5356 | bool done; |
5357 | ||
79ebeaee | 5358 | spin_lock_irqsave(&poll->head->lock, flags); |
807abcb0 | 5359 | done = list_empty(&poll->wait.entry); |
18bceab1 | 5360 | if (!done) |
807abcb0 | 5361 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
5362 | /* make sure double remove sees this as being gone */ |
5363 | wait->private = NULL; | |
79ebeaee | 5364 | spin_unlock_irqrestore(&poll->head->lock, flags); |
c8b5e260 JA |
5365 | if (!done) { |
5366 | /* use wait func handler, so it matches the rq type */ | |
5367 | poll->wait.func(&poll->wait, mode, sync, key); | |
5368 | } | |
18bceab1 | 5369 | } |
de9b4cca | 5370 | req_ref_put(req); |
18bceab1 JA |
5371 | return 1; |
5372 | } | |
5373 | ||
5374 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
5375 | wait_queue_func_t wake_func) | |
5376 | { | |
5377 | poll->head = NULL; | |
5378 | poll->done = false; | |
5379 | poll->canceled = false; | |
464dca61 JA |
5380 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
5381 | /* mask in events that we always want/need */ | |
5382 | poll->events = events | IO_POLL_UNMASK; | |
18bceab1 JA |
5383 | INIT_LIST_HEAD(&poll->wait.entry); |
5384 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
5385 | } | |
5386 | ||
5387 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
5388 | struct wait_queue_head *head, |
5389 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
5390 | { |
5391 | struct io_kiocb *req = pt->req; | |
5392 | ||
5393 | /* | |
68b11e8b PB |
5394 | * The file being polled uses multiple waitqueues for poll handling |
5395 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb | |
5396 | * if this happens. | |
18bceab1 | 5397 | */ |
68b11e8b | 5398 | if (unlikely(pt->nr_entries)) { |
58852d4d PB |
5399 | struct io_poll_iocb *poll_one = poll; |
5400 | ||
23a65db8 PB |
5401 | /* double add on the same waitqueue head, ignore */ |
5402 | if (poll_one->head == head) | |
5403 | return; | |
18bceab1 | 5404 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5405 | if (*poll_ptr) { |
23a65db8 PB |
5406 | if ((*poll_ptr)->head == head) |
5407 | return; | |
18bceab1 JA |
5408 | pt->error = -EINVAL; |
5409 | return; | |
5410 | } | |
ea6a693d JA |
5411 | /* |
5412 | * Can't handle multishot for double wait for now, turn it | |
5413 | * into one-shot mode. | |
5414 | */ | |
7a274727 PB |
5415 | if (!(poll_one->events & EPOLLONESHOT)) |
5416 | poll_one->events |= EPOLLONESHOT; | |
18bceab1 JA |
5417 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
5418 | if (!poll) { | |
5419 | pt->error = -ENOMEM; | |
5420 | return; | |
5421 | } | |
58852d4d | 5422 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
de9b4cca | 5423 | req_ref_get(req); |
18bceab1 | 5424 | poll->wait.private = req; |
807abcb0 | 5425 | *poll_ptr = poll; |
18bceab1 JA |
5426 | } |
5427 | ||
68b11e8b | 5428 | pt->nr_entries++; |
18bceab1 | 5429 | poll->head = head; |
a31eb4a2 JX |
5430 | |
5431 | if (poll->events & EPOLLEXCLUSIVE) | |
5432 | add_wait_queue_exclusive(head, &poll->wait); | |
5433 | else | |
5434 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5435 | } |
5436 | ||
5437 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5438 | struct poll_table_struct *p) | |
5439 | { | |
5440 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5441 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5442 | |
807abcb0 | 5443 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5444 | } |
5445 | ||
f237c30a | 5446 | static void io_async_task_func(struct io_kiocb *req, bool *locked) |
d7718a9d | 5447 | { |
d7718a9d JA |
5448 | struct async_poll *apoll = req->apoll; |
5449 | struct io_ring_ctx *ctx = req->ctx; | |
5450 | ||
236daeae | 5451 | trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data); |
d7718a9d | 5452 | |
74ce6ce4 | 5453 | if (io_poll_rewait(req, &apoll->poll)) { |
79ebeaee | 5454 | spin_unlock(&ctx->completion_lock); |
74ce6ce4 | 5455 | return; |
d7718a9d JA |
5456 | } |
5457 | ||
0ea13b44 | 5458 | hash_del(&req->hash_node); |
d4e7cd36 | 5459 | io_poll_remove_double(req); |
bd99c71b | 5460 | apoll->poll.done = true; |
79ebeaee | 5461 | spin_unlock(&ctx->completion_lock); |
74ce6ce4 | 5462 | |
0be0b0e3 | 5463 | if (!READ_ONCE(apoll->poll.canceled)) |
f237c30a | 5464 | io_req_task_submit(req, locked); |
0be0b0e3 | 5465 | else |
2593553a | 5466 | io_req_complete_failed(req, -ECANCELED); |
d7718a9d JA |
5467 | } |
5468 | ||
5469 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5470 | void *key) | |
5471 | { | |
5472 | struct io_kiocb *req = wait->private; | |
5473 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5474 | ||
5475 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5476 | key_to_poll(key)); | |
5477 | ||
5478 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5479 | } | |
5480 | ||
5481 | static void io_poll_req_insert(struct io_kiocb *req) | |
5482 | { | |
5483 | struct io_ring_ctx *ctx = req->ctx; | |
5484 | struct hlist_head *list; | |
5485 | ||
5486 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5487 | hlist_add_head(&req->hash_node, list); | |
5488 | } | |
5489 | ||
5490 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5491 | struct io_poll_iocb *poll, | |
5492 | struct io_poll_table *ipt, __poll_t mask, | |
5493 | wait_queue_func_t wake_func) | |
5494 | __acquires(&ctx->completion_lock) | |
5495 | { | |
5496 | struct io_ring_ctx *ctx = req->ctx; | |
5497 | bool cancel = false; | |
5498 | ||
4d52f338 | 5499 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5500 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5501 | poll->file = req->file; |
18bceab1 | 5502 | poll->wait.private = req; |
d7718a9d JA |
5503 | |
5504 | ipt->pt._key = mask; | |
5505 | ipt->req = req; | |
68b11e8b PB |
5506 | ipt->error = 0; |
5507 | ipt->nr_entries = 0; | |
d7718a9d | 5508 | |
d7718a9d | 5509 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
68b11e8b PB |
5510 | if (unlikely(!ipt->nr_entries) && !ipt->error) |
5511 | ipt->error = -EINVAL; | |
d7718a9d | 5512 | |
79ebeaee | 5513 | spin_lock(&ctx->completion_lock); |
a890d01e | 5514 | if (ipt->error || (mask && (poll->events & EPOLLONESHOT))) |
46fee9ab | 5515 | io_poll_remove_double(req); |
d7718a9d | 5516 | if (likely(poll->head)) { |
79ebeaee | 5517 | spin_lock_irq(&poll->head->lock); |
d7718a9d JA |
5518 | if (unlikely(list_empty(&poll->wait.entry))) { |
5519 | if (ipt->error) | |
5520 | cancel = true; | |
5521 | ipt->error = 0; | |
5522 | mask = 0; | |
5523 | } | |
88e41cf9 | 5524 | if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error) |
d7718a9d JA |
5525 | list_del_init(&poll->wait.entry); |
5526 | else if (cancel) | |
5527 | WRITE_ONCE(poll->canceled, true); | |
5528 | else if (!poll->done) /* actually waiting for an event */ | |
5529 | io_poll_req_insert(req); | |
79ebeaee | 5530 | spin_unlock_irq(&poll->head->lock); |
d7718a9d JA |
5531 | } |
5532 | ||
5533 | return mask; | |
5534 | } | |
5535 | ||
59b735ae OL |
5536 | enum { |
5537 | IO_APOLL_OK, | |
5538 | IO_APOLL_ABORTED, | |
5539 | IO_APOLL_READY | |
5540 | }; | |
5541 | ||
5542 | static int io_arm_poll_handler(struct io_kiocb *req) | |
d7718a9d JA |
5543 | { |
5544 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5545 | struct io_ring_ctx *ctx = req->ctx; | |
5546 | struct async_poll *apoll; | |
5547 | struct io_poll_table ipt; | |
b2d9c3da | 5548 | __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI; |
9dab14b8 | 5549 | int rw; |
d7718a9d JA |
5550 | |
5551 | if (!req->file || !file_can_poll(req->file)) | |
59b735ae | 5552 | return IO_APOLL_ABORTED; |
24c74678 | 5553 | if (req->flags & REQ_F_POLLED) |
59b735ae | 5554 | return IO_APOLL_ABORTED; |
b2d9c3da PB |
5555 | if (!def->pollin && !def->pollout) |
5556 | return IO_APOLL_ABORTED; | |
5557 | ||
5558 | if (def->pollin) { | |
9dab14b8 | 5559 | rw = READ; |
b2d9c3da PB |
5560 | mask |= POLLIN | POLLRDNORM; |
5561 | ||
5562 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5563 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5564 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5565 | mask &= ~POLLIN; | |
5566 | } else { | |
9dab14b8 | 5567 | rw = WRITE; |
b2d9c3da PB |
5568 | mask |= POLLOUT | POLLWRNORM; |
5569 | } | |
5570 | ||
9dab14b8 | 5571 | /* if we can't nonblock try, then no point in arming a poll handler */ |
b191e2df | 5572 | if (!io_file_supports_nowait(req, rw)) |
59b735ae | 5573 | return IO_APOLL_ABORTED; |
d7718a9d JA |
5574 | |
5575 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
5576 | if (unlikely(!apoll)) | |
59b735ae | 5577 | return IO_APOLL_ABORTED; |
807abcb0 | 5578 | apoll->double_poll = NULL; |
d7718a9d | 5579 | req->apoll = apoll; |
b2d9c3da | 5580 | req->flags |= REQ_F_POLLED; |
d7718a9d | 5581 | ipt.pt._qproc = io_async_queue_proc; |
48dcd38d | 5582 | io_req_set_refcount(req); |
d7718a9d JA |
5583 | |
5584 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5585 | io_async_wake); | |
79ebeaee | 5586 | spin_unlock(&ctx->completion_lock); |
41a5169c HX |
5587 | if (ret || ipt.error) |
5588 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; | |
5589 | ||
236daeae OL |
5590 | trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data, |
5591 | mask, apoll->poll.events); | |
59b735ae | 5592 | return IO_APOLL_OK; |
d7718a9d JA |
5593 | } |
5594 | ||
5595 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
b2e720ac | 5596 | struct io_poll_iocb *poll, bool do_cancel) |
e07785b0 | 5597 | __must_hold(&req->ctx->completion_lock) |
221c5eb2 | 5598 | { |
b41e9852 | 5599 | bool do_complete = false; |
221c5eb2 | 5600 | |
5082620f JA |
5601 | if (!poll->head) |
5602 | return false; | |
79ebeaee | 5603 | spin_lock_irq(&poll->head->lock); |
b2e720ac JA |
5604 | if (do_cancel) |
5605 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5606 | if (!list_empty(&poll->wait.entry)) { |
5607 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5608 | do_complete = true; |
221c5eb2 | 5609 | } |
79ebeaee | 5610 | spin_unlock_irq(&poll->head->lock); |
3bfa5bcb | 5611 | hash_del(&req->hash_node); |
d7718a9d JA |
5612 | return do_complete; |
5613 | } | |
5614 | ||
5d709043 | 5615 | static bool io_poll_remove_one(struct io_kiocb *req) |
e07785b0 | 5616 | __must_hold(&req->ctx->completion_lock) |
d7718a9d JA |
5617 | { |
5618 | bool do_complete; | |
5619 | ||
d4e7cd36 | 5620 | io_poll_remove_double(req); |
e31001a3 | 5621 | do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true); |
d4e7cd36 | 5622 | |
b41e9852 | 5623 | if (do_complete) { |
d4d19c19 | 5624 | io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0); |
b41e9852 | 5625 | io_commit_cqring(req->ctx); |
93d2bcd2 | 5626 | req_set_fail(req); |
91c2f697 | 5627 | io_put_req_deferred(req); |
5d709043 | 5628 | } |
b41e9852 | 5629 | return do_complete; |
221c5eb2 JA |
5630 | } |
5631 | ||
76e1b642 JA |
5632 | /* |
5633 | * Returns true if we found and killed one or more poll requests | |
5634 | */ | |
c072481d PB |
5635 | static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, |
5636 | struct task_struct *tsk, bool cancel_all) | |
221c5eb2 | 5637 | { |
78076bb6 | 5638 | struct hlist_node *tmp; |
221c5eb2 | 5639 | struct io_kiocb *req; |
8e2e1faf | 5640 | int posted = 0, i; |
221c5eb2 | 5641 | |
79ebeaee | 5642 | spin_lock(&ctx->completion_lock); |
78076bb6 JA |
5643 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5644 | struct hlist_head *list; | |
5645 | ||
5646 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5647 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
3dd0c97a | 5648 | if (io_match_task(req, tsk, cancel_all)) |
f3606e3a JA |
5649 | posted += io_poll_remove_one(req); |
5650 | } | |
221c5eb2 | 5651 | } |
79ebeaee | 5652 | spin_unlock(&ctx->completion_lock); |
b41e9852 | 5653 | |
8e2e1faf JA |
5654 | if (posted) |
5655 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5656 | |
5657 | return posted != 0; | |
221c5eb2 JA |
5658 | } |
5659 | ||
9ba5fac8 PB |
5660 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5661 | bool poll_only) | |
e07785b0 | 5662 | __must_hold(&ctx->completion_lock) |
47f46768 | 5663 | { |
78076bb6 | 5664 | struct hlist_head *list; |
47f46768 JA |
5665 | struct io_kiocb *req; |
5666 | ||
78076bb6 JA |
5667 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5668 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5669 | if (sqe_addr != req->user_data) |
5670 | continue; | |
9ba5fac8 PB |
5671 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
5672 | continue; | |
b2cb805f | 5673 | return req; |
47f46768 | 5674 | } |
b2cb805f JA |
5675 | return NULL; |
5676 | } | |
5677 | ||
9ba5fac8 PB |
5678 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5679 | bool poll_only) | |
e07785b0 | 5680 | __must_hold(&ctx->completion_lock) |
b2cb805f JA |
5681 | { |
5682 | struct io_kiocb *req; | |
5683 | ||
9ba5fac8 | 5684 | req = io_poll_find(ctx, sqe_addr, poll_only); |
b2cb805f JA |
5685 | if (!req) |
5686 | return -ENOENT; | |
5687 | if (io_poll_remove_one(req)) | |
5688 | return 0; | |
5689 | ||
5690 | return -EALREADY; | |
47f46768 JA |
5691 | } |
5692 | ||
9096af3e PB |
5693 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
5694 | unsigned int flags) | |
5695 | { | |
5696 | u32 events; | |
47f46768 | 5697 | |
9096af3e PB |
5698 | events = READ_ONCE(sqe->poll32_events); |
5699 | #ifdef __BIG_ENDIAN | |
5700 | events = swahw32(events); | |
5701 | #endif | |
5702 | if (!(flags & IORING_POLL_ADD_MULTI)) | |
5703 | events |= EPOLLONESHOT; | |
5704 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); | |
47f46768 JA |
5705 | } |
5706 | ||
c5de0036 | 5707 | static int io_poll_update_prep(struct io_kiocb *req, |
3529d8c2 | 5708 | const struct io_uring_sqe *sqe) |
0969e783 | 5709 | { |
c5de0036 PB |
5710 | struct io_poll_update *upd = &req->poll_update; |
5711 | u32 flags; | |
5712 | ||
0969e783 JA |
5713 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5714 | return -EINVAL; | |
26578cda | 5715 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
c5de0036 PB |
5716 | return -EINVAL; |
5717 | flags = READ_ONCE(sqe->len); | |
5718 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | | |
5719 | IORING_POLL_ADD_MULTI)) | |
5720 | return -EINVAL; | |
5721 | /* meaningless without update */ | |
5722 | if (flags == IORING_POLL_ADD_MULTI) | |
0969e783 JA |
5723 | return -EINVAL; |
5724 | ||
c5de0036 PB |
5725 | upd->old_user_data = READ_ONCE(sqe->addr); |
5726 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; | |
5727 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; | |
221c5eb2 | 5728 | |
c5de0036 PB |
5729 | upd->new_user_data = READ_ONCE(sqe->off); |
5730 | if (!upd->update_user_data && upd->new_user_data) | |
5731 | return -EINVAL; | |
5732 | if (upd->update_events) | |
5733 | upd->events = io_poll_parse_events(sqe, flags); | |
5734 | else if (sqe->poll32_events) | |
5735 | return -EINVAL; | |
221c5eb2 | 5736 | |
221c5eb2 JA |
5737 | return 0; |
5738 | } | |
5739 | ||
221c5eb2 JA |
5740 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5741 | void *key) | |
5742 | { | |
c2f2eb7d JA |
5743 | struct io_kiocb *req = wait->private; |
5744 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5745 | |
d7718a9d | 5746 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5747 | } |
5748 | ||
221c5eb2 JA |
5749 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5750 | struct poll_table_struct *p) | |
5751 | { | |
5752 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5753 | ||
e8c2bc1f | 5754 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5755 | } |
5756 | ||
3529d8c2 | 5757 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5758 | { |
5759 | struct io_poll_iocb *poll = &req->poll; | |
c5de0036 | 5760 | u32 flags; |
221c5eb2 JA |
5761 | |
5762 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5763 | return -EINVAL; | |
c5de0036 | 5764 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
88e41cf9 JA |
5765 | return -EINVAL; |
5766 | flags = READ_ONCE(sqe->len); | |
c5de0036 | 5767 | if (flags & ~IORING_POLL_ADD_MULTI) |
221c5eb2 JA |
5768 | return -EINVAL; |
5769 | ||
48dcd38d | 5770 | io_req_set_refcount(req); |
c5de0036 | 5771 | poll->events = io_poll_parse_events(sqe, flags); |
0969e783 JA |
5772 | return 0; |
5773 | } | |
5774 | ||
61e98203 | 5775 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
5776 | { |
5777 | struct io_poll_iocb *poll = &req->poll; | |
5778 | struct io_ring_ctx *ctx = req->ctx; | |
5779 | struct io_poll_table ipt; | |
0969e783 | 5780 | __poll_t mask; |
5b7aa38d | 5781 | bool done; |
0969e783 | 5782 | |
d7718a9d | 5783 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5784 | |
d7718a9d JA |
5785 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5786 | io_poll_wake); | |
221c5eb2 | 5787 | |
8c838788 | 5788 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5789 | ipt.error = 0; |
5b7aa38d | 5790 | done = io_poll_complete(req, mask); |
221c5eb2 | 5791 | } |
79ebeaee | 5792 | spin_unlock(&ctx->completion_lock); |
221c5eb2 | 5793 | |
8c838788 JA |
5794 | if (mask) { |
5795 | io_cqring_ev_posted(ctx); | |
5b7aa38d | 5796 | if (done) |
88e41cf9 | 5797 | io_put_req(req); |
221c5eb2 | 5798 | } |
8c838788 | 5799 | return ipt.error; |
221c5eb2 JA |
5800 | } |
5801 | ||
c5de0036 | 5802 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
b69de288 JA |
5803 | { |
5804 | struct io_ring_ctx *ctx = req->ctx; | |
5805 | struct io_kiocb *preq; | |
cb3b200e | 5806 | bool completing; |
b69de288 JA |
5807 | int ret; |
5808 | ||
79ebeaee | 5809 | spin_lock(&ctx->completion_lock); |
9ba5fac8 | 5810 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
b69de288 JA |
5811 | if (!preq) { |
5812 | ret = -ENOENT; | |
5813 | goto err; | |
b69de288 | 5814 | } |
cb3b200e | 5815 | |
c5de0036 PB |
5816 | if (!req->poll_update.update_events && !req->poll_update.update_user_data) { |
5817 | completing = true; | |
5818 | ret = io_poll_remove_one(preq) ? 0 : -EALREADY; | |
5819 | goto err; | |
5820 | } | |
5821 | ||
cb3b200e JA |
5822 | /* |
5823 | * Don't allow racy completion with singleshot, as we cannot safely | |
5824 | * update those. For multishot, if we're racing with completion, just | |
5825 | * let completion re-add it. | |
5826 | */ | |
5827 | completing = !__io_poll_remove_one(preq, &preq->poll, false); | |
5828 | if (completing && (preq->poll.events & EPOLLONESHOT)) { | |
5829 | ret = -EALREADY; | |
5830 | goto err; | |
b69de288 JA |
5831 | } |
5832 | /* we now have a detached poll request. reissue. */ | |
5833 | ret = 0; | |
5834 | err: | |
b69de288 | 5835 | if (ret < 0) { |
79ebeaee | 5836 | spin_unlock(&ctx->completion_lock); |
93d2bcd2 | 5837 | req_set_fail(req); |
b69de288 JA |
5838 | io_req_complete(req, ret); |
5839 | return 0; | |
5840 | } | |
5841 | /* only mask one event flags, keep behavior flags */ | |
9d805892 | 5842 | if (req->poll_update.update_events) { |
b69de288 | 5843 | preq->poll.events &= ~0xffff; |
9d805892 | 5844 | preq->poll.events |= req->poll_update.events & 0xffff; |
b69de288 JA |
5845 | preq->poll.events |= IO_POLL_UNMASK; |
5846 | } | |
9d805892 PB |
5847 | if (req->poll_update.update_user_data) |
5848 | preq->user_data = req->poll_update.new_user_data; | |
79ebeaee | 5849 | spin_unlock(&ctx->completion_lock); |
cb3b200e | 5850 | |
b69de288 JA |
5851 | /* complete update request, we're done with it */ |
5852 | io_req_complete(req, ret); | |
5853 | ||
cb3b200e | 5854 | if (!completing) { |
c5de0036 | 5855 | ret = io_poll_add(preq, issue_flags); |
cb3b200e | 5856 | if (ret < 0) { |
93d2bcd2 | 5857 | req_set_fail(preq); |
cb3b200e JA |
5858 | io_req_complete(preq, ret); |
5859 | } | |
b69de288 JA |
5860 | } |
5861 | return 0; | |
5862 | } | |
5863 | ||
f237c30a | 5864 | static void io_req_task_timeout(struct io_kiocb *req, bool *locked) |
89850fce | 5865 | { |
6224590d PB |
5866 | struct io_timeout_data *data = req->async_data; |
5867 | ||
5868 | if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS)) | |
5869 | req_set_fail(req); | |
505657bc | 5870 | io_req_complete_post(req, -ETIME, 0); |
89850fce JA |
5871 | } |
5872 | ||
5262f567 JA |
5873 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5874 | { | |
ad8a48ac JA |
5875 | struct io_timeout_data *data = container_of(timer, |
5876 | struct io_timeout_data, timer); | |
5877 | struct io_kiocb *req = data->req; | |
5878 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5879 | unsigned long flags; |
5880 | ||
89850fce | 5881 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
a71976f3 | 5882 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5883 | atomic_set(&req->ctx->cq_timeouts, |
5884 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
89850fce | 5885 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
01cec8c1 | 5886 | |
89850fce JA |
5887 | req->io_task_work.func = io_req_task_timeout; |
5888 | io_req_task_work_add(req); | |
5262f567 JA |
5889 | return HRTIMER_NORESTART; |
5890 | } | |
5891 | ||
fbd15848 PB |
5892 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5893 | __u64 user_data) | |
89850fce | 5894 | __must_hold(&ctx->timeout_lock) |
f254ac04 | 5895 | { |
fbd15848 | 5896 | struct io_timeout_data *io; |
47f46768 | 5897 | struct io_kiocb *req; |
fd9c7bc5 | 5898 | bool found = false; |
f254ac04 | 5899 | |
135fcde8 | 5900 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
fd9c7bc5 PB |
5901 | found = user_data == req->user_data; |
5902 | if (found) | |
47f46768 | 5903 | break; |
47f46768 | 5904 | } |
fd9c7bc5 PB |
5905 | if (!found) |
5906 | return ERR_PTR(-ENOENT); | |
fbd15848 PB |
5907 | |
5908 | io = req->async_data; | |
fd9c7bc5 | 5909 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
fbd15848 | 5910 | return ERR_PTR(-EALREADY); |
a71976f3 | 5911 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5912 | return req; |
5913 | } | |
47f46768 | 5914 | |
fbd15848 | 5915 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
ec3c3d0f | 5916 | __must_hold(&ctx->completion_lock) |
89850fce | 5917 | __must_hold(&ctx->timeout_lock) |
fbd15848 PB |
5918 | { |
5919 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5920 | ||
5921 | if (IS_ERR(req)) | |
5922 | return PTR_ERR(req); | |
f254ac04 | 5923 | |
93d2bcd2 | 5924 | req_set_fail(req); |
d4d19c19 | 5925 | io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0); |
91c2f697 | 5926 | io_put_req_deferred(req); |
f254ac04 JA |
5927 | return 0; |
5928 | } | |
5929 | ||
50c1df2b JA |
5930 | static clockid_t io_timeout_get_clock(struct io_timeout_data *data) |
5931 | { | |
5932 | switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) { | |
5933 | case IORING_TIMEOUT_BOOTTIME: | |
5934 | return CLOCK_BOOTTIME; | |
5935 | case IORING_TIMEOUT_REALTIME: | |
5936 | return CLOCK_REALTIME; | |
5937 | default: | |
5938 | /* can't happen, vetted at prep time */ | |
5939 | WARN_ON_ONCE(1); | |
5940 | fallthrough; | |
5941 | case 0: | |
5942 | return CLOCK_MONOTONIC; | |
5943 | } | |
5944 | } | |
5945 | ||
f1042b6c PB |
5946 | static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5947 | struct timespec64 *ts, enum hrtimer_mode mode) | |
5948 | __must_hold(&ctx->timeout_lock) | |
5949 | { | |
5950 | struct io_timeout_data *io; | |
5951 | struct io_kiocb *req; | |
5952 | bool found = false; | |
5953 | ||
5954 | list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) { | |
5955 | found = user_data == req->user_data; | |
5956 | if (found) | |
5957 | break; | |
5958 | } | |
5959 | if (!found) | |
5960 | return -ENOENT; | |
5961 | ||
5962 | io = req->async_data; | |
5963 | if (hrtimer_try_to_cancel(&io->timer) == -1) | |
5964 | return -EALREADY; | |
5965 | hrtimer_init(&io->timer, io_timeout_get_clock(io), mode); | |
5966 | io->timer.function = io_link_timeout_fn; | |
5967 | hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode); | |
5968 | return 0; | |
5969 | } | |
5970 | ||
9c8e11b3 PB |
5971 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5972 | struct timespec64 *ts, enum hrtimer_mode mode) | |
89850fce | 5973 | __must_hold(&ctx->timeout_lock) |
47f46768 | 5974 | { |
9c8e11b3 PB |
5975 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
5976 | struct io_timeout_data *data; | |
47f46768 | 5977 | |
9c8e11b3 PB |
5978 | if (IS_ERR(req)) |
5979 | return PTR_ERR(req); | |
47f46768 | 5980 | |
9c8e11b3 PB |
5981 | req->timeout.off = 0; /* noseq */ |
5982 | data = req->async_data; | |
5983 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
50c1df2b | 5984 | hrtimer_init(&data->timer, io_timeout_get_clock(data), mode); |
9c8e11b3 PB |
5985 | data->timer.function = io_timeout_fn; |
5986 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
5987 | return 0; | |
47f46768 JA |
5988 | } |
5989 | ||
3529d8c2 JA |
5990 | static int io_timeout_remove_prep(struct io_kiocb *req, |
5991 | const struct io_uring_sqe *sqe) | |
b29472ee | 5992 | { |
9c8e11b3 PB |
5993 | struct io_timeout_rem *tr = &req->timeout_rem; |
5994 | ||
b29472ee JA |
5995 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5996 | return -EINVAL; | |
61710e43 DA |
5997 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5998 | return -EINVAL; | |
26578cda | 5999 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
b29472ee JA |
6000 | return -EINVAL; |
6001 | ||
f1042b6c | 6002 | tr->ltimeout = false; |
9c8e11b3 PB |
6003 | tr->addr = READ_ONCE(sqe->addr); |
6004 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
f1042b6c PB |
6005 | if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) { |
6006 | if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
6007 | return -EINVAL; | |
6008 | if (tr->flags & IORING_LINK_TIMEOUT_UPDATE) | |
6009 | tr->ltimeout = true; | |
6010 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS)) | |
9c8e11b3 PB |
6011 | return -EINVAL; |
6012 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
6013 | return -EFAULT; | |
6014 | } else if (tr->flags) { | |
6015 | /* timeout removal doesn't support flags */ | |
b29472ee | 6016 | return -EINVAL; |
9c8e11b3 | 6017 | } |
b29472ee | 6018 | |
b29472ee JA |
6019 | return 0; |
6020 | } | |
6021 | ||
8662daec PB |
6022 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
6023 | { | |
6024 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
6025 | : HRTIMER_MODE_REL; | |
6026 | } | |
6027 | ||
11365043 JA |
6028 | /* |
6029 | * Remove or update an existing timeout command | |
6030 | */ | |
61e98203 | 6031 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 6032 | { |
9c8e11b3 | 6033 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 6034 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 6035 | int ret; |
11365043 | 6036 | |
ec3c3d0f PB |
6037 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
6038 | spin_lock(&ctx->completion_lock); | |
6039 | spin_lock_irq(&ctx->timeout_lock); | |
9c8e11b3 | 6040 | ret = io_timeout_cancel(ctx, tr->addr); |
ec3c3d0f PB |
6041 | spin_unlock_irq(&ctx->timeout_lock); |
6042 | spin_unlock(&ctx->completion_lock); | |
6043 | } else { | |
f1042b6c PB |
6044 | enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags); |
6045 | ||
ec3c3d0f | 6046 | spin_lock_irq(&ctx->timeout_lock); |
f1042b6c PB |
6047 | if (tr->ltimeout) |
6048 | ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
6049 | else | |
6050 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
ec3c3d0f PB |
6051 | spin_unlock_irq(&ctx->timeout_lock); |
6052 | } | |
11365043 | 6053 | |
4e88d6e7 | 6054 | if (ret < 0) |
93d2bcd2 | 6055 | req_set_fail(req); |
505657bc | 6056 | io_req_complete_post(req, ret, 0); |
11365043 | 6057 | return 0; |
5262f567 JA |
6058 | } |
6059 | ||
3529d8c2 | 6060 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 6061 | bool is_timeout_link) |
5262f567 | 6062 | { |
ad8a48ac | 6063 | struct io_timeout_data *data; |
a41525ab | 6064 | unsigned flags; |
56080b02 | 6065 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 6066 | |
ad8a48ac | 6067 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 6068 | return -EINVAL; |
26578cda PB |
6069 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
6070 | sqe->splice_fd_in) | |
a41525ab | 6071 | return -EINVAL; |
56080b02 | 6072 | if (off && is_timeout_link) |
2d28390a | 6073 | return -EINVAL; |
a41525ab | 6074 | flags = READ_ONCE(sqe->timeout_flags); |
6224590d PB |
6075 | if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK | |
6076 | IORING_TIMEOUT_ETIME_SUCCESS)) | |
50c1df2b JA |
6077 | return -EINVAL; |
6078 | /* more than one clock specified is invalid, obviously */ | |
6079 | if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
5262f567 | 6080 | return -EINVAL; |
bdf20073 | 6081 | |
ef9dd637 | 6082 | INIT_LIST_HEAD(&req->timeout.list); |
bfe68a22 | 6083 | req->timeout.off = off; |
f18ee4cf PB |
6084 | if (unlikely(off && !req->ctx->off_timeout_used)) |
6085 | req->ctx->off_timeout_used = true; | |
26a61679 | 6086 | |
e8c2bc1f | 6087 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
6088 | return -ENOMEM; |
6089 | ||
e8c2bc1f | 6090 | data = req->async_data; |
ad8a48ac | 6091 | data->req = req; |
50c1df2b | 6092 | data->flags = flags; |
ad8a48ac JA |
6093 | |
6094 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
6095 | return -EFAULT; |
6096 | ||
8662daec | 6097 | data->mode = io_translate_timeout_mode(flags); |
50c1df2b | 6098 | hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode); |
b97e736a PB |
6099 | |
6100 | if (is_timeout_link) { | |
6101 | struct io_submit_link *link = &req->ctx->submit_state.link; | |
6102 | ||
6103 | if (!link->head) | |
6104 | return -EINVAL; | |
6105 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) | |
6106 | return -EINVAL; | |
4d13d1a4 PB |
6107 | req->timeout.head = link->last; |
6108 | link->last->flags |= REQ_F_ARM_LTIMEOUT; | |
b97e736a | 6109 | } |
ad8a48ac JA |
6110 | return 0; |
6111 | } | |
6112 | ||
61e98203 | 6113 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 6114 | { |
ad8a48ac | 6115 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 6116 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 6117 | struct list_head *entry; |
bfe68a22 | 6118 | u32 tail, off = req->timeout.off; |
ad8a48ac | 6119 | |
89850fce | 6120 | spin_lock_irq(&ctx->timeout_lock); |
93bd25bb | 6121 | |
5262f567 JA |
6122 | /* |
6123 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
6124 | * timeout event to be satisfied. If it isn't set, then this is |
6125 | * a pure timeout request, sequence isn't used. | |
5262f567 | 6126 | */ |
8eb7e2d0 | 6127 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
6128 | entry = ctx->timeout_list.prev; |
6129 | goto add; | |
6130 | } | |
5262f567 | 6131 | |
bfe68a22 PB |
6132 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
6133 | req->timeout.target_seq = tail + off; | |
5262f567 | 6134 | |
f010505b MDG |
6135 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
6136 | * This is safe because ->completion_lock is held, and submissions | |
6137 | * and completions are never mixed in the same ->completion_lock section. | |
6138 | */ | |
6139 | ctx->cq_last_tm_flush = tail; | |
6140 | ||
5262f567 JA |
6141 | /* |
6142 | * Insertion sort, ensuring the first entry in the list is always | |
6143 | * the one we need first. | |
6144 | */ | |
5262f567 | 6145 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
6146 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
6147 | timeout.list); | |
5262f567 | 6148 | |
8eb7e2d0 | 6149 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 6150 | continue; |
bfe68a22 PB |
6151 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
6152 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
6153 | break; |
6154 | } | |
93bd25bb | 6155 | add: |
135fcde8 | 6156 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
6157 | data->timer.function = io_timeout_fn; |
6158 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
89850fce | 6159 | spin_unlock_irq(&ctx->timeout_lock); |
5262f567 JA |
6160 | return 0; |
6161 | } | |
5262f567 | 6162 | |
f458dd84 PB |
6163 | struct io_cancel_data { |
6164 | struct io_ring_ctx *ctx; | |
6165 | u64 user_data; | |
6166 | }; | |
6167 | ||
62755e35 JA |
6168 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
6169 | { | |
6170 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 6171 | struct io_cancel_data *cd = data; |
62755e35 | 6172 | |
f458dd84 | 6173 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
62755e35 JA |
6174 | } |
6175 | ||
f458dd84 PB |
6176 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
6177 | struct io_ring_ctx *ctx) | |
62755e35 | 6178 | { |
f458dd84 | 6179 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
62755e35 | 6180 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
6181 | int ret = 0; |
6182 | ||
f458dd84 | 6183 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
6184 | return -ENOENT; |
6185 | ||
f458dd84 | 6186 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
62755e35 JA |
6187 | switch (cancel_ret) { |
6188 | case IO_WQ_CANCEL_OK: | |
6189 | ret = 0; | |
6190 | break; | |
6191 | case IO_WQ_CANCEL_RUNNING: | |
6192 | ret = -EALREADY; | |
6193 | break; | |
6194 | case IO_WQ_CANCEL_NOTFOUND: | |
6195 | ret = -ENOENT; | |
6196 | break; | |
6197 | } | |
6198 | ||
e977d6d3 JA |
6199 | return ret; |
6200 | } | |
6201 | ||
8cb01fac | 6202 | static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr) |
47f46768 | 6203 | { |
8cb01fac | 6204 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 JA |
6205 | int ret; |
6206 | ||
dadebc35 | 6207 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
8cb01fac | 6208 | |
f458dd84 | 6209 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
df9727af | 6210 | if (ret != -ENOENT) |
8cb01fac | 6211 | return ret; |
505657bc PB |
6212 | |
6213 | spin_lock(&ctx->completion_lock); | |
79ebeaee | 6214 | spin_lock_irq(&ctx->timeout_lock); |
47f46768 | 6215 | ret = io_timeout_cancel(ctx, sqe_addr); |
79ebeaee | 6216 | spin_unlock_irq(&ctx->timeout_lock); |
47f46768 | 6217 | if (ret != -ENOENT) |
505657bc PB |
6218 | goto out; |
6219 | ret = io_poll_cancel(ctx, sqe_addr, false); | |
6220 | out: | |
6221 | spin_unlock(&ctx->completion_lock); | |
6222 | return ret; | |
47f46768 JA |
6223 | } |
6224 | ||
3529d8c2 JA |
6225 | static int io_async_cancel_prep(struct io_kiocb *req, |
6226 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 6227 | { |
fbf23849 | 6228 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 6229 | return -EINVAL; |
61710e43 DA |
6230 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6231 | return -EINVAL; | |
26578cda PB |
6232 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
6233 | sqe->splice_fd_in) | |
e977d6d3 JA |
6234 | return -EINVAL; |
6235 | ||
fbf23849 JA |
6236 | req->cancel.addr = READ_ONCE(sqe->addr); |
6237 | return 0; | |
6238 | } | |
6239 | ||
61e98203 | 6240 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6241 | { |
6242 | struct io_ring_ctx *ctx = req->ctx; | |
58f99373 PB |
6243 | u64 sqe_addr = req->cancel.addr; |
6244 | struct io_tctx_node *node; | |
6245 | int ret; | |
6246 | ||
8cb01fac | 6247 | ret = io_try_cancel_userdata(req, sqe_addr); |
58f99373 PB |
6248 | if (ret != -ENOENT) |
6249 | goto done; | |
58f99373 PB |
6250 | |
6251 | /* slow path, try all io-wq's */ | |
6252 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
6253 | ret = -ENOENT; | |
6254 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
6255 | struct io_uring_task *tctx = node->task->io_uring; | |
fbf23849 | 6256 | |
58f99373 PB |
6257 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
6258 | if (ret != -ENOENT) | |
6259 | break; | |
6260 | } | |
6261 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
58f99373 | 6262 | done: |
58f99373 | 6263 | if (ret < 0) |
93d2bcd2 | 6264 | req_set_fail(req); |
505657bc | 6265 | io_req_complete_post(req, ret, 0); |
5262f567 JA |
6266 | return 0; |
6267 | } | |
6268 | ||
269bbe5f | 6269 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
6270 | const struct io_uring_sqe *sqe) |
6271 | { | |
61710e43 DA |
6272 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6273 | return -EINVAL; | |
26578cda | 6274 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
05f3fb3c JA |
6275 | return -EINVAL; |
6276 | ||
269bbe5f BM |
6277 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
6278 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
6279 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 6280 | return -EINVAL; |
269bbe5f | 6281 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
6282 | return 0; |
6283 | } | |
6284 | ||
889fca73 | 6285 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6286 | { |
6287 | struct io_ring_ctx *ctx = req->ctx; | |
c3bdad02 | 6288 | struct io_uring_rsrc_update2 up; |
05f3fb3c | 6289 | int ret; |
fbf23849 | 6290 | |
269bbe5f BM |
6291 | up.offset = req->rsrc_update.offset; |
6292 | up.data = req->rsrc_update.arg; | |
c3bdad02 PB |
6293 | up.nr = 0; |
6294 | up.tags = 0; | |
615cee49 | 6295 | up.resv = 0; |
05f3fb3c | 6296 | |
cdb31c29 | 6297 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
fdecb662 | 6298 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
98f0b3b4 | 6299 | &up, req->rsrc_update.nr_args); |
cdb31c29 | 6300 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
05f3fb3c JA |
6301 | |
6302 | if (ret < 0) | |
93d2bcd2 | 6303 | req_set_fail(req); |
889fca73 | 6304 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
6305 | return 0; |
6306 | } | |
6307 | ||
bfe76559 | 6308 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 6309 | { |
d625c6ee | 6310 | switch (req->opcode) { |
e781573e | 6311 | case IORING_OP_NOP: |
bfe76559 | 6312 | return 0; |
f67676d1 JA |
6313 | case IORING_OP_READV: |
6314 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 6315 | case IORING_OP_READ: |
bfe76559 | 6316 | return io_read_prep(req, sqe); |
f67676d1 JA |
6317 | case IORING_OP_WRITEV: |
6318 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 6319 | case IORING_OP_WRITE: |
bfe76559 | 6320 | return io_write_prep(req, sqe); |
0969e783 | 6321 | case IORING_OP_POLL_ADD: |
bfe76559 | 6322 | return io_poll_add_prep(req, sqe); |
0969e783 | 6323 | case IORING_OP_POLL_REMOVE: |
c5de0036 | 6324 | return io_poll_update_prep(req, sqe); |
8ed8d3c3 | 6325 | case IORING_OP_FSYNC: |
1155c76a | 6326 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 6327 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 6328 | return io_sfr_prep(req, sqe); |
03b1230c | 6329 | case IORING_OP_SENDMSG: |
fddaface | 6330 | case IORING_OP_SEND: |
bfe76559 | 6331 | return io_sendmsg_prep(req, sqe); |
03b1230c | 6332 | case IORING_OP_RECVMSG: |
fddaface | 6333 | case IORING_OP_RECV: |
bfe76559 | 6334 | return io_recvmsg_prep(req, sqe); |
f499a021 | 6335 | case IORING_OP_CONNECT: |
bfe76559 | 6336 | return io_connect_prep(req, sqe); |
2d28390a | 6337 | case IORING_OP_TIMEOUT: |
bfe76559 | 6338 | return io_timeout_prep(req, sqe, false); |
b29472ee | 6339 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 6340 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 6341 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 6342 | return io_async_cancel_prep(req, sqe); |
2d28390a | 6343 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 6344 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 6345 | case IORING_OP_ACCEPT: |
bfe76559 | 6346 | return io_accept_prep(req, sqe); |
d63d1b5e | 6347 | case IORING_OP_FALLOCATE: |
bfe76559 | 6348 | return io_fallocate_prep(req, sqe); |
15b71abe | 6349 | case IORING_OP_OPENAT: |
bfe76559 | 6350 | return io_openat_prep(req, sqe); |
b5dba59e | 6351 | case IORING_OP_CLOSE: |
bfe76559 | 6352 | return io_close_prep(req, sqe); |
05f3fb3c | 6353 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 6354 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6355 | case IORING_OP_STATX: |
bfe76559 | 6356 | return io_statx_prep(req, sqe); |
4840e418 | 6357 | case IORING_OP_FADVISE: |
bfe76559 | 6358 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6359 | case IORING_OP_MADVISE: |
bfe76559 | 6360 | return io_madvise_prep(req, sqe); |
cebdb986 | 6361 | case IORING_OP_OPENAT2: |
bfe76559 | 6362 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6363 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6364 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6365 | case IORING_OP_SPLICE: |
bfe76559 | 6366 | return io_splice_prep(req, sqe); |
ddf0322d | 6367 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6368 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6369 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6370 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6371 | case IORING_OP_TEE: |
bfe76559 | 6372 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6373 | case IORING_OP_SHUTDOWN: |
6374 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6375 | case IORING_OP_RENAMEAT: |
6376 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6377 | case IORING_OP_UNLINKAT: |
6378 | return io_unlinkat_prep(req, sqe); | |
e34a02dc DK |
6379 | case IORING_OP_MKDIRAT: |
6380 | return io_mkdirat_prep(req, sqe); | |
7a8721f8 DK |
6381 | case IORING_OP_SYMLINKAT: |
6382 | return io_symlinkat_prep(req, sqe); | |
cf30da90 DK |
6383 | case IORING_OP_LINKAT: |
6384 | return io_linkat_prep(req, sqe); | |
f67676d1 JA |
6385 | } |
6386 | ||
bfe76559 PB |
6387 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6388 | req->opcode); | |
bd54b6fe | 6389 | return -EINVAL; |
bfe76559 PB |
6390 | } |
6391 | ||
93642ef8 | 6392 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 6393 | { |
b7e298d2 PB |
6394 | if (!io_op_defs[req->opcode].needs_async_setup) |
6395 | return 0; | |
6396 | if (WARN_ON_ONCE(req->async_data)) | |
6397 | return -EFAULT; | |
6398 | if (io_alloc_async_data(req)) | |
6399 | return -EAGAIN; | |
6400 | ||
93642ef8 PB |
6401 | switch (req->opcode) { |
6402 | case IORING_OP_READV: | |
93642ef8 PB |
6403 | return io_rw_prep_async(req, READ); |
6404 | case IORING_OP_WRITEV: | |
93642ef8 PB |
6405 | return io_rw_prep_async(req, WRITE); |
6406 | case IORING_OP_SENDMSG: | |
93642ef8 PB |
6407 | return io_sendmsg_prep_async(req); |
6408 | case IORING_OP_RECVMSG: | |
93642ef8 PB |
6409 | return io_recvmsg_prep_async(req); |
6410 | case IORING_OP_CONNECT: | |
6411 | return io_connect_prep_async(req); | |
6412 | } | |
b7e298d2 PB |
6413 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
6414 | req->opcode); | |
6415 | return -EFAULT; | |
f67676d1 JA |
6416 | } |
6417 | ||
9cf7c104 PB |
6418 | static u32 io_get_sequence(struct io_kiocb *req) |
6419 | { | |
a3dbdf54 | 6420 | u32 seq = req->ctx->cached_sq_head; |
9cf7c104 | 6421 | |
a3dbdf54 PB |
6422 | /* need original cached_sq_head, but it was increased for each req */ |
6423 | io_for_each_link(req, req) | |
6424 | seq--; | |
6425 | return seq; | |
9cf7c104 PB |
6426 | } |
6427 | ||
c072481d | 6428 | static __cold void io_drain_req(struct io_kiocb *req) |
de0617e4 | 6429 | { |
a197f664 | 6430 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6431 | struct io_defer_entry *de; |
f67676d1 | 6432 | int ret; |
e0eb71dc | 6433 | u32 seq = io_get_sequence(req); |
de0617e4 | 6434 | |
9d858b21 | 6435 | /* Still need defer if there is pending req in defer list. */ |
5e371265 | 6436 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) { |
e0eb71dc | 6437 | queue: |
5e371265 | 6438 | ctx->drain_active = false; |
e0eb71dc PB |
6439 | io_req_task_queue(req); |
6440 | return; | |
5e371265 | 6441 | } |
de0617e4 | 6442 | |
b7e298d2 | 6443 | ret = io_req_prep_async(req); |
e0eb71dc PB |
6444 | if (ret) { |
6445 | fail: | |
6446 | io_req_complete_failed(req, ret); | |
6447 | return; | |
6448 | } | |
cbdcb435 | 6449 | io_prep_async_link(req); |
27dc8338 | 6450 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
76cc33d7 | 6451 | if (!de) { |
1b48773f | 6452 | ret = -ENOMEM; |
e0eb71dc | 6453 | goto fail; |
76cc33d7 | 6454 | } |
2d28390a | 6455 | |
79ebeaee | 6456 | spin_lock(&ctx->completion_lock); |
9cf7c104 | 6457 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
79ebeaee | 6458 | spin_unlock(&ctx->completion_lock); |
27dc8338 | 6459 | kfree(de); |
e0eb71dc | 6460 | goto queue; |
de0617e4 JA |
6461 | } |
6462 | ||
915967f6 | 6463 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 6464 | de->req = req; |
9cf7c104 | 6465 | de->seq = seq; |
27dc8338 | 6466 | list_add_tail(&de->list, &ctx->defer_list); |
79ebeaee | 6467 | spin_unlock(&ctx->completion_lock); |
de0617e4 JA |
6468 | } |
6469 | ||
68fb8979 | 6470 | static void io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6471 | { |
0e1b6fe3 | 6472 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
30d51dd4 PB |
6473 | kfree(req->kbuf); |
6474 | req->kbuf = NULL; | |
99bc4c38 PB |
6475 | } |
6476 | ||
0e1b6fe3 PB |
6477 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6478 | switch (req->opcode) { | |
6479 | case IORING_OP_READV: | |
6480 | case IORING_OP_READ_FIXED: | |
6481 | case IORING_OP_READ: | |
6482 | case IORING_OP_WRITEV: | |
6483 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6484 | case IORING_OP_WRITE: { |
6485 | struct io_async_rw *io = req->async_data; | |
1dacb4df PB |
6486 | |
6487 | kfree(io->free_iovec); | |
0e1b6fe3 | 6488 | break; |
e8c2bc1f | 6489 | } |
0e1b6fe3 | 6490 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6491 | case IORING_OP_SENDMSG: { |
6492 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
6493 | |
6494 | kfree(io->free_iov); | |
0e1b6fe3 | 6495 | break; |
e8c2bc1f | 6496 | } |
0e1b6fe3 PB |
6497 | case IORING_OP_SPLICE: |
6498 | case IORING_OP_TEE: | |
e1d767f0 PB |
6499 | if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED)) |
6500 | io_put_file(req->splice.file_in); | |
0e1b6fe3 | 6501 | break; |
f3cd4850 JA |
6502 | case IORING_OP_OPENAT: |
6503 | case IORING_OP_OPENAT2: | |
6504 | if (req->open.filename) | |
6505 | putname(req->open.filename); | |
6506 | break; | |
80a261fd JA |
6507 | case IORING_OP_RENAMEAT: |
6508 | putname(req->rename.oldpath); | |
6509 | putname(req->rename.newpath); | |
6510 | break; | |
14a1143b JA |
6511 | case IORING_OP_UNLINKAT: |
6512 | putname(req->unlink.filename); | |
6513 | break; | |
e34a02dc DK |
6514 | case IORING_OP_MKDIRAT: |
6515 | putname(req->mkdir.filename); | |
6516 | break; | |
7a8721f8 DK |
6517 | case IORING_OP_SYMLINKAT: |
6518 | putname(req->symlink.oldpath); | |
6519 | putname(req->symlink.newpath); | |
6520 | break; | |
cf30da90 DK |
6521 | case IORING_OP_LINKAT: |
6522 | putname(req->hardlink.oldpath); | |
6523 | putname(req->hardlink.newpath); | |
6524 | break; | |
0e1b6fe3 | 6525 | } |
99bc4c38 | 6526 | } |
75652a30 JA |
6527 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
6528 | kfree(req->apoll->double_poll); | |
6529 | kfree(req->apoll); | |
6530 | req->apoll = NULL; | |
6531 | } | |
3a0a6902 PB |
6532 | if (req->flags & REQ_F_INFLIGHT) { |
6533 | struct io_uring_task *tctx = req->task->io_uring; | |
6534 | ||
6535 | atomic_dec(&tctx->inflight_tracked); | |
3a0a6902 | 6536 | } |
c854357b | 6537 | if (req->flags & REQ_F_CREDS) |
b8e64b53 | 6538 | put_cred(req->creds); |
c854357b PB |
6539 | |
6540 | req->flags &= ~IO_REQ_CLEAN_FLAGS; | |
99bc4c38 PB |
6541 | } |
6542 | ||
889fca73 | 6543 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 6544 | { |
a197f664 | 6545 | struct io_ring_ctx *ctx = req->ctx; |
5730b27e | 6546 | const struct cred *creds = NULL; |
d625c6ee | 6547 | int ret; |
2b188cc1 | 6548 | |
6878b40e | 6549 | if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred())) |
c10d1f98 | 6550 | creds = override_creds(req->creds); |
5730b27e | 6551 | |
d625c6ee | 6552 | switch (req->opcode) { |
2b188cc1 | 6553 | case IORING_OP_NOP: |
889fca73 | 6554 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
6555 | break; |
6556 | case IORING_OP_READV: | |
edafccee | 6557 | case IORING_OP_READ_FIXED: |
3a6820f2 | 6558 | case IORING_OP_READ: |
889fca73 | 6559 | ret = io_read(req, issue_flags); |
edafccee | 6560 | break; |
3529d8c2 | 6561 | case IORING_OP_WRITEV: |
edafccee | 6562 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 6563 | case IORING_OP_WRITE: |
889fca73 | 6564 | ret = io_write(req, issue_flags); |
2b188cc1 | 6565 | break; |
c992fe29 | 6566 | case IORING_OP_FSYNC: |
45d189c6 | 6567 | ret = io_fsync(req, issue_flags); |
c992fe29 | 6568 | break; |
221c5eb2 | 6569 | case IORING_OP_POLL_ADD: |
61e98203 | 6570 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
6571 | break; |
6572 | case IORING_OP_POLL_REMOVE: | |
c5de0036 | 6573 | ret = io_poll_update(req, issue_flags); |
221c5eb2 | 6574 | break; |
5d17b4a4 | 6575 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 6576 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 6577 | break; |
0fa03c62 | 6578 | case IORING_OP_SENDMSG: |
889fca73 | 6579 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 6580 | break; |
fddaface | 6581 | case IORING_OP_SEND: |
889fca73 | 6582 | ret = io_send(req, issue_flags); |
0fa03c62 | 6583 | break; |
aa1fa28f | 6584 | case IORING_OP_RECVMSG: |
889fca73 | 6585 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 6586 | break; |
fddaface | 6587 | case IORING_OP_RECV: |
889fca73 | 6588 | ret = io_recv(req, issue_flags); |
aa1fa28f | 6589 | break; |
5262f567 | 6590 | case IORING_OP_TIMEOUT: |
61e98203 | 6591 | ret = io_timeout(req, issue_flags); |
5262f567 | 6592 | break; |
11365043 | 6593 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 6594 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 6595 | break; |
17f2fe35 | 6596 | case IORING_OP_ACCEPT: |
889fca73 | 6597 | ret = io_accept(req, issue_flags); |
17f2fe35 | 6598 | break; |
f8e85cf2 | 6599 | case IORING_OP_CONNECT: |
889fca73 | 6600 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 6601 | break; |
62755e35 | 6602 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 6603 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 6604 | break; |
d63d1b5e | 6605 | case IORING_OP_FALLOCATE: |
45d189c6 | 6606 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 6607 | break; |
15b71abe | 6608 | case IORING_OP_OPENAT: |
45d189c6 | 6609 | ret = io_openat(req, issue_flags); |
15b71abe | 6610 | break; |
b5dba59e | 6611 | case IORING_OP_CLOSE: |
889fca73 | 6612 | ret = io_close(req, issue_flags); |
b5dba59e | 6613 | break; |
05f3fb3c | 6614 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6615 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6616 | break; |
eddc7ef5 | 6617 | case IORING_OP_STATX: |
45d189c6 | 6618 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6619 | break; |
4840e418 | 6620 | case IORING_OP_FADVISE: |
45d189c6 | 6621 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6622 | break; |
c1ca757b | 6623 | case IORING_OP_MADVISE: |
45d189c6 | 6624 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6625 | break; |
cebdb986 | 6626 | case IORING_OP_OPENAT2: |
45d189c6 | 6627 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6628 | break; |
3e4827b0 | 6629 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6630 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6631 | break; |
7d67af2c | 6632 | case IORING_OP_SPLICE: |
45d189c6 | 6633 | ret = io_splice(req, issue_flags); |
7d67af2c | 6634 | break; |
ddf0322d | 6635 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6636 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6637 | break; |
067524e9 | 6638 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6639 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6640 | break; |
f2a8d5c7 | 6641 | case IORING_OP_TEE: |
45d189c6 | 6642 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6643 | break; |
36f4fa68 | 6644 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6645 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6646 | break; |
80a261fd | 6647 | case IORING_OP_RENAMEAT: |
45d189c6 | 6648 | ret = io_renameat(req, issue_flags); |
80a261fd | 6649 | break; |
14a1143b | 6650 | case IORING_OP_UNLINKAT: |
45d189c6 | 6651 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6652 | break; |
e34a02dc DK |
6653 | case IORING_OP_MKDIRAT: |
6654 | ret = io_mkdirat(req, issue_flags); | |
6655 | break; | |
7a8721f8 DK |
6656 | case IORING_OP_SYMLINKAT: |
6657 | ret = io_symlinkat(req, issue_flags); | |
6658 | break; | |
cf30da90 DK |
6659 | case IORING_OP_LINKAT: |
6660 | ret = io_linkat(req, issue_flags); | |
6661 | break; | |
2b188cc1 JA |
6662 | default: |
6663 | ret = -EINVAL; | |
6664 | break; | |
6665 | } | |
6666 | ||
5730b27e JA |
6667 | if (creds) |
6668 | revert_creds(creds); | |
def596e9 JA |
6669 | if (ret) |
6670 | return ret; | |
b532576e | 6671 | /* If the op doesn't have a file, we're not polling for it */ |
cb3d8972 PB |
6672 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
6673 | io_iopoll_req_issued(req); | |
def596e9 JA |
6674 | |
6675 | return 0; | |
2b188cc1 JA |
6676 | } |
6677 | ||
ebc11b6c PB |
6678 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
6679 | { | |
6680 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6681 | ||
6682 | req = io_put_req_find_next(req); | |
6683 | return req ? &req->work : NULL; | |
6684 | } | |
6685 | ||
5280f7e5 | 6686 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6687 | { |
6688 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6689 | struct io_kiocb *timeout; |
561fb04a | 6690 | int ret = 0; |
2b188cc1 | 6691 | |
48dcd38d PB |
6692 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
6693 | if (!(req->flags & REQ_F_REFCOUNT)) | |
6694 | __io_req_set_refcount(req, 2); | |
6695 | else | |
6696 | req_ref_get(req); | |
5d5901a3 | 6697 | |
6df1db6b PB |
6698 | timeout = io_prep_linked_timeout(req); |
6699 | if (timeout) | |
6700 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6701 | |
dadebc35 | 6702 | /* either cancelled or io-wq is dying, so don't touch tctx->iowq */ |
4014d943 | 6703 | if (work->flags & IO_WQ_WORK_CANCEL) |
561fb04a | 6704 | ret = -ECANCELED; |
31b51510 | 6705 | |
561fb04a | 6706 | if (!ret) { |
561fb04a | 6707 | do { |
889fca73 | 6708 | ret = io_issue_sqe(req, 0); |
561fb04a JA |
6709 | /* |
6710 | * We can get EAGAIN for polled IO even though we're | |
6711 | * forcing a sync submission from here, since we can't | |
6712 | * wait for request slots on the block side. | |
6713 | */ | |
6714 | if (ret != -EAGAIN) | |
6715 | break; | |
6716 | cond_resched(); | |
6717 | } while (1); | |
6718 | } | |
31b51510 | 6719 | |
a3df7698 | 6720 | /* avoid locking problems by failing it from a clean context */ |
5d5901a3 | 6721 | if (ret) |
a3df7698 | 6722 | io_req_task_queue_fail(req, ret); |
2b188cc1 JA |
6723 | } |
6724 | ||
aeca241b | 6725 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
042b0d85 | 6726 | unsigned i) |
65e19f54 | 6727 | { |
042b0d85 | 6728 | return &table->files[i]; |
dafecf19 PB |
6729 | } |
6730 | ||
65e19f54 JA |
6731 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6732 | int index) | |
6733 | { | |
aeca241b | 6734 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
65e19f54 | 6735 | |
a04b0ac0 | 6736 | return (struct file *) (slot->file_ptr & FFS_MASK); |
65e19f54 JA |
6737 | } |
6738 | ||
a04b0ac0 | 6739 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
9a321c98 PB |
6740 | { |
6741 | unsigned long file_ptr = (unsigned long) file; | |
6742 | ||
b191e2df | 6743 | if (__io_file_supports_nowait(file, READ)) |
9a321c98 | 6744 | file_ptr |= FFS_ASYNC_READ; |
b191e2df | 6745 | if (__io_file_supports_nowait(file, WRITE)) |
9a321c98 PB |
6746 | file_ptr |= FFS_ASYNC_WRITE; |
6747 | if (S_ISREG(file_inode(file)->i_mode)) | |
6748 | file_ptr |= FFS_ISREG; | |
a04b0ac0 | 6749 | file_slot->file_ptr = file_ptr; |
65e19f54 JA |
6750 | } |
6751 | ||
ac177053 PB |
6752 | static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx, |
6753 | struct io_kiocb *req, int fd) | |
09bb8394 | 6754 | { |
8da11c19 | 6755 | struct file *file; |
ac177053 | 6756 | unsigned long file_ptr; |
09bb8394 | 6757 | |
ac177053 PB |
6758 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
6759 | return NULL; | |
6760 | fd = array_index_nospec(fd, ctx->nr_user_files); | |
6761 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; | |
6762 | file = (struct file *) (file_ptr & FFS_MASK); | |
6763 | file_ptr &= ~FFS_MASK; | |
6764 | /* mask in overlapping REQ_F and FFS bits */ | |
b191e2df | 6765 | req->flags |= (file_ptr << REQ_F_NOWAIT_READ_BIT); |
ac177053 PB |
6766 | io_req_set_rsrc_node(req); |
6767 | return file; | |
6768 | } | |
d44f554e | 6769 | |
ac177053 | 6770 | static struct file *io_file_get_normal(struct io_ring_ctx *ctx, |
ac177053 PB |
6771 | struct io_kiocb *req, int fd) |
6772 | { | |
62906e89 | 6773 | struct file *file = fget(fd); |
ac177053 PB |
6774 | |
6775 | trace_io_uring_file_get(ctx, fd); | |
09bb8394 | 6776 | |
ac177053 PB |
6777 | /* we don't allow fixed io_uring files */ |
6778 | if (file && unlikely(file->f_op == &io_uring_fops)) | |
6779 | io_req_track_inflight(req); | |
8371adf5 | 6780 | return file; |
09bb8394 JA |
6781 | } |
6782 | ||
ac177053 | 6783 | static inline struct file *io_file_get(struct io_ring_ctx *ctx, |
ac177053 PB |
6784 | struct io_kiocb *req, int fd, bool fixed) |
6785 | { | |
6786 | if (fixed) | |
6787 | return io_file_get_fixed(ctx, req, fd); | |
6788 | else | |
62906e89 | 6789 | return io_file_get_normal(ctx, req, fd); |
ac177053 PB |
6790 | } |
6791 | ||
f237c30a | 6792 | static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked) |
89b263f6 JA |
6793 | { |
6794 | struct io_kiocb *prev = req->timeout.prev; | |
8cb01fac | 6795 | int ret; |
89b263f6 JA |
6796 | |
6797 | if (prev) { | |
8cb01fac | 6798 | ret = io_try_cancel_userdata(req, prev->user_data); |
505657bc | 6799 | io_req_complete_post(req, ret ?: -ETIME, 0); |
89b263f6 | 6800 | io_put_req(prev); |
89b263f6 JA |
6801 | } else { |
6802 | io_req_complete_post(req, -ETIME, 0); | |
6803 | } | |
6804 | } | |
6805 | ||
2665abfd | 6806 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6807 | { |
ad8a48ac JA |
6808 | struct io_timeout_data *data = container_of(timer, |
6809 | struct io_timeout_data, timer); | |
90cd7e42 | 6810 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6811 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6812 | unsigned long flags; |
2665abfd | 6813 | |
89b263f6 | 6814 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
90cd7e42 PB |
6815 | prev = req->timeout.head; |
6816 | req->timeout.head = NULL; | |
2665abfd JA |
6817 | |
6818 | /* | |
6819 | * We don't expect the list to be empty, that will only happen if we | |
6820 | * race with the completion of the linked work. | |
6821 | */ | |
447c19f3 | 6822 | if (prev) { |
f2f87370 | 6823 | io_remove_next_linked(prev); |
447c19f3 PB |
6824 | if (!req_ref_inc_not_zero(prev)) |
6825 | prev = NULL; | |
6826 | } | |
ef9dd637 | 6827 | list_del(&req->timeout.list); |
89b263f6 JA |
6828 | req->timeout.prev = prev; |
6829 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); | |
2665abfd | 6830 | |
89b263f6 JA |
6831 | req->io_task_work.func = io_req_task_link_timeout; |
6832 | io_req_task_work_add(req); | |
2665abfd JA |
6833 | return HRTIMER_NORESTART; |
6834 | } | |
6835 | ||
de968c18 | 6836 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6837 | { |
de968c18 PB |
6838 | struct io_ring_ctx *ctx = req->ctx; |
6839 | ||
89b263f6 | 6840 | spin_lock_irq(&ctx->timeout_lock); |
76a46e06 | 6841 | /* |
f2f87370 PB |
6842 | * If the back reference is NULL, then our linked request finished |
6843 | * before we got a chance to setup the timer | |
76a46e06 | 6844 | */ |
90cd7e42 | 6845 | if (req->timeout.head) { |
e8c2bc1f | 6846 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6847 | |
ad8a48ac JA |
6848 | data->timer.function = io_link_timeout_fn; |
6849 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6850 | data->mode); | |
ef9dd637 | 6851 | list_add_tail(&req->timeout.list, &ctx->ltimeout_list); |
2665abfd | 6852 | } |
89b263f6 | 6853 | spin_unlock_irq(&ctx->timeout_lock); |
2665abfd | 6854 | /* drop submission reference */ |
76a46e06 JA |
6855 | io_put_req(req); |
6856 | } | |
2665abfd | 6857 | |
d475a9a6 PB |
6858 | static void io_queue_sqe_arm_apoll(struct io_kiocb *req) |
6859 | __must_hold(&req->ctx->uring_lock) | |
6860 | { | |
6861 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); | |
6862 | ||
6863 | switch (io_arm_poll_handler(req)) { | |
6864 | case IO_APOLL_READY: | |
6865 | if (linked_timeout) { | |
6866 | io_unprep_linked_timeout(req); | |
6867 | linked_timeout = NULL; | |
6868 | } | |
6869 | io_req_task_queue(req); | |
6870 | break; | |
6871 | case IO_APOLL_ABORTED: | |
6872 | /* | |
6873 | * Queued up for async execution, worker will release | |
6874 | * submit reference when the iocb is actually submitted. | |
6875 | */ | |
6876 | io_queue_async_work(req, NULL); | |
6877 | break; | |
6878 | } | |
6879 | ||
6880 | if (linked_timeout) | |
6881 | io_queue_linked_timeout(linked_timeout); | |
6882 | } | |
6883 | ||
6884 | static inline void __io_queue_sqe(struct io_kiocb *req) | |
282cdc86 | 6885 | __must_hold(&req->ctx->uring_lock) |
2b188cc1 | 6886 | { |
906c6caa | 6887 | struct io_kiocb *linked_timeout; |
e0c5c576 | 6888 | int ret; |
2b188cc1 | 6889 | |
c5eef2b9 | 6890 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 6891 | |
fff4e40e PB |
6892 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
6893 | io_req_add_compl_list(req); | |
d9f9d284 | 6894 | return; |
fff4e40e | 6895 | } |
491381ce JA |
6896 | /* |
6897 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6898 | * doesn't support non-blocking read/write attempts | |
6899 | */ | |
1840038e | 6900 | if (likely(!ret)) { |
906c6caa PB |
6901 | linked_timeout = io_prep_linked_timeout(req); |
6902 | if (linked_timeout) | |
6903 | io_queue_linked_timeout(linked_timeout); | |
1840038e | 6904 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
d475a9a6 | 6905 | io_queue_sqe_arm_apoll(req); |
0d63c148 | 6906 | } else { |
f41db273 | 6907 | io_req_complete_failed(req, ret); |
9e645e11 | 6908 | } |
2b188cc1 JA |
6909 | } |
6910 | ||
4652fe3f | 6911 | static void io_queue_sqe_fallback(struct io_kiocb *req) |
282cdc86 | 6912 | __must_hold(&req->ctx->uring_lock) |
4fe2c963 | 6913 | { |
4652fe3f | 6914 | if (req->flags & REQ_F_FAIL) { |
c6d3d9cb | 6915 | io_req_complete_fail_submit(req); |
e0eb71dc PB |
6916 | } else if (unlikely(req->ctx->drain_active)) { |
6917 | io_drain_req(req); | |
76cc33d7 PB |
6918 | } else { |
6919 | int ret = io_req_prep_async(req); | |
6920 | ||
6921 | if (unlikely(ret)) | |
6922 | io_req_complete_failed(req, ret); | |
6923 | else | |
f237c30a | 6924 | io_queue_async_work(req, NULL); |
ce35a47a | 6925 | } |
4fe2c963 JL |
6926 | } |
6927 | ||
4652fe3f PB |
6928 | static inline void io_queue_sqe(struct io_kiocb *req) |
6929 | __must_hold(&req->ctx->uring_lock) | |
6930 | { | |
6931 | if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) | |
6932 | __io_queue_sqe(req); | |
6933 | else | |
6934 | io_queue_sqe_fallback(req); | |
6935 | } | |
6936 | ||
b16fed66 PB |
6937 | /* |
6938 | * Check SQE restrictions (opcode and flags). | |
6939 | * | |
6940 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6941 | */ | |
6942 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6943 | struct io_kiocb *req, | |
6944 | unsigned int sqe_flags) | |
4fe2c963 | 6945 | { |
b16fed66 PB |
6946 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
6947 | return false; | |
6948 | ||
6949 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6950 | ctx->restrictions.sqe_flags_required) | |
6951 | return false; | |
6952 | ||
6953 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
6954 | ctx->restrictions.sqe_flags_required)) | |
6955 | return false; | |
6956 | ||
6957 | return true; | |
4fe2c963 JL |
6958 | } |
6959 | ||
22b2ca31 PB |
6960 | static void io_init_req_drain(struct io_kiocb *req) |
6961 | { | |
6962 | struct io_ring_ctx *ctx = req->ctx; | |
6963 | struct io_kiocb *head = ctx->submit_state.link.head; | |
6964 | ||
6965 | ctx->drain_active = true; | |
6966 | if (head) { | |
6967 | /* | |
6968 | * If we need to drain a request in the middle of a link, drain | |
6969 | * the head request and the next request/link after the current | |
6970 | * link. Considering sequential execution of links, | |
6971 | * IOSQE_IO_DRAIN will be maintained for every request of our | |
6972 | * link. | |
6973 | */ | |
6974 | head->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC; | |
6975 | ctx->drain_next = true; | |
6976 | } | |
6977 | } | |
6978 | ||
b16fed66 PB |
6979 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
6980 | const struct io_uring_sqe *sqe) | |
282cdc86 | 6981 | __must_hold(&ctx->uring_lock) |
b16fed66 PB |
6982 | { |
6983 | struct io_submit_state *state; | |
6984 | unsigned int sqe_flags; | |
fc0ae024 | 6985 | int personality; |
b16fed66 | 6986 | |
864ea921 | 6987 | /* req is partially pre-initialised, see io_preinit_req() */ |
b16fed66 PB |
6988 | req->opcode = READ_ONCE(sqe->opcode); |
6989 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
6990 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
6991 | req->user_data = READ_ONCE(sqe->user_data); | |
b16fed66 | 6992 | req->file = NULL; |
b16fed66 | 6993 | req->fixed_rsrc_refs = NULL; |
b16fed66 | 6994 | req->task = current; |
b16fed66 | 6995 | |
b16fed66 PB |
6996 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
6997 | return -EINVAL; | |
68fe256a PB |
6998 | if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) { |
6999 | /* enforce forwards compatibility on users */ | |
7000 | if (sqe_flags & ~SQE_VALID_FLAGS) | |
7001 | return -EINVAL; | |
7002 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
7003 | !io_op_defs[req->opcode].buffer_select) | |
7004 | return -EOPNOTSUPP; | |
22b2ca31 PB |
7005 | if (sqe_flags & IOSQE_IO_DRAIN) |
7006 | io_init_req_drain(req); | |
2a56a9bd PB |
7007 | } |
7008 | if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) { | |
7009 | if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags)) | |
7010 | return -EACCES; | |
7011 | /* knock it to the slow queue path, will be drained there */ | |
7012 | if (ctx->drain_active) | |
7013 | req->flags |= REQ_F_FORCE_ASYNC; | |
7014 | /* if there is no link, we're at "next" request and need to drain */ | |
7015 | if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) { | |
7016 | ctx->drain_next = false; | |
7017 | ctx->drain_active = true; | |
22b2ca31 | 7018 | req->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC; |
2a56a9bd | 7019 | } |
68fe256a | 7020 | } |
b16fed66 | 7021 | |
003e8dcc JA |
7022 | personality = READ_ONCE(sqe->personality); |
7023 | if (personality) { | |
c10d1f98 PB |
7024 | req->creds = xa_load(&ctx->personalities, personality); |
7025 | if (!req->creds) | |
003e8dcc | 7026 | return -EINVAL; |
c10d1f98 | 7027 | get_cred(req->creds); |
b8e64b53 | 7028 | req->flags |= REQ_F_CREDS; |
003e8dcc | 7029 | } |
b16fed66 PB |
7030 | state = &ctx->submit_state; |
7031 | ||
7032 | /* | |
7033 | * Plug now if we have more than 1 IO left after this, and the target | |
7034 | * is potentially a read/write to block based storage. | |
7035 | */ | |
4b628aeb | 7036 | if (state->need_plug && io_op_defs[req->opcode].plug) { |
b16fed66 PB |
7037 | blk_start_plug(&state->plug); |
7038 | state->plug_started = true; | |
4b628aeb | 7039 | state->need_plug = false; |
b16fed66 PB |
7040 | } |
7041 | ||
7042 | if (io_op_defs[req->opcode].needs_file) { | |
62906e89 | 7043 | req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd), |
ac177053 | 7044 | (sqe_flags & IOSQE_FIXED_FILE)); |
b16fed66 | 7045 | if (unlikely(!req->file)) |
fc0ae024 | 7046 | return -EBADF; |
b16fed66 | 7047 | } |
fc0ae024 PB |
7048 | |
7049 | return io_req_prep(req, sqe); | |
b16fed66 PB |
7050 | } |
7051 | ||
a6b8cadc | 7052 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 7053 | const struct io_uring_sqe *sqe) |
282cdc86 | 7054 | __must_hold(&ctx->uring_lock) |
9e645e11 | 7055 | { |
a1ab7b35 | 7056 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 7057 | int ret; |
9e645e11 | 7058 | |
a6b8cadc PB |
7059 | ret = io_init_req(ctx, req, sqe); |
7060 | if (unlikely(ret)) { | |
a87acfde JA |
7061 | trace_io_uring_req_failed(sqe, ret); |
7062 | ||
a8295b98 | 7063 | /* fail even hard links since we don't submit */ |
de59bc10 | 7064 | if (link->head) { |
a8295b98 HX |
7065 | /* |
7066 | * we can judge a link req is failed or cancelled by if | |
7067 | * REQ_F_FAIL is set, but the head is an exception since | |
7068 | * it may be set REQ_F_FAIL because of other req's failure | |
7069 | * so let's leverage req->result to distinguish if a head | |
7070 | * is set REQ_F_FAIL because of its failure or other req's | |
7071 | * failure so that we can set the correct ret code for it. | |
7072 | * init result here to avoid affecting the normal path. | |
7073 | */ | |
7074 | if (!(link->head->flags & REQ_F_FAIL)) | |
7075 | req_fail_link_node(link->head, -ECANCELED); | |
7076 | } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { | |
7077 | /* | |
7078 | * the current req is a normal req, we should return | |
7079 | * error and thus break the submittion loop. | |
7080 | */ | |
7081 | io_req_complete_failed(req, ret); | |
7082 | return ret; | |
de59bc10 | 7083 | } |
a8295b98 | 7084 | req_fail_link_node(req, ret); |
a6b8cadc | 7085 | } |
441b8a78 | 7086 | |
be7053b7 | 7087 | /* don't need @sqe from now on */ |
236daeae OL |
7088 | trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data, |
7089 | req->flags, true, | |
7090 | ctx->flags & IORING_SETUP_SQPOLL); | |
a6b8cadc | 7091 | |
9e645e11 JA |
7092 | /* |
7093 | * If we already have a head request, queue this one for async | |
7094 | * submittal once the head completes. If we don't have a head but | |
7095 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
7096 | * submitted sync once the chain is complete. If none of those | |
7097 | * conditions are true (normal request), then just queue it. | |
7098 | */ | |
863e0560 PB |
7099 | if (link->head) { |
7100 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 7101 | |
a8295b98 HX |
7102 | if (!(req->flags & REQ_F_FAIL)) { |
7103 | ret = io_req_prep_async(req); | |
7104 | if (unlikely(ret)) { | |
7105 | req_fail_link_node(req, ret); | |
7106 | if (!(head->flags & REQ_F_FAIL)) | |
7107 | req_fail_link_node(head, -ECANCELED); | |
7108 | } | |
7109 | } | |
9d76377f | 7110 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 7111 | link->last->link = req; |
863e0560 | 7112 | link->last = req; |
32fe525b | 7113 | |
f15a3431 PB |
7114 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) |
7115 | return 0; | |
32fe525b | 7116 | /* last request of a link, enqueue the link */ |
f15a3431 PB |
7117 | link->head = NULL; |
7118 | req = head; | |
7119 | } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { | |
7120 | link->head = req; | |
7121 | link->last = req; | |
7122 | return 0; | |
9e645e11 | 7123 | } |
2e6e1fde | 7124 | |
f15a3431 | 7125 | io_queue_sqe(req); |
1d4240cc | 7126 | return 0; |
9e645e11 JA |
7127 | } |
7128 | ||
9a56a232 JA |
7129 | /* |
7130 | * Batched submission is done, ensure local IO is flushed out. | |
7131 | */ | |
553deffd | 7132 | static void io_submit_state_end(struct io_ring_ctx *ctx) |
9a56a232 | 7133 | { |
553deffd PB |
7134 | struct io_submit_state *state = &ctx->submit_state; |
7135 | ||
a1ab7b35 | 7136 | if (state->link.head) |
de59bc10 | 7137 | io_queue_sqe(state->link.head); |
553deffd | 7138 | /* flush only after queuing links as they can generate completions */ |
c450178d | 7139 | io_submit_flush_completions(ctx); |
27926b68 JA |
7140 | if (state->plug_started) |
7141 | blk_finish_plug(&state->plug); | |
9a56a232 JA |
7142 | } |
7143 | ||
7144 | /* | |
7145 | * Start submission side cache. | |
7146 | */ | |
7147 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 7148 | unsigned int max_ios) |
9a56a232 | 7149 | { |
27926b68 | 7150 | state->plug_started = false; |
4b628aeb | 7151 | state->need_plug = max_ios > 2; |
a1ab7b35 PB |
7152 | /* set only head, no need to init link_last in advance */ |
7153 | state->link.head = NULL; | |
9a56a232 JA |
7154 | } |
7155 | ||
2b188cc1 JA |
7156 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
7157 | { | |
75b28aff | 7158 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 7159 | |
caf582c6 PB |
7160 | /* |
7161 | * Ensure any loads from the SQEs are done at this point, | |
7162 | * since once we write the new head, the application could | |
7163 | * write new data to them. | |
7164 | */ | |
7165 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
7166 | } |
7167 | ||
2b188cc1 | 7168 | /* |
dd9ae8a0 | 7169 | * Fetch an sqe, if one is available. Note this returns a pointer to memory |
2b188cc1 JA |
7170 | * that is mapped by userspace. This means that care needs to be taken to |
7171 | * ensure that reads are stable, as we cannot rely on userspace always | |
7172 | * being a good citizen. If members of the sqe are validated and then later | |
7173 | * used, it's important that those reads are done through READ_ONCE() to | |
7174 | * prevent a re-load down the line. | |
7175 | */ | |
709b302f | 7176 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 7177 | { |
ea5ab3b5 | 7178 | unsigned head, mask = ctx->sq_entries - 1; |
17d3aeb3 | 7179 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
2b188cc1 JA |
7180 | |
7181 | /* | |
7182 | * The cached sq head (or cq tail) serves two purposes: | |
7183 | * | |
7184 | * 1) allows us to batch the cost of updating the user visible | |
7185 | * head updates. | |
7186 | * 2) allows the kernel side to track the head on its own, even | |
7187 | * though the application is the one updating it. | |
7188 | */ | |
17d3aeb3 | 7189 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
709b302f PB |
7190 | if (likely(head < ctx->sq_entries)) |
7191 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
7192 | |
7193 | /* drop invalid entries */ | |
15641e42 PB |
7194 | ctx->cq_extra--; |
7195 | WRITE_ONCE(ctx->rings->sq_dropped, | |
7196 | READ_ONCE(ctx->rings->sq_dropped) + 1); | |
709b302f PB |
7197 | return NULL; |
7198 | } | |
7199 | ||
0f212204 | 7200 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
282cdc86 | 7201 | __must_hold(&ctx->uring_lock) |
6c271ce2 | 7202 | { |
69629809 | 7203 | unsigned int entries = io_sqring_entries(ctx); |
46c4e16a | 7204 | int submitted = 0; |
6c271ce2 | 7205 | |
51d48dab | 7206 | if (unlikely(!entries)) |
69629809 | 7207 | return 0; |
ee7d46d9 | 7208 | /* make sure SQ entry isn't read before tail */ |
69629809 | 7209 | nr = min3(nr, ctx->sq_entries, entries); |
9a10867a | 7210 | io_get_task_refs(nr); |
6c271ce2 | 7211 | |
ba88ff11 | 7212 | io_submit_state_start(&ctx->submit_state, nr); |
69629809 | 7213 | do { |
3529d8c2 | 7214 | const struct io_uring_sqe *sqe; |
196be95c | 7215 | struct io_kiocb *req; |
fb5ccc98 | 7216 | |
a33ae9ce | 7217 | if (unlikely(!io_alloc_req_refill(ctx))) { |
196be95c PB |
7218 | if (!submitted) |
7219 | submitted = -EAGAIN; | |
fb5ccc98 | 7220 | break; |
196be95c | 7221 | } |
a33ae9ce | 7222 | req = io_alloc_req(ctx); |
4fccfcbb PB |
7223 | sqe = io_get_sqe(ctx); |
7224 | if (unlikely(!sqe)) { | |
c2b6c6bc | 7225 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
4fccfcbb PB |
7226 | break; |
7227 | } | |
d3656344 JA |
7228 | /* will complete beyond this point, count as submitted */ |
7229 | submitted++; | |
a1ab7b35 | 7230 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 7231 | break; |
69629809 | 7232 | } while (submitted < nr); |
6c271ce2 | 7233 | |
9466f437 PB |
7234 | if (unlikely(submitted != nr)) { |
7235 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 | 7236 | int unused = nr - ref_used; |
9466f437 | 7237 | |
09899b19 | 7238 | current->io_uring->cached_refs += unused; |
9466f437 | 7239 | } |
6c271ce2 | 7240 | |
553deffd | 7241 | io_submit_state_end(ctx); |
ae9428ca PB |
7242 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
7243 | io_commit_sqring(ctx); | |
7244 | ||
6c271ce2 JA |
7245 | return submitted; |
7246 | } | |
7247 | ||
e4b6d902 PB |
7248 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
7249 | { | |
7250 | return READ_ONCE(sqd->state); | |
7251 | } | |
7252 | ||
23b3628e XW |
7253 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
7254 | { | |
7255 | /* Tell userspace we may need a wakeup call */ | |
79ebeaee | 7256 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7257 | WRITE_ONCE(ctx->rings->sq_flags, |
7258 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7259 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7260 | } |
7261 | ||
7262 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
7263 | { | |
79ebeaee | 7264 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7265 | WRITE_ONCE(ctx->rings->sq_flags, |
7266 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7267 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7268 | } |
7269 | ||
08369246 | 7270 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 7271 | { |
c8d1ba58 | 7272 | unsigned int to_submit; |
bdcd3eab | 7273 | int ret = 0; |
6c271ce2 | 7274 | |
c8d1ba58 | 7275 | to_submit = io_sqring_entries(ctx); |
e95eee2d | 7276 | /* if we're handling multiple rings, cap submit size for fairness */ |
4ce8ad95 OL |
7277 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
7278 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; | |
e95eee2d | 7279 | |
5eef4e87 | 7280 | if (!wq_list_empty(&ctx->iopoll_list) || to_submit) { |
948e1947 PB |
7281 | const struct cred *creds = NULL; |
7282 | ||
7283 | if (ctx->sq_creds != current_cred()) | |
7284 | creds = override_creds(ctx->sq_creds); | |
a4c0b3de | 7285 | |
c8d1ba58 | 7286 | mutex_lock(&ctx->uring_lock); |
5eef4e87 | 7287 | if (!wq_list_empty(&ctx->iopoll_list)) |
5ba3c874 | 7288 | io_do_iopoll(ctx, true); |
906a3c6f | 7289 | |
3b763ba1 PB |
7290 | /* |
7291 | * Don't submit if refs are dying, good for io_uring_register(), | |
7292 | * but also it is relied upon by io_ring_exit_work() | |
7293 | */ | |
0298ef96 PB |
7294 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
7295 | !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
08369246 | 7296 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 | 7297 | mutex_unlock(&ctx->uring_lock); |
6c271ce2 | 7298 | |
acfb381d PB |
7299 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
7300 | wake_up(&ctx->sqo_sq_wait); | |
948e1947 PB |
7301 | if (creds) |
7302 | revert_creds(creds); | |
acfb381d | 7303 | } |
6c271ce2 | 7304 | |
08369246 XW |
7305 | return ret; |
7306 | } | |
6c271ce2 | 7307 | |
c072481d | 7308 | static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
08369246 XW |
7309 | { |
7310 | struct io_ring_ctx *ctx; | |
7311 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 7312 | |
c9dca27d PB |
7313 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7314 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); | |
08369246 | 7315 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 7316 | } |
6c271ce2 | 7317 | |
e4b6d902 PB |
7318 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
7319 | { | |
7320 | bool did_sig = false; | |
7321 | struct ksignal ksig; | |
7322 | ||
7323 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || | |
7324 | signal_pending(current)) { | |
7325 | mutex_unlock(&sqd->lock); | |
7326 | if (signal_pending(current)) | |
7327 | did_sig = get_signal(&ksig); | |
7328 | cond_resched(); | |
7329 | mutex_lock(&sqd->lock); | |
7330 | } | |
e4b6d902 PB |
7331 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
7332 | } | |
7333 | ||
c8d1ba58 JA |
7334 | static int io_sq_thread(void *data) |
7335 | { | |
69fb2131 JA |
7336 | struct io_sq_data *sqd = data; |
7337 | struct io_ring_ctx *ctx; | |
a0d9205f | 7338 | unsigned long timeout = 0; |
37d1e2e3 | 7339 | char buf[TASK_COMM_LEN]; |
08369246 | 7340 | DEFINE_WAIT(wait); |
6c271ce2 | 7341 | |
696ee88a | 7342 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
37d1e2e3 | 7343 | set_task_comm(current, buf); |
37d1e2e3 JA |
7344 | |
7345 | if (sqd->sq_cpu != -1) | |
7346 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
7347 | else | |
7348 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
7349 | current->flags |= PF_NO_SETAFFINITY; | |
7350 | ||
09a6f4ef | 7351 | mutex_lock(&sqd->lock); |
e4b6d902 | 7352 | while (1) { |
1a924a80 | 7353 | bool cap_entries, sqt_spin = false; |
c1edbf5f | 7354 | |
e4b6d902 PB |
7355 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
7356 | if (io_sqd_handle_event(sqd)) | |
c7d95613 | 7357 | break; |
08369246 XW |
7358 | timeout = jiffies + sqd->sq_thread_idle; |
7359 | } | |
e4b6d902 | 7360 | |
e95eee2d | 7361 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 7362 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
948e1947 | 7363 | int ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a | 7364 | |
5eef4e87 | 7365 | if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list))) |
08369246 | 7366 | sqt_spin = true; |
69fb2131 | 7367 | } |
dd432ea5 PB |
7368 | if (io_run_task_work()) |
7369 | sqt_spin = true; | |
6c271ce2 | 7370 | |
08369246 | 7371 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 7372 | cond_resched(); |
08369246 XW |
7373 | if (sqt_spin) |
7374 | timeout = jiffies + sqd->sq_thread_idle; | |
7375 | continue; | |
7376 | } | |
7377 | ||
08369246 | 7378 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
dd432ea5 | 7379 | if (!io_sqd_events_pending(sqd) && !current->task_works) { |
1a924a80 PB |
7380 | bool needs_sched = true; |
7381 | ||
724cb4f9 | 7382 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
aaa9f0f4 PB |
7383 | io_ring_set_wakeup_flag(ctx); |
7384 | ||
724cb4f9 | 7385 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
5eef4e87 | 7386 | !wq_list_empty(&ctx->iopoll_list)) { |
724cb4f9 HX |
7387 | needs_sched = false; |
7388 | break; | |
7389 | } | |
7390 | if (io_sqring_entries(ctx)) { | |
7391 | needs_sched = false; | |
7392 | break; | |
7393 | } | |
7394 | } | |
7395 | ||
7396 | if (needs_sched) { | |
7397 | mutex_unlock(&sqd->lock); | |
7398 | schedule(); | |
7399 | mutex_lock(&sqd->lock); | |
7400 | } | |
69fb2131 JA |
7401 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7402 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 7403 | } |
08369246 XW |
7404 | |
7405 | finish_wait(&sqd->wait, &wait); | |
7406 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 | 7407 | } |
28cea78a | 7408 | |
78cc687b | 7409 | io_uring_cancel_generic(true, sqd); |
37d1e2e3 | 7410 | sqd->thread = NULL; |
05962f95 | 7411 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
5f3f26f9 | 7412 | io_ring_set_wakeup_flag(ctx); |
521d6a73 | 7413 | io_run_task_work(); |
734551df PB |
7414 | mutex_unlock(&sqd->lock); |
7415 | ||
37d1e2e3 JA |
7416 | complete(&sqd->exited); |
7417 | do_exit(0); | |
6c271ce2 JA |
7418 | } |
7419 | ||
bda52162 JA |
7420 | struct io_wait_queue { |
7421 | struct wait_queue_entry wq; | |
7422 | struct io_ring_ctx *ctx; | |
5fd46178 | 7423 | unsigned cq_tail; |
bda52162 JA |
7424 | unsigned nr_timeouts; |
7425 | }; | |
7426 | ||
6c503150 | 7427 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
7428 | { |
7429 | struct io_ring_ctx *ctx = iowq->ctx; | |
5fd46178 | 7430 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
bda52162 JA |
7431 | |
7432 | /* | |
d195a66e | 7433 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
7434 | * started waiting. For timeouts, we always want to return to userspace, |
7435 | * regardless of event count. | |
7436 | */ | |
5fd46178 | 7437 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
bda52162 JA |
7438 | } |
7439 | ||
7440 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
7441 | int wake_flags, void *key) | |
7442 | { | |
7443 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
7444 | wq); | |
7445 | ||
6c503150 PB |
7446 | /* |
7447 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
7448 | * the task, and the next invocation will do it. | |
7449 | */ | |
5ed7a37d | 7450 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow)) |
6c503150 PB |
7451 | return autoremove_wake_function(curr, mode, wake_flags, key); |
7452 | return -1; | |
bda52162 JA |
7453 | } |
7454 | ||
af9c1a44 JA |
7455 | static int io_run_task_work_sig(void) |
7456 | { | |
7457 | if (io_run_task_work()) | |
7458 | return 1; | |
7459 | if (!signal_pending(current)) | |
7460 | return 0; | |
0b8cfa97 | 7461 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
792ee0f6 | 7462 | return -ERESTARTSYS; |
af9c1a44 JA |
7463 | return -EINTR; |
7464 | } | |
7465 | ||
eeb60b9a PB |
7466 | /* when returns >0, the caller should retry */ |
7467 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
7468 | struct io_wait_queue *iowq, | |
7469 | signed long *timeout) | |
7470 | { | |
7471 | int ret; | |
7472 | ||
7473 | /* make sure we run task_work before checking for signals */ | |
7474 | ret = io_run_task_work_sig(); | |
7475 | if (ret || io_should_wake(iowq)) | |
7476 | return ret; | |
7477 | /* let the caller flush overflows, retry */ | |
5ed7a37d | 7478 | if (test_bit(0, &ctx->check_cq_overflow)) |
eeb60b9a PB |
7479 | return 1; |
7480 | ||
7481 | *timeout = schedule_timeout(*timeout); | |
7482 | return !*timeout ? -ETIME : 1; | |
7483 | } | |
7484 | ||
2b188cc1 JA |
7485 | /* |
7486 | * Wait until events become available, if we don't already have some. The | |
7487 | * application must reap them itself, as they reside on the shared cq ring. | |
7488 | */ | |
7489 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
7490 | const sigset_t __user *sig, size_t sigsz, |
7491 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 7492 | { |
90291099 | 7493 | struct io_wait_queue iowq; |
75b28aff | 7494 | struct io_rings *rings = ctx->rings; |
c1d5a224 PB |
7495 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
7496 | int ret; | |
2b188cc1 | 7497 | |
b41e9852 | 7498 | do { |
90f67366 | 7499 | io_cqring_overflow_flush(ctx); |
6c503150 | 7500 | if (io_cqring_events(ctx) >= min_events) |
b41e9852 | 7501 | return 0; |
4c6e277c | 7502 | if (!io_run_task_work()) |
b41e9852 | 7503 | break; |
b41e9852 | 7504 | } while (1); |
2b188cc1 | 7505 | |
44df58d4 XW |
7506 | if (uts) { |
7507 | struct timespec64 ts; | |
7508 | ||
7509 | if (get_timespec64(&ts, uts)) | |
7510 | return -EFAULT; | |
7511 | timeout = timespec64_to_jiffies(&ts); | |
7512 | } | |
7513 | ||
2b188cc1 | 7514 | if (sig) { |
9e75ad5d AB |
7515 | #ifdef CONFIG_COMPAT |
7516 | if (in_compat_syscall()) | |
7517 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 7518 | sigsz); |
9e75ad5d AB |
7519 | else |
7520 | #endif | |
b772434b | 7521 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 7522 | |
2b188cc1 JA |
7523 | if (ret) |
7524 | return ret; | |
7525 | } | |
7526 | ||
90291099 PB |
7527 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
7528 | iowq.wq.private = current; | |
7529 | INIT_LIST_HEAD(&iowq.wq.entry); | |
7530 | iowq.ctx = ctx; | |
bda52162 | 7531 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
5fd46178 | 7532 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
90291099 | 7533 | |
c826bd7a | 7534 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 7535 | do { |
ca0a2651 | 7536 | /* if we can't even flush overflow, don't wait for more */ |
90f67366 | 7537 | if (!io_cqring_overflow_flush(ctx)) { |
ca0a2651 JA |
7538 | ret = -EBUSY; |
7539 | break; | |
7540 | } | |
311997b3 | 7541 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
bda52162 | 7542 | TASK_INTERRUPTIBLE); |
eeb60b9a | 7543 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
311997b3 | 7544 | finish_wait(&ctx->cq_wait, &iowq.wq); |
ca0a2651 | 7545 | cond_resched(); |
eeb60b9a | 7546 | } while (ret > 0); |
bda52162 | 7547 | |
b7db41c9 | 7548 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7549 | |
75b28aff | 7550 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7551 | } |
7552 | ||
9123c8ff | 7553 | static void io_free_page_table(void **table, size_t size) |
05f3fb3c | 7554 | { |
9123c8ff | 7555 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
05f3fb3c | 7556 | |
846a4ef2 | 7557 | for (i = 0; i < nr_tables; i++) |
9123c8ff PB |
7558 | kfree(table[i]); |
7559 | kfree(table); | |
7560 | } | |
7561 | ||
c072481d | 7562 | static __cold void **io_alloc_page_table(size_t size) |
9123c8ff PB |
7563 | { |
7564 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); | |
7565 | size_t init_size = size; | |
7566 | void **table; | |
7567 | ||
0bea96f5 | 7568 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7569 | if (!table) |
7570 | return NULL; | |
7571 | ||
7572 | for (i = 0; i < nr_tables; i++) { | |
27f6b318 | 7573 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
9123c8ff | 7574 | |
0bea96f5 | 7575 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7576 | if (!table[i]) { |
7577 | io_free_page_table(table, init_size); | |
7578 | return NULL; | |
7579 | } | |
7580 | size -= this_size; | |
7581 | } | |
7582 | return table; | |
05f3fb3c JA |
7583 | } |
7584 | ||
28a9fe25 | 7585 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
1642b445 | 7586 | { |
28a9fe25 PB |
7587 | percpu_ref_exit(&ref_node->refs); |
7588 | kfree(ref_node); | |
1642b445 PB |
7589 | } |
7590 | ||
c072481d | 7591 | static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
b9bd2bea PB |
7592 | { |
7593 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); | |
7594 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; | |
7595 | unsigned long flags; | |
7596 | bool first_add = false; | |
7597 | ||
7598 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); | |
7599 | node->done = true; | |
7600 | ||
7601 | while (!list_empty(&ctx->rsrc_ref_list)) { | |
7602 | node = list_first_entry(&ctx->rsrc_ref_list, | |
7603 | struct io_rsrc_node, node); | |
7604 | /* recycle ref nodes in order */ | |
7605 | if (!node->done) | |
7606 | break; | |
7607 | list_del(&node->node); | |
7608 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); | |
7609 | } | |
7610 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); | |
7611 | ||
7612 | if (first_add) | |
7613 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ); | |
7614 | } | |
7615 | ||
7616 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx) | |
7617 | { | |
7618 | struct io_rsrc_node *ref_node; | |
7619 | ||
7620 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); | |
7621 | if (!ref_node) | |
7622 | return NULL; | |
7623 | ||
7624 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, | |
7625 | 0, GFP_KERNEL)) { | |
7626 | kfree(ref_node); | |
7627 | return NULL; | |
7628 | } | |
7629 | INIT_LIST_HEAD(&ref_node->node); | |
7630 | INIT_LIST_HEAD(&ref_node->rsrc_list); | |
7631 | ref_node->done = false; | |
7632 | return ref_node; | |
7633 | } | |
7634 | ||
a7f0ed5a PB |
7635 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
7636 | struct io_rsrc_data *data_to_kill) | |
6b06314c | 7637 | { |
a7f0ed5a PB |
7638 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
7639 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); | |
6b06314c | 7640 | |
a7f0ed5a PB |
7641 | if (data_to_kill) { |
7642 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; | |
82fbcfa9 | 7643 | |
a7f0ed5a | 7644 | rsrc_node->rsrc_data = data_to_kill; |
4956b9ea | 7645 | spin_lock_irq(&ctx->rsrc_ref_lock); |
a7f0ed5a | 7646 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
4956b9ea | 7647 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
82fbcfa9 | 7648 | |
3e942498 | 7649 | atomic_inc(&data_to_kill->refs); |
a7f0ed5a PB |
7650 | percpu_ref_kill(&rsrc_node->refs); |
7651 | ctx->rsrc_node = NULL; | |
7652 | } | |
6b06314c | 7653 | |
a7f0ed5a PB |
7654 | if (!ctx->rsrc_node) { |
7655 | ctx->rsrc_node = ctx->rsrc_backup_node; | |
7656 | ctx->rsrc_backup_node = NULL; | |
7657 | } | |
8bad28d8 HX |
7658 | } |
7659 | ||
a7f0ed5a | 7660 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
8dd03afe PB |
7661 | { |
7662 | if (ctx->rsrc_backup_node) | |
7663 | return 0; | |
b895c9a6 | 7664 | ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx); |
8dd03afe | 7665 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
8bad28d8 HX |
7666 | } |
7667 | ||
c072481d PB |
7668 | static __cold int io_rsrc_ref_quiesce(struct io_rsrc_data *data, |
7669 | struct io_ring_ctx *ctx) | |
8bad28d8 HX |
7670 | { |
7671 | int ret; | |
05589553 | 7672 | |
215c3902 | 7673 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
8bad28d8 HX |
7674 | if (data->quiesce) |
7675 | return -ENXIO; | |
05589553 | 7676 | |
8bad28d8 | 7677 | data->quiesce = true; |
1ffc5422 | 7678 | do { |
a7f0ed5a | 7679 | ret = io_rsrc_node_switch_start(ctx); |
8dd03afe | 7680 | if (ret) |
f2303b1f | 7681 | break; |
a7f0ed5a | 7682 | io_rsrc_node_switch(ctx, data); |
f2303b1f | 7683 | |
3e942498 PB |
7684 | /* kill initial ref, already quiesced if zero */ |
7685 | if (atomic_dec_and_test(&data->refs)) | |
7686 | break; | |
c018db4a | 7687 | mutex_unlock(&ctx->uring_lock); |
8bad28d8 | 7688 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 | 7689 | ret = wait_for_completion_interruptible(&data->done); |
c018db4a JA |
7690 | if (!ret) { |
7691 | mutex_lock(&ctx->uring_lock); | |
1ffc5422 | 7692 | break; |
c018db4a | 7693 | } |
8bad28d8 | 7694 | |
3e942498 PB |
7695 | atomic_inc(&data->refs); |
7696 | /* wait for all works potentially completing data->done */ | |
7697 | flush_delayed_work(&ctx->rsrc_put_work); | |
cb5e1b81 | 7698 | reinit_completion(&data->done); |
8dd03afe | 7699 | |
1ffc5422 | 7700 | ret = io_run_task_work_sig(); |
8bad28d8 | 7701 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 7702 | } while (ret >= 0); |
8bad28d8 | 7703 | data->quiesce = false; |
05f3fb3c | 7704 | |
8bad28d8 | 7705 | return ret; |
d7954b2b BM |
7706 | } |
7707 | ||
2d091d62 PB |
7708 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
7709 | { | |
7710 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; | |
7711 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; | |
7712 | ||
7713 | return &data->tags[table_idx][off]; | |
7714 | } | |
7715 | ||
44b31f2f | 7716 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
1ad555c6 | 7717 | { |
2d091d62 PB |
7718 | size_t size = data->nr * sizeof(data->tags[0][0]); |
7719 | ||
7720 | if (data->tags) | |
7721 | io_free_page_table((void **)data->tags, size); | |
44b31f2f PB |
7722 | kfree(data); |
7723 | } | |
7724 | ||
c072481d PB |
7725 | static __cold int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
7726 | u64 __user *utags, unsigned nr, | |
7727 | struct io_rsrc_data **pdata) | |
1ad555c6 | 7728 | { |
b895c9a6 | 7729 | struct io_rsrc_data *data; |
2d091d62 | 7730 | int ret = -ENOMEM; |
d878c816 | 7731 | unsigned i; |
1ad555c6 BM |
7732 | |
7733 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7734 | if (!data) | |
d878c816 | 7735 | return -ENOMEM; |
2d091d62 | 7736 | data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0])); |
b60c8dce | 7737 | if (!data->tags) { |
1ad555c6 | 7738 | kfree(data); |
d878c816 PB |
7739 | return -ENOMEM; |
7740 | } | |
2d091d62 PB |
7741 | |
7742 | data->nr = nr; | |
7743 | data->ctx = ctx; | |
7744 | data->do_put = do_put; | |
d878c816 | 7745 | if (utags) { |
2d091d62 | 7746 | ret = -EFAULT; |
d878c816 | 7747 | for (i = 0; i < nr; i++) { |
fdd1dc31 CIK |
7748 | u64 *tag_slot = io_get_tag_slot(data, i); |
7749 | ||
7750 | if (copy_from_user(tag_slot, &utags[i], | |
7751 | sizeof(*tag_slot))) | |
2d091d62 | 7752 | goto fail; |
d878c816 | 7753 | } |
1ad555c6 | 7754 | } |
b60c8dce | 7755 | |
3e942498 | 7756 | atomic_set(&data->refs, 1); |
1ad555c6 | 7757 | init_completion(&data->done); |
d878c816 PB |
7758 | *pdata = data; |
7759 | return 0; | |
2d091d62 PB |
7760 | fail: |
7761 | io_rsrc_data_free(data); | |
7762 | return ret; | |
1ad555c6 BM |
7763 | } |
7764 | ||
9123c8ff PB |
7765 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
7766 | { | |
0bea96f5 PB |
7767 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
7768 | GFP_KERNEL_ACCOUNT); | |
9123c8ff PB |
7769 | return !!table->files; |
7770 | } | |
7771 | ||
042b0d85 | 7772 | static void io_free_file_tables(struct io_file_table *table) |
9123c8ff | 7773 | { |
042b0d85 | 7774 | kvfree(table->files); |
9123c8ff PB |
7775 | table->files = NULL; |
7776 | } | |
7777 | ||
fff4db76 | 7778 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
1ad555c6 | 7779 | { |
fff4db76 PB |
7780 | #if defined(CONFIG_UNIX) |
7781 | if (ctx->ring_sock) { | |
7782 | struct sock *sock = ctx->ring_sock->sk; | |
7783 | struct sk_buff *skb; | |
7784 | ||
7785 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
7786 | kfree_skb(skb); | |
7787 | } | |
7788 | #else | |
7789 | int i; | |
7790 | ||
7791 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7792 | struct file *file; | |
7793 | ||
7794 | file = io_file_from_index(ctx, i); | |
7795 | if (file) | |
7796 | fput(file); | |
7797 | } | |
7798 | #endif | |
042b0d85 | 7799 | io_free_file_tables(&ctx->file_table); |
44b31f2f | 7800 | io_rsrc_data_free(ctx->file_data); |
fff4db76 PB |
7801 | ctx->file_data = NULL; |
7802 | ctx->nr_user_files = 0; | |
1ad555c6 BM |
7803 | } |
7804 | ||
d7954b2b BM |
7805 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7806 | { | |
d7954b2b BM |
7807 | int ret; |
7808 | ||
08480400 | 7809 | if (!ctx->file_data) |
d7954b2b | 7810 | return -ENXIO; |
08480400 PB |
7811 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
7812 | if (!ret) | |
7813 | __io_sqe_files_unregister(ctx); | |
7814 | return ret; | |
6b06314c JA |
7815 | } |
7816 | ||
37d1e2e3 | 7817 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
09a6f4ef | 7818 | __releases(&sqd->lock) |
37d1e2e3 | 7819 | { |
521d6a73 PB |
7820 | WARN_ON_ONCE(sqd->thread == current); |
7821 | ||
9e138a48 PB |
7822 | /* |
7823 | * Do the dance but not conditional clear_bit() because it'd race with | |
7824 | * other threads incrementing park_pending and setting the bit. | |
7825 | */ | |
37d1e2e3 | 7826 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
9e138a48 PB |
7827 | if (atomic_dec_return(&sqd->park_pending)) |
7828 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
09a6f4ef | 7829 | mutex_unlock(&sqd->lock); |
37d1e2e3 JA |
7830 | } |
7831 | ||
86e0d676 | 7832 | static void io_sq_thread_park(struct io_sq_data *sqd) |
09a6f4ef | 7833 | __acquires(&sqd->lock) |
37d1e2e3 | 7834 | { |
521d6a73 PB |
7835 | WARN_ON_ONCE(sqd->thread == current); |
7836 | ||
9e138a48 | 7837 | atomic_inc(&sqd->park_pending); |
86e0d676 | 7838 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
09a6f4ef | 7839 | mutex_lock(&sqd->lock); |
05962f95 | 7840 | if (sqd->thread) |
86e0d676 | 7841 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
7842 | } |
7843 | ||
7844 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
7845 | { | |
521d6a73 | 7846 | WARN_ON_ONCE(sqd->thread == current); |
88885f66 | 7847 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
521d6a73 | 7848 | |
05962f95 | 7849 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
88885f66 | 7850 | mutex_lock(&sqd->lock); |
e8f98f24 JA |
7851 | if (sqd->thread) |
7852 | wake_up_process(sqd->thread); | |
09a6f4ef | 7853 | mutex_unlock(&sqd->lock); |
05962f95 | 7854 | wait_for_completion(&sqd->exited); |
37d1e2e3 JA |
7855 | } |
7856 | ||
534ca6d6 | 7857 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7858 | { |
534ca6d6 | 7859 | if (refcount_dec_and_test(&sqd->refs)) { |
9e138a48 PB |
7860 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
7861 | ||
37d1e2e3 JA |
7862 | io_sq_thread_stop(sqd); |
7863 | kfree(sqd); | |
7864 | } | |
7865 | } | |
7866 | ||
7867 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
7868 | { | |
7869 | struct io_sq_data *sqd = ctx->sq_data; | |
7870 | ||
7871 | if (sqd) { | |
05962f95 | 7872 | io_sq_thread_park(sqd); |
521d6a73 | 7873 | list_del_init(&ctx->sqd_list); |
37d1e2e3 | 7874 | io_sqd_update_thread_idle(sqd); |
05962f95 | 7875 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
7876 | |
7877 | io_put_sq_data(sqd); | |
7878 | ctx->sq_data = NULL; | |
534ca6d6 JA |
7879 | } |
7880 | } | |
7881 | ||
aa06165d JA |
7882 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7883 | { | |
7884 | struct io_ring_ctx *ctx_attach; | |
7885 | struct io_sq_data *sqd; | |
7886 | struct fd f; | |
7887 | ||
7888 | f = fdget(p->wq_fd); | |
7889 | if (!f.file) | |
7890 | return ERR_PTR(-ENXIO); | |
7891 | if (f.file->f_op != &io_uring_fops) { | |
7892 | fdput(f); | |
7893 | return ERR_PTR(-EINVAL); | |
7894 | } | |
7895 | ||
7896 | ctx_attach = f.file->private_data; | |
7897 | sqd = ctx_attach->sq_data; | |
7898 | if (!sqd) { | |
7899 | fdput(f); | |
7900 | return ERR_PTR(-EINVAL); | |
7901 | } | |
5c2469e0 JA |
7902 | if (sqd->task_tgid != current->tgid) { |
7903 | fdput(f); | |
7904 | return ERR_PTR(-EPERM); | |
7905 | } | |
aa06165d JA |
7906 | |
7907 | refcount_inc(&sqd->refs); | |
7908 | fdput(f); | |
7909 | return sqd; | |
7910 | } | |
7911 | ||
26984fbf PB |
7912 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
7913 | bool *attached) | |
534ca6d6 JA |
7914 | { |
7915 | struct io_sq_data *sqd; | |
7916 | ||
26984fbf | 7917 | *attached = false; |
5c2469e0 JA |
7918 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
7919 | sqd = io_attach_sq_data(p); | |
26984fbf PB |
7920 | if (!IS_ERR(sqd)) { |
7921 | *attached = true; | |
5c2469e0 | 7922 | return sqd; |
26984fbf | 7923 | } |
5c2469e0 JA |
7924 | /* fall through for EPERM case, setup new sqd/task */ |
7925 | if (PTR_ERR(sqd) != -EPERM) | |
7926 | return sqd; | |
7927 | } | |
aa06165d | 7928 | |
534ca6d6 JA |
7929 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7930 | if (!sqd) | |
7931 | return ERR_PTR(-ENOMEM); | |
7932 | ||
9e138a48 | 7933 | atomic_set(&sqd->park_pending, 0); |
534ca6d6 | 7934 | refcount_set(&sqd->refs, 1); |
69fb2131 | 7935 | INIT_LIST_HEAD(&sqd->ctx_list); |
09a6f4ef | 7936 | mutex_init(&sqd->lock); |
534ca6d6 | 7937 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 7938 | init_completion(&sqd->exited); |
534ca6d6 JA |
7939 | return sqd; |
7940 | } | |
7941 | ||
6b06314c | 7942 | #if defined(CONFIG_UNIX) |
6b06314c JA |
7943 | /* |
7944 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
7945 | * the io_uring can be safely unregistered on process exit, even if we have | |
7946 | * loops in the file referencing. | |
7947 | */ | |
7948 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
7949 | { | |
7950 | struct sock *sk = ctx->ring_sock->sk; | |
7951 | struct scm_fp_list *fpl; | |
7952 | struct sk_buff *skb; | |
08a45173 | 7953 | int i, nr_files; |
6b06314c | 7954 | |
6b06314c JA |
7955 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
7956 | if (!fpl) | |
7957 | return -ENOMEM; | |
7958 | ||
7959 | skb = alloc_skb(0, GFP_KERNEL); | |
7960 | if (!skb) { | |
7961 | kfree(fpl); | |
7962 | return -ENOMEM; | |
7963 | } | |
7964 | ||
7965 | skb->sk = sk; | |
6b06314c | 7966 | |
08a45173 | 7967 | nr_files = 0; |
62e398be | 7968 | fpl->user = get_uid(current_user()); |
6b06314c | 7969 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
7970 | struct file *file = io_file_from_index(ctx, i + offset); |
7971 | ||
7972 | if (!file) | |
08a45173 | 7973 | continue; |
65e19f54 | 7974 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
7975 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
7976 | nr_files++; | |
6b06314c JA |
7977 | } |
7978 | ||
08a45173 JA |
7979 | if (nr_files) { |
7980 | fpl->max = SCM_MAX_FD; | |
7981 | fpl->count = nr_files; | |
7982 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 7983 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
7984 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
7985 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 7986 | |
08a45173 JA |
7987 | for (i = 0; i < nr_files; i++) |
7988 | fput(fpl->fp[i]); | |
7989 | } else { | |
7990 | kfree_skb(skb); | |
7991 | kfree(fpl); | |
7992 | } | |
6b06314c JA |
7993 | |
7994 | return 0; | |
7995 | } | |
7996 | ||
7997 | /* | |
7998 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
7999 | * causes regular reference counting to break down. We rely on the UNIX | |
8000 | * garbage collection to take care of this problem for us. | |
8001 | */ | |
8002 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8003 | { | |
8004 | unsigned left, total; | |
8005 | int ret = 0; | |
8006 | ||
8007 | total = 0; | |
8008 | left = ctx->nr_user_files; | |
8009 | while (left) { | |
8010 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
8011 | |
8012 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
8013 | if (ret) | |
8014 | break; | |
8015 | left -= this_files; | |
8016 | total += this_files; | |
8017 | } | |
8018 | ||
8019 | if (!ret) | |
8020 | return 0; | |
8021 | ||
8022 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
8023 | struct file *file = io_file_from_index(ctx, total); |
8024 | ||
8025 | if (file) | |
8026 | fput(file); | |
6b06314c JA |
8027 | total++; |
8028 | } | |
8029 | ||
8030 | return ret; | |
8031 | } | |
8032 | #else | |
8033 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8034 | { | |
8035 | return 0; | |
8036 | } | |
8037 | #endif | |
8038 | ||
47e90392 | 8039 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 8040 | { |
50238531 | 8041 | struct file *file = prsrc->file; |
05f3fb3c JA |
8042 | #if defined(CONFIG_UNIX) |
8043 | struct sock *sock = ctx->ring_sock->sk; | |
8044 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
8045 | struct sk_buff *skb; | |
8046 | int i; | |
8047 | ||
8048 | __skb_queue_head_init(&list); | |
8049 | ||
8050 | /* | |
8051 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
8052 | * remove this entry and rearrange the file array. | |
8053 | */ | |
8054 | skb = skb_dequeue(head); | |
8055 | while (skb) { | |
8056 | struct scm_fp_list *fp; | |
8057 | ||
8058 | fp = UNIXCB(skb).fp; | |
8059 | for (i = 0; i < fp->count; i++) { | |
8060 | int left; | |
8061 | ||
8062 | if (fp->fp[i] != file) | |
8063 | continue; | |
8064 | ||
8065 | unix_notinflight(fp->user, fp->fp[i]); | |
8066 | left = fp->count - 1 - i; | |
8067 | if (left) { | |
8068 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
8069 | left * sizeof(struct file *)); | |
8070 | } | |
8071 | fp->count--; | |
8072 | if (!fp->count) { | |
8073 | kfree_skb(skb); | |
8074 | skb = NULL; | |
8075 | } else { | |
8076 | __skb_queue_tail(&list, skb); | |
8077 | } | |
8078 | fput(file); | |
8079 | file = NULL; | |
8080 | break; | |
8081 | } | |
8082 | ||
8083 | if (!file) | |
8084 | break; | |
8085 | ||
8086 | __skb_queue_tail(&list, skb); | |
8087 | ||
8088 | skb = skb_dequeue(head); | |
8089 | } | |
8090 | ||
8091 | if (skb_peek(&list)) { | |
8092 | spin_lock_irq(&head->lock); | |
8093 | while ((skb = __skb_dequeue(&list)) != NULL) | |
8094 | __skb_queue_tail(head, skb); | |
8095 | spin_unlock_irq(&head->lock); | |
8096 | } | |
8097 | #else | |
8098 | fput(file); | |
8099 | #endif | |
8100 | } | |
8101 | ||
b895c9a6 | 8102 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
65e19f54 | 8103 | { |
b895c9a6 | 8104 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
269bbe5f BM |
8105 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
8106 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 8107 | |
269bbe5f BM |
8108 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
8109 | list_del(&prsrc->list); | |
b60c8dce PB |
8110 | |
8111 | if (prsrc->tag) { | |
8112 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; | |
b60c8dce PB |
8113 | |
8114 | io_ring_submit_lock(ctx, lock_ring); | |
79ebeaee | 8115 | spin_lock(&ctx->completion_lock); |
b60c8dce | 8116 | io_cqring_fill_event(ctx, prsrc->tag, 0, 0); |
2840f710 | 8117 | ctx->cq_extra++; |
b60c8dce | 8118 | io_commit_cqring(ctx); |
79ebeaee | 8119 | spin_unlock(&ctx->completion_lock); |
b60c8dce PB |
8120 | io_cqring_ev_posted(ctx); |
8121 | io_ring_submit_unlock(ctx, lock_ring); | |
8122 | } | |
8123 | ||
40ae0ff7 | 8124 | rsrc_data->do_put(ctx, prsrc); |
269bbe5f | 8125 | kfree(prsrc); |
65e19f54 | 8126 | } |
05589553 | 8127 | |
28a9fe25 | 8128 | io_rsrc_node_destroy(ref_node); |
3e942498 PB |
8129 | if (atomic_dec_and_test(&rsrc_data->refs)) |
8130 | complete(&rsrc_data->done); | |
2faf852d | 8131 | } |
65e19f54 | 8132 | |
269bbe5f | 8133 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
8134 | { |
8135 | struct io_ring_ctx *ctx; | |
8136 | struct llist_node *node; | |
8137 | ||
269bbe5f BM |
8138 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
8139 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
8140 | |
8141 | while (node) { | |
b895c9a6 | 8142 | struct io_rsrc_node *ref_node; |
4a38aed2 JA |
8143 | struct llist_node *next = node->next; |
8144 | ||
b895c9a6 | 8145 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
269bbe5f | 8146 | __io_rsrc_put_work(ref_node); |
4a38aed2 JA |
8147 | node = next; |
8148 | } | |
8149 | } | |
8150 | ||
6b06314c | 8151 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
792e3582 | 8152 | unsigned nr_args, u64 __user *tags) |
6b06314c JA |
8153 | { |
8154 | __s32 __user *fds = (__s32 __user *) arg; | |
05f3fb3c | 8155 | struct file *file; |
f3baed39 | 8156 | int fd, ret; |
846a4ef2 | 8157 | unsigned i; |
6b06314c | 8158 | |
05f3fb3c | 8159 | if (ctx->file_data) |
6b06314c JA |
8160 | return -EBUSY; |
8161 | if (!nr_args) | |
8162 | return -EINVAL; | |
8163 | if (nr_args > IORING_MAX_FIXED_FILES) | |
8164 | return -EMFILE; | |
3a1b8a4e PB |
8165 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
8166 | return -EMFILE; | |
a7f0ed5a | 8167 | ret = io_rsrc_node_switch_start(ctx); |
f3baed39 PB |
8168 | if (ret) |
8169 | return ret; | |
d878c816 PB |
8170 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
8171 | &ctx->file_data); | |
8172 | if (ret) | |
8173 | return ret; | |
6b06314c | 8174 | |
f3baed39 | 8175 | ret = -ENOMEM; |
aeca241b | 8176 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
1ad555c6 | 8177 | goto out_free; |
65e19f54 | 8178 | |
08a45173 | 8179 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
d878c816 | 8180 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
600cf3f8 PB |
8181 | ret = -EFAULT; |
8182 | goto out_fput; | |
8183 | } | |
08a45173 | 8184 | /* allow sparse sets */ |
792e3582 PB |
8185 | if (fd == -1) { |
8186 | ret = -EINVAL; | |
2d091d62 | 8187 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
792e3582 | 8188 | goto out_fput; |
08a45173 | 8189 | continue; |
792e3582 | 8190 | } |
6b06314c | 8191 | |
05f3fb3c | 8192 | file = fget(fd); |
6b06314c | 8193 | ret = -EBADF; |
792e3582 | 8194 | if (unlikely(!file)) |
600cf3f8 | 8195 | goto out_fput; |
05f3fb3c | 8196 | |
6b06314c JA |
8197 | /* |
8198 | * Don't allow io_uring instances to be registered. If UNIX | |
8199 | * isn't enabled, then this causes a reference cycle and this | |
8200 | * instance can never get freed. If UNIX is enabled we'll | |
8201 | * handle it just fine, but there's still no point in allowing | |
8202 | * a ring fd as it doesn't support regular read/write anyway. | |
8203 | */ | |
05f3fb3c JA |
8204 | if (file->f_op == &io_uring_fops) { |
8205 | fput(file); | |
600cf3f8 | 8206 | goto out_fput; |
6b06314c | 8207 | } |
aeca241b | 8208 | io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file); |
6b06314c JA |
8209 | } |
8210 | ||
6b06314c | 8211 | ret = io_sqe_files_scm(ctx); |
05589553 | 8212 | if (ret) { |
08480400 | 8213 | __io_sqe_files_unregister(ctx); |
05589553 XW |
8214 | return ret; |
8215 | } | |
6b06314c | 8216 | |
a7f0ed5a | 8217 | io_rsrc_node_switch(ctx, NULL); |
6b06314c | 8218 | return ret; |
600cf3f8 PB |
8219 | out_fput: |
8220 | for (i = 0; i < ctx->nr_user_files; i++) { | |
8221 | file = io_file_from_index(ctx, i); | |
8222 | if (file) | |
8223 | fput(file); | |
8224 | } | |
042b0d85 | 8225 | io_free_file_tables(&ctx->file_table); |
600cf3f8 | 8226 | ctx->nr_user_files = 0; |
600cf3f8 | 8227 | out_free: |
44b31f2f | 8228 | io_rsrc_data_free(ctx->file_data); |
55cbc256 | 8229 | ctx->file_data = NULL; |
6b06314c JA |
8230 | return ret; |
8231 | } | |
8232 | ||
c3a31e60 JA |
8233 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
8234 | int index) | |
8235 | { | |
8236 | #if defined(CONFIG_UNIX) | |
8237 | struct sock *sock = ctx->ring_sock->sk; | |
8238 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
8239 | struct sk_buff *skb; | |
8240 | ||
8241 | /* | |
8242 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
8243 | * file set. If there's no room, fall back to allocating a new skb | |
8244 | * and filling it in. | |
8245 | */ | |
8246 | spin_lock_irq(&head->lock); | |
8247 | skb = skb_peek(head); | |
8248 | if (skb) { | |
8249 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
8250 | ||
8251 | if (fpl->count < SCM_MAX_FD) { | |
8252 | __skb_unlink(skb, head); | |
8253 | spin_unlock_irq(&head->lock); | |
8254 | fpl->fp[fpl->count] = get_file(file); | |
8255 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
8256 | fpl->count++; | |
8257 | spin_lock_irq(&head->lock); | |
8258 | __skb_queue_head(head, skb); | |
8259 | } else { | |
8260 | skb = NULL; | |
8261 | } | |
8262 | } | |
8263 | spin_unlock_irq(&head->lock); | |
8264 | ||
8265 | if (skb) { | |
8266 | fput(file); | |
8267 | return 0; | |
8268 | } | |
8269 | ||
8270 | return __io_sqe_files_scm(ctx, 1, index); | |
8271 | #else | |
8272 | return 0; | |
8273 | #endif | |
8274 | } | |
8275 | ||
9c7b0ba8 PB |
8276 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
8277 | struct io_rsrc_node *node, void *rsrc) | |
8278 | { | |
8279 | struct io_rsrc_put *prsrc; | |
8280 | ||
8281 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); | |
8282 | if (!prsrc) | |
8283 | return -ENOMEM; | |
8284 | ||
8285 | prsrc->tag = *io_get_tag_slot(data, idx); | |
8286 | prsrc->rsrc = rsrc; | |
8287 | list_add(&prsrc->list, &node->rsrc_list); | |
8288 | return 0; | |
8289 | } | |
8290 | ||
b9445598 PB |
8291 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
8292 | unsigned int issue_flags, u32 slot_index) | |
8293 | { | |
8294 | struct io_ring_ctx *ctx = req->ctx; | |
8295 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; | |
9c7b0ba8 | 8296 | bool needs_switch = false; |
b9445598 PB |
8297 | struct io_fixed_file *file_slot; |
8298 | int ret = -EBADF; | |
8299 | ||
8300 | io_ring_submit_lock(ctx, !force_nonblock); | |
8301 | if (file->f_op == &io_uring_fops) | |
8302 | goto err; | |
8303 | ret = -ENXIO; | |
8304 | if (!ctx->file_data) | |
8305 | goto err; | |
8306 | ret = -EINVAL; | |
8307 | if (slot_index >= ctx->nr_user_files) | |
8308 | goto err; | |
8309 | ||
8310 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); | |
8311 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); | |
9c7b0ba8 PB |
8312 | |
8313 | if (file_slot->file_ptr) { | |
8314 | struct file *old_file; | |
8315 | ||
8316 | ret = io_rsrc_node_switch_start(ctx); | |
8317 | if (ret) | |
8318 | goto err; | |
8319 | ||
8320 | old_file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8321 | ret = io_queue_rsrc_removal(ctx->file_data, slot_index, | |
8322 | ctx->rsrc_node, old_file); | |
8323 | if (ret) | |
8324 | goto err; | |
8325 | file_slot->file_ptr = 0; | |
8326 | needs_switch = true; | |
8327 | } | |
b9445598 PB |
8328 | |
8329 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; | |
8330 | io_fixed_file_set(file_slot, file); | |
8331 | ret = io_sqe_file_register(ctx, file, slot_index); | |
8332 | if (ret) { | |
8333 | file_slot->file_ptr = 0; | |
8334 | goto err; | |
8335 | } | |
8336 | ||
8337 | ret = 0; | |
8338 | err: | |
9c7b0ba8 PB |
8339 | if (needs_switch) |
8340 | io_rsrc_node_switch(ctx, ctx->file_data); | |
b9445598 PB |
8341 | io_ring_submit_unlock(ctx, !force_nonblock); |
8342 | if (ret) | |
8343 | fput(file); | |
8344 | return ret; | |
8345 | } | |
8346 | ||
7df778be PB |
8347 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) |
8348 | { | |
8349 | unsigned int offset = req->close.file_slot - 1; | |
8350 | struct io_ring_ctx *ctx = req->ctx; | |
8351 | struct io_fixed_file *file_slot; | |
8352 | struct file *file; | |
8353 | int ret, i; | |
8354 | ||
8355 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
8356 | ret = -ENXIO; | |
8357 | if (unlikely(!ctx->file_data)) | |
8358 | goto out; | |
8359 | ret = -EINVAL; | |
8360 | if (offset >= ctx->nr_user_files) | |
8361 | goto out; | |
8362 | ret = io_rsrc_node_switch_start(ctx); | |
8363 | if (ret) | |
8364 | goto out; | |
8365 | ||
8366 | i = array_index_nospec(offset, ctx->nr_user_files); | |
8367 | file_slot = io_fixed_file_slot(&ctx->file_table, i); | |
8368 | ret = -EBADF; | |
8369 | if (!file_slot->file_ptr) | |
8370 | goto out; | |
8371 | ||
8372 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8373 | ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file); | |
8374 | if (ret) | |
8375 | goto out; | |
8376 | ||
8377 | file_slot->file_ptr = 0; | |
8378 | io_rsrc_node_switch(ctx, ctx->file_data); | |
8379 | ret = 0; | |
8380 | out: | |
8381 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
8382 | return ret; | |
8383 | } | |
8384 | ||
05f3fb3c | 8385 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
c3bdad02 | 8386 | struct io_uring_rsrc_update2 *up, |
05f3fb3c JA |
8387 | unsigned nr_args) |
8388 | { | |
c3bdad02 | 8389 | u64 __user *tags = u64_to_user_ptr(up->tags); |
98f0b3b4 | 8390 | __s32 __user *fds = u64_to_user_ptr(up->data); |
b895c9a6 | 8391 | struct io_rsrc_data *data = ctx->file_data; |
a04b0ac0 PB |
8392 | struct io_fixed_file *file_slot; |
8393 | struct file *file; | |
98f0b3b4 PB |
8394 | int fd, i, err = 0; |
8395 | unsigned int done; | |
05589553 | 8396 | bool needs_switch = false; |
c3a31e60 | 8397 | |
98f0b3b4 PB |
8398 | if (!ctx->file_data) |
8399 | return -ENXIO; | |
8400 | if (up->offset + nr_args > ctx->nr_user_files) | |
c3a31e60 JA |
8401 | return -EINVAL; |
8402 | ||
67973b93 | 8403 | for (done = 0; done < nr_args; done++) { |
c3bdad02 PB |
8404 | u64 tag = 0; |
8405 | ||
8406 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || | |
8407 | copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
c3a31e60 JA |
8408 | err = -EFAULT; |
8409 | break; | |
8410 | } | |
c3bdad02 PB |
8411 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
8412 | err = -EINVAL; | |
8413 | break; | |
8414 | } | |
4e0377a1 | 8415 | if (fd == IORING_REGISTER_FILES_SKIP) |
8416 | continue; | |
8417 | ||
67973b93 | 8418 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
aeca241b | 8419 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
ea64ec02 | 8420 | |
a04b0ac0 PB |
8421 | if (file_slot->file_ptr) { |
8422 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
b60c8dce PB |
8423 | err = io_queue_rsrc_removal(data, up->offset + done, |
8424 | ctx->rsrc_node, file); | |
a5318d3c HD |
8425 | if (err) |
8426 | break; | |
a04b0ac0 | 8427 | file_slot->file_ptr = 0; |
05589553 | 8428 | needs_switch = true; |
c3a31e60 JA |
8429 | } |
8430 | if (fd != -1) { | |
c3a31e60 JA |
8431 | file = fget(fd); |
8432 | if (!file) { | |
8433 | err = -EBADF; | |
8434 | break; | |
8435 | } | |
8436 | /* | |
8437 | * Don't allow io_uring instances to be registered. If | |
8438 | * UNIX isn't enabled, then this causes a reference | |
8439 | * cycle and this instance can never get freed. If UNIX | |
8440 | * is enabled we'll handle it just fine, but there's | |
8441 | * still no point in allowing a ring fd as it doesn't | |
8442 | * support regular read/write anyway. | |
8443 | */ | |
8444 | if (file->f_op == &io_uring_fops) { | |
8445 | fput(file); | |
8446 | err = -EBADF; | |
8447 | break; | |
8448 | } | |
2d091d62 | 8449 | *io_get_tag_slot(data, up->offset + done) = tag; |
9a321c98 | 8450 | io_fixed_file_set(file_slot, file); |
c3a31e60 | 8451 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 8452 | if (err) { |
a04b0ac0 | 8453 | file_slot->file_ptr = 0; |
f3bd9dae | 8454 | fput(file); |
c3a31e60 | 8455 | break; |
f3bd9dae | 8456 | } |
c3a31e60 | 8457 | } |
05f3fb3c JA |
8458 | } |
8459 | ||
a7f0ed5a PB |
8460 | if (needs_switch) |
8461 | io_rsrc_node_switch(ctx, data); | |
c3a31e60 JA |
8462 | return done ? done : err; |
8463 | } | |
05589553 | 8464 | |
685fe7fe JA |
8465 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
8466 | struct task_struct *task) | |
24369c2e | 8467 | { |
e941894e | 8468 | struct io_wq_hash *hash; |
24369c2e | 8469 | struct io_wq_data data; |
24369c2e | 8470 | unsigned int concurrency; |
24369c2e | 8471 | |
362a9e65 | 8472 | mutex_lock(&ctx->uring_lock); |
e941894e JA |
8473 | hash = ctx->hash_map; |
8474 | if (!hash) { | |
8475 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
362a9e65 YY |
8476 | if (!hash) { |
8477 | mutex_unlock(&ctx->uring_lock); | |
e941894e | 8478 | return ERR_PTR(-ENOMEM); |
362a9e65 | 8479 | } |
e941894e JA |
8480 | refcount_set(&hash->refs, 1); |
8481 | init_waitqueue_head(&hash->wait); | |
8482 | ctx->hash_map = hash; | |
24369c2e | 8483 | } |
362a9e65 | 8484 | mutex_unlock(&ctx->uring_lock); |
24369c2e | 8485 | |
e941894e | 8486 | data.hash = hash; |
685fe7fe | 8487 | data.task = task; |
ebc11b6c | 8488 | data.free_work = io_wq_free_work; |
f5fa38c5 | 8489 | data.do_work = io_wq_submit_work; |
24369c2e | 8490 | |
d25e3a3d JA |
8491 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
8492 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 8493 | |
5aa75ed5 | 8494 | return io_wq_create(concurrency, &data); |
24369c2e PB |
8495 | } |
8496 | ||
c072481d PB |
8497 | static __cold int io_uring_alloc_task_context(struct task_struct *task, |
8498 | struct io_ring_ctx *ctx) | |
0f212204 JA |
8499 | { |
8500 | struct io_uring_task *tctx; | |
d8a6df10 | 8501 | int ret; |
0f212204 | 8502 | |
09899b19 | 8503 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
0f212204 JA |
8504 | if (unlikely(!tctx)) |
8505 | return -ENOMEM; | |
8506 | ||
d8a6df10 JA |
8507 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
8508 | if (unlikely(ret)) { | |
8509 | kfree(tctx); | |
8510 | return ret; | |
8511 | } | |
8512 | ||
685fe7fe | 8513 | tctx->io_wq = io_init_wq_offload(ctx, task); |
5aa75ed5 JA |
8514 | if (IS_ERR(tctx->io_wq)) { |
8515 | ret = PTR_ERR(tctx->io_wq); | |
8516 | percpu_counter_destroy(&tctx->inflight); | |
8517 | kfree(tctx); | |
8518 | return ret; | |
8519 | } | |
8520 | ||
0f212204 JA |
8521 | xa_init(&tctx->xa); |
8522 | init_waitqueue_head(&tctx->wait); | |
fdaf083c | 8523 | atomic_set(&tctx->in_idle, 0); |
b303fe2e | 8524 | atomic_set(&tctx->inflight_tracked, 0); |
0f212204 | 8525 | task->io_uring = tctx; |
7cbf1722 JA |
8526 | spin_lock_init(&tctx->task_lock); |
8527 | INIT_WQ_LIST(&tctx->task_list); | |
7cbf1722 | 8528 | init_task_work(&tctx->task_work, tctx_task_work); |
0f212204 JA |
8529 | return 0; |
8530 | } | |
8531 | ||
8532 | void __io_uring_free(struct task_struct *tsk) | |
8533 | { | |
8534 | struct io_uring_task *tctx = tsk->io_uring; | |
8535 | ||
8536 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e | 8537 | WARN_ON_ONCE(tctx->io_wq); |
09899b19 | 8538 | WARN_ON_ONCE(tctx->cached_refs); |
ef8eaa4e | 8539 | |
d8a6df10 | 8540 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
8541 | kfree(tctx); |
8542 | tsk->io_uring = NULL; | |
8543 | } | |
8544 | ||
c072481d PB |
8545 | static __cold int io_sq_offload_create(struct io_ring_ctx *ctx, |
8546 | struct io_uring_params *p) | |
2b188cc1 JA |
8547 | { |
8548 | int ret; | |
8549 | ||
d25e3a3d JA |
8550 | /* Retain compatibility with failing for an invalid attach attempt */ |
8551 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
8552 | IORING_SETUP_ATTACH_WQ) { | |
8553 | struct fd f; | |
8554 | ||
8555 | f = fdget(p->wq_fd); | |
8556 | if (!f.file) | |
8557 | return -ENXIO; | |
0cc936f7 JA |
8558 | if (f.file->f_op != &io_uring_fops) { |
8559 | fdput(f); | |
f2a48dd0 | 8560 | return -EINVAL; |
0cc936f7 JA |
8561 | } |
8562 | fdput(f); | |
d25e3a3d | 8563 | } |
6c271ce2 | 8564 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 8565 | struct task_struct *tsk; |
534ca6d6 | 8566 | struct io_sq_data *sqd; |
26984fbf | 8567 | bool attached; |
534ca6d6 | 8568 | |
26984fbf | 8569 | sqd = io_get_sq_data(p, &attached); |
534ca6d6 JA |
8570 | if (IS_ERR(sqd)) { |
8571 | ret = PTR_ERR(sqd); | |
8572 | goto err; | |
8573 | } | |
69fb2131 | 8574 | |
7c30f36a | 8575 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 8576 | ctx->sq_data = sqd; |
917257da JA |
8577 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
8578 | if (!ctx->sq_thread_idle) | |
8579 | ctx->sq_thread_idle = HZ; | |
8580 | ||
78d7f6ba | 8581 | io_sq_thread_park(sqd); |
de75a3d3 PB |
8582 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
8583 | io_sqd_update_thread_idle(sqd); | |
26984fbf | 8584 | /* don't attach to a dying SQPOLL thread, would be racy */ |
f2a48dd0 | 8585 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
78d7f6ba PB |
8586 | io_sq_thread_unpark(sqd); |
8587 | ||
de75a3d3 PB |
8588 | if (ret < 0) |
8589 | goto err; | |
8590 | if (attached) | |
5aa75ed5 | 8591 | return 0; |
aa06165d | 8592 | |
6c271ce2 | 8593 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 8594 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 8595 | |
917257da | 8596 | ret = -EINVAL; |
f2a48dd0 | 8597 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
e8f98f24 | 8598 | goto err_sqpoll; |
37d1e2e3 | 8599 | sqd->sq_cpu = cpu; |
6c271ce2 | 8600 | } else { |
37d1e2e3 | 8601 | sqd->sq_cpu = -1; |
6c271ce2 | 8602 | } |
37d1e2e3 JA |
8603 | |
8604 | sqd->task_pid = current->pid; | |
5c2469e0 | 8605 | sqd->task_tgid = current->tgid; |
46fe18b1 JA |
8606 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
8607 | if (IS_ERR(tsk)) { | |
8608 | ret = PTR_ERR(tsk); | |
e8f98f24 | 8609 | goto err_sqpoll; |
6c271ce2 | 8610 | } |
97a73a0f | 8611 | |
46fe18b1 | 8612 | sqd->thread = tsk; |
97a73a0f | 8613 | ret = io_uring_alloc_task_context(tsk, ctx); |
46fe18b1 | 8614 | wake_up_new_task(tsk); |
0f212204 JA |
8615 | if (ret) |
8616 | goto err; | |
6c271ce2 JA |
8617 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
8618 | /* Can't have SQ_AFF without SQPOLL */ | |
8619 | ret = -EINVAL; | |
8620 | goto err; | |
8621 | } | |
8622 | ||
2b188cc1 | 8623 | return 0; |
f2a48dd0 PB |
8624 | err_sqpoll: |
8625 | complete(&ctx->sq_data->exited); | |
2b188cc1 | 8626 | err: |
37d1e2e3 | 8627 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
8628 | return ret; |
8629 | } | |
8630 | ||
a087e2b5 BM |
8631 | static inline void __io_unaccount_mem(struct user_struct *user, |
8632 | unsigned long nr_pages) | |
2b188cc1 JA |
8633 | { |
8634 | atomic_long_sub(nr_pages, &user->locked_vm); | |
8635 | } | |
8636 | ||
a087e2b5 BM |
8637 | static inline int __io_account_mem(struct user_struct *user, |
8638 | unsigned long nr_pages) | |
2b188cc1 JA |
8639 | { |
8640 | unsigned long page_limit, cur_pages, new_pages; | |
8641 | ||
8642 | /* Don't allow more pages than we can safely lock */ | |
8643 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
8644 | ||
8645 | do { | |
8646 | cur_pages = atomic_long_read(&user->locked_vm); | |
8647 | new_pages = cur_pages + nr_pages; | |
8648 | if (new_pages > page_limit) | |
8649 | return -ENOMEM; | |
8650 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
8651 | new_pages) != cur_pages); | |
8652 | ||
8653 | return 0; | |
8654 | } | |
8655 | ||
26bfa89e | 8656 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8657 | { |
62e398be | 8658 | if (ctx->user) |
a087e2b5 | 8659 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 8660 | |
26bfa89e JA |
8661 | if (ctx->mm_account) |
8662 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8663 | } |
8664 | ||
26bfa89e | 8665 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8666 | { |
30975825 BM |
8667 | int ret; |
8668 | ||
62e398be | 8669 | if (ctx->user) { |
30975825 BM |
8670 | ret = __io_account_mem(ctx->user, nr_pages); |
8671 | if (ret) | |
8672 | return ret; | |
8673 | } | |
8674 | ||
26bfa89e JA |
8675 | if (ctx->mm_account) |
8676 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8677 | |
8678 | return 0; | |
8679 | } | |
8680 | ||
2b188cc1 JA |
8681 | static void io_mem_free(void *ptr) |
8682 | { | |
52e04ef4 MR |
8683 | struct page *page; |
8684 | ||
8685 | if (!ptr) | |
8686 | return; | |
2b188cc1 | 8687 | |
52e04ef4 | 8688 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8689 | if (put_page_testzero(page)) |
8690 | free_compound_page(page); | |
8691 | } | |
8692 | ||
8693 | static void *io_mem_alloc(size_t size) | |
8694 | { | |
8695 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
26bfa89e | 8696 | __GFP_NORETRY | __GFP_ACCOUNT; |
2b188cc1 JA |
8697 | |
8698 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8699 | } | |
8700 | ||
75b28aff HV |
8701 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8702 | size_t *sq_offset) | |
8703 | { | |
8704 | struct io_rings *rings; | |
8705 | size_t off, sq_array_size; | |
8706 | ||
8707 | off = struct_size(rings, cqes, cq_entries); | |
8708 | if (off == SIZE_MAX) | |
8709 | return SIZE_MAX; | |
8710 | ||
8711 | #ifdef CONFIG_SMP | |
8712 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8713 | if (off == 0) | |
8714 | return SIZE_MAX; | |
8715 | #endif | |
8716 | ||
b36200f5 DV |
8717 | if (sq_offset) |
8718 | *sq_offset = off; | |
8719 | ||
75b28aff HV |
8720 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8721 | if (sq_array_size == SIZE_MAX) | |
8722 | return SIZE_MAX; | |
8723 | ||
8724 | if (check_add_overflow(off, sq_array_size, &off)) | |
8725 | return SIZE_MAX; | |
8726 | ||
75b28aff HV |
8727 | return off; |
8728 | } | |
8729 | ||
41edf1a5 | 8730 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot) |
7f61a1e9 | 8731 | { |
41edf1a5 | 8732 | struct io_mapped_ubuf *imu = *slot; |
7f61a1e9 PB |
8733 | unsigned int i; |
8734 | ||
6224843d PB |
8735 | if (imu != ctx->dummy_ubuf) { |
8736 | for (i = 0; i < imu->nr_bvecs; i++) | |
8737 | unpin_user_page(imu->bvec[i].bv_page); | |
8738 | if (imu->acct_pages) | |
8739 | io_unaccount_mem(ctx, imu->acct_pages); | |
8740 | kvfree(imu); | |
8741 | } | |
41edf1a5 | 8742 | *slot = NULL; |
7f61a1e9 PB |
8743 | } |
8744 | ||
bd54b6fe | 8745 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
edafccee | 8746 | { |
634d00df PB |
8747 | io_buffer_unmap(ctx, &prsrc->buf); |
8748 | prsrc->buf = NULL; | |
bd54b6fe | 8749 | } |
edafccee | 8750 | |
bd54b6fe BM |
8751 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
8752 | { | |
8753 | unsigned int i; | |
edafccee | 8754 | |
7f61a1e9 PB |
8755 | for (i = 0; i < ctx->nr_user_bufs; i++) |
8756 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); | |
edafccee | 8757 | kfree(ctx->user_bufs); |
bb6659cc | 8758 | io_rsrc_data_free(ctx->buf_data); |
edafccee | 8759 | ctx->user_bufs = NULL; |
bd54b6fe | 8760 | ctx->buf_data = NULL; |
edafccee | 8761 | ctx->nr_user_bufs = 0; |
bd54b6fe BM |
8762 | } |
8763 | ||
0a96bbe4 | 8764 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee | 8765 | { |
bd54b6fe | 8766 | int ret; |
edafccee | 8767 | |
bd54b6fe | 8768 | if (!ctx->buf_data) |
edafccee JA |
8769 | return -ENXIO; |
8770 | ||
bd54b6fe BM |
8771 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
8772 | if (!ret) | |
8773 | __io_sqe_buffers_unregister(ctx); | |
8774 | return ret; | |
edafccee JA |
8775 | } |
8776 | ||
8777 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8778 | void __user *arg, unsigned index) | |
8779 | { | |
8780 | struct iovec __user *src; | |
8781 | ||
8782 | #ifdef CONFIG_COMPAT | |
8783 | if (ctx->compat) { | |
8784 | struct compat_iovec __user *ciovs; | |
8785 | struct compat_iovec ciov; | |
8786 | ||
8787 | ciovs = (struct compat_iovec __user *) arg; | |
8788 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8789 | return -EFAULT; | |
8790 | ||
d55e5f5b | 8791 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8792 | dst->iov_len = ciov.iov_len; |
8793 | return 0; | |
8794 | } | |
8795 | #endif | |
8796 | src = (struct iovec __user *) arg; | |
8797 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8798 | return -EFAULT; | |
8799 | return 0; | |
8800 | } | |
8801 | ||
de293938 JA |
8802 | /* |
8803 | * Not super efficient, but this is just a registration time. And we do cache | |
8804 | * the last compound head, so generally we'll only do a full search if we don't | |
8805 | * match that one. | |
8806 | * | |
8807 | * We check if the given compound head page has already been accounted, to | |
8808 | * avoid double accounting it. This allows us to account the full size of the | |
8809 | * page, not just the constituent pages of a huge page. | |
8810 | */ | |
8811 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8812 | int nr_pages, struct page *hpage) | |
8813 | { | |
8814 | int i, j; | |
8815 | ||
8816 | /* check current page array */ | |
8817 | for (i = 0; i < nr_pages; i++) { | |
8818 | if (!PageCompound(pages[i])) | |
8819 | continue; | |
8820 | if (compound_head(pages[i]) == hpage) | |
8821 | return true; | |
8822 | } | |
8823 | ||
8824 | /* check previously registered pages */ | |
8825 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
41edf1a5 | 8826 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
de293938 JA |
8827 | |
8828 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8829 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8830 | continue; | |
8831 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8832 | return true; | |
8833 | } | |
8834 | } | |
8835 | ||
8836 | return false; | |
8837 | } | |
8838 | ||
8839 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8840 | int nr_pages, struct io_mapped_ubuf *imu, | |
8841 | struct page **last_hpage) | |
8842 | { | |
8843 | int i, ret; | |
8844 | ||
216e5835 | 8845 | imu->acct_pages = 0; |
de293938 JA |
8846 | for (i = 0; i < nr_pages; i++) { |
8847 | if (!PageCompound(pages[i])) { | |
8848 | imu->acct_pages++; | |
8849 | } else { | |
8850 | struct page *hpage; | |
8851 | ||
8852 | hpage = compound_head(pages[i]); | |
8853 | if (hpage == *last_hpage) | |
8854 | continue; | |
8855 | *last_hpage = hpage; | |
8856 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8857 | continue; | |
8858 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8859 | } | |
8860 | } | |
8861 | ||
8862 | if (!imu->acct_pages) | |
8863 | return 0; | |
8864 | ||
26bfa89e | 8865 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
8866 | if (ret) |
8867 | imu->acct_pages = 0; | |
8868 | return ret; | |
8869 | } | |
8870 | ||
0a96bbe4 | 8871 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
41edf1a5 | 8872 | struct io_mapped_ubuf **pimu, |
0a96bbe4 | 8873 | struct page **last_hpage) |
edafccee | 8874 | { |
41edf1a5 | 8875 | struct io_mapped_ubuf *imu = NULL; |
edafccee JA |
8876 | struct vm_area_struct **vmas = NULL; |
8877 | struct page **pages = NULL; | |
0a96bbe4 BM |
8878 | unsigned long off, start, end, ubuf; |
8879 | size_t size; | |
8880 | int ret, pret, nr_pages, i; | |
8881 | ||
6224843d PB |
8882 | if (!iov->iov_base) { |
8883 | *pimu = ctx->dummy_ubuf; | |
8884 | return 0; | |
8885 | } | |
8886 | ||
0a96bbe4 BM |
8887 | ubuf = (unsigned long) iov->iov_base; |
8888 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8889 | start = ubuf >> PAGE_SHIFT; | |
8890 | nr_pages = end - start; | |
8891 | ||
41edf1a5 | 8892 | *pimu = NULL; |
0a96bbe4 BM |
8893 | ret = -ENOMEM; |
8894 | ||
8895 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8896 | if (!pages) | |
8897 | goto done; | |
8898 | ||
8899 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8900 | GFP_KERNEL); | |
8901 | if (!vmas) | |
8902 | goto done; | |
edafccee | 8903 | |
41edf1a5 | 8904 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
a2b4198c | 8905 | if (!imu) |
0a96bbe4 BM |
8906 | goto done; |
8907 | ||
8908 | ret = 0; | |
8909 | mmap_read_lock(current->mm); | |
8910 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8911 | pages, vmas); | |
8912 | if (pret == nr_pages) { | |
8913 | /* don't support file backed memory */ | |
8914 | for (i = 0; i < nr_pages; i++) { | |
8915 | struct vm_area_struct *vma = vmas[i]; | |
8916 | ||
40dad765 PB |
8917 | if (vma_is_shmem(vma)) |
8918 | continue; | |
0a96bbe4 BM |
8919 | if (vma->vm_file && |
8920 | !is_file_hugepages(vma->vm_file)) { | |
8921 | ret = -EOPNOTSUPP; | |
8922 | break; | |
8923 | } | |
8924 | } | |
8925 | } else { | |
8926 | ret = pret < 0 ? pret : -EFAULT; | |
8927 | } | |
8928 | mmap_read_unlock(current->mm); | |
8929 | if (ret) { | |
8930 | /* | |
8931 | * if we did partial map, or found file backed vmas, | |
8932 | * release any pages we did get | |
8933 | */ | |
8934 | if (pret > 0) | |
8935 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
8936 | goto done; |
8937 | } | |
8938 | ||
8939 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
8940 | if (ret) { | |
8941 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
8942 | goto done; |
8943 | } | |
8944 | ||
8945 | off = ubuf & ~PAGE_MASK; | |
8946 | size = iov->iov_len; | |
8947 | for (i = 0; i < nr_pages; i++) { | |
8948 | size_t vec_len; | |
8949 | ||
8950 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
8951 | imu->bvec[i].bv_page = pages[i]; | |
8952 | imu->bvec[i].bv_len = vec_len; | |
8953 | imu->bvec[i].bv_offset = off; | |
8954 | off = 0; | |
8955 | size -= vec_len; | |
8956 | } | |
8957 | /* store original address for later verification */ | |
8958 | imu->ubuf = ubuf; | |
4751f53d | 8959 | imu->ubuf_end = ubuf + iov->iov_len; |
0a96bbe4 | 8960 | imu->nr_bvecs = nr_pages; |
41edf1a5 | 8961 | *pimu = imu; |
0a96bbe4 BM |
8962 | ret = 0; |
8963 | done: | |
41edf1a5 PB |
8964 | if (ret) |
8965 | kvfree(imu); | |
0a96bbe4 BM |
8966 | kvfree(pages); |
8967 | kvfree(vmas); | |
8968 | return ret; | |
8969 | } | |
8970 | ||
2b358604 | 8971 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 8972 | { |
87094465 PB |
8973 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
8974 | return ctx->user_bufs ? 0 : -ENOMEM; | |
2b358604 | 8975 | } |
edafccee | 8976 | |
2b358604 BM |
8977 | static int io_buffer_validate(struct iovec *iov) |
8978 | { | |
50e96989 PB |
8979 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
8980 | ||
2b358604 BM |
8981 | /* |
8982 | * Don't impose further limits on the size and buffer | |
8983 | * constraints here, we'll -EINVAL later when IO is | |
8984 | * submitted if they are wrong. | |
8985 | */ | |
6224843d PB |
8986 | if (!iov->iov_base) |
8987 | return iov->iov_len ? -EFAULT : 0; | |
8988 | if (!iov->iov_len) | |
2b358604 | 8989 | return -EFAULT; |
edafccee | 8990 | |
2b358604 BM |
8991 | /* arbitrary limit, but we need something */ |
8992 | if (iov->iov_len > SZ_1G) | |
8993 | return -EFAULT; | |
edafccee | 8994 | |
50e96989 PB |
8995 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
8996 | return -EOVERFLOW; | |
8997 | ||
2b358604 BM |
8998 | return 0; |
8999 | } | |
edafccee | 9000 | |
2b358604 | 9001 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
634d00df | 9002 | unsigned int nr_args, u64 __user *tags) |
2b358604 | 9003 | { |
bd54b6fe BM |
9004 | struct page *last_hpage = NULL; |
9005 | struct io_rsrc_data *data; | |
2b358604 BM |
9006 | int i, ret; |
9007 | struct iovec iov; | |
edafccee | 9008 | |
87094465 PB |
9009 | if (ctx->user_bufs) |
9010 | return -EBUSY; | |
489809e2 | 9011 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
87094465 | 9012 | return -EINVAL; |
bd54b6fe | 9013 | ret = io_rsrc_node_switch_start(ctx); |
2b358604 BM |
9014 | if (ret) |
9015 | return ret; | |
d878c816 PB |
9016 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
9017 | if (ret) | |
9018 | return ret; | |
bd54b6fe BM |
9019 | ret = io_buffers_map_alloc(ctx, nr_args); |
9020 | if (ret) { | |
bb6659cc | 9021 | io_rsrc_data_free(data); |
bd54b6fe BM |
9022 | return ret; |
9023 | } | |
edafccee | 9024 | |
87094465 | 9025 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
edafccee JA |
9026 | ret = io_copy_iov(ctx, &iov, arg, i); |
9027 | if (ret) | |
0a96bbe4 | 9028 | break; |
2b358604 BM |
9029 | ret = io_buffer_validate(&iov); |
9030 | if (ret) | |
0a96bbe4 | 9031 | break; |
2d091d62 | 9032 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
cf3770e7 CIK |
9033 | ret = -EINVAL; |
9034 | break; | |
9035 | } | |
edafccee | 9036 | |
41edf1a5 PB |
9037 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
9038 | &last_hpage); | |
0a96bbe4 BM |
9039 | if (ret) |
9040 | break; | |
edafccee | 9041 | } |
0a96bbe4 | 9042 | |
bd54b6fe | 9043 | WARN_ON_ONCE(ctx->buf_data); |
0a96bbe4 | 9044 | |
bd54b6fe BM |
9045 | ctx->buf_data = data; |
9046 | if (ret) | |
9047 | __io_sqe_buffers_unregister(ctx); | |
9048 | else | |
9049 | io_rsrc_node_switch(ctx, NULL); | |
edafccee JA |
9050 | return ret; |
9051 | } | |
9052 | ||
634d00df PB |
9053 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
9054 | struct io_uring_rsrc_update2 *up, | |
9055 | unsigned int nr_args) | |
9056 | { | |
9057 | u64 __user *tags = u64_to_user_ptr(up->tags); | |
9058 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); | |
634d00df PB |
9059 | struct page *last_hpage = NULL; |
9060 | bool needs_switch = false; | |
9061 | __u32 done; | |
9062 | int i, err; | |
9063 | ||
9064 | if (!ctx->buf_data) | |
9065 | return -ENXIO; | |
9066 | if (up->offset + nr_args > ctx->nr_user_bufs) | |
9067 | return -EINVAL; | |
9068 | ||
9069 | for (done = 0; done < nr_args; done++) { | |
0b8c0e7c PB |
9070 | struct io_mapped_ubuf *imu; |
9071 | int offset = up->offset + done; | |
634d00df PB |
9072 | u64 tag = 0; |
9073 | ||
9074 | err = io_copy_iov(ctx, &iov, iovs, done); | |
9075 | if (err) | |
9076 | break; | |
9077 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { | |
9078 | err = -EFAULT; | |
9079 | break; | |
9080 | } | |
0b8c0e7c PB |
9081 | err = io_buffer_validate(&iov); |
9082 | if (err) | |
9083 | break; | |
cf3770e7 CIK |
9084 | if (!iov.iov_base && tag) { |
9085 | err = -EINVAL; | |
9086 | break; | |
9087 | } | |
0b8c0e7c PB |
9088 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
9089 | if (err) | |
9090 | break; | |
634d00df | 9091 | |
0b8c0e7c | 9092 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
6224843d | 9093 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
0b8c0e7c PB |
9094 | err = io_queue_rsrc_removal(ctx->buf_data, offset, |
9095 | ctx->rsrc_node, ctx->user_bufs[i]); | |
9096 | if (unlikely(err)) { | |
9097 | io_buffer_unmap(ctx, &imu); | |
634d00df | 9098 | break; |
0b8c0e7c | 9099 | } |
634d00df PB |
9100 | ctx->user_bufs[i] = NULL; |
9101 | needs_switch = true; | |
9102 | } | |
9103 | ||
0b8c0e7c | 9104 | ctx->user_bufs[i] = imu; |
2d091d62 | 9105 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
634d00df PB |
9106 | } |
9107 | ||
9108 | if (needs_switch) | |
9109 | io_rsrc_node_switch(ctx, ctx->buf_data); | |
9110 | return done ? done : err; | |
9111 | } | |
9112 | ||
9b402849 JA |
9113 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
9114 | { | |
9115 | __s32 __user *fds = arg; | |
9116 | int fd; | |
9117 | ||
9118 | if (ctx->cq_ev_fd) | |
9119 | return -EBUSY; | |
9120 | ||
9121 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
9122 | return -EFAULT; | |
9123 | ||
9124 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
9125 | if (IS_ERR(ctx->cq_ev_fd)) { | |
9126 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
fe7e3257 | 9127 | |
9b402849 JA |
9128 | ctx->cq_ev_fd = NULL; |
9129 | return ret; | |
9130 | } | |
9131 | ||
9132 | return 0; | |
9133 | } | |
9134 | ||
9135 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
9136 | { | |
9137 | if (ctx->cq_ev_fd) { | |
9138 | eventfd_ctx_put(ctx->cq_ev_fd); | |
9139 | ctx->cq_ev_fd = NULL; | |
9140 | return 0; | |
9141 | } | |
9142 | ||
9143 | return -ENXIO; | |
9144 | } | |
9145 | ||
5a2e745d JA |
9146 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
9147 | { | |
9e15c3a0 JA |
9148 | struct io_buffer *buf; |
9149 | unsigned long index; | |
9150 | ||
8bab4c09 | 9151 | xa_for_each(&ctx->io_buffers, index, buf) { |
9e15c3a0 | 9152 | __io_remove_buffers(ctx, buf, index, -1U); |
8bab4c09 JA |
9153 | cond_resched(); |
9154 | } | |
5a2e745d JA |
9155 | } |
9156 | ||
4010fec4 | 9157 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9158 | { |
cd0ca2e0 | 9159 | struct io_submit_state *state = &ctx->submit_state; |
37f0e767 | 9160 | int nr = 0; |
bf019da7 | 9161 | |
9a4fdbd8 | 9162 | mutex_lock(&ctx->uring_lock); |
cd0ca2e0 | 9163 | io_flush_cached_locked_reqs(ctx, state); |
c2b6c6bc PB |
9164 | |
9165 | while (state->free_list.next) { | |
9166 | struct io_wq_work_node *node; | |
9167 | struct io_kiocb *req; | |
9168 | ||
9169 | node = wq_stack_extract(&state->free_list); | |
9170 | req = container_of(node, struct io_kiocb, comp_list); | |
9171 | kmem_cache_free(req_cachep, req); | |
37f0e767 | 9172 | nr++; |
c2b6c6bc | 9173 | } |
37f0e767 PB |
9174 | if (nr) |
9175 | percpu_ref_put_many(&ctx->refs, nr); | |
9a4fdbd8 JA |
9176 | mutex_unlock(&ctx->uring_lock); |
9177 | } | |
9178 | ||
43597aac | 9179 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
2b188cc1 | 9180 | { |
43597aac | 9181 | if (data && !atomic_dec_and_test(&data->refs)) |
bd54b6fe | 9182 | wait_for_completion(&data->done); |
bd54b6fe | 9183 | } |
04fc6c80 | 9184 | |
c072481d | 9185 | static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9186 | { |
37d1e2e3 | 9187 | io_sq_thread_finish(ctx); |
2aede0e4 | 9188 | |
37d1e2e3 | 9189 | if (ctx->mm_account) { |
2aede0e4 JA |
9190 | mmdrop(ctx->mm_account); |
9191 | ctx->mm_account = NULL; | |
30975825 | 9192 | } |
def596e9 | 9193 | |
43597aac PB |
9194 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
9195 | io_wait_rsrc_data(ctx->buf_data); | |
9196 | io_wait_rsrc_data(ctx->file_data); | |
9197 | ||
8bad28d8 | 9198 | mutex_lock(&ctx->uring_lock); |
43597aac | 9199 | if (ctx->buf_data) |
bd54b6fe | 9200 | __io_sqe_buffers_unregister(ctx); |
43597aac | 9201 | if (ctx->file_data) |
08480400 | 9202 | __io_sqe_files_unregister(ctx); |
c4ea060e PB |
9203 | if (ctx->rings) |
9204 | __io_cqring_overflow_flush(ctx, true); | |
8bad28d8 | 9205 | mutex_unlock(&ctx->uring_lock); |
9b402849 | 9206 | io_eventfd_unregister(ctx); |
5a2e745d | 9207 | io_destroy_buffers(ctx); |
07db298a PB |
9208 | if (ctx->sq_creds) |
9209 | put_cred(ctx->sq_creds); | |
def596e9 | 9210 | |
a7f0ed5a PB |
9211 | /* there are no registered resources left, nobody uses it */ |
9212 | if (ctx->rsrc_node) | |
9213 | io_rsrc_node_destroy(ctx->rsrc_node); | |
8dd03afe | 9214 | if (ctx->rsrc_backup_node) |
b895c9a6 | 9215 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
a7f0ed5a PB |
9216 | flush_delayed_work(&ctx->rsrc_put_work); |
9217 | ||
9218 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); | |
9219 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); | |
def596e9 | 9220 | |
2b188cc1 | 9221 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
9222 | if (ctx->ring_sock) { |
9223 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 9224 | sock_release(ctx->ring_sock); |
355e8d26 | 9225 | } |
2b188cc1 | 9226 | #endif |
ef9dd637 | 9227 | WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); |
2b188cc1 | 9228 | |
75b28aff | 9229 | io_mem_free(ctx->rings); |
2b188cc1 | 9230 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
9231 | |
9232 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 9233 | free_uid(ctx->user); |
4010fec4 | 9234 | io_req_caches_free(ctx); |
e941894e JA |
9235 | if (ctx->hash_map) |
9236 | io_wq_put_hash(ctx->hash_map); | |
78076bb6 | 9237 | kfree(ctx->cancel_hash); |
6224843d | 9238 | kfree(ctx->dummy_ubuf); |
2b188cc1 JA |
9239 | kfree(ctx); |
9240 | } | |
9241 | ||
9242 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
9243 | { | |
9244 | struct io_ring_ctx *ctx = file->private_data; | |
9245 | __poll_t mask = 0; | |
9246 | ||
d60aa65b | 9247 | poll_wait(file, &ctx->cq_wait, wait); |
4f7067c3 SB |
9248 | /* |
9249 | * synchronizes with barrier from wq_has_sleeper call in | |
9250 | * io_commit_cqring | |
9251 | */ | |
2b188cc1 | 9252 | smp_rmb(); |
90554200 | 9253 | if (!io_sqring_full(ctx)) |
2b188cc1 | 9254 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
9255 | |
9256 | /* | |
9257 | * Don't flush cqring overflow list here, just do a simple check. | |
9258 | * Otherwise there could possible be ABBA deadlock: | |
9259 | * CPU0 CPU1 | |
9260 | * ---- ---- | |
9261 | * lock(&ctx->uring_lock); | |
9262 | * lock(&ep->mtx); | |
9263 | * lock(&ctx->uring_lock); | |
9264 | * lock(&ep->mtx); | |
9265 | * | |
9266 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
9267 | * pushs them to do the flush. | |
9268 | */ | |
5ed7a37d | 9269 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
2b188cc1 JA |
9270 | mask |= EPOLLIN | EPOLLRDNORM; |
9271 | ||
9272 | return mask; | |
9273 | } | |
9274 | ||
0bead8cd | 9275 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 9276 | { |
4379bf8b | 9277 | const struct cred *creds; |
071698e1 | 9278 | |
61cf9370 | 9279 | creds = xa_erase(&ctx->personalities, id); |
4379bf8b JA |
9280 | if (creds) { |
9281 | put_cred(creds); | |
0bead8cd | 9282 | return 0; |
1e6fa521 | 9283 | } |
0bead8cd YD |
9284 | |
9285 | return -EINVAL; | |
9286 | } | |
9287 | ||
d56d938b PB |
9288 | struct io_tctx_exit { |
9289 | struct callback_head task_work; | |
9290 | struct completion completion; | |
baf186c4 | 9291 | struct io_ring_ctx *ctx; |
d56d938b PB |
9292 | }; |
9293 | ||
c072481d | 9294 | static __cold void io_tctx_exit_cb(struct callback_head *cb) |
d56d938b PB |
9295 | { |
9296 | struct io_uring_task *tctx = current->io_uring; | |
9297 | struct io_tctx_exit *work; | |
9298 | ||
9299 | work = container_of(cb, struct io_tctx_exit, task_work); | |
9300 | /* | |
9301 | * When @in_idle, we're in cancellation and it's racy to remove the | |
9302 | * node. It'll be removed by the end of cancellation, just ignore it. | |
9303 | */ | |
9304 | if (!atomic_read(&tctx->in_idle)) | |
eef51daa | 9305 | io_uring_del_tctx_node((unsigned long)work->ctx); |
d56d938b PB |
9306 | complete(&work->completion); |
9307 | } | |
9308 | ||
c072481d | 9309 | static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
28090c13 PB |
9310 | { |
9311 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
9312 | ||
9313 | return req->ctx == data; | |
9314 | } | |
9315 | ||
c072481d | 9316 | static __cold void io_ring_exit_work(struct work_struct *work) |
85faa7b8 | 9317 | { |
d56d938b | 9318 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 9319 | unsigned long timeout = jiffies + HZ * 60 * 5; |
58d3be2c | 9320 | unsigned long interval = HZ / 20; |
d56d938b PB |
9321 | struct io_tctx_exit exit; |
9322 | struct io_tctx_node *node; | |
9323 | int ret; | |
85faa7b8 | 9324 | |
56952e91 JA |
9325 | /* |
9326 | * If we're doing polled IO and end up having requests being | |
9327 | * submitted async (out-of-line), then completions can come in while | |
9328 | * we're waiting for refs to drop. We need to reap these manually, | |
9329 | * as nobody else will be looking for them. | |
9330 | */ | |
b2edc0a7 | 9331 | do { |
3dd0c97a | 9332 | io_uring_try_cancel_requests(ctx, NULL, true); |
28090c13 PB |
9333 | if (ctx->sq_data) { |
9334 | struct io_sq_data *sqd = ctx->sq_data; | |
9335 | struct task_struct *tsk; | |
9336 | ||
9337 | io_sq_thread_park(sqd); | |
9338 | tsk = sqd->thread; | |
9339 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) | |
9340 | io_wq_cancel_cb(tsk->io_uring->io_wq, | |
9341 | io_cancel_ctx_cb, ctx, true); | |
9342 | io_sq_thread_unpark(sqd); | |
9343 | } | |
b5bb3a24 | 9344 | |
37f0e767 PB |
9345 | io_req_caches_free(ctx); |
9346 | ||
58d3be2c PB |
9347 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
9348 | /* there is little hope left, don't run it too often */ | |
9349 | interval = HZ * 60; | |
9350 | } | |
9351 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); | |
d56d938b | 9352 | |
7f00651a PB |
9353 | init_completion(&exit.completion); |
9354 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
9355 | exit.ctx = ctx; | |
89b5066e PB |
9356 | /* |
9357 | * Some may use context even when all refs and requests have been put, | |
9358 | * and they are free to do so while still holding uring_lock or | |
5b0a6acc | 9359 | * completion_lock, see io_req_task_submit(). Apart from other work, |
89b5066e PB |
9360 | * this lock/unlock section also waits them to finish. |
9361 | */ | |
d56d938b PB |
9362 | mutex_lock(&ctx->uring_lock); |
9363 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
9364 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
9365 | ||
d56d938b PB |
9366 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
9367 | ctx_node); | |
7f00651a PB |
9368 | /* don't spin on a single task if cancellation failed */ |
9369 | list_rotate_left(&ctx->tctx_list); | |
d56d938b PB |
9370 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
9371 | if (WARN_ON_ONCE(ret)) | |
9372 | continue; | |
9373 | wake_up_process(node->task); | |
9374 | ||
9375 | mutex_unlock(&ctx->uring_lock); | |
9376 | wait_for_completion(&exit.completion); | |
d56d938b PB |
9377 | mutex_lock(&ctx->uring_lock); |
9378 | } | |
9379 | mutex_unlock(&ctx->uring_lock); | |
79ebeaee JA |
9380 | spin_lock(&ctx->completion_lock); |
9381 | spin_unlock(&ctx->completion_lock); | |
d56d938b | 9382 | |
85faa7b8 JA |
9383 | io_ring_ctx_free(ctx); |
9384 | } | |
9385 | ||
80c4cbdb | 9386 | /* Returns true if we found and killed one or more timeouts */ |
c072481d PB |
9387 | static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx, |
9388 | struct task_struct *tsk, bool cancel_all) | |
80c4cbdb PB |
9389 | { |
9390 | struct io_kiocb *req, *tmp; | |
9391 | int canceled = 0; | |
9392 | ||
79ebeaee JA |
9393 | spin_lock(&ctx->completion_lock); |
9394 | spin_lock_irq(&ctx->timeout_lock); | |
80c4cbdb | 9395 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
3dd0c97a | 9396 | if (io_match_task(req, tsk, cancel_all)) { |
80c4cbdb PB |
9397 | io_kill_timeout(req, -ECANCELED); |
9398 | canceled++; | |
9399 | } | |
9400 | } | |
79ebeaee | 9401 | spin_unlock_irq(&ctx->timeout_lock); |
51520426 PB |
9402 | if (canceled != 0) |
9403 | io_commit_cqring(ctx); | |
79ebeaee | 9404 | spin_unlock(&ctx->completion_lock); |
80c4cbdb PB |
9405 | if (canceled != 0) |
9406 | io_cqring_ev_posted(ctx); | |
9407 | return canceled != 0; | |
9408 | } | |
9409 | ||
c072481d | 9410 | static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
2b188cc1 | 9411 | { |
61cf9370 MWO |
9412 | unsigned long index; |
9413 | struct creds *creds; | |
9414 | ||
2b188cc1 JA |
9415 | mutex_lock(&ctx->uring_lock); |
9416 | percpu_ref_kill(&ctx->refs); | |
634578f8 | 9417 | if (ctx->rings) |
6c2450ae | 9418 | __io_cqring_overflow_flush(ctx, true); |
61cf9370 MWO |
9419 | xa_for_each(&ctx->personalities, index, creds) |
9420 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
9421 | mutex_unlock(&ctx->uring_lock); |
9422 | ||
3dd0c97a PB |
9423 | io_kill_timeouts(ctx, NULL, true); |
9424 | io_poll_remove_all(ctx, NULL, true); | |
561fb04a | 9425 | |
15dff286 | 9426 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 9427 | io_iopoll_try_reap_events(ctx); |
309fc03a | 9428 | |
85faa7b8 | 9429 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
9430 | /* |
9431 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
9432 | * if we're exiting a ton of rings at the same time. It just adds | |
9433 | * noise and overhead, there's no discernable change in runtime | |
9434 | * over using system_wq. | |
9435 | */ | |
9436 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
9437 | } |
9438 | ||
9439 | static int io_uring_release(struct inode *inode, struct file *file) | |
9440 | { | |
9441 | struct io_ring_ctx *ctx = file->private_data; | |
9442 | ||
9443 | file->private_data = NULL; | |
9444 | io_ring_ctx_wait_and_kill(ctx); | |
9445 | return 0; | |
9446 | } | |
9447 | ||
f6edbabb PB |
9448 | struct io_task_cancel { |
9449 | struct task_struct *task; | |
3dd0c97a | 9450 | bool all; |
f6edbabb | 9451 | }; |
f254ac04 | 9452 | |
f6edbabb | 9453 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 9454 | { |
9a472ef7 | 9455 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 9456 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
9457 | bool ret; |
9458 | ||
3dd0c97a | 9459 | if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
9460 | struct io_ring_ctx *ctx = req->ctx; |
9461 | ||
9462 | /* protect against races with linked timeouts */ | |
79ebeaee | 9463 | spin_lock(&ctx->completion_lock); |
3dd0c97a | 9464 | ret = io_match_task(req, cancel->task, cancel->all); |
79ebeaee | 9465 | spin_unlock(&ctx->completion_lock); |
9a472ef7 | 9466 | } else { |
3dd0c97a | 9467 | ret = io_match_task(req, cancel->task, cancel->all); |
9a472ef7 PB |
9468 | } |
9469 | return ret; | |
b711d4ea JA |
9470 | } |
9471 | ||
c072481d PB |
9472 | static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
9473 | struct task_struct *task, | |
9474 | bool cancel_all) | |
b7ddce3c | 9475 | { |
e1915f76 | 9476 | struct io_defer_entry *de; |
b7ddce3c PB |
9477 | LIST_HEAD(list); |
9478 | ||
79ebeaee | 9479 | spin_lock(&ctx->completion_lock); |
b7ddce3c | 9480 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
3dd0c97a | 9481 | if (io_match_task(de->req, task, cancel_all)) { |
b7ddce3c PB |
9482 | list_cut_position(&list, &ctx->defer_list, &de->list); |
9483 | break; | |
9484 | } | |
9485 | } | |
79ebeaee | 9486 | spin_unlock(&ctx->completion_lock); |
e1915f76 PB |
9487 | if (list_empty(&list)) |
9488 | return false; | |
b7ddce3c PB |
9489 | |
9490 | while (!list_empty(&list)) { | |
9491 | de = list_first_entry(&list, struct io_defer_entry, list); | |
9492 | list_del_init(&de->list); | |
f41db273 | 9493 | io_req_complete_failed(de->req, -ECANCELED); |
b7ddce3c PB |
9494 | kfree(de); |
9495 | } | |
e1915f76 | 9496 | return true; |
b7ddce3c PB |
9497 | } |
9498 | ||
c072481d | 9499 | static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
1b00764f PB |
9500 | { |
9501 | struct io_tctx_node *node; | |
9502 | enum io_wq_cancel cret; | |
9503 | bool ret = false; | |
9504 | ||
9505 | mutex_lock(&ctx->uring_lock); | |
9506 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
9507 | struct io_uring_task *tctx = node->task->io_uring; | |
9508 | ||
9509 | /* | |
9510 | * io_wq will stay alive while we hold uring_lock, because it's | |
9511 | * killed after ctx nodes, which requires to take the lock. | |
9512 | */ | |
9513 | if (!tctx || !tctx->io_wq) | |
9514 | continue; | |
9515 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
9516 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9517 | } | |
9518 | mutex_unlock(&ctx->uring_lock); | |
9519 | ||
9520 | return ret; | |
9521 | } | |
9522 | ||
c072481d PB |
9523 | static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
9524 | struct task_struct *task, | |
9525 | bool cancel_all) | |
9936c7c2 | 9526 | { |
3dd0c97a | 9527 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
1b00764f | 9528 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 PB |
9529 | |
9530 | while (1) { | |
9531 | enum io_wq_cancel cret; | |
9532 | bool ret = false; | |
9533 | ||
1b00764f PB |
9534 | if (!task) { |
9535 | ret |= io_uring_try_cancel_iowq(ctx); | |
9536 | } else if (tctx && tctx->io_wq) { | |
9537 | /* | |
9538 | * Cancels requests of all rings, not only @ctx, but | |
9539 | * it's fine as the task is in exit/exec. | |
9540 | */ | |
5aa75ed5 | 9541 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
9542 | &cancel, true); |
9543 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9544 | } | |
9545 | ||
9546 | /* SQPOLL thread does its own polling */ | |
3dd0c97a | 9547 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
d052d1d6 | 9548 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
5eef4e87 | 9549 | while (!wq_list_empty(&ctx->iopoll_list)) { |
9936c7c2 PB |
9550 | io_iopoll_try_reap_events(ctx); |
9551 | ret = true; | |
9552 | } | |
9553 | } | |
9554 | ||
3dd0c97a PB |
9555 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
9556 | ret |= io_poll_remove_all(ctx, task, cancel_all); | |
9557 | ret |= io_kill_timeouts(ctx, task, cancel_all); | |
e5dc480d PB |
9558 | if (task) |
9559 | ret |= io_run_task_work(); | |
9936c7c2 PB |
9560 | if (!ret) |
9561 | break; | |
9562 | cond_resched(); | |
9563 | } | |
9564 | } | |
9565 | ||
eef51daa | 9566 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
0f212204 | 9567 | { |
236434c3 | 9568 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 9569 | struct io_tctx_node *node; |
a528b04e | 9570 | int ret; |
236434c3 MWO |
9571 | |
9572 | if (unlikely(!tctx)) { | |
5aa75ed5 | 9573 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
9574 | if (unlikely(ret)) |
9575 | return ret; | |
236434c3 | 9576 | tctx = current->io_uring; |
0f212204 | 9577 | } |
cf27f3b1 PB |
9578 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
9579 | node = kmalloc(sizeof(*node), GFP_KERNEL); | |
9580 | if (!node) | |
9581 | return -ENOMEM; | |
9582 | node->ctx = ctx; | |
9583 | node->task = current; | |
13bf43f5 | 9584 | |
cf27f3b1 PB |
9585 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
9586 | node, GFP_KERNEL)); | |
9587 | if (ret) { | |
9588 | kfree(node); | |
9589 | return ret; | |
0f212204 | 9590 | } |
cf27f3b1 PB |
9591 | |
9592 | mutex_lock(&ctx->uring_lock); | |
9593 | list_add(&node->ctx_node, &ctx->tctx_list); | |
9594 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 9595 | } |
cf27f3b1 | 9596 | tctx->last = ctx; |
0f212204 JA |
9597 | return 0; |
9598 | } | |
9599 | ||
cf27f3b1 PB |
9600 | /* |
9601 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
9602 | */ | |
eef51daa | 9603 | static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
cf27f3b1 PB |
9604 | { |
9605 | struct io_uring_task *tctx = current->io_uring; | |
9606 | ||
9607 | if (likely(tctx && tctx->last == ctx)) | |
9608 | return 0; | |
eef51daa | 9609 | return __io_uring_add_tctx_node(ctx); |
cf27f3b1 PB |
9610 | } |
9611 | ||
0f212204 JA |
9612 | /* |
9613 | * Remove this io_uring_file -> task mapping. | |
9614 | */ | |
c072481d | 9615 | static __cold void io_uring_del_tctx_node(unsigned long index) |
0f212204 JA |
9616 | { |
9617 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 9618 | struct io_tctx_node *node; |
2941267b | 9619 | |
eebd2e37 PB |
9620 | if (!tctx) |
9621 | return; | |
13bf43f5 PB |
9622 | node = xa_erase(&tctx->xa, index); |
9623 | if (!node) | |
2941267b | 9624 | return; |
0f212204 | 9625 | |
13bf43f5 PB |
9626 | WARN_ON_ONCE(current != node->task); |
9627 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
9628 | ||
9629 | mutex_lock(&node->ctx->uring_lock); | |
9630 | list_del(&node->ctx_node); | |
9631 | mutex_unlock(&node->ctx->uring_lock); | |
9632 | ||
baf186c4 | 9633 | if (tctx->last == node->ctx) |
0f212204 | 9634 | tctx->last = NULL; |
13bf43f5 | 9635 | kfree(node); |
0f212204 JA |
9636 | } |
9637 | ||
c072481d | 9638 | static __cold void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 9639 | { |
ba5ef6dc | 9640 | struct io_wq *wq = tctx->io_wq; |
13bf43f5 | 9641 | struct io_tctx_node *node; |
de7f1d9e PB |
9642 | unsigned long index; |
9643 | ||
8bab4c09 | 9644 | xa_for_each(&tctx->xa, index, node) { |
eef51daa | 9645 | io_uring_del_tctx_node(index); |
8bab4c09 JA |
9646 | cond_resched(); |
9647 | } | |
b16ef427 ME |
9648 | if (wq) { |
9649 | /* | |
9650 | * Must be after io_uring_del_task_file() (removes nodes under | |
9651 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). | |
9652 | */ | |
ba5ef6dc | 9653 | io_wq_put_and_exit(wq); |
dadebc35 | 9654 | tctx->io_wq = NULL; |
b16ef427 | 9655 | } |
de7f1d9e PB |
9656 | } |
9657 | ||
3f48cf18 | 9658 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
521d6a73 | 9659 | { |
3f48cf18 PB |
9660 | if (tracked) |
9661 | return atomic_read(&tctx->inflight_tracked); | |
521d6a73 PB |
9662 | return percpu_counter_sum(&tctx->inflight); |
9663 | } | |
9664 | ||
c072481d | 9665 | static __cold void io_uring_drop_tctx_refs(struct task_struct *task) |
09899b19 PB |
9666 | { |
9667 | struct io_uring_task *tctx = task->io_uring; | |
9668 | unsigned int refs = tctx->cached_refs; | |
9669 | ||
e9dbe221 PB |
9670 | if (refs) { |
9671 | tctx->cached_refs = 0; | |
9672 | percpu_counter_sub(&tctx->inflight, refs); | |
9673 | put_task_struct_many(task, refs); | |
9674 | } | |
09899b19 PB |
9675 | } |
9676 | ||
78cc687b PB |
9677 | /* |
9678 | * Find any io_uring ctx that this task has registered or done IO on, and cancel | |
9679 | * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation. | |
9680 | */ | |
c072481d PB |
9681 | static __cold void io_uring_cancel_generic(bool cancel_all, |
9682 | struct io_sq_data *sqd) | |
0e9ddb39 | 9683 | { |
521d6a73 | 9684 | struct io_uring_task *tctx = current->io_uring; |
734551df | 9685 | struct io_ring_ctx *ctx; |
0e9ddb39 PB |
9686 | s64 inflight; |
9687 | DEFINE_WAIT(wait); | |
fdaf083c | 9688 | |
78cc687b PB |
9689 | WARN_ON_ONCE(sqd && sqd->thread != current); |
9690 | ||
6d042ffb PO |
9691 | if (!current->io_uring) |
9692 | return; | |
17a91051 PB |
9693 | if (tctx->io_wq) |
9694 | io_wq_exit_start(tctx->io_wq); | |
9695 | ||
0e9ddb39 PB |
9696 | atomic_inc(&tctx->in_idle); |
9697 | do { | |
e9dbe221 | 9698 | io_uring_drop_tctx_refs(current); |
0e9ddb39 | 9699 | /* read completions before cancelations */ |
78cc687b | 9700 | inflight = tctx_inflight(tctx, !cancel_all); |
0e9ddb39 PB |
9701 | if (!inflight) |
9702 | break; | |
fdaf083c | 9703 | |
78cc687b PB |
9704 | if (!sqd) { |
9705 | struct io_tctx_node *node; | |
9706 | unsigned long index; | |
0f212204 | 9707 | |
78cc687b PB |
9708 | xa_for_each(&tctx->xa, index, node) { |
9709 | /* sqpoll task will cancel all its requests */ | |
9710 | if (node->ctx->sq_data) | |
9711 | continue; | |
9712 | io_uring_try_cancel_requests(node->ctx, current, | |
9713 | cancel_all); | |
9714 | } | |
9715 | } else { | |
9716 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
9717 | io_uring_try_cancel_requests(ctx, current, | |
9718 | cancel_all); | |
9719 | } | |
17a91051 | 9720 | |
0f212204 | 9721 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
e9dbe221 | 9722 | io_uring_drop_tctx_refs(current); |
0f212204 | 9723 | /* |
a1bb3cd5 PB |
9724 | * If we've seen completions, retry without waiting. This |
9725 | * avoids a race where a completion comes in before we did | |
9726 | * prepare_to_wait(). | |
0f212204 | 9727 | */ |
3dd0c97a | 9728 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
a1bb3cd5 | 9729 | schedule(); |
f57555ed | 9730 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 9731 | } while (1); |
fdaf083c | 9732 | atomic_dec(&tctx->in_idle); |
de7f1d9e | 9733 | |
8452d4a6 | 9734 | io_uring_clean_tctx(tctx); |
3dd0c97a | 9735 | if (cancel_all) { |
3f48cf18 PB |
9736 | /* for exec all current's requests should be gone, kill tctx */ |
9737 | __io_uring_free(current); | |
9738 | } | |
44e728b8 PB |
9739 | } |
9740 | ||
f552a27a | 9741 | void __io_uring_cancel(bool cancel_all) |
78cc687b | 9742 | { |
f552a27a | 9743 | io_uring_cancel_generic(cancel_all, NULL); |
78cc687b PB |
9744 | } |
9745 | ||
6c5c240e RP |
9746 | static void *io_uring_validate_mmap_request(struct file *file, |
9747 | loff_t pgoff, size_t sz) | |
2b188cc1 | 9748 | { |
2b188cc1 | 9749 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 9750 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
9751 | struct page *page; |
9752 | void *ptr; | |
9753 | ||
9754 | switch (offset) { | |
9755 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
9756 | case IORING_OFF_CQ_RING: |
9757 | ptr = ctx->rings; | |
2b188cc1 JA |
9758 | break; |
9759 | case IORING_OFF_SQES: | |
9760 | ptr = ctx->sq_sqes; | |
9761 | break; | |
2b188cc1 | 9762 | default: |
6c5c240e | 9763 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
9764 | } |
9765 | ||
9766 | page = virt_to_head_page(ptr); | |
a50b854e | 9767 | if (sz > page_size(page)) |
6c5c240e RP |
9768 | return ERR_PTR(-EINVAL); |
9769 | ||
9770 | return ptr; | |
9771 | } | |
9772 | ||
9773 | #ifdef CONFIG_MMU | |
9774 | ||
c072481d | 9775 | static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
6c5c240e RP |
9776 | { |
9777 | size_t sz = vma->vm_end - vma->vm_start; | |
9778 | unsigned long pfn; | |
9779 | void *ptr; | |
9780 | ||
9781 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
9782 | if (IS_ERR(ptr)) | |
9783 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9784 | |
9785 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9786 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9787 | } | |
9788 | ||
6c5c240e RP |
9789 | #else /* !CONFIG_MMU */ |
9790 | ||
9791 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9792 | { | |
9793 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9794 | } | |
9795 | ||
9796 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9797 | { | |
9798 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9799 | } | |
9800 | ||
9801 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9802 | unsigned long addr, unsigned long len, | |
9803 | unsigned long pgoff, unsigned long flags) | |
9804 | { | |
9805 | void *ptr; | |
9806 | ||
9807 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9808 | if (IS_ERR(ptr)) | |
9809 | return PTR_ERR(ptr); | |
9810 | ||
9811 | return (unsigned long) ptr; | |
9812 | } | |
9813 | ||
9814 | #endif /* !CONFIG_MMU */ | |
9815 | ||
d9d05217 | 9816 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 JA |
9817 | { |
9818 | DEFINE_WAIT(wait); | |
9819 | ||
9820 | do { | |
9821 | if (!io_sqring_full(ctx)) | |
9822 | break; | |
90554200 JA |
9823 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
9824 | ||
9825 | if (!io_sqring_full(ctx)) | |
9826 | break; | |
90554200 JA |
9827 | schedule(); |
9828 | } while (!signal_pending(current)); | |
9829 | ||
9830 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
5199328a | 9831 | return 0; |
90554200 JA |
9832 | } |
9833 | ||
c73ebb68 HX |
9834 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9835 | struct __kernel_timespec __user **ts, | |
9836 | const sigset_t __user **sig) | |
9837 | { | |
9838 | struct io_uring_getevents_arg arg; | |
9839 | ||
9840 | /* | |
9841 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9842 | * is just a pointer to the sigset_t. | |
9843 | */ | |
9844 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9845 | *sig = (const sigset_t __user *) argp; | |
9846 | *ts = NULL; | |
9847 | return 0; | |
9848 | } | |
9849 | ||
9850 | /* | |
9851 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9852 | * timespec and sigset_t pointers if good. | |
9853 | */ | |
9854 | if (*argsz != sizeof(arg)) | |
9855 | return -EINVAL; | |
9856 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9857 | return -EFAULT; | |
9858 | *sig = u64_to_user_ptr(arg.sigmask); | |
9859 | *argsz = arg.sigmask_sz; | |
9860 | *ts = u64_to_user_ptr(arg.ts); | |
9861 | return 0; | |
9862 | } | |
9863 | ||
2b188cc1 | 9864 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9865 | u32, min_complete, u32, flags, const void __user *, argp, |
9866 | size_t, argsz) | |
2b188cc1 JA |
9867 | { |
9868 | struct io_ring_ctx *ctx; | |
2b188cc1 JA |
9869 | int submitted = 0; |
9870 | struct fd f; | |
33f993da | 9871 | long ret; |
2b188cc1 | 9872 | |
4c6e277c | 9873 | io_run_task_work(); |
b41e9852 | 9874 | |
33f993da PB |
9875 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
9876 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) | |
2b188cc1 JA |
9877 | return -EINVAL; |
9878 | ||
9879 | f = fdget(fd); | |
33f993da | 9880 | if (unlikely(!f.file)) |
2b188cc1 JA |
9881 | return -EBADF; |
9882 | ||
9883 | ret = -EOPNOTSUPP; | |
33f993da | 9884 | if (unlikely(f.file->f_op != &io_uring_fops)) |
2b188cc1 JA |
9885 | goto out_fput; |
9886 | ||
9887 | ret = -ENXIO; | |
9888 | ctx = f.file->private_data; | |
33f993da | 9889 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
2b188cc1 JA |
9890 | goto out_fput; |
9891 | ||
7e84e1c7 | 9892 | ret = -EBADFD; |
33f993da | 9893 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
9894 | goto out; |
9895 | ||
6c271ce2 JA |
9896 | /* |
9897 | * For SQ polling, the thread will do all submissions and completions. | |
9898 | * Just return the requested submit count, and wake the thread if | |
9899 | * we were asked to. | |
9900 | */ | |
b2a9eada | 9901 | ret = 0; |
6c271ce2 | 9902 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
90f67366 | 9903 | io_cqring_overflow_flush(ctx); |
89448c47 | 9904 | |
21f96522 JA |
9905 | if (unlikely(ctx->sq_data->thread == NULL)) { |
9906 | ret = -EOWNERDEAD; | |
04147488 | 9907 | goto out; |
21f96522 | 9908 | } |
6c271ce2 | 9909 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9910 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9911 | if (flags & IORING_ENTER_SQ_WAIT) { |
9912 | ret = io_sqpoll_wait_sq(ctx); | |
9913 | if (ret) | |
9914 | goto out; | |
9915 | } | |
6c271ce2 | 9916 | submitted = to_submit; |
b2a9eada | 9917 | } else if (to_submit) { |
eef51daa | 9918 | ret = io_uring_add_tctx_node(ctx); |
0f212204 JA |
9919 | if (unlikely(ret)) |
9920 | goto out; | |
2b188cc1 | 9921 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9922 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9923 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9924 | |
9925 | if (submitted != to_submit) | |
9926 | goto out; | |
2b188cc1 JA |
9927 | } |
9928 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9929 | const sigset_t __user *sig; |
9930 | struct __kernel_timespec __user *ts; | |
9931 | ||
9932 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9933 | if (unlikely(ret)) | |
9934 | goto out; | |
9935 | ||
2b188cc1 JA |
9936 | min_complete = min(min_complete, ctx->cq_entries); |
9937 | ||
32b2244a XW |
9938 | /* |
9939 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9940 | * space applications don't need to do io completion events | |
9941 | * polling again, they can rely on io_sq_thread to do polling | |
9942 | * work, which can reduce cpu usage and uring_lock contention. | |
9943 | */ | |
9944 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
9945 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 9946 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 9947 | } else { |
c73ebb68 | 9948 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 9949 | } |
2b188cc1 JA |
9950 | } |
9951 | ||
7c504e65 | 9952 | out: |
6805b32e | 9953 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
9954 | out_fput: |
9955 | fdput(f); | |
9956 | return submitted ? submitted : ret; | |
9957 | } | |
9958 | ||
bebdb65e | 9959 | #ifdef CONFIG_PROC_FS |
c072481d | 9960 | static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id, |
61cf9370 | 9961 | const struct cred *cred) |
87ce955b | 9962 | { |
87ce955b JA |
9963 | struct user_namespace *uns = seq_user_ns(m); |
9964 | struct group_info *gi; | |
9965 | kernel_cap_t cap; | |
9966 | unsigned __capi; | |
9967 | int g; | |
9968 | ||
9969 | seq_printf(m, "%5d\n", id); | |
9970 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
9971 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
9972 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
9973 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
9974 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
9975 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
9976 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
9977 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
9978 | seq_puts(m, "\n\tGroups:\t"); | |
9979 | gi = cred->group_info; | |
9980 | for (g = 0; g < gi->ngroups; g++) { | |
9981 | seq_put_decimal_ull(m, g ? " " : "", | |
9982 | from_kgid_munged(uns, gi->gid[g])); | |
9983 | } | |
9984 | seq_puts(m, "\n\tCapEff:\t"); | |
9985 | cap = cred->cap_effective; | |
9986 | CAP_FOR_EACH_U32(__capi) | |
9987 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
9988 | seq_putc(m, '\n'); | |
9989 | return 0; | |
9990 | } | |
9991 | ||
c072481d PB |
9992 | static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, |
9993 | struct seq_file *m) | |
87ce955b | 9994 | { |
dbbe9c64 | 9995 | struct io_sq_data *sq = NULL; |
83f84356 HX |
9996 | struct io_overflow_cqe *ocqe; |
9997 | struct io_rings *r = ctx->rings; | |
9998 | unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1; | |
9999 | unsigned int cached_sq_head = ctx->cached_sq_head; | |
10000 | unsigned int cached_cq_tail = ctx->cached_cq_tail; | |
10001 | unsigned int sq_head = READ_ONCE(r->sq.head); | |
10002 | unsigned int sq_tail = READ_ONCE(r->sq.tail); | |
10003 | unsigned int cq_head = READ_ONCE(r->cq.head); | |
10004 | unsigned int cq_tail = READ_ONCE(r->cq.tail); | |
fad8e0de | 10005 | bool has_lock; |
83f84356 HX |
10006 | unsigned int i; |
10007 | ||
10008 | /* | |
10009 | * we may get imprecise sqe and cqe info if uring is actively running | |
10010 | * since we get cached_sq_head and cached_cq_tail without uring_lock | |
10011 | * and sq_tail and cq_head are changed by userspace. But it's ok since | |
10012 | * we usually use these info when it is stuck. | |
10013 | */ | |
10014 | seq_printf(m, "SqHead:\t%u\n", sq_head & sq_mask); | |
10015 | seq_printf(m, "SqTail:\t%u\n", sq_tail & sq_mask); | |
10016 | seq_printf(m, "CachedSqHead:\t%u\n", cached_sq_head & sq_mask); | |
10017 | seq_printf(m, "CqHead:\t%u\n", cq_head & cq_mask); | |
10018 | seq_printf(m, "CqTail:\t%u\n", cq_tail & cq_mask); | |
10019 | seq_printf(m, "CachedCqTail:\t%u\n", cached_cq_tail & cq_mask); | |
10020 | seq_printf(m, "SQEs:\t%u\n", sq_tail - cached_sq_head); | |
10021 | for (i = cached_sq_head; i < sq_tail; i++) { | |
10022 | unsigned int sq_idx = READ_ONCE(ctx->sq_array[i & sq_mask]); | |
10023 | ||
10024 | if (likely(sq_idx <= sq_mask)) { | |
10025 | struct io_uring_sqe *sqe = &ctx->sq_sqes[sq_idx]; | |
10026 | ||
10027 | seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n", | |
10028 | sq_idx, sqe->opcode, sqe->fd, sqe->flags, sqe->user_data); | |
10029 | } | |
10030 | } | |
10031 | seq_printf(m, "CQEs:\t%u\n", cached_cq_tail - cq_head); | |
10032 | for (i = cq_head; i < cached_cq_tail; i++) { | |
10033 | struct io_uring_cqe *cqe = &r->cqes[i & cq_mask]; | |
10034 | ||
10035 | seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n", | |
10036 | i & cq_mask, cqe->user_data, cqe->res, cqe->flags); | |
10037 | } | |
87ce955b | 10038 | |
fad8e0de JA |
10039 | /* |
10040 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
10041 | * since fdinfo case grabs it in the opposite direction of normal use | |
10042 | * cases. If we fail to get the lock, we just don't iterate any | |
10043 | * structures that could be going away outside the io_uring mutex. | |
10044 | */ | |
10045 | has_lock = mutex_trylock(&ctx->uring_lock); | |
10046 | ||
5f3f26f9 | 10047 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 10048 | sq = ctx->sq_data; |
5f3f26f9 JA |
10049 | if (!sq->thread) |
10050 | sq = NULL; | |
10051 | } | |
dbbe9c64 JQ |
10052 | |
10053 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
10054 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 10055 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 10056 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
7b29f92d | 10057 | struct file *f = io_file_from_index(ctx, i); |
87ce955b | 10058 | |
87ce955b JA |
10059 | if (f) |
10060 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
10061 | else | |
10062 | seq_printf(m, "%5u: <none>\n", i); | |
10063 | } | |
10064 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 10065 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
41edf1a5 | 10066 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
4751f53d | 10067 | unsigned int len = buf->ubuf_end - buf->ubuf; |
87ce955b | 10068 | |
4751f53d | 10069 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
87ce955b | 10070 | } |
61cf9370 MWO |
10071 | if (has_lock && !xa_empty(&ctx->personalities)) { |
10072 | unsigned long index; | |
10073 | const struct cred *cred; | |
10074 | ||
87ce955b | 10075 | seq_printf(m, "Personalities:\n"); |
61cf9370 MWO |
10076 | xa_for_each(&ctx->personalities, index, cred) |
10077 | io_uring_show_cred(m, index, cred); | |
87ce955b | 10078 | } |
83f84356 HX |
10079 | if (has_lock) |
10080 | mutex_unlock(&ctx->uring_lock); | |
10081 | ||
10082 | seq_puts(m, "PollList:\n"); | |
79ebeaee | 10083 | spin_lock(&ctx->completion_lock); |
d7718a9d JA |
10084 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
10085 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
10086 | struct io_kiocb *req; | |
10087 | ||
10088 | hlist_for_each_entry(req, list, hash_node) | |
10089 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
10090 | req->task->task_works != NULL); | |
10091 | } | |
83f84356 HX |
10092 | |
10093 | seq_puts(m, "CqOverflowList:\n"); | |
10094 | list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) { | |
10095 | struct io_uring_cqe *cqe = &ocqe->cqe; | |
10096 | ||
10097 | seq_printf(m, " user_data=%llu, res=%d, flags=%x\n", | |
10098 | cqe->user_data, cqe->res, cqe->flags); | |
10099 | ||
10100 | } | |
10101 | ||
79ebeaee | 10102 | spin_unlock(&ctx->completion_lock); |
87ce955b JA |
10103 | } |
10104 | ||
c072481d | 10105 | static __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
87ce955b JA |
10106 | { |
10107 | struct io_ring_ctx *ctx = f->private_data; | |
10108 | ||
10109 | if (percpu_ref_tryget(&ctx->refs)) { | |
10110 | __io_uring_show_fdinfo(ctx, m); | |
10111 | percpu_ref_put(&ctx->refs); | |
10112 | } | |
10113 | } | |
bebdb65e | 10114 | #endif |
87ce955b | 10115 | |
2b188cc1 JA |
10116 | static const struct file_operations io_uring_fops = { |
10117 | .release = io_uring_release, | |
10118 | .mmap = io_uring_mmap, | |
6c5c240e RP |
10119 | #ifndef CONFIG_MMU |
10120 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
10121 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
10122 | #endif | |
2b188cc1 | 10123 | .poll = io_uring_poll, |
bebdb65e | 10124 | #ifdef CONFIG_PROC_FS |
87ce955b | 10125 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 10126 | #endif |
2b188cc1 JA |
10127 | }; |
10128 | ||
c072481d PB |
10129 | static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
10130 | struct io_uring_params *p) | |
2b188cc1 | 10131 | { |
75b28aff HV |
10132 | struct io_rings *rings; |
10133 | size_t size, sq_array_offset; | |
2b188cc1 | 10134 | |
bd740481 JA |
10135 | /* make sure these are sane, as we already accounted them */ |
10136 | ctx->sq_entries = p->sq_entries; | |
10137 | ctx->cq_entries = p->cq_entries; | |
10138 | ||
75b28aff HV |
10139 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
10140 | if (size == SIZE_MAX) | |
10141 | return -EOVERFLOW; | |
10142 | ||
10143 | rings = io_mem_alloc(size); | |
10144 | if (!rings) | |
2b188cc1 JA |
10145 | return -ENOMEM; |
10146 | ||
75b28aff HV |
10147 | ctx->rings = rings; |
10148 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
10149 | rings->sq_ring_mask = p->sq_entries - 1; | |
10150 | rings->cq_ring_mask = p->cq_entries - 1; | |
10151 | rings->sq_ring_entries = p->sq_entries; | |
10152 | rings->cq_ring_entries = p->cq_entries; | |
2b188cc1 JA |
10153 | |
10154 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
10155 | if (size == SIZE_MAX) { |
10156 | io_mem_free(ctx->rings); | |
10157 | ctx->rings = NULL; | |
2b188cc1 | 10158 | return -EOVERFLOW; |
eb065d30 | 10159 | } |
2b188cc1 JA |
10160 | |
10161 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
10162 | if (!ctx->sq_sqes) { |
10163 | io_mem_free(ctx->rings); | |
10164 | ctx->rings = NULL; | |
2b188cc1 | 10165 | return -ENOMEM; |
eb065d30 | 10166 | } |
2b188cc1 | 10167 | |
2b188cc1 JA |
10168 | return 0; |
10169 | } | |
10170 | ||
9faadcc8 PB |
10171 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
10172 | { | |
10173 | int ret, fd; | |
10174 | ||
10175 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
10176 | if (fd < 0) | |
10177 | return fd; | |
10178 | ||
eef51daa | 10179 | ret = io_uring_add_tctx_node(ctx); |
9faadcc8 PB |
10180 | if (ret) { |
10181 | put_unused_fd(fd); | |
10182 | return ret; | |
10183 | } | |
10184 | fd_install(fd, file); | |
10185 | return fd; | |
10186 | } | |
10187 | ||
2b188cc1 JA |
10188 | /* |
10189 | * Allocate an anonymous fd, this is what constitutes the application | |
10190 | * visible backing of an io_uring instance. The application mmaps this | |
10191 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
10192 | * we have to tie this fd to a socket for file garbage collection purposes. | |
10193 | */ | |
9faadcc8 | 10194 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
10195 | { |
10196 | struct file *file; | |
9faadcc8 | 10197 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
10198 | int ret; |
10199 | ||
2b188cc1 JA |
10200 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
10201 | &ctx->ring_sock); | |
10202 | if (ret) | |
9faadcc8 | 10203 | return ERR_PTR(ret); |
2b188cc1 JA |
10204 | #endif |
10205 | ||
2b188cc1 JA |
10206 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
10207 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 10208 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
10209 | if (IS_ERR(file)) { |
10210 | sock_release(ctx->ring_sock); | |
10211 | ctx->ring_sock = NULL; | |
10212 | } else { | |
10213 | ctx->ring_sock->file = file; | |
0f212204 | 10214 | } |
2b188cc1 | 10215 | #endif |
9faadcc8 | 10216 | return file; |
2b188cc1 JA |
10217 | } |
10218 | ||
c072481d PB |
10219 | static __cold int io_uring_create(unsigned entries, struct io_uring_params *p, |
10220 | struct io_uring_params __user *params) | |
2b188cc1 | 10221 | { |
2b188cc1 | 10222 | struct io_ring_ctx *ctx; |
9faadcc8 | 10223 | struct file *file; |
2b188cc1 JA |
10224 | int ret; |
10225 | ||
8110c1a6 | 10226 | if (!entries) |
2b188cc1 | 10227 | return -EINVAL; |
8110c1a6 JA |
10228 | if (entries > IORING_MAX_ENTRIES) { |
10229 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
10230 | return -EINVAL; | |
10231 | entries = IORING_MAX_ENTRIES; | |
10232 | } | |
2b188cc1 JA |
10233 | |
10234 | /* | |
10235 | * Use twice as many entries for the CQ ring. It's possible for the | |
10236 | * application to drive a higher depth than the size of the SQ ring, | |
10237 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
10238 | * some flexibility in overcommitting a bit. If the application has |
10239 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
10240 | * of CQ ring entries manually. | |
2b188cc1 JA |
10241 | */ |
10242 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
10243 | if (p->flags & IORING_SETUP_CQSIZE) { |
10244 | /* | |
10245 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
10246 | * to a power-of-two, if it isn't already. We do NOT impose | |
10247 | * any cq vs sq ring sizing. | |
10248 | */ | |
eb2667b3 | 10249 | if (!p->cq_entries) |
33a107f0 | 10250 | return -EINVAL; |
8110c1a6 JA |
10251 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
10252 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
10253 | return -EINVAL; | |
10254 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
10255 | } | |
eb2667b3 JQ |
10256 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
10257 | if (p->cq_entries < p->sq_entries) | |
10258 | return -EINVAL; | |
33a107f0 JA |
10259 | } else { |
10260 | p->cq_entries = 2 * p->sq_entries; | |
10261 | } | |
2b188cc1 | 10262 | |
2b188cc1 | 10263 | ctx = io_ring_ctx_alloc(p); |
62e398be | 10264 | if (!ctx) |
2b188cc1 | 10265 | return -ENOMEM; |
2b188cc1 | 10266 | ctx->compat = in_compat_syscall(); |
62e398be JA |
10267 | if (!capable(CAP_IPC_LOCK)) |
10268 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
10269 | |
10270 | /* | |
10271 | * This is just grabbed for accounting purposes. When a process exits, | |
10272 | * the mm is exited and dropped before the files, hence we need to hang | |
10273 | * on to this mm purely for the purposes of being able to unaccount | |
10274 | * memory (locked/pinned vm). It's not used for anything else. | |
10275 | */ | |
6b7898eb | 10276 | mmgrab(current->mm); |
2aede0e4 | 10277 | ctx->mm_account = current->mm; |
6b7898eb | 10278 | |
2b188cc1 JA |
10279 | ret = io_allocate_scq_urings(ctx, p); |
10280 | if (ret) | |
10281 | goto err; | |
10282 | ||
7e84e1c7 | 10283 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
10284 | if (ret) |
10285 | goto err; | |
eae071c9 | 10286 | /* always set a rsrc node */ |
47b228ce PB |
10287 | ret = io_rsrc_node_switch_start(ctx); |
10288 | if (ret) | |
10289 | goto err; | |
eae071c9 | 10290 | io_rsrc_node_switch(ctx, NULL); |
2b188cc1 | 10291 | |
2b188cc1 | 10292 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
10293 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
10294 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
10295 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
10296 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
10297 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
10298 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
10299 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
10300 | |
10301 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
10302 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
10303 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
10304 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
10305 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
10306 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
10307 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 10308 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 10309 | |
7f13657d XW |
10310 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
10311 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 10312 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 10313 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9690557e PB |
10314 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
10315 | IORING_FEAT_RSRC_TAGS; | |
7f13657d XW |
10316 | |
10317 | if (copy_to_user(params, p, sizeof(*p))) { | |
10318 | ret = -EFAULT; | |
10319 | goto err; | |
10320 | } | |
d1719f70 | 10321 | |
9faadcc8 PB |
10322 | file = io_uring_get_file(ctx); |
10323 | if (IS_ERR(file)) { | |
10324 | ret = PTR_ERR(file); | |
10325 | goto err; | |
10326 | } | |
10327 | ||
044c1ab3 JA |
10328 | /* |
10329 | * Install ring fd as the very last thing, so we don't risk someone | |
10330 | * having closed it before we finish setup | |
10331 | */ | |
9faadcc8 PB |
10332 | ret = io_uring_install_fd(ctx, file); |
10333 | if (ret < 0) { | |
10334 | /* fput will clean it up */ | |
10335 | fput(file); | |
10336 | return ret; | |
10337 | } | |
044c1ab3 | 10338 | |
c826bd7a | 10339 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
10340 | return ret; |
10341 | err: | |
10342 | io_ring_ctx_wait_and_kill(ctx); | |
10343 | return ret; | |
10344 | } | |
10345 | ||
10346 | /* | |
10347 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
10348 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
10349 | * params structure passed in. | |
10350 | */ | |
10351 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
10352 | { | |
10353 | struct io_uring_params p; | |
2b188cc1 JA |
10354 | int i; |
10355 | ||
10356 | if (copy_from_user(&p, params, sizeof(p))) | |
10357 | return -EFAULT; | |
10358 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
10359 | if (p.resv[i]) | |
10360 | return -EINVAL; | |
10361 | } | |
10362 | ||
6c271ce2 | 10363 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 10364 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
10365 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
10366 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
10367 | return -EINVAL; |
10368 | ||
7f13657d | 10369 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
10370 | } |
10371 | ||
10372 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
10373 | struct io_uring_params __user *, params) | |
10374 | { | |
10375 | return io_uring_setup(entries, params); | |
10376 | } | |
10377 | ||
c072481d PB |
10378 | static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg, |
10379 | unsigned nr_args) | |
66f4af93 JA |
10380 | { |
10381 | struct io_uring_probe *p; | |
10382 | size_t size; | |
10383 | int i, ret; | |
10384 | ||
10385 | size = struct_size(p, ops, nr_args); | |
10386 | if (size == SIZE_MAX) | |
10387 | return -EOVERFLOW; | |
10388 | p = kzalloc(size, GFP_KERNEL); | |
10389 | if (!p) | |
10390 | return -ENOMEM; | |
10391 | ||
10392 | ret = -EFAULT; | |
10393 | if (copy_from_user(p, arg, size)) | |
10394 | goto out; | |
10395 | ret = -EINVAL; | |
10396 | if (memchr_inv(p, 0, size)) | |
10397 | goto out; | |
10398 | ||
10399 | p->last_op = IORING_OP_LAST - 1; | |
10400 | if (nr_args > IORING_OP_LAST) | |
10401 | nr_args = IORING_OP_LAST; | |
10402 | ||
10403 | for (i = 0; i < nr_args; i++) { | |
10404 | p->ops[i].op = i; | |
10405 | if (!io_op_defs[i].not_supported) | |
10406 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
10407 | } | |
10408 | p->ops_len = i; | |
10409 | ||
10410 | ret = 0; | |
10411 | if (copy_to_user(arg, p, size)) | |
10412 | ret = -EFAULT; | |
10413 | out: | |
10414 | kfree(p); | |
10415 | return ret; | |
10416 | } | |
10417 | ||
071698e1 JA |
10418 | static int io_register_personality(struct io_ring_ctx *ctx) |
10419 | { | |
4379bf8b | 10420 | const struct cred *creds; |
61cf9370 | 10421 | u32 id; |
1e6fa521 | 10422 | int ret; |
071698e1 | 10423 | |
4379bf8b | 10424 | creds = get_current_cred(); |
1e6fa521 | 10425 | |
61cf9370 MWO |
10426 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
10427 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); | |
a30f895a JA |
10428 | if (ret < 0) { |
10429 | put_cred(creds); | |
10430 | return ret; | |
10431 | } | |
10432 | return id; | |
071698e1 JA |
10433 | } |
10434 | ||
c072481d PB |
10435 | static __cold int io_register_restrictions(struct io_ring_ctx *ctx, |
10436 | void __user *arg, unsigned int nr_args) | |
21b55dbc SG |
10437 | { |
10438 | struct io_uring_restriction *res; | |
10439 | size_t size; | |
10440 | int i, ret; | |
10441 | ||
7e84e1c7 SG |
10442 | /* Restrictions allowed only if rings started disabled */ |
10443 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10444 | return -EBADFD; | |
10445 | ||
21b55dbc | 10446 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 10447 | if (ctx->restrictions.registered) |
21b55dbc SG |
10448 | return -EBUSY; |
10449 | ||
10450 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
10451 | return -EINVAL; | |
10452 | ||
10453 | size = array_size(nr_args, sizeof(*res)); | |
10454 | if (size == SIZE_MAX) | |
10455 | return -EOVERFLOW; | |
10456 | ||
10457 | res = memdup_user(arg, size); | |
10458 | if (IS_ERR(res)) | |
10459 | return PTR_ERR(res); | |
10460 | ||
10461 | ret = 0; | |
10462 | ||
10463 | for (i = 0; i < nr_args; i++) { | |
10464 | switch (res[i].opcode) { | |
10465 | case IORING_RESTRICTION_REGISTER_OP: | |
10466 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
10467 | ret = -EINVAL; | |
10468 | goto out; | |
10469 | } | |
10470 | ||
10471 | __set_bit(res[i].register_op, | |
10472 | ctx->restrictions.register_op); | |
10473 | break; | |
10474 | case IORING_RESTRICTION_SQE_OP: | |
10475 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
10476 | ret = -EINVAL; | |
10477 | goto out; | |
10478 | } | |
10479 | ||
10480 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
10481 | break; | |
10482 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
10483 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
10484 | break; | |
10485 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
10486 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
10487 | break; | |
10488 | default: | |
10489 | ret = -EINVAL; | |
10490 | goto out; | |
10491 | } | |
10492 | } | |
10493 | ||
10494 | out: | |
10495 | /* Reset all restrictions if an error happened */ | |
10496 | if (ret != 0) | |
10497 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
10498 | else | |
7e84e1c7 | 10499 | ctx->restrictions.registered = true; |
21b55dbc SG |
10500 | |
10501 | kfree(res); | |
10502 | return ret; | |
10503 | } | |
10504 | ||
7e84e1c7 SG |
10505 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
10506 | { | |
10507 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10508 | return -EBADFD; | |
10509 | ||
10510 | if (ctx->restrictions.registered) | |
10511 | ctx->restricted = 1; | |
10512 | ||
0298ef96 PB |
10513 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
10514 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) | |
10515 | wake_up(&ctx->sq_data->wait); | |
7e84e1c7 SG |
10516 | return 0; |
10517 | } | |
10518 | ||
fdecb662 | 10519 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 10520 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 PB |
10521 | unsigned nr_args) |
10522 | { | |
10523 | __u32 tmp; | |
10524 | int err; | |
10525 | ||
c3bdad02 PB |
10526 | if (up->resv) |
10527 | return -EINVAL; | |
98f0b3b4 PB |
10528 | if (check_add_overflow(up->offset, nr_args, &tmp)) |
10529 | return -EOVERFLOW; | |
10530 | err = io_rsrc_node_switch_start(ctx); | |
10531 | if (err) | |
10532 | return err; | |
10533 | ||
fdecb662 PB |
10534 | switch (type) { |
10535 | case IORING_RSRC_FILE: | |
98f0b3b4 | 10536 | return __io_sqe_files_update(ctx, up, nr_args); |
634d00df PB |
10537 | case IORING_RSRC_BUFFER: |
10538 | return __io_sqe_buffers_update(ctx, up, nr_args); | |
98f0b3b4 PB |
10539 | } |
10540 | return -EINVAL; | |
10541 | } | |
10542 | ||
c3bdad02 PB |
10543 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
10544 | unsigned nr_args) | |
98f0b3b4 | 10545 | { |
c3bdad02 | 10546 | struct io_uring_rsrc_update2 up; |
98f0b3b4 PB |
10547 | |
10548 | if (!nr_args) | |
10549 | return -EINVAL; | |
c3bdad02 PB |
10550 | memset(&up, 0, sizeof(up)); |
10551 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) | |
10552 | return -EFAULT; | |
10553 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); | |
10554 | } | |
10555 | ||
10556 | static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
992da01a | 10557 | unsigned size, unsigned type) |
c3bdad02 PB |
10558 | { |
10559 | struct io_uring_rsrc_update2 up; | |
10560 | ||
10561 | if (size != sizeof(up)) | |
10562 | return -EINVAL; | |
98f0b3b4 PB |
10563 | if (copy_from_user(&up, arg, sizeof(up))) |
10564 | return -EFAULT; | |
992da01a | 10565 | if (!up.nr || up.resv) |
98f0b3b4 | 10566 | return -EINVAL; |
992da01a | 10567 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
98f0b3b4 PB |
10568 | } |
10569 | ||
c072481d | 10570 | static __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
992da01a | 10571 | unsigned int size, unsigned int type) |
792e3582 PB |
10572 | { |
10573 | struct io_uring_rsrc_register rr; | |
10574 | ||
10575 | /* keep it extendible */ | |
10576 | if (size != sizeof(rr)) | |
10577 | return -EINVAL; | |
10578 | ||
10579 | memset(&rr, 0, sizeof(rr)); | |
10580 | if (copy_from_user(&rr, arg, size)) | |
10581 | return -EFAULT; | |
992da01a | 10582 | if (!rr.nr || rr.resv || rr.resv2) |
792e3582 PB |
10583 | return -EINVAL; |
10584 | ||
992da01a | 10585 | switch (type) { |
792e3582 PB |
10586 | case IORING_RSRC_FILE: |
10587 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), | |
10588 | rr.nr, u64_to_user_ptr(rr.tags)); | |
634d00df PB |
10589 | case IORING_RSRC_BUFFER: |
10590 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), | |
10591 | rr.nr, u64_to_user_ptr(rr.tags)); | |
792e3582 PB |
10592 | } |
10593 | return -EINVAL; | |
10594 | } | |
10595 | ||
c072481d PB |
10596 | static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx, |
10597 | void __user *arg, unsigned len) | |
fe76421d JA |
10598 | { |
10599 | struct io_uring_task *tctx = current->io_uring; | |
10600 | cpumask_var_t new_mask; | |
10601 | int ret; | |
10602 | ||
10603 | if (!tctx || !tctx->io_wq) | |
10604 | return -EINVAL; | |
10605 | ||
10606 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | |
10607 | return -ENOMEM; | |
10608 | ||
10609 | cpumask_clear(new_mask); | |
10610 | if (len > cpumask_size()) | |
10611 | len = cpumask_size(); | |
10612 | ||
10613 | if (copy_from_user(new_mask, arg, len)) { | |
10614 | free_cpumask_var(new_mask); | |
10615 | return -EFAULT; | |
10616 | } | |
10617 | ||
10618 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); | |
10619 | free_cpumask_var(new_mask); | |
10620 | return ret; | |
10621 | } | |
10622 | ||
c072481d | 10623 | static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx) |
fe76421d JA |
10624 | { |
10625 | struct io_uring_task *tctx = current->io_uring; | |
10626 | ||
10627 | if (!tctx || !tctx->io_wq) | |
10628 | return -EINVAL; | |
10629 | ||
10630 | return io_wq_cpu_affinity(tctx->io_wq, NULL); | |
10631 | } | |
10632 | ||
c072481d PB |
10633 | static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, |
10634 | void __user *arg) | |
2e480058 | 10635 | { |
fa84693b JA |
10636 | struct io_uring_task *tctx = NULL; |
10637 | struct io_sq_data *sqd = NULL; | |
2e480058 JA |
10638 | __u32 new_count[2]; |
10639 | int i, ret; | |
10640 | ||
2e480058 JA |
10641 | if (copy_from_user(new_count, arg, sizeof(new_count))) |
10642 | return -EFAULT; | |
10643 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
10644 | if (new_count[i] > INT_MAX) | |
10645 | return -EINVAL; | |
10646 | ||
fa84693b JA |
10647 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
10648 | sqd = ctx->sq_data; | |
10649 | if (sqd) { | |
009ad9f0 JA |
10650 | /* |
10651 | * Observe the correct sqd->lock -> ctx->uring_lock | |
10652 | * ordering. Fine to drop uring_lock here, we hold | |
10653 | * a ref to the ctx. | |
10654 | */ | |
41d3a6bd | 10655 | refcount_inc(&sqd->refs); |
009ad9f0 | 10656 | mutex_unlock(&ctx->uring_lock); |
fa84693b | 10657 | mutex_lock(&sqd->lock); |
009ad9f0 | 10658 | mutex_lock(&ctx->uring_lock); |
41d3a6bd JA |
10659 | if (sqd->thread) |
10660 | tctx = sqd->thread->io_uring; | |
fa84693b JA |
10661 | } |
10662 | } else { | |
10663 | tctx = current->io_uring; | |
10664 | } | |
10665 | ||
10666 | ret = -EINVAL; | |
10667 | if (!tctx || !tctx->io_wq) | |
10668 | goto err; | |
10669 | ||
2e480058 JA |
10670 | ret = io_wq_max_workers(tctx->io_wq, new_count); |
10671 | if (ret) | |
fa84693b JA |
10672 | goto err; |
10673 | ||
41d3a6bd | 10674 | if (sqd) { |
fa84693b | 10675 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
10676 | io_put_sq_data(sqd); |
10677 | } | |
2e480058 JA |
10678 | |
10679 | if (copy_to_user(arg, new_count, sizeof(new_count))) | |
10680 | return -EFAULT; | |
10681 | ||
10682 | return 0; | |
fa84693b | 10683 | err: |
41d3a6bd | 10684 | if (sqd) { |
fa84693b | 10685 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
10686 | io_put_sq_data(sqd); |
10687 | } | |
fa84693b | 10688 | return ret; |
2e480058 JA |
10689 | } |
10690 | ||
071698e1 JA |
10691 | static bool io_register_op_must_quiesce(int op) |
10692 | { | |
10693 | switch (op) { | |
bd54b6fe BM |
10694 | case IORING_REGISTER_BUFFERS: |
10695 | case IORING_UNREGISTER_BUFFERS: | |
f4f7d21c | 10696 | case IORING_REGISTER_FILES: |
071698e1 JA |
10697 | case IORING_UNREGISTER_FILES: |
10698 | case IORING_REGISTER_FILES_UPDATE: | |
10699 | case IORING_REGISTER_PROBE: | |
10700 | case IORING_REGISTER_PERSONALITY: | |
10701 | case IORING_UNREGISTER_PERSONALITY: | |
992da01a PB |
10702 | case IORING_REGISTER_FILES2: |
10703 | case IORING_REGISTER_FILES_UPDATE2: | |
10704 | case IORING_REGISTER_BUFFERS2: | |
10705 | case IORING_REGISTER_BUFFERS_UPDATE: | |
fe76421d JA |
10706 | case IORING_REGISTER_IOWQ_AFF: |
10707 | case IORING_UNREGISTER_IOWQ_AFF: | |
2e480058 | 10708 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
071698e1 JA |
10709 | return false; |
10710 | default: | |
10711 | return true; | |
10712 | } | |
10713 | } | |
10714 | ||
c072481d | 10715 | static __cold int io_ctx_quiesce(struct io_ring_ctx *ctx) |
e73c5c7c PB |
10716 | { |
10717 | long ret; | |
10718 | ||
10719 | percpu_ref_kill(&ctx->refs); | |
10720 | ||
10721 | /* | |
10722 | * Drop uring mutex before waiting for references to exit. If another | |
10723 | * thread is currently inside io_uring_enter() it might need to grab the | |
10724 | * uring_lock to make progress. If we hold it here across the drain | |
10725 | * wait, then we can deadlock. It's safe to drop the mutex here, since | |
10726 | * no new references will come in after we've killed the percpu ref. | |
10727 | */ | |
10728 | mutex_unlock(&ctx->uring_lock); | |
10729 | do { | |
37f0e767 PB |
10730 | ret = wait_for_completion_interruptible_timeout(&ctx->ref_comp, HZ); |
10731 | if (ret) { | |
10732 | ret = min(0L, ret); | |
e73c5c7c | 10733 | break; |
37f0e767 PB |
10734 | } |
10735 | ||
e73c5c7c | 10736 | ret = io_run_task_work_sig(); |
37f0e767 | 10737 | io_req_caches_free(ctx); |
e73c5c7c PB |
10738 | } while (ret >= 0); |
10739 | mutex_lock(&ctx->uring_lock); | |
10740 | ||
10741 | if (ret) | |
10742 | io_refs_resurrect(&ctx->refs, &ctx->ref_comp); | |
10743 | return ret; | |
10744 | } | |
10745 | ||
edafccee JA |
10746 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
10747 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
10748 | __releases(ctx->uring_lock) |
10749 | __acquires(ctx->uring_lock) | |
edafccee JA |
10750 | { |
10751 | int ret; | |
10752 | ||
35fa71a0 JA |
10753 | /* |
10754 | * We're inside the ring mutex, if the ref is already dying, then | |
10755 | * someone else killed the ctx or is already going through | |
10756 | * io_uring_register(). | |
10757 | */ | |
10758 | if (percpu_ref_is_dying(&ctx->refs)) | |
10759 | return -ENXIO; | |
10760 | ||
75c4021a PB |
10761 | if (ctx->restricted) { |
10762 | if (opcode >= IORING_REGISTER_LAST) | |
10763 | return -EINVAL; | |
10764 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); | |
10765 | if (!test_bit(opcode, ctx->restrictions.register_op)) | |
10766 | return -EACCES; | |
10767 | } | |
10768 | ||
071698e1 | 10769 | if (io_register_op_must_quiesce(opcode)) { |
e73c5c7c PB |
10770 | ret = io_ctx_quiesce(ctx); |
10771 | if (ret) | |
f70865db | 10772 | return ret; |
05f3fb3c | 10773 | } |
edafccee JA |
10774 | |
10775 | switch (opcode) { | |
10776 | case IORING_REGISTER_BUFFERS: | |
634d00df | 10777 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
edafccee JA |
10778 | break; |
10779 | case IORING_UNREGISTER_BUFFERS: | |
10780 | ret = -EINVAL; | |
10781 | if (arg || nr_args) | |
10782 | break; | |
0a96bbe4 | 10783 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 10784 | break; |
6b06314c | 10785 | case IORING_REGISTER_FILES: |
792e3582 | 10786 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
6b06314c JA |
10787 | break; |
10788 | case IORING_UNREGISTER_FILES: | |
10789 | ret = -EINVAL; | |
10790 | if (arg || nr_args) | |
10791 | break; | |
10792 | ret = io_sqe_files_unregister(ctx); | |
10793 | break; | |
c3a31e60 | 10794 | case IORING_REGISTER_FILES_UPDATE: |
c3bdad02 | 10795 | ret = io_register_files_update(ctx, arg, nr_args); |
c3a31e60 | 10796 | break; |
9b402849 | 10797 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 10798 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
10799 | ret = -EINVAL; |
10800 | if (nr_args != 1) | |
10801 | break; | |
10802 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
10803 | if (ret) |
10804 | break; | |
10805 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
10806 | ctx->eventfd_async = 1; | |
10807 | else | |
10808 | ctx->eventfd_async = 0; | |
9b402849 JA |
10809 | break; |
10810 | case IORING_UNREGISTER_EVENTFD: | |
10811 | ret = -EINVAL; | |
10812 | if (arg || nr_args) | |
10813 | break; | |
10814 | ret = io_eventfd_unregister(ctx); | |
10815 | break; | |
66f4af93 JA |
10816 | case IORING_REGISTER_PROBE: |
10817 | ret = -EINVAL; | |
10818 | if (!arg || nr_args > 256) | |
10819 | break; | |
10820 | ret = io_probe(ctx, arg, nr_args); | |
10821 | break; | |
071698e1 JA |
10822 | case IORING_REGISTER_PERSONALITY: |
10823 | ret = -EINVAL; | |
10824 | if (arg || nr_args) | |
10825 | break; | |
10826 | ret = io_register_personality(ctx); | |
10827 | break; | |
10828 | case IORING_UNREGISTER_PERSONALITY: | |
10829 | ret = -EINVAL; | |
10830 | if (arg) | |
10831 | break; | |
10832 | ret = io_unregister_personality(ctx, nr_args); | |
10833 | break; | |
7e84e1c7 SG |
10834 | case IORING_REGISTER_ENABLE_RINGS: |
10835 | ret = -EINVAL; | |
10836 | if (arg || nr_args) | |
10837 | break; | |
10838 | ret = io_register_enable_rings(ctx); | |
10839 | break; | |
21b55dbc SG |
10840 | case IORING_REGISTER_RESTRICTIONS: |
10841 | ret = io_register_restrictions(ctx, arg, nr_args); | |
10842 | break; | |
992da01a PB |
10843 | case IORING_REGISTER_FILES2: |
10844 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); | |
10845 | break; | |
10846 | case IORING_REGISTER_FILES_UPDATE2: | |
10847 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
10848 | IORING_RSRC_FILE); | |
10849 | break; | |
10850 | case IORING_REGISTER_BUFFERS2: | |
10851 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); | |
792e3582 | 10852 | break; |
992da01a PB |
10853 | case IORING_REGISTER_BUFFERS_UPDATE: |
10854 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
10855 | IORING_RSRC_BUFFER); | |
c3bdad02 | 10856 | break; |
fe76421d JA |
10857 | case IORING_REGISTER_IOWQ_AFF: |
10858 | ret = -EINVAL; | |
10859 | if (!arg || !nr_args) | |
10860 | break; | |
10861 | ret = io_register_iowq_aff(ctx, arg, nr_args); | |
10862 | break; | |
10863 | case IORING_UNREGISTER_IOWQ_AFF: | |
10864 | ret = -EINVAL; | |
10865 | if (arg || nr_args) | |
10866 | break; | |
10867 | ret = io_unregister_iowq_aff(ctx); | |
10868 | break; | |
2e480058 JA |
10869 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
10870 | ret = -EINVAL; | |
10871 | if (!arg || nr_args != 2) | |
10872 | break; | |
10873 | ret = io_register_iowq_max_workers(ctx, arg); | |
10874 | break; | |
edafccee JA |
10875 | default: |
10876 | ret = -EINVAL; | |
10877 | break; | |
10878 | } | |
10879 | ||
071698e1 | 10880 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 10881 | /* bring the ctx back to life */ |
05f3fb3c | 10882 | percpu_ref_reinit(&ctx->refs); |
0f158b4c | 10883 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 10884 | } |
edafccee JA |
10885 | return ret; |
10886 | } | |
10887 | ||
10888 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
10889 | void __user *, arg, unsigned int, nr_args) | |
10890 | { | |
10891 | struct io_ring_ctx *ctx; | |
10892 | long ret = -EBADF; | |
10893 | struct fd f; | |
10894 | ||
10895 | f = fdget(fd); | |
10896 | if (!f.file) | |
10897 | return -EBADF; | |
10898 | ||
10899 | ret = -EOPNOTSUPP; | |
10900 | if (f.file->f_op != &io_uring_fops) | |
10901 | goto out_fput; | |
10902 | ||
10903 | ctx = f.file->private_data; | |
10904 | ||
b6c23dd5 PB |
10905 | io_run_task_work(); |
10906 | ||
edafccee JA |
10907 | mutex_lock(&ctx->uring_lock); |
10908 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
10909 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
10910 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
10911 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
10912 | out_fput: |
10913 | fdput(f); | |
10914 | return ret; | |
10915 | } | |
10916 | ||
2b188cc1 JA |
10917 | static int __init io_uring_init(void) |
10918 | { | |
d7f62e82 SM |
10919 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
10920 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
10921 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
10922 | } while (0) | |
10923 | ||
10924 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
10925 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
10926 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
10927 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
10928 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
10929 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
10930 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
10931 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
10932 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
10933 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 10934 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
10935 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
10936 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
10937 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
10938 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
10939 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
10940 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
10941 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
10942 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
10943 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
10944 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
10945 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
10946 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
10947 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
10948 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
10949 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 10950 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
10951 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
10952 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
16340eab | 10953 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
d7f62e82 | 10954 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
7d67af2c | 10955 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
b9445598 | 10956 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
d7f62e82 | 10957 | |
b0d658ec PB |
10958 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
10959 | sizeof(struct io_uring_rsrc_update)); | |
10960 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > | |
10961 | sizeof(struct io_uring_rsrc_update2)); | |
90499ad0 PB |
10962 | |
10963 | /* ->buf_index is u16 */ | |
10964 | BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); | |
10965 | ||
b0d658ec PB |
10966 | /* should fit into one byte */ |
10967 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); | |
68fe256a PB |
10968 | BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8)); |
10969 | BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS); | |
b0d658ec | 10970 | |
d3656344 | 10971 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
32c2d33e | 10972 | BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int)); |
16340eab | 10973 | |
91f245d5 JA |
10974 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
10975 | SLAB_ACCOUNT); | |
2b188cc1 JA |
10976 | return 0; |
10977 | }; | |
10978 | __initcall(io_uring_init); |