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 | 111 | #define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \ |
d886e185 PB |
112 | REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS | \ |
113 | REQ_F_ASYNC_DATA) | |
b16fed66 | 114 | |
09899b19 PB |
115 | #define IO_TCTX_REFS_CACHE_NR (1U << 10) |
116 | ||
2b188cc1 JA |
117 | struct io_uring { |
118 | u32 head ____cacheline_aligned_in_smp; | |
119 | u32 tail ____cacheline_aligned_in_smp; | |
120 | }; | |
121 | ||
1e84b97b | 122 | /* |
75b28aff HV |
123 | * This data is shared with the application through the mmap at offsets |
124 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
125 | * |
126 | * The offsets to the member fields are published through struct | |
127 | * io_sqring_offsets when calling io_uring_setup. | |
128 | */ | |
75b28aff | 129 | struct io_rings { |
1e84b97b SB |
130 | /* |
131 | * Head and tail offsets into the ring; the offsets need to be | |
132 | * masked to get valid indices. | |
133 | * | |
75b28aff HV |
134 | * The kernel controls head of the sq ring and the tail of the cq ring, |
135 | * and the application controls tail of the sq ring and the head of the | |
136 | * cq ring. | |
1e84b97b | 137 | */ |
75b28aff | 138 | struct io_uring sq, cq; |
1e84b97b | 139 | /* |
75b28aff | 140 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
141 | * ring_entries - 1) |
142 | */ | |
75b28aff HV |
143 | u32 sq_ring_mask, cq_ring_mask; |
144 | /* Ring sizes (constant, power of 2) */ | |
145 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
146 | /* |
147 | * Number of invalid entries dropped by the kernel due to | |
148 | * invalid index stored in array | |
149 | * | |
150 | * Written by the kernel, shouldn't be modified by the | |
151 | * application (i.e. get number of "new events" by comparing to | |
152 | * cached value). | |
153 | * | |
154 | * After a new SQ head value was read by the application this | |
155 | * counter includes all submissions that were dropped reaching | |
156 | * the new SQ head (and possibly more). | |
157 | */ | |
75b28aff | 158 | u32 sq_dropped; |
1e84b97b | 159 | /* |
0d9b5b3a | 160 | * Runtime SQ flags |
1e84b97b SB |
161 | * |
162 | * Written by the kernel, shouldn't be modified by the | |
163 | * application. | |
164 | * | |
165 | * The application needs a full memory barrier before checking | |
166 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
167 | */ | |
75b28aff | 168 | u32 sq_flags; |
0d9b5b3a SG |
169 | /* |
170 | * Runtime CQ flags | |
171 | * | |
172 | * Written by the application, shouldn't be modified by the | |
173 | * kernel. | |
174 | */ | |
fe7e3257 | 175 | u32 cq_flags; |
1e84b97b SB |
176 | /* |
177 | * Number of completion events lost because the queue was full; | |
178 | * this should be avoided by the application by making sure | |
0b4295b5 | 179 | * there are not more requests pending than there is space in |
1e84b97b SB |
180 | * the completion queue. |
181 | * | |
182 | * Written by the kernel, shouldn't be modified by the | |
183 | * application (i.e. get number of "new events" by comparing to | |
184 | * cached value). | |
185 | * | |
186 | * As completion events come in out of order this counter is not | |
187 | * ordered with any other data. | |
188 | */ | |
75b28aff | 189 | u32 cq_overflow; |
1e84b97b SB |
190 | /* |
191 | * Ring buffer of completion events. | |
192 | * | |
193 | * The kernel writes completion events fresh every time they are | |
194 | * produced, so the application is allowed to modify pending | |
195 | * entries. | |
196 | */ | |
75b28aff | 197 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
198 | }; |
199 | ||
45d189c6 | 200 | enum io_uring_cmd_flags { |
51aac424 | 201 | IO_URING_F_COMPLETE_DEFER = 1, |
3b44b371 | 202 | IO_URING_F_UNLOCKED = 2, |
51aac424 PB |
203 | /* int's last bit, sign checks are usually faster than a bit test */ |
204 | IO_URING_F_NONBLOCK = INT_MIN, | |
45d189c6 PB |
205 | }; |
206 | ||
edafccee JA |
207 | struct io_mapped_ubuf { |
208 | u64 ubuf; | |
4751f53d | 209 | u64 ubuf_end; |
edafccee | 210 | unsigned int nr_bvecs; |
de293938 | 211 | unsigned long acct_pages; |
41edf1a5 | 212 | struct bio_vec bvec[]; |
edafccee JA |
213 | }; |
214 | ||
50238531 BM |
215 | struct io_ring_ctx; |
216 | ||
6c2450ae PB |
217 | struct io_overflow_cqe { |
218 | struct io_uring_cqe cqe; | |
219 | struct list_head list; | |
220 | }; | |
221 | ||
a04b0ac0 PB |
222 | struct io_fixed_file { |
223 | /* file * with additional FFS_* flags */ | |
224 | unsigned long file_ptr; | |
225 | }; | |
226 | ||
269bbe5f BM |
227 | struct io_rsrc_put { |
228 | struct list_head list; | |
b60c8dce | 229 | u64 tag; |
50238531 BM |
230 | union { |
231 | void *rsrc; | |
232 | struct file *file; | |
bd54b6fe | 233 | struct io_mapped_ubuf *buf; |
50238531 | 234 | }; |
269bbe5f BM |
235 | }; |
236 | ||
aeca241b | 237 | struct io_file_table { |
042b0d85 | 238 | struct io_fixed_file *files; |
31b51510 JA |
239 | }; |
240 | ||
b895c9a6 | 241 | struct io_rsrc_node { |
05589553 XW |
242 | struct percpu_ref refs; |
243 | struct list_head node; | |
269bbe5f | 244 | struct list_head rsrc_list; |
b895c9a6 | 245 | struct io_rsrc_data *rsrc_data; |
4a38aed2 | 246 | struct llist_node llist; |
e297822b | 247 | bool done; |
05589553 XW |
248 | }; |
249 | ||
40ae0ff7 PB |
250 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
251 | ||
b895c9a6 | 252 | struct io_rsrc_data { |
05f3fb3c JA |
253 | struct io_ring_ctx *ctx; |
254 | ||
2d091d62 PB |
255 | u64 **tags; |
256 | unsigned int nr; | |
40ae0ff7 | 257 | rsrc_put_fn *do_put; |
3e942498 | 258 | atomic_t refs; |
05f3fb3c | 259 | struct completion done; |
8bad28d8 | 260 | bool quiesce; |
05f3fb3c JA |
261 | }; |
262 | ||
5a2e745d JA |
263 | struct io_buffer { |
264 | struct list_head list; | |
265 | __u64 addr; | |
d1f82808 | 266 | __u32 len; |
5a2e745d JA |
267 | __u16 bid; |
268 | }; | |
269 | ||
21b55dbc SG |
270 | struct io_restriction { |
271 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
272 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
273 | u8 sqe_flags_allowed; | |
274 | u8 sqe_flags_required; | |
7e84e1c7 | 275 | bool registered; |
21b55dbc SG |
276 | }; |
277 | ||
37d1e2e3 JA |
278 | enum { |
279 | IO_SQ_THREAD_SHOULD_STOP = 0, | |
280 | IO_SQ_THREAD_SHOULD_PARK, | |
281 | }; | |
282 | ||
534ca6d6 JA |
283 | struct io_sq_data { |
284 | refcount_t refs; | |
9e138a48 | 285 | atomic_t park_pending; |
09a6f4ef | 286 | struct mutex lock; |
69fb2131 JA |
287 | |
288 | /* ctx's that are using this sqd */ | |
289 | struct list_head ctx_list; | |
69fb2131 | 290 | |
534ca6d6 JA |
291 | struct task_struct *thread; |
292 | struct wait_queue_head wait; | |
08369246 XW |
293 | |
294 | unsigned sq_thread_idle; | |
37d1e2e3 JA |
295 | int sq_cpu; |
296 | pid_t task_pid; | |
5c2469e0 | 297 | pid_t task_tgid; |
37d1e2e3 JA |
298 | |
299 | unsigned long state; | |
37d1e2e3 | 300 | struct completion exited; |
534ca6d6 JA |
301 | }; |
302 | ||
6dd0be1e | 303 | #define IO_COMPL_BATCH 32 |
6ff119a6 | 304 | #define IO_REQ_CACHE_SIZE 32 |
bf019da7 | 305 | #define IO_REQ_ALLOC_BATCH 8 |
258b29a9 | 306 | |
a1ab7b35 PB |
307 | struct io_submit_link { |
308 | struct io_kiocb *head; | |
309 | struct io_kiocb *last; | |
310 | }; | |
311 | ||
258b29a9 | 312 | struct io_submit_state { |
5a158c6b PB |
313 | /* inline/task_work completion list, under ->uring_lock */ |
314 | struct io_wq_work_node free_list; | |
315 | /* batch completion logic */ | |
316 | struct io_wq_work_list compl_reqs; | |
a1ab7b35 | 317 | struct io_submit_link link; |
258b29a9 | 318 | |
258b29a9 | 319 | bool plug_started; |
4b628aeb | 320 | bool need_plug; |
5ca7a8b3 | 321 | unsigned short submit_nr; |
5a158c6b | 322 | struct blk_plug plug; |
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; | |
ab409402 | 366 | int rsrc_cached_refs; |
7f1129d2 PB |
367 | struct io_file_table file_table; |
368 | unsigned nr_user_files; | |
369 | unsigned nr_user_bufs; | |
370 | struct io_mapped_ubuf **user_bufs; | |
371 | ||
372 | struct io_submit_state submit_state; | |
5262f567 | 373 | struct list_head timeout_list; |
ef9dd637 | 374 | struct list_head ltimeout_list; |
1d7bb1d5 | 375 | struct list_head cq_overflow_list; |
7f1129d2 PB |
376 | struct xarray io_buffers; |
377 | struct xarray personalities; | |
378 | u32 pers_next; | |
379 | unsigned sq_thread_idle; | |
2b188cc1 JA |
380 | } ____cacheline_aligned_in_smp; |
381 | ||
d0acdee2 | 382 | /* IRQ completion list, under ->completion_lock */ |
c2b6c6bc | 383 | struct io_wq_work_list locked_free_list; |
d0acdee2 | 384 | unsigned int locked_free_nr; |
3c1a2ead | 385 | |
7c30f36a | 386 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
534ca6d6 JA |
387 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
388 | ||
90554200 | 389 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 390 | struct list_head sqd_list; |
75b28aff | 391 | |
5ed7a37d PB |
392 | unsigned long check_cq_overflow; |
393 | ||
206aefde JA |
394 | struct { |
395 | unsigned cached_cq_tail; | |
396 | unsigned cq_entries; | |
0499e582 | 397 | struct eventfd_ctx *cq_ev_fd; |
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 | ||
f499a021 JA |
687 | struct io_async_connect { |
688 | struct sockaddr_storage address; | |
689 | }; | |
690 | ||
03b1230c JA |
691 | struct io_async_msghdr { |
692 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
693 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
694 | struct iovec *free_iov; | |
03b1230c JA |
695 | struct sockaddr __user *uaddr; |
696 | struct msghdr msg; | |
b537916c | 697 | struct sockaddr_storage addr; |
03b1230c JA |
698 | }; |
699 | ||
538941e2 | 700 | struct io_rw_state { |
ff6165b2 | 701 | struct iov_iter iter; |
cd658695 | 702 | struct iov_iter_state iter_state; |
c88598a9 | 703 | struct iovec fast_iov[UIO_FASTIOV]; |
538941e2 PB |
704 | }; |
705 | ||
706 | struct io_async_rw { | |
707 | struct io_rw_state s; | |
708 | const struct iovec *free_iovec; | |
227c0c96 | 709 | size_t bytes_done; |
bcf5a063 | 710 | struct wait_page_queue wpq; |
f67676d1 JA |
711 | }; |
712 | ||
6b47ee6e PB |
713 | enum { |
714 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
715 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
716 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
717 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
718 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 719 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 720 | |
dddca226 | 721 | /* first byte is taken by user flags, shift it to not overlap */ |
93d2bcd2 | 722 | REQ_F_FAIL_BIT = 8, |
6b47ee6e PB |
723 | REQ_F_INFLIGHT_BIT, |
724 | REQ_F_CUR_POS_BIT, | |
725 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 726 | REQ_F_LINK_TIMEOUT_BIT, |
99bc4c38 | 727 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 728 | REQ_F_POLLED_BIT, |
bcda7baa | 729 | REQ_F_BUFFER_SELECTED_BIT, |
e342c807 | 730 | REQ_F_COMPLETE_INLINE_BIT, |
230d50d4 | 731 | REQ_F_REISSUE_BIT, |
b8e64b53 | 732 | REQ_F_CREDS_BIT, |
20e60a38 | 733 | REQ_F_REFCOUNT_BIT, |
4d13d1a4 | 734 | REQ_F_ARM_LTIMEOUT_BIT, |
d886e185 | 735 | REQ_F_ASYNC_DATA_BIT, |
7b29f92d | 736 | /* keep async read/write and isreg together and in order */ |
35645ac3 | 737 | REQ_F_SUPPORT_NOWAIT_BIT, |
7b29f92d | 738 | REQ_F_ISREG_BIT, |
84557871 JA |
739 | |
740 | /* not a real bit, just to check we're not overflowing the space */ | |
741 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
742 | }; |
743 | ||
744 | enum { | |
745 | /* ctx owns file */ | |
746 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
747 | /* drain existing IO first */ | |
748 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
749 | /* linked sqes */ | |
750 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
751 | /* doesn't sever on completion < 0 */ | |
752 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
753 | /* IOSQE_ASYNC */ | |
754 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
755 | /* IOSQE_BUFFER_SELECT */ |
756 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 757 | |
6b47ee6e | 758 | /* fail rest of links */ |
93d2bcd2 | 759 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
b05a1bcd | 760 | /* on inflight list, should be cancelled and waited on exit reliably */ |
6b47ee6e PB |
761 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
762 | /* read/write uses file position */ | |
763 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
764 | /* must not punt to workers */ | |
765 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 766 | /* has or had linked timeout */ |
6b47ee6e | 767 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
99bc4c38 PB |
768 | /* needs cleanup */ |
769 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
770 | /* already went through poll handler */ |
771 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
772 | /* buffer already selected */ |
773 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
e342c807 PB |
774 | /* completion is deferred through io_comp_state */ |
775 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
230d50d4 JA |
776 | /* caller should reissue async */ |
777 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), | |
35645ac3 PB |
778 | /* supports async reads/writes */ |
779 | REQ_F_SUPPORT_NOWAIT = BIT(REQ_F_SUPPORT_NOWAIT_BIT), | |
7b29f92d JA |
780 | /* regular file */ |
781 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
b8e64b53 PB |
782 | /* has creds assigned */ |
783 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), | |
20e60a38 PB |
784 | /* skip refcounting if not set */ |
785 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), | |
4d13d1a4 PB |
786 | /* there is a linked timeout that has to be armed */ |
787 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT), | |
d886e185 PB |
788 | /* ->async_data allocated */ |
789 | REQ_F_ASYNC_DATA = BIT(REQ_F_ASYNC_DATA_BIT), | |
d7718a9d JA |
790 | }; |
791 | ||
792 | struct async_poll { | |
793 | struct io_poll_iocb poll; | |
807abcb0 | 794 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
795 | }; |
796 | ||
f237c30a | 797 | typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked); |
5b0a6acc | 798 | |
7cbf1722 | 799 | struct io_task_work { |
5b0a6acc PB |
800 | union { |
801 | struct io_wq_work_node node; | |
802 | struct llist_node fallback_node; | |
803 | }; | |
804 | io_req_tw_func_t func; | |
7cbf1722 JA |
805 | }; |
806 | ||
992da01a PB |
807 | enum { |
808 | IORING_RSRC_FILE = 0, | |
809 | IORING_RSRC_BUFFER = 1, | |
810 | }; | |
811 | ||
09bb8394 JA |
812 | /* |
813 | * NOTE! Each of the iocb union members has the file pointer | |
814 | * as the first entry in their struct definition. So you can | |
815 | * access the file pointer through any of the sub-structs, | |
816 | * or directly as just 'ki_filp' in this struct. | |
817 | */ | |
2b188cc1 | 818 | struct io_kiocb { |
221c5eb2 | 819 | union { |
09bb8394 | 820 | struct file *file; |
9adbd45d | 821 | struct io_rw rw; |
221c5eb2 | 822 | struct io_poll_iocb poll; |
9d805892 | 823 | struct io_poll_update poll_update; |
8ed8d3c3 JA |
824 | struct io_accept accept; |
825 | struct io_sync sync; | |
fbf23849 | 826 | struct io_cancel cancel; |
b29472ee | 827 | struct io_timeout timeout; |
0bdf7a2d | 828 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 829 | struct io_connect connect; |
e47293fd | 830 | struct io_sr_msg sr_msg; |
15b71abe | 831 | struct io_open open; |
b5dba59e | 832 | struct io_close close; |
269bbe5f | 833 | struct io_rsrc_update rsrc_update; |
4840e418 | 834 | struct io_fadvise fadvise; |
c1ca757b | 835 | struct io_madvise madvise; |
3e4827b0 | 836 | struct io_epoll epoll; |
7d67af2c | 837 | struct io_splice splice; |
ddf0322d | 838 | struct io_provide_buf pbuf; |
1d9e1288 | 839 | struct io_statx statx; |
36f4fa68 | 840 | struct io_shutdown shutdown; |
80a261fd | 841 | struct io_rename rename; |
14a1143b | 842 | struct io_unlink unlink; |
e34a02dc | 843 | struct io_mkdir mkdir; |
7a8721f8 | 844 | struct io_symlink symlink; |
cf30da90 | 845 | struct io_hardlink hardlink; |
221c5eb2 | 846 | }; |
2b188cc1 | 847 | |
d625c6ee | 848 | u8 opcode; |
65a6543d XW |
849 | /* polled IO has completed */ |
850 | u8 iopoll_completed; | |
4f4eeba8 | 851 | u16 buf_index; |
d17e56eb PB |
852 | unsigned int flags; |
853 | ||
854 | u64 user_data; | |
9cf7c104 | 855 | u32 result; |
d17e56eb | 856 | u32 cflags; |
4f4eeba8 | 857 | |
010e8e6b | 858 | struct io_ring_ctx *ctx; |
010e8e6b | 859 | struct task_struct *task; |
d7718a9d | 860 | |
269bbe5f | 861 | struct percpu_ref *fixed_rsrc_refs; |
d886e185 PB |
862 | /* store used ubuf, so we can prevent reloading */ |
863 | struct io_mapped_ubuf *imu; | |
fcb323cc | 864 | |
7e3709d5 | 865 | /* used by request caches, completion batching and iopoll */ |
ef05d9eb | 866 | struct io_wq_work_node comp_list; |
d17e56eb | 867 | atomic_t refs; |
7e3709d5 | 868 | struct io_kiocb *link; |
5b0a6acc | 869 | struct io_task_work io_task_work; |
010e8e6b PB |
870 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
871 | struct hlist_node hash_node; | |
7e3709d5 | 872 | /* internal polling, see IORING_FEAT_FAST_POLL */ |
010e8e6b | 873 | struct async_poll *apoll; |
d886e185 PB |
874 | /* opcode allocated if it needs to store data for async defer */ |
875 | void *async_data; | |
ef05d9eb | 876 | struct io_wq_work work; |
7e3709d5 | 877 | /* custom credentials, valid IFF REQ_F_CREDS is set */ |
ef05d9eb | 878 | const struct cred *creds; |
7e3709d5 | 879 | /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */ |
30d51dd4 | 880 | struct io_buffer *kbuf; |
2b188cc1 | 881 | }; |
05589553 | 882 | |
13bf43f5 PB |
883 | struct io_tctx_node { |
884 | struct list_head ctx_node; | |
885 | struct task_struct *task; | |
13bf43f5 PB |
886 | struct io_ring_ctx *ctx; |
887 | }; | |
888 | ||
27dc8338 PB |
889 | struct io_defer_entry { |
890 | struct list_head list; | |
891 | struct io_kiocb *req; | |
9cf7c104 | 892 | u32 seq; |
2b188cc1 JA |
893 | }; |
894 | ||
d3656344 | 895 | struct io_op_def { |
d3656344 JA |
896 | /* needs req->file assigned */ |
897 | unsigned needs_file : 1; | |
6d63416d PB |
898 | /* should block plug */ |
899 | unsigned plug : 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; | |
8a72758c JA |
904 | /* set if opcode supports polled "wait" */ |
905 | unsigned pollin : 1; | |
906 | unsigned pollout : 1; | |
bcda7baa JA |
907 | /* op supports buffer selection */ |
908 | unsigned buffer_select : 1; | |
26f0505a PB |
909 | /* do prep async if is going to be punted */ |
910 | unsigned needs_async_setup : 1; | |
6d63416d PB |
911 | /* opcode is not supported by this kernel */ |
912 | unsigned not_supported : 1; | |
e8c2bc1f JA |
913 | /* size of async data needed, if any */ |
914 | unsigned short async_size; | |
d3656344 JA |
915 | }; |
916 | ||
0918682b | 917 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
918 | [IORING_OP_NOP] = {}, |
919 | [IORING_OP_READV] = { | |
d3656344 JA |
920 | .needs_file = 1, |
921 | .unbound_nonreg_file = 1, | |
8a72758c | 922 | .pollin = 1, |
4d954c25 | 923 | .buffer_select = 1, |
26f0505a | 924 | .needs_async_setup = 1, |
27926b68 | 925 | .plug = 1, |
e8c2bc1f | 926 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 927 | }, |
0463b6c5 | 928 | [IORING_OP_WRITEV] = { |
d3656344 JA |
929 | .needs_file = 1, |
930 | .hash_reg_file = 1, | |
931 | .unbound_nonreg_file = 1, | |
8a72758c | 932 | .pollout = 1, |
26f0505a | 933 | .needs_async_setup = 1, |
27926b68 | 934 | .plug = 1, |
e8c2bc1f | 935 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 936 | }, |
0463b6c5 | 937 | [IORING_OP_FSYNC] = { |
d3656344 JA |
938 | .needs_file = 1, |
939 | }, | |
0463b6c5 | 940 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
941 | .needs_file = 1, |
942 | .unbound_nonreg_file = 1, | |
8a72758c | 943 | .pollin = 1, |
27926b68 | 944 | .plug = 1, |
e8c2bc1f | 945 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 946 | }, |
0463b6c5 | 947 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
948 | .needs_file = 1, |
949 | .hash_reg_file = 1, | |
950 | .unbound_nonreg_file = 1, | |
8a72758c | 951 | .pollout = 1, |
27926b68 | 952 | .plug = 1, |
e8c2bc1f | 953 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 954 | }, |
0463b6c5 | 955 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
956 | .needs_file = 1, |
957 | .unbound_nonreg_file = 1, | |
958 | }, | |
0463b6c5 PB |
959 | [IORING_OP_POLL_REMOVE] = {}, |
960 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 JA |
961 | .needs_file = 1, |
962 | }, | |
0463b6c5 | 963 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
964 | .needs_file = 1, |
965 | .unbound_nonreg_file = 1, | |
8a72758c | 966 | .pollout = 1, |
26f0505a | 967 | .needs_async_setup = 1, |
e8c2bc1f | 968 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 969 | }, |
0463b6c5 | 970 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
971 | .needs_file = 1, |
972 | .unbound_nonreg_file = 1, | |
8a72758c | 973 | .pollin = 1, |
52de1fe1 | 974 | .buffer_select = 1, |
26f0505a | 975 | .needs_async_setup = 1, |
e8c2bc1f | 976 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 977 | }, |
0463b6c5 | 978 | [IORING_OP_TIMEOUT] = { |
e8c2bc1f | 979 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 980 | }, |
9c8e11b3 PB |
981 | [IORING_OP_TIMEOUT_REMOVE] = { |
982 | /* used by timeout updates' prep() */ | |
9c8e11b3 | 983 | }, |
0463b6c5 | 984 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
985 | .needs_file = 1, |
986 | .unbound_nonreg_file = 1, | |
8a72758c | 987 | .pollin = 1, |
d3656344 | 988 | }, |
0463b6c5 PB |
989 | [IORING_OP_ASYNC_CANCEL] = {}, |
990 | [IORING_OP_LINK_TIMEOUT] = { | |
e8c2bc1f | 991 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 992 | }, |
0463b6c5 | 993 | [IORING_OP_CONNECT] = { |
d3656344 JA |
994 | .needs_file = 1, |
995 | .unbound_nonreg_file = 1, | |
8a72758c | 996 | .pollout = 1, |
26f0505a | 997 | .needs_async_setup = 1, |
e8c2bc1f | 998 | .async_size = sizeof(struct io_async_connect), |
d3656344 | 999 | }, |
0463b6c5 | 1000 | [IORING_OP_FALLOCATE] = { |
d3656344 | 1001 | .needs_file = 1, |
d3656344 | 1002 | }, |
44526bed JA |
1003 | [IORING_OP_OPENAT] = {}, |
1004 | [IORING_OP_CLOSE] = {}, | |
1005 | [IORING_OP_FILES_UPDATE] = {}, | |
1006 | [IORING_OP_STATX] = {}, | |
0463b6c5 | 1007 | [IORING_OP_READ] = { |
3a6820f2 JA |
1008 | .needs_file = 1, |
1009 | .unbound_nonreg_file = 1, | |
8a72758c | 1010 | .pollin = 1, |
bcda7baa | 1011 | .buffer_select = 1, |
27926b68 | 1012 | .plug = 1, |
e8c2bc1f | 1013 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1014 | }, |
0463b6c5 | 1015 | [IORING_OP_WRITE] = { |
3a6820f2 | 1016 | .needs_file = 1, |
7b3188e7 | 1017 | .hash_reg_file = 1, |
3a6820f2 | 1018 | .unbound_nonreg_file = 1, |
8a72758c | 1019 | .pollout = 1, |
27926b68 | 1020 | .plug = 1, |
e8c2bc1f | 1021 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1022 | }, |
0463b6c5 | 1023 | [IORING_OP_FADVISE] = { |
4840e418 | 1024 | .needs_file = 1, |
c1ca757b | 1025 | }, |
44526bed | 1026 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 1027 | [IORING_OP_SEND] = { |
fddaface JA |
1028 | .needs_file = 1, |
1029 | .unbound_nonreg_file = 1, | |
8a72758c | 1030 | .pollout = 1, |
fddaface | 1031 | }, |
0463b6c5 | 1032 | [IORING_OP_RECV] = { |
fddaface JA |
1033 | .needs_file = 1, |
1034 | .unbound_nonreg_file = 1, | |
8a72758c | 1035 | .pollin = 1, |
bcda7baa | 1036 | .buffer_select = 1, |
fddaface | 1037 | }, |
0463b6c5 | 1038 | [IORING_OP_OPENAT2] = { |
cebdb986 | 1039 | }, |
3e4827b0 JA |
1040 | [IORING_OP_EPOLL_CTL] = { |
1041 | .unbound_nonreg_file = 1, | |
3e4827b0 | 1042 | }, |
7d67af2c PB |
1043 | [IORING_OP_SPLICE] = { |
1044 | .needs_file = 1, | |
1045 | .hash_reg_file = 1, | |
1046 | .unbound_nonreg_file = 1, | |
ddf0322d JA |
1047 | }, |
1048 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 1049 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
1050 | [IORING_OP_TEE] = { |
1051 | .needs_file = 1, | |
1052 | .hash_reg_file = 1, | |
1053 | .unbound_nonreg_file = 1, | |
1054 | }, | |
36f4fa68 JA |
1055 | [IORING_OP_SHUTDOWN] = { |
1056 | .needs_file = 1, | |
1057 | }, | |
44526bed JA |
1058 | [IORING_OP_RENAMEAT] = {}, |
1059 | [IORING_OP_UNLINKAT] = {}, | |
e34a02dc | 1060 | [IORING_OP_MKDIRAT] = {}, |
7a8721f8 | 1061 | [IORING_OP_SYMLINKAT] = {}, |
cf30da90 | 1062 | [IORING_OP_LINKAT] = {}, |
d3656344 JA |
1063 | }; |
1064 | ||
0756a869 PB |
1065 | /* requests with any of those set should undergo io_disarm_next() */ |
1066 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) | |
1067 | ||
7a612350 | 1068 | static bool io_disarm_next(struct io_kiocb *req); |
eef51daa | 1069 | static void io_uring_del_tctx_node(unsigned long index); |
9936c7c2 PB |
1070 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1071 | struct task_struct *task, | |
3dd0c97a | 1072 | bool cancel_all); |
78cc687b | 1073 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); |
1ffc5422 | 1074 | |
d4d19c19 | 1075 | static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
54daa9b2 | 1076 | s32 res, u32 cflags); |
ec9c02ad | 1077 | static void io_put_req(struct io_kiocb *req); |
91c2f697 | 1078 | static void io_put_req_deferred(struct io_kiocb *req); |
c7dae4ba | 1079 | static void io_dismantle_req(struct io_kiocb *req); |
94ae5e77 | 1080 | static void io_queue_linked_timeout(struct io_kiocb *req); |
fdecb662 | 1081 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 1082 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 | 1083 | unsigned nr_args); |
68fb8979 | 1084 | static void io_clean_op(struct io_kiocb *req); |
ac177053 | 1085 | static struct file *io_file_get(struct io_ring_ctx *ctx, |
8371adf5 | 1086 | struct io_kiocb *req, int fd, bool fixed); |
c5eef2b9 | 1087 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1088 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1089 | |
907d1df3 | 1090 | static void io_req_task_queue(struct io_kiocb *req); |
c450178d | 1091 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx); |
179ae0d1 | 1092 | static int io_req_prep_async(struct io_kiocb *req); |
de0617e4 | 1093 | |
b9445598 PB |
1094 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
1095 | unsigned int issue_flags, u32 slot_index); | |
7df778be PB |
1096 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags); |
1097 | ||
f1042b6c | 1098 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer); |
b9445598 | 1099 | |
2b188cc1 JA |
1100 | static struct kmem_cache *req_cachep; |
1101 | ||
0918682b | 1102 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1103 | |
1104 | struct sock *io_uring_get_socket(struct file *file) | |
1105 | { | |
1106 | #if defined(CONFIG_UNIX) | |
1107 | if (file->f_op == &io_uring_fops) { | |
1108 | struct io_ring_ctx *ctx = file->private_data; | |
1109 | ||
1110 | return ctx->ring_sock->sk; | |
1111 | } | |
1112 | #endif | |
1113 | return NULL; | |
1114 | } | |
1115 | EXPORT_SYMBOL(io_uring_get_socket); | |
1116 | ||
f237c30a PB |
1117 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
1118 | { | |
1119 | if (!*locked) { | |
1120 | mutex_lock(&ctx->uring_lock); | |
1121 | *locked = true; | |
1122 | } | |
1123 | } | |
1124 | ||
f2f87370 PB |
1125 | #define io_for_each_link(pos, head) \ |
1126 | for (pos = (head); pos; pos = pos->link) | |
1127 | ||
21c843d5 PB |
1128 | /* |
1129 | * Shamelessly stolen from the mm implementation of page reference checking, | |
1130 | * see commit f958d7b528b1 for details. | |
1131 | */ | |
1132 | #define req_ref_zero_or_close_to_overflow(req) \ | |
1133 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) | |
1134 | ||
1135 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) | |
1136 | { | |
20e60a38 | 1137 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1138 | return atomic_inc_not_zero(&req->refs); |
1139 | } | |
1140 | ||
21c843d5 PB |
1141 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
1142 | { | |
20e60a38 PB |
1143 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
1144 | return true; | |
1145 | ||
21c843d5 PB |
1146 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1147 | return atomic_dec_and_test(&req->refs); | |
1148 | } | |
1149 | ||
1150 | static inline void req_ref_put(struct io_kiocb *req) | |
1151 | { | |
20e60a38 | 1152 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1153 | WARN_ON_ONCE(req_ref_put_and_test(req)); |
1154 | } | |
1155 | ||
1156 | static inline void req_ref_get(struct io_kiocb *req) | |
1157 | { | |
20e60a38 | 1158 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1159 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1160 | atomic_inc(&req->refs); | |
1161 | } | |
1162 | ||
c450178d PB |
1163 | static inline void io_submit_flush_completions(struct io_ring_ctx *ctx) |
1164 | { | |
6f33b0bc | 1165 | if (!wq_list_empty(&ctx->submit_state.compl_reqs)) |
c450178d PB |
1166 | __io_submit_flush_completions(ctx); |
1167 | } | |
1168 | ||
48dcd38d | 1169 | static inline void __io_req_set_refcount(struct io_kiocb *req, int nr) |
20e60a38 PB |
1170 | { |
1171 | if (!(req->flags & REQ_F_REFCOUNT)) { | |
1172 | req->flags |= REQ_F_REFCOUNT; | |
48dcd38d | 1173 | atomic_set(&req->refs, nr); |
20e60a38 PB |
1174 | } |
1175 | } | |
1176 | ||
48dcd38d PB |
1177 | static inline void io_req_set_refcount(struct io_kiocb *req) |
1178 | { | |
1179 | __io_req_set_refcount(req, 1); | |
1180 | } | |
1181 | ||
ab409402 PB |
1182 | #define IO_RSRC_REF_BATCH 100 |
1183 | ||
1184 | static inline void io_req_put_rsrc_locked(struct io_kiocb *req, | |
1185 | struct io_ring_ctx *ctx) | |
1186 | __must_hold(&ctx->uring_lock) | |
1187 | { | |
1188 | struct percpu_ref *ref = req->fixed_rsrc_refs; | |
1189 | ||
1190 | if (ref) { | |
1191 | if (ref == &ctx->rsrc_node->refs) | |
1192 | ctx->rsrc_cached_refs++; | |
1193 | else | |
1194 | percpu_ref_put(ref); | |
1195 | } | |
1196 | } | |
1197 | ||
1198 | static inline void io_req_put_rsrc(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
1199 | { | |
1200 | if (req->fixed_rsrc_refs) | |
1201 | percpu_ref_put(req->fixed_rsrc_refs); | |
1202 | } | |
1203 | ||
1204 | static __cold void io_rsrc_refs_drop(struct io_ring_ctx *ctx) | |
1205 | __must_hold(&ctx->uring_lock) | |
1206 | { | |
1207 | if (ctx->rsrc_cached_refs) { | |
1208 | percpu_ref_put_many(&ctx->rsrc_node->refs, ctx->rsrc_cached_refs); | |
1209 | ctx->rsrc_cached_refs = 0; | |
1210 | } | |
1211 | } | |
1212 | ||
1213 | static void io_rsrc_refs_refill(struct io_ring_ctx *ctx) | |
1214 | __must_hold(&ctx->uring_lock) | |
1215 | { | |
1216 | ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH; | |
1217 | percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH); | |
1218 | } | |
1219 | ||
a46be971 PB |
1220 | static inline void io_req_set_rsrc_node(struct io_kiocb *req, |
1221 | struct io_ring_ctx *ctx) | |
36f72fe2 | 1222 | { |
269bbe5f | 1223 | if (!req->fixed_rsrc_refs) { |
a7f0ed5a | 1224 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
ab409402 PB |
1225 | ctx->rsrc_cached_refs--; |
1226 | if (unlikely(ctx->rsrc_cached_refs < 0)) | |
1227 | io_rsrc_refs_refill(ctx); | |
36f72fe2 PB |
1228 | } |
1229 | } | |
1230 | ||
f70865db PB |
1231 | static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
1232 | { | |
1233 | bool got = percpu_ref_tryget(ref); | |
1234 | ||
1235 | /* already at zero, wait for ->release() */ | |
1236 | if (!got) | |
1237 | wait_for_completion(compl); | |
1238 | percpu_ref_resurrect(ref); | |
1239 | if (got) | |
1240 | percpu_ref_put(ref); | |
1241 | } | |
1242 | ||
3dd0c97a PB |
1243 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
1244 | bool cancel_all) | |
08d23634 PB |
1245 | { |
1246 | struct io_kiocb *req; | |
1247 | ||
68207680 | 1248 | if (task && head->task != task) |
08d23634 | 1249 | return false; |
3dd0c97a | 1250 | if (cancel_all) |
08d23634 PB |
1251 | return true; |
1252 | ||
1253 | io_for_each_link(req, head) { | |
b05a1bcd | 1254 | if (req->flags & REQ_F_INFLIGHT) |
02a13674 | 1255 | return true; |
08d23634 PB |
1256 | } |
1257 | return false; | |
1258 | } | |
1259 | ||
d886e185 PB |
1260 | static inline bool req_has_async_data(struct io_kiocb *req) |
1261 | { | |
1262 | return req->flags & REQ_F_ASYNC_DATA; | |
1263 | } | |
1264 | ||
93d2bcd2 | 1265 | static inline void req_set_fail(struct io_kiocb *req) |
c40f6379 | 1266 | { |
93d2bcd2 | 1267 | req->flags |= REQ_F_FAIL; |
c40f6379 | 1268 | } |
4a38aed2 | 1269 | |
a8295b98 HX |
1270 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
1271 | { | |
1272 | req_set_fail(req); | |
1273 | req->result = res; | |
1274 | } | |
1275 | ||
c072481d | 1276 | static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref) |
2b188cc1 JA |
1277 | { |
1278 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1279 | ||
0f158b4c | 1280 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1281 | } |
1282 | ||
8eb7e2d0 PB |
1283 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1284 | { | |
1285 | return !req->timeout.off; | |
1286 | } | |
1287 | ||
c072481d | 1288 | static __cold void io_fallback_req_func(struct work_struct *work) |
f56165e6 PB |
1289 | { |
1290 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, | |
1291 | fallback_work.work); | |
1292 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); | |
1293 | struct io_kiocb *req, *tmp; | |
f237c30a | 1294 | bool locked = false; |
f56165e6 PB |
1295 | |
1296 | percpu_ref_get(&ctx->refs); | |
1297 | llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node) | |
f237c30a | 1298 | req->io_task_work.func(req, &locked); |
5636c00d | 1299 | |
f237c30a | 1300 | if (locked) { |
c450178d | 1301 | io_submit_flush_completions(ctx); |
f237c30a PB |
1302 | mutex_unlock(&ctx->uring_lock); |
1303 | } | |
f56165e6 PB |
1304 | percpu_ref_put(&ctx->refs); |
1305 | } | |
1306 | ||
c072481d | 1307 | static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
2b188cc1 JA |
1308 | { |
1309 | struct io_ring_ctx *ctx; | |
78076bb6 | 1310 | int hash_bits; |
2b188cc1 JA |
1311 | |
1312 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1313 | if (!ctx) | |
1314 | return NULL; | |
1315 | ||
78076bb6 JA |
1316 | /* |
1317 | * Use 5 bits less than the max cq entries, that should give us around | |
1318 | * 32 entries per hash list if totally full and uniformly spread. | |
1319 | */ | |
1320 | hash_bits = ilog2(p->cq_entries); | |
1321 | hash_bits -= 5; | |
1322 | if (hash_bits <= 0) | |
1323 | hash_bits = 1; | |
1324 | ctx->cancel_hash_bits = hash_bits; | |
1325 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1326 | GFP_KERNEL); | |
1327 | if (!ctx->cancel_hash) | |
1328 | goto err; | |
1329 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1330 | ||
6224843d PB |
1331 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
1332 | if (!ctx->dummy_ubuf) | |
1333 | goto err; | |
1334 | /* set invalid range, so io_import_fixed() fails meeting it */ | |
1335 | ctx->dummy_ubuf->ubuf = -1UL; | |
1336 | ||
21482896 | 1337 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1338 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1339 | goto err; | |
2b188cc1 JA |
1340 | |
1341 | ctx->flags = p->flags; | |
90554200 | 1342 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1343 | INIT_LIST_HEAD(&ctx->sqd_list); |
1d7bb1d5 | 1344 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c | 1345 | init_completion(&ctx->ref_comp); |
9e15c3a0 | 1346 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
61cf9370 | 1347 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
2b188cc1 | 1348 | mutex_init(&ctx->uring_lock); |
311997b3 | 1349 | init_waitqueue_head(&ctx->cq_wait); |
2b188cc1 | 1350 | spin_lock_init(&ctx->completion_lock); |
89850fce | 1351 | spin_lock_init(&ctx->timeout_lock); |
5eef4e87 | 1352 | INIT_WQ_LIST(&ctx->iopoll_list); |
de0617e4 | 1353 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1354 | INIT_LIST_HEAD(&ctx->timeout_list); |
ef9dd637 | 1355 | INIT_LIST_HEAD(&ctx->ltimeout_list); |
d67d2263 BM |
1356 | spin_lock_init(&ctx->rsrc_ref_lock); |
1357 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1358 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1359 | init_llist_head(&ctx->rsrc_put_llist); | |
13bf43f5 | 1360 | INIT_LIST_HEAD(&ctx->tctx_list); |
c2b6c6bc PB |
1361 | ctx->submit_state.free_list.next = NULL; |
1362 | INIT_WQ_LIST(&ctx->locked_free_list); | |
9011bf9a | 1363 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
6f33b0bc | 1364 | INIT_WQ_LIST(&ctx->submit_state.compl_reqs); |
2b188cc1 | 1365 | return ctx; |
206aefde | 1366 | err: |
6224843d | 1367 | kfree(ctx->dummy_ubuf); |
78076bb6 | 1368 | kfree(ctx->cancel_hash); |
206aefde JA |
1369 | kfree(ctx); |
1370 | return NULL; | |
2b188cc1 JA |
1371 | } |
1372 | ||
8f6ed49a PB |
1373 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
1374 | { | |
1375 | struct io_rings *r = ctx->rings; | |
1376 | ||
1377 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); | |
1378 | ctx->cq_extra--; | |
1379 | } | |
1380 | ||
9cf7c104 | 1381 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1382 | { |
2bc9930e JA |
1383 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1384 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1385 | |
8f6ed49a | 1386 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
2bc9930e | 1387 | } |
de0617e4 | 1388 | |
9d858b21 | 1389 | return false; |
de0617e4 JA |
1390 | } |
1391 | ||
35645ac3 PB |
1392 | #define FFS_NOWAIT 0x1UL |
1393 | #define FFS_ISREG 0x2UL | |
1394 | #define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG) | |
c97d8a0f PB |
1395 | |
1396 | static inline bool io_req_ffs_set(struct io_kiocb *req) | |
1397 | { | |
35645ac3 | 1398 | return req->flags & REQ_F_FIXED_FILE; |
c97d8a0f PB |
1399 | } |
1400 | ||
c072481d | 1401 | static inline void io_req_track_inflight(struct io_kiocb *req) |
ce3d5aae | 1402 | { |
ce3d5aae | 1403 | if (!(req->flags & REQ_F_INFLIGHT)) { |
ce3d5aae | 1404 | req->flags |= REQ_F_INFLIGHT; |
b303fe2e | 1405 | atomic_inc(¤t->io_uring->inflight_tracked); |
ce3d5aae PB |
1406 | } |
1407 | } | |
1408 | ||
906c6caa PB |
1409 | static inline void io_unprep_linked_timeout(struct io_kiocb *req) |
1410 | { | |
1411 | req->flags &= ~REQ_F_LINK_TIMEOUT; | |
1412 | } | |
1413 | ||
fd08e530 PB |
1414 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
1415 | { | |
906c6caa PB |
1416 | if (WARN_ON_ONCE(!req->link)) |
1417 | return NULL; | |
1418 | ||
4d13d1a4 PB |
1419 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
1420 | req->flags |= REQ_F_LINK_TIMEOUT; | |
fd08e530 PB |
1421 | |
1422 | /* linked timeouts should have two refs once prep'ed */ | |
48dcd38d | 1423 | io_req_set_refcount(req); |
4d13d1a4 PB |
1424 | __io_req_set_refcount(req->link, 2); |
1425 | return req->link; | |
fd08e530 PB |
1426 | } |
1427 | ||
1428 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) | |
1429 | { | |
4d13d1a4 | 1430 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
fd08e530 PB |
1431 | return NULL; |
1432 | return __io_prep_linked_timeout(req); | |
1433 | } | |
1434 | ||
1e6fa521 JA |
1435 | static void io_prep_async_work(struct io_kiocb *req) |
1436 | { | |
1437 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1438 | struct io_ring_ctx *ctx = req->ctx; |
1439 | ||
b8e64b53 PB |
1440 | if (!(req->flags & REQ_F_CREDS)) { |
1441 | req->flags |= REQ_F_CREDS; | |
c10d1f98 | 1442 | req->creds = get_current_cred(); |
b8e64b53 | 1443 | } |
003e8dcc | 1444 | |
e1d675df PB |
1445 | req->work.list.next = NULL; |
1446 | req->work.flags = 0; | |
feaadc4f PB |
1447 | if (req->flags & REQ_F_FORCE_ASYNC) |
1448 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1449 | ||
1e6fa521 JA |
1450 | if (req->flags & REQ_F_ISREG) { |
1451 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1452 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
4b982bd0 | 1453 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
1e6fa521 JA |
1454 | if (def->unbound_nonreg_file) |
1455 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1456 | } | |
e1d675df PB |
1457 | |
1458 | switch (req->opcode) { | |
1459 | case IORING_OP_SPLICE: | |
1460 | case IORING_OP_TEE: | |
e1d675df PB |
1461 | if (!S_ISREG(file_inode(req->splice.file_in)->i_mode)) |
1462 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1463 | break; | |
1464 | } | |
561fb04a | 1465 | } |
cccf0ee8 | 1466 | |
cbdcb435 | 1467 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1468 | { |
cbdcb435 | 1469 | struct io_kiocb *cur; |
54a91f3b | 1470 | |
44eff40a PB |
1471 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
1472 | struct io_ring_ctx *ctx = req->ctx; | |
1473 | ||
79ebeaee | 1474 | spin_lock(&ctx->completion_lock); |
44eff40a PB |
1475 | io_for_each_link(cur, req) |
1476 | io_prep_async_work(cur); | |
79ebeaee | 1477 | spin_unlock(&ctx->completion_lock); |
44eff40a PB |
1478 | } else { |
1479 | io_for_each_link(cur, req) | |
1480 | io_prep_async_work(cur); | |
1481 | } | |
561fb04a JA |
1482 | } |
1483 | ||
fff4e40e PB |
1484 | static inline void io_req_add_compl_list(struct io_kiocb *req) |
1485 | { | |
1486 | struct io_submit_state *state = &req->ctx->submit_state; | |
1487 | ||
1488 | wq_list_add_tail(&req->comp_list, &state->compl_reqs); | |
1489 | } | |
1490 | ||
00169246 | 1491 | static void io_queue_async_work(struct io_kiocb *req, bool *dont_use) |
561fb04a | 1492 | { |
a197f664 | 1493 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1494 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1495 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1496 | |
3bfe6106 JA |
1497 | BUG_ON(!tctx); |
1498 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1499 | |
cbdcb435 PB |
1500 | /* init ->work of the whole link before punting */ |
1501 | io_prep_async_link(req); | |
991468dc JA |
1502 | |
1503 | /* | |
1504 | * Not expected to happen, but if we do have a bug where this _can_ | |
1505 | * happen, catch it here and ensure the request is marked as | |
1506 | * canceled. That will make io-wq go through the usual work cancel | |
1507 | * procedure rather than attempt to run this request (or create a new | |
1508 | * worker for it). | |
1509 | */ | |
1510 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) | |
1511 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1512 | ||
d07f1e8a PB |
1513 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1514 | &req->work, req->flags); | |
ebf93667 | 1515 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a JA |
1516 | if (link) |
1517 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1518 | } |
1519 | ||
1ee4160c | 1520 | static void io_kill_timeout(struct io_kiocb *req, int status) |
8c855885 | 1521 | __must_hold(&req->ctx->completion_lock) |
89850fce | 1522 | __must_hold(&req->ctx->timeout_lock) |
5262f567 | 1523 | { |
e8c2bc1f | 1524 | struct io_timeout_data *io = req->async_data; |
5262f567 | 1525 | |
fd9c7bc5 | 1526 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
2ae2eb9d PB |
1527 | if (status) |
1528 | req_set_fail(req); | |
01cec8c1 PB |
1529 | atomic_set(&req->ctx->cq_timeouts, |
1530 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1531 | list_del_init(&req->timeout.list); |
d4d19c19 | 1532 | io_cqring_fill_event(req->ctx, req->user_data, status, 0); |
91c2f697 | 1533 | io_put_req_deferred(req); |
5262f567 JA |
1534 | } |
1535 | } | |
1536 | ||
c072481d | 1537 | static __cold void io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1538 | { |
441b8a78 | 1539 | while (!list_empty(&ctx->defer_list)) { |
27dc8338 PB |
1540 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1541 | struct io_defer_entry, list); | |
de0617e4 | 1542 | |
9cf7c104 | 1543 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1544 | break; |
27dc8338 | 1545 | list_del_init(&de->list); |
907d1df3 | 1546 | io_req_task_queue(de->req); |
27dc8338 | 1547 | kfree(de); |
441b8a78 | 1548 | } |
04518945 PB |
1549 | } |
1550 | ||
c072481d | 1551 | static __cold void io_flush_timeouts(struct io_ring_ctx *ctx) |
89850fce | 1552 | __must_hold(&ctx->completion_lock) |
de0617e4 | 1553 | { |
441b8a78 | 1554 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
f010505b | 1555 | |
79ebeaee | 1556 | spin_lock_irq(&ctx->timeout_lock); |
f18ee4cf | 1557 | while (!list_empty(&ctx->timeout_list)) { |
f010505b | 1558 | u32 events_needed, events_got; |
360428f8 | 1559 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1560 | struct io_kiocb, timeout.list); |
de0617e4 | 1561 | |
8eb7e2d0 | 1562 | if (io_is_timeout_noseq(req)) |
360428f8 | 1563 | break; |
f010505b MDG |
1564 | |
1565 | /* | |
1566 | * Since seq can easily wrap around over time, subtract | |
1567 | * the last seq at which timeouts were flushed before comparing. | |
1568 | * Assuming not more than 2^31-1 events have happened since, | |
1569 | * these subtractions won't have wrapped, so we can check if | |
1570 | * target is in [last_seq, current_seq] by comparing the two. | |
1571 | */ | |
1572 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1573 | events_got = seq - ctx->cq_last_tm_flush; | |
1574 | if (events_got < events_needed) | |
360428f8 | 1575 | break; |
bfe68a22 | 1576 | |
135fcde8 | 1577 | list_del_init(&req->timeout.list); |
1ee4160c | 1578 | io_kill_timeout(req, 0); |
f18ee4cf | 1579 | } |
f010505b | 1580 | ctx->cq_last_tm_flush = seq; |
79ebeaee | 1581 | spin_unlock_irq(&ctx->timeout_lock); |
360428f8 | 1582 | } |
5262f567 | 1583 | |
c072481d | 1584 | static __cold void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
360428f8 | 1585 | { |
2335f6f5 PB |
1586 | if (ctx->off_timeout_used) |
1587 | io_flush_timeouts(ctx); | |
1588 | if (ctx->drain_active) | |
1589 | io_queue_deferred(ctx); | |
1590 | } | |
1591 | ||
1592 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) | |
1593 | { | |
1594 | if (unlikely(ctx->off_timeout_used || ctx->drain_active)) | |
1595 | __io_commit_cqring_flush(ctx); | |
ec30e04b PB |
1596 | /* order cqe stores with ring update */ |
1597 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 JA |
1598 | } |
1599 | ||
90554200 JA |
1600 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1601 | { | |
1602 | struct io_rings *r = ctx->rings; | |
1603 | ||
a566c556 | 1604 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries; |
90554200 JA |
1605 | } |
1606 | ||
888aae2e PB |
1607 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1608 | { | |
1609 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1610 | } | |
1611 | ||
d068b506 | 1612 | static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) |
2b188cc1 | 1613 | { |
75b28aff | 1614 | struct io_rings *rings = ctx->rings; |
ea5ab3b5 | 1615 | unsigned tail, mask = ctx->cq_entries - 1; |
2b188cc1 | 1616 | |
115e12e5 SB |
1617 | /* |
1618 | * writes to the cq entry need to come after reading head; the | |
1619 | * control dependency is enough as we're using WRITE_ONCE to | |
1620 | * fill the cq entry | |
1621 | */ | |
a566c556 | 1622 | if (__io_cqring_events(ctx) == ctx->cq_entries) |
2b188cc1 JA |
1623 | return NULL; |
1624 | ||
888aae2e | 1625 | tail = ctx->cached_cq_tail++; |
ea5ab3b5 | 1626 | return &rings->cqes[tail & mask]; |
2b188cc1 JA |
1627 | } |
1628 | ||
f2842ab5 JA |
1629 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1630 | { | |
44c769de | 1631 | if (likely(!ctx->cq_ev_fd)) |
f0b493e6 | 1632 | return false; |
7e55a19c SG |
1633 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1634 | return false; | |
44c769de | 1635 | return !ctx->eventfd_async || io_wq_current_is_worker(); |
f2842ab5 JA |
1636 | } |
1637 | ||
2c5d763c JA |
1638 | /* |
1639 | * This should only get called when at least one event has been posted. | |
1640 | * Some applications rely on the eventfd notification count only changing | |
1641 | * IFF a new CQE has been added to the CQ ring. There's no depedency on | |
1642 | * 1:1 relationship between how many times this function is called (and | |
1643 | * hence the eventfd count) and number of CQEs posted to the CQ ring. | |
1644 | */ | |
b41e9852 | 1645 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1646 | { |
5fd46178 JA |
1647 | /* |
1648 | * wake_up_all() may seem excessive, but io_wake_function() and | |
1649 | * io_should_wake() handle the termination of the loop and only | |
1650 | * wake as many waiters as we need to. | |
1651 | */ | |
1652 | if (wq_has_sleeper(&ctx->cq_wait)) | |
1653 | wake_up_all(&ctx->cq_wait); | |
b41e9852 | 1654 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 JA |
1655 | eventfd_signal(ctx->cq_ev_fd, 1); |
1656 | } | |
1657 | ||
80c18e4a PB |
1658 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1659 | { | |
c57a91fb PB |
1660 | /* see waitqueue_active() comment */ |
1661 | smp_mb(); | |
1662 | ||
80c18e4a | 1663 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
c57a91fb | 1664 | if (waitqueue_active(&ctx->cq_wait)) |
5fd46178 | 1665 | wake_up_all(&ctx->cq_wait); |
80c18e4a PB |
1666 | } |
1667 | if (io_should_trigger_evfd(ctx)) | |
1668 | eventfd_signal(ctx->cq_ev_fd, 1); | |
1669 | } | |
1670 | ||
c4a2ed72 | 1671 | /* Returns true if there are no backlogged entries after the flush */ |
6c2450ae | 1672 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
1d7bb1d5 | 1673 | { |
b18032bb | 1674 | bool all_flushed, posted; |
1d7bb1d5 | 1675 | |
a566c556 | 1676 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
e23de15f | 1677 | return false; |
1d7bb1d5 | 1678 | |
b18032bb | 1679 | posted = false; |
79ebeaee | 1680 | spin_lock(&ctx->completion_lock); |
6c2450ae | 1681 | while (!list_empty(&ctx->cq_overflow_list)) { |
d068b506 | 1682 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
6c2450ae | 1683 | struct io_overflow_cqe *ocqe; |
e6c8aa9a | 1684 | |
1d7bb1d5 JA |
1685 | if (!cqe && !force) |
1686 | break; | |
6c2450ae PB |
1687 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
1688 | struct io_overflow_cqe, list); | |
1689 | if (cqe) | |
1690 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); | |
1691 | else | |
8f6ed49a PB |
1692 | io_account_cq_overflow(ctx); |
1693 | ||
b18032bb | 1694 | posted = true; |
6c2450ae PB |
1695 | list_del(&ocqe->list); |
1696 | kfree(ocqe); | |
1d7bb1d5 JA |
1697 | } |
1698 | ||
09e88404 PB |
1699 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1700 | if (all_flushed) { | |
5ed7a37d | 1701 | clear_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1702 | WRITE_ONCE(ctx->rings->sq_flags, |
1703 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); | |
09e88404 | 1704 | } |
46930143 | 1705 | |
b18032bb JA |
1706 | if (posted) |
1707 | io_commit_cqring(ctx); | |
79ebeaee | 1708 | spin_unlock(&ctx->completion_lock); |
b18032bb JA |
1709 | if (posted) |
1710 | io_cqring_ev_posted(ctx); | |
09e88404 | 1711 | return all_flushed; |
1d7bb1d5 JA |
1712 | } |
1713 | ||
90f67366 | 1714 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
6c503150 | 1715 | { |
ca0a2651 JA |
1716 | bool ret = true; |
1717 | ||
5ed7a37d | 1718 | if (test_bit(0, &ctx->check_cq_overflow)) { |
6c503150 PB |
1719 | /* iopoll syncs against uring_lock, not completion_lock */ |
1720 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1721 | mutex_lock(&ctx->uring_lock); | |
90f67366 | 1722 | ret = __io_cqring_overflow_flush(ctx, false); |
6c503150 PB |
1723 | if (ctx->flags & IORING_SETUP_IOPOLL) |
1724 | mutex_unlock(&ctx->uring_lock); | |
1725 | } | |
ca0a2651 JA |
1726 | |
1727 | return ret; | |
6c503150 PB |
1728 | } |
1729 | ||
6a290a14 PB |
1730 | /* must to be called somewhat shortly after putting a request */ |
1731 | static inline void io_put_task(struct task_struct *task, int nr) | |
1732 | { | |
1733 | struct io_uring_task *tctx = task->io_uring; | |
1734 | ||
e98e49b2 PB |
1735 | if (likely(task == current)) { |
1736 | tctx->cached_refs += nr; | |
1737 | } else { | |
1738 | percpu_counter_sub(&tctx->inflight, nr); | |
1739 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1740 | wake_up(&tctx->wait); | |
1741 | put_task_struct_many(task, nr); | |
1742 | } | |
6a290a14 PB |
1743 | } |
1744 | ||
9a10867a PB |
1745 | static void io_task_refs_refill(struct io_uring_task *tctx) |
1746 | { | |
1747 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; | |
1748 | ||
1749 | percpu_counter_add(&tctx->inflight, refill); | |
1750 | refcount_add(refill, ¤t->usage); | |
1751 | tctx->cached_refs += refill; | |
1752 | } | |
1753 | ||
1754 | static inline void io_get_task_refs(int nr) | |
1755 | { | |
1756 | struct io_uring_task *tctx = current->io_uring; | |
1757 | ||
1758 | tctx->cached_refs -= nr; | |
1759 | if (unlikely(tctx->cached_refs < 0)) | |
1760 | io_task_refs_refill(tctx); | |
1761 | } | |
1762 | ||
d4d19c19 | 1763 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
54daa9b2 | 1764 | s32 res, u32 cflags) |
2b188cc1 | 1765 | { |
cce4b8b0 | 1766 | struct io_overflow_cqe *ocqe; |
2b188cc1 | 1767 | |
cce4b8b0 PB |
1768 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
1769 | if (!ocqe) { | |
1770 | /* | |
1771 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1772 | * or cannot allocate an overflow entry, then we need to drop it | |
1773 | * on the floor. | |
1774 | */ | |
8f6ed49a | 1775 | io_account_cq_overflow(ctx); |
cce4b8b0 | 1776 | return false; |
2b188cc1 | 1777 | } |
cce4b8b0 | 1778 | if (list_empty(&ctx->cq_overflow_list)) { |
5ed7a37d | 1779 | set_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1780 | WRITE_ONCE(ctx->rings->sq_flags, |
1781 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); | |
1782 | ||
cce4b8b0 | 1783 | } |
d4d19c19 | 1784 | ocqe->cqe.user_data = user_data; |
cce4b8b0 PB |
1785 | ocqe->cqe.res = res; |
1786 | ocqe->cqe.flags = cflags; | |
1787 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); | |
1788 | return true; | |
2b188cc1 JA |
1789 | } |
1790 | ||
d4d19c19 | 1791 | static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
54daa9b2 | 1792 | s32 res, u32 cflags) |
2b188cc1 JA |
1793 | { |
1794 | struct io_uring_cqe *cqe; | |
1795 | ||
d4d19c19 | 1796 | trace_io_uring_complete(ctx, user_data, res, cflags); |
51c3ff62 | 1797 | |
2b188cc1 JA |
1798 | /* |
1799 | * If we can't get a cq entry, userspace overflowed the | |
1800 | * submission (by quite a lot). Increment the overflow count in | |
1801 | * the ring. | |
1802 | */ | |
d068b506 | 1803 | cqe = io_get_cqe(ctx); |
1d7bb1d5 | 1804 | if (likely(cqe)) { |
d4d19c19 | 1805 | WRITE_ONCE(cqe->user_data, user_data); |
2b188cc1 | 1806 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1807 | WRITE_ONCE(cqe->flags, cflags); |
8d13326e | 1808 | return true; |
2b188cc1 | 1809 | } |
d4d19c19 | 1810 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
2b188cc1 JA |
1811 | } |
1812 | ||
8d13326e | 1813 | /* not as hot to bloat with inlining */ |
d4d19c19 | 1814 | static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
54daa9b2 | 1815 | s32 res, u32 cflags) |
bcda7baa | 1816 | { |
d4d19c19 | 1817 | return __io_cqring_fill_event(ctx, user_data, res, cflags); |
bcda7baa JA |
1818 | } |
1819 | ||
54daa9b2 PB |
1820 | static void io_req_complete_post(struct io_kiocb *req, s32 res, |
1821 | u32 cflags) | |
2b188cc1 | 1822 | { |
78e19bbe | 1823 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 | 1824 | |
79ebeaee | 1825 | spin_lock(&ctx->completion_lock); |
d4d19c19 | 1826 | __io_cqring_fill_event(ctx, req->user_data, res, cflags); |
c7dae4ba JA |
1827 | /* |
1828 | * If we're the last reference to this request, add to our locked | |
1829 | * free_list cache. | |
1830 | */ | |
de9b4cca | 1831 | if (req_ref_put_and_test(req)) { |
7a612350 | 1832 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
0756a869 | 1833 | if (req->flags & IO_DISARM_MASK) |
7a612350 PB |
1834 | io_disarm_next(req); |
1835 | if (req->link) { | |
1836 | io_req_task_queue(req->link); | |
1837 | req->link = NULL; | |
1838 | } | |
1839 | } | |
ab409402 | 1840 | io_req_put_rsrc(req, ctx); |
c7dae4ba JA |
1841 | io_dismantle_req(req); |
1842 | io_put_task(req->task, 1); | |
c2b6c6bc | 1843 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
d0acdee2 | 1844 | ctx->locked_free_nr++; |
180f829f | 1845 | } |
7a612350 | 1846 | io_commit_cqring(ctx); |
79ebeaee | 1847 | spin_unlock(&ctx->completion_lock); |
a3f34907 | 1848 | io_cqring_ev_posted(ctx); |
229a7b63 JA |
1849 | } |
1850 | ||
54daa9b2 PB |
1851 | static inline void io_req_complete_state(struct io_kiocb *req, s32 res, |
1852 | u32 cflags) | |
229a7b63 | 1853 | { |
a38d68db | 1854 | req->result = res; |
d17e56eb | 1855 | req->cflags = cflags; |
e342c807 | 1856 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
1857 | } |
1858 | ||
889fca73 | 1859 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
54daa9b2 | 1860 | s32 res, u32 cflags) |
bcda7baa | 1861 | { |
889fca73 PB |
1862 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
1863 | io_req_complete_state(req, res, cflags); | |
a38d68db | 1864 | else |
c7dae4ba | 1865 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
1866 | } |
1867 | ||
54daa9b2 | 1868 | static inline void io_req_complete(struct io_kiocb *req, s32 res) |
0ddf92e8 | 1869 | { |
889fca73 | 1870 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
1871 | } |
1872 | ||
54daa9b2 | 1873 | static void io_req_complete_failed(struct io_kiocb *req, s32 res) |
f41db273 | 1874 | { |
93d2bcd2 | 1875 | req_set_fail(req); |
f41db273 PB |
1876 | io_req_complete_post(req, res, 0); |
1877 | } | |
1878 | ||
c6d3d9cb PB |
1879 | static void io_req_complete_fail_submit(struct io_kiocb *req) |
1880 | { | |
1881 | /* | |
1882 | * We don't submit, fail them all, for that replace hardlinks with | |
1883 | * normal links. Extra REQ_F_LINK is tolerated. | |
1884 | */ | |
1885 | req->flags &= ~REQ_F_HARDLINK; | |
1886 | req->flags |= REQ_F_LINK; | |
1887 | io_req_complete_failed(req, req->result); | |
1888 | } | |
1889 | ||
864ea921 PB |
1890 | /* |
1891 | * Don't initialise the fields below on every allocation, but do that in | |
1892 | * advance and keep them valid across allocations. | |
1893 | */ | |
1894 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
1895 | { | |
1896 | req->ctx = ctx; | |
1897 | req->link = NULL; | |
1898 | req->async_data = NULL; | |
1899 | /* not necessary, but safer to zero */ | |
1900 | req->result = 0; | |
1901 | } | |
1902 | ||
dac7a098 | 1903 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
cd0ca2e0 | 1904 | struct io_submit_state *state) |
dac7a098 | 1905 | { |
79ebeaee | 1906 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 1907 | wq_list_splice(&ctx->locked_free_list, &state->free_list); |
d0acdee2 | 1908 | ctx->locked_free_nr = 0; |
79ebeaee | 1909 | spin_unlock(&ctx->completion_lock); |
dac7a098 PB |
1910 | } |
1911 | ||
dd78f492 | 1912 | /* Returns true IFF there are requests in the cache */ |
c7dae4ba | 1913 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 1914 | { |
c7dae4ba | 1915 | struct io_submit_state *state = &ctx->submit_state; |
0ddf92e8 | 1916 | |
c7dae4ba JA |
1917 | /* |
1918 | * If we have more than a batch's worth of requests in our IRQ side | |
1919 | * locked cache, grab the lock and move them over to our submission | |
1920 | * side cache. | |
1921 | */ | |
d0acdee2 | 1922 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
cd0ca2e0 | 1923 | io_flush_cached_locked_reqs(ctx, state); |
c2b6c6bc | 1924 | return !!state->free_list.next; |
0ddf92e8 JA |
1925 | } |
1926 | ||
5d5901a3 PB |
1927 | /* |
1928 | * A request might get retired back into the request caches even before opcode | |
1929 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. | |
1930 | * Because of that, io_alloc_req() should be called only under ->uring_lock | |
1931 | * and with extra caution to not get a request that is still worked on. | |
1932 | */ | |
c072481d | 1933 | static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx) |
5d5901a3 | 1934 | __must_hold(&ctx->uring_lock) |
2b188cc1 | 1935 | { |
e5d1bc0a | 1936 | struct io_submit_state *state = &ctx->submit_state; |
864ea921 | 1937 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
3ab665b7 PB |
1938 | void *reqs[IO_REQ_ALLOC_BATCH]; |
1939 | struct io_kiocb *req; | |
864ea921 | 1940 | int ret, i; |
e5d1bc0a | 1941 | |
c2b6c6bc | 1942 | if (likely(state->free_list.next || io_flush_cached_reqs(ctx))) |
a33ae9ce | 1943 | return true; |
e5d1bc0a | 1944 | |
3ab665b7 | 1945 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs); |
fd6fab2c | 1946 | |
864ea921 PB |
1947 | /* |
1948 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1949 | * retry single alloc to be on the safe side. | |
1950 | */ | |
1951 | if (unlikely(ret <= 0)) { | |
3ab665b7 PB |
1952 | reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
1953 | if (!reqs[0]) | |
a33ae9ce | 1954 | return false; |
864ea921 | 1955 | ret = 1; |
2b188cc1 | 1956 | } |
864ea921 | 1957 | |
37f0e767 | 1958 | percpu_ref_get_many(&ctx->refs, ret); |
3ab665b7 PB |
1959 | for (i = 0; i < ret; i++) { |
1960 | req = reqs[i]; | |
1961 | ||
1962 | io_preinit_req(req, ctx); | |
c2b6c6bc | 1963 | wq_stack_add_head(&req->comp_list, &state->free_list); |
3ab665b7 | 1964 | } |
a33ae9ce PB |
1965 | return true; |
1966 | } | |
1967 | ||
1968 | static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx) | |
1969 | { | |
1970 | if (unlikely(!ctx->submit_state.free_list.next)) | |
1971 | return __io_alloc_req_refill(ctx); | |
1972 | return true; | |
1973 | } | |
1974 | ||
1975 | static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) | |
1976 | { | |
1977 | struct io_wq_work_node *node; | |
1978 | ||
1979 | node = wq_stack_extract(&ctx->submit_state.free_list); | |
c2b6c6bc | 1980 | return container_of(node, struct io_kiocb, comp_list); |
2b188cc1 JA |
1981 | } |
1982 | ||
e1d767f0 | 1983 | static inline void io_put_file(struct file *file) |
8da11c19 | 1984 | { |
e1d767f0 | 1985 | if (file) |
8da11c19 PB |
1986 | fput(file); |
1987 | } | |
1988 | ||
6b639522 | 1989 | static inline void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 1990 | { |
094bae49 | 1991 | unsigned int flags = req->flags; |
929a3af9 | 1992 | |
867f8fa5 | 1993 | if (unlikely(flags & IO_REQ_CLEAN_FLAGS)) |
3a0a6902 | 1994 | io_clean_op(req); |
e1d767f0 PB |
1995 | if (!(flags & REQ_F_FIXED_FILE)) |
1996 | io_put_file(req->file); | |
e65ef56d JA |
1997 | } |
1998 | ||
c072481d | 1999 | static __cold void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 2000 | { |
51a4cc11 | 2001 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 2002 | |
ab409402 | 2003 | io_req_put_rsrc(req, ctx); |
216578e5 | 2004 | io_dismantle_req(req); |
7c660731 | 2005 | io_put_task(req->task, 1); |
c6ca97b3 | 2006 | |
79ebeaee | 2007 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 2008 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
c34b025f | 2009 | ctx->locked_free_nr++; |
79ebeaee | 2010 | spin_unlock(&ctx->completion_lock); |
e65ef56d JA |
2011 | } |
2012 | ||
f2f87370 PB |
2013 | static inline void io_remove_next_linked(struct io_kiocb *req) |
2014 | { | |
2015 | struct io_kiocb *nxt = req->link; | |
2016 | ||
2017 | req->link = nxt->link; | |
2018 | nxt->link = NULL; | |
2019 | } | |
2020 | ||
33cc89a9 PB |
2021 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
2022 | __must_hold(&req->ctx->completion_lock) | |
89b263f6 | 2023 | __must_hold(&req->ctx->timeout_lock) |
2665abfd | 2024 | { |
33cc89a9 | 2025 | struct io_kiocb *link = req->link; |
f2f87370 | 2026 | |
b97e736a | 2027 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
c9abd7ad | 2028 | struct io_timeout_data *io = link->async_data; |
7c86ffee | 2029 | |
f2f87370 | 2030 | io_remove_next_linked(req); |
90cd7e42 | 2031 | link->timeout.head = NULL; |
fd9c7bc5 | 2032 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
ef9dd637 | 2033 | list_del(&link->timeout.list); |
d4d19c19 PB |
2034 | io_cqring_fill_event(link->ctx, link->user_data, |
2035 | -ECANCELED, 0); | |
91c2f697 | 2036 | io_put_req_deferred(link); |
d4729fbd | 2037 | return true; |
c9abd7ad PB |
2038 | } |
2039 | } | |
d4729fbd | 2040 | return false; |
7c86ffee PB |
2041 | } |
2042 | ||
d148ca4b | 2043 | static void io_fail_links(struct io_kiocb *req) |
33cc89a9 | 2044 | __must_hold(&req->ctx->completion_lock) |
9e645e11 | 2045 | { |
33cc89a9 | 2046 | struct io_kiocb *nxt, *link = req->link; |
9e645e11 | 2047 | |
f2f87370 | 2048 | req->link = NULL; |
f2f87370 | 2049 | while (link) { |
a8295b98 HX |
2050 | long res = -ECANCELED; |
2051 | ||
2052 | if (link->flags & REQ_F_FAIL) | |
2053 | res = link->result; | |
2054 | ||
f2f87370 PB |
2055 | nxt = link->link; |
2056 | link->link = NULL; | |
2665abfd | 2057 | |
f2f87370 | 2058 | trace_io_uring_fail_link(req, link); |
a8295b98 | 2059 | io_cqring_fill_event(link->ctx, link->user_data, res, 0); |
91c2f697 | 2060 | io_put_req_deferred(link); |
f2f87370 | 2061 | link = nxt; |
9e645e11 | 2062 | } |
33cc89a9 | 2063 | } |
9e645e11 | 2064 | |
33cc89a9 PB |
2065 | static bool io_disarm_next(struct io_kiocb *req) |
2066 | __must_hold(&req->ctx->completion_lock) | |
2067 | { | |
2068 | bool posted = false; | |
2069 | ||
0756a869 PB |
2070 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
2071 | struct io_kiocb *link = req->link; | |
2072 | ||
906c6caa | 2073 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
0756a869 PB |
2074 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
2075 | io_remove_next_linked(req); | |
2076 | io_cqring_fill_event(link->ctx, link->user_data, | |
2077 | -ECANCELED, 0); | |
2078 | io_put_req_deferred(link); | |
2079 | posted = true; | |
2080 | } | |
2081 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { | |
89b263f6 JA |
2082 | struct io_ring_ctx *ctx = req->ctx; |
2083 | ||
2084 | spin_lock_irq(&ctx->timeout_lock); | |
33cc89a9 | 2085 | posted = io_kill_linked_timeout(req); |
89b263f6 JA |
2086 | spin_unlock_irq(&ctx->timeout_lock); |
2087 | } | |
93d2bcd2 | 2088 | if (unlikely((req->flags & REQ_F_FAIL) && |
e4335ed3 | 2089 | !(req->flags & REQ_F_HARDLINK))) { |
33cc89a9 PB |
2090 | posted |= (req->link != NULL); |
2091 | io_fail_links(req); | |
2092 | } | |
2093 | return posted; | |
9e645e11 JA |
2094 | } |
2095 | ||
d81499bf PB |
2096 | static void __io_req_find_next_prep(struct io_kiocb *req) |
2097 | { | |
2098 | struct io_ring_ctx *ctx = req->ctx; | |
2099 | bool posted; | |
2100 | ||
2101 | spin_lock(&ctx->completion_lock); | |
2102 | posted = io_disarm_next(req); | |
2103 | if (posted) | |
2104 | io_commit_cqring(req->ctx); | |
2105 | spin_unlock(&ctx->completion_lock); | |
2106 | if (posted) | |
2107 | io_cqring_ev_posted(ctx); | |
2108 | } | |
2109 | ||
2110 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) | |
c69f8dbe | 2111 | { |
33cc89a9 | 2112 | struct io_kiocb *nxt; |
944e58bf | 2113 | |
d81499bf PB |
2114 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
2115 | return NULL; | |
9e645e11 JA |
2116 | /* |
2117 | * If LINK is set, we have dependent requests in this chain. If we | |
2118 | * didn't fail this request, queue the first one up, moving any other | |
2119 | * dependencies to the next request. In case of failure, fail the rest | |
2120 | * of the chain. | |
2121 | */ | |
d81499bf PB |
2122 | if (unlikely(req->flags & IO_DISARM_MASK)) |
2123 | __io_req_find_next_prep(req); | |
33cc89a9 PB |
2124 | nxt = req->link; |
2125 | req->link = NULL; | |
2126 | return nxt; | |
4d7dd462 | 2127 | } |
9e645e11 | 2128 | |
f237c30a | 2129 | static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked) |
2c32395d PB |
2130 | { |
2131 | if (!ctx) | |
2132 | return; | |
f237c30a | 2133 | if (*locked) { |
c450178d | 2134 | io_submit_flush_completions(ctx); |
2c32395d | 2135 | mutex_unlock(&ctx->uring_lock); |
f237c30a | 2136 | *locked = false; |
2c32395d PB |
2137 | } |
2138 | percpu_ref_put(&ctx->refs); | |
2139 | } | |
2140 | ||
7cbf1722 | 2141 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 2142 | { |
f237c30a | 2143 | bool locked = false; |
ebd0df2e | 2144 | struct io_ring_ctx *ctx = NULL; |
3f18407d PB |
2145 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
2146 | task_work); | |
c40f6379 | 2147 | |
16f72070 | 2148 | while (1) { |
3f18407d PB |
2149 | struct io_wq_work_node *node; |
2150 | ||
c450178d | 2151 | if (!tctx->task_list.first && locked) |
8d4ad41e PB |
2152 | io_submit_flush_completions(ctx); |
2153 | ||
3f18407d | 2154 | spin_lock_irq(&tctx->task_lock); |
c6538be9 | 2155 | node = tctx->task_list.first; |
3f18407d | 2156 | INIT_WQ_LIST(&tctx->task_list); |
6294f368 PB |
2157 | if (!node) |
2158 | tctx->task_running = false; | |
3f18407d | 2159 | spin_unlock_irq(&tctx->task_lock); |
6294f368 PB |
2160 | if (!node) |
2161 | break; | |
3f18407d | 2162 | |
6294f368 | 2163 | do { |
3f18407d PB |
2164 | struct io_wq_work_node *next = node->next; |
2165 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2166 | io_task_work.node); | |
2167 | ||
2168 | if (req->ctx != ctx) { | |
f237c30a | 2169 | ctx_flush_and_put(ctx, &locked); |
3f18407d | 2170 | ctx = req->ctx; |
126180b9 PB |
2171 | /* if not contended, grab and improve batching */ |
2172 | locked = mutex_trylock(&ctx->uring_lock); | |
3f18407d PB |
2173 | percpu_ref_get(&ctx->refs); |
2174 | } | |
f237c30a | 2175 | req->io_task_work.func(req, &locked); |
3f18407d | 2176 | node = next; |
6294f368 PB |
2177 | } while (node); |
2178 | ||
7cbf1722 | 2179 | cond_resched(); |
3f18407d | 2180 | } |
ebd0df2e | 2181 | |
f237c30a | 2182 | ctx_flush_and_put(ctx, &locked); |
7cbf1722 JA |
2183 | } |
2184 | ||
e09ee510 | 2185 | static void io_req_task_work_add(struct io_kiocb *req) |
7cbf1722 | 2186 | { |
c15b79de | 2187 | struct task_struct *tsk = req->task; |
7cbf1722 | 2188 | struct io_uring_task *tctx = tsk->io_uring; |
c15b79de | 2189 | enum task_work_notify_mode notify; |
e09ee510 | 2190 | struct io_wq_work_node *node; |
0b81e80c | 2191 | unsigned long flags; |
6294f368 | 2192 | bool running; |
7cbf1722 JA |
2193 | |
2194 | WARN_ON_ONCE(!tctx); | |
2195 | ||
0b81e80c | 2196 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 | 2197 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
6294f368 PB |
2198 | running = tctx->task_running; |
2199 | if (!running) | |
2200 | tctx->task_running = true; | |
0b81e80c | 2201 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2202 | |
2203 | /* task_work already pending, we're done */ | |
6294f368 | 2204 | if (running) |
e09ee510 | 2205 | return; |
7cbf1722 | 2206 | |
c15b79de PB |
2207 | /* |
2208 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For | |
2209 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2210 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2211 | * will do the job. | |
2212 | */ | |
2213 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; | |
d97ec623 PB |
2214 | if (likely(!task_work_add(tsk, &tctx->task_work, notify))) { |
2215 | if (notify == TWA_NONE) | |
2216 | wake_up_process(tsk); | |
e09ee510 | 2217 | return; |
c15b79de | 2218 | } |
2215bed9 | 2219 | |
0b81e80c | 2220 | spin_lock_irqsave(&tctx->task_lock, flags); |
6294f368 | 2221 | tctx->task_running = false; |
e09ee510 PB |
2222 | node = tctx->task_list.first; |
2223 | INIT_WQ_LIST(&tctx->task_list); | |
0b81e80c | 2224 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 | 2225 | |
e09ee510 PB |
2226 | while (node) { |
2227 | req = container_of(node, struct io_kiocb, io_task_work.node); | |
2228 | node = node->next; | |
2229 | if (llist_add(&req->io_task_work.fallback_node, | |
2230 | &req->ctx->fallback_llist)) | |
2231 | schedule_delayed_work(&req->ctx->fallback_work, 1); | |
2232 | } | |
eab30c4d PB |
2233 | } |
2234 | ||
f237c30a | 2235 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
c40f6379 | 2236 | { |
87ceb6a6 | 2237 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2238 | |
b18a1a45 | 2239 | /* not needed for normal modes, but SQPOLL depends on it */ |
f237c30a | 2240 | io_tw_lock(ctx, locked); |
2593553a | 2241 | io_req_complete_failed(req, req->result); |
c40f6379 JA |
2242 | } |
2243 | ||
f237c30a | 2244 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
c40f6379 JA |
2245 | { |
2246 | struct io_ring_ctx *ctx = req->ctx; | |
2247 | ||
f237c30a | 2248 | io_tw_lock(ctx, locked); |
316319e8 | 2249 | /* req->task == current here, checking PF_EXITING is safe */ |
af066f31 | 2250 | if (likely(!(req->task->flags & PF_EXITING))) |
c5eef2b9 | 2251 | __io_queue_sqe(req); |
81b6d05c | 2252 | else |
2593553a | 2253 | io_req_complete_failed(req, -EFAULT); |
c40f6379 JA |
2254 | } |
2255 | ||
2c4b8eb6 | 2256 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
c40f6379 | 2257 | { |
2c4b8eb6 | 2258 | req->result = ret; |
5b0a6acc | 2259 | req->io_task_work.func = io_req_task_cancel; |
e09ee510 | 2260 | io_req_task_work_add(req); |
c40f6379 JA |
2261 | } |
2262 | ||
2c4b8eb6 | 2263 | static void io_req_task_queue(struct io_kiocb *req) |
a3df7698 | 2264 | { |
5b0a6acc | 2265 | req->io_task_work.func = io_req_task_submit; |
e09ee510 | 2266 | io_req_task_work_add(req); |
a3df7698 PB |
2267 | } |
2268 | ||
773af691 JA |
2269 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
2270 | { | |
2271 | req->io_task_work.func = io_queue_async_work; | |
2272 | io_req_task_work_add(req); | |
2273 | } | |
2274 | ||
f2f87370 | 2275 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2276 | { |
9b5f7bd9 | 2277 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2278 | |
2279 | if (nxt) | |
906a8c3f | 2280 | io_req_task_queue(nxt); |
c69f8dbe JL |
2281 | } |
2282 | ||
c3524383 | 2283 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2284 | { |
c3524383 PB |
2285 | io_queue_next(req); |
2286 | __io_free_req(req); | |
2287 | } | |
8766dd51 | 2288 | |
f237c30a PB |
2289 | static void io_free_req_work(struct io_kiocb *req, bool *locked) |
2290 | { | |
2291 | io_free_req(req); | |
2292 | } | |
2293 | ||
3aa83bfb | 2294 | static void io_free_batch_list(struct io_ring_ctx *ctx, |
1cce17ac | 2295 | struct io_wq_work_node *node) |
3aa83bfb PB |
2296 | __must_hold(&ctx->uring_lock) |
2297 | { | |
d4b7a5ef | 2298 | struct task_struct *task = NULL; |
37f0e767 | 2299 | int task_refs = 0; |
3aa83bfb | 2300 | |
3aa83bfb PB |
2301 | do { |
2302 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2303 | comp_list); | |
2304 | ||
def77acf | 2305 | if (unlikely(req->flags & REQ_F_REFCOUNT)) { |
c1e53a69 | 2306 | node = req->comp_list.next; |
def77acf PB |
2307 | if (!req_ref_put_and_test(req)) |
2308 | continue; | |
c1e53a69 | 2309 | } |
d4b7a5ef | 2310 | |
ab409402 | 2311 | io_req_put_rsrc_locked(req, ctx); |
d4b7a5ef PB |
2312 | io_queue_next(req); |
2313 | io_dismantle_req(req); | |
2314 | ||
2315 | if (req->task != task) { | |
2316 | if (task) | |
2317 | io_put_task(task, task_refs); | |
2318 | task = req->task; | |
2319 | task_refs = 0; | |
2320 | } | |
2321 | task_refs++; | |
c1e53a69 | 2322 | node = req->comp_list.next; |
d4b7a5ef | 2323 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
3aa83bfb | 2324 | } while (node); |
d4b7a5ef | 2325 | |
d4b7a5ef PB |
2326 | if (task) |
2327 | io_put_task(task, task_refs); | |
3aa83bfb PB |
2328 | } |
2329 | ||
c450178d | 2330 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx) |
a141dd89 | 2331 | __must_hold(&ctx->uring_lock) |
905c172f | 2332 | { |
6f33b0bc | 2333 | struct io_wq_work_node *node, *prev; |
cd0ca2e0 | 2334 | struct io_submit_state *state = &ctx->submit_state; |
905c172f | 2335 | |
79ebeaee | 2336 | spin_lock(&ctx->completion_lock); |
6f33b0bc PB |
2337 | wq_list_for_each(node, prev, &state->compl_reqs) { |
2338 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2339 | comp_list); | |
5182ed2e | 2340 | |
d4d19c19 | 2341 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
d17e56eb | 2342 | req->cflags); |
905c172f PB |
2343 | } |
2344 | io_commit_cqring(ctx); | |
79ebeaee | 2345 | spin_unlock(&ctx->completion_lock); |
905c172f | 2346 | io_cqring_ev_posted(ctx); |
5182ed2e | 2347 | |
1cce17ac | 2348 | io_free_batch_list(ctx, state->compl_reqs.first); |
6f33b0bc | 2349 | INIT_WQ_LIST(&state->compl_reqs); |
7a743e22 PB |
2350 | } |
2351 | ||
ba816ad6 JA |
2352 | /* |
2353 | * Drop reference to request, return next in chain (if there is one) if this | |
2354 | * was the last reference to this request. | |
2355 | */ | |
0d85035a | 2356 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2357 | { |
9b5f7bd9 PB |
2358 | struct io_kiocb *nxt = NULL; |
2359 | ||
de9b4cca | 2360 | if (req_ref_put_and_test(req)) { |
9b5f7bd9 | 2361 | nxt = io_req_find_next(req); |
4d7dd462 | 2362 | __io_free_req(req); |
2a44f467 | 2363 | } |
9b5f7bd9 | 2364 | return nxt; |
2b188cc1 JA |
2365 | } |
2366 | ||
0d85035a | 2367 | static inline void io_put_req(struct io_kiocb *req) |
e65ef56d | 2368 | { |
de9b4cca | 2369 | if (req_ref_put_and_test(req)) |
e65ef56d | 2370 | io_free_req(req); |
2b188cc1 JA |
2371 | } |
2372 | ||
91c2f697 | 2373 | static inline void io_put_req_deferred(struct io_kiocb *req) |
216578e5 | 2374 | { |
91c2f697 | 2375 | if (req_ref_put_and_test(req)) { |
f237c30a | 2376 | req->io_task_work.func = io_free_req_work; |
543af3a1 PB |
2377 | io_req_task_work_add(req); |
2378 | } | |
216578e5 PB |
2379 | } |
2380 | ||
6c503150 | 2381 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2382 | { |
2383 | /* See comment at the top of this file */ | |
2384 | smp_rmb(); | |
e23de15f | 2385 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2386 | } |
2387 | ||
fb5ccc98 PB |
2388 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2389 | { | |
2390 | struct io_rings *rings = ctx->rings; | |
2391 | ||
2392 | /* make sure SQ entry isn't read before tail */ | |
2393 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2394 | } | |
2395 | ||
8ff069bf | 2396 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2397 | { |
8ff069bf | 2398 | unsigned int cflags; |
e94f141b | 2399 | |
bcda7baa JA |
2400 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2401 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2402 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2403 | kfree(kbuf); |
2404 | return cflags; | |
e94f141b JA |
2405 | } |
2406 | ||
8ff069bf | 2407 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2408 | { |
ae421d93 PB |
2409 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
2410 | return 0; | |
30d51dd4 | 2411 | return io_put_kbuf(req, req->kbuf); |
8ff069bf PB |
2412 | } |
2413 | ||
4c6e277c JA |
2414 | static inline bool io_run_task_work(void) |
2415 | { | |
ef98eb04 | 2416 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) { |
4c6e277c | 2417 | __set_current_state(TASK_RUNNING); |
ef98eb04 | 2418 | tracehook_notify_signal(); |
4c6e277c JA |
2419 | return true; |
2420 | } | |
2421 | ||
2422 | return false; | |
bcda7baa JA |
2423 | } |
2424 | ||
5ba3c874 | 2425 | static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) |
def596e9 | 2426 | { |
5eef4e87 | 2427 | struct io_wq_work_node *pos, *start, *prev; |
d729cf9a | 2428 | unsigned int poll_flags = BLK_POLL_NOSLEEP; |
b688f11e | 2429 | DEFINE_IO_COMP_BATCH(iob); |
5ba3c874 | 2430 | int nr_events = 0; |
def596e9 JA |
2431 | |
2432 | /* | |
2433 | * Only spin for completions if we don't have multiple devices hanging | |
87a115fb | 2434 | * off our complete list. |
def596e9 | 2435 | */ |
87a115fb | 2436 | if (ctx->poll_multi_queue || force_nonspin) |
ef99b2d3 | 2437 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2438 | |
5eef4e87 PB |
2439 | wq_list_for_each(pos, start, &ctx->iopoll_list) { |
2440 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
9adbd45d | 2441 | struct kiocb *kiocb = &req->rw.kiocb; |
a2416e1e | 2442 | int ret; |
def596e9 JA |
2443 | |
2444 | /* | |
581f9810 BM |
2445 | * Move completed and retryable entries to our local lists. |
2446 | * If we find a request that requires polling, break out | |
2447 | * and complete those lists first, if we have entries there. | |
def596e9 | 2448 | */ |
e3f721e6 | 2449 | if (READ_ONCE(req->iopoll_completed)) |
def596e9 JA |
2450 | break; |
2451 | ||
b688f11e | 2452 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags); |
a2416e1e PB |
2453 | if (unlikely(ret < 0)) |
2454 | return ret; | |
2455 | else if (ret) | |
ef99b2d3 | 2456 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2457 | |
3aadc23e | 2458 | /* iopoll may have completed current req */ |
b688f11e | 2459 | if (!rq_list_empty(iob.req_list) || |
e3f721e6 PB |
2460 | READ_ONCE(req->iopoll_completed)) |
2461 | break; | |
def596e9 JA |
2462 | } |
2463 | ||
b688f11e JA |
2464 | if (!rq_list_empty(iob.req_list)) |
2465 | iob.complete(&iob); | |
5eef4e87 PB |
2466 | else if (!pos) |
2467 | return 0; | |
2468 | ||
2469 | prev = start; | |
2470 | wq_list_for_each_resume(pos, prev) { | |
2471 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
2472 | ||
b3fa03fd PB |
2473 | /* order with io_complete_rw_iopoll(), e.g. ->result updates */ |
2474 | if (!smp_load_acquire(&req->iopoll_completed)) | |
e3f721e6 | 2475 | break; |
b3fa03fd | 2476 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
f5ed3bcd | 2477 | io_put_rw_kbuf(req)); |
e3f721e6 PB |
2478 | nr_events++; |
2479 | } | |
def596e9 | 2480 | |
f5ed3bcd PB |
2481 | if (unlikely(!nr_events)) |
2482 | return 0; | |
2483 | ||
2484 | io_commit_cqring(ctx); | |
2485 | io_cqring_ev_posted_iopoll(ctx); | |
1cce17ac | 2486 | pos = start ? start->next : ctx->iopoll_list.first; |
5eef4e87 | 2487 | wq_list_cut(&ctx->iopoll_list, prev, start); |
1cce17ac | 2488 | io_free_batch_list(ctx, pos); |
5ba3c874 | 2489 | return nr_events; |
def596e9 JA |
2490 | } |
2491 | ||
def596e9 JA |
2492 | /* |
2493 | * We can't just wait for polled events to come to us, we have to actively | |
2494 | * find and complete them. | |
2495 | */ | |
c072481d | 2496 | static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2497 | { |
2498 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2499 | return; | |
2500 | ||
2501 | mutex_lock(&ctx->uring_lock); | |
5eef4e87 | 2502 | while (!wq_list_empty(&ctx->iopoll_list)) { |
b2edc0a7 | 2503 | /* let it sleep and repeat later if can't complete a request */ |
5ba3c874 | 2504 | if (io_do_iopoll(ctx, true) == 0) |
b2edc0a7 | 2505 | break; |
08f5439f JA |
2506 | /* |
2507 | * Ensure we allow local-to-the-cpu processing to take place, | |
2508 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2509 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2510 | */ |
3fcee5a6 PB |
2511 | if (need_resched()) { |
2512 | mutex_unlock(&ctx->uring_lock); | |
2513 | cond_resched(); | |
2514 | mutex_lock(&ctx->uring_lock); | |
2515 | } | |
def596e9 JA |
2516 | } |
2517 | mutex_unlock(&ctx->uring_lock); | |
2518 | } | |
2519 | ||
7668b92a | 2520 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2521 | { |
7668b92a | 2522 | unsigned int nr_events = 0; |
e9979b36 | 2523 | int ret = 0; |
500f9fba | 2524 | |
c7849be9 XW |
2525 | /* |
2526 | * We disallow the app entering submit/complete with polling, but we | |
2527 | * still need to lock the ring to prevent racing with polled issue | |
2528 | * that got punted to a workqueue. | |
2529 | */ | |
2530 | mutex_lock(&ctx->uring_lock); | |
f39c8a5b PB |
2531 | /* |
2532 | * Don't enter poll loop if we already have events pending. | |
2533 | * If we do, we can potentially be spinning for commands that | |
2534 | * already triggered a CQE (eg in error). | |
2535 | */ | |
5ed7a37d | 2536 | if (test_bit(0, &ctx->check_cq_overflow)) |
f39c8a5b PB |
2537 | __io_cqring_overflow_flush(ctx, false); |
2538 | if (io_cqring_events(ctx)) | |
2539 | goto out; | |
def596e9 | 2540 | do { |
500f9fba JA |
2541 | /* |
2542 | * If a submit got punted to a workqueue, we can have the | |
2543 | * application entering polling for a command before it gets | |
2544 | * issued. That app will hold the uring_lock for the duration | |
2545 | * of the poll right here, so we need to take a breather every | |
2546 | * now and then to ensure that the issue has a chance to add | |
2547 | * the poll to the issued list. Otherwise we can spin here | |
2548 | * forever, while the workqueue is stuck trying to acquire the | |
2549 | * very same mutex. | |
2550 | */ | |
5eef4e87 | 2551 | if (wq_list_empty(&ctx->iopoll_list)) { |
8f487ef2 PB |
2552 | u32 tail = ctx->cached_cq_tail; |
2553 | ||
500f9fba | 2554 | mutex_unlock(&ctx->uring_lock); |
4c6e277c | 2555 | io_run_task_work(); |
500f9fba | 2556 | mutex_lock(&ctx->uring_lock); |
def596e9 | 2557 | |
8f487ef2 PB |
2558 | /* some requests don't go through iopoll_list */ |
2559 | if (tail != ctx->cached_cq_tail || | |
5eef4e87 | 2560 | wq_list_empty(&ctx->iopoll_list)) |
e9979b36 | 2561 | break; |
500f9fba | 2562 | } |
5ba3c874 PB |
2563 | ret = io_do_iopoll(ctx, !min); |
2564 | if (ret < 0) | |
2565 | break; | |
2566 | nr_events += ret; | |
2567 | ret = 0; | |
2568 | } while (nr_events < min && !need_resched()); | |
f39c8a5b | 2569 | out: |
500f9fba | 2570 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2571 | return ret; |
2572 | } | |
2573 | ||
491381ce | 2574 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2575 | { |
491381ce JA |
2576 | /* |
2577 | * Tell lockdep we inherited freeze protection from submission | |
2578 | * thread. | |
2579 | */ | |
2580 | if (req->flags & REQ_F_ISREG) { | |
1c98679d | 2581 | struct super_block *sb = file_inode(req->file)->i_sb; |
2b188cc1 | 2582 | |
1c98679d PB |
2583 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
2584 | sb_end_write(sb); | |
2b188cc1 JA |
2585 | } |
2586 | } | |
2587 | ||
b63534c4 | 2588 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2589 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 | 2590 | { |
ab454438 | 2591 | struct io_async_rw *rw = req->async_data; |
b63534c4 | 2592 | |
d886e185 | 2593 | if (!req_has_async_data(req)) |
ab454438 | 2594 | return !io_req_prep_async(req); |
538941e2 | 2595 | iov_iter_restore(&rw->s.iter, &rw->s.iter_state); |
ab454438 | 2596 | return true; |
b63534c4 | 2597 | } |
b63534c4 | 2598 | |
3e6a0d3c | 2599 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 2600 | { |
355afaeb | 2601 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 2602 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 2603 | |
355afaeb JA |
2604 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2605 | return false; | |
3e6a0d3c JA |
2606 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
2607 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 2608 | return false; |
7c977a58 JA |
2609 | /* |
2610 | * If ref is dying, we might be running poll reap from the exit work. | |
2611 | * Don't attempt to reissue from that path, just let it fail with | |
2612 | * -EAGAIN. | |
2613 | */ | |
3e6a0d3c JA |
2614 | if (percpu_ref_is_dying(&ctx->refs)) |
2615 | return false; | |
ef046888 JA |
2616 | /* |
2617 | * Play it safe and assume not safe to re-import and reissue if we're | |
2618 | * not in the original thread group (or in task context). | |
2619 | */ | |
2620 | if (!same_thread_group(req->task, current) || !in_task()) | |
2621 | return false; | |
3e6a0d3c JA |
2622 | return true; |
2623 | } | |
e82ad485 | 2624 | #else |
a1ff1e3f | 2625 | static bool io_resubmit_prep(struct io_kiocb *req) |
e82ad485 JA |
2626 | { |
2627 | return false; | |
2628 | } | |
e82ad485 | 2629 | static bool io_rw_should_reissue(struct io_kiocb *req) |
3e6a0d3c | 2630 | { |
b63534c4 JA |
2631 | return false; |
2632 | } | |
3e6a0d3c | 2633 | #endif |
b63534c4 | 2634 | |
8ef12efe | 2635 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
a1d7c393 | 2636 | { |
b65c128f PB |
2637 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
2638 | kiocb_end_write(req); | |
258f3a7f | 2639 | if (unlikely(res != req->result)) { |
9532b99b PB |
2640 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && |
2641 | io_rw_should_reissue(req)) { | |
2642 | req->flags |= REQ_F_REISSUE; | |
8ef12efe | 2643 | return true; |
9532b99b | 2644 | } |
93d2bcd2 | 2645 | req_set_fail(req); |
8ef12efe | 2646 | req->result = res; |
9532b99b | 2647 | } |
8ef12efe JA |
2648 | return false; |
2649 | } | |
2650 | ||
f237c30a | 2651 | static void io_req_task_complete(struct io_kiocb *req, bool *locked) |
8ef12efe | 2652 | { |
126180b9 | 2653 | unsigned int cflags = io_put_rw_kbuf(req); |
54daa9b2 | 2654 | int res = req->result; |
126180b9 | 2655 | |
fff4e40e | 2656 | if (*locked) { |
126180b9 | 2657 | io_req_complete_state(req, res, cflags); |
fff4e40e PB |
2658 | io_req_add_compl_list(req); |
2659 | } else { | |
126180b9 | 2660 | io_req_complete_post(req, res, cflags); |
fff4e40e | 2661 | } |
8ef12efe JA |
2662 | } |
2663 | ||
2664 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, | |
2665 | unsigned int issue_flags) | |
2666 | { | |
2667 | if (__io_complete_rw_common(req, res)) | |
2668 | return; | |
63637853 | 2669 | __io_req_complete(req, issue_flags, req->result, io_put_rw_kbuf(req)); |
ba816ad6 JA |
2670 | } |
2671 | ||
2672 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2673 | { | |
9adbd45d | 2674 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2675 | |
8ef12efe JA |
2676 | if (__io_complete_rw_common(req, res)) |
2677 | return; | |
2678 | req->result = res; | |
2679 | req->io_task_work.func = io_req_task_complete; | |
2680 | io_req_task_work_add(req); | |
2b188cc1 JA |
2681 | } |
2682 | ||
def596e9 JA |
2683 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2684 | { | |
9adbd45d | 2685 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2686 | |
491381ce JA |
2687 | if (kiocb->ki_flags & IOCB_WRITE) |
2688 | kiocb_end_write(req); | |
9532b99b | 2689 | if (unlikely(res != req->result)) { |
b66ceaf3 PB |
2690 | if (res == -EAGAIN && io_rw_should_reissue(req)) { |
2691 | req->flags |= REQ_F_REISSUE; | |
2692 | return; | |
9532b99b | 2693 | } |
258f3a7f | 2694 | req->result = res; |
8c130827 | 2695 | } |
bbde017a | 2696 | |
b3fa03fd PB |
2697 | /* order with io_iopoll_complete() checking ->iopoll_completed */ |
2698 | smp_store_release(&req->iopoll_completed, 1); | |
def596e9 JA |
2699 | } |
2700 | ||
2701 | /* | |
2702 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2703 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
f39c8a5b | 2704 | * find it from a io_do_iopoll() thread before the issuer is done |
def596e9 JA |
2705 | * accessing the kiocb cookie. |
2706 | */ | |
9882131c | 2707 | static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags) |
def596e9 JA |
2708 | { |
2709 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 2710 | const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
cb3d8972 PB |
2711 | |
2712 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
3b44b371 | 2713 | if (unlikely(needs_lock)) |
cb3d8972 | 2714 | mutex_lock(&ctx->uring_lock); |
def596e9 JA |
2715 | |
2716 | /* | |
2717 | * Track whether we have multiple files in our lists. This will impact | |
2718 | * how we do polling eventually, not spinning if we're on potentially | |
2719 | * different devices. | |
2720 | */ | |
5eef4e87 | 2721 | if (wq_list_empty(&ctx->iopoll_list)) { |
915b3dde HX |
2722 | ctx->poll_multi_queue = false; |
2723 | } else if (!ctx->poll_multi_queue) { | |
def596e9 JA |
2724 | struct io_kiocb *list_req; |
2725 | ||
5eef4e87 PB |
2726 | list_req = container_of(ctx->iopoll_list.first, struct io_kiocb, |
2727 | comp_list); | |
30da1b45 | 2728 | if (list_req->file != req->file) |
915b3dde | 2729 | ctx->poll_multi_queue = true; |
def596e9 JA |
2730 | } |
2731 | ||
2732 | /* | |
2733 | * For fast devices, IO may have already completed. If it has, add | |
2734 | * it to the front so we find it first. | |
2735 | */ | |
65a6543d | 2736 | if (READ_ONCE(req->iopoll_completed)) |
5eef4e87 | 2737 | wq_list_add_head(&req->comp_list, &ctx->iopoll_list); |
def596e9 | 2738 | else |
5eef4e87 | 2739 | wq_list_add_tail(&req->comp_list, &ctx->iopoll_list); |
bdcd3eab | 2740 | |
3b44b371 | 2741 | if (unlikely(needs_lock)) { |
cb3d8972 PB |
2742 | /* |
2743 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle | |
2744 | * in sq thread task context or in io worker task context. If | |
2745 | * current task context is sq thread, we don't need to check | |
2746 | * whether should wake up sq thread. | |
2747 | */ | |
2748 | if ((ctx->flags & IORING_SETUP_SQPOLL) && | |
2749 | wq_has_sleeper(&ctx->sq_data->wait)) | |
2750 | wake_up(&ctx->sq_data->wait); | |
2751 | ||
2752 | mutex_unlock(&ctx->uring_lock); | |
2753 | } | |
def596e9 JA |
2754 | } |
2755 | ||
4503b767 JA |
2756 | static bool io_bdev_nowait(struct block_device *bdev) |
2757 | { | |
9ba0d0c8 | 2758 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2759 | } |
2760 | ||
2b188cc1 JA |
2761 | /* |
2762 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2763 | * any file. For now, just ensure that anything potentially problematic is done | |
2764 | * inline. | |
2765 | */ | |
88459b50 | 2766 | static bool __io_file_supports_nowait(struct file *file, umode_t mode) |
2b188cc1 | 2767 | { |
4503b767 | 2768 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2769 | if (IS_ENABLED(CONFIG_BLOCK) && |
2770 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2771 | return true; |
2772 | return false; | |
2773 | } | |
976517f1 | 2774 | if (S_ISSOCK(mode)) |
2b188cc1 | 2775 | return true; |
4503b767 | 2776 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2777 | if (IS_ENABLED(CONFIG_BLOCK) && |
2778 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2779 | file->f_op != &io_uring_fops) |
2780 | return true; | |
2781 | return false; | |
2782 | } | |
2b188cc1 | 2783 | |
c5b85625 JA |
2784 | /* any ->read/write should understand O_NONBLOCK */ |
2785 | if (file->f_flags & O_NONBLOCK) | |
2786 | return true; | |
35645ac3 | 2787 | return file->f_mode & FMODE_NOWAIT; |
2b188cc1 JA |
2788 | } |
2789 | ||
88459b50 PB |
2790 | /* |
2791 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2792 | * any file. For now, just ensure that anything potentially problematic is done | |
2793 | * inline. | |
2794 | */ | |
2795 | static unsigned int io_file_get_flags(struct file *file) | |
2796 | { | |
2797 | umode_t mode = file_inode(file)->i_mode; | |
2798 | unsigned int res = 0; | |
2799 | ||
2800 | if (S_ISREG(mode)) | |
2801 | res |= FFS_ISREG; | |
2802 | if (__io_file_supports_nowait(file, mode)) | |
2803 | res |= FFS_NOWAIT; | |
2804 | return res; | |
2805 | } | |
2806 | ||
35645ac3 | 2807 | static inline bool io_file_supports_nowait(struct io_kiocb *req) |
7b29f92d | 2808 | { |
88459b50 | 2809 | return req->flags & REQ_F_SUPPORT_NOWAIT; |
7b29f92d JA |
2810 | } |
2811 | ||
5d329e12 JA |
2812 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2813 | int rw) | |
2b188cc1 | 2814 | { |
def596e9 | 2815 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2816 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 2817 | struct file *file = req->file; |
09bb8394 JA |
2818 | unsigned ioprio; |
2819 | int ret; | |
2b188cc1 | 2820 | |
88459b50 PB |
2821 | if (!io_req_ffs_set(req)) |
2822 | req->flags |= io_file_get_flags(file) << REQ_F_SUPPORT_NOWAIT_BIT; | |
491381ce | 2823 | |
2b188cc1 | 2824 | kiocb->ki_pos = READ_ONCE(sqe->off); |
75c668cd | 2825 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
ba04291e | 2826 | req->flags |= REQ_F_CUR_POS; |
75c668cd | 2827 | kiocb->ki_pos = file->f_pos; |
ba04291e | 2828 | } |
5cb03d63 PB |
2829 | kiocb->ki_hint = ki_hint_validate(file_write_hint(file)); |
2830 | kiocb->ki_flags = iocb_flags(file); | |
3e577dcd PB |
2831 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
2832 | if (unlikely(ret)) | |
2833 | return ret; | |
2b188cc1 | 2834 | |
5d329e12 JA |
2835 | /* |
2836 | * If the file is marked O_NONBLOCK, still allow retry for it if it | |
2837 | * supports async. Otherwise it's impossible to use O_NONBLOCK files | |
2838 | * reliably. If not, or it IOCB_NOWAIT is set, don't retry. | |
2839 | */ | |
2840 | if ((kiocb->ki_flags & IOCB_NOWAIT) || | |
35645ac3 | 2841 | ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req))) |
75c668cd PB |
2842 | req->flags |= REQ_F_NOWAIT; |
2843 | ||
2b188cc1 JA |
2844 | ioprio = READ_ONCE(sqe->ioprio); |
2845 | if (ioprio) { | |
2846 | ret = ioprio_check_cap(ioprio); | |
2847 | if (ret) | |
09bb8394 | 2848 | return ret; |
2b188cc1 JA |
2849 | |
2850 | kiocb->ki_ioprio = ioprio; | |
2851 | } else | |
2852 | kiocb->ki_ioprio = get_current_ioprio(); | |
2853 | ||
def596e9 | 2854 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
5cb03d63 | 2855 | if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll) |
09bb8394 | 2856 | return -EOPNOTSUPP; |
2b188cc1 | 2857 | |
394918eb | 2858 | kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE; |
def596e9 | 2859 | kiocb->ki_complete = io_complete_rw_iopoll; |
65a6543d | 2860 | req->iopoll_completed = 0; |
def596e9 | 2861 | } else { |
09bb8394 JA |
2862 | if (kiocb->ki_flags & IOCB_HIPRI) |
2863 | return -EINVAL; | |
def596e9 JA |
2864 | kiocb->ki_complete = io_complete_rw; |
2865 | } | |
9adbd45d | 2866 | |
578c0ee2 | 2867 | req->imu = NULL; |
3529d8c2 JA |
2868 | req->rw.addr = READ_ONCE(sqe->addr); |
2869 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2870 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2871 | return 0; |
2b188cc1 JA |
2872 | } |
2873 | ||
2874 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2875 | { | |
2876 | switch (ret) { | |
2877 | case -EIOCBQUEUED: | |
2878 | break; | |
2879 | case -ERESTARTSYS: | |
2880 | case -ERESTARTNOINTR: | |
2881 | case -ERESTARTNOHAND: | |
2882 | case -ERESTART_RESTARTBLOCK: | |
2883 | /* | |
2884 | * We can't just restart the syscall, since previously | |
2885 | * submitted sqes may already be in progress. Just fail this | |
2886 | * IO with EINTR. | |
2887 | */ | |
2888 | ret = -EINTR; | |
df561f66 | 2889 | fallthrough; |
2b188cc1 JA |
2890 | default: |
2891 | kiocb->ki_complete(kiocb, ret, 0); | |
2892 | } | |
2893 | } | |
2894 | ||
a1d7c393 | 2895 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
889fca73 | 2896 | unsigned int issue_flags) |
ba816ad6 | 2897 | { |
ba04291e | 2898 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2899 | struct io_async_rw *io = req->async_data; |
ba04291e | 2900 | |
227c0c96 | 2901 | /* add previously done IO, if any */ |
d886e185 | 2902 | if (req_has_async_data(req) && io->bytes_done > 0) { |
227c0c96 | 2903 | if (ret < 0) |
e8c2bc1f | 2904 | ret = io->bytes_done; |
227c0c96 | 2905 | else |
e8c2bc1f | 2906 | ret += io->bytes_done; |
227c0c96 JA |
2907 | } |
2908 | ||
ba04291e JA |
2909 | if (req->flags & REQ_F_CUR_POS) |
2910 | req->file->f_pos = kiocb->ki_pos; | |
b66ceaf3 | 2911 | if (ret >= 0 && (kiocb->ki_complete == io_complete_rw)) |
889fca73 | 2912 | __io_complete_rw(req, ret, 0, issue_flags); |
ba816ad6 JA |
2913 | else |
2914 | io_rw_done(kiocb, ret); | |
97284637 | 2915 | |
b66ceaf3 | 2916 | if (req->flags & REQ_F_REISSUE) { |
97284637 | 2917 | req->flags &= ~REQ_F_REISSUE; |
a7be7c23 | 2918 | if (io_resubmit_prep(req)) { |
773af691 | 2919 | io_req_task_queue_reissue(req); |
8c130827 | 2920 | } else { |
b66ceaf3 PB |
2921 | unsigned int cflags = io_put_rw_kbuf(req); |
2922 | struct io_ring_ctx *ctx = req->ctx; | |
2923 | ||
93d2bcd2 | 2924 | req_set_fail(req); |
3b44b371 | 2925 | if (issue_flags & IO_URING_F_UNLOCKED) { |
b66ceaf3 PB |
2926 | mutex_lock(&ctx->uring_lock); |
2927 | __io_req_complete(req, issue_flags, ret, cflags); | |
2928 | mutex_unlock(&ctx->uring_lock); | |
2929 | } else { | |
2930 | __io_req_complete(req, issue_flags, ret, cflags); | |
2931 | } | |
97284637 PB |
2932 | } |
2933 | } | |
ba816ad6 JA |
2934 | } |
2935 | ||
eae071c9 PB |
2936 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
2937 | struct io_mapped_ubuf *imu) | |
edafccee | 2938 | { |
9adbd45d | 2939 | size_t len = req->rw.len; |
75769e3f | 2940 | u64 buf_end, buf_addr = req->rw.addr; |
edafccee | 2941 | size_t offset; |
edafccee | 2942 | |
75769e3f | 2943 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
edafccee JA |
2944 | return -EFAULT; |
2945 | /* not inside the mapped region */ | |
4751f53d | 2946 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
edafccee JA |
2947 | return -EFAULT; |
2948 | ||
2949 | /* | |
2950 | * May not be a start of buffer, set size appropriately | |
2951 | * and advance us to the beginning. | |
2952 | */ | |
2953 | offset = buf_addr - imu->ubuf; | |
2954 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
2955 | |
2956 | if (offset) { | |
2957 | /* | |
2958 | * Don't use iov_iter_advance() here, as it's really slow for | |
2959 | * using the latter parts of a big fixed buffer - it iterates | |
2960 | * over each segment manually. We can cheat a bit here, because | |
2961 | * we know that: | |
2962 | * | |
2963 | * 1) it's a BVEC iter, we set it up | |
2964 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
2965 | * first and last bvec | |
2966 | * | |
2967 | * So just find our index, and adjust the iterator afterwards. | |
2968 | * If the offset is within the first bvec (or the whole first | |
2969 | * bvec, just use iov_iter_advance(). This makes it easier | |
2970 | * since we can just skip the first segment, which may not | |
2971 | * be PAGE_SIZE aligned. | |
2972 | */ | |
2973 | const struct bio_vec *bvec = imu->bvec; | |
2974 | ||
2975 | if (offset <= bvec->bv_len) { | |
2976 | iov_iter_advance(iter, offset); | |
2977 | } else { | |
2978 | unsigned long seg_skip; | |
2979 | ||
2980 | /* skip first vec */ | |
2981 | offset -= bvec->bv_len; | |
2982 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
2983 | ||
2984 | iter->bvec = bvec + seg_skip; | |
2985 | iter->nr_segs -= seg_skip; | |
99c79f66 | 2986 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 2987 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
2988 | } |
2989 | } | |
2990 | ||
847595de | 2991 | return 0; |
edafccee JA |
2992 | } |
2993 | ||
eae071c9 PB |
2994 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
2995 | { | |
eae071c9 PB |
2996 | struct io_mapped_ubuf *imu = req->imu; |
2997 | u16 index, buf_index = req->buf_index; | |
2998 | ||
2999 | if (likely(!imu)) { | |
578c0ee2 PB |
3000 | struct io_ring_ctx *ctx = req->ctx; |
3001 | ||
eae071c9 PB |
3002 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
3003 | return -EFAULT; | |
578c0ee2 | 3004 | io_req_set_rsrc_node(req, ctx); |
eae071c9 PB |
3005 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
3006 | imu = READ_ONCE(ctx->user_bufs[index]); | |
3007 | req->imu = imu; | |
3008 | } | |
3009 | return __io_import_fixed(req, rw, iter, imu); | |
3010 | } | |
3011 | ||
bcda7baa JA |
3012 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
3013 | { | |
3014 | if (needs_lock) | |
3015 | mutex_unlock(&ctx->uring_lock); | |
3016 | } | |
3017 | ||
3018 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
3019 | { | |
3020 | /* | |
3021 | * "Normal" inline submissions always hold the uring_lock, since we | |
3022 | * grab it from the system call. Same is true for the SQPOLL offload. | |
3023 | * The only exception is when we've detached the request and issue it | |
3024 | * from an async worker thread, grab the lock for that case. | |
3025 | */ | |
3026 | if (needs_lock) | |
3027 | mutex_lock(&ctx->uring_lock); | |
3028 | } | |
3029 | ||
3030 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
51aac424 | 3031 | int bgid, unsigned int issue_flags) |
bcda7baa | 3032 | { |
30d51dd4 | 3033 | struct io_buffer *kbuf = req->kbuf; |
bcda7baa | 3034 | struct io_buffer *head; |
3b44b371 | 3035 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
bcda7baa JA |
3036 | |
3037 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3038 | return kbuf; | |
3039 | ||
3040 | io_ring_submit_lock(req->ctx, needs_lock); | |
3041 | ||
3042 | lockdep_assert_held(&req->ctx->uring_lock); | |
3043 | ||
9e15c3a0 | 3044 | head = xa_load(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
3045 | if (head) { |
3046 | if (!list_empty(&head->list)) { | |
3047 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
3048 | list); | |
3049 | list_del(&kbuf->list); | |
3050 | } else { | |
3051 | kbuf = head; | |
9e15c3a0 | 3052 | xa_erase(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
3053 | } |
3054 | if (*len > kbuf->len) | |
3055 | *len = kbuf->len; | |
30d51dd4 PB |
3056 | req->flags |= REQ_F_BUFFER_SELECTED; |
3057 | req->kbuf = kbuf; | |
bcda7baa JA |
3058 | } else { |
3059 | kbuf = ERR_PTR(-ENOBUFS); | |
3060 | } | |
3061 | ||
3062 | io_ring_submit_unlock(req->ctx, needs_lock); | |
bcda7baa JA |
3063 | return kbuf; |
3064 | } | |
3065 | ||
4d954c25 | 3066 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
51aac424 | 3067 | unsigned int issue_flags) |
4d954c25 JA |
3068 | { |
3069 | struct io_buffer *kbuf; | |
4f4eeba8 | 3070 | u16 bgid; |
4d954c25 | 3071 | |
4f4eeba8 | 3072 | bgid = req->buf_index; |
51aac424 | 3073 | kbuf = io_buffer_select(req, len, bgid, issue_flags); |
4d954c25 JA |
3074 | if (IS_ERR(kbuf)) |
3075 | return kbuf; | |
4d954c25 JA |
3076 | return u64_to_user_ptr(kbuf->addr); |
3077 | } | |
3078 | ||
3079 | #ifdef CONFIG_COMPAT | |
3080 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3081 | unsigned int issue_flags) |
4d954c25 JA |
3082 | { |
3083 | struct compat_iovec __user *uiov; | |
3084 | compat_ssize_t clen; | |
3085 | void __user *buf; | |
3086 | ssize_t len; | |
3087 | ||
3088 | uiov = u64_to_user_ptr(req->rw.addr); | |
3089 | if (!access_ok(uiov, sizeof(*uiov))) | |
3090 | return -EFAULT; | |
3091 | if (__get_user(clen, &uiov->iov_len)) | |
3092 | return -EFAULT; | |
3093 | if (clen < 0) | |
3094 | return -EINVAL; | |
3095 | ||
3096 | len = clen; | |
51aac424 | 3097 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3098 | if (IS_ERR(buf)) |
3099 | return PTR_ERR(buf); | |
3100 | iov[0].iov_base = buf; | |
3101 | iov[0].iov_len = (compat_size_t) len; | |
3102 | return 0; | |
3103 | } | |
3104 | #endif | |
3105 | ||
3106 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3107 | unsigned int issue_flags) |
4d954c25 JA |
3108 | { |
3109 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3110 | void __user *buf; | |
3111 | ssize_t len; | |
3112 | ||
3113 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3114 | return -EFAULT; | |
3115 | ||
3116 | len = iov[0].iov_len; | |
3117 | if (len < 0) | |
3118 | return -EINVAL; | |
51aac424 | 3119 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3120 | if (IS_ERR(buf)) |
3121 | return PTR_ERR(buf); | |
3122 | iov[0].iov_base = buf; | |
3123 | iov[0].iov_len = len; | |
3124 | return 0; | |
3125 | } | |
3126 | ||
3127 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3128 | unsigned int issue_flags) |
4d954c25 | 3129 | { |
dddb3e26 | 3130 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
30d51dd4 | 3131 | struct io_buffer *kbuf = req->kbuf; |
dddb3e26 | 3132 | |
dddb3e26 JA |
3133 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
3134 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3135 | return 0; |
dddb3e26 | 3136 | } |
dd201662 | 3137 | if (req->rw.len != 1) |
4d954c25 JA |
3138 | return -EINVAL; |
3139 | ||
3140 | #ifdef CONFIG_COMPAT | |
3141 | if (req->ctx->compat) | |
51aac424 | 3142 | return io_compat_import(req, iov, issue_flags); |
4d954c25 JA |
3143 | #endif |
3144 | ||
51aac424 | 3145 | return __io_iov_buffer_select(req, iov, issue_flags); |
4d954c25 JA |
3146 | } |
3147 | ||
caa8fe6e PB |
3148 | static struct iovec *__io_import_iovec(int rw, struct io_kiocb *req, |
3149 | struct io_rw_state *s, | |
3150 | unsigned int issue_flags) | |
2b188cc1 | 3151 | { |
5e49c973 | 3152 | struct iov_iter *iter = &s->iter; |
847595de | 3153 | u8 opcode = req->opcode; |
caa8fe6e | 3154 | struct iovec *iovec; |
d1d681b0 PB |
3155 | void __user *buf; |
3156 | size_t sqe_len; | |
4d954c25 | 3157 | ssize_t ret; |
edafccee | 3158 | |
caa8fe6e PB |
3159 | BUILD_BUG_ON(ERR_PTR(0) != NULL); |
3160 | ||
3161 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) | |
3162 | return ERR_PTR(io_import_fixed(req, rw, iter)); | |
2b188cc1 | 3163 | |
bcda7baa | 3164 | /* buffer index only valid with fixed read/write, or buffer select */ |
d1d681b0 | 3165 | if (unlikely(req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))) |
caa8fe6e | 3166 | return ERR_PTR(-EINVAL); |
9adbd45d | 3167 | |
d1d681b0 PB |
3168 | buf = u64_to_user_ptr(req->rw.addr); |
3169 | sqe_len = req->rw.len; | |
3170 | ||
3a6820f2 | 3171 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3172 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 3173 | buf = io_rw_buffer_select(req, &sqe_len, issue_flags); |
867a23ea | 3174 | if (IS_ERR(buf)) |
898df244 | 3175 | return ERR_CAST(buf); |
3f9d6441 | 3176 | req->rw.len = sqe_len; |
bcda7baa JA |
3177 | } |
3178 | ||
5e49c973 | 3179 | ret = import_single_range(rw, buf, sqe_len, s->fast_iov, iter); |
caa8fe6e | 3180 | return ERR_PTR(ret); |
3a6820f2 JA |
3181 | } |
3182 | ||
caa8fe6e | 3183 | iovec = s->fast_iov; |
4d954c25 | 3184 | if (req->flags & REQ_F_BUFFER_SELECT) { |
caa8fe6e | 3185 | ret = io_iov_buffer_select(req, iovec, issue_flags); |
847595de | 3186 | if (!ret) |
caa8fe6e PB |
3187 | iov_iter_init(iter, rw, iovec, 1, iovec->iov_len); |
3188 | return ERR_PTR(ret); | |
4d954c25 JA |
3189 | } |
3190 | ||
caa8fe6e | 3191 | ret = __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, &iovec, iter, |
89cd35c5 | 3192 | req->ctx->compat); |
caa8fe6e PB |
3193 | if (unlikely(ret < 0)) |
3194 | return ERR_PTR(ret); | |
3195 | return iovec; | |
2b188cc1 JA |
3196 | } |
3197 | ||
5e49c973 PB |
3198 | static inline int io_import_iovec(int rw, struct io_kiocb *req, |
3199 | struct iovec **iovec, struct io_rw_state *s, | |
3200 | unsigned int issue_flags) | |
3201 | { | |
caa8fe6e PB |
3202 | *iovec = __io_import_iovec(rw, req, s, issue_flags); |
3203 | if (unlikely(IS_ERR(*iovec))) | |
3204 | return PTR_ERR(*iovec); | |
5e49c973 | 3205 | |
5e49c973 | 3206 | iov_iter_save_state(&s->iter, &s->iter_state); |
caa8fe6e | 3207 | return 0; |
5e49c973 PB |
3208 | } |
3209 | ||
0fef9483 JA |
3210 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3211 | { | |
5b09e37e | 3212 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3213 | } |
3214 | ||
31b51510 | 3215 | /* |
32960613 JA |
3216 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3217 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3218 | */ |
4017eb91 | 3219 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3220 | { |
4017eb91 JA |
3221 | struct kiocb *kiocb = &req->rw.kiocb; |
3222 | struct file *file = req->file; | |
32960613 JA |
3223 | ssize_t ret = 0; |
3224 | ||
3225 | /* | |
3226 | * Don't support polled IO through this interface, and we can't | |
3227 | * support non-blocking either. For the latter, this just causes | |
3228 | * the kiocb to be handled from an async context. | |
3229 | */ | |
3230 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3231 | return -EOPNOTSUPP; | |
35645ac3 PB |
3232 | if ((kiocb->ki_flags & IOCB_NOWAIT) && |
3233 | !(kiocb->ki_filp->f_flags & O_NONBLOCK)) | |
32960613 JA |
3234 | return -EAGAIN; |
3235 | ||
3236 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3237 | struct iovec iovec; |
32960613 JA |
3238 | ssize_t nr; |
3239 | ||
311ae9e1 PB |
3240 | if (!iov_iter_is_bvec(iter)) { |
3241 | iovec = iov_iter_iovec(iter); | |
3242 | } else { | |
4017eb91 JA |
3243 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3244 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3245 | } |
3246 | ||
32960613 JA |
3247 | if (rw == READ) { |
3248 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3249 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3250 | } else { |
3251 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3252 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3253 | } |
3254 | ||
3255 | if (nr < 0) { | |
3256 | if (!ret) | |
3257 | ret = nr; | |
3258 | break; | |
3259 | } | |
16c8d2df JA |
3260 | if (!iov_iter_is_bvec(iter)) { |
3261 | iov_iter_advance(iter, nr); | |
3262 | } else { | |
3263 | req->rw.len -= nr; | |
3264 | req->rw.addr += nr; | |
3265 | } | |
32960613 JA |
3266 | ret += nr; |
3267 | if (nr != iovec.iov_len) | |
3268 | break; | |
32960613 JA |
3269 | } |
3270 | ||
3271 | return ret; | |
3272 | } | |
3273 | ||
ff6165b2 JA |
3274 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3275 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3276 | { |
e8c2bc1f | 3277 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3278 | |
538941e2 | 3279 | memcpy(&rw->s.iter, iter, sizeof(*iter)); |
afb87658 | 3280 | rw->free_iovec = iovec; |
227c0c96 | 3281 | rw->bytes_done = 0; |
ff6165b2 | 3282 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3283 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3284 | return; |
b64e3444 | 3285 | if (!iovec) { |
ff6165b2 JA |
3286 | unsigned iov_off = 0; |
3287 | ||
538941e2 | 3288 | rw->s.iter.iov = rw->s.fast_iov; |
ff6165b2 JA |
3289 | if (iter->iov != fast_iov) { |
3290 | iov_off = iter->iov - fast_iov; | |
538941e2 | 3291 | rw->s.iter.iov += iov_off; |
ff6165b2 | 3292 | } |
538941e2 PB |
3293 | if (rw->s.fast_iov != fast_iov) |
3294 | memcpy(rw->s.fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3295 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3296 | } else { |
3297 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3298 | } |
3299 | } | |
3300 | ||
8d4af685 | 3301 | static inline bool io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3302 | { |
e8c2bc1f JA |
3303 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3304 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
d886e185 PB |
3305 | if (req->async_data) { |
3306 | req->flags |= REQ_F_ASYNC_DATA; | |
3307 | return false; | |
3308 | } | |
3309 | return true; | |
3d9932a8 XW |
3310 | } |
3311 | ||
ff6165b2 | 3312 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
c88598a9 | 3313 | struct io_rw_state *s, bool force) |
b7bb4f7d | 3314 | { |
26f0505a | 3315 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
74566df3 | 3316 | return 0; |
d886e185 | 3317 | if (!req_has_async_data(req)) { |
cd658695 JA |
3318 | struct io_async_rw *iorw; |
3319 | ||
6cb78689 | 3320 | if (io_alloc_async_data(req)) { |
6bf985dc | 3321 | kfree(iovec); |
5d204bcf | 3322 | return -ENOMEM; |
6bf985dc | 3323 | } |
b7bb4f7d | 3324 | |
c88598a9 | 3325 | io_req_map_rw(req, iovec, s->fast_iov, &s->iter); |
cd658695 JA |
3326 | iorw = req->async_data; |
3327 | /* we've copied and mapped the iter, ensure state is saved */ | |
538941e2 | 3328 | iov_iter_save_state(&iorw->s.iter, &iorw->s.iter_state); |
5d204bcf | 3329 | } |
b7bb4f7d | 3330 | return 0; |
f67676d1 JA |
3331 | } |
3332 | ||
73debe68 | 3333 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3334 | { |
e8c2bc1f | 3335 | struct io_async_rw *iorw = req->async_data; |
5e49c973 | 3336 | struct iovec *iov; |
847595de | 3337 | int ret; |
c3e330a4 | 3338 | |
51aac424 | 3339 | /* submission path, ->uring_lock should already be taken */ |
3b44b371 | 3340 | ret = io_import_iovec(rw, req, &iov, &iorw->s, 0); |
c3e330a4 PB |
3341 | if (unlikely(ret < 0)) |
3342 | return ret; | |
3343 | ||
ab0b196c PB |
3344 | iorw->bytes_done = 0; |
3345 | iorw->free_iovec = iov; | |
3346 | if (iov) | |
3347 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3348 | return 0; |
3349 | } | |
3350 | ||
73debe68 | 3351 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3352 | { |
3529d8c2 JA |
3353 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3354 | return -EBADF; | |
5d329e12 | 3355 | return io_prep_rw(req, sqe, READ); |
f67676d1 JA |
3356 | } |
3357 | ||
c1dd91d1 JA |
3358 | /* |
3359 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3360 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3361 | * This gets called when the page is unlocked, and we generally expect that to | |
3362 | * happen when the page IO is completed and the page is now uptodate. This will | |
3363 | * queue a task_work based retry of the operation, attempting to copy the data | |
3364 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3365 | * do a thread based blocking retry of the operation. That's the unexpected | |
3366 | * slow path. | |
3367 | */ | |
bcf5a063 JA |
3368 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3369 | int sync, void *arg) | |
3370 | { | |
3371 | struct wait_page_queue *wpq; | |
3372 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3373 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3374 | |
3375 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3376 | ||
cdc8fcb4 LT |
3377 | if (!wake_page_match(wpq, key)) |
3378 | return 0; | |
3379 | ||
c8d317aa | 3380 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 | 3381 | list_del_init(&wait->entry); |
921b9054 | 3382 | io_req_task_queue(req); |
bcf5a063 JA |
3383 | return 1; |
3384 | } | |
3385 | ||
c1dd91d1 JA |
3386 | /* |
3387 | * This controls whether a given IO request should be armed for async page | |
3388 | * based retry. If we return false here, the request is handed to the async | |
3389 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3390 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3391 | * will either succeed because the page is now uptodate and unlocked, or it | |
3392 | * will register a callback when the page is unlocked at IO completion. Through | |
3393 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3394 | * That retry will attempt the buffered read again. The retry will generally | |
3395 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3396 | * async worker threads for a blocking retry. | |
3397 | */ | |
227c0c96 | 3398 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3399 | { |
e8c2bc1f JA |
3400 | struct io_async_rw *rw = req->async_data; |
3401 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3402 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3403 | |
bcf5a063 JA |
3404 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3405 | if (req->flags & REQ_F_NOWAIT) | |
3406 | return false; | |
f67676d1 | 3407 | |
227c0c96 | 3408 | /* Only for buffered IO */ |
3b2a4439 | 3409 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3410 | return false; |
3b2a4439 | 3411 | |
bcf5a063 JA |
3412 | /* |
3413 | * just use poll if we can, and don't attempt if the fs doesn't | |
3414 | * support callback based unlocks | |
3415 | */ | |
3416 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3417 | return false; | |
f67676d1 | 3418 | |
3b2a4439 JA |
3419 | wait->wait.func = io_async_buf_func; |
3420 | wait->wait.private = req; | |
3421 | wait->wait.flags = 0; | |
3422 | INIT_LIST_HEAD(&wait->wait.entry); | |
3423 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3424 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3425 | kiocb->ki_waitq = wait; |
3b2a4439 | 3426 | return true; |
bcf5a063 JA |
3427 | } |
3428 | ||
aeab9506 | 3429 | static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
bcf5a063 | 3430 | { |
607b6fb8 | 3431 | if (likely(req->file->f_op->read_iter)) |
bcf5a063 | 3432 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
2dd2111d | 3433 | else if (req->file->f_op->read) |
4017eb91 | 3434 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3435 | else |
3436 | return -EINVAL; | |
f67676d1 JA |
3437 | } |
3438 | ||
7db30437 ML |
3439 | static bool need_read_all(struct io_kiocb *req) |
3440 | { | |
3441 | return req->flags & REQ_F_ISREG || | |
3442 | S_ISBLK(file_inode(req->file)->i_mode); | |
3443 | } | |
3444 | ||
889fca73 | 3445 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 3446 | { |
607b6fb8 | 3447 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 3448 | struct iovec *iovec; |
9adbd45d | 3449 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 3450 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
d886e185 | 3451 | struct io_async_rw *rw; |
cd658695 | 3452 | ssize_t ret, ret2; |
ff6165b2 | 3453 | |
607b6fb8 PB |
3454 | if (!req_has_async_data(req)) { |
3455 | ret = io_import_iovec(READ, req, &iovec, s, issue_flags); | |
3456 | if (unlikely(ret < 0)) | |
3457 | return ret; | |
3458 | } else { | |
d886e185 | 3459 | rw = req->async_data; |
c88598a9 | 3460 | s = &rw->s; |
cd658695 JA |
3461 | /* |
3462 | * We come here from an earlier attempt, restore our state to | |
3463 | * match in case it doesn't. It's cheap enough that we don't | |
3464 | * need to make this conditional. | |
3465 | */ | |
c88598a9 | 3466 | iov_iter_restore(&s->iter, &s->iter_state); |
2846c481 | 3467 | iovec = NULL; |
2846c481 | 3468 | } |
c88598a9 | 3469 | req->result = iov_iter_count(&s->iter); |
2b188cc1 | 3470 | |
607b6fb8 PB |
3471 | if (force_nonblock) { |
3472 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 3473 | if (unlikely(!io_file_supports_nowait(req))) { |
607b6fb8 PB |
3474 | ret = io_setup_async_rw(req, iovec, s, true); |
3475 | return ret ?: -EAGAIN; | |
3476 | } | |
a88fc400 | 3477 | kiocb->ki_flags |= IOCB_NOWAIT; |
607b6fb8 PB |
3478 | } else { |
3479 | /* Ensure we clear previously set non-block flag */ | |
3480 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
6713e7a6 | 3481 | } |
9e645e11 | 3482 | |
cd658695 | 3483 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), req->result); |
5ea5dd45 PB |
3484 | if (unlikely(ret)) { |
3485 | kfree(iovec); | |
3486 | return ret; | |
3487 | } | |
2b188cc1 | 3488 | |
c88598a9 | 3489 | ret = io_iter_do_read(req, &s->iter); |
32960613 | 3490 | |
230d50d4 | 3491 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
6ad7f233 | 3492 | req->flags &= ~REQ_F_REISSUE; |
eefdf30f JA |
3493 | /* IOPOLL retry should happen for io-wq threads */ |
3494 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3495 | goto done; |
75c668cd PB |
3496 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3497 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3498 | goto done; |
f38c7e3a | 3499 | ret = 0; |
230d50d4 JA |
3500 | } else if (ret == -EIOCBQUEUED) { |
3501 | goto out_free; | |
f80a50a6 | 3502 | } else if (ret == req->result || ret <= 0 || !force_nonblock || |
7db30437 | 3503 | (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) { |
7335e3bf | 3504 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3505 | goto done; |
227c0c96 JA |
3506 | } |
3507 | ||
cd658695 JA |
3508 | /* |
3509 | * Don't depend on the iter state matching what was consumed, or being | |
3510 | * untouched in case of error. Restore it and we'll advance it | |
3511 | * manually if we need to. | |
3512 | */ | |
c88598a9 | 3513 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 3514 | |
c88598a9 | 3515 | ret2 = io_setup_async_rw(req, iovec, s, true); |
6bf985dc PB |
3516 | if (ret2) |
3517 | return ret2; | |
3518 | ||
fe1cdd55 | 3519 | iovec = NULL; |
e8c2bc1f | 3520 | rw = req->async_data; |
c88598a9 | 3521 | s = &rw->s; |
cd658695 JA |
3522 | /* |
3523 | * Now use our persistent iterator and state, if we aren't already. | |
3524 | * We've restored and mapped the iter to match. | |
3525 | */ | |
227c0c96 | 3526 | |
b23df91b | 3527 | do { |
cd658695 JA |
3528 | /* |
3529 | * We end up here because of a partial read, either from | |
3530 | * above or inside this loop. Advance the iter by the bytes | |
3531 | * that were consumed. | |
3532 | */ | |
c88598a9 PB |
3533 | iov_iter_advance(&s->iter, ret); |
3534 | if (!iov_iter_count(&s->iter)) | |
cd658695 | 3535 | break; |
b23df91b | 3536 | rw->bytes_done += ret; |
c88598a9 | 3537 | iov_iter_save_state(&s->iter, &s->iter_state); |
cd658695 | 3538 | |
b23df91b PB |
3539 | /* if we can retry, do so with the callbacks armed */ |
3540 | if (!io_rw_should_retry(req)) { | |
3541 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3542 | return -EAGAIN; | |
3543 | } | |
3544 | ||
3545 | /* | |
3546 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3547 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3548 | * desired page gets unlocked. We can also get a partial read | |
3549 | * here, and if we do, then just retry at the new offset. | |
3550 | */ | |
c88598a9 | 3551 | ret = io_iter_do_read(req, &s->iter); |
b23df91b PB |
3552 | if (ret == -EIOCBQUEUED) |
3553 | return 0; | |
227c0c96 | 3554 | /* we got some bytes, but not all. retry. */ |
b5b0ecb7 | 3555 | kiocb->ki_flags &= ~IOCB_WAITQ; |
c88598a9 | 3556 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 3557 | } while (ret > 0); |
227c0c96 | 3558 | done: |
889fca73 | 3559 | kiocb_done(kiocb, ret, issue_flags); |
fe1cdd55 PB |
3560 | out_free: |
3561 | /* it's faster to check here then delegate to kfree */ | |
3562 | if (iovec) | |
3563 | kfree(iovec); | |
5ea5dd45 | 3564 | return 0; |
2b188cc1 JA |
3565 | } |
3566 | ||
73debe68 | 3567 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3568 | { |
3529d8c2 JA |
3569 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3570 | return -EBADF; | |
5d329e12 | 3571 | return io_prep_rw(req, sqe, WRITE); |
f67676d1 JA |
3572 | } |
3573 | ||
889fca73 | 3574 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 3575 | { |
607b6fb8 | 3576 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 3577 | struct iovec *iovec; |
9adbd45d | 3578 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 3579 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
cd658695 | 3580 | ssize_t ret, ret2; |
2b188cc1 | 3581 | |
607b6fb8 | 3582 | if (!req_has_async_data(req)) { |
5e49c973 PB |
3583 | ret = io_import_iovec(WRITE, req, &iovec, s, issue_flags); |
3584 | if (unlikely(ret < 0)) | |
2846c481 | 3585 | return ret; |
607b6fb8 PB |
3586 | } else { |
3587 | struct io_async_rw *rw = req->async_data; | |
3588 | ||
3589 | s = &rw->s; | |
3590 | iov_iter_restore(&s->iter, &s->iter_state); | |
3591 | iovec = NULL; | |
2846c481 | 3592 | } |
c88598a9 | 3593 | req->result = iov_iter_count(&s->iter); |
2b188cc1 | 3594 | |
607b6fb8 PB |
3595 | if (force_nonblock) { |
3596 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 3597 | if (unlikely(!io_file_supports_nowait(req))) |
607b6fb8 | 3598 | goto copy_iov; |
fd6c2e4c | 3599 | |
607b6fb8 PB |
3600 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3601 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3602 | (req->flags & REQ_F_ISREG)) | |
3603 | goto copy_iov; | |
31b51510 | 3604 | |
607b6fb8 PB |
3605 | kiocb->ki_flags |= IOCB_NOWAIT; |
3606 | } else { | |
3607 | /* Ensure we clear previously set non-block flag */ | |
3608 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
3609 | } | |
31b51510 | 3610 | |
cd658695 | 3611 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), req->result); |
fa15bafb PB |
3612 | if (unlikely(ret)) |
3613 | goto out_free; | |
4ed734b0 | 3614 | |
fa15bafb PB |
3615 | /* |
3616 | * Open-code file_start_write here to grab freeze protection, | |
3617 | * which will be released by another thread in | |
3618 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3619 | * released so that it doesn't complain about the held lock when | |
3620 | * we return to userspace. | |
3621 | */ | |
3622 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3623 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3624 | __sb_writers_release(file_inode(req->file)->i_sb, |
3625 | SB_FREEZE_WRITE); | |
3626 | } | |
3627 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3628 | |
35645ac3 | 3629 | if (likely(req->file->f_op->write_iter)) |
c88598a9 | 3630 | ret2 = call_write_iter(req->file, kiocb, &s->iter); |
2dd2111d | 3631 | else if (req->file->f_op->write) |
c88598a9 | 3632 | ret2 = loop_rw_iter(WRITE, req, &s->iter); |
2dd2111d GH |
3633 | else |
3634 | ret2 = -EINVAL; | |
4ed734b0 | 3635 | |
6ad7f233 PB |
3636 | if (req->flags & REQ_F_REISSUE) { |
3637 | req->flags &= ~REQ_F_REISSUE; | |
230d50d4 | 3638 | ret2 = -EAGAIN; |
6ad7f233 | 3639 | } |
230d50d4 | 3640 | |
fa15bafb PB |
3641 | /* |
3642 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3643 | * retry them without IOCB_NOWAIT. | |
3644 | */ | |
3645 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3646 | ret2 = -EAGAIN; | |
75c668cd PB |
3647 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3648 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3649 | goto done; |
fa15bafb | 3650 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f | 3651 | /* IOPOLL retry should happen for io-wq threads */ |
b10841c9 | 3652 | if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL)) |
eefdf30f | 3653 | goto copy_iov; |
355afaeb | 3654 | done: |
889fca73 | 3655 | kiocb_done(kiocb, ret2, issue_flags); |
fa15bafb | 3656 | } else { |
f67676d1 | 3657 | copy_iov: |
c88598a9 PB |
3658 | iov_iter_restore(&s->iter, &s->iter_state); |
3659 | ret = io_setup_async_rw(req, iovec, s, false); | |
6bf985dc | 3660 | return ret ?: -EAGAIN; |
2b188cc1 | 3661 | } |
31b51510 | 3662 | out_free: |
f261c168 | 3663 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3664 | if (iovec) |
6f2cc166 | 3665 | kfree(iovec); |
2b188cc1 JA |
3666 | return ret; |
3667 | } | |
3668 | ||
80a261fd JA |
3669 | static int io_renameat_prep(struct io_kiocb *req, |
3670 | const struct io_uring_sqe *sqe) | |
3671 | { | |
3672 | struct io_rename *ren = &req->rename; | |
3673 | const char __user *oldf, *newf; | |
3674 | ||
ed7eb259 JA |
3675 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3676 | return -EINVAL; | |
26578cda | 3677 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
ed7eb259 | 3678 | return -EINVAL; |
80a261fd JA |
3679 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3680 | return -EBADF; | |
3681 | ||
3682 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3683 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3684 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3685 | ren->new_dfd = READ_ONCE(sqe->len); | |
3686 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3687 | ||
3688 | ren->oldpath = getname(oldf); | |
3689 | if (IS_ERR(ren->oldpath)) | |
3690 | return PTR_ERR(ren->oldpath); | |
3691 | ||
3692 | ren->newpath = getname(newf); | |
3693 | if (IS_ERR(ren->newpath)) { | |
3694 | putname(ren->oldpath); | |
3695 | return PTR_ERR(ren->newpath); | |
3696 | } | |
3697 | ||
3698 | req->flags |= REQ_F_NEED_CLEANUP; | |
3699 | return 0; | |
3700 | } | |
3701 | ||
45d189c6 | 3702 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
3703 | { |
3704 | struct io_rename *ren = &req->rename; | |
3705 | int ret; | |
3706 | ||
45d189c6 | 3707 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
3708 | return -EAGAIN; |
3709 | ||
3710 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3711 | ren->newpath, ren->flags); | |
3712 | ||
3713 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3714 | if (ret < 0) | |
93d2bcd2 | 3715 | req_set_fail(req); |
80a261fd JA |
3716 | io_req_complete(req, ret); |
3717 | return 0; | |
3718 | } | |
3719 | ||
14a1143b JA |
3720 | static int io_unlinkat_prep(struct io_kiocb *req, |
3721 | const struct io_uring_sqe *sqe) | |
3722 | { | |
3723 | struct io_unlink *un = &req->unlink; | |
3724 | const char __user *fname; | |
3725 | ||
22634bc5 JA |
3726 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3727 | return -EINVAL; | |
26578cda PB |
3728 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
3729 | sqe->splice_fd_in) | |
22634bc5 | 3730 | return -EINVAL; |
14a1143b JA |
3731 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3732 | return -EBADF; | |
3733 | ||
3734 | un->dfd = READ_ONCE(sqe->fd); | |
3735 | ||
3736 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3737 | if (un->flags & ~AT_REMOVEDIR) | |
3738 | return -EINVAL; | |
3739 | ||
3740 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3741 | un->filename = getname(fname); | |
3742 | if (IS_ERR(un->filename)) | |
3743 | return PTR_ERR(un->filename); | |
3744 | ||
3745 | req->flags |= REQ_F_NEED_CLEANUP; | |
3746 | return 0; | |
3747 | } | |
3748 | ||
45d189c6 | 3749 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
3750 | { |
3751 | struct io_unlink *un = &req->unlink; | |
3752 | int ret; | |
3753 | ||
45d189c6 | 3754 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
3755 | return -EAGAIN; |
3756 | ||
3757 | if (un->flags & AT_REMOVEDIR) | |
3758 | ret = do_rmdir(un->dfd, un->filename); | |
3759 | else | |
3760 | ret = do_unlinkat(un->dfd, un->filename); | |
3761 | ||
3762 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3763 | if (ret < 0) | |
93d2bcd2 | 3764 | req_set_fail(req); |
14a1143b JA |
3765 | io_req_complete(req, ret); |
3766 | return 0; | |
3767 | } | |
3768 | ||
e34a02dc DK |
3769 | static int io_mkdirat_prep(struct io_kiocb *req, |
3770 | const struct io_uring_sqe *sqe) | |
3771 | { | |
3772 | struct io_mkdir *mkd = &req->mkdir; | |
3773 | const char __user *fname; | |
3774 | ||
3775 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3776 | return -EINVAL; | |
3777 | if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index || | |
3778 | sqe->splice_fd_in) | |
3779 | return -EINVAL; | |
3780 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3781 | return -EBADF; | |
3782 | ||
3783 | mkd->dfd = READ_ONCE(sqe->fd); | |
3784 | mkd->mode = READ_ONCE(sqe->len); | |
3785 | ||
3786 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3787 | mkd->filename = getname(fname); | |
3788 | if (IS_ERR(mkd->filename)) | |
3789 | return PTR_ERR(mkd->filename); | |
3790 | ||
3791 | req->flags |= REQ_F_NEED_CLEANUP; | |
3792 | return 0; | |
3793 | } | |
3794 | ||
04f34081 | 3795 | static int io_mkdirat(struct io_kiocb *req, unsigned int issue_flags) |
e34a02dc DK |
3796 | { |
3797 | struct io_mkdir *mkd = &req->mkdir; | |
3798 | int ret; | |
3799 | ||
3800 | if (issue_flags & IO_URING_F_NONBLOCK) | |
3801 | return -EAGAIN; | |
3802 | ||
3803 | ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode); | |
3804 | ||
3805 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3806 | if (ret < 0) | |
3807 | req_set_fail(req); | |
3808 | io_req_complete(req, ret); | |
3809 | return 0; | |
3810 | } | |
3811 | ||
7a8721f8 DK |
3812 | static int io_symlinkat_prep(struct io_kiocb *req, |
3813 | const struct io_uring_sqe *sqe) | |
3814 | { | |
3815 | struct io_symlink *sl = &req->symlink; | |
3816 | const char __user *oldpath, *newpath; | |
3817 | ||
3818 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3819 | return -EINVAL; | |
3820 | if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index || | |
3821 | sqe->splice_fd_in) | |
3822 | return -EINVAL; | |
3823 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3824 | return -EBADF; | |
3825 | ||
3826 | sl->new_dfd = READ_ONCE(sqe->fd); | |
3827 | oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3828 | newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3829 | ||
3830 | sl->oldpath = getname(oldpath); | |
3831 | if (IS_ERR(sl->oldpath)) | |
3832 | return PTR_ERR(sl->oldpath); | |
3833 | ||
3834 | sl->newpath = getname(newpath); | |
3835 | if (IS_ERR(sl->newpath)) { | |
3836 | putname(sl->oldpath); | |
3837 | return PTR_ERR(sl->newpath); | |
3838 | } | |
3839 | ||
3840 | req->flags |= REQ_F_NEED_CLEANUP; | |
3841 | return 0; | |
3842 | } | |
3843 | ||
04f34081 | 3844 | static int io_symlinkat(struct io_kiocb *req, unsigned int issue_flags) |
7a8721f8 DK |
3845 | { |
3846 | struct io_symlink *sl = &req->symlink; | |
3847 | int ret; | |
3848 | ||
3849 | if (issue_flags & IO_URING_F_NONBLOCK) | |
3850 | return -EAGAIN; | |
3851 | ||
3852 | ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath); | |
3853 | ||
3854 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3855 | if (ret < 0) | |
3856 | req_set_fail(req); | |
3857 | io_req_complete(req, ret); | |
3858 | return 0; | |
3859 | } | |
3860 | ||
cf30da90 DK |
3861 | static int io_linkat_prep(struct io_kiocb *req, |
3862 | const struct io_uring_sqe *sqe) | |
3863 | { | |
3864 | struct io_hardlink *lnk = &req->hardlink; | |
3865 | const char __user *oldf, *newf; | |
3866 | ||
3867 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3868 | return -EINVAL; | |
3869 | if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) | |
3870 | return -EINVAL; | |
3871 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3872 | return -EBADF; | |
3873 | ||
3874 | lnk->old_dfd = READ_ONCE(sqe->fd); | |
3875 | lnk->new_dfd = READ_ONCE(sqe->len); | |
3876 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3877 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3878 | lnk->flags = READ_ONCE(sqe->hardlink_flags); | |
3879 | ||
3880 | lnk->oldpath = getname(oldf); | |
3881 | if (IS_ERR(lnk->oldpath)) | |
3882 | return PTR_ERR(lnk->oldpath); | |
3883 | ||
3884 | lnk->newpath = getname(newf); | |
3885 | if (IS_ERR(lnk->newpath)) { | |
3886 | putname(lnk->oldpath); | |
3887 | return PTR_ERR(lnk->newpath); | |
3888 | } | |
3889 | ||
3890 | req->flags |= REQ_F_NEED_CLEANUP; | |
3891 | return 0; | |
3892 | } | |
3893 | ||
04f34081 | 3894 | static int io_linkat(struct io_kiocb *req, unsigned int issue_flags) |
cf30da90 DK |
3895 | { |
3896 | struct io_hardlink *lnk = &req->hardlink; | |
3897 | int ret; | |
3898 | ||
3899 | if (issue_flags & IO_URING_F_NONBLOCK) | |
3900 | return -EAGAIN; | |
3901 | ||
3902 | ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd, | |
3903 | lnk->newpath, lnk->flags); | |
3904 | ||
3905 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3906 | if (ret < 0) | |
3907 | req_set_fail(req); | |
3908 | io_req_complete(req, ret); | |
3909 | return 0; | |
3910 | } | |
3911 | ||
36f4fa68 JA |
3912 | static int io_shutdown_prep(struct io_kiocb *req, |
3913 | const struct io_uring_sqe *sqe) | |
3914 | { | |
3915 | #if defined(CONFIG_NET) | |
3916 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3917 | return -EINVAL; | |
26578cda PB |
3918 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
3919 | sqe->buf_index || sqe->splice_fd_in)) | |
36f4fa68 JA |
3920 | return -EINVAL; |
3921 | ||
3922 | req->shutdown.how = READ_ONCE(sqe->len); | |
3923 | return 0; | |
3924 | #else | |
3925 | return -EOPNOTSUPP; | |
3926 | #endif | |
3927 | } | |
3928 | ||
45d189c6 | 3929 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
3930 | { |
3931 | #if defined(CONFIG_NET) | |
3932 | struct socket *sock; | |
3933 | int ret; | |
3934 | ||
45d189c6 | 3935 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
3936 | return -EAGAIN; |
3937 | ||
48aba79b | 3938 | sock = sock_from_file(req->file); |
36f4fa68 | 3939 | if (unlikely(!sock)) |
48aba79b | 3940 | return -ENOTSOCK; |
36f4fa68 JA |
3941 | |
3942 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d | 3943 | if (ret < 0) |
93d2bcd2 | 3944 | req_set_fail(req); |
36f4fa68 JA |
3945 | io_req_complete(req, ret); |
3946 | return 0; | |
3947 | #else | |
3948 | return -EOPNOTSUPP; | |
3949 | #endif | |
3950 | } | |
3951 | ||
f2a8d5c7 PB |
3952 | static int __io_splice_prep(struct io_kiocb *req, |
3953 | const struct io_uring_sqe *sqe) | |
7d67af2c | 3954 | { |
fe7e3257 | 3955 | struct io_splice *sp = &req->splice; |
7d67af2c | 3956 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
7d67af2c | 3957 | |
3232dd02 PB |
3958 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3959 | return -EINVAL; | |
7d67af2c PB |
3960 | |
3961 | sp->file_in = NULL; | |
7d67af2c PB |
3962 | sp->len = READ_ONCE(sqe->len); |
3963 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3964 | ||
3965 | if (unlikely(sp->flags & ~valid_flags)) | |
3966 | return -EINVAL; | |
3967 | ||
62906e89 | 3968 | sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in), |
8371adf5 PB |
3969 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
3970 | if (!sp->file_in) | |
3971 | return -EBADF; | |
7d67af2c | 3972 | req->flags |= REQ_F_NEED_CLEANUP; |
7d67af2c PB |
3973 | return 0; |
3974 | } | |
3975 | ||
f2a8d5c7 PB |
3976 | static int io_tee_prep(struct io_kiocb *req, |
3977 | const struct io_uring_sqe *sqe) | |
3978 | { | |
3979 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3980 | return -EINVAL; | |
3981 | return __io_splice_prep(req, sqe); | |
3982 | } | |
3983 | ||
45d189c6 | 3984 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
3985 | { |
3986 | struct io_splice *sp = &req->splice; | |
3987 | struct file *in = sp->file_in; | |
3988 | struct file *out = sp->file_out; | |
3989 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3990 | long ret = 0; | |
3991 | ||
45d189c6 | 3992 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
3993 | return -EAGAIN; |
3994 | if (sp->len) | |
3995 | ret = do_tee(in, out, sp->len, flags); | |
3996 | ||
e1d767f0 PB |
3997 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
3998 | io_put_file(in); | |
f2a8d5c7 PB |
3999 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4000 | ||
f2a8d5c7 | 4001 | if (ret != sp->len) |
93d2bcd2 | 4002 | req_set_fail(req); |
e1e16097 | 4003 | io_req_complete(req, ret); |
f2a8d5c7 PB |
4004 | return 0; |
4005 | } | |
4006 | ||
4007 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4008 | { | |
fe7e3257 | 4009 | struct io_splice *sp = &req->splice; |
f2a8d5c7 PB |
4010 | |
4011 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
4012 | sp->off_out = READ_ONCE(sqe->off); | |
4013 | return __io_splice_prep(req, sqe); | |
4014 | } | |
4015 | ||
45d189c6 | 4016 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
4017 | { |
4018 | struct io_splice *sp = &req->splice; | |
4019 | struct file *in = sp->file_in; | |
4020 | struct file *out = sp->file_out; | |
4021 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
4022 | loff_t *poff_in, *poff_out; | |
c9687426 | 4023 | long ret = 0; |
7d67af2c | 4024 | |
45d189c6 | 4025 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 4026 | return -EAGAIN; |
7d67af2c PB |
4027 | |
4028 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
4029 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 4030 | |
948a7749 | 4031 | if (sp->len) |
c9687426 | 4032 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c | 4033 | |
e1d767f0 PB |
4034 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4035 | io_put_file(in); | |
7d67af2c PB |
4036 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4037 | ||
7d67af2c | 4038 | if (ret != sp->len) |
93d2bcd2 | 4039 | req_set_fail(req); |
e1e16097 | 4040 | io_req_complete(req, ret); |
7d67af2c PB |
4041 | return 0; |
4042 | } | |
4043 | ||
2b188cc1 JA |
4044 | /* |
4045 | * IORING_OP_NOP just posts a completion event, nothing else. | |
4046 | */ | |
889fca73 | 4047 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
4048 | { |
4049 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 4050 | |
def596e9 JA |
4051 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4052 | return -EINVAL; | |
4053 | ||
889fca73 | 4054 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
4055 | return 0; |
4056 | } | |
4057 | ||
1155c76a | 4058 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 4059 | { |
6b06314c | 4060 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 4061 | |
09bb8394 JA |
4062 | if (!req->file) |
4063 | return -EBADF; | |
c992fe29 | 4064 | |
6b06314c | 4065 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 4066 | return -EINVAL; |
26578cda PB |
4067 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4068 | sqe->splice_fd_in)) | |
c992fe29 CH |
4069 | return -EINVAL; |
4070 | ||
8ed8d3c3 JA |
4071 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
4072 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
4073 | return -EINVAL; | |
4074 | ||
4075 | req->sync.off = READ_ONCE(sqe->off); | |
4076 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
4077 | return 0; |
4078 | } | |
4079 | ||
45d189c6 | 4080 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4081 | { |
8ed8d3c3 | 4082 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
4083 | int ret; |
4084 | ||
ac45abc0 | 4085 | /* fsync always requires a blocking context */ |
45d189c6 | 4086 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4087 | return -EAGAIN; |
4088 | ||
9adbd45d | 4089 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
4090 | end > 0 ? end : LLONG_MAX, |
4091 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
4092 | if (ret < 0) | |
93d2bcd2 | 4093 | req_set_fail(req); |
e1e16097 | 4094 | io_req_complete(req, ret); |
c992fe29 CH |
4095 | return 0; |
4096 | } | |
4097 | ||
d63d1b5e JA |
4098 | static int io_fallocate_prep(struct io_kiocb *req, |
4099 | const struct io_uring_sqe *sqe) | |
4100 | { | |
26578cda PB |
4101 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
4102 | sqe->splice_fd_in) | |
d63d1b5e | 4103 | return -EINVAL; |
3232dd02 PB |
4104 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4105 | return -EINVAL; | |
d63d1b5e JA |
4106 | |
4107 | req->sync.off = READ_ONCE(sqe->off); | |
4108 | req->sync.len = READ_ONCE(sqe->addr); | |
4109 | req->sync.mode = READ_ONCE(sqe->len); | |
4110 | return 0; | |
4111 | } | |
4112 | ||
45d189c6 | 4113 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 4114 | { |
ac45abc0 PB |
4115 | int ret; |
4116 | ||
d63d1b5e | 4117 | /* fallocate always requiring blocking context */ |
45d189c6 | 4118 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 4119 | return -EAGAIN; |
ac45abc0 PB |
4120 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
4121 | req->sync.len); | |
ac45abc0 | 4122 | if (ret < 0) |
93d2bcd2 | 4123 | req_set_fail(req); |
e1e16097 | 4124 | io_req_complete(req, ret); |
5d17b4a4 JA |
4125 | return 0; |
4126 | } | |
4127 | ||
ec65fea5 | 4128 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 4129 | { |
f8748881 | 4130 | const char __user *fname; |
15b71abe | 4131 | int ret; |
b7bb4f7d | 4132 | |
d3fddf6d PB |
4133 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4134 | return -EINVAL; | |
b9445598 | 4135 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 4136 | return -EINVAL; |
ec65fea5 | 4137 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 4138 | return -EBADF; |
03b1230c | 4139 | |
ec65fea5 PB |
4140 | /* open.how should be already initialised */ |
4141 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 4142 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 4143 | |
25e72d10 PB |
4144 | req->open.dfd = READ_ONCE(sqe->fd); |
4145 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 4146 | req->open.filename = getname(fname); |
15b71abe JA |
4147 | if (IS_ERR(req->open.filename)) { |
4148 | ret = PTR_ERR(req->open.filename); | |
4149 | req->open.filename = NULL; | |
4150 | return ret; | |
4151 | } | |
b9445598 PB |
4152 | |
4153 | req->open.file_slot = READ_ONCE(sqe->file_index); | |
4154 | if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) | |
4155 | return -EINVAL; | |
4156 | ||
4022e7af | 4157 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 4158 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 4159 | return 0; |
03b1230c JA |
4160 | } |
4161 | ||
ec65fea5 PB |
4162 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4163 | { | |
d3fddf6d PB |
4164 | u64 mode = READ_ONCE(sqe->len); |
4165 | u64 flags = READ_ONCE(sqe->open_flags); | |
ec65fea5 | 4166 | |
ec65fea5 PB |
4167 | req->open.how = build_open_how(flags, mode); |
4168 | return __io_openat_prep(req, sqe); | |
4169 | } | |
4170 | ||
cebdb986 | 4171 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 4172 | { |
cebdb986 | 4173 | struct open_how __user *how; |
cebdb986 | 4174 | size_t len; |
0fa03c62 JA |
4175 | int ret; |
4176 | ||
cebdb986 JA |
4177 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4178 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
4179 | if (len < OPEN_HOW_SIZE_VER0) |
4180 | return -EINVAL; | |
3529d8c2 | 4181 | |
cebdb986 JA |
4182 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
4183 | len); | |
4184 | if (ret) | |
4185 | return ret; | |
3529d8c2 | 4186 | |
ec65fea5 | 4187 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
4188 | } |
4189 | ||
45d189c6 | 4190 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
4191 | { |
4192 | struct open_flags op; | |
15b71abe | 4193 | struct file *file; |
b9445598 PB |
4194 | bool resolve_nonblock, nonblock_set; |
4195 | bool fixed = !!req->open.file_slot; | |
15b71abe JA |
4196 | int ret; |
4197 | ||
cebdb986 | 4198 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
4199 | if (ret) |
4200 | goto err; | |
3a81fd02 JA |
4201 | nonblock_set = op.open_flag & O_NONBLOCK; |
4202 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 4203 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
4204 | /* |
4205 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
4206 | * it'll always -EAGAIN | |
4207 | */ | |
4208 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
4209 | return -EAGAIN; | |
4210 | op.lookup_flags |= LOOKUP_CACHED; | |
4211 | op.open_flag |= O_NONBLOCK; | |
4212 | } | |
15b71abe | 4213 | |
b9445598 PB |
4214 | if (!fixed) { |
4215 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); | |
4216 | if (ret < 0) | |
4217 | goto err; | |
4218 | } | |
15b71abe JA |
4219 | |
4220 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
12dcb58a | 4221 | if (IS_ERR(file)) { |
944d1444 | 4222 | /* |
12dcb58a PB |
4223 | * We could hang on to this 'fd' on retrying, but seems like |
4224 | * marginal gain for something that is now known to be a slower | |
4225 | * path. So just put it, and we'll get a new one when we retry. | |
944d1444 | 4226 | */ |
b9445598 PB |
4227 | if (!fixed) |
4228 | put_unused_fd(ret); | |
3a81fd02 | 4229 | |
15b71abe | 4230 | ret = PTR_ERR(file); |
12dcb58a PB |
4231 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
4232 | if (ret == -EAGAIN && | |
4233 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) | |
4234 | return -EAGAIN; | |
4235 | goto err; | |
15b71abe | 4236 | } |
12dcb58a PB |
4237 | |
4238 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) | |
4239 | file->f_flags &= ~O_NONBLOCK; | |
4240 | fsnotify_open(file); | |
b9445598 PB |
4241 | |
4242 | if (!fixed) | |
4243 | fd_install(ret, file); | |
4244 | else | |
4245 | ret = io_install_fixed_file(req, file, issue_flags, | |
4246 | req->open.file_slot - 1); | |
15b71abe JA |
4247 | err: |
4248 | putname(req->open.filename); | |
8fef80bf | 4249 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe | 4250 | if (ret < 0) |
93d2bcd2 | 4251 | req_set_fail(req); |
0bdf3398 | 4252 | __io_req_complete(req, issue_flags, ret, 0); |
15b71abe JA |
4253 | return 0; |
4254 | } | |
4255 | ||
45d189c6 | 4256 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 4257 | { |
e45cff58 | 4258 | return io_openat2(req, issue_flags); |
cebdb986 JA |
4259 | } |
4260 | ||
067524e9 JA |
4261 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4262 | const struct io_uring_sqe *sqe) | |
4263 | { | |
4264 | struct io_provide_buf *p = &req->pbuf; | |
4265 | u64 tmp; | |
4266 | ||
26578cda PB |
4267 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
4268 | sqe->splice_fd_in) | |
067524e9 JA |
4269 | return -EINVAL; |
4270 | ||
4271 | tmp = READ_ONCE(sqe->fd); | |
4272 | if (!tmp || tmp > USHRT_MAX) | |
4273 | return -EINVAL; | |
4274 | ||
4275 | memset(p, 0, sizeof(*p)); | |
4276 | p->nbufs = tmp; | |
4277 | p->bgid = READ_ONCE(sqe->buf_group); | |
4278 | return 0; | |
4279 | } | |
4280 | ||
4281 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
4282 | int bgid, unsigned nbufs) | |
4283 | { | |
4284 | unsigned i = 0; | |
4285 | ||
4286 | /* shouldn't happen */ | |
4287 | if (!nbufs) | |
4288 | return 0; | |
4289 | ||
4290 | /* the head kbuf is the list itself */ | |
4291 | while (!list_empty(&buf->list)) { | |
4292 | struct io_buffer *nxt; | |
4293 | ||
4294 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
4295 | list_del(&nxt->list); | |
4296 | kfree(nxt); | |
4297 | if (++i == nbufs) | |
4298 | return i; | |
4299 | } | |
4300 | i++; | |
4301 | kfree(buf); | |
9e15c3a0 | 4302 | xa_erase(&ctx->io_buffers, bgid); |
067524e9 JA |
4303 | |
4304 | return i; | |
4305 | } | |
4306 | ||
889fca73 | 4307 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
4308 | { |
4309 | struct io_provide_buf *p = &req->pbuf; | |
4310 | struct io_ring_ctx *ctx = req->ctx; | |
4311 | struct io_buffer *head; | |
4312 | int ret = 0; | |
3b44b371 | 4313 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
067524e9 | 4314 | |
3b44b371 | 4315 | io_ring_submit_lock(ctx, needs_lock); |
067524e9 JA |
4316 | |
4317 | lockdep_assert_held(&ctx->uring_lock); | |
4318 | ||
4319 | ret = -ENOENT; | |
9e15c3a0 | 4320 | head = xa_load(&ctx->io_buffers, p->bgid); |
067524e9 JA |
4321 | if (head) |
4322 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 | 4323 | if (ret < 0) |
93d2bcd2 | 4324 | req_set_fail(req); |
067524e9 | 4325 | |
9fb8cb49 PB |
4326 | /* complete before unlock, IOPOLL may need the lock */ |
4327 | __io_req_complete(req, issue_flags, ret, 0); | |
3b44b371 | 4328 | io_ring_submit_unlock(ctx, needs_lock); |
067524e9 JA |
4329 | return 0; |
4330 | } | |
4331 | ||
ddf0322d JA |
4332 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4333 | const struct io_uring_sqe *sqe) | |
4334 | { | |
38134ada | 4335 | unsigned long size, tmp_check; |
ddf0322d JA |
4336 | struct io_provide_buf *p = &req->pbuf; |
4337 | u64 tmp; | |
4338 | ||
26578cda | 4339 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
ddf0322d JA |
4340 | return -EINVAL; |
4341 | ||
4342 | tmp = READ_ONCE(sqe->fd); | |
4343 | if (!tmp || tmp > USHRT_MAX) | |
4344 | return -E2BIG; | |
4345 | p->nbufs = tmp; | |
4346 | p->addr = READ_ONCE(sqe->addr); | |
4347 | p->len = READ_ONCE(sqe->len); | |
4348 | ||
38134ada PB |
4349 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
4350 | &size)) | |
4351 | return -EOVERFLOW; | |
4352 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) | |
4353 | return -EOVERFLOW; | |
4354 | ||
d81269fe PB |
4355 | size = (unsigned long)p->len * p->nbufs; |
4356 | if (!access_ok(u64_to_user_ptr(p->addr), size)) | |
ddf0322d JA |
4357 | return -EFAULT; |
4358 | ||
4359 | p->bgid = READ_ONCE(sqe->buf_group); | |
4360 | tmp = READ_ONCE(sqe->off); | |
4361 | if (tmp > USHRT_MAX) | |
4362 | return -E2BIG; | |
4363 | p->bid = tmp; | |
4364 | return 0; | |
4365 | } | |
4366 | ||
4367 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
4368 | { | |
4369 | struct io_buffer *buf; | |
4370 | u64 addr = pbuf->addr; | |
4371 | int i, bid = pbuf->bid; | |
4372 | ||
4373 | for (i = 0; i < pbuf->nbufs; i++) { | |
9990da93 | 4374 | buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT); |
ddf0322d JA |
4375 | if (!buf) |
4376 | break; | |
4377 | ||
4378 | buf->addr = addr; | |
d1f82808 | 4379 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
ddf0322d JA |
4380 | buf->bid = bid; |
4381 | addr += pbuf->len; | |
4382 | bid++; | |
4383 | if (!*head) { | |
4384 | INIT_LIST_HEAD(&buf->list); | |
4385 | *head = buf; | |
4386 | } else { | |
4387 | list_add_tail(&buf->list, &(*head)->list); | |
4388 | } | |
4389 | } | |
4390 | ||
4391 | return i ? i : -ENOMEM; | |
4392 | } | |
4393 | ||
889fca73 | 4394 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4395 | { |
4396 | struct io_provide_buf *p = &req->pbuf; | |
4397 | struct io_ring_ctx *ctx = req->ctx; | |
4398 | struct io_buffer *head, *list; | |
4399 | int ret = 0; | |
3b44b371 | 4400 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
ddf0322d | 4401 | |
3b44b371 | 4402 | io_ring_submit_lock(ctx, needs_lock); |
ddf0322d JA |
4403 | |
4404 | lockdep_assert_held(&ctx->uring_lock); | |
4405 | ||
9e15c3a0 | 4406 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
ddf0322d JA |
4407 | |
4408 | ret = io_add_buffers(p, &head); | |
9e15c3a0 JA |
4409 | if (ret >= 0 && !list) { |
4410 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); | |
4411 | if (ret < 0) | |
067524e9 | 4412 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d | 4413 | } |
ddf0322d | 4414 | if (ret < 0) |
93d2bcd2 | 4415 | req_set_fail(req); |
9fb8cb49 PB |
4416 | /* complete before unlock, IOPOLL may need the lock */ |
4417 | __io_req_complete(req, issue_flags, ret, 0); | |
3b44b371 | 4418 | io_ring_submit_unlock(ctx, needs_lock); |
ddf0322d | 4419 | return 0; |
cebdb986 JA |
4420 | } |
4421 | ||
3e4827b0 JA |
4422 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4423 | const struct io_uring_sqe *sqe) | |
4424 | { | |
4425 | #if defined(CONFIG_EPOLL) | |
26578cda | 4426 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
3e4827b0 | 4427 | return -EINVAL; |
2d74d042 | 4428 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4429 | return -EINVAL; |
3e4827b0 JA |
4430 | |
4431 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4432 | req->epoll.op = READ_ONCE(sqe->len); | |
4433 | req->epoll.fd = READ_ONCE(sqe->off); | |
4434 | ||
4435 | if (ep_op_has_event(req->epoll.op)) { | |
4436 | struct epoll_event __user *ev; | |
4437 | ||
4438 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4439 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4440 | return -EFAULT; | |
4441 | } | |
4442 | ||
4443 | return 0; | |
4444 | #else | |
4445 | return -EOPNOTSUPP; | |
4446 | #endif | |
4447 | } | |
4448 | ||
889fca73 | 4449 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4450 | { |
4451 | #if defined(CONFIG_EPOLL) | |
4452 | struct io_epoll *ie = &req->epoll; | |
4453 | int ret; | |
45d189c6 | 4454 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4455 | |
4456 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4457 | if (force_nonblock && ret == -EAGAIN) | |
4458 | return -EAGAIN; | |
4459 | ||
4460 | if (ret < 0) | |
93d2bcd2 | 4461 | req_set_fail(req); |
889fca73 | 4462 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4463 | return 0; |
4464 | #else | |
4465 | return -EOPNOTSUPP; | |
4466 | #endif | |
4467 | } | |
4468 | ||
c1ca757b JA |
4469 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4470 | { | |
4471 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
26578cda | 4472 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
c1ca757b | 4473 | return -EINVAL; |
3232dd02 PB |
4474 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4475 | return -EINVAL; | |
c1ca757b JA |
4476 | |
4477 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4478 | req->madvise.len = READ_ONCE(sqe->len); | |
4479 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4480 | return 0; | |
4481 | #else | |
4482 | return -EOPNOTSUPP; | |
4483 | #endif | |
4484 | } | |
4485 | ||
45d189c6 | 4486 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4487 | { |
4488 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4489 | struct io_madvise *ma = &req->madvise; | |
4490 | int ret; | |
4491 | ||
45d189c6 | 4492 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4493 | return -EAGAIN; |
4494 | ||
0726b01e | 4495 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b | 4496 | if (ret < 0) |
93d2bcd2 | 4497 | req_set_fail(req); |
e1e16097 | 4498 | io_req_complete(req, ret); |
c1ca757b JA |
4499 | return 0; |
4500 | #else | |
4501 | return -EOPNOTSUPP; | |
4502 | #endif | |
4503 | } | |
4504 | ||
4840e418 JA |
4505 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4506 | { | |
26578cda | 4507 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
4840e418 | 4508 | return -EINVAL; |
3232dd02 PB |
4509 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4510 | return -EINVAL; | |
4840e418 JA |
4511 | |
4512 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4513 | req->fadvise.len = READ_ONCE(sqe->len); | |
4514 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4515 | return 0; | |
4516 | } | |
4517 | ||
45d189c6 | 4518 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4519 | { |
4520 | struct io_fadvise *fa = &req->fadvise; | |
4521 | int ret; | |
4522 | ||
45d189c6 | 4523 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4524 | switch (fa->advice) { |
4525 | case POSIX_FADV_NORMAL: | |
4526 | case POSIX_FADV_RANDOM: | |
4527 | case POSIX_FADV_SEQUENTIAL: | |
4528 | break; | |
4529 | default: | |
4530 | return -EAGAIN; | |
4531 | } | |
4532 | } | |
4840e418 JA |
4533 | |
4534 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4535 | if (ret < 0) | |
93d2bcd2 | 4536 | req_set_fail(req); |
0bdf3398 | 4537 | __io_req_complete(req, issue_flags, ret, 0); |
4840e418 JA |
4538 | return 0; |
4539 | } | |
4540 | ||
eddc7ef5 JA |
4541 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4542 | { | |
2d74d042 | 4543 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4544 | return -EINVAL; |
26578cda | 4545 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
eddc7ef5 | 4546 | return -EINVAL; |
9c280f90 | 4547 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4548 | return -EBADF; |
eddc7ef5 | 4549 | |
1d9e1288 BM |
4550 | req->statx.dfd = READ_ONCE(sqe->fd); |
4551 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4552 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4553 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4554 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4555 | |
4556 | return 0; | |
4557 | } | |
4558 | ||
45d189c6 | 4559 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4560 | { |
1d9e1288 | 4561 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4562 | int ret; |
4563 | ||
59d70013 | 4564 | if (issue_flags & IO_URING_F_NONBLOCK) |
eddc7ef5 JA |
4565 | return -EAGAIN; |
4566 | ||
e62753e4 BM |
4567 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4568 | ctx->buffer); | |
eddc7ef5 | 4569 | |
eddc7ef5 | 4570 | if (ret < 0) |
93d2bcd2 | 4571 | req_set_fail(req); |
e1e16097 | 4572 | io_req_complete(req, ret); |
eddc7ef5 JA |
4573 | return 0; |
4574 | } | |
4575 | ||
b5dba59e JA |
4576 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4577 | { | |
14587a46 | 4578 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4579 | return -EINVAL; |
b5dba59e | 4580 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
7df778be | 4581 | sqe->rw_flags || sqe->buf_index) |
b5dba59e | 4582 | return -EINVAL; |
9c280f90 | 4583 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4584 | return -EBADF; |
b5dba59e JA |
4585 | |
4586 | req->close.fd = READ_ONCE(sqe->fd); | |
7df778be PB |
4587 | req->close.file_slot = READ_ONCE(sqe->file_index); |
4588 | if (req->close.file_slot && req->close.fd) | |
4589 | return -EINVAL; | |
4590 | ||
b5dba59e | 4591 | return 0; |
b5dba59e JA |
4592 | } |
4593 | ||
889fca73 | 4594 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4595 | { |
9eac1904 | 4596 | struct files_struct *files = current->files; |
3af73b28 | 4597 | struct io_close *close = &req->close; |
9eac1904 | 4598 | struct fdtable *fdt; |
a1fde923 PB |
4599 | struct file *file = NULL; |
4600 | int ret = -EBADF; | |
b5dba59e | 4601 | |
7df778be PB |
4602 | if (req->close.file_slot) { |
4603 | ret = io_close_fixed(req, issue_flags); | |
4604 | goto err; | |
4605 | } | |
4606 | ||
9eac1904 JA |
4607 | spin_lock(&files->file_lock); |
4608 | fdt = files_fdtable(files); | |
4609 | if (close->fd >= fdt->max_fds) { | |
4610 | spin_unlock(&files->file_lock); | |
4611 | goto err; | |
4612 | } | |
4613 | file = fdt->fd[close->fd]; | |
a1fde923 | 4614 | if (!file || file->f_op == &io_uring_fops) { |
9eac1904 JA |
4615 | spin_unlock(&files->file_lock); |
4616 | file = NULL; | |
4617 | goto err; | |
3af73b28 | 4618 | } |
b5dba59e JA |
4619 | |
4620 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 4621 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 4622 | spin_unlock(&files->file_lock); |
0bf0eefd | 4623 | return -EAGAIN; |
a2100672 | 4624 | } |
b5dba59e | 4625 | |
9eac1904 JA |
4626 | ret = __close_fd_get_file(close->fd, &file); |
4627 | spin_unlock(&files->file_lock); | |
4628 | if (ret < 0) { | |
4629 | if (ret == -ENOENT) | |
4630 | ret = -EBADF; | |
4631 | goto err; | |
4632 | } | |
4633 | ||
3af73b28 | 4634 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4635 | ret = filp_close(file, current->files); |
4636 | err: | |
3af73b28 | 4637 | if (ret < 0) |
93d2bcd2 | 4638 | req_set_fail(req); |
9eac1904 JA |
4639 | if (file) |
4640 | fput(file); | |
889fca73 | 4641 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 4642 | return 0; |
b5dba59e JA |
4643 | } |
4644 | ||
1155c76a | 4645 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4646 | { |
4647 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 4648 | |
5d17b4a4 JA |
4649 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4650 | return -EINVAL; | |
26578cda PB |
4651 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4652 | sqe->splice_fd_in)) | |
5d17b4a4 JA |
4653 | return -EINVAL; |
4654 | ||
8ed8d3c3 JA |
4655 | req->sync.off = READ_ONCE(sqe->off); |
4656 | req->sync.len = READ_ONCE(sqe->len); | |
4657 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4658 | return 0; |
4659 | } | |
4660 | ||
45d189c6 | 4661 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4662 | { |
8ed8d3c3 JA |
4663 | int ret; |
4664 | ||
ac45abc0 | 4665 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 4666 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4667 | return -EAGAIN; |
4668 | ||
9adbd45d | 4669 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4670 | req->sync.flags); |
4671 | if (ret < 0) | |
93d2bcd2 | 4672 | req_set_fail(req); |
e1e16097 | 4673 | io_req_complete(req, ret); |
5d17b4a4 JA |
4674 | return 0; |
4675 | } | |
4676 | ||
469956e8 | 4677 | #if defined(CONFIG_NET) |
02d27d89 PB |
4678 | static int io_setup_async_msg(struct io_kiocb *req, |
4679 | struct io_async_msghdr *kmsg) | |
4680 | { | |
e8c2bc1f JA |
4681 | struct io_async_msghdr *async_msg = req->async_data; |
4682 | ||
4683 | if (async_msg) | |
02d27d89 | 4684 | return -EAGAIN; |
e8c2bc1f | 4685 | if (io_alloc_async_data(req)) { |
257e84a5 | 4686 | kfree(kmsg->free_iov); |
02d27d89 PB |
4687 | return -ENOMEM; |
4688 | } | |
e8c2bc1f | 4689 | async_msg = req->async_data; |
02d27d89 | 4690 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4691 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 4692 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
4693 | /* if were using fast_iov, set it to the new one */ |
4694 | if (!async_msg->free_iov) | |
4695 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
4696 | ||
02d27d89 PB |
4697 | return -EAGAIN; |
4698 | } | |
4699 | ||
2ae523ed PB |
4700 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4701 | struct io_async_msghdr *iomsg) | |
4702 | { | |
2ae523ed | 4703 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 4704 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 4705 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 4706 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
4707 | } |
4708 | ||
93642ef8 PB |
4709 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
4710 | { | |
4711 | int ret; | |
4712 | ||
93642ef8 PB |
4713 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
4714 | if (!ret) | |
4715 | req->flags |= REQ_F_NEED_CLEANUP; | |
4716 | return ret; | |
4717 | } | |
4718 | ||
3529d8c2 | 4719 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4720 | { |
e47293fd | 4721 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 4722 | |
d2b6f48b PB |
4723 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4724 | return -EINVAL; | |
4725 | ||
270a5940 | 4726 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4727 | sr->len = READ_ONCE(sqe->len); |
04411806 PB |
4728 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4729 | if (sr->msg_flags & MSG_DONTWAIT) | |
4730 | req->flags |= REQ_F_NOWAIT; | |
3529d8c2 | 4731 | |
d8768362 JA |
4732 | #ifdef CONFIG_COMPAT |
4733 | if (req->ctx->compat) | |
4734 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4735 | #endif | |
93642ef8 | 4736 | return 0; |
03b1230c JA |
4737 | } |
4738 | ||
889fca73 | 4739 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4740 | { |
6b754c8b | 4741 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4742 | struct socket *sock; |
7a7cacba | 4743 | unsigned flags; |
0031275d | 4744 | int min_ret = 0; |
0fa03c62 JA |
4745 | int ret; |
4746 | ||
dba4a925 | 4747 | sock = sock_from_file(req->file); |
7a7cacba | 4748 | if (unlikely(!sock)) |
dba4a925 | 4749 | return -ENOTSOCK; |
3529d8c2 | 4750 | |
d886e185 PB |
4751 | if (req_has_async_data(req)) { |
4752 | kmsg = req->async_data; | |
4753 | } else { | |
7a7cacba PB |
4754 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
4755 | if (ret) | |
4756 | return ret; | |
4757 | kmsg = &iomsg; | |
0fa03c62 | 4758 | } |
0fa03c62 | 4759 | |
04411806 PB |
4760 | flags = req->sr_msg.msg_flags; |
4761 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4762 | flags |= MSG_DONTWAIT; |
0031275d SM |
4763 | if (flags & MSG_WAITALL) |
4764 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4765 | ||
7a7cacba | 4766 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
45d189c6 | 4767 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4768 | return io_setup_async_msg(req, kmsg); |
4769 | if (ret == -ERESTARTSYS) | |
4770 | ret = -EINTR; | |
0fa03c62 | 4771 | |
257e84a5 PB |
4772 | /* fast path, check for non-NULL to avoid function call */ |
4773 | if (kmsg->free_iov) | |
4774 | kfree(kmsg->free_iov); | |
99bc4c38 | 4775 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 4776 | if (ret < min_ret) |
93d2bcd2 | 4777 | req_set_fail(req); |
889fca73 | 4778 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 4779 | return 0; |
03b1230c | 4780 | } |
aa1fa28f | 4781 | |
889fca73 | 4782 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4783 | { |
7a7cacba PB |
4784 | struct io_sr_msg *sr = &req->sr_msg; |
4785 | struct msghdr msg; | |
4786 | struct iovec iov; | |
fddaface | 4787 | struct socket *sock; |
7a7cacba | 4788 | unsigned flags; |
0031275d | 4789 | int min_ret = 0; |
fddaface JA |
4790 | int ret; |
4791 | ||
dba4a925 | 4792 | sock = sock_from_file(req->file); |
7a7cacba | 4793 | if (unlikely(!sock)) |
dba4a925 | 4794 | return -ENOTSOCK; |
fddaface | 4795 | |
7a7cacba PB |
4796 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4797 | if (unlikely(ret)) | |
14db8411 | 4798 | return ret; |
fddaface | 4799 | |
7a7cacba PB |
4800 | msg.msg_name = NULL; |
4801 | msg.msg_control = NULL; | |
4802 | msg.msg_controllen = 0; | |
4803 | msg.msg_namelen = 0; | |
fddaface | 4804 | |
04411806 PB |
4805 | flags = req->sr_msg.msg_flags; |
4806 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4807 | flags |= MSG_DONTWAIT; |
0031275d SM |
4808 | if (flags & MSG_WAITALL) |
4809 | min_ret = iov_iter_count(&msg.msg_iter); | |
4810 | ||
7a7cacba PB |
4811 | msg.msg_flags = flags; |
4812 | ret = sock_sendmsg(sock, &msg); | |
45d189c6 | 4813 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4814 | return -EAGAIN; |
4815 | if (ret == -ERESTARTSYS) | |
4816 | ret = -EINTR; | |
fddaface | 4817 | |
0031275d | 4818 | if (ret < min_ret) |
93d2bcd2 | 4819 | req_set_fail(req); |
889fca73 | 4820 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 4821 | return 0; |
fddaface JA |
4822 | } |
4823 | ||
1400e697 PB |
4824 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4825 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4826 | { |
4827 | struct io_sr_msg *sr = &req->sr_msg; | |
4828 | struct iovec __user *uiov; | |
4829 | size_t iov_len; | |
4830 | int ret; | |
4831 | ||
1400e697 PB |
4832 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4833 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4834 | if (ret) |
4835 | return ret; | |
4836 | ||
4837 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4838 | if (iov_len > 1) | |
4839 | return -EINVAL; | |
5476dfed | 4840 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4841 | return -EFAULT; |
5476dfed | 4842 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 4843 | iomsg->free_iov = NULL; |
52de1fe1 | 4844 | } else { |
257e84a5 | 4845 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4846 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 4847 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 4848 | false); |
52de1fe1 JA |
4849 | if (ret > 0) |
4850 | ret = 0; | |
4851 | } | |
4852 | ||
4853 | return ret; | |
4854 | } | |
4855 | ||
4856 | #ifdef CONFIG_COMPAT | |
4857 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4858 | struct io_async_msghdr *iomsg) |
52de1fe1 | 4859 | { |
52de1fe1 JA |
4860 | struct io_sr_msg *sr = &req->sr_msg; |
4861 | struct compat_iovec __user *uiov; | |
4862 | compat_uptr_t ptr; | |
4863 | compat_size_t len; | |
4864 | int ret; | |
4865 | ||
4af3417a PB |
4866 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
4867 | &ptr, &len); | |
52de1fe1 JA |
4868 | if (ret) |
4869 | return ret; | |
4870 | ||
4871 | uiov = compat_ptr(ptr); | |
4872 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4873 | compat_ssize_t clen; | |
4874 | ||
4875 | if (len > 1) | |
4876 | return -EINVAL; | |
4877 | if (!access_ok(uiov, sizeof(*uiov))) | |
4878 | return -EFAULT; | |
4879 | if (__get_user(clen, &uiov->iov_len)) | |
4880 | return -EFAULT; | |
4881 | if (clen < 0) | |
4882 | return -EINVAL; | |
2d280bc8 | 4883 | sr->len = clen; |
257e84a5 | 4884 | iomsg->free_iov = NULL; |
52de1fe1 | 4885 | } else { |
257e84a5 | 4886 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4887 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 4888 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 4889 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
4890 | if (ret < 0) |
4891 | return ret; | |
4892 | } | |
4893 | ||
4894 | return 0; | |
4895 | } | |
4896 | #endif | |
4897 | ||
1400e697 PB |
4898 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4899 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4900 | { |
1400e697 | 4901 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
4902 | |
4903 | #ifdef CONFIG_COMPAT | |
4904 | if (req->ctx->compat) | |
1400e697 | 4905 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4906 | #endif |
52de1fe1 | 4907 | |
1400e697 | 4908 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4909 | } |
4910 | ||
bcda7baa | 4911 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
51aac424 | 4912 | unsigned int issue_flags) |
bcda7baa JA |
4913 | { |
4914 | struct io_sr_msg *sr = &req->sr_msg; | |
bcda7baa | 4915 | |
51aac424 | 4916 | return io_buffer_select(req, &sr->len, sr->bgid, issue_flags); |
fddaface JA |
4917 | } |
4918 | ||
7fbb1b54 PB |
4919 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4920 | { | |
30d51dd4 | 4921 | return io_put_kbuf(req, req->kbuf); |
7fbb1b54 PB |
4922 | } |
4923 | ||
93642ef8 | 4924 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 4925 | { |
99bc4c38 | 4926 | int ret; |
3529d8c2 | 4927 | |
93642ef8 PB |
4928 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
4929 | if (!ret) | |
4930 | req->flags |= REQ_F_NEED_CLEANUP; | |
4931 | return ret; | |
4932 | } | |
4933 | ||
4934 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4935 | { | |
4936 | struct io_sr_msg *sr = &req->sr_msg; | |
4937 | ||
d2b6f48b PB |
4938 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4939 | return -EINVAL; | |
4940 | ||
270a5940 | 4941 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4942 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4943 | sr->bgid = READ_ONCE(sqe->buf_group); |
04411806 PB |
4944 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4945 | if (sr->msg_flags & MSG_DONTWAIT) | |
4946 | req->flags |= REQ_F_NOWAIT; | |
06b76d44 | 4947 | |
d8768362 JA |
4948 | #ifdef CONFIG_COMPAT |
4949 | if (req->ctx->compat) | |
4950 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4951 | #endif | |
93642ef8 | 4952 | return 0; |
aa1fa28f JA |
4953 | } |
4954 | ||
889fca73 | 4955 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4956 | { |
6b754c8b | 4957 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4958 | struct socket *sock; |
7fbb1b54 | 4959 | struct io_buffer *kbuf; |
7a7cacba | 4960 | unsigned flags; |
0031275d | 4961 | int min_ret = 0; |
52de1fe1 | 4962 | int ret, cflags = 0; |
45d189c6 | 4963 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 4964 | |
dba4a925 | 4965 | sock = sock_from_file(req->file); |
7a7cacba | 4966 | if (unlikely(!sock)) |
dba4a925 | 4967 | return -ENOTSOCK; |
3529d8c2 | 4968 | |
d886e185 PB |
4969 | if (req_has_async_data(req)) { |
4970 | kmsg = req->async_data; | |
4971 | } else { | |
7a7cacba PB |
4972 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
4973 | if (ret) | |
681fda8d | 4974 | return ret; |
7a7cacba PB |
4975 | kmsg = &iomsg; |
4976 | } | |
03b1230c | 4977 | |
bc02ef33 | 4978 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 4979 | kbuf = io_recv_buffer_select(req, issue_flags); |
bc02ef33 | 4980 | if (IS_ERR(kbuf)) |
52de1fe1 | 4981 | return PTR_ERR(kbuf); |
7a7cacba | 4982 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
4983 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
4984 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
4985 | 1, req->sr_msg.len); |
4986 | } | |
52de1fe1 | 4987 | |
04411806 PB |
4988 | flags = req->sr_msg.msg_flags; |
4989 | if (force_nonblock) | |
7a7cacba | 4990 | flags |= MSG_DONTWAIT; |
0031275d SM |
4991 | if (flags & MSG_WAITALL) |
4992 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4993 | ||
7a7cacba PB |
4994 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4995 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4996 | if (force_nonblock && ret == -EAGAIN) |
4997 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4998 | if (ret == -ERESTARTSYS) |
4999 | ret = -EINTR; | |
03b1230c | 5000 | |
7fbb1b54 PB |
5001 | if (req->flags & REQ_F_BUFFER_SELECTED) |
5002 | cflags = io_put_recv_kbuf(req); | |
257e84a5 PB |
5003 | /* fast path, check for non-NULL to avoid function call */ |
5004 | if (kmsg->free_iov) | |
5005 | kfree(kmsg->free_iov); | |
99bc4c38 | 5006 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 5007 | if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
93d2bcd2 | 5008 | req_set_fail(req); |
889fca73 | 5009 | __io_req_complete(req, issue_flags, ret, cflags); |
03b1230c | 5010 | return 0; |
0fa03c62 | 5011 | } |
5d17b4a4 | 5012 | |
889fca73 | 5013 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 5014 | { |
6b754c8b | 5015 | struct io_buffer *kbuf; |
7a7cacba PB |
5016 | struct io_sr_msg *sr = &req->sr_msg; |
5017 | struct msghdr msg; | |
5018 | void __user *buf = sr->buf; | |
fddaface | 5019 | struct socket *sock; |
7a7cacba PB |
5020 | struct iovec iov; |
5021 | unsigned flags; | |
0031275d | 5022 | int min_ret = 0; |
bcda7baa | 5023 | int ret, cflags = 0; |
45d189c6 | 5024 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 5025 | |
dba4a925 | 5026 | sock = sock_from_file(req->file); |
7a7cacba | 5027 | if (unlikely(!sock)) |
dba4a925 | 5028 | return -ENOTSOCK; |
fddaface | 5029 | |
bc02ef33 | 5030 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 5031 | kbuf = io_recv_buffer_select(req, issue_flags); |
bcda7baa JA |
5032 | if (IS_ERR(kbuf)) |
5033 | return PTR_ERR(kbuf); | |
7a7cacba | 5034 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 5035 | } |
bcda7baa | 5036 | |
7a7cacba | 5037 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
5038 | if (unlikely(ret)) |
5039 | goto out_free; | |
fddaface | 5040 | |
7a7cacba PB |
5041 | msg.msg_name = NULL; |
5042 | msg.msg_control = NULL; | |
5043 | msg.msg_controllen = 0; | |
5044 | msg.msg_namelen = 0; | |
5045 | msg.msg_iocb = NULL; | |
5046 | msg.msg_flags = 0; | |
fddaface | 5047 | |
04411806 PB |
5048 | flags = req->sr_msg.msg_flags; |
5049 | if (force_nonblock) | |
7a7cacba | 5050 | flags |= MSG_DONTWAIT; |
0031275d SM |
5051 | if (flags & MSG_WAITALL) |
5052 | min_ret = iov_iter_count(&msg.msg_iter); | |
5053 | ||
7a7cacba PB |
5054 | ret = sock_recvmsg(sock, &msg, flags); |
5055 | if (force_nonblock && ret == -EAGAIN) | |
5056 | return -EAGAIN; | |
5057 | if (ret == -ERESTARTSYS) | |
5058 | ret = -EINTR; | |
14c32eee | 5059 | out_free: |
7fbb1b54 PB |
5060 | if (req->flags & REQ_F_BUFFER_SELECTED) |
5061 | cflags = io_put_recv_kbuf(req); | |
0031275d | 5062 | if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
93d2bcd2 | 5063 | req_set_fail(req); |
889fca73 | 5064 | __io_req_complete(req, issue_flags, ret, cflags); |
fddaface | 5065 | return 0; |
fddaface JA |
5066 | } |
5067 | ||
3529d8c2 | 5068 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 5069 | { |
8ed8d3c3 JA |
5070 | struct io_accept *accept = &req->accept; |
5071 | ||
14587a46 | 5072 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 5073 | return -EINVAL; |
aaa4db12 | 5074 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
5075 | return -EINVAL; |
5076 | ||
d55e5f5b JA |
5077 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5078 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 5079 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 5080 | accept->nofile = rlimit(RLIMIT_NOFILE); |
a7083ad5 | 5081 | |
aaa4db12 PB |
5082 | accept->file_slot = READ_ONCE(sqe->file_index); |
5083 | if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) || | |
5084 | (accept->flags & SOCK_CLOEXEC))) | |
5085 | return -EINVAL; | |
a7083ad5 PB |
5086 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
5087 | return -EINVAL; | |
5088 | if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) | |
5089 | accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; | |
8ed8d3c3 | 5090 | return 0; |
8ed8d3c3 | 5091 | } |
17f2fe35 | 5092 | |
889fca73 | 5093 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
5094 | { |
5095 | struct io_accept *accept = &req->accept; | |
45d189c6 | 5096 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 5097 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
aaa4db12 | 5098 | bool fixed = !!accept->file_slot; |
a7083ad5 PB |
5099 | struct file *file; |
5100 | int ret, fd; | |
8ed8d3c3 | 5101 | |
e697deed JX |
5102 | if (req->file->f_flags & O_NONBLOCK) |
5103 | req->flags |= REQ_F_NOWAIT; | |
5104 | ||
aaa4db12 PB |
5105 | if (!fixed) { |
5106 | fd = __get_unused_fd_flags(accept->flags, accept->nofile); | |
5107 | if (unlikely(fd < 0)) | |
5108 | return fd; | |
5109 | } | |
a7083ad5 PB |
5110 | file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, |
5111 | accept->flags); | |
5112 | if (IS_ERR(file)) { | |
aaa4db12 PB |
5113 | if (!fixed) |
5114 | put_unused_fd(fd); | |
a7083ad5 PB |
5115 | ret = PTR_ERR(file); |
5116 | if (ret == -EAGAIN && force_nonblock) | |
5117 | return -EAGAIN; | |
ac45abc0 PB |
5118 | if (ret == -ERESTARTSYS) |
5119 | ret = -EINTR; | |
93d2bcd2 | 5120 | req_set_fail(req); |
aaa4db12 | 5121 | } else if (!fixed) { |
a7083ad5 PB |
5122 | fd_install(fd, file); |
5123 | ret = fd; | |
aaa4db12 PB |
5124 | } else { |
5125 | ret = io_install_fixed_file(req, file, issue_flags, | |
5126 | accept->file_slot - 1); | |
ac45abc0 | 5127 | } |
889fca73 | 5128 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 5129 | return 0; |
8ed8d3c3 JA |
5130 | } |
5131 | ||
93642ef8 PB |
5132 | static int io_connect_prep_async(struct io_kiocb *req) |
5133 | { | |
5134 | struct io_async_connect *io = req->async_data; | |
5135 | struct io_connect *conn = &req->connect; | |
5136 | ||
5137 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
5138 | } | |
5139 | ||
3529d8c2 | 5140 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 5141 | { |
3529d8c2 | 5142 | struct io_connect *conn = &req->connect; |
f499a021 | 5143 | |
14587a46 | 5144 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 | 5145 | return -EINVAL; |
26578cda PB |
5146 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
5147 | sqe->splice_fd_in) | |
3fbb51c1 JA |
5148 | return -EINVAL; |
5149 | ||
3529d8c2 JA |
5150 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5151 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 5152 | return 0; |
f499a021 JA |
5153 | } |
5154 | ||
889fca73 | 5155 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 5156 | { |
e8c2bc1f | 5157 | struct io_async_connect __io, *io; |
f8e85cf2 | 5158 | unsigned file_flags; |
3fbb51c1 | 5159 | int ret; |
45d189c6 | 5160 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 5161 | |
d886e185 | 5162 | if (req_has_async_data(req)) { |
e8c2bc1f | 5163 | io = req->async_data; |
f499a021 | 5164 | } else { |
3529d8c2 JA |
5165 | ret = move_addr_to_kernel(req->connect.addr, |
5166 | req->connect.addr_len, | |
e8c2bc1f | 5167 | &__io.address); |
f499a021 JA |
5168 | if (ret) |
5169 | goto out; | |
5170 | io = &__io; | |
5171 | } | |
5172 | ||
3fbb51c1 JA |
5173 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
5174 | ||
e8c2bc1f | 5175 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 5176 | req->connect.addr_len, file_flags); |
87f80d62 | 5177 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
d886e185 | 5178 | if (req_has_async_data(req)) |
b7bb4f7d | 5179 | return -EAGAIN; |
e8c2bc1f | 5180 | if (io_alloc_async_data(req)) { |
f499a021 JA |
5181 | ret = -ENOMEM; |
5182 | goto out; | |
5183 | } | |
e8c2bc1f | 5184 | memcpy(req->async_data, &__io, sizeof(__io)); |
f8e85cf2 | 5185 | return -EAGAIN; |
f499a021 | 5186 | } |
f8e85cf2 JA |
5187 | if (ret == -ERESTARTSYS) |
5188 | ret = -EINTR; | |
f499a021 | 5189 | out: |
4e88d6e7 | 5190 | if (ret < 0) |
93d2bcd2 | 5191 | req_set_fail(req); |
889fca73 | 5192 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 5193 | return 0; |
469956e8 Y |
5194 | } |
5195 | #else /* !CONFIG_NET */ | |
99a10081 JA |
5196 | #define IO_NETOP_FN(op) \ |
5197 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
5198 | { \ | |
5199 | return -EOPNOTSUPP; \ | |
5200 | } | |
5201 | ||
5202 | #define IO_NETOP_PREP(op) \ | |
5203 | IO_NETOP_FN(op) \ | |
5204 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
5205 | { \ | |
5206 | return -EOPNOTSUPP; \ | |
5207 | } \ | |
5208 | ||
5209 | #define IO_NETOP_PREP_ASYNC(op) \ | |
5210 | IO_NETOP_PREP(op) \ | |
5211 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
5212 | { \ | |
5213 | return -EOPNOTSUPP; \ | |
5214 | } | |
5215 | ||
5216 | IO_NETOP_PREP_ASYNC(sendmsg); | |
5217 | IO_NETOP_PREP_ASYNC(recvmsg); | |
5218 | IO_NETOP_PREP_ASYNC(connect); | |
5219 | IO_NETOP_PREP(accept); | |
5220 | IO_NETOP_FN(send); | |
5221 | IO_NETOP_FN(recv); | |
469956e8 | 5222 | #endif /* CONFIG_NET */ |
f8e85cf2 | 5223 | |
d7718a9d JA |
5224 | struct io_poll_table { |
5225 | struct poll_table_struct pt; | |
5226 | struct io_kiocb *req; | |
68b11e8b | 5227 | int nr_entries; |
d7718a9d JA |
5228 | int error; |
5229 | }; | |
ce593a6c | 5230 | |
d7718a9d | 5231 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
5b0a6acc | 5232 | __poll_t mask, io_req_tw_func_t func) |
d7718a9d | 5233 | { |
d7718a9d JA |
5234 | /* for instances that support it check for an event match first: */ |
5235 | if (mask && !(mask & poll->events)) | |
5236 | return 0; | |
5237 | ||
5238 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
5239 | ||
5240 | list_del_init(&poll->wait.entry); | |
5241 | ||
d7718a9d | 5242 | req->result = mask; |
5b0a6acc | 5243 | req->io_task_work.func = func; |
6d816e08 | 5244 | |
d7718a9d | 5245 | /* |
e3aabf95 JA |
5246 | * If this fails, then the task is exiting. When a task exits, the |
5247 | * work gets canceled, so just cancel this request as well instead | |
5248 | * of executing it. We can't safely execute it anyway, as we may not | |
5249 | * have the needed state needed for it anyway. | |
d7718a9d | 5250 | */ |
e09ee510 | 5251 | io_req_task_work_add(req); |
d7718a9d JA |
5252 | return 1; |
5253 | } | |
5254 | ||
74ce6ce4 JA |
5255 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
5256 | __acquires(&req->ctx->completion_lock) | |
5257 | { | |
5258 | struct io_ring_ctx *ctx = req->ctx; | |
5259 | ||
316319e8 | 5260 | /* req->task == current here, checking PF_EXITING is safe */ |
e09ee510 PB |
5261 | if (unlikely(req->task->flags & PF_EXITING)) |
5262 | WRITE_ONCE(poll->canceled, true); | |
5263 | ||
74ce6ce4 JA |
5264 | if (!req->result && !READ_ONCE(poll->canceled)) { |
5265 | struct poll_table_struct pt = { ._key = poll->events }; | |
5266 | ||
5267 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
5268 | } | |
5269 | ||
79ebeaee | 5270 | spin_lock(&ctx->completion_lock); |
74ce6ce4 JA |
5271 | if (!req->result && !READ_ONCE(poll->canceled)) { |
5272 | add_wait_queue(poll->head, &poll->wait); | |
5273 | return true; | |
5274 | } | |
5275 | ||
5276 | return false; | |
5277 | } | |
5278 | ||
d4e7cd36 | 5279 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5280 | { |
e8c2bc1f | 5281 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5282 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5283 | return req->async_data; |
d4e7cd36 JA |
5284 | return req->apoll->double_poll; |
5285 | } | |
5286 | ||
5287 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5288 | { | |
5289 | if (req->opcode == IORING_OP_POLL_ADD) | |
5290 | return &req->poll; | |
5291 | return &req->apoll->poll; | |
5292 | } | |
5293 | ||
5294 | static void io_poll_remove_double(struct io_kiocb *req) | |
e07785b0 | 5295 | __must_hold(&req->ctx->completion_lock) |
d4e7cd36 JA |
5296 | { |
5297 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
5298 | |
5299 | lockdep_assert_held(&req->ctx->completion_lock); | |
5300 | ||
5301 | if (poll && poll->head) { | |
5302 | struct wait_queue_head *head = poll->head; | |
5303 | ||
79ebeaee | 5304 | spin_lock_irq(&head->lock); |
18bceab1 JA |
5305 | list_del_init(&poll->wait.entry); |
5306 | if (poll->wait.private) | |
de9b4cca | 5307 | req_ref_put(req); |
18bceab1 | 5308 | poll->head = NULL; |
79ebeaee | 5309 | spin_unlock_irq(&head->lock); |
18bceab1 JA |
5310 | } |
5311 | } | |
5312 | ||
31efe48e | 5313 | static bool __io_poll_complete(struct io_kiocb *req, __poll_t mask) |
e07785b0 | 5314 | __must_hold(&req->ctx->completion_lock) |
18bceab1 JA |
5315 | { |
5316 | struct io_ring_ctx *ctx = req->ctx; | |
88e41cf9 | 5317 | unsigned flags = IORING_CQE_F_MORE; |
e27414be | 5318 | int error; |
18bceab1 | 5319 | |
e27414be | 5320 | if (READ_ONCE(req->poll.canceled)) { |
45ab03b1 | 5321 | error = -ECANCELED; |
88e41cf9 | 5322 | req->poll.events |= EPOLLONESHOT; |
e27414be | 5323 | } else { |
5082620f | 5324 | error = mangle_poll(mask); |
e27414be | 5325 | } |
b69de288 JA |
5326 | if (req->poll.events & EPOLLONESHOT) |
5327 | flags = 0; | |
a62682f9 HX |
5328 | if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) { |
5329 | req->poll.events |= EPOLLONESHOT; | |
88e41cf9 | 5330 | flags = 0; |
a62682f9 | 5331 | } |
7b289c38 HX |
5332 | if (flags & IORING_CQE_F_MORE) |
5333 | ctx->cq_extra++; | |
18bceab1 | 5334 | |
88e41cf9 | 5335 | return !(flags & IORING_CQE_F_MORE); |
18bceab1 JA |
5336 | } |
5337 | ||
f237c30a | 5338 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 JA |
5339 | { |
5340 | struct io_ring_ctx *ctx = req->ctx; | |
dd221f46 | 5341 | struct io_kiocb *nxt; |
18bceab1 JA |
5342 | |
5343 | if (io_poll_rewait(req, &req->poll)) { | |
79ebeaee | 5344 | spin_unlock(&ctx->completion_lock); |
dd221f46 | 5345 | } else { |
f40b964a | 5346 | bool done; |
18bceab1 | 5347 | |
5b7aa38d HX |
5348 | if (req->poll.done) { |
5349 | spin_unlock(&ctx->completion_lock); | |
5350 | return; | |
5351 | } | |
31efe48e | 5352 | done = __io_poll_complete(req, req->result); |
88e41cf9 | 5353 | if (done) { |
a890d01e | 5354 | io_poll_remove_double(req); |
88e41cf9 | 5355 | hash_del(&req->hash_node); |
bd99c71b | 5356 | req->poll.done = true; |
f40b964a | 5357 | } else { |
88e41cf9 JA |
5358 | req->result = 0; |
5359 | add_wait_queue(req->poll.head, &req->poll.wait); | |
5360 | } | |
31efe48e | 5361 | io_commit_cqring(ctx); |
79ebeaee | 5362 | spin_unlock(&ctx->completion_lock); |
dd221f46 | 5363 | io_cqring_ev_posted(ctx); |
18bceab1 | 5364 | |
88e41cf9 JA |
5365 | if (done) { |
5366 | nxt = io_put_req_find_next(req); | |
5367 | if (nxt) | |
f237c30a | 5368 | io_req_task_submit(nxt, locked); |
88e41cf9 | 5369 | } |
dd221f46 | 5370 | } |
18bceab1 JA |
5371 | } |
5372 | ||
5373 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
5374 | int sync, void *key) | |
5375 | { | |
5376 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 5377 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 | 5378 | __poll_t mask = key_to_poll(key); |
79ebeaee | 5379 | unsigned long flags; |
18bceab1 JA |
5380 | |
5381 | /* for instances that support it check for an event match first: */ | |
5382 | if (mask && !(mask & poll->events)) | |
5383 | return 0; | |
88e41cf9 JA |
5384 | if (!(poll->events & EPOLLONESHOT)) |
5385 | return poll->wait.func(&poll->wait, mode, sync, key); | |
18bceab1 | 5386 | |
8706e04e JA |
5387 | list_del_init(&wait->entry); |
5388 | ||
9ce85ef2 | 5389 | if (poll->head) { |
18bceab1 JA |
5390 | bool done; |
5391 | ||
79ebeaee | 5392 | spin_lock_irqsave(&poll->head->lock, flags); |
807abcb0 | 5393 | done = list_empty(&poll->wait.entry); |
18bceab1 | 5394 | if (!done) |
807abcb0 | 5395 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
5396 | /* make sure double remove sees this as being gone */ |
5397 | wait->private = NULL; | |
79ebeaee | 5398 | spin_unlock_irqrestore(&poll->head->lock, flags); |
c8b5e260 JA |
5399 | if (!done) { |
5400 | /* use wait func handler, so it matches the rq type */ | |
5401 | poll->wait.func(&poll->wait, mode, sync, key); | |
5402 | } | |
18bceab1 | 5403 | } |
de9b4cca | 5404 | req_ref_put(req); |
18bceab1 JA |
5405 | return 1; |
5406 | } | |
5407 | ||
5408 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
5409 | wait_queue_func_t wake_func) | |
5410 | { | |
5411 | poll->head = NULL; | |
5412 | poll->done = false; | |
5413 | poll->canceled = false; | |
464dca61 JA |
5414 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
5415 | /* mask in events that we always want/need */ | |
5416 | poll->events = events | IO_POLL_UNMASK; | |
18bceab1 JA |
5417 | INIT_LIST_HEAD(&poll->wait.entry); |
5418 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
5419 | } | |
5420 | ||
5421 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
5422 | struct wait_queue_head *head, |
5423 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
5424 | { |
5425 | struct io_kiocb *req = pt->req; | |
5426 | ||
5427 | /* | |
68b11e8b PB |
5428 | * The file being polled uses multiple waitqueues for poll handling |
5429 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb | |
5430 | * if this happens. | |
18bceab1 | 5431 | */ |
68b11e8b | 5432 | if (unlikely(pt->nr_entries)) { |
58852d4d PB |
5433 | struct io_poll_iocb *poll_one = poll; |
5434 | ||
23a65db8 PB |
5435 | /* double add on the same waitqueue head, ignore */ |
5436 | if (poll_one->head == head) | |
5437 | return; | |
18bceab1 | 5438 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5439 | if (*poll_ptr) { |
23a65db8 PB |
5440 | if ((*poll_ptr)->head == head) |
5441 | return; | |
18bceab1 JA |
5442 | pt->error = -EINVAL; |
5443 | return; | |
5444 | } | |
ea6a693d JA |
5445 | /* |
5446 | * Can't handle multishot for double wait for now, turn it | |
5447 | * into one-shot mode. | |
5448 | */ | |
7a274727 PB |
5449 | if (!(poll_one->events & EPOLLONESHOT)) |
5450 | poll_one->events |= EPOLLONESHOT; | |
18bceab1 JA |
5451 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
5452 | if (!poll) { | |
5453 | pt->error = -ENOMEM; | |
5454 | return; | |
5455 | } | |
58852d4d | 5456 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
de9b4cca | 5457 | req_ref_get(req); |
18bceab1 | 5458 | poll->wait.private = req; |
d886e185 | 5459 | |
807abcb0 | 5460 | *poll_ptr = poll; |
d886e185 PB |
5461 | if (req->opcode == IORING_OP_POLL_ADD) |
5462 | req->flags |= REQ_F_ASYNC_DATA; | |
18bceab1 JA |
5463 | } |
5464 | ||
68b11e8b | 5465 | pt->nr_entries++; |
18bceab1 | 5466 | poll->head = head; |
a31eb4a2 JX |
5467 | |
5468 | if (poll->events & EPOLLEXCLUSIVE) | |
5469 | add_wait_queue_exclusive(head, &poll->wait); | |
5470 | else | |
5471 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5472 | } |
5473 | ||
5474 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5475 | struct poll_table_struct *p) | |
5476 | { | |
5477 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5478 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5479 | |
807abcb0 | 5480 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5481 | } |
5482 | ||
f237c30a | 5483 | static void io_async_task_func(struct io_kiocb *req, bool *locked) |
d7718a9d | 5484 | { |
d7718a9d JA |
5485 | struct async_poll *apoll = req->apoll; |
5486 | struct io_ring_ctx *ctx = req->ctx; | |
5487 | ||
236daeae | 5488 | trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data); |
d7718a9d | 5489 | |
74ce6ce4 | 5490 | if (io_poll_rewait(req, &apoll->poll)) { |
79ebeaee | 5491 | spin_unlock(&ctx->completion_lock); |
74ce6ce4 | 5492 | return; |
d7718a9d JA |
5493 | } |
5494 | ||
0ea13b44 | 5495 | hash_del(&req->hash_node); |
d4e7cd36 | 5496 | io_poll_remove_double(req); |
bd99c71b | 5497 | apoll->poll.done = true; |
79ebeaee | 5498 | spin_unlock(&ctx->completion_lock); |
74ce6ce4 | 5499 | |
0be0b0e3 | 5500 | if (!READ_ONCE(apoll->poll.canceled)) |
f237c30a | 5501 | io_req_task_submit(req, locked); |
0be0b0e3 | 5502 | else |
2593553a | 5503 | io_req_complete_failed(req, -ECANCELED); |
d7718a9d JA |
5504 | } |
5505 | ||
5506 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5507 | void *key) | |
5508 | { | |
5509 | struct io_kiocb *req = wait->private; | |
5510 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5511 | ||
5512 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5513 | key_to_poll(key)); | |
5514 | ||
5515 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5516 | } | |
5517 | ||
5518 | static void io_poll_req_insert(struct io_kiocb *req) | |
5519 | { | |
5520 | struct io_ring_ctx *ctx = req->ctx; | |
5521 | struct hlist_head *list; | |
5522 | ||
5523 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5524 | hlist_add_head(&req->hash_node, list); | |
5525 | } | |
5526 | ||
5527 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5528 | struct io_poll_iocb *poll, | |
5529 | struct io_poll_table *ipt, __poll_t mask, | |
5530 | wait_queue_func_t wake_func) | |
5531 | __acquires(&ctx->completion_lock) | |
5532 | { | |
5533 | struct io_ring_ctx *ctx = req->ctx; | |
5534 | bool cancel = false; | |
5535 | ||
4d52f338 | 5536 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5537 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5538 | poll->file = req->file; |
18bceab1 | 5539 | poll->wait.private = req; |
d7718a9d JA |
5540 | |
5541 | ipt->pt._key = mask; | |
5542 | ipt->req = req; | |
68b11e8b PB |
5543 | ipt->error = 0; |
5544 | ipt->nr_entries = 0; | |
d7718a9d | 5545 | |
d7718a9d | 5546 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
68b11e8b PB |
5547 | if (unlikely(!ipt->nr_entries) && !ipt->error) |
5548 | ipt->error = -EINVAL; | |
d7718a9d | 5549 | |
79ebeaee | 5550 | spin_lock(&ctx->completion_lock); |
a890d01e | 5551 | if (ipt->error || (mask && (poll->events & EPOLLONESHOT))) |
46fee9ab | 5552 | io_poll_remove_double(req); |
d7718a9d | 5553 | if (likely(poll->head)) { |
79ebeaee | 5554 | spin_lock_irq(&poll->head->lock); |
d7718a9d JA |
5555 | if (unlikely(list_empty(&poll->wait.entry))) { |
5556 | if (ipt->error) | |
5557 | cancel = true; | |
5558 | ipt->error = 0; | |
5559 | mask = 0; | |
5560 | } | |
88e41cf9 | 5561 | if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error) |
d7718a9d JA |
5562 | list_del_init(&poll->wait.entry); |
5563 | else if (cancel) | |
5564 | WRITE_ONCE(poll->canceled, true); | |
5565 | else if (!poll->done) /* actually waiting for an event */ | |
5566 | io_poll_req_insert(req); | |
79ebeaee | 5567 | spin_unlock_irq(&poll->head->lock); |
d7718a9d JA |
5568 | } |
5569 | ||
5570 | return mask; | |
5571 | } | |
5572 | ||
59b735ae OL |
5573 | enum { |
5574 | IO_APOLL_OK, | |
5575 | IO_APOLL_ABORTED, | |
5576 | IO_APOLL_READY | |
5577 | }; | |
5578 | ||
5579 | static int io_arm_poll_handler(struct io_kiocb *req) | |
d7718a9d JA |
5580 | { |
5581 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5582 | struct io_ring_ctx *ctx = req->ctx; | |
5583 | struct async_poll *apoll; | |
5584 | struct io_poll_table ipt; | |
b2d9c3da | 5585 | __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI; |
d7718a9d JA |
5586 | |
5587 | if (!req->file || !file_can_poll(req->file)) | |
59b735ae | 5588 | return IO_APOLL_ABORTED; |
24c74678 | 5589 | if (req->flags & REQ_F_POLLED) |
59b735ae | 5590 | return IO_APOLL_ABORTED; |
b2d9c3da PB |
5591 | if (!def->pollin && !def->pollout) |
5592 | return IO_APOLL_ABORTED; | |
5593 | ||
5594 | if (def->pollin) { | |
b2d9c3da PB |
5595 | mask |= POLLIN | POLLRDNORM; |
5596 | ||
5597 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5598 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5599 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5600 | mask &= ~POLLIN; | |
5601 | } else { | |
b2d9c3da PB |
5602 | mask |= POLLOUT | POLLWRNORM; |
5603 | } | |
5604 | ||
d7718a9d JA |
5605 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
5606 | if (unlikely(!apoll)) | |
59b735ae | 5607 | return IO_APOLL_ABORTED; |
807abcb0 | 5608 | apoll->double_poll = NULL; |
d7718a9d | 5609 | req->apoll = apoll; |
b2d9c3da | 5610 | req->flags |= REQ_F_POLLED; |
d7718a9d | 5611 | ipt.pt._qproc = io_async_queue_proc; |
48dcd38d | 5612 | io_req_set_refcount(req); |
d7718a9d JA |
5613 | |
5614 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5615 | io_async_wake); | |
79ebeaee | 5616 | spin_unlock(&ctx->completion_lock); |
41a5169c HX |
5617 | if (ret || ipt.error) |
5618 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; | |
5619 | ||
236daeae OL |
5620 | trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data, |
5621 | mask, apoll->poll.events); | |
59b735ae | 5622 | return IO_APOLL_OK; |
d7718a9d JA |
5623 | } |
5624 | ||
5625 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
b2e720ac | 5626 | struct io_poll_iocb *poll, bool do_cancel) |
e07785b0 | 5627 | __must_hold(&req->ctx->completion_lock) |
221c5eb2 | 5628 | { |
b41e9852 | 5629 | bool do_complete = false; |
221c5eb2 | 5630 | |
5082620f JA |
5631 | if (!poll->head) |
5632 | return false; | |
79ebeaee | 5633 | spin_lock_irq(&poll->head->lock); |
b2e720ac JA |
5634 | if (do_cancel) |
5635 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5636 | if (!list_empty(&poll->wait.entry)) { |
5637 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5638 | do_complete = true; |
221c5eb2 | 5639 | } |
79ebeaee | 5640 | spin_unlock_irq(&poll->head->lock); |
3bfa5bcb | 5641 | hash_del(&req->hash_node); |
d7718a9d JA |
5642 | return do_complete; |
5643 | } | |
5644 | ||
5d709043 | 5645 | static bool io_poll_remove_one(struct io_kiocb *req) |
e07785b0 | 5646 | __must_hold(&req->ctx->completion_lock) |
d7718a9d JA |
5647 | { |
5648 | bool do_complete; | |
5649 | ||
d4e7cd36 | 5650 | io_poll_remove_double(req); |
e31001a3 | 5651 | do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true); |
d4e7cd36 | 5652 | |
b41e9852 | 5653 | if (do_complete) { |
d4d19c19 | 5654 | io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0); |
b41e9852 | 5655 | io_commit_cqring(req->ctx); |
93d2bcd2 | 5656 | req_set_fail(req); |
91c2f697 | 5657 | io_put_req_deferred(req); |
5d709043 | 5658 | } |
b41e9852 | 5659 | return do_complete; |
221c5eb2 JA |
5660 | } |
5661 | ||
76e1b642 JA |
5662 | /* |
5663 | * Returns true if we found and killed one or more poll requests | |
5664 | */ | |
c072481d PB |
5665 | static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, |
5666 | struct task_struct *tsk, bool cancel_all) | |
221c5eb2 | 5667 | { |
78076bb6 | 5668 | struct hlist_node *tmp; |
221c5eb2 | 5669 | struct io_kiocb *req; |
8e2e1faf | 5670 | int posted = 0, i; |
221c5eb2 | 5671 | |
79ebeaee | 5672 | spin_lock(&ctx->completion_lock); |
78076bb6 JA |
5673 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5674 | struct hlist_head *list; | |
5675 | ||
5676 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5677 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
3dd0c97a | 5678 | if (io_match_task(req, tsk, cancel_all)) |
f3606e3a JA |
5679 | posted += io_poll_remove_one(req); |
5680 | } | |
221c5eb2 | 5681 | } |
79ebeaee | 5682 | spin_unlock(&ctx->completion_lock); |
b41e9852 | 5683 | |
8e2e1faf JA |
5684 | if (posted) |
5685 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5686 | |
5687 | return posted != 0; | |
221c5eb2 JA |
5688 | } |
5689 | ||
9ba5fac8 PB |
5690 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5691 | bool poll_only) | |
e07785b0 | 5692 | __must_hold(&ctx->completion_lock) |
47f46768 | 5693 | { |
78076bb6 | 5694 | struct hlist_head *list; |
47f46768 JA |
5695 | struct io_kiocb *req; |
5696 | ||
78076bb6 JA |
5697 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5698 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5699 | if (sqe_addr != req->user_data) |
5700 | continue; | |
9ba5fac8 PB |
5701 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
5702 | continue; | |
b2cb805f | 5703 | return req; |
47f46768 | 5704 | } |
b2cb805f JA |
5705 | return NULL; |
5706 | } | |
5707 | ||
9ba5fac8 PB |
5708 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5709 | bool poll_only) | |
e07785b0 | 5710 | __must_hold(&ctx->completion_lock) |
b2cb805f JA |
5711 | { |
5712 | struct io_kiocb *req; | |
5713 | ||
9ba5fac8 | 5714 | req = io_poll_find(ctx, sqe_addr, poll_only); |
b2cb805f JA |
5715 | if (!req) |
5716 | return -ENOENT; | |
5717 | if (io_poll_remove_one(req)) | |
5718 | return 0; | |
5719 | ||
5720 | return -EALREADY; | |
47f46768 JA |
5721 | } |
5722 | ||
9096af3e PB |
5723 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
5724 | unsigned int flags) | |
5725 | { | |
5726 | u32 events; | |
47f46768 | 5727 | |
9096af3e PB |
5728 | events = READ_ONCE(sqe->poll32_events); |
5729 | #ifdef __BIG_ENDIAN | |
5730 | events = swahw32(events); | |
5731 | #endif | |
5732 | if (!(flags & IORING_POLL_ADD_MULTI)) | |
5733 | events |= EPOLLONESHOT; | |
5734 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); | |
47f46768 JA |
5735 | } |
5736 | ||
c5de0036 | 5737 | static int io_poll_update_prep(struct io_kiocb *req, |
3529d8c2 | 5738 | const struct io_uring_sqe *sqe) |
0969e783 | 5739 | { |
c5de0036 PB |
5740 | struct io_poll_update *upd = &req->poll_update; |
5741 | u32 flags; | |
5742 | ||
0969e783 JA |
5743 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5744 | return -EINVAL; | |
26578cda | 5745 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
c5de0036 PB |
5746 | return -EINVAL; |
5747 | flags = READ_ONCE(sqe->len); | |
5748 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | | |
5749 | IORING_POLL_ADD_MULTI)) | |
5750 | return -EINVAL; | |
5751 | /* meaningless without update */ | |
5752 | if (flags == IORING_POLL_ADD_MULTI) | |
0969e783 JA |
5753 | return -EINVAL; |
5754 | ||
c5de0036 PB |
5755 | upd->old_user_data = READ_ONCE(sqe->addr); |
5756 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; | |
5757 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; | |
221c5eb2 | 5758 | |
c5de0036 PB |
5759 | upd->new_user_data = READ_ONCE(sqe->off); |
5760 | if (!upd->update_user_data && upd->new_user_data) | |
5761 | return -EINVAL; | |
5762 | if (upd->update_events) | |
5763 | upd->events = io_poll_parse_events(sqe, flags); | |
5764 | else if (sqe->poll32_events) | |
5765 | return -EINVAL; | |
221c5eb2 | 5766 | |
221c5eb2 JA |
5767 | return 0; |
5768 | } | |
5769 | ||
221c5eb2 JA |
5770 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5771 | void *key) | |
5772 | { | |
c2f2eb7d JA |
5773 | struct io_kiocb *req = wait->private; |
5774 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5775 | |
d7718a9d | 5776 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5777 | } |
5778 | ||
221c5eb2 JA |
5779 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5780 | struct poll_table_struct *p) | |
5781 | { | |
5782 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5783 | ||
e8c2bc1f | 5784 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5785 | } |
5786 | ||
3529d8c2 | 5787 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5788 | { |
5789 | struct io_poll_iocb *poll = &req->poll; | |
c5de0036 | 5790 | u32 flags; |
221c5eb2 JA |
5791 | |
5792 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5793 | return -EINVAL; | |
c5de0036 | 5794 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
88e41cf9 JA |
5795 | return -EINVAL; |
5796 | flags = READ_ONCE(sqe->len); | |
c5de0036 | 5797 | if (flags & ~IORING_POLL_ADD_MULTI) |
221c5eb2 JA |
5798 | return -EINVAL; |
5799 | ||
48dcd38d | 5800 | io_req_set_refcount(req); |
c5de0036 | 5801 | poll->events = io_poll_parse_events(sqe, flags); |
0969e783 JA |
5802 | return 0; |
5803 | } | |
5804 | ||
61e98203 | 5805 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
5806 | { |
5807 | struct io_poll_iocb *poll = &req->poll; | |
5808 | struct io_ring_ctx *ctx = req->ctx; | |
5809 | struct io_poll_table ipt; | |
0969e783 | 5810 | __poll_t mask; |
5b7aa38d | 5811 | bool done; |
0969e783 | 5812 | |
d7718a9d | 5813 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5814 | |
d7718a9d JA |
5815 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5816 | io_poll_wake); | |
221c5eb2 | 5817 | |
8c838788 | 5818 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5819 | ipt.error = 0; |
eb6e6f06 PB |
5820 | done = __io_poll_complete(req, mask); |
5821 | io_commit_cqring(req->ctx); | |
221c5eb2 | 5822 | } |
79ebeaee | 5823 | spin_unlock(&ctx->completion_lock); |
221c5eb2 | 5824 | |
8c838788 JA |
5825 | if (mask) { |
5826 | io_cqring_ev_posted(ctx); | |
5b7aa38d | 5827 | if (done) |
88e41cf9 | 5828 | io_put_req(req); |
221c5eb2 | 5829 | } |
8c838788 | 5830 | return ipt.error; |
221c5eb2 JA |
5831 | } |
5832 | ||
c5de0036 | 5833 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
b69de288 JA |
5834 | { |
5835 | struct io_ring_ctx *ctx = req->ctx; | |
5836 | struct io_kiocb *preq; | |
cb3b200e | 5837 | bool completing; |
b69de288 JA |
5838 | int ret; |
5839 | ||
79ebeaee | 5840 | spin_lock(&ctx->completion_lock); |
9ba5fac8 | 5841 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
b69de288 JA |
5842 | if (!preq) { |
5843 | ret = -ENOENT; | |
5844 | goto err; | |
b69de288 | 5845 | } |
cb3b200e | 5846 | |
c5de0036 PB |
5847 | if (!req->poll_update.update_events && !req->poll_update.update_user_data) { |
5848 | completing = true; | |
5849 | ret = io_poll_remove_one(preq) ? 0 : -EALREADY; | |
5850 | goto err; | |
5851 | } | |
5852 | ||
cb3b200e JA |
5853 | /* |
5854 | * Don't allow racy completion with singleshot, as we cannot safely | |
5855 | * update those. For multishot, if we're racing with completion, just | |
5856 | * let completion re-add it. | |
5857 | */ | |
5858 | completing = !__io_poll_remove_one(preq, &preq->poll, false); | |
5859 | if (completing && (preq->poll.events & EPOLLONESHOT)) { | |
5860 | ret = -EALREADY; | |
5861 | goto err; | |
b69de288 JA |
5862 | } |
5863 | /* we now have a detached poll request. reissue. */ | |
5864 | ret = 0; | |
5865 | err: | |
b69de288 | 5866 | if (ret < 0) { |
79ebeaee | 5867 | spin_unlock(&ctx->completion_lock); |
93d2bcd2 | 5868 | req_set_fail(req); |
b69de288 JA |
5869 | io_req_complete(req, ret); |
5870 | return 0; | |
5871 | } | |
5872 | /* only mask one event flags, keep behavior flags */ | |
9d805892 | 5873 | if (req->poll_update.update_events) { |
b69de288 | 5874 | preq->poll.events &= ~0xffff; |
9d805892 | 5875 | preq->poll.events |= req->poll_update.events & 0xffff; |
b69de288 JA |
5876 | preq->poll.events |= IO_POLL_UNMASK; |
5877 | } | |
9d805892 PB |
5878 | if (req->poll_update.update_user_data) |
5879 | preq->user_data = req->poll_update.new_user_data; | |
79ebeaee | 5880 | spin_unlock(&ctx->completion_lock); |
cb3b200e | 5881 | |
b69de288 JA |
5882 | /* complete update request, we're done with it */ |
5883 | io_req_complete(req, ret); | |
5884 | ||
cb3b200e | 5885 | if (!completing) { |
c5de0036 | 5886 | ret = io_poll_add(preq, issue_flags); |
cb3b200e | 5887 | if (ret < 0) { |
93d2bcd2 | 5888 | req_set_fail(preq); |
cb3b200e JA |
5889 | io_req_complete(preq, ret); |
5890 | } | |
b69de288 JA |
5891 | } |
5892 | return 0; | |
5893 | } | |
5894 | ||
f237c30a | 5895 | static void io_req_task_timeout(struct io_kiocb *req, bool *locked) |
89850fce | 5896 | { |
6224590d PB |
5897 | struct io_timeout_data *data = req->async_data; |
5898 | ||
5899 | if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS)) | |
5900 | req_set_fail(req); | |
505657bc | 5901 | io_req_complete_post(req, -ETIME, 0); |
89850fce JA |
5902 | } |
5903 | ||
5262f567 JA |
5904 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5905 | { | |
ad8a48ac JA |
5906 | struct io_timeout_data *data = container_of(timer, |
5907 | struct io_timeout_data, timer); | |
5908 | struct io_kiocb *req = data->req; | |
5909 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5910 | unsigned long flags; |
5911 | ||
89850fce | 5912 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
a71976f3 | 5913 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5914 | atomic_set(&req->ctx->cq_timeouts, |
5915 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
89850fce | 5916 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
01cec8c1 | 5917 | |
89850fce JA |
5918 | req->io_task_work.func = io_req_task_timeout; |
5919 | io_req_task_work_add(req); | |
5262f567 JA |
5920 | return HRTIMER_NORESTART; |
5921 | } | |
5922 | ||
fbd15848 PB |
5923 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5924 | __u64 user_data) | |
89850fce | 5925 | __must_hold(&ctx->timeout_lock) |
f254ac04 | 5926 | { |
fbd15848 | 5927 | struct io_timeout_data *io; |
47f46768 | 5928 | struct io_kiocb *req; |
fd9c7bc5 | 5929 | bool found = false; |
f254ac04 | 5930 | |
135fcde8 | 5931 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
fd9c7bc5 PB |
5932 | found = user_data == req->user_data; |
5933 | if (found) | |
47f46768 | 5934 | break; |
47f46768 | 5935 | } |
fd9c7bc5 PB |
5936 | if (!found) |
5937 | return ERR_PTR(-ENOENT); | |
fbd15848 PB |
5938 | |
5939 | io = req->async_data; | |
fd9c7bc5 | 5940 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
fbd15848 | 5941 | return ERR_PTR(-EALREADY); |
a71976f3 | 5942 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5943 | return req; |
5944 | } | |
47f46768 | 5945 | |
fbd15848 | 5946 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
ec3c3d0f | 5947 | __must_hold(&ctx->completion_lock) |
89850fce | 5948 | __must_hold(&ctx->timeout_lock) |
fbd15848 PB |
5949 | { |
5950 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5951 | ||
5952 | if (IS_ERR(req)) | |
5953 | return PTR_ERR(req); | |
f254ac04 | 5954 | |
93d2bcd2 | 5955 | req_set_fail(req); |
d4d19c19 | 5956 | io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0); |
91c2f697 | 5957 | io_put_req_deferred(req); |
f254ac04 JA |
5958 | return 0; |
5959 | } | |
5960 | ||
50c1df2b JA |
5961 | static clockid_t io_timeout_get_clock(struct io_timeout_data *data) |
5962 | { | |
5963 | switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) { | |
5964 | case IORING_TIMEOUT_BOOTTIME: | |
5965 | return CLOCK_BOOTTIME; | |
5966 | case IORING_TIMEOUT_REALTIME: | |
5967 | return CLOCK_REALTIME; | |
5968 | default: | |
5969 | /* can't happen, vetted at prep time */ | |
5970 | WARN_ON_ONCE(1); | |
5971 | fallthrough; | |
5972 | case 0: | |
5973 | return CLOCK_MONOTONIC; | |
5974 | } | |
5975 | } | |
5976 | ||
f1042b6c PB |
5977 | static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5978 | struct timespec64 *ts, enum hrtimer_mode mode) | |
5979 | __must_hold(&ctx->timeout_lock) | |
5980 | { | |
5981 | struct io_timeout_data *io; | |
5982 | struct io_kiocb *req; | |
5983 | bool found = false; | |
5984 | ||
5985 | list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) { | |
5986 | found = user_data == req->user_data; | |
5987 | if (found) | |
5988 | break; | |
5989 | } | |
5990 | if (!found) | |
5991 | return -ENOENT; | |
5992 | ||
5993 | io = req->async_data; | |
5994 | if (hrtimer_try_to_cancel(&io->timer) == -1) | |
5995 | return -EALREADY; | |
5996 | hrtimer_init(&io->timer, io_timeout_get_clock(io), mode); | |
5997 | io->timer.function = io_link_timeout_fn; | |
5998 | hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode); | |
5999 | return 0; | |
6000 | } | |
6001 | ||
9c8e11b3 PB |
6002 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
6003 | struct timespec64 *ts, enum hrtimer_mode mode) | |
89850fce | 6004 | __must_hold(&ctx->timeout_lock) |
47f46768 | 6005 | { |
9c8e11b3 PB |
6006 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
6007 | struct io_timeout_data *data; | |
47f46768 | 6008 | |
9c8e11b3 PB |
6009 | if (IS_ERR(req)) |
6010 | return PTR_ERR(req); | |
47f46768 | 6011 | |
9c8e11b3 PB |
6012 | req->timeout.off = 0; /* noseq */ |
6013 | data = req->async_data; | |
6014 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
50c1df2b | 6015 | hrtimer_init(&data->timer, io_timeout_get_clock(data), mode); |
9c8e11b3 PB |
6016 | data->timer.function = io_timeout_fn; |
6017 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
6018 | return 0; | |
47f46768 JA |
6019 | } |
6020 | ||
3529d8c2 JA |
6021 | static int io_timeout_remove_prep(struct io_kiocb *req, |
6022 | const struct io_uring_sqe *sqe) | |
b29472ee | 6023 | { |
9c8e11b3 PB |
6024 | struct io_timeout_rem *tr = &req->timeout_rem; |
6025 | ||
b29472ee JA |
6026 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
6027 | return -EINVAL; | |
61710e43 DA |
6028 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6029 | return -EINVAL; | |
26578cda | 6030 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
b29472ee JA |
6031 | return -EINVAL; |
6032 | ||
f1042b6c | 6033 | tr->ltimeout = false; |
9c8e11b3 PB |
6034 | tr->addr = READ_ONCE(sqe->addr); |
6035 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
f1042b6c PB |
6036 | if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) { |
6037 | if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
6038 | return -EINVAL; | |
6039 | if (tr->flags & IORING_LINK_TIMEOUT_UPDATE) | |
6040 | tr->ltimeout = true; | |
6041 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS)) | |
9c8e11b3 PB |
6042 | return -EINVAL; |
6043 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
6044 | return -EFAULT; | |
6045 | } else if (tr->flags) { | |
6046 | /* timeout removal doesn't support flags */ | |
b29472ee | 6047 | return -EINVAL; |
9c8e11b3 | 6048 | } |
b29472ee | 6049 | |
b29472ee JA |
6050 | return 0; |
6051 | } | |
6052 | ||
8662daec PB |
6053 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
6054 | { | |
6055 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
6056 | : HRTIMER_MODE_REL; | |
6057 | } | |
6058 | ||
11365043 JA |
6059 | /* |
6060 | * Remove or update an existing timeout command | |
6061 | */ | |
61e98203 | 6062 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 6063 | { |
9c8e11b3 | 6064 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 6065 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 6066 | int ret; |
11365043 | 6067 | |
ec3c3d0f PB |
6068 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
6069 | spin_lock(&ctx->completion_lock); | |
6070 | spin_lock_irq(&ctx->timeout_lock); | |
9c8e11b3 | 6071 | ret = io_timeout_cancel(ctx, tr->addr); |
ec3c3d0f PB |
6072 | spin_unlock_irq(&ctx->timeout_lock); |
6073 | spin_unlock(&ctx->completion_lock); | |
6074 | } else { | |
f1042b6c PB |
6075 | enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags); |
6076 | ||
ec3c3d0f | 6077 | spin_lock_irq(&ctx->timeout_lock); |
f1042b6c PB |
6078 | if (tr->ltimeout) |
6079 | ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
6080 | else | |
6081 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
ec3c3d0f PB |
6082 | spin_unlock_irq(&ctx->timeout_lock); |
6083 | } | |
11365043 | 6084 | |
4e88d6e7 | 6085 | if (ret < 0) |
93d2bcd2 | 6086 | req_set_fail(req); |
505657bc | 6087 | io_req_complete_post(req, ret, 0); |
11365043 | 6088 | return 0; |
5262f567 JA |
6089 | } |
6090 | ||
3529d8c2 | 6091 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 6092 | bool is_timeout_link) |
5262f567 | 6093 | { |
ad8a48ac | 6094 | struct io_timeout_data *data; |
a41525ab | 6095 | unsigned flags; |
56080b02 | 6096 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 6097 | |
ad8a48ac | 6098 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 6099 | return -EINVAL; |
26578cda PB |
6100 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
6101 | sqe->splice_fd_in) | |
a41525ab | 6102 | return -EINVAL; |
56080b02 | 6103 | if (off && is_timeout_link) |
2d28390a | 6104 | return -EINVAL; |
a41525ab | 6105 | flags = READ_ONCE(sqe->timeout_flags); |
6224590d PB |
6106 | if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK | |
6107 | IORING_TIMEOUT_ETIME_SUCCESS)) | |
50c1df2b JA |
6108 | return -EINVAL; |
6109 | /* more than one clock specified is invalid, obviously */ | |
6110 | if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
5262f567 | 6111 | return -EINVAL; |
bdf20073 | 6112 | |
ef9dd637 | 6113 | INIT_LIST_HEAD(&req->timeout.list); |
bfe68a22 | 6114 | req->timeout.off = off; |
f18ee4cf PB |
6115 | if (unlikely(off && !req->ctx->off_timeout_used)) |
6116 | req->ctx->off_timeout_used = true; | |
26a61679 | 6117 | |
d886e185 | 6118 | if (!req_has_async_data(req) && io_alloc_async_data(req)) |
26a61679 JA |
6119 | return -ENOMEM; |
6120 | ||
e8c2bc1f | 6121 | data = req->async_data; |
ad8a48ac | 6122 | data->req = req; |
50c1df2b | 6123 | data->flags = flags; |
ad8a48ac JA |
6124 | |
6125 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
6126 | return -EFAULT; |
6127 | ||
8662daec | 6128 | data->mode = io_translate_timeout_mode(flags); |
50c1df2b | 6129 | hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode); |
b97e736a PB |
6130 | |
6131 | if (is_timeout_link) { | |
6132 | struct io_submit_link *link = &req->ctx->submit_state.link; | |
6133 | ||
6134 | if (!link->head) | |
6135 | return -EINVAL; | |
6136 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) | |
6137 | return -EINVAL; | |
4d13d1a4 PB |
6138 | req->timeout.head = link->last; |
6139 | link->last->flags |= REQ_F_ARM_LTIMEOUT; | |
b97e736a | 6140 | } |
ad8a48ac JA |
6141 | return 0; |
6142 | } | |
6143 | ||
61e98203 | 6144 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 6145 | { |
ad8a48ac | 6146 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 6147 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 6148 | struct list_head *entry; |
bfe68a22 | 6149 | u32 tail, off = req->timeout.off; |
ad8a48ac | 6150 | |
89850fce | 6151 | spin_lock_irq(&ctx->timeout_lock); |
93bd25bb | 6152 | |
5262f567 JA |
6153 | /* |
6154 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
6155 | * timeout event to be satisfied. If it isn't set, then this is |
6156 | * a pure timeout request, sequence isn't used. | |
5262f567 | 6157 | */ |
8eb7e2d0 | 6158 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
6159 | entry = ctx->timeout_list.prev; |
6160 | goto add; | |
6161 | } | |
5262f567 | 6162 | |
bfe68a22 PB |
6163 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
6164 | req->timeout.target_seq = tail + off; | |
5262f567 | 6165 | |
f010505b MDG |
6166 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
6167 | * This is safe because ->completion_lock is held, and submissions | |
6168 | * and completions are never mixed in the same ->completion_lock section. | |
6169 | */ | |
6170 | ctx->cq_last_tm_flush = tail; | |
6171 | ||
5262f567 JA |
6172 | /* |
6173 | * Insertion sort, ensuring the first entry in the list is always | |
6174 | * the one we need first. | |
6175 | */ | |
5262f567 | 6176 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
6177 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
6178 | timeout.list); | |
5262f567 | 6179 | |
8eb7e2d0 | 6180 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 6181 | continue; |
bfe68a22 PB |
6182 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
6183 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
6184 | break; |
6185 | } | |
93bd25bb | 6186 | add: |
135fcde8 | 6187 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
6188 | data->timer.function = io_timeout_fn; |
6189 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
89850fce | 6190 | spin_unlock_irq(&ctx->timeout_lock); |
5262f567 JA |
6191 | return 0; |
6192 | } | |
5262f567 | 6193 | |
f458dd84 PB |
6194 | struct io_cancel_data { |
6195 | struct io_ring_ctx *ctx; | |
6196 | u64 user_data; | |
6197 | }; | |
6198 | ||
62755e35 JA |
6199 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
6200 | { | |
6201 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 6202 | struct io_cancel_data *cd = data; |
62755e35 | 6203 | |
f458dd84 | 6204 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
62755e35 JA |
6205 | } |
6206 | ||
f458dd84 PB |
6207 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
6208 | struct io_ring_ctx *ctx) | |
62755e35 | 6209 | { |
f458dd84 | 6210 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
62755e35 | 6211 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
6212 | int ret = 0; |
6213 | ||
f458dd84 | 6214 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
6215 | return -ENOENT; |
6216 | ||
f458dd84 | 6217 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
62755e35 JA |
6218 | switch (cancel_ret) { |
6219 | case IO_WQ_CANCEL_OK: | |
6220 | ret = 0; | |
6221 | break; | |
6222 | case IO_WQ_CANCEL_RUNNING: | |
6223 | ret = -EALREADY; | |
6224 | break; | |
6225 | case IO_WQ_CANCEL_NOTFOUND: | |
6226 | ret = -ENOENT; | |
6227 | break; | |
6228 | } | |
6229 | ||
e977d6d3 JA |
6230 | return ret; |
6231 | } | |
6232 | ||
8cb01fac | 6233 | static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr) |
47f46768 | 6234 | { |
8cb01fac | 6235 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 JA |
6236 | int ret; |
6237 | ||
dadebc35 | 6238 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
8cb01fac | 6239 | |
f458dd84 | 6240 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
df9727af | 6241 | if (ret != -ENOENT) |
8cb01fac | 6242 | return ret; |
505657bc PB |
6243 | |
6244 | spin_lock(&ctx->completion_lock); | |
79ebeaee | 6245 | spin_lock_irq(&ctx->timeout_lock); |
47f46768 | 6246 | ret = io_timeout_cancel(ctx, sqe_addr); |
79ebeaee | 6247 | spin_unlock_irq(&ctx->timeout_lock); |
47f46768 | 6248 | if (ret != -ENOENT) |
505657bc PB |
6249 | goto out; |
6250 | ret = io_poll_cancel(ctx, sqe_addr, false); | |
6251 | out: | |
6252 | spin_unlock(&ctx->completion_lock); | |
6253 | return ret; | |
47f46768 JA |
6254 | } |
6255 | ||
3529d8c2 JA |
6256 | static int io_async_cancel_prep(struct io_kiocb *req, |
6257 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 6258 | { |
fbf23849 | 6259 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 6260 | return -EINVAL; |
61710e43 DA |
6261 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6262 | return -EINVAL; | |
26578cda PB |
6263 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
6264 | sqe->splice_fd_in) | |
e977d6d3 JA |
6265 | return -EINVAL; |
6266 | ||
fbf23849 JA |
6267 | req->cancel.addr = READ_ONCE(sqe->addr); |
6268 | return 0; | |
6269 | } | |
6270 | ||
61e98203 | 6271 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6272 | { |
6273 | struct io_ring_ctx *ctx = req->ctx; | |
58f99373 | 6274 | u64 sqe_addr = req->cancel.addr; |
3b44b371 | 6275 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
58f99373 PB |
6276 | struct io_tctx_node *node; |
6277 | int ret; | |
6278 | ||
8cb01fac | 6279 | ret = io_try_cancel_userdata(req, sqe_addr); |
58f99373 PB |
6280 | if (ret != -ENOENT) |
6281 | goto done; | |
58f99373 PB |
6282 | |
6283 | /* slow path, try all io-wq's */ | |
3b44b371 | 6284 | io_ring_submit_lock(ctx, needs_lock); |
58f99373 PB |
6285 | ret = -ENOENT; |
6286 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
6287 | struct io_uring_task *tctx = node->task->io_uring; | |
fbf23849 | 6288 | |
58f99373 PB |
6289 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
6290 | if (ret != -ENOENT) | |
6291 | break; | |
6292 | } | |
3b44b371 | 6293 | io_ring_submit_unlock(ctx, needs_lock); |
58f99373 | 6294 | done: |
58f99373 | 6295 | if (ret < 0) |
93d2bcd2 | 6296 | req_set_fail(req); |
505657bc | 6297 | io_req_complete_post(req, ret, 0); |
5262f567 JA |
6298 | return 0; |
6299 | } | |
6300 | ||
269bbe5f | 6301 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
6302 | const struct io_uring_sqe *sqe) |
6303 | { | |
61710e43 DA |
6304 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6305 | return -EINVAL; | |
26578cda | 6306 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
05f3fb3c JA |
6307 | return -EINVAL; |
6308 | ||
269bbe5f BM |
6309 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
6310 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
6311 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 6312 | return -EINVAL; |
269bbe5f | 6313 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
6314 | return 0; |
6315 | } | |
6316 | ||
889fca73 | 6317 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6318 | { |
6319 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 6320 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
c3bdad02 | 6321 | struct io_uring_rsrc_update2 up; |
05f3fb3c | 6322 | int ret; |
fbf23849 | 6323 | |
269bbe5f BM |
6324 | up.offset = req->rsrc_update.offset; |
6325 | up.data = req->rsrc_update.arg; | |
c3bdad02 PB |
6326 | up.nr = 0; |
6327 | up.tags = 0; | |
615cee49 | 6328 | up.resv = 0; |
05f3fb3c | 6329 | |
3b44b371 | 6330 | io_ring_submit_lock(ctx, needs_lock); |
fdecb662 | 6331 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
98f0b3b4 | 6332 | &up, req->rsrc_update.nr_args); |
3b44b371 | 6333 | io_ring_submit_unlock(ctx, needs_lock); |
05f3fb3c JA |
6334 | |
6335 | if (ret < 0) | |
93d2bcd2 | 6336 | req_set_fail(req); |
889fca73 | 6337 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
6338 | return 0; |
6339 | } | |
6340 | ||
bfe76559 | 6341 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 6342 | { |
d625c6ee | 6343 | switch (req->opcode) { |
e781573e | 6344 | case IORING_OP_NOP: |
bfe76559 | 6345 | return 0; |
f67676d1 JA |
6346 | case IORING_OP_READV: |
6347 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 6348 | case IORING_OP_READ: |
bfe76559 | 6349 | return io_read_prep(req, sqe); |
f67676d1 JA |
6350 | case IORING_OP_WRITEV: |
6351 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 6352 | case IORING_OP_WRITE: |
bfe76559 | 6353 | return io_write_prep(req, sqe); |
0969e783 | 6354 | case IORING_OP_POLL_ADD: |
bfe76559 | 6355 | return io_poll_add_prep(req, sqe); |
0969e783 | 6356 | case IORING_OP_POLL_REMOVE: |
c5de0036 | 6357 | return io_poll_update_prep(req, sqe); |
8ed8d3c3 | 6358 | case IORING_OP_FSYNC: |
1155c76a | 6359 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 6360 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 6361 | return io_sfr_prep(req, sqe); |
03b1230c | 6362 | case IORING_OP_SENDMSG: |
fddaface | 6363 | case IORING_OP_SEND: |
bfe76559 | 6364 | return io_sendmsg_prep(req, sqe); |
03b1230c | 6365 | case IORING_OP_RECVMSG: |
fddaface | 6366 | case IORING_OP_RECV: |
bfe76559 | 6367 | return io_recvmsg_prep(req, sqe); |
f499a021 | 6368 | case IORING_OP_CONNECT: |
bfe76559 | 6369 | return io_connect_prep(req, sqe); |
2d28390a | 6370 | case IORING_OP_TIMEOUT: |
bfe76559 | 6371 | return io_timeout_prep(req, sqe, false); |
b29472ee | 6372 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 6373 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 6374 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 6375 | return io_async_cancel_prep(req, sqe); |
2d28390a | 6376 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 6377 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 6378 | case IORING_OP_ACCEPT: |
bfe76559 | 6379 | return io_accept_prep(req, sqe); |
d63d1b5e | 6380 | case IORING_OP_FALLOCATE: |
bfe76559 | 6381 | return io_fallocate_prep(req, sqe); |
15b71abe | 6382 | case IORING_OP_OPENAT: |
bfe76559 | 6383 | return io_openat_prep(req, sqe); |
b5dba59e | 6384 | case IORING_OP_CLOSE: |
bfe76559 | 6385 | return io_close_prep(req, sqe); |
05f3fb3c | 6386 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 6387 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6388 | case IORING_OP_STATX: |
bfe76559 | 6389 | return io_statx_prep(req, sqe); |
4840e418 | 6390 | case IORING_OP_FADVISE: |
bfe76559 | 6391 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6392 | case IORING_OP_MADVISE: |
bfe76559 | 6393 | return io_madvise_prep(req, sqe); |
cebdb986 | 6394 | case IORING_OP_OPENAT2: |
bfe76559 | 6395 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6396 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6397 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6398 | case IORING_OP_SPLICE: |
bfe76559 | 6399 | return io_splice_prep(req, sqe); |
ddf0322d | 6400 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6401 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6402 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6403 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6404 | case IORING_OP_TEE: |
bfe76559 | 6405 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6406 | case IORING_OP_SHUTDOWN: |
6407 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6408 | case IORING_OP_RENAMEAT: |
6409 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6410 | case IORING_OP_UNLINKAT: |
6411 | return io_unlinkat_prep(req, sqe); | |
e34a02dc DK |
6412 | case IORING_OP_MKDIRAT: |
6413 | return io_mkdirat_prep(req, sqe); | |
7a8721f8 DK |
6414 | case IORING_OP_SYMLINKAT: |
6415 | return io_symlinkat_prep(req, sqe); | |
cf30da90 DK |
6416 | case IORING_OP_LINKAT: |
6417 | return io_linkat_prep(req, sqe); | |
f67676d1 JA |
6418 | } |
6419 | ||
bfe76559 PB |
6420 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6421 | req->opcode); | |
bd54b6fe | 6422 | return -EINVAL; |
bfe76559 PB |
6423 | } |
6424 | ||
93642ef8 | 6425 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 6426 | { |
b7e298d2 PB |
6427 | if (!io_op_defs[req->opcode].needs_async_setup) |
6428 | return 0; | |
d886e185 | 6429 | if (WARN_ON_ONCE(req_has_async_data(req))) |
b7e298d2 PB |
6430 | return -EFAULT; |
6431 | if (io_alloc_async_data(req)) | |
6432 | return -EAGAIN; | |
6433 | ||
93642ef8 PB |
6434 | switch (req->opcode) { |
6435 | case IORING_OP_READV: | |
93642ef8 PB |
6436 | return io_rw_prep_async(req, READ); |
6437 | case IORING_OP_WRITEV: | |
93642ef8 PB |
6438 | return io_rw_prep_async(req, WRITE); |
6439 | case IORING_OP_SENDMSG: | |
93642ef8 PB |
6440 | return io_sendmsg_prep_async(req); |
6441 | case IORING_OP_RECVMSG: | |
93642ef8 PB |
6442 | return io_recvmsg_prep_async(req); |
6443 | case IORING_OP_CONNECT: | |
6444 | return io_connect_prep_async(req); | |
6445 | } | |
b7e298d2 PB |
6446 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
6447 | req->opcode); | |
6448 | return -EFAULT; | |
f67676d1 JA |
6449 | } |
6450 | ||
9cf7c104 PB |
6451 | static u32 io_get_sequence(struct io_kiocb *req) |
6452 | { | |
a3dbdf54 | 6453 | u32 seq = req->ctx->cached_sq_head; |
9cf7c104 | 6454 | |
a3dbdf54 PB |
6455 | /* need original cached_sq_head, but it was increased for each req */ |
6456 | io_for_each_link(req, req) | |
6457 | seq--; | |
6458 | return seq; | |
9cf7c104 PB |
6459 | } |
6460 | ||
c072481d | 6461 | static __cold void io_drain_req(struct io_kiocb *req) |
de0617e4 | 6462 | { |
a197f664 | 6463 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6464 | struct io_defer_entry *de; |
f67676d1 | 6465 | int ret; |
e0eb71dc | 6466 | u32 seq = io_get_sequence(req); |
de0617e4 | 6467 | |
9d858b21 | 6468 | /* Still need defer if there is pending req in defer list. */ |
5e371265 | 6469 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) { |
e0eb71dc | 6470 | queue: |
5e371265 | 6471 | ctx->drain_active = false; |
e0eb71dc PB |
6472 | io_req_task_queue(req); |
6473 | return; | |
5e371265 | 6474 | } |
de0617e4 | 6475 | |
b7e298d2 | 6476 | ret = io_req_prep_async(req); |
e0eb71dc PB |
6477 | if (ret) { |
6478 | fail: | |
6479 | io_req_complete_failed(req, ret); | |
6480 | return; | |
6481 | } | |
cbdcb435 | 6482 | io_prep_async_link(req); |
27dc8338 | 6483 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
76cc33d7 | 6484 | if (!de) { |
1b48773f | 6485 | ret = -ENOMEM; |
e0eb71dc | 6486 | goto fail; |
76cc33d7 | 6487 | } |
2d28390a | 6488 | |
79ebeaee | 6489 | spin_lock(&ctx->completion_lock); |
9cf7c104 | 6490 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
79ebeaee | 6491 | spin_unlock(&ctx->completion_lock); |
27dc8338 | 6492 | kfree(de); |
e0eb71dc | 6493 | goto queue; |
de0617e4 JA |
6494 | } |
6495 | ||
915967f6 | 6496 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 6497 | de->req = req; |
9cf7c104 | 6498 | de->seq = seq; |
27dc8338 | 6499 | list_add_tail(&de->list, &ctx->defer_list); |
79ebeaee | 6500 | spin_unlock(&ctx->completion_lock); |
de0617e4 JA |
6501 | } |
6502 | ||
68fb8979 | 6503 | static void io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6504 | { |
0e1b6fe3 | 6505 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
30d51dd4 PB |
6506 | kfree(req->kbuf); |
6507 | req->kbuf = NULL; | |
99bc4c38 PB |
6508 | } |
6509 | ||
0e1b6fe3 PB |
6510 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6511 | switch (req->opcode) { | |
6512 | case IORING_OP_READV: | |
6513 | case IORING_OP_READ_FIXED: | |
6514 | case IORING_OP_READ: | |
6515 | case IORING_OP_WRITEV: | |
6516 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6517 | case IORING_OP_WRITE: { |
6518 | struct io_async_rw *io = req->async_data; | |
1dacb4df PB |
6519 | |
6520 | kfree(io->free_iovec); | |
0e1b6fe3 | 6521 | break; |
e8c2bc1f | 6522 | } |
0e1b6fe3 | 6523 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6524 | case IORING_OP_SENDMSG: { |
6525 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
6526 | |
6527 | kfree(io->free_iov); | |
0e1b6fe3 | 6528 | break; |
e8c2bc1f | 6529 | } |
0e1b6fe3 PB |
6530 | case IORING_OP_SPLICE: |
6531 | case IORING_OP_TEE: | |
e1d767f0 PB |
6532 | if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED)) |
6533 | io_put_file(req->splice.file_in); | |
0e1b6fe3 | 6534 | break; |
f3cd4850 JA |
6535 | case IORING_OP_OPENAT: |
6536 | case IORING_OP_OPENAT2: | |
6537 | if (req->open.filename) | |
6538 | putname(req->open.filename); | |
6539 | break; | |
80a261fd JA |
6540 | case IORING_OP_RENAMEAT: |
6541 | putname(req->rename.oldpath); | |
6542 | putname(req->rename.newpath); | |
6543 | break; | |
14a1143b JA |
6544 | case IORING_OP_UNLINKAT: |
6545 | putname(req->unlink.filename); | |
6546 | break; | |
e34a02dc DK |
6547 | case IORING_OP_MKDIRAT: |
6548 | putname(req->mkdir.filename); | |
6549 | break; | |
7a8721f8 DK |
6550 | case IORING_OP_SYMLINKAT: |
6551 | putname(req->symlink.oldpath); | |
6552 | putname(req->symlink.newpath); | |
6553 | break; | |
cf30da90 DK |
6554 | case IORING_OP_LINKAT: |
6555 | putname(req->hardlink.oldpath); | |
6556 | putname(req->hardlink.newpath); | |
6557 | break; | |
0e1b6fe3 | 6558 | } |
99bc4c38 | 6559 | } |
75652a30 JA |
6560 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
6561 | kfree(req->apoll->double_poll); | |
6562 | kfree(req->apoll); | |
6563 | req->apoll = NULL; | |
6564 | } | |
3a0a6902 PB |
6565 | if (req->flags & REQ_F_INFLIGHT) { |
6566 | struct io_uring_task *tctx = req->task->io_uring; | |
6567 | ||
6568 | atomic_dec(&tctx->inflight_tracked); | |
3a0a6902 | 6569 | } |
c854357b | 6570 | if (req->flags & REQ_F_CREDS) |
b8e64b53 | 6571 | put_cred(req->creds); |
d886e185 PB |
6572 | if (req->flags & REQ_F_ASYNC_DATA) { |
6573 | kfree(req->async_data); | |
6574 | req->async_data = NULL; | |
6575 | } | |
c854357b | 6576 | req->flags &= ~IO_REQ_CLEAN_FLAGS; |
99bc4c38 PB |
6577 | } |
6578 | ||
889fca73 | 6579 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 6580 | { |
5730b27e | 6581 | const struct cred *creds = NULL; |
d625c6ee | 6582 | int ret; |
2b188cc1 | 6583 | |
6878b40e | 6584 | if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred())) |
c10d1f98 | 6585 | creds = override_creds(req->creds); |
5730b27e | 6586 | |
d625c6ee | 6587 | switch (req->opcode) { |
2b188cc1 | 6588 | case IORING_OP_NOP: |
889fca73 | 6589 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
6590 | break; |
6591 | case IORING_OP_READV: | |
edafccee | 6592 | case IORING_OP_READ_FIXED: |
3a6820f2 | 6593 | case IORING_OP_READ: |
889fca73 | 6594 | ret = io_read(req, issue_flags); |
edafccee | 6595 | break; |
3529d8c2 | 6596 | case IORING_OP_WRITEV: |
edafccee | 6597 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 6598 | case IORING_OP_WRITE: |
889fca73 | 6599 | ret = io_write(req, issue_flags); |
2b188cc1 | 6600 | break; |
c992fe29 | 6601 | case IORING_OP_FSYNC: |
45d189c6 | 6602 | ret = io_fsync(req, issue_flags); |
c992fe29 | 6603 | break; |
221c5eb2 | 6604 | case IORING_OP_POLL_ADD: |
61e98203 | 6605 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
6606 | break; |
6607 | case IORING_OP_POLL_REMOVE: | |
c5de0036 | 6608 | ret = io_poll_update(req, issue_flags); |
221c5eb2 | 6609 | break; |
5d17b4a4 | 6610 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 6611 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 6612 | break; |
0fa03c62 | 6613 | case IORING_OP_SENDMSG: |
889fca73 | 6614 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 6615 | break; |
fddaface | 6616 | case IORING_OP_SEND: |
889fca73 | 6617 | ret = io_send(req, issue_flags); |
0fa03c62 | 6618 | break; |
aa1fa28f | 6619 | case IORING_OP_RECVMSG: |
889fca73 | 6620 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 6621 | break; |
fddaface | 6622 | case IORING_OP_RECV: |
889fca73 | 6623 | ret = io_recv(req, issue_flags); |
aa1fa28f | 6624 | break; |
5262f567 | 6625 | case IORING_OP_TIMEOUT: |
61e98203 | 6626 | ret = io_timeout(req, issue_flags); |
5262f567 | 6627 | break; |
11365043 | 6628 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 6629 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 6630 | break; |
17f2fe35 | 6631 | case IORING_OP_ACCEPT: |
889fca73 | 6632 | ret = io_accept(req, issue_flags); |
17f2fe35 | 6633 | break; |
f8e85cf2 | 6634 | case IORING_OP_CONNECT: |
889fca73 | 6635 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 6636 | break; |
62755e35 | 6637 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 6638 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 6639 | break; |
d63d1b5e | 6640 | case IORING_OP_FALLOCATE: |
45d189c6 | 6641 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 6642 | break; |
15b71abe | 6643 | case IORING_OP_OPENAT: |
45d189c6 | 6644 | ret = io_openat(req, issue_flags); |
15b71abe | 6645 | break; |
b5dba59e | 6646 | case IORING_OP_CLOSE: |
889fca73 | 6647 | ret = io_close(req, issue_flags); |
b5dba59e | 6648 | break; |
05f3fb3c | 6649 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6650 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6651 | break; |
eddc7ef5 | 6652 | case IORING_OP_STATX: |
45d189c6 | 6653 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6654 | break; |
4840e418 | 6655 | case IORING_OP_FADVISE: |
45d189c6 | 6656 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6657 | break; |
c1ca757b | 6658 | case IORING_OP_MADVISE: |
45d189c6 | 6659 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6660 | break; |
cebdb986 | 6661 | case IORING_OP_OPENAT2: |
45d189c6 | 6662 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6663 | break; |
3e4827b0 | 6664 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6665 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6666 | break; |
7d67af2c | 6667 | case IORING_OP_SPLICE: |
45d189c6 | 6668 | ret = io_splice(req, issue_flags); |
7d67af2c | 6669 | break; |
ddf0322d | 6670 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6671 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6672 | break; |
067524e9 | 6673 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6674 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6675 | break; |
f2a8d5c7 | 6676 | case IORING_OP_TEE: |
45d189c6 | 6677 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6678 | break; |
36f4fa68 | 6679 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6680 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6681 | break; |
80a261fd | 6682 | case IORING_OP_RENAMEAT: |
45d189c6 | 6683 | ret = io_renameat(req, issue_flags); |
80a261fd | 6684 | break; |
14a1143b | 6685 | case IORING_OP_UNLINKAT: |
45d189c6 | 6686 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6687 | break; |
e34a02dc DK |
6688 | case IORING_OP_MKDIRAT: |
6689 | ret = io_mkdirat(req, issue_flags); | |
6690 | break; | |
7a8721f8 DK |
6691 | case IORING_OP_SYMLINKAT: |
6692 | ret = io_symlinkat(req, issue_flags); | |
6693 | break; | |
cf30da90 DK |
6694 | case IORING_OP_LINKAT: |
6695 | ret = io_linkat(req, issue_flags); | |
6696 | break; | |
2b188cc1 JA |
6697 | default: |
6698 | ret = -EINVAL; | |
6699 | break; | |
6700 | } | |
6701 | ||
5730b27e JA |
6702 | if (creds) |
6703 | revert_creds(creds); | |
def596e9 JA |
6704 | if (ret) |
6705 | return ret; | |
b532576e | 6706 | /* If the op doesn't have a file, we're not polling for it */ |
9983028e | 6707 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
9882131c | 6708 | io_iopoll_req_issued(req, issue_flags); |
def596e9 JA |
6709 | |
6710 | return 0; | |
2b188cc1 JA |
6711 | } |
6712 | ||
ebc11b6c PB |
6713 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
6714 | { | |
6715 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6716 | ||
6717 | req = io_put_req_find_next(req); | |
6718 | return req ? &req->work : NULL; | |
6719 | } | |
6720 | ||
5280f7e5 | 6721 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6722 | { |
6723 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
d01905db PB |
6724 | unsigned int issue_flags = IO_URING_F_UNLOCKED; |
6725 | bool needs_poll = false; | |
6df1db6b | 6726 | struct io_kiocb *timeout; |
561fb04a | 6727 | int ret = 0; |
2b188cc1 | 6728 | |
48dcd38d PB |
6729 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
6730 | if (!(req->flags & REQ_F_REFCOUNT)) | |
6731 | __io_req_set_refcount(req, 2); | |
6732 | else | |
6733 | req_ref_get(req); | |
5d5901a3 | 6734 | |
6df1db6b PB |
6735 | timeout = io_prep_linked_timeout(req); |
6736 | if (timeout) | |
6737 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6738 | |
dadebc35 | 6739 | /* either cancelled or io-wq is dying, so don't touch tctx->iowq */ |
d01905db PB |
6740 | if (work->flags & IO_WQ_WORK_CANCEL) { |
6741 | io_req_task_queue_fail(req, -ECANCELED); | |
6742 | return; | |
6743 | } | |
31b51510 | 6744 | |
d01905db PB |
6745 | if (req->flags & REQ_F_FORCE_ASYNC) { |
6746 | needs_poll = req->file && file_can_poll(req->file); | |
6747 | if (needs_poll) | |
6748 | issue_flags |= IO_URING_F_NONBLOCK; | |
6749 | } | |
90fa0288 | 6750 | |
d01905db PB |
6751 | do { |
6752 | ret = io_issue_sqe(req, issue_flags); | |
6753 | if (ret != -EAGAIN) | |
6754 | break; | |
6755 | /* | |
6756 | * We can get EAGAIN for iopolled IO even though we're | |
6757 | * forcing a sync submission from here, since we can't | |
6758 | * wait for request slots on the block side. | |
6759 | */ | |
6760 | if (!needs_poll) { | |
6761 | cond_resched(); | |
6762 | continue; | |
90fa0288 HX |
6763 | } |
6764 | ||
d01905db PB |
6765 | if (io_arm_poll_handler(req) == IO_APOLL_OK) |
6766 | return; | |
6767 | /* aborted or ready, in either case retry blocking */ | |
6768 | needs_poll = false; | |
6769 | issue_flags &= ~IO_URING_F_NONBLOCK; | |
6770 | } while (1); | |
31b51510 | 6771 | |
a3df7698 | 6772 | /* avoid locking problems by failing it from a clean context */ |
5d5901a3 | 6773 | if (ret) |
a3df7698 | 6774 | io_req_task_queue_fail(req, ret); |
2b188cc1 JA |
6775 | } |
6776 | ||
aeca241b | 6777 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
042b0d85 | 6778 | unsigned i) |
65e19f54 | 6779 | { |
042b0d85 | 6780 | return &table->files[i]; |
dafecf19 PB |
6781 | } |
6782 | ||
65e19f54 JA |
6783 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6784 | int index) | |
6785 | { | |
aeca241b | 6786 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
65e19f54 | 6787 | |
a04b0ac0 | 6788 | return (struct file *) (slot->file_ptr & FFS_MASK); |
65e19f54 JA |
6789 | } |
6790 | ||
a04b0ac0 | 6791 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
9a321c98 PB |
6792 | { |
6793 | unsigned long file_ptr = (unsigned long) file; | |
6794 | ||
88459b50 | 6795 | file_ptr |= io_file_get_flags(file); |
a04b0ac0 | 6796 | file_slot->file_ptr = file_ptr; |
65e19f54 JA |
6797 | } |
6798 | ||
ac177053 PB |
6799 | static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx, |
6800 | struct io_kiocb *req, int fd) | |
09bb8394 | 6801 | { |
8da11c19 | 6802 | struct file *file; |
ac177053 | 6803 | unsigned long file_ptr; |
09bb8394 | 6804 | |
ac177053 PB |
6805 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
6806 | return NULL; | |
6807 | fd = array_index_nospec(fd, ctx->nr_user_files); | |
6808 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; | |
6809 | file = (struct file *) (file_ptr & FFS_MASK); | |
6810 | file_ptr &= ~FFS_MASK; | |
6811 | /* mask in overlapping REQ_F and FFS bits */ | |
35645ac3 | 6812 | req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT); |
a46be971 | 6813 | io_req_set_rsrc_node(req, ctx); |
ac177053 PB |
6814 | return file; |
6815 | } | |
d44f554e | 6816 | |
ac177053 | 6817 | static struct file *io_file_get_normal(struct io_ring_ctx *ctx, |
ac177053 PB |
6818 | struct io_kiocb *req, int fd) |
6819 | { | |
62906e89 | 6820 | struct file *file = fget(fd); |
ac177053 PB |
6821 | |
6822 | trace_io_uring_file_get(ctx, fd); | |
09bb8394 | 6823 | |
ac177053 PB |
6824 | /* we don't allow fixed io_uring files */ |
6825 | if (file && unlikely(file->f_op == &io_uring_fops)) | |
6826 | io_req_track_inflight(req); | |
8371adf5 | 6827 | return file; |
09bb8394 JA |
6828 | } |
6829 | ||
ac177053 | 6830 | static inline struct file *io_file_get(struct io_ring_ctx *ctx, |
ac177053 PB |
6831 | struct io_kiocb *req, int fd, bool fixed) |
6832 | { | |
6833 | if (fixed) | |
6834 | return io_file_get_fixed(ctx, req, fd); | |
6835 | else | |
62906e89 | 6836 | return io_file_get_normal(ctx, req, fd); |
ac177053 PB |
6837 | } |
6838 | ||
f237c30a | 6839 | static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked) |
89b263f6 JA |
6840 | { |
6841 | struct io_kiocb *prev = req->timeout.prev; | |
8cb01fac | 6842 | int ret; |
89b263f6 JA |
6843 | |
6844 | if (prev) { | |
8cb01fac | 6845 | ret = io_try_cancel_userdata(req, prev->user_data); |
505657bc | 6846 | io_req_complete_post(req, ret ?: -ETIME, 0); |
89b263f6 | 6847 | io_put_req(prev); |
89b263f6 JA |
6848 | } else { |
6849 | io_req_complete_post(req, -ETIME, 0); | |
6850 | } | |
6851 | } | |
6852 | ||
2665abfd | 6853 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6854 | { |
ad8a48ac JA |
6855 | struct io_timeout_data *data = container_of(timer, |
6856 | struct io_timeout_data, timer); | |
90cd7e42 | 6857 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6858 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6859 | unsigned long flags; |
2665abfd | 6860 | |
89b263f6 | 6861 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
90cd7e42 PB |
6862 | prev = req->timeout.head; |
6863 | req->timeout.head = NULL; | |
2665abfd JA |
6864 | |
6865 | /* | |
6866 | * We don't expect the list to be empty, that will only happen if we | |
6867 | * race with the completion of the linked work. | |
6868 | */ | |
447c19f3 | 6869 | if (prev) { |
f2f87370 | 6870 | io_remove_next_linked(prev); |
447c19f3 PB |
6871 | if (!req_ref_inc_not_zero(prev)) |
6872 | prev = NULL; | |
6873 | } | |
ef9dd637 | 6874 | list_del(&req->timeout.list); |
89b263f6 JA |
6875 | req->timeout.prev = prev; |
6876 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); | |
2665abfd | 6877 | |
89b263f6 JA |
6878 | req->io_task_work.func = io_req_task_link_timeout; |
6879 | io_req_task_work_add(req); | |
2665abfd JA |
6880 | return HRTIMER_NORESTART; |
6881 | } | |
6882 | ||
de968c18 | 6883 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6884 | { |
de968c18 PB |
6885 | struct io_ring_ctx *ctx = req->ctx; |
6886 | ||
89b263f6 | 6887 | spin_lock_irq(&ctx->timeout_lock); |
76a46e06 | 6888 | /* |
f2f87370 PB |
6889 | * If the back reference is NULL, then our linked request finished |
6890 | * before we got a chance to setup the timer | |
76a46e06 | 6891 | */ |
90cd7e42 | 6892 | if (req->timeout.head) { |
e8c2bc1f | 6893 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6894 | |
ad8a48ac JA |
6895 | data->timer.function = io_link_timeout_fn; |
6896 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6897 | data->mode); | |
ef9dd637 | 6898 | list_add_tail(&req->timeout.list, &ctx->ltimeout_list); |
2665abfd | 6899 | } |
89b263f6 | 6900 | spin_unlock_irq(&ctx->timeout_lock); |
2665abfd | 6901 | /* drop submission reference */ |
76a46e06 JA |
6902 | io_put_req(req); |
6903 | } | |
2665abfd | 6904 | |
d475a9a6 PB |
6905 | static void io_queue_sqe_arm_apoll(struct io_kiocb *req) |
6906 | __must_hold(&req->ctx->uring_lock) | |
6907 | { | |
6908 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); | |
6909 | ||
6910 | switch (io_arm_poll_handler(req)) { | |
6911 | case IO_APOLL_READY: | |
6912 | if (linked_timeout) { | |
6913 | io_unprep_linked_timeout(req); | |
6914 | linked_timeout = NULL; | |
6915 | } | |
6916 | io_req_task_queue(req); | |
6917 | break; | |
6918 | case IO_APOLL_ABORTED: | |
6919 | /* | |
6920 | * Queued up for async execution, worker will release | |
6921 | * submit reference when the iocb is actually submitted. | |
6922 | */ | |
6923 | io_queue_async_work(req, NULL); | |
6924 | break; | |
6925 | } | |
6926 | ||
6927 | if (linked_timeout) | |
6928 | io_queue_linked_timeout(linked_timeout); | |
6929 | } | |
6930 | ||
6931 | static inline void __io_queue_sqe(struct io_kiocb *req) | |
282cdc86 | 6932 | __must_hold(&req->ctx->uring_lock) |
2b188cc1 | 6933 | { |
906c6caa | 6934 | struct io_kiocb *linked_timeout; |
e0c5c576 | 6935 | int ret; |
2b188cc1 | 6936 | |
c5eef2b9 | 6937 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 6938 | |
fff4e40e PB |
6939 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
6940 | io_req_add_compl_list(req); | |
d9f9d284 | 6941 | return; |
fff4e40e | 6942 | } |
491381ce JA |
6943 | /* |
6944 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6945 | * doesn't support non-blocking read/write attempts | |
6946 | */ | |
1840038e | 6947 | if (likely(!ret)) { |
906c6caa PB |
6948 | linked_timeout = io_prep_linked_timeout(req); |
6949 | if (linked_timeout) | |
6950 | io_queue_linked_timeout(linked_timeout); | |
1840038e | 6951 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
d475a9a6 | 6952 | io_queue_sqe_arm_apoll(req); |
0d63c148 | 6953 | } else { |
f41db273 | 6954 | io_req_complete_failed(req, ret); |
9e645e11 | 6955 | } |
2b188cc1 JA |
6956 | } |
6957 | ||
4652fe3f | 6958 | static void io_queue_sqe_fallback(struct io_kiocb *req) |
282cdc86 | 6959 | __must_hold(&req->ctx->uring_lock) |
4fe2c963 | 6960 | { |
4652fe3f | 6961 | if (req->flags & REQ_F_FAIL) { |
c6d3d9cb | 6962 | io_req_complete_fail_submit(req); |
e0eb71dc PB |
6963 | } else if (unlikely(req->ctx->drain_active)) { |
6964 | io_drain_req(req); | |
76cc33d7 PB |
6965 | } else { |
6966 | int ret = io_req_prep_async(req); | |
6967 | ||
6968 | if (unlikely(ret)) | |
6969 | io_req_complete_failed(req, ret); | |
6970 | else | |
f237c30a | 6971 | io_queue_async_work(req, NULL); |
ce35a47a | 6972 | } |
4fe2c963 JL |
6973 | } |
6974 | ||
4652fe3f PB |
6975 | static inline void io_queue_sqe(struct io_kiocb *req) |
6976 | __must_hold(&req->ctx->uring_lock) | |
6977 | { | |
6978 | if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) | |
6979 | __io_queue_sqe(req); | |
6980 | else | |
6981 | io_queue_sqe_fallback(req); | |
6982 | } | |
6983 | ||
b16fed66 PB |
6984 | /* |
6985 | * Check SQE restrictions (opcode and flags). | |
6986 | * | |
6987 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6988 | */ | |
6989 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6990 | struct io_kiocb *req, | |
6991 | unsigned int sqe_flags) | |
4fe2c963 | 6992 | { |
b16fed66 PB |
6993 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
6994 | return false; | |
6995 | ||
6996 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6997 | ctx->restrictions.sqe_flags_required) | |
6998 | return false; | |
6999 | ||
7000 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
7001 | ctx->restrictions.sqe_flags_required)) | |
7002 | return false; | |
7003 | ||
7004 | return true; | |
4fe2c963 JL |
7005 | } |
7006 | ||
22b2ca31 PB |
7007 | static void io_init_req_drain(struct io_kiocb *req) |
7008 | { | |
7009 | struct io_ring_ctx *ctx = req->ctx; | |
7010 | struct io_kiocb *head = ctx->submit_state.link.head; | |
7011 | ||
7012 | ctx->drain_active = true; | |
7013 | if (head) { | |
7014 | /* | |
7015 | * If we need to drain a request in the middle of a link, drain | |
7016 | * the head request and the next request/link after the current | |
7017 | * link. Considering sequential execution of links, | |
7018 | * IOSQE_IO_DRAIN will be maintained for every request of our | |
7019 | * link. | |
7020 | */ | |
7021 | head->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC; | |
7022 | ctx->drain_next = true; | |
7023 | } | |
7024 | } | |
7025 | ||
b16fed66 PB |
7026 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
7027 | const struct io_uring_sqe *sqe) | |
282cdc86 | 7028 | __must_hold(&ctx->uring_lock) |
b16fed66 | 7029 | { |
b16fed66 | 7030 | unsigned int sqe_flags; |
fc0ae024 | 7031 | int personality; |
4a04d1d1 | 7032 | u8 opcode; |
b16fed66 | 7033 | |
864ea921 | 7034 | /* req is partially pre-initialised, see io_preinit_req() */ |
4a04d1d1 | 7035 | req->opcode = opcode = READ_ONCE(sqe->opcode); |
b16fed66 PB |
7036 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
7037 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
7038 | req->user_data = READ_ONCE(sqe->user_data); | |
b16fed66 | 7039 | req->file = NULL; |
b16fed66 | 7040 | req->fixed_rsrc_refs = NULL; |
b16fed66 | 7041 | req->task = current; |
b16fed66 | 7042 | |
4a04d1d1 PB |
7043 | if (unlikely(opcode >= IORING_OP_LAST)) { |
7044 | req->opcode = 0; | |
b16fed66 | 7045 | return -EINVAL; |
4a04d1d1 | 7046 | } |
68fe256a PB |
7047 | if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) { |
7048 | /* enforce forwards compatibility on users */ | |
7049 | if (sqe_flags & ~SQE_VALID_FLAGS) | |
7050 | return -EINVAL; | |
7051 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
4a04d1d1 | 7052 | !io_op_defs[opcode].buffer_select) |
68fe256a | 7053 | return -EOPNOTSUPP; |
22b2ca31 PB |
7054 | if (sqe_flags & IOSQE_IO_DRAIN) |
7055 | io_init_req_drain(req); | |
2a56a9bd PB |
7056 | } |
7057 | if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) { | |
7058 | if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags)) | |
7059 | return -EACCES; | |
7060 | /* knock it to the slow queue path, will be drained there */ | |
7061 | if (ctx->drain_active) | |
7062 | req->flags |= REQ_F_FORCE_ASYNC; | |
7063 | /* if there is no link, we're at "next" request and need to drain */ | |
7064 | if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) { | |
7065 | ctx->drain_next = false; | |
7066 | ctx->drain_active = true; | |
22b2ca31 | 7067 | req->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC; |
2a56a9bd | 7068 | } |
68fe256a | 7069 | } |
b16fed66 | 7070 | |
4a04d1d1 | 7071 | if (io_op_defs[opcode].needs_file) { |
6d63416d PB |
7072 | struct io_submit_state *state = &ctx->submit_state; |
7073 | ||
7074 | /* | |
7075 | * Plug now if we have more than 2 IO left after this, and the | |
7076 | * target is potentially a read/write to block based storage. | |
7077 | */ | |
4a04d1d1 | 7078 | if (state->need_plug && io_op_defs[opcode].plug) { |
6d63416d PB |
7079 | state->plug_started = true; |
7080 | state->need_plug = false; | |
5ca7a8b3 | 7081 | blk_start_plug_nr_ios(&state->plug, state->submit_nr); |
6d63416d PB |
7082 | } |
7083 | ||
62906e89 | 7084 | req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd), |
ac177053 | 7085 | (sqe_flags & IOSQE_FIXED_FILE)); |
b16fed66 | 7086 | if (unlikely(!req->file)) |
fc0ae024 | 7087 | return -EBADF; |
b16fed66 | 7088 | } |
fc0ae024 | 7089 | |
4a04d1d1 PB |
7090 | personality = READ_ONCE(sqe->personality); |
7091 | if (personality) { | |
7092 | req->creds = xa_load(&ctx->personalities, personality); | |
7093 | if (!req->creds) | |
7094 | return -EINVAL; | |
7095 | get_cred(req->creds); | |
7096 | req->flags |= REQ_F_CREDS; | |
7097 | } | |
7098 | ||
fc0ae024 | 7099 | return io_req_prep(req, sqe); |
b16fed66 PB |
7100 | } |
7101 | ||
a6b8cadc | 7102 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 7103 | const struct io_uring_sqe *sqe) |
282cdc86 | 7104 | __must_hold(&ctx->uring_lock) |
9e645e11 | 7105 | { |
a1ab7b35 | 7106 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 7107 | int ret; |
9e645e11 | 7108 | |
a6b8cadc PB |
7109 | ret = io_init_req(ctx, req, sqe); |
7110 | if (unlikely(ret)) { | |
a87acfde JA |
7111 | trace_io_uring_req_failed(sqe, ret); |
7112 | ||
a8295b98 | 7113 | /* fail even hard links since we don't submit */ |
de59bc10 | 7114 | if (link->head) { |
a8295b98 HX |
7115 | /* |
7116 | * we can judge a link req is failed or cancelled by if | |
7117 | * REQ_F_FAIL is set, but the head is an exception since | |
7118 | * it may be set REQ_F_FAIL because of other req's failure | |
7119 | * so let's leverage req->result to distinguish if a head | |
7120 | * is set REQ_F_FAIL because of its failure or other req's | |
7121 | * failure so that we can set the correct ret code for it. | |
7122 | * init result here to avoid affecting the normal path. | |
7123 | */ | |
7124 | if (!(link->head->flags & REQ_F_FAIL)) | |
7125 | req_fail_link_node(link->head, -ECANCELED); | |
7126 | } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { | |
7127 | /* | |
7128 | * the current req is a normal req, we should return | |
7129 | * error and thus break the submittion loop. | |
7130 | */ | |
7131 | io_req_complete_failed(req, ret); | |
7132 | return ret; | |
de59bc10 | 7133 | } |
a8295b98 | 7134 | req_fail_link_node(req, ret); |
a6b8cadc | 7135 | } |
441b8a78 | 7136 | |
be7053b7 | 7137 | /* don't need @sqe from now on */ |
236daeae OL |
7138 | trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data, |
7139 | req->flags, true, | |
7140 | ctx->flags & IORING_SETUP_SQPOLL); | |
a6b8cadc | 7141 | |
9e645e11 JA |
7142 | /* |
7143 | * If we already have a head request, queue this one for async | |
7144 | * submittal once the head completes. If we don't have a head but | |
7145 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
7146 | * submitted sync once the chain is complete. If none of those | |
7147 | * conditions are true (normal request), then just queue it. | |
7148 | */ | |
863e0560 PB |
7149 | if (link->head) { |
7150 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 7151 | |
a8295b98 HX |
7152 | if (!(req->flags & REQ_F_FAIL)) { |
7153 | ret = io_req_prep_async(req); | |
7154 | if (unlikely(ret)) { | |
7155 | req_fail_link_node(req, ret); | |
7156 | if (!(head->flags & REQ_F_FAIL)) | |
7157 | req_fail_link_node(head, -ECANCELED); | |
7158 | } | |
7159 | } | |
9d76377f | 7160 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 7161 | link->last->link = req; |
863e0560 | 7162 | link->last = req; |
32fe525b | 7163 | |
f15a3431 PB |
7164 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) |
7165 | return 0; | |
32fe525b | 7166 | /* last request of a link, enqueue the link */ |
f15a3431 PB |
7167 | link->head = NULL; |
7168 | req = head; | |
7169 | } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { | |
7170 | link->head = req; | |
7171 | link->last = req; | |
7172 | return 0; | |
9e645e11 | 7173 | } |
2e6e1fde | 7174 | |
f15a3431 | 7175 | io_queue_sqe(req); |
1d4240cc | 7176 | return 0; |
9e645e11 JA |
7177 | } |
7178 | ||
9a56a232 JA |
7179 | /* |
7180 | * Batched submission is done, ensure local IO is flushed out. | |
7181 | */ | |
553deffd | 7182 | static void io_submit_state_end(struct io_ring_ctx *ctx) |
9a56a232 | 7183 | { |
553deffd PB |
7184 | struct io_submit_state *state = &ctx->submit_state; |
7185 | ||
a1ab7b35 | 7186 | if (state->link.head) |
de59bc10 | 7187 | io_queue_sqe(state->link.head); |
553deffd | 7188 | /* flush only after queuing links as they can generate completions */ |
c450178d | 7189 | io_submit_flush_completions(ctx); |
27926b68 JA |
7190 | if (state->plug_started) |
7191 | blk_finish_plug(&state->plug); | |
9a56a232 JA |
7192 | } |
7193 | ||
7194 | /* | |
7195 | * Start submission side cache. | |
7196 | */ | |
7197 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 7198 | unsigned int max_ios) |
9a56a232 | 7199 | { |
27926b68 | 7200 | state->plug_started = false; |
4b628aeb | 7201 | state->need_plug = max_ios > 2; |
5ca7a8b3 | 7202 | state->submit_nr = max_ios; |
a1ab7b35 PB |
7203 | /* set only head, no need to init link_last in advance */ |
7204 | state->link.head = NULL; | |
9a56a232 JA |
7205 | } |
7206 | ||
2b188cc1 JA |
7207 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
7208 | { | |
75b28aff | 7209 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 7210 | |
caf582c6 PB |
7211 | /* |
7212 | * Ensure any loads from the SQEs are done at this point, | |
7213 | * since once we write the new head, the application could | |
7214 | * write new data to them. | |
7215 | */ | |
7216 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
7217 | } |
7218 | ||
2b188cc1 | 7219 | /* |
dd9ae8a0 | 7220 | * Fetch an sqe, if one is available. Note this returns a pointer to memory |
2b188cc1 JA |
7221 | * that is mapped by userspace. This means that care needs to be taken to |
7222 | * ensure that reads are stable, as we cannot rely on userspace always | |
7223 | * being a good citizen. If members of the sqe are validated and then later | |
7224 | * used, it's important that those reads are done through READ_ONCE() to | |
7225 | * prevent a re-load down the line. | |
7226 | */ | |
709b302f | 7227 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 7228 | { |
ea5ab3b5 | 7229 | unsigned head, mask = ctx->sq_entries - 1; |
17d3aeb3 | 7230 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
2b188cc1 JA |
7231 | |
7232 | /* | |
7233 | * The cached sq head (or cq tail) serves two purposes: | |
7234 | * | |
7235 | * 1) allows us to batch the cost of updating the user visible | |
7236 | * head updates. | |
7237 | * 2) allows the kernel side to track the head on its own, even | |
7238 | * though the application is the one updating it. | |
7239 | */ | |
17d3aeb3 | 7240 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
709b302f PB |
7241 | if (likely(head < ctx->sq_entries)) |
7242 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
7243 | |
7244 | /* drop invalid entries */ | |
15641e42 PB |
7245 | ctx->cq_extra--; |
7246 | WRITE_ONCE(ctx->rings->sq_dropped, | |
7247 | READ_ONCE(ctx->rings->sq_dropped) + 1); | |
709b302f PB |
7248 | return NULL; |
7249 | } | |
7250 | ||
0f212204 | 7251 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
282cdc86 | 7252 | __must_hold(&ctx->uring_lock) |
6c271ce2 | 7253 | { |
69629809 | 7254 | unsigned int entries = io_sqring_entries(ctx); |
46c4e16a | 7255 | int submitted = 0; |
6c271ce2 | 7256 | |
51d48dab | 7257 | if (unlikely(!entries)) |
69629809 | 7258 | return 0; |
ee7d46d9 | 7259 | /* make sure SQ entry isn't read before tail */ |
69629809 | 7260 | nr = min3(nr, ctx->sq_entries, entries); |
9a10867a | 7261 | io_get_task_refs(nr); |
6c271ce2 | 7262 | |
ba88ff11 | 7263 | io_submit_state_start(&ctx->submit_state, nr); |
69629809 | 7264 | do { |
3529d8c2 | 7265 | const struct io_uring_sqe *sqe; |
196be95c | 7266 | struct io_kiocb *req; |
fb5ccc98 | 7267 | |
a33ae9ce | 7268 | if (unlikely(!io_alloc_req_refill(ctx))) { |
196be95c PB |
7269 | if (!submitted) |
7270 | submitted = -EAGAIN; | |
fb5ccc98 | 7271 | break; |
196be95c | 7272 | } |
a33ae9ce | 7273 | req = io_alloc_req(ctx); |
4fccfcbb PB |
7274 | sqe = io_get_sqe(ctx); |
7275 | if (unlikely(!sqe)) { | |
c2b6c6bc | 7276 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
4fccfcbb PB |
7277 | break; |
7278 | } | |
d3656344 JA |
7279 | /* will complete beyond this point, count as submitted */ |
7280 | submitted++; | |
a1ab7b35 | 7281 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 7282 | break; |
69629809 | 7283 | } while (submitted < nr); |
6c271ce2 | 7284 | |
9466f437 PB |
7285 | if (unlikely(submitted != nr)) { |
7286 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 | 7287 | int unused = nr - ref_used; |
9466f437 | 7288 | |
09899b19 | 7289 | current->io_uring->cached_refs += unused; |
9466f437 | 7290 | } |
6c271ce2 | 7291 | |
553deffd | 7292 | io_submit_state_end(ctx); |
ae9428ca PB |
7293 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
7294 | io_commit_sqring(ctx); | |
7295 | ||
6c271ce2 JA |
7296 | return submitted; |
7297 | } | |
7298 | ||
e4b6d902 PB |
7299 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
7300 | { | |
7301 | return READ_ONCE(sqd->state); | |
7302 | } | |
7303 | ||
23b3628e XW |
7304 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
7305 | { | |
7306 | /* Tell userspace we may need a wakeup call */ | |
79ebeaee | 7307 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7308 | WRITE_ONCE(ctx->rings->sq_flags, |
7309 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7310 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7311 | } |
7312 | ||
7313 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
7314 | { | |
79ebeaee | 7315 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7316 | WRITE_ONCE(ctx->rings->sq_flags, |
7317 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7318 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7319 | } |
7320 | ||
08369246 | 7321 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 7322 | { |
c8d1ba58 | 7323 | unsigned int to_submit; |
bdcd3eab | 7324 | int ret = 0; |
6c271ce2 | 7325 | |
c8d1ba58 | 7326 | to_submit = io_sqring_entries(ctx); |
e95eee2d | 7327 | /* if we're handling multiple rings, cap submit size for fairness */ |
4ce8ad95 OL |
7328 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
7329 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; | |
e95eee2d | 7330 | |
5eef4e87 | 7331 | if (!wq_list_empty(&ctx->iopoll_list) || to_submit) { |
948e1947 PB |
7332 | const struct cred *creds = NULL; |
7333 | ||
7334 | if (ctx->sq_creds != current_cred()) | |
7335 | creds = override_creds(ctx->sq_creds); | |
a4c0b3de | 7336 | |
c8d1ba58 | 7337 | mutex_lock(&ctx->uring_lock); |
5eef4e87 | 7338 | if (!wq_list_empty(&ctx->iopoll_list)) |
5ba3c874 | 7339 | io_do_iopoll(ctx, true); |
906a3c6f | 7340 | |
3b763ba1 PB |
7341 | /* |
7342 | * Don't submit if refs are dying, good for io_uring_register(), | |
7343 | * but also it is relied upon by io_ring_exit_work() | |
7344 | */ | |
0298ef96 PB |
7345 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
7346 | !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
08369246 | 7347 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 | 7348 | mutex_unlock(&ctx->uring_lock); |
6c271ce2 | 7349 | |
acfb381d PB |
7350 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
7351 | wake_up(&ctx->sqo_sq_wait); | |
948e1947 PB |
7352 | if (creds) |
7353 | revert_creds(creds); | |
acfb381d | 7354 | } |
6c271ce2 | 7355 | |
08369246 XW |
7356 | return ret; |
7357 | } | |
6c271ce2 | 7358 | |
c072481d | 7359 | static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
08369246 XW |
7360 | { |
7361 | struct io_ring_ctx *ctx; | |
7362 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 7363 | |
c9dca27d PB |
7364 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7365 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); | |
08369246 | 7366 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 7367 | } |
6c271ce2 | 7368 | |
e4b6d902 PB |
7369 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
7370 | { | |
7371 | bool did_sig = false; | |
7372 | struct ksignal ksig; | |
7373 | ||
7374 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || | |
7375 | signal_pending(current)) { | |
7376 | mutex_unlock(&sqd->lock); | |
7377 | if (signal_pending(current)) | |
7378 | did_sig = get_signal(&ksig); | |
7379 | cond_resched(); | |
7380 | mutex_lock(&sqd->lock); | |
7381 | } | |
e4b6d902 PB |
7382 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
7383 | } | |
7384 | ||
c8d1ba58 JA |
7385 | static int io_sq_thread(void *data) |
7386 | { | |
69fb2131 JA |
7387 | struct io_sq_data *sqd = data; |
7388 | struct io_ring_ctx *ctx; | |
a0d9205f | 7389 | unsigned long timeout = 0; |
37d1e2e3 | 7390 | char buf[TASK_COMM_LEN]; |
08369246 | 7391 | DEFINE_WAIT(wait); |
6c271ce2 | 7392 | |
696ee88a | 7393 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
37d1e2e3 | 7394 | set_task_comm(current, buf); |
37d1e2e3 JA |
7395 | |
7396 | if (sqd->sq_cpu != -1) | |
7397 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
7398 | else | |
7399 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
7400 | current->flags |= PF_NO_SETAFFINITY; | |
7401 | ||
09a6f4ef | 7402 | mutex_lock(&sqd->lock); |
e4b6d902 | 7403 | while (1) { |
1a924a80 | 7404 | bool cap_entries, sqt_spin = false; |
c1edbf5f | 7405 | |
e4b6d902 PB |
7406 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
7407 | if (io_sqd_handle_event(sqd)) | |
c7d95613 | 7408 | break; |
08369246 XW |
7409 | timeout = jiffies + sqd->sq_thread_idle; |
7410 | } | |
e4b6d902 | 7411 | |
e95eee2d | 7412 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 7413 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
948e1947 | 7414 | int ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a | 7415 | |
5eef4e87 | 7416 | if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list))) |
08369246 | 7417 | sqt_spin = true; |
69fb2131 | 7418 | } |
dd432ea5 PB |
7419 | if (io_run_task_work()) |
7420 | sqt_spin = true; | |
6c271ce2 | 7421 | |
08369246 | 7422 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 7423 | cond_resched(); |
08369246 XW |
7424 | if (sqt_spin) |
7425 | timeout = jiffies + sqd->sq_thread_idle; | |
7426 | continue; | |
7427 | } | |
7428 | ||
08369246 | 7429 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
dd432ea5 | 7430 | if (!io_sqd_events_pending(sqd) && !current->task_works) { |
1a924a80 PB |
7431 | bool needs_sched = true; |
7432 | ||
724cb4f9 | 7433 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
aaa9f0f4 PB |
7434 | io_ring_set_wakeup_flag(ctx); |
7435 | ||
724cb4f9 | 7436 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
5eef4e87 | 7437 | !wq_list_empty(&ctx->iopoll_list)) { |
724cb4f9 HX |
7438 | needs_sched = false; |
7439 | break; | |
7440 | } | |
7441 | if (io_sqring_entries(ctx)) { | |
7442 | needs_sched = false; | |
7443 | break; | |
7444 | } | |
7445 | } | |
7446 | ||
7447 | if (needs_sched) { | |
7448 | mutex_unlock(&sqd->lock); | |
7449 | schedule(); | |
7450 | mutex_lock(&sqd->lock); | |
7451 | } | |
69fb2131 JA |
7452 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7453 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 7454 | } |
08369246 XW |
7455 | |
7456 | finish_wait(&sqd->wait, &wait); | |
7457 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 | 7458 | } |
28cea78a | 7459 | |
78cc687b | 7460 | io_uring_cancel_generic(true, sqd); |
37d1e2e3 | 7461 | sqd->thread = NULL; |
05962f95 | 7462 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
5f3f26f9 | 7463 | io_ring_set_wakeup_flag(ctx); |
521d6a73 | 7464 | io_run_task_work(); |
734551df PB |
7465 | mutex_unlock(&sqd->lock); |
7466 | ||
37d1e2e3 JA |
7467 | complete(&sqd->exited); |
7468 | do_exit(0); | |
6c271ce2 JA |
7469 | } |
7470 | ||
bda52162 JA |
7471 | struct io_wait_queue { |
7472 | struct wait_queue_entry wq; | |
7473 | struct io_ring_ctx *ctx; | |
5fd46178 | 7474 | unsigned cq_tail; |
bda52162 JA |
7475 | unsigned nr_timeouts; |
7476 | }; | |
7477 | ||
6c503150 | 7478 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
7479 | { |
7480 | struct io_ring_ctx *ctx = iowq->ctx; | |
5fd46178 | 7481 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
bda52162 JA |
7482 | |
7483 | /* | |
d195a66e | 7484 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
7485 | * started waiting. For timeouts, we always want to return to userspace, |
7486 | * regardless of event count. | |
7487 | */ | |
5fd46178 | 7488 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
bda52162 JA |
7489 | } |
7490 | ||
7491 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
7492 | int wake_flags, void *key) | |
7493 | { | |
7494 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
7495 | wq); | |
7496 | ||
6c503150 PB |
7497 | /* |
7498 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
7499 | * the task, and the next invocation will do it. | |
7500 | */ | |
5ed7a37d | 7501 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow)) |
6c503150 PB |
7502 | return autoremove_wake_function(curr, mode, wake_flags, key); |
7503 | return -1; | |
bda52162 JA |
7504 | } |
7505 | ||
af9c1a44 JA |
7506 | static int io_run_task_work_sig(void) |
7507 | { | |
7508 | if (io_run_task_work()) | |
7509 | return 1; | |
7510 | if (!signal_pending(current)) | |
7511 | return 0; | |
0b8cfa97 | 7512 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
792ee0f6 | 7513 | return -ERESTARTSYS; |
af9c1a44 JA |
7514 | return -EINTR; |
7515 | } | |
7516 | ||
eeb60b9a PB |
7517 | /* when returns >0, the caller should retry */ |
7518 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
7519 | struct io_wait_queue *iowq, | |
7520 | signed long *timeout) | |
7521 | { | |
7522 | int ret; | |
7523 | ||
7524 | /* make sure we run task_work before checking for signals */ | |
7525 | ret = io_run_task_work_sig(); | |
7526 | if (ret || io_should_wake(iowq)) | |
7527 | return ret; | |
7528 | /* let the caller flush overflows, retry */ | |
5ed7a37d | 7529 | if (test_bit(0, &ctx->check_cq_overflow)) |
eeb60b9a PB |
7530 | return 1; |
7531 | ||
7532 | *timeout = schedule_timeout(*timeout); | |
7533 | return !*timeout ? -ETIME : 1; | |
7534 | } | |
7535 | ||
2b188cc1 JA |
7536 | /* |
7537 | * Wait until events become available, if we don't already have some. The | |
7538 | * application must reap them itself, as they reside on the shared cq ring. | |
7539 | */ | |
7540 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
7541 | const sigset_t __user *sig, size_t sigsz, |
7542 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 7543 | { |
90291099 | 7544 | struct io_wait_queue iowq; |
75b28aff | 7545 | struct io_rings *rings = ctx->rings; |
c1d5a224 PB |
7546 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
7547 | int ret; | |
2b188cc1 | 7548 | |
b41e9852 | 7549 | do { |
90f67366 | 7550 | io_cqring_overflow_flush(ctx); |
6c503150 | 7551 | if (io_cqring_events(ctx) >= min_events) |
b41e9852 | 7552 | return 0; |
4c6e277c | 7553 | if (!io_run_task_work()) |
b41e9852 | 7554 | break; |
b41e9852 | 7555 | } while (1); |
2b188cc1 | 7556 | |
44df58d4 XW |
7557 | if (uts) { |
7558 | struct timespec64 ts; | |
7559 | ||
7560 | if (get_timespec64(&ts, uts)) | |
7561 | return -EFAULT; | |
7562 | timeout = timespec64_to_jiffies(&ts); | |
7563 | } | |
7564 | ||
2b188cc1 | 7565 | if (sig) { |
9e75ad5d AB |
7566 | #ifdef CONFIG_COMPAT |
7567 | if (in_compat_syscall()) | |
7568 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 7569 | sigsz); |
9e75ad5d AB |
7570 | else |
7571 | #endif | |
b772434b | 7572 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 7573 | |
2b188cc1 JA |
7574 | if (ret) |
7575 | return ret; | |
7576 | } | |
7577 | ||
90291099 PB |
7578 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
7579 | iowq.wq.private = current; | |
7580 | INIT_LIST_HEAD(&iowq.wq.entry); | |
7581 | iowq.ctx = ctx; | |
bda52162 | 7582 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
5fd46178 | 7583 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
90291099 | 7584 | |
c826bd7a | 7585 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 7586 | do { |
ca0a2651 | 7587 | /* if we can't even flush overflow, don't wait for more */ |
90f67366 | 7588 | if (!io_cqring_overflow_flush(ctx)) { |
ca0a2651 JA |
7589 | ret = -EBUSY; |
7590 | break; | |
7591 | } | |
311997b3 | 7592 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
bda52162 | 7593 | TASK_INTERRUPTIBLE); |
eeb60b9a | 7594 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
311997b3 | 7595 | finish_wait(&ctx->cq_wait, &iowq.wq); |
ca0a2651 | 7596 | cond_resched(); |
eeb60b9a | 7597 | } while (ret > 0); |
bda52162 | 7598 | |
b7db41c9 | 7599 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7600 | |
75b28aff | 7601 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7602 | } |
7603 | ||
9123c8ff | 7604 | static void io_free_page_table(void **table, size_t size) |
05f3fb3c | 7605 | { |
9123c8ff | 7606 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
05f3fb3c | 7607 | |
846a4ef2 | 7608 | for (i = 0; i < nr_tables; i++) |
9123c8ff PB |
7609 | kfree(table[i]); |
7610 | kfree(table); | |
7611 | } | |
7612 | ||
c072481d | 7613 | static __cold void **io_alloc_page_table(size_t size) |
9123c8ff PB |
7614 | { |
7615 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); | |
7616 | size_t init_size = size; | |
7617 | void **table; | |
7618 | ||
0bea96f5 | 7619 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7620 | if (!table) |
7621 | return NULL; | |
7622 | ||
7623 | for (i = 0; i < nr_tables; i++) { | |
27f6b318 | 7624 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
9123c8ff | 7625 | |
0bea96f5 | 7626 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7627 | if (!table[i]) { |
7628 | io_free_page_table(table, init_size); | |
7629 | return NULL; | |
7630 | } | |
7631 | size -= this_size; | |
7632 | } | |
7633 | return table; | |
05f3fb3c JA |
7634 | } |
7635 | ||
28a9fe25 | 7636 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
1642b445 | 7637 | { |
28a9fe25 PB |
7638 | percpu_ref_exit(&ref_node->refs); |
7639 | kfree(ref_node); | |
1642b445 PB |
7640 | } |
7641 | ||
c072481d | 7642 | static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
b9bd2bea PB |
7643 | { |
7644 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); | |
7645 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; | |
7646 | unsigned long flags; | |
7647 | bool first_add = false; | |
7648 | ||
7649 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); | |
7650 | node->done = true; | |
7651 | ||
7652 | while (!list_empty(&ctx->rsrc_ref_list)) { | |
7653 | node = list_first_entry(&ctx->rsrc_ref_list, | |
7654 | struct io_rsrc_node, node); | |
7655 | /* recycle ref nodes in order */ | |
7656 | if (!node->done) | |
7657 | break; | |
7658 | list_del(&node->node); | |
7659 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); | |
7660 | } | |
7661 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); | |
7662 | ||
7663 | if (first_add) | |
7664 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ); | |
7665 | } | |
7666 | ||
7667 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx) | |
7668 | { | |
7669 | struct io_rsrc_node *ref_node; | |
7670 | ||
7671 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); | |
7672 | if (!ref_node) | |
7673 | return NULL; | |
7674 | ||
7675 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, | |
7676 | 0, GFP_KERNEL)) { | |
7677 | kfree(ref_node); | |
7678 | return NULL; | |
7679 | } | |
7680 | INIT_LIST_HEAD(&ref_node->node); | |
7681 | INIT_LIST_HEAD(&ref_node->rsrc_list); | |
7682 | ref_node->done = false; | |
7683 | return ref_node; | |
7684 | } | |
7685 | ||
a7f0ed5a PB |
7686 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
7687 | struct io_rsrc_data *data_to_kill) | |
ab409402 | 7688 | __must_hold(&ctx->uring_lock) |
6b06314c | 7689 | { |
a7f0ed5a PB |
7690 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
7691 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); | |
6b06314c | 7692 | |
ab409402 PB |
7693 | io_rsrc_refs_drop(ctx); |
7694 | ||
a7f0ed5a PB |
7695 | if (data_to_kill) { |
7696 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; | |
82fbcfa9 | 7697 | |
a7f0ed5a | 7698 | rsrc_node->rsrc_data = data_to_kill; |
4956b9ea | 7699 | spin_lock_irq(&ctx->rsrc_ref_lock); |
a7f0ed5a | 7700 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
4956b9ea | 7701 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
82fbcfa9 | 7702 | |
3e942498 | 7703 | atomic_inc(&data_to_kill->refs); |
a7f0ed5a PB |
7704 | percpu_ref_kill(&rsrc_node->refs); |
7705 | ctx->rsrc_node = NULL; | |
7706 | } | |
6b06314c | 7707 | |
a7f0ed5a PB |
7708 | if (!ctx->rsrc_node) { |
7709 | ctx->rsrc_node = ctx->rsrc_backup_node; | |
7710 | ctx->rsrc_backup_node = NULL; | |
7711 | } | |
8bad28d8 HX |
7712 | } |
7713 | ||
a7f0ed5a | 7714 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
8dd03afe PB |
7715 | { |
7716 | if (ctx->rsrc_backup_node) | |
7717 | return 0; | |
b895c9a6 | 7718 | ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx); |
8dd03afe | 7719 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
8bad28d8 HX |
7720 | } |
7721 | ||
c072481d PB |
7722 | static __cold int io_rsrc_ref_quiesce(struct io_rsrc_data *data, |
7723 | struct io_ring_ctx *ctx) | |
8bad28d8 HX |
7724 | { |
7725 | int ret; | |
05589553 | 7726 | |
215c3902 | 7727 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
8bad28d8 HX |
7728 | if (data->quiesce) |
7729 | return -ENXIO; | |
05589553 | 7730 | |
8bad28d8 | 7731 | data->quiesce = true; |
1ffc5422 | 7732 | do { |
a7f0ed5a | 7733 | ret = io_rsrc_node_switch_start(ctx); |
8dd03afe | 7734 | if (ret) |
f2303b1f | 7735 | break; |
a7f0ed5a | 7736 | io_rsrc_node_switch(ctx, data); |
f2303b1f | 7737 | |
3e942498 PB |
7738 | /* kill initial ref, already quiesced if zero */ |
7739 | if (atomic_dec_and_test(&data->refs)) | |
7740 | break; | |
c018db4a | 7741 | mutex_unlock(&ctx->uring_lock); |
8bad28d8 | 7742 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 | 7743 | ret = wait_for_completion_interruptible(&data->done); |
c018db4a JA |
7744 | if (!ret) { |
7745 | mutex_lock(&ctx->uring_lock); | |
1ffc5422 | 7746 | break; |
c018db4a | 7747 | } |
8bad28d8 | 7748 | |
3e942498 PB |
7749 | atomic_inc(&data->refs); |
7750 | /* wait for all works potentially completing data->done */ | |
7751 | flush_delayed_work(&ctx->rsrc_put_work); | |
cb5e1b81 | 7752 | reinit_completion(&data->done); |
8dd03afe | 7753 | |
1ffc5422 | 7754 | ret = io_run_task_work_sig(); |
8bad28d8 | 7755 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 7756 | } while (ret >= 0); |
8bad28d8 | 7757 | data->quiesce = false; |
05f3fb3c | 7758 | |
8bad28d8 | 7759 | return ret; |
d7954b2b BM |
7760 | } |
7761 | ||
2d091d62 PB |
7762 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
7763 | { | |
7764 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; | |
7765 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; | |
7766 | ||
7767 | return &data->tags[table_idx][off]; | |
7768 | } | |
7769 | ||
44b31f2f | 7770 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
1ad555c6 | 7771 | { |
2d091d62 PB |
7772 | size_t size = data->nr * sizeof(data->tags[0][0]); |
7773 | ||
7774 | if (data->tags) | |
7775 | io_free_page_table((void **)data->tags, size); | |
44b31f2f PB |
7776 | kfree(data); |
7777 | } | |
7778 | ||
c072481d PB |
7779 | static __cold int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
7780 | u64 __user *utags, unsigned nr, | |
7781 | struct io_rsrc_data **pdata) | |
1ad555c6 | 7782 | { |
b895c9a6 | 7783 | struct io_rsrc_data *data; |
2d091d62 | 7784 | int ret = -ENOMEM; |
d878c816 | 7785 | unsigned i; |
1ad555c6 BM |
7786 | |
7787 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7788 | if (!data) | |
d878c816 | 7789 | return -ENOMEM; |
2d091d62 | 7790 | data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0])); |
b60c8dce | 7791 | if (!data->tags) { |
1ad555c6 | 7792 | kfree(data); |
d878c816 PB |
7793 | return -ENOMEM; |
7794 | } | |
2d091d62 PB |
7795 | |
7796 | data->nr = nr; | |
7797 | data->ctx = ctx; | |
7798 | data->do_put = do_put; | |
d878c816 | 7799 | if (utags) { |
2d091d62 | 7800 | ret = -EFAULT; |
d878c816 | 7801 | for (i = 0; i < nr; i++) { |
fdd1dc31 CIK |
7802 | u64 *tag_slot = io_get_tag_slot(data, i); |
7803 | ||
7804 | if (copy_from_user(tag_slot, &utags[i], | |
7805 | sizeof(*tag_slot))) | |
2d091d62 | 7806 | goto fail; |
d878c816 | 7807 | } |
1ad555c6 | 7808 | } |
b60c8dce | 7809 | |
3e942498 | 7810 | atomic_set(&data->refs, 1); |
1ad555c6 | 7811 | init_completion(&data->done); |
d878c816 PB |
7812 | *pdata = data; |
7813 | return 0; | |
2d091d62 PB |
7814 | fail: |
7815 | io_rsrc_data_free(data); | |
7816 | return ret; | |
1ad555c6 BM |
7817 | } |
7818 | ||
9123c8ff PB |
7819 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
7820 | { | |
0bea96f5 PB |
7821 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
7822 | GFP_KERNEL_ACCOUNT); | |
9123c8ff PB |
7823 | return !!table->files; |
7824 | } | |
7825 | ||
042b0d85 | 7826 | static void io_free_file_tables(struct io_file_table *table) |
9123c8ff | 7827 | { |
042b0d85 | 7828 | kvfree(table->files); |
9123c8ff PB |
7829 | table->files = NULL; |
7830 | } | |
7831 | ||
fff4db76 | 7832 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
1ad555c6 | 7833 | { |
fff4db76 PB |
7834 | #if defined(CONFIG_UNIX) |
7835 | if (ctx->ring_sock) { | |
7836 | struct sock *sock = ctx->ring_sock->sk; | |
7837 | struct sk_buff *skb; | |
7838 | ||
7839 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
7840 | kfree_skb(skb); | |
7841 | } | |
7842 | #else | |
7843 | int i; | |
7844 | ||
7845 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7846 | struct file *file; | |
7847 | ||
7848 | file = io_file_from_index(ctx, i); | |
7849 | if (file) | |
7850 | fput(file); | |
7851 | } | |
7852 | #endif | |
042b0d85 | 7853 | io_free_file_tables(&ctx->file_table); |
44b31f2f | 7854 | io_rsrc_data_free(ctx->file_data); |
fff4db76 PB |
7855 | ctx->file_data = NULL; |
7856 | ctx->nr_user_files = 0; | |
1ad555c6 BM |
7857 | } |
7858 | ||
d7954b2b BM |
7859 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7860 | { | |
d7954b2b BM |
7861 | int ret; |
7862 | ||
08480400 | 7863 | if (!ctx->file_data) |
d7954b2b | 7864 | return -ENXIO; |
08480400 PB |
7865 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
7866 | if (!ret) | |
7867 | __io_sqe_files_unregister(ctx); | |
7868 | return ret; | |
6b06314c JA |
7869 | } |
7870 | ||
37d1e2e3 | 7871 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
09a6f4ef | 7872 | __releases(&sqd->lock) |
37d1e2e3 | 7873 | { |
521d6a73 PB |
7874 | WARN_ON_ONCE(sqd->thread == current); |
7875 | ||
9e138a48 PB |
7876 | /* |
7877 | * Do the dance but not conditional clear_bit() because it'd race with | |
7878 | * other threads incrementing park_pending and setting the bit. | |
7879 | */ | |
37d1e2e3 | 7880 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
9e138a48 PB |
7881 | if (atomic_dec_return(&sqd->park_pending)) |
7882 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
09a6f4ef | 7883 | mutex_unlock(&sqd->lock); |
37d1e2e3 JA |
7884 | } |
7885 | ||
86e0d676 | 7886 | static void io_sq_thread_park(struct io_sq_data *sqd) |
09a6f4ef | 7887 | __acquires(&sqd->lock) |
37d1e2e3 | 7888 | { |
521d6a73 PB |
7889 | WARN_ON_ONCE(sqd->thread == current); |
7890 | ||
9e138a48 | 7891 | atomic_inc(&sqd->park_pending); |
86e0d676 | 7892 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
09a6f4ef | 7893 | mutex_lock(&sqd->lock); |
05962f95 | 7894 | if (sqd->thread) |
86e0d676 | 7895 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
7896 | } |
7897 | ||
7898 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
7899 | { | |
521d6a73 | 7900 | WARN_ON_ONCE(sqd->thread == current); |
88885f66 | 7901 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
521d6a73 | 7902 | |
05962f95 | 7903 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
88885f66 | 7904 | mutex_lock(&sqd->lock); |
e8f98f24 JA |
7905 | if (sqd->thread) |
7906 | wake_up_process(sqd->thread); | |
09a6f4ef | 7907 | mutex_unlock(&sqd->lock); |
05962f95 | 7908 | wait_for_completion(&sqd->exited); |
37d1e2e3 JA |
7909 | } |
7910 | ||
534ca6d6 | 7911 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7912 | { |
534ca6d6 | 7913 | if (refcount_dec_and_test(&sqd->refs)) { |
9e138a48 PB |
7914 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
7915 | ||
37d1e2e3 JA |
7916 | io_sq_thread_stop(sqd); |
7917 | kfree(sqd); | |
7918 | } | |
7919 | } | |
7920 | ||
7921 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
7922 | { | |
7923 | struct io_sq_data *sqd = ctx->sq_data; | |
7924 | ||
7925 | if (sqd) { | |
05962f95 | 7926 | io_sq_thread_park(sqd); |
521d6a73 | 7927 | list_del_init(&ctx->sqd_list); |
37d1e2e3 | 7928 | io_sqd_update_thread_idle(sqd); |
05962f95 | 7929 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
7930 | |
7931 | io_put_sq_data(sqd); | |
7932 | ctx->sq_data = NULL; | |
534ca6d6 JA |
7933 | } |
7934 | } | |
7935 | ||
aa06165d JA |
7936 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7937 | { | |
7938 | struct io_ring_ctx *ctx_attach; | |
7939 | struct io_sq_data *sqd; | |
7940 | struct fd f; | |
7941 | ||
7942 | f = fdget(p->wq_fd); | |
7943 | if (!f.file) | |
7944 | return ERR_PTR(-ENXIO); | |
7945 | if (f.file->f_op != &io_uring_fops) { | |
7946 | fdput(f); | |
7947 | return ERR_PTR(-EINVAL); | |
7948 | } | |
7949 | ||
7950 | ctx_attach = f.file->private_data; | |
7951 | sqd = ctx_attach->sq_data; | |
7952 | if (!sqd) { | |
7953 | fdput(f); | |
7954 | return ERR_PTR(-EINVAL); | |
7955 | } | |
5c2469e0 JA |
7956 | if (sqd->task_tgid != current->tgid) { |
7957 | fdput(f); | |
7958 | return ERR_PTR(-EPERM); | |
7959 | } | |
aa06165d JA |
7960 | |
7961 | refcount_inc(&sqd->refs); | |
7962 | fdput(f); | |
7963 | return sqd; | |
7964 | } | |
7965 | ||
26984fbf PB |
7966 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
7967 | bool *attached) | |
534ca6d6 JA |
7968 | { |
7969 | struct io_sq_data *sqd; | |
7970 | ||
26984fbf | 7971 | *attached = false; |
5c2469e0 JA |
7972 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
7973 | sqd = io_attach_sq_data(p); | |
26984fbf PB |
7974 | if (!IS_ERR(sqd)) { |
7975 | *attached = true; | |
5c2469e0 | 7976 | return sqd; |
26984fbf | 7977 | } |
5c2469e0 JA |
7978 | /* fall through for EPERM case, setup new sqd/task */ |
7979 | if (PTR_ERR(sqd) != -EPERM) | |
7980 | return sqd; | |
7981 | } | |
aa06165d | 7982 | |
534ca6d6 JA |
7983 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7984 | if (!sqd) | |
7985 | return ERR_PTR(-ENOMEM); | |
7986 | ||
9e138a48 | 7987 | atomic_set(&sqd->park_pending, 0); |
534ca6d6 | 7988 | refcount_set(&sqd->refs, 1); |
69fb2131 | 7989 | INIT_LIST_HEAD(&sqd->ctx_list); |
09a6f4ef | 7990 | mutex_init(&sqd->lock); |
534ca6d6 | 7991 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 7992 | init_completion(&sqd->exited); |
534ca6d6 JA |
7993 | return sqd; |
7994 | } | |
7995 | ||
6b06314c | 7996 | #if defined(CONFIG_UNIX) |
6b06314c JA |
7997 | /* |
7998 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
7999 | * the io_uring can be safely unregistered on process exit, even if we have | |
8000 | * loops in the file referencing. | |
8001 | */ | |
8002 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
8003 | { | |
8004 | struct sock *sk = ctx->ring_sock->sk; | |
8005 | struct scm_fp_list *fpl; | |
8006 | struct sk_buff *skb; | |
08a45173 | 8007 | int i, nr_files; |
6b06314c | 8008 | |
6b06314c JA |
8009 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
8010 | if (!fpl) | |
8011 | return -ENOMEM; | |
8012 | ||
8013 | skb = alloc_skb(0, GFP_KERNEL); | |
8014 | if (!skb) { | |
8015 | kfree(fpl); | |
8016 | return -ENOMEM; | |
8017 | } | |
8018 | ||
8019 | skb->sk = sk; | |
6b06314c | 8020 | |
08a45173 | 8021 | nr_files = 0; |
62e398be | 8022 | fpl->user = get_uid(current_user()); |
6b06314c | 8023 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
8024 | struct file *file = io_file_from_index(ctx, i + offset); |
8025 | ||
8026 | if (!file) | |
08a45173 | 8027 | continue; |
65e19f54 | 8028 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
8029 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
8030 | nr_files++; | |
6b06314c JA |
8031 | } |
8032 | ||
08a45173 JA |
8033 | if (nr_files) { |
8034 | fpl->max = SCM_MAX_FD; | |
8035 | fpl->count = nr_files; | |
8036 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 8037 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
8038 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
8039 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 8040 | |
08a45173 JA |
8041 | for (i = 0; i < nr_files; i++) |
8042 | fput(fpl->fp[i]); | |
8043 | } else { | |
8044 | kfree_skb(skb); | |
8045 | kfree(fpl); | |
8046 | } | |
6b06314c JA |
8047 | |
8048 | return 0; | |
8049 | } | |
8050 | ||
8051 | /* | |
8052 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
8053 | * causes regular reference counting to break down. We rely on the UNIX | |
8054 | * garbage collection to take care of this problem for us. | |
8055 | */ | |
8056 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8057 | { | |
8058 | unsigned left, total; | |
8059 | int ret = 0; | |
8060 | ||
8061 | total = 0; | |
8062 | left = ctx->nr_user_files; | |
8063 | while (left) { | |
8064 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
8065 | |
8066 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
8067 | if (ret) | |
8068 | break; | |
8069 | left -= this_files; | |
8070 | total += this_files; | |
8071 | } | |
8072 | ||
8073 | if (!ret) | |
8074 | return 0; | |
8075 | ||
8076 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
8077 | struct file *file = io_file_from_index(ctx, total); |
8078 | ||
8079 | if (file) | |
8080 | fput(file); | |
6b06314c JA |
8081 | total++; |
8082 | } | |
8083 | ||
8084 | return ret; | |
8085 | } | |
8086 | #else | |
8087 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8088 | { | |
8089 | return 0; | |
8090 | } | |
8091 | #endif | |
8092 | ||
47e90392 | 8093 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 8094 | { |
50238531 | 8095 | struct file *file = prsrc->file; |
05f3fb3c JA |
8096 | #if defined(CONFIG_UNIX) |
8097 | struct sock *sock = ctx->ring_sock->sk; | |
8098 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
8099 | struct sk_buff *skb; | |
8100 | int i; | |
8101 | ||
8102 | __skb_queue_head_init(&list); | |
8103 | ||
8104 | /* | |
8105 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
8106 | * remove this entry and rearrange the file array. | |
8107 | */ | |
8108 | skb = skb_dequeue(head); | |
8109 | while (skb) { | |
8110 | struct scm_fp_list *fp; | |
8111 | ||
8112 | fp = UNIXCB(skb).fp; | |
8113 | for (i = 0; i < fp->count; i++) { | |
8114 | int left; | |
8115 | ||
8116 | if (fp->fp[i] != file) | |
8117 | continue; | |
8118 | ||
8119 | unix_notinflight(fp->user, fp->fp[i]); | |
8120 | left = fp->count - 1 - i; | |
8121 | if (left) { | |
8122 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
8123 | left * sizeof(struct file *)); | |
8124 | } | |
8125 | fp->count--; | |
8126 | if (!fp->count) { | |
8127 | kfree_skb(skb); | |
8128 | skb = NULL; | |
8129 | } else { | |
8130 | __skb_queue_tail(&list, skb); | |
8131 | } | |
8132 | fput(file); | |
8133 | file = NULL; | |
8134 | break; | |
8135 | } | |
8136 | ||
8137 | if (!file) | |
8138 | break; | |
8139 | ||
8140 | __skb_queue_tail(&list, skb); | |
8141 | ||
8142 | skb = skb_dequeue(head); | |
8143 | } | |
8144 | ||
8145 | if (skb_peek(&list)) { | |
8146 | spin_lock_irq(&head->lock); | |
8147 | while ((skb = __skb_dequeue(&list)) != NULL) | |
8148 | __skb_queue_tail(head, skb); | |
8149 | spin_unlock_irq(&head->lock); | |
8150 | } | |
8151 | #else | |
8152 | fput(file); | |
8153 | #endif | |
8154 | } | |
8155 | ||
b895c9a6 | 8156 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
65e19f54 | 8157 | { |
b895c9a6 | 8158 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
269bbe5f BM |
8159 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
8160 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 8161 | |
269bbe5f BM |
8162 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
8163 | list_del(&prsrc->list); | |
b60c8dce PB |
8164 | |
8165 | if (prsrc->tag) { | |
8166 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; | |
b60c8dce PB |
8167 | |
8168 | io_ring_submit_lock(ctx, lock_ring); | |
79ebeaee | 8169 | spin_lock(&ctx->completion_lock); |
b60c8dce | 8170 | io_cqring_fill_event(ctx, prsrc->tag, 0, 0); |
2840f710 | 8171 | ctx->cq_extra++; |
b60c8dce | 8172 | io_commit_cqring(ctx); |
79ebeaee | 8173 | spin_unlock(&ctx->completion_lock); |
b60c8dce PB |
8174 | io_cqring_ev_posted(ctx); |
8175 | io_ring_submit_unlock(ctx, lock_ring); | |
8176 | } | |
8177 | ||
40ae0ff7 | 8178 | rsrc_data->do_put(ctx, prsrc); |
269bbe5f | 8179 | kfree(prsrc); |
65e19f54 | 8180 | } |
05589553 | 8181 | |
28a9fe25 | 8182 | io_rsrc_node_destroy(ref_node); |
3e942498 PB |
8183 | if (atomic_dec_and_test(&rsrc_data->refs)) |
8184 | complete(&rsrc_data->done); | |
2faf852d | 8185 | } |
65e19f54 | 8186 | |
269bbe5f | 8187 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
8188 | { |
8189 | struct io_ring_ctx *ctx; | |
8190 | struct llist_node *node; | |
8191 | ||
269bbe5f BM |
8192 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
8193 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
8194 | |
8195 | while (node) { | |
b895c9a6 | 8196 | struct io_rsrc_node *ref_node; |
4a38aed2 JA |
8197 | struct llist_node *next = node->next; |
8198 | ||
b895c9a6 | 8199 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
269bbe5f | 8200 | __io_rsrc_put_work(ref_node); |
4a38aed2 JA |
8201 | node = next; |
8202 | } | |
8203 | } | |
8204 | ||
6b06314c | 8205 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
792e3582 | 8206 | unsigned nr_args, u64 __user *tags) |
6b06314c JA |
8207 | { |
8208 | __s32 __user *fds = (__s32 __user *) arg; | |
05f3fb3c | 8209 | struct file *file; |
f3baed39 | 8210 | int fd, ret; |
846a4ef2 | 8211 | unsigned i; |
6b06314c | 8212 | |
05f3fb3c | 8213 | if (ctx->file_data) |
6b06314c JA |
8214 | return -EBUSY; |
8215 | if (!nr_args) | |
8216 | return -EINVAL; | |
8217 | if (nr_args > IORING_MAX_FIXED_FILES) | |
8218 | return -EMFILE; | |
3a1b8a4e PB |
8219 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
8220 | return -EMFILE; | |
a7f0ed5a | 8221 | ret = io_rsrc_node_switch_start(ctx); |
f3baed39 PB |
8222 | if (ret) |
8223 | return ret; | |
d878c816 PB |
8224 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
8225 | &ctx->file_data); | |
8226 | if (ret) | |
8227 | return ret; | |
6b06314c | 8228 | |
f3baed39 | 8229 | ret = -ENOMEM; |
aeca241b | 8230 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
1ad555c6 | 8231 | goto out_free; |
65e19f54 | 8232 | |
08a45173 | 8233 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
d878c816 | 8234 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
600cf3f8 PB |
8235 | ret = -EFAULT; |
8236 | goto out_fput; | |
8237 | } | |
08a45173 | 8238 | /* allow sparse sets */ |
792e3582 PB |
8239 | if (fd == -1) { |
8240 | ret = -EINVAL; | |
2d091d62 | 8241 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
792e3582 | 8242 | goto out_fput; |
08a45173 | 8243 | continue; |
792e3582 | 8244 | } |
6b06314c | 8245 | |
05f3fb3c | 8246 | file = fget(fd); |
6b06314c | 8247 | ret = -EBADF; |
792e3582 | 8248 | if (unlikely(!file)) |
600cf3f8 | 8249 | goto out_fput; |
05f3fb3c | 8250 | |
6b06314c JA |
8251 | /* |
8252 | * Don't allow io_uring instances to be registered. If UNIX | |
8253 | * isn't enabled, then this causes a reference cycle and this | |
8254 | * instance can never get freed. If UNIX is enabled we'll | |
8255 | * handle it just fine, but there's still no point in allowing | |
8256 | * a ring fd as it doesn't support regular read/write anyway. | |
8257 | */ | |
05f3fb3c JA |
8258 | if (file->f_op == &io_uring_fops) { |
8259 | fput(file); | |
600cf3f8 | 8260 | goto out_fput; |
6b06314c | 8261 | } |
aeca241b | 8262 | io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file); |
6b06314c JA |
8263 | } |
8264 | ||
6b06314c | 8265 | ret = io_sqe_files_scm(ctx); |
05589553 | 8266 | if (ret) { |
08480400 | 8267 | __io_sqe_files_unregister(ctx); |
05589553 XW |
8268 | return ret; |
8269 | } | |
6b06314c | 8270 | |
a7f0ed5a | 8271 | io_rsrc_node_switch(ctx, NULL); |
6b06314c | 8272 | return ret; |
600cf3f8 PB |
8273 | out_fput: |
8274 | for (i = 0; i < ctx->nr_user_files; i++) { | |
8275 | file = io_file_from_index(ctx, i); | |
8276 | if (file) | |
8277 | fput(file); | |
8278 | } | |
042b0d85 | 8279 | io_free_file_tables(&ctx->file_table); |
600cf3f8 | 8280 | ctx->nr_user_files = 0; |
600cf3f8 | 8281 | out_free: |
44b31f2f | 8282 | io_rsrc_data_free(ctx->file_data); |
55cbc256 | 8283 | ctx->file_data = NULL; |
6b06314c JA |
8284 | return ret; |
8285 | } | |
8286 | ||
c3a31e60 JA |
8287 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
8288 | int index) | |
8289 | { | |
8290 | #if defined(CONFIG_UNIX) | |
8291 | struct sock *sock = ctx->ring_sock->sk; | |
8292 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
8293 | struct sk_buff *skb; | |
8294 | ||
8295 | /* | |
8296 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
8297 | * file set. If there's no room, fall back to allocating a new skb | |
8298 | * and filling it in. | |
8299 | */ | |
8300 | spin_lock_irq(&head->lock); | |
8301 | skb = skb_peek(head); | |
8302 | if (skb) { | |
8303 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
8304 | ||
8305 | if (fpl->count < SCM_MAX_FD) { | |
8306 | __skb_unlink(skb, head); | |
8307 | spin_unlock_irq(&head->lock); | |
8308 | fpl->fp[fpl->count] = get_file(file); | |
8309 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
8310 | fpl->count++; | |
8311 | spin_lock_irq(&head->lock); | |
8312 | __skb_queue_head(head, skb); | |
8313 | } else { | |
8314 | skb = NULL; | |
8315 | } | |
8316 | } | |
8317 | spin_unlock_irq(&head->lock); | |
8318 | ||
8319 | if (skb) { | |
8320 | fput(file); | |
8321 | return 0; | |
8322 | } | |
8323 | ||
8324 | return __io_sqe_files_scm(ctx, 1, index); | |
8325 | #else | |
8326 | return 0; | |
8327 | #endif | |
8328 | } | |
8329 | ||
9c7b0ba8 PB |
8330 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
8331 | struct io_rsrc_node *node, void *rsrc) | |
8332 | { | |
8333 | struct io_rsrc_put *prsrc; | |
8334 | ||
8335 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); | |
8336 | if (!prsrc) | |
8337 | return -ENOMEM; | |
8338 | ||
8339 | prsrc->tag = *io_get_tag_slot(data, idx); | |
8340 | prsrc->rsrc = rsrc; | |
8341 | list_add(&prsrc->list, &node->rsrc_list); | |
8342 | return 0; | |
8343 | } | |
8344 | ||
b9445598 PB |
8345 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
8346 | unsigned int issue_flags, u32 slot_index) | |
8347 | { | |
8348 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 8349 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
9c7b0ba8 | 8350 | bool needs_switch = false; |
b9445598 PB |
8351 | struct io_fixed_file *file_slot; |
8352 | int ret = -EBADF; | |
8353 | ||
3b44b371 | 8354 | io_ring_submit_lock(ctx, needs_lock); |
b9445598 PB |
8355 | if (file->f_op == &io_uring_fops) |
8356 | goto err; | |
8357 | ret = -ENXIO; | |
8358 | if (!ctx->file_data) | |
8359 | goto err; | |
8360 | ret = -EINVAL; | |
8361 | if (slot_index >= ctx->nr_user_files) | |
8362 | goto err; | |
8363 | ||
8364 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); | |
8365 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); | |
9c7b0ba8 PB |
8366 | |
8367 | if (file_slot->file_ptr) { | |
8368 | struct file *old_file; | |
8369 | ||
8370 | ret = io_rsrc_node_switch_start(ctx); | |
8371 | if (ret) | |
8372 | goto err; | |
8373 | ||
8374 | old_file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8375 | ret = io_queue_rsrc_removal(ctx->file_data, slot_index, | |
8376 | ctx->rsrc_node, old_file); | |
8377 | if (ret) | |
8378 | goto err; | |
8379 | file_slot->file_ptr = 0; | |
8380 | needs_switch = true; | |
8381 | } | |
b9445598 PB |
8382 | |
8383 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; | |
8384 | io_fixed_file_set(file_slot, file); | |
8385 | ret = io_sqe_file_register(ctx, file, slot_index); | |
8386 | if (ret) { | |
8387 | file_slot->file_ptr = 0; | |
8388 | goto err; | |
8389 | } | |
8390 | ||
8391 | ret = 0; | |
8392 | err: | |
9c7b0ba8 PB |
8393 | if (needs_switch) |
8394 | io_rsrc_node_switch(ctx, ctx->file_data); | |
3b44b371 | 8395 | io_ring_submit_unlock(ctx, needs_lock); |
b9445598 PB |
8396 | if (ret) |
8397 | fput(file); | |
8398 | return ret; | |
8399 | } | |
8400 | ||
7df778be PB |
8401 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) |
8402 | { | |
8403 | unsigned int offset = req->close.file_slot - 1; | |
8404 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 8405 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
7df778be PB |
8406 | struct io_fixed_file *file_slot; |
8407 | struct file *file; | |
8408 | int ret, i; | |
8409 | ||
3b44b371 | 8410 | io_ring_submit_lock(ctx, needs_lock); |
7df778be PB |
8411 | ret = -ENXIO; |
8412 | if (unlikely(!ctx->file_data)) | |
8413 | goto out; | |
8414 | ret = -EINVAL; | |
8415 | if (offset >= ctx->nr_user_files) | |
8416 | goto out; | |
8417 | ret = io_rsrc_node_switch_start(ctx); | |
8418 | if (ret) | |
8419 | goto out; | |
8420 | ||
8421 | i = array_index_nospec(offset, ctx->nr_user_files); | |
8422 | file_slot = io_fixed_file_slot(&ctx->file_table, i); | |
8423 | ret = -EBADF; | |
8424 | if (!file_slot->file_ptr) | |
8425 | goto out; | |
8426 | ||
8427 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8428 | ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file); | |
8429 | if (ret) | |
8430 | goto out; | |
8431 | ||
8432 | file_slot->file_ptr = 0; | |
8433 | io_rsrc_node_switch(ctx, ctx->file_data); | |
8434 | ret = 0; | |
8435 | out: | |
3b44b371 | 8436 | io_ring_submit_unlock(ctx, needs_lock); |
7df778be PB |
8437 | return ret; |
8438 | } | |
8439 | ||
05f3fb3c | 8440 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
c3bdad02 | 8441 | struct io_uring_rsrc_update2 *up, |
05f3fb3c JA |
8442 | unsigned nr_args) |
8443 | { | |
c3bdad02 | 8444 | u64 __user *tags = u64_to_user_ptr(up->tags); |
98f0b3b4 | 8445 | __s32 __user *fds = u64_to_user_ptr(up->data); |
b895c9a6 | 8446 | struct io_rsrc_data *data = ctx->file_data; |
a04b0ac0 PB |
8447 | struct io_fixed_file *file_slot; |
8448 | struct file *file; | |
98f0b3b4 PB |
8449 | int fd, i, err = 0; |
8450 | unsigned int done; | |
05589553 | 8451 | bool needs_switch = false; |
c3a31e60 | 8452 | |
98f0b3b4 PB |
8453 | if (!ctx->file_data) |
8454 | return -ENXIO; | |
8455 | if (up->offset + nr_args > ctx->nr_user_files) | |
c3a31e60 JA |
8456 | return -EINVAL; |
8457 | ||
67973b93 | 8458 | for (done = 0; done < nr_args; done++) { |
c3bdad02 PB |
8459 | u64 tag = 0; |
8460 | ||
8461 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || | |
8462 | copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
c3a31e60 JA |
8463 | err = -EFAULT; |
8464 | break; | |
8465 | } | |
c3bdad02 PB |
8466 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
8467 | err = -EINVAL; | |
8468 | break; | |
8469 | } | |
4e0377a1 | 8470 | if (fd == IORING_REGISTER_FILES_SKIP) |
8471 | continue; | |
8472 | ||
67973b93 | 8473 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
aeca241b | 8474 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
ea64ec02 | 8475 | |
a04b0ac0 PB |
8476 | if (file_slot->file_ptr) { |
8477 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
b60c8dce PB |
8478 | err = io_queue_rsrc_removal(data, up->offset + done, |
8479 | ctx->rsrc_node, file); | |
a5318d3c HD |
8480 | if (err) |
8481 | break; | |
a04b0ac0 | 8482 | file_slot->file_ptr = 0; |
05589553 | 8483 | needs_switch = true; |
c3a31e60 JA |
8484 | } |
8485 | if (fd != -1) { | |
c3a31e60 JA |
8486 | file = fget(fd); |
8487 | if (!file) { | |
8488 | err = -EBADF; | |
8489 | break; | |
8490 | } | |
8491 | /* | |
8492 | * Don't allow io_uring instances to be registered. If | |
8493 | * UNIX isn't enabled, then this causes a reference | |
8494 | * cycle and this instance can never get freed. If UNIX | |
8495 | * is enabled we'll handle it just fine, but there's | |
8496 | * still no point in allowing a ring fd as it doesn't | |
8497 | * support regular read/write anyway. | |
8498 | */ | |
8499 | if (file->f_op == &io_uring_fops) { | |
8500 | fput(file); | |
8501 | err = -EBADF; | |
8502 | break; | |
8503 | } | |
2d091d62 | 8504 | *io_get_tag_slot(data, up->offset + done) = tag; |
9a321c98 | 8505 | io_fixed_file_set(file_slot, file); |
c3a31e60 | 8506 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 8507 | if (err) { |
a04b0ac0 | 8508 | file_slot->file_ptr = 0; |
f3bd9dae | 8509 | fput(file); |
c3a31e60 | 8510 | break; |
f3bd9dae | 8511 | } |
c3a31e60 | 8512 | } |
05f3fb3c JA |
8513 | } |
8514 | ||
a7f0ed5a PB |
8515 | if (needs_switch) |
8516 | io_rsrc_node_switch(ctx, data); | |
c3a31e60 JA |
8517 | return done ? done : err; |
8518 | } | |
05589553 | 8519 | |
685fe7fe JA |
8520 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
8521 | struct task_struct *task) | |
24369c2e | 8522 | { |
e941894e | 8523 | struct io_wq_hash *hash; |
24369c2e | 8524 | struct io_wq_data data; |
24369c2e | 8525 | unsigned int concurrency; |
24369c2e | 8526 | |
362a9e65 | 8527 | mutex_lock(&ctx->uring_lock); |
e941894e JA |
8528 | hash = ctx->hash_map; |
8529 | if (!hash) { | |
8530 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
362a9e65 YY |
8531 | if (!hash) { |
8532 | mutex_unlock(&ctx->uring_lock); | |
e941894e | 8533 | return ERR_PTR(-ENOMEM); |
362a9e65 | 8534 | } |
e941894e JA |
8535 | refcount_set(&hash->refs, 1); |
8536 | init_waitqueue_head(&hash->wait); | |
8537 | ctx->hash_map = hash; | |
24369c2e | 8538 | } |
362a9e65 | 8539 | mutex_unlock(&ctx->uring_lock); |
24369c2e | 8540 | |
e941894e | 8541 | data.hash = hash; |
685fe7fe | 8542 | data.task = task; |
ebc11b6c | 8543 | data.free_work = io_wq_free_work; |
f5fa38c5 | 8544 | data.do_work = io_wq_submit_work; |
24369c2e | 8545 | |
d25e3a3d JA |
8546 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
8547 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 8548 | |
5aa75ed5 | 8549 | return io_wq_create(concurrency, &data); |
24369c2e PB |
8550 | } |
8551 | ||
c072481d PB |
8552 | static __cold int io_uring_alloc_task_context(struct task_struct *task, |
8553 | struct io_ring_ctx *ctx) | |
0f212204 JA |
8554 | { |
8555 | struct io_uring_task *tctx; | |
d8a6df10 | 8556 | int ret; |
0f212204 | 8557 | |
09899b19 | 8558 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
0f212204 JA |
8559 | if (unlikely(!tctx)) |
8560 | return -ENOMEM; | |
8561 | ||
d8a6df10 JA |
8562 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
8563 | if (unlikely(ret)) { | |
8564 | kfree(tctx); | |
8565 | return ret; | |
8566 | } | |
8567 | ||
685fe7fe | 8568 | tctx->io_wq = io_init_wq_offload(ctx, task); |
5aa75ed5 JA |
8569 | if (IS_ERR(tctx->io_wq)) { |
8570 | ret = PTR_ERR(tctx->io_wq); | |
8571 | percpu_counter_destroy(&tctx->inflight); | |
8572 | kfree(tctx); | |
8573 | return ret; | |
8574 | } | |
8575 | ||
0f212204 JA |
8576 | xa_init(&tctx->xa); |
8577 | init_waitqueue_head(&tctx->wait); | |
fdaf083c | 8578 | atomic_set(&tctx->in_idle, 0); |
b303fe2e | 8579 | atomic_set(&tctx->inflight_tracked, 0); |
0f212204 | 8580 | task->io_uring = tctx; |
7cbf1722 JA |
8581 | spin_lock_init(&tctx->task_lock); |
8582 | INIT_WQ_LIST(&tctx->task_list); | |
7cbf1722 | 8583 | init_task_work(&tctx->task_work, tctx_task_work); |
0f212204 JA |
8584 | return 0; |
8585 | } | |
8586 | ||
8587 | void __io_uring_free(struct task_struct *tsk) | |
8588 | { | |
8589 | struct io_uring_task *tctx = tsk->io_uring; | |
8590 | ||
8591 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e | 8592 | WARN_ON_ONCE(tctx->io_wq); |
09899b19 | 8593 | WARN_ON_ONCE(tctx->cached_refs); |
ef8eaa4e | 8594 | |
d8a6df10 | 8595 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
8596 | kfree(tctx); |
8597 | tsk->io_uring = NULL; | |
8598 | } | |
8599 | ||
c072481d PB |
8600 | static __cold int io_sq_offload_create(struct io_ring_ctx *ctx, |
8601 | struct io_uring_params *p) | |
2b188cc1 JA |
8602 | { |
8603 | int ret; | |
8604 | ||
d25e3a3d JA |
8605 | /* Retain compatibility with failing for an invalid attach attempt */ |
8606 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
8607 | IORING_SETUP_ATTACH_WQ) { | |
8608 | struct fd f; | |
8609 | ||
8610 | f = fdget(p->wq_fd); | |
8611 | if (!f.file) | |
8612 | return -ENXIO; | |
0cc936f7 JA |
8613 | if (f.file->f_op != &io_uring_fops) { |
8614 | fdput(f); | |
f2a48dd0 | 8615 | return -EINVAL; |
0cc936f7 JA |
8616 | } |
8617 | fdput(f); | |
d25e3a3d | 8618 | } |
6c271ce2 | 8619 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 8620 | struct task_struct *tsk; |
534ca6d6 | 8621 | struct io_sq_data *sqd; |
26984fbf | 8622 | bool attached; |
534ca6d6 | 8623 | |
26984fbf | 8624 | sqd = io_get_sq_data(p, &attached); |
534ca6d6 JA |
8625 | if (IS_ERR(sqd)) { |
8626 | ret = PTR_ERR(sqd); | |
8627 | goto err; | |
8628 | } | |
69fb2131 | 8629 | |
7c30f36a | 8630 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 8631 | ctx->sq_data = sqd; |
917257da JA |
8632 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
8633 | if (!ctx->sq_thread_idle) | |
8634 | ctx->sq_thread_idle = HZ; | |
8635 | ||
78d7f6ba | 8636 | io_sq_thread_park(sqd); |
de75a3d3 PB |
8637 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
8638 | io_sqd_update_thread_idle(sqd); | |
26984fbf | 8639 | /* don't attach to a dying SQPOLL thread, would be racy */ |
f2a48dd0 | 8640 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
78d7f6ba PB |
8641 | io_sq_thread_unpark(sqd); |
8642 | ||
de75a3d3 PB |
8643 | if (ret < 0) |
8644 | goto err; | |
8645 | if (attached) | |
5aa75ed5 | 8646 | return 0; |
aa06165d | 8647 | |
6c271ce2 | 8648 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 8649 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 8650 | |
917257da | 8651 | ret = -EINVAL; |
f2a48dd0 | 8652 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
e8f98f24 | 8653 | goto err_sqpoll; |
37d1e2e3 | 8654 | sqd->sq_cpu = cpu; |
6c271ce2 | 8655 | } else { |
37d1e2e3 | 8656 | sqd->sq_cpu = -1; |
6c271ce2 | 8657 | } |
37d1e2e3 JA |
8658 | |
8659 | sqd->task_pid = current->pid; | |
5c2469e0 | 8660 | sqd->task_tgid = current->tgid; |
46fe18b1 JA |
8661 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
8662 | if (IS_ERR(tsk)) { | |
8663 | ret = PTR_ERR(tsk); | |
e8f98f24 | 8664 | goto err_sqpoll; |
6c271ce2 | 8665 | } |
97a73a0f | 8666 | |
46fe18b1 | 8667 | sqd->thread = tsk; |
97a73a0f | 8668 | ret = io_uring_alloc_task_context(tsk, ctx); |
46fe18b1 | 8669 | wake_up_new_task(tsk); |
0f212204 JA |
8670 | if (ret) |
8671 | goto err; | |
6c271ce2 JA |
8672 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
8673 | /* Can't have SQ_AFF without SQPOLL */ | |
8674 | ret = -EINVAL; | |
8675 | goto err; | |
8676 | } | |
8677 | ||
2b188cc1 | 8678 | return 0; |
f2a48dd0 PB |
8679 | err_sqpoll: |
8680 | complete(&ctx->sq_data->exited); | |
2b188cc1 | 8681 | err: |
37d1e2e3 | 8682 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
8683 | return ret; |
8684 | } | |
8685 | ||
a087e2b5 BM |
8686 | static inline void __io_unaccount_mem(struct user_struct *user, |
8687 | unsigned long nr_pages) | |
2b188cc1 JA |
8688 | { |
8689 | atomic_long_sub(nr_pages, &user->locked_vm); | |
8690 | } | |
8691 | ||
a087e2b5 BM |
8692 | static inline int __io_account_mem(struct user_struct *user, |
8693 | unsigned long nr_pages) | |
2b188cc1 JA |
8694 | { |
8695 | unsigned long page_limit, cur_pages, new_pages; | |
8696 | ||
8697 | /* Don't allow more pages than we can safely lock */ | |
8698 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
8699 | ||
8700 | do { | |
8701 | cur_pages = atomic_long_read(&user->locked_vm); | |
8702 | new_pages = cur_pages + nr_pages; | |
8703 | if (new_pages > page_limit) | |
8704 | return -ENOMEM; | |
8705 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
8706 | new_pages) != cur_pages); | |
8707 | ||
8708 | return 0; | |
8709 | } | |
8710 | ||
26bfa89e | 8711 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8712 | { |
62e398be | 8713 | if (ctx->user) |
a087e2b5 | 8714 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 8715 | |
26bfa89e JA |
8716 | if (ctx->mm_account) |
8717 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8718 | } |
8719 | ||
26bfa89e | 8720 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8721 | { |
30975825 BM |
8722 | int ret; |
8723 | ||
62e398be | 8724 | if (ctx->user) { |
30975825 BM |
8725 | ret = __io_account_mem(ctx->user, nr_pages); |
8726 | if (ret) | |
8727 | return ret; | |
8728 | } | |
8729 | ||
26bfa89e JA |
8730 | if (ctx->mm_account) |
8731 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8732 | |
8733 | return 0; | |
8734 | } | |
8735 | ||
2b188cc1 JA |
8736 | static void io_mem_free(void *ptr) |
8737 | { | |
52e04ef4 MR |
8738 | struct page *page; |
8739 | ||
8740 | if (!ptr) | |
8741 | return; | |
2b188cc1 | 8742 | |
52e04ef4 | 8743 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8744 | if (put_page_testzero(page)) |
8745 | free_compound_page(page); | |
8746 | } | |
8747 | ||
8748 | static void *io_mem_alloc(size_t size) | |
8749 | { | |
8750 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
26bfa89e | 8751 | __GFP_NORETRY | __GFP_ACCOUNT; |
2b188cc1 JA |
8752 | |
8753 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8754 | } | |
8755 | ||
75b28aff HV |
8756 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8757 | size_t *sq_offset) | |
8758 | { | |
8759 | struct io_rings *rings; | |
8760 | size_t off, sq_array_size; | |
8761 | ||
8762 | off = struct_size(rings, cqes, cq_entries); | |
8763 | if (off == SIZE_MAX) | |
8764 | return SIZE_MAX; | |
8765 | ||
8766 | #ifdef CONFIG_SMP | |
8767 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8768 | if (off == 0) | |
8769 | return SIZE_MAX; | |
8770 | #endif | |
8771 | ||
b36200f5 DV |
8772 | if (sq_offset) |
8773 | *sq_offset = off; | |
8774 | ||
75b28aff HV |
8775 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8776 | if (sq_array_size == SIZE_MAX) | |
8777 | return SIZE_MAX; | |
8778 | ||
8779 | if (check_add_overflow(off, sq_array_size, &off)) | |
8780 | return SIZE_MAX; | |
8781 | ||
75b28aff HV |
8782 | return off; |
8783 | } | |
8784 | ||
41edf1a5 | 8785 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot) |
7f61a1e9 | 8786 | { |
41edf1a5 | 8787 | struct io_mapped_ubuf *imu = *slot; |
7f61a1e9 PB |
8788 | unsigned int i; |
8789 | ||
6224843d PB |
8790 | if (imu != ctx->dummy_ubuf) { |
8791 | for (i = 0; i < imu->nr_bvecs; i++) | |
8792 | unpin_user_page(imu->bvec[i].bv_page); | |
8793 | if (imu->acct_pages) | |
8794 | io_unaccount_mem(ctx, imu->acct_pages); | |
8795 | kvfree(imu); | |
8796 | } | |
41edf1a5 | 8797 | *slot = NULL; |
7f61a1e9 PB |
8798 | } |
8799 | ||
bd54b6fe | 8800 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
edafccee | 8801 | { |
634d00df PB |
8802 | io_buffer_unmap(ctx, &prsrc->buf); |
8803 | prsrc->buf = NULL; | |
bd54b6fe | 8804 | } |
edafccee | 8805 | |
bd54b6fe BM |
8806 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
8807 | { | |
8808 | unsigned int i; | |
edafccee | 8809 | |
7f61a1e9 PB |
8810 | for (i = 0; i < ctx->nr_user_bufs; i++) |
8811 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); | |
edafccee | 8812 | kfree(ctx->user_bufs); |
bb6659cc | 8813 | io_rsrc_data_free(ctx->buf_data); |
edafccee | 8814 | ctx->user_bufs = NULL; |
bd54b6fe | 8815 | ctx->buf_data = NULL; |
edafccee | 8816 | ctx->nr_user_bufs = 0; |
bd54b6fe BM |
8817 | } |
8818 | ||
0a96bbe4 | 8819 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee | 8820 | { |
bd54b6fe | 8821 | int ret; |
edafccee | 8822 | |
bd54b6fe | 8823 | if (!ctx->buf_data) |
edafccee JA |
8824 | return -ENXIO; |
8825 | ||
bd54b6fe BM |
8826 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
8827 | if (!ret) | |
8828 | __io_sqe_buffers_unregister(ctx); | |
8829 | return ret; | |
edafccee JA |
8830 | } |
8831 | ||
8832 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8833 | void __user *arg, unsigned index) | |
8834 | { | |
8835 | struct iovec __user *src; | |
8836 | ||
8837 | #ifdef CONFIG_COMPAT | |
8838 | if (ctx->compat) { | |
8839 | struct compat_iovec __user *ciovs; | |
8840 | struct compat_iovec ciov; | |
8841 | ||
8842 | ciovs = (struct compat_iovec __user *) arg; | |
8843 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8844 | return -EFAULT; | |
8845 | ||
d55e5f5b | 8846 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8847 | dst->iov_len = ciov.iov_len; |
8848 | return 0; | |
8849 | } | |
8850 | #endif | |
8851 | src = (struct iovec __user *) arg; | |
8852 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8853 | return -EFAULT; | |
8854 | return 0; | |
8855 | } | |
8856 | ||
de293938 JA |
8857 | /* |
8858 | * Not super efficient, but this is just a registration time. And we do cache | |
8859 | * the last compound head, so generally we'll only do a full search if we don't | |
8860 | * match that one. | |
8861 | * | |
8862 | * We check if the given compound head page has already been accounted, to | |
8863 | * avoid double accounting it. This allows us to account the full size of the | |
8864 | * page, not just the constituent pages of a huge page. | |
8865 | */ | |
8866 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8867 | int nr_pages, struct page *hpage) | |
8868 | { | |
8869 | int i, j; | |
8870 | ||
8871 | /* check current page array */ | |
8872 | for (i = 0; i < nr_pages; i++) { | |
8873 | if (!PageCompound(pages[i])) | |
8874 | continue; | |
8875 | if (compound_head(pages[i]) == hpage) | |
8876 | return true; | |
8877 | } | |
8878 | ||
8879 | /* check previously registered pages */ | |
8880 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
41edf1a5 | 8881 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
de293938 JA |
8882 | |
8883 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8884 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8885 | continue; | |
8886 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8887 | return true; | |
8888 | } | |
8889 | } | |
8890 | ||
8891 | return false; | |
8892 | } | |
8893 | ||
8894 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8895 | int nr_pages, struct io_mapped_ubuf *imu, | |
8896 | struct page **last_hpage) | |
8897 | { | |
8898 | int i, ret; | |
8899 | ||
216e5835 | 8900 | imu->acct_pages = 0; |
de293938 JA |
8901 | for (i = 0; i < nr_pages; i++) { |
8902 | if (!PageCompound(pages[i])) { | |
8903 | imu->acct_pages++; | |
8904 | } else { | |
8905 | struct page *hpage; | |
8906 | ||
8907 | hpage = compound_head(pages[i]); | |
8908 | if (hpage == *last_hpage) | |
8909 | continue; | |
8910 | *last_hpage = hpage; | |
8911 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8912 | continue; | |
8913 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8914 | } | |
8915 | } | |
8916 | ||
8917 | if (!imu->acct_pages) | |
8918 | return 0; | |
8919 | ||
26bfa89e | 8920 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
8921 | if (ret) |
8922 | imu->acct_pages = 0; | |
8923 | return ret; | |
8924 | } | |
8925 | ||
0a96bbe4 | 8926 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
41edf1a5 | 8927 | struct io_mapped_ubuf **pimu, |
0a96bbe4 | 8928 | struct page **last_hpage) |
edafccee | 8929 | { |
41edf1a5 | 8930 | struct io_mapped_ubuf *imu = NULL; |
edafccee JA |
8931 | struct vm_area_struct **vmas = NULL; |
8932 | struct page **pages = NULL; | |
0a96bbe4 BM |
8933 | unsigned long off, start, end, ubuf; |
8934 | size_t size; | |
8935 | int ret, pret, nr_pages, i; | |
8936 | ||
6224843d PB |
8937 | if (!iov->iov_base) { |
8938 | *pimu = ctx->dummy_ubuf; | |
8939 | return 0; | |
8940 | } | |
8941 | ||
0a96bbe4 BM |
8942 | ubuf = (unsigned long) iov->iov_base; |
8943 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8944 | start = ubuf >> PAGE_SHIFT; | |
8945 | nr_pages = end - start; | |
8946 | ||
41edf1a5 | 8947 | *pimu = NULL; |
0a96bbe4 BM |
8948 | ret = -ENOMEM; |
8949 | ||
8950 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8951 | if (!pages) | |
8952 | goto done; | |
8953 | ||
8954 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8955 | GFP_KERNEL); | |
8956 | if (!vmas) | |
8957 | goto done; | |
edafccee | 8958 | |
41edf1a5 | 8959 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
a2b4198c | 8960 | if (!imu) |
0a96bbe4 BM |
8961 | goto done; |
8962 | ||
8963 | ret = 0; | |
8964 | mmap_read_lock(current->mm); | |
8965 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8966 | pages, vmas); | |
8967 | if (pret == nr_pages) { | |
8968 | /* don't support file backed memory */ | |
8969 | for (i = 0; i < nr_pages; i++) { | |
8970 | struct vm_area_struct *vma = vmas[i]; | |
8971 | ||
40dad765 PB |
8972 | if (vma_is_shmem(vma)) |
8973 | continue; | |
0a96bbe4 BM |
8974 | if (vma->vm_file && |
8975 | !is_file_hugepages(vma->vm_file)) { | |
8976 | ret = -EOPNOTSUPP; | |
8977 | break; | |
8978 | } | |
8979 | } | |
8980 | } else { | |
8981 | ret = pret < 0 ? pret : -EFAULT; | |
8982 | } | |
8983 | mmap_read_unlock(current->mm); | |
8984 | if (ret) { | |
8985 | /* | |
8986 | * if we did partial map, or found file backed vmas, | |
8987 | * release any pages we did get | |
8988 | */ | |
8989 | if (pret > 0) | |
8990 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
8991 | goto done; |
8992 | } | |
8993 | ||
8994 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
8995 | if (ret) { | |
8996 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
8997 | goto done; |
8998 | } | |
8999 | ||
9000 | off = ubuf & ~PAGE_MASK; | |
9001 | size = iov->iov_len; | |
9002 | for (i = 0; i < nr_pages; i++) { | |
9003 | size_t vec_len; | |
9004 | ||
9005 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
9006 | imu->bvec[i].bv_page = pages[i]; | |
9007 | imu->bvec[i].bv_len = vec_len; | |
9008 | imu->bvec[i].bv_offset = off; | |
9009 | off = 0; | |
9010 | size -= vec_len; | |
9011 | } | |
9012 | /* store original address for later verification */ | |
9013 | imu->ubuf = ubuf; | |
4751f53d | 9014 | imu->ubuf_end = ubuf + iov->iov_len; |
0a96bbe4 | 9015 | imu->nr_bvecs = nr_pages; |
41edf1a5 | 9016 | *pimu = imu; |
0a96bbe4 BM |
9017 | ret = 0; |
9018 | done: | |
41edf1a5 PB |
9019 | if (ret) |
9020 | kvfree(imu); | |
0a96bbe4 BM |
9021 | kvfree(pages); |
9022 | kvfree(vmas); | |
9023 | return ret; | |
9024 | } | |
9025 | ||
2b358604 | 9026 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 9027 | { |
87094465 PB |
9028 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
9029 | return ctx->user_bufs ? 0 : -ENOMEM; | |
2b358604 | 9030 | } |
edafccee | 9031 | |
2b358604 BM |
9032 | static int io_buffer_validate(struct iovec *iov) |
9033 | { | |
50e96989 PB |
9034 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
9035 | ||
2b358604 BM |
9036 | /* |
9037 | * Don't impose further limits on the size and buffer | |
9038 | * constraints here, we'll -EINVAL later when IO is | |
9039 | * submitted if they are wrong. | |
9040 | */ | |
6224843d PB |
9041 | if (!iov->iov_base) |
9042 | return iov->iov_len ? -EFAULT : 0; | |
9043 | if (!iov->iov_len) | |
2b358604 | 9044 | return -EFAULT; |
edafccee | 9045 | |
2b358604 BM |
9046 | /* arbitrary limit, but we need something */ |
9047 | if (iov->iov_len > SZ_1G) | |
9048 | return -EFAULT; | |
edafccee | 9049 | |
50e96989 PB |
9050 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
9051 | return -EOVERFLOW; | |
9052 | ||
2b358604 BM |
9053 | return 0; |
9054 | } | |
edafccee | 9055 | |
2b358604 | 9056 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
634d00df | 9057 | unsigned int nr_args, u64 __user *tags) |
2b358604 | 9058 | { |
bd54b6fe BM |
9059 | struct page *last_hpage = NULL; |
9060 | struct io_rsrc_data *data; | |
2b358604 BM |
9061 | int i, ret; |
9062 | struct iovec iov; | |
edafccee | 9063 | |
87094465 PB |
9064 | if (ctx->user_bufs) |
9065 | return -EBUSY; | |
489809e2 | 9066 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
87094465 | 9067 | return -EINVAL; |
bd54b6fe | 9068 | ret = io_rsrc_node_switch_start(ctx); |
2b358604 BM |
9069 | if (ret) |
9070 | return ret; | |
d878c816 PB |
9071 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
9072 | if (ret) | |
9073 | return ret; | |
bd54b6fe BM |
9074 | ret = io_buffers_map_alloc(ctx, nr_args); |
9075 | if (ret) { | |
bb6659cc | 9076 | io_rsrc_data_free(data); |
bd54b6fe BM |
9077 | return ret; |
9078 | } | |
edafccee | 9079 | |
87094465 | 9080 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
edafccee JA |
9081 | ret = io_copy_iov(ctx, &iov, arg, i); |
9082 | if (ret) | |
0a96bbe4 | 9083 | break; |
2b358604 BM |
9084 | ret = io_buffer_validate(&iov); |
9085 | if (ret) | |
0a96bbe4 | 9086 | break; |
2d091d62 | 9087 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
cf3770e7 CIK |
9088 | ret = -EINVAL; |
9089 | break; | |
9090 | } | |
edafccee | 9091 | |
41edf1a5 PB |
9092 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
9093 | &last_hpage); | |
0a96bbe4 BM |
9094 | if (ret) |
9095 | break; | |
edafccee | 9096 | } |
0a96bbe4 | 9097 | |
bd54b6fe | 9098 | WARN_ON_ONCE(ctx->buf_data); |
0a96bbe4 | 9099 | |
bd54b6fe BM |
9100 | ctx->buf_data = data; |
9101 | if (ret) | |
9102 | __io_sqe_buffers_unregister(ctx); | |
9103 | else | |
9104 | io_rsrc_node_switch(ctx, NULL); | |
edafccee JA |
9105 | return ret; |
9106 | } | |
9107 | ||
634d00df PB |
9108 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
9109 | struct io_uring_rsrc_update2 *up, | |
9110 | unsigned int nr_args) | |
9111 | { | |
9112 | u64 __user *tags = u64_to_user_ptr(up->tags); | |
9113 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); | |
634d00df PB |
9114 | struct page *last_hpage = NULL; |
9115 | bool needs_switch = false; | |
9116 | __u32 done; | |
9117 | int i, err; | |
9118 | ||
9119 | if (!ctx->buf_data) | |
9120 | return -ENXIO; | |
9121 | if (up->offset + nr_args > ctx->nr_user_bufs) | |
9122 | return -EINVAL; | |
9123 | ||
9124 | for (done = 0; done < nr_args; done++) { | |
0b8c0e7c PB |
9125 | struct io_mapped_ubuf *imu; |
9126 | int offset = up->offset + done; | |
634d00df PB |
9127 | u64 tag = 0; |
9128 | ||
9129 | err = io_copy_iov(ctx, &iov, iovs, done); | |
9130 | if (err) | |
9131 | break; | |
9132 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { | |
9133 | err = -EFAULT; | |
9134 | break; | |
9135 | } | |
0b8c0e7c PB |
9136 | err = io_buffer_validate(&iov); |
9137 | if (err) | |
9138 | break; | |
cf3770e7 CIK |
9139 | if (!iov.iov_base && tag) { |
9140 | err = -EINVAL; | |
9141 | break; | |
9142 | } | |
0b8c0e7c PB |
9143 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
9144 | if (err) | |
9145 | break; | |
634d00df | 9146 | |
0b8c0e7c | 9147 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
6224843d | 9148 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
0b8c0e7c PB |
9149 | err = io_queue_rsrc_removal(ctx->buf_data, offset, |
9150 | ctx->rsrc_node, ctx->user_bufs[i]); | |
9151 | if (unlikely(err)) { | |
9152 | io_buffer_unmap(ctx, &imu); | |
634d00df | 9153 | break; |
0b8c0e7c | 9154 | } |
634d00df PB |
9155 | ctx->user_bufs[i] = NULL; |
9156 | needs_switch = true; | |
9157 | } | |
9158 | ||
0b8c0e7c | 9159 | ctx->user_bufs[i] = imu; |
2d091d62 | 9160 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
634d00df PB |
9161 | } |
9162 | ||
9163 | if (needs_switch) | |
9164 | io_rsrc_node_switch(ctx, ctx->buf_data); | |
9165 | return done ? done : err; | |
9166 | } | |
9167 | ||
9b402849 JA |
9168 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
9169 | { | |
9170 | __s32 __user *fds = arg; | |
9171 | int fd; | |
9172 | ||
9173 | if (ctx->cq_ev_fd) | |
9174 | return -EBUSY; | |
9175 | ||
9176 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
9177 | return -EFAULT; | |
9178 | ||
9179 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
9180 | if (IS_ERR(ctx->cq_ev_fd)) { | |
9181 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
fe7e3257 | 9182 | |
9b402849 JA |
9183 | ctx->cq_ev_fd = NULL; |
9184 | return ret; | |
9185 | } | |
9186 | ||
9187 | return 0; | |
9188 | } | |
9189 | ||
9190 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
9191 | { | |
9192 | if (ctx->cq_ev_fd) { | |
9193 | eventfd_ctx_put(ctx->cq_ev_fd); | |
9194 | ctx->cq_ev_fd = NULL; | |
9195 | return 0; | |
9196 | } | |
9197 | ||
9198 | return -ENXIO; | |
9199 | } | |
9200 | ||
5a2e745d JA |
9201 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
9202 | { | |
9e15c3a0 JA |
9203 | struct io_buffer *buf; |
9204 | unsigned long index; | |
9205 | ||
8bab4c09 | 9206 | xa_for_each(&ctx->io_buffers, index, buf) { |
9e15c3a0 | 9207 | __io_remove_buffers(ctx, buf, index, -1U); |
8bab4c09 JA |
9208 | cond_resched(); |
9209 | } | |
5a2e745d JA |
9210 | } |
9211 | ||
4010fec4 | 9212 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9213 | { |
cd0ca2e0 | 9214 | struct io_submit_state *state = &ctx->submit_state; |
37f0e767 | 9215 | int nr = 0; |
bf019da7 | 9216 | |
9a4fdbd8 | 9217 | mutex_lock(&ctx->uring_lock); |
cd0ca2e0 | 9218 | io_flush_cached_locked_reqs(ctx, state); |
c2b6c6bc PB |
9219 | |
9220 | while (state->free_list.next) { | |
9221 | struct io_wq_work_node *node; | |
9222 | struct io_kiocb *req; | |
9223 | ||
9224 | node = wq_stack_extract(&state->free_list); | |
9225 | req = container_of(node, struct io_kiocb, comp_list); | |
9226 | kmem_cache_free(req_cachep, req); | |
37f0e767 | 9227 | nr++; |
c2b6c6bc | 9228 | } |
37f0e767 PB |
9229 | if (nr) |
9230 | percpu_ref_put_many(&ctx->refs, nr); | |
9a4fdbd8 JA |
9231 | mutex_unlock(&ctx->uring_lock); |
9232 | } | |
9233 | ||
43597aac | 9234 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
2b188cc1 | 9235 | { |
43597aac | 9236 | if (data && !atomic_dec_and_test(&data->refs)) |
bd54b6fe | 9237 | wait_for_completion(&data->done); |
bd54b6fe | 9238 | } |
04fc6c80 | 9239 | |
c072481d | 9240 | static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9241 | { |
37d1e2e3 | 9242 | io_sq_thread_finish(ctx); |
2aede0e4 | 9243 | |
37d1e2e3 | 9244 | if (ctx->mm_account) { |
2aede0e4 JA |
9245 | mmdrop(ctx->mm_account); |
9246 | ctx->mm_account = NULL; | |
30975825 | 9247 | } |
def596e9 | 9248 | |
ab409402 | 9249 | io_rsrc_refs_drop(ctx); |
43597aac PB |
9250 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
9251 | io_wait_rsrc_data(ctx->buf_data); | |
9252 | io_wait_rsrc_data(ctx->file_data); | |
9253 | ||
8bad28d8 | 9254 | mutex_lock(&ctx->uring_lock); |
43597aac | 9255 | if (ctx->buf_data) |
bd54b6fe | 9256 | __io_sqe_buffers_unregister(ctx); |
43597aac | 9257 | if (ctx->file_data) |
08480400 | 9258 | __io_sqe_files_unregister(ctx); |
c4ea060e PB |
9259 | if (ctx->rings) |
9260 | __io_cqring_overflow_flush(ctx, true); | |
8bad28d8 | 9261 | mutex_unlock(&ctx->uring_lock); |
9b402849 | 9262 | io_eventfd_unregister(ctx); |
5a2e745d | 9263 | io_destroy_buffers(ctx); |
07db298a PB |
9264 | if (ctx->sq_creds) |
9265 | put_cred(ctx->sq_creds); | |
def596e9 | 9266 | |
a7f0ed5a PB |
9267 | /* there are no registered resources left, nobody uses it */ |
9268 | if (ctx->rsrc_node) | |
9269 | io_rsrc_node_destroy(ctx->rsrc_node); | |
8dd03afe | 9270 | if (ctx->rsrc_backup_node) |
b895c9a6 | 9271 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
a7f0ed5a | 9272 | flush_delayed_work(&ctx->rsrc_put_work); |
756ab7c0 | 9273 | flush_delayed_work(&ctx->fallback_work); |
a7f0ed5a PB |
9274 | |
9275 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); | |
9276 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); | |
def596e9 | 9277 | |
2b188cc1 | 9278 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
9279 | if (ctx->ring_sock) { |
9280 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 9281 | sock_release(ctx->ring_sock); |
355e8d26 | 9282 | } |
2b188cc1 | 9283 | #endif |
ef9dd637 | 9284 | WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); |
2b188cc1 | 9285 | |
75b28aff | 9286 | io_mem_free(ctx->rings); |
2b188cc1 | 9287 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
9288 | |
9289 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 9290 | free_uid(ctx->user); |
4010fec4 | 9291 | io_req_caches_free(ctx); |
e941894e JA |
9292 | if (ctx->hash_map) |
9293 | io_wq_put_hash(ctx->hash_map); | |
78076bb6 | 9294 | kfree(ctx->cancel_hash); |
6224843d | 9295 | kfree(ctx->dummy_ubuf); |
2b188cc1 JA |
9296 | kfree(ctx); |
9297 | } | |
9298 | ||
9299 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
9300 | { | |
9301 | struct io_ring_ctx *ctx = file->private_data; | |
9302 | __poll_t mask = 0; | |
9303 | ||
d60aa65b | 9304 | poll_wait(file, &ctx->cq_wait, wait); |
4f7067c3 SB |
9305 | /* |
9306 | * synchronizes with barrier from wq_has_sleeper call in | |
9307 | * io_commit_cqring | |
9308 | */ | |
2b188cc1 | 9309 | smp_rmb(); |
90554200 | 9310 | if (!io_sqring_full(ctx)) |
2b188cc1 | 9311 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
9312 | |
9313 | /* | |
9314 | * Don't flush cqring overflow list here, just do a simple check. | |
9315 | * Otherwise there could possible be ABBA deadlock: | |
9316 | * CPU0 CPU1 | |
9317 | * ---- ---- | |
9318 | * lock(&ctx->uring_lock); | |
9319 | * lock(&ep->mtx); | |
9320 | * lock(&ctx->uring_lock); | |
9321 | * lock(&ep->mtx); | |
9322 | * | |
9323 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
9324 | * pushs them to do the flush. | |
9325 | */ | |
5ed7a37d | 9326 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
2b188cc1 JA |
9327 | mask |= EPOLLIN | EPOLLRDNORM; |
9328 | ||
9329 | return mask; | |
9330 | } | |
9331 | ||
0bead8cd | 9332 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 9333 | { |
4379bf8b | 9334 | const struct cred *creds; |
071698e1 | 9335 | |
61cf9370 | 9336 | creds = xa_erase(&ctx->personalities, id); |
4379bf8b JA |
9337 | if (creds) { |
9338 | put_cred(creds); | |
0bead8cd | 9339 | return 0; |
1e6fa521 | 9340 | } |
0bead8cd YD |
9341 | |
9342 | return -EINVAL; | |
9343 | } | |
9344 | ||
d56d938b PB |
9345 | struct io_tctx_exit { |
9346 | struct callback_head task_work; | |
9347 | struct completion completion; | |
baf186c4 | 9348 | struct io_ring_ctx *ctx; |
d56d938b PB |
9349 | }; |
9350 | ||
c072481d | 9351 | static __cold void io_tctx_exit_cb(struct callback_head *cb) |
d56d938b PB |
9352 | { |
9353 | struct io_uring_task *tctx = current->io_uring; | |
9354 | struct io_tctx_exit *work; | |
9355 | ||
9356 | work = container_of(cb, struct io_tctx_exit, task_work); | |
9357 | /* | |
9358 | * When @in_idle, we're in cancellation and it's racy to remove the | |
9359 | * node. It'll be removed by the end of cancellation, just ignore it. | |
9360 | */ | |
9361 | if (!atomic_read(&tctx->in_idle)) | |
eef51daa | 9362 | io_uring_del_tctx_node((unsigned long)work->ctx); |
d56d938b PB |
9363 | complete(&work->completion); |
9364 | } | |
9365 | ||
c072481d | 9366 | static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
28090c13 PB |
9367 | { |
9368 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
9369 | ||
9370 | return req->ctx == data; | |
9371 | } | |
9372 | ||
c072481d | 9373 | static __cold void io_ring_exit_work(struct work_struct *work) |
85faa7b8 | 9374 | { |
d56d938b | 9375 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 9376 | unsigned long timeout = jiffies + HZ * 60 * 5; |
58d3be2c | 9377 | unsigned long interval = HZ / 20; |
d56d938b PB |
9378 | struct io_tctx_exit exit; |
9379 | struct io_tctx_node *node; | |
9380 | int ret; | |
85faa7b8 | 9381 | |
56952e91 JA |
9382 | /* |
9383 | * If we're doing polled IO and end up having requests being | |
9384 | * submitted async (out-of-line), then completions can come in while | |
9385 | * we're waiting for refs to drop. We need to reap these manually, | |
9386 | * as nobody else will be looking for them. | |
9387 | */ | |
b2edc0a7 | 9388 | do { |
3dd0c97a | 9389 | io_uring_try_cancel_requests(ctx, NULL, true); |
28090c13 PB |
9390 | if (ctx->sq_data) { |
9391 | struct io_sq_data *sqd = ctx->sq_data; | |
9392 | struct task_struct *tsk; | |
9393 | ||
9394 | io_sq_thread_park(sqd); | |
9395 | tsk = sqd->thread; | |
9396 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) | |
9397 | io_wq_cancel_cb(tsk->io_uring->io_wq, | |
9398 | io_cancel_ctx_cb, ctx, true); | |
9399 | io_sq_thread_unpark(sqd); | |
9400 | } | |
b5bb3a24 | 9401 | |
37f0e767 PB |
9402 | io_req_caches_free(ctx); |
9403 | ||
58d3be2c PB |
9404 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
9405 | /* there is little hope left, don't run it too often */ | |
9406 | interval = HZ * 60; | |
9407 | } | |
9408 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); | |
d56d938b | 9409 | |
7f00651a PB |
9410 | init_completion(&exit.completion); |
9411 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
9412 | exit.ctx = ctx; | |
89b5066e PB |
9413 | /* |
9414 | * Some may use context even when all refs and requests have been put, | |
9415 | * and they are free to do so while still holding uring_lock or | |
5b0a6acc | 9416 | * completion_lock, see io_req_task_submit(). Apart from other work, |
89b5066e PB |
9417 | * this lock/unlock section also waits them to finish. |
9418 | */ | |
d56d938b PB |
9419 | mutex_lock(&ctx->uring_lock); |
9420 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
9421 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
9422 | ||
d56d938b PB |
9423 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
9424 | ctx_node); | |
7f00651a PB |
9425 | /* don't spin on a single task if cancellation failed */ |
9426 | list_rotate_left(&ctx->tctx_list); | |
d56d938b PB |
9427 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
9428 | if (WARN_ON_ONCE(ret)) | |
9429 | continue; | |
d56d938b PB |
9430 | |
9431 | mutex_unlock(&ctx->uring_lock); | |
9432 | wait_for_completion(&exit.completion); | |
d56d938b PB |
9433 | mutex_lock(&ctx->uring_lock); |
9434 | } | |
9435 | mutex_unlock(&ctx->uring_lock); | |
79ebeaee JA |
9436 | spin_lock(&ctx->completion_lock); |
9437 | spin_unlock(&ctx->completion_lock); | |
d56d938b | 9438 | |
85faa7b8 JA |
9439 | io_ring_ctx_free(ctx); |
9440 | } | |
9441 | ||
80c4cbdb | 9442 | /* Returns true if we found and killed one or more timeouts */ |
c072481d PB |
9443 | static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx, |
9444 | struct task_struct *tsk, bool cancel_all) | |
80c4cbdb PB |
9445 | { |
9446 | struct io_kiocb *req, *tmp; | |
9447 | int canceled = 0; | |
9448 | ||
79ebeaee JA |
9449 | spin_lock(&ctx->completion_lock); |
9450 | spin_lock_irq(&ctx->timeout_lock); | |
80c4cbdb | 9451 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
3dd0c97a | 9452 | if (io_match_task(req, tsk, cancel_all)) { |
80c4cbdb PB |
9453 | io_kill_timeout(req, -ECANCELED); |
9454 | canceled++; | |
9455 | } | |
9456 | } | |
79ebeaee | 9457 | spin_unlock_irq(&ctx->timeout_lock); |
51520426 PB |
9458 | if (canceled != 0) |
9459 | io_commit_cqring(ctx); | |
79ebeaee | 9460 | spin_unlock(&ctx->completion_lock); |
80c4cbdb PB |
9461 | if (canceled != 0) |
9462 | io_cqring_ev_posted(ctx); | |
9463 | return canceled != 0; | |
9464 | } | |
9465 | ||
c072481d | 9466 | static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
2b188cc1 | 9467 | { |
61cf9370 MWO |
9468 | unsigned long index; |
9469 | struct creds *creds; | |
9470 | ||
2b188cc1 JA |
9471 | mutex_lock(&ctx->uring_lock); |
9472 | percpu_ref_kill(&ctx->refs); | |
634578f8 | 9473 | if (ctx->rings) |
6c2450ae | 9474 | __io_cqring_overflow_flush(ctx, true); |
61cf9370 MWO |
9475 | xa_for_each(&ctx->personalities, index, creds) |
9476 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
9477 | mutex_unlock(&ctx->uring_lock); |
9478 | ||
3dd0c97a PB |
9479 | io_kill_timeouts(ctx, NULL, true); |
9480 | io_poll_remove_all(ctx, NULL, true); | |
561fb04a | 9481 | |
15dff286 | 9482 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 9483 | io_iopoll_try_reap_events(ctx); |
309fc03a | 9484 | |
85faa7b8 | 9485 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
9486 | /* |
9487 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
9488 | * if we're exiting a ton of rings at the same time. It just adds | |
9489 | * noise and overhead, there's no discernable change in runtime | |
9490 | * over using system_wq. | |
9491 | */ | |
9492 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
9493 | } |
9494 | ||
9495 | static int io_uring_release(struct inode *inode, struct file *file) | |
9496 | { | |
9497 | struct io_ring_ctx *ctx = file->private_data; | |
9498 | ||
9499 | file->private_data = NULL; | |
9500 | io_ring_ctx_wait_and_kill(ctx); | |
9501 | return 0; | |
9502 | } | |
9503 | ||
f6edbabb PB |
9504 | struct io_task_cancel { |
9505 | struct task_struct *task; | |
3dd0c97a | 9506 | bool all; |
f6edbabb | 9507 | }; |
f254ac04 | 9508 | |
f6edbabb | 9509 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 9510 | { |
9a472ef7 | 9511 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 9512 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
9513 | bool ret; |
9514 | ||
3dd0c97a | 9515 | if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
9516 | struct io_ring_ctx *ctx = req->ctx; |
9517 | ||
9518 | /* protect against races with linked timeouts */ | |
79ebeaee | 9519 | spin_lock(&ctx->completion_lock); |
3dd0c97a | 9520 | ret = io_match_task(req, cancel->task, cancel->all); |
79ebeaee | 9521 | spin_unlock(&ctx->completion_lock); |
9a472ef7 | 9522 | } else { |
3dd0c97a | 9523 | ret = io_match_task(req, cancel->task, cancel->all); |
9a472ef7 PB |
9524 | } |
9525 | return ret; | |
b711d4ea JA |
9526 | } |
9527 | ||
c072481d PB |
9528 | static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
9529 | struct task_struct *task, | |
9530 | bool cancel_all) | |
b7ddce3c | 9531 | { |
e1915f76 | 9532 | struct io_defer_entry *de; |
b7ddce3c PB |
9533 | LIST_HEAD(list); |
9534 | ||
79ebeaee | 9535 | spin_lock(&ctx->completion_lock); |
b7ddce3c | 9536 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
3dd0c97a | 9537 | if (io_match_task(de->req, task, cancel_all)) { |
b7ddce3c PB |
9538 | list_cut_position(&list, &ctx->defer_list, &de->list); |
9539 | break; | |
9540 | } | |
9541 | } | |
79ebeaee | 9542 | spin_unlock(&ctx->completion_lock); |
e1915f76 PB |
9543 | if (list_empty(&list)) |
9544 | return false; | |
b7ddce3c PB |
9545 | |
9546 | while (!list_empty(&list)) { | |
9547 | de = list_first_entry(&list, struct io_defer_entry, list); | |
9548 | list_del_init(&de->list); | |
f41db273 | 9549 | io_req_complete_failed(de->req, -ECANCELED); |
b7ddce3c PB |
9550 | kfree(de); |
9551 | } | |
e1915f76 | 9552 | return true; |
b7ddce3c PB |
9553 | } |
9554 | ||
c072481d | 9555 | static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
1b00764f PB |
9556 | { |
9557 | struct io_tctx_node *node; | |
9558 | enum io_wq_cancel cret; | |
9559 | bool ret = false; | |
9560 | ||
9561 | mutex_lock(&ctx->uring_lock); | |
9562 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
9563 | struct io_uring_task *tctx = node->task->io_uring; | |
9564 | ||
9565 | /* | |
9566 | * io_wq will stay alive while we hold uring_lock, because it's | |
9567 | * killed after ctx nodes, which requires to take the lock. | |
9568 | */ | |
9569 | if (!tctx || !tctx->io_wq) | |
9570 | continue; | |
9571 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
9572 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9573 | } | |
9574 | mutex_unlock(&ctx->uring_lock); | |
9575 | ||
9576 | return ret; | |
9577 | } | |
9578 | ||
c072481d PB |
9579 | static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
9580 | struct task_struct *task, | |
9581 | bool cancel_all) | |
9936c7c2 | 9582 | { |
3dd0c97a | 9583 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
1b00764f | 9584 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 PB |
9585 | |
9586 | while (1) { | |
9587 | enum io_wq_cancel cret; | |
9588 | bool ret = false; | |
9589 | ||
1b00764f PB |
9590 | if (!task) { |
9591 | ret |= io_uring_try_cancel_iowq(ctx); | |
9592 | } else if (tctx && tctx->io_wq) { | |
9593 | /* | |
9594 | * Cancels requests of all rings, not only @ctx, but | |
9595 | * it's fine as the task is in exit/exec. | |
9596 | */ | |
5aa75ed5 | 9597 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
9598 | &cancel, true); |
9599 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9600 | } | |
9601 | ||
9602 | /* SQPOLL thread does its own polling */ | |
3dd0c97a | 9603 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
d052d1d6 | 9604 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
5eef4e87 | 9605 | while (!wq_list_empty(&ctx->iopoll_list)) { |
9936c7c2 PB |
9606 | io_iopoll_try_reap_events(ctx); |
9607 | ret = true; | |
9608 | } | |
9609 | } | |
9610 | ||
3dd0c97a PB |
9611 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
9612 | ret |= io_poll_remove_all(ctx, task, cancel_all); | |
9613 | ret |= io_kill_timeouts(ctx, task, cancel_all); | |
e5dc480d PB |
9614 | if (task) |
9615 | ret |= io_run_task_work(); | |
9936c7c2 PB |
9616 | if (!ret) |
9617 | break; | |
9618 | cond_resched(); | |
9619 | } | |
9620 | } | |
9621 | ||
eef51daa | 9622 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
0f212204 | 9623 | { |
236434c3 | 9624 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 9625 | struct io_tctx_node *node; |
a528b04e | 9626 | int ret; |
236434c3 MWO |
9627 | |
9628 | if (unlikely(!tctx)) { | |
5aa75ed5 | 9629 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
9630 | if (unlikely(ret)) |
9631 | return ret; | |
236434c3 | 9632 | tctx = current->io_uring; |
0f212204 | 9633 | } |
cf27f3b1 PB |
9634 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
9635 | node = kmalloc(sizeof(*node), GFP_KERNEL); | |
9636 | if (!node) | |
9637 | return -ENOMEM; | |
9638 | node->ctx = ctx; | |
9639 | node->task = current; | |
13bf43f5 | 9640 | |
cf27f3b1 PB |
9641 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
9642 | node, GFP_KERNEL)); | |
9643 | if (ret) { | |
9644 | kfree(node); | |
9645 | return ret; | |
0f212204 | 9646 | } |
cf27f3b1 PB |
9647 | |
9648 | mutex_lock(&ctx->uring_lock); | |
9649 | list_add(&node->ctx_node, &ctx->tctx_list); | |
9650 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 9651 | } |
cf27f3b1 | 9652 | tctx->last = ctx; |
0f212204 JA |
9653 | return 0; |
9654 | } | |
9655 | ||
cf27f3b1 PB |
9656 | /* |
9657 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
9658 | */ | |
eef51daa | 9659 | static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
cf27f3b1 PB |
9660 | { |
9661 | struct io_uring_task *tctx = current->io_uring; | |
9662 | ||
9663 | if (likely(tctx && tctx->last == ctx)) | |
9664 | return 0; | |
eef51daa | 9665 | return __io_uring_add_tctx_node(ctx); |
cf27f3b1 PB |
9666 | } |
9667 | ||
0f212204 JA |
9668 | /* |
9669 | * Remove this io_uring_file -> task mapping. | |
9670 | */ | |
c072481d | 9671 | static __cold void io_uring_del_tctx_node(unsigned long index) |
0f212204 JA |
9672 | { |
9673 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 9674 | struct io_tctx_node *node; |
2941267b | 9675 | |
eebd2e37 PB |
9676 | if (!tctx) |
9677 | return; | |
13bf43f5 PB |
9678 | node = xa_erase(&tctx->xa, index); |
9679 | if (!node) | |
2941267b | 9680 | return; |
0f212204 | 9681 | |
13bf43f5 PB |
9682 | WARN_ON_ONCE(current != node->task); |
9683 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
9684 | ||
9685 | mutex_lock(&node->ctx->uring_lock); | |
9686 | list_del(&node->ctx_node); | |
9687 | mutex_unlock(&node->ctx->uring_lock); | |
9688 | ||
baf186c4 | 9689 | if (tctx->last == node->ctx) |
0f212204 | 9690 | tctx->last = NULL; |
13bf43f5 | 9691 | kfree(node); |
0f212204 JA |
9692 | } |
9693 | ||
c072481d | 9694 | static __cold void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 9695 | { |
ba5ef6dc | 9696 | struct io_wq *wq = tctx->io_wq; |
13bf43f5 | 9697 | struct io_tctx_node *node; |
de7f1d9e PB |
9698 | unsigned long index; |
9699 | ||
8bab4c09 | 9700 | xa_for_each(&tctx->xa, index, node) { |
eef51daa | 9701 | io_uring_del_tctx_node(index); |
8bab4c09 JA |
9702 | cond_resched(); |
9703 | } | |
b16ef427 ME |
9704 | if (wq) { |
9705 | /* | |
9706 | * Must be after io_uring_del_task_file() (removes nodes under | |
9707 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). | |
9708 | */ | |
ba5ef6dc | 9709 | io_wq_put_and_exit(wq); |
dadebc35 | 9710 | tctx->io_wq = NULL; |
b16ef427 | 9711 | } |
de7f1d9e PB |
9712 | } |
9713 | ||
3f48cf18 | 9714 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
521d6a73 | 9715 | { |
3f48cf18 PB |
9716 | if (tracked) |
9717 | return atomic_read(&tctx->inflight_tracked); | |
521d6a73 PB |
9718 | return percpu_counter_sum(&tctx->inflight); |
9719 | } | |
9720 | ||
c072481d | 9721 | static __cold void io_uring_drop_tctx_refs(struct task_struct *task) |
09899b19 PB |
9722 | { |
9723 | struct io_uring_task *tctx = task->io_uring; | |
9724 | unsigned int refs = tctx->cached_refs; | |
9725 | ||
e9dbe221 PB |
9726 | if (refs) { |
9727 | tctx->cached_refs = 0; | |
9728 | percpu_counter_sub(&tctx->inflight, refs); | |
9729 | put_task_struct_many(task, refs); | |
9730 | } | |
09899b19 PB |
9731 | } |
9732 | ||
78cc687b PB |
9733 | /* |
9734 | * Find any io_uring ctx that this task has registered or done IO on, and cancel | |
9735 | * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation. | |
9736 | */ | |
c072481d PB |
9737 | static __cold void io_uring_cancel_generic(bool cancel_all, |
9738 | struct io_sq_data *sqd) | |
0e9ddb39 | 9739 | { |
521d6a73 | 9740 | struct io_uring_task *tctx = current->io_uring; |
734551df | 9741 | struct io_ring_ctx *ctx; |
0e9ddb39 PB |
9742 | s64 inflight; |
9743 | DEFINE_WAIT(wait); | |
fdaf083c | 9744 | |
78cc687b PB |
9745 | WARN_ON_ONCE(sqd && sqd->thread != current); |
9746 | ||
6d042ffb PO |
9747 | if (!current->io_uring) |
9748 | return; | |
17a91051 PB |
9749 | if (tctx->io_wq) |
9750 | io_wq_exit_start(tctx->io_wq); | |
9751 | ||
0e9ddb39 PB |
9752 | atomic_inc(&tctx->in_idle); |
9753 | do { | |
e9dbe221 | 9754 | io_uring_drop_tctx_refs(current); |
0e9ddb39 | 9755 | /* read completions before cancelations */ |
78cc687b | 9756 | inflight = tctx_inflight(tctx, !cancel_all); |
0e9ddb39 PB |
9757 | if (!inflight) |
9758 | break; | |
fdaf083c | 9759 | |
78cc687b PB |
9760 | if (!sqd) { |
9761 | struct io_tctx_node *node; | |
9762 | unsigned long index; | |
0f212204 | 9763 | |
78cc687b PB |
9764 | xa_for_each(&tctx->xa, index, node) { |
9765 | /* sqpoll task will cancel all its requests */ | |
9766 | if (node->ctx->sq_data) | |
9767 | continue; | |
9768 | io_uring_try_cancel_requests(node->ctx, current, | |
9769 | cancel_all); | |
9770 | } | |
9771 | } else { | |
9772 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
9773 | io_uring_try_cancel_requests(ctx, current, | |
9774 | cancel_all); | |
9775 | } | |
17a91051 | 9776 | |
0f212204 | 9777 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
e9dbe221 | 9778 | io_uring_drop_tctx_refs(current); |
0f212204 | 9779 | /* |
a1bb3cd5 PB |
9780 | * If we've seen completions, retry without waiting. This |
9781 | * avoids a race where a completion comes in before we did | |
9782 | * prepare_to_wait(). | |
0f212204 | 9783 | */ |
3dd0c97a | 9784 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
a1bb3cd5 | 9785 | schedule(); |
f57555ed | 9786 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 9787 | } while (1); |
fdaf083c | 9788 | atomic_dec(&tctx->in_idle); |
de7f1d9e | 9789 | |
8452d4a6 | 9790 | io_uring_clean_tctx(tctx); |
3dd0c97a | 9791 | if (cancel_all) { |
3f48cf18 PB |
9792 | /* for exec all current's requests should be gone, kill tctx */ |
9793 | __io_uring_free(current); | |
9794 | } | |
44e728b8 PB |
9795 | } |
9796 | ||
f552a27a | 9797 | void __io_uring_cancel(bool cancel_all) |
78cc687b | 9798 | { |
f552a27a | 9799 | io_uring_cancel_generic(cancel_all, NULL); |
78cc687b PB |
9800 | } |
9801 | ||
6c5c240e RP |
9802 | static void *io_uring_validate_mmap_request(struct file *file, |
9803 | loff_t pgoff, size_t sz) | |
2b188cc1 | 9804 | { |
2b188cc1 | 9805 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 9806 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
9807 | struct page *page; |
9808 | void *ptr; | |
9809 | ||
9810 | switch (offset) { | |
9811 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
9812 | case IORING_OFF_CQ_RING: |
9813 | ptr = ctx->rings; | |
2b188cc1 JA |
9814 | break; |
9815 | case IORING_OFF_SQES: | |
9816 | ptr = ctx->sq_sqes; | |
9817 | break; | |
2b188cc1 | 9818 | default: |
6c5c240e | 9819 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
9820 | } |
9821 | ||
9822 | page = virt_to_head_page(ptr); | |
a50b854e | 9823 | if (sz > page_size(page)) |
6c5c240e RP |
9824 | return ERR_PTR(-EINVAL); |
9825 | ||
9826 | return ptr; | |
9827 | } | |
9828 | ||
9829 | #ifdef CONFIG_MMU | |
9830 | ||
c072481d | 9831 | static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
6c5c240e RP |
9832 | { |
9833 | size_t sz = vma->vm_end - vma->vm_start; | |
9834 | unsigned long pfn; | |
9835 | void *ptr; | |
9836 | ||
9837 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
9838 | if (IS_ERR(ptr)) | |
9839 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9840 | |
9841 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9842 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9843 | } | |
9844 | ||
6c5c240e RP |
9845 | #else /* !CONFIG_MMU */ |
9846 | ||
9847 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9848 | { | |
9849 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9850 | } | |
9851 | ||
9852 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9853 | { | |
9854 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9855 | } | |
9856 | ||
9857 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9858 | unsigned long addr, unsigned long len, | |
9859 | unsigned long pgoff, unsigned long flags) | |
9860 | { | |
9861 | void *ptr; | |
9862 | ||
9863 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9864 | if (IS_ERR(ptr)) | |
9865 | return PTR_ERR(ptr); | |
9866 | ||
9867 | return (unsigned long) ptr; | |
9868 | } | |
9869 | ||
9870 | #endif /* !CONFIG_MMU */ | |
9871 | ||
d9d05217 | 9872 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 JA |
9873 | { |
9874 | DEFINE_WAIT(wait); | |
9875 | ||
9876 | do { | |
9877 | if (!io_sqring_full(ctx)) | |
9878 | break; | |
90554200 JA |
9879 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
9880 | ||
9881 | if (!io_sqring_full(ctx)) | |
9882 | break; | |
90554200 JA |
9883 | schedule(); |
9884 | } while (!signal_pending(current)); | |
9885 | ||
9886 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
5199328a | 9887 | return 0; |
90554200 JA |
9888 | } |
9889 | ||
c73ebb68 HX |
9890 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9891 | struct __kernel_timespec __user **ts, | |
9892 | const sigset_t __user **sig) | |
9893 | { | |
9894 | struct io_uring_getevents_arg arg; | |
9895 | ||
9896 | /* | |
9897 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9898 | * is just a pointer to the sigset_t. | |
9899 | */ | |
9900 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9901 | *sig = (const sigset_t __user *) argp; | |
9902 | *ts = NULL; | |
9903 | return 0; | |
9904 | } | |
9905 | ||
9906 | /* | |
9907 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9908 | * timespec and sigset_t pointers if good. | |
9909 | */ | |
9910 | if (*argsz != sizeof(arg)) | |
9911 | return -EINVAL; | |
9912 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9913 | return -EFAULT; | |
9914 | *sig = u64_to_user_ptr(arg.sigmask); | |
9915 | *argsz = arg.sigmask_sz; | |
9916 | *ts = u64_to_user_ptr(arg.ts); | |
9917 | return 0; | |
9918 | } | |
9919 | ||
2b188cc1 | 9920 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9921 | u32, min_complete, u32, flags, const void __user *, argp, |
9922 | size_t, argsz) | |
2b188cc1 JA |
9923 | { |
9924 | struct io_ring_ctx *ctx; | |
2b188cc1 JA |
9925 | int submitted = 0; |
9926 | struct fd f; | |
33f993da | 9927 | long ret; |
2b188cc1 | 9928 | |
4c6e277c | 9929 | io_run_task_work(); |
b41e9852 | 9930 | |
33f993da PB |
9931 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
9932 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) | |
2b188cc1 JA |
9933 | return -EINVAL; |
9934 | ||
9935 | f = fdget(fd); | |
33f993da | 9936 | if (unlikely(!f.file)) |
2b188cc1 JA |
9937 | return -EBADF; |
9938 | ||
9939 | ret = -EOPNOTSUPP; | |
33f993da | 9940 | if (unlikely(f.file->f_op != &io_uring_fops)) |
2b188cc1 JA |
9941 | goto out_fput; |
9942 | ||
9943 | ret = -ENXIO; | |
9944 | ctx = f.file->private_data; | |
33f993da | 9945 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
2b188cc1 JA |
9946 | goto out_fput; |
9947 | ||
7e84e1c7 | 9948 | ret = -EBADFD; |
33f993da | 9949 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
9950 | goto out; |
9951 | ||
6c271ce2 JA |
9952 | /* |
9953 | * For SQ polling, the thread will do all submissions and completions. | |
9954 | * Just return the requested submit count, and wake the thread if | |
9955 | * we were asked to. | |
9956 | */ | |
b2a9eada | 9957 | ret = 0; |
6c271ce2 | 9958 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
90f67366 | 9959 | io_cqring_overflow_flush(ctx); |
89448c47 | 9960 | |
21f96522 JA |
9961 | if (unlikely(ctx->sq_data->thread == NULL)) { |
9962 | ret = -EOWNERDEAD; | |
04147488 | 9963 | goto out; |
21f96522 | 9964 | } |
6c271ce2 | 9965 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9966 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9967 | if (flags & IORING_ENTER_SQ_WAIT) { |
9968 | ret = io_sqpoll_wait_sq(ctx); | |
9969 | if (ret) | |
9970 | goto out; | |
9971 | } | |
6c271ce2 | 9972 | submitted = to_submit; |
b2a9eada | 9973 | } else if (to_submit) { |
eef51daa | 9974 | ret = io_uring_add_tctx_node(ctx); |
0f212204 JA |
9975 | if (unlikely(ret)) |
9976 | goto out; | |
2b188cc1 | 9977 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9978 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9979 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9980 | |
9981 | if (submitted != to_submit) | |
9982 | goto out; | |
2b188cc1 JA |
9983 | } |
9984 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9985 | const sigset_t __user *sig; |
9986 | struct __kernel_timespec __user *ts; | |
9987 | ||
9988 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9989 | if (unlikely(ret)) | |
9990 | goto out; | |
9991 | ||
2b188cc1 JA |
9992 | min_complete = min(min_complete, ctx->cq_entries); |
9993 | ||
32b2244a XW |
9994 | /* |
9995 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9996 | * space applications don't need to do io completion events | |
9997 | * polling again, they can rely on io_sq_thread to do polling | |
9998 | * work, which can reduce cpu usage and uring_lock contention. | |
9999 | */ | |
10000 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
10001 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 10002 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 10003 | } else { |
c73ebb68 | 10004 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 10005 | } |
2b188cc1 JA |
10006 | } |
10007 | ||
7c504e65 | 10008 | out: |
6805b32e | 10009 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
10010 | out_fput: |
10011 | fdput(f); | |
10012 | return submitted ? submitted : ret; | |
10013 | } | |
10014 | ||
bebdb65e | 10015 | #ifdef CONFIG_PROC_FS |
c072481d | 10016 | static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id, |
61cf9370 | 10017 | const struct cred *cred) |
87ce955b | 10018 | { |
87ce955b JA |
10019 | struct user_namespace *uns = seq_user_ns(m); |
10020 | struct group_info *gi; | |
10021 | kernel_cap_t cap; | |
10022 | unsigned __capi; | |
10023 | int g; | |
10024 | ||
10025 | seq_printf(m, "%5d\n", id); | |
10026 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
10027 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
10028 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
10029 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
10030 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
10031 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
10032 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
10033 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
10034 | seq_puts(m, "\n\tGroups:\t"); | |
10035 | gi = cred->group_info; | |
10036 | for (g = 0; g < gi->ngroups; g++) { | |
10037 | seq_put_decimal_ull(m, g ? " " : "", | |
10038 | from_kgid_munged(uns, gi->gid[g])); | |
10039 | } | |
10040 | seq_puts(m, "\n\tCapEff:\t"); | |
10041 | cap = cred->cap_effective; | |
10042 | CAP_FOR_EACH_U32(__capi) | |
10043 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
10044 | seq_putc(m, '\n'); | |
10045 | return 0; | |
10046 | } | |
10047 | ||
c072481d PB |
10048 | static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, |
10049 | struct seq_file *m) | |
87ce955b | 10050 | { |
dbbe9c64 | 10051 | struct io_sq_data *sq = NULL; |
83f84356 HX |
10052 | struct io_overflow_cqe *ocqe; |
10053 | struct io_rings *r = ctx->rings; | |
10054 | unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1; | |
10055 | unsigned int cached_sq_head = ctx->cached_sq_head; | |
10056 | unsigned int cached_cq_tail = ctx->cached_cq_tail; | |
10057 | unsigned int sq_head = READ_ONCE(r->sq.head); | |
10058 | unsigned int sq_tail = READ_ONCE(r->sq.tail); | |
10059 | unsigned int cq_head = READ_ONCE(r->cq.head); | |
10060 | unsigned int cq_tail = READ_ONCE(r->cq.tail); | |
fad8e0de | 10061 | bool has_lock; |
83f84356 HX |
10062 | unsigned int i; |
10063 | ||
10064 | /* | |
10065 | * we may get imprecise sqe and cqe info if uring is actively running | |
10066 | * since we get cached_sq_head and cached_cq_tail without uring_lock | |
10067 | * and sq_tail and cq_head are changed by userspace. But it's ok since | |
10068 | * we usually use these info when it is stuck. | |
10069 | */ | |
10070 | seq_printf(m, "SqHead:\t%u\n", sq_head & sq_mask); | |
10071 | seq_printf(m, "SqTail:\t%u\n", sq_tail & sq_mask); | |
10072 | seq_printf(m, "CachedSqHead:\t%u\n", cached_sq_head & sq_mask); | |
10073 | seq_printf(m, "CqHead:\t%u\n", cq_head & cq_mask); | |
10074 | seq_printf(m, "CqTail:\t%u\n", cq_tail & cq_mask); | |
10075 | seq_printf(m, "CachedCqTail:\t%u\n", cached_cq_tail & cq_mask); | |
10076 | seq_printf(m, "SQEs:\t%u\n", sq_tail - cached_sq_head); | |
10077 | for (i = cached_sq_head; i < sq_tail; i++) { | |
10078 | unsigned int sq_idx = READ_ONCE(ctx->sq_array[i & sq_mask]); | |
10079 | ||
10080 | if (likely(sq_idx <= sq_mask)) { | |
10081 | struct io_uring_sqe *sqe = &ctx->sq_sqes[sq_idx]; | |
10082 | ||
10083 | seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n", | |
10084 | sq_idx, sqe->opcode, sqe->fd, sqe->flags, sqe->user_data); | |
10085 | } | |
10086 | } | |
10087 | seq_printf(m, "CQEs:\t%u\n", cached_cq_tail - cq_head); | |
10088 | for (i = cq_head; i < cached_cq_tail; i++) { | |
10089 | struct io_uring_cqe *cqe = &r->cqes[i & cq_mask]; | |
10090 | ||
10091 | seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n", | |
10092 | i & cq_mask, cqe->user_data, cqe->res, cqe->flags); | |
10093 | } | |
87ce955b | 10094 | |
fad8e0de JA |
10095 | /* |
10096 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
10097 | * since fdinfo case grabs it in the opposite direction of normal use | |
10098 | * cases. If we fail to get the lock, we just don't iterate any | |
10099 | * structures that could be going away outside the io_uring mutex. | |
10100 | */ | |
10101 | has_lock = mutex_trylock(&ctx->uring_lock); | |
10102 | ||
5f3f26f9 | 10103 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 10104 | sq = ctx->sq_data; |
5f3f26f9 JA |
10105 | if (!sq->thread) |
10106 | sq = NULL; | |
10107 | } | |
dbbe9c64 JQ |
10108 | |
10109 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
10110 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 10111 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 10112 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
7b29f92d | 10113 | struct file *f = io_file_from_index(ctx, i); |
87ce955b | 10114 | |
87ce955b JA |
10115 | if (f) |
10116 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
10117 | else | |
10118 | seq_printf(m, "%5u: <none>\n", i); | |
10119 | } | |
10120 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 10121 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
41edf1a5 | 10122 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
4751f53d | 10123 | unsigned int len = buf->ubuf_end - buf->ubuf; |
87ce955b | 10124 | |
4751f53d | 10125 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
87ce955b | 10126 | } |
61cf9370 MWO |
10127 | if (has_lock && !xa_empty(&ctx->personalities)) { |
10128 | unsigned long index; | |
10129 | const struct cred *cred; | |
10130 | ||
87ce955b | 10131 | seq_printf(m, "Personalities:\n"); |
61cf9370 MWO |
10132 | xa_for_each(&ctx->personalities, index, cred) |
10133 | io_uring_show_cred(m, index, cred); | |
87ce955b | 10134 | } |
83f84356 HX |
10135 | if (has_lock) |
10136 | mutex_unlock(&ctx->uring_lock); | |
10137 | ||
10138 | seq_puts(m, "PollList:\n"); | |
79ebeaee | 10139 | spin_lock(&ctx->completion_lock); |
d7718a9d JA |
10140 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
10141 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
10142 | struct io_kiocb *req; | |
10143 | ||
10144 | hlist_for_each_entry(req, list, hash_node) | |
10145 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
10146 | req->task->task_works != NULL); | |
10147 | } | |
83f84356 HX |
10148 | |
10149 | seq_puts(m, "CqOverflowList:\n"); | |
10150 | list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) { | |
10151 | struct io_uring_cqe *cqe = &ocqe->cqe; | |
10152 | ||
10153 | seq_printf(m, " user_data=%llu, res=%d, flags=%x\n", | |
10154 | cqe->user_data, cqe->res, cqe->flags); | |
10155 | ||
10156 | } | |
10157 | ||
79ebeaee | 10158 | spin_unlock(&ctx->completion_lock); |
87ce955b JA |
10159 | } |
10160 | ||
c072481d | 10161 | static __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
87ce955b JA |
10162 | { |
10163 | struct io_ring_ctx *ctx = f->private_data; | |
10164 | ||
10165 | if (percpu_ref_tryget(&ctx->refs)) { | |
10166 | __io_uring_show_fdinfo(ctx, m); | |
10167 | percpu_ref_put(&ctx->refs); | |
10168 | } | |
10169 | } | |
bebdb65e | 10170 | #endif |
87ce955b | 10171 | |
2b188cc1 JA |
10172 | static const struct file_operations io_uring_fops = { |
10173 | .release = io_uring_release, | |
10174 | .mmap = io_uring_mmap, | |
6c5c240e RP |
10175 | #ifndef CONFIG_MMU |
10176 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
10177 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
10178 | #endif | |
2b188cc1 | 10179 | .poll = io_uring_poll, |
bebdb65e | 10180 | #ifdef CONFIG_PROC_FS |
87ce955b | 10181 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 10182 | #endif |
2b188cc1 JA |
10183 | }; |
10184 | ||
c072481d PB |
10185 | static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
10186 | struct io_uring_params *p) | |
2b188cc1 | 10187 | { |
75b28aff HV |
10188 | struct io_rings *rings; |
10189 | size_t size, sq_array_offset; | |
2b188cc1 | 10190 | |
bd740481 JA |
10191 | /* make sure these are sane, as we already accounted them */ |
10192 | ctx->sq_entries = p->sq_entries; | |
10193 | ctx->cq_entries = p->cq_entries; | |
10194 | ||
75b28aff HV |
10195 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
10196 | if (size == SIZE_MAX) | |
10197 | return -EOVERFLOW; | |
10198 | ||
10199 | rings = io_mem_alloc(size); | |
10200 | if (!rings) | |
2b188cc1 JA |
10201 | return -ENOMEM; |
10202 | ||
75b28aff HV |
10203 | ctx->rings = rings; |
10204 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
10205 | rings->sq_ring_mask = p->sq_entries - 1; | |
10206 | rings->cq_ring_mask = p->cq_entries - 1; | |
10207 | rings->sq_ring_entries = p->sq_entries; | |
10208 | rings->cq_ring_entries = p->cq_entries; | |
2b188cc1 JA |
10209 | |
10210 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
10211 | if (size == SIZE_MAX) { |
10212 | io_mem_free(ctx->rings); | |
10213 | ctx->rings = NULL; | |
2b188cc1 | 10214 | return -EOVERFLOW; |
eb065d30 | 10215 | } |
2b188cc1 JA |
10216 | |
10217 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
10218 | if (!ctx->sq_sqes) { |
10219 | io_mem_free(ctx->rings); | |
10220 | ctx->rings = NULL; | |
2b188cc1 | 10221 | return -ENOMEM; |
eb065d30 | 10222 | } |
2b188cc1 | 10223 | |
2b188cc1 JA |
10224 | return 0; |
10225 | } | |
10226 | ||
9faadcc8 PB |
10227 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
10228 | { | |
10229 | int ret, fd; | |
10230 | ||
10231 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
10232 | if (fd < 0) | |
10233 | return fd; | |
10234 | ||
eef51daa | 10235 | ret = io_uring_add_tctx_node(ctx); |
9faadcc8 PB |
10236 | if (ret) { |
10237 | put_unused_fd(fd); | |
10238 | return ret; | |
10239 | } | |
10240 | fd_install(fd, file); | |
10241 | return fd; | |
10242 | } | |
10243 | ||
2b188cc1 JA |
10244 | /* |
10245 | * Allocate an anonymous fd, this is what constitutes the application | |
10246 | * visible backing of an io_uring instance. The application mmaps this | |
10247 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
10248 | * we have to tie this fd to a socket for file garbage collection purposes. | |
10249 | */ | |
9faadcc8 | 10250 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
10251 | { |
10252 | struct file *file; | |
9faadcc8 | 10253 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
10254 | int ret; |
10255 | ||
2b188cc1 JA |
10256 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
10257 | &ctx->ring_sock); | |
10258 | if (ret) | |
9faadcc8 | 10259 | return ERR_PTR(ret); |
2b188cc1 JA |
10260 | #endif |
10261 | ||
2b188cc1 JA |
10262 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
10263 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 10264 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
10265 | if (IS_ERR(file)) { |
10266 | sock_release(ctx->ring_sock); | |
10267 | ctx->ring_sock = NULL; | |
10268 | } else { | |
10269 | ctx->ring_sock->file = file; | |
0f212204 | 10270 | } |
2b188cc1 | 10271 | #endif |
9faadcc8 | 10272 | return file; |
2b188cc1 JA |
10273 | } |
10274 | ||
c072481d PB |
10275 | static __cold int io_uring_create(unsigned entries, struct io_uring_params *p, |
10276 | struct io_uring_params __user *params) | |
2b188cc1 | 10277 | { |
2b188cc1 | 10278 | struct io_ring_ctx *ctx; |
9faadcc8 | 10279 | struct file *file; |
2b188cc1 JA |
10280 | int ret; |
10281 | ||
8110c1a6 | 10282 | if (!entries) |
2b188cc1 | 10283 | return -EINVAL; |
8110c1a6 JA |
10284 | if (entries > IORING_MAX_ENTRIES) { |
10285 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
10286 | return -EINVAL; | |
10287 | entries = IORING_MAX_ENTRIES; | |
10288 | } | |
2b188cc1 JA |
10289 | |
10290 | /* | |
10291 | * Use twice as many entries for the CQ ring. It's possible for the | |
10292 | * application to drive a higher depth than the size of the SQ ring, | |
10293 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
10294 | * some flexibility in overcommitting a bit. If the application has |
10295 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
10296 | * of CQ ring entries manually. | |
2b188cc1 JA |
10297 | */ |
10298 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
10299 | if (p->flags & IORING_SETUP_CQSIZE) { |
10300 | /* | |
10301 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
10302 | * to a power-of-two, if it isn't already. We do NOT impose | |
10303 | * any cq vs sq ring sizing. | |
10304 | */ | |
eb2667b3 | 10305 | if (!p->cq_entries) |
33a107f0 | 10306 | return -EINVAL; |
8110c1a6 JA |
10307 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
10308 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
10309 | return -EINVAL; | |
10310 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
10311 | } | |
eb2667b3 JQ |
10312 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
10313 | if (p->cq_entries < p->sq_entries) | |
10314 | return -EINVAL; | |
33a107f0 JA |
10315 | } else { |
10316 | p->cq_entries = 2 * p->sq_entries; | |
10317 | } | |
2b188cc1 | 10318 | |
2b188cc1 | 10319 | ctx = io_ring_ctx_alloc(p); |
62e398be | 10320 | if (!ctx) |
2b188cc1 | 10321 | return -ENOMEM; |
2b188cc1 | 10322 | ctx->compat = in_compat_syscall(); |
62e398be JA |
10323 | if (!capable(CAP_IPC_LOCK)) |
10324 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
10325 | |
10326 | /* | |
10327 | * This is just grabbed for accounting purposes. When a process exits, | |
10328 | * the mm is exited and dropped before the files, hence we need to hang | |
10329 | * on to this mm purely for the purposes of being able to unaccount | |
10330 | * memory (locked/pinned vm). It's not used for anything else. | |
10331 | */ | |
6b7898eb | 10332 | mmgrab(current->mm); |
2aede0e4 | 10333 | ctx->mm_account = current->mm; |
6b7898eb | 10334 | |
2b188cc1 JA |
10335 | ret = io_allocate_scq_urings(ctx, p); |
10336 | if (ret) | |
10337 | goto err; | |
10338 | ||
7e84e1c7 | 10339 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
10340 | if (ret) |
10341 | goto err; | |
eae071c9 | 10342 | /* always set a rsrc node */ |
47b228ce PB |
10343 | ret = io_rsrc_node_switch_start(ctx); |
10344 | if (ret) | |
10345 | goto err; | |
eae071c9 | 10346 | io_rsrc_node_switch(ctx, NULL); |
2b188cc1 | 10347 | |
2b188cc1 | 10348 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
10349 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
10350 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
10351 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
10352 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
10353 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
10354 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
10355 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
10356 | |
10357 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
10358 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
10359 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
10360 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
10361 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
10362 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
10363 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 10364 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 10365 | |
7f13657d XW |
10366 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
10367 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 10368 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 10369 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9690557e PB |
10370 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
10371 | IORING_FEAT_RSRC_TAGS; | |
7f13657d XW |
10372 | |
10373 | if (copy_to_user(params, p, sizeof(*p))) { | |
10374 | ret = -EFAULT; | |
10375 | goto err; | |
10376 | } | |
d1719f70 | 10377 | |
9faadcc8 PB |
10378 | file = io_uring_get_file(ctx); |
10379 | if (IS_ERR(file)) { | |
10380 | ret = PTR_ERR(file); | |
10381 | goto err; | |
10382 | } | |
10383 | ||
044c1ab3 JA |
10384 | /* |
10385 | * Install ring fd as the very last thing, so we don't risk someone | |
10386 | * having closed it before we finish setup | |
10387 | */ | |
9faadcc8 PB |
10388 | ret = io_uring_install_fd(ctx, file); |
10389 | if (ret < 0) { | |
10390 | /* fput will clean it up */ | |
10391 | fput(file); | |
10392 | return ret; | |
10393 | } | |
044c1ab3 | 10394 | |
c826bd7a | 10395 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
10396 | return ret; |
10397 | err: | |
10398 | io_ring_ctx_wait_and_kill(ctx); | |
10399 | return ret; | |
10400 | } | |
10401 | ||
10402 | /* | |
10403 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
10404 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
10405 | * params structure passed in. | |
10406 | */ | |
10407 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
10408 | { | |
10409 | struct io_uring_params p; | |
2b188cc1 JA |
10410 | int i; |
10411 | ||
10412 | if (copy_from_user(&p, params, sizeof(p))) | |
10413 | return -EFAULT; | |
10414 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
10415 | if (p.resv[i]) | |
10416 | return -EINVAL; | |
10417 | } | |
10418 | ||
6c271ce2 | 10419 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 10420 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
10421 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
10422 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
10423 | return -EINVAL; |
10424 | ||
7f13657d | 10425 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
10426 | } |
10427 | ||
10428 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
10429 | struct io_uring_params __user *, params) | |
10430 | { | |
10431 | return io_uring_setup(entries, params); | |
10432 | } | |
10433 | ||
c072481d PB |
10434 | static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg, |
10435 | unsigned nr_args) | |
66f4af93 JA |
10436 | { |
10437 | struct io_uring_probe *p; | |
10438 | size_t size; | |
10439 | int i, ret; | |
10440 | ||
10441 | size = struct_size(p, ops, nr_args); | |
10442 | if (size == SIZE_MAX) | |
10443 | return -EOVERFLOW; | |
10444 | p = kzalloc(size, GFP_KERNEL); | |
10445 | if (!p) | |
10446 | return -ENOMEM; | |
10447 | ||
10448 | ret = -EFAULT; | |
10449 | if (copy_from_user(p, arg, size)) | |
10450 | goto out; | |
10451 | ret = -EINVAL; | |
10452 | if (memchr_inv(p, 0, size)) | |
10453 | goto out; | |
10454 | ||
10455 | p->last_op = IORING_OP_LAST - 1; | |
10456 | if (nr_args > IORING_OP_LAST) | |
10457 | nr_args = IORING_OP_LAST; | |
10458 | ||
10459 | for (i = 0; i < nr_args; i++) { | |
10460 | p->ops[i].op = i; | |
10461 | if (!io_op_defs[i].not_supported) | |
10462 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
10463 | } | |
10464 | p->ops_len = i; | |
10465 | ||
10466 | ret = 0; | |
10467 | if (copy_to_user(arg, p, size)) | |
10468 | ret = -EFAULT; | |
10469 | out: | |
10470 | kfree(p); | |
10471 | return ret; | |
10472 | } | |
10473 | ||
071698e1 JA |
10474 | static int io_register_personality(struct io_ring_ctx *ctx) |
10475 | { | |
4379bf8b | 10476 | const struct cred *creds; |
61cf9370 | 10477 | u32 id; |
1e6fa521 | 10478 | int ret; |
071698e1 | 10479 | |
4379bf8b | 10480 | creds = get_current_cred(); |
1e6fa521 | 10481 | |
61cf9370 MWO |
10482 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
10483 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); | |
a30f895a JA |
10484 | if (ret < 0) { |
10485 | put_cred(creds); | |
10486 | return ret; | |
10487 | } | |
10488 | return id; | |
071698e1 JA |
10489 | } |
10490 | ||
c072481d PB |
10491 | static __cold int io_register_restrictions(struct io_ring_ctx *ctx, |
10492 | void __user *arg, unsigned int nr_args) | |
21b55dbc SG |
10493 | { |
10494 | struct io_uring_restriction *res; | |
10495 | size_t size; | |
10496 | int i, ret; | |
10497 | ||
7e84e1c7 SG |
10498 | /* Restrictions allowed only if rings started disabled */ |
10499 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10500 | return -EBADFD; | |
10501 | ||
21b55dbc | 10502 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 10503 | if (ctx->restrictions.registered) |
21b55dbc SG |
10504 | return -EBUSY; |
10505 | ||
10506 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
10507 | return -EINVAL; | |
10508 | ||
10509 | size = array_size(nr_args, sizeof(*res)); | |
10510 | if (size == SIZE_MAX) | |
10511 | return -EOVERFLOW; | |
10512 | ||
10513 | res = memdup_user(arg, size); | |
10514 | if (IS_ERR(res)) | |
10515 | return PTR_ERR(res); | |
10516 | ||
10517 | ret = 0; | |
10518 | ||
10519 | for (i = 0; i < nr_args; i++) { | |
10520 | switch (res[i].opcode) { | |
10521 | case IORING_RESTRICTION_REGISTER_OP: | |
10522 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
10523 | ret = -EINVAL; | |
10524 | goto out; | |
10525 | } | |
10526 | ||
10527 | __set_bit(res[i].register_op, | |
10528 | ctx->restrictions.register_op); | |
10529 | break; | |
10530 | case IORING_RESTRICTION_SQE_OP: | |
10531 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
10532 | ret = -EINVAL; | |
10533 | goto out; | |
10534 | } | |
10535 | ||
10536 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
10537 | break; | |
10538 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
10539 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
10540 | break; | |
10541 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
10542 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
10543 | break; | |
10544 | default: | |
10545 | ret = -EINVAL; | |
10546 | goto out; | |
10547 | } | |
10548 | } | |
10549 | ||
10550 | out: | |
10551 | /* Reset all restrictions if an error happened */ | |
10552 | if (ret != 0) | |
10553 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
10554 | else | |
7e84e1c7 | 10555 | ctx->restrictions.registered = true; |
21b55dbc SG |
10556 | |
10557 | kfree(res); | |
10558 | return ret; | |
10559 | } | |
10560 | ||
7e84e1c7 SG |
10561 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
10562 | { | |
10563 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10564 | return -EBADFD; | |
10565 | ||
10566 | if (ctx->restrictions.registered) | |
10567 | ctx->restricted = 1; | |
10568 | ||
0298ef96 PB |
10569 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
10570 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) | |
10571 | wake_up(&ctx->sq_data->wait); | |
7e84e1c7 SG |
10572 | return 0; |
10573 | } | |
10574 | ||
fdecb662 | 10575 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 10576 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 PB |
10577 | unsigned nr_args) |
10578 | { | |
10579 | __u32 tmp; | |
10580 | int err; | |
10581 | ||
c3bdad02 PB |
10582 | if (up->resv) |
10583 | return -EINVAL; | |
98f0b3b4 PB |
10584 | if (check_add_overflow(up->offset, nr_args, &tmp)) |
10585 | return -EOVERFLOW; | |
10586 | err = io_rsrc_node_switch_start(ctx); | |
10587 | if (err) | |
10588 | return err; | |
10589 | ||
fdecb662 PB |
10590 | switch (type) { |
10591 | case IORING_RSRC_FILE: | |
98f0b3b4 | 10592 | return __io_sqe_files_update(ctx, up, nr_args); |
634d00df PB |
10593 | case IORING_RSRC_BUFFER: |
10594 | return __io_sqe_buffers_update(ctx, up, nr_args); | |
98f0b3b4 PB |
10595 | } |
10596 | return -EINVAL; | |
10597 | } | |
10598 | ||
c3bdad02 PB |
10599 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
10600 | unsigned nr_args) | |
98f0b3b4 | 10601 | { |
c3bdad02 | 10602 | struct io_uring_rsrc_update2 up; |
98f0b3b4 PB |
10603 | |
10604 | if (!nr_args) | |
10605 | return -EINVAL; | |
c3bdad02 PB |
10606 | memset(&up, 0, sizeof(up)); |
10607 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) | |
10608 | return -EFAULT; | |
10609 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); | |
10610 | } | |
10611 | ||
10612 | static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
992da01a | 10613 | unsigned size, unsigned type) |
c3bdad02 PB |
10614 | { |
10615 | struct io_uring_rsrc_update2 up; | |
10616 | ||
10617 | if (size != sizeof(up)) | |
10618 | return -EINVAL; | |
98f0b3b4 PB |
10619 | if (copy_from_user(&up, arg, sizeof(up))) |
10620 | return -EFAULT; | |
992da01a | 10621 | if (!up.nr || up.resv) |
98f0b3b4 | 10622 | return -EINVAL; |
992da01a | 10623 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
98f0b3b4 PB |
10624 | } |
10625 | ||
c072481d | 10626 | static __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
992da01a | 10627 | unsigned int size, unsigned int type) |
792e3582 PB |
10628 | { |
10629 | struct io_uring_rsrc_register rr; | |
10630 | ||
10631 | /* keep it extendible */ | |
10632 | if (size != sizeof(rr)) | |
10633 | return -EINVAL; | |
10634 | ||
10635 | memset(&rr, 0, sizeof(rr)); | |
10636 | if (copy_from_user(&rr, arg, size)) | |
10637 | return -EFAULT; | |
992da01a | 10638 | if (!rr.nr || rr.resv || rr.resv2) |
792e3582 PB |
10639 | return -EINVAL; |
10640 | ||
992da01a | 10641 | switch (type) { |
792e3582 PB |
10642 | case IORING_RSRC_FILE: |
10643 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), | |
10644 | rr.nr, u64_to_user_ptr(rr.tags)); | |
634d00df PB |
10645 | case IORING_RSRC_BUFFER: |
10646 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), | |
10647 | rr.nr, u64_to_user_ptr(rr.tags)); | |
792e3582 PB |
10648 | } |
10649 | return -EINVAL; | |
10650 | } | |
10651 | ||
c072481d PB |
10652 | static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx, |
10653 | void __user *arg, unsigned len) | |
fe76421d JA |
10654 | { |
10655 | struct io_uring_task *tctx = current->io_uring; | |
10656 | cpumask_var_t new_mask; | |
10657 | int ret; | |
10658 | ||
10659 | if (!tctx || !tctx->io_wq) | |
10660 | return -EINVAL; | |
10661 | ||
10662 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | |
10663 | return -ENOMEM; | |
10664 | ||
10665 | cpumask_clear(new_mask); | |
10666 | if (len > cpumask_size()) | |
10667 | len = cpumask_size(); | |
10668 | ||
10669 | if (copy_from_user(new_mask, arg, len)) { | |
10670 | free_cpumask_var(new_mask); | |
10671 | return -EFAULT; | |
10672 | } | |
10673 | ||
10674 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); | |
10675 | free_cpumask_var(new_mask); | |
10676 | return ret; | |
10677 | } | |
10678 | ||
c072481d | 10679 | static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx) |
fe76421d JA |
10680 | { |
10681 | struct io_uring_task *tctx = current->io_uring; | |
10682 | ||
10683 | if (!tctx || !tctx->io_wq) | |
10684 | return -EINVAL; | |
10685 | ||
10686 | return io_wq_cpu_affinity(tctx->io_wq, NULL); | |
10687 | } | |
10688 | ||
c072481d PB |
10689 | static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, |
10690 | void __user *arg) | |
2e480058 | 10691 | { |
fa84693b JA |
10692 | struct io_uring_task *tctx = NULL; |
10693 | struct io_sq_data *sqd = NULL; | |
2e480058 JA |
10694 | __u32 new_count[2]; |
10695 | int i, ret; | |
10696 | ||
2e480058 JA |
10697 | if (copy_from_user(new_count, arg, sizeof(new_count))) |
10698 | return -EFAULT; | |
10699 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
10700 | if (new_count[i] > INT_MAX) | |
10701 | return -EINVAL; | |
10702 | ||
fa84693b JA |
10703 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
10704 | sqd = ctx->sq_data; | |
10705 | if (sqd) { | |
009ad9f0 JA |
10706 | /* |
10707 | * Observe the correct sqd->lock -> ctx->uring_lock | |
10708 | * ordering. Fine to drop uring_lock here, we hold | |
10709 | * a ref to the ctx. | |
10710 | */ | |
41d3a6bd | 10711 | refcount_inc(&sqd->refs); |
009ad9f0 | 10712 | mutex_unlock(&ctx->uring_lock); |
fa84693b | 10713 | mutex_lock(&sqd->lock); |
009ad9f0 | 10714 | mutex_lock(&ctx->uring_lock); |
41d3a6bd JA |
10715 | if (sqd->thread) |
10716 | tctx = sqd->thread->io_uring; | |
fa84693b JA |
10717 | } |
10718 | } else { | |
10719 | tctx = current->io_uring; | |
10720 | } | |
10721 | ||
10722 | ret = -EINVAL; | |
10723 | if (!tctx || !tctx->io_wq) | |
10724 | goto err; | |
10725 | ||
2e480058 JA |
10726 | ret = io_wq_max_workers(tctx->io_wq, new_count); |
10727 | if (ret) | |
fa84693b JA |
10728 | goto err; |
10729 | ||
41d3a6bd | 10730 | if (sqd) { |
fa84693b | 10731 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
10732 | io_put_sq_data(sqd); |
10733 | } | |
2e480058 JA |
10734 | |
10735 | if (copy_to_user(arg, new_count, sizeof(new_count))) | |
10736 | return -EFAULT; | |
10737 | ||
10738 | return 0; | |
fa84693b | 10739 | err: |
41d3a6bd | 10740 | if (sqd) { |
fa84693b | 10741 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
10742 | io_put_sq_data(sqd); |
10743 | } | |
fa84693b | 10744 | return ret; |
2e480058 JA |
10745 | } |
10746 | ||
071698e1 JA |
10747 | static bool io_register_op_must_quiesce(int op) |
10748 | { | |
10749 | switch (op) { | |
bd54b6fe BM |
10750 | case IORING_REGISTER_BUFFERS: |
10751 | case IORING_UNREGISTER_BUFFERS: | |
f4f7d21c | 10752 | case IORING_REGISTER_FILES: |
071698e1 JA |
10753 | case IORING_UNREGISTER_FILES: |
10754 | case IORING_REGISTER_FILES_UPDATE: | |
10755 | case IORING_REGISTER_PROBE: | |
10756 | case IORING_REGISTER_PERSONALITY: | |
10757 | case IORING_UNREGISTER_PERSONALITY: | |
992da01a PB |
10758 | case IORING_REGISTER_FILES2: |
10759 | case IORING_REGISTER_FILES_UPDATE2: | |
10760 | case IORING_REGISTER_BUFFERS2: | |
10761 | case IORING_REGISTER_BUFFERS_UPDATE: | |
fe76421d JA |
10762 | case IORING_REGISTER_IOWQ_AFF: |
10763 | case IORING_UNREGISTER_IOWQ_AFF: | |
2e480058 | 10764 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
071698e1 JA |
10765 | return false; |
10766 | default: | |
10767 | return true; | |
10768 | } | |
10769 | } | |
10770 | ||
c072481d | 10771 | static __cold int io_ctx_quiesce(struct io_ring_ctx *ctx) |
e73c5c7c PB |
10772 | { |
10773 | long ret; | |
10774 | ||
10775 | percpu_ref_kill(&ctx->refs); | |
10776 | ||
10777 | /* | |
10778 | * Drop uring mutex before waiting for references to exit. If another | |
10779 | * thread is currently inside io_uring_enter() it might need to grab the | |
10780 | * uring_lock to make progress. If we hold it here across the drain | |
10781 | * wait, then we can deadlock. It's safe to drop the mutex here, since | |
10782 | * no new references will come in after we've killed the percpu ref. | |
10783 | */ | |
10784 | mutex_unlock(&ctx->uring_lock); | |
10785 | do { | |
37f0e767 PB |
10786 | ret = wait_for_completion_interruptible_timeout(&ctx->ref_comp, HZ); |
10787 | if (ret) { | |
10788 | ret = min(0L, ret); | |
e73c5c7c | 10789 | break; |
37f0e767 PB |
10790 | } |
10791 | ||
e73c5c7c | 10792 | ret = io_run_task_work_sig(); |
37f0e767 | 10793 | io_req_caches_free(ctx); |
e73c5c7c PB |
10794 | } while (ret >= 0); |
10795 | mutex_lock(&ctx->uring_lock); | |
10796 | ||
10797 | if (ret) | |
10798 | io_refs_resurrect(&ctx->refs, &ctx->ref_comp); | |
10799 | return ret; | |
10800 | } | |
10801 | ||
edafccee JA |
10802 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
10803 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
10804 | __releases(ctx->uring_lock) |
10805 | __acquires(ctx->uring_lock) | |
edafccee JA |
10806 | { |
10807 | int ret; | |
10808 | ||
35fa71a0 JA |
10809 | /* |
10810 | * We're inside the ring mutex, if the ref is already dying, then | |
10811 | * someone else killed the ctx or is already going through | |
10812 | * io_uring_register(). | |
10813 | */ | |
10814 | if (percpu_ref_is_dying(&ctx->refs)) | |
10815 | return -ENXIO; | |
10816 | ||
75c4021a PB |
10817 | if (ctx->restricted) { |
10818 | if (opcode >= IORING_REGISTER_LAST) | |
10819 | return -EINVAL; | |
10820 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); | |
10821 | if (!test_bit(opcode, ctx->restrictions.register_op)) | |
10822 | return -EACCES; | |
10823 | } | |
10824 | ||
071698e1 | 10825 | if (io_register_op_must_quiesce(opcode)) { |
e73c5c7c PB |
10826 | ret = io_ctx_quiesce(ctx); |
10827 | if (ret) | |
f70865db | 10828 | return ret; |
05f3fb3c | 10829 | } |
edafccee JA |
10830 | |
10831 | switch (opcode) { | |
10832 | case IORING_REGISTER_BUFFERS: | |
634d00df | 10833 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
edafccee JA |
10834 | break; |
10835 | case IORING_UNREGISTER_BUFFERS: | |
10836 | ret = -EINVAL; | |
10837 | if (arg || nr_args) | |
10838 | break; | |
0a96bbe4 | 10839 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 10840 | break; |
6b06314c | 10841 | case IORING_REGISTER_FILES: |
792e3582 | 10842 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
6b06314c JA |
10843 | break; |
10844 | case IORING_UNREGISTER_FILES: | |
10845 | ret = -EINVAL; | |
10846 | if (arg || nr_args) | |
10847 | break; | |
10848 | ret = io_sqe_files_unregister(ctx); | |
10849 | break; | |
c3a31e60 | 10850 | case IORING_REGISTER_FILES_UPDATE: |
c3bdad02 | 10851 | ret = io_register_files_update(ctx, arg, nr_args); |
c3a31e60 | 10852 | break; |
9b402849 | 10853 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 10854 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
10855 | ret = -EINVAL; |
10856 | if (nr_args != 1) | |
10857 | break; | |
10858 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
10859 | if (ret) |
10860 | break; | |
10861 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
10862 | ctx->eventfd_async = 1; | |
10863 | else | |
10864 | ctx->eventfd_async = 0; | |
9b402849 JA |
10865 | break; |
10866 | case IORING_UNREGISTER_EVENTFD: | |
10867 | ret = -EINVAL; | |
10868 | if (arg || nr_args) | |
10869 | break; | |
10870 | ret = io_eventfd_unregister(ctx); | |
10871 | break; | |
66f4af93 JA |
10872 | case IORING_REGISTER_PROBE: |
10873 | ret = -EINVAL; | |
10874 | if (!arg || nr_args > 256) | |
10875 | break; | |
10876 | ret = io_probe(ctx, arg, nr_args); | |
10877 | break; | |
071698e1 JA |
10878 | case IORING_REGISTER_PERSONALITY: |
10879 | ret = -EINVAL; | |
10880 | if (arg || nr_args) | |
10881 | break; | |
10882 | ret = io_register_personality(ctx); | |
10883 | break; | |
10884 | case IORING_UNREGISTER_PERSONALITY: | |
10885 | ret = -EINVAL; | |
10886 | if (arg) | |
10887 | break; | |
10888 | ret = io_unregister_personality(ctx, nr_args); | |
10889 | break; | |
7e84e1c7 SG |
10890 | case IORING_REGISTER_ENABLE_RINGS: |
10891 | ret = -EINVAL; | |
10892 | if (arg || nr_args) | |
10893 | break; | |
10894 | ret = io_register_enable_rings(ctx); | |
10895 | break; | |
21b55dbc SG |
10896 | case IORING_REGISTER_RESTRICTIONS: |
10897 | ret = io_register_restrictions(ctx, arg, nr_args); | |
10898 | break; | |
992da01a PB |
10899 | case IORING_REGISTER_FILES2: |
10900 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); | |
10901 | break; | |
10902 | case IORING_REGISTER_FILES_UPDATE2: | |
10903 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
10904 | IORING_RSRC_FILE); | |
10905 | break; | |
10906 | case IORING_REGISTER_BUFFERS2: | |
10907 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); | |
792e3582 | 10908 | break; |
992da01a PB |
10909 | case IORING_REGISTER_BUFFERS_UPDATE: |
10910 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
10911 | IORING_RSRC_BUFFER); | |
c3bdad02 | 10912 | break; |
fe76421d JA |
10913 | case IORING_REGISTER_IOWQ_AFF: |
10914 | ret = -EINVAL; | |
10915 | if (!arg || !nr_args) | |
10916 | break; | |
10917 | ret = io_register_iowq_aff(ctx, arg, nr_args); | |
10918 | break; | |
10919 | case IORING_UNREGISTER_IOWQ_AFF: | |
10920 | ret = -EINVAL; | |
10921 | if (arg || nr_args) | |
10922 | break; | |
10923 | ret = io_unregister_iowq_aff(ctx); | |
10924 | break; | |
2e480058 JA |
10925 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
10926 | ret = -EINVAL; | |
10927 | if (!arg || nr_args != 2) | |
10928 | break; | |
10929 | ret = io_register_iowq_max_workers(ctx, arg); | |
10930 | break; | |
edafccee JA |
10931 | default: |
10932 | ret = -EINVAL; | |
10933 | break; | |
10934 | } | |
10935 | ||
071698e1 | 10936 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 10937 | /* bring the ctx back to life */ |
05f3fb3c | 10938 | percpu_ref_reinit(&ctx->refs); |
0f158b4c | 10939 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 10940 | } |
edafccee JA |
10941 | return ret; |
10942 | } | |
10943 | ||
10944 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
10945 | void __user *, arg, unsigned int, nr_args) | |
10946 | { | |
10947 | struct io_ring_ctx *ctx; | |
10948 | long ret = -EBADF; | |
10949 | struct fd f; | |
10950 | ||
10951 | f = fdget(fd); | |
10952 | if (!f.file) | |
10953 | return -EBADF; | |
10954 | ||
10955 | ret = -EOPNOTSUPP; | |
10956 | if (f.file->f_op != &io_uring_fops) | |
10957 | goto out_fput; | |
10958 | ||
10959 | ctx = f.file->private_data; | |
10960 | ||
b6c23dd5 PB |
10961 | io_run_task_work(); |
10962 | ||
edafccee JA |
10963 | mutex_lock(&ctx->uring_lock); |
10964 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
10965 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
10966 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
10967 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
10968 | out_fput: |
10969 | fdput(f); | |
10970 | return ret; | |
10971 | } | |
10972 | ||
2b188cc1 JA |
10973 | static int __init io_uring_init(void) |
10974 | { | |
d7f62e82 SM |
10975 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
10976 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
10977 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
10978 | } while (0) | |
10979 | ||
10980 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
10981 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
10982 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
10983 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
10984 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
10985 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
10986 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
10987 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
10988 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
10989 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 10990 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
10991 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
10992 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
10993 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
10994 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
10995 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
10996 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
10997 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
10998 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
10999 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
11000 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
11001 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
11002 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
11003 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
11004 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
11005 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 11006 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
11007 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
11008 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
16340eab | 11009 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
d7f62e82 | 11010 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
7d67af2c | 11011 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
b9445598 | 11012 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
d7f62e82 | 11013 | |
b0d658ec PB |
11014 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
11015 | sizeof(struct io_uring_rsrc_update)); | |
11016 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > | |
11017 | sizeof(struct io_uring_rsrc_update2)); | |
90499ad0 PB |
11018 | |
11019 | /* ->buf_index is u16 */ | |
11020 | BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); | |
11021 | ||
b0d658ec PB |
11022 | /* should fit into one byte */ |
11023 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); | |
68fe256a PB |
11024 | BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8)); |
11025 | BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS); | |
b0d658ec | 11026 | |
d3656344 | 11027 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
32c2d33e | 11028 | BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int)); |
16340eab | 11029 | |
91f245d5 JA |
11030 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
11031 | SLAB_ACCOUNT); | |
2b188cc1 JA |
11032 | return 0; |
11033 | }; | |
11034 | __initcall(io_uring_init); |