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; |
311997b3 | 397 | struct wait_queue_head poll_wait; |
0499e582 PB |
398 | struct wait_queue_head cq_wait; |
399 | unsigned cq_extra; | |
400 | atomic_t cq_timeouts; | |
0499e582 | 401 | unsigned cq_last_tm_flush; |
206aefde | 402 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 403 | |
2b188cc1 JA |
404 | struct { |
405 | spinlock_t completion_lock; | |
e94f141b | 406 | |
89850fce JA |
407 | spinlock_t timeout_lock; |
408 | ||
def596e9 | 409 | /* |
540e32a0 | 410 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
411 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
412 | * For SQPOLL, only the single threaded io_sq_thread() will | |
413 | * manipulate the list, hence no extra locking is needed there. | |
414 | */ | |
5eef4e87 | 415 | struct io_wq_work_list iopoll_list; |
78076bb6 JA |
416 | struct hlist_head *cancel_hash; |
417 | unsigned cancel_hash_bits; | |
915b3dde | 418 | bool poll_multi_queue; |
2b188cc1 | 419 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 420 | |
21b55dbc | 421 | struct io_restriction restrictions; |
3c1a2ead | 422 | |
b13a8918 PB |
423 | /* slow path rsrc auxilary data, used by update/register */ |
424 | struct { | |
425 | struct io_rsrc_node *rsrc_backup_node; | |
426 | struct io_mapped_ubuf *dummy_ubuf; | |
427 | struct io_rsrc_data *file_data; | |
428 | struct io_rsrc_data *buf_data; | |
429 | ||
430 | struct delayed_work rsrc_put_work; | |
431 | struct llist_head rsrc_put_llist; | |
432 | struct list_head rsrc_ref_list; | |
433 | spinlock_t rsrc_ref_lock; | |
434 | }; | |
435 | ||
3c1a2ead | 436 | /* Keep this last, we don't need it for the fast path */ |
b986af7e PB |
437 | struct { |
438 | #if defined(CONFIG_UNIX) | |
439 | struct socket *ring_sock; | |
440 | #endif | |
441 | /* hashed buffered write serialization */ | |
442 | struct io_wq_hash *hash_map; | |
443 | ||
444 | /* Only used for accounting purposes */ | |
445 | struct user_struct *user; | |
446 | struct mm_struct *mm_account; | |
447 | ||
448 | /* ctx exit and cancelation */ | |
9011bf9a PB |
449 | struct llist_head fallback_llist; |
450 | struct delayed_work fallback_work; | |
b986af7e PB |
451 | struct work_struct exit_work; |
452 | struct list_head tctx_list; | |
453 | struct completion ref_comp; | |
454 | }; | |
2b188cc1 JA |
455 | }; |
456 | ||
53e043b2 SM |
457 | struct io_uring_task { |
458 | /* submission side */ | |
09899b19 | 459 | int cached_refs; |
53e043b2 SM |
460 | struct xarray xa; |
461 | struct wait_queue_head wait; | |
ee53fb2b SM |
462 | const struct io_ring_ctx *last; |
463 | struct io_wq *io_wq; | |
53e043b2 | 464 | struct percpu_counter inflight; |
b303fe2e | 465 | atomic_t inflight_tracked; |
53e043b2 | 466 | atomic_t in_idle; |
53e043b2 SM |
467 | |
468 | spinlock_t task_lock; | |
469 | struct io_wq_work_list task_list; | |
53e043b2 | 470 | struct callback_head task_work; |
6294f368 | 471 | bool task_running; |
53e043b2 SM |
472 | }; |
473 | ||
09bb8394 JA |
474 | /* |
475 | * First field must be the file pointer in all the | |
476 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
477 | */ | |
221c5eb2 JA |
478 | struct io_poll_iocb { |
479 | struct file *file; | |
018043be | 480 | struct wait_queue_head *head; |
221c5eb2 | 481 | __poll_t events; |
8c838788 | 482 | bool done; |
221c5eb2 | 483 | bool canceled; |
392edb45 | 484 | struct wait_queue_entry wait; |
221c5eb2 JA |
485 | }; |
486 | ||
9d805892 | 487 | struct io_poll_update { |
018043be | 488 | struct file *file; |
9d805892 PB |
489 | u64 old_user_data; |
490 | u64 new_user_data; | |
491 | __poll_t events; | |
b69de288 JA |
492 | bool update_events; |
493 | bool update_user_data; | |
018043be PB |
494 | }; |
495 | ||
b5dba59e JA |
496 | struct io_close { |
497 | struct file *file; | |
b5dba59e | 498 | int fd; |
7df778be | 499 | u32 file_slot; |
b5dba59e JA |
500 | }; |
501 | ||
ad8a48ac JA |
502 | struct io_timeout_data { |
503 | struct io_kiocb *req; | |
504 | struct hrtimer timer; | |
505 | struct timespec64 ts; | |
506 | enum hrtimer_mode mode; | |
50c1df2b | 507 | u32 flags; |
ad8a48ac JA |
508 | }; |
509 | ||
8ed8d3c3 JA |
510 | struct io_accept { |
511 | struct file *file; | |
512 | struct sockaddr __user *addr; | |
513 | int __user *addr_len; | |
514 | int flags; | |
aaa4db12 | 515 | u32 file_slot; |
09952e3e | 516 | unsigned long nofile; |
8ed8d3c3 JA |
517 | }; |
518 | ||
519 | struct io_sync { | |
520 | struct file *file; | |
521 | loff_t len; | |
522 | loff_t off; | |
523 | int flags; | |
d63d1b5e | 524 | int mode; |
8ed8d3c3 JA |
525 | }; |
526 | ||
fbf23849 JA |
527 | struct io_cancel { |
528 | struct file *file; | |
529 | u64 addr; | |
530 | }; | |
531 | ||
b29472ee JA |
532 | struct io_timeout { |
533 | struct file *file; | |
bfe68a22 PB |
534 | u32 off; |
535 | u32 target_seq; | |
135fcde8 | 536 | struct list_head list; |
90cd7e42 PB |
537 | /* head of the link, used by linked timeouts only */ |
538 | struct io_kiocb *head; | |
89b263f6 JA |
539 | /* for linked completions */ |
540 | struct io_kiocb *prev; | |
b29472ee JA |
541 | }; |
542 | ||
0bdf7a2d PB |
543 | struct io_timeout_rem { |
544 | struct file *file; | |
545 | u64 addr; | |
9c8e11b3 PB |
546 | |
547 | /* timeout update */ | |
548 | struct timespec64 ts; | |
549 | u32 flags; | |
f1042b6c | 550 | bool ltimeout; |
0bdf7a2d PB |
551 | }; |
552 | ||
9adbd45d JA |
553 | struct io_rw { |
554 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
555 | struct kiocb kiocb; | |
556 | u64 addr; | |
557 | u64 len; | |
558 | }; | |
559 | ||
3fbb51c1 JA |
560 | struct io_connect { |
561 | struct file *file; | |
562 | struct sockaddr __user *addr; | |
563 | int addr_len; | |
564 | }; | |
565 | ||
e47293fd JA |
566 | struct io_sr_msg { |
567 | struct file *file; | |
fddaface | 568 | union { |
4af3417a PB |
569 | struct compat_msghdr __user *umsg_compat; |
570 | struct user_msghdr __user *umsg; | |
571 | void __user *buf; | |
fddaface | 572 | }; |
e47293fd | 573 | int msg_flags; |
bcda7baa | 574 | int bgid; |
fddaface | 575 | size_t len; |
e47293fd JA |
576 | }; |
577 | ||
15b71abe JA |
578 | struct io_open { |
579 | struct file *file; | |
580 | int dfd; | |
b9445598 | 581 | u32 file_slot; |
15b71abe | 582 | struct filename *filename; |
c12cedf2 | 583 | struct open_how how; |
4022e7af | 584 | unsigned long nofile; |
15b71abe JA |
585 | }; |
586 | ||
269bbe5f | 587 | struct io_rsrc_update { |
05f3fb3c JA |
588 | struct file *file; |
589 | u64 arg; | |
590 | u32 nr_args; | |
591 | u32 offset; | |
592 | }; | |
593 | ||
4840e418 JA |
594 | struct io_fadvise { |
595 | struct file *file; | |
596 | u64 offset; | |
597 | u32 len; | |
598 | u32 advice; | |
599 | }; | |
600 | ||
c1ca757b JA |
601 | struct io_madvise { |
602 | struct file *file; | |
603 | u64 addr; | |
604 | u32 len; | |
605 | u32 advice; | |
606 | }; | |
607 | ||
3e4827b0 JA |
608 | struct io_epoll { |
609 | struct file *file; | |
610 | int epfd; | |
611 | int op; | |
612 | int fd; | |
613 | struct epoll_event event; | |
e47293fd JA |
614 | }; |
615 | ||
7d67af2c PB |
616 | struct io_splice { |
617 | struct file *file_out; | |
618 | struct file *file_in; | |
619 | loff_t off_out; | |
620 | loff_t off_in; | |
621 | u64 len; | |
622 | unsigned int flags; | |
623 | }; | |
624 | ||
ddf0322d JA |
625 | struct io_provide_buf { |
626 | struct file *file; | |
627 | __u64 addr; | |
38134ada | 628 | __u32 len; |
ddf0322d JA |
629 | __u32 bgid; |
630 | __u16 nbufs; | |
631 | __u16 bid; | |
632 | }; | |
633 | ||
1d9e1288 BM |
634 | struct io_statx { |
635 | struct file *file; | |
636 | int dfd; | |
637 | unsigned int mask; | |
638 | unsigned int flags; | |
e62753e4 | 639 | const char __user *filename; |
1d9e1288 BM |
640 | struct statx __user *buffer; |
641 | }; | |
642 | ||
36f4fa68 JA |
643 | struct io_shutdown { |
644 | struct file *file; | |
645 | int how; | |
646 | }; | |
647 | ||
80a261fd JA |
648 | struct io_rename { |
649 | struct file *file; | |
650 | int old_dfd; | |
651 | int new_dfd; | |
652 | struct filename *oldpath; | |
653 | struct filename *newpath; | |
654 | int flags; | |
655 | }; | |
656 | ||
14a1143b JA |
657 | struct io_unlink { |
658 | struct file *file; | |
659 | int dfd; | |
660 | int flags; | |
661 | struct filename *filename; | |
662 | }; | |
663 | ||
e34a02dc DK |
664 | struct io_mkdir { |
665 | struct file *file; | |
666 | int dfd; | |
667 | umode_t mode; | |
668 | struct filename *filename; | |
669 | }; | |
670 | ||
7a8721f8 DK |
671 | struct io_symlink { |
672 | struct file *file; | |
673 | int new_dfd; | |
674 | struct filename *oldpath; | |
675 | struct filename *newpath; | |
676 | }; | |
677 | ||
cf30da90 DK |
678 | struct io_hardlink { |
679 | struct file *file; | |
680 | int old_dfd; | |
681 | int new_dfd; | |
682 | struct filename *oldpath; | |
683 | struct filename *newpath; | |
684 | int flags; | |
685 | }; | |
686 | ||
3ca405eb PB |
687 | struct io_completion { |
688 | struct file *file; | |
8c3f9cd1 | 689 | u32 cflags; |
3ca405eb PB |
690 | }; |
691 | ||
f499a021 JA |
692 | struct io_async_connect { |
693 | struct sockaddr_storage address; | |
694 | }; | |
695 | ||
03b1230c JA |
696 | struct io_async_msghdr { |
697 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
698 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
699 | struct iovec *free_iov; | |
03b1230c JA |
700 | struct sockaddr __user *uaddr; |
701 | struct msghdr msg; | |
b537916c | 702 | struct sockaddr_storage addr; |
03b1230c JA |
703 | }; |
704 | ||
f67676d1 JA |
705 | struct io_async_rw { |
706 | struct iovec fast_iov[UIO_FASTIOV]; | |
ff6165b2 JA |
707 | const struct iovec *free_iovec; |
708 | struct iov_iter iter; | |
cd658695 | 709 | struct iov_iter_state iter_state; |
227c0c96 | 710 | size_t bytes_done; |
bcf5a063 | 711 | struct wait_page_queue wpq; |
f67676d1 JA |
712 | }; |
713 | ||
6b47ee6e PB |
714 | enum { |
715 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
716 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
717 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
718 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
719 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 720 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 721 | |
dddca226 | 722 | /* first byte is taken by user flags, shift it to not overlap */ |
93d2bcd2 | 723 | REQ_F_FAIL_BIT = 8, |
6b47ee6e PB |
724 | REQ_F_INFLIGHT_BIT, |
725 | REQ_F_CUR_POS_BIT, | |
726 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 727 | REQ_F_LINK_TIMEOUT_BIT, |
99bc4c38 | 728 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 729 | REQ_F_POLLED_BIT, |
bcda7baa | 730 | REQ_F_BUFFER_SELECTED_BIT, |
e342c807 | 731 | REQ_F_COMPLETE_INLINE_BIT, |
230d50d4 | 732 | REQ_F_REISSUE_BIT, |
b8e64b53 | 733 | REQ_F_CREDS_BIT, |
20e60a38 | 734 | REQ_F_REFCOUNT_BIT, |
4d13d1a4 | 735 | REQ_F_ARM_LTIMEOUT_BIT, |
7b29f92d | 736 | /* keep async read/write and isreg together and in order */ |
b191e2df PB |
737 | REQ_F_NOWAIT_READ_BIT, |
738 | REQ_F_NOWAIT_WRITE_BIT, | |
7b29f92d | 739 | REQ_F_ISREG_BIT, |
84557871 JA |
740 | |
741 | /* not a real bit, just to check we're not overflowing the space */ | |
742 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
743 | }; |
744 | ||
745 | enum { | |
746 | /* ctx owns file */ | |
747 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
748 | /* drain existing IO first */ | |
749 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
750 | /* linked sqes */ | |
751 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
752 | /* doesn't sever on completion < 0 */ | |
753 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
754 | /* IOSQE_ASYNC */ | |
755 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
756 | /* IOSQE_BUFFER_SELECT */ |
757 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 758 | |
6b47ee6e | 759 | /* fail rest of links */ |
93d2bcd2 | 760 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
b05a1bcd | 761 | /* on inflight list, should be cancelled and waited on exit reliably */ |
6b47ee6e PB |
762 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
763 | /* read/write uses file position */ | |
764 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
765 | /* must not punt to workers */ | |
766 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 767 | /* has or had linked timeout */ |
6b47ee6e | 768 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
99bc4c38 PB |
769 | /* needs cleanup */ |
770 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
771 | /* already went through poll handler */ |
772 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
773 | /* buffer already selected */ |
774 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
e342c807 PB |
775 | /* completion is deferred through io_comp_state */ |
776 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
230d50d4 JA |
777 | /* caller should reissue async */ |
778 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), | |
7b29f92d | 779 | /* supports async reads */ |
b191e2df | 780 | REQ_F_NOWAIT_READ = BIT(REQ_F_NOWAIT_READ_BIT), |
7b29f92d | 781 | /* supports async writes */ |
b191e2df | 782 | REQ_F_NOWAIT_WRITE = BIT(REQ_F_NOWAIT_WRITE_BIT), |
7b29f92d JA |
783 | /* regular file */ |
784 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
b8e64b53 PB |
785 | /* has creds assigned */ |
786 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), | |
20e60a38 PB |
787 | /* skip refcounting if not set */ |
788 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), | |
4d13d1a4 PB |
789 | /* there is a linked timeout that has to be armed */ |
790 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT), | |
d7718a9d JA |
791 | }; |
792 | ||
793 | struct async_poll { | |
794 | struct io_poll_iocb poll; | |
807abcb0 | 795 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
796 | }; |
797 | ||
f237c30a | 798 | typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked); |
5b0a6acc | 799 | |
7cbf1722 | 800 | struct io_task_work { |
5b0a6acc PB |
801 | union { |
802 | struct io_wq_work_node node; | |
803 | struct llist_node fallback_node; | |
804 | }; | |
805 | io_req_tw_func_t func; | |
7cbf1722 JA |
806 | }; |
807 | ||
992da01a PB |
808 | enum { |
809 | IORING_RSRC_FILE = 0, | |
810 | IORING_RSRC_BUFFER = 1, | |
811 | }; | |
812 | ||
09bb8394 JA |
813 | /* |
814 | * NOTE! Each of the iocb union members has the file pointer | |
815 | * as the first entry in their struct definition. So you can | |
816 | * access the file pointer through any of the sub-structs, | |
817 | * or directly as just 'ki_filp' in this struct. | |
818 | */ | |
2b188cc1 | 819 | struct io_kiocb { |
221c5eb2 | 820 | union { |
09bb8394 | 821 | struct file *file; |
9adbd45d | 822 | struct io_rw rw; |
221c5eb2 | 823 | struct io_poll_iocb poll; |
9d805892 | 824 | struct io_poll_update poll_update; |
8ed8d3c3 JA |
825 | struct io_accept accept; |
826 | struct io_sync sync; | |
fbf23849 | 827 | struct io_cancel cancel; |
b29472ee | 828 | struct io_timeout timeout; |
0bdf7a2d | 829 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 830 | struct io_connect connect; |
e47293fd | 831 | struct io_sr_msg sr_msg; |
15b71abe | 832 | struct io_open open; |
b5dba59e | 833 | struct io_close close; |
269bbe5f | 834 | struct io_rsrc_update rsrc_update; |
4840e418 | 835 | struct io_fadvise fadvise; |
c1ca757b | 836 | struct io_madvise madvise; |
3e4827b0 | 837 | struct io_epoll epoll; |
7d67af2c | 838 | struct io_splice splice; |
ddf0322d | 839 | struct io_provide_buf pbuf; |
1d9e1288 | 840 | struct io_statx statx; |
36f4fa68 | 841 | struct io_shutdown shutdown; |
80a261fd | 842 | struct io_rename rename; |
14a1143b | 843 | struct io_unlink unlink; |
e34a02dc | 844 | struct io_mkdir mkdir; |
7a8721f8 | 845 | struct io_symlink symlink; |
cf30da90 | 846 | struct io_hardlink hardlink; |
3ca405eb PB |
847 | /* use only after cleaning per-op data, see io_clean_op() */ |
848 | struct io_completion compl; | |
221c5eb2 | 849 | }; |
2b188cc1 | 850 | |
e8c2bc1f JA |
851 | /* opcode allocated if it needs to store data for async defer */ |
852 | void *async_data; | |
d625c6ee | 853 | u8 opcode; |
65a6543d XW |
854 | /* polled IO has completed */ |
855 | u8 iopoll_completed; | |
2b188cc1 | 856 | |
4f4eeba8 | 857 | u16 buf_index; |
9cf7c104 | 858 | u32 result; |
4f4eeba8 | 859 | |
010e8e6b PB |
860 | struct io_ring_ctx *ctx; |
861 | unsigned int flags; | |
abc54d63 | 862 | atomic_t refs; |
010e8e6b PB |
863 | struct task_struct *task; |
864 | u64 user_data; | |
d7718a9d | 865 | |
269bbe5f | 866 | struct percpu_ref *fixed_rsrc_refs; |
fcb323cc | 867 | |
7e3709d5 | 868 | /* used by request caches, completion batching and iopoll */ |
ef05d9eb | 869 | struct io_wq_work_node comp_list; |
7e3709d5 | 870 | struct io_kiocb *link; |
5b0a6acc | 871 | struct io_task_work io_task_work; |
010e8e6b PB |
872 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
873 | struct hlist_node hash_node; | |
7e3709d5 | 874 | /* internal polling, see IORING_FEAT_FAST_POLL */ |
010e8e6b | 875 | struct async_poll *apoll; |
eae071c9 PB |
876 | /* store used ubuf, so we can prevent reloading */ |
877 | struct io_mapped_ubuf *imu; | |
ef05d9eb | 878 | struct io_wq_work work; |
7e3709d5 | 879 | /* custom credentials, valid IFF REQ_F_CREDS is set */ |
ef05d9eb | 880 | const struct cred *creds; |
7e3709d5 | 881 | /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */ |
30d51dd4 | 882 | struct io_buffer *kbuf; |
2b188cc1 | 883 | }; |
05589553 | 884 | |
13bf43f5 PB |
885 | struct io_tctx_node { |
886 | struct list_head ctx_node; | |
887 | struct task_struct *task; | |
13bf43f5 PB |
888 | struct io_ring_ctx *ctx; |
889 | }; | |
890 | ||
27dc8338 PB |
891 | struct io_defer_entry { |
892 | struct list_head list; | |
893 | struct io_kiocb *req; | |
9cf7c104 | 894 | u32 seq; |
2b188cc1 JA |
895 | }; |
896 | ||
d3656344 | 897 | struct io_op_def { |
d3656344 JA |
898 | /* needs req->file assigned */ |
899 | unsigned needs_file : 1; | |
d3656344 JA |
900 | /* hash wq insertion if file is a regular file */ |
901 | unsigned hash_reg_file : 1; | |
902 | /* unbound wq insertion if file is a non-regular file */ | |
903 | unsigned unbound_nonreg_file : 1; | |
66f4af93 JA |
904 | /* opcode is not supported by this kernel */ |
905 | unsigned not_supported : 1; | |
8a72758c JA |
906 | /* set if opcode supports polled "wait" */ |
907 | unsigned pollin : 1; | |
908 | unsigned pollout : 1; | |
bcda7baa JA |
909 | /* op supports buffer selection */ |
910 | unsigned buffer_select : 1; | |
26f0505a PB |
911 | /* do prep async if is going to be punted */ |
912 | unsigned needs_async_setup : 1; | |
27926b68 JA |
913 | /* should block plug */ |
914 | unsigned plug : 1; | |
e8c2bc1f JA |
915 | /* size of async data needed, if any */ |
916 | unsigned short async_size; | |
d3656344 JA |
917 | }; |
918 | ||
0918682b | 919 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
920 | [IORING_OP_NOP] = {}, |
921 | [IORING_OP_READV] = { | |
d3656344 JA |
922 | .needs_file = 1, |
923 | .unbound_nonreg_file = 1, | |
8a72758c | 924 | .pollin = 1, |
4d954c25 | 925 | .buffer_select = 1, |
26f0505a | 926 | .needs_async_setup = 1, |
27926b68 | 927 | .plug = 1, |
e8c2bc1f | 928 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 929 | }, |
0463b6c5 | 930 | [IORING_OP_WRITEV] = { |
d3656344 JA |
931 | .needs_file = 1, |
932 | .hash_reg_file = 1, | |
933 | .unbound_nonreg_file = 1, | |
8a72758c | 934 | .pollout = 1, |
26f0505a | 935 | .needs_async_setup = 1, |
27926b68 | 936 | .plug = 1, |
e8c2bc1f | 937 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 938 | }, |
0463b6c5 | 939 | [IORING_OP_FSYNC] = { |
d3656344 JA |
940 | .needs_file = 1, |
941 | }, | |
0463b6c5 | 942 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
943 | .needs_file = 1, |
944 | .unbound_nonreg_file = 1, | |
8a72758c | 945 | .pollin = 1, |
27926b68 | 946 | .plug = 1, |
e8c2bc1f | 947 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 948 | }, |
0463b6c5 | 949 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
950 | .needs_file = 1, |
951 | .hash_reg_file = 1, | |
952 | .unbound_nonreg_file = 1, | |
8a72758c | 953 | .pollout = 1, |
27926b68 | 954 | .plug = 1, |
e8c2bc1f | 955 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 956 | }, |
0463b6c5 | 957 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
958 | .needs_file = 1, |
959 | .unbound_nonreg_file = 1, | |
960 | }, | |
0463b6c5 PB |
961 | [IORING_OP_POLL_REMOVE] = {}, |
962 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 JA |
963 | .needs_file = 1, |
964 | }, | |
0463b6c5 | 965 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
966 | .needs_file = 1, |
967 | .unbound_nonreg_file = 1, | |
8a72758c | 968 | .pollout = 1, |
26f0505a | 969 | .needs_async_setup = 1, |
e8c2bc1f | 970 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 971 | }, |
0463b6c5 | 972 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
973 | .needs_file = 1, |
974 | .unbound_nonreg_file = 1, | |
8a72758c | 975 | .pollin = 1, |
52de1fe1 | 976 | .buffer_select = 1, |
26f0505a | 977 | .needs_async_setup = 1, |
e8c2bc1f | 978 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 979 | }, |
0463b6c5 | 980 | [IORING_OP_TIMEOUT] = { |
e8c2bc1f | 981 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 982 | }, |
9c8e11b3 PB |
983 | [IORING_OP_TIMEOUT_REMOVE] = { |
984 | /* used by timeout updates' prep() */ | |
9c8e11b3 | 985 | }, |
0463b6c5 | 986 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
987 | .needs_file = 1, |
988 | .unbound_nonreg_file = 1, | |
8a72758c | 989 | .pollin = 1, |
d3656344 | 990 | }, |
0463b6c5 PB |
991 | [IORING_OP_ASYNC_CANCEL] = {}, |
992 | [IORING_OP_LINK_TIMEOUT] = { | |
e8c2bc1f | 993 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 994 | }, |
0463b6c5 | 995 | [IORING_OP_CONNECT] = { |
d3656344 JA |
996 | .needs_file = 1, |
997 | .unbound_nonreg_file = 1, | |
8a72758c | 998 | .pollout = 1, |
26f0505a | 999 | .needs_async_setup = 1, |
e8c2bc1f | 1000 | .async_size = sizeof(struct io_async_connect), |
d3656344 | 1001 | }, |
0463b6c5 | 1002 | [IORING_OP_FALLOCATE] = { |
d3656344 | 1003 | .needs_file = 1, |
d3656344 | 1004 | }, |
44526bed JA |
1005 | [IORING_OP_OPENAT] = {}, |
1006 | [IORING_OP_CLOSE] = {}, | |
1007 | [IORING_OP_FILES_UPDATE] = {}, | |
1008 | [IORING_OP_STATX] = {}, | |
0463b6c5 | 1009 | [IORING_OP_READ] = { |
3a6820f2 JA |
1010 | .needs_file = 1, |
1011 | .unbound_nonreg_file = 1, | |
8a72758c | 1012 | .pollin = 1, |
bcda7baa | 1013 | .buffer_select = 1, |
27926b68 | 1014 | .plug = 1, |
e8c2bc1f | 1015 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1016 | }, |
0463b6c5 | 1017 | [IORING_OP_WRITE] = { |
3a6820f2 | 1018 | .needs_file = 1, |
7b3188e7 | 1019 | .hash_reg_file = 1, |
3a6820f2 | 1020 | .unbound_nonreg_file = 1, |
8a72758c | 1021 | .pollout = 1, |
27926b68 | 1022 | .plug = 1, |
e8c2bc1f | 1023 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1024 | }, |
0463b6c5 | 1025 | [IORING_OP_FADVISE] = { |
4840e418 | 1026 | .needs_file = 1, |
c1ca757b | 1027 | }, |
44526bed | 1028 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 1029 | [IORING_OP_SEND] = { |
fddaface JA |
1030 | .needs_file = 1, |
1031 | .unbound_nonreg_file = 1, | |
8a72758c | 1032 | .pollout = 1, |
fddaface | 1033 | }, |
0463b6c5 | 1034 | [IORING_OP_RECV] = { |
fddaface JA |
1035 | .needs_file = 1, |
1036 | .unbound_nonreg_file = 1, | |
8a72758c | 1037 | .pollin = 1, |
bcda7baa | 1038 | .buffer_select = 1, |
fddaface | 1039 | }, |
0463b6c5 | 1040 | [IORING_OP_OPENAT2] = { |
cebdb986 | 1041 | }, |
3e4827b0 JA |
1042 | [IORING_OP_EPOLL_CTL] = { |
1043 | .unbound_nonreg_file = 1, | |
3e4827b0 | 1044 | }, |
7d67af2c PB |
1045 | [IORING_OP_SPLICE] = { |
1046 | .needs_file = 1, | |
1047 | .hash_reg_file = 1, | |
1048 | .unbound_nonreg_file = 1, | |
ddf0322d JA |
1049 | }, |
1050 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 1051 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
1052 | [IORING_OP_TEE] = { |
1053 | .needs_file = 1, | |
1054 | .hash_reg_file = 1, | |
1055 | .unbound_nonreg_file = 1, | |
1056 | }, | |
36f4fa68 JA |
1057 | [IORING_OP_SHUTDOWN] = { |
1058 | .needs_file = 1, | |
1059 | }, | |
44526bed JA |
1060 | [IORING_OP_RENAMEAT] = {}, |
1061 | [IORING_OP_UNLINKAT] = {}, | |
e34a02dc | 1062 | [IORING_OP_MKDIRAT] = {}, |
7a8721f8 | 1063 | [IORING_OP_SYMLINKAT] = {}, |
cf30da90 | 1064 | [IORING_OP_LINKAT] = {}, |
d3656344 JA |
1065 | }; |
1066 | ||
0756a869 PB |
1067 | /* requests with any of those set should undergo io_disarm_next() */ |
1068 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) | |
1069 | ||
7a612350 | 1070 | static bool io_disarm_next(struct io_kiocb *req); |
eef51daa | 1071 | static void io_uring_del_tctx_node(unsigned long index); |
9936c7c2 PB |
1072 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1073 | struct task_struct *task, | |
3dd0c97a | 1074 | bool cancel_all); |
78cc687b | 1075 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); |
1ffc5422 | 1076 | |
d4d19c19 PB |
1077 | static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
1078 | long res, unsigned int cflags); | |
ec9c02ad | 1079 | static void io_put_req(struct io_kiocb *req); |
91c2f697 | 1080 | static void io_put_req_deferred(struct io_kiocb *req); |
c7dae4ba | 1081 | static void io_dismantle_req(struct io_kiocb *req); |
94ae5e77 | 1082 | static void io_queue_linked_timeout(struct io_kiocb *req); |
fdecb662 | 1083 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 1084 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 | 1085 | unsigned nr_args); |
68fb8979 | 1086 | static void io_clean_op(struct io_kiocb *req); |
ac177053 | 1087 | static struct file *io_file_get(struct io_ring_ctx *ctx, |
8371adf5 | 1088 | struct io_kiocb *req, int fd, bool fixed); |
c5eef2b9 | 1089 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1090 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1091 | |
907d1df3 | 1092 | static void io_req_task_queue(struct io_kiocb *req); |
c450178d | 1093 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx); |
179ae0d1 | 1094 | static int io_req_prep_async(struct io_kiocb *req); |
de0617e4 | 1095 | |
b9445598 PB |
1096 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
1097 | unsigned int issue_flags, u32 slot_index); | |
7df778be PB |
1098 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags); |
1099 | ||
f1042b6c | 1100 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer); |
b9445598 | 1101 | |
2b188cc1 JA |
1102 | static struct kmem_cache *req_cachep; |
1103 | ||
0918682b | 1104 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1105 | |
1106 | struct sock *io_uring_get_socket(struct file *file) | |
1107 | { | |
1108 | #if defined(CONFIG_UNIX) | |
1109 | if (file->f_op == &io_uring_fops) { | |
1110 | struct io_ring_ctx *ctx = file->private_data; | |
1111 | ||
1112 | return ctx->ring_sock->sk; | |
1113 | } | |
1114 | #endif | |
1115 | return NULL; | |
1116 | } | |
1117 | EXPORT_SYMBOL(io_uring_get_socket); | |
1118 | ||
f237c30a PB |
1119 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
1120 | { | |
1121 | if (!*locked) { | |
1122 | mutex_lock(&ctx->uring_lock); | |
1123 | *locked = true; | |
1124 | } | |
1125 | } | |
1126 | ||
f2f87370 PB |
1127 | #define io_for_each_link(pos, head) \ |
1128 | for (pos = (head); pos; pos = pos->link) | |
1129 | ||
21c843d5 PB |
1130 | /* |
1131 | * Shamelessly stolen from the mm implementation of page reference checking, | |
1132 | * see commit f958d7b528b1 for details. | |
1133 | */ | |
1134 | #define req_ref_zero_or_close_to_overflow(req) \ | |
1135 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) | |
1136 | ||
1137 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) | |
1138 | { | |
20e60a38 | 1139 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1140 | return atomic_inc_not_zero(&req->refs); |
1141 | } | |
1142 | ||
21c843d5 PB |
1143 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
1144 | { | |
20e60a38 PB |
1145 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
1146 | return true; | |
1147 | ||
21c843d5 PB |
1148 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1149 | return atomic_dec_and_test(&req->refs); | |
1150 | } | |
1151 | ||
1152 | static inline void req_ref_put(struct io_kiocb *req) | |
1153 | { | |
20e60a38 | 1154 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1155 | WARN_ON_ONCE(req_ref_put_and_test(req)); |
1156 | } | |
1157 | ||
1158 | static inline void req_ref_get(struct io_kiocb *req) | |
1159 | { | |
20e60a38 | 1160 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1161 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1162 | atomic_inc(&req->refs); | |
1163 | } | |
1164 | ||
c450178d PB |
1165 | static inline void io_submit_flush_completions(struct io_ring_ctx *ctx) |
1166 | { | |
6f33b0bc | 1167 | if (!wq_list_empty(&ctx->submit_state.compl_reqs)) |
c450178d PB |
1168 | __io_submit_flush_completions(ctx); |
1169 | } | |
1170 | ||
48dcd38d | 1171 | static inline void __io_req_set_refcount(struct io_kiocb *req, int nr) |
20e60a38 PB |
1172 | { |
1173 | if (!(req->flags & REQ_F_REFCOUNT)) { | |
1174 | req->flags |= REQ_F_REFCOUNT; | |
48dcd38d | 1175 | atomic_set(&req->refs, nr); |
20e60a38 PB |
1176 | } |
1177 | } | |
1178 | ||
48dcd38d PB |
1179 | static inline void io_req_set_refcount(struct io_kiocb *req) |
1180 | { | |
1181 | __io_req_set_refcount(req, 1); | |
1182 | } | |
1183 | ||
b895c9a6 | 1184 | static inline void io_req_set_rsrc_node(struct io_kiocb *req) |
36f72fe2 PB |
1185 | { |
1186 | struct io_ring_ctx *ctx = req->ctx; | |
1187 | ||
269bbe5f | 1188 | if (!req->fixed_rsrc_refs) { |
a7f0ed5a | 1189 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
269bbe5f | 1190 | percpu_ref_get(req->fixed_rsrc_refs); |
36f72fe2 PB |
1191 | } |
1192 | } | |
1193 | ||
f70865db PB |
1194 | static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
1195 | { | |
1196 | bool got = percpu_ref_tryget(ref); | |
1197 | ||
1198 | /* already at zero, wait for ->release() */ | |
1199 | if (!got) | |
1200 | wait_for_completion(compl); | |
1201 | percpu_ref_resurrect(ref); | |
1202 | if (got) | |
1203 | percpu_ref_put(ref); | |
1204 | } | |
1205 | ||
3dd0c97a PB |
1206 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
1207 | bool cancel_all) | |
08d23634 PB |
1208 | { |
1209 | struct io_kiocb *req; | |
1210 | ||
68207680 | 1211 | if (task && head->task != task) |
08d23634 | 1212 | return false; |
3dd0c97a | 1213 | if (cancel_all) |
08d23634 PB |
1214 | return true; |
1215 | ||
1216 | io_for_each_link(req, head) { | |
b05a1bcd | 1217 | if (req->flags & REQ_F_INFLIGHT) |
02a13674 | 1218 | return true; |
08d23634 PB |
1219 | } |
1220 | return false; | |
1221 | } | |
1222 | ||
93d2bcd2 | 1223 | static inline void req_set_fail(struct io_kiocb *req) |
c40f6379 | 1224 | { |
93d2bcd2 | 1225 | req->flags |= REQ_F_FAIL; |
c40f6379 | 1226 | } |
4a38aed2 | 1227 | |
a8295b98 HX |
1228 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
1229 | { | |
1230 | req_set_fail(req); | |
1231 | req->result = res; | |
1232 | } | |
1233 | ||
2b188cc1 JA |
1234 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
1235 | { | |
1236 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1237 | ||
0f158b4c | 1238 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1239 | } |
1240 | ||
8eb7e2d0 PB |
1241 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1242 | { | |
1243 | return !req->timeout.off; | |
1244 | } | |
1245 | ||
f56165e6 PB |
1246 | static void io_fallback_req_func(struct work_struct *work) |
1247 | { | |
1248 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, | |
1249 | fallback_work.work); | |
1250 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); | |
1251 | struct io_kiocb *req, *tmp; | |
f237c30a | 1252 | bool locked = false; |
f56165e6 PB |
1253 | |
1254 | percpu_ref_get(&ctx->refs); | |
1255 | llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node) | |
f237c30a | 1256 | req->io_task_work.func(req, &locked); |
5636c00d | 1257 | |
f237c30a | 1258 | if (locked) { |
c450178d | 1259 | io_submit_flush_completions(ctx); |
f237c30a PB |
1260 | mutex_unlock(&ctx->uring_lock); |
1261 | } | |
f56165e6 | 1262 | percpu_ref_put(&ctx->refs); |
f237c30a | 1263 | |
f56165e6 PB |
1264 | } |
1265 | ||
2b188cc1 JA |
1266 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
1267 | { | |
1268 | struct io_ring_ctx *ctx; | |
78076bb6 | 1269 | int hash_bits; |
2b188cc1 JA |
1270 | |
1271 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1272 | if (!ctx) | |
1273 | return NULL; | |
1274 | ||
78076bb6 JA |
1275 | /* |
1276 | * Use 5 bits less than the max cq entries, that should give us around | |
1277 | * 32 entries per hash list if totally full and uniformly spread. | |
1278 | */ | |
1279 | hash_bits = ilog2(p->cq_entries); | |
1280 | hash_bits -= 5; | |
1281 | if (hash_bits <= 0) | |
1282 | hash_bits = 1; | |
1283 | ctx->cancel_hash_bits = hash_bits; | |
1284 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1285 | GFP_KERNEL); | |
1286 | if (!ctx->cancel_hash) | |
1287 | goto err; | |
1288 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1289 | ||
6224843d PB |
1290 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
1291 | if (!ctx->dummy_ubuf) | |
1292 | goto err; | |
1293 | /* set invalid range, so io_import_fixed() fails meeting it */ | |
1294 | ctx->dummy_ubuf->ubuf = -1UL; | |
1295 | ||
21482896 | 1296 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1297 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1298 | goto err; | |
2b188cc1 JA |
1299 | |
1300 | ctx->flags = p->flags; | |
90554200 | 1301 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1302 | INIT_LIST_HEAD(&ctx->sqd_list); |
311997b3 | 1303 | init_waitqueue_head(&ctx->poll_wait); |
1d7bb1d5 | 1304 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c | 1305 | init_completion(&ctx->ref_comp); |
9e15c3a0 | 1306 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
61cf9370 | 1307 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
2b188cc1 | 1308 | mutex_init(&ctx->uring_lock); |
311997b3 | 1309 | init_waitqueue_head(&ctx->cq_wait); |
2b188cc1 | 1310 | spin_lock_init(&ctx->completion_lock); |
89850fce | 1311 | spin_lock_init(&ctx->timeout_lock); |
5eef4e87 | 1312 | INIT_WQ_LIST(&ctx->iopoll_list); |
de0617e4 | 1313 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1314 | INIT_LIST_HEAD(&ctx->timeout_list); |
ef9dd637 | 1315 | INIT_LIST_HEAD(&ctx->ltimeout_list); |
d67d2263 BM |
1316 | spin_lock_init(&ctx->rsrc_ref_lock); |
1317 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1318 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1319 | init_llist_head(&ctx->rsrc_put_llist); | |
13bf43f5 | 1320 | INIT_LIST_HEAD(&ctx->tctx_list); |
c2b6c6bc PB |
1321 | ctx->submit_state.free_list.next = NULL; |
1322 | INIT_WQ_LIST(&ctx->locked_free_list); | |
9011bf9a | 1323 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
6f33b0bc | 1324 | INIT_WQ_LIST(&ctx->submit_state.compl_reqs); |
2b188cc1 | 1325 | return ctx; |
206aefde | 1326 | err: |
6224843d | 1327 | kfree(ctx->dummy_ubuf); |
78076bb6 | 1328 | kfree(ctx->cancel_hash); |
206aefde JA |
1329 | kfree(ctx); |
1330 | return NULL; | |
2b188cc1 JA |
1331 | } |
1332 | ||
8f6ed49a PB |
1333 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
1334 | { | |
1335 | struct io_rings *r = ctx->rings; | |
1336 | ||
1337 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); | |
1338 | ctx->cq_extra--; | |
1339 | } | |
1340 | ||
9cf7c104 | 1341 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1342 | { |
2bc9930e JA |
1343 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1344 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1345 | |
8f6ed49a | 1346 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
2bc9930e | 1347 | } |
de0617e4 | 1348 | |
9d858b21 | 1349 | return false; |
de0617e4 JA |
1350 | } |
1351 | ||
c97d8a0f PB |
1352 | #define FFS_ASYNC_READ 0x1UL |
1353 | #define FFS_ASYNC_WRITE 0x2UL | |
1354 | #ifdef CONFIG_64BIT | |
1355 | #define FFS_ISREG 0x4UL | |
1356 | #else | |
1357 | #define FFS_ISREG 0x0UL | |
1358 | #endif | |
1359 | #define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG) | |
1360 | ||
1361 | static inline bool io_req_ffs_set(struct io_kiocb *req) | |
1362 | { | |
1363 | return IS_ENABLED(CONFIG_64BIT) && (req->flags & REQ_F_FIXED_FILE); | |
1364 | } | |
1365 | ||
ce3d5aae PB |
1366 | static void io_req_track_inflight(struct io_kiocb *req) |
1367 | { | |
ce3d5aae | 1368 | if (!(req->flags & REQ_F_INFLIGHT)) { |
ce3d5aae | 1369 | req->flags |= REQ_F_INFLIGHT; |
b303fe2e | 1370 | atomic_inc(¤t->io_uring->inflight_tracked); |
ce3d5aae PB |
1371 | } |
1372 | } | |
1373 | ||
906c6caa PB |
1374 | static inline void io_unprep_linked_timeout(struct io_kiocb *req) |
1375 | { | |
1376 | req->flags &= ~REQ_F_LINK_TIMEOUT; | |
1377 | } | |
1378 | ||
fd08e530 PB |
1379 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
1380 | { | |
906c6caa PB |
1381 | if (WARN_ON_ONCE(!req->link)) |
1382 | return NULL; | |
1383 | ||
4d13d1a4 PB |
1384 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
1385 | req->flags |= REQ_F_LINK_TIMEOUT; | |
fd08e530 PB |
1386 | |
1387 | /* linked timeouts should have two refs once prep'ed */ | |
48dcd38d | 1388 | io_req_set_refcount(req); |
4d13d1a4 PB |
1389 | __io_req_set_refcount(req->link, 2); |
1390 | return req->link; | |
fd08e530 PB |
1391 | } |
1392 | ||
1393 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) | |
1394 | { | |
4d13d1a4 | 1395 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
fd08e530 PB |
1396 | return NULL; |
1397 | return __io_prep_linked_timeout(req); | |
1398 | } | |
1399 | ||
1e6fa521 JA |
1400 | static void io_prep_async_work(struct io_kiocb *req) |
1401 | { | |
1402 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1403 | struct io_ring_ctx *ctx = req->ctx; |
1404 | ||
b8e64b53 PB |
1405 | if (!(req->flags & REQ_F_CREDS)) { |
1406 | req->flags |= REQ_F_CREDS; | |
c10d1f98 | 1407 | req->creds = get_current_cred(); |
b8e64b53 | 1408 | } |
003e8dcc | 1409 | |
e1d675df PB |
1410 | req->work.list.next = NULL; |
1411 | req->work.flags = 0; | |
feaadc4f PB |
1412 | if (req->flags & REQ_F_FORCE_ASYNC) |
1413 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1414 | ||
1e6fa521 JA |
1415 | if (req->flags & REQ_F_ISREG) { |
1416 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1417 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
4b982bd0 | 1418 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
1e6fa521 JA |
1419 | if (def->unbound_nonreg_file) |
1420 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1421 | } | |
e1d675df PB |
1422 | |
1423 | switch (req->opcode) { | |
1424 | case IORING_OP_SPLICE: | |
1425 | case IORING_OP_TEE: | |
e1d675df PB |
1426 | if (!S_ISREG(file_inode(req->splice.file_in)->i_mode)) |
1427 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1428 | break; | |
1429 | } | |
561fb04a | 1430 | } |
cccf0ee8 | 1431 | |
cbdcb435 | 1432 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1433 | { |
cbdcb435 | 1434 | struct io_kiocb *cur; |
54a91f3b | 1435 | |
44eff40a PB |
1436 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
1437 | struct io_ring_ctx *ctx = req->ctx; | |
1438 | ||
79ebeaee | 1439 | spin_lock(&ctx->completion_lock); |
44eff40a PB |
1440 | io_for_each_link(cur, req) |
1441 | io_prep_async_work(cur); | |
79ebeaee | 1442 | spin_unlock(&ctx->completion_lock); |
44eff40a PB |
1443 | } else { |
1444 | io_for_each_link(cur, req) | |
1445 | io_prep_async_work(cur); | |
1446 | } | |
561fb04a JA |
1447 | } |
1448 | ||
fff4e40e PB |
1449 | static inline void io_req_add_compl_list(struct io_kiocb *req) |
1450 | { | |
1451 | struct io_submit_state *state = &req->ctx->submit_state; | |
1452 | ||
1453 | wq_list_add_tail(&req->comp_list, &state->compl_reqs); | |
1454 | } | |
1455 | ||
f237c30a | 1456 | static void io_queue_async_work(struct io_kiocb *req, bool *locked) |
561fb04a | 1457 | { |
a197f664 | 1458 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1459 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1460 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1461 | |
f237c30a PB |
1462 | /* must not take the lock, NULL it as a precaution */ |
1463 | locked = NULL; | |
1464 | ||
3bfe6106 JA |
1465 | BUG_ON(!tctx); |
1466 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1467 | |
cbdcb435 PB |
1468 | /* init ->work of the whole link before punting */ |
1469 | io_prep_async_link(req); | |
991468dc JA |
1470 | |
1471 | /* | |
1472 | * Not expected to happen, but if we do have a bug where this _can_ | |
1473 | * happen, catch it here and ensure the request is marked as | |
1474 | * canceled. That will make io-wq go through the usual work cancel | |
1475 | * procedure rather than attempt to run this request (or create a new | |
1476 | * worker for it). | |
1477 | */ | |
1478 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) | |
1479 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1480 | ||
d07f1e8a PB |
1481 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1482 | &req->work, req->flags); | |
ebf93667 | 1483 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a JA |
1484 | if (link) |
1485 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1486 | } |
1487 | ||
1ee4160c | 1488 | static void io_kill_timeout(struct io_kiocb *req, int status) |
8c855885 | 1489 | __must_hold(&req->ctx->completion_lock) |
89850fce | 1490 | __must_hold(&req->ctx->timeout_lock) |
5262f567 | 1491 | { |
e8c2bc1f | 1492 | struct io_timeout_data *io = req->async_data; |
5262f567 | 1493 | |
fd9c7bc5 | 1494 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
2ae2eb9d PB |
1495 | if (status) |
1496 | req_set_fail(req); | |
01cec8c1 PB |
1497 | atomic_set(&req->ctx->cq_timeouts, |
1498 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1499 | list_del_init(&req->timeout.list); |
d4d19c19 | 1500 | io_cqring_fill_event(req->ctx, req->user_data, status, 0); |
91c2f697 | 1501 | io_put_req_deferred(req); |
5262f567 JA |
1502 | } |
1503 | } | |
1504 | ||
441b8a78 | 1505 | static void io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1506 | { |
441b8a78 | 1507 | while (!list_empty(&ctx->defer_list)) { |
27dc8338 PB |
1508 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1509 | struct io_defer_entry, list); | |
de0617e4 | 1510 | |
9cf7c104 | 1511 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1512 | break; |
27dc8338 | 1513 | list_del_init(&de->list); |
907d1df3 | 1514 | io_req_task_queue(de->req); |
27dc8338 | 1515 | kfree(de); |
441b8a78 | 1516 | } |
04518945 PB |
1517 | } |
1518 | ||
360428f8 | 1519 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
89850fce | 1520 | __must_hold(&ctx->completion_lock) |
de0617e4 | 1521 | { |
441b8a78 | 1522 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
f010505b | 1523 | |
79ebeaee | 1524 | spin_lock_irq(&ctx->timeout_lock); |
f18ee4cf | 1525 | while (!list_empty(&ctx->timeout_list)) { |
f010505b | 1526 | u32 events_needed, events_got; |
360428f8 | 1527 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1528 | struct io_kiocb, timeout.list); |
de0617e4 | 1529 | |
8eb7e2d0 | 1530 | if (io_is_timeout_noseq(req)) |
360428f8 | 1531 | break; |
f010505b MDG |
1532 | |
1533 | /* | |
1534 | * Since seq can easily wrap around over time, subtract | |
1535 | * the last seq at which timeouts were flushed before comparing. | |
1536 | * Assuming not more than 2^31-1 events have happened since, | |
1537 | * these subtractions won't have wrapped, so we can check if | |
1538 | * target is in [last_seq, current_seq] by comparing the two. | |
1539 | */ | |
1540 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1541 | events_got = seq - ctx->cq_last_tm_flush; | |
1542 | if (events_got < events_needed) | |
360428f8 | 1543 | break; |
bfe68a22 | 1544 | |
135fcde8 | 1545 | list_del_init(&req->timeout.list); |
1ee4160c | 1546 | io_kill_timeout(req, 0); |
f18ee4cf | 1547 | } |
f010505b | 1548 | ctx->cq_last_tm_flush = seq; |
79ebeaee | 1549 | spin_unlock_irq(&ctx->timeout_lock); |
360428f8 | 1550 | } |
5262f567 | 1551 | |
2335f6f5 | 1552 | static void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
360428f8 | 1553 | { |
2335f6f5 PB |
1554 | if (ctx->off_timeout_used) |
1555 | io_flush_timeouts(ctx); | |
1556 | if (ctx->drain_active) | |
1557 | io_queue_deferred(ctx); | |
1558 | } | |
1559 | ||
1560 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) | |
1561 | { | |
1562 | if (unlikely(ctx->off_timeout_used || ctx->drain_active)) | |
1563 | __io_commit_cqring_flush(ctx); | |
ec30e04b PB |
1564 | /* order cqe stores with ring update */ |
1565 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 JA |
1566 | } |
1567 | ||
90554200 JA |
1568 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1569 | { | |
1570 | struct io_rings *r = ctx->rings; | |
1571 | ||
a566c556 | 1572 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries; |
90554200 JA |
1573 | } |
1574 | ||
888aae2e PB |
1575 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1576 | { | |
1577 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1578 | } | |
1579 | ||
d068b506 | 1580 | static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) |
2b188cc1 | 1581 | { |
75b28aff | 1582 | struct io_rings *rings = ctx->rings; |
ea5ab3b5 | 1583 | unsigned tail, mask = ctx->cq_entries - 1; |
2b188cc1 | 1584 | |
115e12e5 SB |
1585 | /* |
1586 | * writes to the cq entry need to come after reading head; the | |
1587 | * control dependency is enough as we're using WRITE_ONCE to | |
1588 | * fill the cq entry | |
1589 | */ | |
a566c556 | 1590 | if (__io_cqring_events(ctx) == ctx->cq_entries) |
2b188cc1 JA |
1591 | return NULL; |
1592 | ||
888aae2e | 1593 | tail = ctx->cached_cq_tail++; |
ea5ab3b5 | 1594 | return &rings->cqes[tail & mask]; |
2b188cc1 JA |
1595 | } |
1596 | ||
f2842ab5 JA |
1597 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1598 | { | |
44c769de | 1599 | if (likely(!ctx->cq_ev_fd)) |
f0b493e6 | 1600 | return false; |
7e55a19c SG |
1601 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1602 | return false; | |
44c769de | 1603 | return !ctx->eventfd_async || io_wq_current_is_worker(); |
f2842ab5 JA |
1604 | } |
1605 | ||
2c5d763c JA |
1606 | /* |
1607 | * This should only get called when at least one event has been posted. | |
1608 | * Some applications rely on the eventfd notification count only changing | |
1609 | * IFF a new CQE has been added to the CQ ring. There's no depedency on | |
1610 | * 1:1 relationship between how many times this function is called (and | |
1611 | * hence the eventfd count) and number of CQEs posted to the CQ ring. | |
1612 | */ | |
b41e9852 | 1613 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1614 | { |
5fd46178 JA |
1615 | /* |
1616 | * wake_up_all() may seem excessive, but io_wake_function() and | |
1617 | * io_should_wake() handle the termination of the loop and only | |
1618 | * wake as many waiters as we need to. | |
1619 | */ | |
1620 | if (wq_has_sleeper(&ctx->cq_wait)) | |
1621 | wake_up_all(&ctx->cq_wait); | |
534ca6d6 JA |
1622 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
1623 | wake_up(&ctx->sq_data->wait); | |
b41e9852 | 1624 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 | 1625 | eventfd_signal(ctx->cq_ev_fd, 1); |
3f008385 | 1626 | if (waitqueue_active(&ctx->poll_wait)) |
311997b3 | 1627 | wake_up_interruptible(&ctx->poll_wait); |
1d7bb1d5 JA |
1628 | } |
1629 | ||
80c18e4a PB |
1630 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1631 | { | |
c57a91fb PB |
1632 | /* see waitqueue_active() comment */ |
1633 | smp_mb(); | |
1634 | ||
80c18e4a | 1635 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
c57a91fb | 1636 | if (waitqueue_active(&ctx->cq_wait)) |
5fd46178 | 1637 | wake_up_all(&ctx->cq_wait); |
80c18e4a PB |
1638 | } |
1639 | if (io_should_trigger_evfd(ctx)) | |
1640 | eventfd_signal(ctx->cq_ev_fd, 1); | |
3f008385 | 1641 | if (waitqueue_active(&ctx->poll_wait)) |
311997b3 | 1642 | wake_up_interruptible(&ctx->poll_wait); |
80c18e4a PB |
1643 | } |
1644 | ||
c4a2ed72 | 1645 | /* Returns true if there are no backlogged entries after the flush */ |
6c2450ae | 1646 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
1d7bb1d5 | 1647 | { |
b18032bb | 1648 | bool all_flushed, posted; |
1d7bb1d5 | 1649 | |
a566c556 | 1650 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
e23de15f | 1651 | return false; |
1d7bb1d5 | 1652 | |
b18032bb | 1653 | posted = false; |
79ebeaee | 1654 | spin_lock(&ctx->completion_lock); |
6c2450ae | 1655 | while (!list_empty(&ctx->cq_overflow_list)) { |
d068b506 | 1656 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
6c2450ae | 1657 | struct io_overflow_cqe *ocqe; |
e6c8aa9a | 1658 | |
1d7bb1d5 JA |
1659 | if (!cqe && !force) |
1660 | break; | |
6c2450ae PB |
1661 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
1662 | struct io_overflow_cqe, list); | |
1663 | if (cqe) | |
1664 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); | |
1665 | else | |
8f6ed49a PB |
1666 | io_account_cq_overflow(ctx); |
1667 | ||
b18032bb | 1668 | posted = true; |
6c2450ae PB |
1669 | list_del(&ocqe->list); |
1670 | kfree(ocqe); | |
1d7bb1d5 JA |
1671 | } |
1672 | ||
09e88404 PB |
1673 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1674 | if (all_flushed) { | |
5ed7a37d | 1675 | clear_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1676 | WRITE_ONCE(ctx->rings->sq_flags, |
1677 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); | |
09e88404 | 1678 | } |
46930143 | 1679 | |
b18032bb JA |
1680 | if (posted) |
1681 | io_commit_cqring(ctx); | |
79ebeaee | 1682 | spin_unlock(&ctx->completion_lock); |
b18032bb JA |
1683 | if (posted) |
1684 | io_cqring_ev_posted(ctx); | |
09e88404 | 1685 | return all_flushed; |
1d7bb1d5 JA |
1686 | } |
1687 | ||
90f67366 | 1688 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
6c503150 | 1689 | { |
ca0a2651 JA |
1690 | bool ret = true; |
1691 | ||
5ed7a37d | 1692 | if (test_bit(0, &ctx->check_cq_overflow)) { |
6c503150 PB |
1693 | /* iopoll syncs against uring_lock, not completion_lock */ |
1694 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1695 | mutex_lock(&ctx->uring_lock); | |
90f67366 | 1696 | ret = __io_cqring_overflow_flush(ctx, false); |
6c503150 PB |
1697 | if (ctx->flags & IORING_SETUP_IOPOLL) |
1698 | mutex_unlock(&ctx->uring_lock); | |
1699 | } | |
ca0a2651 JA |
1700 | |
1701 | return ret; | |
6c503150 PB |
1702 | } |
1703 | ||
6a290a14 PB |
1704 | /* must to be called somewhat shortly after putting a request */ |
1705 | static inline void io_put_task(struct task_struct *task, int nr) | |
1706 | { | |
1707 | struct io_uring_task *tctx = task->io_uring; | |
1708 | ||
e98e49b2 PB |
1709 | if (likely(task == current)) { |
1710 | tctx->cached_refs += nr; | |
1711 | } else { | |
1712 | percpu_counter_sub(&tctx->inflight, nr); | |
1713 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1714 | wake_up(&tctx->wait); | |
1715 | put_task_struct_many(task, nr); | |
1716 | } | |
6a290a14 PB |
1717 | } |
1718 | ||
9a10867a PB |
1719 | static void io_task_refs_refill(struct io_uring_task *tctx) |
1720 | { | |
1721 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; | |
1722 | ||
1723 | percpu_counter_add(&tctx->inflight, refill); | |
1724 | refcount_add(refill, ¤t->usage); | |
1725 | tctx->cached_refs += refill; | |
1726 | } | |
1727 | ||
1728 | static inline void io_get_task_refs(int nr) | |
1729 | { | |
1730 | struct io_uring_task *tctx = current->io_uring; | |
1731 | ||
1732 | tctx->cached_refs -= nr; | |
1733 | if (unlikely(tctx->cached_refs < 0)) | |
1734 | io_task_refs_refill(tctx); | |
1735 | } | |
1736 | ||
d4d19c19 PB |
1737 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
1738 | long res, unsigned int cflags) | |
2b188cc1 | 1739 | { |
cce4b8b0 | 1740 | struct io_overflow_cqe *ocqe; |
2b188cc1 | 1741 | |
cce4b8b0 PB |
1742 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
1743 | if (!ocqe) { | |
1744 | /* | |
1745 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1746 | * or cannot allocate an overflow entry, then we need to drop it | |
1747 | * on the floor. | |
1748 | */ | |
8f6ed49a | 1749 | io_account_cq_overflow(ctx); |
cce4b8b0 | 1750 | return false; |
2b188cc1 | 1751 | } |
cce4b8b0 | 1752 | if (list_empty(&ctx->cq_overflow_list)) { |
5ed7a37d | 1753 | set_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1754 | WRITE_ONCE(ctx->rings->sq_flags, |
1755 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); | |
1756 | ||
cce4b8b0 | 1757 | } |
d4d19c19 | 1758 | ocqe->cqe.user_data = user_data; |
cce4b8b0 PB |
1759 | ocqe->cqe.res = res; |
1760 | ocqe->cqe.flags = cflags; | |
1761 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); | |
1762 | return true; | |
2b188cc1 JA |
1763 | } |
1764 | ||
d4d19c19 PB |
1765 | static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
1766 | long res, unsigned int cflags) | |
2b188cc1 JA |
1767 | { |
1768 | struct io_uring_cqe *cqe; | |
1769 | ||
d4d19c19 | 1770 | trace_io_uring_complete(ctx, user_data, res, cflags); |
51c3ff62 | 1771 | |
2b188cc1 JA |
1772 | /* |
1773 | * If we can't get a cq entry, userspace overflowed the | |
1774 | * submission (by quite a lot). Increment the overflow count in | |
1775 | * the ring. | |
1776 | */ | |
d068b506 | 1777 | cqe = io_get_cqe(ctx); |
1d7bb1d5 | 1778 | if (likely(cqe)) { |
d4d19c19 | 1779 | WRITE_ONCE(cqe->user_data, user_data); |
2b188cc1 | 1780 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1781 | WRITE_ONCE(cqe->flags, cflags); |
8d13326e | 1782 | return true; |
2b188cc1 | 1783 | } |
d4d19c19 | 1784 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
2b188cc1 JA |
1785 | } |
1786 | ||
8d13326e | 1787 | /* not as hot to bloat with inlining */ |
d4d19c19 PB |
1788 | static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
1789 | long res, unsigned int cflags) | |
bcda7baa | 1790 | { |
d4d19c19 | 1791 | return __io_cqring_fill_event(ctx, user_data, res, cflags); |
bcda7baa JA |
1792 | } |
1793 | ||
7a612350 PB |
1794 | static void io_req_complete_post(struct io_kiocb *req, long res, |
1795 | unsigned int cflags) | |
2b188cc1 | 1796 | { |
78e19bbe | 1797 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 | 1798 | |
79ebeaee | 1799 | spin_lock(&ctx->completion_lock); |
d4d19c19 | 1800 | __io_cqring_fill_event(ctx, req->user_data, res, cflags); |
c7dae4ba JA |
1801 | /* |
1802 | * If we're the last reference to this request, add to our locked | |
1803 | * free_list cache. | |
1804 | */ | |
de9b4cca | 1805 | if (req_ref_put_and_test(req)) { |
7a612350 | 1806 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
0756a869 | 1807 | if (req->flags & IO_DISARM_MASK) |
7a612350 PB |
1808 | io_disarm_next(req); |
1809 | if (req->link) { | |
1810 | io_req_task_queue(req->link); | |
1811 | req->link = NULL; | |
1812 | } | |
1813 | } | |
c7dae4ba JA |
1814 | io_dismantle_req(req); |
1815 | io_put_task(req->task, 1); | |
c2b6c6bc | 1816 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
d0acdee2 | 1817 | ctx->locked_free_nr++; |
a3f34907 | 1818 | percpu_ref_put(&ctx->refs); |
180f829f | 1819 | } |
7a612350 | 1820 | io_commit_cqring(ctx); |
79ebeaee | 1821 | spin_unlock(&ctx->completion_lock); |
a3f34907 | 1822 | io_cqring_ev_posted(ctx); |
229a7b63 JA |
1823 | } |
1824 | ||
4e3d9ff9 JA |
1825 | static inline bool io_req_needs_clean(struct io_kiocb *req) |
1826 | { | |
c854357b | 1827 | return req->flags & IO_REQ_CLEAN_FLAGS; |
4e3d9ff9 JA |
1828 | } |
1829 | ||
fff4e40e PB |
1830 | static inline void io_req_complete_state(struct io_kiocb *req, long res, |
1831 | unsigned int cflags) | |
229a7b63 | 1832 | { |
a1cdbb4c | 1833 | /* clean per-opcode space, because req->compl is aliased with it */ |
4e3d9ff9 | 1834 | if (io_req_needs_clean(req)) |
68fb8979 | 1835 | io_clean_op(req); |
a38d68db PB |
1836 | req->result = res; |
1837 | req->compl.cflags = cflags; | |
e342c807 | 1838 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
1839 | } |
1840 | ||
889fca73 PB |
1841 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
1842 | long res, unsigned cflags) | |
bcda7baa | 1843 | { |
889fca73 PB |
1844 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
1845 | io_req_complete_state(req, res, cflags); | |
a38d68db | 1846 | else |
c7dae4ba | 1847 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
1848 | } |
1849 | ||
a38d68db | 1850 | static inline void io_req_complete(struct io_kiocb *req, long res) |
0ddf92e8 | 1851 | { |
889fca73 | 1852 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
1853 | } |
1854 | ||
f41db273 PB |
1855 | static void io_req_complete_failed(struct io_kiocb *req, long res) |
1856 | { | |
93d2bcd2 | 1857 | req_set_fail(req); |
f41db273 PB |
1858 | io_req_complete_post(req, res, 0); |
1859 | } | |
1860 | ||
c6d3d9cb PB |
1861 | static void io_req_complete_fail_submit(struct io_kiocb *req) |
1862 | { | |
1863 | /* | |
1864 | * We don't submit, fail them all, for that replace hardlinks with | |
1865 | * normal links. Extra REQ_F_LINK is tolerated. | |
1866 | */ | |
1867 | req->flags &= ~REQ_F_HARDLINK; | |
1868 | req->flags |= REQ_F_LINK; | |
1869 | io_req_complete_failed(req, req->result); | |
1870 | } | |
1871 | ||
864ea921 PB |
1872 | /* |
1873 | * Don't initialise the fields below on every allocation, but do that in | |
1874 | * advance and keep them valid across allocations. | |
1875 | */ | |
1876 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
1877 | { | |
1878 | req->ctx = ctx; | |
1879 | req->link = NULL; | |
1880 | req->async_data = NULL; | |
1881 | /* not necessary, but safer to zero */ | |
1882 | req->result = 0; | |
1883 | } | |
1884 | ||
dac7a098 | 1885 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
cd0ca2e0 | 1886 | struct io_submit_state *state) |
dac7a098 | 1887 | { |
79ebeaee | 1888 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 1889 | wq_list_splice(&ctx->locked_free_list, &state->free_list); |
d0acdee2 | 1890 | ctx->locked_free_nr = 0; |
79ebeaee | 1891 | spin_unlock(&ctx->completion_lock); |
dac7a098 PB |
1892 | } |
1893 | ||
dd78f492 | 1894 | /* Returns true IFF there are requests in the cache */ |
c7dae4ba | 1895 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 1896 | { |
c7dae4ba | 1897 | struct io_submit_state *state = &ctx->submit_state; |
0ddf92e8 | 1898 | |
c7dae4ba JA |
1899 | /* |
1900 | * If we have more than a batch's worth of requests in our IRQ side | |
1901 | * locked cache, grab the lock and move them over to our submission | |
1902 | * side cache. | |
1903 | */ | |
d0acdee2 | 1904 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
cd0ca2e0 | 1905 | io_flush_cached_locked_reqs(ctx, state); |
c2b6c6bc | 1906 | return !!state->free_list.next; |
0ddf92e8 JA |
1907 | } |
1908 | ||
5d5901a3 PB |
1909 | /* |
1910 | * A request might get retired back into the request caches even before opcode | |
1911 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. | |
1912 | * Because of that, io_alloc_req() should be called only under ->uring_lock | |
1913 | * and with extra caution to not get a request that is still worked on. | |
1914 | */ | |
e5d1bc0a | 1915 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
5d5901a3 | 1916 | __must_hold(&ctx->uring_lock) |
2b188cc1 | 1917 | { |
e5d1bc0a | 1918 | struct io_submit_state *state = &ctx->submit_state; |
864ea921 | 1919 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
3ab665b7 | 1920 | void *reqs[IO_REQ_ALLOC_BATCH]; |
c2b6c6bc | 1921 | struct io_wq_work_node *node; |
3ab665b7 | 1922 | struct io_kiocb *req; |
864ea921 | 1923 | int ret, i; |
e5d1bc0a | 1924 | |
c2b6c6bc | 1925 | if (likely(state->free_list.next || io_flush_cached_reqs(ctx))) |
864ea921 | 1926 | goto got_req; |
e5d1bc0a | 1927 | |
3ab665b7 | 1928 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs); |
fd6fab2c | 1929 | |
864ea921 PB |
1930 | /* |
1931 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1932 | * retry single alloc to be on the safe side. | |
1933 | */ | |
1934 | if (unlikely(ret <= 0)) { | |
3ab665b7 PB |
1935 | reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
1936 | if (!reqs[0]) | |
864ea921 PB |
1937 | return NULL; |
1938 | ret = 1; | |
2b188cc1 | 1939 | } |
864ea921 | 1940 | |
3ab665b7 PB |
1941 | for (i = 0; i < ret; i++) { |
1942 | req = reqs[i]; | |
1943 | ||
1944 | io_preinit_req(req, ctx); | |
c2b6c6bc | 1945 | wq_stack_add_head(&req->comp_list, &state->free_list); |
3ab665b7 | 1946 | } |
e5d1bc0a | 1947 | got_req: |
c2b6c6bc PB |
1948 | node = wq_stack_extract(&state->free_list); |
1949 | return container_of(node, struct io_kiocb, comp_list); | |
2b188cc1 JA |
1950 | } |
1951 | ||
e1d767f0 | 1952 | static inline void io_put_file(struct file *file) |
8da11c19 | 1953 | { |
e1d767f0 | 1954 | if (file) |
8da11c19 PB |
1955 | fput(file); |
1956 | } | |
1957 | ||
6b639522 | 1958 | static inline void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 1959 | { |
094bae49 | 1960 | unsigned int flags = req->flags; |
929a3af9 | 1961 | |
51d48dab | 1962 | if (unlikely(io_req_needs_clean(req))) |
3a0a6902 | 1963 | io_clean_op(req); |
e1d767f0 PB |
1964 | if (!(flags & REQ_F_FIXED_FILE)) |
1965 | io_put_file(req->file); | |
269bbe5f BM |
1966 | if (req->fixed_rsrc_refs) |
1967 | percpu_ref_put(req->fixed_rsrc_refs); | |
99ebe4ef | 1968 | if (req->async_data) { |
094bae49 | 1969 | kfree(req->async_data); |
99ebe4ef PB |
1970 | req->async_data = NULL; |
1971 | } | |
e65ef56d JA |
1972 | } |
1973 | ||
216578e5 | 1974 | static void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 1975 | { |
51a4cc11 | 1976 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 1977 | |
216578e5 | 1978 | io_dismantle_req(req); |
7c660731 | 1979 | io_put_task(req->task, 1); |
c6ca97b3 | 1980 | |
79ebeaee | 1981 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 1982 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
c34b025f | 1983 | ctx->locked_free_nr++; |
79ebeaee | 1984 | spin_unlock(&ctx->completion_lock); |
c34b025f | 1985 | |
ecfc5177 | 1986 | percpu_ref_put(&ctx->refs); |
e65ef56d JA |
1987 | } |
1988 | ||
f2f87370 PB |
1989 | static inline void io_remove_next_linked(struct io_kiocb *req) |
1990 | { | |
1991 | struct io_kiocb *nxt = req->link; | |
1992 | ||
1993 | req->link = nxt->link; | |
1994 | nxt->link = NULL; | |
1995 | } | |
1996 | ||
33cc89a9 PB |
1997 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
1998 | __must_hold(&req->ctx->completion_lock) | |
89b263f6 | 1999 | __must_hold(&req->ctx->timeout_lock) |
2665abfd | 2000 | { |
33cc89a9 | 2001 | struct io_kiocb *link = req->link; |
f2f87370 | 2002 | |
b97e736a | 2003 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
c9abd7ad | 2004 | struct io_timeout_data *io = link->async_data; |
7c86ffee | 2005 | |
f2f87370 | 2006 | io_remove_next_linked(req); |
90cd7e42 | 2007 | link->timeout.head = NULL; |
fd9c7bc5 | 2008 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
ef9dd637 | 2009 | list_del(&link->timeout.list); |
d4d19c19 PB |
2010 | io_cqring_fill_event(link->ctx, link->user_data, |
2011 | -ECANCELED, 0); | |
91c2f697 | 2012 | io_put_req_deferred(link); |
d4729fbd | 2013 | return true; |
c9abd7ad PB |
2014 | } |
2015 | } | |
d4729fbd | 2016 | return false; |
7c86ffee PB |
2017 | } |
2018 | ||
d148ca4b | 2019 | static void io_fail_links(struct io_kiocb *req) |
33cc89a9 | 2020 | __must_hold(&req->ctx->completion_lock) |
9e645e11 | 2021 | { |
33cc89a9 | 2022 | struct io_kiocb *nxt, *link = req->link; |
9e645e11 | 2023 | |
f2f87370 | 2024 | req->link = NULL; |
f2f87370 | 2025 | while (link) { |
a8295b98 HX |
2026 | long res = -ECANCELED; |
2027 | ||
2028 | if (link->flags & REQ_F_FAIL) | |
2029 | res = link->result; | |
2030 | ||
f2f87370 PB |
2031 | nxt = link->link; |
2032 | link->link = NULL; | |
2665abfd | 2033 | |
f2f87370 | 2034 | trace_io_uring_fail_link(req, link); |
a8295b98 | 2035 | io_cqring_fill_event(link->ctx, link->user_data, res, 0); |
91c2f697 | 2036 | io_put_req_deferred(link); |
f2f87370 | 2037 | link = nxt; |
9e645e11 | 2038 | } |
33cc89a9 | 2039 | } |
9e645e11 | 2040 | |
33cc89a9 PB |
2041 | static bool io_disarm_next(struct io_kiocb *req) |
2042 | __must_hold(&req->ctx->completion_lock) | |
2043 | { | |
2044 | bool posted = false; | |
2045 | ||
0756a869 PB |
2046 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
2047 | struct io_kiocb *link = req->link; | |
2048 | ||
906c6caa | 2049 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
0756a869 PB |
2050 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
2051 | io_remove_next_linked(req); | |
2052 | io_cqring_fill_event(link->ctx, link->user_data, | |
2053 | -ECANCELED, 0); | |
2054 | io_put_req_deferred(link); | |
2055 | posted = true; | |
2056 | } | |
2057 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { | |
89b263f6 JA |
2058 | struct io_ring_ctx *ctx = req->ctx; |
2059 | ||
2060 | spin_lock_irq(&ctx->timeout_lock); | |
33cc89a9 | 2061 | posted = io_kill_linked_timeout(req); |
89b263f6 JA |
2062 | spin_unlock_irq(&ctx->timeout_lock); |
2063 | } | |
93d2bcd2 | 2064 | if (unlikely((req->flags & REQ_F_FAIL) && |
e4335ed3 | 2065 | !(req->flags & REQ_F_HARDLINK))) { |
33cc89a9 PB |
2066 | posted |= (req->link != NULL); |
2067 | io_fail_links(req); | |
2068 | } | |
2069 | return posted; | |
9e645e11 JA |
2070 | } |
2071 | ||
d81499bf PB |
2072 | static void __io_req_find_next_prep(struct io_kiocb *req) |
2073 | { | |
2074 | struct io_ring_ctx *ctx = req->ctx; | |
2075 | bool posted; | |
2076 | ||
2077 | spin_lock(&ctx->completion_lock); | |
2078 | posted = io_disarm_next(req); | |
2079 | if (posted) | |
2080 | io_commit_cqring(req->ctx); | |
2081 | spin_unlock(&ctx->completion_lock); | |
2082 | if (posted) | |
2083 | io_cqring_ev_posted(ctx); | |
2084 | } | |
2085 | ||
2086 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) | |
c69f8dbe | 2087 | { |
33cc89a9 | 2088 | struct io_kiocb *nxt; |
944e58bf | 2089 | |
d81499bf PB |
2090 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
2091 | return NULL; | |
9e645e11 JA |
2092 | /* |
2093 | * If LINK is set, we have dependent requests in this chain. If we | |
2094 | * didn't fail this request, queue the first one up, moving any other | |
2095 | * dependencies to the next request. In case of failure, fail the rest | |
2096 | * of the chain. | |
2097 | */ | |
d81499bf PB |
2098 | if (unlikely(req->flags & IO_DISARM_MASK)) |
2099 | __io_req_find_next_prep(req); | |
33cc89a9 PB |
2100 | nxt = req->link; |
2101 | req->link = NULL; | |
2102 | return nxt; | |
4d7dd462 | 2103 | } |
9e645e11 | 2104 | |
f237c30a | 2105 | static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked) |
2c32395d PB |
2106 | { |
2107 | if (!ctx) | |
2108 | return; | |
f237c30a | 2109 | if (*locked) { |
c450178d | 2110 | io_submit_flush_completions(ctx); |
2c32395d | 2111 | mutex_unlock(&ctx->uring_lock); |
f237c30a | 2112 | *locked = false; |
2c32395d PB |
2113 | } |
2114 | percpu_ref_put(&ctx->refs); | |
2115 | } | |
2116 | ||
7cbf1722 | 2117 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 2118 | { |
f237c30a | 2119 | bool locked = false; |
ebd0df2e | 2120 | struct io_ring_ctx *ctx = NULL; |
3f18407d PB |
2121 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
2122 | task_work); | |
c40f6379 | 2123 | |
16f72070 | 2124 | while (1) { |
3f18407d PB |
2125 | struct io_wq_work_node *node; |
2126 | ||
c450178d | 2127 | if (!tctx->task_list.first && locked) |
8d4ad41e PB |
2128 | io_submit_flush_completions(ctx); |
2129 | ||
3f18407d | 2130 | spin_lock_irq(&tctx->task_lock); |
c6538be9 | 2131 | node = tctx->task_list.first; |
3f18407d | 2132 | INIT_WQ_LIST(&tctx->task_list); |
6294f368 PB |
2133 | if (!node) |
2134 | tctx->task_running = false; | |
3f18407d | 2135 | spin_unlock_irq(&tctx->task_lock); |
6294f368 PB |
2136 | if (!node) |
2137 | break; | |
3f18407d | 2138 | |
6294f368 | 2139 | do { |
3f18407d PB |
2140 | struct io_wq_work_node *next = node->next; |
2141 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2142 | io_task_work.node); | |
2143 | ||
2144 | if (req->ctx != ctx) { | |
f237c30a | 2145 | ctx_flush_and_put(ctx, &locked); |
3f18407d | 2146 | ctx = req->ctx; |
126180b9 PB |
2147 | /* if not contended, grab and improve batching */ |
2148 | locked = mutex_trylock(&ctx->uring_lock); | |
3f18407d PB |
2149 | percpu_ref_get(&ctx->refs); |
2150 | } | |
f237c30a | 2151 | req->io_task_work.func(req, &locked); |
3f18407d | 2152 | node = next; |
6294f368 PB |
2153 | } while (node); |
2154 | ||
7cbf1722 | 2155 | cond_resched(); |
3f18407d | 2156 | } |
ebd0df2e | 2157 | |
f237c30a | 2158 | ctx_flush_and_put(ctx, &locked); |
7cbf1722 JA |
2159 | } |
2160 | ||
e09ee510 | 2161 | static void io_req_task_work_add(struct io_kiocb *req) |
7cbf1722 | 2162 | { |
c15b79de | 2163 | struct task_struct *tsk = req->task; |
7cbf1722 | 2164 | struct io_uring_task *tctx = tsk->io_uring; |
c15b79de | 2165 | enum task_work_notify_mode notify; |
e09ee510 | 2166 | struct io_wq_work_node *node; |
0b81e80c | 2167 | unsigned long flags; |
6294f368 | 2168 | bool running; |
7cbf1722 JA |
2169 | |
2170 | WARN_ON_ONCE(!tctx); | |
2171 | ||
0b81e80c | 2172 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 | 2173 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
6294f368 PB |
2174 | running = tctx->task_running; |
2175 | if (!running) | |
2176 | tctx->task_running = true; | |
0b81e80c | 2177 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2178 | |
2179 | /* task_work already pending, we're done */ | |
6294f368 | 2180 | if (running) |
e09ee510 | 2181 | return; |
7cbf1722 | 2182 | |
c15b79de PB |
2183 | /* |
2184 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For | |
2185 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2186 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2187 | * will do the job. | |
2188 | */ | |
2189 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; | |
d97ec623 PB |
2190 | if (likely(!task_work_add(tsk, &tctx->task_work, notify))) { |
2191 | if (notify == TWA_NONE) | |
2192 | wake_up_process(tsk); | |
e09ee510 | 2193 | return; |
c15b79de | 2194 | } |
2215bed9 | 2195 | |
0b81e80c | 2196 | spin_lock_irqsave(&tctx->task_lock, flags); |
6294f368 | 2197 | tctx->task_running = false; |
e09ee510 PB |
2198 | node = tctx->task_list.first; |
2199 | INIT_WQ_LIST(&tctx->task_list); | |
0b81e80c | 2200 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 | 2201 | |
e09ee510 PB |
2202 | while (node) { |
2203 | req = container_of(node, struct io_kiocb, io_task_work.node); | |
2204 | node = node->next; | |
2205 | if (llist_add(&req->io_task_work.fallback_node, | |
2206 | &req->ctx->fallback_llist)) | |
2207 | schedule_delayed_work(&req->ctx->fallback_work, 1); | |
2208 | } | |
eab30c4d PB |
2209 | } |
2210 | ||
f237c30a | 2211 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
c40f6379 | 2212 | { |
87ceb6a6 | 2213 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2214 | |
b18a1a45 | 2215 | /* not needed for normal modes, but SQPOLL depends on it */ |
f237c30a | 2216 | io_tw_lock(ctx, locked); |
2593553a | 2217 | io_req_complete_failed(req, req->result); |
c40f6379 JA |
2218 | } |
2219 | ||
f237c30a | 2220 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
c40f6379 JA |
2221 | { |
2222 | struct io_ring_ctx *ctx = req->ctx; | |
2223 | ||
f237c30a | 2224 | io_tw_lock(ctx, locked); |
316319e8 | 2225 | /* req->task == current here, checking PF_EXITING is safe */ |
af066f31 | 2226 | if (likely(!(req->task->flags & PF_EXITING))) |
c5eef2b9 | 2227 | __io_queue_sqe(req); |
81b6d05c | 2228 | else |
2593553a | 2229 | io_req_complete_failed(req, -EFAULT); |
c40f6379 JA |
2230 | } |
2231 | ||
2c4b8eb6 | 2232 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
c40f6379 | 2233 | { |
2c4b8eb6 | 2234 | req->result = ret; |
5b0a6acc | 2235 | req->io_task_work.func = io_req_task_cancel; |
e09ee510 | 2236 | io_req_task_work_add(req); |
c40f6379 JA |
2237 | } |
2238 | ||
2c4b8eb6 | 2239 | static void io_req_task_queue(struct io_kiocb *req) |
a3df7698 | 2240 | { |
5b0a6acc | 2241 | req->io_task_work.func = io_req_task_submit; |
e09ee510 | 2242 | io_req_task_work_add(req); |
a3df7698 PB |
2243 | } |
2244 | ||
773af691 JA |
2245 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
2246 | { | |
2247 | req->io_task_work.func = io_queue_async_work; | |
2248 | io_req_task_work_add(req); | |
2249 | } | |
2250 | ||
f2f87370 | 2251 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2252 | { |
9b5f7bd9 | 2253 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2254 | |
2255 | if (nxt) | |
906a8c3f | 2256 | io_req_task_queue(nxt); |
c69f8dbe JL |
2257 | } |
2258 | ||
c3524383 | 2259 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2260 | { |
c3524383 PB |
2261 | io_queue_next(req); |
2262 | __io_free_req(req); | |
2263 | } | |
8766dd51 | 2264 | |
f237c30a PB |
2265 | static void io_free_req_work(struct io_kiocb *req, bool *locked) |
2266 | { | |
2267 | io_free_req(req); | |
2268 | } | |
2269 | ||
3aa83bfb | 2270 | static void io_free_batch_list(struct io_ring_ctx *ctx, |
1cce17ac | 2271 | struct io_wq_work_node *node) |
3aa83bfb PB |
2272 | __must_hold(&ctx->uring_lock) |
2273 | { | |
d4b7a5ef PB |
2274 | struct task_struct *task = NULL; |
2275 | int task_refs = 0, ctx_refs = 0; | |
3aa83bfb | 2276 | |
3aa83bfb PB |
2277 | do { |
2278 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2279 | comp_list); | |
2280 | ||
2281 | node = req->comp_list.next; | |
d4b7a5ef PB |
2282 | if (!req_ref_put_and_test(req)) |
2283 | continue; | |
2284 | ||
2285 | io_queue_next(req); | |
2286 | io_dismantle_req(req); | |
2287 | ||
2288 | if (req->task != task) { | |
2289 | if (task) | |
2290 | io_put_task(task, task_refs); | |
2291 | task = req->task; | |
2292 | task_refs = 0; | |
2293 | } | |
2294 | task_refs++; | |
2295 | ctx_refs++; | |
2296 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); | |
3aa83bfb | 2297 | } while (node); |
d4b7a5ef PB |
2298 | |
2299 | if (ctx_refs) | |
2300 | percpu_ref_put_many(&ctx->refs, ctx_refs); | |
2301 | if (task) | |
2302 | io_put_task(task, task_refs); | |
3aa83bfb PB |
2303 | } |
2304 | ||
c450178d | 2305 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx) |
a141dd89 | 2306 | __must_hold(&ctx->uring_lock) |
905c172f | 2307 | { |
6f33b0bc | 2308 | struct io_wq_work_node *node, *prev; |
cd0ca2e0 | 2309 | struct io_submit_state *state = &ctx->submit_state; |
905c172f | 2310 | |
79ebeaee | 2311 | spin_lock(&ctx->completion_lock); |
6f33b0bc PB |
2312 | wq_list_for_each(node, prev, &state->compl_reqs) { |
2313 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2314 | comp_list); | |
5182ed2e | 2315 | |
d4d19c19 PB |
2316 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
2317 | req->compl.cflags); | |
905c172f PB |
2318 | } |
2319 | io_commit_cqring(ctx); | |
79ebeaee | 2320 | spin_unlock(&ctx->completion_lock); |
905c172f | 2321 | io_cqring_ev_posted(ctx); |
5182ed2e | 2322 | |
1cce17ac | 2323 | io_free_batch_list(ctx, state->compl_reqs.first); |
6f33b0bc | 2324 | INIT_WQ_LIST(&state->compl_reqs); |
7a743e22 PB |
2325 | } |
2326 | ||
ba816ad6 JA |
2327 | /* |
2328 | * Drop reference to request, return next in chain (if there is one) if this | |
2329 | * was the last reference to this request. | |
2330 | */ | |
0d85035a | 2331 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2332 | { |
9b5f7bd9 PB |
2333 | struct io_kiocb *nxt = NULL; |
2334 | ||
de9b4cca | 2335 | if (req_ref_put_and_test(req)) { |
9b5f7bd9 | 2336 | nxt = io_req_find_next(req); |
4d7dd462 | 2337 | __io_free_req(req); |
2a44f467 | 2338 | } |
9b5f7bd9 | 2339 | return nxt; |
2b188cc1 JA |
2340 | } |
2341 | ||
0d85035a | 2342 | static inline void io_put_req(struct io_kiocb *req) |
e65ef56d | 2343 | { |
de9b4cca | 2344 | if (req_ref_put_and_test(req)) |
e65ef56d | 2345 | io_free_req(req); |
2b188cc1 JA |
2346 | } |
2347 | ||
91c2f697 | 2348 | static inline void io_put_req_deferred(struct io_kiocb *req) |
216578e5 | 2349 | { |
91c2f697 | 2350 | if (req_ref_put_and_test(req)) { |
f237c30a | 2351 | req->io_task_work.func = io_free_req_work; |
543af3a1 PB |
2352 | io_req_task_work_add(req); |
2353 | } | |
216578e5 PB |
2354 | } |
2355 | ||
6c503150 | 2356 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2357 | { |
2358 | /* See comment at the top of this file */ | |
2359 | smp_rmb(); | |
e23de15f | 2360 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2361 | } |
2362 | ||
fb5ccc98 PB |
2363 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2364 | { | |
2365 | struct io_rings *rings = ctx->rings; | |
2366 | ||
2367 | /* make sure SQ entry isn't read before tail */ | |
2368 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2369 | } | |
2370 | ||
8ff069bf | 2371 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2372 | { |
8ff069bf | 2373 | unsigned int cflags; |
e94f141b | 2374 | |
bcda7baa JA |
2375 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2376 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2377 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2378 | kfree(kbuf); |
2379 | return cflags; | |
e94f141b JA |
2380 | } |
2381 | ||
8ff069bf | 2382 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2383 | { |
ae421d93 PB |
2384 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
2385 | return 0; | |
30d51dd4 | 2386 | return io_put_kbuf(req, req->kbuf); |
8ff069bf PB |
2387 | } |
2388 | ||
4c6e277c JA |
2389 | static inline bool io_run_task_work(void) |
2390 | { | |
ef98eb04 | 2391 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) { |
4c6e277c | 2392 | __set_current_state(TASK_RUNNING); |
ef98eb04 | 2393 | tracehook_notify_signal(); |
4c6e277c JA |
2394 | return true; |
2395 | } | |
2396 | ||
2397 | return false; | |
bcda7baa JA |
2398 | } |
2399 | ||
5ba3c874 | 2400 | static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) |
def596e9 | 2401 | { |
5eef4e87 | 2402 | struct io_wq_work_node *pos, *start, *prev; |
d729cf9a | 2403 | unsigned int poll_flags = BLK_POLL_NOSLEEP; |
b688f11e | 2404 | DEFINE_IO_COMP_BATCH(iob); |
5ba3c874 | 2405 | int nr_events = 0; |
def596e9 JA |
2406 | |
2407 | /* | |
2408 | * Only spin for completions if we don't have multiple devices hanging | |
87a115fb | 2409 | * off our complete list. |
def596e9 | 2410 | */ |
87a115fb | 2411 | if (ctx->poll_multi_queue || force_nonspin) |
ef99b2d3 | 2412 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2413 | |
5eef4e87 PB |
2414 | wq_list_for_each(pos, start, &ctx->iopoll_list) { |
2415 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
9adbd45d | 2416 | struct kiocb *kiocb = &req->rw.kiocb; |
a2416e1e | 2417 | int ret; |
def596e9 JA |
2418 | |
2419 | /* | |
581f9810 BM |
2420 | * Move completed and retryable entries to our local lists. |
2421 | * If we find a request that requires polling, break out | |
2422 | * and complete those lists first, if we have entries there. | |
def596e9 | 2423 | */ |
e3f721e6 | 2424 | if (READ_ONCE(req->iopoll_completed)) |
def596e9 JA |
2425 | break; |
2426 | ||
b688f11e | 2427 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags); |
a2416e1e PB |
2428 | if (unlikely(ret < 0)) |
2429 | return ret; | |
2430 | else if (ret) | |
ef99b2d3 | 2431 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2432 | |
3aadc23e | 2433 | /* iopoll may have completed current req */ |
b688f11e | 2434 | if (!rq_list_empty(iob.req_list) || |
e3f721e6 PB |
2435 | READ_ONCE(req->iopoll_completed)) |
2436 | break; | |
def596e9 JA |
2437 | } |
2438 | ||
b688f11e JA |
2439 | if (!rq_list_empty(iob.req_list)) |
2440 | iob.complete(&iob); | |
5eef4e87 PB |
2441 | else if (!pos) |
2442 | return 0; | |
2443 | ||
2444 | prev = start; | |
2445 | wq_list_for_each_resume(pos, prev) { | |
2446 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
2447 | ||
b3fa03fd PB |
2448 | /* order with io_complete_rw_iopoll(), e.g. ->result updates */ |
2449 | if (!smp_load_acquire(&req->iopoll_completed)) | |
e3f721e6 | 2450 | break; |
b3fa03fd | 2451 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
f5ed3bcd | 2452 | io_put_rw_kbuf(req)); |
e3f721e6 PB |
2453 | nr_events++; |
2454 | } | |
def596e9 | 2455 | |
f5ed3bcd PB |
2456 | if (unlikely(!nr_events)) |
2457 | return 0; | |
2458 | ||
2459 | io_commit_cqring(ctx); | |
2460 | io_cqring_ev_posted_iopoll(ctx); | |
1cce17ac | 2461 | pos = start ? start->next : ctx->iopoll_list.first; |
5eef4e87 | 2462 | wq_list_cut(&ctx->iopoll_list, prev, start); |
1cce17ac | 2463 | io_free_batch_list(ctx, pos); |
5ba3c874 | 2464 | return nr_events; |
def596e9 JA |
2465 | } |
2466 | ||
def596e9 JA |
2467 | /* |
2468 | * We can't just wait for polled events to come to us, we have to actively | |
2469 | * find and complete them. | |
2470 | */ | |
b2edc0a7 | 2471 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2472 | { |
2473 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2474 | return; | |
2475 | ||
2476 | mutex_lock(&ctx->uring_lock); | |
5eef4e87 | 2477 | while (!wq_list_empty(&ctx->iopoll_list)) { |
b2edc0a7 | 2478 | /* let it sleep and repeat later if can't complete a request */ |
5ba3c874 | 2479 | if (io_do_iopoll(ctx, true) == 0) |
b2edc0a7 | 2480 | break; |
08f5439f JA |
2481 | /* |
2482 | * Ensure we allow local-to-the-cpu processing to take place, | |
2483 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2484 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2485 | */ |
3fcee5a6 PB |
2486 | if (need_resched()) { |
2487 | mutex_unlock(&ctx->uring_lock); | |
2488 | cond_resched(); | |
2489 | mutex_lock(&ctx->uring_lock); | |
2490 | } | |
def596e9 JA |
2491 | } |
2492 | mutex_unlock(&ctx->uring_lock); | |
2493 | } | |
2494 | ||
7668b92a | 2495 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2496 | { |
7668b92a | 2497 | unsigned int nr_events = 0; |
e9979b36 | 2498 | int ret = 0; |
500f9fba | 2499 | |
c7849be9 XW |
2500 | /* |
2501 | * We disallow the app entering submit/complete with polling, but we | |
2502 | * still need to lock the ring to prevent racing with polled issue | |
2503 | * that got punted to a workqueue. | |
2504 | */ | |
2505 | mutex_lock(&ctx->uring_lock); | |
f39c8a5b PB |
2506 | /* |
2507 | * Don't enter poll loop if we already have events pending. | |
2508 | * If we do, we can potentially be spinning for commands that | |
2509 | * already triggered a CQE (eg in error). | |
2510 | */ | |
5ed7a37d | 2511 | if (test_bit(0, &ctx->check_cq_overflow)) |
f39c8a5b PB |
2512 | __io_cqring_overflow_flush(ctx, false); |
2513 | if (io_cqring_events(ctx)) | |
2514 | goto out; | |
def596e9 | 2515 | do { |
500f9fba JA |
2516 | /* |
2517 | * If a submit got punted to a workqueue, we can have the | |
2518 | * application entering polling for a command before it gets | |
2519 | * issued. That app will hold the uring_lock for the duration | |
2520 | * of the poll right here, so we need to take a breather every | |
2521 | * now and then to ensure that the issue has a chance to add | |
2522 | * the poll to the issued list. Otherwise we can spin here | |
2523 | * forever, while the workqueue is stuck trying to acquire the | |
2524 | * very same mutex. | |
2525 | */ | |
5eef4e87 | 2526 | if (wq_list_empty(&ctx->iopoll_list)) { |
8f487ef2 PB |
2527 | u32 tail = ctx->cached_cq_tail; |
2528 | ||
500f9fba | 2529 | mutex_unlock(&ctx->uring_lock); |
4c6e277c | 2530 | io_run_task_work(); |
500f9fba | 2531 | mutex_lock(&ctx->uring_lock); |
def596e9 | 2532 | |
8f487ef2 PB |
2533 | /* some requests don't go through iopoll_list */ |
2534 | if (tail != ctx->cached_cq_tail || | |
5eef4e87 | 2535 | wq_list_empty(&ctx->iopoll_list)) |
e9979b36 | 2536 | break; |
500f9fba | 2537 | } |
5ba3c874 PB |
2538 | ret = io_do_iopoll(ctx, !min); |
2539 | if (ret < 0) | |
2540 | break; | |
2541 | nr_events += ret; | |
2542 | ret = 0; | |
2543 | } while (nr_events < min && !need_resched()); | |
f39c8a5b | 2544 | out: |
500f9fba | 2545 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2546 | return ret; |
2547 | } | |
2548 | ||
491381ce | 2549 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2550 | { |
491381ce JA |
2551 | /* |
2552 | * Tell lockdep we inherited freeze protection from submission | |
2553 | * thread. | |
2554 | */ | |
2555 | if (req->flags & REQ_F_ISREG) { | |
1c98679d | 2556 | struct super_block *sb = file_inode(req->file)->i_sb; |
2b188cc1 | 2557 | |
1c98679d PB |
2558 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
2559 | sb_end_write(sb); | |
2b188cc1 JA |
2560 | } |
2561 | } | |
2562 | ||
b63534c4 | 2563 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2564 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 | 2565 | { |
ab454438 | 2566 | struct io_async_rw *rw = req->async_data; |
b63534c4 | 2567 | |
ab454438 PB |
2568 | if (!rw) |
2569 | return !io_req_prep_async(req); | |
cd658695 | 2570 | iov_iter_restore(&rw->iter, &rw->iter_state); |
ab454438 | 2571 | return true; |
b63534c4 | 2572 | } |
b63534c4 | 2573 | |
3e6a0d3c | 2574 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 2575 | { |
355afaeb | 2576 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 2577 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 2578 | |
355afaeb JA |
2579 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2580 | return false; | |
3e6a0d3c JA |
2581 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
2582 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 2583 | return false; |
7c977a58 JA |
2584 | /* |
2585 | * If ref is dying, we might be running poll reap from the exit work. | |
2586 | * Don't attempt to reissue from that path, just let it fail with | |
2587 | * -EAGAIN. | |
2588 | */ | |
3e6a0d3c JA |
2589 | if (percpu_ref_is_dying(&ctx->refs)) |
2590 | return false; | |
ef046888 JA |
2591 | /* |
2592 | * Play it safe and assume not safe to re-import and reissue if we're | |
2593 | * not in the original thread group (or in task context). | |
2594 | */ | |
2595 | if (!same_thread_group(req->task, current) || !in_task()) | |
2596 | return false; | |
3e6a0d3c JA |
2597 | return true; |
2598 | } | |
e82ad485 | 2599 | #else |
a1ff1e3f | 2600 | static bool io_resubmit_prep(struct io_kiocb *req) |
e82ad485 JA |
2601 | { |
2602 | return false; | |
2603 | } | |
e82ad485 | 2604 | static bool io_rw_should_reissue(struct io_kiocb *req) |
3e6a0d3c | 2605 | { |
b63534c4 JA |
2606 | return false; |
2607 | } | |
3e6a0d3c | 2608 | #endif |
b63534c4 | 2609 | |
8ef12efe | 2610 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
a1d7c393 | 2611 | { |
b65c128f PB |
2612 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
2613 | kiocb_end_write(req); | |
9532b99b PB |
2614 | if (res != req->result) { |
2615 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && | |
2616 | io_rw_should_reissue(req)) { | |
2617 | req->flags |= REQ_F_REISSUE; | |
8ef12efe | 2618 | return true; |
9532b99b | 2619 | } |
93d2bcd2 | 2620 | req_set_fail(req); |
8ef12efe | 2621 | req->result = res; |
9532b99b | 2622 | } |
8ef12efe JA |
2623 | return false; |
2624 | } | |
2625 | ||
f237c30a | 2626 | static void io_req_task_complete(struct io_kiocb *req, bool *locked) |
8ef12efe | 2627 | { |
126180b9 PB |
2628 | unsigned int cflags = io_put_rw_kbuf(req); |
2629 | long res = req->result; | |
2630 | ||
fff4e40e | 2631 | if (*locked) { |
126180b9 | 2632 | io_req_complete_state(req, res, cflags); |
fff4e40e PB |
2633 | io_req_add_compl_list(req); |
2634 | } else { | |
126180b9 | 2635 | io_req_complete_post(req, res, cflags); |
fff4e40e | 2636 | } |
8ef12efe JA |
2637 | } |
2638 | ||
2639 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, | |
2640 | unsigned int issue_flags) | |
2641 | { | |
2642 | if (__io_complete_rw_common(req, res)) | |
2643 | return; | |
63637853 | 2644 | __io_req_complete(req, issue_flags, req->result, io_put_rw_kbuf(req)); |
ba816ad6 JA |
2645 | } |
2646 | ||
2647 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2648 | { | |
9adbd45d | 2649 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2650 | |
8ef12efe JA |
2651 | if (__io_complete_rw_common(req, res)) |
2652 | return; | |
2653 | req->result = res; | |
2654 | req->io_task_work.func = io_req_task_complete; | |
2655 | io_req_task_work_add(req); | |
2b188cc1 JA |
2656 | } |
2657 | ||
def596e9 JA |
2658 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2659 | { | |
9adbd45d | 2660 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2661 | |
491381ce JA |
2662 | if (kiocb->ki_flags & IOCB_WRITE) |
2663 | kiocb_end_write(req); | |
9532b99b | 2664 | if (unlikely(res != req->result)) { |
b66ceaf3 PB |
2665 | if (res == -EAGAIN && io_rw_should_reissue(req)) { |
2666 | req->flags |= REQ_F_REISSUE; | |
2667 | return; | |
9532b99b | 2668 | } |
8c130827 | 2669 | } |
bbde017a | 2670 | |
b3fa03fd PB |
2671 | req->result = res; |
2672 | /* order with io_iopoll_complete() checking ->iopoll_completed */ | |
2673 | smp_store_release(&req->iopoll_completed, 1); | |
def596e9 JA |
2674 | } |
2675 | ||
2676 | /* | |
2677 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2678 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
f39c8a5b | 2679 | * find it from a io_do_iopoll() thread before the issuer is done |
def596e9 JA |
2680 | * accessing the kiocb cookie. |
2681 | */ | |
cb3d8972 | 2682 | static void io_iopoll_req_issued(struct io_kiocb *req) |
def596e9 JA |
2683 | { |
2684 | struct io_ring_ctx *ctx = req->ctx; | |
cb3d8972 PB |
2685 | const bool in_async = io_wq_current_is_worker(); |
2686 | ||
2687 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
2688 | if (unlikely(in_async)) | |
2689 | mutex_lock(&ctx->uring_lock); | |
def596e9 JA |
2690 | |
2691 | /* | |
2692 | * Track whether we have multiple files in our lists. This will impact | |
2693 | * how we do polling eventually, not spinning if we're on potentially | |
2694 | * different devices. | |
2695 | */ | |
5eef4e87 | 2696 | if (wq_list_empty(&ctx->iopoll_list)) { |
915b3dde HX |
2697 | ctx->poll_multi_queue = false; |
2698 | } else if (!ctx->poll_multi_queue) { | |
def596e9 JA |
2699 | struct io_kiocb *list_req; |
2700 | ||
5eef4e87 PB |
2701 | list_req = container_of(ctx->iopoll_list.first, struct io_kiocb, |
2702 | comp_list); | |
30da1b45 | 2703 | if (list_req->file != req->file) |
915b3dde | 2704 | ctx->poll_multi_queue = true; |
def596e9 JA |
2705 | } |
2706 | ||
2707 | /* | |
2708 | * For fast devices, IO may have already completed. If it has, add | |
2709 | * it to the front so we find it first. | |
2710 | */ | |
65a6543d | 2711 | if (READ_ONCE(req->iopoll_completed)) |
5eef4e87 | 2712 | wq_list_add_head(&req->comp_list, &ctx->iopoll_list); |
def596e9 | 2713 | else |
5eef4e87 | 2714 | wq_list_add_tail(&req->comp_list, &ctx->iopoll_list); |
bdcd3eab | 2715 | |
cb3d8972 PB |
2716 | if (unlikely(in_async)) { |
2717 | /* | |
2718 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle | |
2719 | * in sq thread task context or in io worker task context. If | |
2720 | * current task context is sq thread, we don't need to check | |
2721 | * whether should wake up sq thread. | |
2722 | */ | |
2723 | if ((ctx->flags & IORING_SETUP_SQPOLL) && | |
2724 | wq_has_sleeper(&ctx->sq_data->wait)) | |
2725 | wake_up(&ctx->sq_data->wait); | |
2726 | ||
2727 | mutex_unlock(&ctx->uring_lock); | |
2728 | } | |
def596e9 JA |
2729 | } |
2730 | ||
4503b767 JA |
2731 | static bool io_bdev_nowait(struct block_device *bdev) |
2732 | { | |
9ba0d0c8 | 2733 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2734 | } |
2735 | ||
2b188cc1 JA |
2736 | /* |
2737 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2738 | * any file. For now, just ensure that anything potentially problematic is done | |
2739 | * inline. | |
2740 | */ | |
b191e2df | 2741 | static bool __io_file_supports_nowait(struct file *file, int rw) |
2b188cc1 JA |
2742 | { |
2743 | umode_t mode = file_inode(file)->i_mode; | |
2744 | ||
4503b767 | 2745 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2746 | if (IS_ENABLED(CONFIG_BLOCK) && |
2747 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2748 | return true; |
2749 | return false; | |
2750 | } | |
976517f1 | 2751 | if (S_ISSOCK(mode)) |
2b188cc1 | 2752 | return true; |
4503b767 | 2753 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2754 | if (IS_ENABLED(CONFIG_BLOCK) && |
2755 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2756 | file->f_op != &io_uring_fops) |
2757 | return true; | |
2758 | return false; | |
2759 | } | |
2b188cc1 | 2760 | |
c5b85625 JA |
2761 | /* any ->read/write should understand O_NONBLOCK */ |
2762 | if (file->f_flags & O_NONBLOCK) | |
2763 | return true; | |
2764 | ||
af197f50 JA |
2765 | if (!(file->f_mode & FMODE_NOWAIT)) |
2766 | return false; | |
2767 | ||
2768 | if (rw == READ) | |
2769 | return file->f_op->read_iter != NULL; | |
2770 | ||
2771 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
2772 | } |
2773 | ||
b191e2df | 2774 | static bool io_file_supports_nowait(struct io_kiocb *req, int rw) |
7b29f92d | 2775 | { |
b191e2df | 2776 | if (rw == READ && (req->flags & REQ_F_NOWAIT_READ)) |
7b29f92d | 2777 | return true; |
b191e2df | 2778 | else if (rw == WRITE && (req->flags & REQ_F_NOWAIT_WRITE)) |
7b29f92d JA |
2779 | return true; |
2780 | ||
b191e2df | 2781 | return __io_file_supports_nowait(req->file, rw); |
7b29f92d JA |
2782 | } |
2783 | ||
5d329e12 JA |
2784 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2785 | int rw) | |
2b188cc1 | 2786 | { |
def596e9 | 2787 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2788 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 2789 | struct file *file = req->file; |
09bb8394 JA |
2790 | unsigned ioprio; |
2791 | int ret; | |
2b188cc1 | 2792 | |
c97d8a0f | 2793 | if (!io_req_ffs_set(req) && S_ISREG(file_inode(file)->i_mode)) |
491381ce JA |
2794 | req->flags |= REQ_F_ISREG; |
2795 | ||
2b188cc1 | 2796 | kiocb->ki_pos = READ_ONCE(sqe->off); |
75c668cd | 2797 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
ba04291e | 2798 | req->flags |= REQ_F_CUR_POS; |
75c668cd | 2799 | kiocb->ki_pos = file->f_pos; |
ba04291e | 2800 | } |
2b188cc1 | 2801 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
2802 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
2803 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
2804 | if (unlikely(ret)) | |
2805 | return ret; | |
2b188cc1 | 2806 | |
5d329e12 JA |
2807 | /* |
2808 | * If the file is marked O_NONBLOCK, still allow retry for it if it | |
2809 | * supports async. Otherwise it's impossible to use O_NONBLOCK files | |
2810 | * reliably. If not, or it IOCB_NOWAIT is set, don't retry. | |
2811 | */ | |
2812 | if ((kiocb->ki_flags & IOCB_NOWAIT) || | |
2813 | ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req, rw))) | |
75c668cd PB |
2814 | req->flags |= REQ_F_NOWAIT; |
2815 | ||
2b188cc1 JA |
2816 | ioprio = READ_ONCE(sqe->ioprio); |
2817 | if (ioprio) { | |
2818 | ret = ioprio_check_cap(ioprio); | |
2819 | if (ret) | |
09bb8394 | 2820 | return ret; |
2b188cc1 JA |
2821 | |
2822 | kiocb->ki_ioprio = ioprio; | |
2823 | } else | |
2824 | kiocb->ki_ioprio = get_current_ioprio(); | |
2825 | ||
def596e9 | 2826 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
2827 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
2828 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 2829 | return -EOPNOTSUPP; |
2b188cc1 | 2830 | |
394918eb | 2831 | kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE; |
def596e9 | 2832 | kiocb->ki_complete = io_complete_rw_iopoll; |
65a6543d | 2833 | req->iopoll_completed = 0; |
def596e9 | 2834 | } else { |
09bb8394 JA |
2835 | if (kiocb->ki_flags & IOCB_HIPRI) |
2836 | return -EINVAL; | |
def596e9 JA |
2837 | kiocb->ki_complete = io_complete_rw; |
2838 | } | |
9adbd45d | 2839 | |
eae071c9 PB |
2840 | if (req->opcode == IORING_OP_READ_FIXED || |
2841 | req->opcode == IORING_OP_WRITE_FIXED) { | |
2842 | req->imu = NULL; | |
2843 | io_req_set_rsrc_node(req); | |
2844 | } | |
2845 | ||
3529d8c2 JA |
2846 | req->rw.addr = READ_ONCE(sqe->addr); |
2847 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2848 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2849 | return 0; |
2b188cc1 JA |
2850 | } |
2851 | ||
2852 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2853 | { | |
2854 | switch (ret) { | |
2855 | case -EIOCBQUEUED: | |
2856 | break; | |
2857 | case -ERESTARTSYS: | |
2858 | case -ERESTARTNOINTR: | |
2859 | case -ERESTARTNOHAND: | |
2860 | case -ERESTART_RESTARTBLOCK: | |
2861 | /* | |
2862 | * We can't just restart the syscall, since previously | |
2863 | * submitted sqes may already be in progress. Just fail this | |
2864 | * IO with EINTR. | |
2865 | */ | |
2866 | ret = -EINTR; | |
df561f66 | 2867 | fallthrough; |
2b188cc1 JA |
2868 | default: |
2869 | kiocb->ki_complete(kiocb, ret, 0); | |
2870 | } | |
2871 | } | |
2872 | ||
a1d7c393 | 2873 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
889fca73 | 2874 | unsigned int issue_flags) |
ba816ad6 | 2875 | { |
ba04291e | 2876 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2877 | struct io_async_rw *io = req->async_data; |
ba04291e | 2878 | |
227c0c96 | 2879 | /* add previously done IO, if any */ |
e8c2bc1f | 2880 | if (io && io->bytes_done > 0) { |
227c0c96 | 2881 | if (ret < 0) |
e8c2bc1f | 2882 | ret = io->bytes_done; |
227c0c96 | 2883 | else |
e8c2bc1f | 2884 | ret += io->bytes_done; |
227c0c96 JA |
2885 | } |
2886 | ||
ba04291e JA |
2887 | if (req->flags & REQ_F_CUR_POS) |
2888 | req->file->f_pos = kiocb->ki_pos; | |
b66ceaf3 | 2889 | if (ret >= 0 && (kiocb->ki_complete == io_complete_rw)) |
889fca73 | 2890 | __io_complete_rw(req, ret, 0, issue_flags); |
ba816ad6 JA |
2891 | else |
2892 | io_rw_done(kiocb, ret); | |
97284637 | 2893 | |
b66ceaf3 | 2894 | if (req->flags & REQ_F_REISSUE) { |
97284637 | 2895 | req->flags &= ~REQ_F_REISSUE; |
a7be7c23 | 2896 | if (io_resubmit_prep(req)) { |
773af691 | 2897 | io_req_task_queue_reissue(req); |
8c130827 | 2898 | } else { |
b66ceaf3 PB |
2899 | unsigned int cflags = io_put_rw_kbuf(req); |
2900 | struct io_ring_ctx *ctx = req->ctx; | |
2901 | ||
93d2bcd2 | 2902 | req_set_fail(req); |
14cfbb7a | 2903 | if (!(issue_flags & IO_URING_F_NONBLOCK)) { |
b66ceaf3 PB |
2904 | mutex_lock(&ctx->uring_lock); |
2905 | __io_req_complete(req, issue_flags, ret, cflags); | |
2906 | mutex_unlock(&ctx->uring_lock); | |
2907 | } else { | |
2908 | __io_req_complete(req, issue_flags, ret, cflags); | |
2909 | } | |
97284637 PB |
2910 | } |
2911 | } | |
ba816ad6 JA |
2912 | } |
2913 | ||
eae071c9 PB |
2914 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
2915 | struct io_mapped_ubuf *imu) | |
edafccee | 2916 | { |
9adbd45d | 2917 | size_t len = req->rw.len; |
75769e3f | 2918 | u64 buf_end, buf_addr = req->rw.addr; |
edafccee | 2919 | size_t offset; |
edafccee | 2920 | |
75769e3f | 2921 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
edafccee JA |
2922 | return -EFAULT; |
2923 | /* not inside the mapped region */ | |
4751f53d | 2924 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
edafccee JA |
2925 | return -EFAULT; |
2926 | ||
2927 | /* | |
2928 | * May not be a start of buffer, set size appropriately | |
2929 | * and advance us to the beginning. | |
2930 | */ | |
2931 | offset = buf_addr - imu->ubuf; | |
2932 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
2933 | |
2934 | if (offset) { | |
2935 | /* | |
2936 | * Don't use iov_iter_advance() here, as it's really slow for | |
2937 | * using the latter parts of a big fixed buffer - it iterates | |
2938 | * over each segment manually. We can cheat a bit here, because | |
2939 | * we know that: | |
2940 | * | |
2941 | * 1) it's a BVEC iter, we set it up | |
2942 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
2943 | * first and last bvec | |
2944 | * | |
2945 | * So just find our index, and adjust the iterator afterwards. | |
2946 | * If the offset is within the first bvec (or the whole first | |
2947 | * bvec, just use iov_iter_advance(). This makes it easier | |
2948 | * since we can just skip the first segment, which may not | |
2949 | * be PAGE_SIZE aligned. | |
2950 | */ | |
2951 | const struct bio_vec *bvec = imu->bvec; | |
2952 | ||
2953 | if (offset <= bvec->bv_len) { | |
2954 | iov_iter_advance(iter, offset); | |
2955 | } else { | |
2956 | unsigned long seg_skip; | |
2957 | ||
2958 | /* skip first vec */ | |
2959 | offset -= bvec->bv_len; | |
2960 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
2961 | ||
2962 | iter->bvec = bvec + seg_skip; | |
2963 | iter->nr_segs -= seg_skip; | |
99c79f66 | 2964 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 2965 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
2966 | } |
2967 | } | |
2968 | ||
847595de | 2969 | return 0; |
edafccee JA |
2970 | } |
2971 | ||
eae071c9 PB |
2972 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
2973 | { | |
2974 | struct io_ring_ctx *ctx = req->ctx; | |
2975 | struct io_mapped_ubuf *imu = req->imu; | |
2976 | u16 index, buf_index = req->buf_index; | |
2977 | ||
2978 | if (likely(!imu)) { | |
2979 | if (unlikely(buf_index >= ctx->nr_user_bufs)) | |
2980 | return -EFAULT; | |
2981 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); | |
2982 | imu = READ_ONCE(ctx->user_bufs[index]); | |
2983 | req->imu = imu; | |
2984 | } | |
2985 | return __io_import_fixed(req, rw, iter, imu); | |
2986 | } | |
2987 | ||
bcda7baa JA |
2988 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
2989 | { | |
2990 | if (needs_lock) | |
2991 | mutex_unlock(&ctx->uring_lock); | |
2992 | } | |
2993 | ||
2994 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
2995 | { | |
2996 | /* | |
2997 | * "Normal" inline submissions always hold the uring_lock, since we | |
2998 | * grab it from the system call. Same is true for the SQPOLL offload. | |
2999 | * The only exception is when we've detached the request and issue it | |
3000 | * from an async worker thread, grab the lock for that case. | |
3001 | */ | |
3002 | if (needs_lock) | |
3003 | mutex_lock(&ctx->uring_lock); | |
3004 | } | |
3005 | ||
3006 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
30d51dd4 | 3007 | int bgid, bool needs_lock) |
bcda7baa | 3008 | { |
30d51dd4 | 3009 | struct io_buffer *kbuf = req->kbuf; |
bcda7baa JA |
3010 | struct io_buffer *head; |
3011 | ||
3012 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3013 | return kbuf; | |
3014 | ||
3015 | io_ring_submit_lock(req->ctx, needs_lock); | |
3016 | ||
3017 | lockdep_assert_held(&req->ctx->uring_lock); | |
3018 | ||
9e15c3a0 | 3019 | head = xa_load(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
3020 | if (head) { |
3021 | if (!list_empty(&head->list)) { | |
3022 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
3023 | list); | |
3024 | list_del(&kbuf->list); | |
3025 | } else { | |
3026 | kbuf = head; | |
9e15c3a0 | 3027 | xa_erase(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
3028 | } |
3029 | if (*len > kbuf->len) | |
3030 | *len = kbuf->len; | |
30d51dd4 PB |
3031 | req->flags |= REQ_F_BUFFER_SELECTED; |
3032 | req->kbuf = kbuf; | |
bcda7baa JA |
3033 | } else { |
3034 | kbuf = ERR_PTR(-ENOBUFS); | |
3035 | } | |
3036 | ||
3037 | io_ring_submit_unlock(req->ctx, needs_lock); | |
bcda7baa JA |
3038 | return kbuf; |
3039 | } | |
3040 | ||
4d954c25 JA |
3041 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
3042 | bool needs_lock) | |
3043 | { | |
3044 | struct io_buffer *kbuf; | |
4f4eeba8 | 3045 | u16 bgid; |
4d954c25 | 3046 | |
4f4eeba8 | 3047 | bgid = req->buf_index; |
30d51dd4 | 3048 | kbuf = io_buffer_select(req, len, bgid, needs_lock); |
4d954c25 JA |
3049 | if (IS_ERR(kbuf)) |
3050 | return kbuf; | |
4d954c25 JA |
3051 | return u64_to_user_ptr(kbuf->addr); |
3052 | } | |
3053 | ||
3054 | #ifdef CONFIG_COMPAT | |
3055 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
3056 | bool needs_lock) | |
3057 | { | |
3058 | struct compat_iovec __user *uiov; | |
3059 | compat_ssize_t clen; | |
3060 | void __user *buf; | |
3061 | ssize_t len; | |
3062 | ||
3063 | uiov = u64_to_user_ptr(req->rw.addr); | |
3064 | if (!access_ok(uiov, sizeof(*uiov))) | |
3065 | return -EFAULT; | |
3066 | if (__get_user(clen, &uiov->iov_len)) | |
3067 | return -EFAULT; | |
3068 | if (clen < 0) | |
3069 | return -EINVAL; | |
3070 | ||
3071 | len = clen; | |
3072 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3073 | if (IS_ERR(buf)) | |
3074 | return PTR_ERR(buf); | |
3075 | iov[0].iov_base = buf; | |
3076 | iov[0].iov_len = (compat_size_t) len; | |
3077 | return 0; | |
3078 | } | |
3079 | #endif | |
3080 | ||
3081 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3082 | bool needs_lock) | |
3083 | { | |
3084 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3085 | void __user *buf; | |
3086 | ssize_t len; | |
3087 | ||
3088 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3089 | return -EFAULT; | |
3090 | ||
3091 | len = iov[0].iov_len; | |
3092 | if (len < 0) | |
3093 | return -EINVAL; | |
3094 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3095 | if (IS_ERR(buf)) | |
3096 | return PTR_ERR(buf); | |
3097 | iov[0].iov_base = buf; | |
3098 | iov[0].iov_len = len; | |
3099 | return 0; | |
3100 | } | |
3101 | ||
3102 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3103 | bool needs_lock) | |
3104 | { | |
dddb3e26 | 3105 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
30d51dd4 | 3106 | struct io_buffer *kbuf = req->kbuf; |
dddb3e26 | 3107 | |
dddb3e26 JA |
3108 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
3109 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3110 | return 0; |
dddb3e26 | 3111 | } |
dd201662 | 3112 | if (req->rw.len != 1) |
4d954c25 JA |
3113 | return -EINVAL; |
3114 | ||
3115 | #ifdef CONFIG_COMPAT | |
3116 | if (req->ctx->compat) | |
3117 | return io_compat_import(req, iov, needs_lock); | |
3118 | #endif | |
3119 | ||
3120 | return __io_iov_buffer_select(req, iov, needs_lock); | |
3121 | } | |
3122 | ||
847595de PB |
3123 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
3124 | struct iov_iter *iter, bool needs_lock) | |
2b188cc1 | 3125 | { |
9adbd45d JA |
3126 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
3127 | size_t sqe_len = req->rw.len; | |
847595de | 3128 | u8 opcode = req->opcode; |
4d954c25 | 3129 | ssize_t ret; |
edafccee | 3130 | |
7d009165 | 3131 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 3132 | *iovec = NULL; |
9adbd45d | 3133 | return io_import_fixed(req, rw, iter); |
edafccee | 3134 | } |
2b188cc1 | 3135 | |
bcda7baa | 3136 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 3137 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
3138 | return -EINVAL; |
3139 | ||
3a6820f2 | 3140 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3141 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 3142 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 3143 | if (IS_ERR(buf)) |
4d954c25 | 3144 | return PTR_ERR(buf); |
3f9d6441 | 3145 | req->rw.len = sqe_len; |
bcda7baa JA |
3146 | } |
3147 | ||
3a6820f2 JA |
3148 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
3149 | *iovec = NULL; | |
10fc72e4 | 3150 | return ret; |
3a6820f2 JA |
3151 | } |
3152 | ||
4d954c25 JA |
3153 | if (req->flags & REQ_F_BUFFER_SELECT) { |
3154 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
847595de PB |
3155 | if (!ret) |
3156 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); | |
4d954c25 JA |
3157 | *iovec = NULL; |
3158 | return ret; | |
3159 | } | |
3160 | ||
89cd35c5 CH |
3161 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
3162 | req->ctx->compat); | |
2b188cc1 JA |
3163 | } |
3164 | ||
0fef9483 JA |
3165 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3166 | { | |
5b09e37e | 3167 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3168 | } |
3169 | ||
31b51510 | 3170 | /* |
32960613 JA |
3171 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3172 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3173 | */ |
4017eb91 | 3174 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3175 | { |
4017eb91 JA |
3176 | struct kiocb *kiocb = &req->rw.kiocb; |
3177 | struct file *file = req->file; | |
32960613 JA |
3178 | ssize_t ret = 0; |
3179 | ||
3180 | /* | |
3181 | * Don't support polled IO through this interface, and we can't | |
3182 | * support non-blocking either. For the latter, this just causes | |
3183 | * the kiocb to be handled from an async context. | |
3184 | */ | |
3185 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3186 | return -EOPNOTSUPP; | |
3187 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3188 | return -EAGAIN; | |
3189 | ||
3190 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3191 | struct iovec iovec; |
32960613 JA |
3192 | ssize_t nr; |
3193 | ||
311ae9e1 PB |
3194 | if (!iov_iter_is_bvec(iter)) { |
3195 | iovec = iov_iter_iovec(iter); | |
3196 | } else { | |
4017eb91 JA |
3197 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3198 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3199 | } |
3200 | ||
32960613 JA |
3201 | if (rw == READ) { |
3202 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3203 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3204 | } else { |
3205 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3206 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3207 | } |
3208 | ||
3209 | if (nr < 0) { | |
3210 | if (!ret) | |
3211 | ret = nr; | |
3212 | break; | |
3213 | } | |
16c8d2df JA |
3214 | if (!iov_iter_is_bvec(iter)) { |
3215 | iov_iter_advance(iter, nr); | |
3216 | } else { | |
3217 | req->rw.len -= nr; | |
3218 | req->rw.addr += nr; | |
3219 | } | |
32960613 JA |
3220 | ret += nr; |
3221 | if (nr != iovec.iov_len) | |
3222 | break; | |
32960613 JA |
3223 | } |
3224 | ||
3225 | return ret; | |
3226 | } | |
3227 | ||
ff6165b2 JA |
3228 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3229 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3230 | { |
e8c2bc1f | 3231 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3232 | |
ff6165b2 | 3233 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3234 | rw->free_iovec = iovec; |
227c0c96 | 3235 | rw->bytes_done = 0; |
ff6165b2 | 3236 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3237 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3238 | return; |
b64e3444 | 3239 | if (!iovec) { |
ff6165b2 JA |
3240 | unsigned iov_off = 0; |
3241 | ||
3242 | rw->iter.iov = rw->fast_iov; | |
3243 | if (iter->iov != fast_iov) { | |
3244 | iov_off = iter->iov - fast_iov; | |
3245 | rw->iter.iov += iov_off; | |
3246 | } | |
3247 | if (rw->fast_iov != fast_iov) | |
3248 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3249 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3250 | } else { |
3251 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3252 | } |
3253 | } | |
3254 | ||
8d4af685 | 3255 | static inline bool io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3256 | { |
e8c2bc1f JA |
3257 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3258 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3259 | return req->async_data == NULL; | |
3d9932a8 XW |
3260 | } |
3261 | ||
ff6165b2 JA |
3262 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3263 | const struct iovec *fast_iov, | |
227c0c96 | 3264 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3265 | { |
26f0505a | 3266 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
74566df3 | 3267 | return 0; |
e8c2bc1f | 3268 | if (!req->async_data) { |
cd658695 JA |
3269 | struct io_async_rw *iorw; |
3270 | ||
6cb78689 | 3271 | if (io_alloc_async_data(req)) { |
6bf985dc | 3272 | kfree(iovec); |
5d204bcf | 3273 | return -ENOMEM; |
6bf985dc | 3274 | } |
b7bb4f7d | 3275 | |
ff6165b2 | 3276 | io_req_map_rw(req, iovec, fast_iov, iter); |
cd658695 JA |
3277 | iorw = req->async_data; |
3278 | /* we've copied and mapped the iter, ensure state is saved */ | |
3279 | iov_iter_save_state(&iorw->iter, &iorw->iter_state); | |
5d204bcf | 3280 | } |
b7bb4f7d | 3281 | return 0; |
f67676d1 JA |
3282 | } |
3283 | ||
73debe68 | 3284 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3285 | { |
e8c2bc1f | 3286 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3287 | struct iovec *iov = iorw->fast_iov; |
847595de | 3288 | int ret; |
c3e330a4 | 3289 | |
2846c481 | 3290 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
c3e330a4 PB |
3291 | if (unlikely(ret < 0)) |
3292 | return ret; | |
3293 | ||
ab0b196c PB |
3294 | iorw->bytes_done = 0; |
3295 | iorw->free_iovec = iov; | |
3296 | if (iov) | |
3297 | req->flags |= REQ_F_NEED_CLEANUP; | |
cd658695 | 3298 | iov_iter_save_state(&iorw->iter, &iorw->iter_state); |
c3e330a4 PB |
3299 | return 0; |
3300 | } | |
3301 | ||
73debe68 | 3302 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3303 | { |
3529d8c2 JA |
3304 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3305 | return -EBADF; | |
5d329e12 | 3306 | return io_prep_rw(req, sqe, READ); |
f67676d1 JA |
3307 | } |
3308 | ||
c1dd91d1 JA |
3309 | /* |
3310 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3311 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3312 | * This gets called when the page is unlocked, and we generally expect that to | |
3313 | * happen when the page IO is completed and the page is now uptodate. This will | |
3314 | * queue a task_work based retry of the operation, attempting to copy the data | |
3315 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3316 | * do a thread based blocking retry of the operation. That's the unexpected | |
3317 | * slow path. | |
3318 | */ | |
bcf5a063 JA |
3319 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3320 | int sync, void *arg) | |
3321 | { | |
3322 | struct wait_page_queue *wpq; | |
3323 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3324 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3325 | |
3326 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3327 | ||
cdc8fcb4 LT |
3328 | if (!wake_page_match(wpq, key)) |
3329 | return 0; | |
3330 | ||
c8d317aa | 3331 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 | 3332 | list_del_init(&wait->entry); |
921b9054 | 3333 | io_req_task_queue(req); |
bcf5a063 JA |
3334 | return 1; |
3335 | } | |
3336 | ||
c1dd91d1 JA |
3337 | /* |
3338 | * This controls whether a given IO request should be armed for async page | |
3339 | * based retry. If we return false here, the request is handed to the async | |
3340 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3341 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3342 | * will either succeed because the page is now uptodate and unlocked, or it | |
3343 | * will register a callback when the page is unlocked at IO completion. Through | |
3344 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3345 | * That retry will attempt the buffered read again. The retry will generally | |
3346 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3347 | * async worker threads for a blocking retry. | |
3348 | */ | |
227c0c96 | 3349 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3350 | { |
e8c2bc1f JA |
3351 | struct io_async_rw *rw = req->async_data; |
3352 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3353 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3354 | |
bcf5a063 JA |
3355 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3356 | if (req->flags & REQ_F_NOWAIT) | |
3357 | return false; | |
f67676d1 | 3358 | |
227c0c96 | 3359 | /* Only for buffered IO */ |
3b2a4439 | 3360 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3361 | return false; |
3b2a4439 | 3362 | |
bcf5a063 JA |
3363 | /* |
3364 | * just use poll if we can, and don't attempt if the fs doesn't | |
3365 | * support callback based unlocks | |
3366 | */ | |
3367 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3368 | return false; | |
f67676d1 | 3369 | |
3b2a4439 JA |
3370 | wait->wait.func = io_async_buf_func; |
3371 | wait->wait.private = req; | |
3372 | wait->wait.flags = 0; | |
3373 | INIT_LIST_HEAD(&wait->wait.entry); | |
3374 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3375 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3376 | kiocb->ki_waitq = wait; |
3b2a4439 | 3377 | return true; |
bcf5a063 JA |
3378 | } |
3379 | ||
aeab9506 | 3380 | static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
bcf5a063 JA |
3381 | { |
3382 | if (req->file->f_op->read_iter) | |
3383 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d | 3384 | else if (req->file->f_op->read) |
4017eb91 | 3385 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3386 | else |
3387 | return -EINVAL; | |
f67676d1 JA |
3388 | } |
3389 | ||
7db30437 ML |
3390 | static bool need_read_all(struct io_kiocb *req) |
3391 | { | |
3392 | return req->flags & REQ_F_ISREG || | |
3393 | S_ISBLK(file_inode(req->file)->i_mode); | |
3394 | } | |
3395 | ||
889fca73 | 3396 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3397 | { |
3398 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3399 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3400 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3401 | struct io_async_rw *rw = req->async_data; |
45d189c6 | 3402 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
cd658695 JA |
3403 | struct iov_iter_state __state, *state; |
3404 | ssize_t ret, ret2; | |
ff6165b2 | 3405 | |
2846c481 | 3406 | if (rw) { |
e8c2bc1f | 3407 | iter = &rw->iter; |
cd658695 JA |
3408 | state = &rw->iter_state; |
3409 | /* | |
3410 | * We come here from an earlier attempt, restore our state to | |
3411 | * match in case it doesn't. It's cheap enough that we don't | |
3412 | * need to make this conditional. | |
3413 | */ | |
3414 | iov_iter_restore(iter, state); | |
2846c481 PB |
3415 | iovec = NULL; |
3416 | } else { | |
3417 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); | |
3418 | if (ret < 0) | |
3419 | return ret; | |
cd658695 JA |
3420 | state = &__state; |
3421 | iov_iter_save_state(iter, state); | |
2846c481 | 3422 | } |
cd658695 | 3423 | req->result = iov_iter_count(iter); |
2b188cc1 | 3424 | |
fd6c2e4c JA |
3425 | /* Ensure we clear previously set non-block flag */ |
3426 | if (!force_nonblock) | |
29de5f6a | 3427 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
a88fc400 PB |
3428 | else |
3429 | kiocb->ki_flags |= IOCB_NOWAIT; | |
3430 | ||
24c74678 | 3431 | /* If the file doesn't support async, just async punt */ |
b191e2df | 3432 | if (force_nonblock && !io_file_supports_nowait(req, READ)) { |
6713e7a6 | 3433 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc | 3434 | return ret ?: -EAGAIN; |
6713e7a6 | 3435 | } |
9e645e11 | 3436 | |
cd658695 | 3437 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), req->result); |
5ea5dd45 PB |
3438 | if (unlikely(ret)) { |
3439 | kfree(iovec); | |
3440 | return ret; | |
3441 | } | |
2b188cc1 | 3442 | |
227c0c96 | 3443 | ret = io_iter_do_read(req, iter); |
32960613 | 3444 | |
230d50d4 | 3445 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
6ad7f233 | 3446 | req->flags &= ~REQ_F_REISSUE; |
eefdf30f JA |
3447 | /* IOPOLL retry should happen for io-wq threads */ |
3448 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3449 | goto done; |
75c668cd PB |
3450 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3451 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3452 | goto done; |
f38c7e3a | 3453 | ret = 0; |
230d50d4 JA |
3454 | } else if (ret == -EIOCBQUEUED) { |
3455 | goto out_free; | |
cd658695 | 3456 | } else if (ret <= 0 || ret == req->result || !force_nonblock || |
7db30437 | 3457 | (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) { |
7335e3bf | 3458 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3459 | goto done; |
227c0c96 JA |
3460 | } |
3461 | ||
cd658695 JA |
3462 | /* |
3463 | * Don't depend on the iter state matching what was consumed, or being | |
3464 | * untouched in case of error. Restore it and we'll advance it | |
3465 | * manually if we need to. | |
3466 | */ | |
3467 | iov_iter_restore(iter, state); | |
3468 | ||
227c0c96 | 3469 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc PB |
3470 | if (ret2) |
3471 | return ret2; | |
3472 | ||
fe1cdd55 | 3473 | iovec = NULL; |
e8c2bc1f | 3474 | rw = req->async_data; |
cd658695 JA |
3475 | /* |
3476 | * Now use our persistent iterator and state, if we aren't already. | |
3477 | * We've restored and mapped the iter to match. | |
3478 | */ | |
3479 | if (iter != &rw->iter) { | |
3480 | iter = &rw->iter; | |
3481 | state = &rw->iter_state; | |
3482 | } | |
227c0c96 | 3483 | |
b23df91b | 3484 | do { |
cd658695 JA |
3485 | /* |
3486 | * We end up here because of a partial read, either from | |
3487 | * above or inside this loop. Advance the iter by the bytes | |
3488 | * that were consumed. | |
3489 | */ | |
3490 | iov_iter_advance(iter, ret); | |
3491 | if (!iov_iter_count(iter)) | |
3492 | break; | |
b23df91b | 3493 | rw->bytes_done += ret; |
cd658695 JA |
3494 | iov_iter_save_state(iter, state); |
3495 | ||
b23df91b PB |
3496 | /* if we can retry, do so with the callbacks armed */ |
3497 | if (!io_rw_should_retry(req)) { | |
3498 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3499 | return -EAGAIN; | |
3500 | } | |
3501 | ||
3502 | /* | |
3503 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3504 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3505 | * desired page gets unlocked. We can also get a partial read | |
3506 | * here, and if we do, then just retry at the new offset. | |
3507 | */ | |
3508 | ret = io_iter_do_read(req, iter); | |
3509 | if (ret == -EIOCBQUEUED) | |
3510 | return 0; | |
227c0c96 | 3511 | /* we got some bytes, but not all. retry. */ |
b5b0ecb7 | 3512 | kiocb->ki_flags &= ~IOCB_WAITQ; |
cd658695 JA |
3513 | iov_iter_restore(iter, state); |
3514 | } while (ret > 0); | |
227c0c96 | 3515 | done: |
889fca73 | 3516 | kiocb_done(kiocb, ret, issue_flags); |
fe1cdd55 PB |
3517 | out_free: |
3518 | /* it's faster to check here then delegate to kfree */ | |
3519 | if (iovec) | |
3520 | kfree(iovec); | |
5ea5dd45 | 3521 | return 0; |
2b188cc1 JA |
3522 | } |
3523 | ||
73debe68 | 3524 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3525 | { |
3529d8c2 JA |
3526 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3527 | return -EBADF; | |
5d329e12 | 3528 | return io_prep_rw(req, sqe, WRITE); |
f67676d1 JA |
3529 | } |
3530 | ||
889fca73 | 3531 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3532 | { |
3533 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3534 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3535 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3536 | struct io_async_rw *rw = req->async_data; |
45d189c6 | 3537 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
cd658695 JA |
3538 | struct iov_iter_state __state, *state; |
3539 | ssize_t ret, ret2; | |
2b188cc1 | 3540 | |
2846c481 | 3541 | if (rw) { |
e8c2bc1f | 3542 | iter = &rw->iter; |
cd658695 JA |
3543 | state = &rw->iter_state; |
3544 | iov_iter_restore(iter, state); | |
2846c481 PB |
3545 | iovec = NULL; |
3546 | } else { | |
3547 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
3548 | if (ret < 0) | |
3549 | return ret; | |
cd658695 JA |
3550 | state = &__state; |
3551 | iov_iter_save_state(iter, state); | |
2846c481 | 3552 | } |
cd658695 | 3553 | req->result = iov_iter_count(iter); |
2b188cc1 | 3554 | |
fd6c2e4c JA |
3555 | /* Ensure we clear previously set non-block flag */ |
3556 | if (!force_nonblock) | |
a88fc400 PB |
3557 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3558 | else | |
3559 | kiocb->ki_flags |= IOCB_NOWAIT; | |
fd6c2e4c | 3560 | |
24c74678 | 3561 | /* If the file doesn't support async, just async punt */ |
b191e2df | 3562 | if (force_nonblock && !io_file_supports_nowait(req, WRITE)) |
f67676d1 | 3563 | goto copy_iov; |
31b51510 | 3564 | |
10d59345 JA |
3565 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3566 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3567 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3568 | goto copy_iov; |
31b51510 | 3569 | |
cd658695 | 3570 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), req->result); |
fa15bafb PB |
3571 | if (unlikely(ret)) |
3572 | goto out_free; | |
4ed734b0 | 3573 | |
fa15bafb PB |
3574 | /* |
3575 | * Open-code file_start_write here to grab freeze protection, | |
3576 | * which will be released by another thread in | |
3577 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3578 | * released so that it doesn't complain about the held lock when | |
3579 | * we return to userspace. | |
3580 | */ | |
3581 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3582 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3583 | __sb_writers_release(file_inode(req->file)->i_sb, |
3584 | SB_FREEZE_WRITE); | |
3585 | } | |
3586 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3587 | |
fa15bafb | 3588 | if (req->file->f_op->write_iter) |
ff6165b2 | 3589 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3590 | else if (req->file->f_op->write) |
4017eb91 | 3591 | ret2 = loop_rw_iter(WRITE, req, iter); |
2dd2111d GH |
3592 | else |
3593 | ret2 = -EINVAL; | |
4ed734b0 | 3594 | |
6ad7f233 PB |
3595 | if (req->flags & REQ_F_REISSUE) { |
3596 | req->flags &= ~REQ_F_REISSUE; | |
230d50d4 | 3597 | ret2 = -EAGAIN; |
6ad7f233 | 3598 | } |
230d50d4 | 3599 | |
fa15bafb PB |
3600 | /* |
3601 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3602 | * retry them without IOCB_NOWAIT. | |
3603 | */ | |
3604 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3605 | ret2 = -EAGAIN; | |
75c668cd PB |
3606 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3607 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3608 | goto done; |
fa15bafb | 3609 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3610 | /* IOPOLL retry should happen for io-wq threads */ |
3611 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3612 | goto copy_iov; | |
355afaeb | 3613 | done: |
889fca73 | 3614 | kiocb_done(kiocb, ret2, issue_flags); |
fa15bafb | 3615 | } else { |
f67676d1 | 3616 | copy_iov: |
cd658695 | 3617 | iov_iter_restore(iter, state); |
227c0c96 | 3618 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
6bf985dc | 3619 | return ret ?: -EAGAIN; |
2b188cc1 | 3620 | } |
31b51510 | 3621 | out_free: |
f261c168 | 3622 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3623 | if (iovec) |
6f2cc166 | 3624 | kfree(iovec); |
2b188cc1 JA |
3625 | return ret; |
3626 | } | |
3627 | ||
80a261fd JA |
3628 | static int io_renameat_prep(struct io_kiocb *req, |
3629 | const struct io_uring_sqe *sqe) | |
3630 | { | |
3631 | struct io_rename *ren = &req->rename; | |
3632 | const char __user *oldf, *newf; | |
3633 | ||
ed7eb259 JA |
3634 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3635 | return -EINVAL; | |
26578cda | 3636 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
ed7eb259 | 3637 | return -EINVAL; |
80a261fd JA |
3638 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3639 | return -EBADF; | |
3640 | ||
3641 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3642 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3643 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3644 | ren->new_dfd = READ_ONCE(sqe->len); | |
3645 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3646 | ||
3647 | ren->oldpath = getname(oldf); | |
3648 | if (IS_ERR(ren->oldpath)) | |
3649 | return PTR_ERR(ren->oldpath); | |
3650 | ||
3651 | ren->newpath = getname(newf); | |
3652 | if (IS_ERR(ren->newpath)) { | |
3653 | putname(ren->oldpath); | |
3654 | return PTR_ERR(ren->newpath); | |
3655 | } | |
3656 | ||
3657 | req->flags |= REQ_F_NEED_CLEANUP; | |
3658 | return 0; | |
3659 | } | |
3660 | ||
45d189c6 | 3661 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
3662 | { |
3663 | struct io_rename *ren = &req->rename; | |
3664 | int ret; | |
3665 | ||
45d189c6 | 3666 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
3667 | return -EAGAIN; |
3668 | ||
3669 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3670 | ren->newpath, ren->flags); | |
3671 | ||
3672 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3673 | if (ret < 0) | |
93d2bcd2 | 3674 | req_set_fail(req); |
80a261fd JA |
3675 | io_req_complete(req, ret); |
3676 | return 0; | |
3677 | } | |
3678 | ||
14a1143b JA |
3679 | static int io_unlinkat_prep(struct io_kiocb *req, |
3680 | const struct io_uring_sqe *sqe) | |
3681 | { | |
3682 | struct io_unlink *un = &req->unlink; | |
3683 | const char __user *fname; | |
3684 | ||
22634bc5 JA |
3685 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3686 | return -EINVAL; | |
26578cda PB |
3687 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
3688 | sqe->splice_fd_in) | |
22634bc5 | 3689 | return -EINVAL; |
14a1143b JA |
3690 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3691 | return -EBADF; | |
3692 | ||
3693 | un->dfd = READ_ONCE(sqe->fd); | |
3694 | ||
3695 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3696 | if (un->flags & ~AT_REMOVEDIR) | |
3697 | return -EINVAL; | |
3698 | ||
3699 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3700 | un->filename = getname(fname); | |
3701 | if (IS_ERR(un->filename)) | |
3702 | return PTR_ERR(un->filename); | |
3703 | ||
3704 | req->flags |= REQ_F_NEED_CLEANUP; | |
3705 | return 0; | |
3706 | } | |
3707 | ||
45d189c6 | 3708 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
3709 | { |
3710 | struct io_unlink *un = &req->unlink; | |
3711 | int ret; | |
3712 | ||
45d189c6 | 3713 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
3714 | return -EAGAIN; |
3715 | ||
3716 | if (un->flags & AT_REMOVEDIR) | |
3717 | ret = do_rmdir(un->dfd, un->filename); | |
3718 | else | |
3719 | ret = do_unlinkat(un->dfd, un->filename); | |
3720 | ||
3721 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3722 | if (ret < 0) | |
93d2bcd2 | 3723 | req_set_fail(req); |
14a1143b JA |
3724 | io_req_complete(req, ret); |
3725 | return 0; | |
3726 | } | |
3727 | ||
e34a02dc DK |
3728 | static int io_mkdirat_prep(struct io_kiocb *req, |
3729 | const struct io_uring_sqe *sqe) | |
3730 | { | |
3731 | struct io_mkdir *mkd = &req->mkdir; | |
3732 | const char __user *fname; | |
3733 | ||
3734 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3735 | return -EINVAL; | |
3736 | if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index || | |
3737 | sqe->splice_fd_in) | |
3738 | return -EINVAL; | |
3739 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3740 | return -EBADF; | |
3741 | ||
3742 | mkd->dfd = READ_ONCE(sqe->fd); | |
3743 | mkd->mode = READ_ONCE(sqe->len); | |
3744 | ||
3745 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3746 | mkd->filename = getname(fname); | |
3747 | if (IS_ERR(mkd->filename)) | |
3748 | return PTR_ERR(mkd->filename); | |
3749 | ||
3750 | req->flags |= REQ_F_NEED_CLEANUP; | |
3751 | return 0; | |
3752 | } | |
3753 | ||
3754 | static int io_mkdirat(struct io_kiocb *req, int issue_flags) | |
3755 | { | |
3756 | struct io_mkdir *mkd = &req->mkdir; | |
3757 | int ret; | |
3758 | ||
3759 | if (issue_flags & IO_URING_F_NONBLOCK) | |
3760 | return -EAGAIN; | |
3761 | ||
3762 | ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode); | |
3763 | ||
3764 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3765 | if (ret < 0) | |
3766 | req_set_fail(req); | |
3767 | io_req_complete(req, ret); | |
3768 | return 0; | |
3769 | } | |
3770 | ||
7a8721f8 DK |
3771 | static int io_symlinkat_prep(struct io_kiocb *req, |
3772 | const struct io_uring_sqe *sqe) | |
3773 | { | |
3774 | struct io_symlink *sl = &req->symlink; | |
3775 | const char __user *oldpath, *newpath; | |
3776 | ||
3777 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3778 | return -EINVAL; | |
3779 | if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index || | |
3780 | sqe->splice_fd_in) | |
3781 | return -EINVAL; | |
3782 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3783 | return -EBADF; | |
3784 | ||
3785 | sl->new_dfd = READ_ONCE(sqe->fd); | |
3786 | oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3787 | newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3788 | ||
3789 | sl->oldpath = getname(oldpath); | |
3790 | if (IS_ERR(sl->oldpath)) | |
3791 | return PTR_ERR(sl->oldpath); | |
3792 | ||
3793 | sl->newpath = getname(newpath); | |
3794 | if (IS_ERR(sl->newpath)) { | |
3795 | putname(sl->oldpath); | |
3796 | return PTR_ERR(sl->newpath); | |
3797 | } | |
3798 | ||
3799 | req->flags |= REQ_F_NEED_CLEANUP; | |
3800 | return 0; | |
3801 | } | |
3802 | ||
3803 | static int io_symlinkat(struct io_kiocb *req, int issue_flags) | |
3804 | { | |
3805 | struct io_symlink *sl = &req->symlink; | |
3806 | int ret; | |
3807 | ||
3808 | if (issue_flags & IO_URING_F_NONBLOCK) | |
3809 | return -EAGAIN; | |
3810 | ||
3811 | ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath); | |
3812 | ||
3813 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3814 | if (ret < 0) | |
3815 | req_set_fail(req); | |
3816 | io_req_complete(req, ret); | |
3817 | return 0; | |
3818 | } | |
3819 | ||
cf30da90 DK |
3820 | static int io_linkat_prep(struct io_kiocb *req, |
3821 | const struct io_uring_sqe *sqe) | |
3822 | { | |
3823 | struct io_hardlink *lnk = &req->hardlink; | |
3824 | const char __user *oldf, *newf; | |
3825 | ||
3826 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3827 | return -EINVAL; | |
3828 | if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) | |
3829 | return -EINVAL; | |
3830 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3831 | return -EBADF; | |
3832 | ||
3833 | lnk->old_dfd = READ_ONCE(sqe->fd); | |
3834 | lnk->new_dfd = READ_ONCE(sqe->len); | |
3835 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3836 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3837 | lnk->flags = READ_ONCE(sqe->hardlink_flags); | |
3838 | ||
3839 | lnk->oldpath = getname(oldf); | |
3840 | if (IS_ERR(lnk->oldpath)) | |
3841 | return PTR_ERR(lnk->oldpath); | |
3842 | ||
3843 | lnk->newpath = getname(newf); | |
3844 | if (IS_ERR(lnk->newpath)) { | |
3845 | putname(lnk->oldpath); | |
3846 | return PTR_ERR(lnk->newpath); | |
3847 | } | |
3848 | ||
3849 | req->flags |= REQ_F_NEED_CLEANUP; | |
3850 | return 0; | |
3851 | } | |
3852 | ||
3853 | static int io_linkat(struct io_kiocb *req, int issue_flags) | |
3854 | { | |
3855 | struct io_hardlink *lnk = &req->hardlink; | |
3856 | int ret; | |
3857 | ||
3858 | if (issue_flags & IO_URING_F_NONBLOCK) | |
3859 | return -EAGAIN; | |
3860 | ||
3861 | ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd, | |
3862 | lnk->newpath, lnk->flags); | |
3863 | ||
3864 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3865 | if (ret < 0) | |
3866 | req_set_fail(req); | |
3867 | io_req_complete(req, ret); | |
3868 | return 0; | |
3869 | } | |
3870 | ||
36f4fa68 JA |
3871 | static int io_shutdown_prep(struct io_kiocb *req, |
3872 | const struct io_uring_sqe *sqe) | |
3873 | { | |
3874 | #if defined(CONFIG_NET) | |
3875 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3876 | return -EINVAL; | |
26578cda PB |
3877 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
3878 | sqe->buf_index || sqe->splice_fd_in)) | |
36f4fa68 JA |
3879 | return -EINVAL; |
3880 | ||
3881 | req->shutdown.how = READ_ONCE(sqe->len); | |
3882 | return 0; | |
3883 | #else | |
3884 | return -EOPNOTSUPP; | |
3885 | #endif | |
3886 | } | |
3887 | ||
45d189c6 | 3888 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
3889 | { |
3890 | #if defined(CONFIG_NET) | |
3891 | struct socket *sock; | |
3892 | int ret; | |
3893 | ||
45d189c6 | 3894 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
3895 | return -EAGAIN; |
3896 | ||
48aba79b | 3897 | sock = sock_from_file(req->file); |
36f4fa68 | 3898 | if (unlikely(!sock)) |
48aba79b | 3899 | return -ENOTSOCK; |
36f4fa68 JA |
3900 | |
3901 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d | 3902 | if (ret < 0) |
93d2bcd2 | 3903 | req_set_fail(req); |
36f4fa68 JA |
3904 | io_req_complete(req, ret); |
3905 | return 0; | |
3906 | #else | |
3907 | return -EOPNOTSUPP; | |
3908 | #endif | |
3909 | } | |
3910 | ||
f2a8d5c7 PB |
3911 | static int __io_splice_prep(struct io_kiocb *req, |
3912 | const struct io_uring_sqe *sqe) | |
7d67af2c | 3913 | { |
fe7e3257 | 3914 | struct io_splice *sp = &req->splice; |
7d67af2c | 3915 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
7d67af2c | 3916 | |
3232dd02 PB |
3917 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3918 | return -EINVAL; | |
7d67af2c PB |
3919 | |
3920 | sp->file_in = NULL; | |
7d67af2c PB |
3921 | sp->len = READ_ONCE(sqe->len); |
3922 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3923 | ||
3924 | if (unlikely(sp->flags & ~valid_flags)) | |
3925 | return -EINVAL; | |
3926 | ||
62906e89 | 3927 | sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in), |
8371adf5 PB |
3928 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
3929 | if (!sp->file_in) | |
3930 | return -EBADF; | |
7d67af2c | 3931 | req->flags |= REQ_F_NEED_CLEANUP; |
7d67af2c PB |
3932 | return 0; |
3933 | } | |
3934 | ||
f2a8d5c7 PB |
3935 | static int io_tee_prep(struct io_kiocb *req, |
3936 | const struct io_uring_sqe *sqe) | |
3937 | { | |
3938 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3939 | return -EINVAL; | |
3940 | return __io_splice_prep(req, sqe); | |
3941 | } | |
3942 | ||
45d189c6 | 3943 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
3944 | { |
3945 | struct io_splice *sp = &req->splice; | |
3946 | struct file *in = sp->file_in; | |
3947 | struct file *out = sp->file_out; | |
3948 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3949 | long ret = 0; | |
3950 | ||
45d189c6 | 3951 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
3952 | return -EAGAIN; |
3953 | if (sp->len) | |
3954 | ret = do_tee(in, out, sp->len, flags); | |
3955 | ||
e1d767f0 PB |
3956 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
3957 | io_put_file(in); | |
f2a8d5c7 PB |
3958 | req->flags &= ~REQ_F_NEED_CLEANUP; |
3959 | ||
f2a8d5c7 | 3960 | if (ret != sp->len) |
93d2bcd2 | 3961 | req_set_fail(req); |
e1e16097 | 3962 | io_req_complete(req, ret); |
f2a8d5c7 PB |
3963 | return 0; |
3964 | } | |
3965 | ||
3966 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
3967 | { | |
fe7e3257 | 3968 | struct io_splice *sp = &req->splice; |
f2a8d5c7 PB |
3969 | |
3970 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
3971 | sp->off_out = READ_ONCE(sqe->off); | |
3972 | return __io_splice_prep(req, sqe); | |
3973 | } | |
3974 | ||
45d189c6 | 3975 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
3976 | { |
3977 | struct io_splice *sp = &req->splice; | |
3978 | struct file *in = sp->file_in; | |
3979 | struct file *out = sp->file_out; | |
3980 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3981 | loff_t *poff_in, *poff_out; | |
c9687426 | 3982 | long ret = 0; |
7d67af2c | 3983 | |
45d189c6 | 3984 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 3985 | return -EAGAIN; |
7d67af2c PB |
3986 | |
3987 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
3988 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 3989 | |
948a7749 | 3990 | if (sp->len) |
c9687426 | 3991 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c | 3992 | |
e1d767f0 PB |
3993 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
3994 | io_put_file(in); | |
7d67af2c PB |
3995 | req->flags &= ~REQ_F_NEED_CLEANUP; |
3996 | ||
7d67af2c | 3997 | if (ret != sp->len) |
93d2bcd2 | 3998 | req_set_fail(req); |
e1e16097 | 3999 | io_req_complete(req, ret); |
7d67af2c PB |
4000 | return 0; |
4001 | } | |
4002 | ||
2b188cc1 JA |
4003 | /* |
4004 | * IORING_OP_NOP just posts a completion event, nothing else. | |
4005 | */ | |
889fca73 | 4006 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
4007 | { |
4008 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 4009 | |
def596e9 JA |
4010 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4011 | return -EINVAL; | |
4012 | ||
889fca73 | 4013 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
4014 | return 0; |
4015 | } | |
4016 | ||
1155c76a | 4017 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 4018 | { |
6b06314c | 4019 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 4020 | |
09bb8394 JA |
4021 | if (!req->file) |
4022 | return -EBADF; | |
c992fe29 | 4023 | |
6b06314c | 4024 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 4025 | return -EINVAL; |
26578cda PB |
4026 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4027 | sqe->splice_fd_in)) | |
c992fe29 CH |
4028 | return -EINVAL; |
4029 | ||
8ed8d3c3 JA |
4030 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
4031 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
4032 | return -EINVAL; | |
4033 | ||
4034 | req->sync.off = READ_ONCE(sqe->off); | |
4035 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
4036 | return 0; |
4037 | } | |
4038 | ||
45d189c6 | 4039 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4040 | { |
8ed8d3c3 | 4041 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
4042 | int ret; |
4043 | ||
ac45abc0 | 4044 | /* fsync always requires a blocking context */ |
45d189c6 | 4045 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4046 | return -EAGAIN; |
4047 | ||
9adbd45d | 4048 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
4049 | end > 0 ? end : LLONG_MAX, |
4050 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
4051 | if (ret < 0) | |
93d2bcd2 | 4052 | req_set_fail(req); |
e1e16097 | 4053 | io_req_complete(req, ret); |
c992fe29 CH |
4054 | return 0; |
4055 | } | |
4056 | ||
d63d1b5e JA |
4057 | static int io_fallocate_prep(struct io_kiocb *req, |
4058 | const struct io_uring_sqe *sqe) | |
4059 | { | |
26578cda PB |
4060 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
4061 | sqe->splice_fd_in) | |
d63d1b5e | 4062 | return -EINVAL; |
3232dd02 PB |
4063 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4064 | return -EINVAL; | |
d63d1b5e JA |
4065 | |
4066 | req->sync.off = READ_ONCE(sqe->off); | |
4067 | req->sync.len = READ_ONCE(sqe->addr); | |
4068 | req->sync.mode = READ_ONCE(sqe->len); | |
4069 | return 0; | |
4070 | } | |
4071 | ||
45d189c6 | 4072 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 4073 | { |
ac45abc0 PB |
4074 | int ret; |
4075 | ||
d63d1b5e | 4076 | /* fallocate always requiring blocking context */ |
45d189c6 | 4077 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 4078 | return -EAGAIN; |
ac45abc0 PB |
4079 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
4080 | req->sync.len); | |
ac45abc0 | 4081 | if (ret < 0) |
93d2bcd2 | 4082 | req_set_fail(req); |
e1e16097 | 4083 | io_req_complete(req, ret); |
5d17b4a4 JA |
4084 | return 0; |
4085 | } | |
4086 | ||
ec65fea5 | 4087 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 4088 | { |
f8748881 | 4089 | const char __user *fname; |
15b71abe | 4090 | int ret; |
b7bb4f7d | 4091 | |
d3fddf6d PB |
4092 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4093 | return -EINVAL; | |
b9445598 | 4094 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 4095 | return -EINVAL; |
ec65fea5 | 4096 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 4097 | return -EBADF; |
03b1230c | 4098 | |
ec65fea5 PB |
4099 | /* open.how should be already initialised */ |
4100 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 4101 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 4102 | |
25e72d10 PB |
4103 | req->open.dfd = READ_ONCE(sqe->fd); |
4104 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 4105 | req->open.filename = getname(fname); |
15b71abe JA |
4106 | if (IS_ERR(req->open.filename)) { |
4107 | ret = PTR_ERR(req->open.filename); | |
4108 | req->open.filename = NULL; | |
4109 | return ret; | |
4110 | } | |
b9445598 PB |
4111 | |
4112 | req->open.file_slot = READ_ONCE(sqe->file_index); | |
4113 | if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) | |
4114 | return -EINVAL; | |
4115 | ||
4022e7af | 4116 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 4117 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 4118 | return 0; |
03b1230c JA |
4119 | } |
4120 | ||
ec65fea5 PB |
4121 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4122 | { | |
d3fddf6d PB |
4123 | u64 mode = READ_ONCE(sqe->len); |
4124 | u64 flags = READ_ONCE(sqe->open_flags); | |
ec65fea5 | 4125 | |
ec65fea5 PB |
4126 | req->open.how = build_open_how(flags, mode); |
4127 | return __io_openat_prep(req, sqe); | |
4128 | } | |
4129 | ||
cebdb986 | 4130 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 4131 | { |
cebdb986 | 4132 | struct open_how __user *how; |
cebdb986 | 4133 | size_t len; |
0fa03c62 JA |
4134 | int ret; |
4135 | ||
cebdb986 JA |
4136 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4137 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
4138 | if (len < OPEN_HOW_SIZE_VER0) |
4139 | return -EINVAL; | |
3529d8c2 | 4140 | |
cebdb986 JA |
4141 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
4142 | len); | |
4143 | if (ret) | |
4144 | return ret; | |
3529d8c2 | 4145 | |
ec65fea5 | 4146 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
4147 | } |
4148 | ||
45d189c6 | 4149 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
4150 | { |
4151 | struct open_flags op; | |
15b71abe | 4152 | struct file *file; |
b9445598 PB |
4153 | bool resolve_nonblock, nonblock_set; |
4154 | bool fixed = !!req->open.file_slot; | |
15b71abe JA |
4155 | int ret; |
4156 | ||
cebdb986 | 4157 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
4158 | if (ret) |
4159 | goto err; | |
3a81fd02 JA |
4160 | nonblock_set = op.open_flag & O_NONBLOCK; |
4161 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 4162 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
4163 | /* |
4164 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
4165 | * it'll always -EAGAIN | |
4166 | */ | |
4167 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
4168 | return -EAGAIN; | |
4169 | op.lookup_flags |= LOOKUP_CACHED; | |
4170 | op.open_flag |= O_NONBLOCK; | |
4171 | } | |
15b71abe | 4172 | |
b9445598 PB |
4173 | if (!fixed) { |
4174 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); | |
4175 | if (ret < 0) | |
4176 | goto err; | |
4177 | } | |
15b71abe JA |
4178 | |
4179 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
12dcb58a | 4180 | if (IS_ERR(file)) { |
944d1444 | 4181 | /* |
12dcb58a PB |
4182 | * We could hang on to this 'fd' on retrying, but seems like |
4183 | * marginal gain for something that is now known to be a slower | |
4184 | * path. So just put it, and we'll get a new one when we retry. | |
944d1444 | 4185 | */ |
b9445598 PB |
4186 | if (!fixed) |
4187 | put_unused_fd(ret); | |
3a81fd02 | 4188 | |
15b71abe | 4189 | ret = PTR_ERR(file); |
12dcb58a PB |
4190 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
4191 | if (ret == -EAGAIN && | |
4192 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) | |
4193 | return -EAGAIN; | |
4194 | goto err; | |
15b71abe | 4195 | } |
12dcb58a PB |
4196 | |
4197 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) | |
4198 | file->f_flags &= ~O_NONBLOCK; | |
4199 | fsnotify_open(file); | |
b9445598 PB |
4200 | |
4201 | if (!fixed) | |
4202 | fd_install(ret, file); | |
4203 | else | |
4204 | ret = io_install_fixed_file(req, file, issue_flags, | |
4205 | req->open.file_slot - 1); | |
15b71abe JA |
4206 | err: |
4207 | putname(req->open.filename); | |
8fef80bf | 4208 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe | 4209 | if (ret < 0) |
93d2bcd2 | 4210 | req_set_fail(req); |
0bdf3398 | 4211 | __io_req_complete(req, issue_flags, ret, 0); |
15b71abe JA |
4212 | return 0; |
4213 | } | |
4214 | ||
45d189c6 | 4215 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 4216 | { |
e45cff58 | 4217 | return io_openat2(req, issue_flags); |
cebdb986 JA |
4218 | } |
4219 | ||
067524e9 JA |
4220 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4221 | const struct io_uring_sqe *sqe) | |
4222 | { | |
4223 | struct io_provide_buf *p = &req->pbuf; | |
4224 | u64 tmp; | |
4225 | ||
26578cda PB |
4226 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
4227 | sqe->splice_fd_in) | |
067524e9 JA |
4228 | return -EINVAL; |
4229 | ||
4230 | tmp = READ_ONCE(sqe->fd); | |
4231 | if (!tmp || tmp > USHRT_MAX) | |
4232 | return -EINVAL; | |
4233 | ||
4234 | memset(p, 0, sizeof(*p)); | |
4235 | p->nbufs = tmp; | |
4236 | p->bgid = READ_ONCE(sqe->buf_group); | |
4237 | return 0; | |
4238 | } | |
4239 | ||
4240 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
4241 | int bgid, unsigned nbufs) | |
4242 | { | |
4243 | unsigned i = 0; | |
4244 | ||
4245 | /* shouldn't happen */ | |
4246 | if (!nbufs) | |
4247 | return 0; | |
4248 | ||
4249 | /* the head kbuf is the list itself */ | |
4250 | while (!list_empty(&buf->list)) { | |
4251 | struct io_buffer *nxt; | |
4252 | ||
4253 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
4254 | list_del(&nxt->list); | |
4255 | kfree(nxt); | |
4256 | if (++i == nbufs) | |
4257 | return i; | |
4258 | } | |
4259 | i++; | |
4260 | kfree(buf); | |
9e15c3a0 | 4261 | xa_erase(&ctx->io_buffers, bgid); |
067524e9 JA |
4262 | |
4263 | return i; | |
4264 | } | |
4265 | ||
889fca73 | 4266 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
4267 | { |
4268 | struct io_provide_buf *p = &req->pbuf; | |
4269 | struct io_ring_ctx *ctx = req->ctx; | |
4270 | struct io_buffer *head; | |
4271 | int ret = 0; | |
45d189c6 | 4272 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
067524e9 JA |
4273 | |
4274 | io_ring_submit_lock(ctx, !force_nonblock); | |
4275 | ||
4276 | lockdep_assert_held(&ctx->uring_lock); | |
4277 | ||
4278 | ret = -ENOENT; | |
9e15c3a0 | 4279 | head = xa_load(&ctx->io_buffers, p->bgid); |
067524e9 JA |
4280 | if (head) |
4281 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 | 4282 | if (ret < 0) |
93d2bcd2 | 4283 | req_set_fail(req); |
067524e9 | 4284 | |
9fb8cb49 PB |
4285 | /* complete before unlock, IOPOLL may need the lock */ |
4286 | __io_req_complete(req, issue_flags, ret, 0); | |
4287 | io_ring_submit_unlock(ctx, !force_nonblock); | |
067524e9 JA |
4288 | return 0; |
4289 | } | |
4290 | ||
ddf0322d JA |
4291 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4292 | const struct io_uring_sqe *sqe) | |
4293 | { | |
38134ada | 4294 | unsigned long size, tmp_check; |
ddf0322d JA |
4295 | struct io_provide_buf *p = &req->pbuf; |
4296 | u64 tmp; | |
4297 | ||
26578cda | 4298 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
ddf0322d JA |
4299 | return -EINVAL; |
4300 | ||
4301 | tmp = READ_ONCE(sqe->fd); | |
4302 | if (!tmp || tmp > USHRT_MAX) | |
4303 | return -E2BIG; | |
4304 | p->nbufs = tmp; | |
4305 | p->addr = READ_ONCE(sqe->addr); | |
4306 | p->len = READ_ONCE(sqe->len); | |
4307 | ||
38134ada PB |
4308 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
4309 | &size)) | |
4310 | return -EOVERFLOW; | |
4311 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) | |
4312 | return -EOVERFLOW; | |
4313 | ||
d81269fe PB |
4314 | size = (unsigned long)p->len * p->nbufs; |
4315 | if (!access_ok(u64_to_user_ptr(p->addr), size)) | |
ddf0322d JA |
4316 | return -EFAULT; |
4317 | ||
4318 | p->bgid = READ_ONCE(sqe->buf_group); | |
4319 | tmp = READ_ONCE(sqe->off); | |
4320 | if (tmp > USHRT_MAX) | |
4321 | return -E2BIG; | |
4322 | p->bid = tmp; | |
4323 | return 0; | |
4324 | } | |
4325 | ||
4326 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
4327 | { | |
4328 | struct io_buffer *buf; | |
4329 | u64 addr = pbuf->addr; | |
4330 | int i, bid = pbuf->bid; | |
4331 | ||
4332 | for (i = 0; i < pbuf->nbufs; i++) { | |
9990da93 | 4333 | buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT); |
ddf0322d JA |
4334 | if (!buf) |
4335 | break; | |
4336 | ||
4337 | buf->addr = addr; | |
d1f82808 | 4338 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
ddf0322d JA |
4339 | buf->bid = bid; |
4340 | addr += pbuf->len; | |
4341 | bid++; | |
4342 | if (!*head) { | |
4343 | INIT_LIST_HEAD(&buf->list); | |
4344 | *head = buf; | |
4345 | } else { | |
4346 | list_add_tail(&buf->list, &(*head)->list); | |
4347 | } | |
4348 | } | |
4349 | ||
4350 | return i ? i : -ENOMEM; | |
4351 | } | |
4352 | ||
889fca73 | 4353 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4354 | { |
4355 | struct io_provide_buf *p = &req->pbuf; | |
4356 | struct io_ring_ctx *ctx = req->ctx; | |
4357 | struct io_buffer *head, *list; | |
4358 | int ret = 0; | |
45d189c6 | 4359 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ddf0322d JA |
4360 | |
4361 | io_ring_submit_lock(ctx, !force_nonblock); | |
4362 | ||
4363 | lockdep_assert_held(&ctx->uring_lock); | |
4364 | ||
9e15c3a0 | 4365 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
ddf0322d JA |
4366 | |
4367 | ret = io_add_buffers(p, &head); | |
9e15c3a0 JA |
4368 | if (ret >= 0 && !list) { |
4369 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); | |
4370 | if (ret < 0) | |
067524e9 | 4371 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d | 4372 | } |
ddf0322d | 4373 | if (ret < 0) |
93d2bcd2 | 4374 | req_set_fail(req); |
9fb8cb49 PB |
4375 | /* complete before unlock, IOPOLL may need the lock */ |
4376 | __io_req_complete(req, issue_flags, ret, 0); | |
4377 | io_ring_submit_unlock(ctx, !force_nonblock); | |
ddf0322d | 4378 | return 0; |
cebdb986 JA |
4379 | } |
4380 | ||
3e4827b0 JA |
4381 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4382 | const struct io_uring_sqe *sqe) | |
4383 | { | |
4384 | #if defined(CONFIG_EPOLL) | |
26578cda | 4385 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
3e4827b0 | 4386 | return -EINVAL; |
2d74d042 | 4387 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4388 | return -EINVAL; |
3e4827b0 JA |
4389 | |
4390 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4391 | req->epoll.op = READ_ONCE(sqe->len); | |
4392 | req->epoll.fd = READ_ONCE(sqe->off); | |
4393 | ||
4394 | if (ep_op_has_event(req->epoll.op)) { | |
4395 | struct epoll_event __user *ev; | |
4396 | ||
4397 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4398 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4399 | return -EFAULT; | |
4400 | } | |
4401 | ||
4402 | return 0; | |
4403 | #else | |
4404 | return -EOPNOTSUPP; | |
4405 | #endif | |
4406 | } | |
4407 | ||
889fca73 | 4408 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4409 | { |
4410 | #if defined(CONFIG_EPOLL) | |
4411 | struct io_epoll *ie = &req->epoll; | |
4412 | int ret; | |
45d189c6 | 4413 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4414 | |
4415 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4416 | if (force_nonblock && ret == -EAGAIN) | |
4417 | return -EAGAIN; | |
4418 | ||
4419 | if (ret < 0) | |
93d2bcd2 | 4420 | req_set_fail(req); |
889fca73 | 4421 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4422 | return 0; |
4423 | #else | |
4424 | return -EOPNOTSUPP; | |
4425 | #endif | |
4426 | } | |
4427 | ||
c1ca757b JA |
4428 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4429 | { | |
4430 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
26578cda | 4431 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
c1ca757b | 4432 | return -EINVAL; |
3232dd02 PB |
4433 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4434 | return -EINVAL; | |
c1ca757b JA |
4435 | |
4436 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4437 | req->madvise.len = READ_ONCE(sqe->len); | |
4438 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4439 | return 0; | |
4440 | #else | |
4441 | return -EOPNOTSUPP; | |
4442 | #endif | |
4443 | } | |
4444 | ||
45d189c6 | 4445 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4446 | { |
4447 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4448 | struct io_madvise *ma = &req->madvise; | |
4449 | int ret; | |
4450 | ||
45d189c6 | 4451 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4452 | return -EAGAIN; |
4453 | ||
0726b01e | 4454 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b | 4455 | if (ret < 0) |
93d2bcd2 | 4456 | req_set_fail(req); |
e1e16097 | 4457 | io_req_complete(req, ret); |
c1ca757b JA |
4458 | return 0; |
4459 | #else | |
4460 | return -EOPNOTSUPP; | |
4461 | #endif | |
4462 | } | |
4463 | ||
4840e418 JA |
4464 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4465 | { | |
26578cda | 4466 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
4840e418 | 4467 | return -EINVAL; |
3232dd02 PB |
4468 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4469 | return -EINVAL; | |
4840e418 JA |
4470 | |
4471 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4472 | req->fadvise.len = READ_ONCE(sqe->len); | |
4473 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4474 | return 0; | |
4475 | } | |
4476 | ||
45d189c6 | 4477 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4478 | { |
4479 | struct io_fadvise *fa = &req->fadvise; | |
4480 | int ret; | |
4481 | ||
45d189c6 | 4482 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4483 | switch (fa->advice) { |
4484 | case POSIX_FADV_NORMAL: | |
4485 | case POSIX_FADV_RANDOM: | |
4486 | case POSIX_FADV_SEQUENTIAL: | |
4487 | break; | |
4488 | default: | |
4489 | return -EAGAIN; | |
4490 | } | |
4491 | } | |
4840e418 JA |
4492 | |
4493 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4494 | if (ret < 0) | |
93d2bcd2 | 4495 | req_set_fail(req); |
0bdf3398 | 4496 | __io_req_complete(req, issue_flags, ret, 0); |
4840e418 JA |
4497 | return 0; |
4498 | } | |
4499 | ||
eddc7ef5 JA |
4500 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4501 | { | |
2d74d042 | 4502 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4503 | return -EINVAL; |
26578cda | 4504 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
eddc7ef5 | 4505 | return -EINVAL; |
9c280f90 | 4506 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4507 | return -EBADF; |
eddc7ef5 | 4508 | |
1d9e1288 BM |
4509 | req->statx.dfd = READ_ONCE(sqe->fd); |
4510 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4511 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4512 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4513 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4514 | |
4515 | return 0; | |
4516 | } | |
4517 | ||
45d189c6 | 4518 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4519 | { |
1d9e1288 | 4520 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4521 | int ret; |
4522 | ||
59d70013 | 4523 | if (issue_flags & IO_URING_F_NONBLOCK) |
eddc7ef5 JA |
4524 | return -EAGAIN; |
4525 | ||
e62753e4 BM |
4526 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4527 | ctx->buffer); | |
eddc7ef5 | 4528 | |
eddc7ef5 | 4529 | if (ret < 0) |
93d2bcd2 | 4530 | req_set_fail(req); |
e1e16097 | 4531 | io_req_complete(req, ret); |
eddc7ef5 JA |
4532 | return 0; |
4533 | } | |
4534 | ||
b5dba59e JA |
4535 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4536 | { | |
14587a46 | 4537 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4538 | return -EINVAL; |
b5dba59e | 4539 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
7df778be | 4540 | sqe->rw_flags || sqe->buf_index) |
b5dba59e | 4541 | return -EINVAL; |
9c280f90 | 4542 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4543 | return -EBADF; |
b5dba59e JA |
4544 | |
4545 | req->close.fd = READ_ONCE(sqe->fd); | |
7df778be PB |
4546 | req->close.file_slot = READ_ONCE(sqe->file_index); |
4547 | if (req->close.file_slot && req->close.fd) | |
4548 | return -EINVAL; | |
4549 | ||
b5dba59e | 4550 | return 0; |
b5dba59e JA |
4551 | } |
4552 | ||
889fca73 | 4553 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4554 | { |
9eac1904 | 4555 | struct files_struct *files = current->files; |
3af73b28 | 4556 | struct io_close *close = &req->close; |
9eac1904 | 4557 | struct fdtable *fdt; |
a1fde923 PB |
4558 | struct file *file = NULL; |
4559 | int ret = -EBADF; | |
b5dba59e | 4560 | |
7df778be PB |
4561 | if (req->close.file_slot) { |
4562 | ret = io_close_fixed(req, issue_flags); | |
4563 | goto err; | |
4564 | } | |
4565 | ||
9eac1904 JA |
4566 | spin_lock(&files->file_lock); |
4567 | fdt = files_fdtable(files); | |
4568 | if (close->fd >= fdt->max_fds) { | |
4569 | spin_unlock(&files->file_lock); | |
4570 | goto err; | |
4571 | } | |
4572 | file = fdt->fd[close->fd]; | |
a1fde923 | 4573 | if (!file || file->f_op == &io_uring_fops) { |
9eac1904 JA |
4574 | spin_unlock(&files->file_lock); |
4575 | file = NULL; | |
4576 | goto err; | |
3af73b28 | 4577 | } |
b5dba59e JA |
4578 | |
4579 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 4580 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 4581 | spin_unlock(&files->file_lock); |
0bf0eefd | 4582 | return -EAGAIN; |
a2100672 | 4583 | } |
b5dba59e | 4584 | |
9eac1904 JA |
4585 | ret = __close_fd_get_file(close->fd, &file); |
4586 | spin_unlock(&files->file_lock); | |
4587 | if (ret < 0) { | |
4588 | if (ret == -ENOENT) | |
4589 | ret = -EBADF; | |
4590 | goto err; | |
4591 | } | |
4592 | ||
3af73b28 | 4593 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4594 | ret = filp_close(file, current->files); |
4595 | err: | |
3af73b28 | 4596 | if (ret < 0) |
93d2bcd2 | 4597 | req_set_fail(req); |
9eac1904 JA |
4598 | if (file) |
4599 | fput(file); | |
889fca73 | 4600 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 4601 | return 0; |
b5dba59e JA |
4602 | } |
4603 | ||
1155c76a | 4604 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4605 | { |
4606 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 4607 | |
5d17b4a4 JA |
4608 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4609 | return -EINVAL; | |
26578cda PB |
4610 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4611 | sqe->splice_fd_in)) | |
5d17b4a4 JA |
4612 | return -EINVAL; |
4613 | ||
8ed8d3c3 JA |
4614 | req->sync.off = READ_ONCE(sqe->off); |
4615 | req->sync.len = READ_ONCE(sqe->len); | |
4616 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4617 | return 0; |
4618 | } | |
4619 | ||
45d189c6 | 4620 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4621 | { |
8ed8d3c3 JA |
4622 | int ret; |
4623 | ||
ac45abc0 | 4624 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 4625 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4626 | return -EAGAIN; |
4627 | ||
9adbd45d | 4628 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4629 | req->sync.flags); |
4630 | if (ret < 0) | |
93d2bcd2 | 4631 | req_set_fail(req); |
e1e16097 | 4632 | io_req_complete(req, ret); |
5d17b4a4 JA |
4633 | return 0; |
4634 | } | |
4635 | ||
469956e8 | 4636 | #if defined(CONFIG_NET) |
02d27d89 PB |
4637 | static int io_setup_async_msg(struct io_kiocb *req, |
4638 | struct io_async_msghdr *kmsg) | |
4639 | { | |
e8c2bc1f JA |
4640 | struct io_async_msghdr *async_msg = req->async_data; |
4641 | ||
4642 | if (async_msg) | |
02d27d89 | 4643 | return -EAGAIN; |
e8c2bc1f | 4644 | if (io_alloc_async_data(req)) { |
257e84a5 | 4645 | kfree(kmsg->free_iov); |
02d27d89 PB |
4646 | return -ENOMEM; |
4647 | } | |
e8c2bc1f | 4648 | async_msg = req->async_data; |
02d27d89 | 4649 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4650 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 4651 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
4652 | /* if were using fast_iov, set it to the new one */ |
4653 | if (!async_msg->free_iov) | |
4654 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
4655 | ||
02d27d89 PB |
4656 | return -EAGAIN; |
4657 | } | |
4658 | ||
2ae523ed PB |
4659 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4660 | struct io_async_msghdr *iomsg) | |
4661 | { | |
2ae523ed | 4662 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 4663 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 4664 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 4665 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
4666 | } |
4667 | ||
93642ef8 PB |
4668 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
4669 | { | |
4670 | int ret; | |
4671 | ||
93642ef8 PB |
4672 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
4673 | if (!ret) | |
4674 | req->flags |= REQ_F_NEED_CLEANUP; | |
4675 | return ret; | |
4676 | } | |
4677 | ||
3529d8c2 | 4678 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4679 | { |
e47293fd | 4680 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 4681 | |
d2b6f48b PB |
4682 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4683 | return -EINVAL; | |
4684 | ||
270a5940 | 4685 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4686 | sr->len = READ_ONCE(sqe->len); |
04411806 PB |
4687 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4688 | if (sr->msg_flags & MSG_DONTWAIT) | |
4689 | req->flags |= REQ_F_NOWAIT; | |
3529d8c2 | 4690 | |
d8768362 JA |
4691 | #ifdef CONFIG_COMPAT |
4692 | if (req->ctx->compat) | |
4693 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4694 | #endif | |
93642ef8 | 4695 | return 0; |
03b1230c JA |
4696 | } |
4697 | ||
889fca73 | 4698 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4699 | { |
6b754c8b | 4700 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4701 | struct socket *sock; |
7a7cacba | 4702 | unsigned flags; |
0031275d | 4703 | int min_ret = 0; |
0fa03c62 JA |
4704 | int ret; |
4705 | ||
dba4a925 | 4706 | sock = sock_from_file(req->file); |
7a7cacba | 4707 | if (unlikely(!sock)) |
dba4a925 | 4708 | return -ENOTSOCK; |
3529d8c2 | 4709 | |
257e84a5 PB |
4710 | kmsg = req->async_data; |
4711 | if (!kmsg) { | |
7a7cacba PB |
4712 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
4713 | if (ret) | |
4714 | return ret; | |
4715 | kmsg = &iomsg; | |
0fa03c62 | 4716 | } |
0fa03c62 | 4717 | |
04411806 PB |
4718 | flags = req->sr_msg.msg_flags; |
4719 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4720 | flags |= MSG_DONTWAIT; |
0031275d SM |
4721 | if (flags & MSG_WAITALL) |
4722 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4723 | ||
7a7cacba | 4724 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
45d189c6 | 4725 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4726 | return io_setup_async_msg(req, kmsg); |
4727 | if (ret == -ERESTARTSYS) | |
4728 | ret = -EINTR; | |
0fa03c62 | 4729 | |
257e84a5 PB |
4730 | /* fast path, check for non-NULL to avoid function call */ |
4731 | if (kmsg->free_iov) | |
4732 | kfree(kmsg->free_iov); | |
99bc4c38 | 4733 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 4734 | if (ret < min_ret) |
93d2bcd2 | 4735 | req_set_fail(req); |
889fca73 | 4736 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 4737 | return 0; |
03b1230c | 4738 | } |
aa1fa28f | 4739 | |
889fca73 | 4740 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4741 | { |
7a7cacba PB |
4742 | struct io_sr_msg *sr = &req->sr_msg; |
4743 | struct msghdr msg; | |
4744 | struct iovec iov; | |
fddaface | 4745 | struct socket *sock; |
7a7cacba | 4746 | unsigned flags; |
0031275d | 4747 | int min_ret = 0; |
fddaface JA |
4748 | int ret; |
4749 | ||
dba4a925 | 4750 | sock = sock_from_file(req->file); |
7a7cacba | 4751 | if (unlikely(!sock)) |
dba4a925 | 4752 | return -ENOTSOCK; |
fddaface | 4753 | |
7a7cacba PB |
4754 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4755 | if (unlikely(ret)) | |
14db8411 | 4756 | return ret; |
fddaface | 4757 | |
7a7cacba PB |
4758 | msg.msg_name = NULL; |
4759 | msg.msg_control = NULL; | |
4760 | msg.msg_controllen = 0; | |
4761 | msg.msg_namelen = 0; | |
fddaface | 4762 | |
04411806 PB |
4763 | flags = req->sr_msg.msg_flags; |
4764 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4765 | flags |= MSG_DONTWAIT; |
0031275d SM |
4766 | if (flags & MSG_WAITALL) |
4767 | min_ret = iov_iter_count(&msg.msg_iter); | |
4768 | ||
7a7cacba PB |
4769 | msg.msg_flags = flags; |
4770 | ret = sock_sendmsg(sock, &msg); | |
45d189c6 | 4771 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4772 | return -EAGAIN; |
4773 | if (ret == -ERESTARTSYS) | |
4774 | ret = -EINTR; | |
fddaface | 4775 | |
0031275d | 4776 | if (ret < min_ret) |
93d2bcd2 | 4777 | req_set_fail(req); |
889fca73 | 4778 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 4779 | return 0; |
fddaface JA |
4780 | } |
4781 | ||
1400e697 PB |
4782 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4783 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4784 | { |
4785 | struct io_sr_msg *sr = &req->sr_msg; | |
4786 | struct iovec __user *uiov; | |
4787 | size_t iov_len; | |
4788 | int ret; | |
4789 | ||
1400e697 PB |
4790 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4791 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4792 | if (ret) |
4793 | return ret; | |
4794 | ||
4795 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4796 | if (iov_len > 1) | |
4797 | return -EINVAL; | |
5476dfed | 4798 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4799 | return -EFAULT; |
5476dfed | 4800 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 4801 | iomsg->free_iov = NULL; |
52de1fe1 | 4802 | } else { |
257e84a5 | 4803 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4804 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 4805 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 4806 | false); |
52de1fe1 JA |
4807 | if (ret > 0) |
4808 | ret = 0; | |
4809 | } | |
4810 | ||
4811 | return ret; | |
4812 | } | |
4813 | ||
4814 | #ifdef CONFIG_COMPAT | |
4815 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4816 | struct io_async_msghdr *iomsg) |
52de1fe1 | 4817 | { |
52de1fe1 JA |
4818 | struct io_sr_msg *sr = &req->sr_msg; |
4819 | struct compat_iovec __user *uiov; | |
4820 | compat_uptr_t ptr; | |
4821 | compat_size_t len; | |
4822 | int ret; | |
4823 | ||
4af3417a PB |
4824 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
4825 | &ptr, &len); | |
52de1fe1 JA |
4826 | if (ret) |
4827 | return ret; | |
4828 | ||
4829 | uiov = compat_ptr(ptr); | |
4830 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4831 | compat_ssize_t clen; | |
4832 | ||
4833 | if (len > 1) | |
4834 | return -EINVAL; | |
4835 | if (!access_ok(uiov, sizeof(*uiov))) | |
4836 | return -EFAULT; | |
4837 | if (__get_user(clen, &uiov->iov_len)) | |
4838 | return -EFAULT; | |
4839 | if (clen < 0) | |
4840 | return -EINVAL; | |
2d280bc8 | 4841 | sr->len = clen; |
257e84a5 | 4842 | iomsg->free_iov = NULL; |
52de1fe1 | 4843 | } else { |
257e84a5 | 4844 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4845 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 4846 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 4847 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
4848 | if (ret < 0) |
4849 | return ret; | |
4850 | } | |
4851 | ||
4852 | return 0; | |
4853 | } | |
4854 | #endif | |
4855 | ||
1400e697 PB |
4856 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4857 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4858 | { |
1400e697 | 4859 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
4860 | |
4861 | #ifdef CONFIG_COMPAT | |
4862 | if (req->ctx->compat) | |
1400e697 | 4863 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4864 | #endif |
52de1fe1 | 4865 | |
1400e697 | 4866 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4867 | } |
4868 | ||
bcda7baa | 4869 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4870 | bool needs_lock) |
bcda7baa JA |
4871 | { |
4872 | struct io_sr_msg *sr = &req->sr_msg; | |
bcda7baa | 4873 | |
30d51dd4 | 4874 | return io_buffer_select(req, &sr->len, sr->bgid, needs_lock); |
fddaface JA |
4875 | } |
4876 | ||
7fbb1b54 PB |
4877 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4878 | { | |
30d51dd4 | 4879 | return io_put_kbuf(req, req->kbuf); |
7fbb1b54 PB |
4880 | } |
4881 | ||
93642ef8 | 4882 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 4883 | { |
99bc4c38 | 4884 | int ret; |
3529d8c2 | 4885 | |
93642ef8 PB |
4886 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
4887 | if (!ret) | |
4888 | req->flags |= REQ_F_NEED_CLEANUP; | |
4889 | return ret; | |
4890 | } | |
4891 | ||
4892 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4893 | { | |
4894 | struct io_sr_msg *sr = &req->sr_msg; | |
4895 | ||
d2b6f48b PB |
4896 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4897 | return -EINVAL; | |
4898 | ||
270a5940 | 4899 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4900 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4901 | sr->bgid = READ_ONCE(sqe->buf_group); |
04411806 PB |
4902 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4903 | if (sr->msg_flags & MSG_DONTWAIT) | |
4904 | req->flags |= REQ_F_NOWAIT; | |
06b76d44 | 4905 | |
d8768362 JA |
4906 | #ifdef CONFIG_COMPAT |
4907 | if (req->ctx->compat) | |
4908 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4909 | #endif | |
93642ef8 | 4910 | return 0; |
aa1fa28f JA |
4911 | } |
4912 | ||
889fca73 | 4913 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4914 | { |
6b754c8b | 4915 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4916 | struct socket *sock; |
7fbb1b54 | 4917 | struct io_buffer *kbuf; |
7a7cacba | 4918 | unsigned flags; |
0031275d | 4919 | int min_ret = 0; |
52de1fe1 | 4920 | int ret, cflags = 0; |
45d189c6 | 4921 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 4922 | |
dba4a925 | 4923 | sock = sock_from_file(req->file); |
7a7cacba | 4924 | if (unlikely(!sock)) |
dba4a925 | 4925 | return -ENOTSOCK; |
3529d8c2 | 4926 | |
257e84a5 PB |
4927 | kmsg = req->async_data; |
4928 | if (!kmsg) { | |
7a7cacba PB |
4929 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
4930 | if (ret) | |
681fda8d | 4931 | return ret; |
7a7cacba PB |
4932 | kmsg = &iomsg; |
4933 | } | |
03b1230c | 4934 | |
bc02ef33 | 4935 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4936 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4937 | if (IS_ERR(kbuf)) |
52de1fe1 | 4938 | return PTR_ERR(kbuf); |
7a7cacba | 4939 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
4940 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
4941 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
4942 | 1, req->sr_msg.len); |
4943 | } | |
52de1fe1 | 4944 | |
04411806 PB |
4945 | flags = req->sr_msg.msg_flags; |
4946 | if (force_nonblock) | |
7a7cacba | 4947 | flags |= MSG_DONTWAIT; |
0031275d SM |
4948 | if (flags & MSG_WAITALL) |
4949 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4950 | ||
7a7cacba PB |
4951 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4952 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4953 | if (force_nonblock && ret == -EAGAIN) |
4954 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4955 | if (ret == -ERESTARTSYS) |
4956 | ret = -EINTR; | |
03b1230c | 4957 | |
7fbb1b54 PB |
4958 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4959 | cflags = io_put_recv_kbuf(req); | |
257e84a5 PB |
4960 | /* fast path, check for non-NULL to avoid function call */ |
4961 | if (kmsg->free_iov) | |
4962 | kfree(kmsg->free_iov); | |
99bc4c38 | 4963 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 4964 | if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
93d2bcd2 | 4965 | req_set_fail(req); |
889fca73 | 4966 | __io_req_complete(req, issue_flags, ret, cflags); |
03b1230c | 4967 | return 0; |
0fa03c62 | 4968 | } |
5d17b4a4 | 4969 | |
889fca73 | 4970 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4971 | { |
6b754c8b | 4972 | struct io_buffer *kbuf; |
7a7cacba PB |
4973 | struct io_sr_msg *sr = &req->sr_msg; |
4974 | struct msghdr msg; | |
4975 | void __user *buf = sr->buf; | |
fddaface | 4976 | struct socket *sock; |
7a7cacba PB |
4977 | struct iovec iov; |
4978 | unsigned flags; | |
0031275d | 4979 | int min_ret = 0; |
bcda7baa | 4980 | int ret, cflags = 0; |
45d189c6 | 4981 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 4982 | |
dba4a925 | 4983 | sock = sock_from_file(req->file); |
7a7cacba | 4984 | if (unlikely(!sock)) |
dba4a925 | 4985 | return -ENOTSOCK; |
fddaface | 4986 | |
bc02ef33 | 4987 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4988 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
4989 | if (IS_ERR(kbuf)) |
4990 | return PTR_ERR(kbuf); | |
7a7cacba | 4991 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 4992 | } |
bcda7baa | 4993 | |
7a7cacba | 4994 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
4995 | if (unlikely(ret)) |
4996 | goto out_free; | |
fddaface | 4997 | |
7a7cacba PB |
4998 | msg.msg_name = NULL; |
4999 | msg.msg_control = NULL; | |
5000 | msg.msg_controllen = 0; | |
5001 | msg.msg_namelen = 0; | |
5002 | msg.msg_iocb = NULL; | |
5003 | msg.msg_flags = 0; | |
fddaface | 5004 | |
04411806 PB |
5005 | flags = req->sr_msg.msg_flags; |
5006 | if (force_nonblock) | |
7a7cacba | 5007 | flags |= MSG_DONTWAIT; |
0031275d SM |
5008 | if (flags & MSG_WAITALL) |
5009 | min_ret = iov_iter_count(&msg.msg_iter); | |
5010 | ||
7a7cacba PB |
5011 | ret = sock_recvmsg(sock, &msg, flags); |
5012 | if (force_nonblock && ret == -EAGAIN) | |
5013 | return -EAGAIN; | |
5014 | if (ret == -ERESTARTSYS) | |
5015 | ret = -EINTR; | |
14c32eee | 5016 | out_free: |
7fbb1b54 PB |
5017 | if (req->flags & REQ_F_BUFFER_SELECTED) |
5018 | cflags = io_put_recv_kbuf(req); | |
0031275d | 5019 | if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
93d2bcd2 | 5020 | req_set_fail(req); |
889fca73 | 5021 | __io_req_complete(req, issue_flags, ret, cflags); |
fddaface | 5022 | return 0; |
fddaface JA |
5023 | } |
5024 | ||
3529d8c2 | 5025 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 5026 | { |
8ed8d3c3 JA |
5027 | struct io_accept *accept = &req->accept; |
5028 | ||
14587a46 | 5029 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 5030 | return -EINVAL; |
aaa4db12 | 5031 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
5032 | return -EINVAL; |
5033 | ||
d55e5f5b JA |
5034 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5035 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 5036 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 5037 | accept->nofile = rlimit(RLIMIT_NOFILE); |
a7083ad5 | 5038 | |
aaa4db12 PB |
5039 | accept->file_slot = READ_ONCE(sqe->file_index); |
5040 | if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) || | |
5041 | (accept->flags & SOCK_CLOEXEC))) | |
5042 | return -EINVAL; | |
a7083ad5 PB |
5043 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
5044 | return -EINVAL; | |
5045 | if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) | |
5046 | accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; | |
8ed8d3c3 | 5047 | return 0; |
8ed8d3c3 | 5048 | } |
17f2fe35 | 5049 | |
889fca73 | 5050 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
5051 | { |
5052 | struct io_accept *accept = &req->accept; | |
45d189c6 | 5053 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 5054 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
aaa4db12 | 5055 | bool fixed = !!accept->file_slot; |
a7083ad5 PB |
5056 | struct file *file; |
5057 | int ret, fd; | |
8ed8d3c3 | 5058 | |
e697deed JX |
5059 | if (req->file->f_flags & O_NONBLOCK) |
5060 | req->flags |= REQ_F_NOWAIT; | |
5061 | ||
aaa4db12 PB |
5062 | if (!fixed) { |
5063 | fd = __get_unused_fd_flags(accept->flags, accept->nofile); | |
5064 | if (unlikely(fd < 0)) | |
5065 | return fd; | |
5066 | } | |
a7083ad5 PB |
5067 | file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, |
5068 | accept->flags); | |
5069 | if (IS_ERR(file)) { | |
aaa4db12 PB |
5070 | if (!fixed) |
5071 | put_unused_fd(fd); | |
a7083ad5 PB |
5072 | ret = PTR_ERR(file); |
5073 | if (ret == -EAGAIN && force_nonblock) | |
5074 | return -EAGAIN; | |
ac45abc0 PB |
5075 | if (ret == -ERESTARTSYS) |
5076 | ret = -EINTR; | |
93d2bcd2 | 5077 | req_set_fail(req); |
aaa4db12 | 5078 | } else if (!fixed) { |
a7083ad5 PB |
5079 | fd_install(fd, file); |
5080 | ret = fd; | |
aaa4db12 PB |
5081 | } else { |
5082 | ret = io_install_fixed_file(req, file, issue_flags, | |
5083 | accept->file_slot - 1); | |
ac45abc0 | 5084 | } |
889fca73 | 5085 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 5086 | return 0; |
8ed8d3c3 JA |
5087 | } |
5088 | ||
93642ef8 PB |
5089 | static int io_connect_prep_async(struct io_kiocb *req) |
5090 | { | |
5091 | struct io_async_connect *io = req->async_data; | |
5092 | struct io_connect *conn = &req->connect; | |
5093 | ||
5094 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
5095 | } | |
5096 | ||
3529d8c2 | 5097 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 5098 | { |
3529d8c2 | 5099 | struct io_connect *conn = &req->connect; |
f499a021 | 5100 | |
14587a46 | 5101 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 | 5102 | return -EINVAL; |
26578cda PB |
5103 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
5104 | sqe->splice_fd_in) | |
3fbb51c1 JA |
5105 | return -EINVAL; |
5106 | ||
3529d8c2 JA |
5107 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5108 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 5109 | return 0; |
f499a021 JA |
5110 | } |
5111 | ||
889fca73 | 5112 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 5113 | { |
e8c2bc1f | 5114 | struct io_async_connect __io, *io; |
f8e85cf2 | 5115 | unsigned file_flags; |
3fbb51c1 | 5116 | int ret; |
45d189c6 | 5117 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 5118 | |
e8c2bc1f JA |
5119 | if (req->async_data) { |
5120 | io = req->async_data; | |
f499a021 | 5121 | } else { |
3529d8c2 JA |
5122 | ret = move_addr_to_kernel(req->connect.addr, |
5123 | req->connect.addr_len, | |
e8c2bc1f | 5124 | &__io.address); |
f499a021 JA |
5125 | if (ret) |
5126 | goto out; | |
5127 | io = &__io; | |
5128 | } | |
5129 | ||
3fbb51c1 JA |
5130 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
5131 | ||
e8c2bc1f | 5132 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 5133 | req->connect.addr_len, file_flags); |
87f80d62 | 5134 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 5135 | if (req->async_data) |
b7bb4f7d | 5136 | return -EAGAIN; |
e8c2bc1f | 5137 | if (io_alloc_async_data(req)) { |
f499a021 JA |
5138 | ret = -ENOMEM; |
5139 | goto out; | |
5140 | } | |
e8c2bc1f | 5141 | memcpy(req->async_data, &__io, sizeof(__io)); |
f8e85cf2 | 5142 | return -EAGAIN; |
f499a021 | 5143 | } |
f8e85cf2 JA |
5144 | if (ret == -ERESTARTSYS) |
5145 | ret = -EINTR; | |
f499a021 | 5146 | out: |
4e88d6e7 | 5147 | if (ret < 0) |
93d2bcd2 | 5148 | req_set_fail(req); |
889fca73 | 5149 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 5150 | return 0; |
469956e8 Y |
5151 | } |
5152 | #else /* !CONFIG_NET */ | |
99a10081 JA |
5153 | #define IO_NETOP_FN(op) \ |
5154 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
5155 | { \ | |
5156 | return -EOPNOTSUPP; \ | |
5157 | } | |
5158 | ||
5159 | #define IO_NETOP_PREP(op) \ | |
5160 | IO_NETOP_FN(op) \ | |
5161 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
5162 | { \ | |
5163 | return -EOPNOTSUPP; \ | |
5164 | } \ | |
5165 | ||
5166 | #define IO_NETOP_PREP_ASYNC(op) \ | |
5167 | IO_NETOP_PREP(op) \ | |
5168 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
5169 | { \ | |
5170 | return -EOPNOTSUPP; \ | |
5171 | } | |
5172 | ||
5173 | IO_NETOP_PREP_ASYNC(sendmsg); | |
5174 | IO_NETOP_PREP_ASYNC(recvmsg); | |
5175 | IO_NETOP_PREP_ASYNC(connect); | |
5176 | IO_NETOP_PREP(accept); | |
5177 | IO_NETOP_FN(send); | |
5178 | IO_NETOP_FN(recv); | |
469956e8 | 5179 | #endif /* CONFIG_NET */ |
f8e85cf2 | 5180 | |
d7718a9d JA |
5181 | struct io_poll_table { |
5182 | struct poll_table_struct pt; | |
5183 | struct io_kiocb *req; | |
68b11e8b | 5184 | int nr_entries; |
d7718a9d JA |
5185 | int error; |
5186 | }; | |
ce593a6c | 5187 | |
d7718a9d | 5188 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
5b0a6acc | 5189 | __poll_t mask, io_req_tw_func_t func) |
d7718a9d | 5190 | { |
d7718a9d JA |
5191 | /* for instances that support it check for an event match first: */ |
5192 | if (mask && !(mask & poll->events)) | |
5193 | return 0; | |
5194 | ||
5195 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
5196 | ||
5197 | list_del_init(&poll->wait.entry); | |
5198 | ||
d7718a9d | 5199 | req->result = mask; |
5b0a6acc | 5200 | req->io_task_work.func = func; |
6d816e08 | 5201 | |
d7718a9d | 5202 | /* |
e3aabf95 JA |
5203 | * If this fails, then the task is exiting. When a task exits, the |
5204 | * work gets canceled, so just cancel this request as well instead | |
5205 | * of executing it. We can't safely execute it anyway, as we may not | |
5206 | * have the needed state needed for it anyway. | |
d7718a9d | 5207 | */ |
e09ee510 | 5208 | io_req_task_work_add(req); |
d7718a9d JA |
5209 | return 1; |
5210 | } | |
5211 | ||
74ce6ce4 JA |
5212 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
5213 | __acquires(&req->ctx->completion_lock) | |
5214 | { | |
5215 | struct io_ring_ctx *ctx = req->ctx; | |
5216 | ||
316319e8 | 5217 | /* req->task == current here, checking PF_EXITING is safe */ |
e09ee510 PB |
5218 | if (unlikely(req->task->flags & PF_EXITING)) |
5219 | WRITE_ONCE(poll->canceled, true); | |
5220 | ||
74ce6ce4 JA |
5221 | if (!req->result && !READ_ONCE(poll->canceled)) { |
5222 | struct poll_table_struct pt = { ._key = poll->events }; | |
5223 | ||
5224 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
5225 | } | |
5226 | ||
79ebeaee | 5227 | spin_lock(&ctx->completion_lock); |
74ce6ce4 JA |
5228 | if (!req->result && !READ_ONCE(poll->canceled)) { |
5229 | add_wait_queue(poll->head, &poll->wait); | |
5230 | return true; | |
5231 | } | |
5232 | ||
5233 | return false; | |
5234 | } | |
5235 | ||
d4e7cd36 | 5236 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5237 | { |
e8c2bc1f | 5238 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5239 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5240 | return req->async_data; |
d4e7cd36 JA |
5241 | return req->apoll->double_poll; |
5242 | } | |
5243 | ||
5244 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5245 | { | |
5246 | if (req->opcode == IORING_OP_POLL_ADD) | |
5247 | return &req->poll; | |
5248 | return &req->apoll->poll; | |
5249 | } | |
5250 | ||
5251 | static void io_poll_remove_double(struct io_kiocb *req) | |
e07785b0 | 5252 | __must_hold(&req->ctx->completion_lock) |
d4e7cd36 JA |
5253 | { |
5254 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
5255 | |
5256 | lockdep_assert_held(&req->ctx->completion_lock); | |
5257 | ||
5258 | if (poll && poll->head) { | |
5259 | struct wait_queue_head *head = poll->head; | |
5260 | ||
79ebeaee | 5261 | spin_lock_irq(&head->lock); |
18bceab1 JA |
5262 | list_del_init(&poll->wait.entry); |
5263 | if (poll->wait.private) | |
de9b4cca | 5264 | req_ref_put(req); |
18bceab1 | 5265 | poll->head = NULL; |
79ebeaee | 5266 | spin_unlock_irq(&head->lock); |
18bceab1 JA |
5267 | } |
5268 | } | |
5269 | ||
31efe48e | 5270 | static bool __io_poll_complete(struct io_kiocb *req, __poll_t mask) |
e07785b0 | 5271 | __must_hold(&req->ctx->completion_lock) |
18bceab1 JA |
5272 | { |
5273 | struct io_ring_ctx *ctx = req->ctx; | |
88e41cf9 | 5274 | unsigned flags = IORING_CQE_F_MORE; |
e27414be | 5275 | int error; |
18bceab1 | 5276 | |
e27414be | 5277 | if (READ_ONCE(req->poll.canceled)) { |
45ab03b1 | 5278 | error = -ECANCELED; |
88e41cf9 | 5279 | req->poll.events |= EPOLLONESHOT; |
e27414be | 5280 | } else { |
5082620f | 5281 | error = mangle_poll(mask); |
e27414be | 5282 | } |
b69de288 JA |
5283 | if (req->poll.events & EPOLLONESHOT) |
5284 | flags = 0; | |
a62682f9 HX |
5285 | if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) { |
5286 | req->poll.events |= EPOLLONESHOT; | |
88e41cf9 | 5287 | flags = 0; |
a62682f9 | 5288 | } |
7b289c38 HX |
5289 | if (flags & IORING_CQE_F_MORE) |
5290 | ctx->cq_extra++; | |
18bceab1 | 5291 | |
88e41cf9 | 5292 | return !(flags & IORING_CQE_F_MORE); |
18bceab1 JA |
5293 | } |
5294 | ||
31efe48e XW |
5295 | static inline bool io_poll_complete(struct io_kiocb *req, __poll_t mask) |
5296 | __must_hold(&req->ctx->completion_lock) | |
5297 | { | |
5298 | bool done; | |
5299 | ||
5300 | done = __io_poll_complete(req, mask); | |
5301 | io_commit_cqring(req->ctx); | |
5302 | return done; | |
5303 | } | |
5304 | ||
f237c30a | 5305 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 JA |
5306 | { |
5307 | struct io_ring_ctx *ctx = req->ctx; | |
dd221f46 | 5308 | struct io_kiocb *nxt; |
18bceab1 JA |
5309 | |
5310 | if (io_poll_rewait(req, &req->poll)) { | |
79ebeaee | 5311 | spin_unlock(&ctx->completion_lock); |
dd221f46 | 5312 | } else { |
f40b964a | 5313 | bool done; |
18bceab1 | 5314 | |
5b7aa38d HX |
5315 | if (req->poll.done) { |
5316 | spin_unlock(&ctx->completion_lock); | |
5317 | return; | |
5318 | } | |
31efe48e | 5319 | done = __io_poll_complete(req, req->result); |
88e41cf9 | 5320 | if (done) { |
a890d01e | 5321 | io_poll_remove_double(req); |
88e41cf9 | 5322 | hash_del(&req->hash_node); |
bd99c71b | 5323 | req->poll.done = true; |
f40b964a | 5324 | } else { |
88e41cf9 JA |
5325 | req->result = 0; |
5326 | add_wait_queue(req->poll.head, &req->poll.wait); | |
5327 | } | |
31efe48e | 5328 | io_commit_cqring(ctx); |
79ebeaee | 5329 | spin_unlock(&ctx->completion_lock); |
dd221f46 | 5330 | io_cqring_ev_posted(ctx); |
18bceab1 | 5331 | |
88e41cf9 JA |
5332 | if (done) { |
5333 | nxt = io_put_req_find_next(req); | |
5334 | if (nxt) | |
f237c30a | 5335 | io_req_task_submit(nxt, locked); |
88e41cf9 | 5336 | } |
dd221f46 | 5337 | } |
18bceab1 JA |
5338 | } |
5339 | ||
5340 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
5341 | int sync, void *key) | |
5342 | { | |
5343 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 5344 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 | 5345 | __poll_t mask = key_to_poll(key); |
79ebeaee | 5346 | unsigned long flags; |
18bceab1 JA |
5347 | |
5348 | /* for instances that support it check for an event match first: */ | |
5349 | if (mask && !(mask & poll->events)) | |
5350 | return 0; | |
88e41cf9 JA |
5351 | if (!(poll->events & EPOLLONESHOT)) |
5352 | return poll->wait.func(&poll->wait, mode, sync, key); | |
18bceab1 | 5353 | |
8706e04e JA |
5354 | list_del_init(&wait->entry); |
5355 | ||
9ce85ef2 | 5356 | if (poll->head) { |
18bceab1 JA |
5357 | bool done; |
5358 | ||
79ebeaee | 5359 | spin_lock_irqsave(&poll->head->lock, flags); |
807abcb0 | 5360 | done = list_empty(&poll->wait.entry); |
18bceab1 | 5361 | if (!done) |
807abcb0 | 5362 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
5363 | /* make sure double remove sees this as being gone */ |
5364 | wait->private = NULL; | |
79ebeaee | 5365 | spin_unlock_irqrestore(&poll->head->lock, flags); |
c8b5e260 JA |
5366 | if (!done) { |
5367 | /* use wait func handler, so it matches the rq type */ | |
5368 | poll->wait.func(&poll->wait, mode, sync, key); | |
5369 | } | |
18bceab1 | 5370 | } |
de9b4cca | 5371 | req_ref_put(req); |
18bceab1 JA |
5372 | return 1; |
5373 | } | |
5374 | ||
5375 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
5376 | wait_queue_func_t wake_func) | |
5377 | { | |
5378 | poll->head = NULL; | |
5379 | poll->done = false; | |
5380 | poll->canceled = false; | |
464dca61 JA |
5381 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
5382 | /* mask in events that we always want/need */ | |
5383 | poll->events = events | IO_POLL_UNMASK; | |
18bceab1 JA |
5384 | INIT_LIST_HEAD(&poll->wait.entry); |
5385 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
5386 | } | |
5387 | ||
5388 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
5389 | struct wait_queue_head *head, |
5390 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
5391 | { |
5392 | struct io_kiocb *req = pt->req; | |
5393 | ||
5394 | /* | |
68b11e8b PB |
5395 | * The file being polled uses multiple waitqueues for poll handling |
5396 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb | |
5397 | * if this happens. | |
18bceab1 | 5398 | */ |
68b11e8b | 5399 | if (unlikely(pt->nr_entries)) { |
58852d4d PB |
5400 | struct io_poll_iocb *poll_one = poll; |
5401 | ||
23a65db8 PB |
5402 | /* double add on the same waitqueue head, ignore */ |
5403 | if (poll_one->head == head) | |
5404 | return; | |
18bceab1 | 5405 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5406 | if (*poll_ptr) { |
23a65db8 PB |
5407 | if ((*poll_ptr)->head == head) |
5408 | return; | |
18bceab1 JA |
5409 | pt->error = -EINVAL; |
5410 | return; | |
5411 | } | |
ea6a693d JA |
5412 | /* |
5413 | * Can't handle multishot for double wait for now, turn it | |
5414 | * into one-shot mode. | |
5415 | */ | |
7a274727 PB |
5416 | if (!(poll_one->events & EPOLLONESHOT)) |
5417 | poll_one->events |= EPOLLONESHOT; | |
18bceab1 JA |
5418 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
5419 | if (!poll) { | |
5420 | pt->error = -ENOMEM; | |
5421 | return; | |
5422 | } | |
58852d4d | 5423 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
de9b4cca | 5424 | req_ref_get(req); |
18bceab1 | 5425 | poll->wait.private = req; |
807abcb0 | 5426 | *poll_ptr = poll; |
18bceab1 JA |
5427 | } |
5428 | ||
68b11e8b | 5429 | pt->nr_entries++; |
18bceab1 | 5430 | poll->head = head; |
a31eb4a2 JX |
5431 | |
5432 | if (poll->events & EPOLLEXCLUSIVE) | |
5433 | add_wait_queue_exclusive(head, &poll->wait); | |
5434 | else | |
5435 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5436 | } |
5437 | ||
5438 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5439 | struct poll_table_struct *p) | |
5440 | { | |
5441 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5442 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5443 | |
807abcb0 | 5444 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5445 | } |
5446 | ||
f237c30a | 5447 | static void io_async_task_func(struct io_kiocb *req, bool *locked) |
d7718a9d | 5448 | { |
d7718a9d JA |
5449 | struct async_poll *apoll = req->apoll; |
5450 | struct io_ring_ctx *ctx = req->ctx; | |
5451 | ||
236daeae | 5452 | trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data); |
d7718a9d | 5453 | |
74ce6ce4 | 5454 | if (io_poll_rewait(req, &apoll->poll)) { |
79ebeaee | 5455 | spin_unlock(&ctx->completion_lock); |
74ce6ce4 | 5456 | return; |
d7718a9d JA |
5457 | } |
5458 | ||
0ea13b44 | 5459 | hash_del(&req->hash_node); |
d4e7cd36 | 5460 | io_poll_remove_double(req); |
bd99c71b | 5461 | apoll->poll.done = true; |
79ebeaee | 5462 | spin_unlock(&ctx->completion_lock); |
74ce6ce4 | 5463 | |
0be0b0e3 | 5464 | if (!READ_ONCE(apoll->poll.canceled)) |
f237c30a | 5465 | io_req_task_submit(req, locked); |
0be0b0e3 | 5466 | else |
2593553a | 5467 | io_req_complete_failed(req, -ECANCELED); |
d7718a9d JA |
5468 | } |
5469 | ||
5470 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5471 | void *key) | |
5472 | { | |
5473 | struct io_kiocb *req = wait->private; | |
5474 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5475 | ||
5476 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5477 | key_to_poll(key)); | |
5478 | ||
5479 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5480 | } | |
5481 | ||
5482 | static void io_poll_req_insert(struct io_kiocb *req) | |
5483 | { | |
5484 | struct io_ring_ctx *ctx = req->ctx; | |
5485 | struct hlist_head *list; | |
5486 | ||
5487 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5488 | hlist_add_head(&req->hash_node, list); | |
5489 | } | |
5490 | ||
5491 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5492 | struct io_poll_iocb *poll, | |
5493 | struct io_poll_table *ipt, __poll_t mask, | |
5494 | wait_queue_func_t wake_func) | |
5495 | __acquires(&ctx->completion_lock) | |
5496 | { | |
5497 | struct io_ring_ctx *ctx = req->ctx; | |
5498 | bool cancel = false; | |
5499 | ||
4d52f338 | 5500 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5501 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5502 | poll->file = req->file; |
18bceab1 | 5503 | poll->wait.private = req; |
d7718a9d JA |
5504 | |
5505 | ipt->pt._key = mask; | |
5506 | ipt->req = req; | |
68b11e8b PB |
5507 | ipt->error = 0; |
5508 | ipt->nr_entries = 0; | |
d7718a9d | 5509 | |
d7718a9d | 5510 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
68b11e8b PB |
5511 | if (unlikely(!ipt->nr_entries) && !ipt->error) |
5512 | ipt->error = -EINVAL; | |
d7718a9d | 5513 | |
79ebeaee | 5514 | spin_lock(&ctx->completion_lock); |
a890d01e | 5515 | if (ipt->error || (mask && (poll->events & EPOLLONESHOT))) |
46fee9ab | 5516 | io_poll_remove_double(req); |
d7718a9d | 5517 | if (likely(poll->head)) { |
79ebeaee | 5518 | spin_lock_irq(&poll->head->lock); |
d7718a9d JA |
5519 | if (unlikely(list_empty(&poll->wait.entry))) { |
5520 | if (ipt->error) | |
5521 | cancel = true; | |
5522 | ipt->error = 0; | |
5523 | mask = 0; | |
5524 | } | |
88e41cf9 | 5525 | if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error) |
d7718a9d JA |
5526 | list_del_init(&poll->wait.entry); |
5527 | else if (cancel) | |
5528 | WRITE_ONCE(poll->canceled, true); | |
5529 | else if (!poll->done) /* actually waiting for an event */ | |
5530 | io_poll_req_insert(req); | |
79ebeaee | 5531 | spin_unlock_irq(&poll->head->lock); |
d7718a9d JA |
5532 | } |
5533 | ||
5534 | return mask; | |
5535 | } | |
5536 | ||
59b735ae OL |
5537 | enum { |
5538 | IO_APOLL_OK, | |
5539 | IO_APOLL_ABORTED, | |
5540 | IO_APOLL_READY | |
5541 | }; | |
5542 | ||
5543 | static int io_arm_poll_handler(struct io_kiocb *req) | |
d7718a9d JA |
5544 | { |
5545 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5546 | struct io_ring_ctx *ctx = req->ctx; | |
5547 | struct async_poll *apoll; | |
5548 | struct io_poll_table ipt; | |
b2d9c3da | 5549 | __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI; |
9dab14b8 | 5550 | int rw; |
d7718a9d JA |
5551 | |
5552 | if (!req->file || !file_can_poll(req->file)) | |
59b735ae | 5553 | return IO_APOLL_ABORTED; |
24c74678 | 5554 | if (req->flags & REQ_F_POLLED) |
59b735ae | 5555 | return IO_APOLL_ABORTED; |
b2d9c3da PB |
5556 | if (!def->pollin && !def->pollout) |
5557 | return IO_APOLL_ABORTED; | |
5558 | ||
5559 | if (def->pollin) { | |
9dab14b8 | 5560 | rw = READ; |
b2d9c3da PB |
5561 | mask |= POLLIN | POLLRDNORM; |
5562 | ||
5563 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5564 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5565 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5566 | mask &= ~POLLIN; | |
5567 | } else { | |
9dab14b8 | 5568 | rw = WRITE; |
b2d9c3da PB |
5569 | mask |= POLLOUT | POLLWRNORM; |
5570 | } | |
5571 | ||
9dab14b8 | 5572 | /* if we can't nonblock try, then no point in arming a poll handler */ |
b191e2df | 5573 | if (!io_file_supports_nowait(req, rw)) |
59b735ae | 5574 | return IO_APOLL_ABORTED; |
d7718a9d JA |
5575 | |
5576 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
5577 | if (unlikely(!apoll)) | |
59b735ae | 5578 | return IO_APOLL_ABORTED; |
807abcb0 | 5579 | apoll->double_poll = NULL; |
d7718a9d | 5580 | req->apoll = apoll; |
b2d9c3da | 5581 | req->flags |= REQ_F_POLLED; |
d7718a9d | 5582 | ipt.pt._qproc = io_async_queue_proc; |
48dcd38d | 5583 | io_req_set_refcount(req); |
d7718a9d JA |
5584 | |
5585 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5586 | io_async_wake); | |
79ebeaee | 5587 | spin_unlock(&ctx->completion_lock); |
41a5169c HX |
5588 | if (ret || ipt.error) |
5589 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; | |
5590 | ||
236daeae OL |
5591 | trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data, |
5592 | mask, apoll->poll.events); | |
59b735ae | 5593 | return IO_APOLL_OK; |
d7718a9d JA |
5594 | } |
5595 | ||
5596 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
b2e720ac | 5597 | struct io_poll_iocb *poll, bool do_cancel) |
e07785b0 | 5598 | __must_hold(&req->ctx->completion_lock) |
221c5eb2 | 5599 | { |
b41e9852 | 5600 | bool do_complete = false; |
221c5eb2 | 5601 | |
5082620f JA |
5602 | if (!poll->head) |
5603 | return false; | |
79ebeaee | 5604 | spin_lock_irq(&poll->head->lock); |
b2e720ac JA |
5605 | if (do_cancel) |
5606 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5607 | if (!list_empty(&poll->wait.entry)) { |
5608 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5609 | do_complete = true; |
221c5eb2 | 5610 | } |
79ebeaee | 5611 | spin_unlock_irq(&poll->head->lock); |
3bfa5bcb | 5612 | hash_del(&req->hash_node); |
d7718a9d JA |
5613 | return do_complete; |
5614 | } | |
5615 | ||
5d709043 | 5616 | static bool io_poll_remove_one(struct io_kiocb *req) |
e07785b0 | 5617 | __must_hold(&req->ctx->completion_lock) |
d7718a9d JA |
5618 | { |
5619 | bool do_complete; | |
5620 | ||
d4e7cd36 | 5621 | io_poll_remove_double(req); |
e31001a3 | 5622 | do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true); |
d4e7cd36 | 5623 | |
b41e9852 | 5624 | if (do_complete) { |
d4d19c19 | 5625 | io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0); |
b41e9852 | 5626 | io_commit_cqring(req->ctx); |
93d2bcd2 | 5627 | req_set_fail(req); |
91c2f697 | 5628 | io_put_req_deferred(req); |
5d709043 | 5629 | } |
b41e9852 | 5630 | return do_complete; |
221c5eb2 JA |
5631 | } |
5632 | ||
76e1b642 JA |
5633 | /* |
5634 | * Returns true if we found and killed one or more poll requests | |
5635 | */ | |
6b81928d | 5636 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
3dd0c97a | 5637 | bool cancel_all) |
221c5eb2 | 5638 | { |
78076bb6 | 5639 | struct hlist_node *tmp; |
221c5eb2 | 5640 | struct io_kiocb *req; |
8e2e1faf | 5641 | int posted = 0, i; |
221c5eb2 | 5642 | |
79ebeaee | 5643 | spin_lock(&ctx->completion_lock); |
78076bb6 JA |
5644 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5645 | struct hlist_head *list; | |
5646 | ||
5647 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5648 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
3dd0c97a | 5649 | if (io_match_task(req, tsk, cancel_all)) |
f3606e3a JA |
5650 | posted += io_poll_remove_one(req); |
5651 | } | |
221c5eb2 | 5652 | } |
79ebeaee | 5653 | spin_unlock(&ctx->completion_lock); |
b41e9852 | 5654 | |
8e2e1faf JA |
5655 | if (posted) |
5656 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5657 | |
5658 | return posted != 0; | |
221c5eb2 JA |
5659 | } |
5660 | ||
9ba5fac8 PB |
5661 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5662 | bool poll_only) | |
e07785b0 | 5663 | __must_hold(&ctx->completion_lock) |
47f46768 | 5664 | { |
78076bb6 | 5665 | struct hlist_head *list; |
47f46768 JA |
5666 | struct io_kiocb *req; |
5667 | ||
78076bb6 JA |
5668 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5669 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5670 | if (sqe_addr != req->user_data) |
5671 | continue; | |
9ba5fac8 PB |
5672 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
5673 | continue; | |
b2cb805f | 5674 | return req; |
47f46768 | 5675 | } |
b2cb805f JA |
5676 | return NULL; |
5677 | } | |
5678 | ||
9ba5fac8 PB |
5679 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5680 | bool poll_only) | |
e07785b0 | 5681 | __must_hold(&ctx->completion_lock) |
b2cb805f JA |
5682 | { |
5683 | struct io_kiocb *req; | |
5684 | ||
9ba5fac8 | 5685 | req = io_poll_find(ctx, sqe_addr, poll_only); |
b2cb805f JA |
5686 | if (!req) |
5687 | return -ENOENT; | |
5688 | if (io_poll_remove_one(req)) | |
5689 | return 0; | |
5690 | ||
5691 | return -EALREADY; | |
47f46768 JA |
5692 | } |
5693 | ||
9096af3e PB |
5694 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
5695 | unsigned int flags) | |
5696 | { | |
5697 | u32 events; | |
47f46768 | 5698 | |
9096af3e PB |
5699 | events = READ_ONCE(sqe->poll32_events); |
5700 | #ifdef __BIG_ENDIAN | |
5701 | events = swahw32(events); | |
5702 | #endif | |
5703 | if (!(flags & IORING_POLL_ADD_MULTI)) | |
5704 | events |= EPOLLONESHOT; | |
5705 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); | |
47f46768 JA |
5706 | } |
5707 | ||
c5de0036 | 5708 | static int io_poll_update_prep(struct io_kiocb *req, |
3529d8c2 | 5709 | const struct io_uring_sqe *sqe) |
0969e783 | 5710 | { |
c5de0036 PB |
5711 | struct io_poll_update *upd = &req->poll_update; |
5712 | u32 flags; | |
5713 | ||
0969e783 JA |
5714 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5715 | return -EINVAL; | |
26578cda | 5716 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
c5de0036 PB |
5717 | return -EINVAL; |
5718 | flags = READ_ONCE(sqe->len); | |
5719 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | | |
5720 | IORING_POLL_ADD_MULTI)) | |
5721 | return -EINVAL; | |
5722 | /* meaningless without update */ | |
5723 | if (flags == IORING_POLL_ADD_MULTI) | |
0969e783 JA |
5724 | return -EINVAL; |
5725 | ||
c5de0036 PB |
5726 | upd->old_user_data = READ_ONCE(sqe->addr); |
5727 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; | |
5728 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; | |
221c5eb2 | 5729 | |
c5de0036 PB |
5730 | upd->new_user_data = READ_ONCE(sqe->off); |
5731 | if (!upd->update_user_data && upd->new_user_data) | |
5732 | return -EINVAL; | |
5733 | if (upd->update_events) | |
5734 | upd->events = io_poll_parse_events(sqe, flags); | |
5735 | else if (sqe->poll32_events) | |
5736 | return -EINVAL; | |
221c5eb2 | 5737 | |
221c5eb2 JA |
5738 | return 0; |
5739 | } | |
5740 | ||
221c5eb2 JA |
5741 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5742 | void *key) | |
5743 | { | |
c2f2eb7d JA |
5744 | struct io_kiocb *req = wait->private; |
5745 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5746 | |
d7718a9d | 5747 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5748 | } |
5749 | ||
221c5eb2 JA |
5750 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5751 | struct poll_table_struct *p) | |
5752 | { | |
5753 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5754 | ||
e8c2bc1f | 5755 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5756 | } |
5757 | ||
3529d8c2 | 5758 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5759 | { |
5760 | struct io_poll_iocb *poll = &req->poll; | |
c5de0036 | 5761 | u32 flags; |
221c5eb2 JA |
5762 | |
5763 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5764 | return -EINVAL; | |
c5de0036 | 5765 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
88e41cf9 JA |
5766 | return -EINVAL; |
5767 | flags = READ_ONCE(sqe->len); | |
c5de0036 | 5768 | if (flags & ~IORING_POLL_ADD_MULTI) |
221c5eb2 JA |
5769 | return -EINVAL; |
5770 | ||
48dcd38d | 5771 | io_req_set_refcount(req); |
c5de0036 | 5772 | poll->events = io_poll_parse_events(sqe, flags); |
0969e783 JA |
5773 | return 0; |
5774 | } | |
5775 | ||
61e98203 | 5776 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
5777 | { |
5778 | struct io_poll_iocb *poll = &req->poll; | |
5779 | struct io_ring_ctx *ctx = req->ctx; | |
5780 | struct io_poll_table ipt; | |
0969e783 | 5781 | __poll_t mask; |
5b7aa38d | 5782 | bool done; |
0969e783 | 5783 | |
d7718a9d | 5784 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5785 | |
d7718a9d JA |
5786 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5787 | io_poll_wake); | |
221c5eb2 | 5788 | |
8c838788 | 5789 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5790 | ipt.error = 0; |
5b7aa38d | 5791 | done = io_poll_complete(req, mask); |
221c5eb2 | 5792 | } |
79ebeaee | 5793 | spin_unlock(&ctx->completion_lock); |
221c5eb2 | 5794 | |
8c838788 JA |
5795 | if (mask) { |
5796 | io_cqring_ev_posted(ctx); | |
5b7aa38d | 5797 | if (done) |
88e41cf9 | 5798 | io_put_req(req); |
221c5eb2 | 5799 | } |
8c838788 | 5800 | return ipt.error; |
221c5eb2 JA |
5801 | } |
5802 | ||
c5de0036 | 5803 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
b69de288 JA |
5804 | { |
5805 | struct io_ring_ctx *ctx = req->ctx; | |
5806 | struct io_kiocb *preq; | |
cb3b200e | 5807 | bool completing; |
b69de288 JA |
5808 | int ret; |
5809 | ||
79ebeaee | 5810 | spin_lock(&ctx->completion_lock); |
9ba5fac8 | 5811 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
b69de288 JA |
5812 | if (!preq) { |
5813 | ret = -ENOENT; | |
5814 | goto err; | |
b69de288 | 5815 | } |
cb3b200e | 5816 | |
c5de0036 PB |
5817 | if (!req->poll_update.update_events && !req->poll_update.update_user_data) { |
5818 | completing = true; | |
5819 | ret = io_poll_remove_one(preq) ? 0 : -EALREADY; | |
5820 | goto err; | |
5821 | } | |
5822 | ||
cb3b200e JA |
5823 | /* |
5824 | * Don't allow racy completion with singleshot, as we cannot safely | |
5825 | * update those. For multishot, if we're racing with completion, just | |
5826 | * let completion re-add it. | |
5827 | */ | |
5828 | completing = !__io_poll_remove_one(preq, &preq->poll, false); | |
5829 | if (completing && (preq->poll.events & EPOLLONESHOT)) { | |
5830 | ret = -EALREADY; | |
5831 | goto err; | |
b69de288 JA |
5832 | } |
5833 | /* we now have a detached poll request. reissue. */ | |
5834 | ret = 0; | |
5835 | err: | |
b69de288 | 5836 | if (ret < 0) { |
79ebeaee | 5837 | spin_unlock(&ctx->completion_lock); |
93d2bcd2 | 5838 | req_set_fail(req); |
b69de288 JA |
5839 | io_req_complete(req, ret); |
5840 | return 0; | |
5841 | } | |
5842 | /* only mask one event flags, keep behavior flags */ | |
9d805892 | 5843 | if (req->poll_update.update_events) { |
b69de288 | 5844 | preq->poll.events &= ~0xffff; |
9d805892 | 5845 | preq->poll.events |= req->poll_update.events & 0xffff; |
b69de288 JA |
5846 | preq->poll.events |= IO_POLL_UNMASK; |
5847 | } | |
9d805892 PB |
5848 | if (req->poll_update.update_user_data) |
5849 | preq->user_data = req->poll_update.new_user_data; | |
79ebeaee | 5850 | spin_unlock(&ctx->completion_lock); |
cb3b200e | 5851 | |
b69de288 JA |
5852 | /* complete update request, we're done with it */ |
5853 | io_req_complete(req, ret); | |
5854 | ||
cb3b200e | 5855 | if (!completing) { |
c5de0036 | 5856 | ret = io_poll_add(preq, issue_flags); |
cb3b200e | 5857 | if (ret < 0) { |
93d2bcd2 | 5858 | req_set_fail(preq); |
cb3b200e JA |
5859 | io_req_complete(preq, ret); |
5860 | } | |
b69de288 JA |
5861 | } |
5862 | return 0; | |
5863 | } | |
5864 | ||
f237c30a | 5865 | static void io_req_task_timeout(struct io_kiocb *req, bool *locked) |
89850fce | 5866 | { |
6224590d PB |
5867 | struct io_timeout_data *data = req->async_data; |
5868 | ||
5869 | if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS)) | |
5870 | req_set_fail(req); | |
505657bc | 5871 | io_req_complete_post(req, -ETIME, 0); |
89850fce JA |
5872 | } |
5873 | ||
5262f567 JA |
5874 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5875 | { | |
ad8a48ac JA |
5876 | struct io_timeout_data *data = container_of(timer, |
5877 | struct io_timeout_data, timer); | |
5878 | struct io_kiocb *req = data->req; | |
5879 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5880 | unsigned long flags; |
5881 | ||
89850fce | 5882 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
a71976f3 | 5883 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5884 | atomic_set(&req->ctx->cq_timeouts, |
5885 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
89850fce | 5886 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
01cec8c1 | 5887 | |
89850fce JA |
5888 | req->io_task_work.func = io_req_task_timeout; |
5889 | io_req_task_work_add(req); | |
5262f567 JA |
5890 | return HRTIMER_NORESTART; |
5891 | } | |
5892 | ||
fbd15848 PB |
5893 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5894 | __u64 user_data) | |
89850fce | 5895 | __must_hold(&ctx->timeout_lock) |
f254ac04 | 5896 | { |
fbd15848 | 5897 | struct io_timeout_data *io; |
47f46768 | 5898 | struct io_kiocb *req; |
fd9c7bc5 | 5899 | bool found = false; |
f254ac04 | 5900 | |
135fcde8 | 5901 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
fd9c7bc5 PB |
5902 | found = user_data == req->user_data; |
5903 | if (found) | |
47f46768 | 5904 | break; |
47f46768 | 5905 | } |
fd9c7bc5 PB |
5906 | if (!found) |
5907 | return ERR_PTR(-ENOENT); | |
fbd15848 PB |
5908 | |
5909 | io = req->async_data; | |
fd9c7bc5 | 5910 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
fbd15848 | 5911 | return ERR_PTR(-EALREADY); |
a71976f3 | 5912 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5913 | return req; |
5914 | } | |
47f46768 | 5915 | |
fbd15848 | 5916 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
ec3c3d0f | 5917 | __must_hold(&ctx->completion_lock) |
89850fce | 5918 | __must_hold(&ctx->timeout_lock) |
fbd15848 PB |
5919 | { |
5920 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5921 | ||
5922 | if (IS_ERR(req)) | |
5923 | return PTR_ERR(req); | |
f254ac04 | 5924 | |
93d2bcd2 | 5925 | req_set_fail(req); |
d4d19c19 | 5926 | io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0); |
91c2f697 | 5927 | io_put_req_deferred(req); |
f254ac04 JA |
5928 | return 0; |
5929 | } | |
5930 | ||
50c1df2b JA |
5931 | static clockid_t io_timeout_get_clock(struct io_timeout_data *data) |
5932 | { | |
5933 | switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) { | |
5934 | case IORING_TIMEOUT_BOOTTIME: | |
5935 | return CLOCK_BOOTTIME; | |
5936 | case IORING_TIMEOUT_REALTIME: | |
5937 | return CLOCK_REALTIME; | |
5938 | default: | |
5939 | /* can't happen, vetted at prep time */ | |
5940 | WARN_ON_ONCE(1); | |
5941 | fallthrough; | |
5942 | case 0: | |
5943 | return CLOCK_MONOTONIC; | |
5944 | } | |
5945 | } | |
5946 | ||
f1042b6c PB |
5947 | static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5948 | struct timespec64 *ts, enum hrtimer_mode mode) | |
5949 | __must_hold(&ctx->timeout_lock) | |
5950 | { | |
5951 | struct io_timeout_data *io; | |
5952 | struct io_kiocb *req; | |
5953 | bool found = false; | |
5954 | ||
5955 | list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) { | |
5956 | found = user_data == req->user_data; | |
5957 | if (found) | |
5958 | break; | |
5959 | } | |
5960 | if (!found) | |
5961 | return -ENOENT; | |
5962 | ||
5963 | io = req->async_data; | |
5964 | if (hrtimer_try_to_cancel(&io->timer) == -1) | |
5965 | return -EALREADY; | |
5966 | hrtimer_init(&io->timer, io_timeout_get_clock(io), mode); | |
5967 | io->timer.function = io_link_timeout_fn; | |
5968 | hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode); | |
5969 | return 0; | |
5970 | } | |
5971 | ||
9c8e11b3 PB |
5972 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5973 | struct timespec64 *ts, enum hrtimer_mode mode) | |
89850fce | 5974 | __must_hold(&ctx->timeout_lock) |
47f46768 | 5975 | { |
9c8e11b3 PB |
5976 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
5977 | struct io_timeout_data *data; | |
47f46768 | 5978 | |
9c8e11b3 PB |
5979 | if (IS_ERR(req)) |
5980 | return PTR_ERR(req); | |
47f46768 | 5981 | |
9c8e11b3 PB |
5982 | req->timeout.off = 0; /* noseq */ |
5983 | data = req->async_data; | |
5984 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
50c1df2b | 5985 | hrtimer_init(&data->timer, io_timeout_get_clock(data), mode); |
9c8e11b3 PB |
5986 | data->timer.function = io_timeout_fn; |
5987 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
5988 | return 0; | |
47f46768 JA |
5989 | } |
5990 | ||
3529d8c2 JA |
5991 | static int io_timeout_remove_prep(struct io_kiocb *req, |
5992 | const struct io_uring_sqe *sqe) | |
b29472ee | 5993 | { |
9c8e11b3 PB |
5994 | struct io_timeout_rem *tr = &req->timeout_rem; |
5995 | ||
b29472ee JA |
5996 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5997 | return -EINVAL; | |
61710e43 DA |
5998 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5999 | return -EINVAL; | |
26578cda | 6000 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
b29472ee JA |
6001 | return -EINVAL; |
6002 | ||
f1042b6c | 6003 | tr->ltimeout = false; |
9c8e11b3 PB |
6004 | tr->addr = READ_ONCE(sqe->addr); |
6005 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
f1042b6c PB |
6006 | if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) { |
6007 | if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
6008 | return -EINVAL; | |
6009 | if (tr->flags & IORING_LINK_TIMEOUT_UPDATE) | |
6010 | tr->ltimeout = true; | |
6011 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS)) | |
9c8e11b3 PB |
6012 | return -EINVAL; |
6013 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
6014 | return -EFAULT; | |
6015 | } else if (tr->flags) { | |
6016 | /* timeout removal doesn't support flags */ | |
b29472ee | 6017 | return -EINVAL; |
9c8e11b3 | 6018 | } |
b29472ee | 6019 | |
b29472ee JA |
6020 | return 0; |
6021 | } | |
6022 | ||
8662daec PB |
6023 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
6024 | { | |
6025 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
6026 | : HRTIMER_MODE_REL; | |
6027 | } | |
6028 | ||
11365043 JA |
6029 | /* |
6030 | * Remove or update an existing timeout command | |
6031 | */ | |
61e98203 | 6032 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 6033 | { |
9c8e11b3 | 6034 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 6035 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 6036 | int ret; |
11365043 | 6037 | |
ec3c3d0f PB |
6038 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
6039 | spin_lock(&ctx->completion_lock); | |
6040 | spin_lock_irq(&ctx->timeout_lock); | |
9c8e11b3 | 6041 | ret = io_timeout_cancel(ctx, tr->addr); |
ec3c3d0f PB |
6042 | spin_unlock_irq(&ctx->timeout_lock); |
6043 | spin_unlock(&ctx->completion_lock); | |
6044 | } else { | |
f1042b6c PB |
6045 | enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags); |
6046 | ||
ec3c3d0f | 6047 | spin_lock_irq(&ctx->timeout_lock); |
f1042b6c PB |
6048 | if (tr->ltimeout) |
6049 | ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
6050 | else | |
6051 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
ec3c3d0f PB |
6052 | spin_unlock_irq(&ctx->timeout_lock); |
6053 | } | |
11365043 | 6054 | |
4e88d6e7 | 6055 | if (ret < 0) |
93d2bcd2 | 6056 | req_set_fail(req); |
505657bc | 6057 | io_req_complete_post(req, ret, 0); |
11365043 | 6058 | return 0; |
5262f567 JA |
6059 | } |
6060 | ||
3529d8c2 | 6061 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 6062 | bool is_timeout_link) |
5262f567 | 6063 | { |
ad8a48ac | 6064 | struct io_timeout_data *data; |
a41525ab | 6065 | unsigned flags; |
56080b02 | 6066 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 6067 | |
ad8a48ac | 6068 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 6069 | return -EINVAL; |
26578cda PB |
6070 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
6071 | sqe->splice_fd_in) | |
a41525ab | 6072 | return -EINVAL; |
56080b02 | 6073 | if (off && is_timeout_link) |
2d28390a | 6074 | return -EINVAL; |
a41525ab | 6075 | flags = READ_ONCE(sqe->timeout_flags); |
6224590d PB |
6076 | if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK | |
6077 | IORING_TIMEOUT_ETIME_SUCCESS)) | |
50c1df2b JA |
6078 | return -EINVAL; |
6079 | /* more than one clock specified is invalid, obviously */ | |
6080 | if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
5262f567 | 6081 | return -EINVAL; |
bdf20073 | 6082 | |
ef9dd637 | 6083 | INIT_LIST_HEAD(&req->timeout.list); |
bfe68a22 | 6084 | req->timeout.off = off; |
f18ee4cf PB |
6085 | if (unlikely(off && !req->ctx->off_timeout_used)) |
6086 | req->ctx->off_timeout_used = true; | |
26a61679 | 6087 | |
e8c2bc1f | 6088 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
6089 | return -ENOMEM; |
6090 | ||
e8c2bc1f | 6091 | data = req->async_data; |
ad8a48ac | 6092 | data->req = req; |
50c1df2b | 6093 | data->flags = flags; |
ad8a48ac JA |
6094 | |
6095 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
6096 | return -EFAULT; |
6097 | ||
8662daec | 6098 | data->mode = io_translate_timeout_mode(flags); |
50c1df2b | 6099 | hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode); |
b97e736a PB |
6100 | |
6101 | if (is_timeout_link) { | |
6102 | struct io_submit_link *link = &req->ctx->submit_state.link; | |
6103 | ||
6104 | if (!link->head) | |
6105 | return -EINVAL; | |
6106 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) | |
6107 | return -EINVAL; | |
4d13d1a4 PB |
6108 | req->timeout.head = link->last; |
6109 | link->last->flags |= REQ_F_ARM_LTIMEOUT; | |
b97e736a | 6110 | } |
ad8a48ac JA |
6111 | return 0; |
6112 | } | |
6113 | ||
61e98203 | 6114 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 6115 | { |
ad8a48ac | 6116 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 6117 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 6118 | struct list_head *entry; |
bfe68a22 | 6119 | u32 tail, off = req->timeout.off; |
ad8a48ac | 6120 | |
89850fce | 6121 | spin_lock_irq(&ctx->timeout_lock); |
93bd25bb | 6122 | |
5262f567 JA |
6123 | /* |
6124 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
6125 | * timeout event to be satisfied. If it isn't set, then this is |
6126 | * a pure timeout request, sequence isn't used. | |
5262f567 | 6127 | */ |
8eb7e2d0 | 6128 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
6129 | entry = ctx->timeout_list.prev; |
6130 | goto add; | |
6131 | } | |
5262f567 | 6132 | |
bfe68a22 PB |
6133 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
6134 | req->timeout.target_seq = tail + off; | |
5262f567 | 6135 | |
f010505b MDG |
6136 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
6137 | * This is safe because ->completion_lock is held, and submissions | |
6138 | * and completions are never mixed in the same ->completion_lock section. | |
6139 | */ | |
6140 | ctx->cq_last_tm_flush = tail; | |
6141 | ||
5262f567 JA |
6142 | /* |
6143 | * Insertion sort, ensuring the first entry in the list is always | |
6144 | * the one we need first. | |
6145 | */ | |
5262f567 | 6146 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
6147 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
6148 | timeout.list); | |
5262f567 | 6149 | |
8eb7e2d0 | 6150 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 6151 | continue; |
bfe68a22 PB |
6152 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
6153 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
6154 | break; |
6155 | } | |
93bd25bb | 6156 | add: |
135fcde8 | 6157 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
6158 | data->timer.function = io_timeout_fn; |
6159 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
89850fce | 6160 | spin_unlock_irq(&ctx->timeout_lock); |
5262f567 JA |
6161 | return 0; |
6162 | } | |
5262f567 | 6163 | |
f458dd84 PB |
6164 | struct io_cancel_data { |
6165 | struct io_ring_ctx *ctx; | |
6166 | u64 user_data; | |
6167 | }; | |
6168 | ||
62755e35 JA |
6169 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
6170 | { | |
6171 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 6172 | struct io_cancel_data *cd = data; |
62755e35 | 6173 | |
f458dd84 | 6174 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
62755e35 JA |
6175 | } |
6176 | ||
f458dd84 PB |
6177 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
6178 | struct io_ring_ctx *ctx) | |
62755e35 | 6179 | { |
f458dd84 | 6180 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
62755e35 | 6181 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
6182 | int ret = 0; |
6183 | ||
f458dd84 | 6184 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
6185 | return -ENOENT; |
6186 | ||
f458dd84 | 6187 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
62755e35 JA |
6188 | switch (cancel_ret) { |
6189 | case IO_WQ_CANCEL_OK: | |
6190 | ret = 0; | |
6191 | break; | |
6192 | case IO_WQ_CANCEL_RUNNING: | |
6193 | ret = -EALREADY; | |
6194 | break; | |
6195 | case IO_WQ_CANCEL_NOTFOUND: | |
6196 | ret = -ENOENT; | |
6197 | break; | |
6198 | } | |
6199 | ||
e977d6d3 JA |
6200 | return ret; |
6201 | } | |
6202 | ||
8cb01fac | 6203 | static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr) |
47f46768 | 6204 | { |
8cb01fac | 6205 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 JA |
6206 | int ret; |
6207 | ||
dadebc35 | 6208 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
8cb01fac | 6209 | |
f458dd84 | 6210 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
df9727af | 6211 | if (ret != -ENOENT) |
8cb01fac | 6212 | return ret; |
505657bc PB |
6213 | |
6214 | spin_lock(&ctx->completion_lock); | |
79ebeaee | 6215 | spin_lock_irq(&ctx->timeout_lock); |
47f46768 | 6216 | ret = io_timeout_cancel(ctx, sqe_addr); |
79ebeaee | 6217 | spin_unlock_irq(&ctx->timeout_lock); |
47f46768 | 6218 | if (ret != -ENOENT) |
505657bc PB |
6219 | goto out; |
6220 | ret = io_poll_cancel(ctx, sqe_addr, false); | |
6221 | out: | |
6222 | spin_unlock(&ctx->completion_lock); | |
6223 | return ret; | |
47f46768 JA |
6224 | } |
6225 | ||
3529d8c2 JA |
6226 | static int io_async_cancel_prep(struct io_kiocb *req, |
6227 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 6228 | { |
fbf23849 | 6229 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 6230 | return -EINVAL; |
61710e43 DA |
6231 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6232 | return -EINVAL; | |
26578cda PB |
6233 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
6234 | sqe->splice_fd_in) | |
e977d6d3 JA |
6235 | return -EINVAL; |
6236 | ||
fbf23849 JA |
6237 | req->cancel.addr = READ_ONCE(sqe->addr); |
6238 | return 0; | |
6239 | } | |
6240 | ||
61e98203 | 6241 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6242 | { |
6243 | struct io_ring_ctx *ctx = req->ctx; | |
58f99373 PB |
6244 | u64 sqe_addr = req->cancel.addr; |
6245 | struct io_tctx_node *node; | |
6246 | int ret; | |
6247 | ||
8cb01fac | 6248 | ret = io_try_cancel_userdata(req, sqe_addr); |
58f99373 PB |
6249 | if (ret != -ENOENT) |
6250 | goto done; | |
58f99373 PB |
6251 | |
6252 | /* slow path, try all io-wq's */ | |
6253 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
6254 | ret = -ENOENT; | |
6255 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
6256 | struct io_uring_task *tctx = node->task->io_uring; | |
fbf23849 | 6257 | |
58f99373 PB |
6258 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
6259 | if (ret != -ENOENT) | |
6260 | break; | |
6261 | } | |
6262 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
58f99373 | 6263 | done: |
58f99373 | 6264 | if (ret < 0) |
93d2bcd2 | 6265 | req_set_fail(req); |
505657bc | 6266 | io_req_complete_post(req, ret, 0); |
5262f567 JA |
6267 | return 0; |
6268 | } | |
6269 | ||
269bbe5f | 6270 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
6271 | const struct io_uring_sqe *sqe) |
6272 | { | |
61710e43 DA |
6273 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6274 | return -EINVAL; | |
26578cda | 6275 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
05f3fb3c JA |
6276 | return -EINVAL; |
6277 | ||
269bbe5f BM |
6278 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
6279 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
6280 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 6281 | return -EINVAL; |
269bbe5f | 6282 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
6283 | return 0; |
6284 | } | |
6285 | ||
889fca73 | 6286 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6287 | { |
6288 | struct io_ring_ctx *ctx = req->ctx; | |
c3bdad02 | 6289 | struct io_uring_rsrc_update2 up; |
05f3fb3c | 6290 | int ret; |
fbf23849 | 6291 | |
269bbe5f BM |
6292 | up.offset = req->rsrc_update.offset; |
6293 | up.data = req->rsrc_update.arg; | |
c3bdad02 PB |
6294 | up.nr = 0; |
6295 | up.tags = 0; | |
615cee49 | 6296 | up.resv = 0; |
05f3fb3c | 6297 | |
cdb31c29 | 6298 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
fdecb662 | 6299 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
98f0b3b4 | 6300 | &up, req->rsrc_update.nr_args); |
cdb31c29 | 6301 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
05f3fb3c JA |
6302 | |
6303 | if (ret < 0) | |
93d2bcd2 | 6304 | req_set_fail(req); |
889fca73 | 6305 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
6306 | return 0; |
6307 | } | |
6308 | ||
bfe76559 | 6309 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 6310 | { |
d625c6ee | 6311 | switch (req->opcode) { |
e781573e | 6312 | case IORING_OP_NOP: |
bfe76559 | 6313 | return 0; |
f67676d1 JA |
6314 | case IORING_OP_READV: |
6315 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 6316 | case IORING_OP_READ: |
bfe76559 | 6317 | return io_read_prep(req, sqe); |
f67676d1 JA |
6318 | case IORING_OP_WRITEV: |
6319 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 6320 | case IORING_OP_WRITE: |
bfe76559 | 6321 | return io_write_prep(req, sqe); |
0969e783 | 6322 | case IORING_OP_POLL_ADD: |
bfe76559 | 6323 | return io_poll_add_prep(req, sqe); |
0969e783 | 6324 | case IORING_OP_POLL_REMOVE: |
c5de0036 | 6325 | return io_poll_update_prep(req, sqe); |
8ed8d3c3 | 6326 | case IORING_OP_FSYNC: |
1155c76a | 6327 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 6328 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 6329 | return io_sfr_prep(req, sqe); |
03b1230c | 6330 | case IORING_OP_SENDMSG: |
fddaface | 6331 | case IORING_OP_SEND: |
bfe76559 | 6332 | return io_sendmsg_prep(req, sqe); |
03b1230c | 6333 | case IORING_OP_RECVMSG: |
fddaface | 6334 | case IORING_OP_RECV: |
bfe76559 | 6335 | return io_recvmsg_prep(req, sqe); |
f499a021 | 6336 | case IORING_OP_CONNECT: |
bfe76559 | 6337 | return io_connect_prep(req, sqe); |
2d28390a | 6338 | case IORING_OP_TIMEOUT: |
bfe76559 | 6339 | return io_timeout_prep(req, sqe, false); |
b29472ee | 6340 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 6341 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 6342 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 6343 | return io_async_cancel_prep(req, sqe); |
2d28390a | 6344 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 6345 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 6346 | case IORING_OP_ACCEPT: |
bfe76559 | 6347 | return io_accept_prep(req, sqe); |
d63d1b5e | 6348 | case IORING_OP_FALLOCATE: |
bfe76559 | 6349 | return io_fallocate_prep(req, sqe); |
15b71abe | 6350 | case IORING_OP_OPENAT: |
bfe76559 | 6351 | return io_openat_prep(req, sqe); |
b5dba59e | 6352 | case IORING_OP_CLOSE: |
bfe76559 | 6353 | return io_close_prep(req, sqe); |
05f3fb3c | 6354 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 6355 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6356 | case IORING_OP_STATX: |
bfe76559 | 6357 | return io_statx_prep(req, sqe); |
4840e418 | 6358 | case IORING_OP_FADVISE: |
bfe76559 | 6359 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6360 | case IORING_OP_MADVISE: |
bfe76559 | 6361 | return io_madvise_prep(req, sqe); |
cebdb986 | 6362 | case IORING_OP_OPENAT2: |
bfe76559 | 6363 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6364 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6365 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6366 | case IORING_OP_SPLICE: |
bfe76559 | 6367 | return io_splice_prep(req, sqe); |
ddf0322d | 6368 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6369 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6370 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6371 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6372 | case IORING_OP_TEE: |
bfe76559 | 6373 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6374 | case IORING_OP_SHUTDOWN: |
6375 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6376 | case IORING_OP_RENAMEAT: |
6377 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6378 | case IORING_OP_UNLINKAT: |
6379 | return io_unlinkat_prep(req, sqe); | |
e34a02dc DK |
6380 | case IORING_OP_MKDIRAT: |
6381 | return io_mkdirat_prep(req, sqe); | |
7a8721f8 DK |
6382 | case IORING_OP_SYMLINKAT: |
6383 | return io_symlinkat_prep(req, sqe); | |
cf30da90 DK |
6384 | case IORING_OP_LINKAT: |
6385 | return io_linkat_prep(req, sqe); | |
f67676d1 JA |
6386 | } |
6387 | ||
bfe76559 PB |
6388 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6389 | req->opcode); | |
bd54b6fe | 6390 | return -EINVAL; |
bfe76559 PB |
6391 | } |
6392 | ||
93642ef8 | 6393 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 6394 | { |
b7e298d2 PB |
6395 | if (!io_op_defs[req->opcode].needs_async_setup) |
6396 | return 0; | |
6397 | if (WARN_ON_ONCE(req->async_data)) | |
6398 | return -EFAULT; | |
6399 | if (io_alloc_async_data(req)) | |
6400 | return -EAGAIN; | |
6401 | ||
93642ef8 PB |
6402 | switch (req->opcode) { |
6403 | case IORING_OP_READV: | |
93642ef8 PB |
6404 | return io_rw_prep_async(req, READ); |
6405 | case IORING_OP_WRITEV: | |
93642ef8 PB |
6406 | return io_rw_prep_async(req, WRITE); |
6407 | case IORING_OP_SENDMSG: | |
93642ef8 PB |
6408 | return io_sendmsg_prep_async(req); |
6409 | case IORING_OP_RECVMSG: | |
93642ef8 PB |
6410 | return io_recvmsg_prep_async(req); |
6411 | case IORING_OP_CONNECT: | |
6412 | return io_connect_prep_async(req); | |
6413 | } | |
b7e298d2 PB |
6414 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
6415 | req->opcode); | |
6416 | return -EFAULT; | |
f67676d1 JA |
6417 | } |
6418 | ||
9cf7c104 PB |
6419 | static u32 io_get_sequence(struct io_kiocb *req) |
6420 | { | |
a3dbdf54 | 6421 | u32 seq = req->ctx->cached_sq_head; |
9cf7c104 | 6422 | |
a3dbdf54 PB |
6423 | /* need original cached_sq_head, but it was increased for each req */ |
6424 | io_for_each_link(req, req) | |
6425 | seq--; | |
6426 | return seq; | |
9cf7c104 PB |
6427 | } |
6428 | ||
e0eb71dc | 6429 | static void io_drain_req(struct io_kiocb *req) |
de0617e4 | 6430 | { |
a197f664 | 6431 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6432 | struct io_defer_entry *de; |
f67676d1 | 6433 | int ret; |
e0eb71dc | 6434 | u32 seq = io_get_sequence(req); |
de0617e4 | 6435 | |
9d858b21 | 6436 | /* Still need defer if there is pending req in defer list. */ |
5e371265 | 6437 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) { |
e0eb71dc | 6438 | queue: |
5e371265 | 6439 | ctx->drain_active = false; |
e0eb71dc PB |
6440 | io_req_task_queue(req); |
6441 | return; | |
5e371265 | 6442 | } |
de0617e4 | 6443 | |
b7e298d2 | 6444 | ret = io_req_prep_async(req); |
e0eb71dc PB |
6445 | if (ret) { |
6446 | fail: | |
6447 | io_req_complete_failed(req, ret); | |
6448 | return; | |
6449 | } | |
cbdcb435 | 6450 | io_prep_async_link(req); |
27dc8338 | 6451 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
76cc33d7 | 6452 | if (!de) { |
1b48773f | 6453 | ret = -ENOMEM; |
e0eb71dc | 6454 | goto fail; |
76cc33d7 | 6455 | } |
2d28390a | 6456 | |
79ebeaee | 6457 | spin_lock(&ctx->completion_lock); |
9cf7c104 | 6458 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
79ebeaee | 6459 | spin_unlock(&ctx->completion_lock); |
27dc8338 | 6460 | kfree(de); |
e0eb71dc | 6461 | goto queue; |
de0617e4 JA |
6462 | } |
6463 | ||
915967f6 | 6464 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 6465 | de->req = req; |
9cf7c104 | 6466 | de->seq = seq; |
27dc8338 | 6467 | list_add_tail(&de->list, &ctx->defer_list); |
79ebeaee | 6468 | spin_unlock(&ctx->completion_lock); |
de0617e4 JA |
6469 | } |
6470 | ||
68fb8979 | 6471 | static void io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6472 | { |
0e1b6fe3 | 6473 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
30d51dd4 PB |
6474 | kfree(req->kbuf); |
6475 | req->kbuf = NULL; | |
99bc4c38 PB |
6476 | } |
6477 | ||
0e1b6fe3 PB |
6478 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6479 | switch (req->opcode) { | |
6480 | case IORING_OP_READV: | |
6481 | case IORING_OP_READ_FIXED: | |
6482 | case IORING_OP_READ: | |
6483 | case IORING_OP_WRITEV: | |
6484 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6485 | case IORING_OP_WRITE: { |
6486 | struct io_async_rw *io = req->async_data; | |
1dacb4df PB |
6487 | |
6488 | kfree(io->free_iovec); | |
0e1b6fe3 | 6489 | break; |
e8c2bc1f | 6490 | } |
0e1b6fe3 | 6491 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6492 | case IORING_OP_SENDMSG: { |
6493 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
6494 | |
6495 | kfree(io->free_iov); | |
0e1b6fe3 | 6496 | break; |
e8c2bc1f | 6497 | } |
0e1b6fe3 PB |
6498 | case IORING_OP_SPLICE: |
6499 | case IORING_OP_TEE: | |
e1d767f0 PB |
6500 | if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED)) |
6501 | io_put_file(req->splice.file_in); | |
0e1b6fe3 | 6502 | break; |
f3cd4850 JA |
6503 | case IORING_OP_OPENAT: |
6504 | case IORING_OP_OPENAT2: | |
6505 | if (req->open.filename) | |
6506 | putname(req->open.filename); | |
6507 | break; | |
80a261fd JA |
6508 | case IORING_OP_RENAMEAT: |
6509 | putname(req->rename.oldpath); | |
6510 | putname(req->rename.newpath); | |
6511 | break; | |
14a1143b JA |
6512 | case IORING_OP_UNLINKAT: |
6513 | putname(req->unlink.filename); | |
6514 | break; | |
e34a02dc DK |
6515 | case IORING_OP_MKDIRAT: |
6516 | putname(req->mkdir.filename); | |
6517 | break; | |
7a8721f8 DK |
6518 | case IORING_OP_SYMLINKAT: |
6519 | putname(req->symlink.oldpath); | |
6520 | putname(req->symlink.newpath); | |
6521 | break; | |
cf30da90 DK |
6522 | case IORING_OP_LINKAT: |
6523 | putname(req->hardlink.oldpath); | |
6524 | putname(req->hardlink.newpath); | |
6525 | break; | |
0e1b6fe3 | 6526 | } |
99bc4c38 | 6527 | } |
75652a30 JA |
6528 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
6529 | kfree(req->apoll->double_poll); | |
6530 | kfree(req->apoll); | |
6531 | req->apoll = NULL; | |
6532 | } | |
3a0a6902 PB |
6533 | if (req->flags & REQ_F_INFLIGHT) { |
6534 | struct io_uring_task *tctx = req->task->io_uring; | |
6535 | ||
6536 | atomic_dec(&tctx->inflight_tracked); | |
3a0a6902 | 6537 | } |
c854357b | 6538 | if (req->flags & REQ_F_CREDS) |
b8e64b53 | 6539 | put_cred(req->creds); |
c854357b PB |
6540 | |
6541 | req->flags &= ~IO_REQ_CLEAN_FLAGS; | |
99bc4c38 PB |
6542 | } |
6543 | ||
889fca73 | 6544 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 6545 | { |
a197f664 | 6546 | struct io_ring_ctx *ctx = req->ctx; |
5730b27e | 6547 | const struct cred *creds = NULL; |
d625c6ee | 6548 | int ret; |
2b188cc1 | 6549 | |
6878b40e | 6550 | if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred())) |
c10d1f98 | 6551 | creds = override_creds(req->creds); |
5730b27e | 6552 | |
d625c6ee | 6553 | switch (req->opcode) { |
2b188cc1 | 6554 | case IORING_OP_NOP: |
889fca73 | 6555 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
6556 | break; |
6557 | case IORING_OP_READV: | |
edafccee | 6558 | case IORING_OP_READ_FIXED: |
3a6820f2 | 6559 | case IORING_OP_READ: |
889fca73 | 6560 | ret = io_read(req, issue_flags); |
edafccee | 6561 | break; |
3529d8c2 | 6562 | case IORING_OP_WRITEV: |
edafccee | 6563 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 6564 | case IORING_OP_WRITE: |
889fca73 | 6565 | ret = io_write(req, issue_flags); |
2b188cc1 | 6566 | break; |
c992fe29 | 6567 | case IORING_OP_FSYNC: |
45d189c6 | 6568 | ret = io_fsync(req, issue_flags); |
c992fe29 | 6569 | break; |
221c5eb2 | 6570 | case IORING_OP_POLL_ADD: |
61e98203 | 6571 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
6572 | break; |
6573 | case IORING_OP_POLL_REMOVE: | |
c5de0036 | 6574 | ret = io_poll_update(req, issue_flags); |
221c5eb2 | 6575 | break; |
5d17b4a4 | 6576 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 6577 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 6578 | break; |
0fa03c62 | 6579 | case IORING_OP_SENDMSG: |
889fca73 | 6580 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 6581 | break; |
fddaface | 6582 | case IORING_OP_SEND: |
889fca73 | 6583 | ret = io_send(req, issue_flags); |
0fa03c62 | 6584 | break; |
aa1fa28f | 6585 | case IORING_OP_RECVMSG: |
889fca73 | 6586 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 6587 | break; |
fddaface | 6588 | case IORING_OP_RECV: |
889fca73 | 6589 | ret = io_recv(req, issue_flags); |
aa1fa28f | 6590 | break; |
5262f567 | 6591 | case IORING_OP_TIMEOUT: |
61e98203 | 6592 | ret = io_timeout(req, issue_flags); |
5262f567 | 6593 | break; |
11365043 | 6594 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 6595 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 6596 | break; |
17f2fe35 | 6597 | case IORING_OP_ACCEPT: |
889fca73 | 6598 | ret = io_accept(req, issue_flags); |
17f2fe35 | 6599 | break; |
f8e85cf2 | 6600 | case IORING_OP_CONNECT: |
889fca73 | 6601 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 6602 | break; |
62755e35 | 6603 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 6604 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 6605 | break; |
d63d1b5e | 6606 | case IORING_OP_FALLOCATE: |
45d189c6 | 6607 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 6608 | break; |
15b71abe | 6609 | case IORING_OP_OPENAT: |
45d189c6 | 6610 | ret = io_openat(req, issue_flags); |
15b71abe | 6611 | break; |
b5dba59e | 6612 | case IORING_OP_CLOSE: |
889fca73 | 6613 | ret = io_close(req, issue_flags); |
b5dba59e | 6614 | break; |
05f3fb3c | 6615 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6616 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6617 | break; |
eddc7ef5 | 6618 | case IORING_OP_STATX: |
45d189c6 | 6619 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6620 | break; |
4840e418 | 6621 | case IORING_OP_FADVISE: |
45d189c6 | 6622 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6623 | break; |
c1ca757b | 6624 | case IORING_OP_MADVISE: |
45d189c6 | 6625 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6626 | break; |
cebdb986 | 6627 | case IORING_OP_OPENAT2: |
45d189c6 | 6628 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6629 | break; |
3e4827b0 | 6630 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6631 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6632 | break; |
7d67af2c | 6633 | case IORING_OP_SPLICE: |
45d189c6 | 6634 | ret = io_splice(req, issue_flags); |
7d67af2c | 6635 | break; |
ddf0322d | 6636 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6637 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6638 | break; |
067524e9 | 6639 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6640 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6641 | break; |
f2a8d5c7 | 6642 | case IORING_OP_TEE: |
45d189c6 | 6643 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6644 | break; |
36f4fa68 | 6645 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6646 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6647 | break; |
80a261fd | 6648 | case IORING_OP_RENAMEAT: |
45d189c6 | 6649 | ret = io_renameat(req, issue_flags); |
80a261fd | 6650 | break; |
14a1143b | 6651 | case IORING_OP_UNLINKAT: |
45d189c6 | 6652 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6653 | break; |
e34a02dc DK |
6654 | case IORING_OP_MKDIRAT: |
6655 | ret = io_mkdirat(req, issue_flags); | |
6656 | break; | |
7a8721f8 DK |
6657 | case IORING_OP_SYMLINKAT: |
6658 | ret = io_symlinkat(req, issue_flags); | |
6659 | break; | |
cf30da90 DK |
6660 | case IORING_OP_LINKAT: |
6661 | ret = io_linkat(req, issue_flags); | |
6662 | break; | |
2b188cc1 JA |
6663 | default: |
6664 | ret = -EINVAL; | |
6665 | break; | |
6666 | } | |
6667 | ||
5730b27e JA |
6668 | if (creds) |
6669 | revert_creds(creds); | |
def596e9 JA |
6670 | if (ret) |
6671 | return ret; | |
b532576e | 6672 | /* If the op doesn't have a file, we're not polling for it */ |
cb3d8972 PB |
6673 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
6674 | io_iopoll_req_issued(req); | |
def596e9 JA |
6675 | |
6676 | return 0; | |
2b188cc1 JA |
6677 | } |
6678 | ||
ebc11b6c PB |
6679 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
6680 | { | |
6681 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6682 | ||
6683 | req = io_put_req_find_next(req); | |
6684 | return req ? &req->work : NULL; | |
6685 | } | |
6686 | ||
5280f7e5 | 6687 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6688 | { |
6689 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6690 | struct io_kiocb *timeout; |
561fb04a | 6691 | int ret = 0; |
2b188cc1 | 6692 | |
48dcd38d PB |
6693 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
6694 | if (!(req->flags & REQ_F_REFCOUNT)) | |
6695 | __io_req_set_refcount(req, 2); | |
6696 | else | |
6697 | req_ref_get(req); | |
5d5901a3 | 6698 | |
6df1db6b PB |
6699 | timeout = io_prep_linked_timeout(req); |
6700 | if (timeout) | |
6701 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6702 | |
dadebc35 | 6703 | /* either cancelled or io-wq is dying, so don't touch tctx->iowq */ |
4014d943 | 6704 | if (work->flags & IO_WQ_WORK_CANCEL) |
561fb04a | 6705 | ret = -ECANCELED; |
31b51510 | 6706 | |
561fb04a | 6707 | if (!ret) { |
561fb04a | 6708 | do { |
889fca73 | 6709 | ret = io_issue_sqe(req, 0); |
561fb04a JA |
6710 | /* |
6711 | * We can get EAGAIN for polled IO even though we're | |
6712 | * forcing a sync submission from here, since we can't | |
6713 | * wait for request slots on the block side. | |
6714 | */ | |
6715 | if (ret != -EAGAIN) | |
6716 | break; | |
6717 | cond_resched(); | |
6718 | } while (1); | |
6719 | } | |
31b51510 | 6720 | |
a3df7698 | 6721 | /* avoid locking problems by failing it from a clean context */ |
5d5901a3 | 6722 | if (ret) |
a3df7698 | 6723 | io_req_task_queue_fail(req, ret); |
2b188cc1 JA |
6724 | } |
6725 | ||
aeca241b | 6726 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
042b0d85 | 6727 | unsigned i) |
65e19f54 | 6728 | { |
042b0d85 | 6729 | return &table->files[i]; |
dafecf19 PB |
6730 | } |
6731 | ||
65e19f54 JA |
6732 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6733 | int index) | |
6734 | { | |
aeca241b | 6735 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
65e19f54 | 6736 | |
a04b0ac0 | 6737 | return (struct file *) (slot->file_ptr & FFS_MASK); |
65e19f54 JA |
6738 | } |
6739 | ||
a04b0ac0 | 6740 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
9a321c98 PB |
6741 | { |
6742 | unsigned long file_ptr = (unsigned long) file; | |
6743 | ||
b191e2df | 6744 | if (__io_file_supports_nowait(file, READ)) |
9a321c98 | 6745 | file_ptr |= FFS_ASYNC_READ; |
b191e2df | 6746 | if (__io_file_supports_nowait(file, WRITE)) |
9a321c98 PB |
6747 | file_ptr |= FFS_ASYNC_WRITE; |
6748 | if (S_ISREG(file_inode(file)->i_mode)) | |
6749 | file_ptr |= FFS_ISREG; | |
a04b0ac0 | 6750 | file_slot->file_ptr = file_ptr; |
65e19f54 JA |
6751 | } |
6752 | ||
ac177053 PB |
6753 | static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx, |
6754 | struct io_kiocb *req, int fd) | |
09bb8394 | 6755 | { |
8da11c19 | 6756 | struct file *file; |
ac177053 | 6757 | unsigned long file_ptr; |
09bb8394 | 6758 | |
ac177053 PB |
6759 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
6760 | return NULL; | |
6761 | fd = array_index_nospec(fd, ctx->nr_user_files); | |
6762 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; | |
6763 | file = (struct file *) (file_ptr & FFS_MASK); | |
6764 | file_ptr &= ~FFS_MASK; | |
6765 | /* mask in overlapping REQ_F and FFS bits */ | |
b191e2df | 6766 | req->flags |= (file_ptr << REQ_F_NOWAIT_READ_BIT); |
ac177053 PB |
6767 | io_req_set_rsrc_node(req); |
6768 | return file; | |
6769 | } | |
d44f554e | 6770 | |
ac177053 | 6771 | static struct file *io_file_get_normal(struct io_ring_ctx *ctx, |
ac177053 PB |
6772 | struct io_kiocb *req, int fd) |
6773 | { | |
62906e89 | 6774 | struct file *file = fget(fd); |
ac177053 PB |
6775 | |
6776 | trace_io_uring_file_get(ctx, fd); | |
09bb8394 | 6777 | |
ac177053 PB |
6778 | /* we don't allow fixed io_uring files */ |
6779 | if (file && unlikely(file->f_op == &io_uring_fops)) | |
6780 | io_req_track_inflight(req); | |
8371adf5 | 6781 | return file; |
09bb8394 JA |
6782 | } |
6783 | ||
ac177053 | 6784 | static inline struct file *io_file_get(struct io_ring_ctx *ctx, |
ac177053 PB |
6785 | struct io_kiocb *req, int fd, bool fixed) |
6786 | { | |
6787 | if (fixed) | |
6788 | return io_file_get_fixed(ctx, req, fd); | |
6789 | else | |
62906e89 | 6790 | return io_file_get_normal(ctx, req, fd); |
ac177053 PB |
6791 | } |
6792 | ||
f237c30a | 6793 | static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked) |
89b263f6 JA |
6794 | { |
6795 | struct io_kiocb *prev = req->timeout.prev; | |
8cb01fac | 6796 | int ret; |
89b263f6 JA |
6797 | |
6798 | if (prev) { | |
8cb01fac | 6799 | ret = io_try_cancel_userdata(req, prev->user_data); |
505657bc | 6800 | io_req_complete_post(req, ret ?: -ETIME, 0); |
89b263f6 | 6801 | io_put_req(prev); |
89b263f6 JA |
6802 | } else { |
6803 | io_req_complete_post(req, -ETIME, 0); | |
6804 | } | |
6805 | } | |
6806 | ||
2665abfd | 6807 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6808 | { |
ad8a48ac JA |
6809 | struct io_timeout_data *data = container_of(timer, |
6810 | struct io_timeout_data, timer); | |
90cd7e42 | 6811 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6812 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6813 | unsigned long flags; |
2665abfd | 6814 | |
89b263f6 | 6815 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
90cd7e42 PB |
6816 | prev = req->timeout.head; |
6817 | req->timeout.head = NULL; | |
2665abfd JA |
6818 | |
6819 | /* | |
6820 | * We don't expect the list to be empty, that will only happen if we | |
6821 | * race with the completion of the linked work. | |
6822 | */ | |
447c19f3 | 6823 | if (prev) { |
f2f87370 | 6824 | io_remove_next_linked(prev); |
447c19f3 PB |
6825 | if (!req_ref_inc_not_zero(prev)) |
6826 | prev = NULL; | |
6827 | } | |
ef9dd637 | 6828 | list_del(&req->timeout.list); |
89b263f6 JA |
6829 | req->timeout.prev = prev; |
6830 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); | |
2665abfd | 6831 | |
89b263f6 JA |
6832 | req->io_task_work.func = io_req_task_link_timeout; |
6833 | io_req_task_work_add(req); | |
2665abfd JA |
6834 | return HRTIMER_NORESTART; |
6835 | } | |
6836 | ||
de968c18 | 6837 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6838 | { |
de968c18 PB |
6839 | struct io_ring_ctx *ctx = req->ctx; |
6840 | ||
89b263f6 | 6841 | spin_lock_irq(&ctx->timeout_lock); |
76a46e06 | 6842 | /* |
f2f87370 PB |
6843 | * If the back reference is NULL, then our linked request finished |
6844 | * before we got a chance to setup the timer | |
76a46e06 | 6845 | */ |
90cd7e42 | 6846 | if (req->timeout.head) { |
e8c2bc1f | 6847 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6848 | |
ad8a48ac JA |
6849 | data->timer.function = io_link_timeout_fn; |
6850 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6851 | data->mode); | |
ef9dd637 | 6852 | list_add_tail(&req->timeout.list, &ctx->ltimeout_list); |
2665abfd | 6853 | } |
89b263f6 | 6854 | spin_unlock_irq(&ctx->timeout_lock); |
2665abfd | 6855 | /* drop submission reference */ |
76a46e06 JA |
6856 | io_put_req(req); |
6857 | } | |
2665abfd | 6858 | |
d475a9a6 PB |
6859 | static void io_queue_sqe_arm_apoll(struct io_kiocb *req) |
6860 | __must_hold(&req->ctx->uring_lock) | |
6861 | { | |
6862 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); | |
6863 | ||
6864 | switch (io_arm_poll_handler(req)) { | |
6865 | case IO_APOLL_READY: | |
6866 | if (linked_timeout) { | |
6867 | io_unprep_linked_timeout(req); | |
6868 | linked_timeout = NULL; | |
6869 | } | |
6870 | io_req_task_queue(req); | |
6871 | break; | |
6872 | case IO_APOLL_ABORTED: | |
6873 | /* | |
6874 | * Queued up for async execution, worker will release | |
6875 | * submit reference when the iocb is actually submitted. | |
6876 | */ | |
6877 | io_queue_async_work(req, NULL); | |
6878 | break; | |
6879 | } | |
6880 | ||
6881 | if (linked_timeout) | |
6882 | io_queue_linked_timeout(linked_timeout); | |
6883 | } | |
6884 | ||
6885 | static inline void __io_queue_sqe(struct io_kiocb *req) | |
282cdc86 | 6886 | __must_hold(&req->ctx->uring_lock) |
2b188cc1 | 6887 | { |
906c6caa | 6888 | struct io_kiocb *linked_timeout; |
e0c5c576 | 6889 | int ret; |
2b188cc1 | 6890 | |
c5eef2b9 | 6891 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 6892 | |
fff4e40e PB |
6893 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
6894 | io_req_add_compl_list(req); | |
d9f9d284 | 6895 | return; |
fff4e40e | 6896 | } |
491381ce JA |
6897 | /* |
6898 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6899 | * doesn't support non-blocking read/write attempts | |
6900 | */ | |
1840038e | 6901 | if (likely(!ret)) { |
906c6caa PB |
6902 | linked_timeout = io_prep_linked_timeout(req); |
6903 | if (linked_timeout) | |
6904 | io_queue_linked_timeout(linked_timeout); | |
1840038e | 6905 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
d475a9a6 | 6906 | io_queue_sqe_arm_apoll(req); |
0d63c148 | 6907 | } else { |
f41db273 | 6908 | io_req_complete_failed(req, ret); |
9e645e11 | 6909 | } |
2b188cc1 JA |
6910 | } |
6911 | ||
4652fe3f | 6912 | static void io_queue_sqe_fallback(struct io_kiocb *req) |
282cdc86 | 6913 | __must_hold(&req->ctx->uring_lock) |
4fe2c963 | 6914 | { |
4652fe3f | 6915 | if (req->flags & REQ_F_FAIL) { |
c6d3d9cb | 6916 | io_req_complete_fail_submit(req); |
e0eb71dc PB |
6917 | } else if (unlikely(req->ctx->drain_active)) { |
6918 | io_drain_req(req); | |
76cc33d7 PB |
6919 | } else { |
6920 | int ret = io_req_prep_async(req); | |
6921 | ||
6922 | if (unlikely(ret)) | |
6923 | io_req_complete_failed(req, ret); | |
6924 | else | |
f237c30a | 6925 | io_queue_async_work(req, NULL); |
ce35a47a | 6926 | } |
4fe2c963 JL |
6927 | } |
6928 | ||
4652fe3f PB |
6929 | static inline void io_queue_sqe(struct io_kiocb *req) |
6930 | __must_hold(&req->ctx->uring_lock) | |
6931 | { | |
6932 | if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) | |
6933 | __io_queue_sqe(req); | |
6934 | else | |
6935 | io_queue_sqe_fallback(req); | |
6936 | } | |
6937 | ||
b16fed66 PB |
6938 | /* |
6939 | * Check SQE restrictions (opcode and flags). | |
6940 | * | |
6941 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6942 | */ | |
6943 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6944 | struct io_kiocb *req, | |
6945 | unsigned int sqe_flags) | |
4fe2c963 | 6946 | { |
b16fed66 PB |
6947 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
6948 | return false; | |
6949 | ||
6950 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6951 | ctx->restrictions.sqe_flags_required) | |
6952 | return false; | |
6953 | ||
6954 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
6955 | ctx->restrictions.sqe_flags_required)) | |
6956 | return false; | |
6957 | ||
6958 | return true; | |
4fe2c963 JL |
6959 | } |
6960 | ||
22b2ca31 PB |
6961 | static void io_init_req_drain(struct io_kiocb *req) |
6962 | { | |
6963 | struct io_ring_ctx *ctx = req->ctx; | |
6964 | struct io_kiocb *head = ctx->submit_state.link.head; | |
6965 | ||
6966 | ctx->drain_active = true; | |
6967 | if (head) { | |
6968 | /* | |
6969 | * If we need to drain a request in the middle of a link, drain | |
6970 | * the head request and the next request/link after the current | |
6971 | * link. Considering sequential execution of links, | |
6972 | * IOSQE_IO_DRAIN will be maintained for every request of our | |
6973 | * link. | |
6974 | */ | |
6975 | head->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC; | |
6976 | ctx->drain_next = true; | |
6977 | } | |
6978 | } | |
6979 | ||
b16fed66 PB |
6980 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
6981 | const struct io_uring_sqe *sqe) | |
282cdc86 | 6982 | __must_hold(&ctx->uring_lock) |
b16fed66 PB |
6983 | { |
6984 | struct io_submit_state *state; | |
6985 | unsigned int sqe_flags; | |
fc0ae024 | 6986 | int personality; |
b16fed66 | 6987 | |
864ea921 | 6988 | /* req is partially pre-initialised, see io_preinit_req() */ |
b16fed66 PB |
6989 | req->opcode = READ_ONCE(sqe->opcode); |
6990 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
6991 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
6992 | req->user_data = READ_ONCE(sqe->user_data); | |
b16fed66 | 6993 | req->file = NULL; |
b16fed66 | 6994 | req->fixed_rsrc_refs = NULL; |
b16fed66 | 6995 | req->task = current; |
b16fed66 | 6996 | |
b16fed66 PB |
6997 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
6998 | return -EINVAL; | |
68fe256a PB |
6999 | if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) { |
7000 | /* enforce forwards compatibility on users */ | |
7001 | if (sqe_flags & ~SQE_VALID_FLAGS) | |
7002 | return -EINVAL; | |
7003 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
7004 | !io_op_defs[req->opcode].buffer_select) | |
7005 | return -EOPNOTSUPP; | |
22b2ca31 PB |
7006 | if (sqe_flags & IOSQE_IO_DRAIN) |
7007 | io_init_req_drain(req); | |
2a56a9bd PB |
7008 | } |
7009 | if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) { | |
7010 | if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags)) | |
7011 | return -EACCES; | |
7012 | /* knock it to the slow queue path, will be drained there */ | |
7013 | if (ctx->drain_active) | |
7014 | req->flags |= REQ_F_FORCE_ASYNC; | |
7015 | /* if there is no link, we're at "next" request and need to drain */ | |
7016 | if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) { | |
7017 | ctx->drain_next = false; | |
7018 | ctx->drain_active = true; | |
22b2ca31 | 7019 | req->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC; |
2a56a9bd | 7020 | } |
68fe256a | 7021 | } |
b16fed66 | 7022 | |
003e8dcc JA |
7023 | personality = READ_ONCE(sqe->personality); |
7024 | if (personality) { | |
c10d1f98 PB |
7025 | req->creds = xa_load(&ctx->personalities, personality); |
7026 | if (!req->creds) | |
003e8dcc | 7027 | return -EINVAL; |
c10d1f98 | 7028 | get_cred(req->creds); |
b8e64b53 | 7029 | req->flags |= REQ_F_CREDS; |
003e8dcc | 7030 | } |
b16fed66 PB |
7031 | state = &ctx->submit_state; |
7032 | ||
7033 | /* | |
7034 | * Plug now if we have more than 1 IO left after this, and the target | |
7035 | * is potentially a read/write to block based storage. | |
7036 | */ | |
4b628aeb | 7037 | if (state->need_plug && io_op_defs[req->opcode].plug) { |
b16fed66 PB |
7038 | blk_start_plug(&state->plug); |
7039 | state->plug_started = true; | |
4b628aeb | 7040 | state->need_plug = false; |
b16fed66 PB |
7041 | } |
7042 | ||
7043 | if (io_op_defs[req->opcode].needs_file) { | |
62906e89 | 7044 | req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd), |
ac177053 | 7045 | (sqe_flags & IOSQE_FIXED_FILE)); |
b16fed66 | 7046 | if (unlikely(!req->file)) |
fc0ae024 | 7047 | return -EBADF; |
b16fed66 | 7048 | } |
fc0ae024 PB |
7049 | |
7050 | return io_req_prep(req, sqe); | |
b16fed66 PB |
7051 | } |
7052 | ||
a6b8cadc | 7053 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 7054 | const struct io_uring_sqe *sqe) |
282cdc86 | 7055 | __must_hold(&ctx->uring_lock) |
9e645e11 | 7056 | { |
a1ab7b35 | 7057 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 7058 | int ret; |
9e645e11 | 7059 | |
a6b8cadc PB |
7060 | ret = io_init_req(ctx, req, sqe); |
7061 | if (unlikely(ret)) { | |
a87acfde JA |
7062 | trace_io_uring_req_failed(sqe, ret); |
7063 | ||
a8295b98 | 7064 | /* fail even hard links since we don't submit */ |
de59bc10 | 7065 | if (link->head) { |
a8295b98 HX |
7066 | /* |
7067 | * we can judge a link req is failed or cancelled by if | |
7068 | * REQ_F_FAIL is set, but the head is an exception since | |
7069 | * it may be set REQ_F_FAIL because of other req's failure | |
7070 | * so let's leverage req->result to distinguish if a head | |
7071 | * is set REQ_F_FAIL because of its failure or other req's | |
7072 | * failure so that we can set the correct ret code for it. | |
7073 | * init result here to avoid affecting the normal path. | |
7074 | */ | |
7075 | if (!(link->head->flags & REQ_F_FAIL)) | |
7076 | req_fail_link_node(link->head, -ECANCELED); | |
7077 | } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { | |
7078 | /* | |
7079 | * the current req is a normal req, we should return | |
7080 | * error and thus break the submittion loop. | |
7081 | */ | |
7082 | io_req_complete_failed(req, ret); | |
7083 | return ret; | |
de59bc10 | 7084 | } |
a8295b98 | 7085 | req_fail_link_node(req, ret); |
a6b8cadc | 7086 | } |
441b8a78 | 7087 | |
be7053b7 | 7088 | /* don't need @sqe from now on */ |
236daeae OL |
7089 | trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data, |
7090 | req->flags, true, | |
7091 | ctx->flags & IORING_SETUP_SQPOLL); | |
a6b8cadc | 7092 | |
9e645e11 JA |
7093 | /* |
7094 | * If we already have a head request, queue this one for async | |
7095 | * submittal once the head completes. If we don't have a head but | |
7096 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
7097 | * submitted sync once the chain is complete. If none of those | |
7098 | * conditions are true (normal request), then just queue it. | |
7099 | */ | |
863e0560 PB |
7100 | if (link->head) { |
7101 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 7102 | |
a8295b98 HX |
7103 | if (!(req->flags & REQ_F_FAIL)) { |
7104 | ret = io_req_prep_async(req); | |
7105 | if (unlikely(ret)) { | |
7106 | req_fail_link_node(req, ret); | |
7107 | if (!(head->flags & REQ_F_FAIL)) | |
7108 | req_fail_link_node(head, -ECANCELED); | |
7109 | } | |
7110 | } | |
9d76377f | 7111 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 7112 | link->last->link = req; |
863e0560 | 7113 | link->last = req; |
32fe525b | 7114 | |
f15a3431 PB |
7115 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) |
7116 | return 0; | |
32fe525b | 7117 | /* last request of a link, enqueue the link */ |
f15a3431 PB |
7118 | link->head = NULL; |
7119 | req = head; | |
7120 | } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { | |
7121 | link->head = req; | |
7122 | link->last = req; | |
7123 | return 0; | |
9e645e11 | 7124 | } |
2e6e1fde | 7125 | |
f15a3431 | 7126 | io_queue_sqe(req); |
1d4240cc | 7127 | return 0; |
9e645e11 JA |
7128 | } |
7129 | ||
9a56a232 JA |
7130 | /* |
7131 | * Batched submission is done, ensure local IO is flushed out. | |
7132 | */ | |
553deffd | 7133 | static void io_submit_state_end(struct io_ring_ctx *ctx) |
9a56a232 | 7134 | { |
553deffd PB |
7135 | struct io_submit_state *state = &ctx->submit_state; |
7136 | ||
a1ab7b35 | 7137 | if (state->link.head) |
de59bc10 | 7138 | io_queue_sqe(state->link.head); |
553deffd | 7139 | /* flush only after queuing links as they can generate completions */ |
c450178d | 7140 | io_submit_flush_completions(ctx); |
27926b68 JA |
7141 | if (state->plug_started) |
7142 | blk_finish_plug(&state->plug); | |
9a56a232 JA |
7143 | } |
7144 | ||
7145 | /* | |
7146 | * Start submission side cache. | |
7147 | */ | |
7148 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 7149 | unsigned int max_ios) |
9a56a232 | 7150 | { |
27926b68 | 7151 | state->plug_started = false; |
4b628aeb | 7152 | state->need_plug = max_ios > 2; |
a1ab7b35 PB |
7153 | /* set only head, no need to init link_last in advance */ |
7154 | state->link.head = NULL; | |
9a56a232 JA |
7155 | } |
7156 | ||
2b188cc1 JA |
7157 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
7158 | { | |
75b28aff | 7159 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 7160 | |
caf582c6 PB |
7161 | /* |
7162 | * Ensure any loads from the SQEs are done at this point, | |
7163 | * since once we write the new head, the application could | |
7164 | * write new data to them. | |
7165 | */ | |
7166 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
7167 | } |
7168 | ||
2b188cc1 | 7169 | /* |
dd9ae8a0 | 7170 | * Fetch an sqe, if one is available. Note this returns a pointer to memory |
2b188cc1 JA |
7171 | * that is mapped by userspace. This means that care needs to be taken to |
7172 | * ensure that reads are stable, as we cannot rely on userspace always | |
7173 | * being a good citizen. If members of the sqe are validated and then later | |
7174 | * used, it's important that those reads are done through READ_ONCE() to | |
7175 | * prevent a re-load down the line. | |
7176 | */ | |
709b302f | 7177 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 7178 | { |
ea5ab3b5 | 7179 | unsigned head, mask = ctx->sq_entries - 1; |
17d3aeb3 | 7180 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
2b188cc1 JA |
7181 | |
7182 | /* | |
7183 | * The cached sq head (or cq tail) serves two purposes: | |
7184 | * | |
7185 | * 1) allows us to batch the cost of updating the user visible | |
7186 | * head updates. | |
7187 | * 2) allows the kernel side to track the head on its own, even | |
7188 | * though the application is the one updating it. | |
7189 | */ | |
17d3aeb3 | 7190 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
709b302f PB |
7191 | if (likely(head < ctx->sq_entries)) |
7192 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
7193 | |
7194 | /* drop invalid entries */ | |
15641e42 PB |
7195 | ctx->cq_extra--; |
7196 | WRITE_ONCE(ctx->rings->sq_dropped, | |
7197 | READ_ONCE(ctx->rings->sq_dropped) + 1); | |
709b302f PB |
7198 | return NULL; |
7199 | } | |
7200 | ||
0f212204 | 7201 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
282cdc86 | 7202 | __must_hold(&ctx->uring_lock) |
6c271ce2 | 7203 | { |
69629809 | 7204 | unsigned int entries = io_sqring_entries(ctx); |
46c4e16a | 7205 | int submitted = 0; |
6c271ce2 | 7206 | |
51d48dab | 7207 | if (unlikely(!entries)) |
69629809 | 7208 | return 0; |
ee7d46d9 | 7209 | /* make sure SQ entry isn't read before tail */ |
69629809 | 7210 | nr = min3(nr, ctx->sq_entries, entries); |
51d48dab | 7211 | if (unlikely(!percpu_ref_tryget_many(&ctx->refs, nr))) |
2b85edfc | 7212 | return -EAGAIN; |
9a10867a | 7213 | io_get_task_refs(nr); |
6c271ce2 | 7214 | |
ba88ff11 | 7215 | io_submit_state_start(&ctx->submit_state, nr); |
69629809 | 7216 | do { |
3529d8c2 | 7217 | const struct io_uring_sqe *sqe; |
196be95c | 7218 | struct io_kiocb *req; |
fb5ccc98 | 7219 | |
258b29a9 | 7220 | req = io_alloc_req(ctx); |
196be95c PB |
7221 | if (unlikely(!req)) { |
7222 | if (!submitted) | |
7223 | submitted = -EAGAIN; | |
fb5ccc98 | 7224 | break; |
196be95c | 7225 | } |
4fccfcbb PB |
7226 | sqe = io_get_sqe(ctx); |
7227 | if (unlikely(!sqe)) { | |
c2b6c6bc | 7228 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
4fccfcbb PB |
7229 | break; |
7230 | } | |
d3656344 JA |
7231 | /* will complete beyond this point, count as submitted */ |
7232 | submitted++; | |
a1ab7b35 | 7233 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 7234 | break; |
69629809 | 7235 | } while (submitted < nr); |
6c271ce2 | 7236 | |
9466f437 PB |
7237 | if (unlikely(submitted != nr)) { |
7238 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 | 7239 | int unused = nr - ref_used; |
9466f437 | 7240 | |
09899b19 | 7241 | current->io_uring->cached_refs += unused; |
d8a6df10 | 7242 | percpu_ref_put_many(&ctx->refs, unused); |
9466f437 | 7243 | } |
6c271ce2 | 7244 | |
553deffd | 7245 | io_submit_state_end(ctx); |
ae9428ca PB |
7246 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
7247 | io_commit_sqring(ctx); | |
7248 | ||
6c271ce2 JA |
7249 | return submitted; |
7250 | } | |
7251 | ||
e4b6d902 PB |
7252 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
7253 | { | |
7254 | return READ_ONCE(sqd->state); | |
7255 | } | |
7256 | ||
23b3628e XW |
7257 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
7258 | { | |
7259 | /* Tell userspace we may need a wakeup call */ | |
79ebeaee | 7260 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7261 | WRITE_ONCE(ctx->rings->sq_flags, |
7262 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7263 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7264 | } |
7265 | ||
7266 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
7267 | { | |
79ebeaee | 7268 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7269 | WRITE_ONCE(ctx->rings->sq_flags, |
7270 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7271 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7272 | } |
7273 | ||
08369246 | 7274 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 7275 | { |
c8d1ba58 | 7276 | unsigned int to_submit; |
bdcd3eab | 7277 | int ret = 0; |
6c271ce2 | 7278 | |
c8d1ba58 | 7279 | to_submit = io_sqring_entries(ctx); |
e95eee2d | 7280 | /* if we're handling multiple rings, cap submit size for fairness */ |
4ce8ad95 OL |
7281 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
7282 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; | |
e95eee2d | 7283 | |
5eef4e87 | 7284 | if (!wq_list_empty(&ctx->iopoll_list) || to_submit) { |
948e1947 PB |
7285 | const struct cred *creds = NULL; |
7286 | ||
7287 | if (ctx->sq_creds != current_cred()) | |
7288 | creds = override_creds(ctx->sq_creds); | |
a4c0b3de | 7289 | |
c8d1ba58 | 7290 | mutex_lock(&ctx->uring_lock); |
5eef4e87 | 7291 | if (!wq_list_empty(&ctx->iopoll_list)) |
5ba3c874 | 7292 | io_do_iopoll(ctx, true); |
906a3c6f | 7293 | |
3b763ba1 PB |
7294 | /* |
7295 | * Don't submit if refs are dying, good for io_uring_register(), | |
7296 | * but also it is relied upon by io_ring_exit_work() | |
7297 | */ | |
0298ef96 PB |
7298 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
7299 | !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
08369246 | 7300 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 | 7301 | mutex_unlock(&ctx->uring_lock); |
6c271ce2 | 7302 | |
acfb381d PB |
7303 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
7304 | wake_up(&ctx->sqo_sq_wait); | |
948e1947 PB |
7305 | if (creds) |
7306 | revert_creds(creds); | |
acfb381d | 7307 | } |
6c271ce2 | 7308 | |
08369246 XW |
7309 | return ret; |
7310 | } | |
6c271ce2 | 7311 | |
08369246 XW |
7312 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
7313 | { | |
7314 | struct io_ring_ctx *ctx; | |
7315 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 7316 | |
c9dca27d PB |
7317 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7318 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); | |
08369246 | 7319 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 7320 | } |
6c271ce2 | 7321 | |
e4b6d902 PB |
7322 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
7323 | { | |
7324 | bool did_sig = false; | |
7325 | struct ksignal ksig; | |
7326 | ||
7327 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || | |
7328 | signal_pending(current)) { | |
7329 | mutex_unlock(&sqd->lock); | |
7330 | if (signal_pending(current)) | |
7331 | did_sig = get_signal(&ksig); | |
7332 | cond_resched(); | |
7333 | mutex_lock(&sqd->lock); | |
7334 | } | |
e4b6d902 PB |
7335 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
7336 | } | |
7337 | ||
c8d1ba58 JA |
7338 | static int io_sq_thread(void *data) |
7339 | { | |
69fb2131 JA |
7340 | struct io_sq_data *sqd = data; |
7341 | struct io_ring_ctx *ctx; | |
a0d9205f | 7342 | unsigned long timeout = 0; |
37d1e2e3 | 7343 | char buf[TASK_COMM_LEN]; |
08369246 | 7344 | DEFINE_WAIT(wait); |
6c271ce2 | 7345 | |
696ee88a | 7346 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
37d1e2e3 | 7347 | set_task_comm(current, buf); |
37d1e2e3 JA |
7348 | |
7349 | if (sqd->sq_cpu != -1) | |
7350 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
7351 | else | |
7352 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
7353 | current->flags |= PF_NO_SETAFFINITY; | |
7354 | ||
09a6f4ef | 7355 | mutex_lock(&sqd->lock); |
e4b6d902 | 7356 | while (1) { |
1a924a80 | 7357 | bool cap_entries, sqt_spin = false; |
c1edbf5f | 7358 | |
e4b6d902 PB |
7359 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
7360 | if (io_sqd_handle_event(sqd)) | |
c7d95613 | 7361 | break; |
08369246 XW |
7362 | timeout = jiffies + sqd->sq_thread_idle; |
7363 | } | |
e4b6d902 | 7364 | |
e95eee2d | 7365 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 7366 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
948e1947 | 7367 | int ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a | 7368 | |
5eef4e87 | 7369 | if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list))) |
08369246 | 7370 | sqt_spin = true; |
69fb2131 | 7371 | } |
dd432ea5 PB |
7372 | if (io_run_task_work()) |
7373 | sqt_spin = true; | |
6c271ce2 | 7374 | |
08369246 | 7375 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 7376 | cond_resched(); |
08369246 XW |
7377 | if (sqt_spin) |
7378 | timeout = jiffies + sqd->sq_thread_idle; | |
7379 | continue; | |
7380 | } | |
7381 | ||
08369246 | 7382 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
dd432ea5 | 7383 | if (!io_sqd_events_pending(sqd) && !current->task_works) { |
1a924a80 PB |
7384 | bool needs_sched = true; |
7385 | ||
724cb4f9 | 7386 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
aaa9f0f4 PB |
7387 | io_ring_set_wakeup_flag(ctx); |
7388 | ||
724cb4f9 | 7389 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
5eef4e87 | 7390 | !wq_list_empty(&ctx->iopoll_list)) { |
724cb4f9 HX |
7391 | needs_sched = false; |
7392 | break; | |
7393 | } | |
7394 | if (io_sqring_entries(ctx)) { | |
7395 | needs_sched = false; | |
7396 | break; | |
7397 | } | |
7398 | } | |
7399 | ||
7400 | if (needs_sched) { | |
7401 | mutex_unlock(&sqd->lock); | |
7402 | schedule(); | |
7403 | mutex_lock(&sqd->lock); | |
7404 | } | |
69fb2131 JA |
7405 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7406 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 7407 | } |
08369246 XW |
7408 | |
7409 | finish_wait(&sqd->wait, &wait); | |
7410 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 | 7411 | } |
28cea78a | 7412 | |
78cc687b | 7413 | io_uring_cancel_generic(true, sqd); |
37d1e2e3 | 7414 | sqd->thread = NULL; |
05962f95 | 7415 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
5f3f26f9 | 7416 | io_ring_set_wakeup_flag(ctx); |
521d6a73 | 7417 | io_run_task_work(); |
734551df PB |
7418 | mutex_unlock(&sqd->lock); |
7419 | ||
37d1e2e3 JA |
7420 | complete(&sqd->exited); |
7421 | do_exit(0); | |
6c271ce2 JA |
7422 | } |
7423 | ||
bda52162 JA |
7424 | struct io_wait_queue { |
7425 | struct wait_queue_entry wq; | |
7426 | struct io_ring_ctx *ctx; | |
5fd46178 | 7427 | unsigned cq_tail; |
bda52162 JA |
7428 | unsigned nr_timeouts; |
7429 | }; | |
7430 | ||
6c503150 | 7431 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
7432 | { |
7433 | struct io_ring_ctx *ctx = iowq->ctx; | |
5fd46178 | 7434 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
bda52162 JA |
7435 | |
7436 | /* | |
d195a66e | 7437 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
7438 | * started waiting. For timeouts, we always want to return to userspace, |
7439 | * regardless of event count. | |
7440 | */ | |
5fd46178 | 7441 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
bda52162 JA |
7442 | } |
7443 | ||
7444 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
7445 | int wake_flags, void *key) | |
7446 | { | |
7447 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
7448 | wq); | |
7449 | ||
6c503150 PB |
7450 | /* |
7451 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
7452 | * the task, and the next invocation will do it. | |
7453 | */ | |
5ed7a37d | 7454 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow)) |
6c503150 PB |
7455 | return autoremove_wake_function(curr, mode, wake_flags, key); |
7456 | return -1; | |
bda52162 JA |
7457 | } |
7458 | ||
af9c1a44 JA |
7459 | static int io_run_task_work_sig(void) |
7460 | { | |
7461 | if (io_run_task_work()) | |
7462 | return 1; | |
7463 | if (!signal_pending(current)) | |
7464 | return 0; | |
0b8cfa97 | 7465 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
792ee0f6 | 7466 | return -ERESTARTSYS; |
af9c1a44 JA |
7467 | return -EINTR; |
7468 | } | |
7469 | ||
eeb60b9a PB |
7470 | /* when returns >0, the caller should retry */ |
7471 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
7472 | struct io_wait_queue *iowq, | |
7473 | signed long *timeout) | |
7474 | { | |
7475 | int ret; | |
7476 | ||
7477 | /* make sure we run task_work before checking for signals */ | |
7478 | ret = io_run_task_work_sig(); | |
7479 | if (ret || io_should_wake(iowq)) | |
7480 | return ret; | |
7481 | /* let the caller flush overflows, retry */ | |
5ed7a37d | 7482 | if (test_bit(0, &ctx->check_cq_overflow)) |
eeb60b9a PB |
7483 | return 1; |
7484 | ||
7485 | *timeout = schedule_timeout(*timeout); | |
7486 | return !*timeout ? -ETIME : 1; | |
7487 | } | |
7488 | ||
2b188cc1 JA |
7489 | /* |
7490 | * Wait until events become available, if we don't already have some. The | |
7491 | * application must reap them itself, as they reside on the shared cq ring. | |
7492 | */ | |
7493 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
7494 | const sigset_t __user *sig, size_t sigsz, |
7495 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 7496 | { |
90291099 | 7497 | struct io_wait_queue iowq; |
75b28aff | 7498 | struct io_rings *rings = ctx->rings; |
c1d5a224 PB |
7499 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
7500 | int ret; | |
2b188cc1 | 7501 | |
b41e9852 | 7502 | do { |
90f67366 | 7503 | io_cqring_overflow_flush(ctx); |
6c503150 | 7504 | if (io_cqring_events(ctx) >= min_events) |
b41e9852 | 7505 | return 0; |
4c6e277c | 7506 | if (!io_run_task_work()) |
b41e9852 | 7507 | break; |
b41e9852 | 7508 | } while (1); |
2b188cc1 | 7509 | |
44df58d4 XW |
7510 | if (uts) { |
7511 | struct timespec64 ts; | |
7512 | ||
7513 | if (get_timespec64(&ts, uts)) | |
7514 | return -EFAULT; | |
7515 | timeout = timespec64_to_jiffies(&ts); | |
7516 | } | |
7517 | ||
2b188cc1 | 7518 | if (sig) { |
9e75ad5d AB |
7519 | #ifdef CONFIG_COMPAT |
7520 | if (in_compat_syscall()) | |
7521 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 7522 | sigsz); |
9e75ad5d AB |
7523 | else |
7524 | #endif | |
b772434b | 7525 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 7526 | |
2b188cc1 JA |
7527 | if (ret) |
7528 | return ret; | |
7529 | } | |
7530 | ||
90291099 PB |
7531 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
7532 | iowq.wq.private = current; | |
7533 | INIT_LIST_HEAD(&iowq.wq.entry); | |
7534 | iowq.ctx = ctx; | |
bda52162 | 7535 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
5fd46178 | 7536 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
90291099 | 7537 | |
c826bd7a | 7538 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 7539 | do { |
ca0a2651 | 7540 | /* if we can't even flush overflow, don't wait for more */ |
90f67366 | 7541 | if (!io_cqring_overflow_flush(ctx)) { |
ca0a2651 JA |
7542 | ret = -EBUSY; |
7543 | break; | |
7544 | } | |
311997b3 | 7545 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
bda52162 | 7546 | TASK_INTERRUPTIBLE); |
eeb60b9a | 7547 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
311997b3 | 7548 | finish_wait(&ctx->cq_wait, &iowq.wq); |
ca0a2651 | 7549 | cond_resched(); |
eeb60b9a | 7550 | } while (ret > 0); |
bda52162 | 7551 | |
b7db41c9 | 7552 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7553 | |
75b28aff | 7554 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7555 | } |
7556 | ||
9123c8ff | 7557 | static void io_free_page_table(void **table, size_t size) |
05f3fb3c | 7558 | { |
9123c8ff | 7559 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
05f3fb3c | 7560 | |
846a4ef2 | 7561 | for (i = 0; i < nr_tables; i++) |
9123c8ff PB |
7562 | kfree(table[i]); |
7563 | kfree(table); | |
7564 | } | |
7565 | ||
7566 | static void **io_alloc_page_table(size_t size) | |
7567 | { | |
7568 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); | |
7569 | size_t init_size = size; | |
7570 | void **table; | |
7571 | ||
0bea96f5 | 7572 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7573 | if (!table) |
7574 | return NULL; | |
7575 | ||
7576 | for (i = 0; i < nr_tables; i++) { | |
27f6b318 | 7577 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
9123c8ff | 7578 | |
0bea96f5 | 7579 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7580 | if (!table[i]) { |
7581 | io_free_page_table(table, init_size); | |
7582 | return NULL; | |
7583 | } | |
7584 | size -= this_size; | |
7585 | } | |
7586 | return table; | |
05f3fb3c JA |
7587 | } |
7588 | ||
28a9fe25 | 7589 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
1642b445 | 7590 | { |
28a9fe25 PB |
7591 | percpu_ref_exit(&ref_node->refs); |
7592 | kfree(ref_node); | |
1642b445 PB |
7593 | } |
7594 | ||
b9bd2bea PB |
7595 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
7596 | { | |
7597 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); | |
7598 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; | |
7599 | unsigned long flags; | |
7600 | bool first_add = false; | |
7601 | ||
7602 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); | |
7603 | node->done = true; | |
7604 | ||
7605 | while (!list_empty(&ctx->rsrc_ref_list)) { | |
7606 | node = list_first_entry(&ctx->rsrc_ref_list, | |
7607 | struct io_rsrc_node, node); | |
7608 | /* recycle ref nodes in order */ | |
7609 | if (!node->done) | |
7610 | break; | |
7611 | list_del(&node->node); | |
7612 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); | |
7613 | } | |
7614 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); | |
7615 | ||
7616 | if (first_add) | |
7617 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ); | |
7618 | } | |
7619 | ||
7620 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx) | |
7621 | { | |
7622 | struct io_rsrc_node *ref_node; | |
7623 | ||
7624 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); | |
7625 | if (!ref_node) | |
7626 | return NULL; | |
7627 | ||
7628 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, | |
7629 | 0, GFP_KERNEL)) { | |
7630 | kfree(ref_node); | |
7631 | return NULL; | |
7632 | } | |
7633 | INIT_LIST_HEAD(&ref_node->node); | |
7634 | INIT_LIST_HEAD(&ref_node->rsrc_list); | |
7635 | ref_node->done = false; | |
7636 | return ref_node; | |
7637 | } | |
7638 | ||
a7f0ed5a PB |
7639 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
7640 | struct io_rsrc_data *data_to_kill) | |
6b06314c | 7641 | { |
a7f0ed5a PB |
7642 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
7643 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); | |
6b06314c | 7644 | |
a7f0ed5a PB |
7645 | if (data_to_kill) { |
7646 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; | |
82fbcfa9 | 7647 | |
a7f0ed5a | 7648 | rsrc_node->rsrc_data = data_to_kill; |
4956b9ea | 7649 | spin_lock_irq(&ctx->rsrc_ref_lock); |
a7f0ed5a | 7650 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
4956b9ea | 7651 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
82fbcfa9 | 7652 | |
3e942498 | 7653 | atomic_inc(&data_to_kill->refs); |
a7f0ed5a PB |
7654 | percpu_ref_kill(&rsrc_node->refs); |
7655 | ctx->rsrc_node = NULL; | |
7656 | } | |
6b06314c | 7657 | |
a7f0ed5a PB |
7658 | if (!ctx->rsrc_node) { |
7659 | ctx->rsrc_node = ctx->rsrc_backup_node; | |
7660 | ctx->rsrc_backup_node = NULL; | |
7661 | } | |
8bad28d8 HX |
7662 | } |
7663 | ||
a7f0ed5a | 7664 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
8dd03afe PB |
7665 | { |
7666 | if (ctx->rsrc_backup_node) | |
7667 | return 0; | |
b895c9a6 | 7668 | ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx); |
8dd03afe | 7669 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
8bad28d8 HX |
7670 | } |
7671 | ||
40ae0ff7 | 7672 | static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, struct io_ring_ctx *ctx) |
8bad28d8 HX |
7673 | { |
7674 | int ret; | |
05589553 | 7675 | |
215c3902 | 7676 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
8bad28d8 HX |
7677 | if (data->quiesce) |
7678 | return -ENXIO; | |
05589553 | 7679 | |
8bad28d8 | 7680 | data->quiesce = true; |
1ffc5422 | 7681 | do { |
a7f0ed5a | 7682 | ret = io_rsrc_node_switch_start(ctx); |
8dd03afe | 7683 | if (ret) |
f2303b1f | 7684 | break; |
a7f0ed5a | 7685 | io_rsrc_node_switch(ctx, data); |
f2303b1f | 7686 | |
3e942498 PB |
7687 | /* kill initial ref, already quiesced if zero */ |
7688 | if (atomic_dec_and_test(&data->refs)) | |
7689 | break; | |
c018db4a | 7690 | mutex_unlock(&ctx->uring_lock); |
8bad28d8 | 7691 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 | 7692 | ret = wait_for_completion_interruptible(&data->done); |
c018db4a JA |
7693 | if (!ret) { |
7694 | mutex_lock(&ctx->uring_lock); | |
1ffc5422 | 7695 | break; |
c018db4a | 7696 | } |
8bad28d8 | 7697 | |
3e942498 PB |
7698 | atomic_inc(&data->refs); |
7699 | /* wait for all works potentially completing data->done */ | |
7700 | flush_delayed_work(&ctx->rsrc_put_work); | |
cb5e1b81 | 7701 | reinit_completion(&data->done); |
8dd03afe | 7702 | |
1ffc5422 | 7703 | ret = io_run_task_work_sig(); |
8bad28d8 | 7704 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 7705 | } while (ret >= 0); |
8bad28d8 | 7706 | data->quiesce = false; |
05f3fb3c | 7707 | |
8bad28d8 | 7708 | return ret; |
d7954b2b BM |
7709 | } |
7710 | ||
2d091d62 PB |
7711 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
7712 | { | |
7713 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; | |
7714 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; | |
7715 | ||
7716 | return &data->tags[table_idx][off]; | |
7717 | } | |
7718 | ||
44b31f2f | 7719 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
1ad555c6 | 7720 | { |
2d091d62 PB |
7721 | size_t size = data->nr * sizeof(data->tags[0][0]); |
7722 | ||
7723 | if (data->tags) | |
7724 | io_free_page_table((void **)data->tags, size); | |
44b31f2f PB |
7725 | kfree(data); |
7726 | } | |
7727 | ||
d878c816 PB |
7728 | static int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
7729 | u64 __user *utags, unsigned nr, | |
7730 | struct io_rsrc_data **pdata) | |
1ad555c6 | 7731 | { |
b895c9a6 | 7732 | struct io_rsrc_data *data; |
2d091d62 | 7733 | int ret = -ENOMEM; |
d878c816 | 7734 | unsigned i; |
1ad555c6 BM |
7735 | |
7736 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7737 | if (!data) | |
d878c816 | 7738 | return -ENOMEM; |
2d091d62 | 7739 | data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0])); |
b60c8dce | 7740 | if (!data->tags) { |
1ad555c6 | 7741 | kfree(data); |
d878c816 PB |
7742 | return -ENOMEM; |
7743 | } | |
2d091d62 PB |
7744 | |
7745 | data->nr = nr; | |
7746 | data->ctx = ctx; | |
7747 | data->do_put = do_put; | |
d878c816 | 7748 | if (utags) { |
2d091d62 | 7749 | ret = -EFAULT; |
d878c816 | 7750 | for (i = 0; i < nr; i++) { |
fdd1dc31 CIK |
7751 | u64 *tag_slot = io_get_tag_slot(data, i); |
7752 | ||
7753 | if (copy_from_user(tag_slot, &utags[i], | |
7754 | sizeof(*tag_slot))) | |
2d091d62 | 7755 | goto fail; |
d878c816 | 7756 | } |
1ad555c6 | 7757 | } |
b60c8dce | 7758 | |
3e942498 | 7759 | atomic_set(&data->refs, 1); |
1ad555c6 | 7760 | init_completion(&data->done); |
d878c816 PB |
7761 | *pdata = data; |
7762 | return 0; | |
2d091d62 PB |
7763 | fail: |
7764 | io_rsrc_data_free(data); | |
7765 | return ret; | |
1ad555c6 BM |
7766 | } |
7767 | ||
9123c8ff PB |
7768 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
7769 | { | |
0bea96f5 PB |
7770 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
7771 | GFP_KERNEL_ACCOUNT); | |
9123c8ff PB |
7772 | return !!table->files; |
7773 | } | |
7774 | ||
042b0d85 | 7775 | static void io_free_file_tables(struct io_file_table *table) |
9123c8ff | 7776 | { |
042b0d85 | 7777 | kvfree(table->files); |
9123c8ff PB |
7778 | table->files = NULL; |
7779 | } | |
7780 | ||
fff4db76 | 7781 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
1ad555c6 | 7782 | { |
fff4db76 PB |
7783 | #if defined(CONFIG_UNIX) |
7784 | if (ctx->ring_sock) { | |
7785 | struct sock *sock = ctx->ring_sock->sk; | |
7786 | struct sk_buff *skb; | |
7787 | ||
7788 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
7789 | kfree_skb(skb); | |
7790 | } | |
7791 | #else | |
7792 | int i; | |
7793 | ||
7794 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7795 | struct file *file; | |
7796 | ||
7797 | file = io_file_from_index(ctx, i); | |
7798 | if (file) | |
7799 | fput(file); | |
7800 | } | |
7801 | #endif | |
042b0d85 | 7802 | io_free_file_tables(&ctx->file_table); |
44b31f2f | 7803 | io_rsrc_data_free(ctx->file_data); |
fff4db76 PB |
7804 | ctx->file_data = NULL; |
7805 | ctx->nr_user_files = 0; | |
1ad555c6 BM |
7806 | } |
7807 | ||
d7954b2b BM |
7808 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7809 | { | |
d7954b2b BM |
7810 | int ret; |
7811 | ||
08480400 | 7812 | if (!ctx->file_data) |
d7954b2b | 7813 | return -ENXIO; |
08480400 PB |
7814 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
7815 | if (!ret) | |
7816 | __io_sqe_files_unregister(ctx); | |
7817 | return ret; | |
6b06314c JA |
7818 | } |
7819 | ||
37d1e2e3 | 7820 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
09a6f4ef | 7821 | __releases(&sqd->lock) |
37d1e2e3 | 7822 | { |
521d6a73 PB |
7823 | WARN_ON_ONCE(sqd->thread == current); |
7824 | ||
9e138a48 PB |
7825 | /* |
7826 | * Do the dance but not conditional clear_bit() because it'd race with | |
7827 | * other threads incrementing park_pending and setting the bit. | |
7828 | */ | |
37d1e2e3 | 7829 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
9e138a48 PB |
7830 | if (atomic_dec_return(&sqd->park_pending)) |
7831 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
09a6f4ef | 7832 | mutex_unlock(&sqd->lock); |
37d1e2e3 JA |
7833 | } |
7834 | ||
86e0d676 | 7835 | static void io_sq_thread_park(struct io_sq_data *sqd) |
09a6f4ef | 7836 | __acquires(&sqd->lock) |
37d1e2e3 | 7837 | { |
521d6a73 PB |
7838 | WARN_ON_ONCE(sqd->thread == current); |
7839 | ||
9e138a48 | 7840 | atomic_inc(&sqd->park_pending); |
86e0d676 | 7841 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
09a6f4ef | 7842 | mutex_lock(&sqd->lock); |
05962f95 | 7843 | if (sqd->thread) |
86e0d676 | 7844 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
7845 | } |
7846 | ||
7847 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
7848 | { | |
521d6a73 | 7849 | WARN_ON_ONCE(sqd->thread == current); |
88885f66 | 7850 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
521d6a73 | 7851 | |
05962f95 | 7852 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
88885f66 | 7853 | mutex_lock(&sqd->lock); |
e8f98f24 JA |
7854 | if (sqd->thread) |
7855 | wake_up_process(sqd->thread); | |
09a6f4ef | 7856 | mutex_unlock(&sqd->lock); |
05962f95 | 7857 | wait_for_completion(&sqd->exited); |
37d1e2e3 JA |
7858 | } |
7859 | ||
534ca6d6 | 7860 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7861 | { |
534ca6d6 | 7862 | if (refcount_dec_and_test(&sqd->refs)) { |
9e138a48 PB |
7863 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
7864 | ||
37d1e2e3 JA |
7865 | io_sq_thread_stop(sqd); |
7866 | kfree(sqd); | |
7867 | } | |
7868 | } | |
7869 | ||
7870 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
7871 | { | |
7872 | struct io_sq_data *sqd = ctx->sq_data; | |
7873 | ||
7874 | if (sqd) { | |
05962f95 | 7875 | io_sq_thread_park(sqd); |
521d6a73 | 7876 | list_del_init(&ctx->sqd_list); |
37d1e2e3 | 7877 | io_sqd_update_thread_idle(sqd); |
05962f95 | 7878 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
7879 | |
7880 | io_put_sq_data(sqd); | |
7881 | ctx->sq_data = NULL; | |
534ca6d6 JA |
7882 | } |
7883 | } | |
7884 | ||
aa06165d JA |
7885 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7886 | { | |
7887 | struct io_ring_ctx *ctx_attach; | |
7888 | struct io_sq_data *sqd; | |
7889 | struct fd f; | |
7890 | ||
7891 | f = fdget(p->wq_fd); | |
7892 | if (!f.file) | |
7893 | return ERR_PTR(-ENXIO); | |
7894 | if (f.file->f_op != &io_uring_fops) { | |
7895 | fdput(f); | |
7896 | return ERR_PTR(-EINVAL); | |
7897 | } | |
7898 | ||
7899 | ctx_attach = f.file->private_data; | |
7900 | sqd = ctx_attach->sq_data; | |
7901 | if (!sqd) { | |
7902 | fdput(f); | |
7903 | return ERR_PTR(-EINVAL); | |
7904 | } | |
5c2469e0 JA |
7905 | if (sqd->task_tgid != current->tgid) { |
7906 | fdput(f); | |
7907 | return ERR_PTR(-EPERM); | |
7908 | } | |
aa06165d JA |
7909 | |
7910 | refcount_inc(&sqd->refs); | |
7911 | fdput(f); | |
7912 | return sqd; | |
7913 | } | |
7914 | ||
26984fbf PB |
7915 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
7916 | bool *attached) | |
534ca6d6 JA |
7917 | { |
7918 | struct io_sq_data *sqd; | |
7919 | ||
26984fbf | 7920 | *attached = false; |
5c2469e0 JA |
7921 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
7922 | sqd = io_attach_sq_data(p); | |
26984fbf PB |
7923 | if (!IS_ERR(sqd)) { |
7924 | *attached = true; | |
5c2469e0 | 7925 | return sqd; |
26984fbf | 7926 | } |
5c2469e0 JA |
7927 | /* fall through for EPERM case, setup new sqd/task */ |
7928 | if (PTR_ERR(sqd) != -EPERM) | |
7929 | return sqd; | |
7930 | } | |
aa06165d | 7931 | |
534ca6d6 JA |
7932 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7933 | if (!sqd) | |
7934 | return ERR_PTR(-ENOMEM); | |
7935 | ||
9e138a48 | 7936 | atomic_set(&sqd->park_pending, 0); |
534ca6d6 | 7937 | refcount_set(&sqd->refs, 1); |
69fb2131 | 7938 | INIT_LIST_HEAD(&sqd->ctx_list); |
09a6f4ef | 7939 | mutex_init(&sqd->lock); |
534ca6d6 | 7940 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 7941 | init_completion(&sqd->exited); |
534ca6d6 JA |
7942 | return sqd; |
7943 | } | |
7944 | ||
6b06314c | 7945 | #if defined(CONFIG_UNIX) |
6b06314c JA |
7946 | /* |
7947 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
7948 | * the io_uring can be safely unregistered on process exit, even if we have | |
7949 | * loops in the file referencing. | |
7950 | */ | |
7951 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
7952 | { | |
7953 | struct sock *sk = ctx->ring_sock->sk; | |
7954 | struct scm_fp_list *fpl; | |
7955 | struct sk_buff *skb; | |
08a45173 | 7956 | int i, nr_files; |
6b06314c | 7957 | |
6b06314c JA |
7958 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
7959 | if (!fpl) | |
7960 | return -ENOMEM; | |
7961 | ||
7962 | skb = alloc_skb(0, GFP_KERNEL); | |
7963 | if (!skb) { | |
7964 | kfree(fpl); | |
7965 | return -ENOMEM; | |
7966 | } | |
7967 | ||
7968 | skb->sk = sk; | |
6b06314c | 7969 | |
08a45173 | 7970 | nr_files = 0; |
62e398be | 7971 | fpl->user = get_uid(current_user()); |
6b06314c | 7972 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
7973 | struct file *file = io_file_from_index(ctx, i + offset); |
7974 | ||
7975 | if (!file) | |
08a45173 | 7976 | continue; |
65e19f54 | 7977 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
7978 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
7979 | nr_files++; | |
6b06314c JA |
7980 | } |
7981 | ||
08a45173 JA |
7982 | if (nr_files) { |
7983 | fpl->max = SCM_MAX_FD; | |
7984 | fpl->count = nr_files; | |
7985 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 7986 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
7987 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
7988 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 7989 | |
08a45173 JA |
7990 | for (i = 0; i < nr_files; i++) |
7991 | fput(fpl->fp[i]); | |
7992 | } else { | |
7993 | kfree_skb(skb); | |
7994 | kfree(fpl); | |
7995 | } | |
6b06314c JA |
7996 | |
7997 | return 0; | |
7998 | } | |
7999 | ||
8000 | /* | |
8001 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
8002 | * causes regular reference counting to break down. We rely on the UNIX | |
8003 | * garbage collection to take care of this problem for us. | |
8004 | */ | |
8005 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8006 | { | |
8007 | unsigned left, total; | |
8008 | int ret = 0; | |
8009 | ||
8010 | total = 0; | |
8011 | left = ctx->nr_user_files; | |
8012 | while (left) { | |
8013 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
8014 | |
8015 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
8016 | if (ret) | |
8017 | break; | |
8018 | left -= this_files; | |
8019 | total += this_files; | |
8020 | } | |
8021 | ||
8022 | if (!ret) | |
8023 | return 0; | |
8024 | ||
8025 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
8026 | struct file *file = io_file_from_index(ctx, total); |
8027 | ||
8028 | if (file) | |
8029 | fput(file); | |
6b06314c JA |
8030 | total++; |
8031 | } | |
8032 | ||
8033 | return ret; | |
8034 | } | |
8035 | #else | |
8036 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8037 | { | |
8038 | return 0; | |
8039 | } | |
8040 | #endif | |
8041 | ||
47e90392 | 8042 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 8043 | { |
50238531 | 8044 | struct file *file = prsrc->file; |
05f3fb3c JA |
8045 | #if defined(CONFIG_UNIX) |
8046 | struct sock *sock = ctx->ring_sock->sk; | |
8047 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
8048 | struct sk_buff *skb; | |
8049 | int i; | |
8050 | ||
8051 | __skb_queue_head_init(&list); | |
8052 | ||
8053 | /* | |
8054 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
8055 | * remove this entry and rearrange the file array. | |
8056 | */ | |
8057 | skb = skb_dequeue(head); | |
8058 | while (skb) { | |
8059 | struct scm_fp_list *fp; | |
8060 | ||
8061 | fp = UNIXCB(skb).fp; | |
8062 | for (i = 0; i < fp->count; i++) { | |
8063 | int left; | |
8064 | ||
8065 | if (fp->fp[i] != file) | |
8066 | continue; | |
8067 | ||
8068 | unix_notinflight(fp->user, fp->fp[i]); | |
8069 | left = fp->count - 1 - i; | |
8070 | if (left) { | |
8071 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
8072 | left * sizeof(struct file *)); | |
8073 | } | |
8074 | fp->count--; | |
8075 | if (!fp->count) { | |
8076 | kfree_skb(skb); | |
8077 | skb = NULL; | |
8078 | } else { | |
8079 | __skb_queue_tail(&list, skb); | |
8080 | } | |
8081 | fput(file); | |
8082 | file = NULL; | |
8083 | break; | |
8084 | } | |
8085 | ||
8086 | if (!file) | |
8087 | break; | |
8088 | ||
8089 | __skb_queue_tail(&list, skb); | |
8090 | ||
8091 | skb = skb_dequeue(head); | |
8092 | } | |
8093 | ||
8094 | if (skb_peek(&list)) { | |
8095 | spin_lock_irq(&head->lock); | |
8096 | while ((skb = __skb_dequeue(&list)) != NULL) | |
8097 | __skb_queue_tail(head, skb); | |
8098 | spin_unlock_irq(&head->lock); | |
8099 | } | |
8100 | #else | |
8101 | fput(file); | |
8102 | #endif | |
8103 | } | |
8104 | ||
b895c9a6 | 8105 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
65e19f54 | 8106 | { |
b895c9a6 | 8107 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
269bbe5f BM |
8108 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
8109 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 8110 | |
269bbe5f BM |
8111 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
8112 | list_del(&prsrc->list); | |
b60c8dce PB |
8113 | |
8114 | if (prsrc->tag) { | |
8115 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; | |
b60c8dce PB |
8116 | |
8117 | io_ring_submit_lock(ctx, lock_ring); | |
79ebeaee | 8118 | spin_lock(&ctx->completion_lock); |
b60c8dce | 8119 | io_cqring_fill_event(ctx, prsrc->tag, 0, 0); |
2840f710 | 8120 | ctx->cq_extra++; |
b60c8dce | 8121 | io_commit_cqring(ctx); |
79ebeaee | 8122 | spin_unlock(&ctx->completion_lock); |
b60c8dce PB |
8123 | io_cqring_ev_posted(ctx); |
8124 | io_ring_submit_unlock(ctx, lock_ring); | |
8125 | } | |
8126 | ||
40ae0ff7 | 8127 | rsrc_data->do_put(ctx, prsrc); |
269bbe5f | 8128 | kfree(prsrc); |
65e19f54 | 8129 | } |
05589553 | 8130 | |
28a9fe25 | 8131 | io_rsrc_node_destroy(ref_node); |
3e942498 PB |
8132 | if (atomic_dec_and_test(&rsrc_data->refs)) |
8133 | complete(&rsrc_data->done); | |
2faf852d | 8134 | } |
65e19f54 | 8135 | |
269bbe5f | 8136 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
8137 | { |
8138 | struct io_ring_ctx *ctx; | |
8139 | struct llist_node *node; | |
8140 | ||
269bbe5f BM |
8141 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
8142 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
8143 | |
8144 | while (node) { | |
b895c9a6 | 8145 | struct io_rsrc_node *ref_node; |
4a38aed2 JA |
8146 | struct llist_node *next = node->next; |
8147 | ||
b895c9a6 | 8148 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
269bbe5f | 8149 | __io_rsrc_put_work(ref_node); |
4a38aed2 JA |
8150 | node = next; |
8151 | } | |
8152 | } | |
8153 | ||
6b06314c | 8154 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
792e3582 | 8155 | unsigned nr_args, u64 __user *tags) |
6b06314c JA |
8156 | { |
8157 | __s32 __user *fds = (__s32 __user *) arg; | |
05f3fb3c | 8158 | struct file *file; |
f3baed39 | 8159 | int fd, ret; |
846a4ef2 | 8160 | unsigned i; |
6b06314c | 8161 | |
05f3fb3c | 8162 | if (ctx->file_data) |
6b06314c JA |
8163 | return -EBUSY; |
8164 | if (!nr_args) | |
8165 | return -EINVAL; | |
8166 | if (nr_args > IORING_MAX_FIXED_FILES) | |
8167 | return -EMFILE; | |
3a1b8a4e PB |
8168 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
8169 | return -EMFILE; | |
a7f0ed5a | 8170 | ret = io_rsrc_node_switch_start(ctx); |
f3baed39 PB |
8171 | if (ret) |
8172 | return ret; | |
d878c816 PB |
8173 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
8174 | &ctx->file_data); | |
8175 | if (ret) | |
8176 | return ret; | |
6b06314c | 8177 | |
f3baed39 | 8178 | ret = -ENOMEM; |
aeca241b | 8179 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
1ad555c6 | 8180 | goto out_free; |
65e19f54 | 8181 | |
08a45173 | 8182 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
d878c816 | 8183 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
600cf3f8 PB |
8184 | ret = -EFAULT; |
8185 | goto out_fput; | |
8186 | } | |
08a45173 | 8187 | /* allow sparse sets */ |
792e3582 PB |
8188 | if (fd == -1) { |
8189 | ret = -EINVAL; | |
2d091d62 | 8190 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
792e3582 | 8191 | goto out_fput; |
08a45173 | 8192 | continue; |
792e3582 | 8193 | } |
6b06314c | 8194 | |
05f3fb3c | 8195 | file = fget(fd); |
6b06314c | 8196 | ret = -EBADF; |
792e3582 | 8197 | if (unlikely(!file)) |
600cf3f8 | 8198 | goto out_fput; |
05f3fb3c | 8199 | |
6b06314c JA |
8200 | /* |
8201 | * Don't allow io_uring instances to be registered. If UNIX | |
8202 | * isn't enabled, then this causes a reference cycle and this | |
8203 | * instance can never get freed. If UNIX is enabled we'll | |
8204 | * handle it just fine, but there's still no point in allowing | |
8205 | * a ring fd as it doesn't support regular read/write anyway. | |
8206 | */ | |
05f3fb3c JA |
8207 | if (file->f_op == &io_uring_fops) { |
8208 | fput(file); | |
600cf3f8 | 8209 | goto out_fput; |
6b06314c | 8210 | } |
aeca241b | 8211 | io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file); |
6b06314c JA |
8212 | } |
8213 | ||
6b06314c | 8214 | ret = io_sqe_files_scm(ctx); |
05589553 | 8215 | if (ret) { |
08480400 | 8216 | __io_sqe_files_unregister(ctx); |
05589553 XW |
8217 | return ret; |
8218 | } | |
6b06314c | 8219 | |
a7f0ed5a | 8220 | io_rsrc_node_switch(ctx, NULL); |
6b06314c | 8221 | return ret; |
600cf3f8 PB |
8222 | out_fput: |
8223 | for (i = 0; i < ctx->nr_user_files; i++) { | |
8224 | file = io_file_from_index(ctx, i); | |
8225 | if (file) | |
8226 | fput(file); | |
8227 | } | |
042b0d85 | 8228 | io_free_file_tables(&ctx->file_table); |
600cf3f8 | 8229 | ctx->nr_user_files = 0; |
600cf3f8 | 8230 | out_free: |
44b31f2f | 8231 | io_rsrc_data_free(ctx->file_data); |
55cbc256 | 8232 | ctx->file_data = NULL; |
6b06314c JA |
8233 | return ret; |
8234 | } | |
8235 | ||
c3a31e60 JA |
8236 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
8237 | int index) | |
8238 | { | |
8239 | #if defined(CONFIG_UNIX) | |
8240 | struct sock *sock = ctx->ring_sock->sk; | |
8241 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
8242 | struct sk_buff *skb; | |
8243 | ||
8244 | /* | |
8245 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
8246 | * file set. If there's no room, fall back to allocating a new skb | |
8247 | * and filling it in. | |
8248 | */ | |
8249 | spin_lock_irq(&head->lock); | |
8250 | skb = skb_peek(head); | |
8251 | if (skb) { | |
8252 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
8253 | ||
8254 | if (fpl->count < SCM_MAX_FD) { | |
8255 | __skb_unlink(skb, head); | |
8256 | spin_unlock_irq(&head->lock); | |
8257 | fpl->fp[fpl->count] = get_file(file); | |
8258 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
8259 | fpl->count++; | |
8260 | spin_lock_irq(&head->lock); | |
8261 | __skb_queue_head(head, skb); | |
8262 | } else { | |
8263 | skb = NULL; | |
8264 | } | |
8265 | } | |
8266 | spin_unlock_irq(&head->lock); | |
8267 | ||
8268 | if (skb) { | |
8269 | fput(file); | |
8270 | return 0; | |
8271 | } | |
8272 | ||
8273 | return __io_sqe_files_scm(ctx, 1, index); | |
8274 | #else | |
8275 | return 0; | |
8276 | #endif | |
8277 | } | |
8278 | ||
9c7b0ba8 PB |
8279 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
8280 | struct io_rsrc_node *node, void *rsrc) | |
8281 | { | |
8282 | struct io_rsrc_put *prsrc; | |
8283 | ||
8284 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); | |
8285 | if (!prsrc) | |
8286 | return -ENOMEM; | |
8287 | ||
8288 | prsrc->tag = *io_get_tag_slot(data, idx); | |
8289 | prsrc->rsrc = rsrc; | |
8290 | list_add(&prsrc->list, &node->rsrc_list); | |
8291 | return 0; | |
8292 | } | |
8293 | ||
b9445598 PB |
8294 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
8295 | unsigned int issue_flags, u32 slot_index) | |
8296 | { | |
8297 | struct io_ring_ctx *ctx = req->ctx; | |
8298 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; | |
9c7b0ba8 | 8299 | bool needs_switch = false; |
b9445598 PB |
8300 | struct io_fixed_file *file_slot; |
8301 | int ret = -EBADF; | |
8302 | ||
8303 | io_ring_submit_lock(ctx, !force_nonblock); | |
8304 | if (file->f_op == &io_uring_fops) | |
8305 | goto err; | |
8306 | ret = -ENXIO; | |
8307 | if (!ctx->file_data) | |
8308 | goto err; | |
8309 | ret = -EINVAL; | |
8310 | if (slot_index >= ctx->nr_user_files) | |
8311 | goto err; | |
8312 | ||
8313 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); | |
8314 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); | |
9c7b0ba8 PB |
8315 | |
8316 | if (file_slot->file_ptr) { | |
8317 | struct file *old_file; | |
8318 | ||
8319 | ret = io_rsrc_node_switch_start(ctx); | |
8320 | if (ret) | |
8321 | goto err; | |
8322 | ||
8323 | old_file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8324 | ret = io_queue_rsrc_removal(ctx->file_data, slot_index, | |
8325 | ctx->rsrc_node, old_file); | |
8326 | if (ret) | |
8327 | goto err; | |
8328 | file_slot->file_ptr = 0; | |
8329 | needs_switch = true; | |
8330 | } | |
b9445598 PB |
8331 | |
8332 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; | |
8333 | io_fixed_file_set(file_slot, file); | |
8334 | ret = io_sqe_file_register(ctx, file, slot_index); | |
8335 | if (ret) { | |
8336 | file_slot->file_ptr = 0; | |
8337 | goto err; | |
8338 | } | |
8339 | ||
8340 | ret = 0; | |
8341 | err: | |
9c7b0ba8 PB |
8342 | if (needs_switch) |
8343 | io_rsrc_node_switch(ctx, ctx->file_data); | |
b9445598 PB |
8344 | io_ring_submit_unlock(ctx, !force_nonblock); |
8345 | if (ret) | |
8346 | fput(file); | |
8347 | return ret; | |
8348 | } | |
8349 | ||
7df778be PB |
8350 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) |
8351 | { | |
8352 | unsigned int offset = req->close.file_slot - 1; | |
8353 | struct io_ring_ctx *ctx = req->ctx; | |
8354 | struct io_fixed_file *file_slot; | |
8355 | struct file *file; | |
8356 | int ret, i; | |
8357 | ||
8358 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
8359 | ret = -ENXIO; | |
8360 | if (unlikely(!ctx->file_data)) | |
8361 | goto out; | |
8362 | ret = -EINVAL; | |
8363 | if (offset >= ctx->nr_user_files) | |
8364 | goto out; | |
8365 | ret = io_rsrc_node_switch_start(ctx); | |
8366 | if (ret) | |
8367 | goto out; | |
8368 | ||
8369 | i = array_index_nospec(offset, ctx->nr_user_files); | |
8370 | file_slot = io_fixed_file_slot(&ctx->file_table, i); | |
8371 | ret = -EBADF; | |
8372 | if (!file_slot->file_ptr) | |
8373 | goto out; | |
8374 | ||
8375 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8376 | ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file); | |
8377 | if (ret) | |
8378 | goto out; | |
8379 | ||
8380 | file_slot->file_ptr = 0; | |
8381 | io_rsrc_node_switch(ctx, ctx->file_data); | |
8382 | ret = 0; | |
8383 | out: | |
8384 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
8385 | return ret; | |
8386 | } | |
8387 | ||
05f3fb3c | 8388 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
c3bdad02 | 8389 | struct io_uring_rsrc_update2 *up, |
05f3fb3c JA |
8390 | unsigned nr_args) |
8391 | { | |
c3bdad02 | 8392 | u64 __user *tags = u64_to_user_ptr(up->tags); |
98f0b3b4 | 8393 | __s32 __user *fds = u64_to_user_ptr(up->data); |
b895c9a6 | 8394 | struct io_rsrc_data *data = ctx->file_data; |
a04b0ac0 PB |
8395 | struct io_fixed_file *file_slot; |
8396 | struct file *file; | |
98f0b3b4 PB |
8397 | int fd, i, err = 0; |
8398 | unsigned int done; | |
05589553 | 8399 | bool needs_switch = false; |
c3a31e60 | 8400 | |
98f0b3b4 PB |
8401 | if (!ctx->file_data) |
8402 | return -ENXIO; | |
8403 | if (up->offset + nr_args > ctx->nr_user_files) | |
c3a31e60 JA |
8404 | return -EINVAL; |
8405 | ||
67973b93 | 8406 | for (done = 0; done < nr_args; done++) { |
c3bdad02 PB |
8407 | u64 tag = 0; |
8408 | ||
8409 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || | |
8410 | copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
c3a31e60 JA |
8411 | err = -EFAULT; |
8412 | break; | |
8413 | } | |
c3bdad02 PB |
8414 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
8415 | err = -EINVAL; | |
8416 | break; | |
8417 | } | |
4e0377a1 | 8418 | if (fd == IORING_REGISTER_FILES_SKIP) |
8419 | continue; | |
8420 | ||
67973b93 | 8421 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
aeca241b | 8422 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
ea64ec02 | 8423 | |
a04b0ac0 PB |
8424 | if (file_slot->file_ptr) { |
8425 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
b60c8dce PB |
8426 | err = io_queue_rsrc_removal(data, up->offset + done, |
8427 | ctx->rsrc_node, file); | |
a5318d3c HD |
8428 | if (err) |
8429 | break; | |
a04b0ac0 | 8430 | file_slot->file_ptr = 0; |
05589553 | 8431 | needs_switch = true; |
c3a31e60 JA |
8432 | } |
8433 | if (fd != -1) { | |
c3a31e60 JA |
8434 | file = fget(fd); |
8435 | if (!file) { | |
8436 | err = -EBADF; | |
8437 | break; | |
8438 | } | |
8439 | /* | |
8440 | * Don't allow io_uring instances to be registered. If | |
8441 | * UNIX isn't enabled, then this causes a reference | |
8442 | * cycle and this instance can never get freed. If UNIX | |
8443 | * is enabled we'll handle it just fine, but there's | |
8444 | * still no point in allowing a ring fd as it doesn't | |
8445 | * support regular read/write anyway. | |
8446 | */ | |
8447 | if (file->f_op == &io_uring_fops) { | |
8448 | fput(file); | |
8449 | err = -EBADF; | |
8450 | break; | |
8451 | } | |
2d091d62 | 8452 | *io_get_tag_slot(data, up->offset + done) = tag; |
9a321c98 | 8453 | io_fixed_file_set(file_slot, file); |
c3a31e60 | 8454 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 8455 | if (err) { |
a04b0ac0 | 8456 | file_slot->file_ptr = 0; |
f3bd9dae | 8457 | fput(file); |
c3a31e60 | 8458 | break; |
f3bd9dae | 8459 | } |
c3a31e60 | 8460 | } |
05f3fb3c JA |
8461 | } |
8462 | ||
a7f0ed5a PB |
8463 | if (needs_switch) |
8464 | io_rsrc_node_switch(ctx, data); | |
c3a31e60 JA |
8465 | return done ? done : err; |
8466 | } | |
05589553 | 8467 | |
685fe7fe JA |
8468 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
8469 | struct task_struct *task) | |
24369c2e | 8470 | { |
e941894e | 8471 | struct io_wq_hash *hash; |
24369c2e | 8472 | struct io_wq_data data; |
24369c2e | 8473 | unsigned int concurrency; |
24369c2e | 8474 | |
362a9e65 | 8475 | mutex_lock(&ctx->uring_lock); |
e941894e JA |
8476 | hash = ctx->hash_map; |
8477 | if (!hash) { | |
8478 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
362a9e65 YY |
8479 | if (!hash) { |
8480 | mutex_unlock(&ctx->uring_lock); | |
e941894e | 8481 | return ERR_PTR(-ENOMEM); |
362a9e65 | 8482 | } |
e941894e JA |
8483 | refcount_set(&hash->refs, 1); |
8484 | init_waitqueue_head(&hash->wait); | |
8485 | ctx->hash_map = hash; | |
24369c2e | 8486 | } |
362a9e65 | 8487 | mutex_unlock(&ctx->uring_lock); |
24369c2e | 8488 | |
e941894e | 8489 | data.hash = hash; |
685fe7fe | 8490 | data.task = task; |
ebc11b6c | 8491 | data.free_work = io_wq_free_work; |
f5fa38c5 | 8492 | data.do_work = io_wq_submit_work; |
24369c2e | 8493 | |
d25e3a3d JA |
8494 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
8495 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 8496 | |
5aa75ed5 | 8497 | return io_wq_create(concurrency, &data); |
24369c2e PB |
8498 | } |
8499 | ||
5aa75ed5 JA |
8500 | static int io_uring_alloc_task_context(struct task_struct *task, |
8501 | struct io_ring_ctx *ctx) | |
0f212204 JA |
8502 | { |
8503 | struct io_uring_task *tctx; | |
d8a6df10 | 8504 | int ret; |
0f212204 | 8505 | |
09899b19 | 8506 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
0f212204 JA |
8507 | if (unlikely(!tctx)) |
8508 | return -ENOMEM; | |
8509 | ||
d8a6df10 JA |
8510 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
8511 | if (unlikely(ret)) { | |
8512 | kfree(tctx); | |
8513 | return ret; | |
8514 | } | |
8515 | ||
685fe7fe | 8516 | tctx->io_wq = io_init_wq_offload(ctx, task); |
5aa75ed5 JA |
8517 | if (IS_ERR(tctx->io_wq)) { |
8518 | ret = PTR_ERR(tctx->io_wq); | |
8519 | percpu_counter_destroy(&tctx->inflight); | |
8520 | kfree(tctx); | |
8521 | return ret; | |
8522 | } | |
8523 | ||
0f212204 JA |
8524 | xa_init(&tctx->xa); |
8525 | init_waitqueue_head(&tctx->wait); | |
fdaf083c | 8526 | atomic_set(&tctx->in_idle, 0); |
b303fe2e | 8527 | atomic_set(&tctx->inflight_tracked, 0); |
0f212204 | 8528 | task->io_uring = tctx; |
7cbf1722 JA |
8529 | spin_lock_init(&tctx->task_lock); |
8530 | INIT_WQ_LIST(&tctx->task_list); | |
7cbf1722 | 8531 | init_task_work(&tctx->task_work, tctx_task_work); |
0f212204 JA |
8532 | return 0; |
8533 | } | |
8534 | ||
8535 | void __io_uring_free(struct task_struct *tsk) | |
8536 | { | |
8537 | struct io_uring_task *tctx = tsk->io_uring; | |
8538 | ||
8539 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e | 8540 | WARN_ON_ONCE(tctx->io_wq); |
09899b19 | 8541 | WARN_ON_ONCE(tctx->cached_refs); |
ef8eaa4e | 8542 | |
d8a6df10 | 8543 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
8544 | kfree(tctx); |
8545 | tsk->io_uring = NULL; | |
8546 | } | |
8547 | ||
7e84e1c7 SG |
8548 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
8549 | struct io_uring_params *p) | |
2b188cc1 JA |
8550 | { |
8551 | int ret; | |
8552 | ||
d25e3a3d JA |
8553 | /* Retain compatibility with failing for an invalid attach attempt */ |
8554 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
8555 | IORING_SETUP_ATTACH_WQ) { | |
8556 | struct fd f; | |
8557 | ||
8558 | f = fdget(p->wq_fd); | |
8559 | if (!f.file) | |
8560 | return -ENXIO; | |
0cc936f7 JA |
8561 | if (f.file->f_op != &io_uring_fops) { |
8562 | fdput(f); | |
f2a48dd0 | 8563 | return -EINVAL; |
0cc936f7 JA |
8564 | } |
8565 | fdput(f); | |
d25e3a3d | 8566 | } |
6c271ce2 | 8567 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 8568 | struct task_struct *tsk; |
534ca6d6 | 8569 | struct io_sq_data *sqd; |
26984fbf | 8570 | bool attached; |
534ca6d6 | 8571 | |
26984fbf | 8572 | sqd = io_get_sq_data(p, &attached); |
534ca6d6 JA |
8573 | if (IS_ERR(sqd)) { |
8574 | ret = PTR_ERR(sqd); | |
8575 | goto err; | |
8576 | } | |
69fb2131 | 8577 | |
7c30f36a | 8578 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 8579 | ctx->sq_data = sqd; |
917257da JA |
8580 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
8581 | if (!ctx->sq_thread_idle) | |
8582 | ctx->sq_thread_idle = HZ; | |
8583 | ||
78d7f6ba | 8584 | io_sq_thread_park(sqd); |
de75a3d3 PB |
8585 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
8586 | io_sqd_update_thread_idle(sqd); | |
26984fbf | 8587 | /* don't attach to a dying SQPOLL thread, would be racy */ |
f2a48dd0 | 8588 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
78d7f6ba PB |
8589 | io_sq_thread_unpark(sqd); |
8590 | ||
de75a3d3 PB |
8591 | if (ret < 0) |
8592 | goto err; | |
8593 | if (attached) | |
5aa75ed5 | 8594 | return 0; |
aa06165d | 8595 | |
6c271ce2 | 8596 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 8597 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 8598 | |
917257da | 8599 | ret = -EINVAL; |
f2a48dd0 | 8600 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
e8f98f24 | 8601 | goto err_sqpoll; |
37d1e2e3 | 8602 | sqd->sq_cpu = cpu; |
6c271ce2 | 8603 | } else { |
37d1e2e3 | 8604 | sqd->sq_cpu = -1; |
6c271ce2 | 8605 | } |
37d1e2e3 JA |
8606 | |
8607 | sqd->task_pid = current->pid; | |
5c2469e0 | 8608 | sqd->task_tgid = current->tgid; |
46fe18b1 JA |
8609 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
8610 | if (IS_ERR(tsk)) { | |
8611 | ret = PTR_ERR(tsk); | |
e8f98f24 | 8612 | goto err_sqpoll; |
6c271ce2 | 8613 | } |
97a73a0f | 8614 | |
46fe18b1 | 8615 | sqd->thread = tsk; |
97a73a0f | 8616 | ret = io_uring_alloc_task_context(tsk, ctx); |
46fe18b1 | 8617 | wake_up_new_task(tsk); |
0f212204 JA |
8618 | if (ret) |
8619 | goto err; | |
6c271ce2 JA |
8620 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
8621 | /* Can't have SQ_AFF without SQPOLL */ | |
8622 | ret = -EINVAL; | |
8623 | goto err; | |
8624 | } | |
8625 | ||
2b188cc1 | 8626 | return 0; |
f2a48dd0 PB |
8627 | err_sqpoll: |
8628 | complete(&ctx->sq_data->exited); | |
2b188cc1 | 8629 | err: |
37d1e2e3 | 8630 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
8631 | return ret; |
8632 | } | |
8633 | ||
a087e2b5 BM |
8634 | static inline void __io_unaccount_mem(struct user_struct *user, |
8635 | unsigned long nr_pages) | |
2b188cc1 JA |
8636 | { |
8637 | atomic_long_sub(nr_pages, &user->locked_vm); | |
8638 | } | |
8639 | ||
a087e2b5 BM |
8640 | static inline int __io_account_mem(struct user_struct *user, |
8641 | unsigned long nr_pages) | |
2b188cc1 JA |
8642 | { |
8643 | unsigned long page_limit, cur_pages, new_pages; | |
8644 | ||
8645 | /* Don't allow more pages than we can safely lock */ | |
8646 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
8647 | ||
8648 | do { | |
8649 | cur_pages = atomic_long_read(&user->locked_vm); | |
8650 | new_pages = cur_pages + nr_pages; | |
8651 | if (new_pages > page_limit) | |
8652 | return -ENOMEM; | |
8653 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
8654 | new_pages) != cur_pages); | |
8655 | ||
8656 | return 0; | |
8657 | } | |
8658 | ||
26bfa89e | 8659 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8660 | { |
62e398be | 8661 | if (ctx->user) |
a087e2b5 | 8662 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 8663 | |
26bfa89e JA |
8664 | if (ctx->mm_account) |
8665 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8666 | } |
8667 | ||
26bfa89e | 8668 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8669 | { |
30975825 BM |
8670 | int ret; |
8671 | ||
62e398be | 8672 | if (ctx->user) { |
30975825 BM |
8673 | ret = __io_account_mem(ctx->user, nr_pages); |
8674 | if (ret) | |
8675 | return ret; | |
8676 | } | |
8677 | ||
26bfa89e JA |
8678 | if (ctx->mm_account) |
8679 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8680 | |
8681 | return 0; | |
8682 | } | |
8683 | ||
2b188cc1 JA |
8684 | static void io_mem_free(void *ptr) |
8685 | { | |
52e04ef4 MR |
8686 | struct page *page; |
8687 | ||
8688 | if (!ptr) | |
8689 | return; | |
2b188cc1 | 8690 | |
52e04ef4 | 8691 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8692 | if (put_page_testzero(page)) |
8693 | free_compound_page(page); | |
8694 | } | |
8695 | ||
8696 | static void *io_mem_alloc(size_t size) | |
8697 | { | |
8698 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
26bfa89e | 8699 | __GFP_NORETRY | __GFP_ACCOUNT; |
2b188cc1 JA |
8700 | |
8701 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8702 | } | |
8703 | ||
75b28aff HV |
8704 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8705 | size_t *sq_offset) | |
8706 | { | |
8707 | struct io_rings *rings; | |
8708 | size_t off, sq_array_size; | |
8709 | ||
8710 | off = struct_size(rings, cqes, cq_entries); | |
8711 | if (off == SIZE_MAX) | |
8712 | return SIZE_MAX; | |
8713 | ||
8714 | #ifdef CONFIG_SMP | |
8715 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8716 | if (off == 0) | |
8717 | return SIZE_MAX; | |
8718 | #endif | |
8719 | ||
b36200f5 DV |
8720 | if (sq_offset) |
8721 | *sq_offset = off; | |
8722 | ||
75b28aff HV |
8723 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8724 | if (sq_array_size == SIZE_MAX) | |
8725 | return SIZE_MAX; | |
8726 | ||
8727 | if (check_add_overflow(off, sq_array_size, &off)) | |
8728 | return SIZE_MAX; | |
8729 | ||
75b28aff HV |
8730 | return off; |
8731 | } | |
8732 | ||
41edf1a5 | 8733 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot) |
7f61a1e9 | 8734 | { |
41edf1a5 | 8735 | struct io_mapped_ubuf *imu = *slot; |
7f61a1e9 PB |
8736 | unsigned int i; |
8737 | ||
6224843d PB |
8738 | if (imu != ctx->dummy_ubuf) { |
8739 | for (i = 0; i < imu->nr_bvecs; i++) | |
8740 | unpin_user_page(imu->bvec[i].bv_page); | |
8741 | if (imu->acct_pages) | |
8742 | io_unaccount_mem(ctx, imu->acct_pages); | |
8743 | kvfree(imu); | |
8744 | } | |
41edf1a5 | 8745 | *slot = NULL; |
7f61a1e9 PB |
8746 | } |
8747 | ||
bd54b6fe | 8748 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
edafccee | 8749 | { |
634d00df PB |
8750 | io_buffer_unmap(ctx, &prsrc->buf); |
8751 | prsrc->buf = NULL; | |
bd54b6fe | 8752 | } |
edafccee | 8753 | |
bd54b6fe BM |
8754 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
8755 | { | |
8756 | unsigned int i; | |
edafccee | 8757 | |
7f61a1e9 PB |
8758 | for (i = 0; i < ctx->nr_user_bufs; i++) |
8759 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); | |
edafccee | 8760 | kfree(ctx->user_bufs); |
bb6659cc | 8761 | io_rsrc_data_free(ctx->buf_data); |
edafccee | 8762 | ctx->user_bufs = NULL; |
bd54b6fe | 8763 | ctx->buf_data = NULL; |
edafccee | 8764 | ctx->nr_user_bufs = 0; |
bd54b6fe BM |
8765 | } |
8766 | ||
0a96bbe4 | 8767 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee | 8768 | { |
bd54b6fe | 8769 | int ret; |
edafccee | 8770 | |
bd54b6fe | 8771 | if (!ctx->buf_data) |
edafccee JA |
8772 | return -ENXIO; |
8773 | ||
bd54b6fe BM |
8774 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
8775 | if (!ret) | |
8776 | __io_sqe_buffers_unregister(ctx); | |
8777 | return ret; | |
edafccee JA |
8778 | } |
8779 | ||
8780 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8781 | void __user *arg, unsigned index) | |
8782 | { | |
8783 | struct iovec __user *src; | |
8784 | ||
8785 | #ifdef CONFIG_COMPAT | |
8786 | if (ctx->compat) { | |
8787 | struct compat_iovec __user *ciovs; | |
8788 | struct compat_iovec ciov; | |
8789 | ||
8790 | ciovs = (struct compat_iovec __user *) arg; | |
8791 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8792 | return -EFAULT; | |
8793 | ||
d55e5f5b | 8794 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8795 | dst->iov_len = ciov.iov_len; |
8796 | return 0; | |
8797 | } | |
8798 | #endif | |
8799 | src = (struct iovec __user *) arg; | |
8800 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8801 | return -EFAULT; | |
8802 | return 0; | |
8803 | } | |
8804 | ||
de293938 JA |
8805 | /* |
8806 | * Not super efficient, but this is just a registration time. And we do cache | |
8807 | * the last compound head, so generally we'll only do a full search if we don't | |
8808 | * match that one. | |
8809 | * | |
8810 | * We check if the given compound head page has already been accounted, to | |
8811 | * avoid double accounting it. This allows us to account the full size of the | |
8812 | * page, not just the constituent pages of a huge page. | |
8813 | */ | |
8814 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8815 | int nr_pages, struct page *hpage) | |
8816 | { | |
8817 | int i, j; | |
8818 | ||
8819 | /* check current page array */ | |
8820 | for (i = 0; i < nr_pages; i++) { | |
8821 | if (!PageCompound(pages[i])) | |
8822 | continue; | |
8823 | if (compound_head(pages[i]) == hpage) | |
8824 | return true; | |
8825 | } | |
8826 | ||
8827 | /* check previously registered pages */ | |
8828 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
41edf1a5 | 8829 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
de293938 JA |
8830 | |
8831 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8832 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8833 | continue; | |
8834 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8835 | return true; | |
8836 | } | |
8837 | } | |
8838 | ||
8839 | return false; | |
8840 | } | |
8841 | ||
8842 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8843 | int nr_pages, struct io_mapped_ubuf *imu, | |
8844 | struct page **last_hpage) | |
8845 | { | |
8846 | int i, ret; | |
8847 | ||
216e5835 | 8848 | imu->acct_pages = 0; |
de293938 JA |
8849 | for (i = 0; i < nr_pages; i++) { |
8850 | if (!PageCompound(pages[i])) { | |
8851 | imu->acct_pages++; | |
8852 | } else { | |
8853 | struct page *hpage; | |
8854 | ||
8855 | hpage = compound_head(pages[i]); | |
8856 | if (hpage == *last_hpage) | |
8857 | continue; | |
8858 | *last_hpage = hpage; | |
8859 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8860 | continue; | |
8861 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8862 | } | |
8863 | } | |
8864 | ||
8865 | if (!imu->acct_pages) | |
8866 | return 0; | |
8867 | ||
26bfa89e | 8868 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
8869 | if (ret) |
8870 | imu->acct_pages = 0; | |
8871 | return ret; | |
8872 | } | |
8873 | ||
0a96bbe4 | 8874 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
41edf1a5 | 8875 | struct io_mapped_ubuf **pimu, |
0a96bbe4 | 8876 | struct page **last_hpage) |
edafccee | 8877 | { |
41edf1a5 | 8878 | struct io_mapped_ubuf *imu = NULL; |
edafccee JA |
8879 | struct vm_area_struct **vmas = NULL; |
8880 | struct page **pages = NULL; | |
0a96bbe4 BM |
8881 | unsigned long off, start, end, ubuf; |
8882 | size_t size; | |
8883 | int ret, pret, nr_pages, i; | |
8884 | ||
6224843d PB |
8885 | if (!iov->iov_base) { |
8886 | *pimu = ctx->dummy_ubuf; | |
8887 | return 0; | |
8888 | } | |
8889 | ||
0a96bbe4 BM |
8890 | ubuf = (unsigned long) iov->iov_base; |
8891 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8892 | start = ubuf >> PAGE_SHIFT; | |
8893 | nr_pages = end - start; | |
8894 | ||
41edf1a5 | 8895 | *pimu = NULL; |
0a96bbe4 BM |
8896 | ret = -ENOMEM; |
8897 | ||
8898 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8899 | if (!pages) | |
8900 | goto done; | |
8901 | ||
8902 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8903 | GFP_KERNEL); | |
8904 | if (!vmas) | |
8905 | goto done; | |
edafccee | 8906 | |
41edf1a5 | 8907 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
a2b4198c | 8908 | if (!imu) |
0a96bbe4 BM |
8909 | goto done; |
8910 | ||
8911 | ret = 0; | |
8912 | mmap_read_lock(current->mm); | |
8913 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8914 | pages, vmas); | |
8915 | if (pret == nr_pages) { | |
8916 | /* don't support file backed memory */ | |
8917 | for (i = 0; i < nr_pages; i++) { | |
8918 | struct vm_area_struct *vma = vmas[i]; | |
8919 | ||
40dad765 PB |
8920 | if (vma_is_shmem(vma)) |
8921 | continue; | |
0a96bbe4 BM |
8922 | if (vma->vm_file && |
8923 | !is_file_hugepages(vma->vm_file)) { | |
8924 | ret = -EOPNOTSUPP; | |
8925 | break; | |
8926 | } | |
8927 | } | |
8928 | } else { | |
8929 | ret = pret < 0 ? pret : -EFAULT; | |
8930 | } | |
8931 | mmap_read_unlock(current->mm); | |
8932 | if (ret) { | |
8933 | /* | |
8934 | * if we did partial map, or found file backed vmas, | |
8935 | * release any pages we did get | |
8936 | */ | |
8937 | if (pret > 0) | |
8938 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
8939 | goto done; |
8940 | } | |
8941 | ||
8942 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
8943 | if (ret) { | |
8944 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
8945 | goto done; |
8946 | } | |
8947 | ||
8948 | off = ubuf & ~PAGE_MASK; | |
8949 | size = iov->iov_len; | |
8950 | for (i = 0; i < nr_pages; i++) { | |
8951 | size_t vec_len; | |
8952 | ||
8953 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
8954 | imu->bvec[i].bv_page = pages[i]; | |
8955 | imu->bvec[i].bv_len = vec_len; | |
8956 | imu->bvec[i].bv_offset = off; | |
8957 | off = 0; | |
8958 | size -= vec_len; | |
8959 | } | |
8960 | /* store original address for later verification */ | |
8961 | imu->ubuf = ubuf; | |
4751f53d | 8962 | imu->ubuf_end = ubuf + iov->iov_len; |
0a96bbe4 | 8963 | imu->nr_bvecs = nr_pages; |
41edf1a5 | 8964 | *pimu = imu; |
0a96bbe4 BM |
8965 | ret = 0; |
8966 | done: | |
41edf1a5 PB |
8967 | if (ret) |
8968 | kvfree(imu); | |
0a96bbe4 BM |
8969 | kvfree(pages); |
8970 | kvfree(vmas); | |
8971 | return ret; | |
8972 | } | |
8973 | ||
2b358604 | 8974 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 8975 | { |
87094465 PB |
8976 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
8977 | return ctx->user_bufs ? 0 : -ENOMEM; | |
2b358604 | 8978 | } |
edafccee | 8979 | |
2b358604 BM |
8980 | static int io_buffer_validate(struct iovec *iov) |
8981 | { | |
50e96989 PB |
8982 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
8983 | ||
2b358604 BM |
8984 | /* |
8985 | * Don't impose further limits on the size and buffer | |
8986 | * constraints here, we'll -EINVAL later when IO is | |
8987 | * submitted if they are wrong. | |
8988 | */ | |
6224843d PB |
8989 | if (!iov->iov_base) |
8990 | return iov->iov_len ? -EFAULT : 0; | |
8991 | if (!iov->iov_len) | |
2b358604 | 8992 | return -EFAULT; |
edafccee | 8993 | |
2b358604 BM |
8994 | /* arbitrary limit, but we need something */ |
8995 | if (iov->iov_len > SZ_1G) | |
8996 | return -EFAULT; | |
edafccee | 8997 | |
50e96989 PB |
8998 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
8999 | return -EOVERFLOW; | |
9000 | ||
2b358604 BM |
9001 | return 0; |
9002 | } | |
edafccee | 9003 | |
2b358604 | 9004 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
634d00df | 9005 | unsigned int nr_args, u64 __user *tags) |
2b358604 | 9006 | { |
bd54b6fe BM |
9007 | struct page *last_hpage = NULL; |
9008 | struct io_rsrc_data *data; | |
2b358604 BM |
9009 | int i, ret; |
9010 | struct iovec iov; | |
edafccee | 9011 | |
87094465 PB |
9012 | if (ctx->user_bufs) |
9013 | return -EBUSY; | |
489809e2 | 9014 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
87094465 | 9015 | return -EINVAL; |
bd54b6fe | 9016 | ret = io_rsrc_node_switch_start(ctx); |
2b358604 BM |
9017 | if (ret) |
9018 | return ret; | |
d878c816 PB |
9019 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
9020 | if (ret) | |
9021 | return ret; | |
bd54b6fe BM |
9022 | ret = io_buffers_map_alloc(ctx, nr_args); |
9023 | if (ret) { | |
bb6659cc | 9024 | io_rsrc_data_free(data); |
bd54b6fe BM |
9025 | return ret; |
9026 | } | |
edafccee | 9027 | |
87094465 | 9028 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
edafccee JA |
9029 | ret = io_copy_iov(ctx, &iov, arg, i); |
9030 | if (ret) | |
0a96bbe4 | 9031 | break; |
2b358604 BM |
9032 | ret = io_buffer_validate(&iov); |
9033 | if (ret) | |
0a96bbe4 | 9034 | break; |
2d091d62 | 9035 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
cf3770e7 CIK |
9036 | ret = -EINVAL; |
9037 | break; | |
9038 | } | |
edafccee | 9039 | |
41edf1a5 PB |
9040 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
9041 | &last_hpage); | |
0a96bbe4 BM |
9042 | if (ret) |
9043 | break; | |
edafccee | 9044 | } |
0a96bbe4 | 9045 | |
bd54b6fe | 9046 | WARN_ON_ONCE(ctx->buf_data); |
0a96bbe4 | 9047 | |
bd54b6fe BM |
9048 | ctx->buf_data = data; |
9049 | if (ret) | |
9050 | __io_sqe_buffers_unregister(ctx); | |
9051 | else | |
9052 | io_rsrc_node_switch(ctx, NULL); | |
edafccee JA |
9053 | return ret; |
9054 | } | |
9055 | ||
634d00df PB |
9056 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
9057 | struct io_uring_rsrc_update2 *up, | |
9058 | unsigned int nr_args) | |
9059 | { | |
9060 | u64 __user *tags = u64_to_user_ptr(up->tags); | |
9061 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); | |
634d00df PB |
9062 | struct page *last_hpage = NULL; |
9063 | bool needs_switch = false; | |
9064 | __u32 done; | |
9065 | int i, err; | |
9066 | ||
9067 | if (!ctx->buf_data) | |
9068 | return -ENXIO; | |
9069 | if (up->offset + nr_args > ctx->nr_user_bufs) | |
9070 | return -EINVAL; | |
9071 | ||
9072 | for (done = 0; done < nr_args; done++) { | |
0b8c0e7c PB |
9073 | struct io_mapped_ubuf *imu; |
9074 | int offset = up->offset + done; | |
634d00df PB |
9075 | u64 tag = 0; |
9076 | ||
9077 | err = io_copy_iov(ctx, &iov, iovs, done); | |
9078 | if (err) | |
9079 | break; | |
9080 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { | |
9081 | err = -EFAULT; | |
9082 | break; | |
9083 | } | |
0b8c0e7c PB |
9084 | err = io_buffer_validate(&iov); |
9085 | if (err) | |
9086 | break; | |
cf3770e7 CIK |
9087 | if (!iov.iov_base && tag) { |
9088 | err = -EINVAL; | |
9089 | break; | |
9090 | } | |
0b8c0e7c PB |
9091 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
9092 | if (err) | |
9093 | break; | |
634d00df | 9094 | |
0b8c0e7c | 9095 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
6224843d | 9096 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
0b8c0e7c PB |
9097 | err = io_queue_rsrc_removal(ctx->buf_data, offset, |
9098 | ctx->rsrc_node, ctx->user_bufs[i]); | |
9099 | if (unlikely(err)) { | |
9100 | io_buffer_unmap(ctx, &imu); | |
634d00df | 9101 | break; |
0b8c0e7c | 9102 | } |
634d00df PB |
9103 | ctx->user_bufs[i] = NULL; |
9104 | needs_switch = true; | |
9105 | } | |
9106 | ||
0b8c0e7c | 9107 | ctx->user_bufs[i] = imu; |
2d091d62 | 9108 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
634d00df PB |
9109 | } |
9110 | ||
9111 | if (needs_switch) | |
9112 | io_rsrc_node_switch(ctx, ctx->buf_data); | |
9113 | return done ? done : err; | |
9114 | } | |
9115 | ||
9b402849 JA |
9116 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
9117 | { | |
9118 | __s32 __user *fds = arg; | |
9119 | int fd; | |
9120 | ||
9121 | if (ctx->cq_ev_fd) | |
9122 | return -EBUSY; | |
9123 | ||
9124 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
9125 | return -EFAULT; | |
9126 | ||
9127 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
9128 | if (IS_ERR(ctx->cq_ev_fd)) { | |
9129 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
fe7e3257 | 9130 | |
9b402849 JA |
9131 | ctx->cq_ev_fd = NULL; |
9132 | return ret; | |
9133 | } | |
9134 | ||
9135 | return 0; | |
9136 | } | |
9137 | ||
9138 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
9139 | { | |
9140 | if (ctx->cq_ev_fd) { | |
9141 | eventfd_ctx_put(ctx->cq_ev_fd); | |
9142 | ctx->cq_ev_fd = NULL; | |
9143 | return 0; | |
9144 | } | |
9145 | ||
9146 | return -ENXIO; | |
9147 | } | |
9148 | ||
5a2e745d JA |
9149 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
9150 | { | |
9e15c3a0 JA |
9151 | struct io_buffer *buf; |
9152 | unsigned long index; | |
9153 | ||
8bab4c09 | 9154 | xa_for_each(&ctx->io_buffers, index, buf) { |
9e15c3a0 | 9155 | __io_remove_buffers(ctx, buf, index, -1U); |
8bab4c09 JA |
9156 | cond_resched(); |
9157 | } | |
5a2e745d JA |
9158 | } |
9159 | ||
4010fec4 | 9160 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9161 | { |
cd0ca2e0 | 9162 | struct io_submit_state *state = &ctx->submit_state; |
bf019da7 | 9163 | |
9a4fdbd8 | 9164 | mutex_lock(&ctx->uring_lock); |
cd0ca2e0 | 9165 | io_flush_cached_locked_reqs(ctx, state); |
c2b6c6bc PB |
9166 | |
9167 | while (state->free_list.next) { | |
9168 | struct io_wq_work_node *node; | |
9169 | struct io_kiocb *req; | |
9170 | ||
9171 | node = wq_stack_extract(&state->free_list); | |
9172 | req = container_of(node, struct io_kiocb, comp_list); | |
9173 | kmem_cache_free(req_cachep, req); | |
9174 | } | |
9a4fdbd8 JA |
9175 | mutex_unlock(&ctx->uring_lock); |
9176 | } | |
9177 | ||
43597aac | 9178 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
2b188cc1 | 9179 | { |
43597aac | 9180 | if (data && !atomic_dec_and_test(&data->refs)) |
bd54b6fe | 9181 | wait_for_completion(&data->done); |
bd54b6fe | 9182 | } |
04fc6c80 | 9183 | |
2b188cc1 JA |
9184 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
9185 | { | |
37d1e2e3 | 9186 | io_sq_thread_finish(ctx); |
2aede0e4 | 9187 | |
37d1e2e3 | 9188 | if (ctx->mm_account) { |
2aede0e4 JA |
9189 | mmdrop(ctx->mm_account); |
9190 | ctx->mm_account = NULL; | |
30975825 | 9191 | } |
def596e9 | 9192 | |
43597aac PB |
9193 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
9194 | io_wait_rsrc_data(ctx->buf_data); | |
9195 | io_wait_rsrc_data(ctx->file_data); | |
9196 | ||
8bad28d8 | 9197 | mutex_lock(&ctx->uring_lock); |
43597aac | 9198 | if (ctx->buf_data) |
bd54b6fe | 9199 | __io_sqe_buffers_unregister(ctx); |
43597aac | 9200 | if (ctx->file_data) |
08480400 | 9201 | __io_sqe_files_unregister(ctx); |
c4ea060e PB |
9202 | if (ctx->rings) |
9203 | __io_cqring_overflow_flush(ctx, true); | |
8bad28d8 | 9204 | mutex_unlock(&ctx->uring_lock); |
9b402849 | 9205 | io_eventfd_unregister(ctx); |
5a2e745d | 9206 | io_destroy_buffers(ctx); |
07db298a PB |
9207 | if (ctx->sq_creds) |
9208 | put_cred(ctx->sq_creds); | |
def596e9 | 9209 | |
a7f0ed5a PB |
9210 | /* there are no registered resources left, nobody uses it */ |
9211 | if (ctx->rsrc_node) | |
9212 | io_rsrc_node_destroy(ctx->rsrc_node); | |
8dd03afe | 9213 | if (ctx->rsrc_backup_node) |
b895c9a6 | 9214 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
a7f0ed5a PB |
9215 | flush_delayed_work(&ctx->rsrc_put_work); |
9216 | ||
9217 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); | |
9218 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); | |
def596e9 | 9219 | |
2b188cc1 | 9220 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
9221 | if (ctx->ring_sock) { |
9222 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 9223 | sock_release(ctx->ring_sock); |
355e8d26 | 9224 | } |
2b188cc1 | 9225 | #endif |
ef9dd637 | 9226 | WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); |
2b188cc1 | 9227 | |
75b28aff | 9228 | io_mem_free(ctx->rings); |
2b188cc1 | 9229 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
9230 | |
9231 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 9232 | free_uid(ctx->user); |
4010fec4 | 9233 | io_req_caches_free(ctx); |
e941894e JA |
9234 | if (ctx->hash_map) |
9235 | io_wq_put_hash(ctx->hash_map); | |
78076bb6 | 9236 | kfree(ctx->cancel_hash); |
6224843d | 9237 | kfree(ctx->dummy_ubuf); |
2b188cc1 JA |
9238 | kfree(ctx); |
9239 | } | |
9240 | ||
9241 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
9242 | { | |
9243 | struct io_ring_ctx *ctx = file->private_data; | |
9244 | __poll_t mask = 0; | |
9245 | ||
311997b3 | 9246 | poll_wait(file, &ctx->poll_wait, wait); |
4f7067c3 SB |
9247 | /* |
9248 | * synchronizes with barrier from wq_has_sleeper call in | |
9249 | * io_commit_cqring | |
9250 | */ | |
2b188cc1 | 9251 | smp_rmb(); |
90554200 | 9252 | if (!io_sqring_full(ctx)) |
2b188cc1 | 9253 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
9254 | |
9255 | /* | |
9256 | * Don't flush cqring overflow list here, just do a simple check. | |
9257 | * Otherwise there could possible be ABBA deadlock: | |
9258 | * CPU0 CPU1 | |
9259 | * ---- ---- | |
9260 | * lock(&ctx->uring_lock); | |
9261 | * lock(&ep->mtx); | |
9262 | * lock(&ctx->uring_lock); | |
9263 | * lock(&ep->mtx); | |
9264 | * | |
9265 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
9266 | * pushs them to do the flush. | |
9267 | */ | |
5ed7a37d | 9268 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
2b188cc1 JA |
9269 | mask |= EPOLLIN | EPOLLRDNORM; |
9270 | ||
9271 | return mask; | |
9272 | } | |
9273 | ||
0bead8cd | 9274 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 9275 | { |
4379bf8b | 9276 | const struct cred *creds; |
071698e1 | 9277 | |
61cf9370 | 9278 | creds = xa_erase(&ctx->personalities, id); |
4379bf8b JA |
9279 | if (creds) { |
9280 | put_cred(creds); | |
0bead8cd | 9281 | return 0; |
1e6fa521 | 9282 | } |
0bead8cd YD |
9283 | |
9284 | return -EINVAL; | |
9285 | } | |
9286 | ||
d56d938b PB |
9287 | struct io_tctx_exit { |
9288 | struct callback_head task_work; | |
9289 | struct completion completion; | |
baf186c4 | 9290 | struct io_ring_ctx *ctx; |
d56d938b PB |
9291 | }; |
9292 | ||
9293 | static void io_tctx_exit_cb(struct callback_head *cb) | |
9294 | { | |
9295 | struct io_uring_task *tctx = current->io_uring; | |
9296 | struct io_tctx_exit *work; | |
9297 | ||
9298 | work = container_of(cb, struct io_tctx_exit, task_work); | |
9299 | /* | |
9300 | * When @in_idle, we're in cancellation and it's racy to remove the | |
9301 | * node. It'll be removed by the end of cancellation, just ignore it. | |
9302 | */ | |
9303 | if (!atomic_read(&tctx->in_idle)) | |
eef51daa | 9304 | io_uring_del_tctx_node((unsigned long)work->ctx); |
d56d938b PB |
9305 | complete(&work->completion); |
9306 | } | |
9307 | ||
28090c13 PB |
9308 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
9309 | { | |
9310 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
9311 | ||
9312 | return req->ctx == data; | |
9313 | } | |
9314 | ||
85faa7b8 JA |
9315 | static void io_ring_exit_work(struct work_struct *work) |
9316 | { | |
d56d938b | 9317 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 9318 | unsigned long timeout = jiffies + HZ * 60 * 5; |
58d3be2c | 9319 | unsigned long interval = HZ / 20; |
d56d938b PB |
9320 | struct io_tctx_exit exit; |
9321 | struct io_tctx_node *node; | |
9322 | int ret; | |
85faa7b8 | 9323 | |
56952e91 JA |
9324 | /* |
9325 | * If we're doing polled IO and end up having requests being | |
9326 | * submitted async (out-of-line), then completions can come in while | |
9327 | * we're waiting for refs to drop. We need to reap these manually, | |
9328 | * as nobody else will be looking for them. | |
9329 | */ | |
b2edc0a7 | 9330 | do { |
3dd0c97a | 9331 | io_uring_try_cancel_requests(ctx, NULL, true); |
28090c13 PB |
9332 | if (ctx->sq_data) { |
9333 | struct io_sq_data *sqd = ctx->sq_data; | |
9334 | struct task_struct *tsk; | |
9335 | ||
9336 | io_sq_thread_park(sqd); | |
9337 | tsk = sqd->thread; | |
9338 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) | |
9339 | io_wq_cancel_cb(tsk->io_uring->io_wq, | |
9340 | io_cancel_ctx_cb, ctx, true); | |
9341 | io_sq_thread_unpark(sqd); | |
9342 | } | |
b5bb3a24 | 9343 | |
58d3be2c PB |
9344 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
9345 | /* there is little hope left, don't run it too often */ | |
9346 | interval = HZ * 60; | |
9347 | } | |
9348 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); | |
d56d938b | 9349 | |
7f00651a PB |
9350 | init_completion(&exit.completion); |
9351 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
9352 | exit.ctx = ctx; | |
89b5066e PB |
9353 | /* |
9354 | * Some may use context even when all refs and requests have been put, | |
9355 | * and they are free to do so while still holding uring_lock or | |
5b0a6acc | 9356 | * completion_lock, see io_req_task_submit(). Apart from other work, |
89b5066e PB |
9357 | * this lock/unlock section also waits them to finish. |
9358 | */ | |
d56d938b PB |
9359 | mutex_lock(&ctx->uring_lock); |
9360 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
9361 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
9362 | ||
d56d938b PB |
9363 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
9364 | ctx_node); | |
7f00651a PB |
9365 | /* don't spin on a single task if cancellation failed */ |
9366 | list_rotate_left(&ctx->tctx_list); | |
d56d938b PB |
9367 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
9368 | if (WARN_ON_ONCE(ret)) | |
9369 | continue; | |
9370 | wake_up_process(node->task); | |
9371 | ||
9372 | mutex_unlock(&ctx->uring_lock); | |
9373 | wait_for_completion(&exit.completion); | |
d56d938b PB |
9374 | mutex_lock(&ctx->uring_lock); |
9375 | } | |
9376 | mutex_unlock(&ctx->uring_lock); | |
79ebeaee JA |
9377 | spin_lock(&ctx->completion_lock); |
9378 | spin_unlock(&ctx->completion_lock); | |
d56d938b | 9379 | |
85faa7b8 JA |
9380 | io_ring_ctx_free(ctx); |
9381 | } | |
9382 | ||
80c4cbdb PB |
9383 | /* Returns true if we found and killed one or more timeouts */ |
9384 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, | |
3dd0c97a | 9385 | bool cancel_all) |
80c4cbdb PB |
9386 | { |
9387 | struct io_kiocb *req, *tmp; | |
9388 | int canceled = 0; | |
9389 | ||
79ebeaee JA |
9390 | spin_lock(&ctx->completion_lock); |
9391 | spin_lock_irq(&ctx->timeout_lock); | |
80c4cbdb | 9392 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
3dd0c97a | 9393 | if (io_match_task(req, tsk, cancel_all)) { |
80c4cbdb PB |
9394 | io_kill_timeout(req, -ECANCELED); |
9395 | canceled++; | |
9396 | } | |
9397 | } | |
79ebeaee | 9398 | spin_unlock_irq(&ctx->timeout_lock); |
51520426 PB |
9399 | if (canceled != 0) |
9400 | io_commit_cqring(ctx); | |
79ebeaee | 9401 | spin_unlock(&ctx->completion_lock); |
80c4cbdb PB |
9402 | if (canceled != 0) |
9403 | io_cqring_ev_posted(ctx); | |
9404 | return canceled != 0; | |
9405 | } | |
9406 | ||
2b188cc1 JA |
9407 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
9408 | { | |
61cf9370 MWO |
9409 | unsigned long index; |
9410 | struct creds *creds; | |
9411 | ||
2b188cc1 JA |
9412 | mutex_lock(&ctx->uring_lock); |
9413 | percpu_ref_kill(&ctx->refs); | |
634578f8 | 9414 | if (ctx->rings) |
6c2450ae | 9415 | __io_cqring_overflow_flush(ctx, true); |
61cf9370 MWO |
9416 | xa_for_each(&ctx->personalities, index, creds) |
9417 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
9418 | mutex_unlock(&ctx->uring_lock); |
9419 | ||
3dd0c97a PB |
9420 | io_kill_timeouts(ctx, NULL, true); |
9421 | io_poll_remove_all(ctx, NULL, true); | |
561fb04a | 9422 | |
15dff286 | 9423 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 9424 | io_iopoll_try_reap_events(ctx); |
309fc03a | 9425 | |
85faa7b8 | 9426 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
9427 | /* |
9428 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
9429 | * if we're exiting a ton of rings at the same time. It just adds | |
9430 | * noise and overhead, there's no discernable change in runtime | |
9431 | * over using system_wq. | |
9432 | */ | |
9433 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
9434 | } |
9435 | ||
9436 | static int io_uring_release(struct inode *inode, struct file *file) | |
9437 | { | |
9438 | struct io_ring_ctx *ctx = file->private_data; | |
9439 | ||
9440 | file->private_data = NULL; | |
9441 | io_ring_ctx_wait_and_kill(ctx); | |
9442 | return 0; | |
9443 | } | |
9444 | ||
f6edbabb PB |
9445 | struct io_task_cancel { |
9446 | struct task_struct *task; | |
3dd0c97a | 9447 | bool all; |
f6edbabb | 9448 | }; |
f254ac04 | 9449 | |
f6edbabb | 9450 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 9451 | { |
9a472ef7 | 9452 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 9453 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
9454 | bool ret; |
9455 | ||
3dd0c97a | 9456 | if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
9457 | struct io_ring_ctx *ctx = req->ctx; |
9458 | ||
9459 | /* protect against races with linked timeouts */ | |
79ebeaee | 9460 | spin_lock(&ctx->completion_lock); |
3dd0c97a | 9461 | ret = io_match_task(req, cancel->task, cancel->all); |
79ebeaee | 9462 | spin_unlock(&ctx->completion_lock); |
9a472ef7 | 9463 | } else { |
3dd0c97a | 9464 | ret = io_match_task(req, cancel->task, cancel->all); |
9a472ef7 PB |
9465 | } |
9466 | return ret; | |
b711d4ea JA |
9467 | } |
9468 | ||
e1915f76 | 9469 | static bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
3dd0c97a | 9470 | struct task_struct *task, bool cancel_all) |
b7ddce3c | 9471 | { |
e1915f76 | 9472 | struct io_defer_entry *de; |
b7ddce3c PB |
9473 | LIST_HEAD(list); |
9474 | ||
79ebeaee | 9475 | spin_lock(&ctx->completion_lock); |
b7ddce3c | 9476 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
3dd0c97a | 9477 | if (io_match_task(de->req, task, cancel_all)) { |
b7ddce3c PB |
9478 | list_cut_position(&list, &ctx->defer_list, &de->list); |
9479 | break; | |
9480 | } | |
9481 | } | |
79ebeaee | 9482 | spin_unlock(&ctx->completion_lock); |
e1915f76 PB |
9483 | if (list_empty(&list)) |
9484 | return false; | |
b7ddce3c PB |
9485 | |
9486 | while (!list_empty(&list)) { | |
9487 | de = list_first_entry(&list, struct io_defer_entry, list); | |
9488 | list_del_init(&de->list); | |
f41db273 | 9489 | io_req_complete_failed(de->req, -ECANCELED); |
b7ddce3c PB |
9490 | kfree(de); |
9491 | } | |
e1915f76 | 9492 | return true; |
b7ddce3c PB |
9493 | } |
9494 | ||
1b00764f PB |
9495 | static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
9496 | { | |
9497 | struct io_tctx_node *node; | |
9498 | enum io_wq_cancel cret; | |
9499 | bool ret = false; | |
9500 | ||
9501 | mutex_lock(&ctx->uring_lock); | |
9502 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
9503 | struct io_uring_task *tctx = node->task->io_uring; | |
9504 | ||
9505 | /* | |
9506 | * io_wq will stay alive while we hold uring_lock, because it's | |
9507 | * killed after ctx nodes, which requires to take the lock. | |
9508 | */ | |
9509 | if (!tctx || !tctx->io_wq) | |
9510 | continue; | |
9511 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
9512 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9513 | } | |
9514 | mutex_unlock(&ctx->uring_lock); | |
9515 | ||
9516 | return ret; | |
9517 | } | |
9518 | ||
9936c7c2 PB |
9519 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
9520 | struct task_struct *task, | |
3dd0c97a | 9521 | bool cancel_all) |
9936c7c2 | 9522 | { |
3dd0c97a | 9523 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
1b00764f | 9524 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 PB |
9525 | |
9526 | while (1) { | |
9527 | enum io_wq_cancel cret; | |
9528 | bool ret = false; | |
9529 | ||
1b00764f PB |
9530 | if (!task) { |
9531 | ret |= io_uring_try_cancel_iowq(ctx); | |
9532 | } else if (tctx && tctx->io_wq) { | |
9533 | /* | |
9534 | * Cancels requests of all rings, not only @ctx, but | |
9535 | * it's fine as the task is in exit/exec. | |
9536 | */ | |
5aa75ed5 | 9537 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
9538 | &cancel, true); |
9539 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9540 | } | |
9541 | ||
9542 | /* SQPOLL thread does its own polling */ | |
3dd0c97a | 9543 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
d052d1d6 | 9544 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
5eef4e87 | 9545 | while (!wq_list_empty(&ctx->iopoll_list)) { |
9936c7c2 PB |
9546 | io_iopoll_try_reap_events(ctx); |
9547 | ret = true; | |
9548 | } | |
9549 | } | |
9550 | ||
3dd0c97a PB |
9551 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
9552 | ret |= io_poll_remove_all(ctx, task, cancel_all); | |
9553 | ret |= io_kill_timeouts(ctx, task, cancel_all); | |
e5dc480d PB |
9554 | if (task) |
9555 | ret |= io_run_task_work(); | |
9936c7c2 PB |
9556 | if (!ret) |
9557 | break; | |
9558 | cond_resched(); | |
9559 | } | |
9560 | } | |
9561 | ||
eef51daa | 9562 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
0f212204 | 9563 | { |
236434c3 | 9564 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 9565 | struct io_tctx_node *node; |
a528b04e | 9566 | int ret; |
236434c3 MWO |
9567 | |
9568 | if (unlikely(!tctx)) { | |
5aa75ed5 | 9569 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
9570 | if (unlikely(ret)) |
9571 | return ret; | |
236434c3 | 9572 | tctx = current->io_uring; |
0f212204 | 9573 | } |
cf27f3b1 PB |
9574 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
9575 | node = kmalloc(sizeof(*node), GFP_KERNEL); | |
9576 | if (!node) | |
9577 | return -ENOMEM; | |
9578 | node->ctx = ctx; | |
9579 | node->task = current; | |
13bf43f5 | 9580 | |
cf27f3b1 PB |
9581 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
9582 | node, GFP_KERNEL)); | |
9583 | if (ret) { | |
9584 | kfree(node); | |
9585 | return ret; | |
0f212204 | 9586 | } |
cf27f3b1 PB |
9587 | |
9588 | mutex_lock(&ctx->uring_lock); | |
9589 | list_add(&node->ctx_node, &ctx->tctx_list); | |
9590 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 9591 | } |
cf27f3b1 | 9592 | tctx->last = ctx; |
0f212204 JA |
9593 | return 0; |
9594 | } | |
9595 | ||
cf27f3b1 PB |
9596 | /* |
9597 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
9598 | */ | |
eef51daa | 9599 | static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
cf27f3b1 PB |
9600 | { |
9601 | struct io_uring_task *tctx = current->io_uring; | |
9602 | ||
9603 | if (likely(tctx && tctx->last == ctx)) | |
9604 | return 0; | |
eef51daa | 9605 | return __io_uring_add_tctx_node(ctx); |
cf27f3b1 PB |
9606 | } |
9607 | ||
0f212204 JA |
9608 | /* |
9609 | * Remove this io_uring_file -> task mapping. | |
9610 | */ | |
eef51daa | 9611 | static void io_uring_del_tctx_node(unsigned long index) |
0f212204 JA |
9612 | { |
9613 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 9614 | struct io_tctx_node *node; |
2941267b | 9615 | |
eebd2e37 PB |
9616 | if (!tctx) |
9617 | return; | |
13bf43f5 PB |
9618 | node = xa_erase(&tctx->xa, index); |
9619 | if (!node) | |
2941267b | 9620 | return; |
0f212204 | 9621 | |
13bf43f5 PB |
9622 | WARN_ON_ONCE(current != node->task); |
9623 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
9624 | ||
9625 | mutex_lock(&node->ctx->uring_lock); | |
9626 | list_del(&node->ctx_node); | |
9627 | mutex_unlock(&node->ctx->uring_lock); | |
9628 | ||
baf186c4 | 9629 | if (tctx->last == node->ctx) |
0f212204 | 9630 | tctx->last = NULL; |
13bf43f5 | 9631 | kfree(node); |
0f212204 JA |
9632 | } |
9633 | ||
8452d4a6 | 9634 | static void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 9635 | { |
ba5ef6dc | 9636 | struct io_wq *wq = tctx->io_wq; |
13bf43f5 | 9637 | struct io_tctx_node *node; |
de7f1d9e PB |
9638 | unsigned long index; |
9639 | ||
8bab4c09 | 9640 | xa_for_each(&tctx->xa, index, node) { |
eef51daa | 9641 | io_uring_del_tctx_node(index); |
8bab4c09 JA |
9642 | cond_resched(); |
9643 | } | |
b16ef427 ME |
9644 | if (wq) { |
9645 | /* | |
9646 | * Must be after io_uring_del_task_file() (removes nodes under | |
9647 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). | |
9648 | */ | |
ba5ef6dc | 9649 | io_wq_put_and_exit(wq); |
dadebc35 | 9650 | tctx->io_wq = NULL; |
b16ef427 | 9651 | } |
de7f1d9e PB |
9652 | } |
9653 | ||
3f48cf18 | 9654 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
521d6a73 | 9655 | { |
3f48cf18 PB |
9656 | if (tracked) |
9657 | return atomic_read(&tctx->inflight_tracked); | |
521d6a73 PB |
9658 | return percpu_counter_sum(&tctx->inflight); |
9659 | } | |
9660 | ||
09899b19 PB |
9661 | static void io_uring_drop_tctx_refs(struct task_struct *task) |
9662 | { | |
9663 | struct io_uring_task *tctx = task->io_uring; | |
9664 | unsigned int refs = tctx->cached_refs; | |
9665 | ||
e9dbe221 PB |
9666 | if (refs) { |
9667 | tctx->cached_refs = 0; | |
9668 | percpu_counter_sub(&tctx->inflight, refs); | |
9669 | put_task_struct_many(task, refs); | |
9670 | } | |
09899b19 PB |
9671 | } |
9672 | ||
78cc687b PB |
9673 | /* |
9674 | * Find any io_uring ctx that this task has registered or done IO on, and cancel | |
9675 | * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation. | |
9676 | */ | |
9677 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd) | |
0e9ddb39 | 9678 | { |
521d6a73 | 9679 | struct io_uring_task *tctx = current->io_uring; |
734551df | 9680 | struct io_ring_ctx *ctx; |
0e9ddb39 PB |
9681 | s64 inflight; |
9682 | DEFINE_WAIT(wait); | |
fdaf083c | 9683 | |
78cc687b PB |
9684 | WARN_ON_ONCE(sqd && sqd->thread != current); |
9685 | ||
6d042ffb PO |
9686 | if (!current->io_uring) |
9687 | return; | |
17a91051 PB |
9688 | if (tctx->io_wq) |
9689 | io_wq_exit_start(tctx->io_wq); | |
9690 | ||
0e9ddb39 PB |
9691 | atomic_inc(&tctx->in_idle); |
9692 | do { | |
e9dbe221 | 9693 | io_uring_drop_tctx_refs(current); |
0e9ddb39 | 9694 | /* read completions before cancelations */ |
78cc687b | 9695 | inflight = tctx_inflight(tctx, !cancel_all); |
0e9ddb39 PB |
9696 | if (!inflight) |
9697 | break; | |
fdaf083c | 9698 | |
78cc687b PB |
9699 | if (!sqd) { |
9700 | struct io_tctx_node *node; | |
9701 | unsigned long index; | |
0f212204 | 9702 | |
78cc687b PB |
9703 | xa_for_each(&tctx->xa, index, node) { |
9704 | /* sqpoll task will cancel all its requests */ | |
9705 | if (node->ctx->sq_data) | |
9706 | continue; | |
9707 | io_uring_try_cancel_requests(node->ctx, current, | |
9708 | cancel_all); | |
9709 | } | |
9710 | } else { | |
9711 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
9712 | io_uring_try_cancel_requests(ctx, current, | |
9713 | cancel_all); | |
9714 | } | |
17a91051 | 9715 | |
0f212204 | 9716 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
e9dbe221 | 9717 | io_uring_drop_tctx_refs(current); |
0f212204 | 9718 | /* |
a1bb3cd5 PB |
9719 | * If we've seen completions, retry without waiting. This |
9720 | * avoids a race where a completion comes in before we did | |
9721 | * prepare_to_wait(). | |
0f212204 | 9722 | */ |
3dd0c97a | 9723 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
a1bb3cd5 | 9724 | schedule(); |
f57555ed | 9725 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 9726 | } while (1); |
fdaf083c | 9727 | atomic_dec(&tctx->in_idle); |
de7f1d9e | 9728 | |
8452d4a6 | 9729 | io_uring_clean_tctx(tctx); |
3dd0c97a | 9730 | if (cancel_all) { |
3f48cf18 PB |
9731 | /* for exec all current's requests should be gone, kill tctx */ |
9732 | __io_uring_free(current); | |
9733 | } | |
44e728b8 PB |
9734 | } |
9735 | ||
f552a27a | 9736 | void __io_uring_cancel(bool cancel_all) |
78cc687b | 9737 | { |
f552a27a | 9738 | io_uring_cancel_generic(cancel_all, NULL); |
78cc687b PB |
9739 | } |
9740 | ||
6c5c240e RP |
9741 | static void *io_uring_validate_mmap_request(struct file *file, |
9742 | loff_t pgoff, size_t sz) | |
2b188cc1 | 9743 | { |
2b188cc1 | 9744 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 9745 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
9746 | struct page *page; |
9747 | void *ptr; | |
9748 | ||
9749 | switch (offset) { | |
9750 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
9751 | case IORING_OFF_CQ_RING: |
9752 | ptr = ctx->rings; | |
2b188cc1 JA |
9753 | break; |
9754 | case IORING_OFF_SQES: | |
9755 | ptr = ctx->sq_sqes; | |
9756 | break; | |
2b188cc1 | 9757 | default: |
6c5c240e | 9758 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
9759 | } |
9760 | ||
9761 | page = virt_to_head_page(ptr); | |
a50b854e | 9762 | if (sz > page_size(page)) |
6c5c240e RP |
9763 | return ERR_PTR(-EINVAL); |
9764 | ||
9765 | return ptr; | |
9766 | } | |
9767 | ||
9768 | #ifdef CONFIG_MMU | |
9769 | ||
9770 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9771 | { | |
9772 | size_t sz = vma->vm_end - vma->vm_start; | |
9773 | unsigned long pfn; | |
9774 | void *ptr; | |
9775 | ||
9776 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
9777 | if (IS_ERR(ptr)) | |
9778 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9779 | |
9780 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9781 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9782 | } | |
9783 | ||
6c5c240e RP |
9784 | #else /* !CONFIG_MMU */ |
9785 | ||
9786 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9787 | { | |
9788 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9789 | } | |
9790 | ||
9791 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9792 | { | |
9793 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9794 | } | |
9795 | ||
9796 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9797 | unsigned long addr, unsigned long len, | |
9798 | unsigned long pgoff, unsigned long flags) | |
9799 | { | |
9800 | void *ptr; | |
9801 | ||
9802 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9803 | if (IS_ERR(ptr)) | |
9804 | return PTR_ERR(ptr); | |
9805 | ||
9806 | return (unsigned long) ptr; | |
9807 | } | |
9808 | ||
9809 | #endif /* !CONFIG_MMU */ | |
9810 | ||
d9d05217 | 9811 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 JA |
9812 | { |
9813 | DEFINE_WAIT(wait); | |
9814 | ||
9815 | do { | |
9816 | if (!io_sqring_full(ctx)) | |
9817 | break; | |
90554200 JA |
9818 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
9819 | ||
9820 | if (!io_sqring_full(ctx)) | |
9821 | break; | |
90554200 JA |
9822 | schedule(); |
9823 | } while (!signal_pending(current)); | |
9824 | ||
9825 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
5199328a | 9826 | return 0; |
90554200 JA |
9827 | } |
9828 | ||
c73ebb68 HX |
9829 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9830 | struct __kernel_timespec __user **ts, | |
9831 | const sigset_t __user **sig) | |
9832 | { | |
9833 | struct io_uring_getevents_arg arg; | |
9834 | ||
9835 | /* | |
9836 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9837 | * is just a pointer to the sigset_t. | |
9838 | */ | |
9839 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9840 | *sig = (const sigset_t __user *) argp; | |
9841 | *ts = NULL; | |
9842 | return 0; | |
9843 | } | |
9844 | ||
9845 | /* | |
9846 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9847 | * timespec and sigset_t pointers if good. | |
9848 | */ | |
9849 | if (*argsz != sizeof(arg)) | |
9850 | return -EINVAL; | |
9851 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9852 | return -EFAULT; | |
9853 | *sig = u64_to_user_ptr(arg.sigmask); | |
9854 | *argsz = arg.sigmask_sz; | |
9855 | *ts = u64_to_user_ptr(arg.ts); | |
9856 | return 0; | |
9857 | } | |
9858 | ||
2b188cc1 | 9859 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9860 | u32, min_complete, u32, flags, const void __user *, argp, |
9861 | size_t, argsz) | |
2b188cc1 JA |
9862 | { |
9863 | struct io_ring_ctx *ctx; | |
2b188cc1 JA |
9864 | int submitted = 0; |
9865 | struct fd f; | |
33f993da | 9866 | long ret; |
2b188cc1 | 9867 | |
4c6e277c | 9868 | io_run_task_work(); |
b41e9852 | 9869 | |
33f993da PB |
9870 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
9871 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) | |
2b188cc1 JA |
9872 | return -EINVAL; |
9873 | ||
9874 | f = fdget(fd); | |
33f993da | 9875 | if (unlikely(!f.file)) |
2b188cc1 JA |
9876 | return -EBADF; |
9877 | ||
9878 | ret = -EOPNOTSUPP; | |
33f993da | 9879 | if (unlikely(f.file->f_op != &io_uring_fops)) |
2b188cc1 JA |
9880 | goto out_fput; |
9881 | ||
9882 | ret = -ENXIO; | |
9883 | ctx = f.file->private_data; | |
33f993da | 9884 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
2b188cc1 JA |
9885 | goto out_fput; |
9886 | ||
7e84e1c7 | 9887 | ret = -EBADFD; |
33f993da | 9888 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
9889 | goto out; |
9890 | ||
6c271ce2 JA |
9891 | /* |
9892 | * For SQ polling, the thread will do all submissions and completions. | |
9893 | * Just return the requested submit count, and wake the thread if | |
9894 | * we were asked to. | |
9895 | */ | |
b2a9eada | 9896 | ret = 0; |
6c271ce2 | 9897 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
90f67366 | 9898 | io_cqring_overflow_flush(ctx); |
89448c47 | 9899 | |
21f96522 JA |
9900 | if (unlikely(ctx->sq_data->thread == NULL)) { |
9901 | ret = -EOWNERDEAD; | |
04147488 | 9902 | goto out; |
21f96522 | 9903 | } |
6c271ce2 | 9904 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9905 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9906 | if (flags & IORING_ENTER_SQ_WAIT) { |
9907 | ret = io_sqpoll_wait_sq(ctx); | |
9908 | if (ret) | |
9909 | goto out; | |
9910 | } | |
6c271ce2 | 9911 | submitted = to_submit; |
b2a9eada | 9912 | } else if (to_submit) { |
eef51daa | 9913 | ret = io_uring_add_tctx_node(ctx); |
0f212204 JA |
9914 | if (unlikely(ret)) |
9915 | goto out; | |
2b188cc1 | 9916 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9917 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9918 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9919 | |
9920 | if (submitted != to_submit) | |
9921 | goto out; | |
2b188cc1 JA |
9922 | } |
9923 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9924 | const sigset_t __user *sig; |
9925 | struct __kernel_timespec __user *ts; | |
9926 | ||
9927 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9928 | if (unlikely(ret)) | |
9929 | goto out; | |
9930 | ||
2b188cc1 JA |
9931 | min_complete = min(min_complete, ctx->cq_entries); |
9932 | ||
32b2244a XW |
9933 | /* |
9934 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9935 | * space applications don't need to do io completion events | |
9936 | * polling again, they can rely on io_sq_thread to do polling | |
9937 | * work, which can reduce cpu usage and uring_lock contention. | |
9938 | */ | |
9939 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
9940 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 9941 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 9942 | } else { |
c73ebb68 | 9943 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 9944 | } |
2b188cc1 JA |
9945 | } |
9946 | ||
7c504e65 | 9947 | out: |
6805b32e | 9948 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
9949 | out_fput: |
9950 | fdput(f); | |
9951 | return submitted ? submitted : ret; | |
9952 | } | |
9953 | ||
bebdb65e | 9954 | #ifdef CONFIG_PROC_FS |
61cf9370 MWO |
9955 | static int io_uring_show_cred(struct seq_file *m, unsigned int id, |
9956 | const struct cred *cred) | |
87ce955b | 9957 | { |
87ce955b JA |
9958 | struct user_namespace *uns = seq_user_ns(m); |
9959 | struct group_info *gi; | |
9960 | kernel_cap_t cap; | |
9961 | unsigned __capi; | |
9962 | int g; | |
9963 | ||
9964 | seq_printf(m, "%5d\n", id); | |
9965 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
9966 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
9967 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
9968 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
9969 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
9970 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
9971 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
9972 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
9973 | seq_puts(m, "\n\tGroups:\t"); | |
9974 | gi = cred->group_info; | |
9975 | for (g = 0; g < gi->ngroups; g++) { | |
9976 | seq_put_decimal_ull(m, g ? " " : "", | |
9977 | from_kgid_munged(uns, gi->gid[g])); | |
9978 | } | |
9979 | seq_puts(m, "\n\tCapEff:\t"); | |
9980 | cap = cred->cap_effective; | |
9981 | CAP_FOR_EACH_U32(__capi) | |
9982 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
9983 | seq_putc(m, '\n'); | |
9984 | return 0; | |
9985 | } | |
9986 | ||
9987 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
9988 | { | |
dbbe9c64 | 9989 | struct io_sq_data *sq = NULL; |
83f84356 HX |
9990 | struct io_overflow_cqe *ocqe; |
9991 | struct io_rings *r = ctx->rings; | |
9992 | unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1; | |
9993 | unsigned int cached_sq_head = ctx->cached_sq_head; | |
9994 | unsigned int cached_cq_tail = ctx->cached_cq_tail; | |
9995 | unsigned int sq_head = READ_ONCE(r->sq.head); | |
9996 | unsigned int sq_tail = READ_ONCE(r->sq.tail); | |
9997 | unsigned int cq_head = READ_ONCE(r->cq.head); | |
9998 | unsigned int cq_tail = READ_ONCE(r->cq.tail); | |
fad8e0de | 9999 | bool has_lock; |
83f84356 HX |
10000 | unsigned int i; |
10001 | ||
10002 | /* | |
10003 | * we may get imprecise sqe and cqe info if uring is actively running | |
10004 | * since we get cached_sq_head and cached_cq_tail without uring_lock | |
10005 | * and sq_tail and cq_head are changed by userspace. But it's ok since | |
10006 | * we usually use these info when it is stuck. | |
10007 | */ | |
10008 | seq_printf(m, "SqHead:\t%u\n", sq_head & sq_mask); | |
10009 | seq_printf(m, "SqTail:\t%u\n", sq_tail & sq_mask); | |
10010 | seq_printf(m, "CachedSqHead:\t%u\n", cached_sq_head & sq_mask); | |
10011 | seq_printf(m, "CqHead:\t%u\n", cq_head & cq_mask); | |
10012 | seq_printf(m, "CqTail:\t%u\n", cq_tail & cq_mask); | |
10013 | seq_printf(m, "CachedCqTail:\t%u\n", cached_cq_tail & cq_mask); | |
10014 | seq_printf(m, "SQEs:\t%u\n", sq_tail - cached_sq_head); | |
10015 | for (i = cached_sq_head; i < sq_tail; i++) { | |
10016 | unsigned int sq_idx = READ_ONCE(ctx->sq_array[i & sq_mask]); | |
10017 | ||
10018 | if (likely(sq_idx <= sq_mask)) { | |
10019 | struct io_uring_sqe *sqe = &ctx->sq_sqes[sq_idx]; | |
10020 | ||
10021 | seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n", | |
10022 | sq_idx, sqe->opcode, sqe->fd, sqe->flags, sqe->user_data); | |
10023 | } | |
10024 | } | |
10025 | seq_printf(m, "CQEs:\t%u\n", cached_cq_tail - cq_head); | |
10026 | for (i = cq_head; i < cached_cq_tail; i++) { | |
10027 | struct io_uring_cqe *cqe = &r->cqes[i & cq_mask]; | |
10028 | ||
10029 | seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n", | |
10030 | i & cq_mask, cqe->user_data, cqe->res, cqe->flags); | |
10031 | } | |
87ce955b | 10032 | |
fad8e0de JA |
10033 | /* |
10034 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
10035 | * since fdinfo case grabs it in the opposite direction of normal use | |
10036 | * cases. If we fail to get the lock, we just don't iterate any | |
10037 | * structures that could be going away outside the io_uring mutex. | |
10038 | */ | |
10039 | has_lock = mutex_trylock(&ctx->uring_lock); | |
10040 | ||
5f3f26f9 | 10041 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 10042 | sq = ctx->sq_data; |
5f3f26f9 JA |
10043 | if (!sq->thread) |
10044 | sq = NULL; | |
10045 | } | |
dbbe9c64 JQ |
10046 | |
10047 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
10048 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 10049 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 10050 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
7b29f92d | 10051 | struct file *f = io_file_from_index(ctx, i); |
87ce955b | 10052 | |
87ce955b JA |
10053 | if (f) |
10054 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
10055 | else | |
10056 | seq_printf(m, "%5u: <none>\n", i); | |
10057 | } | |
10058 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 10059 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
41edf1a5 | 10060 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
4751f53d | 10061 | unsigned int len = buf->ubuf_end - buf->ubuf; |
87ce955b | 10062 | |
4751f53d | 10063 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
87ce955b | 10064 | } |
61cf9370 MWO |
10065 | if (has_lock && !xa_empty(&ctx->personalities)) { |
10066 | unsigned long index; | |
10067 | const struct cred *cred; | |
10068 | ||
87ce955b | 10069 | seq_printf(m, "Personalities:\n"); |
61cf9370 MWO |
10070 | xa_for_each(&ctx->personalities, index, cred) |
10071 | io_uring_show_cred(m, index, cred); | |
87ce955b | 10072 | } |
83f84356 HX |
10073 | if (has_lock) |
10074 | mutex_unlock(&ctx->uring_lock); | |
10075 | ||
10076 | seq_puts(m, "PollList:\n"); | |
79ebeaee | 10077 | spin_lock(&ctx->completion_lock); |
d7718a9d JA |
10078 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
10079 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
10080 | struct io_kiocb *req; | |
10081 | ||
10082 | hlist_for_each_entry(req, list, hash_node) | |
10083 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
10084 | req->task->task_works != NULL); | |
10085 | } | |
83f84356 HX |
10086 | |
10087 | seq_puts(m, "CqOverflowList:\n"); | |
10088 | list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) { | |
10089 | struct io_uring_cqe *cqe = &ocqe->cqe; | |
10090 | ||
10091 | seq_printf(m, " user_data=%llu, res=%d, flags=%x\n", | |
10092 | cqe->user_data, cqe->res, cqe->flags); | |
10093 | ||
10094 | } | |
10095 | ||
79ebeaee | 10096 | spin_unlock(&ctx->completion_lock); |
87ce955b JA |
10097 | } |
10098 | ||
10099 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
10100 | { | |
10101 | struct io_ring_ctx *ctx = f->private_data; | |
10102 | ||
10103 | if (percpu_ref_tryget(&ctx->refs)) { | |
10104 | __io_uring_show_fdinfo(ctx, m); | |
10105 | percpu_ref_put(&ctx->refs); | |
10106 | } | |
10107 | } | |
bebdb65e | 10108 | #endif |
87ce955b | 10109 | |
2b188cc1 JA |
10110 | static const struct file_operations io_uring_fops = { |
10111 | .release = io_uring_release, | |
10112 | .mmap = io_uring_mmap, | |
6c5c240e RP |
10113 | #ifndef CONFIG_MMU |
10114 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
10115 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
10116 | #endif | |
2b188cc1 | 10117 | .poll = io_uring_poll, |
bebdb65e | 10118 | #ifdef CONFIG_PROC_FS |
87ce955b | 10119 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 10120 | #endif |
2b188cc1 JA |
10121 | }; |
10122 | ||
10123 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
10124 | struct io_uring_params *p) | |
10125 | { | |
75b28aff HV |
10126 | struct io_rings *rings; |
10127 | size_t size, sq_array_offset; | |
2b188cc1 | 10128 | |
bd740481 JA |
10129 | /* make sure these are sane, as we already accounted them */ |
10130 | ctx->sq_entries = p->sq_entries; | |
10131 | ctx->cq_entries = p->cq_entries; | |
10132 | ||
75b28aff HV |
10133 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
10134 | if (size == SIZE_MAX) | |
10135 | return -EOVERFLOW; | |
10136 | ||
10137 | rings = io_mem_alloc(size); | |
10138 | if (!rings) | |
2b188cc1 JA |
10139 | return -ENOMEM; |
10140 | ||
75b28aff HV |
10141 | ctx->rings = rings; |
10142 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
10143 | rings->sq_ring_mask = p->sq_entries - 1; | |
10144 | rings->cq_ring_mask = p->cq_entries - 1; | |
10145 | rings->sq_ring_entries = p->sq_entries; | |
10146 | rings->cq_ring_entries = p->cq_entries; | |
2b188cc1 JA |
10147 | |
10148 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
10149 | if (size == SIZE_MAX) { |
10150 | io_mem_free(ctx->rings); | |
10151 | ctx->rings = NULL; | |
2b188cc1 | 10152 | return -EOVERFLOW; |
eb065d30 | 10153 | } |
2b188cc1 JA |
10154 | |
10155 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
10156 | if (!ctx->sq_sqes) { |
10157 | io_mem_free(ctx->rings); | |
10158 | ctx->rings = NULL; | |
2b188cc1 | 10159 | return -ENOMEM; |
eb065d30 | 10160 | } |
2b188cc1 | 10161 | |
2b188cc1 JA |
10162 | return 0; |
10163 | } | |
10164 | ||
9faadcc8 PB |
10165 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
10166 | { | |
10167 | int ret, fd; | |
10168 | ||
10169 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
10170 | if (fd < 0) | |
10171 | return fd; | |
10172 | ||
eef51daa | 10173 | ret = io_uring_add_tctx_node(ctx); |
9faadcc8 PB |
10174 | if (ret) { |
10175 | put_unused_fd(fd); | |
10176 | return ret; | |
10177 | } | |
10178 | fd_install(fd, file); | |
10179 | return fd; | |
10180 | } | |
10181 | ||
2b188cc1 JA |
10182 | /* |
10183 | * Allocate an anonymous fd, this is what constitutes the application | |
10184 | * visible backing of an io_uring instance. The application mmaps this | |
10185 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
10186 | * we have to tie this fd to a socket for file garbage collection purposes. | |
10187 | */ | |
9faadcc8 | 10188 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
10189 | { |
10190 | struct file *file; | |
9faadcc8 | 10191 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
10192 | int ret; |
10193 | ||
2b188cc1 JA |
10194 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
10195 | &ctx->ring_sock); | |
10196 | if (ret) | |
9faadcc8 | 10197 | return ERR_PTR(ret); |
2b188cc1 JA |
10198 | #endif |
10199 | ||
2b188cc1 JA |
10200 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
10201 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 10202 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
10203 | if (IS_ERR(file)) { |
10204 | sock_release(ctx->ring_sock); | |
10205 | ctx->ring_sock = NULL; | |
10206 | } else { | |
10207 | ctx->ring_sock->file = file; | |
0f212204 | 10208 | } |
2b188cc1 | 10209 | #endif |
9faadcc8 | 10210 | return file; |
2b188cc1 JA |
10211 | } |
10212 | ||
7f13657d XW |
10213 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
10214 | struct io_uring_params __user *params) | |
2b188cc1 | 10215 | { |
2b188cc1 | 10216 | struct io_ring_ctx *ctx; |
9faadcc8 | 10217 | struct file *file; |
2b188cc1 JA |
10218 | int ret; |
10219 | ||
8110c1a6 | 10220 | if (!entries) |
2b188cc1 | 10221 | return -EINVAL; |
8110c1a6 JA |
10222 | if (entries > IORING_MAX_ENTRIES) { |
10223 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
10224 | return -EINVAL; | |
10225 | entries = IORING_MAX_ENTRIES; | |
10226 | } | |
2b188cc1 JA |
10227 | |
10228 | /* | |
10229 | * Use twice as many entries for the CQ ring. It's possible for the | |
10230 | * application to drive a higher depth than the size of the SQ ring, | |
10231 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
10232 | * some flexibility in overcommitting a bit. If the application has |
10233 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
10234 | * of CQ ring entries manually. | |
2b188cc1 JA |
10235 | */ |
10236 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
10237 | if (p->flags & IORING_SETUP_CQSIZE) { |
10238 | /* | |
10239 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
10240 | * to a power-of-two, if it isn't already. We do NOT impose | |
10241 | * any cq vs sq ring sizing. | |
10242 | */ | |
eb2667b3 | 10243 | if (!p->cq_entries) |
33a107f0 | 10244 | return -EINVAL; |
8110c1a6 JA |
10245 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
10246 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
10247 | return -EINVAL; | |
10248 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
10249 | } | |
eb2667b3 JQ |
10250 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
10251 | if (p->cq_entries < p->sq_entries) | |
10252 | return -EINVAL; | |
33a107f0 JA |
10253 | } else { |
10254 | p->cq_entries = 2 * p->sq_entries; | |
10255 | } | |
2b188cc1 | 10256 | |
2b188cc1 | 10257 | ctx = io_ring_ctx_alloc(p); |
62e398be | 10258 | if (!ctx) |
2b188cc1 | 10259 | return -ENOMEM; |
2b188cc1 | 10260 | ctx->compat = in_compat_syscall(); |
62e398be JA |
10261 | if (!capable(CAP_IPC_LOCK)) |
10262 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
10263 | |
10264 | /* | |
10265 | * This is just grabbed for accounting purposes. When a process exits, | |
10266 | * the mm is exited and dropped before the files, hence we need to hang | |
10267 | * on to this mm purely for the purposes of being able to unaccount | |
10268 | * memory (locked/pinned vm). It's not used for anything else. | |
10269 | */ | |
6b7898eb | 10270 | mmgrab(current->mm); |
2aede0e4 | 10271 | ctx->mm_account = current->mm; |
6b7898eb | 10272 | |
2b188cc1 JA |
10273 | ret = io_allocate_scq_urings(ctx, p); |
10274 | if (ret) | |
10275 | goto err; | |
10276 | ||
7e84e1c7 | 10277 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
10278 | if (ret) |
10279 | goto err; | |
eae071c9 | 10280 | /* always set a rsrc node */ |
47b228ce PB |
10281 | ret = io_rsrc_node_switch_start(ctx); |
10282 | if (ret) | |
10283 | goto err; | |
eae071c9 | 10284 | io_rsrc_node_switch(ctx, NULL); |
2b188cc1 | 10285 | |
2b188cc1 | 10286 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
10287 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
10288 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
10289 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
10290 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
10291 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
10292 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
10293 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
10294 | |
10295 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
10296 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
10297 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
10298 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
10299 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
10300 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
10301 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 10302 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 10303 | |
7f13657d XW |
10304 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
10305 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 10306 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 10307 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9690557e PB |
10308 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
10309 | IORING_FEAT_RSRC_TAGS; | |
7f13657d XW |
10310 | |
10311 | if (copy_to_user(params, p, sizeof(*p))) { | |
10312 | ret = -EFAULT; | |
10313 | goto err; | |
10314 | } | |
d1719f70 | 10315 | |
9faadcc8 PB |
10316 | file = io_uring_get_file(ctx); |
10317 | if (IS_ERR(file)) { | |
10318 | ret = PTR_ERR(file); | |
10319 | goto err; | |
10320 | } | |
10321 | ||
044c1ab3 JA |
10322 | /* |
10323 | * Install ring fd as the very last thing, so we don't risk someone | |
10324 | * having closed it before we finish setup | |
10325 | */ | |
9faadcc8 PB |
10326 | ret = io_uring_install_fd(ctx, file); |
10327 | if (ret < 0) { | |
10328 | /* fput will clean it up */ | |
10329 | fput(file); | |
10330 | return ret; | |
10331 | } | |
044c1ab3 | 10332 | |
c826bd7a | 10333 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
10334 | return ret; |
10335 | err: | |
10336 | io_ring_ctx_wait_and_kill(ctx); | |
10337 | return ret; | |
10338 | } | |
10339 | ||
10340 | /* | |
10341 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
10342 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
10343 | * params structure passed in. | |
10344 | */ | |
10345 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
10346 | { | |
10347 | struct io_uring_params p; | |
2b188cc1 JA |
10348 | int i; |
10349 | ||
10350 | if (copy_from_user(&p, params, sizeof(p))) | |
10351 | return -EFAULT; | |
10352 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
10353 | if (p.resv[i]) | |
10354 | return -EINVAL; | |
10355 | } | |
10356 | ||
6c271ce2 | 10357 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 10358 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
10359 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
10360 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
10361 | return -EINVAL; |
10362 | ||
7f13657d | 10363 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
10364 | } |
10365 | ||
10366 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
10367 | struct io_uring_params __user *, params) | |
10368 | { | |
10369 | return io_uring_setup(entries, params); | |
10370 | } | |
10371 | ||
66f4af93 JA |
10372 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
10373 | { | |
10374 | struct io_uring_probe *p; | |
10375 | size_t size; | |
10376 | int i, ret; | |
10377 | ||
10378 | size = struct_size(p, ops, nr_args); | |
10379 | if (size == SIZE_MAX) | |
10380 | return -EOVERFLOW; | |
10381 | p = kzalloc(size, GFP_KERNEL); | |
10382 | if (!p) | |
10383 | return -ENOMEM; | |
10384 | ||
10385 | ret = -EFAULT; | |
10386 | if (copy_from_user(p, arg, size)) | |
10387 | goto out; | |
10388 | ret = -EINVAL; | |
10389 | if (memchr_inv(p, 0, size)) | |
10390 | goto out; | |
10391 | ||
10392 | p->last_op = IORING_OP_LAST - 1; | |
10393 | if (nr_args > IORING_OP_LAST) | |
10394 | nr_args = IORING_OP_LAST; | |
10395 | ||
10396 | for (i = 0; i < nr_args; i++) { | |
10397 | p->ops[i].op = i; | |
10398 | if (!io_op_defs[i].not_supported) | |
10399 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
10400 | } | |
10401 | p->ops_len = i; | |
10402 | ||
10403 | ret = 0; | |
10404 | if (copy_to_user(arg, p, size)) | |
10405 | ret = -EFAULT; | |
10406 | out: | |
10407 | kfree(p); | |
10408 | return ret; | |
10409 | } | |
10410 | ||
071698e1 JA |
10411 | static int io_register_personality(struct io_ring_ctx *ctx) |
10412 | { | |
4379bf8b | 10413 | const struct cred *creds; |
61cf9370 | 10414 | u32 id; |
1e6fa521 | 10415 | int ret; |
071698e1 | 10416 | |
4379bf8b | 10417 | creds = get_current_cred(); |
1e6fa521 | 10418 | |
61cf9370 MWO |
10419 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
10420 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); | |
a30f895a JA |
10421 | if (ret < 0) { |
10422 | put_cred(creds); | |
10423 | return ret; | |
10424 | } | |
10425 | return id; | |
071698e1 JA |
10426 | } |
10427 | ||
21b55dbc SG |
10428 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
10429 | unsigned int nr_args) | |
10430 | { | |
10431 | struct io_uring_restriction *res; | |
10432 | size_t size; | |
10433 | int i, ret; | |
10434 | ||
7e84e1c7 SG |
10435 | /* Restrictions allowed only if rings started disabled */ |
10436 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10437 | return -EBADFD; | |
10438 | ||
21b55dbc | 10439 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 10440 | if (ctx->restrictions.registered) |
21b55dbc SG |
10441 | return -EBUSY; |
10442 | ||
10443 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
10444 | return -EINVAL; | |
10445 | ||
10446 | size = array_size(nr_args, sizeof(*res)); | |
10447 | if (size == SIZE_MAX) | |
10448 | return -EOVERFLOW; | |
10449 | ||
10450 | res = memdup_user(arg, size); | |
10451 | if (IS_ERR(res)) | |
10452 | return PTR_ERR(res); | |
10453 | ||
10454 | ret = 0; | |
10455 | ||
10456 | for (i = 0; i < nr_args; i++) { | |
10457 | switch (res[i].opcode) { | |
10458 | case IORING_RESTRICTION_REGISTER_OP: | |
10459 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
10460 | ret = -EINVAL; | |
10461 | goto out; | |
10462 | } | |
10463 | ||
10464 | __set_bit(res[i].register_op, | |
10465 | ctx->restrictions.register_op); | |
10466 | break; | |
10467 | case IORING_RESTRICTION_SQE_OP: | |
10468 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
10469 | ret = -EINVAL; | |
10470 | goto out; | |
10471 | } | |
10472 | ||
10473 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
10474 | break; | |
10475 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
10476 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
10477 | break; | |
10478 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
10479 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
10480 | break; | |
10481 | default: | |
10482 | ret = -EINVAL; | |
10483 | goto out; | |
10484 | } | |
10485 | } | |
10486 | ||
10487 | out: | |
10488 | /* Reset all restrictions if an error happened */ | |
10489 | if (ret != 0) | |
10490 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
10491 | else | |
7e84e1c7 | 10492 | ctx->restrictions.registered = true; |
21b55dbc SG |
10493 | |
10494 | kfree(res); | |
10495 | return ret; | |
10496 | } | |
10497 | ||
7e84e1c7 SG |
10498 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
10499 | { | |
10500 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10501 | return -EBADFD; | |
10502 | ||
10503 | if (ctx->restrictions.registered) | |
10504 | ctx->restricted = 1; | |
10505 | ||
0298ef96 PB |
10506 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
10507 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) | |
10508 | wake_up(&ctx->sq_data->wait); | |
7e84e1c7 SG |
10509 | return 0; |
10510 | } | |
10511 | ||
fdecb662 | 10512 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 10513 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 PB |
10514 | unsigned nr_args) |
10515 | { | |
10516 | __u32 tmp; | |
10517 | int err; | |
10518 | ||
c3bdad02 PB |
10519 | if (up->resv) |
10520 | return -EINVAL; | |
98f0b3b4 PB |
10521 | if (check_add_overflow(up->offset, nr_args, &tmp)) |
10522 | return -EOVERFLOW; | |
10523 | err = io_rsrc_node_switch_start(ctx); | |
10524 | if (err) | |
10525 | return err; | |
10526 | ||
fdecb662 PB |
10527 | switch (type) { |
10528 | case IORING_RSRC_FILE: | |
98f0b3b4 | 10529 | return __io_sqe_files_update(ctx, up, nr_args); |
634d00df PB |
10530 | case IORING_RSRC_BUFFER: |
10531 | return __io_sqe_buffers_update(ctx, up, nr_args); | |
98f0b3b4 PB |
10532 | } |
10533 | return -EINVAL; | |
10534 | } | |
10535 | ||
c3bdad02 PB |
10536 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
10537 | unsigned nr_args) | |
98f0b3b4 | 10538 | { |
c3bdad02 | 10539 | struct io_uring_rsrc_update2 up; |
98f0b3b4 PB |
10540 | |
10541 | if (!nr_args) | |
10542 | return -EINVAL; | |
c3bdad02 PB |
10543 | memset(&up, 0, sizeof(up)); |
10544 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) | |
10545 | return -EFAULT; | |
10546 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); | |
10547 | } | |
10548 | ||
10549 | static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
992da01a | 10550 | unsigned size, unsigned type) |
c3bdad02 PB |
10551 | { |
10552 | struct io_uring_rsrc_update2 up; | |
10553 | ||
10554 | if (size != sizeof(up)) | |
10555 | return -EINVAL; | |
98f0b3b4 PB |
10556 | if (copy_from_user(&up, arg, sizeof(up))) |
10557 | return -EFAULT; | |
992da01a | 10558 | if (!up.nr || up.resv) |
98f0b3b4 | 10559 | return -EINVAL; |
992da01a | 10560 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
98f0b3b4 PB |
10561 | } |
10562 | ||
792e3582 | 10563 | static int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
992da01a | 10564 | unsigned int size, unsigned int type) |
792e3582 PB |
10565 | { |
10566 | struct io_uring_rsrc_register rr; | |
10567 | ||
10568 | /* keep it extendible */ | |
10569 | if (size != sizeof(rr)) | |
10570 | return -EINVAL; | |
10571 | ||
10572 | memset(&rr, 0, sizeof(rr)); | |
10573 | if (copy_from_user(&rr, arg, size)) | |
10574 | return -EFAULT; | |
992da01a | 10575 | if (!rr.nr || rr.resv || rr.resv2) |
792e3582 PB |
10576 | return -EINVAL; |
10577 | ||
992da01a | 10578 | switch (type) { |
792e3582 PB |
10579 | case IORING_RSRC_FILE: |
10580 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), | |
10581 | rr.nr, u64_to_user_ptr(rr.tags)); | |
634d00df PB |
10582 | case IORING_RSRC_BUFFER: |
10583 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), | |
10584 | rr.nr, u64_to_user_ptr(rr.tags)); | |
792e3582 PB |
10585 | } |
10586 | return -EINVAL; | |
10587 | } | |
10588 | ||
fe76421d JA |
10589 | static int io_register_iowq_aff(struct io_ring_ctx *ctx, void __user *arg, |
10590 | unsigned len) | |
10591 | { | |
10592 | struct io_uring_task *tctx = current->io_uring; | |
10593 | cpumask_var_t new_mask; | |
10594 | int ret; | |
10595 | ||
10596 | if (!tctx || !tctx->io_wq) | |
10597 | return -EINVAL; | |
10598 | ||
10599 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | |
10600 | return -ENOMEM; | |
10601 | ||
10602 | cpumask_clear(new_mask); | |
10603 | if (len > cpumask_size()) | |
10604 | len = cpumask_size(); | |
10605 | ||
10606 | if (copy_from_user(new_mask, arg, len)) { | |
10607 | free_cpumask_var(new_mask); | |
10608 | return -EFAULT; | |
10609 | } | |
10610 | ||
10611 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); | |
10612 | free_cpumask_var(new_mask); | |
10613 | return ret; | |
10614 | } | |
10615 | ||
10616 | static int io_unregister_iowq_aff(struct io_ring_ctx *ctx) | |
10617 | { | |
10618 | struct io_uring_task *tctx = current->io_uring; | |
10619 | ||
10620 | if (!tctx || !tctx->io_wq) | |
10621 | return -EINVAL; | |
10622 | ||
10623 | return io_wq_cpu_affinity(tctx->io_wq, NULL); | |
10624 | } | |
10625 | ||
2e480058 JA |
10626 | static int io_register_iowq_max_workers(struct io_ring_ctx *ctx, |
10627 | void __user *arg) | |
10628 | { | |
fa84693b JA |
10629 | struct io_uring_task *tctx = NULL; |
10630 | struct io_sq_data *sqd = NULL; | |
2e480058 JA |
10631 | __u32 new_count[2]; |
10632 | int i, ret; | |
10633 | ||
2e480058 JA |
10634 | if (copy_from_user(new_count, arg, sizeof(new_count))) |
10635 | return -EFAULT; | |
10636 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
10637 | if (new_count[i] > INT_MAX) | |
10638 | return -EINVAL; | |
10639 | ||
fa84693b JA |
10640 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
10641 | sqd = ctx->sq_data; | |
10642 | if (sqd) { | |
009ad9f0 JA |
10643 | /* |
10644 | * Observe the correct sqd->lock -> ctx->uring_lock | |
10645 | * ordering. Fine to drop uring_lock here, we hold | |
10646 | * a ref to the ctx. | |
10647 | */ | |
41d3a6bd | 10648 | refcount_inc(&sqd->refs); |
009ad9f0 | 10649 | mutex_unlock(&ctx->uring_lock); |
fa84693b | 10650 | mutex_lock(&sqd->lock); |
009ad9f0 | 10651 | mutex_lock(&ctx->uring_lock); |
41d3a6bd JA |
10652 | if (sqd->thread) |
10653 | tctx = sqd->thread->io_uring; | |
fa84693b JA |
10654 | } |
10655 | } else { | |
10656 | tctx = current->io_uring; | |
10657 | } | |
10658 | ||
10659 | ret = -EINVAL; | |
10660 | if (!tctx || !tctx->io_wq) | |
10661 | goto err; | |
10662 | ||
2e480058 JA |
10663 | ret = io_wq_max_workers(tctx->io_wq, new_count); |
10664 | if (ret) | |
fa84693b JA |
10665 | goto err; |
10666 | ||
41d3a6bd | 10667 | if (sqd) { |
fa84693b | 10668 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
10669 | io_put_sq_data(sqd); |
10670 | } | |
2e480058 JA |
10671 | |
10672 | if (copy_to_user(arg, new_count, sizeof(new_count))) | |
10673 | return -EFAULT; | |
10674 | ||
10675 | return 0; | |
fa84693b | 10676 | err: |
41d3a6bd | 10677 | if (sqd) { |
fa84693b | 10678 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
10679 | io_put_sq_data(sqd); |
10680 | } | |
fa84693b | 10681 | return ret; |
2e480058 JA |
10682 | } |
10683 | ||
071698e1 JA |
10684 | static bool io_register_op_must_quiesce(int op) |
10685 | { | |
10686 | switch (op) { | |
bd54b6fe BM |
10687 | case IORING_REGISTER_BUFFERS: |
10688 | case IORING_UNREGISTER_BUFFERS: | |
f4f7d21c | 10689 | case IORING_REGISTER_FILES: |
071698e1 JA |
10690 | case IORING_UNREGISTER_FILES: |
10691 | case IORING_REGISTER_FILES_UPDATE: | |
10692 | case IORING_REGISTER_PROBE: | |
10693 | case IORING_REGISTER_PERSONALITY: | |
10694 | case IORING_UNREGISTER_PERSONALITY: | |
992da01a PB |
10695 | case IORING_REGISTER_FILES2: |
10696 | case IORING_REGISTER_FILES_UPDATE2: | |
10697 | case IORING_REGISTER_BUFFERS2: | |
10698 | case IORING_REGISTER_BUFFERS_UPDATE: | |
fe76421d JA |
10699 | case IORING_REGISTER_IOWQ_AFF: |
10700 | case IORING_UNREGISTER_IOWQ_AFF: | |
2e480058 | 10701 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
071698e1 JA |
10702 | return false; |
10703 | default: | |
10704 | return true; | |
10705 | } | |
10706 | } | |
10707 | ||
e73c5c7c PB |
10708 | static int io_ctx_quiesce(struct io_ring_ctx *ctx) |
10709 | { | |
10710 | long ret; | |
10711 | ||
10712 | percpu_ref_kill(&ctx->refs); | |
10713 | ||
10714 | /* | |
10715 | * Drop uring mutex before waiting for references to exit. If another | |
10716 | * thread is currently inside io_uring_enter() it might need to grab the | |
10717 | * uring_lock to make progress. If we hold it here across the drain | |
10718 | * wait, then we can deadlock. It's safe to drop the mutex here, since | |
10719 | * no new references will come in after we've killed the percpu ref. | |
10720 | */ | |
10721 | mutex_unlock(&ctx->uring_lock); | |
10722 | do { | |
10723 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
10724 | if (!ret) | |
10725 | break; | |
10726 | ret = io_run_task_work_sig(); | |
10727 | } while (ret >= 0); | |
10728 | mutex_lock(&ctx->uring_lock); | |
10729 | ||
10730 | if (ret) | |
10731 | io_refs_resurrect(&ctx->refs, &ctx->ref_comp); | |
10732 | return ret; | |
10733 | } | |
10734 | ||
edafccee JA |
10735 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
10736 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
10737 | __releases(ctx->uring_lock) |
10738 | __acquires(ctx->uring_lock) | |
edafccee JA |
10739 | { |
10740 | int ret; | |
10741 | ||
35fa71a0 JA |
10742 | /* |
10743 | * We're inside the ring mutex, if the ref is already dying, then | |
10744 | * someone else killed the ctx or is already going through | |
10745 | * io_uring_register(). | |
10746 | */ | |
10747 | if (percpu_ref_is_dying(&ctx->refs)) | |
10748 | return -ENXIO; | |
10749 | ||
75c4021a PB |
10750 | if (ctx->restricted) { |
10751 | if (opcode >= IORING_REGISTER_LAST) | |
10752 | return -EINVAL; | |
10753 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); | |
10754 | if (!test_bit(opcode, ctx->restrictions.register_op)) | |
10755 | return -EACCES; | |
10756 | } | |
10757 | ||
071698e1 | 10758 | if (io_register_op_must_quiesce(opcode)) { |
e73c5c7c PB |
10759 | ret = io_ctx_quiesce(ctx); |
10760 | if (ret) | |
f70865db | 10761 | return ret; |
05f3fb3c | 10762 | } |
edafccee JA |
10763 | |
10764 | switch (opcode) { | |
10765 | case IORING_REGISTER_BUFFERS: | |
634d00df | 10766 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
edafccee JA |
10767 | break; |
10768 | case IORING_UNREGISTER_BUFFERS: | |
10769 | ret = -EINVAL; | |
10770 | if (arg || nr_args) | |
10771 | break; | |
0a96bbe4 | 10772 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 10773 | break; |
6b06314c | 10774 | case IORING_REGISTER_FILES: |
792e3582 | 10775 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
6b06314c JA |
10776 | break; |
10777 | case IORING_UNREGISTER_FILES: | |
10778 | ret = -EINVAL; | |
10779 | if (arg || nr_args) | |
10780 | break; | |
10781 | ret = io_sqe_files_unregister(ctx); | |
10782 | break; | |
c3a31e60 | 10783 | case IORING_REGISTER_FILES_UPDATE: |
c3bdad02 | 10784 | ret = io_register_files_update(ctx, arg, nr_args); |
c3a31e60 | 10785 | break; |
9b402849 | 10786 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 10787 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
10788 | ret = -EINVAL; |
10789 | if (nr_args != 1) | |
10790 | break; | |
10791 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
10792 | if (ret) |
10793 | break; | |
10794 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
10795 | ctx->eventfd_async = 1; | |
10796 | else | |
10797 | ctx->eventfd_async = 0; | |
9b402849 JA |
10798 | break; |
10799 | case IORING_UNREGISTER_EVENTFD: | |
10800 | ret = -EINVAL; | |
10801 | if (arg || nr_args) | |
10802 | break; | |
10803 | ret = io_eventfd_unregister(ctx); | |
10804 | break; | |
66f4af93 JA |
10805 | case IORING_REGISTER_PROBE: |
10806 | ret = -EINVAL; | |
10807 | if (!arg || nr_args > 256) | |
10808 | break; | |
10809 | ret = io_probe(ctx, arg, nr_args); | |
10810 | break; | |
071698e1 JA |
10811 | case IORING_REGISTER_PERSONALITY: |
10812 | ret = -EINVAL; | |
10813 | if (arg || nr_args) | |
10814 | break; | |
10815 | ret = io_register_personality(ctx); | |
10816 | break; | |
10817 | case IORING_UNREGISTER_PERSONALITY: | |
10818 | ret = -EINVAL; | |
10819 | if (arg) | |
10820 | break; | |
10821 | ret = io_unregister_personality(ctx, nr_args); | |
10822 | break; | |
7e84e1c7 SG |
10823 | case IORING_REGISTER_ENABLE_RINGS: |
10824 | ret = -EINVAL; | |
10825 | if (arg || nr_args) | |
10826 | break; | |
10827 | ret = io_register_enable_rings(ctx); | |
10828 | break; | |
21b55dbc SG |
10829 | case IORING_REGISTER_RESTRICTIONS: |
10830 | ret = io_register_restrictions(ctx, arg, nr_args); | |
10831 | break; | |
992da01a PB |
10832 | case IORING_REGISTER_FILES2: |
10833 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); | |
10834 | break; | |
10835 | case IORING_REGISTER_FILES_UPDATE2: | |
10836 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
10837 | IORING_RSRC_FILE); | |
10838 | break; | |
10839 | case IORING_REGISTER_BUFFERS2: | |
10840 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); | |
792e3582 | 10841 | break; |
992da01a PB |
10842 | case IORING_REGISTER_BUFFERS_UPDATE: |
10843 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
10844 | IORING_RSRC_BUFFER); | |
c3bdad02 | 10845 | break; |
fe76421d JA |
10846 | case IORING_REGISTER_IOWQ_AFF: |
10847 | ret = -EINVAL; | |
10848 | if (!arg || !nr_args) | |
10849 | break; | |
10850 | ret = io_register_iowq_aff(ctx, arg, nr_args); | |
10851 | break; | |
10852 | case IORING_UNREGISTER_IOWQ_AFF: | |
10853 | ret = -EINVAL; | |
10854 | if (arg || nr_args) | |
10855 | break; | |
10856 | ret = io_unregister_iowq_aff(ctx); | |
10857 | break; | |
2e480058 JA |
10858 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
10859 | ret = -EINVAL; | |
10860 | if (!arg || nr_args != 2) | |
10861 | break; | |
10862 | ret = io_register_iowq_max_workers(ctx, arg); | |
10863 | break; | |
edafccee JA |
10864 | default: |
10865 | ret = -EINVAL; | |
10866 | break; | |
10867 | } | |
10868 | ||
071698e1 | 10869 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 10870 | /* bring the ctx back to life */ |
05f3fb3c | 10871 | percpu_ref_reinit(&ctx->refs); |
0f158b4c | 10872 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 10873 | } |
edafccee JA |
10874 | return ret; |
10875 | } | |
10876 | ||
10877 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
10878 | void __user *, arg, unsigned int, nr_args) | |
10879 | { | |
10880 | struct io_ring_ctx *ctx; | |
10881 | long ret = -EBADF; | |
10882 | struct fd f; | |
10883 | ||
10884 | f = fdget(fd); | |
10885 | if (!f.file) | |
10886 | return -EBADF; | |
10887 | ||
10888 | ret = -EOPNOTSUPP; | |
10889 | if (f.file->f_op != &io_uring_fops) | |
10890 | goto out_fput; | |
10891 | ||
10892 | ctx = f.file->private_data; | |
10893 | ||
b6c23dd5 PB |
10894 | io_run_task_work(); |
10895 | ||
edafccee JA |
10896 | mutex_lock(&ctx->uring_lock); |
10897 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
10898 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
10899 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
10900 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
10901 | out_fput: |
10902 | fdput(f); | |
10903 | return ret; | |
10904 | } | |
10905 | ||
2b188cc1 JA |
10906 | static int __init io_uring_init(void) |
10907 | { | |
d7f62e82 SM |
10908 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
10909 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
10910 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
10911 | } while (0) | |
10912 | ||
10913 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
10914 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
10915 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
10916 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
10917 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
10918 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
10919 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
10920 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
10921 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
10922 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 10923 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
10924 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
10925 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
10926 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
10927 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
10928 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
10929 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
10930 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
10931 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
10932 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
10933 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
10934 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
10935 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
10936 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
10937 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
10938 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 10939 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
10940 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
10941 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
16340eab | 10942 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
d7f62e82 | 10943 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
7d67af2c | 10944 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
b9445598 | 10945 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
d7f62e82 | 10946 | |
b0d658ec PB |
10947 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
10948 | sizeof(struct io_uring_rsrc_update)); | |
10949 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > | |
10950 | sizeof(struct io_uring_rsrc_update2)); | |
90499ad0 PB |
10951 | |
10952 | /* ->buf_index is u16 */ | |
10953 | BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); | |
10954 | ||
b0d658ec PB |
10955 | /* should fit into one byte */ |
10956 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); | |
68fe256a PB |
10957 | BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8)); |
10958 | BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS); | |
b0d658ec | 10959 | |
d3656344 | 10960 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
32c2d33e | 10961 | BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int)); |
16340eab | 10962 | |
91f245d5 JA |
10963 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
10964 | SLAB_ACCOUNT); | |
2b188cc1 JA |
10965 | return 0; |
10966 | }; | |
10967 | __initcall(io_uring_init); |