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> |
5bd2182d | 82 | #include <linux/audit.h> |
cdc1404a | 83 | #include <linux/security.h> |
2b188cc1 | 84 | |
c826bd7a DD |
85 | #define CREATE_TRACE_POINTS |
86 | #include <trace/events/io_uring.h> | |
87 | ||
2b188cc1 JA |
88 | #include <uapi/linux/io_uring.h> |
89 | ||
90 | #include "internal.h" | |
561fb04a | 91 | #include "io-wq.h" |
2b188cc1 | 92 | |
5277deaa | 93 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 94 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
4ce8ad95 | 95 | #define IORING_SQPOLL_CAP_ENTRIES_VALUE 8 |
65e19f54 | 96 | |
187f08c1 | 97 | /* only define max */ |
042b0d85 | 98 | #define IORING_MAX_FIXED_FILES (1U << 15) |
21b55dbc SG |
99 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
100 | IORING_REGISTER_LAST + IORING_OP_LAST) | |
2b188cc1 | 101 | |
187f08c1 | 102 | #define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3) |
2d091d62 PB |
103 | #define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT) |
104 | #define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1) | |
105 | ||
489809e2 PB |
106 | #define IORING_MAX_REG_BUFFERS (1U << 14) |
107 | ||
68fe256a | 108 | #define SQE_COMMON_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_LINK | \ |
5562a8d7 | 109 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) |
68fe256a | 110 | |
5562a8d7 PB |
111 | #define SQE_VALID_FLAGS (SQE_COMMON_FLAGS | IOSQE_BUFFER_SELECT | \ |
112 | IOSQE_IO_DRAIN | IOSQE_CQE_SKIP_SUCCESS) | |
68fe256a | 113 | |
c854357b | 114 | #define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \ |
d886e185 PB |
115 | REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS | \ |
116 | REQ_F_ASYNC_DATA) | |
b16fed66 | 117 | |
09899b19 PB |
118 | #define IO_TCTX_REFS_CACHE_NR (1U << 10) |
119 | ||
2b188cc1 JA |
120 | struct io_uring { |
121 | u32 head ____cacheline_aligned_in_smp; | |
122 | u32 tail ____cacheline_aligned_in_smp; | |
123 | }; | |
124 | ||
1e84b97b | 125 | /* |
75b28aff HV |
126 | * This data is shared with the application through the mmap at offsets |
127 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
128 | * |
129 | * The offsets to the member fields are published through struct | |
130 | * io_sqring_offsets when calling io_uring_setup. | |
131 | */ | |
75b28aff | 132 | struct io_rings { |
1e84b97b SB |
133 | /* |
134 | * Head and tail offsets into the ring; the offsets need to be | |
135 | * masked to get valid indices. | |
136 | * | |
75b28aff HV |
137 | * The kernel controls head of the sq ring and the tail of the cq ring, |
138 | * and the application controls tail of the sq ring and the head of the | |
139 | * cq ring. | |
1e84b97b | 140 | */ |
75b28aff | 141 | struct io_uring sq, cq; |
1e84b97b | 142 | /* |
75b28aff | 143 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
144 | * ring_entries - 1) |
145 | */ | |
75b28aff HV |
146 | u32 sq_ring_mask, cq_ring_mask; |
147 | /* Ring sizes (constant, power of 2) */ | |
148 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
149 | /* |
150 | * Number of invalid entries dropped by the kernel due to | |
151 | * invalid index stored in array | |
152 | * | |
153 | * Written by the kernel, shouldn't be modified by the | |
154 | * application (i.e. get number of "new events" by comparing to | |
155 | * cached value). | |
156 | * | |
157 | * After a new SQ head value was read by the application this | |
158 | * counter includes all submissions that were dropped reaching | |
159 | * the new SQ head (and possibly more). | |
160 | */ | |
75b28aff | 161 | u32 sq_dropped; |
1e84b97b | 162 | /* |
0d9b5b3a | 163 | * Runtime SQ flags |
1e84b97b SB |
164 | * |
165 | * Written by the kernel, shouldn't be modified by the | |
166 | * application. | |
167 | * | |
168 | * The application needs a full memory barrier before checking | |
169 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
170 | */ | |
75b28aff | 171 | u32 sq_flags; |
0d9b5b3a SG |
172 | /* |
173 | * Runtime CQ flags | |
174 | * | |
175 | * Written by the application, shouldn't be modified by the | |
176 | * kernel. | |
177 | */ | |
fe7e3257 | 178 | u32 cq_flags; |
1e84b97b SB |
179 | /* |
180 | * Number of completion events lost because the queue was full; | |
181 | * this should be avoided by the application by making sure | |
0b4295b5 | 182 | * there are not more requests pending than there is space in |
1e84b97b SB |
183 | * the completion queue. |
184 | * | |
185 | * Written by the kernel, shouldn't be modified by the | |
186 | * application (i.e. get number of "new events" by comparing to | |
187 | * cached value). | |
188 | * | |
189 | * As completion events come in out of order this counter is not | |
190 | * ordered with any other data. | |
191 | */ | |
75b28aff | 192 | u32 cq_overflow; |
1e84b97b SB |
193 | /* |
194 | * Ring buffer of completion events. | |
195 | * | |
196 | * The kernel writes completion events fresh every time they are | |
197 | * produced, so the application is allowed to modify pending | |
198 | * entries. | |
199 | */ | |
75b28aff | 200 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
201 | }; |
202 | ||
45d189c6 | 203 | enum io_uring_cmd_flags { |
51aac424 | 204 | IO_URING_F_COMPLETE_DEFER = 1, |
3b44b371 | 205 | IO_URING_F_UNLOCKED = 2, |
51aac424 PB |
206 | /* int's last bit, sign checks are usually faster than a bit test */ |
207 | IO_URING_F_NONBLOCK = INT_MIN, | |
45d189c6 PB |
208 | }; |
209 | ||
edafccee JA |
210 | struct io_mapped_ubuf { |
211 | u64 ubuf; | |
4751f53d | 212 | u64 ubuf_end; |
edafccee | 213 | unsigned int nr_bvecs; |
de293938 | 214 | unsigned long acct_pages; |
41edf1a5 | 215 | struct bio_vec bvec[]; |
edafccee JA |
216 | }; |
217 | ||
50238531 BM |
218 | struct io_ring_ctx; |
219 | ||
6c2450ae PB |
220 | struct io_overflow_cqe { |
221 | struct io_uring_cqe cqe; | |
222 | struct list_head list; | |
223 | }; | |
224 | ||
a04b0ac0 PB |
225 | struct io_fixed_file { |
226 | /* file * with additional FFS_* flags */ | |
227 | unsigned long file_ptr; | |
228 | }; | |
229 | ||
269bbe5f BM |
230 | struct io_rsrc_put { |
231 | struct list_head list; | |
b60c8dce | 232 | u64 tag; |
50238531 BM |
233 | union { |
234 | void *rsrc; | |
235 | struct file *file; | |
bd54b6fe | 236 | struct io_mapped_ubuf *buf; |
50238531 | 237 | }; |
269bbe5f BM |
238 | }; |
239 | ||
aeca241b | 240 | struct io_file_table { |
042b0d85 | 241 | struct io_fixed_file *files; |
31b51510 JA |
242 | }; |
243 | ||
b895c9a6 | 244 | struct io_rsrc_node { |
05589553 XW |
245 | struct percpu_ref refs; |
246 | struct list_head node; | |
269bbe5f | 247 | struct list_head rsrc_list; |
b895c9a6 | 248 | struct io_rsrc_data *rsrc_data; |
4a38aed2 | 249 | struct llist_node llist; |
e297822b | 250 | bool done; |
05589553 XW |
251 | }; |
252 | ||
40ae0ff7 PB |
253 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
254 | ||
b895c9a6 | 255 | struct io_rsrc_data { |
05f3fb3c JA |
256 | struct io_ring_ctx *ctx; |
257 | ||
2d091d62 PB |
258 | u64 **tags; |
259 | unsigned int nr; | |
40ae0ff7 | 260 | rsrc_put_fn *do_put; |
3e942498 | 261 | atomic_t refs; |
05f3fb3c | 262 | struct completion done; |
8bad28d8 | 263 | bool quiesce; |
05f3fb3c JA |
264 | }; |
265 | ||
5a2e745d JA |
266 | struct io_buffer { |
267 | struct list_head list; | |
268 | __u64 addr; | |
d1f82808 | 269 | __u32 len; |
5a2e745d JA |
270 | __u16 bid; |
271 | }; | |
272 | ||
21b55dbc SG |
273 | struct io_restriction { |
274 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
275 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
276 | u8 sqe_flags_allowed; | |
277 | u8 sqe_flags_required; | |
7e84e1c7 | 278 | bool registered; |
21b55dbc SG |
279 | }; |
280 | ||
37d1e2e3 JA |
281 | enum { |
282 | IO_SQ_THREAD_SHOULD_STOP = 0, | |
283 | IO_SQ_THREAD_SHOULD_PARK, | |
284 | }; | |
285 | ||
534ca6d6 JA |
286 | struct io_sq_data { |
287 | refcount_t refs; | |
9e138a48 | 288 | atomic_t park_pending; |
09a6f4ef | 289 | struct mutex lock; |
69fb2131 JA |
290 | |
291 | /* ctx's that are using this sqd */ | |
292 | struct list_head ctx_list; | |
69fb2131 | 293 | |
534ca6d6 JA |
294 | struct task_struct *thread; |
295 | struct wait_queue_head wait; | |
08369246 XW |
296 | |
297 | unsigned sq_thread_idle; | |
37d1e2e3 JA |
298 | int sq_cpu; |
299 | pid_t task_pid; | |
5c2469e0 | 300 | pid_t task_tgid; |
37d1e2e3 JA |
301 | |
302 | unsigned long state; | |
37d1e2e3 | 303 | struct completion exited; |
534ca6d6 JA |
304 | }; |
305 | ||
6dd0be1e | 306 | #define IO_COMPL_BATCH 32 |
6ff119a6 | 307 | #define IO_REQ_CACHE_SIZE 32 |
bf019da7 | 308 | #define IO_REQ_ALLOC_BATCH 8 |
258b29a9 | 309 | |
a1ab7b35 PB |
310 | struct io_submit_link { |
311 | struct io_kiocb *head; | |
312 | struct io_kiocb *last; | |
313 | }; | |
314 | ||
258b29a9 | 315 | struct io_submit_state { |
5a158c6b PB |
316 | /* inline/task_work completion list, under ->uring_lock */ |
317 | struct io_wq_work_node free_list; | |
318 | /* batch completion logic */ | |
319 | struct io_wq_work_list compl_reqs; | |
a1ab7b35 | 320 | struct io_submit_link link; |
258b29a9 | 321 | |
258b29a9 | 322 | bool plug_started; |
4b628aeb | 323 | bool need_plug; |
3d4aeb9f | 324 | bool flush_cqes; |
5ca7a8b3 | 325 | unsigned short submit_nr; |
5a158c6b | 326 | struct blk_plug plug; |
258b29a9 PB |
327 | }; |
328 | ||
2b188cc1 | 329 | struct io_ring_ctx { |
b52ecf8c | 330 | /* const or read-mostly hot data */ |
2b188cc1 JA |
331 | struct { |
332 | struct percpu_ref refs; | |
2b188cc1 | 333 | |
b52ecf8c | 334 | struct io_rings *rings; |
2b188cc1 | 335 | unsigned int flags; |
e1d85334 | 336 | unsigned int compat: 1; |
e1d85334 RD |
337 | unsigned int drain_next: 1; |
338 | unsigned int eventfd_async: 1; | |
21b55dbc | 339 | unsigned int restricted: 1; |
f18ee4cf | 340 | unsigned int off_timeout_used: 1; |
10c66904 | 341 | unsigned int drain_active: 1; |
5562a8d7 | 342 | unsigned int drain_disabled: 1; |
b52ecf8c | 343 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 344 | |
7f1129d2 | 345 | /* submission data */ |
b52ecf8c | 346 | struct { |
0499e582 PB |
347 | struct mutex uring_lock; |
348 | ||
75b28aff HV |
349 | /* |
350 | * Ring buffer of indices into array of io_uring_sqe, which is | |
351 | * mmapped by the application using the IORING_OFF_SQES offset. | |
352 | * | |
353 | * This indirection could e.g. be used to assign fixed | |
354 | * io_uring_sqe entries to operations and only submit them to | |
355 | * the queue when needed. | |
356 | * | |
357 | * The kernel modifies neither the indices array nor the entries | |
358 | * array. | |
359 | */ | |
360 | u32 *sq_array; | |
c7af47cf | 361 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
362 | unsigned cached_sq_head; |
363 | unsigned sq_entries; | |
de0617e4 | 364 | struct list_head defer_list; |
7f1129d2 PB |
365 | |
366 | /* | |
367 | * Fixed resources fast path, should be accessed only under | |
368 | * uring_lock, and updated through io_uring_register(2) | |
369 | */ | |
370 | struct io_rsrc_node *rsrc_node; | |
ab409402 | 371 | int rsrc_cached_refs; |
7f1129d2 PB |
372 | struct io_file_table file_table; |
373 | unsigned nr_user_files; | |
374 | unsigned nr_user_bufs; | |
375 | struct io_mapped_ubuf **user_bufs; | |
376 | ||
377 | struct io_submit_state submit_state; | |
5262f567 | 378 | struct list_head timeout_list; |
ef9dd637 | 379 | struct list_head ltimeout_list; |
1d7bb1d5 | 380 | struct list_head cq_overflow_list; |
7f1129d2 PB |
381 | struct xarray io_buffers; |
382 | struct xarray personalities; | |
383 | u32 pers_next; | |
384 | unsigned sq_thread_idle; | |
2b188cc1 JA |
385 | } ____cacheline_aligned_in_smp; |
386 | ||
d0acdee2 | 387 | /* IRQ completion list, under ->completion_lock */ |
c2b6c6bc | 388 | struct io_wq_work_list locked_free_list; |
d0acdee2 | 389 | unsigned int locked_free_nr; |
3c1a2ead | 390 | |
7c30f36a | 391 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
534ca6d6 JA |
392 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
393 | ||
90554200 | 394 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 395 | struct list_head sqd_list; |
75b28aff | 396 | |
5ed7a37d PB |
397 | unsigned long check_cq_overflow; |
398 | ||
206aefde JA |
399 | struct { |
400 | unsigned cached_cq_tail; | |
401 | unsigned cq_entries; | |
0499e582 | 402 | struct eventfd_ctx *cq_ev_fd; |
0499e582 PB |
403 | struct wait_queue_head cq_wait; |
404 | unsigned cq_extra; | |
405 | atomic_t cq_timeouts; | |
0499e582 | 406 | unsigned cq_last_tm_flush; |
206aefde | 407 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 408 | |
2b188cc1 JA |
409 | struct { |
410 | spinlock_t completion_lock; | |
e94f141b | 411 | |
89850fce JA |
412 | spinlock_t timeout_lock; |
413 | ||
def596e9 | 414 | /* |
540e32a0 | 415 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
416 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
417 | * For SQPOLL, only the single threaded io_sq_thread() will | |
418 | * manipulate the list, hence no extra locking is needed there. | |
419 | */ | |
5eef4e87 | 420 | struct io_wq_work_list iopoll_list; |
78076bb6 JA |
421 | struct hlist_head *cancel_hash; |
422 | unsigned cancel_hash_bits; | |
915b3dde | 423 | bool poll_multi_queue; |
2b188cc1 | 424 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 425 | |
21b55dbc | 426 | struct io_restriction restrictions; |
3c1a2ead | 427 | |
b13a8918 PB |
428 | /* slow path rsrc auxilary data, used by update/register */ |
429 | struct { | |
430 | struct io_rsrc_node *rsrc_backup_node; | |
431 | struct io_mapped_ubuf *dummy_ubuf; | |
432 | struct io_rsrc_data *file_data; | |
433 | struct io_rsrc_data *buf_data; | |
434 | ||
435 | struct delayed_work rsrc_put_work; | |
436 | struct llist_head rsrc_put_llist; | |
437 | struct list_head rsrc_ref_list; | |
438 | spinlock_t rsrc_ref_lock; | |
439 | }; | |
440 | ||
3c1a2ead | 441 | /* Keep this last, we don't need it for the fast path */ |
b986af7e PB |
442 | struct { |
443 | #if defined(CONFIG_UNIX) | |
444 | struct socket *ring_sock; | |
445 | #endif | |
446 | /* hashed buffered write serialization */ | |
447 | struct io_wq_hash *hash_map; | |
448 | ||
449 | /* Only used for accounting purposes */ | |
450 | struct user_struct *user; | |
451 | struct mm_struct *mm_account; | |
452 | ||
453 | /* ctx exit and cancelation */ | |
9011bf9a PB |
454 | struct llist_head fallback_llist; |
455 | struct delayed_work fallback_work; | |
b986af7e PB |
456 | struct work_struct exit_work; |
457 | struct list_head tctx_list; | |
458 | struct completion ref_comp; | |
e139a1ec PB |
459 | u32 iowq_limits[2]; |
460 | bool iowq_limits_set; | |
b986af7e | 461 | }; |
2b188cc1 JA |
462 | }; |
463 | ||
53e043b2 SM |
464 | struct io_uring_task { |
465 | /* submission side */ | |
09899b19 | 466 | int cached_refs; |
53e043b2 SM |
467 | struct xarray xa; |
468 | struct wait_queue_head wait; | |
ee53fb2b SM |
469 | const struct io_ring_ctx *last; |
470 | struct io_wq *io_wq; | |
53e043b2 | 471 | struct percpu_counter inflight; |
b303fe2e | 472 | atomic_t inflight_tracked; |
53e043b2 | 473 | atomic_t in_idle; |
53e043b2 SM |
474 | |
475 | spinlock_t task_lock; | |
476 | struct io_wq_work_list task_list; | |
53e043b2 | 477 | struct callback_head task_work; |
6294f368 | 478 | bool task_running; |
53e043b2 SM |
479 | }; |
480 | ||
09bb8394 JA |
481 | /* |
482 | * First field must be the file pointer in all the | |
483 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
484 | */ | |
221c5eb2 JA |
485 | struct io_poll_iocb { |
486 | struct file *file; | |
018043be | 487 | struct wait_queue_head *head; |
221c5eb2 | 488 | __poll_t events; |
8c838788 | 489 | bool done; |
221c5eb2 | 490 | bool canceled; |
392edb45 | 491 | struct wait_queue_entry wait; |
221c5eb2 JA |
492 | }; |
493 | ||
9d805892 | 494 | struct io_poll_update { |
018043be | 495 | struct file *file; |
9d805892 PB |
496 | u64 old_user_data; |
497 | u64 new_user_data; | |
498 | __poll_t events; | |
b69de288 JA |
499 | bool update_events; |
500 | bool update_user_data; | |
018043be PB |
501 | }; |
502 | ||
b5dba59e JA |
503 | struct io_close { |
504 | struct file *file; | |
b5dba59e | 505 | int fd; |
7df778be | 506 | u32 file_slot; |
b5dba59e JA |
507 | }; |
508 | ||
ad8a48ac JA |
509 | struct io_timeout_data { |
510 | struct io_kiocb *req; | |
511 | struct hrtimer timer; | |
512 | struct timespec64 ts; | |
513 | enum hrtimer_mode mode; | |
50c1df2b | 514 | u32 flags; |
ad8a48ac JA |
515 | }; |
516 | ||
8ed8d3c3 JA |
517 | struct io_accept { |
518 | struct file *file; | |
519 | struct sockaddr __user *addr; | |
520 | int __user *addr_len; | |
521 | int flags; | |
aaa4db12 | 522 | u32 file_slot; |
09952e3e | 523 | unsigned long nofile; |
8ed8d3c3 JA |
524 | }; |
525 | ||
526 | struct io_sync { | |
527 | struct file *file; | |
528 | loff_t len; | |
529 | loff_t off; | |
530 | int flags; | |
d63d1b5e | 531 | int mode; |
8ed8d3c3 JA |
532 | }; |
533 | ||
fbf23849 JA |
534 | struct io_cancel { |
535 | struct file *file; | |
536 | u64 addr; | |
537 | }; | |
538 | ||
b29472ee JA |
539 | struct io_timeout { |
540 | struct file *file; | |
bfe68a22 PB |
541 | u32 off; |
542 | u32 target_seq; | |
135fcde8 | 543 | struct list_head list; |
90cd7e42 PB |
544 | /* head of the link, used by linked timeouts only */ |
545 | struct io_kiocb *head; | |
89b263f6 JA |
546 | /* for linked completions */ |
547 | struct io_kiocb *prev; | |
b29472ee JA |
548 | }; |
549 | ||
0bdf7a2d PB |
550 | struct io_timeout_rem { |
551 | struct file *file; | |
552 | u64 addr; | |
9c8e11b3 PB |
553 | |
554 | /* timeout update */ | |
555 | struct timespec64 ts; | |
556 | u32 flags; | |
f1042b6c | 557 | bool ltimeout; |
0bdf7a2d PB |
558 | }; |
559 | ||
9adbd45d JA |
560 | struct io_rw { |
561 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
562 | struct kiocb kiocb; | |
563 | u64 addr; | |
564 | u64 len; | |
565 | }; | |
566 | ||
3fbb51c1 JA |
567 | struct io_connect { |
568 | struct file *file; | |
569 | struct sockaddr __user *addr; | |
570 | int addr_len; | |
571 | }; | |
572 | ||
e47293fd JA |
573 | struct io_sr_msg { |
574 | struct file *file; | |
fddaface | 575 | union { |
4af3417a PB |
576 | struct compat_msghdr __user *umsg_compat; |
577 | struct user_msghdr __user *umsg; | |
578 | void __user *buf; | |
fddaface | 579 | }; |
e47293fd | 580 | int msg_flags; |
bcda7baa | 581 | int bgid; |
fddaface | 582 | size_t len; |
e47293fd JA |
583 | }; |
584 | ||
15b71abe JA |
585 | struct io_open { |
586 | struct file *file; | |
587 | int dfd; | |
b9445598 | 588 | u32 file_slot; |
15b71abe | 589 | struct filename *filename; |
c12cedf2 | 590 | struct open_how how; |
4022e7af | 591 | unsigned long nofile; |
15b71abe JA |
592 | }; |
593 | ||
269bbe5f | 594 | struct io_rsrc_update { |
05f3fb3c JA |
595 | struct file *file; |
596 | u64 arg; | |
597 | u32 nr_args; | |
598 | u32 offset; | |
599 | }; | |
600 | ||
4840e418 JA |
601 | struct io_fadvise { |
602 | struct file *file; | |
603 | u64 offset; | |
604 | u32 len; | |
605 | u32 advice; | |
606 | }; | |
607 | ||
c1ca757b JA |
608 | struct io_madvise { |
609 | struct file *file; | |
610 | u64 addr; | |
611 | u32 len; | |
612 | u32 advice; | |
613 | }; | |
614 | ||
3e4827b0 JA |
615 | struct io_epoll { |
616 | struct file *file; | |
617 | int epfd; | |
618 | int op; | |
619 | int fd; | |
620 | struct epoll_event event; | |
e47293fd JA |
621 | }; |
622 | ||
7d67af2c PB |
623 | struct io_splice { |
624 | struct file *file_out; | |
625 | struct file *file_in; | |
626 | loff_t off_out; | |
627 | loff_t off_in; | |
628 | u64 len; | |
629 | unsigned int flags; | |
630 | }; | |
631 | ||
ddf0322d JA |
632 | struct io_provide_buf { |
633 | struct file *file; | |
634 | __u64 addr; | |
38134ada | 635 | __u32 len; |
ddf0322d JA |
636 | __u32 bgid; |
637 | __u16 nbufs; | |
638 | __u16 bid; | |
639 | }; | |
640 | ||
1d9e1288 BM |
641 | struct io_statx { |
642 | struct file *file; | |
643 | int dfd; | |
644 | unsigned int mask; | |
645 | unsigned int flags; | |
e62753e4 | 646 | const char __user *filename; |
1d9e1288 BM |
647 | struct statx __user *buffer; |
648 | }; | |
649 | ||
36f4fa68 JA |
650 | struct io_shutdown { |
651 | struct file *file; | |
652 | int how; | |
653 | }; | |
654 | ||
80a261fd JA |
655 | struct io_rename { |
656 | struct file *file; | |
657 | int old_dfd; | |
658 | int new_dfd; | |
659 | struct filename *oldpath; | |
660 | struct filename *newpath; | |
661 | int flags; | |
662 | }; | |
663 | ||
14a1143b JA |
664 | struct io_unlink { |
665 | struct file *file; | |
666 | int dfd; | |
667 | int flags; | |
668 | struct filename *filename; | |
669 | }; | |
670 | ||
e34a02dc DK |
671 | struct io_mkdir { |
672 | struct file *file; | |
673 | int dfd; | |
674 | umode_t mode; | |
675 | struct filename *filename; | |
676 | }; | |
677 | ||
7a8721f8 DK |
678 | struct io_symlink { |
679 | struct file *file; | |
680 | int new_dfd; | |
681 | struct filename *oldpath; | |
682 | struct filename *newpath; | |
683 | }; | |
684 | ||
cf30da90 DK |
685 | struct io_hardlink { |
686 | struct file *file; | |
687 | int old_dfd; | |
688 | int new_dfd; | |
689 | struct filename *oldpath; | |
690 | struct filename *newpath; | |
691 | int flags; | |
692 | }; | |
693 | ||
f499a021 JA |
694 | struct io_async_connect { |
695 | struct sockaddr_storage address; | |
696 | }; | |
697 | ||
03b1230c JA |
698 | struct io_async_msghdr { |
699 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
700 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
701 | struct iovec *free_iov; | |
03b1230c JA |
702 | struct sockaddr __user *uaddr; |
703 | struct msghdr msg; | |
b537916c | 704 | struct sockaddr_storage addr; |
03b1230c JA |
705 | }; |
706 | ||
538941e2 | 707 | struct io_rw_state { |
ff6165b2 | 708 | struct iov_iter iter; |
cd658695 | 709 | struct iov_iter_state iter_state; |
c88598a9 | 710 | struct iovec fast_iov[UIO_FASTIOV]; |
538941e2 PB |
711 | }; |
712 | ||
713 | struct io_async_rw { | |
714 | struct io_rw_state s; | |
715 | const struct iovec *free_iovec; | |
227c0c96 | 716 | size_t bytes_done; |
bcf5a063 | 717 | struct wait_page_queue wpq; |
f67676d1 JA |
718 | }; |
719 | ||
6b47ee6e PB |
720 | enum { |
721 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
722 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
723 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
724 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
725 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 726 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
04c76b41 | 727 | REQ_F_CQE_SKIP_BIT = IOSQE_CQE_SKIP_SUCCESS_BIT, |
6b47ee6e | 728 | |
dddca226 | 729 | /* first byte is taken by user flags, shift it to not overlap */ |
93d2bcd2 | 730 | REQ_F_FAIL_BIT = 8, |
6b47ee6e PB |
731 | REQ_F_INFLIGHT_BIT, |
732 | REQ_F_CUR_POS_BIT, | |
733 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 734 | REQ_F_LINK_TIMEOUT_BIT, |
99bc4c38 | 735 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 736 | REQ_F_POLLED_BIT, |
bcda7baa | 737 | REQ_F_BUFFER_SELECTED_BIT, |
e342c807 | 738 | REQ_F_COMPLETE_INLINE_BIT, |
230d50d4 | 739 | REQ_F_REISSUE_BIT, |
b8e64b53 | 740 | REQ_F_CREDS_BIT, |
20e60a38 | 741 | REQ_F_REFCOUNT_BIT, |
4d13d1a4 | 742 | REQ_F_ARM_LTIMEOUT_BIT, |
d886e185 | 743 | REQ_F_ASYNC_DATA_BIT, |
04c76b41 | 744 | REQ_F_SKIP_LINK_CQES_BIT, |
7b29f92d | 745 | /* keep async read/write and isreg together and in order */ |
35645ac3 | 746 | REQ_F_SUPPORT_NOWAIT_BIT, |
7b29f92d | 747 | REQ_F_ISREG_BIT, |
84557871 JA |
748 | |
749 | /* not a real bit, just to check we're not overflowing the space */ | |
750 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
751 | }; |
752 | ||
753 | enum { | |
754 | /* ctx owns file */ | |
755 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
756 | /* drain existing IO first */ | |
757 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
758 | /* linked sqes */ | |
759 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
760 | /* doesn't sever on completion < 0 */ | |
761 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
762 | /* IOSQE_ASYNC */ | |
763 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
764 | /* IOSQE_BUFFER_SELECT */ |
765 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
04c76b41 PB |
766 | /* IOSQE_CQE_SKIP_SUCCESS */ |
767 | REQ_F_CQE_SKIP = BIT(REQ_F_CQE_SKIP_BIT), | |
6b47ee6e | 768 | |
6b47ee6e | 769 | /* fail rest of links */ |
93d2bcd2 | 770 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
b05a1bcd | 771 | /* on inflight list, should be cancelled and waited on exit reliably */ |
6b47ee6e PB |
772 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
773 | /* read/write uses file position */ | |
774 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
775 | /* must not punt to workers */ | |
776 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 777 | /* has or had linked timeout */ |
6b47ee6e | 778 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
99bc4c38 PB |
779 | /* needs cleanup */ |
780 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
781 | /* already went through poll handler */ |
782 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
783 | /* buffer already selected */ |
784 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
e342c807 PB |
785 | /* completion is deferred through io_comp_state */ |
786 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
230d50d4 JA |
787 | /* caller should reissue async */ |
788 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), | |
35645ac3 PB |
789 | /* supports async reads/writes */ |
790 | REQ_F_SUPPORT_NOWAIT = BIT(REQ_F_SUPPORT_NOWAIT_BIT), | |
7b29f92d JA |
791 | /* regular file */ |
792 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
b8e64b53 PB |
793 | /* has creds assigned */ |
794 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), | |
20e60a38 PB |
795 | /* skip refcounting if not set */ |
796 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), | |
4d13d1a4 PB |
797 | /* there is a linked timeout that has to be armed */ |
798 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT), | |
d886e185 PB |
799 | /* ->async_data allocated */ |
800 | REQ_F_ASYNC_DATA = BIT(REQ_F_ASYNC_DATA_BIT), | |
04c76b41 PB |
801 | /* don't post CQEs while failing linked requests */ |
802 | REQ_F_SKIP_LINK_CQES = BIT(REQ_F_SKIP_LINK_CQES_BIT), | |
d7718a9d JA |
803 | }; |
804 | ||
805 | struct async_poll { | |
806 | struct io_poll_iocb poll; | |
807abcb0 | 807 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
808 | }; |
809 | ||
f237c30a | 810 | typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked); |
5b0a6acc | 811 | |
7cbf1722 | 812 | struct io_task_work { |
5b0a6acc PB |
813 | union { |
814 | struct io_wq_work_node node; | |
815 | struct llist_node fallback_node; | |
816 | }; | |
817 | io_req_tw_func_t func; | |
7cbf1722 JA |
818 | }; |
819 | ||
992da01a PB |
820 | enum { |
821 | IORING_RSRC_FILE = 0, | |
822 | IORING_RSRC_BUFFER = 1, | |
823 | }; | |
824 | ||
09bb8394 JA |
825 | /* |
826 | * NOTE! Each of the iocb union members has the file pointer | |
827 | * as the first entry in their struct definition. So you can | |
828 | * access the file pointer through any of the sub-structs, | |
829 | * or directly as just 'ki_filp' in this struct. | |
830 | */ | |
2b188cc1 | 831 | struct io_kiocb { |
221c5eb2 | 832 | union { |
09bb8394 | 833 | struct file *file; |
9adbd45d | 834 | struct io_rw rw; |
221c5eb2 | 835 | struct io_poll_iocb poll; |
9d805892 | 836 | struct io_poll_update poll_update; |
8ed8d3c3 JA |
837 | struct io_accept accept; |
838 | struct io_sync sync; | |
fbf23849 | 839 | struct io_cancel cancel; |
b29472ee | 840 | struct io_timeout timeout; |
0bdf7a2d | 841 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 842 | struct io_connect connect; |
e47293fd | 843 | struct io_sr_msg sr_msg; |
15b71abe | 844 | struct io_open open; |
b5dba59e | 845 | struct io_close close; |
269bbe5f | 846 | struct io_rsrc_update rsrc_update; |
4840e418 | 847 | struct io_fadvise fadvise; |
c1ca757b | 848 | struct io_madvise madvise; |
3e4827b0 | 849 | struct io_epoll epoll; |
7d67af2c | 850 | struct io_splice splice; |
ddf0322d | 851 | struct io_provide_buf pbuf; |
1d9e1288 | 852 | struct io_statx statx; |
36f4fa68 | 853 | struct io_shutdown shutdown; |
80a261fd | 854 | struct io_rename rename; |
14a1143b | 855 | struct io_unlink unlink; |
e34a02dc | 856 | struct io_mkdir mkdir; |
7a8721f8 | 857 | struct io_symlink symlink; |
cf30da90 | 858 | struct io_hardlink hardlink; |
221c5eb2 | 859 | }; |
2b188cc1 | 860 | |
d625c6ee | 861 | u8 opcode; |
65a6543d XW |
862 | /* polled IO has completed */ |
863 | u8 iopoll_completed; | |
4f4eeba8 | 864 | u16 buf_index; |
d17e56eb PB |
865 | unsigned int flags; |
866 | ||
867 | u64 user_data; | |
9cf7c104 | 868 | u32 result; |
d17e56eb | 869 | u32 cflags; |
4f4eeba8 | 870 | |
010e8e6b | 871 | struct io_ring_ctx *ctx; |
010e8e6b | 872 | struct task_struct *task; |
d7718a9d | 873 | |
269bbe5f | 874 | struct percpu_ref *fixed_rsrc_refs; |
d886e185 PB |
875 | /* store used ubuf, so we can prevent reloading */ |
876 | struct io_mapped_ubuf *imu; | |
fcb323cc | 877 | |
7e3709d5 | 878 | /* used by request caches, completion batching and iopoll */ |
ef05d9eb | 879 | struct io_wq_work_node comp_list; |
d17e56eb | 880 | atomic_t refs; |
7e3709d5 | 881 | struct io_kiocb *link; |
5b0a6acc | 882 | struct io_task_work io_task_work; |
010e8e6b PB |
883 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
884 | struct hlist_node hash_node; | |
7e3709d5 | 885 | /* internal polling, see IORING_FEAT_FAST_POLL */ |
010e8e6b | 886 | struct async_poll *apoll; |
d886e185 PB |
887 | /* opcode allocated if it needs to store data for async defer */ |
888 | void *async_data; | |
010e8e6b | 889 | struct io_wq_work work; |
7e3709d5 | 890 | /* custom credentials, valid IFF REQ_F_CREDS is set */ |
fe7e3257 | 891 | const struct cred *creds; |
7e3709d5 | 892 | /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */ |
30d51dd4 | 893 | struct io_buffer *kbuf; |
2b188cc1 | 894 | }; |
05589553 | 895 | |
13bf43f5 PB |
896 | struct io_tctx_node { |
897 | struct list_head ctx_node; | |
898 | struct task_struct *task; | |
13bf43f5 PB |
899 | struct io_ring_ctx *ctx; |
900 | }; | |
901 | ||
27dc8338 PB |
902 | struct io_defer_entry { |
903 | struct list_head list; | |
904 | struct io_kiocb *req; | |
9cf7c104 | 905 | u32 seq; |
2b188cc1 JA |
906 | }; |
907 | ||
d3656344 | 908 | struct io_op_def { |
d3656344 JA |
909 | /* needs req->file assigned */ |
910 | unsigned needs_file : 1; | |
6d63416d PB |
911 | /* should block plug */ |
912 | unsigned plug : 1; | |
d3656344 JA |
913 | /* hash wq insertion if file is a regular file */ |
914 | unsigned hash_reg_file : 1; | |
915 | /* unbound wq insertion if file is a non-regular file */ | |
916 | unsigned unbound_nonreg_file : 1; | |
8a72758c JA |
917 | /* set if opcode supports polled "wait" */ |
918 | unsigned pollin : 1; | |
919 | unsigned pollout : 1; | |
bcda7baa JA |
920 | /* op supports buffer selection */ |
921 | unsigned buffer_select : 1; | |
26f0505a PB |
922 | /* do prep async if is going to be punted */ |
923 | unsigned needs_async_setup : 1; | |
6d63416d PB |
924 | /* opcode is not supported by this kernel */ |
925 | unsigned not_supported : 1; | |
5bd2182d PM |
926 | /* skip auditing */ |
927 | unsigned audit_skip : 1; | |
e8c2bc1f JA |
928 | /* size of async data needed, if any */ |
929 | unsigned short async_size; | |
d3656344 JA |
930 | }; |
931 | ||
0918682b | 932 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
933 | [IORING_OP_NOP] = {}, |
934 | [IORING_OP_READV] = { | |
d3656344 JA |
935 | .needs_file = 1, |
936 | .unbound_nonreg_file = 1, | |
8a72758c | 937 | .pollin = 1, |
4d954c25 | 938 | .buffer_select = 1, |
26f0505a | 939 | .needs_async_setup = 1, |
27926b68 | 940 | .plug = 1, |
5bd2182d | 941 | .audit_skip = 1, |
e8c2bc1f | 942 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 943 | }, |
0463b6c5 | 944 | [IORING_OP_WRITEV] = { |
d3656344 JA |
945 | .needs_file = 1, |
946 | .hash_reg_file = 1, | |
947 | .unbound_nonreg_file = 1, | |
8a72758c | 948 | .pollout = 1, |
26f0505a | 949 | .needs_async_setup = 1, |
27926b68 | 950 | .plug = 1, |
5bd2182d | 951 | .audit_skip = 1, |
e8c2bc1f | 952 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 953 | }, |
0463b6c5 | 954 | [IORING_OP_FSYNC] = { |
d3656344 | 955 | .needs_file = 1, |
5bd2182d | 956 | .audit_skip = 1, |
d3656344 | 957 | }, |
0463b6c5 | 958 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
959 | .needs_file = 1, |
960 | .unbound_nonreg_file = 1, | |
8a72758c | 961 | .pollin = 1, |
27926b68 | 962 | .plug = 1, |
5bd2182d | 963 | .audit_skip = 1, |
e8c2bc1f | 964 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 965 | }, |
0463b6c5 | 966 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
967 | .needs_file = 1, |
968 | .hash_reg_file = 1, | |
969 | .unbound_nonreg_file = 1, | |
8a72758c | 970 | .pollout = 1, |
27926b68 | 971 | .plug = 1, |
5bd2182d | 972 | .audit_skip = 1, |
e8c2bc1f | 973 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 974 | }, |
0463b6c5 | 975 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
976 | .needs_file = 1, |
977 | .unbound_nonreg_file = 1, | |
5bd2182d PM |
978 | .audit_skip = 1, |
979 | }, | |
980 | [IORING_OP_POLL_REMOVE] = { | |
981 | .audit_skip = 1, | |
d3656344 | 982 | }, |
0463b6c5 | 983 | [IORING_OP_SYNC_FILE_RANGE] = { |
d3656344 | 984 | .needs_file = 1, |
5bd2182d | 985 | .audit_skip = 1, |
d3656344 | 986 | }, |
0463b6c5 | 987 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
988 | .needs_file = 1, |
989 | .unbound_nonreg_file = 1, | |
8a72758c | 990 | .pollout = 1, |
26f0505a | 991 | .needs_async_setup = 1, |
e8c2bc1f | 992 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 993 | }, |
0463b6c5 | 994 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
995 | .needs_file = 1, |
996 | .unbound_nonreg_file = 1, | |
8a72758c | 997 | .pollin = 1, |
52de1fe1 | 998 | .buffer_select = 1, |
26f0505a | 999 | .needs_async_setup = 1, |
e8c2bc1f | 1000 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 1001 | }, |
0463b6c5 | 1002 | [IORING_OP_TIMEOUT] = { |
5bd2182d | 1003 | .audit_skip = 1, |
e8c2bc1f | 1004 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 1005 | }, |
9c8e11b3 PB |
1006 | [IORING_OP_TIMEOUT_REMOVE] = { |
1007 | /* used by timeout updates' prep() */ | |
5bd2182d | 1008 | .audit_skip = 1, |
9c8e11b3 | 1009 | }, |
0463b6c5 | 1010 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
1011 | .needs_file = 1, |
1012 | .unbound_nonreg_file = 1, | |
8a72758c | 1013 | .pollin = 1, |
d3656344 | 1014 | }, |
5bd2182d PM |
1015 | [IORING_OP_ASYNC_CANCEL] = { |
1016 | .audit_skip = 1, | |
1017 | }, | |
0463b6c5 | 1018 | [IORING_OP_LINK_TIMEOUT] = { |
5bd2182d | 1019 | .audit_skip = 1, |
e8c2bc1f | 1020 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 1021 | }, |
0463b6c5 | 1022 | [IORING_OP_CONNECT] = { |
d3656344 JA |
1023 | .needs_file = 1, |
1024 | .unbound_nonreg_file = 1, | |
8a72758c | 1025 | .pollout = 1, |
26f0505a | 1026 | .needs_async_setup = 1, |
e8c2bc1f | 1027 | .async_size = sizeof(struct io_async_connect), |
d3656344 | 1028 | }, |
0463b6c5 | 1029 | [IORING_OP_FALLOCATE] = { |
d3656344 | 1030 | .needs_file = 1, |
d3656344 | 1031 | }, |
44526bed JA |
1032 | [IORING_OP_OPENAT] = {}, |
1033 | [IORING_OP_CLOSE] = {}, | |
5bd2182d PM |
1034 | [IORING_OP_FILES_UPDATE] = { |
1035 | .audit_skip = 1, | |
1036 | }, | |
1037 | [IORING_OP_STATX] = { | |
1038 | .audit_skip = 1, | |
1039 | }, | |
0463b6c5 | 1040 | [IORING_OP_READ] = { |
3a6820f2 JA |
1041 | .needs_file = 1, |
1042 | .unbound_nonreg_file = 1, | |
8a72758c | 1043 | .pollin = 1, |
bcda7baa | 1044 | .buffer_select = 1, |
27926b68 | 1045 | .plug = 1, |
5bd2182d | 1046 | .audit_skip = 1, |
e8c2bc1f | 1047 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1048 | }, |
0463b6c5 | 1049 | [IORING_OP_WRITE] = { |
3a6820f2 | 1050 | .needs_file = 1, |
7b3188e7 | 1051 | .hash_reg_file = 1, |
3a6820f2 | 1052 | .unbound_nonreg_file = 1, |
8a72758c | 1053 | .pollout = 1, |
27926b68 | 1054 | .plug = 1, |
5bd2182d | 1055 | .audit_skip = 1, |
e8c2bc1f | 1056 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1057 | }, |
0463b6c5 | 1058 | [IORING_OP_FADVISE] = { |
4840e418 | 1059 | .needs_file = 1, |
5bd2182d | 1060 | .audit_skip = 1, |
c1ca757b | 1061 | }, |
44526bed | 1062 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 1063 | [IORING_OP_SEND] = { |
fddaface JA |
1064 | .needs_file = 1, |
1065 | .unbound_nonreg_file = 1, | |
8a72758c | 1066 | .pollout = 1, |
5bd2182d | 1067 | .audit_skip = 1, |
fddaface | 1068 | }, |
0463b6c5 | 1069 | [IORING_OP_RECV] = { |
fddaface JA |
1070 | .needs_file = 1, |
1071 | .unbound_nonreg_file = 1, | |
8a72758c | 1072 | .pollin = 1, |
bcda7baa | 1073 | .buffer_select = 1, |
5bd2182d | 1074 | .audit_skip = 1, |
fddaface | 1075 | }, |
0463b6c5 | 1076 | [IORING_OP_OPENAT2] = { |
cebdb986 | 1077 | }, |
3e4827b0 JA |
1078 | [IORING_OP_EPOLL_CTL] = { |
1079 | .unbound_nonreg_file = 1, | |
5bd2182d | 1080 | .audit_skip = 1, |
3e4827b0 | 1081 | }, |
7d67af2c PB |
1082 | [IORING_OP_SPLICE] = { |
1083 | .needs_file = 1, | |
1084 | .hash_reg_file = 1, | |
1085 | .unbound_nonreg_file = 1, | |
5bd2182d PM |
1086 | .audit_skip = 1, |
1087 | }, | |
1088 | [IORING_OP_PROVIDE_BUFFERS] = { | |
1089 | .audit_skip = 1, | |
1090 | }, | |
1091 | [IORING_OP_REMOVE_BUFFERS] = { | |
1092 | .audit_skip = 1, | |
ddf0322d | 1093 | }, |
f2a8d5c7 PB |
1094 | [IORING_OP_TEE] = { |
1095 | .needs_file = 1, | |
1096 | .hash_reg_file = 1, | |
1097 | .unbound_nonreg_file = 1, | |
5bd2182d | 1098 | .audit_skip = 1, |
f2a8d5c7 | 1099 | }, |
36f4fa68 JA |
1100 | [IORING_OP_SHUTDOWN] = { |
1101 | .needs_file = 1, | |
1102 | }, | |
44526bed JA |
1103 | [IORING_OP_RENAMEAT] = {}, |
1104 | [IORING_OP_UNLINKAT] = {}, | |
e34a02dc | 1105 | [IORING_OP_MKDIRAT] = {}, |
7a8721f8 | 1106 | [IORING_OP_SYMLINKAT] = {}, |
cf30da90 | 1107 | [IORING_OP_LINKAT] = {}, |
d3656344 JA |
1108 | }; |
1109 | ||
0756a869 PB |
1110 | /* requests with any of those set should undergo io_disarm_next() */ |
1111 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) | |
1112 | ||
7a612350 | 1113 | static bool io_disarm_next(struct io_kiocb *req); |
eef51daa | 1114 | static void io_uring_del_tctx_node(unsigned long index); |
9936c7c2 PB |
1115 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1116 | struct task_struct *task, | |
3dd0c97a | 1117 | bool cancel_all); |
78cc687b | 1118 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); |
1ffc5422 | 1119 | |
913a571a PB |
1120 | static void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags); |
1121 | ||
ec9c02ad | 1122 | static void io_put_req(struct io_kiocb *req); |
91c2f697 | 1123 | static void io_put_req_deferred(struct io_kiocb *req); |
c7dae4ba | 1124 | static void io_dismantle_req(struct io_kiocb *req); |
94ae5e77 | 1125 | static void io_queue_linked_timeout(struct io_kiocb *req); |
fdecb662 | 1126 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 1127 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 | 1128 | unsigned nr_args); |
68fb8979 | 1129 | static void io_clean_op(struct io_kiocb *req); |
ac177053 | 1130 | static struct file *io_file_get(struct io_ring_ctx *ctx, |
8371adf5 | 1131 | struct io_kiocb *req, int fd, bool fixed); |
c5eef2b9 | 1132 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1133 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1134 | |
907d1df3 | 1135 | static void io_req_task_queue(struct io_kiocb *req); |
c450178d | 1136 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx); |
179ae0d1 | 1137 | static int io_req_prep_async(struct io_kiocb *req); |
de0617e4 | 1138 | |
b9445598 PB |
1139 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
1140 | unsigned int issue_flags, u32 slot_index); | |
7df778be PB |
1141 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags); |
1142 | ||
f1042b6c | 1143 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer); |
b9445598 | 1144 | |
2b188cc1 JA |
1145 | static struct kmem_cache *req_cachep; |
1146 | ||
0918682b | 1147 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1148 | |
1149 | struct sock *io_uring_get_socket(struct file *file) | |
1150 | { | |
1151 | #if defined(CONFIG_UNIX) | |
1152 | if (file->f_op == &io_uring_fops) { | |
1153 | struct io_ring_ctx *ctx = file->private_data; | |
1154 | ||
1155 | return ctx->ring_sock->sk; | |
1156 | } | |
1157 | #endif | |
1158 | return NULL; | |
1159 | } | |
1160 | EXPORT_SYMBOL(io_uring_get_socket); | |
1161 | ||
f237c30a PB |
1162 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
1163 | { | |
1164 | if (!*locked) { | |
1165 | mutex_lock(&ctx->uring_lock); | |
1166 | *locked = true; | |
1167 | } | |
1168 | } | |
1169 | ||
f2f87370 PB |
1170 | #define io_for_each_link(pos, head) \ |
1171 | for (pos = (head); pos; pos = pos->link) | |
1172 | ||
21c843d5 PB |
1173 | /* |
1174 | * Shamelessly stolen from the mm implementation of page reference checking, | |
1175 | * see commit f958d7b528b1 for details. | |
1176 | */ | |
1177 | #define req_ref_zero_or_close_to_overflow(req) \ | |
1178 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) | |
1179 | ||
1180 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) | |
1181 | { | |
20e60a38 | 1182 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1183 | return atomic_inc_not_zero(&req->refs); |
1184 | } | |
1185 | ||
21c843d5 PB |
1186 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
1187 | { | |
20e60a38 PB |
1188 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
1189 | return true; | |
1190 | ||
21c843d5 PB |
1191 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1192 | return atomic_dec_and_test(&req->refs); | |
1193 | } | |
1194 | ||
1195 | static inline void req_ref_put(struct io_kiocb *req) | |
1196 | { | |
20e60a38 | 1197 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1198 | WARN_ON_ONCE(req_ref_put_and_test(req)); |
1199 | } | |
1200 | ||
1201 | static inline void req_ref_get(struct io_kiocb *req) | |
1202 | { | |
20e60a38 | 1203 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1204 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1205 | atomic_inc(&req->refs); | |
1206 | } | |
1207 | ||
c450178d PB |
1208 | static inline void io_submit_flush_completions(struct io_ring_ctx *ctx) |
1209 | { | |
6f33b0bc | 1210 | if (!wq_list_empty(&ctx->submit_state.compl_reqs)) |
c450178d PB |
1211 | __io_submit_flush_completions(ctx); |
1212 | } | |
1213 | ||
48dcd38d | 1214 | static inline void __io_req_set_refcount(struct io_kiocb *req, int nr) |
20e60a38 PB |
1215 | { |
1216 | if (!(req->flags & REQ_F_REFCOUNT)) { | |
1217 | req->flags |= REQ_F_REFCOUNT; | |
48dcd38d | 1218 | atomic_set(&req->refs, nr); |
20e60a38 PB |
1219 | } |
1220 | } | |
1221 | ||
48dcd38d PB |
1222 | static inline void io_req_set_refcount(struct io_kiocb *req) |
1223 | { | |
1224 | __io_req_set_refcount(req, 1); | |
1225 | } | |
1226 | ||
ab409402 PB |
1227 | #define IO_RSRC_REF_BATCH 100 |
1228 | ||
1229 | static inline void io_req_put_rsrc_locked(struct io_kiocb *req, | |
1230 | struct io_ring_ctx *ctx) | |
1231 | __must_hold(&ctx->uring_lock) | |
36f72fe2 | 1232 | { |
ab409402 PB |
1233 | struct percpu_ref *ref = req->fixed_rsrc_refs; |
1234 | ||
1235 | if (ref) { | |
1236 | if (ref == &ctx->rsrc_node->refs) | |
1237 | ctx->rsrc_cached_refs++; | |
1238 | else | |
1239 | percpu_ref_put(ref); | |
1240 | } | |
1241 | } | |
1242 | ||
1243 | static inline void io_req_put_rsrc(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
1244 | { | |
1245 | if (req->fixed_rsrc_refs) | |
1246 | percpu_ref_put(req->fixed_rsrc_refs); | |
1247 | } | |
1248 | ||
1249 | static __cold void io_rsrc_refs_drop(struct io_ring_ctx *ctx) | |
1250 | __must_hold(&ctx->uring_lock) | |
1251 | { | |
1252 | if (ctx->rsrc_cached_refs) { | |
1253 | percpu_ref_put_many(&ctx->rsrc_node->refs, ctx->rsrc_cached_refs); | |
1254 | ctx->rsrc_cached_refs = 0; | |
1255 | } | |
1256 | } | |
1257 | ||
1258 | static void io_rsrc_refs_refill(struct io_ring_ctx *ctx) | |
1259 | __must_hold(&ctx->uring_lock) | |
1260 | { | |
1261 | ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH; | |
1262 | percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH); | |
1263 | } | |
36f72fe2 | 1264 | |
a46be971 PB |
1265 | static inline void io_req_set_rsrc_node(struct io_kiocb *req, |
1266 | struct io_ring_ctx *ctx) | |
36f72fe2 | 1267 | { |
269bbe5f | 1268 | if (!req->fixed_rsrc_refs) { |
a7f0ed5a | 1269 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
ab409402 PB |
1270 | ctx->rsrc_cached_refs--; |
1271 | if (unlikely(ctx->rsrc_cached_refs < 0)) | |
1272 | io_rsrc_refs_refill(ctx); | |
36f72fe2 PB |
1273 | } |
1274 | } | |
1275 | ||
f70865db PB |
1276 | static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
1277 | { | |
1278 | bool got = percpu_ref_tryget(ref); | |
1279 | ||
1280 | /* already at zero, wait for ->release() */ | |
1281 | if (!got) | |
1282 | wait_for_completion(compl); | |
1283 | percpu_ref_resurrect(ref); | |
1284 | if (got) | |
1285 | percpu_ref_put(ref); | |
1286 | } | |
1287 | ||
3dd0c97a PB |
1288 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
1289 | bool cancel_all) | |
08d23634 PB |
1290 | { |
1291 | struct io_kiocb *req; | |
1292 | ||
68207680 | 1293 | if (task && head->task != task) |
08d23634 | 1294 | return false; |
3dd0c97a | 1295 | if (cancel_all) |
08d23634 PB |
1296 | return true; |
1297 | ||
1298 | io_for_each_link(req, head) { | |
b05a1bcd | 1299 | if (req->flags & REQ_F_INFLIGHT) |
02a13674 | 1300 | return true; |
08d23634 PB |
1301 | } |
1302 | return false; | |
1303 | } | |
1304 | ||
d886e185 PB |
1305 | static inline bool req_has_async_data(struct io_kiocb *req) |
1306 | { | |
1307 | return req->flags & REQ_F_ASYNC_DATA; | |
1308 | } | |
1309 | ||
93d2bcd2 | 1310 | static inline void req_set_fail(struct io_kiocb *req) |
c40f6379 | 1311 | { |
93d2bcd2 | 1312 | req->flags |= REQ_F_FAIL; |
04c76b41 PB |
1313 | if (req->flags & REQ_F_CQE_SKIP) { |
1314 | req->flags &= ~REQ_F_CQE_SKIP; | |
1315 | req->flags |= REQ_F_SKIP_LINK_CQES; | |
1316 | } | |
c40f6379 | 1317 | } |
4a38aed2 | 1318 | |
a8295b98 HX |
1319 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
1320 | { | |
1321 | req_set_fail(req); | |
1322 | req->result = res; | |
1323 | } | |
1324 | ||
c072481d | 1325 | static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref) |
2b188cc1 JA |
1326 | { |
1327 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1328 | ||
0f158b4c | 1329 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1330 | } |
1331 | ||
8eb7e2d0 PB |
1332 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1333 | { | |
1334 | return !req->timeout.off; | |
1335 | } | |
1336 | ||
c072481d | 1337 | static __cold void io_fallback_req_func(struct work_struct *work) |
f56165e6 PB |
1338 | { |
1339 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, | |
1340 | fallback_work.work); | |
1341 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); | |
1342 | struct io_kiocb *req, *tmp; | |
f237c30a | 1343 | bool locked = false; |
f56165e6 PB |
1344 | |
1345 | percpu_ref_get(&ctx->refs); | |
1346 | llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node) | |
f237c30a | 1347 | req->io_task_work.func(req, &locked); |
5636c00d | 1348 | |
f237c30a | 1349 | if (locked) { |
c450178d | 1350 | io_submit_flush_completions(ctx); |
f237c30a PB |
1351 | mutex_unlock(&ctx->uring_lock); |
1352 | } | |
f56165e6 PB |
1353 | percpu_ref_put(&ctx->refs); |
1354 | } | |
1355 | ||
c072481d | 1356 | static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
2b188cc1 JA |
1357 | { |
1358 | struct io_ring_ctx *ctx; | |
78076bb6 | 1359 | int hash_bits; |
2b188cc1 JA |
1360 | |
1361 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1362 | if (!ctx) | |
1363 | return NULL; | |
1364 | ||
78076bb6 JA |
1365 | /* |
1366 | * Use 5 bits less than the max cq entries, that should give us around | |
1367 | * 32 entries per hash list if totally full and uniformly spread. | |
1368 | */ | |
1369 | hash_bits = ilog2(p->cq_entries); | |
1370 | hash_bits -= 5; | |
1371 | if (hash_bits <= 0) | |
1372 | hash_bits = 1; | |
1373 | ctx->cancel_hash_bits = hash_bits; | |
1374 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1375 | GFP_KERNEL); | |
1376 | if (!ctx->cancel_hash) | |
1377 | goto err; | |
1378 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1379 | ||
6224843d PB |
1380 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
1381 | if (!ctx->dummy_ubuf) | |
1382 | goto err; | |
1383 | /* set invalid range, so io_import_fixed() fails meeting it */ | |
1384 | ctx->dummy_ubuf->ubuf = -1UL; | |
1385 | ||
21482896 | 1386 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1387 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1388 | goto err; | |
2b188cc1 JA |
1389 | |
1390 | ctx->flags = p->flags; | |
90554200 | 1391 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1392 | INIT_LIST_HEAD(&ctx->sqd_list); |
1d7bb1d5 | 1393 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c | 1394 | init_completion(&ctx->ref_comp); |
9e15c3a0 | 1395 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
61cf9370 | 1396 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
2b188cc1 | 1397 | mutex_init(&ctx->uring_lock); |
311997b3 | 1398 | init_waitqueue_head(&ctx->cq_wait); |
2b188cc1 | 1399 | spin_lock_init(&ctx->completion_lock); |
89850fce | 1400 | spin_lock_init(&ctx->timeout_lock); |
5eef4e87 | 1401 | INIT_WQ_LIST(&ctx->iopoll_list); |
de0617e4 | 1402 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1403 | INIT_LIST_HEAD(&ctx->timeout_list); |
ef9dd637 | 1404 | INIT_LIST_HEAD(&ctx->ltimeout_list); |
d67d2263 BM |
1405 | spin_lock_init(&ctx->rsrc_ref_lock); |
1406 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1407 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1408 | init_llist_head(&ctx->rsrc_put_llist); | |
13bf43f5 | 1409 | INIT_LIST_HEAD(&ctx->tctx_list); |
c2b6c6bc PB |
1410 | ctx->submit_state.free_list.next = NULL; |
1411 | INIT_WQ_LIST(&ctx->locked_free_list); | |
9011bf9a | 1412 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
6f33b0bc | 1413 | INIT_WQ_LIST(&ctx->submit_state.compl_reqs); |
2b188cc1 | 1414 | return ctx; |
206aefde | 1415 | err: |
6224843d | 1416 | kfree(ctx->dummy_ubuf); |
78076bb6 | 1417 | kfree(ctx->cancel_hash); |
206aefde JA |
1418 | kfree(ctx); |
1419 | return NULL; | |
2b188cc1 JA |
1420 | } |
1421 | ||
8f6ed49a PB |
1422 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
1423 | { | |
1424 | struct io_rings *r = ctx->rings; | |
1425 | ||
1426 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); | |
1427 | ctx->cq_extra--; | |
1428 | } | |
1429 | ||
9cf7c104 | 1430 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1431 | { |
2bc9930e JA |
1432 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1433 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1434 | |
8f6ed49a | 1435 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
2bc9930e | 1436 | } |
de0617e4 | 1437 | |
9d858b21 | 1438 | return false; |
de0617e4 JA |
1439 | } |
1440 | ||
35645ac3 PB |
1441 | #define FFS_NOWAIT 0x1UL |
1442 | #define FFS_ISREG 0x2UL | |
1443 | #define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG) | |
c97d8a0f PB |
1444 | |
1445 | static inline bool io_req_ffs_set(struct io_kiocb *req) | |
1446 | { | |
35645ac3 | 1447 | return req->flags & REQ_F_FIXED_FILE; |
c97d8a0f PB |
1448 | } |
1449 | ||
c072481d | 1450 | static inline void io_req_track_inflight(struct io_kiocb *req) |
ce3d5aae | 1451 | { |
ce3d5aae | 1452 | if (!(req->flags & REQ_F_INFLIGHT)) { |
ce3d5aae | 1453 | req->flags |= REQ_F_INFLIGHT; |
b303fe2e | 1454 | atomic_inc(¤t->io_uring->inflight_tracked); |
ce3d5aae PB |
1455 | } |
1456 | } | |
1457 | ||
fd08e530 PB |
1458 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
1459 | { | |
906c6caa PB |
1460 | if (WARN_ON_ONCE(!req->link)) |
1461 | return NULL; | |
1462 | ||
4d13d1a4 PB |
1463 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
1464 | req->flags |= REQ_F_LINK_TIMEOUT; | |
fd08e530 PB |
1465 | |
1466 | /* linked timeouts should have two refs once prep'ed */ | |
48dcd38d | 1467 | io_req_set_refcount(req); |
4d13d1a4 PB |
1468 | __io_req_set_refcount(req->link, 2); |
1469 | return req->link; | |
fd08e530 PB |
1470 | } |
1471 | ||
1472 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) | |
1473 | { | |
4d13d1a4 | 1474 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
fd08e530 PB |
1475 | return NULL; |
1476 | return __io_prep_linked_timeout(req); | |
1477 | } | |
1478 | ||
1e6fa521 JA |
1479 | static void io_prep_async_work(struct io_kiocb *req) |
1480 | { | |
1481 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1482 | struct io_ring_ctx *ctx = req->ctx; |
1483 | ||
b8e64b53 PB |
1484 | if (!(req->flags & REQ_F_CREDS)) { |
1485 | req->flags |= REQ_F_CREDS; | |
c10d1f98 | 1486 | req->creds = get_current_cred(); |
b8e64b53 | 1487 | } |
003e8dcc | 1488 | |
e1d675df PB |
1489 | req->work.list.next = NULL; |
1490 | req->work.flags = 0; | |
feaadc4f PB |
1491 | if (req->flags & REQ_F_FORCE_ASYNC) |
1492 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1493 | ||
1e6fa521 JA |
1494 | if (req->flags & REQ_F_ISREG) { |
1495 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1496 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
4b982bd0 | 1497 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
1e6fa521 JA |
1498 | if (def->unbound_nonreg_file) |
1499 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1500 | } | |
e1d675df PB |
1501 | |
1502 | switch (req->opcode) { | |
1503 | case IORING_OP_SPLICE: | |
1504 | case IORING_OP_TEE: | |
e1d675df PB |
1505 | if (!S_ISREG(file_inode(req->splice.file_in)->i_mode)) |
1506 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1507 | break; | |
1508 | } | |
561fb04a | 1509 | } |
cccf0ee8 | 1510 | |
cbdcb435 | 1511 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1512 | { |
cbdcb435 | 1513 | struct io_kiocb *cur; |
54a91f3b | 1514 | |
44eff40a PB |
1515 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
1516 | struct io_ring_ctx *ctx = req->ctx; | |
1517 | ||
79ebeaee | 1518 | spin_lock(&ctx->completion_lock); |
44eff40a PB |
1519 | io_for_each_link(cur, req) |
1520 | io_prep_async_work(cur); | |
79ebeaee | 1521 | spin_unlock(&ctx->completion_lock); |
44eff40a PB |
1522 | } else { |
1523 | io_for_each_link(cur, req) | |
1524 | io_prep_async_work(cur); | |
1525 | } | |
561fb04a JA |
1526 | } |
1527 | ||
fff4e40e PB |
1528 | static inline void io_req_add_compl_list(struct io_kiocb *req) |
1529 | { | |
3d4aeb9f | 1530 | struct io_ring_ctx *ctx = req->ctx; |
fff4e40e PB |
1531 | struct io_submit_state *state = &req->ctx->submit_state; |
1532 | ||
3d4aeb9f PB |
1533 | if (!(req->flags & REQ_F_CQE_SKIP)) |
1534 | ctx->submit_state.flush_cqes = true; | |
fff4e40e PB |
1535 | wq_list_add_tail(&req->comp_list, &state->compl_reqs); |
1536 | } | |
1537 | ||
00169246 | 1538 | static void io_queue_async_work(struct io_kiocb *req, bool *dont_use) |
561fb04a | 1539 | { |
a197f664 | 1540 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1541 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1542 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1543 | |
3bfe6106 JA |
1544 | BUG_ON(!tctx); |
1545 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1546 | |
cbdcb435 PB |
1547 | /* init ->work of the whole link before punting */ |
1548 | io_prep_async_link(req); | |
991468dc JA |
1549 | |
1550 | /* | |
1551 | * Not expected to happen, but if we do have a bug where this _can_ | |
1552 | * happen, catch it here and ensure the request is marked as | |
1553 | * canceled. That will make io-wq go through the usual work cancel | |
1554 | * procedure rather than attempt to run this request (or create a new | |
1555 | * worker for it). | |
1556 | */ | |
1557 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) | |
1558 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1559 | ||
d07f1e8a PB |
1560 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1561 | &req->work, req->flags); | |
ebf93667 | 1562 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a JA |
1563 | if (link) |
1564 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1565 | } |
1566 | ||
1ee4160c | 1567 | static void io_kill_timeout(struct io_kiocb *req, int status) |
8c855885 | 1568 | __must_hold(&req->ctx->completion_lock) |
89850fce | 1569 | __must_hold(&req->ctx->timeout_lock) |
5262f567 | 1570 | { |
e8c2bc1f | 1571 | struct io_timeout_data *io = req->async_data; |
5262f567 | 1572 | |
fd9c7bc5 | 1573 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
2ae2eb9d PB |
1574 | if (status) |
1575 | req_set_fail(req); | |
01cec8c1 PB |
1576 | atomic_set(&req->ctx->cq_timeouts, |
1577 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1578 | list_del_init(&req->timeout.list); |
913a571a | 1579 | io_fill_cqe_req(req, status, 0); |
91c2f697 | 1580 | io_put_req_deferred(req); |
5262f567 JA |
1581 | } |
1582 | } | |
1583 | ||
c072481d | 1584 | static __cold void io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1585 | { |
441b8a78 | 1586 | while (!list_empty(&ctx->defer_list)) { |
27dc8338 PB |
1587 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1588 | struct io_defer_entry, list); | |
de0617e4 | 1589 | |
9cf7c104 | 1590 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1591 | break; |
27dc8338 | 1592 | list_del_init(&de->list); |
907d1df3 | 1593 | io_req_task_queue(de->req); |
27dc8338 | 1594 | kfree(de); |
441b8a78 | 1595 | } |
04518945 PB |
1596 | } |
1597 | ||
c072481d | 1598 | static __cold void io_flush_timeouts(struct io_ring_ctx *ctx) |
89850fce | 1599 | __must_hold(&ctx->completion_lock) |
de0617e4 | 1600 | { |
441b8a78 | 1601 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
f010505b | 1602 | |
79ebeaee | 1603 | spin_lock_irq(&ctx->timeout_lock); |
f18ee4cf | 1604 | while (!list_empty(&ctx->timeout_list)) { |
f010505b | 1605 | u32 events_needed, events_got; |
360428f8 | 1606 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1607 | struct io_kiocb, timeout.list); |
de0617e4 | 1608 | |
8eb7e2d0 | 1609 | if (io_is_timeout_noseq(req)) |
360428f8 | 1610 | break; |
f010505b MDG |
1611 | |
1612 | /* | |
1613 | * Since seq can easily wrap around over time, subtract | |
1614 | * the last seq at which timeouts were flushed before comparing. | |
1615 | * Assuming not more than 2^31-1 events have happened since, | |
1616 | * these subtractions won't have wrapped, so we can check if | |
1617 | * target is in [last_seq, current_seq] by comparing the two. | |
1618 | */ | |
1619 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1620 | events_got = seq - ctx->cq_last_tm_flush; | |
1621 | if (events_got < events_needed) | |
360428f8 | 1622 | break; |
bfe68a22 | 1623 | |
135fcde8 | 1624 | list_del_init(&req->timeout.list); |
1ee4160c | 1625 | io_kill_timeout(req, 0); |
f18ee4cf | 1626 | } |
f010505b | 1627 | ctx->cq_last_tm_flush = seq; |
79ebeaee | 1628 | spin_unlock_irq(&ctx->timeout_lock); |
360428f8 | 1629 | } |
5262f567 | 1630 | |
c072481d | 1631 | static __cold void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
360428f8 | 1632 | { |
2335f6f5 PB |
1633 | if (ctx->off_timeout_used) |
1634 | io_flush_timeouts(ctx); | |
1635 | if (ctx->drain_active) | |
1636 | io_queue_deferred(ctx); | |
1637 | } | |
1638 | ||
1639 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) | |
1640 | { | |
1641 | if (unlikely(ctx->off_timeout_used || ctx->drain_active)) | |
1642 | __io_commit_cqring_flush(ctx); | |
ec30e04b PB |
1643 | /* order cqe stores with ring update */ |
1644 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 JA |
1645 | } |
1646 | ||
90554200 JA |
1647 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1648 | { | |
1649 | struct io_rings *r = ctx->rings; | |
1650 | ||
a566c556 | 1651 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries; |
90554200 JA |
1652 | } |
1653 | ||
888aae2e PB |
1654 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1655 | { | |
1656 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1657 | } | |
1658 | ||
d068b506 | 1659 | static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) |
2b188cc1 | 1660 | { |
75b28aff | 1661 | struct io_rings *rings = ctx->rings; |
ea5ab3b5 | 1662 | unsigned tail, mask = ctx->cq_entries - 1; |
2b188cc1 | 1663 | |
115e12e5 SB |
1664 | /* |
1665 | * writes to the cq entry need to come after reading head; the | |
1666 | * control dependency is enough as we're using WRITE_ONCE to | |
1667 | * fill the cq entry | |
1668 | */ | |
a566c556 | 1669 | if (__io_cqring_events(ctx) == ctx->cq_entries) |
2b188cc1 JA |
1670 | return NULL; |
1671 | ||
888aae2e | 1672 | tail = ctx->cached_cq_tail++; |
ea5ab3b5 | 1673 | return &rings->cqes[tail & mask]; |
2b188cc1 JA |
1674 | } |
1675 | ||
f2842ab5 JA |
1676 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1677 | { | |
44c769de | 1678 | if (likely(!ctx->cq_ev_fd)) |
f0b493e6 | 1679 | return false; |
7e55a19c SG |
1680 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1681 | return false; | |
44c769de | 1682 | return !ctx->eventfd_async || io_wq_current_is_worker(); |
f2842ab5 JA |
1683 | } |
1684 | ||
2c5d763c JA |
1685 | /* |
1686 | * This should only get called when at least one event has been posted. | |
1687 | * Some applications rely on the eventfd notification count only changing | |
1688 | * IFF a new CQE has been added to the CQ ring. There's no depedency on | |
1689 | * 1:1 relationship between how many times this function is called (and | |
1690 | * hence the eventfd count) and number of CQEs posted to the CQ ring. | |
1691 | */ | |
b41e9852 | 1692 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1693 | { |
5fd46178 JA |
1694 | /* |
1695 | * wake_up_all() may seem excessive, but io_wake_function() and | |
1696 | * io_should_wake() handle the termination of the loop and only | |
1697 | * wake as many waiters as we need to. | |
1698 | */ | |
1699 | if (wq_has_sleeper(&ctx->cq_wait)) | |
1700 | wake_up_all(&ctx->cq_wait); | |
b41e9852 | 1701 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 JA |
1702 | eventfd_signal(ctx->cq_ev_fd, 1); |
1703 | } | |
1704 | ||
80c18e4a PB |
1705 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1706 | { | |
c57a91fb PB |
1707 | /* see waitqueue_active() comment */ |
1708 | smp_mb(); | |
1709 | ||
80c18e4a | 1710 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
c57a91fb | 1711 | if (waitqueue_active(&ctx->cq_wait)) |
5fd46178 | 1712 | wake_up_all(&ctx->cq_wait); |
80c18e4a PB |
1713 | } |
1714 | if (io_should_trigger_evfd(ctx)) | |
1715 | eventfd_signal(ctx->cq_ev_fd, 1); | |
1716 | } | |
1717 | ||
c4a2ed72 | 1718 | /* Returns true if there are no backlogged entries after the flush */ |
6c2450ae | 1719 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
1d7bb1d5 | 1720 | { |
b18032bb | 1721 | bool all_flushed, posted; |
1d7bb1d5 | 1722 | |
a566c556 | 1723 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
e23de15f | 1724 | return false; |
1d7bb1d5 | 1725 | |
b18032bb | 1726 | posted = false; |
79ebeaee | 1727 | spin_lock(&ctx->completion_lock); |
6c2450ae | 1728 | while (!list_empty(&ctx->cq_overflow_list)) { |
d068b506 | 1729 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
6c2450ae | 1730 | struct io_overflow_cqe *ocqe; |
e6c8aa9a | 1731 | |
1d7bb1d5 JA |
1732 | if (!cqe && !force) |
1733 | break; | |
6c2450ae PB |
1734 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
1735 | struct io_overflow_cqe, list); | |
1736 | if (cqe) | |
1737 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); | |
1738 | else | |
8f6ed49a PB |
1739 | io_account_cq_overflow(ctx); |
1740 | ||
b18032bb | 1741 | posted = true; |
6c2450ae PB |
1742 | list_del(&ocqe->list); |
1743 | kfree(ocqe); | |
1d7bb1d5 JA |
1744 | } |
1745 | ||
09e88404 PB |
1746 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1747 | if (all_flushed) { | |
5ed7a37d | 1748 | clear_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1749 | WRITE_ONCE(ctx->rings->sq_flags, |
1750 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); | |
09e88404 | 1751 | } |
46930143 | 1752 | |
b18032bb JA |
1753 | if (posted) |
1754 | io_commit_cqring(ctx); | |
79ebeaee | 1755 | spin_unlock(&ctx->completion_lock); |
b18032bb JA |
1756 | if (posted) |
1757 | io_cqring_ev_posted(ctx); | |
09e88404 | 1758 | return all_flushed; |
1d7bb1d5 JA |
1759 | } |
1760 | ||
90f67366 | 1761 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
6c503150 | 1762 | { |
ca0a2651 JA |
1763 | bool ret = true; |
1764 | ||
5ed7a37d | 1765 | if (test_bit(0, &ctx->check_cq_overflow)) { |
6c503150 PB |
1766 | /* iopoll syncs against uring_lock, not completion_lock */ |
1767 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1768 | mutex_lock(&ctx->uring_lock); | |
90f67366 | 1769 | ret = __io_cqring_overflow_flush(ctx, false); |
6c503150 PB |
1770 | if (ctx->flags & IORING_SETUP_IOPOLL) |
1771 | mutex_unlock(&ctx->uring_lock); | |
1772 | } | |
ca0a2651 JA |
1773 | |
1774 | return ret; | |
6c503150 PB |
1775 | } |
1776 | ||
6a290a14 PB |
1777 | /* must to be called somewhat shortly after putting a request */ |
1778 | static inline void io_put_task(struct task_struct *task, int nr) | |
1779 | { | |
1780 | struct io_uring_task *tctx = task->io_uring; | |
1781 | ||
e98e49b2 PB |
1782 | if (likely(task == current)) { |
1783 | tctx->cached_refs += nr; | |
1784 | } else { | |
1785 | percpu_counter_sub(&tctx->inflight, nr); | |
1786 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1787 | wake_up(&tctx->wait); | |
1788 | put_task_struct_many(task, nr); | |
1789 | } | |
6a290a14 PB |
1790 | } |
1791 | ||
9a10867a PB |
1792 | static void io_task_refs_refill(struct io_uring_task *tctx) |
1793 | { | |
1794 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; | |
1795 | ||
1796 | percpu_counter_add(&tctx->inflight, refill); | |
1797 | refcount_add(refill, ¤t->usage); | |
1798 | tctx->cached_refs += refill; | |
1799 | } | |
1800 | ||
1801 | static inline void io_get_task_refs(int nr) | |
1802 | { | |
1803 | struct io_uring_task *tctx = current->io_uring; | |
1804 | ||
1805 | tctx->cached_refs -= nr; | |
1806 | if (unlikely(tctx->cached_refs < 0)) | |
1807 | io_task_refs_refill(tctx); | |
1808 | } | |
1809 | ||
d4d19c19 | 1810 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
54daa9b2 | 1811 | s32 res, u32 cflags) |
2b188cc1 | 1812 | { |
cce4b8b0 | 1813 | struct io_overflow_cqe *ocqe; |
2b188cc1 | 1814 | |
cce4b8b0 PB |
1815 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
1816 | if (!ocqe) { | |
1817 | /* | |
1818 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1819 | * or cannot allocate an overflow entry, then we need to drop it | |
1820 | * on the floor. | |
1821 | */ | |
8f6ed49a | 1822 | io_account_cq_overflow(ctx); |
cce4b8b0 | 1823 | return false; |
2b188cc1 | 1824 | } |
cce4b8b0 | 1825 | if (list_empty(&ctx->cq_overflow_list)) { |
5ed7a37d | 1826 | set_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1827 | WRITE_ONCE(ctx->rings->sq_flags, |
1828 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); | |
1829 | ||
cce4b8b0 | 1830 | } |
d4d19c19 | 1831 | ocqe->cqe.user_data = user_data; |
cce4b8b0 PB |
1832 | ocqe->cqe.res = res; |
1833 | ocqe->cqe.flags = cflags; | |
1834 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); | |
1835 | return true; | |
2b188cc1 JA |
1836 | } |
1837 | ||
913a571a PB |
1838 | static inline bool __io_fill_cqe(struct io_ring_ctx *ctx, u64 user_data, |
1839 | s32 res, u32 cflags) | |
2b188cc1 JA |
1840 | { |
1841 | struct io_uring_cqe *cqe; | |
1842 | ||
d4d19c19 | 1843 | trace_io_uring_complete(ctx, user_data, res, cflags); |
51c3ff62 | 1844 | |
2b188cc1 JA |
1845 | /* |
1846 | * If we can't get a cq entry, userspace overflowed the | |
1847 | * submission (by quite a lot). Increment the overflow count in | |
1848 | * the ring. | |
1849 | */ | |
d068b506 | 1850 | cqe = io_get_cqe(ctx); |
1d7bb1d5 | 1851 | if (likely(cqe)) { |
d4d19c19 | 1852 | WRITE_ONCE(cqe->user_data, user_data); |
2b188cc1 | 1853 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1854 | WRITE_ONCE(cqe->flags, cflags); |
8d13326e | 1855 | return true; |
2b188cc1 | 1856 | } |
d4d19c19 | 1857 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
2b188cc1 JA |
1858 | } |
1859 | ||
913a571a PB |
1860 | static noinline void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags) |
1861 | { | |
04c76b41 PB |
1862 | if (!(req->flags & REQ_F_CQE_SKIP)) |
1863 | __io_fill_cqe(req->ctx, req->user_data, res, cflags); | |
913a571a PB |
1864 | } |
1865 | ||
1866 | static noinline bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data, | |
1867 | s32 res, u32 cflags) | |
bcda7baa | 1868 | { |
913a571a PB |
1869 | ctx->cq_extra++; |
1870 | return __io_fill_cqe(ctx, user_data, res, cflags); | |
bcda7baa JA |
1871 | } |
1872 | ||
54daa9b2 PB |
1873 | static void io_req_complete_post(struct io_kiocb *req, s32 res, |
1874 | u32 cflags) | |
2b188cc1 | 1875 | { |
78e19bbe | 1876 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 | 1877 | |
79ebeaee | 1878 | spin_lock(&ctx->completion_lock); |
04c76b41 PB |
1879 | if (!(req->flags & REQ_F_CQE_SKIP)) |
1880 | __io_fill_cqe(ctx, req->user_data, res, cflags); | |
c7dae4ba JA |
1881 | /* |
1882 | * If we're the last reference to this request, add to our locked | |
1883 | * free_list cache. | |
1884 | */ | |
de9b4cca | 1885 | if (req_ref_put_and_test(req)) { |
7a612350 | 1886 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
0756a869 | 1887 | if (req->flags & IO_DISARM_MASK) |
7a612350 PB |
1888 | io_disarm_next(req); |
1889 | if (req->link) { | |
1890 | io_req_task_queue(req->link); | |
1891 | req->link = NULL; | |
1892 | } | |
1893 | } | |
ab409402 | 1894 | io_req_put_rsrc(req, ctx); |
c7dae4ba JA |
1895 | io_dismantle_req(req); |
1896 | io_put_task(req->task, 1); | |
c2b6c6bc | 1897 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
d0acdee2 | 1898 | ctx->locked_free_nr++; |
180f829f | 1899 | } |
7a612350 | 1900 | io_commit_cqring(ctx); |
79ebeaee | 1901 | spin_unlock(&ctx->completion_lock); |
a3f34907 | 1902 | io_cqring_ev_posted(ctx); |
4e3d9ff9 JA |
1903 | } |
1904 | ||
54daa9b2 PB |
1905 | static inline void io_req_complete_state(struct io_kiocb *req, s32 res, |
1906 | u32 cflags) | |
229a7b63 | 1907 | { |
a38d68db | 1908 | req->result = res; |
d17e56eb | 1909 | req->cflags = cflags; |
e342c807 | 1910 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
1911 | } |
1912 | ||
889fca73 | 1913 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
54daa9b2 | 1914 | s32 res, u32 cflags) |
bcda7baa | 1915 | { |
889fca73 PB |
1916 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
1917 | io_req_complete_state(req, res, cflags); | |
a38d68db | 1918 | else |
c7dae4ba | 1919 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
1920 | } |
1921 | ||
54daa9b2 | 1922 | static inline void io_req_complete(struct io_kiocb *req, s32 res) |
0ddf92e8 | 1923 | { |
889fca73 | 1924 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
1925 | } |
1926 | ||
54daa9b2 | 1927 | static void io_req_complete_failed(struct io_kiocb *req, s32 res) |
f41db273 | 1928 | { |
93d2bcd2 | 1929 | req_set_fail(req); |
f41db273 PB |
1930 | io_req_complete_post(req, res, 0); |
1931 | } | |
1932 | ||
c6d3d9cb PB |
1933 | static void io_req_complete_fail_submit(struct io_kiocb *req) |
1934 | { | |
1935 | /* | |
1936 | * We don't submit, fail them all, for that replace hardlinks with | |
1937 | * normal links. Extra REQ_F_LINK is tolerated. | |
1938 | */ | |
1939 | req->flags &= ~REQ_F_HARDLINK; | |
1940 | req->flags |= REQ_F_LINK; | |
1941 | io_req_complete_failed(req, req->result); | |
1942 | } | |
1943 | ||
864ea921 PB |
1944 | /* |
1945 | * Don't initialise the fields below on every allocation, but do that in | |
1946 | * advance and keep them valid across allocations. | |
1947 | */ | |
1948 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
1949 | { | |
1950 | req->ctx = ctx; | |
1951 | req->link = NULL; | |
1952 | req->async_data = NULL; | |
1953 | /* not necessary, but safer to zero */ | |
1954 | req->result = 0; | |
1955 | } | |
1956 | ||
dac7a098 | 1957 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
cd0ca2e0 | 1958 | struct io_submit_state *state) |
dac7a098 | 1959 | { |
79ebeaee | 1960 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 1961 | wq_list_splice(&ctx->locked_free_list, &state->free_list); |
d0acdee2 | 1962 | ctx->locked_free_nr = 0; |
79ebeaee | 1963 | spin_unlock(&ctx->completion_lock); |
dac7a098 PB |
1964 | } |
1965 | ||
dd78f492 | 1966 | /* Returns true IFF there are requests in the cache */ |
c7dae4ba | 1967 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 1968 | { |
c7dae4ba | 1969 | struct io_submit_state *state = &ctx->submit_state; |
0ddf92e8 | 1970 | |
c7dae4ba JA |
1971 | /* |
1972 | * If we have more than a batch's worth of requests in our IRQ side | |
1973 | * locked cache, grab the lock and move them over to our submission | |
1974 | * side cache. | |
1975 | */ | |
d0acdee2 | 1976 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
cd0ca2e0 | 1977 | io_flush_cached_locked_reqs(ctx, state); |
c2b6c6bc | 1978 | return !!state->free_list.next; |
0ddf92e8 JA |
1979 | } |
1980 | ||
5d5901a3 PB |
1981 | /* |
1982 | * A request might get retired back into the request caches even before opcode | |
1983 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. | |
1984 | * Because of that, io_alloc_req() should be called only under ->uring_lock | |
1985 | * and with extra caution to not get a request that is still worked on. | |
1986 | */ | |
c072481d | 1987 | static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx) |
5d5901a3 | 1988 | __must_hold(&ctx->uring_lock) |
2b188cc1 | 1989 | { |
e5d1bc0a | 1990 | struct io_submit_state *state = &ctx->submit_state; |
864ea921 | 1991 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
3ab665b7 PB |
1992 | void *reqs[IO_REQ_ALLOC_BATCH]; |
1993 | struct io_kiocb *req; | |
864ea921 | 1994 | int ret, i; |
e5d1bc0a | 1995 | |
c2b6c6bc | 1996 | if (likely(state->free_list.next || io_flush_cached_reqs(ctx))) |
a33ae9ce | 1997 | return true; |
e5d1bc0a | 1998 | |
3ab665b7 | 1999 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs); |
fd6fab2c | 2000 | |
864ea921 PB |
2001 | /* |
2002 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
2003 | * retry single alloc to be on the safe side. | |
2004 | */ | |
2005 | if (unlikely(ret <= 0)) { | |
3ab665b7 PB |
2006 | reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
2007 | if (!reqs[0]) | |
a33ae9ce | 2008 | return false; |
864ea921 | 2009 | ret = 1; |
2b188cc1 | 2010 | } |
864ea921 | 2011 | |
37f0e767 | 2012 | percpu_ref_get_many(&ctx->refs, ret); |
3ab665b7 PB |
2013 | for (i = 0; i < ret; i++) { |
2014 | req = reqs[i]; | |
2015 | ||
2016 | io_preinit_req(req, ctx); | |
c2b6c6bc | 2017 | wq_stack_add_head(&req->comp_list, &state->free_list); |
3ab665b7 | 2018 | } |
a33ae9ce PB |
2019 | return true; |
2020 | } | |
2021 | ||
2022 | static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx) | |
2023 | { | |
2024 | if (unlikely(!ctx->submit_state.free_list.next)) | |
2025 | return __io_alloc_req_refill(ctx); | |
2026 | return true; | |
2027 | } | |
2028 | ||
2029 | static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) | |
2030 | { | |
2031 | struct io_wq_work_node *node; | |
2032 | ||
2033 | node = wq_stack_extract(&ctx->submit_state.free_list); | |
c2b6c6bc | 2034 | return container_of(node, struct io_kiocb, comp_list); |
2b188cc1 JA |
2035 | } |
2036 | ||
e1d767f0 | 2037 | static inline void io_put_file(struct file *file) |
8da11c19 | 2038 | { |
e1d767f0 | 2039 | if (file) |
8da11c19 PB |
2040 | fput(file); |
2041 | } | |
2042 | ||
6b639522 | 2043 | static inline void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 2044 | { |
094bae49 | 2045 | unsigned int flags = req->flags; |
929a3af9 | 2046 | |
867f8fa5 | 2047 | if (unlikely(flags & IO_REQ_CLEAN_FLAGS)) |
3a0a6902 | 2048 | io_clean_op(req); |
e1d767f0 PB |
2049 | if (!(flags & REQ_F_FIXED_FILE)) |
2050 | io_put_file(req->file); | |
e65ef56d JA |
2051 | } |
2052 | ||
c072481d | 2053 | static __cold void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 2054 | { |
51a4cc11 | 2055 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 2056 | |
ab409402 | 2057 | io_req_put_rsrc(req, ctx); |
216578e5 | 2058 | io_dismantle_req(req); |
7c660731 | 2059 | io_put_task(req->task, 1); |
c6ca97b3 | 2060 | |
79ebeaee | 2061 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 2062 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
c34b025f | 2063 | ctx->locked_free_nr++; |
79ebeaee | 2064 | spin_unlock(&ctx->completion_lock); |
e65ef56d JA |
2065 | } |
2066 | ||
f2f87370 PB |
2067 | static inline void io_remove_next_linked(struct io_kiocb *req) |
2068 | { | |
2069 | struct io_kiocb *nxt = req->link; | |
2070 | ||
2071 | req->link = nxt->link; | |
2072 | nxt->link = NULL; | |
2073 | } | |
2074 | ||
33cc89a9 PB |
2075 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
2076 | __must_hold(&req->ctx->completion_lock) | |
89b263f6 | 2077 | __must_hold(&req->ctx->timeout_lock) |
2665abfd | 2078 | { |
33cc89a9 | 2079 | struct io_kiocb *link = req->link; |
f2f87370 | 2080 | |
b97e736a | 2081 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
c9abd7ad | 2082 | struct io_timeout_data *io = link->async_data; |
7c86ffee | 2083 | |
f2f87370 | 2084 | io_remove_next_linked(req); |
90cd7e42 | 2085 | link->timeout.head = NULL; |
fd9c7bc5 | 2086 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
ef9dd637 | 2087 | list_del(&link->timeout.list); |
04c76b41 | 2088 | /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */ |
913a571a | 2089 | io_fill_cqe_req(link, -ECANCELED, 0); |
91c2f697 | 2090 | io_put_req_deferred(link); |
d4729fbd | 2091 | return true; |
c9abd7ad PB |
2092 | } |
2093 | } | |
d4729fbd | 2094 | return false; |
7c86ffee PB |
2095 | } |
2096 | ||
d148ca4b | 2097 | static void io_fail_links(struct io_kiocb *req) |
33cc89a9 | 2098 | __must_hold(&req->ctx->completion_lock) |
9e645e11 | 2099 | { |
33cc89a9 | 2100 | struct io_kiocb *nxt, *link = req->link; |
04c76b41 | 2101 | bool ignore_cqes = req->flags & REQ_F_SKIP_LINK_CQES; |
9e645e11 | 2102 | |
f2f87370 | 2103 | req->link = NULL; |
f2f87370 | 2104 | while (link) { |
a8295b98 HX |
2105 | long res = -ECANCELED; |
2106 | ||
2107 | if (link->flags & REQ_F_FAIL) | |
2108 | res = link->result; | |
2109 | ||
f2f87370 PB |
2110 | nxt = link->link; |
2111 | link->link = NULL; | |
2665abfd | 2112 | |
f2f87370 | 2113 | trace_io_uring_fail_link(req, link); |
04c76b41 PB |
2114 | if (!ignore_cqes) { |
2115 | link->flags &= ~REQ_F_CQE_SKIP; | |
2116 | io_fill_cqe_req(link, res, 0); | |
2117 | } | |
91c2f697 | 2118 | io_put_req_deferred(link); |
f2f87370 | 2119 | link = nxt; |
9e645e11 | 2120 | } |
33cc89a9 | 2121 | } |
9e645e11 | 2122 | |
33cc89a9 PB |
2123 | static bool io_disarm_next(struct io_kiocb *req) |
2124 | __must_hold(&req->ctx->completion_lock) | |
2125 | { | |
2126 | bool posted = false; | |
2127 | ||
0756a869 PB |
2128 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
2129 | struct io_kiocb *link = req->link; | |
2130 | ||
906c6caa | 2131 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
0756a869 PB |
2132 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
2133 | io_remove_next_linked(req); | |
04c76b41 | 2134 | /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */ |
913a571a | 2135 | io_fill_cqe_req(link, -ECANCELED, 0); |
0756a869 PB |
2136 | io_put_req_deferred(link); |
2137 | posted = true; | |
2138 | } | |
2139 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { | |
89b263f6 JA |
2140 | struct io_ring_ctx *ctx = req->ctx; |
2141 | ||
2142 | spin_lock_irq(&ctx->timeout_lock); | |
33cc89a9 | 2143 | posted = io_kill_linked_timeout(req); |
89b263f6 JA |
2144 | spin_unlock_irq(&ctx->timeout_lock); |
2145 | } | |
93d2bcd2 | 2146 | if (unlikely((req->flags & REQ_F_FAIL) && |
e4335ed3 | 2147 | !(req->flags & REQ_F_HARDLINK))) { |
33cc89a9 PB |
2148 | posted |= (req->link != NULL); |
2149 | io_fail_links(req); | |
2150 | } | |
2151 | return posted; | |
9e645e11 JA |
2152 | } |
2153 | ||
d81499bf PB |
2154 | static void __io_req_find_next_prep(struct io_kiocb *req) |
2155 | { | |
2156 | struct io_ring_ctx *ctx = req->ctx; | |
2157 | bool posted; | |
2158 | ||
2159 | spin_lock(&ctx->completion_lock); | |
2160 | posted = io_disarm_next(req); | |
2161 | if (posted) | |
2162 | io_commit_cqring(req->ctx); | |
2163 | spin_unlock(&ctx->completion_lock); | |
2164 | if (posted) | |
2165 | io_cqring_ev_posted(ctx); | |
2166 | } | |
2167 | ||
2168 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) | |
c69f8dbe | 2169 | { |
33cc89a9 | 2170 | struct io_kiocb *nxt; |
944e58bf | 2171 | |
d81499bf PB |
2172 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
2173 | return NULL; | |
9e645e11 JA |
2174 | /* |
2175 | * If LINK is set, we have dependent requests in this chain. If we | |
2176 | * didn't fail this request, queue the first one up, moving any other | |
2177 | * dependencies to the next request. In case of failure, fail the rest | |
2178 | * of the chain. | |
2179 | */ | |
d81499bf PB |
2180 | if (unlikely(req->flags & IO_DISARM_MASK)) |
2181 | __io_req_find_next_prep(req); | |
33cc89a9 PB |
2182 | nxt = req->link; |
2183 | req->link = NULL; | |
2184 | return nxt; | |
4d7dd462 | 2185 | } |
9e645e11 | 2186 | |
f237c30a | 2187 | static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked) |
2c32395d PB |
2188 | { |
2189 | if (!ctx) | |
2190 | return; | |
f237c30a | 2191 | if (*locked) { |
c450178d | 2192 | io_submit_flush_completions(ctx); |
2c32395d | 2193 | mutex_unlock(&ctx->uring_lock); |
f237c30a | 2194 | *locked = false; |
2c32395d PB |
2195 | } |
2196 | percpu_ref_put(&ctx->refs); | |
2197 | } | |
2198 | ||
7cbf1722 | 2199 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 2200 | { |
f237c30a | 2201 | bool locked = false; |
ebd0df2e | 2202 | struct io_ring_ctx *ctx = NULL; |
3f18407d PB |
2203 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
2204 | task_work); | |
c40f6379 | 2205 | |
16f72070 | 2206 | while (1) { |
3f18407d PB |
2207 | struct io_wq_work_node *node; |
2208 | ||
c450178d | 2209 | if (!tctx->task_list.first && locked) |
8d4ad41e PB |
2210 | io_submit_flush_completions(ctx); |
2211 | ||
3f18407d | 2212 | spin_lock_irq(&tctx->task_lock); |
c6538be9 | 2213 | node = tctx->task_list.first; |
3f18407d | 2214 | INIT_WQ_LIST(&tctx->task_list); |
6294f368 PB |
2215 | if (!node) |
2216 | tctx->task_running = false; | |
3f18407d | 2217 | spin_unlock_irq(&tctx->task_lock); |
6294f368 PB |
2218 | if (!node) |
2219 | break; | |
3f18407d | 2220 | |
6294f368 | 2221 | do { |
3f18407d PB |
2222 | struct io_wq_work_node *next = node->next; |
2223 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2224 | io_task_work.node); | |
2225 | ||
2226 | if (req->ctx != ctx) { | |
f237c30a | 2227 | ctx_flush_and_put(ctx, &locked); |
3f18407d | 2228 | ctx = req->ctx; |
126180b9 PB |
2229 | /* if not contended, grab and improve batching */ |
2230 | locked = mutex_trylock(&ctx->uring_lock); | |
3f18407d PB |
2231 | percpu_ref_get(&ctx->refs); |
2232 | } | |
f237c30a | 2233 | req->io_task_work.func(req, &locked); |
3f18407d | 2234 | node = next; |
6294f368 PB |
2235 | } while (node); |
2236 | ||
7cbf1722 | 2237 | cond_resched(); |
3f18407d | 2238 | } |
ebd0df2e | 2239 | |
f237c30a | 2240 | ctx_flush_and_put(ctx, &locked); |
7cbf1722 JA |
2241 | } |
2242 | ||
e09ee510 | 2243 | static void io_req_task_work_add(struct io_kiocb *req) |
7cbf1722 | 2244 | { |
c15b79de | 2245 | struct task_struct *tsk = req->task; |
7cbf1722 | 2246 | struct io_uring_task *tctx = tsk->io_uring; |
c15b79de | 2247 | enum task_work_notify_mode notify; |
e09ee510 | 2248 | struct io_wq_work_node *node; |
0b81e80c | 2249 | unsigned long flags; |
6294f368 | 2250 | bool running; |
7cbf1722 JA |
2251 | |
2252 | WARN_ON_ONCE(!tctx); | |
2253 | ||
0b81e80c | 2254 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 | 2255 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
6294f368 PB |
2256 | running = tctx->task_running; |
2257 | if (!running) | |
2258 | tctx->task_running = true; | |
0b81e80c | 2259 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2260 | |
2261 | /* task_work already pending, we're done */ | |
6294f368 | 2262 | if (running) |
e09ee510 | 2263 | return; |
7cbf1722 | 2264 | |
c15b79de PB |
2265 | /* |
2266 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For | |
2267 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2268 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2269 | * will do the job. | |
2270 | */ | |
2271 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; | |
d97ec623 PB |
2272 | if (likely(!task_work_add(tsk, &tctx->task_work, notify))) { |
2273 | if (notify == TWA_NONE) | |
2274 | wake_up_process(tsk); | |
e09ee510 | 2275 | return; |
c15b79de | 2276 | } |
2215bed9 | 2277 | |
0b81e80c | 2278 | spin_lock_irqsave(&tctx->task_lock, flags); |
6294f368 | 2279 | tctx->task_running = false; |
e09ee510 PB |
2280 | node = tctx->task_list.first; |
2281 | INIT_WQ_LIST(&tctx->task_list); | |
0b81e80c | 2282 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 | 2283 | |
e09ee510 PB |
2284 | while (node) { |
2285 | req = container_of(node, struct io_kiocb, io_task_work.node); | |
2286 | node = node->next; | |
2287 | if (llist_add(&req->io_task_work.fallback_node, | |
2288 | &req->ctx->fallback_llist)) | |
2289 | schedule_delayed_work(&req->ctx->fallback_work, 1); | |
2290 | } | |
eab30c4d PB |
2291 | } |
2292 | ||
f237c30a | 2293 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
c40f6379 | 2294 | { |
87ceb6a6 | 2295 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2296 | |
b18a1a45 | 2297 | /* not needed for normal modes, but SQPOLL depends on it */ |
f237c30a | 2298 | io_tw_lock(ctx, locked); |
2593553a | 2299 | io_req_complete_failed(req, req->result); |
c40f6379 JA |
2300 | } |
2301 | ||
f237c30a | 2302 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
c40f6379 JA |
2303 | { |
2304 | struct io_ring_ctx *ctx = req->ctx; | |
2305 | ||
f237c30a | 2306 | io_tw_lock(ctx, locked); |
316319e8 | 2307 | /* req->task == current here, checking PF_EXITING is safe */ |
af066f31 | 2308 | if (likely(!(req->task->flags & PF_EXITING))) |
c5eef2b9 | 2309 | __io_queue_sqe(req); |
81b6d05c | 2310 | else |
2593553a | 2311 | io_req_complete_failed(req, -EFAULT); |
c40f6379 JA |
2312 | } |
2313 | ||
2c4b8eb6 | 2314 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
c40f6379 | 2315 | { |
2c4b8eb6 | 2316 | req->result = ret; |
5b0a6acc | 2317 | req->io_task_work.func = io_req_task_cancel; |
e09ee510 | 2318 | io_req_task_work_add(req); |
c40f6379 JA |
2319 | } |
2320 | ||
2c4b8eb6 | 2321 | static void io_req_task_queue(struct io_kiocb *req) |
a3df7698 | 2322 | { |
5b0a6acc | 2323 | req->io_task_work.func = io_req_task_submit; |
e09ee510 | 2324 | io_req_task_work_add(req); |
a3df7698 PB |
2325 | } |
2326 | ||
773af691 JA |
2327 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
2328 | { | |
2329 | req->io_task_work.func = io_queue_async_work; | |
2330 | io_req_task_work_add(req); | |
2331 | } | |
2332 | ||
f2f87370 | 2333 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2334 | { |
9b5f7bd9 | 2335 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2336 | |
2337 | if (nxt) | |
906a8c3f | 2338 | io_req_task_queue(nxt); |
c69f8dbe JL |
2339 | } |
2340 | ||
c3524383 | 2341 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2342 | { |
c3524383 PB |
2343 | io_queue_next(req); |
2344 | __io_free_req(req); | |
2345 | } | |
8766dd51 | 2346 | |
f237c30a PB |
2347 | static void io_free_req_work(struct io_kiocb *req, bool *locked) |
2348 | { | |
2349 | io_free_req(req); | |
2350 | } | |
2351 | ||
3aa83bfb | 2352 | static void io_free_batch_list(struct io_ring_ctx *ctx, |
1cce17ac | 2353 | struct io_wq_work_node *node) |
3aa83bfb | 2354 | __must_hold(&ctx->uring_lock) |
5af1d13e | 2355 | { |
d4b7a5ef | 2356 | struct task_struct *task = NULL; |
37f0e767 | 2357 | int task_refs = 0; |
5af1d13e | 2358 | |
3aa83bfb PB |
2359 | do { |
2360 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2361 | comp_list); | |
2d6500d4 | 2362 | |
def77acf | 2363 | if (unlikely(req->flags & REQ_F_REFCOUNT)) { |
c1e53a69 | 2364 | node = req->comp_list.next; |
def77acf PB |
2365 | if (!req_ref_put_and_test(req)) |
2366 | continue; | |
c1e53a69 | 2367 | } |
2d6500d4 | 2368 | |
ab409402 | 2369 | io_req_put_rsrc_locked(req, ctx); |
d4b7a5ef PB |
2370 | io_queue_next(req); |
2371 | io_dismantle_req(req); | |
5af1d13e | 2372 | |
d4b7a5ef PB |
2373 | if (req->task != task) { |
2374 | if (task) | |
2375 | io_put_task(task, task_refs); | |
2376 | task = req->task; | |
2377 | task_refs = 0; | |
2378 | } | |
2379 | task_refs++; | |
c1e53a69 | 2380 | node = req->comp_list.next; |
d4b7a5ef | 2381 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
3aa83bfb | 2382 | } while (node); |
d4b7a5ef | 2383 | |
d4b7a5ef PB |
2384 | if (task) |
2385 | io_put_task(task, task_refs); | |
7a743e22 PB |
2386 | } |
2387 | ||
c450178d | 2388 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx) |
a141dd89 | 2389 | __must_hold(&ctx->uring_lock) |
905c172f | 2390 | { |
6f33b0bc | 2391 | struct io_wq_work_node *node, *prev; |
cd0ca2e0 | 2392 | struct io_submit_state *state = &ctx->submit_state; |
905c172f | 2393 | |
3d4aeb9f PB |
2394 | if (state->flush_cqes) { |
2395 | spin_lock(&ctx->completion_lock); | |
2396 | wq_list_for_each(node, prev, &state->compl_reqs) { | |
2397 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
6f33b0bc | 2398 | comp_list); |
5182ed2e | 2399 | |
3d4aeb9f PB |
2400 | if (!(req->flags & REQ_F_CQE_SKIP)) |
2401 | __io_fill_cqe(ctx, req->user_data, req->result, | |
2402 | req->cflags); | |
2403 | } | |
2404 | ||
2405 | io_commit_cqring(ctx); | |
2406 | spin_unlock(&ctx->completion_lock); | |
2407 | io_cqring_ev_posted(ctx); | |
2408 | state->flush_cqes = false; | |
905c172f | 2409 | } |
5182ed2e | 2410 | |
1cce17ac | 2411 | io_free_batch_list(ctx, state->compl_reqs.first); |
6f33b0bc | 2412 | INIT_WQ_LIST(&state->compl_reqs); |
7a743e22 PB |
2413 | } |
2414 | ||
ba816ad6 JA |
2415 | /* |
2416 | * Drop reference to request, return next in chain (if there is one) if this | |
2417 | * was the last reference to this request. | |
2418 | */ | |
0d85035a | 2419 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2420 | { |
9b5f7bd9 PB |
2421 | struct io_kiocb *nxt = NULL; |
2422 | ||
de9b4cca | 2423 | if (req_ref_put_and_test(req)) { |
9b5f7bd9 | 2424 | nxt = io_req_find_next(req); |
4d7dd462 | 2425 | __io_free_req(req); |
2a44f467 | 2426 | } |
9b5f7bd9 | 2427 | return nxt; |
2b188cc1 JA |
2428 | } |
2429 | ||
0d85035a | 2430 | static inline void io_put_req(struct io_kiocb *req) |
e65ef56d | 2431 | { |
de9b4cca | 2432 | if (req_ref_put_and_test(req)) |
e65ef56d | 2433 | io_free_req(req); |
2b188cc1 JA |
2434 | } |
2435 | ||
91c2f697 | 2436 | static inline void io_put_req_deferred(struct io_kiocb *req) |
216578e5 | 2437 | { |
91c2f697 | 2438 | if (req_ref_put_and_test(req)) { |
f237c30a | 2439 | req->io_task_work.func = io_free_req_work; |
543af3a1 PB |
2440 | io_req_task_work_add(req); |
2441 | } | |
216578e5 PB |
2442 | } |
2443 | ||
6c503150 | 2444 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2445 | { |
2446 | /* See comment at the top of this file */ | |
2447 | smp_rmb(); | |
e23de15f | 2448 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2449 | } |
2450 | ||
fb5ccc98 PB |
2451 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2452 | { | |
2453 | struct io_rings *rings = ctx->rings; | |
2454 | ||
2455 | /* make sure SQ entry isn't read before tail */ | |
2456 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2457 | } | |
2458 | ||
8ff069bf | 2459 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2460 | { |
8ff069bf | 2461 | unsigned int cflags; |
e94f141b | 2462 | |
bcda7baa JA |
2463 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2464 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2465 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2466 | kfree(kbuf); |
2467 | return cflags; | |
e94f141b JA |
2468 | } |
2469 | ||
8ff069bf | 2470 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2471 | { |
ae421d93 PB |
2472 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
2473 | return 0; | |
30d51dd4 | 2474 | return io_put_kbuf(req, req->kbuf); |
8ff069bf PB |
2475 | } |
2476 | ||
4c6e277c JA |
2477 | static inline bool io_run_task_work(void) |
2478 | { | |
ef98eb04 | 2479 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) { |
4c6e277c | 2480 | __set_current_state(TASK_RUNNING); |
ef98eb04 | 2481 | tracehook_notify_signal(); |
4c6e277c JA |
2482 | return true; |
2483 | } | |
2484 | ||
2485 | return false; | |
bcda7baa JA |
2486 | } |
2487 | ||
5ba3c874 | 2488 | static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) |
def596e9 | 2489 | { |
5eef4e87 | 2490 | struct io_wq_work_node *pos, *start, *prev; |
d729cf9a | 2491 | unsigned int poll_flags = BLK_POLL_NOSLEEP; |
b688f11e | 2492 | DEFINE_IO_COMP_BATCH(iob); |
5ba3c874 | 2493 | int nr_events = 0; |
def596e9 JA |
2494 | |
2495 | /* | |
2496 | * Only spin for completions if we don't have multiple devices hanging | |
87a115fb | 2497 | * off our complete list. |
def596e9 | 2498 | */ |
87a115fb | 2499 | if (ctx->poll_multi_queue || force_nonspin) |
ef99b2d3 | 2500 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2501 | |
5eef4e87 PB |
2502 | wq_list_for_each(pos, start, &ctx->iopoll_list) { |
2503 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
9adbd45d | 2504 | struct kiocb *kiocb = &req->rw.kiocb; |
a2416e1e | 2505 | int ret; |
def596e9 JA |
2506 | |
2507 | /* | |
581f9810 BM |
2508 | * Move completed and retryable entries to our local lists. |
2509 | * If we find a request that requires polling, break out | |
2510 | * and complete those lists first, if we have entries there. | |
def596e9 | 2511 | */ |
e3f721e6 | 2512 | if (READ_ONCE(req->iopoll_completed)) |
def596e9 JA |
2513 | break; |
2514 | ||
b688f11e | 2515 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags); |
a2416e1e PB |
2516 | if (unlikely(ret < 0)) |
2517 | return ret; | |
2518 | else if (ret) | |
ef99b2d3 | 2519 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2520 | |
3aadc23e | 2521 | /* iopoll may have completed current req */ |
b688f11e JA |
2522 | if (!rq_list_empty(iob.req_list) || |
2523 | READ_ONCE(req->iopoll_completed)) | |
e3f721e6 | 2524 | break; |
def596e9 JA |
2525 | } |
2526 | ||
b688f11e JA |
2527 | if (!rq_list_empty(iob.req_list)) |
2528 | iob.complete(&iob); | |
5eef4e87 PB |
2529 | else if (!pos) |
2530 | return 0; | |
def596e9 | 2531 | |
5eef4e87 PB |
2532 | prev = start; |
2533 | wq_list_for_each_resume(pos, prev) { | |
2534 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
04c76b41 | 2535 | u32 cflags; |
5eef4e87 | 2536 | |
b3fa03fd PB |
2537 | /* order with io_complete_rw_iopoll(), e.g. ->result updates */ |
2538 | if (!smp_load_acquire(&req->iopoll_completed)) | |
e3f721e6 | 2539 | break; |
04c76b41 PB |
2540 | cflags = io_put_rw_kbuf(req); |
2541 | if (!(req->flags & REQ_F_CQE_SKIP)) | |
2542 | __io_fill_cqe(ctx, req->user_data, req->result, cflags); | |
e3f721e6 PB |
2543 | nr_events++; |
2544 | } | |
def596e9 | 2545 | |
f5ed3bcd PB |
2546 | if (unlikely(!nr_events)) |
2547 | return 0; | |
2548 | ||
2549 | io_commit_cqring(ctx); | |
2550 | io_cqring_ev_posted_iopoll(ctx); | |
1cce17ac | 2551 | pos = start ? start->next : ctx->iopoll_list.first; |
5eef4e87 | 2552 | wq_list_cut(&ctx->iopoll_list, prev, start); |
1cce17ac | 2553 | io_free_batch_list(ctx, pos); |
5ba3c874 | 2554 | return nr_events; |
def596e9 JA |
2555 | } |
2556 | ||
def596e9 JA |
2557 | /* |
2558 | * We can't just wait for polled events to come to us, we have to actively | |
2559 | * find and complete them. | |
2560 | */ | |
c072481d | 2561 | static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2562 | { |
2563 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2564 | return; | |
2565 | ||
2566 | mutex_lock(&ctx->uring_lock); | |
5eef4e87 | 2567 | while (!wq_list_empty(&ctx->iopoll_list)) { |
b2edc0a7 | 2568 | /* let it sleep and repeat later if can't complete a request */ |
5ba3c874 | 2569 | if (io_do_iopoll(ctx, true) == 0) |
b2edc0a7 | 2570 | break; |
08f5439f JA |
2571 | /* |
2572 | * Ensure we allow local-to-the-cpu processing to take place, | |
2573 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2574 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2575 | */ |
3fcee5a6 PB |
2576 | if (need_resched()) { |
2577 | mutex_unlock(&ctx->uring_lock); | |
2578 | cond_resched(); | |
2579 | mutex_lock(&ctx->uring_lock); | |
2580 | } | |
def596e9 JA |
2581 | } |
2582 | mutex_unlock(&ctx->uring_lock); | |
2583 | } | |
2584 | ||
7668b92a | 2585 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2586 | { |
7668b92a | 2587 | unsigned int nr_events = 0; |
e9979b36 | 2588 | int ret = 0; |
500f9fba | 2589 | |
c7849be9 XW |
2590 | /* |
2591 | * We disallow the app entering submit/complete with polling, but we | |
2592 | * still need to lock the ring to prevent racing with polled issue | |
2593 | * that got punted to a workqueue. | |
2594 | */ | |
2595 | mutex_lock(&ctx->uring_lock); | |
f39c8a5b PB |
2596 | /* |
2597 | * Don't enter poll loop if we already have events pending. | |
2598 | * If we do, we can potentially be spinning for commands that | |
2599 | * already triggered a CQE (eg in error). | |
2600 | */ | |
5ed7a37d | 2601 | if (test_bit(0, &ctx->check_cq_overflow)) |
f39c8a5b PB |
2602 | __io_cqring_overflow_flush(ctx, false); |
2603 | if (io_cqring_events(ctx)) | |
2604 | goto out; | |
def596e9 | 2605 | do { |
500f9fba JA |
2606 | /* |
2607 | * If a submit got punted to a workqueue, we can have the | |
2608 | * application entering polling for a command before it gets | |
2609 | * issued. That app will hold the uring_lock for the duration | |
2610 | * of the poll right here, so we need to take a breather every | |
2611 | * now and then to ensure that the issue has a chance to add | |
2612 | * the poll to the issued list. Otherwise we can spin here | |
2613 | * forever, while the workqueue is stuck trying to acquire the | |
2614 | * very same mutex. | |
2615 | */ | |
5eef4e87 | 2616 | if (wq_list_empty(&ctx->iopoll_list)) { |
8f487ef2 PB |
2617 | u32 tail = ctx->cached_cq_tail; |
2618 | ||
500f9fba | 2619 | mutex_unlock(&ctx->uring_lock); |
4c6e277c | 2620 | io_run_task_work(); |
500f9fba | 2621 | mutex_lock(&ctx->uring_lock); |
def596e9 | 2622 | |
8f487ef2 PB |
2623 | /* some requests don't go through iopoll_list */ |
2624 | if (tail != ctx->cached_cq_tail || | |
5eef4e87 | 2625 | wq_list_empty(&ctx->iopoll_list)) |
e9979b36 | 2626 | break; |
500f9fba | 2627 | } |
5ba3c874 PB |
2628 | ret = io_do_iopoll(ctx, !min); |
2629 | if (ret < 0) | |
2630 | break; | |
2631 | nr_events += ret; | |
2632 | ret = 0; | |
2633 | } while (nr_events < min && !need_resched()); | |
f39c8a5b | 2634 | out: |
500f9fba | 2635 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2636 | return ret; |
2637 | } | |
2638 | ||
491381ce | 2639 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2640 | { |
491381ce JA |
2641 | /* |
2642 | * Tell lockdep we inherited freeze protection from submission | |
2643 | * thread. | |
2644 | */ | |
2645 | if (req->flags & REQ_F_ISREG) { | |
1c98679d | 2646 | struct super_block *sb = file_inode(req->file)->i_sb; |
2b188cc1 | 2647 | |
1c98679d PB |
2648 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
2649 | sb_end_write(sb); | |
2b188cc1 JA |
2650 | } |
2651 | } | |
2652 | ||
b63534c4 | 2653 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2654 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 | 2655 | { |
ab454438 | 2656 | struct io_async_rw *rw = req->async_data; |
b63534c4 | 2657 | |
d886e185 | 2658 | if (!req_has_async_data(req)) |
ab454438 | 2659 | return !io_req_prep_async(req); |
538941e2 | 2660 | iov_iter_restore(&rw->s.iter, &rw->s.iter_state); |
ab454438 | 2661 | return true; |
b63534c4 | 2662 | } |
b63534c4 | 2663 | |
3e6a0d3c | 2664 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 2665 | { |
355afaeb | 2666 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 2667 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 2668 | |
355afaeb JA |
2669 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2670 | return false; | |
3e6a0d3c JA |
2671 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
2672 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 2673 | return false; |
7c977a58 JA |
2674 | /* |
2675 | * If ref is dying, we might be running poll reap from the exit work. | |
2676 | * Don't attempt to reissue from that path, just let it fail with | |
2677 | * -EAGAIN. | |
2678 | */ | |
3e6a0d3c JA |
2679 | if (percpu_ref_is_dying(&ctx->refs)) |
2680 | return false; | |
ef046888 JA |
2681 | /* |
2682 | * Play it safe and assume not safe to re-import and reissue if we're | |
2683 | * not in the original thread group (or in task context). | |
2684 | */ | |
2685 | if (!same_thread_group(req->task, current) || !in_task()) | |
2686 | return false; | |
3e6a0d3c JA |
2687 | return true; |
2688 | } | |
e82ad485 | 2689 | #else |
a1ff1e3f | 2690 | static bool io_resubmit_prep(struct io_kiocb *req) |
e82ad485 JA |
2691 | { |
2692 | return false; | |
2693 | } | |
e82ad485 | 2694 | static bool io_rw_should_reissue(struct io_kiocb *req) |
3e6a0d3c | 2695 | { |
b63534c4 JA |
2696 | return false; |
2697 | } | |
3e6a0d3c | 2698 | #endif |
b63534c4 | 2699 | |
8ef12efe | 2700 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
a1d7c393 | 2701 | { |
b65c128f PB |
2702 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
2703 | kiocb_end_write(req); | |
258f3a7f | 2704 | if (unlikely(res != req->result)) { |
9532b99b PB |
2705 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && |
2706 | io_rw_should_reissue(req)) { | |
2707 | req->flags |= REQ_F_REISSUE; | |
8ef12efe | 2708 | return true; |
9532b99b | 2709 | } |
93d2bcd2 | 2710 | req_set_fail(req); |
8ef12efe | 2711 | req->result = res; |
9532b99b | 2712 | } |
8ef12efe JA |
2713 | return false; |
2714 | } | |
2715 | ||
f237c30a | 2716 | static void io_req_task_complete(struct io_kiocb *req, bool *locked) |
8ef12efe | 2717 | { |
126180b9 | 2718 | unsigned int cflags = io_put_rw_kbuf(req); |
54daa9b2 | 2719 | int res = req->result; |
126180b9 PB |
2720 | |
2721 | if (*locked) { | |
126180b9 | 2722 | io_req_complete_state(req, res, cflags); |
fff4e40e | 2723 | io_req_add_compl_list(req); |
126180b9 PB |
2724 | } else { |
2725 | io_req_complete_post(req, res, cflags); | |
2726 | } | |
8ef12efe JA |
2727 | } |
2728 | ||
2729 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, | |
2730 | unsigned int issue_flags) | |
2731 | { | |
2732 | if (__io_complete_rw_common(req, res)) | |
2733 | return; | |
63637853 | 2734 | __io_req_complete(req, issue_flags, req->result, io_put_rw_kbuf(req)); |
ba816ad6 JA |
2735 | } |
2736 | ||
6b19b766 | 2737 | static void io_complete_rw(struct kiocb *kiocb, long res) |
ba816ad6 | 2738 | { |
9adbd45d | 2739 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2740 | |
8ef12efe JA |
2741 | if (__io_complete_rw_common(req, res)) |
2742 | return; | |
2743 | req->result = res; | |
2744 | req->io_task_work.func = io_req_task_complete; | |
2745 | io_req_task_work_add(req); | |
2b188cc1 JA |
2746 | } |
2747 | ||
6b19b766 | 2748 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res) |
def596e9 | 2749 | { |
9adbd45d | 2750 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2751 | |
491381ce JA |
2752 | if (kiocb->ki_flags & IOCB_WRITE) |
2753 | kiocb_end_write(req); | |
9532b99b | 2754 | if (unlikely(res != req->result)) { |
b66ceaf3 PB |
2755 | if (res == -EAGAIN && io_rw_should_reissue(req)) { |
2756 | req->flags |= REQ_F_REISSUE; | |
2757 | return; | |
9532b99b | 2758 | } |
258f3a7f | 2759 | req->result = res; |
8c130827 | 2760 | } |
bbde017a | 2761 | |
b3fa03fd PB |
2762 | /* order with io_iopoll_complete() checking ->iopoll_completed */ |
2763 | smp_store_release(&req->iopoll_completed, 1); | |
def596e9 JA |
2764 | } |
2765 | ||
2766 | /* | |
2767 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2768 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
f39c8a5b | 2769 | * find it from a io_do_iopoll() thread before the issuer is done |
def596e9 JA |
2770 | * accessing the kiocb cookie. |
2771 | */ | |
9882131c | 2772 | static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags) |
def596e9 JA |
2773 | { |
2774 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 2775 | const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
cb3d8972 PB |
2776 | |
2777 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
3b44b371 | 2778 | if (unlikely(needs_lock)) |
cb3d8972 | 2779 | mutex_lock(&ctx->uring_lock); |
def596e9 JA |
2780 | |
2781 | /* | |
2782 | * Track whether we have multiple files in our lists. This will impact | |
2783 | * how we do polling eventually, not spinning if we're on potentially | |
2784 | * different devices. | |
2785 | */ | |
5eef4e87 | 2786 | if (wq_list_empty(&ctx->iopoll_list)) { |
915b3dde HX |
2787 | ctx->poll_multi_queue = false; |
2788 | } else if (!ctx->poll_multi_queue) { | |
def596e9 JA |
2789 | struct io_kiocb *list_req; |
2790 | ||
5eef4e87 PB |
2791 | list_req = container_of(ctx->iopoll_list.first, struct io_kiocb, |
2792 | comp_list); | |
30da1b45 | 2793 | if (list_req->file != req->file) |
915b3dde | 2794 | ctx->poll_multi_queue = true; |
def596e9 JA |
2795 | } |
2796 | ||
2797 | /* | |
2798 | * For fast devices, IO may have already completed. If it has, add | |
2799 | * it to the front so we find it first. | |
2800 | */ | |
65a6543d | 2801 | if (READ_ONCE(req->iopoll_completed)) |
5eef4e87 | 2802 | wq_list_add_head(&req->comp_list, &ctx->iopoll_list); |
def596e9 | 2803 | else |
5eef4e87 | 2804 | wq_list_add_tail(&req->comp_list, &ctx->iopoll_list); |
bdcd3eab | 2805 | |
3b44b371 | 2806 | if (unlikely(needs_lock)) { |
cb3d8972 PB |
2807 | /* |
2808 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle | |
2809 | * in sq thread task context or in io worker task context. If | |
2810 | * current task context is sq thread, we don't need to check | |
2811 | * whether should wake up sq thread. | |
2812 | */ | |
2813 | if ((ctx->flags & IORING_SETUP_SQPOLL) && | |
2814 | wq_has_sleeper(&ctx->sq_data->wait)) | |
2815 | wake_up(&ctx->sq_data->wait); | |
2816 | ||
2817 | mutex_unlock(&ctx->uring_lock); | |
2818 | } | |
def596e9 JA |
2819 | } |
2820 | ||
4503b767 JA |
2821 | static bool io_bdev_nowait(struct block_device *bdev) |
2822 | { | |
9ba0d0c8 | 2823 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2824 | } |
2825 | ||
2b188cc1 JA |
2826 | /* |
2827 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2828 | * any file. For now, just ensure that anything potentially problematic is done | |
2829 | * inline. | |
2830 | */ | |
88459b50 | 2831 | static bool __io_file_supports_nowait(struct file *file, umode_t mode) |
2b188cc1 | 2832 | { |
4503b767 | 2833 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2834 | if (IS_ENABLED(CONFIG_BLOCK) && |
2835 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2836 | return true; |
2837 | return false; | |
2838 | } | |
976517f1 | 2839 | if (S_ISSOCK(mode)) |
2b188cc1 | 2840 | return true; |
4503b767 | 2841 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2842 | if (IS_ENABLED(CONFIG_BLOCK) && |
2843 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2844 | file->f_op != &io_uring_fops) |
2845 | return true; | |
2846 | return false; | |
2847 | } | |
2b188cc1 | 2848 | |
c5b85625 JA |
2849 | /* any ->read/write should understand O_NONBLOCK */ |
2850 | if (file->f_flags & O_NONBLOCK) | |
2851 | return true; | |
35645ac3 | 2852 | return file->f_mode & FMODE_NOWAIT; |
2b188cc1 | 2853 | } |
c5b85625 | 2854 | |
88459b50 PB |
2855 | /* |
2856 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2857 | * any file. For now, just ensure that anything potentially problematic is done | |
2858 | * inline. | |
2859 | */ | |
2860 | static unsigned int io_file_get_flags(struct file *file) | |
2861 | { | |
2862 | umode_t mode = file_inode(file)->i_mode; | |
2863 | unsigned int res = 0; | |
af197f50 | 2864 | |
88459b50 PB |
2865 | if (S_ISREG(mode)) |
2866 | res |= FFS_ISREG; | |
2867 | if (__io_file_supports_nowait(file, mode)) | |
2868 | res |= FFS_NOWAIT; | |
2869 | return res; | |
2b188cc1 JA |
2870 | } |
2871 | ||
35645ac3 | 2872 | static inline bool io_file_supports_nowait(struct io_kiocb *req) |
7b29f92d | 2873 | { |
88459b50 | 2874 | return req->flags & REQ_F_SUPPORT_NOWAIT; |
7b29f92d JA |
2875 | } |
2876 | ||
b9a6b8f9 | 2877 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 2878 | { |
def596e9 | 2879 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2880 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 2881 | struct file *file = req->file; |
09bb8394 JA |
2882 | unsigned ioprio; |
2883 | int ret; | |
2b188cc1 | 2884 | |
88459b50 PB |
2885 | if (!io_req_ffs_set(req)) |
2886 | req->flags |= io_file_get_flags(file) << REQ_F_SUPPORT_NOWAIT_BIT; | |
491381ce | 2887 | |
2b188cc1 | 2888 | kiocb->ki_pos = READ_ONCE(sqe->off); |
75c668cd | 2889 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
ba04291e | 2890 | req->flags |= REQ_F_CUR_POS; |
75c668cd | 2891 | kiocb->ki_pos = file->f_pos; |
ba04291e | 2892 | } |
5cb03d63 | 2893 | kiocb->ki_flags = iocb_flags(file); |
3e577dcd PB |
2894 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
2895 | if (unlikely(ret)) | |
2896 | return ret; | |
2b188cc1 | 2897 | |
5d329e12 JA |
2898 | /* |
2899 | * If the file is marked O_NONBLOCK, still allow retry for it if it | |
2900 | * supports async. Otherwise it's impossible to use O_NONBLOCK files | |
2901 | * reliably. If not, or it IOCB_NOWAIT is set, don't retry. | |
2902 | */ | |
2903 | if ((kiocb->ki_flags & IOCB_NOWAIT) || | |
35645ac3 | 2904 | ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req))) |
75c668cd PB |
2905 | req->flags |= REQ_F_NOWAIT; |
2906 | ||
def596e9 | 2907 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
5cb03d63 | 2908 | if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll) |
09bb8394 | 2909 | return -EOPNOTSUPP; |
2b188cc1 | 2910 | |
394918eb | 2911 | kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE; |
def596e9 | 2912 | kiocb->ki_complete = io_complete_rw_iopoll; |
65a6543d | 2913 | req->iopoll_completed = 0; |
def596e9 | 2914 | } else { |
09bb8394 JA |
2915 | if (kiocb->ki_flags & IOCB_HIPRI) |
2916 | return -EINVAL; | |
def596e9 JA |
2917 | kiocb->ki_complete = io_complete_rw; |
2918 | } | |
9adbd45d | 2919 | |
fb27274a PB |
2920 | ioprio = READ_ONCE(sqe->ioprio); |
2921 | if (ioprio) { | |
2922 | ret = ioprio_check_cap(ioprio); | |
2923 | if (ret) | |
2924 | return ret; | |
2925 | ||
2926 | kiocb->ki_ioprio = ioprio; | |
2927 | } else { | |
2928 | kiocb->ki_ioprio = get_current_ioprio(); | |
eae071c9 PB |
2929 | } |
2930 | ||
578c0ee2 | 2931 | req->imu = NULL; |
3529d8c2 JA |
2932 | req->rw.addr = READ_ONCE(sqe->addr); |
2933 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2934 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2935 | return 0; |
2b188cc1 JA |
2936 | } |
2937 | ||
2938 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2939 | { | |
2940 | switch (ret) { | |
2941 | case -EIOCBQUEUED: | |
2942 | break; | |
2943 | case -ERESTARTSYS: | |
2944 | case -ERESTARTNOINTR: | |
2945 | case -ERESTARTNOHAND: | |
2946 | case -ERESTART_RESTARTBLOCK: | |
2947 | /* | |
2948 | * We can't just restart the syscall, since previously | |
2949 | * submitted sqes may already be in progress. Just fail this | |
2950 | * IO with EINTR. | |
2951 | */ | |
2952 | ret = -EINTR; | |
df561f66 | 2953 | fallthrough; |
2b188cc1 | 2954 | default: |
6b19b766 | 2955 | kiocb->ki_complete(kiocb, ret); |
2b188cc1 JA |
2956 | } |
2957 | } | |
2958 | ||
2ea537ca | 2959 | static void kiocb_done(struct io_kiocb *req, ssize_t ret, |
889fca73 | 2960 | unsigned int issue_flags) |
ba816ad6 | 2961 | { |
e8c2bc1f | 2962 | struct io_async_rw *io = req->async_data; |
ba04291e | 2963 | |
227c0c96 | 2964 | /* add previously done IO, if any */ |
d886e185 | 2965 | if (req_has_async_data(req) && io->bytes_done > 0) { |
227c0c96 | 2966 | if (ret < 0) |
e8c2bc1f | 2967 | ret = io->bytes_done; |
227c0c96 | 2968 | else |
e8c2bc1f | 2969 | ret += io->bytes_done; |
227c0c96 JA |
2970 | } |
2971 | ||
ba04291e | 2972 | if (req->flags & REQ_F_CUR_POS) |
2ea537ca PB |
2973 | req->file->f_pos = req->rw.kiocb.ki_pos; |
2974 | if (ret >= 0 && (req->rw.kiocb.ki_complete == io_complete_rw)) | |
889fca73 | 2975 | __io_complete_rw(req, ret, 0, issue_flags); |
ba816ad6 | 2976 | else |
2ea537ca | 2977 | io_rw_done(&req->rw.kiocb, ret); |
97284637 | 2978 | |
b66ceaf3 | 2979 | if (req->flags & REQ_F_REISSUE) { |
97284637 | 2980 | req->flags &= ~REQ_F_REISSUE; |
a7be7c23 | 2981 | if (io_resubmit_prep(req)) { |
773af691 | 2982 | io_req_task_queue_reissue(req); |
8c130827 | 2983 | } else { |
93d2bcd2 | 2984 | req_set_fail(req); |
06bdea20 PB |
2985 | req->result = ret; |
2986 | req->io_task_work.func = io_req_task_complete; | |
2987 | io_req_task_work_add(req); | |
97284637 PB |
2988 | } |
2989 | } | |
ba816ad6 JA |
2990 | } |
2991 | ||
eae071c9 PB |
2992 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
2993 | struct io_mapped_ubuf *imu) | |
edafccee | 2994 | { |
9adbd45d | 2995 | size_t len = req->rw.len; |
75769e3f | 2996 | u64 buf_end, buf_addr = req->rw.addr; |
edafccee | 2997 | size_t offset; |
edafccee | 2998 | |
75769e3f | 2999 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
edafccee JA |
3000 | return -EFAULT; |
3001 | /* not inside the mapped region */ | |
4751f53d | 3002 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
edafccee JA |
3003 | return -EFAULT; |
3004 | ||
3005 | /* | |
3006 | * May not be a start of buffer, set size appropriately | |
3007 | * and advance us to the beginning. | |
3008 | */ | |
3009 | offset = buf_addr - imu->ubuf; | |
3010 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
3011 | |
3012 | if (offset) { | |
3013 | /* | |
3014 | * Don't use iov_iter_advance() here, as it's really slow for | |
3015 | * using the latter parts of a big fixed buffer - it iterates | |
3016 | * over each segment manually. We can cheat a bit here, because | |
3017 | * we know that: | |
3018 | * | |
3019 | * 1) it's a BVEC iter, we set it up | |
3020 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
3021 | * first and last bvec | |
3022 | * | |
3023 | * So just find our index, and adjust the iterator afterwards. | |
3024 | * If the offset is within the first bvec (or the whole first | |
3025 | * bvec, just use iov_iter_advance(). This makes it easier | |
3026 | * since we can just skip the first segment, which may not | |
3027 | * be PAGE_SIZE aligned. | |
3028 | */ | |
3029 | const struct bio_vec *bvec = imu->bvec; | |
3030 | ||
3031 | if (offset <= bvec->bv_len) { | |
3032 | iov_iter_advance(iter, offset); | |
3033 | } else { | |
3034 | unsigned long seg_skip; | |
3035 | ||
3036 | /* skip first vec */ | |
3037 | offset -= bvec->bv_len; | |
3038 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
3039 | ||
3040 | iter->bvec = bvec + seg_skip; | |
3041 | iter->nr_segs -= seg_skip; | |
99c79f66 | 3042 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 3043 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
3044 | } |
3045 | } | |
3046 | ||
847595de | 3047 | return 0; |
edafccee JA |
3048 | } |
3049 | ||
eae071c9 PB |
3050 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
3051 | { | |
eae071c9 PB |
3052 | struct io_mapped_ubuf *imu = req->imu; |
3053 | u16 index, buf_index = req->buf_index; | |
3054 | ||
3055 | if (likely(!imu)) { | |
578c0ee2 PB |
3056 | struct io_ring_ctx *ctx = req->ctx; |
3057 | ||
eae071c9 PB |
3058 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
3059 | return -EFAULT; | |
578c0ee2 | 3060 | io_req_set_rsrc_node(req, ctx); |
eae071c9 PB |
3061 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
3062 | imu = READ_ONCE(ctx->user_bufs[index]); | |
3063 | req->imu = imu; | |
3064 | } | |
3065 | return __io_import_fixed(req, rw, iter, imu); | |
3066 | } | |
3067 | ||
bcda7baa JA |
3068 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
3069 | { | |
3070 | if (needs_lock) | |
3071 | mutex_unlock(&ctx->uring_lock); | |
3072 | } | |
3073 | ||
3074 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
3075 | { | |
3076 | /* | |
3077 | * "Normal" inline submissions always hold the uring_lock, since we | |
3078 | * grab it from the system call. Same is true for the SQPOLL offload. | |
3079 | * The only exception is when we've detached the request and issue it | |
3080 | * from an async worker thread, grab the lock for that case. | |
3081 | */ | |
3082 | if (needs_lock) | |
3083 | mutex_lock(&ctx->uring_lock); | |
3084 | } | |
3085 | ||
3086 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
51aac424 | 3087 | int bgid, unsigned int issue_flags) |
bcda7baa | 3088 | { |
30d51dd4 | 3089 | struct io_buffer *kbuf = req->kbuf; |
bcda7baa | 3090 | struct io_buffer *head; |
3b44b371 | 3091 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
bcda7baa JA |
3092 | |
3093 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3094 | return kbuf; | |
3095 | ||
3096 | io_ring_submit_lock(req->ctx, needs_lock); | |
3097 | ||
3098 | lockdep_assert_held(&req->ctx->uring_lock); | |
3099 | ||
9e15c3a0 | 3100 | head = xa_load(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
3101 | if (head) { |
3102 | if (!list_empty(&head->list)) { | |
3103 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
3104 | list); | |
3105 | list_del(&kbuf->list); | |
3106 | } else { | |
3107 | kbuf = head; | |
9e15c3a0 | 3108 | xa_erase(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
3109 | } |
3110 | if (*len > kbuf->len) | |
3111 | *len = kbuf->len; | |
30d51dd4 PB |
3112 | req->flags |= REQ_F_BUFFER_SELECTED; |
3113 | req->kbuf = kbuf; | |
bcda7baa JA |
3114 | } else { |
3115 | kbuf = ERR_PTR(-ENOBUFS); | |
3116 | } | |
3117 | ||
3118 | io_ring_submit_unlock(req->ctx, needs_lock); | |
bcda7baa JA |
3119 | return kbuf; |
3120 | } | |
3121 | ||
4d954c25 | 3122 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
51aac424 | 3123 | unsigned int issue_flags) |
4d954c25 JA |
3124 | { |
3125 | struct io_buffer *kbuf; | |
4f4eeba8 | 3126 | u16 bgid; |
4d954c25 | 3127 | |
4f4eeba8 | 3128 | bgid = req->buf_index; |
51aac424 | 3129 | kbuf = io_buffer_select(req, len, bgid, issue_flags); |
4d954c25 JA |
3130 | if (IS_ERR(kbuf)) |
3131 | return kbuf; | |
4d954c25 JA |
3132 | return u64_to_user_ptr(kbuf->addr); |
3133 | } | |
3134 | ||
3135 | #ifdef CONFIG_COMPAT | |
3136 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3137 | unsigned int issue_flags) |
4d954c25 JA |
3138 | { |
3139 | struct compat_iovec __user *uiov; | |
3140 | compat_ssize_t clen; | |
3141 | void __user *buf; | |
3142 | ssize_t len; | |
3143 | ||
3144 | uiov = u64_to_user_ptr(req->rw.addr); | |
3145 | if (!access_ok(uiov, sizeof(*uiov))) | |
3146 | return -EFAULT; | |
3147 | if (__get_user(clen, &uiov->iov_len)) | |
3148 | return -EFAULT; | |
3149 | if (clen < 0) | |
3150 | return -EINVAL; | |
3151 | ||
3152 | len = clen; | |
51aac424 | 3153 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3154 | if (IS_ERR(buf)) |
3155 | return PTR_ERR(buf); | |
3156 | iov[0].iov_base = buf; | |
3157 | iov[0].iov_len = (compat_size_t) len; | |
3158 | return 0; | |
3159 | } | |
3160 | #endif | |
3161 | ||
3162 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3163 | unsigned int issue_flags) |
4d954c25 JA |
3164 | { |
3165 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3166 | void __user *buf; | |
3167 | ssize_t len; | |
3168 | ||
3169 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3170 | return -EFAULT; | |
3171 | ||
3172 | len = iov[0].iov_len; | |
3173 | if (len < 0) | |
3174 | return -EINVAL; | |
51aac424 | 3175 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3176 | if (IS_ERR(buf)) |
3177 | return PTR_ERR(buf); | |
3178 | iov[0].iov_base = buf; | |
3179 | iov[0].iov_len = len; | |
3180 | return 0; | |
3181 | } | |
3182 | ||
3183 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3184 | unsigned int issue_flags) |
4d954c25 | 3185 | { |
dddb3e26 | 3186 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
30d51dd4 | 3187 | struct io_buffer *kbuf = req->kbuf; |
dddb3e26 | 3188 | |
dddb3e26 JA |
3189 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
3190 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3191 | return 0; |
dddb3e26 | 3192 | } |
dd201662 | 3193 | if (req->rw.len != 1) |
4d954c25 JA |
3194 | return -EINVAL; |
3195 | ||
3196 | #ifdef CONFIG_COMPAT | |
3197 | if (req->ctx->compat) | |
51aac424 | 3198 | return io_compat_import(req, iov, issue_flags); |
4d954c25 JA |
3199 | #endif |
3200 | ||
51aac424 | 3201 | return __io_iov_buffer_select(req, iov, issue_flags); |
4d954c25 JA |
3202 | } |
3203 | ||
caa8fe6e PB |
3204 | static struct iovec *__io_import_iovec(int rw, struct io_kiocb *req, |
3205 | struct io_rw_state *s, | |
3206 | unsigned int issue_flags) | |
2b188cc1 | 3207 | { |
5e49c973 | 3208 | struct iov_iter *iter = &s->iter; |
847595de | 3209 | u8 opcode = req->opcode; |
caa8fe6e | 3210 | struct iovec *iovec; |
d1d681b0 PB |
3211 | void __user *buf; |
3212 | size_t sqe_len; | |
4d954c25 | 3213 | ssize_t ret; |
edafccee | 3214 | |
f3251183 PB |
3215 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
3216 | ret = io_import_fixed(req, rw, iter); | |
3217 | if (ret) | |
3218 | return ERR_PTR(ret); | |
3219 | return NULL; | |
3220 | } | |
2b188cc1 | 3221 | |
bcda7baa | 3222 | /* buffer index only valid with fixed read/write, or buffer select */ |
d1d681b0 | 3223 | if (unlikely(req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))) |
caa8fe6e | 3224 | return ERR_PTR(-EINVAL); |
9adbd45d | 3225 | |
d1d681b0 PB |
3226 | buf = u64_to_user_ptr(req->rw.addr); |
3227 | sqe_len = req->rw.len; | |
9adbd45d | 3228 | |
3a6820f2 | 3229 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3230 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 3231 | buf = io_rw_buffer_select(req, &sqe_len, issue_flags); |
867a23ea | 3232 | if (IS_ERR(buf)) |
898df244 | 3233 | return ERR_CAST(buf); |
3f9d6441 | 3234 | req->rw.len = sqe_len; |
bcda7baa JA |
3235 | } |
3236 | ||
5e49c973 | 3237 | ret = import_single_range(rw, buf, sqe_len, s->fast_iov, iter); |
f3251183 PB |
3238 | if (ret) |
3239 | return ERR_PTR(ret); | |
3240 | return NULL; | |
3a6820f2 JA |
3241 | } |
3242 | ||
caa8fe6e | 3243 | iovec = s->fast_iov; |
4d954c25 | 3244 | if (req->flags & REQ_F_BUFFER_SELECT) { |
caa8fe6e | 3245 | ret = io_iov_buffer_select(req, iovec, issue_flags); |
f3251183 PB |
3246 | if (ret) |
3247 | return ERR_PTR(ret); | |
3248 | iov_iter_init(iter, rw, iovec, 1, iovec->iov_len); | |
3249 | return NULL; | |
4d954c25 JA |
3250 | } |
3251 | ||
caa8fe6e | 3252 | ret = __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, &iovec, iter, |
89cd35c5 | 3253 | req->ctx->compat); |
caa8fe6e PB |
3254 | if (unlikely(ret < 0)) |
3255 | return ERR_PTR(ret); | |
3256 | return iovec; | |
2b188cc1 JA |
3257 | } |
3258 | ||
5e49c973 PB |
3259 | static inline int io_import_iovec(int rw, struct io_kiocb *req, |
3260 | struct iovec **iovec, struct io_rw_state *s, | |
3261 | unsigned int issue_flags) | |
3262 | { | |
caa8fe6e PB |
3263 | *iovec = __io_import_iovec(rw, req, s, issue_flags); |
3264 | if (unlikely(IS_ERR(*iovec))) | |
3265 | return PTR_ERR(*iovec); | |
5e49c973 | 3266 | |
5e49c973 | 3267 | iov_iter_save_state(&s->iter, &s->iter_state); |
caa8fe6e | 3268 | return 0; |
2b188cc1 JA |
3269 | } |
3270 | ||
0fef9483 JA |
3271 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3272 | { | |
5b09e37e | 3273 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3274 | } |
3275 | ||
31b51510 | 3276 | /* |
32960613 JA |
3277 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3278 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3279 | */ |
4017eb91 | 3280 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3281 | { |
4017eb91 JA |
3282 | struct kiocb *kiocb = &req->rw.kiocb; |
3283 | struct file *file = req->file; | |
32960613 JA |
3284 | ssize_t ret = 0; |
3285 | ||
3286 | /* | |
3287 | * Don't support polled IO through this interface, and we can't | |
3288 | * support non-blocking either. For the latter, this just causes | |
3289 | * the kiocb to be handled from an async context. | |
3290 | */ | |
3291 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3292 | return -EOPNOTSUPP; | |
35645ac3 PB |
3293 | if ((kiocb->ki_flags & IOCB_NOWAIT) && |
3294 | !(kiocb->ki_filp->f_flags & O_NONBLOCK)) | |
32960613 JA |
3295 | return -EAGAIN; |
3296 | ||
3297 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3298 | struct iovec iovec; |
32960613 JA |
3299 | ssize_t nr; |
3300 | ||
311ae9e1 PB |
3301 | if (!iov_iter_is_bvec(iter)) { |
3302 | iovec = iov_iter_iovec(iter); | |
3303 | } else { | |
4017eb91 JA |
3304 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3305 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3306 | } |
3307 | ||
32960613 JA |
3308 | if (rw == READ) { |
3309 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3310 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3311 | } else { |
3312 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3313 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3314 | } |
3315 | ||
3316 | if (nr < 0) { | |
3317 | if (!ret) | |
3318 | ret = nr; | |
3319 | break; | |
3320 | } | |
16c8d2df JA |
3321 | if (!iov_iter_is_bvec(iter)) { |
3322 | iov_iter_advance(iter, nr); | |
3323 | } else { | |
3324 | req->rw.len -= nr; | |
3325 | req->rw.addr += nr; | |
3326 | } | |
32960613 JA |
3327 | ret += nr; |
3328 | if (nr != iovec.iov_len) | |
3329 | break; | |
32960613 JA |
3330 | } |
3331 | ||
3332 | return ret; | |
3333 | } | |
3334 | ||
ff6165b2 JA |
3335 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3336 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3337 | { |
e8c2bc1f | 3338 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3339 | |
538941e2 | 3340 | memcpy(&rw->s.iter, iter, sizeof(*iter)); |
afb87658 | 3341 | rw->free_iovec = iovec; |
227c0c96 | 3342 | rw->bytes_done = 0; |
ff6165b2 | 3343 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3344 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3345 | return; |
b64e3444 | 3346 | if (!iovec) { |
ff6165b2 JA |
3347 | unsigned iov_off = 0; |
3348 | ||
538941e2 | 3349 | rw->s.iter.iov = rw->s.fast_iov; |
ff6165b2 JA |
3350 | if (iter->iov != fast_iov) { |
3351 | iov_off = iter->iov - fast_iov; | |
538941e2 | 3352 | rw->s.iter.iov += iov_off; |
ff6165b2 | 3353 | } |
538941e2 PB |
3354 | if (rw->s.fast_iov != fast_iov) |
3355 | memcpy(rw->s.fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3356 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3357 | } else { |
3358 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3359 | } |
3360 | } | |
3361 | ||
8d4af685 | 3362 | static inline bool io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3363 | { |
e8c2bc1f JA |
3364 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3365 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
d886e185 PB |
3366 | if (req->async_data) { |
3367 | req->flags |= REQ_F_ASYNC_DATA; | |
3368 | return false; | |
3369 | } | |
3370 | return true; | |
3d9932a8 XW |
3371 | } |
3372 | ||
ff6165b2 | 3373 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
c88598a9 | 3374 | struct io_rw_state *s, bool force) |
b7bb4f7d | 3375 | { |
26f0505a | 3376 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
74566df3 | 3377 | return 0; |
d886e185 | 3378 | if (!req_has_async_data(req)) { |
cd658695 JA |
3379 | struct io_async_rw *iorw; |
3380 | ||
6cb78689 | 3381 | if (io_alloc_async_data(req)) { |
6bf985dc | 3382 | kfree(iovec); |
5d204bcf | 3383 | return -ENOMEM; |
6bf985dc | 3384 | } |
b7bb4f7d | 3385 | |
c88598a9 | 3386 | io_req_map_rw(req, iovec, s->fast_iov, &s->iter); |
cd658695 JA |
3387 | iorw = req->async_data; |
3388 | /* we've copied and mapped the iter, ensure state is saved */ | |
538941e2 | 3389 | iov_iter_save_state(&iorw->s.iter, &iorw->s.iter_state); |
5d204bcf | 3390 | } |
b7bb4f7d | 3391 | return 0; |
f67676d1 JA |
3392 | } |
3393 | ||
73debe68 | 3394 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3395 | { |
e8c2bc1f | 3396 | struct io_async_rw *iorw = req->async_data; |
5e49c973 | 3397 | struct iovec *iov; |
847595de | 3398 | int ret; |
c3e330a4 | 3399 | |
51aac424 | 3400 | /* submission path, ->uring_lock should already be taken */ |
3b44b371 | 3401 | ret = io_import_iovec(rw, req, &iov, &iorw->s, 0); |
c3e330a4 PB |
3402 | if (unlikely(ret < 0)) |
3403 | return ret; | |
3404 | ||
ab0b196c PB |
3405 | iorw->bytes_done = 0; |
3406 | iorw->free_iovec = iov; | |
3407 | if (iov) | |
3408 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3409 | return 0; |
3410 | } | |
3411 | ||
73debe68 | 3412 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3413 | { |
3529d8c2 JA |
3414 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3415 | return -EBADF; | |
b9a6b8f9 | 3416 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3417 | } |
3418 | ||
c1dd91d1 | 3419 | /* |
ffdc8dab | 3420 | * This is our waitqueue callback handler, registered through __folio_lock_async() |
c1dd91d1 JA |
3421 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
3422 | * This gets called when the page is unlocked, and we generally expect that to | |
3423 | * happen when the page IO is completed and the page is now uptodate. This will | |
3424 | * queue a task_work based retry of the operation, attempting to copy the data | |
3425 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3426 | * do a thread based blocking retry of the operation. That's the unexpected | |
3427 | * slow path. | |
3428 | */ | |
bcf5a063 JA |
3429 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3430 | int sync, void *arg) | |
3431 | { | |
3432 | struct wait_page_queue *wpq; | |
3433 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3434 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3435 | |
3436 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3437 | ||
cdc8fcb4 LT |
3438 | if (!wake_page_match(wpq, key)) |
3439 | return 0; | |
3440 | ||
c8d317aa | 3441 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 | 3442 | list_del_init(&wait->entry); |
921b9054 | 3443 | io_req_task_queue(req); |
bcf5a063 JA |
3444 | return 1; |
3445 | } | |
3446 | ||
c1dd91d1 JA |
3447 | /* |
3448 | * This controls whether a given IO request should be armed for async page | |
3449 | * based retry. If we return false here, the request is handed to the async | |
3450 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3451 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3452 | * will either succeed because the page is now uptodate and unlocked, or it | |
3453 | * will register a callback when the page is unlocked at IO completion. Through | |
3454 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3455 | * That retry will attempt the buffered read again. The retry will generally | |
3456 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3457 | * async worker threads for a blocking retry. | |
3458 | */ | |
227c0c96 | 3459 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3460 | { |
e8c2bc1f JA |
3461 | struct io_async_rw *rw = req->async_data; |
3462 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3463 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3464 | |
bcf5a063 JA |
3465 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3466 | if (req->flags & REQ_F_NOWAIT) | |
3467 | return false; | |
f67676d1 | 3468 | |
227c0c96 | 3469 | /* Only for buffered IO */ |
3b2a4439 | 3470 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3471 | return false; |
3b2a4439 | 3472 | |
bcf5a063 JA |
3473 | /* |
3474 | * just use poll if we can, and don't attempt if the fs doesn't | |
3475 | * support callback based unlocks | |
3476 | */ | |
3477 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3478 | return false; | |
f67676d1 | 3479 | |
3b2a4439 JA |
3480 | wait->wait.func = io_async_buf_func; |
3481 | wait->wait.private = req; | |
3482 | wait->wait.flags = 0; | |
3483 | INIT_LIST_HEAD(&wait->wait.entry); | |
3484 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3485 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3486 | kiocb->ki_waitq = wait; |
3b2a4439 | 3487 | return true; |
bcf5a063 JA |
3488 | } |
3489 | ||
aeab9506 | 3490 | static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
bcf5a063 | 3491 | { |
607b6fb8 | 3492 | if (likely(req->file->f_op->read_iter)) |
bcf5a063 | 3493 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
2dd2111d | 3494 | else if (req->file->f_op->read) |
4017eb91 | 3495 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3496 | else |
3497 | return -EINVAL; | |
f67676d1 JA |
3498 | } |
3499 | ||
7db30437 ML |
3500 | static bool need_read_all(struct io_kiocb *req) |
3501 | { | |
3502 | return req->flags & REQ_F_ISREG || | |
3503 | S_ISBLK(file_inode(req->file)->i_mode); | |
3504 | } | |
3505 | ||
889fca73 | 3506 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 3507 | { |
607b6fb8 | 3508 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 3509 | struct iovec *iovec; |
9adbd45d | 3510 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 3511 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
d886e185 | 3512 | struct io_async_rw *rw; |
cd658695 | 3513 | ssize_t ret, ret2; |
ff6165b2 | 3514 | |
607b6fb8 PB |
3515 | if (!req_has_async_data(req)) { |
3516 | ret = io_import_iovec(READ, req, &iovec, s, issue_flags); | |
3517 | if (unlikely(ret < 0)) | |
3518 | return ret; | |
3519 | } else { | |
d886e185 | 3520 | rw = req->async_data; |
c88598a9 | 3521 | s = &rw->s; |
cd658695 JA |
3522 | /* |
3523 | * We come here from an earlier attempt, restore our state to | |
3524 | * match in case it doesn't. It's cheap enough that we don't | |
3525 | * need to make this conditional. | |
3526 | */ | |
c88598a9 | 3527 | iov_iter_restore(&s->iter, &s->iter_state); |
2846c481 | 3528 | iovec = NULL; |
2846c481 | 3529 | } |
c88598a9 | 3530 | req->result = iov_iter_count(&s->iter); |
2b188cc1 | 3531 | |
607b6fb8 PB |
3532 | if (force_nonblock) { |
3533 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 3534 | if (unlikely(!io_file_supports_nowait(req))) { |
607b6fb8 PB |
3535 | ret = io_setup_async_rw(req, iovec, s, true); |
3536 | return ret ?: -EAGAIN; | |
3537 | } | |
a88fc400 | 3538 | kiocb->ki_flags |= IOCB_NOWAIT; |
607b6fb8 PB |
3539 | } else { |
3540 | /* Ensure we clear previously set non-block flag */ | |
3541 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
6713e7a6 | 3542 | } |
9e645e11 | 3543 | |
cd658695 | 3544 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), req->result); |
5ea5dd45 PB |
3545 | if (unlikely(ret)) { |
3546 | kfree(iovec); | |
3547 | return ret; | |
3548 | } | |
2b188cc1 | 3549 | |
c88598a9 | 3550 | ret = io_iter_do_read(req, &s->iter); |
32960613 | 3551 | |
230d50d4 | 3552 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
6ad7f233 | 3553 | req->flags &= ~REQ_F_REISSUE; |
eefdf30f JA |
3554 | /* IOPOLL retry should happen for io-wq threads */ |
3555 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3556 | goto done; |
75c668cd PB |
3557 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3558 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3559 | goto done; |
f38c7e3a | 3560 | ret = 0; |
230d50d4 JA |
3561 | } else if (ret == -EIOCBQUEUED) { |
3562 | goto out_free; | |
f80a50a6 | 3563 | } else if (ret == req->result || ret <= 0 || !force_nonblock || |
7db30437 | 3564 | (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) { |
7335e3bf | 3565 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3566 | goto done; |
227c0c96 JA |
3567 | } |
3568 | ||
cd658695 JA |
3569 | /* |
3570 | * Don't depend on the iter state matching what was consumed, or being | |
3571 | * untouched in case of error. Restore it and we'll advance it | |
3572 | * manually if we need to. | |
3573 | */ | |
c88598a9 | 3574 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 3575 | |
c88598a9 | 3576 | ret2 = io_setup_async_rw(req, iovec, s, true); |
6bf985dc PB |
3577 | if (ret2) |
3578 | return ret2; | |
3579 | ||
fe1cdd55 | 3580 | iovec = NULL; |
e8c2bc1f | 3581 | rw = req->async_data; |
c88598a9 | 3582 | s = &rw->s; |
cd658695 JA |
3583 | /* |
3584 | * Now use our persistent iterator and state, if we aren't already. | |
3585 | * We've restored and mapped the iter to match. | |
3586 | */ | |
227c0c96 | 3587 | |
b23df91b | 3588 | do { |
cd658695 JA |
3589 | /* |
3590 | * We end up here because of a partial read, either from | |
3591 | * above or inside this loop. Advance the iter by the bytes | |
3592 | * that were consumed. | |
3593 | */ | |
c88598a9 PB |
3594 | iov_iter_advance(&s->iter, ret); |
3595 | if (!iov_iter_count(&s->iter)) | |
cd658695 | 3596 | break; |
b23df91b | 3597 | rw->bytes_done += ret; |
c88598a9 | 3598 | iov_iter_save_state(&s->iter, &s->iter_state); |
cd658695 | 3599 | |
b23df91b PB |
3600 | /* if we can retry, do so with the callbacks armed */ |
3601 | if (!io_rw_should_retry(req)) { | |
3602 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3603 | return -EAGAIN; | |
3604 | } | |
3605 | ||
3606 | /* | |
3607 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3608 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3609 | * desired page gets unlocked. We can also get a partial read | |
3610 | * here, and if we do, then just retry at the new offset. | |
3611 | */ | |
c88598a9 | 3612 | ret = io_iter_do_read(req, &s->iter); |
b23df91b PB |
3613 | if (ret == -EIOCBQUEUED) |
3614 | return 0; | |
227c0c96 | 3615 | /* we got some bytes, but not all. retry. */ |
b5b0ecb7 | 3616 | kiocb->ki_flags &= ~IOCB_WAITQ; |
c88598a9 | 3617 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 3618 | } while (ret > 0); |
227c0c96 | 3619 | done: |
2ea537ca | 3620 | kiocb_done(req, ret, issue_flags); |
fe1cdd55 PB |
3621 | out_free: |
3622 | /* it's faster to check here then delegate to kfree */ | |
3623 | if (iovec) | |
3624 | kfree(iovec); | |
5ea5dd45 | 3625 | return 0; |
2b188cc1 JA |
3626 | } |
3627 | ||
73debe68 | 3628 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3629 | { |
3529d8c2 JA |
3630 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3631 | return -EBADF; | |
3884b83d | 3632 | req->rw.kiocb.ki_hint = ki_hint_validate(file_write_hint(req->file)); |
b9a6b8f9 | 3633 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3634 | } |
3635 | ||
889fca73 | 3636 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 3637 | { |
607b6fb8 | 3638 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 3639 | struct iovec *iovec; |
9adbd45d | 3640 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 3641 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
cd658695 | 3642 | ssize_t ret, ret2; |
2b188cc1 | 3643 | |
607b6fb8 | 3644 | if (!req_has_async_data(req)) { |
5e49c973 PB |
3645 | ret = io_import_iovec(WRITE, req, &iovec, s, issue_flags); |
3646 | if (unlikely(ret < 0)) | |
2846c481 | 3647 | return ret; |
607b6fb8 PB |
3648 | } else { |
3649 | struct io_async_rw *rw = req->async_data; | |
3650 | ||
3651 | s = &rw->s; | |
3652 | iov_iter_restore(&s->iter, &s->iter_state); | |
2846c481 | 3653 | iovec = NULL; |
2846c481 | 3654 | } |
c88598a9 | 3655 | req->result = iov_iter_count(&s->iter); |
2b188cc1 | 3656 | |
607b6fb8 PB |
3657 | if (force_nonblock) { |
3658 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 3659 | if (unlikely(!io_file_supports_nowait(req))) |
607b6fb8 | 3660 | goto copy_iov; |
fd6c2e4c | 3661 | |
607b6fb8 PB |
3662 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3663 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3664 | (req->flags & REQ_F_ISREG)) | |
3665 | goto copy_iov; | |
31b51510 | 3666 | |
607b6fb8 PB |
3667 | kiocb->ki_flags |= IOCB_NOWAIT; |
3668 | } else { | |
3669 | /* Ensure we clear previously set non-block flag */ | |
3670 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
3671 | } | |
31b51510 | 3672 | |
cd658695 | 3673 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), req->result); |
fa15bafb PB |
3674 | if (unlikely(ret)) |
3675 | goto out_free; | |
4ed734b0 | 3676 | |
fa15bafb PB |
3677 | /* |
3678 | * Open-code file_start_write here to grab freeze protection, | |
3679 | * which will be released by another thread in | |
3680 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3681 | * released so that it doesn't complain about the held lock when | |
3682 | * we return to userspace. | |
3683 | */ | |
3684 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3685 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3686 | __sb_writers_release(file_inode(req->file)->i_sb, |
3687 | SB_FREEZE_WRITE); | |
3688 | } | |
3689 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3690 | |
35645ac3 | 3691 | if (likely(req->file->f_op->write_iter)) |
c88598a9 | 3692 | ret2 = call_write_iter(req->file, kiocb, &s->iter); |
2dd2111d | 3693 | else if (req->file->f_op->write) |
c88598a9 | 3694 | ret2 = loop_rw_iter(WRITE, req, &s->iter); |
2dd2111d GH |
3695 | else |
3696 | ret2 = -EINVAL; | |
4ed734b0 | 3697 | |
6ad7f233 PB |
3698 | if (req->flags & REQ_F_REISSUE) { |
3699 | req->flags &= ~REQ_F_REISSUE; | |
230d50d4 | 3700 | ret2 = -EAGAIN; |
6ad7f233 | 3701 | } |
230d50d4 | 3702 | |
fa15bafb PB |
3703 | /* |
3704 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3705 | * retry them without IOCB_NOWAIT. | |
3706 | */ | |
3707 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3708 | ret2 = -EAGAIN; | |
75c668cd PB |
3709 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3710 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3711 | goto done; |
fa15bafb | 3712 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f | 3713 | /* IOPOLL retry should happen for io-wq threads */ |
b10841c9 | 3714 | if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL)) |
eefdf30f | 3715 | goto copy_iov; |
355afaeb | 3716 | done: |
2ea537ca | 3717 | kiocb_done(req, ret2, issue_flags); |
fa15bafb | 3718 | } else { |
f67676d1 | 3719 | copy_iov: |
c88598a9 PB |
3720 | iov_iter_restore(&s->iter, &s->iter_state); |
3721 | ret = io_setup_async_rw(req, iovec, s, false); | |
6bf985dc | 3722 | return ret ?: -EAGAIN; |
2b188cc1 | 3723 | } |
31b51510 | 3724 | out_free: |
f261c168 | 3725 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3726 | if (iovec) |
6f2cc166 | 3727 | kfree(iovec); |
2b188cc1 JA |
3728 | return ret; |
3729 | } | |
3730 | ||
80a261fd JA |
3731 | static int io_renameat_prep(struct io_kiocb *req, |
3732 | const struct io_uring_sqe *sqe) | |
3733 | { | |
3734 | struct io_rename *ren = &req->rename; | |
3735 | const char __user *oldf, *newf; | |
3736 | ||
ed7eb259 JA |
3737 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3738 | return -EINVAL; | |
26578cda | 3739 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
ed7eb259 | 3740 | return -EINVAL; |
80a261fd JA |
3741 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3742 | return -EBADF; | |
3743 | ||
3744 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3745 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3746 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3747 | ren->new_dfd = READ_ONCE(sqe->len); | |
3748 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3749 | ||
3750 | ren->oldpath = getname(oldf); | |
3751 | if (IS_ERR(ren->oldpath)) | |
3752 | return PTR_ERR(ren->oldpath); | |
3753 | ||
3754 | ren->newpath = getname(newf); | |
3755 | if (IS_ERR(ren->newpath)) { | |
3756 | putname(ren->oldpath); | |
3757 | return PTR_ERR(ren->newpath); | |
3758 | } | |
3759 | ||
3760 | req->flags |= REQ_F_NEED_CLEANUP; | |
3761 | return 0; | |
3762 | } | |
3763 | ||
45d189c6 | 3764 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
3765 | { |
3766 | struct io_rename *ren = &req->rename; | |
3767 | int ret; | |
3768 | ||
45d189c6 | 3769 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
3770 | return -EAGAIN; |
3771 | ||
3772 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3773 | ren->newpath, ren->flags); | |
3774 | ||
3775 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3776 | if (ret < 0) | |
93d2bcd2 | 3777 | req_set_fail(req); |
80a261fd JA |
3778 | io_req_complete(req, ret); |
3779 | return 0; | |
3780 | } | |
3781 | ||
14a1143b JA |
3782 | static int io_unlinkat_prep(struct io_kiocb *req, |
3783 | const struct io_uring_sqe *sqe) | |
3784 | { | |
3785 | struct io_unlink *un = &req->unlink; | |
3786 | const char __user *fname; | |
3787 | ||
22634bc5 JA |
3788 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3789 | return -EINVAL; | |
26578cda PB |
3790 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
3791 | sqe->splice_fd_in) | |
22634bc5 | 3792 | return -EINVAL; |
14a1143b JA |
3793 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3794 | return -EBADF; | |
3795 | ||
3796 | un->dfd = READ_ONCE(sqe->fd); | |
3797 | ||
3798 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3799 | if (un->flags & ~AT_REMOVEDIR) | |
3800 | return -EINVAL; | |
3801 | ||
3802 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3803 | un->filename = getname(fname); | |
3804 | if (IS_ERR(un->filename)) | |
3805 | return PTR_ERR(un->filename); | |
3806 | ||
3807 | req->flags |= REQ_F_NEED_CLEANUP; | |
3808 | return 0; | |
3809 | } | |
3810 | ||
45d189c6 | 3811 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
3812 | { |
3813 | struct io_unlink *un = &req->unlink; | |
3814 | int ret; | |
3815 | ||
45d189c6 | 3816 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
3817 | return -EAGAIN; |
3818 | ||
3819 | if (un->flags & AT_REMOVEDIR) | |
3820 | ret = do_rmdir(un->dfd, un->filename); | |
3821 | else | |
3822 | ret = do_unlinkat(un->dfd, un->filename); | |
3823 | ||
3824 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3825 | if (ret < 0) | |
93d2bcd2 | 3826 | req_set_fail(req); |
14a1143b JA |
3827 | io_req_complete(req, ret); |
3828 | return 0; | |
3829 | } | |
3830 | ||
e34a02dc DK |
3831 | static int io_mkdirat_prep(struct io_kiocb *req, |
3832 | const struct io_uring_sqe *sqe) | |
3833 | { | |
3834 | struct io_mkdir *mkd = &req->mkdir; | |
3835 | const char __user *fname; | |
3836 | ||
3837 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3838 | return -EINVAL; | |
3839 | if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index || | |
3840 | sqe->splice_fd_in) | |
3841 | return -EINVAL; | |
3842 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3843 | return -EBADF; | |
3844 | ||
3845 | mkd->dfd = READ_ONCE(sqe->fd); | |
3846 | mkd->mode = READ_ONCE(sqe->len); | |
3847 | ||
3848 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3849 | mkd->filename = getname(fname); | |
3850 | if (IS_ERR(mkd->filename)) | |
3851 | return PTR_ERR(mkd->filename); | |
3852 | ||
3853 | req->flags |= REQ_F_NEED_CLEANUP; | |
3854 | return 0; | |
3855 | } | |
3856 | ||
04f34081 | 3857 | static int io_mkdirat(struct io_kiocb *req, unsigned int issue_flags) |
e34a02dc DK |
3858 | { |
3859 | struct io_mkdir *mkd = &req->mkdir; | |
3860 | int ret; | |
3861 | ||
3862 | if (issue_flags & IO_URING_F_NONBLOCK) | |
3863 | return -EAGAIN; | |
3864 | ||
3865 | ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode); | |
3866 | ||
3867 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3868 | if (ret < 0) | |
3869 | req_set_fail(req); | |
3870 | io_req_complete(req, ret); | |
3871 | return 0; | |
3872 | } | |
3873 | ||
7a8721f8 DK |
3874 | static int io_symlinkat_prep(struct io_kiocb *req, |
3875 | const struct io_uring_sqe *sqe) | |
3876 | { | |
3877 | struct io_symlink *sl = &req->symlink; | |
3878 | const char __user *oldpath, *newpath; | |
3879 | ||
3880 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3881 | return -EINVAL; | |
3882 | if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index || | |
3883 | sqe->splice_fd_in) | |
3884 | return -EINVAL; | |
3885 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3886 | return -EBADF; | |
3887 | ||
3888 | sl->new_dfd = READ_ONCE(sqe->fd); | |
3889 | oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3890 | newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3891 | ||
3892 | sl->oldpath = getname(oldpath); | |
3893 | if (IS_ERR(sl->oldpath)) | |
3894 | return PTR_ERR(sl->oldpath); | |
3895 | ||
3896 | sl->newpath = getname(newpath); | |
3897 | if (IS_ERR(sl->newpath)) { | |
3898 | putname(sl->oldpath); | |
3899 | return PTR_ERR(sl->newpath); | |
3900 | } | |
3901 | ||
3902 | req->flags |= REQ_F_NEED_CLEANUP; | |
3903 | return 0; | |
3904 | } | |
3905 | ||
04f34081 | 3906 | static int io_symlinkat(struct io_kiocb *req, unsigned int issue_flags) |
7a8721f8 DK |
3907 | { |
3908 | struct io_symlink *sl = &req->symlink; | |
3909 | int ret; | |
3910 | ||
3911 | if (issue_flags & IO_URING_F_NONBLOCK) | |
3912 | return -EAGAIN; | |
3913 | ||
3914 | ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath); | |
3915 | ||
3916 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3917 | if (ret < 0) | |
3918 | req_set_fail(req); | |
3919 | io_req_complete(req, ret); | |
3920 | return 0; | |
3921 | } | |
3922 | ||
cf30da90 DK |
3923 | static int io_linkat_prep(struct io_kiocb *req, |
3924 | const struct io_uring_sqe *sqe) | |
3925 | { | |
3926 | struct io_hardlink *lnk = &req->hardlink; | |
3927 | const char __user *oldf, *newf; | |
3928 | ||
3929 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3930 | return -EINVAL; | |
3931 | if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) | |
3932 | return -EINVAL; | |
3933 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3934 | return -EBADF; | |
3935 | ||
3936 | lnk->old_dfd = READ_ONCE(sqe->fd); | |
3937 | lnk->new_dfd = READ_ONCE(sqe->len); | |
3938 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3939 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3940 | lnk->flags = READ_ONCE(sqe->hardlink_flags); | |
3941 | ||
3942 | lnk->oldpath = getname(oldf); | |
3943 | if (IS_ERR(lnk->oldpath)) | |
3944 | return PTR_ERR(lnk->oldpath); | |
3945 | ||
3946 | lnk->newpath = getname(newf); | |
3947 | if (IS_ERR(lnk->newpath)) { | |
3948 | putname(lnk->oldpath); | |
3949 | return PTR_ERR(lnk->newpath); | |
3950 | } | |
3951 | ||
3952 | req->flags |= REQ_F_NEED_CLEANUP; | |
3953 | return 0; | |
3954 | } | |
3955 | ||
04f34081 | 3956 | static int io_linkat(struct io_kiocb *req, unsigned int issue_flags) |
cf30da90 DK |
3957 | { |
3958 | struct io_hardlink *lnk = &req->hardlink; | |
3959 | int ret; | |
3960 | ||
3961 | if (issue_flags & IO_URING_F_NONBLOCK) | |
3962 | return -EAGAIN; | |
3963 | ||
3964 | ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd, | |
3965 | lnk->newpath, lnk->flags); | |
3966 | ||
3967 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3968 | if (ret < 0) | |
3969 | req_set_fail(req); | |
3970 | io_req_complete(req, ret); | |
3971 | return 0; | |
3972 | } | |
3973 | ||
36f4fa68 JA |
3974 | static int io_shutdown_prep(struct io_kiocb *req, |
3975 | const struct io_uring_sqe *sqe) | |
3976 | { | |
3977 | #if defined(CONFIG_NET) | |
3978 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3979 | return -EINVAL; | |
26578cda PB |
3980 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
3981 | sqe->buf_index || sqe->splice_fd_in)) | |
36f4fa68 JA |
3982 | return -EINVAL; |
3983 | ||
3984 | req->shutdown.how = READ_ONCE(sqe->len); | |
3985 | return 0; | |
3986 | #else | |
3987 | return -EOPNOTSUPP; | |
3988 | #endif | |
3989 | } | |
3990 | ||
45d189c6 | 3991 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
3992 | { |
3993 | #if defined(CONFIG_NET) | |
3994 | struct socket *sock; | |
3995 | int ret; | |
3996 | ||
45d189c6 | 3997 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
3998 | return -EAGAIN; |
3999 | ||
48aba79b | 4000 | sock = sock_from_file(req->file); |
36f4fa68 | 4001 | if (unlikely(!sock)) |
48aba79b | 4002 | return -ENOTSOCK; |
36f4fa68 JA |
4003 | |
4004 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d | 4005 | if (ret < 0) |
93d2bcd2 | 4006 | req_set_fail(req); |
36f4fa68 JA |
4007 | io_req_complete(req, ret); |
4008 | return 0; | |
4009 | #else | |
4010 | return -EOPNOTSUPP; | |
4011 | #endif | |
4012 | } | |
4013 | ||
f2a8d5c7 PB |
4014 | static int __io_splice_prep(struct io_kiocb *req, |
4015 | const struct io_uring_sqe *sqe) | |
7d67af2c | 4016 | { |
fe7e3257 | 4017 | struct io_splice *sp = &req->splice; |
7d67af2c | 4018 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
7d67af2c | 4019 | |
3232dd02 PB |
4020 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4021 | return -EINVAL; | |
7d67af2c PB |
4022 | |
4023 | sp->file_in = NULL; | |
7d67af2c PB |
4024 | sp->len = READ_ONCE(sqe->len); |
4025 | sp->flags = READ_ONCE(sqe->splice_flags); | |
4026 | ||
4027 | if (unlikely(sp->flags & ~valid_flags)) | |
4028 | return -EINVAL; | |
4029 | ||
62906e89 | 4030 | sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in), |
8371adf5 PB |
4031 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
4032 | if (!sp->file_in) | |
4033 | return -EBADF; | |
7d67af2c | 4034 | req->flags |= REQ_F_NEED_CLEANUP; |
7d67af2c PB |
4035 | return 0; |
4036 | } | |
4037 | ||
f2a8d5c7 PB |
4038 | static int io_tee_prep(struct io_kiocb *req, |
4039 | const struct io_uring_sqe *sqe) | |
4040 | { | |
4041 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
4042 | return -EINVAL; | |
4043 | return __io_splice_prep(req, sqe); | |
4044 | } | |
4045 | ||
45d189c6 | 4046 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
4047 | { |
4048 | struct io_splice *sp = &req->splice; | |
4049 | struct file *in = sp->file_in; | |
4050 | struct file *out = sp->file_out; | |
4051 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
4052 | long ret = 0; | |
4053 | ||
45d189c6 | 4054 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
4055 | return -EAGAIN; |
4056 | if (sp->len) | |
4057 | ret = do_tee(in, out, sp->len, flags); | |
4058 | ||
e1d767f0 PB |
4059 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4060 | io_put_file(in); | |
f2a8d5c7 PB |
4061 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4062 | ||
f2a8d5c7 | 4063 | if (ret != sp->len) |
93d2bcd2 | 4064 | req_set_fail(req); |
e1e16097 | 4065 | io_req_complete(req, ret); |
f2a8d5c7 PB |
4066 | return 0; |
4067 | } | |
4068 | ||
4069 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4070 | { | |
fe7e3257 | 4071 | struct io_splice *sp = &req->splice; |
f2a8d5c7 PB |
4072 | |
4073 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
4074 | sp->off_out = READ_ONCE(sqe->off); | |
4075 | return __io_splice_prep(req, sqe); | |
4076 | } | |
4077 | ||
45d189c6 | 4078 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
4079 | { |
4080 | struct io_splice *sp = &req->splice; | |
4081 | struct file *in = sp->file_in; | |
4082 | struct file *out = sp->file_out; | |
4083 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
4084 | loff_t *poff_in, *poff_out; | |
c9687426 | 4085 | long ret = 0; |
7d67af2c | 4086 | |
45d189c6 | 4087 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 4088 | return -EAGAIN; |
7d67af2c PB |
4089 | |
4090 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
4091 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 4092 | |
948a7749 | 4093 | if (sp->len) |
c9687426 | 4094 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c | 4095 | |
e1d767f0 PB |
4096 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4097 | io_put_file(in); | |
7d67af2c PB |
4098 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4099 | ||
7d67af2c | 4100 | if (ret != sp->len) |
93d2bcd2 | 4101 | req_set_fail(req); |
e1e16097 | 4102 | io_req_complete(req, ret); |
7d67af2c PB |
4103 | return 0; |
4104 | } | |
4105 | ||
2b188cc1 JA |
4106 | /* |
4107 | * IORING_OP_NOP just posts a completion event, nothing else. | |
4108 | */ | |
889fca73 | 4109 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
4110 | { |
4111 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 4112 | |
def596e9 JA |
4113 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4114 | return -EINVAL; | |
4115 | ||
889fca73 | 4116 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
4117 | return 0; |
4118 | } | |
4119 | ||
1155c76a | 4120 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 4121 | { |
6b06314c | 4122 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 4123 | |
09bb8394 JA |
4124 | if (!req->file) |
4125 | return -EBADF; | |
c992fe29 | 4126 | |
6b06314c | 4127 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 4128 | return -EINVAL; |
26578cda PB |
4129 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4130 | sqe->splice_fd_in)) | |
c992fe29 CH |
4131 | return -EINVAL; |
4132 | ||
8ed8d3c3 JA |
4133 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
4134 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
4135 | return -EINVAL; | |
4136 | ||
4137 | req->sync.off = READ_ONCE(sqe->off); | |
4138 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
4139 | return 0; |
4140 | } | |
4141 | ||
45d189c6 | 4142 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4143 | { |
8ed8d3c3 | 4144 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
4145 | int ret; |
4146 | ||
ac45abc0 | 4147 | /* fsync always requires a blocking context */ |
45d189c6 | 4148 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4149 | return -EAGAIN; |
4150 | ||
9adbd45d | 4151 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
4152 | end > 0 ? end : LLONG_MAX, |
4153 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
4154 | if (ret < 0) | |
93d2bcd2 | 4155 | req_set_fail(req); |
e1e16097 | 4156 | io_req_complete(req, ret); |
c992fe29 CH |
4157 | return 0; |
4158 | } | |
4159 | ||
d63d1b5e JA |
4160 | static int io_fallocate_prep(struct io_kiocb *req, |
4161 | const struct io_uring_sqe *sqe) | |
4162 | { | |
26578cda PB |
4163 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
4164 | sqe->splice_fd_in) | |
d63d1b5e | 4165 | return -EINVAL; |
3232dd02 PB |
4166 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4167 | return -EINVAL; | |
d63d1b5e JA |
4168 | |
4169 | req->sync.off = READ_ONCE(sqe->off); | |
4170 | req->sync.len = READ_ONCE(sqe->addr); | |
4171 | req->sync.mode = READ_ONCE(sqe->len); | |
4172 | return 0; | |
4173 | } | |
4174 | ||
45d189c6 | 4175 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 4176 | { |
ac45abc0 PB |
4177 | int ret; |
4178 | ||
d63d1b5e | 4179 | /* fallocate always requiring blocking context */ |
45d189c6 | 4180 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 4181 | return -EAGAIN; |
ac45abc0 PB |
4182 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
4183 | req->sync.len); | |
ac45abc0 | 4184 | if (ret < 0) |
93d2bcd2 | 4185 | req_set_fail(req); |
e1e16097 | 4186 | io_req_complete(req, ret); |
5d17b4a4 JA |
4187 | return 0; |
4188 | } | |
4189 | ||
ec65fea5 | 4190 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 4191 | { |
f8748881 | 4192 | const char __user *fname; |
15b71abe | 4193 | int ret; |
b7bb4f7d | 4194 | |
d3fddf6d PB |
4195 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4196 | return -EINVAL; | |
b9445598 | 4197 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 4198 | return -EINVAL; |
ec65fea5 | 4199 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 4200 | return -EBADF; |
03b1230c | 4201 | |
ec65fea5 PB |
4202 | /* open.how should be already initialised */ |
4203 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 4204 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 4205 | |
25e72d10 PB |
4206 | req->open.dfd = READ_ONCE(sqe->fd); |
4207 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 4208 | req->open.filename = getname(fname); |
15b71abe JA |
4209 | if (IS_ERR(req->open.filename)) { |
4210 | ret = PTR_ERR(req->open.filename); | |
4211 | req->open.filename = NULL; | |
4212 | return ret; | |
4213 | } | |
b9445598 PB |
4214 | |
4215 | req->open.file_slot = READ_ONCE(sqe->file_index); | |
4216 | if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) | |
4217 | return -EINVAL; | |
4218 | ||
4022e7af | 4219 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 4220 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 4221 | return 0; |
03b1230c JA |
4222 | } |
4223 | ||
ec65fea5 PB |
4224 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4225 | { | |
d3fddf6d PB |
4226 | u64 mode = READ_ONCE(sqe->len); |
4227 | u64 flags = READ_ONCE(sqe->open_flags); | |
ec65fea5 | 4228 | |
ec65fea5 PB |
4229 | req->open.how = build_open_how(flags, mode); |
4230 | return __io_openat_prep(req, sqe); | |
4231 | } | |
4232 | ||
cebdb986 | 4233 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 4234 | { |
cebdb986 | 4235 | struct open_how __user *how; |
cebdb986 | 4236 | size_t len; |
0fa03c62 JA |
4237 | int ret; |
4238 | ||
cebdb986 JA |
4239 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4240 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
4241 | if (len < OPEN_HOW_SIZE_VER0) |
4242 | return -EINVAL; | |
3529d8c2 | 4243 | |
cebdb986 JA |
4244 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
4245 | len); | |
4246 | if (ret) | |
4247 | return ret; | |
3529d8c2 | 4248 | |
ec65fea5 | 4249 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
4250 | } |
4251 | ||
45d189c6 | 4252 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
4253 | { |
4254 | struct open_flags op; | |
15b71abe | 4255 | struct file *file; |
b9445598 PB |
4256 | bool resolve_nonblock, nonblock_set; |
4257 | bool fixed = !!req->open.file_slot; | |
15b71abe JA |
4258 | int ret; |
4259 | ||
cebdb986 | 4260 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
4261 | if (ret) |
4262 | goto err; | |
3a81fd02 JA |
4263 | nonblock_set = op.open_flag & O_NONBLOCK; |
4264 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 4265 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
4266 | /* |
4267 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
4268 | * it'll always -EAGAIN | |
4269 | */ | |
4270 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
4271 | return -EAGAIN; | |
4272 | op.lookup_flags |= LOOKUP_CACHED; | |
4273 | op.open_flag |= O_NONBLOCK; | |
4274 | } | |
15b71abe | 4275 | |
b9445598 PB |
4276 | if (!fixed) { |
4277 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); | |
4278 | if (ret < 0) | |
4279 | goto err; | |
4280 | } | |
15b71abe JA |
4281 | |
4282 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
12dcb58a | 4283 | if (IS_ERR(file)) { |
944d1444 | 4284 | /* |
12dcb58a PB |
4285 | * We could hang on to this 'fd' on retrying, but seems like |
4286 | * marginal gain for something that is now known to be a slower | |
4287 | * path. So just put it, and we'll get a new one when we retry. | |
944d1444 | 4288 | */ |
b9445598 PB |
4289 | if (!fixed) |
4290 | put_unused_fd(ret); | |
3a81fd02 | 4291 | |
15b71abe | 4292 | ret = PTR_ERR(file); |
12dcb58a PB |
4293 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
4294 | if (ret == -EAGAIN && | |
4295 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) | |
4296 | return -EAGAIN; | |
4297 | goto err; | |
15b71abe | 4298 | } |
12dcb58a PB |
4299 | |
4300 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) | |
4301 | file->f_flags &= ~O_NONBLOCK; | |
4302 | fsnotify_open(file); | |
b9445598 PB |
4303 | |
4304 | if (!fixed) | |
4305 | fd_install(ret, file); | |
4306 | else | |
4307 | ret = io_install_fixed_file(req, file, issue_flags, | |
4308 | req->open.file_slot - 1); | |
15b71abe JA |
4309 | err: |
4310 | putname(req->open.filename); | |
8fef80bf | 4311 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe | 4312 | if (ret < 0) |
93d2bcd2 | 4313 | req_set_fail(req); |
0bdf3398 | 4314 | __io_req_complete(req, issue_flags, ret, 0); |
15b71abe JA |
4315 | return 0; |
4316 | } | |
4317 | ||
45d189c6 | 4318 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 4319 | { |
e45cff58 | 4320 | return io_openat2(req, issue_flags); |
cebdb986 JA |
4321 | } |
4322 | ||
067524e9 JA |
4323 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4324 | const struct io_uring_sqe *sqe) | |
4325 | { | |
4326 | struct io_provide_buf *p = &req->pbuf; | |
4327 | u64 tmp; | |
4328 | ||
26578cda PB |
4329 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
4330 | sqe->splice_fd_in) | |
067524e9 JA |
4331 | return -EINVAL; |
4332 | ||
4333 | tmp = READ_ONCE(sqe->fd); | |
4334 | if (!tmp || tmp > USHRT_MAX) | |
4335 | return -EINVAL; | |
4336 | ||
4337 | memset(p, 0, sizeof(*p)); | |
4338 | p->nbufs = tmp; | |
4339 | p->bgid = READ_ONCE(sqe->buf_group); | |
4340 | return 0; | |
4341 | } | |
4342 | ||
4343 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
4344 | int bgid, unsigned nbufs) | |
4345 | { | |
4346 | unsigned i = 0; | |
4347 | ||
4348 | /* shouldn't happen */ | |
4349 | if (!nbufs) | |
4350 | return 0; | |
4351 | ||
4352 | /* the head kbuf is the list itself */ | |
4353 | while (!list_empty(&buf->list)) { | |
4354 | struct io_buffer *nxt; | |
4355 | ||
4356 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
4357 | list_del(&nxt->list); | |
4358 | kfree(nxt); | |
4359 | if (++i == nbufs) | |
4360 | return i; | |
4361 | } | |
4362 | i++; | |
4363 | kfree(buf); | |
9e15c3a0 | 4364 | xa_erase(&ctx->io_buffers, bgid); |
067524e9 JA |
4365 | |
4366 | return i; | |
4367 | } | |
4368 | ||
889fca73 | 4369 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
4370 | { |
4371 | struct io_provide_buf *p = &req->pbuf; | |
4372 | struct io_ring_ctx *ctx = req->ctx; | |
4373 | struct io_buffer *head; | |
4374 | int ret = 0; | |
3b44b371 | 4375 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
067524e9 | 4376 | |
3b44b371 | 4377 | io_ring_submit_lock(ctx, needs_lock); |
067524e9 JA |
4378 | |
4379 | lockdep_assert_held(&ctx->uring_lock); | |
4380 | ||
4381 | ret = -ENOENT; | |
9e15c3a0 | 4382 | head = xa_load(&ctx->io_buffers, p->bgid); |
067524e9 JA |
4383 | if (head) |
4384 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 | 4385 | if (ret < 0) |
93d2bcd2 | 4386 | req_set_fail(req); |
067524e9 | 4387 | |
9fb8cb49 PB |
4388 | /* complete before unlock, IOPOLL may need the lock */ |
4389 | __io_req_complete(req, issue_flags, ret, 0); | |
3b44b371 | 4390 | io_ring_submit_unlock(ctx, needs_lock); |
067524e9 JA |
4391 | return 0; |
4392 | } | |
4393 | ||
ddf0322d JA |
4394 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4395 | const struct io_uring_sqe *sqe) | |
4396 | { | |
38134ada | 4397 | unsigned long size, tmp_check; |
ddf0322d JA |
4398 | struct io_provide_buf *p = &req->pbuf; |
4399 | u64 tmp; | |
4400 | ||
26578cda | 4401 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
ddf0322d JA |
4402 | return -EINVAL; |
4403 | ||
4404 | tmp = READ_ONCE(sqe->fd); | |
4405 | if (!tmp || tmp > USHRT_MAX) | |
4406 | return -E2BIG; | |
4407 | p->nbufs = tmp; | |
4408 | p->addr = READ_ONCE(sqe->addr); | |
4409 | p->len = READ_ONCE(sqe->len); | |
4410 | ||
38134ada PB |
4411 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
4412 | &size)) | |
4413 | return -EOVERFLOW; | |
4414 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) | |
4415 | return -EOVERFLOW; | |
4416 | ||
d81269fe PB |
4417 | size = (unsigned long)p->len * p->nbufs; |
4418 | if (!access_ok(u64_to_user_ptr(p->addr), size)) | |
ddf0322d JA |
4419 | return -EFAULT; |
4420 | ||
4421 | p->bgid = READ_ONCE(sqe->buf_group); | |
4422 | tmp = READ_ONCE(sqe->off); | |
4423 | if (tmp > USHRT_MAX) | |
4424 | return -E2BIG; | |
4425 | p->bid = tmp; | |
4426 | return 0; | |
4427 | } | |
4428 | ||
4429 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
4430 | { | |
4431 | struct io_buffer *buf; | |
4432 | u64 addr = pbuf->addr; | |
4433 | int i, bid = pbuf->bid; | |
4434 | ||
4435 | for (i = 0; i < pbuf->nbufs; i++) { | |
9990da93 | 4436 | buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT); |
ddf0322d JA |
4437 | if (!buf) |
4438 | break; | |
4439 | ||
4440 | buf->addr = addr; | |
d1f82808 | 4441 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
ddf0322d JA |
4442 | buf->bid = bid; |
4443 | addr += pbuf->len; | |
4444 | bid++; | |
4445 | if (!*head) { | |
4446 | INIT_LIST_HEAD(&buf->list); | |
4447 | *head = buf; | |
4448 | } else { | |
4449 | list_add_tail(&buf->list, &(*head)->list); | |
4450 | } | |
4451 | } | |
4452 | ||
4453 | return i ? i : -ENOMEM; | |
4454 | } | |
4455 | ||
889fca73 | 4456 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4457 | { |
4458 | struct io_provide_buf *p = &req->pbuf; | |
4459 | struct io_ring_ctx *ctx = req->ctx; | |
4460 | struct io_buffer *head, *list; | |
4461 | int ret = 0; | |
3b44b371 | 4462 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
ddf0322d | 4463 | |
3b44b371 | 4464 | io_ring_submit_lock(ctx, needs_lock); |
ddf0322d JA |
4465 | |
4466 | lockdep_assert_held(&ctx->uring_lock); | |
4467 | ||
9e15c3a0 | 4468 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
ddf0322d JA |
4469 | |
4470 | ret = io_add_buffers(p, &head); | |
9e15c3a0 JA |
4471 | if (ret >= 0 && !list) { |
4472 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); | |
4473 | if (ret < 0) | |
067524e9 | 4474 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d | 4475 | } |
ddf0322d | 4476 | if (ret < 0) |
93d2bcd2 | 4477 | req_set_fail(req); |
9fb8cb49 PB |
4478 | /* complete before unlock, IOPOLL may need the lock */ |
4479 | __io_req_complete(req, issue_flags, ret, 0); | |
3b44b371 | 4480 | io_ring_submit_unlock(ctx, needs_lock); |
ddf0322d | 4481 | return 0; |
cebdb986 JA |
4482 | } |
4483 | ||
3e4827b0 JA |
4484 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4485 | const struct io_uring_sqe *sqe) | |
4486 | { | |
4487 | #if defined(CONFIG_EPOLL) | |
26578cda | 4488 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
3e4827b0 | 4489 | return -EINVAL; |
2d74d042 | 4490 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4491 | return -EINVAL; |
3e4827b0 JA |
4492 | |
4493 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4494 | req->epoll.op = READ_ONCE(sqe->len); | |
4495 | req->epoll.fd = READ_ONCE(sqe->off); | |
4496 | ||
4497 | if (ep_op_has_event(req->epoll.op)) { | |
4498 | struct epoll_event __user *ev; | |
4499 | ||
4500 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4501 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4502 | return -EFAULT; | |
4503 | } | |
4504 | ||
4505 | return 0; | |
4506 | #else | |
4507 | return -EOPNOTSUPP; | |
4508 | #endif | |
4509 | } | |
4510 | ||
889fca73 | 4511 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4512 | { |
4513 | #if defined(CONFIG_EPOLL) | |
4514 | struct io_epoll *ie = &req->epoll; | |
4515 | int ret; | |
45d189c6 | 4516 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4517 | |
4518 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4519 | if (force_nonblock && ret == -EAGAIN) | |
4520 | return -EAGAIN; | |
4521 | ||
4522 | if (ret < 0) | |
93d2bcd2 | 4523 | req_set_fail(req); |
889fca73 | 4524 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4525 | return 0; |
4526 | #else | |
4527 | return -EOPNOTSUPP; | |
4528 | #endif | |
4529 | } | |
4530 | ||
c1ca757b JA |
4531 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4532 | { | |
4533 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
26578cda | 4534 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
c1ca757b | 4535 | return -EINVAL; |
3232dd02 PB |
4536 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4537 | return -EINVAL; | |
c1ca757b JA |
4538 | |
4539 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4540 | req->madvise.len = READ_ONCE(sqe->len); | |
4541 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4542 | return 0; | |
4543 | #else | |
4544 | return -EOPNOTSUPP; | |
4545 | #endif | |
4546 | } | |
4547 | ||
45d189c6 | 4548 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4549 | { |
4550 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4551 | struct io_madvise *ma = &req->madvise; | |
4552 | int ret; | |
4553 | ||
45d189c6 | 4554 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4555 | return -EAGAIN; |
4556 | ||
0726b01e | 4557 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b | 4558 | if (ret < 0) |
93d2bcd2 | 4559 | req_set_fail(req); |
e1e16097 | 4560 | io_req_complete(req, ret); |
c1ca757b JA |
4561 | return 0; |
4562 | #else | |
4563 | return -EOPNOTSUPP; | |
4564 | #endif | |
4565 | } | |
4566 | ||
4840e418 JA |
4567 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4568 | { | |
26578cda | 4569 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
4840e418 | 4570 | return -EINVAL; |
3232dd02 PB |
4571 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4572 | return -EINVAL; | |
4840e418 JA |
4573 | |
4574 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4575 | req->fadvise.len = READ_ONCE(sqe->len); | |
4576 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4577 | return 0; | |
4578 | } | |
4579 | ||
45d189c6 | 4580 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4581 | { |
4582 | struct io_fadvise *fa = &req->fadvise; | |
4583 | int ret; | |
4584 | ||
45d189c6 | 4585 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4586 | switch (fa->advice) { |
4587 | case POSIX_FADV_NORMAL: | |
4588 | case POSIX_FADV_RANDOM: | |
4589 | case POSIX_FADV_SEQUENTIAL: | |
4590 | break; | |
4591 | default: | |
4592 | return -EAGAIN; | |
4593 | } | |
4594 | } | |
4840e418 JA |
4595 | |
4596 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4597 | if (ret < 0) | |
93d2bcd2 | 4598 | req_set_fail(req); |
0bdf3398 | 4599 | __io_req_complete(req, issue_flags, ret, 0); |
4840e418 JA |
4600 | return 0; |
4601 | } | |
4602 | ||
eddc7ef5 JA |
4603 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4604 | { | |
2d74d042 | 4605 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4606 | return -EINVAL; |
26578cda | 4607 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
eddc7ef5 | 4608 | return -EINVAL; |
9c280f90 | 4609 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4610 | return -EBADF; |
eddc7ef5 | 4611 | |
1d9e1288 BM |
4612 | req->statx.dfd = READ_ONCE(sqe->fd); |
4613 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4614 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4615 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4616 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4617 | |
4618 | return 0; | |
4619 | } | |
4620 | ||
45d189c6 | 4621 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4622 | { |
1d9e1288 | 4623 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4624 | int ret; |
4625 | ||
59d70013 | 4626 | if (issue_flags & IO_URING_F_NONBLOCK) |
eddc7ef5 JA |
4627 | return -EAGAIN; |
4628 | ||
e62753e4 BM |
4629 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4630 | ctx->buffer); | |
eddc7ef5 | 4631 | |
eddc7ef5 | 4632 | if (ret < 0) |
93d2bcd2 | 4633 | req_set_fail(req); |
e1e16097 | 4634 | io_req_complete(req, ret); |
eddc7ef5 JA |
4635 | return 0; |
4636 | } | |
4637 | ||
b5dba59e JA |
4638 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4639 | { | |
14587a46 | 4640 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4641 | return -EINVAL; |
b5dba59e | 4642 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
7df778be | 4643 | sqe->rw_flags || sqe->buf_index) |
b5dba59e | 4644 | return -EINVAL; |
9c280f90 | 4645 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4646 | return -EBADF; |
b5dba59e JA |
4647 | |
4648 | req->close.fd = READ_ONCE(sqe->fd); | |
7df778be PB |
4649 | req->close.file_slot = READ_ONCE(sqe->file_index); |
4650 | if (req->close.file_slot && req->close.fd) | |
4651 | return -EINVAL; | |
4652 | ||
b5dba59e | 4653 | return 0; |
b5dba59e JA |
4654 | } |
4655 | ||
889fca73 | 4656 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4657 | { |
9eac1904 | 4658 | struct files_struct *files = current->files; |
3af73b28 | 4659 | struct io_close *close = &req->close; |
9eac1904 | 4660 | struct fdtable *fdt; |
a1fde923 PB |
4661 | struct file *file = NULL; |
4662 | int ret = -EBADF; | |
b5dba59e | 4663 | |
7df778be PB |
4664 | if (req->close.file_slot) { |
4665 | ret = io_close_fixed(req, issue_flags); | |
4666 | goto err; | |
4667 | } | |
4668 | ||
9eac1904 JA |
4669 | spin_lock(&files->file_lock); |
4670 | fdt = files_fdtable(files); | |
4671 | if (close->fd >= fdt->max_fds) { | |
4672 | spin_unlock(&files->file_lock); | |
4673 | goto err; | |
4674 | } | |
4675 | file = fdt->fd[close->fd]; | |
a1fde923 | 4676 | if (!file || file->f_op == &io_uring_fops) { |
9eac1904 JA |
4677 | spin_unlock(&files->file_lock); |
4678 | file = NULL; | |
4679 | goto err; | |
3af73b28 | 4680 | } |
b5dba59e JA |
4681 | |
4682 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 4683 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 4684 | spin_unlock(&files->file_lock); |
0bf0eefd | 4685 | return -EAGAIN; |
a2100672 | 4686 | } |
b5dba59e | 4687 | |
9eac1904 JA |
4688 | ret = __close_fd_get_file(close->fd, &file); |
4689 | spin_unlock(&files->file_lock); | |
4690 | if (ret < 0) { | |
4691 | if (ret == -ENOENT) | |
4692 | ret = -EBADF; | |
4693 | goto err; | |
4694 | } | |
4695 | ||
3af73b28 | 4696 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4697 | ret = filp_close(file, current->files); |
4698 | err: | |
3af73b28 | 4699 | if (ret < 0) |
93d2bcd2 | 4700 | req_set_fail(req); |
9eac1904 JA |
4701 | if (file) |
4702 | fput(file); | |
889fca73 | 4703 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 4704 | return 0; |
b5dba59e JA |
4705 | } |
4706 | ||
1155c76a | 4707 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4708 | { |
4709 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 4710 | |
5d17b4a4 JA |
4711 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4712 | return -EINVAL; | |
26578cda PB |
4713 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4714 | sqe->splice_fd_in)) | |
5d17b4a4 JA |
4715 | return -EINVAL; |
4716 | ||
8ed8d3c3 JA |
4717 | req->sync.off = READ_ONCE(sqe->off); |
4718 | req->sync.len = READ_ONCE(sqe->len); | |
4719 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4720 | return 0; |
4721 | } | |
4722 | ||
45d189c6 | 4723 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4724 | { |
8ed8d3c3 JA |
4725 | int ret; |
4726 | ||
ac45abc0 | 4727 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 4728 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4729 | return -EAGAIN; |
4730 | ||
9adbd45d | 4731 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4732 | req->sync.flags); |
4733 | if (ret < 0) | |
93d2bcd2 | 4734 | req_set_fail(req); |
e1e16097 | 4735 | io_req_complete(req, ret); |
5d17b4a4 JA |
4736 | return 0; |
4737 | } | |
4738 | ||
469956e8 | 4739 | #if defined(CONFIG_NET) |
02d27d89 PB |
4740 | static int io_setup_async_msg(struct io_kiocb *req, |
4741 | struct io_async_msghdr *kmsg) | |
4742 | { | |
e8c2bc1f JA |
4743 | struct io_async_msghdr *async_msg = req->async_data; |
4744 | ||
4745 | if (async_msg) | |
02d27d89 | 4746 | return -EAGAIN; |
e8c2bc1f | 4747 | if (io_alloc_async_data(req)) { |
257e84a5 | 4748 | kfree(kmsg->free_iov); |
02d27d89 PB |
4749 | return -ENOMEM; |
4750 | } | |
e8c2bc1f | 4751 | async_msg = req->async_data; |
02d27d89 | 4752 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4753 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 4754 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
4755 | /* if were using fast_iov, set it to the new one */ |
4756 | if (!async_msg->free_iov) | |
4757 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
4758 | ||
02d27d89 PB |
4759 | return -EAGAIN; |
4760 | } | |
4761 | ||
2ae523ed PB |
4762 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4763 | struct io_async_msghdr *iomsg) | |
4764 | { | |
2ae523ed | 4765 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 4766 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 4767 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 4768 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
4769 | } |
4770 | ||
93642ef8 PB |
4771 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
4772 | { | |
4773 | int ret; | |
4774 | ||
93642ef8 PB |
4775 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
4776 | if (!ret) | |
4777 | req->flags |= REQ_F_NEED_CLEANUP; | |
4778 | return ret; | |
4779 | } | |
4780 | ||
3529d8c2 | 4781 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4782 | { |
e47293fd | 4783 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 4784 | |
d2b6f48b PB |
4785 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4786 | return -EINVAL; | |
4787 | ||
270a5940 | 4788 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4789 | sr->len = READ_ONCE(sqe->len); |
04411806 PB |
4790 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4791 | if (sr->msg_flags & MSG_DONTWAIT) | |
4792 | req->flags |= REQ_F_NOWAIT; | |
3529d8c2 | 4793 | |
d8768362 JA |
4794 | #ifdef CONFIG_COMPAT |
4795 | if (req->ctx->compat) | |
4796 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4797 | #endif | |
93642ef8 | 4798 | return 0; |
03b1230c JA |
4799 | } |
4800 | ||
889fca73 | 4801 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4802 | { |
6b754c8b | 4803 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4804 | struct socket *sock; |
7a7cacba | 4805 | unsigned flags; |
0031275d | 4806 | int min_ret = 0; |
0fa03c62 JA |
4807 | int ret; |
4808 | ||
dba4a925 | 4809 | sock = sock_from_file(req->file); |
7a7cacba | 4810 | if (unlikely(!sock)) |
dba4a925 | 4811 | return -ENOTSOCK; |
3529d8c2 | 4812 | |
d886e185 PB |
4813 | if (req_has_async_data(req)) { |
4814 | kmsg = req->async_data; | |
4815 | } else { | |
7a7cacba PB |
4816 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
4817 | if (ret) | |
4818 | return ret; | |
4819 | kmsg = &iomsg; | |
0fa03c62 | 4820 | } |
0fa03c62 | 4821 | |
04411806 PB |
4822 | flags = req->sr_msg.msg_flags; |
4823 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4824 | flags |= MSG_DONTWAIT; |
0031275d SM |
4825 | if (flags & MSG_WAITALL) |
4826 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4827 | ||
7a7cacba | 4828 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
0fa03c62 | 4829 | |
7297ce3d PB |
4830 | if (ret < min_ret) { |
4831 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) | |
4832 | return io_setup_async_msg(req, kmsg); | |
4833 | if (ret == -ERESTARTSYS) | |
4834 | ret = -EINTR; | |
4835 | req_set_fail(req); | |
4836 | } | |
257e84a5 PB |
4837 | /* fast path, check for non-NULL to avoid function call */ |
4838 | if (kmsg->free_iov) | |
4839 | kfree(kmsg->free_iov); | |
99bc4c38 | 4840 | req->flags &= ~REQ_F_NEED_CLEANUP; |
889fca73 | 4841 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 4842 | return 0; |
03b1230c | 4843 | } |
aa1fa28f | 4844 | |
889fca73 | 4845 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4846 | { |
7a7cacba PB |
4847 | struct io_sr_msg *sr = &req->sr_msg; |
4848 | struct msghdr msg; | |
4849 | struct iovec iov; | |
fddaface | 4850 | struct socket *sock; |
7a7cacba | 4851 | unsigned flags; |
0031275d | 4852 | int min_ret = 0; |
fddaface JA |
4853 | int ret; |
4854 | ||
dba4a925 | 4855 | sock = sock_from_file(req->file); |
7a7cacba | 4856 | if (unlikely(!sock)) |
dba4a925 | 4857 | return -ENOTSOCK; |
fddaface | 4858 | |
7a7cacba PB |
4859 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4860 | if (unlikely(ret)) | |
14db8411 | 4861 | return ret; |
fddaface | 4862 | |
7a7cacba PB |
4863 | msg.msg_name = NULL; |
4864 | msg.msg_control = NULL; | |
4865 | msg.msg_controllen = 0; | |
4866 | msg.msg_namelen = 0; | |
fddaface | 4867 | |
04411806 PB |
4868 | flags = req->sr_msg.msg_flags; |
4869 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4870 | flags |= MSG_DONTWAIT; |
0031275d SM |
4871 | if (flags & MSG_WAITALL) |
4872 | min_ret = iov_iter_count(&msg.msg_iter); | |
4873 | ||
7a7cacba PB |
4874 | msg.msg_flags = flags; |
4875 | ret = sock_sendmsg(sock, &msg); | |
7297ce3d PB |
4876 | if (ret < min_ret) { |
4877 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) | |
4878 | return -EAGAIN; | |
4879 | if (ret == -ERESTARTSYS) | |
4880 | ret = -EINTR; | |
93d2bcd2 | 4881 | req_set_fail(req); |
7297ce3d | 4882 | } |
889fca73 | 4883 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 4884 | return 0; |
fddaface JA |
4885 | } |
4886 | ||
1400e697 PB |
4887 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4888 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4889 | { |
4890 | struct io_sr_msg *sr = &req->sr_msg; | |
4891 | struct iovec __user *uiov; | |
4892 | size_t iov_len; | |
4893 | int ret; | |
4894 | ||
1400e697 PB |
4895 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4896 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4897 | if (ret) |
4898 | return ret; | |
4899 | ||
4900 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4901 | if (iov_len > 1) | |
4902 | return -EINVAL; | |
5476dfed | 4903 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4904 | return -EFAULT; |
5476dfed | 4905 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 4906 | iomsg->free_iov = NULL; |
52de1fe1 | 4907 | } else { |
257e84a5 | 4908 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4909 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 4910 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 4911 | false); |
52de1fe1 JA |
4912 | if (ret > 0) |
4913 | ret = 0; | |
4914 | } | |
4915 | ||
4916 | return ret; | |
4917 | } | |
4918 | ||
4919 | #ifdef CONFIG_COMPAT | |
4920 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4921 | struct io_async_msghdr *iomsg) |
52de1fe1 | 4922 | { |
52de1fe1 JA |
4923 | struct io_sr_msg *sr = &req->sr_msg; |
4924 | struct compat_iovec __user *uiov; | |
4925 | compat_uptr_t ptr; | |
4926 | compat_size_t len; | |
4927 | int ret; | |
4928 | ||
4af3417a PB |
4929 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
4930 | &ptr, &len); | |
52de1fe1 JA |
4931 | if (ret) |
4932 | return ret; | |
4933 | ||
4934 | uiov = compat_ptr(ptr); | |
4935 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4936 | compat_ssize_t clen; | |
4937 | ||
4938 | if (len > 1) | |
4939 | return -EINVAL; | |
4940 | if (!access_ok(uiov, sizeof(*uiov))) | |
4941 | return -EFAULT; | |
4942 | if (__get_user(clen, &uiov->iov_len)) | |
4943 | return -EFAULT; | |
4944 | if (clen < 0) | |
4945 | return -EINVAL; | |
2d280bc8 | 4946 | sr->len = clen; |
257e84a5 | 4947 | iomsg->free_iov = NULL; |
52de1fe1 | 4948 | } else { |
257e84a5 | 4949 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4950 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 4951 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 4952 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
4953 | if (ret < 0) |
4954 | return ret; | |
4955 | } | |
4956 | ||
4957 | return 0; | |
4958 | } | |
4959 | #endif | |
4960 | ||
1400e697 PB |
4961 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4962 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4963 | { |
1400e697 | 4964 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
4965 | |
4966 | #ifdef CONFIG_COMPAT | |
4967 | if (req->ctx->compat) | |
1400e697 | 4968 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4969 | #endif |
52de1fe1 | 4970 | |
1400e697 | 4971 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4972 | } |
4973 | ||
bcda7baa | 4974 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
51aac424 | 4975 | unsigned int issue_flags) |
bcda7baa JA |
4976 | { |
4977 | struct io_sr_msg *sr = &req->sr_msg; | |
bcda7baa | 4978 | |
51aac424 | 4979 | return io_buffer_select(req, &sr->len, sr->bgid, issue_flags); |
fddaface JA |
4980 | } |
4981 | ||
7fbb1b54 PB |
4982 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4983 | { | |
30d51dd4 | 4984 | return io_put_kbuf(req, req->kbuf); |
7fbb1b54 PB |
4985 | } |
4986 | ||
93642ef8 | 4987 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 4988 | { |
99bc4c38 | 4989 | int ret; |
3529d8c2 | 4990 | |
93642ef8 PB |
4991 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
4992 | if (!ret) | |
4993 | req->flags |= REQ_F_NEED_CLEANUP; | |
4994 | return ret; | |
4995 | } | |
4996 | ||
4997 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4998 | { | |
4999 | struct io_sr_msg *sr = &req->sr_msg; | |
5000 | ||
d2b6f48b PB |
5001 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5002 | return -EINVAL; | |
5003 | ||
270a5940 | 5004 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 5005 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 5006 | sr->bgid = READ_ONCE(sqe->buf_group); |
04411806 PB |
5007 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
5008 | if (sr->msg_flags & MSG_DONTWAIT) | |
5009 | req->flags |= REQ_F_NOWAIT; | |
06b76d44 | 5010 | |
d8768362 JA |
5011 | #ifdef CONFIG_COMPAT |
5012 | if (req->ctx->compat) | |
5013 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
5014 | #endif | |
93642ef8 | 5015 | return 0; |
aa1fa28f JA |
5016 | } |
5017 | ||
889fca73 | 5018 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 5019 | { |
6b754c8b | 5020 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 5021 | struct socket *sock; |
7fbb1b54 | 5022 | struct io_buffer *kbuf; |
7a7cacba | 5023 | unsigned flags; |
0031275d | 5024 | int min_ret = 0; |
52de1fe1 | 5025 | int ret, cflags = 0; |
45d189c6 | 5026 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 5027 | |
dba4a925 | 5028 | sock = sock_from_file(req->file); |
7a7cacba | 5029 | if (unlikely(!sock)) |
dba4a925 | 5030 | return -ENOTSOCK; |
3529d8c2 | 5031 | |
d886e185 PB |
5032 | if (req_has_async_data(req)) { |
5033 | kmsg = req->async_data; | |
5034 | } else { | |
7a7cacba PB |
5035 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
5036 | if (ret) | |
681fda8d | 5037 | return ret; |
7a7cacba PB |
5038 | kmsg = &iomsg; |
5039 | } | |
03b1230c | 5040 | |
bc02ef33 | 5041 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 5042 | kbuf = io_recv_buffer_select(req, issue_flags); |
bc02ef33 | 5043 | if (IS_ERR(kbuf)) |
52de1fe1 | 5044 | return PTR_ERR(kbuf); |
7a7cacba | 5045 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
5046 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
5047 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
5048 | 1, req->sr_msg.len); |
5049 | } | |
52de1fe1 | 5050 | |
04411806 PB |
5051 | flags = req->sr_msg.msg_flags; |
5052 | if (force_nonblock) | |
7a7cacba | 5053 | flags |= MSG_DONTWAIT; |
0031275d SM |
5054 | if (flags & MSG_WAITALL) |
5055 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
5056 | ||
7a7cacba PB |
5057 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
5058 | kmsg->uaddr, flags); | |
7297ce3d PB |
5059 | if (ret < min_ret) { |
5060 | if (ret == -EAGAIN && force_nonblock) | |
5061 | return io_setup_async_msg(req, kmsg); | |
5062 | if (ret == -ERESTARTSYS) | |
5063 | ret = -EINTR; | |
5064 | req_set_fail(req); | |
5065 | } else if ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { | |
5066 | req_set_fail(req); | |
5067 | } | |
03b1230c | 5068 | |
7fbb1b54 PB |
5069 | if (req->flags & REQ_F_BUFFER_SELECTED) |
5070 | cflags = io_put_recv_kbuf(req); | |
257e84a5 PB |
5071 | /* fast path, check for non-NULL to avoid function call */ |
5072 | if (kmsg->free_iov) | |
5073 | kfree(kmsg->free_iov); | |
99bc4c38 | 5074 | req->flags &= ~REQ_F_NEED_CLEANUP; |
889fca73 | 5075 | __io_req_complete(req, issue_flags, ret, cflags); |
03b1230c | 5076 | return 0; |
0fa03c62 | 5077 | } |
5d17b4a4 | 5078 | |
889fca73 | 5079 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 5080 | { |
6b754c8b | 5081 | struct io_buffer *kbuf; |
7a7cacba PB |
5082 | struct io_sr_msg *sr = &req->sr_msg; |
5083 | struct msghdr msg; | |
5084 | void __user *buf = sr->buf; | |
fddaface | 5085 | struct socket *sock; |
7a7cacba PB |
5086 | struct iovec iov; |
5087 | unsigned flags; | |
0031275d | 5088 | int min_ret = 0; |
bcda7baa | 5089 | int ret, cflags = 0; |
45d189c6 | 5090 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 5091 | |
dba4a925 | 5092 | sock = sock_from_file(req->file); |
7a7cacba | 5093 | if (unlikely(!sock)) |
dba4a925 | 5094 | return -ENOTSOCK; |
fddaface | 5095 | |
bc02ef33 | 5096 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 5097 | kbuf = io_recv_buffer_select(req, issue_flags); |
bcda7baa JA |
5098 | if (IS_ERR(kbuf)) |
5099 | return PTR_ERR(kbuf); | |
7a7cacba | 5100 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 5101 | } |
bcda7baa | 5102 | |
7a7cacba | 5103 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
5104 | if (unlikely(ret)) |
5105 | goto out_free; | |
fddaface | 5106 | |
7a7cacba PB |
5107 | msg.msg_name = NULL; |
5108 | msg.msg_control = NULL; | |
5109 | msg.msg_controllen = 0; | |
5110 | msg.msg_namelen = 0; | |
5111 | msg.msg_iocb = NULL; | |
5112 | msg.msg_flags = 0; | |
fddaface | 5113 | |
04411806 PB |
5114 | flags = req->sr_msg.msg_flags; |
5115 | if (force_nonblock) | |
7a7cacba | 5116 | flags |= MSG_DONTWAIT; |
0031275d SM |
5117 | if (flags & MSG_WAITALL) |
5118 | min_ret = iov_iter_count(&msg.msg_iter); | |
5119 | ||
7a7cacba | 5120 | ret = sock_recvmsg(sock, &msg, flags); |
14c32eee | 5121 | out_free: |
7297ce3d PB |
5122 | if (ret < min_ret) { |
5123 | if (ret == -EAGAIN && force_nonblock) | |
5124 | return -EAGAIN; | |
5125 | if (ret == -ERESTARTSYS) | |
5126 | ret = -EINTR; | |
5127 | req_set_fail(req); | |
5128 | } else if ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { | |
5129 | req_set_fail(req); | |
5130 | } | |
7fbb1b54 PB |
5131 | if (req->flags & REQ_F_BUFFER_SELECTED) |
5132 | cflags = io_put_recv_kbuf(req); | |
889fca73 | 5133 | __io_req_complete(req, issue_flags, ret, cflags); |
fddaface | 5134 | return 0; |
fddaface JA |
5135 | } |
5136 | ||
3529d8c2 | 5137 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 5138 | { |
8ed8d3c3 JA |
5139 | struct io_accept *accept = &req->accept; |
5140 | ||
14587a46 | 5141 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 5142 | return -EINVAL; |
aaa4db12 | 5143 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
5144 | return -EINVAL; |
5145 | ||
d55e5f5b JA |
5146 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5147 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 5148 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 5149 | accept->nofile = rlimit(RLIMIT_NOFILE); |
a7083ad5 | 5150 | |
aaa4db12 PB |
5151 | accept->file_slot = READ_ONCE(sqe->file_index); |
5152 | if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) || | |
5153 | (accept->flags & SOCK_CLOEXEC))) | |
5154 | return -EINVAL; | |
a7083ad5 PB |
5155 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
5156 | return -EINVAL; | |
5157 | if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) | |
5158 | accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; | |
8ed8d3c3 | 5159 | return 0; |
8ed8d3c3 | 5160 | } |
17f2fe35 | 5161 | |
889fca73 | 5162 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
5163 | { |
5164 | struct io_accept *accept = &req->accept; | |
45d189c6 | 5165 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 5166 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
aaa4db12 | 5167 | bool fixed = !!accept->file_slot; |
a7083ad5 PB |
5168 | struct file *file; |
5169 | int ret, fd; | |
8ed8d3c3 | 5170 | |
e697deed JX |
5171 | if (req->file->f_flags & O_NONBLOCK) |
5172 | req->flags |= REQ_F_NOWAIT; | |
5173 | ||
aaa4db12 PB |
5174 | if (!fixed) { |
5175 | fd = __get_unused_fd_flags(accept->flags, accept->nofile); | |
5176 | if (unlikely(fd < 0)) | |
5177 | return fd; | |
5178 | } | |
a7083ad5 PB |
5179 | file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, |
5180 | accept->flags); | |
5181 | if (IS_ERR(file)) { | |
aaa4db12 PB |
5182 | if (!fixed) |
5183 | put_unused_fd(fd); | |
a7083ad5 PB |
5184 | ret = PTR_ERR(file); |
5185 | if (ret == -EAGAIN && force_nonblock) | |
5186 | return -EAGAIN; | |
ac45abc0 PB |
5187 | if (ret == -ERESTARTSYS) |
5188 | ret = -EINTR; | |
93d2bcd2 | 5189 | req_set_fail(req); |
aaa4db12 | 5190 | } else if (!fixed) { |
a7083ad5 PB |
5191 | fd_install(fd, file); |
5192 | ret = fd; | |
aaa4db12 PB |
5193 | } else { |
5194 | ret = io_install_fixed_file(req, file, issue_flags, | |
5195 | accept->file_slot - 1); | |
ac45abc0 | 5196 | } |
889fca73 | 5197 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 5198 | return 0; |
8ed8d3c3 JA |
5199 | } |
5200 | ||
93642ef8 PB |
5201 | static int io_connect_prep_async(struct io_kiocb *req) |
5202 | { | |
5203 | struct io_async_connect *io = req->async_data; | |
5204 | struct io_connect *conn = &req->connect; | |
5205 | ||
5206 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
5207 | } | |
5208 | ||
3529d8c2 | 5209 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 5210 | { |
3529d8c2 | 5211 | struct io_connect *conn = &req->connect; |
f499a021 | 5212 | |
14587a46 | 5213 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 | 5214 | return -EINVAL; |
26578cda PB |
5215 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
5216 | sqe->splice_fd_in) | |
3fbb51c1 JA |
5217 | return -EINVAL; |
5218 | ||
3529d8c2 JA |
5219 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5220 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 5221 | return 0; |
f499a021 JA |
5222 | } |
5223 | ||
889fca73 | 5224 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 5225 | { |
e8c2bc1f | 5226 | struct io_async_connect __io, *io; |
f8e85cf2 | 5227 | unsigned file_flags; |
3fbb51c1 | 5228 | int ret; |
45d189c6 | 5229 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 5230 | |
d886e185 | 5231 | if (req_has_async_data(req)) { |
e8c2bc1f | 5232 | io = req->async_data; |
f499a021 | 5233 | } else { |
3529d8c2 JA |
5234 | ret = move_addr_to_kernel(req->connect.addr, |
5235 | req->connect.addr_len, | |
e8c2bc1f | 5236 | &__io.address); |
f499a021 JA |
5237 | if (ret) |
5238 | goto out; | |
5239 | io = &__io; | |
5240 | } | |
5241 | ||
3fbb51c1 JA |
5242 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
5243 | ||
e8c2bc1f | 5244 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 5245 | req->connect.addr_len, file_flags); |
87f80d62 | 5246 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
d886e185 | 5247 | if (req_has_async_data(req)) |
b7bb4f7d | 5248 | return -EAGAIN; |
e8c2bc1f | 5249 | if (io_alloc_async_data(req)) { |
f499a021 JA |
5250 | ret = -ENOMEM; |
5251 | goto out; | |
5252 | } | |
e8c2bc1f | 5253 | memcpy(req->async_data, &__io, sizeof(__io)); |
f8e85cf2 | 5254 | return -EAGAIN; |
f499a021 | 5255 | } |
f8e85cf2 JA |
5256 | if (ret == -ERESTARTSYS) |
5257 | ret = -EINTR; | |
f499a021 | 5258 | out: |
4e88d6e7 | 5259 | if (ret < 0) |
93d2bcd2 | 5260 | req_set_fail(req); |
889fca73 | 5261 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 5262 | return 0; |
469956e8 Y |
5263 | } |
5264 | #else /* !CONFIG_NET */ | |
99a10081 JA |
5265 | #define IO_NETOP_FN(op) \ |
5266 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
5267 | { \ | |
5268 | return -EOPNOTSUPP; \ | |
5269 | } | |
5270 | ||
5271 | #define IO_NETOP_PREP(op) \ | |
5272 | IO_NETOP_FN(op) \ | |
5273 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
5274 | { \ | |
5275 | return -EOPNOTSUPP; \ | |
5276 | } \ | |
5277 | ||
5278 | #define IO_NETOP_PREP_ASYNC(op) \ | |
5279 | IO_NETOP_PREP(op) \ | |
5280 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
5281 | { \ | |
5282 | return -EOPNOTSUPP; \ | |
5283 | } | |
5284 | ||
5285 | IO_NETOP_PREP_ASYNC(sendmsg); | |
5286 | IO_NETOP_PREP_ASYNC(recvmsg); | |
5287 | IO_NETOP_PREP_ASYNC(connect); | |
5288 | IO_NETOP_PREP(accept); | |
5289 | IO_NETOP_FN(send); | |
5290 | IO_NETOP_FN(recv); | |
469956e8 | 5291 | #endif /* CONFIG_NET */ |
f8e85cf2 | 5292 | |
d7718a9d JA |
5293 | struct io_poll_table { |
5294 | struct poll_table_struct pt; | |
5295 | struct io_kiocb *req; | |
68b11e8b | 5296 | int nr_entries; |
d7718a9d JA |
5297 | int error; |
5298 | }; | |
ce593a6c | 5299 | |
d7718a9d | 5300 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
5b0a6acc | 5301 | __poll_t mask, io_req_tw_func_t func) |
d7718a9d | 5302 | { |
d7718a9d JA |
5303 | /* for instances that support it check for an event match first: */ |
5304 | if (mask && !(mask & poll->events)) | |
5305 | return 0; | |
5306 | ||
5307 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
5308 | ||
5309 | list_del_init(&poll->wait.entry); | |
5310 | ||
d7718a9d | 5311 | req->result = mask; |
5b0a6acc | 5312 | req->io_task_work.func = func; |
6d816e08 | 5313 | |
d7718a9d | 5314 | /* |
e3aabf95 JA |
5315 | * If this fails, then the task is exiting. When a task exits, the |
5316 | * work gets canceled, so just cancel this request as well instead | |
5317 | * of executing it. We can't safely execute it anyway, as we may not | |
5318 | * have the needed state needed for it anyway. | |
d7718a9d | 5319 | */ |
e09ee510 | 5320 | io_req_task_work_add(req); |
d7718a9d JA |
5321 | return 1; |
5322 | } | |
5323 | ||
74ce6ce4 JA |
5324 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
5325 | __acquires(&req->ctx->completion_lock) | |
5326 | { | |
5327 | struct io_ring_ctx *ctx = req->ctx; | |
5328 | ||
316319e8 | 5329 | /* req->task == current here, checking PF_EXITING is safe */ |
e09ee510 PB |
5330 | if (unlikely(req->task->flags & PF_EXITING)) |
5331 | WRITE_ONCE(poll->canceled, true); | |
5332 | ||
74ce6ce4 JA |
5333 | if (!req->result && !READ_ONCE(poll->canceled)) { |
5334 | struct poll_table_struct pt = { ._key = poll->events }; | |
5335 | ||
5336 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
5337 | } | |
5338 | ||
79ebeaee | 5339 | spin_lock(&ctx->completion_lock); |
74ce6ce4 JA |
5340 | if (!req->result && !READ_ONCE(poll->canceled)) { |
5341 | add_wait_queue(poll->head, &poll->wait); | |
5342 | return true; | |
5343 | } | |
5344 | ||
5345 | return false; | |
5346 | } | |
5347 | ||
d4e7cd36 | 5348 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5349 | { |
e8c2bc1f | 5350 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5351 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5352 | return req->async_data; |
d4e7cd36 JA |
5353 | return req->apoll->double_poll; |
5354 | } | |
5355 | ||
5356 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5357 | { | |
5358 | if (req->opcode == IORING_OP_POLL_ADD) | |
5359 | return &req->poll; | |
5360 | return &req->apoll->poll; | |
5361 | } | |
5362 | ||
5363 | static void io_poll_remove_double(struct io_kiocb *req) | |
e07785b0 | 5364 | __must_hold(&req->ctx->completion_lock) |
d4e7cd36 JA |
5365 | { |
5366 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
5367 | |
5368 | lockdep_assert_held(&req->ctx->completion_lock); | |
5369 | ||
5370 | if (poll && poll->head) { | |
5371 | struct wait_queue_head *head = poll->head; | |
5372 | ||
79ebeaee | 5373 | spin_lock_irq(&head->lock); |
18bceab1 JA |
5374 | list_del_init(&poll->wait.entry); |
5375 | if (poll->wait.private) | |
de9b4cca | 5376 | req_ref_put(req); |
18bceab1 | 5377 | poll->head = NULL; |
79ebeaee | 5378 | spin_unlock_irq(&head->lock); |
18bceab1 JA |
5379 | } |
5380 | } | |
5381 | ||
31efe48e | 5382 | static bool __io_poll_complete(struct io_kiocb *req, __poll_t mask) |
e07785b0 | 5383 | __must_hold(&req->ctx->completion_lock) |
18bceab1 JA |
5384 | { |
5385 | struct io_ring_ctx *ctx = req->ctx; | |
88e41cf9 | 5386 | unsigned flags = IORING_CQE_F_MORE; |
e27414be | 5387 | int error; |
18bceab1 | 5388 | |
e27414be | 5389 | if (READ_ONCE(req->poll.canceled)) { |
45ab03b1 | 5390 | error = -ECANCELED; |
88e41cf9 | 5391 | req->poll.events |= EPOLLONESHOT; |
e27414be | 5392 | } else { |
5082620f | 5393 | error = mangle_poll(mask); |
e27414be | 5394 | } |
b69de288 JA |
5395 | if (req->poll.events & EPOLLONESHOT) |
5396 | flags = 0; | |
913a571a PB |
5397 | |
5398 | if (!(flags & IORING_CQE_F_MORE)) { | |
5399 | io_fill_cqe_req(req, error, flags); | |
5400 | } else if (!io_fill_cqe_aux(ctx, req->user_data, error, flags)) { | |
a62682f9 | 5401 | req->poll.events |= EPOLLONESHOT; |
88e41cf9 | 5402 | flags = 0; |
a62682f9 | 5403 | } |
88e41cf9 | 5404 | return !(flags & IORING_CQE_F_MORE); |
18bceab1 JA |
5405 | } |
5406 | ||
f237c30a | 5407 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 JA |
5408 | { |
5409 | struct io_ring_ctx *ctx = req->ctx; | |
dd221f46 | 5410 | struct io_kiocb *nxt; |
18bceab1 JA |
5411 | |
5412 | if (io_poll_rewait(req, &req->poll)) { | |
79ebeaee | 5413 | spin_unlock(&ctx->completion_lock); |
dd221f46 | 5414 | } else { |
f40b964a | 5415 | bool done; |
18bceab1 | 5416 | |
5b7aa38d HX |
5417 | if (req->poll.done) { |
5418 | spin_unlock(&ctx->completion_lock); | |
5419 | return; | |
5420 | } | |
31efe48e | 5421 | done = __io_poll_complete(req, req->result); |
88e41cf9 | 5422 | if (done) { |
a890d01e | 5423 | io_poll_remove_double(req); |
88e41cf9 | 5424 | hash_del(&req->hash_node); |
bd99c71b | 5425 | req->poll.done = true; |
f40b964a | 5426 | } else { |
88e41cf9 JA |
5427 | req->result = 0; |
5428 | add_wait_queue(req->poll.head, &req->poll.wait); | |
5429 | } | |
31efe48e | 5430 | io_commit_cqring(ctx); |
79ebeaee | 5431 | spin_unlock(&ctx->completion_lock); |
dd221f46 | 5432 | io_cqring_ev_posted(ctx); |
18bceab1 | 5433 | |
88e41cf9 JA |
5434 | if (done) { |
5435 | nxt = io_put_req_find_next(req); | |
5436 | if (nxt) | |
f237c30a | 5437 | io_req_task_submit(nxt, locked); |
88e41cf9 | 5438 | } |
dd221f46 | 5439 | } |
18bceab1 JA |
5440 | } |
5441 | ||
5442 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
5443 | int sync, void *key) | |
5444 | { | |
5445 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 5446 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 | 5447 | __poll_t mask = key_to_poll(key); |
79ebeaee | 5448 | unsigned long flags; |
18bceab1 JA |
5449 | |
5450 | /* for instances that support it check for an event match first: */ | |
5451 | if (mask && !(mask & poll->events)) | |
5452 | return 0; | |
88e41cf9 JA |
5453 | if (!(poll->events & EPOLLONESHOT)) |
5454 | return poll->wait.func(&poll->wait, mode, sync, key); | |
18bceab1 | 5455 | |
8706e04e JA |
5456 | list_del_init(&wait->entry); |
5457 | ||
9ce85ef2 | 5458 | if (poll->head) { |
18bceab1 JA |
5459 | bool done; |
5460 | ||
79ebeaee | 5461 | spin_lock_irqsave(&poll->head->lock, flags); |
807abcb0 | 5462 | done = list_empty(&poll->wait.entry); |
18bceab1 | 5463 | if (!done) |
807abcb0 | 5464 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
5465 | /* make sure double remove sees this as being gone */ |
5466 | wait->private = NULL; | |
79ebeaee | 5467 | spin_unlock_irqrestore(&poll->head->lock, flags); |
c8b5e260 JA |
5468 | if (!done) { |
5469 | /* use wait func handler, so it matches the rq type */ | |
5470 | poll->wait.func(&poll->wait, mode, sync, key); | |
5471 | } | |
18bceab1 | 5472 | } |
de9b4cca | 5473 | req_ref_put(req); |
18bceab1 JA |
5474 | return 1; |
5475 | } | |
5476 | ||
5477 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
5478 | wait_queue_func_t wake_func) | |
5479 | { | |
5480 | poll->head = NULL; | |
5481 | poll->done = false; | |
5482 | poll->canceled = false; | |
464dca61 JA |
5483 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
5484 | /* mask in events that we always want/need */ | |
5485 | poll->events = events | IO_POLL_UNMASK; | |
18bceab1 JA |
5486 | INIT_LIST_HEAD(&poll->wait.entry); |
5487 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
5488 | } | |
5489 | ||
5490 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
5491 | struct wait_queue_head *head, |
5492 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
5493 | { |
5494 | struct io_kiocb *req = pt->req; | |
5495 | ||
5496 | /* | |
68b11e8b PB |
5497 | * The file being polled uses multiple waitqueues for poll handling |
5498 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb | |
5499 | * if this happens. | |
18bceab1 | 5500 | */ |
68b11e8b | 5501 | if (unlikely(pt->nr_entries)) { |
58852d4d PB |
5502 | struct io_poll_iocb *poll_one = poll; |
5503 | ||
23a65db8 PB |
5504 | /* double add on the same waitqueue head, ignore */ |
5505 | if (poll_one->head == head) | |
5506 | return; | |
18bceab1 | 5507 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5508 | if (*poll_ptr) { |
23a65db8 PB |
5509 | if ((*poll_ptr)->head == head) |
5510 | return; | |
18bceab1 JA |
5511 | pt->error = -EINVAL; |
5512 | return; | |
5513 | } | |
ea6a693d JA |
5514 | /* |
5515 | * Can't handle multishot for double wait for now, turn it | |
5516 | * into one-shot mode. | |
5517 | */ | |
7a274727 PB |
5518 | if (!(poll_one->events & EPOLLONESHOT)) |
5519 | poll_one->events |= EPOLLONESHOT; | |
18bceab1 JA |
5520 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
5521 | if (!poll) { | |
5522 | pt->error = -ENOMEM; | |
5523 | return; | |
5524 | } | |
58852d4d | 5525 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
de9b4cca | 5526 | req_ref_get(req); |
18bceab1 | 5527 | poll->wait.private = req; |
d886e185 | 5528 | |
807abcb0 | 5529 | *poll_ptr = poll; |
d886e185 PB |
5530 | if (req->opcode == IORING_OP_POLL_ADD) |
5531 | req->flags |= REQ_F_ASYNC_DATA; | |
18bceab1 JA |
5532 | } |
5533 | ||
68b11e8b | 5534 | pt->nr_entries++; |
18bceab1 | 5535 | poll->head = head; |
a31eb4a2 JX |
5536 | |
5537 | if (poll->events & EPOLLEXCLUSIVE) | |
5538 | add_wait_queue_exclusive(head, &poll->wait); | |
5539 | else | |
5540 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5541 | } |
5542 | ||
5543 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5544 | struct poll_table_struct *p) | |
5545 | { | |
5546 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5547 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5548 | |
807abcb0 | 5549 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5550 | } |
5551 | ||
f237c30a | 5552 | static void io_async_task_func(struct io_kiocb *req, bool *locked) |
d7718a9d | 5553 | { |
d7718a9d JA |
5554 | struct async_poll *apoll = req->apoll; |
5555 | struct io_ring_ctx *ctx = req->ctx; | |
5556 | ||
236daeae | 5557 | trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data); |
d7718a9d | 5558 | |
74ce6ce4 | 5559 | if (io_poll_rewait(req, &apoll->poll)) { |
79ebeaee | 5560 | spin_unlock(&ctx->completion_lock); |
74ce6ce4 | 5561 | return; |
d7718a9d JA |
5562 | } |
5563 | ||
0ea13b44 | 5564 | hash_del(&req->hash_node); |
d4e7cd36 | 5565 | io_poll_remove_double(req); |
bd99c71b | 5566 | apoll->poll.done = true; |
79ebeaee | 5567 | spin_unlock(&ctx->completion_lock); |
74ce6ce4 | 5568 | |
0be0b0e3 | 5569 | if (!READ_ONCE(apoll->poll.canceled)) |
f237c30a | 5570 | io_req_task_submit(req, locked); |
0be0b0e3 | 5571 | else |
2593553a | 5572 | io_req_complete_failed(req, -ECANCELED); |
d7718a9d JA |
5573 | } |
5574 | ||
5575 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5576 | void *key) | |
5577 | { | |
5578 | struct io_kiocb *req = wait->private; | |
5579 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5580 | ||
5581 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5582 | key_to_poll(key)); | |
5583 | ||
5584 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5585 | } | |
5586 | ||
5587 | static void io_poll_req_insert(struct io_kiocb *req) | |
5588 | { | |
5589 | struct io_ring_ctx *ctx = req->ctx; | |
5590 | struct hlist_head *list; | |
5591 | ||
5592 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5593 | hlist_add_head(&req->hash_node, list); | |
5594 | } | |
5595 | ||
5596 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5597 | struct io_poll_iocb *poll, | |
5598 | struct io_poll_table *ipt, __poll_t mask, | |
5599 | wait_queue_func_t wake_func) | |
5600 | __acquires(&ctx->completion_lock) | |
5601 | { | |
5602 | struct io_ring_ctx *ctx = req->ctx; | |
5603 | bool cancel = false; | |
5604 | ||
4d52f338 | 5605 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5606 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5607 | poll->file = req->file; |
18bceab1 | 5608 | poll->wait.private = req; |
d7718a9d JA |
5609 | |
5610 | ipt->pt._key = mask; | |
5611 | ipt->req = req; | |
68b11e8b PB |
5612 | ipt->error = 0; |
5613 | ipt->nr_entries = 0; | |
d7718a9d | 5614 | |
d7718a9d | 5615 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
68b11e8b PB |
5616 | if (unlikely(!ipt->nr_entries) && !ipt->error) |
5617 | ipt->error = -EINVAL; | |
d7718a9d | 5618 | |
79ebeaee | 5619 | spin_lock(&ctx->completion_lock); |
a890d01e | 5620 | if (ipt->error || (mask && (poll->events & EPOLLONESHOT))) |
46fee9ab | 5621 | io_poll_remove_double(req); |
d7718a9d | 5622 | if (likely(poll->head)) { |
79ebeaee | 5623 | spin_lock_irq(&poll->head->lock); |
d7718a9d JA |
5624 | if (unlikely(list_empty(&poll->wait.entry))) { |
5625 | if (ipt->error) | |
5626 | cancel = true; | |
5627 | ipt->error = 0; | |
5628 | mask = 0; | |
5629 | } | |
88e41cf9 | 5630 | if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error) |
d7718a9d JA |
5631 | list_del_init(&poll->wait.entry); |
5632 | else if (cancel) | |
5633 | WRITE_ONCE(poll->canceled, true); | |
5634 | else if (!poll->done) /* actually waiting for an event */ | |
5635 | io_poll_req_insert(req); | |
79ebeaee | 5636 | spin_unlock_irq(&poll->head->lock); |
d7718a9d JA |
5637 | } |
5638 | ||
5639 | return mask; | |
5640 | } | |
5641 | ||
59b735ae OL |
5642 | enum { |
5643 | IO_APOLL_OK, | |
5644 | IO_APOLL_ABORTED, | |
5645 | IO_APOLL_READY | |
5646 | }; | |
5647 | ||
5648 | static int io_arm_poll_handler(struct io_kiocb *req) | |
d7718a9d JA |
5649 | { |
5650 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5651 | struct io_ring_ctx *ctx = req->ctx; | |
5652 | struct async_poll *apoll; | |
5653 | struct io_poll_table ipt; | |
b2d9c3da | 5654 | __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI; |
d7718a9d | 5655 | |
b2d9c3da PB |
5656 | if (!def->pollin && !def->pollout) |
5657 | return IO_APOLL_ABORTED; | |
658d0a40 PB |
5658 | if (!file_can_poll(req->file) || (req->flags & REQ_F_POLLED)) |
5659 | return IO_APOLL_ABORTED; | |
b2d9c3da PB |
5660 | |
5661 | if (def->pollin) { | |
b2d9c3da PB |
5662 | mask |= POLLIN | POLLRDNORM; |
5663 | ||
5664 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5665 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5666 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5667 | mask &= ~POLLIN; | |
5668 | } else { | |
b2d9c3da PB |
5669 | mask |= POLLOUT | POLLWRNORM; |
5670 | } | |
5671 | ||
d7718a9d JA |
5672 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
5673 | if (unlikely(!apoll)) | |
59b735ae | 5674 | return IO_APOLL_ABORTED; |
807abcb0 | 5675 | apoll->double_poll = NULL; |
d7718a9d | 5676 | req->apoll = apoll; |
b2d9c3da | 5677 | req->flags |= REQ_F_POLLED; |
d7718a9d | 5678 | ipt.pt._qproc = io_async_queue_proc; |
48dcd38d | 5679 | io_req_set_refcount(req); |
d7718a9d JA |
5680 | |
5681 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5682 | io_async_wake); | |
79ebeaee | 5683 | spin_unlock(&ctx->completion_lock); |
41a5169c HX |
5684 | if (ret || ipt.error) |
5685 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; | |
5686 | ||
236daeae OL |
5687 | trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data, |
5688 | mask, apoll->poll.events); | |
59b735ae | 5689 | return IO_APOLL_OK; |
d7718a9d JA |
5690 | } |
5691 | ||
5692 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
b2e720ac | 5693 | struct io_poll_iocb *poll, bool do_cancel) |
e07785b0 | 5694 | __must_hold(&req->ctx->completion_lock) |
221c5eb2 | 5695 | { |
b41e9852 | 5696 | bool do_complete = false; |
221c5eb2 | 5697 | |
5082620f JA |
5698 | if (!poll->head) |
5699 | return false; | |
79ebeaee | 5700 | spin_lock_irq(&poll->head->lock); |
b2e720ac JA |
5701 | if (do_cancel) |
5702 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5703 | if (!list_empty(&poll->wait.entry)) { |
5704 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5705 | do_complete = true; |
221c5eb2 | 5706 | } |
79ebeaee | 5707 | spin_unlock_irq(&poll->head->lock); |
3bfa5bcb | 5708 | hash_del(&req->hash_node); |
d7718a9d JA |
5709 | return do_complete; |
5710 | } | |
5711 | ||
5d709043 | 5712 | static bool io_poll_remove_one(struct io_kiocb *req) |
e07785b0 | 5713 | __must_hold(&req->ctx->completion_lock) |
d7718a9d JA |
5714 | { |
5715 | bool do_complete; | |
5716 | ||
d4e7cd36 | 5717 | io_poll_remove_double(req); |
e31001a3 | 5718 | do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true); |
d4e7cd36 | 5719 | |
b41e9852 | 5720 | if (do_complete) { |
93d2bcd2 | 5721 | req_set_fail(req); |
913a571a PB |
5722 | io_fill_cqe_req(req, -ECANCELED, 0); |
5723 | io_commit_cqring(req->ctx); | |
91c2f697 | 5724 | io_put_req_deferred(req); |
5d709043 | 5725 | } |
b41e9852 | 5726 | return do_complete; |
221c5eb2 JA |
5727 | } |
5728 | ||
76e1b642 JA |
5729 | /* |
5730 | * Returns true if we found and killed one or more poll requests | |
5731 | */ | |
c072481d PB |
5732 | static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, |
5733 | struct task_struct *tsk, bool cancel_all) | |
221c5eb2 | 5734 | { |
78076bb6 | 5735 | struct hlist_node *tmp; |
221c5eb2 | 5736 | struct io_kiocb *req; |
8e2e1faf | 5737 | int posted = 0, i; |
221c5eb2 | 5738 | |
79ebeaee | 5739 | spin_lock(&ctx->completion_lock); |
78076bb6 JA |
5740 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5741 | struct hlist_head *list; | |
5742 | ||
5743 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5744 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
3dd0c97a | 5745 | if (io_match_task(req, tsk, cancel_all)) |
f3606e3a JA |
5746 | posted += io_poll_remove_one(req); |
5747 | } | |
221c5eb2 | 5748 | } |
79ebeaee | 5749 | spin_unlock(&ctx->completion_lock); |
b41e9852 | 5750 | |
8e2e1faf JA |
5751 | if (posted) |
5752 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5753 | |
5754 | return posted != 0; | |
221c5eb2 JA |
5755 | } |
5756 | ||
9ba5fac8 PB |
5757 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5758 | bool poll_only) | |
e07785b0 | 5759 | __must_hold(&ctx->completion_lock) |
47f46768 | 5760 | { |
78076bb6 | 5761 | struct hlist_head *list; |
47f46768 JA |
5762 | struct io_kiocb *req; |
5763 | ||
78076bb6 JA |
5764 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5765 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5766 | if (sqe_addr != req->user_data) |
5767 | continue; | |
9ba5fac8 PB |
5768 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
5769 | continue; | |
b2cb805f | 5770 | return req; |
47f46768 | 5771 | } |
b2cb805f JA |
5772 | return NULL; |
5773 | } | |
5774 | ||
9ba5fac8 PB |
5775 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5776 | bool poll_only) | |
e07785b0 | 5777 | __must_hold(&ctx->completion_lock) |
b2cb805f JA |
5778 | { |
5779 | struct io_kiocb *req; | |
5780 | ||
9ba5fac8 | 5781 | req = io_poll_find(ctx, sqe_addr, poll_only); |
b2cb805f JA |
5782 | if (!req) |
5783 | return -ENOENT; | |
5784 | if (io_poll_remove_one(req)) | |
5785 | return 0; | |
5786 | ||
5787 | return -EALREADY; | |
47f46768 JA |
5788 | } |
5789 | ||
9096af3e PB |
5790 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
5791 | unsigned int flags) | |
5792 | { | |
5793 | u32 events; | |
47f46768 | 5794 | |
9096af3e PB |
5795 | events = READ_ONCE(sqe->poll32_events); |
5796 | #ifdef __BIG_ENDIAN | |
5797 | events = swahw32(events); | |
5798 | #endif | |
5799 | if (!(flags & IORING_POLL_ADD_MULTI)) | |
5800 | events |= EPOLLONESHOT; | |
5801 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); | |
47f46768 JA |
5802 | } |
5803 | ||
c5de0036 | 5804 | static int io_poll_update_prep(struct io_kiocb *req, |
3529d8c2 | 5805 | const struct io_uring_sqe *sqe) |
0969e783 | 5806 | { |
c5de0036 PB |
5807 | struct io_poll_update *upd = &req->poll_update; |
5808 | u32 flags; | |
5809 | ||
0969e783 JA |
5810 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5811 | return -EINVAL; | |
26578cda | 5812 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
c5de0036 PB |
5813 | return -EINVAL; |
5814 | flags = READ_ONCE(sqe->len); | |
5815 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | | |
5816 | IORING_POLL_ADD_MULTI)) | |
5817 | return -EINVAL; | |
5818 | /* meaningless without update */ | |
5819 | if (flags == IORING_POLL_ADD_MULTI) | |
0969e783 JA |
5820 | return -EINVAL; |
5821 | ||
c5de0036 PB |
5822 | upd->old_user_data = READ_ONCE(sqe->addr); |
5823 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; | |
5824 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; | |
221c5eb2 | 5825 | |
c5de0036 PB |
5826 | upd->new_user_data = READ_ONCE(sqe->off); |
5827 | if (!upd->update_user_data && upd->new_user_data) | |
5828 | return -EINVAL; | |
5829 | if (upd->update_events) | |
5830 | upd->events = io_poll_parse_events(sqe, flags); | |
5831 | else if (sqe->poll32_events) | |
5832 | return -EINVAL; | |
221c5eb2 | 5833 | |
221c5eb2 JA |
5834 | return 0; |
5835 | } | |
5836 | ||
221c5eb2 JA |
5837 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5838 | void *key) | |
5839 | { | |
c2f2eb7d JA |
5840 | struct io_kiocb *req = wait->private; |
5841 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5842 | |
d7718a9d | 5843 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5844 | } |
5845 | ||
221c5eb2 JA |
5846 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5847 | struct poll_table_struct *p) | |
5848 | { | |
5849 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5850 | ||
e8c2bc1f | 5851 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5852 | } |
5853 | ||
3529d8c2 | 5854 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5855 | { |
5856 | struct io_poll_iocb *poll = &req->poll; | |
c5de0036 | 5857 | u32 flags; |
221c5eb2 JA |
5858 | |
5859 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5860 | return -EINVAL; | |
c5de0036 | 5861 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
88e41cf9 JA |
5862 | return -EINVAL; |
5863 | flags = READ_ONCE(sqe->len); | |
c5de0036 | 5864 | if (flags & ~IORING_POLL_ADD_MULTI) |
221c5eb2 | 5865 | return -EINVAL; |
04c76b41 PB |
5866 | if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP)) |
5867 | return -EINVAL; | |
221c5eb2 | 5868 | |
48dcd38d | 5869 | io_req_set_refcount(req); |
c5de0036 | 5870 | poll->events = io_poll_parse_events(sqe, flags); |
0969e783 JA |
5871 | return 0; |
5872 | } | |
5873 | ||
61e98203 | 5874 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
5875 | { |
5876 | struct io_poll_iocb *poll = &req->poll; | |
5877 | struct io_ring_ctx *ctx = req->ctx; | |
5878 | struct io_poll_table ipt; | |
0969e783 | 5879 | __poll_t mask; |
5b7aa38d | 5880 | bool done; |
0969e783 | 5881 | |
d7718a9d | 5882 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5883 | |
d7718a9d JA |
5884 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5885 | io_poll_wake); | |
221c5eb2 | 5886 | |
8c838788 | 5887 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5888 | ipt.error = 0; |
eb6e6f06 PB |
5889 | done = __io_poll_complete(req, mask); |
5890 | io_commit_cqring(req->ctx); | |
221c5eb2 | 5891 | } |
79ebeaee | 5892 | spin_unlock(&ctx->completion_lock); |
221c5eb2 | 5893 | |
8c838788 JA |
5894 | if (mask) { |
5895 | io_cqring_ev_posted(ctx); | |
5b7aa38d | 5896 | if (done) |
88e41cf9 | 5897 | io_put_req(req); |
221c5eb2 | 5898 | } |
8c838788 | 5899 | return ipt.error; |
221c5eb2 JA |
5900 | } |
5901 | ||
c5de0036 | 5902 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
b69de288 JA |
5903 | { |
5904 | struct io_ring_ctx *ctx = req->ctx; | |
5905 | struct io_kiocb *preq; | |
cb3b200e | 5906 | bool completing; |
b69de288 JA |
5907 | int ret; |
5908 | ||
79ebeaee | 5909 | spin_lock(&ctx->completion_lock); |
9ba5fac8 | 5910 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
b69de288 JA |
5911 | if (!preq) { |
5912 | ret = -ENOENT; | |
5913 | goto err; | |
b69de288 | 5914 | } |
cb3b200e | 5915 | |
c5de0036 PB |
5916 | if (!req->poll_update.update_events && !req->poll_update.update_user_data) { |
5917 | completing = true; | |
5918 | ret = io_poll_remove_one(preq) ? 0 : -EALREADY; | |
5919 | goto err; | |
5920 | } | |
5921 | ||
cb3b200e JA |
5922 | /* |
5923 | * Don't allow racy completion with singleshot, as we cannot safely | |
5924 | * update those. For multishot, if we're racing with completion, just | |
5925 | * let completion re-add it. | |
5926 | */ | |
5927 | completing = !__io_poll_remove_one(preq, &preq->poll, false); | |
5928 | if (completing && (preq->poll.events & EPOLLONESHOT)) { | |
5929 | ret = -EALREADY; | |
5930 | goto err; | |
b69de288 JA |
5931 | } |
5932 | /* we now have a detached poll request. reissue. */ | |
5933 | ret = 0; | |
5934 | err: | |
b69de288 | 5935 | if (ret < 0) { |
79ebeaee | 5936 | spin_unlock(&ctx->completion_lock); |
93d2bcd2 | 5937 | req_set_fail(req); |
b69de288 JA |
5938 | io_req_complete(req, ret); |
5939 | return 0; | |
5940 | } | |
5941 | /* only mask one event flags, keep behavior flags */ | |
9d805892 | 5942 | if (req->poll_update.update_events) { |
b69de288 | 5943 | preq->poll.events &= ~0xffff; |
9d805892 | 5944 | preq->poll.events |= req->poll_update.events & 0xffff; |
b69de288 JA |
5945 | preq->poll.events |= IO_POLL_UNMASK; |
5946 | } | |
9d805892 PB |
5947 | if (req->poll_update.update_user_data) |
5948 | preq->user_data = req->poll_update.new_user_data; | |
79ebeaee | 5949 | spin_unlock(&ctx->completion_lock); |
cb3b200e | 5950 | |
b69de288 JA |
5951 | /* complete update request, we're done with it */ |
5952 | io_req_complete(req, ret); | |
5953 | ||
cb3b200e | 5954 | if (!completing) { |
c5de0036 | 5955 | ret = io_poll_add(preq, issue_flags); |
cb3b200e | 5956 | if (ret < 0) { |
93d2bcd2 | 5957 | req_set_fail(preq); |
cb3b200e JA |
5958 | io_req_complete(preq, ret); |
5959 | } | |
b69de288 JA |
5960 | } |
5961 | return 0; | |
5962 | } | |
5963 | ||
f237c30a | 5964 | static void io_req_task_timeout(struct io_kiocb *req, bool *locked) |
89850fce | 5965 | { |
6224590d PB |
5966 | struct io_timeout_data *data = req->async_data; |
5967 | ||
5968 | if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS)) | |
5969 | req_set_fail(req); | |
505657bc | 5970 | io_req_complete_post(req, -ETIME, 0); |
89850fce JA |
5971 | } |
5972 | ||
5262f567 JA |
5973 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5974 | { | |
ad8a48ac JA |
5975 | struct io_timeout_data *data = container_of(timer, |
5976 | struct io_timeout_data, timer); | |
5977 | struct io_kiocb *req = data->req; | |
5978 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5979 | unsigned long flags; |
5980 | ||
89850fce | 5981 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
a71976f3 | 5982 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5983 | atomic_set(&req->ctx->cq_timeouts, |
5984 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
89850fce | 5985 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
01cec8c1 | 5986 | |
89850fce JA |
5987 | req->io_task_work.func = io_req_task_timeout; |
5988 | io_req_task_work_add(req); | |
5262f567 JA |
5989 | return HRTIMER_NORESTART; |
5990 | } | |
5991 | ||
fbd15848 PB |
5992 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5993 | __u64 user_data) | |
89850fce | 5994 | __must_hold(&ctx->timeout_lock) |
f254ac04 | 5995 | { |
fbd15848 | 5996 | struct io_timeout_data *io; |
47f46768 | 5997 | struct io_kiocb *req; |
fd9c7bc5 | 5998 | bool found = false; |
f254ac04 | 5999 | |
135fcde8 | 6000 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
fd9c7bc5 PB |
6001 | found = user_data == req->user_data; |
6002 | if (found) | |
47f46768 | 6003 | break; |
47f46768 | 6004 | } |
fd9c7bc5 PB |
6005 | if (!found) |
6006 | return ERR_PTR(-ENOENT); | |
fbd15848 PB |
6007 | |
6008 | io = req->async_data; | |
fd9c7bc5 | 6009 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
fbd15848 | 6010 | return ERR_PTR(-EALREADY); |
a71976f3 | 6011 | list_del_init(&req->timeout.list); |
fbd15848 PB |
6012 | return req; |
6013 | } | |
47f46768 | 6014 | |
fbd15848 | 6015 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
ec3c3d0f | 6016 | __must_hold(&ctx->completion_lock) |
89850fce | 6017 | __must_hold(&ctx->timeout_lock) |
fbd15848 PB |
6018 | { |
6019 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
6020 | ||
6021 | if (IS_ERR(req)) | |
6022 | return PTR_ERR(req); | |
f254ac04 | 6023 | |
93d2bcd2 | 6024 | req_set_fail(req); |
913a571a | 6025 | io_fill_cqe_req(req, -ECANCELED, 0); |
91c2f697 | 6026 | io_put_req_deferred(req); |
f254ac04 JA |
6027 | return 0; |
6028 | } | |
6029 | ||
50c1df2b JA |
6030 | static clockid_t io_timeout_get_clock(struct io_timeout_data *data) |
6031 | { | |
6032 | switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) { | |
6033 | case IORING_TIMEOUT_BOOTTIME: | |
6034 | return CLOCK_BOOTTIME; | |
6035 | case IORING_TIMEOUT_REALTIME: | |
6036 | return CLOCK_REALTIME; | |
6037 | default: | |
6038 | /* can't happen, vetted at prep time */ | |
6039 | WARN_ON_ONCE(1); | |
6040 | fallthrough; | |
6041 | case 0: | |
6042 | return CLOCK_MONOTONIC; | |
6043 | } | |
6044 | } | |
6045 | ||
f1042b6c PB |
6046 | static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
6047 | struct timespec64 *ts, enum hrtimer_mode mode) | |
6048 | __must_hold(&ctx->timeout_lock) | |
6049 | { | |
6050 | struct io_timeout_data *io; | |
6051 | struct io_kiocb *req; | |
6052 | bool found = false; | |
6053 | ||
6054 | list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) { | |
6055 | found = user_data == req->user_data; | |
6056 | if (found) | |
6057 | break; | |
6058 | } | |
6059 | if (!found) | |
6060 | return -ENOENT; | |
6061 | ||
6062 | io = req->async_data; | |
6063 | if (hrtimer_try_to_cancel(&io->timer) == -1) | |
6064 | return -EALREADY; | |
6065 | hrtimer_init(&io->timer, io_timeout_get_clock(io), mode); | |
6066 | io->timer.function = io_link_timeout_fn; | |
6067 | hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode); | |
6068 | return 0; | |
6069 | } | |
6070 | ||
9c8e11b3 PB |
6071 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
6072 | struct timespec64 *ts, enum hrtimer_mode mode) | |
89850fce | 6073 | __must_hold(&ctx->timeout_lock) |
47f46768 | 6074 | { |
9c8e11b3 PB |
6075 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
6076 | struct io_timeout_data *data; | |
47f46768 | 6077 | |
9c8e11b3 PB |
6078 | if (IS_ERR(req)) |
6079 | return PTR_ERR(req); | |
47f46768 | 6080 | |
9c8e11b3 PB |
6081 | req->timeout.off = 0; /* noseq */ |
6082 | data = req->async_data; | |
6083 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
50c1df2b | 6084 | hrtimer_init(&data->timer, io_timeout_get_clock(data), mode); |
9c8e11b3 PB |
6085 | data->timer.function = io_timeout_fn; |
6086 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
6087 | return 0; | |
47f46768 JA |
6088 | } |
6089 | ||
3529d8c2 JA |
6090 | static int io_timeout_remove_prep(struct io_kiocb *req, |
6091 | const struct io_uring_sqe *sqe) | |
b29472ee | 6092 | { |
9c8e11b3 PB |
6093 | struct io_timeout_rem *tr = &req->timeout_rem; |
6094 | ||
b29472ee JA |
6095 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
6096 | return -EINVAL; | |
61710e43 DA |
6097 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6098 | return -EINVAL; | |
26578cda | 6099 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
b29472ee JA |
6100 | return -EINVAL; |
6101 | ||
f1042b6c | 6102 | tr->ltimeout = false; |
9c8e11b3 PB |
6103 | tr->addr = READ_ONCE(sqe->addr); |
6104 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
f1042b6c PB |
6105 | if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) { |
6106 | if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
6107 | return -EINVAL; | |
6108 | if (tr->flags & IORING_LINK_TIMEOUT_UPDATE) | |
6109 | tr->ltimeout = true; | |
6110 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS)) | |
9c8e11b3 PB |
6111 | return -EINVAL; |
6112 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
6113 | return -EFAULT; | |
6114 | } else if (tr->flags) { | |
6115 | /* timeout removal doesn't support flags */ | |
b29472ee | 6116 | return -EINVAL; |
9c8e11b3 | 6117 | } |
b29472ee | 6118 | |
b29472ee JA |
6119 | return 0; |
6120 | } | |
6121 | ||
8662daec PB |
6122 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
6123 | { | |
6124 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
6125 | : HRTIMER_MODE_REL; | |
6126 | } | |
6127 | ||
11365043 JA |
6128 | /* |
6129 | * Remove or update an existing timeout command | |
6130 | */ | |
61e98203 | 6131 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 6132 | { |
9c8e11b3 | 6133 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 6134 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 6135 | int ret; |
11365043 | 6136 | |
ec3c3d0f PB |
6137 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
6138 | spin_lock(&ctx->completion_lock); | |
6139 | spin_lock_irq(&ctx->timeout_lock); | |
9c8e11b3 | 6140 | ret = io_timeout_cancel(ctx, tr->addr); |
ec3c3d0f PB |
6141 | spin_unlock_irq(&ctx->timeout_lock); |
6142 | spin_unlock(&ctx->completion_lock); | |
6143 | } else { | |
f1042b6c PB |
6144 | enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags); |
6145 | ||
ec3c3d0f | 6146 | spin_lock_irq(&ctx->timeout_lock); |
f1042b6c PB |
6147 | if (tr->ltimeout) |
6148 | ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
6149 | else | |
6150 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
ec3c3d0f PB |
6151 | spin_unlock_irq(&ctx->timeout_lock); |
6152 | } | |
11365043 | 6153 | |
4e88d6e7 | 6154 | if (ret < 0) |
93d2bcd2 | 6155 | req_set_fail(req); |
505657bc | 6156 | io_req_complete_post(req, ret, 0); |
11365043 | 6157 | return 0; |
5262f567 JA |
6158 | } |
6159 | ||
3529d8c2 | 6160 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 6161 | bool is_timeout_link) |
5262f567 | 6162 | { |
ad8a48ac | 6163 | struct io_timeout_data *data; |
a41525ab | 6164 | unsigned flags; |
56080b02 | 6165 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 6166 | |
ad8a48ac | 6167 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 6168 | return -EINVAL; |
26578cda PB |
6169 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
6170 | sqe->splice_fd_in) | |
a41525ab | 6171 | return -EINVAL; |
56080b02 | 6172 | if (off && is_timeout_link) |
2d28390a | 6173 | return -EINVAL; |
a41525ab | 6174 | flags = READ_ONCE(sqe->timeout_flags); |
6224590d PB |
6175 | if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK | |
6176 | IORING_TIMEOUT_ETIME_SUCCESS)) | |
50c1df2b JA |
6177 | return -EINVAL; |
6178 | /* more than one clock specified is invalid, obviously */ | |
6179 | if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
5262f567 | 6180 | return -EINVAL; |
bdf20073 | 6181 | |
ef9dd637 | 6182 | INIT_LIST_HEAD(&req->timeout.list); |
bfe68a22 | 6183 | req->timeout.off = off; |
f18ee4cf PB |
6184 | if (unlikely(off && !req->ctx->off_timeout_used)) |
6185 | req->ctx->off_timeout_used = true; | |
26a61679 | 6186 | |
d6a644a7 PB |
6187 | if (WARN_ON_ONCE(req_has_async_data(req))) |
6188 | return -EFAULT; | |
6189 | if (io_alloc_async_data(req)) | |
26a61679 JA |
6190 | return -ENOMEM; |
6191 | ||
e8c2bc1f | 6192 | data = req->async_data; |
ad8a48ac | 6193 | data->req = req; |
50c1df2b | 6194 | data->flags = flags; |
ad8a48ac JA |
6195 | |
6196 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
6197 | return -EFAULT; |
6198 | ||
8662daec | 6199 | data->mode = io_translate_timeout_mode(flags); |
50c1df2b | 6200 | hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode); |
b97e736a PB |
6201 | |
6202 | if (is_timeout_link) { | |
6203 | struct io_submit_link *link = &req->ctx->submit_state.link; | |
6204 | ||
6205 | if (!link->head) | |
6206 | return -EINVAL; | |
6207 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) | |
6208 | return -EINVAL; | |
4d13d1a4 PB |
6209 | req->timeout.head = link->last; |
6210 | link->last->flags |= REQ_F_ARM_LTIMEOUT; | |
b97e736a | 6211 | } |
ad8a48ac JA |
6212 | return 0; |
6213 | } | |
6214 | ||
61e98203 | 6215 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 6216 | { |
ad8a48ac | 6217 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 6218 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 6219 | struct list_head *entry; |
bfe68a22 | 6220 | u32 tail, off = req->timeout.off; |
ad8a48ac | 6221 | |
89850fce | 6222 | spin_lock_irq(&ctx->timeout_lock); |
93bd25bb | 6223 | |
5262f567 JA |
6224 | /* |
6225 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
6226 | * timeout event to be satisfied. If it isn't set, then this is |
6227 | * a pure timeout request, sequence isn't used. | |
5262f567 | 6228 | */ |
8eb7e2d0 | 6229 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
6230 | entry = ctx->timeout_list.prev; |
6231 | goto add; | |
6232 | } | |
5262f567 | 6233 | |
bfe68a22 PB |
6234 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
6235 | req->timeout.target_seq = tail + off; | |
5262f567 | 6236 | |
f010505b MDG |
6237 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
6238 | * This is safe because ->completion_lock is held, and submissions | |
6239 | * and completions are never mixed in the same ->completion_lock section. | |
6240 | */ | |
6241 | ctx->cq_last_tm_flush = tail; | |
6242 | ||
5262f567 JA |
6243 | /* |
6244 | * Insertion sort, ensuring the first entry in the list is always | |
6245 | * the one we need first. | |
6246 | */ | |
5262f567 | 6247 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
6248 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
6249 | timeout.list); | |
5262f567 | 6250 | |
8eb7e2d0 | 6251 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 6252 | continue; |
bfe68a22 PB |
6253 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
6254 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
6255 | break; |
6256 | } | |
93bd25bb | 6257 | add: |
135fcde8 | 6258 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
6259 | data->timer.function = io_timeout_fn; |
6260 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
89850fce | 6261 | spin_unlock_irq(&ctx->timeout_lock); |
5262f567 JA |
6262 | return 0; |
6263 | } | |
5262f567 | 6264 | |
f458dd84 PB |
6265 | struct io_cancel_data { |
6266 | struct io_ring_ctx *ctx; | |
6267 | u64 user_data; | |
6268 | }; | |
6269 | ||
62755e35 JA |
6270 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
6271 | { | |
6272 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 6273 | struct io_cancel_data *cd = data; |
62755e35 | 6274 | |
f458dd84 | 6275 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
62755e35 JA |
6276 | } |
6277 | ||
f458dd84 PB |
6278 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
6279 | struct io_ring_ctx *ctx) | |
62755e35 | 6280 | { |
f458dd84 | 6281 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
62755e35 | 6282 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
6283 | int ret = 0; |
6284 | ||
f458dd84 | 6285 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
6286 | return -ENOENT; |
6287 | ||
f458dd84 | 6288 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
62755e35 JA |
6289 | switch (cancel_ret) { |
6290 | case IO_WQ_CANCEL_OK: | |
6291 | ret = 0; | |
6292 | break; | |
6293 | case IO_WQ_CANCEL_RUNNING: | |
6294 | ret = -EALREADY; | |
6295 | break; | |
6296 | case IO_WQ_CANCEL_NOTFOUND: | |
6297 | ret = -ENOENT; | |
6298 | break; | |
6299 | } | |
6300 | ||
e977d6d3 JA |
6301 | return ret; |
6302 | } | |
6303 | ||
8cb01fac | 6304 | static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr) |
47f46768 | 6305 | { |
8cb01fac | 6306 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 JA |
6307 | int ret; |
6308 | ||
dadebc35 | 6309 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
8cb01fac | 6310 | |
f458dd84 | 6311 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
df9727af | 6312 | if (ret != -ENOENT) |
8cb01fac | 6313 | return ret; |
505657bc PB |
6314 | |
6315 | spin_lock(&ctx->completion_lock); | |
79ebeaee | 6316 | spin_lock_irq(&ctx->timeout_lock); |
47f46768 | 6317 | ret = io_timeout_cancel(ctx, sqe_addr); |
79ebeaee | 6318 | spin_unlock_irq(&ctx->timeout_lock); |
47f46768 | 6319 | if (ret != -ENOENT) |
505657bc PB |
6320 | goto out; |
6321 | ret = io_poll_cancel(ctx, sqe_addr, false); | |
6322 | out: | |
6323 | spin_unlock(&ctx->completion_lock); | |
6324 | return ret; | |
47f46768 JA |
6325 | } |
6326 | ||
3529d8c2 JA |
6327 | static int io_async_cancel_prep(struct io_kiocb *req, |
6328 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 6329 | { |
fbf23849 | 6330 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 6331 | return -EINVAL; |
61710e43 DA |
6332 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6333 | return -EINVAL; | |
26578cda PB |
6334 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
6335 | sqe->splice_fd_in) | |
e977d6d3 JA |
6336 | return -EINVAL; |
6337 | ||
fbf23849 JA |
6338 | req->cancel.addr = READ_ONCE(sqe->addr); |
6339 | return 0; | |
6340 | } | |
6341 | ||
61e98203 | 6342 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6343 | { |
6344 | struct io_ring_ctx *ctx = req->ctx; | |
58f99373 | 6345 | u64 sqe_addr = req->cancel.addr; |
3b44b371 | 6346 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
58f99373 PB |
6347 | struct io_tctx_node *node; |
6348 | int ret; | |
6349 | ||
8cb01fac | 6350 | ret = io_try_cancel_userdata(req, sqe_addr); |
58f99373 PB |
6351 | if (ret != -ENOENT) |
6352 | goto done; | |
58f99373 PB |
6353 | |
6354 | /* slow path, try all io-wq's */ | |
3b44b371 | 6355 | io_ring_submit_lock(ctx, needs_lock); |
58f99373 PB |
6356 | ret = -ENOENT; |
6357 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
6358 | struct io_uring_task *tctx = node->task->io_uring; | |
fbf23849 | 6359 | |
58f99373 PB |
6360 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
6361 | if (ret != -ENOENT) | |
6362 | break; | |
6363 | } | |
3b44b371 | 6364 | io_ring_submit_unlock(ctx, needs_lock); |
58f99373 | 6365 | done: |
58f99373 | 6366 | if (ret < 0) |
93d2bcd2 | 6367 | req_set_fail(req); |
505657bc | 6368 | io_req_complete_post(req, ret, 0); |
5262f567 JA |
6369 | return 0; |
6370 | } | |
6371 | ||
269bbe5f | 6372 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
6373 | const struct io_uring_sqe *sqe) |
6374 | { | |
61710e43 DA |
6375 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6376 | return -EINVAL; | |
26578cda | 6377 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
05f3fb3c JA |
6378 | return -EINVAL; |
6379 | ||
269bbe5f BM |
6380 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
6381 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
6382 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 6383 | return -EINVAL; |
269bbe5f | 6384 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
6385 | return 0; |
6386 | } | |
6387 | ||
889fca73 | 6388 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6389 | { |
6390 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 6391 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
c3bdad02 | 6392 | struct io_uring_rsrc_update2 up; |
05f3fb3c | 6393 | int ret; |
fbf23849 | 6394 | |
269bbe5f BM |
6395 | up.offset = req->rsrc_update.offset; |
6396 | up.data = req->rsrc_update.arg; | |
c3bdad02 PB |
6397 | up.nr = 0; |
6398 | up.tags = 0; | |
615cee49 | 6399 | up.resv = 0; |
05f3fb3c | 6400 | |
3b44b371 | 6401 | io_ring_submit_lock(ctx, needs_lock); |
fdecb662 | 6402 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
98f0b3b4 | 6403 | &up, req->rsrc_update.nr_args); |
3b44b371 | 6404 | io_ring_submit_unlock(ctx, needs_lock); |
05f3fb3c JA |
6405 | |
6406 | if (ret < 0) | |
93d2bcd2 | 6407 | req_set_fail(req); |
889fca73 | 6408 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
6409 | return 0; |
6410 | } | |
6411 | ||
bfe76559 | 6412 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 6413 | { |
d625c6ee | 6414 | switch (req->opcode) { |
e781573e | 6415 | case IORING_OP_NOP: |
bfe76559 | 6416 | return 0; |
f67676d1 JA |
6417 | case IORING_OP_READV: |
6418 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 6419 | case IORING_OP_READ: |
bfe76559 | 6420 | return io_read_prep(req, sqe); |
f67676d1 JA |
6421 | case IORING_OP_WRITEV: |
6422 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 6423 | case IORING_OP_WRITE: |
bfe76559 | 6424 | return io_write_prep(req, sqe); |
0969e783 | 6425 | case IORING_OP_POLL_ADD: |
bfe76559 | 6426 | return io_poll_add_prep(req, sqe); |
0969e783 | 6427 | case IORING_OP_POLL_REMOVE: |
c5de0036 | 6428 | return io_poll_update_prep(req, sqe); |
8ed8d3c3 | 6429 | case IORING_OP_FSYNC: |
1155c76a | 6430 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 6431 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 6432 | return io_sfr_prep(req, sqe); |
03b1230c | 6433 | case IORING_OP_SENDMSG: |
fddaface | 6434 | case IORING_OP_SEND: |
bfe76559 | 6435 | return io_sendmsg_prep(req, sqe); |
03b1230c | 6436 | case IORING_OP_RECVMSG: |
fddaface | 6437 | case IORING_OP_RECV: |
bfe76559 | 6438 | return io_recvmsg_prep(req, sqe); |
f499a021 | 6439 | case IORING_OP_CONNECT: |
bfe76559 | 6440 | return io_connect_prep(req, sqe); |
2d28390a | 6441 | case IORING_OP_TIMEOUT: |
bfe76559 | 6442 | return io_timeout_prep(req, sqe, false); |
b29472ee | 6443 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 6444 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 6445 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 6446 | return io_async_cancel_prep(req, sqe); |
2d28390a | 6447 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 6448 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 6449 | case IORING_OP_ACCEPT: |
bfe76559 | 6450 | return io_accept_prep(req, sqe); |
d63d1b5e | 6451 | case IORING_OP_FALLOCATE: |
bfe76559 | 6452 | return io_fallocate_prep(req, sqe); |
15b71abe | 6453 | case IORING_OP_OPENAT: |
bfe76559 | 6454 | return io_openat_prep(req, sqe); |
b5dba59e | 6455 | case IORING_OP_CLOSE: |
bfe76559 | 6456 | return io_close_prep(req, sqe); |
05f3fb3c | 6457 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 6458 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6459 | case IORING_OP_STATX: |
bfe76559 | 6460 | return io_statx_prep(req, sqe); |
4840e418 | 6461 | case IORING_OP_FADVISE: |
bfe76559 | 6462 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6463 | case IORING_OP_MADVISE: |
bfe76559 | 6464 | return io_madvise_prep(req, sqe); |
cebdb986 | 6465 | case IORING_OP_OPENAT2: |
bfe76559 | 6466 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6467 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6468 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6469 | case IORING_OP_SPLICE: |
bfe76559 | 6470 | return io_splice_prep(req, sqe); |
ddf0322d | 6471 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6472 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6473 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6474 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6475 | case IORING_OP_TEE: |
bfe76559 | 6476 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6477 | case IORING_OP_SHUTDOWN: |
6478 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6479 | case IORING_OP_RENAMEAT: |
6480 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6481 | case IORING_OP_UNLINKAT: |
6482 | return io_unlinkat_prep(req, sqe); | |
e34a02dc DK |
6483 | case IORING_OP_MKDIRAT: |
6484 | return io_mkdirat_prep(req, sqe); | |
7a8721f8 DK |
6485 | case IORING_OP_SYMLINKAT: |
6486 | return io_symlinkat_prep(req, sqe); | |
cf30da90 DK |
6487 | case IORING_OP_LINKAT: |
6488 | return io_linkat_prep(req, sqe); | |
f67676d1 JA |
6489 | } |
6490 | ||
bfe76559 PB |
6491 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6492 | req->opcode); | |
bd54b6fe | 6493 | return -EINVAL; |
bfe76559 PB |
6494 | } |
6495 | ||
93642ef8 | 6496 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 6497 | { |
b7e298d2 PB |
6498 | if (!io_op_defs[req->opcode].needs_async_setup) |
6499 | return 0; | |
d886e185 | 6500 | if (WARN_ON_ONCE(req_has_async_data(req))) |
b7e298d2 PB |
6501 | return -EFAULT; |
6502 | if (io_alloc_async_data(req)) | |
6503 | return -EAGAIN; | |
6504 | ||
93642ef8 PB |
6505 | switch (req->opcode) { |
6506 | case IORING_OP_READV: | |
93642ef8 PB |
6507 | return io_rw_prep_async(req, READ); |
6508 | case IORING_OP_WRITEV: | |
93642ef8 PB |
6509 | return io_rw_prep_async(req, WRITE); |
6510 | case IORING_OP_SENDMSG: | |
93642ef8 PB |
6511 | return io_sendmsg_prep_async(req); |
6512 | case IORING_OP_RECVMSG: | |
93642ef8 PB |
6513 | return io_recvmsg_prep_async(req); |
6514 | case IORING_OP_CONNECT: | |
6515 | return io_connect_prep_async(req); | |
6516 | } | |
b7e298d2 PB |
6517 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
6518 | req->opcode); | |
6519 | return -EFAULT; | |
f67676d1 JA |
6520 | } |
6521 | ||
9cf7c104 PB |
6522 | static u32 io_get_sequence(struct io_kiocb *req) |
6523 | { | |
a3dbdf54 | 6524 | u32 seq = req->ctx->cached_sq_head; |
9cf7c104 | 6525 | |
a3dbdf54 PB |
6526 | /* need original cached_sq_head, but it was increased for each req */ |
6527 | io_for_each_link(req, req) | |
6528 | seq--; | |
6529 | return seq; | |
9cf7c104 PB |
6530 | } |
6531 | ||
c072481d | 6532 | static __cold void io_drain_req(struct io_kiocb *req) |
de0617e4 | 6533 | { |
a197f664 | 6534 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6535 | struct io_defer_entry *de; |
f67676d1 | 6536 | int ret; |
e0eb71dc | 6537 | u32 seq = io_get_sequence(req); |
3c19966d | 6538 | |
9d858b21 | 6539 | /* Still need defer if there is pending req in defer list. */ |
5e371265 | 6540 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) { |
e0eb71dc | 6541 | queue: |
10c66904 | 6542 | ctx->drain_active = false; |
e0eb71dc PB |
6543 | io_req_task_queue(req); |
6544 | return; | |
10c66904 | 6545 | } |
9cf7c104 | 6546 | |
b7e298d2 | 6547 | ret = io_req_prep_async(req); |
e0eb71dc PB |
6548 | if (ret) { |
6549 | fail: | |
6550 | io_req_complete_failed(req, ret); | |
6551 | return; | |
6552 | } | |
cbdcb435 | 6553 | io_prep_async_link(req); |
27dc8338 | 6554 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
76cc33d7 | 6555 | if (!de) { |
1b48773f | 6556 | ret = -ENOMEM; |
e0eb71dc | 6557 | goto fail; |
76cc33d7 | 6558 | } |
2d28390a | 6559 | |
79ebeaee | 6560 | spin_lock(&ctx->completion_lock); |
9cf7c104 | 6561 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
79ebeaee | 6562 | spin_unlock(&ctx->completion_lock); |
27dc8338 | 6563 | kfree(de); |
e0eb71dc | 6564 | goto queue; |
de0617e4 JA |
6565 | } |
6566 | ||
915967f6 | 6567 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 6568 | de->req = req; |
9cf7c104 | 6569 | de->seq = seq; |
27dc8338 | 6570 | list_add_tail(&de->list, &ctx->defer_list); |
79ebeaee | 6571 | spin_unlock(&ctx->completion_lock); |
de0617e4 JA |
6572 | } |
6573 | ||
68fb8979 | 6574 | static void io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6575 | { |
0e1b6fe3 | 6576 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
30d51dd4 PB |
6577 | kfree(req->kbuf); |
6578 | req->kbuf = NULL; | |
99bc4c38 PB |
6579 | } |
6580 | ||
0e1b6fe3 PB |
6581 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6582 | switch (req->opcode) { | |
6583 | case IORING_OP_READV: | |
6584 | case IORING_OP_READ_FIXED: | |
6585 | case IORING_OP_READ: | |
6586 | case IORING_OP_WRITEV: | |
6587 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6588 | case IORING_OP_WRITE: { |
6589 | struct io_async_rw *io = req->async_data; | |
1dacb4df PB |
6590 | |
6591 | kfree(io->free_iovec); | |
0e1b6fe3 | 6592 | break; |
e8c2bc1f | 6593 | } |
0e1b6fe3 | 6594 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6595 | case IORING_OP_SENDMSG: { |
6596 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
6597 | |
6598 | kfree(io->free_iov); | |
0e1b6fe3 | 6599 | break; |
e8c2bc1f | 6600 | } |
0e1b6fe3 PB |
6601 | case IORING_OP_SPLICE: |
6602 | case IORING_OP_TEE: | |
e1d767f0 PB |
6603 | if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED)) |
6604 | io_put_file(req->splice.file_in); | |
0e1b6fe3 | 6605 | break; |
f3cd4850 JA |
6606 | case IORING_OP_OPENAT: |
6607 | case IORING_OP_OPENAT2: | |
6608 | if (req->open.filename) | |
6609 | putname(req->open.filename); | |
6610 | break; | |
80a261fd JA |
6611 | case IORING_OP_RENAMEAT: |
6612 | putname(req->rename.oldpath); | |
6613 | putname(req->rename.newpath); | |
6614 | break; | |
14a1143b JA |
6615 | case IORING_OP_UNLINKAT: |
6616 | putname(req->unlink.filename); | |
6617 | break; | |
e34a02dc DK |
6618 | case IORING_OP_MKDIRAT: |
6619 | putname(req->mkdir.filename); | |
6620 | break; | |
7a8721f8 DK |
6621 | case IORING_OP_SYMLINKAT: |
6622 | putname(req->symlink.oldpath); | |
6623 | putname(req->symlink.newpath); | |
6624 | break; | |
cf30da90 DK |
6625 | case IORING_OP_LINKAT: |
6626 | putname(req->hardlink.oldpath); | |
6627 | putname(req->hardlink.newpath); | |
6628 | break; | |
0e1b6fe3 | 6629 | } |
99bc4c38 | 6630 | } |
75652a30 JA |
6631 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
6632 | kfree(req->apoll->double_poll); | |
6633 | kfree(req->apoll); | |
6634 | req->apoll = NULL; | |
6635 | } | |
3a0a6902 PB |
6636 | if (req->flags & REQ_F_INFLIGHT) { |
6637 | struct io_uring_task *tctx = req->task->io_uring; | |
6638 | ||
6639 | atomic_dec(&tctx->inflight_tracked); | |
3a0a6902 | 6640 | } |
c854357b | 6641 | if (req->flags & REQ_F_CREDS) |
b8e64b53 | 6642 | put_cred(req->creds); |
d886e185 PB |
6643 | if (req->flags & REQ_F_ASYNC_DATA) { |
6644 | kfree(req->async_data); | |
6645 | req->async_data = NULL; | |
6646 | } | |
c854357b | 6647 | req->flags &= ~IO_REQ_CLEAN_FLAGS; |
99bc4c38 PB |
6648 | } |
6649 | ||
889fca73 | 6650 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 6651 | { |
5730b27e | 6652 | const struct cred *creds = NULL; |
d625c6ee | 6653 | int ret; |
2b188cc1 | 6654 | |
6878b40e | 6655 | if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred())) |
c10d1f98 | 6656 | creds = override_creds(req->creds); |
5730b27e | 6657 | |
5bd2182d PM |
6658 | if (!io_op_defs[req->opcode].audit_skip) |
6659 | audit_uring_entry(req->opcode); | |
6660 | ||
d625c6ee | 6661 | switch (req->opcode) { |
2b188cc1 | 6662 | case IORING_OP_NOP: |
889fca73 | 6663 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
6664 | break; |
6665 | case IORING_OP_READV: | |
edafccee | 6666 | case IORING_OP_READ_FIXED: |
3a6820f2 | 6667 | case IORING_OP_READ: |
889fca73 | 6668 | ret = io_read(req, issue_flags); |
edafccee | 6669 | break; |
3529d8c2 | 6670 | case IORING_OP_WRITEV: |
edafccee | 6671 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 6672 | case IORING_OP_WRITE: |
889fca73 | 6673 | ret = io_write(req, issue_flags); |
2b188cc1 | 6674 | break; |
c992fe29 | 6675 | case IORING_OP_FSYNC: |
45d189c6 | 6676 | ret = io_fsync(req, issue_flags); |
c992fe29 | 6677 | break; |
221c5eb2 | 6678 | case IORING_OP_POLL_ADD: |
61e98203 | 6679 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
6680 | break; |
6681 | case IORING_OP_POLL_REMOVE: | |
c5de0036 | 6682 | ret = io_poll_update(req, issue_flags); |
221c5eb2 | 6683 | break; |
5d17b4a4 | 6684 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 6685 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 6686 | break; |
0fa03c62 | 6687 | case IORING_OP_SENDMSG: |
889fca73 | 6688 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 6689 | break; |
fddaface | 6690 | case IORING_OP_SEND: |
889fca73 | 6691 | ret = io_send(req, issue_flags); |
0fa03c62 | 6692 | break; |
aa1fa28f | 6693 | case IORING_OP_RECVMSG: |
889fca73 | 6694 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 6695 | break; |
fddaface | 6696 | case IORING_OP_RECV: |
889fca73 | 6697 | ret = io_recv(req, issue_flags); |
aa1fa28f | 6698 | break; |
5262f567 | 6699 | case IORING_OP_TIMEOUT: |
61e98203 | 6700 | ret = io_timeout(req, issue_flags); |
5262f567 | 6701 | break; |
11365043 | 6702 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 6703 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 6704 | break; |
17f2fe35 | 6705 | case IORING_OP_ACCEPT: |
889fca73 | 6706 | ret = io_accept(req, issue_flags); |
17f2fe35 | 6707 | break; |
f8e85cf2 | 6708 | case IORING_OP_CONNECT: |
889fca73 | 6709 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 6710 | break; |
62755e35 | 6711 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 6712 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 6713 | break; |
d63d1b5e | 6714 | case IORING_OP_FALLOCATE: |
45d189c6 | 6715 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 6716 | break; |
15b71abe | 6717 | case IORING_OP_OPENAT: |
45d189c6 | 6718 | ret = io_openat(req, issue_flags); |
15b71abe | 6719 | break; |
b5dba59e | 6720 | case IORING_OP_CLOSE: |
889fca73 | 6721 | ret = io_close(req, issue_flags); |
b5dba59e | 6722 | break; |
05f3fb3c | 6723 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6724 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6725 | break; |
eddc7ef5 | 6726 | case IORING_OP_STATX: |
45d189c6 | 6727 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6728 | break; |
4840e418 | 6729 | case IORING_OP_FADVISE: |
45d189c6 | 6730 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6731 | break; |
c1ca757b | 6732 | case IORING_OP_MADVISE: |
45d189c6 | 6733 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6734 | break; |
cebdb986 | 6735 | case IORING_OP_OPENAT2: |
45d189c6 | 6736 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6737 | break; |
3e4827b0 | 6738 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6739 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6740 | break; |
7d67af2c | 6741 | case IORING_OP_SPLICE: |
45d189c6 | 6742 | ret = io_splice(req, issue_flags); |
7d67af2c | 6743 | break; |
ddf0322d | 6744 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6745 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6746 | break; |
067524e9 | 6747 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6748 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6749 | break; |
f2a8d5c7 | 6750 | case IORING_OP_TEE: |
45d189c6 | 6751 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6752 | break; |
36f4fa68 | 6753 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6754 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6755 | break; |
80a261fd | 6756 | case IORING_OP_RENAMEAT: |
45d189c6 | 6757 | ret = io_renameat(req, issue_flags); |
80a261fd | 6758 | break; |
14a1143b | 6759 | case IORING_OP_UNLINKAT: |
45d189c6 | 6760 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6761 | break; |
e34a02dc DK |
6762 | case IORING_OP_MKDIRAT: |
6763 | ret = io_mkdirat(req, issue_flags); | |
6764 | break; | |
7a8721f8 DK |
6765 | case IORING_OP_SYMLINKAT: |
6766 | ret = io_symlinkat(req, issue_flags); | |
6767 | break; | |
cf30da90 DK |
6768 | case IORING_OP_LINKAT: |
6769 | ret = io_linkat(req, issue_flags); | |
6770 | break; | |
2b188cc1 JA |
6771 | default: |
6772 | ret = -EINVAL; | |
6773 | break; | |
6774 | } | |
6775 | ||
5bd2182d PM |
6776 | if (!io_op_defs[req->opcode].audit_skip) |
6777 | audit_uring_exit(!ret, ret); | |
6778 | ||
5730b27e JA |
6779 | if (creds) |
6780 | revert_creds(creds); | |
def596e9 JA |
6781 | if (ret) |
6782 | return ret; | |
b532576e | 6783 | /* If the op doesn't have a file, we're not polling for it */ |
9983028e | 6784 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
9882131c | 6785 | io_iopoll_req_issued(req, issue_flags); |
def596e9 JA |
6786 | |
6787 | return 0; | |
2b188cc1 JA |
6788 | } |
6789 | ||
ebc11b6c PB |
6790 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
6791 | { | |
6792 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6793 | ||
6794 | req = io_put_req_find_next(req); | |
6795 | return req ? &req->work : NULL; | |
6796 | } | |
6797 | ||
5280f7e5 | 6798 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6799 | { |
6800 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
d01905db PB |
6801 | unsigned int issue_flags = IO_URING_F_UNLOCKED; |
6802 | bool needs_poll = false; | |
6df1db6b | 6803 | struct io_kiocb *timeout; |
561fb04a | 6804 | int ret = 0; |
2b188cc1 | 6805 | |
48dcd38d PB |
6806 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
6807 | if (!(req->flags & REQ_F_REFCOUNT)) | |
6808 | __io_req_set_refcount(req, 2); | |
6809 | else | |
6810 | req_ref_get(req); | |
5d5901a3 | 6811 | |
6df1db6b PB |
6812 | timeout = io_prep_linked_timeout(req); |
6813 | if (timeout) | |
6814 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6815 | |
dadebc35 | 6816 | /* either cancelled or io-wq is dying, so don't touch tctx->iowq */ |
d01905db PB |
6817 | if (work->flags & IO_WQ_WORK_CANCEL) { |
6818 | io_req_task_queue_fail(req, -ECANCELED); | |
6819 | return; | |
6820 | } | |
31b51510 | 6821 | |
d01905db | 6822 | if (req->flags & REQ_F_FORCE_ASYNC) { |
afb7f56f PB |
6823 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
6824 | bool opcode_poll = def->pollin || def->pollout; | |
6825 | ||
6826 | if (opcode_poll && file_can_poll(req->file)) { | |
6827 | needs_poll = true; | |
d01905db | 6828 | issue_flags |= IO_URING_F_NONBLOCK; |
afb7f56f | 6829 | } |
561fb04a | 6830 | } |
31b51510 | 6831 | |
d01905db PB |
6832 | do { |
6833 | ret = io_issue_sqe(req, issue_flags); | |
6834 | if (ret != -EAGAIN) | |
6835 | break; | |
6836 | /* | |
6837 | * We can get EAGAIN for iopolled IO even though we're | |
6838 | * forcing a sync submission from here, since we can't | |
6839 | * wait for request slots on the block side. | |
6840 | */ | |
6841 | if (!needs_poll) { | |
6842 | cond_resched(); | |
6843 | continue; | |
90fa0288 HX |
6844 | } |
6845 | ||
d01905db PB |
6846 | if (io_arm_poll_handler(req) == IO_APOLL_OK) |
6847 | return; | |
6848 | /* aborted or ready, in either case retry blocking */ | |
6849 | needs_poll = false; | |
6850 | issue_flags &= ~IO_URING_F_NONBLOCK; | |
6851 | } while (1); | |
31b51510 | 6852 | |
a3df7698 | 6853 | /* avoid locking problems by failing it from a clean context */ |
5d5901a3 | 6854 | if (ret) |
a3df7698 | 6855 | io_req_task_queue_fail(req, ret); |
2b188cc1 JA |
6856 | } |
6857 | ||
aeca241b | 6858 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
042b0d85 | 6859 | unsigned i) |
65e19f54 | 6860 | { |
042b0d85 | 6861 | return &table->files[i]; |
dafecf19 PB |
6862 | } |
6863 | ||
65e19f54 JA |
6864 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6865 | int index) | |
6866 | { | |
aeca241b | 6867 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
65e19f54 | 6868 | |
a04b0ac0 | 6869 | return (struct file *) (slot->file_ptr & FFS_MASK); |
65e19f54 JA |
6870 | } |
6871 | ||
a04b0ac0 | 6872 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
9a321c98 PB |
6873 | { |
6874 | unsigned long file_ptr = (unsigned long) file; | |
6875 | ||
88459b50 | 6876 | file_ptr |= io_file_get_flags(file); |
a04b0ac0 | 6877 | file_slot->file_ptr = file_ptr; |
65e19f54 JA |
6878 | } |
6879 | ||
ac177053 PB |
6880 | static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx, |
6881 | struct io_kiocb *req, int fd) | |
09bb8394 | 6882 | { |
8da11c19 | 6883 | struct file *file; |
ac177053 | 6884 | unsigned long file_ptr; |
09bb8394 | 6885 | |
ac177053 PB |
6886 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
6887 | return NULL; | |
6888 | fd = array_index_nospec(fd, ctx->nr_user_files); | |
6889 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; | |
6890 | file = (struct file *) (file_ptr & FFS_MASK); | |
6891 | file_ptr &= ~FFS_MASK; | |
6892 | /* mask in overlapping REQ_F and FFS bits */ | |
35645ac3 | 6893 | req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT); |
a46be971 | 6894 | io_req_set_rsrc_node(req, ctx); |
ac177053 PB |
6895 | return file; |
6896 | } | |
d44f554e | 6897 | |
ac177053 | 6898 | static struct file *io_file_get_normal(struct io_ring_ctx *ctx, |
ac177053 PB |
6899 | struct io_kiocb *req, int fd) |
6900 | { | |
62906e89 | 6901 | struct file *file = fget(fd); |
ac177053 PB |
6902 | |
6903 | trace_io_uring_file_get(ctx, fd); | |
09bb8394 | 6904 | |
ac177053 PB |
6905 | /* we don't allow fixed io_uring files */ |
6906 | if (file && unlikely(file->f_op == &io_uring_fops)) | |
6907 | io_req_track_inflight(req); | |
8371adf5 | 6908 | return file; |
09bb8394 JA |
6909 | } |
6910 | ||
ac177053 | 6911 | static inline struct file *io_file_get(struct io_ring_ctx *ctx, |
ac177053 PB |
6912 | struct io_kiocb *req, int fd, bool fixed) |
6913 | { | |
6914 | if (fixed) | |
6915 | return io_file_get_fixed(ctx, req, fd); | |
6916 | else | |
62906e89 | 6917 | return io_file_get_normal(ctx, req, fd); |
ac177053 PB |
6918 | } |
6919 | ||
f237c30a | 6920 | static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked) |
89b263f6 JA |
6921 | { |
6922 | struct io_kiocb *prev = req->timeout.prev; | |
8cb01fac | 6923 | int ret; |
89b263f6 JA |
6924 | |
6925 | if (prev) { | |
8cb01fac | 6926 | ret = io_try_cancel_userdata(req, prev->user_data); |
505657bc | 6927 | io_req_complete_post(req, ret ?: -ETIME, 0); |
89b263f6 | 6928 | io_put_req(prev); |
89b263f6 JA |
6929 | } else { |
6930 | io_req_complete_post(req, -ETIME, 0); | |
6931 | } | |
6932 | } | |
6933 | ||
2665abfd | 6934 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6935 | { |
ad8a48ac JA |
6936 | struct io_timeout_data *data = container_of(timer, |
6937 | struct io_timeout_data, timer); | |
90cd7e42 | 6938 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6939 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6940 | unsigned long flags; |
2665abfd | 6941 | |
89b263f6 | 6942 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
90cd7e42 PB |
6943 | prev = req->timeout.head; |
6944 | req->timeout.head = NULL; | |
2665abfd JA |
6945 | |
6946 | /* | |
6947 | * We don't expect the list to be empty, that will only happen if we | |
6948 | * race with the completion of the linked work. | |
6949 | */ | |
447c19f3 | 6950 | if (prev) { |
f2f87370 | 6951 | io_remove_next_linked(prev); |
447c19f3 PB |
6952 | if (!req_ref_inc_not_zero(prev)) |
6953 | prev = NULL; | |
6954 | } | |
ef9dd637 | 6955 | list_del(&req->timeout.list); |
89b263f6 JA |
6956 | req->timeout.prev = prev; |
6957 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); | |
2665abfd | 6958 | |
89b263f6 JA |
6959 | req->io_task_work.func = io_req_task_link_timeout; |
6960 | io_req_task_work_add(req); | |
2665abfd JA |
6961 | return HRTIMER_NORESTART; |
6962 | } | |
6963 | ||
de968c18 | 6964 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6965 | { |
de968c18 PB |
6966 | struct io_ring_ctx *ctx = req->ctx; |
6967 | ||
89b263f6 | 6968 | spin_lock_irq(&ctx->timeout_lock); |
76a46e06 | 6969 | /* |
f2f87370 PB |
6970 | * If the back reference is NULL, then our linked request finished |
6971 | * before we got a chance to setup the timer | |
76a46e06 | 6972 | */ |
90cd7e42 | 6973 | if (req->timeout.head) { |
e8c2bc1f | 6974 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6975 | |
ad8a48ac JA |
6976 | data->timer.function = io_link_timeout_fn; |
6977 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6978 | data->mode); | |
ef9dd637 | 6979 | list_add_tail(&req->timeout.list, &ctx->ltimeout_list); |
2665abfd | 6980 | } |
89b263f6 | 6981 | spin_unlock_irq(&ctx->timeout_lock); |
2665abfd | 6982 | /* drop submission reference */ |
76a46e06 JA |
6983 | io_put_req(req); |
6984 | } | |
2665abfd | 6985 | |
d475a9a6 PB |
6986 | static void io_queue_sqe_arm_apoll(struct io_kiocb *req) |
6987 | __must_hold(&req->ctx->uring_lock) | |
6988 | { | |
6989 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); | |
6990 | ||
6991 | switch (io_arm_poll_handler(req)) { | |
6992 | case IO_APOLL_READY: | |
d475a9a6 PB |
6993 | io_req_task_queue(req); |
6994 | break; | |
6995 | case IO_APOLL_ABORTED: | |
6996 | /* | |
6997 | * Queued up for async execution, worker will release | |
6998 | * submit reference when the iocb is actually submitted. | |
6999 | */ | |
7000 | io_queue_async_work(req, NULL); | |
7001 | break; | |
7002 | } | |
7003 | ||
7004 | if (linked_timeout) | |
7005 | io_queue_linked_timeout(linked_timeout); | |
7006 | } | |
7007 | ||
7008 | static inline void __io_queue_sqe(struct io_kiocb *req) | |
282cdc86 | 7009 | __must_hold(&req->ctx->uring_lock) |
2b188cc1 | 7010 | { |
906c6caa | 7011 | struct io_kiocb *linked_timeout; |
e0c5c576 | 7012 | int ret; |
2b188cc1 | 7013 | |
c5eef2b9 | 7014 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 7015 | |
fff4e40e PB |
7016 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
7017 | io_req_add_compl_list(req); | |
d9f9d284 | 7018 | return; |
fff4e40e | 7019 | } |
491381ce JA |
7020 | /* |
7021 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
7022 | * doesn't support non-blocking read/write attempts | |
7023 | */ | |
1840038e | 7024 | if (likely(!ret)) { |
906c6caa PB |
7025 | linked_timeout = io_prep_linked_timeout(req); |
7026 | if (linked_timeout) | |
7027 | io_queue_linked_timeout(linked_timeout); | |
1840038e | 7028 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
d475a9a6 | 7029 | io_queue_sqe_arm_apoll(req); |
0d63c148 | 7030 | } else { |
f41db273 | 7031 | io_req_complete_failed(req, ret); |
9e645e11 | 7032 | } |
2b188cc1 JA |
7033 | } |
7034 | ||
4652fe3f | 7035 | static void io_queue_sqe_fallback(struct io_kiocb *req) |
282cdc86 | 7036 | __must_hold(&req->ctx->uring_lock) |
4fe2c963 | 7037 | { |
4652fe3f | 7038 | if (req->flags & REQ_F_FAIL) { |
c6d3d9cb | 7039 | io_req_complete_fail_submit(req); |
e0eb71dc PB |
7040 | } else if (unlikely(req->ctx->drain_active)) { |
7041 | io_drain_req(req); | |
76cc33d7 PB |
7042 | } else { |
7043 | int ret = io_req_prep_async(req); | |
7044 | ||
7045 | if (unlikely(ret)) | |
7046 | io_req_complete_failed(req, ret); | |
7047 | else | |
f237c30a | 7048 | io_queue_async_work(req, NULL); |
ce35a47a | 7049 | } |
4fe2c963 JL |
7050 | } |
7051 | ||
4652fe3f PB |
7052 | static inline void io_queue_sqe(struct io_kiocb *req) |
7053 | __must_hold(&req->ctx->uring_lock) | |
7054 | { | |
7055 | if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) | |
7056 | __io_queue_sqe(req); | |
7057 | else | |
7058 | io_queue_sqe_fallback(req); | |
7059 | } | |
7060 | ||
b16fed66 PB |
7061 | /* |
7062 | * Check SQE restrictions (opcode and flags). | |
7063 | * | |
7064 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
7065 | */ | |
7066 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
7067 | struct io_kiocb *req, | |
7068 | unsigned int sqe_flags) | |
4fe2c963 | 7069 | { |
b16fed66 PB |
7070 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
7071 | return false; | |
7072 | ||
7073 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
7074 | ctx->restrictions.sqe_flags_required) | |
7075 | return false; | |
7076 | ||
7077 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
7078 | ctx->restrictions.sqe_flags_required)) | |
7079 | return false; | |
7080 | ||
7081 | return true; | |
4fe2c963 JL |
7082 | } |
7083 | ||
22b2ca31 PB |
7084 | static void io_init_req_drain(struct io_kiocb *req) |
7085 | { | |
7086 | struct io_ring_ctx *ctx = req->ctx; | |
7087 | struct io_kiocb *head = ctx->submit_state.link.head; | |
7088 | ||
7089 | ctx->drain_active = true; | |
7090 | if (head) { | |
7091 | /* | |
7092 | * If we need to drain a request in the middle of a link, drain | |
7093 | * the head request and the next request/link after the current | |
7094 | * link. Considering sequential execution of links, | |
7095 | * IOSQE_IO_DRAIN will be maintained for every request of our | |
7096 | * link. | |
7097 | */ | |
7098 | head->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC; | |
7099 | ctx->drain_next = true; | |
7100 | } | |
7101 | } | |
7102 | ||
b16fed66 PB |
7103 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
7104 | const struct io_uring_sqe *sqe) | |
282cdc86 | 7105 | __must_hold(&ctx->uring_lock) |
b16fed66 | 7106 | { |
b16fed66 | 7107 | unsigned int sqe_flags; |
fc0ae024 | 7108 | int personality; |
4a04d1d1 | 7109 | u8 opcode; |
b16fed66 | 7110 | |
864ea921 | 7111 | /* req is partially pre-initialised, see io_preinit_req() */ |
4a04d1d1 | 7112 | req->opcode = opcode = READ_ONCE(sqe->opcode); |
b16fed66 PB |
7113 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
7114 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
7115 | req->user_data = READ_ONCE(sqe->user_data); | |
b16fed66 | 7116 | req->file = NULL; |
b16fed66 | 7117 | req->fixed_rsrc_refs = NULL; |
b16fed66 | 7118 | req->task = current; |
b16fed66 | 7119 | |
4a04d1d1 PB |
7120 | if (unlikely(opcode >= IORING_OP_LAST)) { |
7121 | req->opcode = 0; | |
b16fed66 | 7122 | return -EINVAL; |
4a04d1d1 | 7123 | } |
68fe256a PB |
7124 | if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) { |
7125 | /* enforce forwards compatibility on users */ | |
7126 | if (sqe_flags & ~SQE_VALID_FLAGS) | |
7127 | return -EINVAL; | |
7128 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
4a04d1d1 | 7129 | !io_op_defs[opcode].buffer_select) |
68fe256a | 7130 | return -EOPNOTSUPP; |
5562a8d7 PB |
7131 | if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS) |
7132 | ctx->drain_disabled = true; | |
7133 | if (sqe_flags & IOSQE_IO_DRAIN) { | |
7134 | if (ctx->drain_disabled) | |
7135 | return -EOPNOTSUPP; | |
22b2ca31 | 7136 | io_init_req_drain(req); |
5562a8d7 | 7137 | } |
2a56a9bd PB |
7138 | } |
7139 | if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) { | |
7140 | if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags)) | |
7141 | return -EACCES; | |
7142 | /* knock it to the slow queue path, will be drained there */ | |
7143 | if (ctx->drain_active) | |
7144 | req->flags |= REQ_F_FORCE_ASYNC; | |
7145 | /* if there is no link, we're at "next" request and need to drain */ | |
7146 | if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) { | |
7147 | ctx->drain_next = false; | |
7148 | ctx->drain_active = true; | |
22b2ca31 | 7149 | req->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC; |
2a56a9bd | 7150 | } |
68fe256a | 7151 | } |
b16fed66 | 7152 | |
4a04d1d1 | 7153 | if (io_op_defs[opcode].needs_file) { |
6d63416d PB |
7154 | struct io_submit_state *state = &ctx->submit_state; |
7155 | ||
7156 | /* | |
7157 | * Plug now if we have more than 2 IO left after this, and the | |
7158 | * target is potentially a read/write to block based storage. | |
7159 | */ | |
4a04d1d1 | 7160 | if (state->need_plug && io_op_defs[opcode].plug) { |
6d63416d PB |
7161 | state->plug_started = true; |
7162 | state->need_plug = false; | |
5ca7a8b3 | 7163 | blk_start_plug_nr_ios(&state->plug, state->submit_nr); |
6d63416d PB |
7164 | } |
7165 | ||
62906e89 | 7166 | req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd), |
ac177053 | 7167 | (sqe_flags & IOSQE_FIXED_FILE)); |
b16fed66 | 7168 | if (unlikely(!req->file)) |
fc0ae024 | 7169 | return -EBADF; |
b16fed66 | 7170 | } |
863e0560 | 7171 | |
003e8dcc JA |
7172 | personality = READ_ONCE(sqe->personality); |
7173 | if (personality) { | |
cdab10bf LT |
7174 | int ret; |
7175 | ||
c10d1f98 PB |
7176 | req->creds = xa_load(&ctx->personalities, personality); |
7177 | if (!req->creds) | |
003e8dcc | 7178 | return -EINVAL; |
c10d1f98 | 7179 | get_cred(req->creds); |
cdc1404a PM |
7180 | ret = security_uring_override_creds(req->creds); |
7181 | if (ret) { | |
7182 | put_cred(req->creds); | |
7183 | return ret; | |
7184 | } | |
b8e64b53 | 7185 | req->flags |= REQ_F_CREDS; |
003e8dcc | 7186 | } |
b16fed66 | 7187 | |
fc0ae024 | 7188 | return io_req_prep(req, sqe); |
b16fed66 PB |
7189 | } |
7190 | ||
a6b8cadc | 7191 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 7192 | const struct io_uring_sqe *sqe) |
282cdc86 | 7193 | __must_hold(&ctx->uring_lock) |
9e645e11 | 7194 | { |
a1ab7b35 | 7195 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 7196 | int ret; |
9e645e11 | 7197 | |
a6b8cadc PB |
7198 | ret = io_init_req(ctx, req, sqe); |
7199 | if (unlikely(ret)) { | |
a87acfde JA |
7200 | trace_io_uring_req_failed(sqe, ret); |
7201 | ||
a8295b98 | 7202 | /* fail even hard links since we don't submit */ |
de59bc10 | 7203 | if (link->head) { |
a8295b98 HX |
7204 | /* |
7205 | * we can judge a link req is failed or cancelled by if | |
7206 | * REQ_F_FAIL is set, but the head is an exception since | |
7207 | * it may be set REQ_F_FAIL because of other req's failure | |
7208 | * so let's leverage req->result to distinguish if a head | |
7209 | * is set REQ_F_FAIL because of its failure or other req's | |
7210 | * failure so that we can set the correct ret code for it. | |
7211 | * init result here to avoid affecting the normal path. | |
7212 | */ | |
7213 | if (!(link->head->flags & REQ_F_FAIL)) | |
7214 | req_fail_link_node(link->head, -ECANCELED); | |
7215 | } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { | |
7216 | /* | |
7217 | * the current req is a normal req, we should return | |
7218 | * error and thus break the submittion loop. | |
7219 | */ | |
7220 | io_req_complete_failed(req, ret); | |
7221 | return ret; | |
de59bc10 | 7222 | } |
a8295b98 | 7223 | req_fail_link_node(req, ret); |
a6b8cadc | 7224 | } |
441b8a78 | 7225 | |
be7053b7 | 7226 | /* don't need @sqe from now on */ |
236daeae OL |
7227 | trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data, |
7228 | req->flags, true, | |
7229 | ctx->flags & IORING_SETUP_SQPOLL); | |
a6b8cadc | 7230 | |
9e645e11 JA |
7231 | /* |
7232 | * If we already have a head request, queue this one for async | |
7233 | * submittal once the head completes. If we don't have a head but | |
7234 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
7235 | * submitted sync once the chain is complete. If none of those | |
7236 | * conditions are true (normal request), then just queue it. | |
7237 | */ | |
863e0560 PB |
7238 | if (link->head) { |
7239 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 7240 | |
a8295b98 HX |
7241 | if (!(req->flags & REQ_F_FAIL)) { |
7242 | ret = io_req_prep_async(req); | |
7243 | if (unlikely(ret)) { | |
7244 | req_fail_link_node(req, ret); | |
7245 | if (!(head->flags & REQ_F_FAIL)) | |
7246 | req_fail_link_node(head, -ECANCELED); | |
7247 | } | |
7248 | } | |
9d76377f | 7249 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 7250 | link->last->link = req; |
863e0560 | 7251 | link->last = req; |
32fe525b | 7252 | |
f15a3431 PB |
7253 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) |
7254 | return 0; | |
32fe525b | 7255 | /* last request of a link, enqueue the link */ |
f15a3431 PB |
7256 | link->head = NULL; |
7257 | req = head; | |
7258 | } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { | |
7259 | link->head = req; | |
7260 | link->last = req; | |
7261 | return 0; | |
9e645e11 | 7262 | } |
2e6e1fde | 7263 | |
f15a3431 | 7264 | io_queue_sqe(req); |
1d4240cc | 7265 | return 0; |
9e645e11 JA |
7266 | } |
7267 | ||
9a56a232 JA |
7268 | /* |
7269 | * Batched submission is done, ensure local IO is flushed out. | |
7270 | */ | |
553deffd | 7271 | static void io_submit_state_end(struct io_ring_ctx *ctx) |
9a56a232 | 7272 | { |
553deffd PB |
7273 | struct io_submit_state *state = &ctx->submit_state; |
7274 | ||
a1ab7b35 | 7275 | if (state->link.head) |
de59bc10 | 7276 | io_queue_sqe(state->link.head); |
553deffd | 7277 | /* flush only after queuing links as they can generate completions */ |
c450178d | 7278 | io_submit_flush_completions(ctx); |
27926b68 JA |
7279 | if (state->plug_started) |
7280 | blk_finish_plug(&state->plug); | |
9a56a232 JA |
7281 | } |
7282 | ||
7283 | /* | |
7284 | * Start submission side cache. | |
7285 | */ | |
7286 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 7287 | unsigned int max_ios) |
9a56a232 | 7288 | { |
27926b68 | 7289 | state->plug_started = false; |
4b628aeb | 7290 | state->need_plug = max_ios > 2; |
5ca7a8b3 | 7291 | state->submit_nr = max_ios; |
a1ab7b35 PB |
7292 | /* set only head, no need to init link_last in advance */ |
7293 | state->link.head = NULL; | |
9a56a232 JA |
7294 | } |
7295 | ||
2b188cc1 JA |
7296 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
7297 | { | |
75b28aff | 7298 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 7299 | |
caf582c6 PB |
7300 | /* |
7301 | * Ensure any loads from the SQEs are done at this point, | |
7302 | * since once we write the new head, the application could | |
7303 | * write new data to them. | |
7304 | */ | |
7305 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
7306 | } |
7307 | ||
2b188cc1 | 7308 | /* |
dd9ae8a0 | 7309 | * Fetch an sqe, if one is available. Note this returns a pointer to memory |
2b188cc1 JA |
7310 | * that is mapped by userspace. This means that care needs to be taken to |
7311 | * ensure that reads are stable, as we cannot rely on userspace always | |
7312 | * being a good citizen. If members of the sqe are validated and then later | |
7313 | * used, it's important that those reads are done through READ_ONCE() to | |
7314 | * prevent a re-load down the line. | |
7315 | */ | |
709b302f | 7316 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 7317 | { |
ea5ab3b5 | 7318 | unsigned head, mask = ctx->sq_entries - 1; |
17d3aeb3 | 7319 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
2b188cc1 JA |
7320 | |
7321 | /* | |
7322 | * The cached sq head (or cq tail) serves two purposes: | |
7323 | * | |
7324 | * 1) allows us to batch the cost of updating the user visible | |
7325 | * head updates. | |
7326 | * 2) allows the kernel side to track the head on its own, even | |
7327 | * though the application is the one updating it. | |
7328 | */ | |
17d3aeb3 | 7329 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
709b302f PB |
7330 | if (likely(head < ctx->sq_entries)) |
7331 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
7332 | |
7333 | /* drop invalid entries */ | |
15641e42 PB |
7334 | ctx->cq_extra--; |
7335 | WRITE_ONCE(ctx->rings->sq_dropped, | |
7336 | READ_ONCE(ctx->rings->sq_dropped) + 1); | |
709b302f PB |
7337 | return NULL; |
7338 | } | |
7339 | ||
0f212204 | 7340 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
282cdc86 | 7341 | __must_hold(&ctx->uring_lock) |
6c271ce2 | 7342 | { |
69629809 | 7343 | unsigned int entries = io_sqring_entries(ctx); |
46c4e16a | 7344 | int submitted = 0; |
6c271ce2 | 7345 | |
51d48dab | 7346 | if (unlikely(!entries)) |
69629809 | 7347 | return 0; |
ee7d46d9 | 7348 | /* make sure SQ entry isn't read before tail */ |
69629809 | 7349 | nr = min3(nr, ctx->sq_entries, entries); |
9a10867a | 7350 | io_get_task_refs(nr); |
6c271ce2 | 7351 | |
ba88ff11 | 7352 | io_submit_state_start(&ctx->submit_state, nr); |
69629809 | 7353 | do { |
3529d8c2 | 7354 | const struct io_uring_sqe *sqe; |
196be95c | 7355 | struct io_kiocb *req; |
fb5ccc98 | 7356 | |
a33ae9ce | 7357 | if (unlikely(!io_alloc_req_refill(ctx))) { |
196be95c PB |
7358 | if (!submitted) |
7359 | submitted = -EAGAIN; | |
fb5ccc98 | 7360 | break; |
196be95c | 7361 | } |
a33ae9ce | 7362 | req = io_alloc_req(ctx); |
4fccfcbb PB |
7363 | sqe = io_get_sqe(ctx); |
7364 | if (unlikely(!sqe)) { | |
c2b6c6bc | 7365 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
4fccfcbb PB |
7366 | break; |
7367 | } | |
d3656344 JA |
7368 | /* will complete beyond this point, count as submitted */ |
7369 | submitted++; | |
a1ab7b35 | 7370 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 7371 | break; |
69629809 | 7372 | } while (submitted < nr); |
6c271ce2 | 7373 | |
9466f437 PB |
7374 | if (unlikely(submitted != nr)) { |
7375 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 | 7376 | int unused = nr - ref_used; |
9466f437 | 7377 | |
09899b19 | 7378 | current->io_uring->cached_refs += unused; |
9466f437 | 7379 | } |
6c271ce2 | 7380 | |
553deffd | 7381 | io_submit_state_end(ctx); |
ae9428ca PB |
7382 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
7383 | io_commit_sqring(ctx); | |
7384 | ||
6c271ce2 JA |
7385 | return submitted; |
7386 | } | |
7387 | ||
e4b6d902 PB |
7388 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
7389 | { | |
7390 | return READ_ONCE(sqd->state); | |
7391 | } | |
7392 | ||
23b3628e XW |
7393 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
7394 | { | |
7395 | /* Tell userspace we may need a wakeup call */ | |
79ebeaee | 7396 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7397 | WRITE_ONCE(ctx->rings->sq_flags, |
7398 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7399 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7400 | } |
7401 | ||
7402 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
7403 | { | |
79ebeaee | 7404 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7405 | WRITE_ONCE(ctx->rings->sq_flags, |
7406 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7407 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7408 | } |
7409 | ||
08369246 | 7410 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 7411 | { |
c8d1ba58 | 7412 | unsigned int to_submit; |
bdcd3eab | 7413 | int ret = 0; |
6c271ce2 | 7414 | |
c8d1ba58 | 7415 | to_submit = io_sqring_entries(ctx); |
e95eee2d | 7416 | /* if we're handling multiple rings, cap submit size for fairness */ |
4ce8ad95 OL |
7417 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
7418 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; | |
e95eee2d | 7419 | |
5eef4e87 | 7420 | if (!wq_list_empty(&ctx->iopoll_list) || to_submit) { |
948e1947 PB |
7421 | const struct cred *creds = NULL; |
7422 | ||
7423 | if (ctx->sq_creds != current_cred()) | |
7424 | creds = override_creds(ctx->sq_creds); | |
a4c0b3de | 7425 | |
c8d1ba58 | 7426 | mutex_lock(&ctx->uring_lock); |
5eef4e87 | 7427 | if (!wq_list_empty(&ctx->iopoll_list)) |
5ba3c874 | 7428 | io_do_iopoll(ctx, true); |
906a3c6f | 7429 | |
3b763ba1 PB |
7430 | /* |
7431 | * Don't submit if refs are dying, good for io_uring_register(), | |
7432 | * but also it is relied upon by io_ring_exit_work() | |
7433 | */ | |
0298ef96 PB |
7434 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
7435 | !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
08369246 | 7436 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 | 7437 | mutex_unlock(&ctx->uring_lock); |
6c271ce2 | 7438 | |
acfb381d PB |
7439 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
7440 | wake_up(&ctx->sqo_sq_wait); | |
948e1947 PB |
7441 | if (creds) |
7442 | revert_creds(creds); | |
acfb381d | 7443 | } |
6c271ce2 | 7444 | |
08369246 XW |
7445 | return ret; |
7446 | } | |
6c271ce2 | 7447 | |
c072481d | 7448 | static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
08369246 XW |
7449 | { |
7450 | struct io_ring_ctx *ctx; | |
7451 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 7452 | |
c9dca27d PB |
7453 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7454 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); | |
08369246 | 7455 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 7456 | } |
6c271ce2 | 7457 | |
e4b6d902 PB |
7458 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
7459 | { | |
7460 | bool did_sig = false; | |
7461 | struct ksignal ksig; | |
7462 | ||
7463 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || | |
7464 | signal_pending(current)) { | |
7465 | mutex_unlock(&sqd->lock); | |
7466 | if (signal_pending(current)) | |
7467 | did_sig = get_signal(&ksig); | |
7468 | cond_resched(); | |
7469 | mutex_lock(&sqd->lock); | |
7470 | } | |
e4b6d902 PB |
7471 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
7472 | } | |
7473 | ||
c8d1ba58 JA |
7474 | static int io_sq_thread(void *data) |
7475 | { | |
69fb2131 JA |
7476 | struct io_sq_data *sqd = data; |
7477 | struct io_ring_ctx *ctx; | |
a0d9205f | 7478 | unsigned long timeout = 0; |
37d1e2e3 | 7479 | char buf[TASK_COMM_LEN]; |
08369246 | 7480 | DEFINE_WAIT(wait); |
6c271ce2 | 7481 | |
696ee88a | 7482 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
37d1e2e3 | 7483 | set_task_comm(current, buf); |
37d1e2e3 JA |
7484 | |
7485 | if (sqd->sq_cpu != -1) | |
7486 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
7487 | else | |
7488 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
7489 | current->flags |= PF_NO_SETAFFINITY; | |
7490 | ||
5bd2182d PM |
7491 | audit_alloc_kernel(current); |
7492 | ||
09a6f4ef | 7493 | mutex_lock(&sqd->lock); |
e4b6d902 | 7494 | while (1) { |
1a924a80 | 7495 | bool cap_entries, sqt_spin = false; |
c1edbf5f | 7496 | |
e4b6d902 PB |
7497 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
7498 | if (io_sqd_handle_event(sqd)) | |
c7d95613 | 7499 | break; |
08369246 XW |
7500 | timeout = jiffies + sqd->sq_thread_idle; |
7501 | } | |
e4b6d902 | 7502 | |
e95eee2d | 7503 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 7504 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
948e1947 | 7505 | int ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a | 7506 | |
5eef4e87 | 7507 | if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list))) |
08369246 | 7508 | sqt_spin = true; |
69fb2131 | 7509 | } |
dd432ea5 PB |
7510 | if (io_run_task_work()) |
7511 | sqt_spin = true; | |
6c271ce2 | 7512 | |
08369246 | 7513 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 7514 | cond_resched(); |
08369246 XW |
7515 | if (sqt_spin) |
7516 | timeout = jiffies + sqd->sq_thread_idle; | |
7517 | continue; | |
7518 | } | |
7519 | ||
08369246 | 7520 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
dd432ea5 | 7521 | if (!io_sqd_events_pending(sqd) && !current->task_works) { |
1a924a80 PB |
7522 | bool needs_sched = true; |
7523 | ||
724cb4f9 | 7524 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
aaa9f0f4 PB |
7525 | io_ring_set_wakeup_flag(ctx); |
7526 | ||
724cb4f9 | 7527 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
5eef4e87 | 7528 | !wq_list_empty(&ctx->iopoll_list)) { |
724cb4f9 HX |
7529 | needs_sched = false; |
7530 | break; | |
7531 | } | |
7532 | if (io_sqring_entries(ctx)) { | |
7533 | needs_sched = false; | |
7534 | break; | |
7535 | } | |
7536 | } | |
7537 | ||
7538 | if (needs_sched) { | |
7539 | mutex_unlock(&sqd->lock); | |
7540 | schedule(); | |
7541 | mutex_lock(&sqd->lock); | |
7542 | } | |
69fb2131 JA |
7543 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7544 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 7545 | } |
08369246 XW |
7546 | |
7547 | finish_wait(&sqd->wait, &wait); | |
7548 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 | 7549 | } |
28cea78a | 7550 | |
78cc687b | 7551 | io_uring_cancel_generic(true, sqd); |
37d1e2e3 | 7552 | sqd->thread = NULL; |
05962f95 | 7553 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
5f3f26f9 | 7554 | io_ring_set_wakeup_flag(ctx); |
521d6a73 | 7555 | io_run_task_work(); |
734551df PB |
7556 | mutex_unlock(&sqd->lock); |
7557 | ||
5bd2182d PM |
7558 | audit_free(current); |
7559 | ||
37d1e2e3 JA |
7560 | complete(&sqd->exited); |
7561 | do_exit(0); | |
6c271ce2 JA |
7562 | } |
7563 | ||
bda52162 JA |
7564 | struct io_wait_queue { |
7565 | struct wait_queue_entry wq; | |
7566 | struct io_ring_ctx *ctx; | |
5fd46178 | 7567 | unsigned cq_tail; |
bda52162 JA |
7568 | unsigned nr_timeouts; |
7569 | }; | |
7570 | ||
6c503150 | 7571 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
7572 | { |
7573 | struct io_ring_ctx *ctx = iowq->ctx; | |
5fd46178 | 7574 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
bda52162 JA |
7575 | |
7576 | /* | |
d195a66e | 7577 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
7578 | * started waiting. For timeouts, we always want to return to userspace, |
7579 | * regardless of event count. | |
7580 | */ | |
5fd46178 | 7581 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
bda52162 JA |
7582 | } |
7583 | ||
7584 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
7585 | int wake_flags, void *key) | |
7586 | { | |
7587 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
7588 | wq); | |
7589 | ||
6c503150 PB |
7590 | /* |
7591 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
7592 | * the task, and the next invocation will do it. | |
7593 | */ | |
5ed7a37d | 7594 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow)) |
6c503150 PB |
7595 | return autoremove_wake_function(curr, mode, wake_flags, key); |
7596 | return -1; | |
bda52162 JA |
7597 | } |
7598 | ||
af9c1a44 JA |
7599 | static int io_run_task_work_sig(void) |
7600 | { | |
7601 | if (io_run_task_work()) | |
7602 | return 1; | |
7603 | if (!signal_pending(current)) | |
7604 | return 0; | |
0b8cfa97 | 7605 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
792ee0f6 | 7606 | return -ERESTARTSYS; |
af9c1a44 JA |
7607 | return -EINTR; |
7608 | } | |
7609 | ||
eeb60b9a PB |
7610 | /* when returns >0, the caller should retry */ |
7611 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
7612 | struct io_wait_queue *iowq, | |
7613 | signed long *timeout) | |
7614 | { | |
7615 | int ret; | |
7616 | ||
7617 | /* make sure we run task_work before checking for signals */ | |
7618 | ret = io_run_task_work_sig(); | |
7619 | if (ret || io_should_wake(iowq)) | |
7620 | return ret; | |
7621 | /* let the caller flush overflows, retry */ | |
5ed7a37d | 7622 | if (test_bit(0, &ctx->check_cq_overflow)) |
eeb60b9a PB |
7623 | return 1; |
7624 | ||
7625 | *timeout = schedule_timeout(*timeout); | |
7626 | return !*timeout ? -ETIME : 1; | |
7627 | } | |
7628 | ||
2b188cc1 JA |
7629 | /* |
7630 | * Wait until events become available, if we don't already have some. The | |
7631 | * application must reap them itself, as they reside on the shared cq ring. | |
7632 | */ | |
7633 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
7634 | const sigset_t __user *sig, size_t sigsz, |
7635 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 7636 | { |
90291099 | 7637 | struct io_wait_queue iowq; |
75b28aff | 7638 | struct io_rings *rings = ctx->rings; |
c1d5a224 PB |
7639 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
7640 | int ret; | |
2b188cc1 | 7641 | |
b41e9852 | 7642 | do { |
90f67366 | 7643 | io_cqring_overflow_flush(ctx); |
6c503150 | 7644 | if (io_cqring_events(ctx) >= min_events) |
b41e9852 | 7645 | return 0; |
4c6e277c | 7646 | if (!io_run_task_work()) |
b41e9852 | 7647 | break; |
b41e9852 | 7648 | } while (1); |
2b188cc1 | 7649 | |
44df58d4 XW |
7650 | if (uts) { |
7651 | struct timespec64 ts; | |
7652 | ||
7653 | if (get_timespec64(&ts, uts)) | |
7654 | return -EFAULT; | |
7655 | timeout = timespec64_to_jiffies(&ts); | |
7656 | } | |
7657 | ||
2b188cc1 | 7658 | if (sig) { |
9e75ad5d AB |
7659 | #ifdef CONFIG_COMPAT |
7660 | if (in_compat_syscall()) | |
7661 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 7662 | sigsz); |
9e75ad5d AB |
7663 | else |
7664 | #endif | |
b772434b | 7665 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 7666 | |
2b188cc1 JA |
7667 | if (ret) |
7668 | return ret; | |
7669 | } | |
7670 | ||
90291099 PB |
7671 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
7672 | iowq.wq.private = current; | |
7673 | INIT_LIST_HEAD(&iowq.wq.entry); | |
7674 | iowq.ctx = ctx; | |
bda52162 | 7675 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
5fd46178 | 7676 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
90291099 | 7677 | |
c826bd7a | 7678 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 7679 | do { |
ca0a2651 | 7680 | /* if we can't even flush overflow, don't wait for more */ |
90f67366 | 7681 | if (!io_cqring_overflow_flush(ctx)) { |
ca0a2651 JA |
7682 | ret = -EBUSY; |
7683 | break; | |
7684 | } | |
311997b3 | 7685 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
bda52162 | 7686 | TASK_INTERRUPTIBLE); |
eeb60b9a | 7687 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
311997b3 | 7688 | finish_wait(&ctx->cq_wait, &iowq.wq); |
ca0a2651 | 7689 | cond_resched(); |
eeb60b9a | 7690 | } while (ret > 0); |
bda52162 | 7691 | |
b7db41c9 | 7692 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7693 | |
75b28aff | 7694 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7695 | } |
7696 | ||
9123c8ff | 7697 | static void io_free_page_table(void **table, size_t size) |
05f3fb3c | 7698 | { |
9123c8ff | 7699 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
05f3fb3c | 7700 | |
846a4ef2 | 7701 | for (i = 0; i < nr_tables; i++) |
9123c8ff PB |
7702 | kfree(table[i]); |
7703 | kfree(table); | |
7704 | } | |
7705 | ||
c072481d | 7706 | static __cold void **io_alloc_page_table(size_t size) |
9123c8ff PB |
7707 | { |
7708 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); | |
7709 | size_t init_size = size; | |
7710 | void **table; | |
7711 | ||
0bea96f5 | 7712 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7713 | if (!table) |
7714 | return NULL; | |
7715 | ||
7716 | for (i = 0; i < nr_tables; i++) { | |
27f6b318 | 7717 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
9123c8ff | 7718 | |
0bea96f5 | 7719 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7720 | if (!table[i]) { |
7721 | io_free_page_table(table, init_size); | |
7722 | return NULL; | |
7723 | } | |
7724 | size -= this_size; | |
7725 | } | |
7726 | return table; | |
05f3fb3c JA |
7727 | } |
7728 | ||
28a9fe25 | 7729 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
1642b445 | 7730 | { |
28a9fe25 PB |
7731 | percpu_ref_exit(&ref_node->refs); |
7732 | kfree(ref_node); | |
1642b445 PB |
7733 | } |
7734 | ||
c072481d | 7735 | static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
b9bd2bea PB |
7736 | { |
7737 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); | |
7738 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; | |
7739 | unsigned long flags; | |
7740 | bool first_add = false; | |
7741 | ||
7742 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); | |
7743 | node->done = true; | |
7744 | ||
7745 | while (!list_empty(&ctx->rsrc_ref_list)) { | |
7746 | node = list_first_entry(&ctx->rsrc_ref_list, | |
7747 | struct io_rsrc_node, node); | |
7748 | /* recycle ref nodes in order */ | |
7749 | if (!node->done) | |
7750 | break; | |
7751 | list_del(&node->node); | |
7752 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); | |
7753 | } | |
7754 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); | |
7755 | ||
7756 | if (first_add) | |
7757 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ); | |
7758 | } | |
7759 | ||
7760 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx) | |
7761 | { | |
7762 | struct io_rsrc_node *ref_node; | |
7763 | ||
7764 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); | |
7765 | if (!ref_node) | |
7766 | return NULL; | |
7767 | ||
7768 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, | |
7769 | 0, GFP_KERNEL)) { | |
7770 | kfree(ref_node); | |
7771 | return NULL; | |
7772 | } | |
7773 | INIT_LIST_HEAD(&ref_node->node); | |
7774 | INIT_LIST_HEAD(&ref_node->rsrc_list); | |
7775 | ref_node->done = false; | |
7776 | return ref_node; | |
7777 | } | |
7778 | ||
a7f0ed5a PB |
7779 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
7780 | struct io_rsrc_data *data_to_kill) | |
ab409402 | 7781 | __must_hold(&ctx->uring_lock) |
6b06314c | 7782 | { |
a7f0ed5a PB |
7783 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
7784 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); | |
6b06314c | 7785 | |
ab409402 PB |
7786 | io_rsrc_refs_drop(ctx); |
7787 | ||
a7f0ed5a PB |
7788 | if (data_to_kill) { |
7789 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; | |
82fbcfa9 | 7790 | |
a7f0ed5a | 7791 | rsrc_node->rsrc_data = data_to_kill; |
4956b9ea | 7792 | spin_lock_irq(&ctx->rsrc_ref_lock); |
a7f0ed5a | 7793 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
4956b9ea | 7794 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
82fbcfa9 | 7795 | |
3e942498 | 7796 | atomic_inc(&data_to_kill->refs); |
a7f0ed5a PB |
7797 | percpu_ref_kill(&rsrc_node->refs); |
7798 | ctx->rsrc_node = NULL; | |
7799 | } | |
6b06314c | 7800 | |
a7f0ed5a PB |
7801 | if (!ctx->rsrc_node) { |
7802 | ctx->rsrc_node = ctx->rsrc_backup_node; | |
7803 | ctx->rsrc_backup_node = NULL; | |
7804 | } | |
8bad28d8 HX |
7805 | } |
7806 | ||
a7f0ed5a | 7807 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
8dd03afe PB |
7808 | { |
7809 | if (ctx->rsrc_backup_node) | |
7810 | return 0; | |
b895c9a6 | 7811 | ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx); |
8dd03afe | 7812 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
8bad28d8 HX |
7813 | } |
7814 | ||
c072481d PB |
7815 | static __cold int io_rsrc_ref_quiesce(struct io_rsrc_data *data, |
7816 | struct io_ring_ctx *ctx) | |
8bad28d8 HX |
7817 | { |
7818 | int ret; | |
05589553 | 7819 | |
215c3902 | 7820 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
8bad28d8 HX |
7821 | if (data->quiesce) |
7822 | return -ENXIO; | |
05589553 | 7823 | |
8bad28d8 | 7824 | data->quiesce = true; |
1ffc5422 | 7825 | do { |
a7f0ed5a | 7826 | ret = io_rsrc_node_switch_start(ctx); |
8dd03afe | 7827 | if (ret) |
f2303b1f | 7828 | break; |
a7f0ed5a | 7829 | io_rsrc_node_switch(ctx, data); |
f2303b1f | 7830 | |
3e942498 PB |
7831 | /* kill initial ref, already quiesced if zero */ |
7832 | if (atomic_dec_and_test(&data->refs)) | |
7833 | break; | |
c018db4a | 7834 | mutex_unlock(&ctx->uring_lock); |
8bad28d8 | 7835 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 | 7836 | ret = wait_for_completion_interruptible(&data->done); |
c018db4a JA |
7837 | if (!ret) { |
7838 | mutex_lock(&ctx->uring_lock); | |
1ffc5422 | 7839 | break; |
c018db4a | 7840 | } |
8bad28d8 | 7841 | |
3e942498 PB |
7842 | atomic_inc(&data->refs); |
7843 | /* wait for all works potentially completing data->done */ | |
7844 | flush_delayed_work(&ctx->rsrc_put_work); | |
cb5e1b81 | 7845 | reinit_completion(&data->done); |
8dd03afe | 7846 | |
1ffc5422 | 7847 | ret = io_run_task_work_sig(); |
8bad28d8 | 7848 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 7849 | } while (ret >= 0); |
8bad28d8 | 7850 | data->quiesce = false; |
05f3fb3c | 7851 | |
8bad28d8 | 7852 | return ret; |
d7954b2b BM |
7853 | } |
7854 | ||
2d091d62 PB |
7855 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
7856 | { | |
7857 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; | |
7858 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; | |
7859 | ||
7860 | return &data->tags[table_idx][off]; | |
7861 | } | |
7862 | ||
44b31f2f | 7863 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
1ad555c6 | 7864 | { |
2d091d62 PB |
7865 | size_t size = data->nr * sizeof(data->tags[0][0]); |
7866 | ||
7867 | if (data->tags) | |
7868 | io_free_page_table((void **)data->tags, size); | |
44b31f2f PB |
7869 | kfree(data); |
7870 | } | |
7871 | ||
c072481d PB |
7872 | static __cold int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
7873 | u64 __user *utags, unsigned nr, | |
7874 | struct io_rsrc_data **pdata) | |
1ad555c6 | 7875 | { |
b895c9a6 | 7876 | struct io_rsrc_data *data; |
2d091d62 | 7877 | int ret = -ENOMEM; |
d878c816 | 7878 | unsigned i; |
1ad555c6 BM |
7879 | |
7880 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7881 | if (!data) | |
d878c816 | 7882 | return -ENOMEM; |
2d091d62 | 7883 | data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0])); |
b60c8dce | 7884 | if (!data->tags) { |
1ad555c6 | 7885 | kfree(data); |
d878c816 PB |
7886 | return -ENOMEM; |
7887 | } | |
2d091d62 PB |
7888 | |
7889 | data->nr = nr; | |
7890 | data->ctx = ctx; | |
7891 | data->do_put = do_put; | |
d878c816 | 7892 | if (utags) { |
2d091d62 | 7893 | ret = -EFAULT; |
d878c816 | 7894 | for (i = 0; i < nr; i++) { |
fdd1dc31 CIK |
7895 | u64 *tag_slot = io_get_tag_slot(data, i); |
7896 | ||
7897 | if (copy_from_user(tag_slot, &utags[i], | |
7898 | sizeof(*tag_slot))) | |
2d091d62 | 7899 | goto fail; |
d878c816 | 7900 | } |
1ad555c6 | 7901 | } |
b60c8dce | 7902 | |
3e942498 | 7903 | atomic_set(&data->refs, 1); |
1ad555c6 | 7904 | init_completion(&data->done); |
d878c816 PB |
7905 | *pdata = data; |
7906 | return 0; | |
2d091d62 PB |
7907 | fail: |
7908 | io_rsrc_data_free(data); | |
7909 | return ret; | |
1ad555c6 BM |
7910 | } |
7911 | ||
9123c8ff PB |
7912 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
7913 | { | |
0bea96f5 PB |
7914 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
7915 | GFP_KERNEL_ACCOUNT); | |
9123c8ff PB |
7916 | return !!table->files; |
7917 | } | |
7918 | ||
042b0d85 | 7919 | static void io_free_file_tables(struct io_file_table *table) |
9123c8ff | 7920 | { |
042b0d85 | 7921 | kvfree(table->files); |
9123c8ff PB |
7922 | table->files = NULL; |
7923 | } | |
7924 | ||
fff4db76 | 7925 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
1ad555c6 | 7926 | { |
fff4db76 PB |
7927 | #if defined(CONFIG_UNIX) |
7928 | if (ctx->ring_sock) { | |
7929 | struct sock *sock = ctx->ring_sock->sk; | |
7930 | struct sk_buff *skb; | |
7931 | ||
7932 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
7933 | kfree_skb(skb); | |
7934 | } | |
7935 | #else | |
7936 | int i; | |
7937 | ||
7938 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7939 | struct file *file; | |
7940 | ||
7941 | file = io_file_from_index(ctx, i); | |
7942 | if (file) | |
7943 | fput(file); | |
7944 | } | |
7945 | #endif | |
042b0d85 | 7946 | io_free_file_tables(&ctx->file_table); |
44b31f2f | 7947 | io_rsrc_data_free(ctx->file_data); |
fff4db76 PB |
7948 | ctx->file_data = NULL; |
7949 | ctx->nr_user_files = 0; | |
1ad555c6 BM |
7950 | } |
7951 | ||
d7954b2b BM |
7952 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7953 | { | |
d7954b2b BM |
7954 | int ret; |
7955 | ||
08480400 | 7956 | if (!ctx->file_data) |
d7954b2b | 7957 | return -ENXIO; |
08480400 PB |
7958 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
7959 | if (!ret) | |
7960 | __io_sqe_files_unregister(ctx); | |
7961 | return ret; | |
6b06314c JA |
7962 | } |
7963 | ||
37d1e2e3 | 7964 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
09a6f4ef | 7965 | __releases(&sqd->lock) |
37d1e2e3 | 7966 | { |
521d6a73 PB |
7967 | WARN_ON_ONCE(sqd->thread == current); |
7968 | ||
9e138a48 PB |
7969 | /* |
7970 | * Do the dance but not conditional clear_bit() because it'd race with | |
7971 | * other threads incrementing park_pending and setting the bit. | |
7972 | */ | |
37d1e2e3 | 7973 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
9e138a48 PB |
7974 | if (atomic_dec_return(&sqd->park_pending)) |
7975 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
09a6f4ef | 7976 | mutex_unlock(&sqd->lock); |
37d1e2e3 JA |
7977 | } |
7978 | ||
86e0d676 | 7979 | static void io_sq_thread_park(struct io_sq_data *sqd) |
09a6f4ef | 7980 | __acquires(&sqd->lock) |
37d1e2e3 | 7981 | { |
521d6a73 PB |
7982 | WARN_ON_ONCE(sqd->thread == current); |
7983 | ||
9e138a48 | 7984 | atomic_inc(&sqd->park_pending); |
86e0d676 | 7985 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
09a6f4ef | 7986 | mutex_lock(&sqd->lock); |
05962f95 | 7987 | if (sqd->thread) |
86e0d676 | 7988 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
7989 | } |
7990 | ||
7991 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
7992 | { | |
521d6a73 | 7993 | WARN_ON_ONCE(sqd->thread == current); |
88885f66 | 7994 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
521d6a73 | 7995 | |
05962f95 | 7996 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
88885f66 | 7997 | mutex_lock(&sqd->lock); |
e8f98f24 JA |
7998 | if (sqd->thread) |
7999 | wake_up_process(sqd->thread); | |
09a6f4ef | 8000 | mutex_unlock(&sqd->lock); |
05962f95 | 8001 | wait_for_completion(&sqd->exited); |
37d1e2e3 JA |
8002 | } |
8003 | ||
534ca6d6 | 8004 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 8005 | { |
534ca6d6 | 8006 | if (refcount_dec_and_test(&sqd->refs)) { |
9e138a48 PB |
8007 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
8008 | ||
37d1e2e3 JA |
8009 | io_sq_thread_stop(sqd); |
8010 | kfree(sqd); | |
8011 | } | |
8012 | } | |
8013 | ||
8014 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
8015 | { | |
8016 | struct io_sq_data *sqd = ctx->sq_data; | |
8017 | ||
8018 | if (sqd) { | |
05962f95 | 8019 | io_sq_thread_park(sqd); |
521d6a73 | 8020 | list_del_init(&ctx->sqd_list); |
37d1e2e3 | 8021 | io_sqd_update_thread_idle(sqd); |
05962f95 | 8022 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
8023 | |
8024 | io_put_sq_data(sqd); | |
8025 | ctx->sq_data = NULL; | |
534ca6d6 JA |
8026 | } |
8027 | } | |
8028 | ||
aa06165d JA |
8029 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
8030 | { | |
8031 | struct io_ring_ctx *ctx_attach; | |
8032 | struct io_sq_data *sqd; | |
8033 | struct fd f; | |
8034 | ||
8035 | f = fdget(p->wq_fd); | |
8036 | if (!f.file) | |
8037 | return ERR_PTR(-ENXIO); | |
8038 | if (f.file->f_op != &io_uring_fops) { | |
8039 | fdput(f); | |
8040 | return ERR_PTR(-EINVAL); | |
8041 | } | |
8042 | ||
8043 | ctx_attach = f.file->private_data; | |
8044 | sqd = ctx_attach->sq_data; | |
8045 | if (!sqd) { | |
8046 | fdput(f); | |
8047 | return ERR_PTR(-EINVAL); | |
8048 | } | |
5c2469e0 JA |
8049 | if (sqd->task_tgid != current->tgid) { |
8050 | fdput(f); | |
8051 | return ERR_PTR(-EPERM); | |
8052 | } | |
aa06165d JA |
8053 | |
8054 | refcount_inc(&sqd->refs); | |
8055 | fdput(f); | |
8056 | return sqd; | |
8057 | } | |
8058 | ||
26984fbf PB |
8059 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
8060 | bool *attached) | |
534ca6d6 JA |
8061 | { |
8062 | struct io_sq_data *sqd; | |
8063 | ||
26984fbf | 8064 | *attached = false; |
5c2469e0 JA |
8065 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
8066 | sqd = io_attach_sq_data(p); | |
26984fbf PB |
8067 | if (!IS_ERR(sqd)) { |
8068 | *attached = true; | |
5c2469e0 | 8069 | return sqd; |
26984fbf | 8070 | } |
5c2469e0 JA |
8071 | /* fall through for EPERM case, setup new sqd/task */ |
8072 | if (PTR_ERR(sqd) != -EPERM) | |
8073 | return sqd; | |
8074 | } | |
aa06165d | 8075 | |
534ca6d6 JA |
8076 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
8077 | if (!sqd) | |
8078 | return ERR_PTR(-ENOMEM); | |
8079 | ||
9e138a48 | 8080 | atomic_set(&sqd->park_pending, 0); |
534ca6d6 | 8081 | refcount_set(&sqd->refs, 1); |
69fb2131 | 8082 | INIT_LIST_HEAD(&sqd->ctx_list); |
09a6f4ef | 8083 | mutex_init(&sqd->lock); |
534ca6d6 | 8084 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 8085 | init_completion(&sqd->exited); |
534ca6d6 JA |
8086 | return sqd; |
8087 | } | |
8088 | ||
6b06314c | 8089 | #if defined(CONFIG_UNIX) |
6b06314c JA |
8090 | /* |
8091 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
8092 | * the io_uring can be safely unregistered on process exit, even if we have | |
8093 | * loops in the file referencing. | |
8094 | */ | |
8095 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
8096 | { | |
8097 | struct sock *sk = ctx->ring_sock->sk; | |
8098 | struct scm_fp_list *fpl; | |
8099 | struct sk_buff *skb; | |
08a45173 | 8100 | int i, nr_files; |
6b06314c | 8101 | |
6b06314c JA |
8102 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
8103 | if (!fpl) | |
8104 | return -ENOMEM; | |
8105 | ||
8106 | skb = alloc_skb(0, GFP_KERNEL); | |
8107 | if (!skb) { | |
8108 | kfree(fpl); | |
8109 | return -ENOMEM; | |
8110 | } | |
8111 | ||
8112 | skb->sk = sk; | |
6b06314c | 8113 | |
08a45173 | 8114 | nr_files = 0; |
62e398be | 8115 | fpl->user = get_uid(current_user()); |
6b06314c | 8116 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
8117 | struct file *file = io_file_from_index(ctx, i + offset); |
8118 | ||
8119 | if (!file) | |
08a45173 | 8120 | continue; |
65e19f54 | 8121 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
8122 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
8123 | nr_files++; | |
6b06314c JA |
8124 | } |
8125 | ||
08a45173 JA |
8126 | if (nr_files) { |
8127 | fpl->max = SCM_MAX_FD; | |
8128 | fpl->count = nr_files; | |
8129 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 8130 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
8131 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
8132 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 8133 | |
08a45173 JA |
8134 | for (i = 0; i < nr_files; i++) |
8135 | fput(fpl->fp[i]); | |
8136 | } else { | |
8137 | kfree_skb(skb); | |
8138 | kfree(fpl); | |
8139 | } | |
6b06314c JA |
8140 | |
8141 | return 0; | |
8142 | } | |
8143 | ||
8144 | /* | |
8145 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
8146 | * causes regular reference counting to break down. We rely on the UNIX | |
8147 | * garbage collection to take care of this problem for us. | |
8148 | */ | |
8149 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8150 | { | |
8151 | unsigned left, total; | |
8152 | int ret = 0; | |
8153 | ||
8154 | total = 0; | |
8155 | left = ctx->nr_user_files; | |
8156 | while (left) { | |
8157 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
8158 | |
8159 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
8160 | if (ret) | |
8161 | break; | |
8162 | left -= this_files; | |
8163 | total += this_files; | |
8164 | } | |
8165 | ||
8166 | if (!ret) | |
8167 | return 0; | |
8168 | ||
8169 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
8170 | struct file *file = io_file_from_index(ctx, total); |
8171 | ||
8172 | if (file) | |
8173 | fput(file); | |
6b06314c JA |
8174 | total++; |
8175 | } | |
8176 | ||
8177 | return ret; | |
8178 | } | |
8179 | #else | |
8180 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8181 | { | |
8182 | return 0; | |
8183 | } | |
8184 | #endif | |
8185 | ||
47e90392 | 8186 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 8187 | { |
50238531 | 8188 | struct file *file = prsrc->file; |
05f3fb3c JA |
8189 | #if defined(CONFIG_UNIX) |
8190 | struct sock *sock = ctx->ring_sock->sk; | |
8191 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
8192 | struct sk_buff *skb; | |
8193 | int i; | |
8194 | ||
8195 | __skb_queue_head_init(&list); | |
8196 | ||
8197 | /* | |
8198 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
8199 | * remove this entry and rearrange the file array. | |
8200 | */ | |
8201 | skb = skb_dequeue(head); | |
8202 | while (skb) { | |
8203 | struct scm_fp_list *fp; | |
8204 | ||
8205 | fp = UNIXCB(skb).fp; | |
8206 | for (i = 0; i < fp->count; i++) { | |
8207 | int left; | |
8208 | ||
8209 | if (fp->fp[i] != file) | |
8210 | continue; | |
8211 | ||
8212 | unix_notinflight(fp->user, fp->fp[i]); | |
8213 | left = fp->count - 1 - i; | |
8214 | if (left) { | |
8215 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
8216 | left * sizeof(struct file *)); | |
8217 | } | |
8218 | fp->count--; | |
8219 | if (!fp->count) { | |
8220 | kfree_skb(skb); | |
8221 | skb = NULL; | |
8222 | } else { | |
8223 | __skb_queue_tail(&list, skb); | |
8224 | } | |
8225 | fput(file); | |
8226 | file = NULL; | |
8227 | break; | |
8228 | } | |
8229 | ||
8230 | if (!file) | |
8231 | break; | |
8232 | ||
8233 | __skb_queue_tail(&list, skb); | |
8234 | ||
8235 | skb = skb_dequeue(head); | |
8236 | } | |
8237 | ||
8238 | if (skb_peek(&list)) { | |
8239 | spin_lock_irq(&head->lock); | |
8240 | while ((skb = __skb_dequeue(&list)) != NULL) | |
8241 | __skb_queue_tail(head, skb); | |
8242 | spin_unlock_irq(&head->lock); | |
8243 | } | |
8244 | #else | |
8245 | fput(file); | |
8246 | #endif | |
8247 | } | |
8248 | ||
b895c9a6 | 8249 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
65e19f54 | 8250 | { |
b895c9a6 | 8251 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
269bbe5f BM |
8252 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
8253 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 8254 | |
269bbe5f BM |
8255 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
8256 | list_del(&prsrc->list); | |
b60c8dce PB |
8257 | |
8258 | if (prsrc->tag) { | |
8259 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; | |
b60c8dce PB |
8260 | |
8261 | io_ring_submit_lock(ctx, lock_ring); | |
79ebeaee | 8262 | spin_lock(&ctx->completion_lock); |
913a571a | 8263 | io_fill_cqe_aux(ctx, prsrc->tag, 0, 0); |
b60c8dce | 8264 | io_commit_cqring(ctx); |
79ebeaee | 8265 | spin_unlock(&ctx->completion_lock); |
b60c8dce PB |
8266 | io_cqring_ev_posted(ctx); |
8267 | io_ring_submit_unlock(ctx, lock_ring); | |
8268 | } | |
8269 | ||
40ae0ff7 | 8270 | rsrc_data->do_put(ctx, prsrc); |
269bbe5f | 8271 | kfree(prsrc); |
65e19f54 | 8272 | } |
05589553 | 8273 | |
28a9fe25 | 8274 | io_rsrc_node_destroy(ref_node); |
3e942498 PB |
8275 | if (atomic_dec_and_test(&rsrc_data->refs)) |
8276 | complete(&rsrc_data->done); | |
2faf852d | 8277 | } |
65e19f54 | 8278 | |
269bbe5f | 8279 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
8280 | { |
8281 | struct io_ring_ctx *ctx; | |
8282 | struct llist_node *node; | |
8283 | ||
269bbe5f BM |
8284 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
8285 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
8286 | |
8287 | while (node) { | |
b895c9a6 | 8288 | struct io_rsrc_node *ref_node; |
4a38aed2 JA |
8289 | struct llist_node *next = node->next; |
8290 | ||
b895c9a6 | 8291 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
269bbe5f | 8292 | __io_rsrc_put_work(ref_node); |
4a38aed2 JA |
8293 | node = next; |
8294 | } | |
8295 | } | |
8296 | ||
6b06314c | 8297 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
792e3582 | 8298 | unsigned nr_args, u64 __user *tags) |
6b06314c JA |
8299 | { |
8300 | __s32 __user *fds = (__s32 __user *) arg; | |
05f3fb3c | 8301 | struct file *file; |
f3baed39 | 8302 | int fd, ret; |
846a4ef2 | 8303 | unsigned i; |
6b06314c | 8304 | |
05f3fb3c | 8305 | if (ctx->file_data) |
6b06314c JA |
8306 | return -EBUSY; |
8307 | if (!nr_args) | |
8308 | return -EINVAL; | |
8309 | if (nr_args > IORING_MAX_FIXED_FILES) | |
8310 | return -EMFILE; | |
3a1b8a4e PB |
8311 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
8312 | return -EMFILE; | |
a7f0ed5a | 8313 | ret = io_rsrc_node_switch_start(ctx); |
f3baed39 PB |
8314 | if (ret) |
8315 | return ret; | |
d878c816 PB |
8316 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
8317 | &ctx->file_data); | |
8318 | if (ret) | |
8319 | return ret; | |
6b06314c | 8320 | |
f3baed39 | 8321 | ret = -ENOMEM; |
aeca241b | 8322 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
1ad555c6 | 8323 | goto out_free; |
65e19f54 | 8324 | |
08a45173 | 8325 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
d878c816 | 8326 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
600cf3f8 PB |
8327 | ret = -EFAULT; |
8328 | goto out_fput; | |
8329 | } | |
08a45173 | 8330 | /* allow sparse sets */ |
792e3582 PB |
8331 | if (fd == -1) { |
8332 | ret = -EINVAL; | |
2d091d62 | 8333 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
792e3582 | 8334 | goto out_fput; |
08a45173 | 8335 | continue; |
792e3582 | 8336 | } |
6b06314c | 8337 | |
05f3fb3c | 8338 | file = fget(fd); |
6b06314c | 8339 | ret = -EBADF; |
792e3582 | 8340 | if (unlikely(!file)) |
600cf3f8 | 8341 | goto out_fput; |
05f3fb3c | 8342 | |
6b06314c JA |
8343 | /* |
8344 | * Don't allow io_uring instances to be registered. If UNIX | |
8345 | * isn't enabled, then this causes a reference cycle and this | |
8346 | * instance can never get freed. If UNIX is enabled we'll | |
8347 | * handle it just fine, but there's still no point in allowing | |
8348 | * a ring fd as it doesn't support regular read/write anyway. | |
8349 | */ | |
05f3fb3c JA |
8350 | if (file->f_op == &io_uring_fops) { |
8351 | fput(file); | |
600cf3f8 | 8352 | goto out_fput; |
6b06314c | 8353 | } |
aeca241b | 8354 | io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file); |
6b06314c JA |
8355 | } |
8356 | ||
6b06314c | 8357 | ret = io_sqe_files_scm(ctx); |
05589553 | 8358 | if (ret) { |
08480400 | 8359 | __io_sqe_files_unregister(ctx); |
05589553 XW |
8360 | return ret; |
8361 | } | |
6b06314c | 8362 | |
a7f0ed5a | 8363 | io_rsrc_node_switch(ctx, NULL); |
6b06314c | 8364 | return ret; |
600cf3f8 PB |
8365 | out_fput: |
8366 | for (i = 0; i < ctx->nr_user_files; i++) { | |
8367 | file = io_file_from_index(ctx, i); | |
8368 | if (file) | |
8369 | fput(file); | |
8370 | } | |
042b0d85 | 8371 | io_free_file_tables(&ctx->file_table); |
600cf3f8 | 8372 | ctx->nr_user_files = 0; |
600cf3f8 | 8373 | out_free: |
44b31f2f | 8374 | io_rsrc_data_free(ctx->file_data); |
55cbc256 | 8375 | ctx->file_data = NULL; |
6b06314c JA |
8376 | return ret; |
8377 | } | |
8378 | ||
c3a31e60 JA |
8379 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
8380 | int index) | |
8381 | { | |
8382 | #if defined(CONFIG_UNIX) | |
8383 | struct sock *sock = ctx->ring_sock->sk; | |
8384 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
8385 | struct sk_buff *skb; | |
8386 | ||
8387 | /* | |
8388 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
8389 | * file set. If there's no room, fall back to allocating a new skb | |
8390 | * and filling it in. | |
8391 | */ | |
8392 | spin_lock_irq(&head->lock); | |
8393 | skb = skb_peek(head); | |
8394 | if (skb) { | |
8395 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
8396 | ||
8397 | if (fpl->count < SCM_MAX_FD) { | |
8398 | __skb_unlink(skb, head); | |
8399 | spin_unlock_irq(&head->lock); | |
8400 | fpl->fp[fpl->count] = get_file(file); | |
8401 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
8402 | fpl->count++; | |
8403 | spin_lock_irq(&head->lock); | |
8404 | __skb_queue_head(head, skb); | |
8405 | } else { | |
8406 | skb = NULL; | |
8407 | } | |
8408 | } | |
8409 | spin_unlock_irq(&head->lock); | |
8410 | ||
8411 | if (skb) { | |
8412 | fput(file); | |
8413 | return 0; | |
8414 | } | |
8415 | ||
8416 | return __io_sqe_files_scm(ctx, 1, index); | |
8417 | #else | |
8418 | return 0; | |
8419 | #endif | |
8420 | } | |
8421 | ||
9c7b0ba8 PB |
8422 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
8423 | struct io_rsrc_node *node, void *rsrc) | |
8424 | { | |
8425 | struct io_rsrc_put *prsrc; | |
8426 | ||
8427 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); | |
8428 | if (!prsrc) | |
8429 | return -ENOMEM; | |
8430 | ||
8431 | prsrc->tag = *io_get_tag_slot(data, idx); | |
8432 | prsrc->rsrc = rsrc; | |
8433 | list_add(&prsrc->list, &node->rsrc_list); | |
8434 | return 0; | |
8435 | } | |
8436 | ||
b9445598 PB |
8437 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
8438 | unsigned int issue_flags, u32 slot_index) | |
8439 | { | |
8440 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 8441 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
9c7b0ba8 | 8442 | bool needs_switch = false; |
b9445598 PB |
8443 | struct io_fixed_file *file_slot; |
8444 | int ret = -EBADF; | |
8445 | ||
3b44b371 | 8446 | io_ring_submit_lock(ctx, needs_lock); |
b9445598 PB |
8447 | if (file->f_op == &io_uring_fops) |
8448 | goto err; | |
8449 | ret = -ENXIO; | |
8450 | if (!ctx->file_data) | |
8451 | goto err; | |
8452 | ret = -EINVAL; | |
8453 | if (slot_index >= ctx->nr_user_files) | |
8454 | goto err; | |
8455 | ||
8456 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); | |
8457 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); | |
9c7b0ba8 PB |
8458 | |
8459 | if (file_slot->file_ptr) { | |
8460 | struct file *old_file; | |
8461 | ||
8462 | ret = io_rsrc_node_switch_start(ctx); | |
8463 | if (ret) | |
8464 | goto err; | |
8465 | ||
8466 | old_file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8467 | ret = io_queue_rsrc_removal(ctx->file_data, slot_index, | |
8468 | ctx->rsrc_node, old_file); | |
8469 | if (ret) | |
8470 | goto err; | |
8471 | file_slot->file_ptr = 0; | |
8472 | needs_switch = true; | |
8473 | } | |
b9445598 PB |
8474 | |
8475 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; | |
8476 | io_fixed_file_set(file_slot, file); | |
8477 | ret = io_sqe_file_register(ctx, file, slot_index); | |
8478 | if (ret) { | |
8479 | file_slot->file_ptr = 0; | |
8480 | goto err; | |
8481 | } | |
8482 | ||
8483 | ret = 0; | |
8484 | err: | |
9c7b0ba8 PB |
8485 | if (needs_switch) |
8486 | io_rsrc_node_switch(ctx, ctx->file_data); | |
3b44b371 | 8487 | io_ring_submit_unlock(ctx, needs_lock); |
b9445598 PB |
8488 | if (ret) |
8489 | fput(file); | |
8490 | return ret; | |
8491 | } | |
8492 | ||
7df778be PB |
8493 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) |
8494 | { | |
8495 | unsigned int offset = req->close.file_slot - 1; | |
8496 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 8497 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
7df778be PB |
8498 | struct io_fixed_file *file_slot; |
8499 | struct file *file; | |
8500 | int ret, i; | |
8501 | ||
3b44b371 | 8502 | io_ring_submit_lock(ctx, needs_lock); |
7df778be PB |
8503 | ret = -ENXIO; |
8504 | if (unlikely(!ctx->file_data)) | |
8505 | goto out; | |
8506 | ret = -EINVAL; | |
8507 | if (offset >= ctx->nr_user_files) | |
8508 | goto out; | |
8509 | ret = io_rsrc_node_switch_start(ctx); | |
8510 | if (ret) | |
8511 | goto out; | |
8512 | ||
8513 | i = array_index_nospec(offset, ctx->nr_user_files); | |
8514 | file_slot = io_fixed_file_slot(&ctx->file_table, i); | |
8515 | ret = -EBADF; | |
8516 | if (!file_slot->file_ptr) | |
8517 | goto out; | |
8518 | ||
8519 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8520 | ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file); | |
8521 | if (ret) | |
8522 | goto out; | |
8523 | ||
8524 | file_slot->file_ptr = 0; | |
8525 | io_rsrc_node_switch(ctx, ctx->file_data); | |
8526 | ret = 0; | |
8527 | out: | |
3b44b371 | 8528 | io_ring_submit_unlock(ctx, needs_lock); |
7df778be PB |
8529 | return ret; |
8530 | } | |
8531 | ||
05f3fb3c | 8532 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
c3bdad02 | 8533 | struct io_uring_rsrc_update2 *up, |
05f3fb3c JA |
8534 | unsigned nr_args) |
8535 | { | |
c3bdad02 | 8536 | u64 __user *tags = u64_to_user_ptr(up->tags); |
98f0b3b4 | 8537 | __s32 __user *fds = u64_to_user_ptr(up->data); |
b895c9a6 | 8538 | struct io_rsrc_data *data = ctx->file_data; |
a04b0ac0 PB |
8539 | struct io_fixed_file *file_slot; |
8540 | struct file *file; | |
98f0b3b4 PB |
8541 | int fd, i, err = 0; |
8542 | unsigned int done; | |
05589553 | 8543 | bool needs_switch = false; |
c3a31e60 | 8544 | |
98f0b3b4 PB |
8545 | if (!ctx->file_data) |
8546 | return -ENXIO; | |
8547 | if (up->offset + nr_args > ctx->nr_user_files) | |
c3a31e60 JA |
8548 | return -EINVAL; |
8549 | ||
67973b93 | 8550 | for (done = 0; done < nr_args; done++) { |
c3bdad02 PB |
8551 | u64 tag = 0; |
8552 | ||
8553 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || | |
8554 | copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
c3a31e60 JA |
8555 | err = -EFAULT; |
8556 | break; | |
8557 | } | |
c3bdad02 PB |
8558 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
8559 | err = -EINVAL; | |
8560 | break; | |
8561 | } | |
4e0377a1 | 8562 | if (fd == IORING_REGISTER_FILES_SKIP) |
8563 | continue; | |
8564 | ||
67973b93 | 8565 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
aeca241b | 8566 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
ea64ec02 | 8567 | |
a04b0ac0 PB |
8568 | if (file_slot->file_ptr) { |
8569 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
b60c8dce PB |
8570 | err = io_queue_rsrc_removal(data, up->offset + done, |
8571 | ctx->rsrc_node, file); | |
a5318d3c HD |
8572 | if (err) |
8573 | break; | |
a04b0ac0 | 8574 | file_slot->file_ptr = 0; |
05589553 | 8575 | needs_switch = true; |
c3a31e60 JA |
8576 | } |
8577 | if (fd != -1) { | |
c3a31e60 JA |
8578 | file = fget(fd); |
8579 | if (!file) { | |
8580 | err = -EBADF; | |
8581 | break; | |
8582 | } | |
8583 | /* | |
8584 | * Don't allow io_uring instances to be registered. If | |
8585 | * UNIX isn't enabled, then this causes a reference | |
8586 | * cycle and this instance can never get freed. If UNIX | |
8587 | * is enabled we'll handle it just fine, but there's | |
8588 | * still no point in allowing a ring fd as it doesn't | |
8589 | * support regular read/write anyway. | |
8590 | */ | |
8591 | if (file->f_op == &io_uring_fops) { | |
8592 | fput(file); | |
8593 | err = -EBADF; | |
8594 | break; | |
8595 | } | |
2d091d62 | 8596 | *io_get_tag_slot(data, up->offset + done) = tag; |
9a321c98 | 8597 | io_fixed_file_set(file_slot, file); |
c3a31e60 | 8598 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 8599 | if (err) { |
a04b0ac0 | 8600 | file_slot->file_ptr = 0; |
f3bd9dae | 8601 | fput(file); |
c3a31e60 | 8602 | break; |
f3bd9dae | 8603 | } |
c3a31e60 | 8604 | } |
05f3fb3c JA |
8605 | } |
8606 | ||
a7f0ed5a PB |
8607 | if (needs_switch) |
8608 | io_rsrc_node_switch(ctx, data); | |
c3a31e60 JA |
8609 | return done ? done : err; |
8610 | } | |
05589553 | 8611 | |
685fe7fe JA |
8612 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
8613 | struct task_struct *task) | |
24369c2e | 8614 | { |
e941894e | 8615 | struct io_wq_hash *hash; |
24369c2e | 8616 | struct io_wq_data data; |
24369c2e | 8617 | unsigned int concurrency; |
24369c2e | 8618 | |
362a9e65 | 8619 | mutex_lock(&ctx->uring_lock); |
e941894e JA |
8620 | hash = ctx->hash_map; |
8621 | if (!hash) { | |
8622 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
362a9e65 YY |
8623 | if (!hash) { |
8624 | mutex_unlock(&ctx->uring_lock); | |
e941894e | 8625 | return ERR_PTR(-ENOMEM); |
362a9e65 | 8626 | } |
e941894e JA |
8627 | refcount_set(&hash->refs, 1); |
8628 | init_waitqueue_head(&hash->wait); | |
8629 | ctx->hash_map = hash; | |
24369c2e | 8630 | } |
362a9e65 | 8631 | mutex_unlock(&ctx->uring_lock); |
24369c2e | 8632 | |
e941894e | 8633 | data.hash = hash; |
685fe7fe | 8634 | data.task = task; |
ebc11b6c | 8635 | data.free_work = io_wq_free_work; |
f5fa38c5 | 8636 | data.do_work = io_wq_submit_work; |
24369c2e | 8637 | |
d25e3a3d JA |
8638 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
8639 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 8640 | |
5aa75ed5 | 8641 | return io_wq_create(concurrency, &data); |
24369c2e PB |
8642 | } |
8643 | ||
c072481d PB |
8644 | static __cold int io_uring_alloc_task_context(struct task_struct *task, |
8645 | struct io_ring_ctx *ctx) | |
0f212204 JA |
8646 | { |
8647 | struct io_uring_task *tctx; | |
d8a6df10 | 8648 | int ret; |
0f212204 | 8649 | |
09899b19 | 8650 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
0f212204 JA |
8651 | if (unlikely(!tctx)) |
8652 | return -ENOMEM; | |
8653 | ||
d8a6df10 JA |
8654 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
8655 | if (unlikely(ret)) { | |
8656 | kfree(tctx); | |
8657 | return ret; | |
8658 | } | |
8659 | ||
685fe7fe | 8660 | tctx->io_wq = io_init_wq_offload(ctx, task); |
5aa75ed5 JA |
8661 | if (IS_ERR(tctx->io_wq)) { |
8662 | ret = PTR_ERR(tctx->io_wq); | |
8663 | percpu_counter_destroy(&tctx->inflight); | |
8664 | kfree(tctx); | |
8665 | return ret; | |
8666 | } | |
8667 | ||
0f212204 JA |
8668 | xa_init(&tctx->xa); |
8669 | init_waitqueue_head(&tctx->wait); | |
fdaf083c | 8670 | atomic_set(&tctx->in_idle, 0); |
b303fe2e | 8671 | atomic_set(&tctx->inflight_tracked, 0); |
0f212204 | 8672 | task->io_uring = tctx; |
7cbf1722 JA |
8673 | spin_lock_init(&tctx->task_lock); |
8674 | INIT_WQ_LIST(&tctx->task_list); | |
7cbf1722 | 8675 | init_task_work(&tctx->task_work, tctx_task_work); |
0f212204 JA |
8676 | return 0; |
8677 | } | |
8678 | ||
8679 | void __io_uring_free(struct task_struct *tsk) | |
8680 | { | |
8681 | struct io_uring_task *tctx = tsk->io_uring; | |
8682 | ||
8683 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e | 8684 | WARN_ON_ONCE(tctx->io_wq); |
09899b19 | 8685 | WARN_ON_ONCE(tctx->cached_refs); |
ef8eaa4e | 8686 | |
d8a6df10 | 8687 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
8688 | kfree(tctx); |
8689 | tsk->io_uring = NULL; | |
8690 | } | |
8691 | ||
c072481d PB |
8692 | static __cold int io_sq_offload_create(struct io_ring_ctx *ctx, |
8693 | struct io_uring_params *p) | |
2b188cc1 JA |
8694 | { |
8695 | int ret; | |
8696 | ||
d25e3a3d JA |
8697 | /* Retain compatibility with failing for an invalid attach attempt */ |
8698 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
8699 | IORING_SETUP_ATTACH_WQ) { | |
8700 | struct fd f; | |
8701 | ||
8702 | f = fdget(p->wq_fd); | |
8703 | if (!f.file) | |
8704 | return -ENXIO; | |
0cc936f7 JA |
8705 | if (f.file->f_op != &io_uring_fops) { |
8706 | fdput(f); | |
f2a48dd0 | 8707 | return -EINVAL; |
0cc936f7 JA |
8708 | } |
8709 | fdput(f); | |
d25e3a3d | 8710 | } |
6c271ce2 | 8711 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 8712 | struct task_struct *tsk; |
534ca6d6 | 8713 | struct io_sq_data *sqd; |
26984fbf | 8714 | bool attached; |
534ca6d6 | 8715 | |
cdc1404a PM |
8716 | ret = security_uring_sqpoll(); |
8717 | if (ret) | |
8718 | return ret; | |
8719 | ||
26984fbf | 8720 | sqd = io_get_sq_data(p, &attached); |
534ca6d6 JA |
8721 | if (IS_ERR(sqd)) { |
8722 | ret = PTR_ERR(sqd); | |
8723 | goto err; | |
8724 | } | |
69fb2131 | 8725 | |
7c30f36a | 8726 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 8727 | ctx->sq_data = sqd; |
917257da JA |
8728 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
8729 | if (!ctx->sq_thread_idle) | |
8730 | ctx->sq_thread_idle = HZ; | |
8731 | ||
78d7f6ba | 8732 | io_sq_thread_park(sqd); |
de75a3d3 PB |
8733 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
8734 | io_sqd_update_thread_idle(sqd); | |
26984fbf | 8735 | /* don't attach to a dying SQPOLL thread, would be racy */ |
f2a48dd0 | 8736 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
78d7f6ba PB |
8737 | io_sq_thread_unpark(sqd); |
8738 | ||
de75a3d3 PB |
8739 | if (ret < 0) |
8740 | goto err; | |
8741 | if (attached) | |
5aa75ed5 | 8742 | return 0; |
aa06165d | 8743 | |
6c271ce2 | 8744 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 8745 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 8746 | |
917257da | 8747 | ret = -EINVAL; |
f2a48dd0 | 8748 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
e8f98f24 | 8749 | goto err_sqpoll; |
37d1e2e3 | 8750 | sqd->sq_cpu = cpu; |
6c271ce2 | 8751 | } else { |
37d1e2e3 | 8752 | sqd->sq_cpu = -1; |
6c271ce2 | 8753 | } |
37d1e2e3 JA |
8754 | |
8755 | sqd->task_pid = current->pid; | |
5c2469e0 | 8756 | sqd->task_tgid = current->tgid; |
46fe18b1 JA |
8757 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
8758 | if (IS_ERR(tsk)) { | |
8759 | ret = PTR_ERR(tsk); | |
e8f98f24 | 8760 | goto err_sqpoll; |
6c271ce2 | 8761 | } |
97a73a0f | 8762 | |
46fe18b1 | 8763 | sqd->thread = tsk; |
97a73a0f | 8764 | ret = io_uring_alloc_task_context(tsk, ctx); |
46fe18b1 | 8765 | wake_up_new_task(tsk); |
0f212204 JA |
8766 | if (ret) |
8767 | goto err; | |
6c271ce2 JA |
8768 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
8769 | /* Can't have SQ_AFF without SQPOLL */ | |
8770 | ret = -EINVAL; | |
8771 | goto err; | |
8772 | } | |
8773 | ||
2b188cc1 | 8774 | return 0; |
f2a48dd0 PB |
8775 | err_sqpoll: |
8776 | complete(&ctx->sq_data->exited); | |
2b188cc1 | 8777 | err: |
37d1e2e3 | 8778 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
8779 | return ret; |
8780 | } | |
8781 | ||
a087e2b5 BM |
8782 | static inline void __io_unaccount_mem(struct user_struct *user, |
8783 | unsigned long nr_pages) | |
2b188cc1 JA |
8784 | { |
8785 | atomic_long_sub(nr_pages, &user->locked_vm); | |
8786 | } | |
8787 | ||
a087e2b5 BM |
8788 | static inline int __io_account_mem(struct user_struct *user, |
8789 | unsigned long nr_pages) | |
2b188cc1 JA |
8790 | { |
8791 | unsigned long page_limit, cur_pages, new_pages; | |
8792 | ||
8793 | /* Don't allow more pages than we can safely lock */ | |
8794 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
8795 | ||
8796 | do { | |
8797 | cur_pages = atomic_long_read(&user->locked_vm); | |
8798 | new_pages = cur_pages + nr_pages; | |
8799 | if (new_pages > page_limit) | |
8800 | return -ENOMEM; | |
8801 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
8802 | new_pages) != cur_pages); | |
8803 | ||
8804 | return 0; | |
8805 | } | |
8806 | ||
26bfa89e | 8807 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8808 | { |
62e398be | 8809 | if (ctx->user) |
a087e2b5 | 8810 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 8811 | |
26bfa89e JA |
8812 | if (ctx->mm_account) |
8813 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8814 | } |
8815 | ||
26bfa89e | 8816 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8817 | { |
30975825 BM |
8818 | int ret; |
8819 | ||
62e398be | 8820 | if (ctx->user) { |
30975825 BM |
8821 | ret = __io_account_mem(ctx->user, nr_pages); |
8822 | if (ret) | |
8823 | return ret; | |
8824 | } | |
8825 | ||
26bfa89e JA |
8826 | if (ctx->mm_account) |
8827 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8828 | |
8829 | return 0; | |
8830 | } | |
8831 | ||
2b188cc1 JA |
8832 | static void io_mem_free(void *ptr) |
8833 | { | |
52e04ef4 MR |
8834 | struct page *page; |
8835 | ||
8836 | if (!ptr) | |
8837 | return; | |
2b188cc1 | 8838 | |
52e04ef4 | 8839 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8840 | if (put_page_testzero(page)) |
8841 | free_compound_page(page); | |
8842 | } | |
8843 | ||
8844 | static void *io_mem_alloc(size_t size) | |
8845 | { | |
8846 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
26bfa89e | 8847 | __GFP_NORETRY | __GFP_ACCOUNT; |
2b188cc1 JA |
8848 | |
8849 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8850 | } | |
8851 | ||
75b28aff HV |
8852 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8853 | size_t *sq_offset) | |
8854 | { | |
8855 | struct io_rings *rings; | |
8856 | size_t off, sq_array_size; | |
8857 | ||
8858 | off = struct_size(rings, cqes, cq_entries); | |
8859 | if (off == SIZE_MAX) | |
8860 | return SIZE_MAX; | |
8861 | ||
8862 | #ifdef CONFIG_SMP | |
8863 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8864 | if (off == 0) | |
8865 | return SIZE_MAX; | |
8866 | #endif | |
8867 | ||
b36200f5 DV |
8868 | if (sq_offset) |
8869 | *sq_offset = off; | |
8870 | ||
75b28aff HV |
8871 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8872 | if (sq_array_size == SIZE_MAX) | |
8873 | return SIZE_MAX; | |
8874 | ||
8875 | if (check_add_overflow(off, sq_array_size, &off)) | |
8876 | return SIZE_MAX; | |
8877 | ||
75b28aff HV |
8878 | return off; |
8879 | } | |
8880 | ||
41edf1a5 | 8881 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot) |
7f61a1e9 | 8882 | { |
41edf1a5 | 8883 | struct io_mapped_ubuf *imu = *slot; |
7f61a1e9 PB |
8884 | unsigned int i; |
8885 | ||
6224843d PB |
8886 | if (imu != ctx->dummy_ubuf) { |
8887 | for (i = 0; i < imu->nr_bvecs; i++) | |
8888 | unpin_user_page(imu->bvec[i].bv_page); | |
8889 | if (imu->acct_pages) | |
8890 | io_unaccount_mem(ctx, imu->acct_pages); | |
8891 | kvfree(imu); | |
8892 | } | |
41edf1a5 | 8893 | *slot = NULL; |
7f61a1e9 PB |
8894 | } |
8895 | ||
bd54b6fe | 8896 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
edafccee | 8897 | { |
634d00df PB |
8898 | io_buffer_unmap(ctx, &prsrc->buf); |
8899 | prsrc->buf = NULL; | |
bd54b6fe | 8900 | } |
edafccee | 8901 | |
bd54b6fe BM |
8902 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
8903 | { | |
8904 | unsigned int i; | |
edafccee | 8905 | |
7f61a1e9 PB |
8906 | for (i = 0; i < ctx->nr_user_bufs; i++) |
8907 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); | |
edafccee | 8908 | kfree(ctx->user_bufs); |
bb6659cc | 8909 | io_rsrc_data_free(ctx->buf_data); |
edafccee | 8910 | ctx->user_bufs = NULL; |
bd54b6fe | 8911 | ctx->buf_data = NULL; |
edafccee | 8912 | ctx->nr_user_bufs = 0; |
bd54b6fe BM |
8913 | } |
8914 | ||
0a96bbe4 | 8915 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee | 8916 | { |
bd54b6fe | 8917 | int ret; |
edafccee | 8918 | |
bd54b6fe | 8919 | if (!ctx->buf_data) |
edafccee JA |
8920 | return -ENXIO; |
8921 | ||
bd54b6fe BM |
8922 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
8923 | if (!ret) | |
8924 | __io_sqe_buffers_unregister(ctx); | |
8925 | return ret; | |
edafccee JA |
8926 | } |
8927 | ||
8928 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8929 | void __user *arg, unsigned index) | |
8930 | { | |
8931 | struct iovec __user *src; | |
8932 | ||
8933 | #ifdef CONFIG_COMPAT | |
8934 | if (ctx->compat) { | |
8935 | struct compat_iovec __user *ciovs; | |
8936 | struct compat_iovec ciov; | |
8937 | ||
8938 | ciovs = (struct compat_iovec __user *) arg; | |
8939 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8940 | return -EFAULT; | |
8941 | ||
d55e5f5b | 8942 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8943 | dst->iov_len = ciov.iov_len; |
8944 | return 0; | |
8945 | } | |
8946 | #endif | |
8947 | src = (struct iovec __user *) arg; | |
8948 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8949 | return -EFAULT; | |
8950 | return 0; | |
8951 | } | |
8952 | ||
de293938 JA |
8953 | /* |
8954 | * Not super efficient, but this is just a registration time. And we do cache | |
8955 | * the last compound head, so generally we'll only do a full search if we don't | |
8956 | * match that one. | |
8957 | * | |
8958 | * We check if the given compound head page has already been accounted, to | |
8959 | * avoid double accounting it. This allows us to account the full size of the | |
8960 | * page, not just the constituent pages of a huge page. | |
8961 | */ | |
8962 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8963 | int nr_pages, struct page *hpage) | |
8964 | { | |
8965 | int i, j; | |
8966 | ||
8967 | /* check current page array */ | |
8968 | for (i = 0; i < nr_pages; i++) { | |
8969 | if (!PageCompound(pages[i])) | |
8970 | continue; | |
8971 | if (compound_head(pages[i]) == hpage) | |
8972 | return true; | |
8973 | } | |
8974 | ||
8975 | /* check previously registered pages */ | |
8976 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
41edf1a5 | 8977 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
de293938 JA |
8978 | |
8979 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8980 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8981 | continue; | |
8982 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8983 | return true; | |
8984 | } | |
8985 | } | |
8986 | ||
8987 | return false; | |
8988 | } | |
8989 | ||
8990 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8991 | int nr_pages, struct io_mapped_ubuf *imu, | |
8992 | struct page **last_hpage) | |
8993 | { | |
8994 | int i, ret; | |
8995 | ||
216e5835 | 8996 | imu->acct_pages = 0; |
de293938 JA |
8997 | for (i = 0; i < nr_pages; i++) { |
8998 | if (!PageCompound(pages[i])) { | |
8999 | imu->acct_pages++; | |
9000 | } else { | |
9001 | struct page *hpage; | |
9002 | ||
9003 | hpage = compound_head(pages[i]); | |
9004 | if (hpage == *last_hpage) | |
9005 | continue; | |
9006 | *last_hpage = hpage; | |
9007 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
9008 | continue; | |
9009 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
9010 | } | |
9011 | } | |
9012 | ||
9013 | if (!imu->acct_pages) | |
9014 | return 0; | |
9015 | ||
26bfa89e | 9016 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
9017 | if (ret) |
9018 | imu->acct_pages = 0; | |
9019 | return ret; | |
9020 | } | |
9021 | ||
0a96bbe4 | 9022 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
41edf1a5 | 9023 | struct io_mapped_ubuf **pimu, |
0a96bbe4 | 9024 | struct page **last_hpage) |
edafccee | 9025 | { |
41edf1a5 | 9026 | struct io_mapped_ubuf *imu = NULL; |
edafccee JA |
9027 | struct vm_area_struct **vmas = NULL; |
9028 | struct page **pages = NULL; | |
0a96bbe4 BM |
9029 | unsigned long off, start, end, ubuf; |
9030 | size_t size; | |
9031 | int ret, pret, nr_pages, i; | |
9032 | ||
6224843d PB |
9033 | if (!iov->iov_base) { |
9034 | *pimu = ctx->dummy_ubuf; | |
9035 | return 0; | |
9036 | } | |
9037 | ||
0a96bbe4 BM |
9038 | ubuf = (unsigned long) iov->iov_base; |
9039 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
9040 | start = ubuf >> PAGE_SHIFT; | |
9041 | nr_pages = end - start; | |
9042 | ||
41edf1a5 | 9043 | *pimu = NULL; |
0a96bbe4 BM |
9044 | ret = -ENOMEM; |
9045 | ||
9046 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
9047 | if (!pages) | |
9048 | goto done; | |
9049 | ||
9050 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
9051 | GFP_KERNEL); | |
9052 | if (!vmas) | |
9053 | goto done; | |
edafccee | 9054 | |
41edf1a5 | 9055 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
a2b4198c | 9056 | if (!imu) |
0a96bbe4 BM |
9057 | goto done; |
9058 | ||
9059 | ret = 0; | |
9060 | mmap_read_lock(current->mm); | |
9061 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
9062 | pages, vmas); | |
9063 | if (pret == nr_pages) { | |
9064 | /* don't support file backed memory */ | |
9065 | for (i = 0; i < nr_pages; i++) { | |
9066 | struct vm_area_struct *vma = vmas[i]; | |
9067 | ||
40dad765 PB |
9068 | if (vma_is_shmem(vma)) |
9069 | continue; | |
0a96bbe4 BM |
9070 | if (vma->vm_file && |
9071 | !is_file_hugepages(vma->vm_file)) { | |
9072 | ret = -EOPNOTSUPP; | |
9073 | break; | |
9074 | } | |
9075 | } | |
9076 | } else { | |
9077 | ret = pret < 0 ? pret : -EFAULT; | |
9078 | } | |
9079 | mmap_read_unlock(current->mm); | |
9080 | if (ret) { | |
9081 | /* | |
9082 | * if we did partial map, or found file backed vmas, | |
9083 | * release any pages we did get | |
9084 | */ | |
9085 | if (pret > 0) | |
9086 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9087 | goto done; |
9088 | } | |
9089 | ||
9090 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
9091 | if (ret) { | |
9092 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9093 | goto done; |
9094 | } | |
9095 | ||
9096 | off = ubuf & ~PAGE_MASK; | |
9097 | size = iov->iov_len; | |
9098 | for (i = 0; i < nr_pages; i++) { | |
9099 | size_t vec_len; | |
9100 | ||
9101 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
9102 | imu->bvec[i].bv_page = pages[i]; | |
9103 | imu->bvec[i].bv_len = vec_len; | |
9104 | imu->bvec[i].bv_offset = off; | |
9105 | off = 0; | |
9106 | size -= vec_len; | |
9107 | } | |
9108 | /* store original address for later verification */ | |
9109 | imu->ubuf = ubuf; | |
4751f53d | 9110 | imu->ubuf_end = ubuf + iov->iov_len; |
0a96bbe4 | 9111 | imu->nr_bvecs = nr_pages; |
41edf1a5 | 9112 | *pimu = imu; |
0a96bbe4 BM |
9113 | ret = 0; |
9114 | done: | |
41edf1a5 PB |
9115 | if (ret) |
9116 | kvfree(imu); | |
0a96bbe4 BM |
9117 | kvfree(pages); |
9118 | kvfree(vmas); | |
9119 | return ret; | |
9120 | } | |
9121 | ||
2b358604 | 9122 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 9123 | { |
87094465 PB |
9124 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
9125 | return ctx->user_bufs ? 0 : -ENOMEM; | |
2b358604 | 9126 | } |
edafccee | 9127 | |
2b358604 BM |
9128 | static int io_buffer_validate(struct iovec *iov) |
9129 | { | |
50e96989 PB |
9130 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
9131 | ||
2b358604 BM |
9132 | /* |
9133 | * Don't impose further limits on the size and buffer | |
9134 | * constraints here, we'll -EINVAL later when IO is | |
9135 | * submitted if they are wrong. | |
9136 | */ | |
6224843d PB |
9137 | if (!iov->iov_base) |
9138 | return iov->iov_len ? -EFAULT : 0; | |
9139 | if (!iov->iov_len) | |
2b358604 | 9140 | return -EFAULT; |
edafccee | 9141 | |
2b358604 BM |
9142 | /* arbitrary limit, but we need something */ |
9143 | if (iov->iov_len > SZ_1G) | |
9144 | return -EFAULT; | |
edafccee | 9145 | |
50e96989 PB |
9146 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
9147 | return -EOVERFLOW; | |
9148 | ||
2b358604 BM |
9149 | return 0; |
9150 | } | |
edafccee | 9151 | |
2b358604 | 9152 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
634d00df | 9153 | unsigned int nr_args, u64 __user *tags) |
2b358604 | 9154 | { |
bd54b6fe BM |
9155 | struct page *last_hpage = NULL; |
9156 | struct io_rsrc_data *data; | |
2b358604 BM |
9157 | int i, ret; |
9158 | struct iovec iov; | |
edafccee | 9159 | |
87094465 PB |
9160 | if (ctx->user_bufs) |
9161 | return -EBUSY; | |
489809e2 | 9162 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
87094465 | 9163 | return -EINVAL; |
bd54b6fe | 9164 | ret = io_rsrc_node_switch_start(ctx); |
2b358604 BM |
9165 | if (ret) |
9166 | return ret; | |
d878c816 PB |
9167 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
9168 | if (ret) | |
9169 | return ret; | |
bd54b6fe BM |
9170 | ret = io_buffers_map_alloc(ctx, nr_args); |
9171 | if (ret) { | |
bb6659cc | 9172 | io_rsrc_data_free(data); |
bd54b6fe BM |
9173 | return ret; |
9174 | } | |
edafccee | 9175 | |
87094465 | 9176 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
edafccee JA |
9177 | ret = io_copy_iov(ctx, &iov, arg, i); |
9178 | if (ret) | |
0a96bbe4 | 9179 | break; |
2b358604 BM |
9180 | ret = io_buffer_validate(&iov); |
9181 | if (ret) | |
0a96bbe4 | 9182 | break; |
2d091d62 | 9183 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
cf3770e7 CIK |
9184 | ret = -EINVAL; |
9185 | break; | |
9186 | } | |
edafccee | 9187 | |
41edf1a5 PB |
9188 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
9189 | &last_hpage); | |
0a96bbe4 BM |
9190 | if (ret) |
9191 | break; | |
edafccee | 9192 | } |
0a96bbe4 | 9193 | |
bd54b6fe | 9194 | WARN_ON_ONCE(ctx->buf_data); |
0a96bbe4 | 9195 | |
bd54b6fe BM |
9196 | ctx->buf_data = data; |
9197 | if (ret) | |
9198 | __io_sqe_buffers_unregister(ctx); | |
9199 | else | |
9200 | io_rsrc_node_switch(ctx, NULL); | |
edafccee JA |
9201 | return ret; |
9202 | } | |
9203 | ||
634d00df PB |
9204 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
9205 | struct io_uring_rsrc_update2 *up, | |
9206 | unsigned int nr_args) | |
9207 | { | |
9208 | u64 __user *tags = u64_to_user_ptr(up->tags); | |
9209 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); | |
634d00df PB |
9210 | struct page *last_hpage = NULL; |
9211 | bool needs_switch = false; | |
9212 | __u32 done; | |
9213 | int i, err; | |
9214 | ||
9215 | if (!ctx->buf_data) | |
9216 | return -ENXIO; | |
9217 | if (up->offset + nr_args > ctx->nr_user_bufs) | |
9218 | return -EINVAL; | |
9219 | ||
9220 | for (done = 0; done < nr_args; done++) { | |
0b8c0e7c PB |
9221 | struct io_mapped_ubuf *imu; |
9222 | int offset = up->offset + done; | |
634d00df PB |
9223 | u64 tag = 0; |
9224 | ||
9225 | err = io_copy_iov(ctx, &iov, iovs, done); | |
9226 | if (err) | |
9227 | break; | |
9228 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { | |
9229 | err = -EFAULT; | |
9230 | break; | |
9231 | } | |
0b8c0e7c PB |
9232 | err = io_buffer_validate(&iov); |
9233 | if (err) | |
9234 | break; | |
cf3770e7 CIK |
9235 | if (!iov.iov_base && tag) { |
9236 | err = -EINVAL; | |
9237 | break; | |
9238 | } | |
0b8c0e7c PB |
9239 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
9240 | if (err) | |
9241 | break; | |
634d00df | 9242 | |
0b8c0e7c | 9243 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
6224843d | 9244 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
0b8c0e7c PB |
9245 | err = io_queue_rsrc_removal(ctx->buf_data, offset, |
9246 | ctx->rsrc_node, ctx->user_bufs[i]); | |
9247 | if (unlikely(err)) { | |
9248 | io_buffer_unmap(ctx, &imu); | |
634d00df | 9249 | break; |
0b8c0e7c | 9250 | } |
634d00df PB |
9251 | ctx->user_bufs[i] = NULL; |
9252 | needs_switch = true; | |
9253 | } | |
9254 | ||
0b8c0e7c | 9255 | ctx->user_bufs[i] = imu; |
2d091d62 | 9256 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
634d00df PB |
9257 | } |
9258 | ||
9259 | if (needs_switch) | |
9260 | io_rsrc_node_switch(ctx, ctx->buf_data); | |
9261 | return done ? done : err; | |
9262 | } | |
9263 | ||
9b402849 JA |
9264 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
9265 | { | |
9266 | __s32 __user *fds = arg; | |
9267 | int fd; | |
9268 | ||
9269 | if (ctx->cq_ev_fd) | |
9270 | return -EBUSY; | |
9271 | ||
9272 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
9273 | return -EFAULT; | |
9274 | ||
9275 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
9276 | if (IS_ERR(ctx->cq_ev_fd)) { | |
9277 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
fe7e3257 | 9278 | |
9b402849 JA |
9279 | ctx->cq_ev_fd = NULL; |
9280 | return ret; | |
9281 | } | |
9282 | ||
9283 | return 0; | |
9284 | } | |
9285 | ||
9286 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
9287 | { | |
9288 | if (ctx->cq_ev_fd) { | |
9289 | eventfd_ctx_put(ctx->cq_ev_fd); | |
9290 | ctx->cq_ev_fd = NULL; | |
9291 | return 0; | |
9292 | } | |
9293 | ||
9294 | return -ENXIO; | |
9295 | } | |
9296 | ||
5a2e745d JA |
9297 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
9298 | { | |
9e15c3a0 JA |
9299 | struct io_buffer *buf; |
9300 | unsigned long index; | |
9301 | ||
8bab4c09 | 9302 | xa_for_each(&ctx->io_buffers, index, buf) { |
9e15c3a0 | 9303 | __io_remove_buffers(ctx, buf, index, -1U); |
8bab4c09 JA |
9304 | cond_resched(); |
9305 | } | |
5a2e745d JA |
9306 | } |
9307 | ||
4010fec4 | 9308 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9309 | { |
cd0ca2e0 | 9310 | struct io_submit_state *state = &ctx->submit_state; |
37f0e767 | 9311 | int nr = 0; |
bf019da7 | 9312 | |
9a4fdbd8 | 9313 | mutex_lock(&ctx->uring_lock); |
cd0ca2e0 | 9314 | io_flush_cached_locked_reqs(ctx, state); |
9a4fdbd8 | 9315 | |
c2b6c6bc PB |
9316 | while (state->free_list.next) { |
9317 | struct io_wq_work_node *node; | |
9318 | struct io_kiocb *req; | |
9a4fdbd8 | 9319 | |
c2b6c6bc PB |
9320 | node = wq_stack_extract(&state->free_list); |
9321 | req = container_of(node, struct io_kiocb, comp_list); | |
9322 | kmem_cache_free(req_cachep, req); | |
37f0e767 | 9323 | nr++; |
c2b6c6bc | 9324 | } |
37f0e767 PB |
9325 | if (nr) |
9326 | percpu_ref_put_many(&ctx->refs, nr); | |
9a4fdbd8 JA |
9327 | mutex_unlock(&ctx->uring_lock); |
9328 | } | |
9329 | ||
43597aac | 9330 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
2b188cc1 | 9331 | { |
43597aac | 9332 | if (data && !atomic_dec_and_test(&data->refs)) |
bd54b6fe | 9333 | wait_for_completion(&data->done); |
bd54b6fe | 9334 | } |
04fc6c80 | 9335 | |
c072481d | 9336 | static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9337 | { |
37d1e2e3 | 9338 | io_sq_thread_finish(ctx); |
2aede0e4 | 9339 | |
37d1e2e3 | 9340 | if (ctx->mm_account) { |
2aede0e4 JA |
9341 | mmdrop(ctx->mm_account); |
9342 | ctx->mm_account = NULL; | |
30975825 | 9343 | } |
def596e9 | 9344 | |
ab409402 | 9345 | io_rsrc_refs_drop(ctx); |
43597aac PB |
9346 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
9347 | io_wait_rsrc_data(ctx->buf_data); | |
9348 | io_wait_rsrc_data(ctx->file_data); | |
9349 | ||
8bad28d8 | 9350 | mutex_lock(&ctx->uring_lock); |
43597aac | 9351 | if (ctx->buf_data) |
bd54b6fe | 9352 | __io_sqe_buffers_unregister(ctx); |
43597aac | 9353 | if (ctx->file_data) |
08480400 | 9354 | __io_sqe_files_unregister(ctx); |
c4ea060e PB |
9355 | if (ctx->rings) |
9356 | __io_cqring_overflow_flush(ctx, true); | |
8bad28d8 | 9357 | mutex_unlock(&ctx->uring_lock); |
9b402849 | 9358 | io_eventfd_unregister(ctx); |
5a2e745d | 9359 | io_destroy_buffers(ctx); |
07db298a PB |
9360 | if (ctx->sq_creds) |
9361 | put_cred(ctx->sq_creds); | |
def596e9 | 9362 | |
a7f0ed5a PB |
9363 | /* there are no registered resources left, nobody uses it */ |
9364 | if (ctx->rsrc_node) | |
9365 | io_rsrc_node_destroy(ctx->rsrc_node); | |
8dd03afe | 9366 | if (ctx->rsrc_backup_node) |
b895c9a6 | 9367 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
a7f0ed5a | 9368 | flush_delayed_work(&ctx->rsrc_put_work); |
756ab7c0 | 9369 | flush_delayed_work(&ctx->fallback_work); |
a7f0ed5a PB |
9370 | |
9371 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); | |
9372 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); | |
def596e9 | 9373 | |
2b188cc1 | 9374 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
9375 | if (ctx->ring_sock) { |
9376 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 9377 | sock_release(ctx->ring_sock); |
355e8d26 | 9378 | } |
2b188cc1 | 9379 | #endif |
ef9dd637 | 9380 | WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); |
2b188cc1 | 9381 | |
75b28aff | 9382 | io_mem_free(ctx->rings); |
2b188cc1 | 9383 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
9384 | |
9385 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 9386 | free_uid(ctx->user); |
4010fec4 | 9387 | io_req_caches_free(ctx); |
e941894e JA |
9388 | if (ctx->hash_map) |
9389 | io_wq_put_hash(ctx->hash_map); | |
78076bb6 | 9390 | kfree(ctx->cancel_hash); |
6224843d | 9391 | kfree(ctx->dummy_ubuf); |
2b188cc1 JA |
9392 | kfree(ctx); |
9393 | } | |
9394 | ||
9395 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
9396 | { | |
9397 | struct io_ring_ctx *ctx = file->private_data; | |
9398 | __poll_t mask = 0; | |
9399 | ||
d60aa65b | 9400 | poll_wait(file, &ctx->cq_wait, wait); |
4f7067c3 SB |
9401 | /* |
9402 | * synchronizes with barrier from wq_has_sleeper call in | |
9403 | * io_commit_cqring | |
9404 | */ | |
2b188cc1 | 9405 | smp_rmb(); |
90554200 | 9406 | if (!io_sqring_full(ctx)) |
2b188cc1 | 9407 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
9408 | |
9409 | /* | |
9410 | * Don't flush cqring overflow list here, just do a simple check. | |
9411 | * Otherwise there could possible be ABBA deadlock: | |
9412 | * CPU0 CPU1 | |
9413 | * ---- ---- | |
9414 | * lock(&ctx->uring_lock); | |
9415 | * lock(&ep->mtx); | |
9416 | * lock(&ctx->uring_lock); | |
9417 | * lock(&ep->mtx); | |
9418 | * | |
9419 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
9420 | * pushs them to do the flush. | |
9421 | */ | |
5ed7a37d | 9422 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
2b188cc1 JA |
9423 | mask |= EPOLLIN | EPOLLRDNORM; |
9424 | ||
9425 | return mask; | |
9426 | } | |
9427 | ||
0bead8cd | 9428 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 9429 | { |
4379bf8b | 9430 | const struct cred *creds; |
071698e1 | 9431 | |
61cf9370 | 9432 | creds = xa_erase(&ctx->personalities, id); |
4379bf8b JA |
9433 | if (creds) { |
9434 | put_cred(creds); | |
0bead8cd | 9435 | return 0; |
1e6fa521 | 9436 | } |
0bead8cd YD |
9437 | |
9438 | return -EINVAL; | |
9439 | } | |
9440 | ||
d56d938b PB |
9441 | struct io_tctx_exit { |
9442 | struct callback_head task_work; | |
9443 | struct completion completion; | |
baf186c4 | 9444 | struct io_ring_ctx *ctx; |
d56d938b PB |
9445 | }; |
9446 | ||
c072481d | 9447 | static __cold void io_tctx_exit_cb(struct callback_head *cb) |
d56d938b PB |
9448 | { |
9449 | struct io_uring_task *tctx = current->io_uring; | |
9450 | struct io_tctx_exit *work; | |
9451 | ||
9452 | work = container_of(cb, struct io_tctx_exit, task_work); | |
9453 | /* | |
9454 | * When @in_idle, we're in cancellation and it's racy to remove the | |
9455 | * node. It'll be removed by the end of cancellation, just ignore it. | |
9456 | */ | |
9457 | if (!atomic_read(&tctx->in_idle)) | |
eef51daa | 9458 | io_uring_del_tctx_node((unsigned long)work->ctx); |
d56d938b PB |
9459 | complete(&work->completion); |
9460 | } | |
9461 | ||
c072481d | 9462 | static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
28090c13 PB |
9463 | { |
9464 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
9465 | ||
9466 | return req->ctx == data; | |
9467 | } | |
9468 | ||
c072481d | 9469 | static __cold void io_ring_exit_work(struct work_struct *work) |
85faa7b8 | 9470 | { |
d56d938b | 9471 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 9472 | unsigned long timeout = jiffies + HZ * 60 * 5; |
58d3be2c | 9473 | unsigned long interval = HZ / 20; |
d56d938b PB |
9474 | struct io_tctx_exit exit; |
9475 | struct io_tctx_node *node; | |
9476 | int ret; | |
85faa7b8 | 9477 | |
56952e91 JA |
9478 | /* |
9479 | * If we're doing polled IO and end up having requests being | |
9480 | * submitted async (out-of-line), then completions can come in while | |
9481 | * we're waiting for refs to drop. We need to reap these manually, | |
9482 | * as nobody else will be looking for them. | |
9483 | */ | |
b2edc0a7 | 9484 | do { |
3dd0c97a | 9485 | io_uring_try_cancel_requests(ctx, NULL, true); |
28090c13 PB |
9486 | if (ctx->sq_data) { |
9487 | struct io_sq_data *sqd = ctx->sq_data; | |
9488 | struct task_struct *tsk; | |
9489 | ||
9490 | io_sq_thread_park(sqd); | |
9491 | tsk = sqd->thread; | |
9492 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) | |
9493 | io_wq_cancel_cb(tsk->io_uring->io_wq, | |
9494 | io_cancel_ctx_cb, ctx, true); | |
9495 | io_sq_thread_unpark(sqd); | |
9496 | } | |
b5bb3a24 | 9497 | |
37f0e767 PB |
9498 | io_req_caches_free(ctx); |
9499 | ||
58d3be2c PB |
9500 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
9501 | /* there is little hope left, don't run it too often */ | |
9502 | interval = HZ * 60; | |
9503 | } | |
9504 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); | |
d56d938b | 9505 | |
7f00651a PB |
9506 | init_completion(&exit.completion); |
9507 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
9508 | exit.ctx = ctx; | |
89b5066e PB |
9509 | /* |
9510 | * Some may use context even when all refs and requests have been put, | |
9511 | * and they are free to do so while still holding uring_lock or | |
5b0a6acc | 9512 | * completion_lock, see io_req_task_submit(). Apart from other work, |
89b5066e PB |
9513 | * this lock/unlock section also waits them to finish. |
9514 | */ | |
d56d938b PB |
9515 | mutex_lock(&ctx->uring_lock); |
9516 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
9517 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
9518 | ||
d56d938b PB |
9519 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
9520 | ctx_node); | |
7f00651a PB |
9521 | /* don't spin on a single task if cancellation failed */ |
9522 | list_rotate_left(&ctx->tctx_list); | |
d56d938b PB |
9523 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
9524 | if (WARN_ON_ONCE(ret)) | |
9525 | continue; | |
d56d938b PB |
9526 | |
9527 | mutex_unlock(&ctx->uring_lock); | |
9528 | wait_for_completion(&exit.completion); | |
d56d938b PB |
9529 | mutex_lock(&ctx->uring_lock); |
9530 | } | |
9531 | mutex_unlock(&ctx->uring_lock); | |
79ebeaee JA |
9532 | spin_lock(&ctx->completion_lock); |
9533 | spin_unlock(&ctx->completion_lock); | |
d56d938b | 9534 | |
85faa7b8 JA |
9535 | io_ring_ctx_free(ctx); |
9536 | } | |
9537 | ||
80c4cbdb | 9538 | /* Returns true if we found and killed one or more timeouts */ |
c072481d PB |
9539 | static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx, |
9540 | struct task_struct *tsk, bool cancel_all) | |
80c4cbdb PB |
9541 | { |
9542 | struct io_kiocb *req, *tmp; | |
9543 | int canceled = 0; | |
9544 | ||
79ebeaee JA |
9545 | spin_lock(&ctx->completion_lock); |
9546 | spin_lock_irq(&ctx->timeout_lock); | |
80c4cbdb | 9547 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
3dd0c97a | 9548 | if (io_match_task(req, tsk, cancel_all)) { |
80c4cbdb PB |
9549 | io_kill_timeout(req, -ECANCELED); |
9550 | canceled++; | |
9551 | } | |
9552 | } | |
79ebeaee | 9553 | spin_unlock_irq(&ctx->timeout_lock); |
51520426 PB |
9554 | if (canceled != 0) |
9555 | io_commit_cqring(ctx); | |
79ebeaee | 9556 | spin_unlock(&ctx->completion_lock); |
80c4cbdb PB |
9557 | if (canceled != 0) |
9558 | io_cqring_ev_posted(ctx); | |
9559 | return canceled != 0; | |
9560 | } | |
9561 | ||
c072481d | 9562 | static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
2b188cc1 | 9563 | { |
61cf9370 MWO |
9564 | unsigned long index; |
9565 | struct creds *creds; | |
9566 | ||
2b188cc1 JA |
9567 | mutex_lock(&ctx->uring_lock); |
9568 | percpu_ref_kill(&ctx->refs); | |
634578f8 | 9569 | if (ctx->rings) |
6c2450ae | 9570 | __io_cqring_overflow_flush(ctx, true); |
61cf9370 MWO |
9571 | xa_for_each(&ctx->personalities, index, creds) |
9572 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
9573 | mutex_unlock(&ctx->uring_lock); |
9574 | ||
3dd0c97a PB |
9575 | io_kill_timeouts(ctx, NULL, true); |
9576 | io_poll_remove_all(ctx, NULL, true); | |
561fb04a | 9577 | |
15dff286 | 9578 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 9579 | io_iopoll_try_reap_events(ctx); |
309fc03a | 9580 | |
85faa7b8 | 9581 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
9582 | /* |
9583 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
9584 | * if we're exiting a ton of rings at the same time. It just adds | |
9585 | * noise and overhead, there's no discernable change in runtime | |
9586 | * over using system_wq. | |
9587 | */ | |
9588 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
9589 | } |
9590 | ||
9591 | static int io_uring_release(struct inode *inode, struct file *file) | |
9592 | { | |
9593 | struct io_ring_ctx *ctx = file->private_data; | |
9594 | ||
9595 | file->private_data = NULL; | |
9596 | io_ring_ctx_wait_and_kill(ctx); | |
9597 | return 0; | |
9598 | } | |
9599 | ||
f6edbabb PB |
9600 | struct io_task_cancel { |
9601 | struct task_struct *task; | |
3dd0c97a | 9602 | bool all; |
f6edbabb | 9603 | }; |
f254ac04 | 9604 | |
f6edbabb | 9605 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 9606 | { |
9a472ef7 | 9607 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 9608 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
9609 | bool ret; |
9610 | ||
3dd0c97a | 9611 | if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
9612 | struct io_ring_ctx *ctx = req->ctx; |
9613 | ||
9614 | /* protect against races with linked timeouts */ | |
79ebeaee | 9615 | spin_lock(&ctx->completion_lock); |
3dd0c97a | 9616 | ret = io_match_task(req, cancel->task, cancel->all); |
79ebeaee | 9617 | spin_unlock(&ctx->completion_lock); |
9a472ef7 | 9618 | } else { |
3dd0c97a | 9619 | ret = io_match_task(req, cancel->task, cancel->all); |
9a472ef7 PB |
9620 | } |
9621 | return ret; | |
b711d4ea JA |
9622 | } |
9623 | ||
c072481d PB |
9624 | static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
9625 | struct task_struct *task, | |
9626 | bool cancel_all) | |
b7ddce3c | 9627 | { |
e1915f76 | 9628 | struct io_defer_entry *de; |
b7ddce3c PB |
9629 | LIST_HEAD(list); |
9630 | ||
79ebeaee | 9631 | spin_lock(&ctx->completion_lock); |
b7ddce3c | 9632 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
3dd0c97a | 9633 | if (io_match_task(de->req, task, cancel_all)) { |
b7ddce3c PB |
9634 | list_cut_position(&list, &ctx->defer_list, &de->list); |
9635 | break; | |
9636 | } | |
9637 | } | |
79ebeaee | 9638 | spin_unlock(&ctx->completion_lock); |
e1915f76 PB |
9639 | if (list_empty(&list)) |
9640 | return false; | |
b7ddce3c PB |
9641 | |
9642 | while (!list_empty(&list)) { | |
9643 | de = list_first_entry(&list, struct io_defer_entry, list); | |
9644 | list_del_init(&de->list); | |
f41db273 | 9645 | io_req_complete_failed(de->req, -ECANCELED); |
b7ddce3c PB |
9646 | kfree(de); |
9647 | } | |
e1915f76 | 9648 | return true; |
b7ddce3c PB |
9649 | } |
9650 | ||
c072481d | 9651 | static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
1b00764f PB |
9652 | { |
9653 | struct io_tctx_node *node; | |
9654 | enum io_wq_cancel cret; | |
9655 | bool ret = false; | |
9656 | ||
9657 | mutex_lock(&ctx->uring_lock); | |
9658 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
9659 | struct io_uring_task *tctx = node->task->io_uring; | |
9660 | ||
9661 | /* | |
9662 | * io_wq will stay alive while we hold uring_lock, because it's | |
9663 | * killed after ctx nodes, which requires to take the lock. | |
9664 | */ | |
9665 | if (!tctx || !tctx->io_wq) | |
9666 | continue; | |
9667 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
9668 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9669 | } | |
9670 | mutex_unlock(&ctx->uring_lock); | |
9671 | ||
9672 | return ret; | |
9673 | } | |
9674 | ||
c072481d PB |
9675 | static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
9676 | struct task_struct *task, | |
9677 | bool cancel_all) | |
9936c7c2 | 9678 | { |
3dd0c97a | 9679 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
1b00764f | 9680 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 PB |
9681 | |
9682 | while (1) { | |
9683 | enum io_wq_cancel cret; | |
9684 | bool ret = false; | |
9685 | ||
1b00764f PB |
9686 | if (!task) { |
9687 | ret |= io_uring_try_cancel_iowq(ctx); | |
9688 | } else if (tctx && tctx->io_wq) { | |
9689 | /* | |
9690 | * Cancels requests of all rings, not only @ctx, but | |
9691 | * it's fine as the task is in exit/exec. | |
9692 | */ | |
5aa75ed5 | 9693 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
9694 | &cancel, true); |
9695 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9696 | } | |
9697 | ||
9698 | /* SQPOLL thread does its own polling */ | |
3dd0c97a | 9699 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
d052d1d6 | 9700 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
5eef4e87 | 9701 | while (!wq_list_empty(&ctx->iopoll_list)) { |
9936c7c2 PB |
9702 | io_iopoll_try_reap_events(ctx); |
9703 | ret = true; | |
9704 | } | |
9705 | } | |
9706 | ||
3dd0c97a PB |
9707 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
9708 | ret |= io_poll_remove_all(ctx, task, cancel_all); | |
9709 | ret |= io_kill_timeouts(ctx, task, cancel_all); | |
e5dc480d PB |
9710 | if (task) |
9711 | ret |= io_run_task_work(); | |
9936c7c2 PB |
9712 | if (!ret) |
9713 | break; | |
9714 | cond_resched(); | |
9715 | } | |
9716 | } | |
9717 | ||
eef51daa | 9718 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
0f212204 | 9719 | { |
236434c3 | 9720 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 9721 | struct io_tctx_node *node; |
a528b04e | 9722 | int ret; |
236434c3 MWO |
9723 | |
9724 | if (unlikely(!tctx)) { | |
5aa75ed5 | 9725 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
9726 | if (unlikely(ret)) |
9727 | return ret; | |
e139a1ec | 9728 | |
236434c3 | 9729 | tctx = current->io_uring; |
e139a1ec PB |
9730 | if (ctx->iowq_limits_set) { |
9731 | unsigned int limits[2] = { ctx->iowq_limits[0], | |
9732 | ctx->iowq_limits[1], }; | |
9733 | ||
9734 | ret = io_wq_max_workers(tctx->io_wq, limits); | |
9735 | if (ret) | |
9736 | return ret; | |
9737 | } | |
0f212204 | 9738 | } |
cf27f3b1 PB |
9739 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
9740 | node = kmalloc(sizeof(*node), GFP_KERNEL); | |
9741 | if (!node) | |
9742 | return -ENOMEM; | |
9743 | node->ctx = ctx; | |
9744 | node->task = current; | |
13bf43f5 | 9745 | |
cf27f3b1 PB |
9746 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
9747 | node, GFP_KERNEL)); | |
9748 | if (ret) { | |
9749 | kfree(node); | |
9750 | return ret; | |
0f212204 | 9751 | } |
cf27f3b1 PB |
9752 | |
9753 | mutex_lock(&ctx->uring_lock); | |
9754 | list_add(&node->ctx_node, &ctx->tctx_list); | |
9755 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 9756 | } |
cf27f3b1 | 9757 | tctx->last = ctx; |
0f212204 JA |
9758 | return 0; |
9759 | } | |
9760 | ||
cf27f3b1 PB |
9761 | /* |
9762 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
9763 | */ | |
eef51daa | 9764 | static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
cf27f3b1 PB |
9765 | { |
9766 | struct io_uring_task *tctx = current->io_uring; | |
9767 | ||
9768 | if (likely(tctx && tctx->last == ctx)) | |
9769 | return 0; | |
eef51daa | 9770 | return __io_uring_add_tctx_node(ctx); |
cf27f3b1 PB |
9771 | } |
9772 | ||
0f212204 JA |
9773 | /* |
9774 | * Remove this io_uring_file -> task mapping. | |
9775 | */ | |
c072481d | 9776 | static __cold void io_uring_del_tctx_node(unsigned long index) |
0f212204 JA |
9777 | { |
9778 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 9779 | struct io_tctx_node *node; |
2941267b | 9780 | |
eebd2e37 PB |
9781 | if (!tctx) |
9782 | return; | |
13bf43f5 PB |
9783 | node = xa_erase(&tctx->xa, index); |
9784 | if (!node) | |
2941267b | 9785 | return; |
0f212204 | 9786 | |
13bf43f5 PB |
9787 | WARN_ON_ONCE(current != node->task); |
9788 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
9789 | ||
9790 | mutex_lock(&node->ctx->uring_lock); | |
9791 | list_del(&node->ctx_node); | |
9792 | mutex_unlock(&node->ctx->uring_lock); | |
9793 | ||
baf186c4 | 9794 | if (tctx->last == node->ctx) |
0f212204 | 9795 | tctx->last = NULL; |
13bf43f5 | 9796 | kfree(node); |
0f212204 JA |
9797 | } |
9798 | ||
c072481d | 9799 | static __cold void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 9800 | { |
ba5ef6dc | 9801 | struct io_wq *wq = tctx->io_wq; |
13bf43f5 | 9802 | struct io_tctx_node *node; |
de7f1d9e PB |
9803 | unsigned long index; |
9804 | ||
8bab4c09 | 9805 | xa_for_each(&tctx->xa, index, node) { |
eef51daa | 9806 | io_uring_del_tctx_node(index); |
8bab4c09 JA |
9807 | cond_resched(); |
9808 | } | |
b16ef427 ME |
9809 | if (wq) { |
9810 | /* | |
9811 | * Must be after io_uring_del_task_file() (removes nodes under | |
9812 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). | |
9813 | */ | |
ba5ef6dc | 9814 | io_wq_put_and_exit(wq); |
dadebc35 | 9815 | tctx->io_wq = NULL; |
b16ef427 | 9816 | } |
de7f1d9e PB |
9817 | } |
9818 | ||
3f48cf18 | 9819 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
521d6a73 | 9820 | { |
3f48cf18 PB |
9821 | if (tracked) |
9822 | return atomic_read(&tctx->inflight_tracked); | |
521d6a73 PB |
9823 | return percpu_counter_sum(&tctx->inflight); |
9824 | } | |
9825 | ||
c072481d | 9826 | static __cold void io_uring_drop_tctx_refs(struct task_struct *task) |
09899b19 PB |
9827 | { |
9828 | struct io_uring_task *tctx = task->io_uring; | |
9829 | unsigned int refs = tctx->cached_refs; | |
9830 | ||
e9dbe221 PB |
9831 | if (refs) { |
9832 | tctx->cached_refs = 0; | |
9833 | percpu_counter_sub(&tctx->inflight, refs); | |
9834 | put_task_struct_many(task, refs); | |
9835 | } | |
09899b19 PB |
9836 | } |
9837 | ||
78cc687b PB |
9838 | /* |
9839 | * Find any io_uring ctx that this task has registered or done IO on, and cancel | |
9840 | * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation. | |
9841 | */ | |
c072481d PB |
9842 | static __cold void io_uring_cancel_generic(bool cancel_all, |
9843 | struct io_sq_data *sqd) | |
0e9ddb39 | 9844 | { |
521d6a73 | 9845 | struct io_uring_task *tctx = current->io_uring; |
734551df | 9846 | struct io_ring_ctx *ctx; |
0e9ddb39 PB |
9847 | s64 inflight; |
9848 | DEFINE_WAIT(wait); | |
fdaf083c | 9849 | |
78cc687b PB |
9850 | WARN_ON_ONCE(sqd && sqd->thread != current); |
9851 | ||
6d042ffb PO |
9852 | if (!current->io_uring) |
9853 | return; | |
17a91051 PB |
9854 | if (tctx->io_wq) |
9855 | io_wq_exit_start(tctx->io_wq); | |
9856 | ||
0e9ddb39 PB |
9857 | atomic_inc(&tctx->in_idle); |
9858 | do { | |
e9dbe221 | 9859 | io_uring_drop_tctx_refs(current); |
0e9ddb39 | 9860 | /* read completions before cancelations */ |
78cc687b | 9861 | inflight = tctx_inflight(tctx, !cancel_all); |
0e9ddb39 PB |
9862 | if (!inflight) |
9863 | break; | |
fdaf083c | 9864 | |
78cc687b PB |
9865 | if (!sqd) { |
9866 | struct io_tctx_node *node; | |
9867 | unsigned long index; | |
0f212204 | 9868 | |
78cc687b PB |
9869 | xa_for_each(&tctx->xa, index, node) { |
9870 | /* sqpoll task will cancel all its requests */ | |
9871 | if (node->ctx->sq_data) | |
9872 | continue; | |
9873 | io_uring_try_cancel_requests(node->ctx, current, | |
9874 | cancel_all); | |
9875 | } | |
9876 | } else { | |
9877 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
9878 | io_uring_try_cancel_requests(ctx, current, | |
9879 | cancel_all); | |
9880 | } | |
17a91051 | 9881 | |
0f212204 | 9882 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
e9dbe221 | 9883 | io_uring_drop_tctx_refs(current); |
0f212204 | 9884 | /* |
a1bb3cd5 PB |
9885 | * If we've seen completions, retry without waiting. This |
9886 | * avoids a race where a completion comes in before we did | |
9887 | * prepare_to_wait(). | |
0f212204 | 9888 | */ |
3dd0c97a | 9889 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
a1bb3cd5 | 9890 | schedule(); |
f57555ed | 9891 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 9892 | } while (1); |
fdaf083c | 9893 | atomic_dec(&tctx->in_idle); |
de7f1d9e | 9894 | |
8452d4a6 | 9895 | io_uring_clean_tctx(tctx); |
3dd0c97a | 9896 | if (cancel_all) { |
3f48cf18 PB |
9897 | /* for exec all current's requests should be gone, kill tctx */ |
9898 | __io_uring_free(current); | |
9899 | } | |
44e728b8 PB |
9900 | } |
9901 | ||
f552a27a | 9902 | void __io_uring_cancel(bool cancel_all) |
78cc687b | 9903 | { |
f552a27a | 9904 | io_uring_cancel_generic(cancel_all, NULL); |
78cc687b PB |
9905 | } |
9906 | ||
6c5c240e RP |
9907 | static void *io_uring_validate_mmap_request(struct file *file, |
9908 | loff_t pgoff, size_t sz) | |
2b188cc1 | 9909 | { |
2b188cc1 | 9910 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 9911 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
9912 | struct page *page; |
9913 | void *ptr; | |
9914 | ||
9915 | switch (offset) { | |
9916 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
9917 | case IORING_OFF_CQ_RING: |
9918 | ptr = ctx->rings; | |
2b188cc1 JA |
9919 | break; |
9920 | case IORING_OFF_SQES: | |
9921 | ptr = ctx->sq_sqes; | |
9922 | break; | |
2b188cc1 | 9923 | default: |
6c5c240e | 9924 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
9925 | } |
9926 | ||
9927 | page = virt_to_head_page(ptr); | |
a50b854e | 9928 | if (sz > page_size(page)) |
6c5c240e RP |
9929 | return ERR_PTR(-EINVAL); |
9930 | ||
9931 | return ptr; | |
9932 | } | |
9933 | ||
9934 | #ifdef CONFIG_MMU | |
9935 | ||
c072481d | 9936 | static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
6c5c240e RP |
9937 | { |
9938 | size_t sz = vma->vm_end - vma->vm_start; | |
9939 | unsigned long pfn; | |
9940 | void *ptr; | |
9941 | ||
9942 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
9943 | if (IS_ERR(ptr)) | |
9944 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9945 | |
9946 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9947 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9948 | } | |
9949 | ||
6c5c240e RP |
9950 | #else /* !CONFIG_MMU */ |
9951 | ||
9952 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9953 | { | |
9954 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9955 | } | |
9956 | ||
9957 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9958 | { | |
9959 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9960 | } | |
9961 | ||
9962 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9963 | unsigned long addr, unsigned long len, | |
9964 | unsigned long pgoff, unsigned long flags) | |
9965 | { | |
9966 | void *ptr; | |
9967 | ||
9968 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9969 | if (IS_ERR(ptr)) | |
9970 | return PTR_ERR(ptr); | |
9971 | ||
9972 | return (unsigned long) ptr; | |
9973 | } | |
9974 | ||
9975 | #endif /* !CONFIG_MMU */ | |
9976 | ||
d9d05217 | 9977 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 JA |
9978 | { |
9979 | DEFINE_WAIT(wait); | |
9980 | ||
9981 | do { | |
9982 | if (!io_sqring_full(ctx)) | |
9983 | break; | |
90554200 JA |
9984 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
9985 | ||
9986 | if (!io_sqring_full(ctx)) | |
9987 | break; | |
90554200 JA |
9988 | schedule(); |
9989 | } while (!signal_pending(current)); | |
9990 | ||
9991 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
5199328a | 9992 | return 0; |
90554200 JA |
9993 | } |
9994 | ||
c73ebb68 HX |
9995 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9996 | struct __kernel_timespec __user **ts, | |
9997 | const sigset_t __user **sig) | |
9998 | { | |
9999 | struct io_uring_getevents_arg arg; | |
10000 | ||
10001 | /* | |
10002 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
10003 | * is just a pointer to the sigset_t. | |
10004 | */ | |
10005 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
10006 | *sig = (const sigset_t __user *) argp; | |
10007 | *ts = NULL; | |
10008 | return 0; | |
10009 | } | |
10010 | ||
10011 | /* | |
10012 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
10013 | * timespec and sigset_t pointers if good. | |
10014 | */ | |
10015 | if (*argsz != sizeof(arg)) | |
10016 | return -EINVAL; | |
10017 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
10018 | return -EFAULT; | |
10019 | *sig = u64_to_user_ptr(arg.sigmask); | |
10020 | *argsz = arg.sigmask_sz; | |
10021 | *ts = u64_to_user_ptr(arg.ts); | |
10022 | return 0; | |
10023 | } | |
10024 | ||
2b188cc1 | 10025 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
10026 | u32, min_complete, u32, flags, const void __user *, argp, |
10027 | size_t, argsz) | |
2b188cc1 JA |
10028 | { |
10029 | struct io_ring_ctx *ctx; | |
2b188cc1 JA |
10030 | int submitted = 0; |
10031 | struct fd f; | |
33f993da | 10032 | long ret; |
2b188cc1 | 10033 | |
4c6e277c | 10034 | io_run_task_work(); |
b41e9852 | 10035 | |
33f993da PB |
10036 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
10037 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) | |
2b188cc1 JA |
10038 | return -EINVAL; |
10039 | ||
10040 | f = fdget(fd); | |
33f993da | 10041 | if (unlikely(!f.file)) |
2b188cc1 JA |
10042 | return -EBADF; |
10043 | ||
10044 | ret = -EOPNOTSUPP; | |
33f993da | 10045 | if (unlikely(f.file->f_op != &io_uring_fops)) |
2b188cc1 JA |
10046 | goto out_fput; |
10047 | ||
10048 | ret = -ENXIO; | |
10049 | ctx = f.file->private_data; | |
33f993da | 10050 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
2b188cc1 JA |
10051 | goto out_fput; |
10052 | ||
7e84e1c7 | 10053 | ret = -EBADFD; |
33f993da | 10054 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
10055 | goto out; |
10056 | ||
6c271ce2 JA |
10057 | /* |
10058 | * For SQ polling, the thread will do all submissions and completions. | |
10059 | * Just return the requested submit count, and wake the thread if | |
10060 | * we were asked to. | |
10061 | */ | |
b2a9eada | 10062 | ret = 0; |
6c271ce2 | 10063 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
90f67366 | 10064 | io_cqring_overflow_flush(ctx); |
89448c47 | 10065 | |
21f96522 JA |
10066 | if (unlikely(ctx->sq_data->thread == NULL)) { |
10067 | ret = -EOWNERDEAD; | |
04147488 | 10068 | goto out; |
21f96522 | 10069 | } |
6c271ce2 | 10070 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 10071 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
10072 | if (flags & IORING_ENTER_SQ_WAIT) { |
10073 | ret = io_sqpoll_wait_sq(ctx); | |
10074 | if (ret) | |
10075 | goto out; | |
10076 | } | |
6c271ce2 | 10077 | submitted = to_submit; |
b2a9eada | 10078 | } else if (to_submit) { |
eef51daa | 10079 | ret = io_uring_add_tctx_node(ctx); |
0f212204 JA |
10080 | if (unlikely(ret)) |
10081 | goto out; | |
2b188cc1 | 10082 | mutex_lock(&ctx->uring_lock); |
0f212204 | 10083 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 10084 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
10085 | |
10086 | if (submitted != to_submit) | |
10087 | goto out; | |
2b188cc1 JA |
10088 | } |
10089 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
10090 | const sigset_t __user *sig; |
10091 | struct __kernel_timespec __user *ts; | |
10092 | ||
10093 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
10094 | if (unlikely(ret)) | |
10095 | goto out; | |
10096 | ||
2b188cc1 JA |
10097 | min_complete = min(min_complete, ctx->cq_entries); |
10098 | ||
32b2244a XW |
10099 | /* |
10100 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
10101 | * space applications don't need to do io completion events | |
10102 | * polling again, they can rely on io_sq_thread to do polling | |
10103 | * work, which can reduce cpu usage and uring_lock contention. | |
10104 | */ | |
10105 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
10106 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 10107 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 10108 | } else { |
c73ebb68 | 10109 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 10110 | } |
2b188cc1 JA |
10111 | } |
10112 | ||
7c504e65 | 10113 | out: |
6805b32e | 10114 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
10115 | out_fput: |
10116 | fdput(f); | |
10117 | return submitted ? submitted : ret; | |
10118 | } | |
10119 | ||
bebdb65e | 10120 | #ifdef CONFIG_PROC_FS |
c072481d | 10121 | static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id, |
61cf9370 | 10122 | const struct cred *cred) |
87ce955b | 10123 | { |
87ce955b JA |
10124 | struct user_namespace *uns = seq_user_ns(m); |
10125 | struct group_info *gi; | |
10126 | kernel_cap_t cap; | |
10127 | unsigned __capi; | |
10128 | int g; | |
10129 | ||
10130 | seq_printf(m, "%5d\n", id); | |
10131 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
10132 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
10133 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
10134 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
10135 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
10136 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
10137 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
10138 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
10139 | seq_puts(m, "\n\tGroups:\t"); | |
10140 | gi = cred->group_info; | |
10141 | for (g = 0; g < gi->ngroups; g++) { | |
10142 | seq_put_decimal_ull(m, g ? " " : "", | |
10143 | from_kgid_munged(uns, gi->gid[g])); | |
10144 | } | |
10145 | seq_puts(m, "\n\tCapEff:\t"); | |
10146 | cap = cred->cap_effective; | |
10147 | CAP_FOR_EACH_U32(__capi) | |
10148 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
10149 | seq_putc(m, '\n'); | |
10150 | return 0; | |
10151 | } | |
10152 | ||
c072481d PB |
10153 | static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, |
10154 | struct seq_file *m) | |
87ce955b | 10155 | { |
dbbe9c64 | 10156 | struct io_sq_data *sq = NULL; |
83f84356 HX |
10157 | struct io_overflow_cqe *ocqe; |
10158 | struct io_rings *r = ctx->rings; | |
10159 | unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1; | |
83f84356 HX |
10160 | unsigned int sq_head = READ_ONCE(r->sq.head); |
10161 | unsigned int sq_tail = READ_ONCE(r->sq.tail); | |
10162 | unsigned int cq_head = READ_ONCE(r->cq.head); | |
10163 | unsigned int cq_tail = READ_ONCE(r->cq.tail); | |
f75d1183 | 10164 | unsigned int sq_entries, cq_entries; |
fad8e0de | 10165 | bool has_lock; |
83f84356 HX |
10166 | unsigned int i; |
10167 | ||
10168 | /* | |
10169 | * we may get imprecise sqe and cqe info if uring is actively running | |
10170 | * since we get cached_sq_head and cached_cq_tail without uring_lock | |
10171 | * and sq_tail and cq_head are changed by userspace. But it's ok since | |
10172 | * we usually use these info when it is stuck. | |
10173 | */ | |
f75d1183 JA |
10174 | seq_printf(m, "SqMask:\t\t0x%x\n", sq_mask); |
10175 | seq_printf(m, "SqHead:\t%u\n", sq_head); | |
10176 | seq_printf(m, "SqTail:\t%u\n", sq_tail); | |
10177 | seq_printf(m, "CachedSqHead:\t%u\n", ctx->cached_sq_head); | |
10178 | seq_printf(m, "CqMask:\t0x%x\n", cq_mask); | |
10179 | seq_printf(m, "CqHead:\t%u\n", cq_head); | |
10180 | seq_printf(m, "CqTail:\t%u\n", cq_tail); | |
10181 | seq_printf(m, "CachedCqTail:\t%u\n", ctx->cached_cq_tail); | |
10182 | seq_printf(m, "SQEs:\t%u\n", sq_tail - ctx->cached_sq_head); | |
10183 | sq_entries = min(sq_tail - sq_head, ctx->sq_entries); | |
10184 | for (i = 0; i < sq_entries; i++) { | |
10185 | unsigned int entry = i + sq_head; | |
10186 | unsigned int sq_idx = READ_ONCE(ctx->sq_array[entry & sq_mask]); | |
a1957780 | 10187 | struct io_uring_sqe *sqe; |
f75d1183 JA |
10188 | |
10189 | if (sq_idx > sq_mask) | |
10190 | continue; | |
10191 | sqe = &ctx->sq_sqes[sq_idx]; | |
10192 | seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n", | |
10193 | sq_idx, sqe->opcode, sqe->fd, sqe->flags, | |
10194 | sqe->user_data); | |
83f84356 | 10195 | } |
f75d1183 JA |
10196 | seq_printf(m, "CQEs:\t%u\n", cq_tail - cq_head); |
10197 | cq_entries = min(cq_tail - cq_head, ctx->cq_entries); | |
10198 | for (i = 0; i < cq_entries; i++) { | |
10199 | unsigned int entry = i + cq_head; | |
10200 | struct io_uring_cqe *cqe = &r->cqes[entry & cq_mask]; | |
83f84356 HX |
10201 | |
10202 | seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n", | |
f75d1183 JA |
10203 | entry & cq_mask, cqe->user_data, cqe->res, |
10204 | cqe->flags); | |
83f84356 | 10205 | } |
87ce955b | 10206 | |
fad8e0de JA |
10207 | /* |
10208 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
10209 | * since fdinfo case grabs it in the opposite direction of normal use | |
10210 | * cases. If we fail to get the lock, we just don't iterate any | |
10211 | * structures that could be going away outside the io_uring mutex. | |
10212 | */ | |
10213 | has_lock = mutex_trylock(&ctx->uring_lock); | |
10214 | ||
5f3f26f9 | 10215 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 10216 | sq = ctx->sq_data; |
5f3f26f9 JA |
10217 | if (!sq->thread) |
10218 | sq = NULL; | |
10219 | } | |
dbbe9c64 JQ |
10220 | |
10221 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
10222 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 10223 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 10224 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
7b29f92d | 10225 | struct file *f = io_file_from_index(ctx, i); |
87ce955b | 10226 | |
87ce955b JA |
10227 | if (f) |
10228 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
10229 | else | |
10230 | seq_printf(m, "%5u: <none>\n", i); | |
10231 | } | |
10232 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 10233 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
41edf1a5 | 10234 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
4751f53d | 10235 | unsigned int len = buf->ubuf_end - buf->ubuf; |
87ce955b | 10236 | |
4751f53d | 10237 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
87ce955b | 10238 | } |
61cf9370 MWO |
10239 | if (has_lock && !xa_empty(&ctx->personalities)) { |
10240 | unsigned long index; | |
10241 | const struct cred *cred; | |
10242 | ||
87ce955b | 10243 | seq_printf(m, "Personalities:\n"); |
61cf9370 MWO |
10244 | xa_for_each(&ctx->personalities, index, cred) |
10245 | io_uring_show_cred(m, index, cred); | |
87ce955b | 10246 | } |
83f84356 HX |
10247 | if (has_lock) |
10248 | mutex_unlock(&ctx->uring_lock); | |
10249 | ||
10250 | seq_puts(m, "PollList:\n"); | |
79ebeaee | 10251 | spin_lock(&ctx->completion_lock); |
d7718a9d JA |
10252 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
10253 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
10254 | struct io_kiocb *req; | |
10255 | ||
10256 | hlist_for_each_entry(req, list, hash_node) | |
10257 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
10258 | req->task->task_works != NULL); | |
10259 | } | |
83f84356 HX |
10260 | |
10261 | seq_puts(m, "CqOverflowList:\n"); | |
10262 | list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) { | |
10263 | struct io_uring_cqe *cqe = &ocqe->cqe; | |
10264 | ||
10265 | seq_printf(m, " user_data=%llu, res=%d, flags=%x\n", | |
10266 | cqe->user_data, cqe->res, cqe->flags); | |
10267 | ||
10268 | } | |
10269 | ||
79ebeaee | 10270 | spin_unlock(&ctx->completion_lock); |
87ce955b JA |
10271 | } |
10272 | ||
c072481d | 10273 | static __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
87ce955b JA |
10274 | { |
10275 | struct io_ring_ctx *ctx = f->private_data; | |
10276 | ||
10277 | if (percpu_ref_tryget(&ctx->refs)) { | |
10278 | __io_uring_show_fdinfo(ctx, m); | |
10279 | percpu_ref_put(&ctx->refs); | |
10280 | } | |
10281 | } | |
bebdb65e | 10282 | #endif |
87ce955b | 10283 | |
2b188cc1 JA |
10284 | static const struct file_operations io_uring_fops = { |
10285 | .release = io_uring_release, | |
10286 | .mmap = io_uring_mmap, | |
6c5c240e RP |
10287 | #ifndef CONFIG_MMU |
10288 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
10289 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
10290 | #endif | |
2b188cc1 | 10291 | .poll = io_uring_poll, |
bebdb65e | 10292 | #ifdef CONFIG_PROC_FS |
87ce955b | 10293 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 10294 | #endif |
2b188cc1 JA |
10295 | }; |
10296 | ||
c072481d PB |
10297 | static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
10298 | struct io_uring_params *p) | |
2b188cc1 | 10299 | { |
75b28aff HV |
10300 | struct io_rings *rings; |
10301 | size_t size, sq_array_offset; | |
2b188cc1 | 10302 | |
bd740481 JA |
10303 | /* make sure these are sane, as we already accounted them */ |
10304 | ctx->sq_entries = p->sq_entries; | |
10305 | ctx->cq_entries = p->cq_entries; | |
10306 | ||
75b28aff HV |
10307 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
10308 | if (size == SIZE_MAX) | |
10309 | return -EOVERFLOW; | |
10310 | ||
10311 | rings = io_mem_alloc(size); | |
10312 | if (!rings) | |
2b188cc1 JA |
10313 | return -ENOMEM; |
10314 | ||
75b28aff HV |
10315 | ctx->rings = rings; |
10316 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
10317 | rings->sq_ring_mask = p->sq_entries - 1; | |
10318 | rings->cq_ring_mask = p->cq_entries - 1; | |
10319 | rings->sq_ring_entries = p->sq_entries; | |
10320 | rings->cq_ring_entries = p->cq_entries; | |
2b188cc1 JA |
10321 | |
10322 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
10323 | if (size == SIZE_MAX) { |
10324 | io_mem_free(ctx->rings); | |
10325 | ctx->rings = NULL; | |
2b188cc1 | 10326 | return -EOVERFLOW; |
eb065d30 | 10327 | } |
2b188cc1 JA |
10328 | |
10329 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
10330 | if (!ctx->sq_sqes) { |
10331 | io_mem_free(ctx->rings); | |
10332 | ctx->rings = NULL; | |
2b188cc1 | 10333 | return -ENOMEM; |
eb065d30 | 10334 | } |
2b188cc1 | 10335 | |
2b188cc1 JA |
10336 | return 0; |
10337 | } | |
10338 | ||
9faadcc8 PB |
10339 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
10340 | { | |
10341 | int ret, fd; | |
10342 | ||
10343 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
10344 | if (fd < 0) | |
10345 | return fd; | |
10346 | ||
eef51daa | 10347 | ret = io_uring_add_tctx_node(ctx); |
9faadcc8 PB |
10348 | if (ret) { |
10349 | put_unused_fd(fd); | |
10350 | return ret; | |
10351 | } | |
10352 | fd_install(fd, file); | |
10353 | return fd; | |
10354 | } | |
10355 | ||
2b188cc1 JA |
10356 | /* |
10357 | * Allocate an anonymous fd, this is what constitutes the application | |
10358 | * visible backing of an io_uring instance. The application mmaps this | |
10359 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
10360 | * we have to tie this fd to a socket for file garbage collection purposes. | |
10361 | */ | |
9faadcc8 | 10362 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
10363 | { |
10364 | struct file *file; | |
9faadcc8 | 10365 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
10366 | int ret; |
10367 | ||
2b188cc1 JA |
10368 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
10369 | &ctx->ring_sock); | |
10370 | if (ret) | |
9faadcc8 | 10371 | return ERR_PTR(ret); |
2b188cc1 JA |
10372 | #endif |
10373 | ||
91a9ab7c PM |
10374 | file = anon_inode_getfile_secure("[io_uring]", &io_uring_fops, ctx, |
10375 | O_RDWR | O_CLOEXEC, NULL); | |
2b188cc1 | 10376 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
10377 | if (IS_ERR(file)) { |
10378 | sock_release(ctx->ring_sock); | |
10379 | ctx->ring_sock = NULL; | |
10380 | } else { | |
10381 | ctx->ring_sock->file = file; | |
0f212204 | 10382 | } |
2b188cc1 | 10383 | #endif |
9faadcc8 | 10384 | return file; |
2b188cc1 JA |
10385 | } |
10386 | ||
c072481d PB |
10387 | static __cold int io_uring_create(unsigned entries, struct io_uring_params *p, |
10388 | struct io_uring_params __user *params) | |
2b188cc1 | 10389 | { |
2b188cc1 | 10390 | struct io_ring_ctx *ctx; |
9faadcc8 | 10391 | struct file *file; |
2b188cc1 JA |
10392 | int ret; |
10393 | ||
8110c1a6 | 10394 | if (!entries) |
2b188cc1 | 10395 | return -EINVAL; |
8110c1a6 JA |
10396 | if (entries > IORING_MAX_ENTRIES) { |
10397 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
10398 | return -EINVAL; | |
10399 | entries = IORING_MAX_ENTRIES; | |
10400 | } | |
2b188cc1 JA |
10401 | |
10402 | /* | |
10403 | * Use twice as many entries for the CQ ring. It's possible for the | |
10404 | * application to drive a higher depth than the size of the SQ ring, | |
10405 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
10406 | * some flexibility in overcommitting a bit. If the application has |
10407 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
10408 | * of CQ ring entries manually. | |
2b188cc1 JA |
10409 | */ |
10410 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
10411 | if (p->flags & IORING_SETUP_CQSIZE) { |
10412 | /* | |
10413 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
10414 | * to a power-of-two, if it isn't already. We do NOT impose | |
10415 | * any cq vs sq ring sizing. | |
10416 | */ | |
eb2667b3 | 10417 | if (!p->cq_entries) |
33a107f0 | 10418 | return -EINVAL; |
8110c1a6 JA |
10419 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
10420 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
10421 | return -EINVAL; | |
10422 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
10423 | } | |
eb2667b3 JQ |
10424 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
10425 | if (p->cq_entries < p->sq_entries) | |
10426 | return -EINVAL; | |
33a107f0 JA |
10427 | } else { |
10428 | p->cq_entries = 2 * p->sq_entries; | |
10429 | } | |
2b188cc1 | 10430 | |
2b188cc1 | 10431 | ctx = io_ring_ctx_alloc(p); |
62e398be | 10432 | if (!ctx) |
2b188cc1 | 10433 | return -ENOMEM; |
2b188cc1 | 10434 | ctx->compat = in_compat_syscall(); |
62e398be JA |
10435 | if (!capable(CAP_IPC_LOCK)) |
10436 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
10437 | |
10438 | /* | |
10439 | * This is just grabbed for accounting purposes. When a process exits, | |
10440 | * the mm is exited and dropped before the files, hence we need to hang | |
10441 | * on to this mm purely for the purposes of being able to unaccount | |
10442 | * memory (locked/pinned vm). It's not used for anything else. | |
10443 | */ | |
6b7898eb | 10444 | mmgrab(current->mm); |
2aede0e4 | 10445 | ctx->mm_account = current->mm; |
6b7898eb | 10446 | |
2b188cc1 JA |
10447 | ret = io_allocate_scq_urings(ctx, p); |
10448 | if (ret) | |
10449 | goto err; | |
10450 | ||
7e84e1c7 | 10451 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
10452 | if (ret) |
10453 | goto err; | |
eae071c9 | 10454 | /* always set a rsrc node */ |
47b228ce PB |
10455 | ret = io_rsrc_node_switch_start(ctx); |
10456 | if (ret) | |
10457 | goto err; | |
eae071c9 | 10458 | io_rsrc_node_switch(ctx, NULL); |
2b188cc1 | 10459 | |
2b188cc1 | 10460 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
10461 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
10462 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
10463 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
10464 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
10465 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
10466 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
10467 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
10468 | |
10469 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
10470 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
10471 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
10472 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
10473 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
10474 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
10475 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 10476 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 10477 | |
7f13657d XW |
10478 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
10479 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 10480 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 10481 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9690557e | 10482 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
04c76b41 | 10483 | IORING_FEAT_RSRC_TAGS | IORING_FEAT_CQE_SKIP; |
7f13657d XW |
10484 | |
10485 | if (copy_to_user(params, p, sizeof(*p))) { | |
10486 | ret = -EFAULT; | |
10487 | goto err; | |
10488 | } | |
d1719f70 | 10489 | |
9faadcc8 PB |
10490 | file = io_uring_get_file(ctx); |
10491 | if (IS_ERR(file)) { | |
10492 | ret = PTR_ERR(file); | |
10493 | goto err; | |
10494 | } | |
10495 | ||
044c1ab3 JA |
10496 | /* |
10497 | * Install ring fd as the very last thing, so we don't risk someone | |
10498 | * having closed it before we finish setup | |
10499 | */ | |
9faadcc8 PB |
10500 | ret = io_uring_install_fd(ctx, file); |
10501 | if (ret < 0) { | |
10502 | /* fput will clean it up */ | |
10503 | fput(file); | |
10504 | return ret; | |
10505 | } | |
044c1ab3 | 10506 | |
c826bd7a | 10507 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
10508 | return ret; |
10509 | err: | |
10510 | io_ring_ctx_wait_and_kill(ctx); | |
10511 | return ret; | |
10512 | } | |
10513 | ||
10514 | /* | |
10515 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
10516 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
10517 | * params structure passed in. | |
10518 | */ | |
10519 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
10520 | { | |
10521 | struct io_uring_params p; | |
2b188cc1 JA |
10522 | int i; |
10523 | ||
10524 | if (copy_from_user(&p, params, sizeof(p))) | |
10525 | return -EFAULT; | |
10526 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
10527 | if (p.resv[i]) | |
10528 | return -EINVAL; | |
10529 | } | |
10530 | ||
6c271ce2 | 10531 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 10532 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
10533 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
10534 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
10535 | return -EINVAL; |
10536 | ||
7f13657d | 10537 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
10538 | } |
10539 | ||
10540 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
10541 | struct io_uring_params __user *, params) | |
10542 | { | |
10543 | return io_uring_setup(entries, params); | |
10544 | } | |
10545 | ||
c072481d PB |
10546 | static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg, |
10547 | unsigned nr_args) | |
66f4af93 JA |
10548 | { |
10549 | struct io_uring_probe *p; | |
10550 | size_t size; | |
10551 | int i, ret; | |
10552 | ||
10553 | size = struct_size(p, ops, nr_args); | |
10554 | if (size == SIZE_MAX) | |
10555 | return -EOVERFLOW; | |
10556 | p = kzalloc(size, GFP_KERNEL); | |
10557 | if (!p) | |
10558 | return -ENOMEM; | |
10559 | ||
10560 | ret = -EFAULT; | |
10561 | if (copy_from_user(p, arg, size)) | |
10562 | goto out; | |
10563 | ret = -EINVAL; | |
10564 | if (memchr_inv(p, 0, size)) | |
10565 | goto out; | |
10566 | ||
10567 | p->last_op = IORING_OP_LAST - 1; | |
10568 | if (nr_args > IORING_OP_LAST) | |
10569 | nr_args = IORING_OP_LAST; | |
10570 | ||
10571 | for (i = 0; i < nr_args; i++) { | |
10572 | p->ops[i].op = i; | |
10573 | if (!io_op_defs[i].not_supported) | |
10574 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
10575 | } | |
10576 | p->ops_len = i; | |
10577 | ||
10578 | ret = 0; | |
10579 | if (copy_to_user(arg, p, size)) | |
10580 | ret = -EFAULT; | |
10581 | out: | |
10582 | kfree(p); | |
10583 | return ret; | |
10584 | } | |
10585 | ||
071698e1 JA |
10586 | static int io_register_personality(struct io_ring_ctx *ctx) |
10587 | { | |
4379bf8b | 10588 | const struct cred *creds; |
61cf9370 | 10589 | u32 id; |
1e6fa521 | 10590 | int ret; |
071698e1 | 10591 | |
4379bf8b | 10592 | creds = get_current_cred(); |
1e6fa521 | 10593 | |
61cf9370 MWO |
10594 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
10595 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); | |
a30f895a JA |
10596 | if (ret < 0) { |
10597 | put_cred(creds); | |
10598 | return ret; | |
10599 | } | |
10600 | return id; | |
071698e1 JA |
10601 | } |
10602 | ||
c072481d PB |
10603 | static __cold int io_register_restrictions(struct io_ring_ctx *ctx, |
10604 | void __user *arg, unsigned int nr_args) | |
21b55dbc SG |
10605 | { |
10606 | struct io_uring_restriction *res; | |
10607 | size_t size; | |
10608 | int i, ret; | |
10609 | ||
7e84e1c7 SG |
10610 | /* Restrictions allowed only if rings started disabled */ |
10611 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10612 | return -EBADFD; | |
10613 | ||
21b55dbc | 10614 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 10615 | if (ctx->restrictions.registered) |
21b55dbc SG |
10616 | return -EBUSY; |
10617 | ||
10618 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
10619 | return -EINVAL; | |
10620 | ||
10621 | size = array_size(nr_args, sizeof(*res)); | |
10622 | if (size == SIZE_MAX) | |
10623 | return -EOVERFLOW; | |
10624 | ||
10625 | res = memdup_user(arg, size); | |
10626 | if (IS_ERR(res)) | |
10627 | return PTR_ERR(res); | |
10628 | ||
10629 | ret = 0; | |
10630 | ||
10631 | for (i = 0; i < nr_args; i++) { | |
10632 | switch (res[i].opcode) { | |
10633 | case IORING_RESTRICTION_REGISTER_OP: | |
10634 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
10635 | ret = -EINVAL; | |
10636 | goto out; | |
10637 | } | |
10638 | ||
10639 | __set_bit(res[i].register_op, | |
10640 | ctx->restrictions.register_op); | |
10641 | break; | |
10642 | case IORING_RESTRICTION_SQE_OP: | |
10643 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
10644 | ret = -EINVAL; | |
10645 | goto out; | |
10646 | } | |
10647 | ||
10648 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
10649 | break; | |
10650 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
10651 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
10652 | break; | |
10653 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
10654 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
10655 | break; | |
10656 | default: | |
10657 | ret = -EINVAL; | |
10658 | goto out; | |
10659 | } | |
10660 | } | |
10661 | ||
10662 | out: | |
10663 | /* Reset all restrictions if an error happened */ | |
10664 | if (ret != 0) | |
10665 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
10666 | else | |
7e84e1c7 | 10667 | ctx->restrictions.registered = true; |
21b55dbc SG |
10668 | |
10669 | kfree(res); | |
10670 | return ret; | |
10671 | } | |
10672 | ||
7e84e1c7 SG |
10673 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
10674 | { | |
10675 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10676 | return -EBADFD; | |
10677 | ||
10678 | if (ctx->restrictions.registered) | |
10679 | ctx->restricted = 1; | |
10680 | ||
0298ef96 PB |
10681 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
10682 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) | |
10683 | wake_up(&ctx->sq_data->wait); | |
7e84e1c7 SG |
10684 | return 0; |
10685 | } | |
10686 | ||
fdecb662 | 10687 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 10688 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 PB |
10689 | unsigned nr_args) |
10690 | { | |
10691 | __u32 tmp; | |
10692 | int err; | |
10693 | ||
c3bdad02 PB |
10694 | if (up->resv) |
10695 | return -EINVAL; | |
98f0b3b4 PB |
10696 | if (check_add_overflow(up->offset, nr_args, &tmp)) |
10697 | return -EOVERFLOW; | |
10698 | err = io_rsrc_node_switch_start(ctx); | |
10699 | if (err) | |
10700 | return err; | |
10701 | ||
fdecb662 PB |
10702 | switch (type) { |
10703 | case IORING_RSRC_FILE: | |
98f0b3b4 | 10704 | return __io_sqe_files_update(ctx, up, nr_args); |
634d00df PB |
10705 | case IORING_RSRC_BUFFER: |
10706 | return __io_sqe_buffers_update(ctx, up, nr_args); | |
98f0b3b4 PB |
10707 | } |
10708 | return -EINVAL; | |
10709 | } | |
10710 | ||
c3bdad02 PB |
10711 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
10712 | unsigned nr_args) | |
98f0b3b4 | 10713 | { |
c3bdad02 | 10714 | struct io_uring_rsrc_update2 up; |
98f0b3b4 PB |
10715 | |
10716 | if (!nr_args) | |
10717 | return -EINVAL; | |
c3bdad02 PB |
10718 | memset(&up, 0, sizeof(up)); |
10719 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) | |
10720 | return -EFAULT; | |
10721 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); | |
10722 | } | |
10723 | ||
10724 | static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
992da01a | 10725 | unsigned size, unsigned type) |
c3bdad02 PB |
10726 | { |
10727 | struct io_uring_rsrc_update2 up; | |
10728 | ||
10729 | if (size != sizeof(up)) | |
10730 | return -EINVAL; | |
98f0b3b4 PB |
10731 | if (copy_from_user(&up, arg, sizeof(up))) |
10732 | return -EFAULT; | |
992da01a | 10733 | if (!up.nr || up.resv) |
98f0b3b4 | 10734 | return -EINVAL; |
992da01a | 10735 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
98f0b3b4 PB |
10736 | } |
10737 | ||
c072481d | 10738 | static __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
992da01a | 10739 | unsigned int size, unsigned int type) |
792e3582 PB |
10740 | { |
10741 | struct io_uring_rsrc_register rr; | |
10742 | ||
10743 | /* keep it extendible */ | |
10744 | if (size != sizeof(rr)) | |
10745 | return -EINVAL; | |
10746 | ||
10747 | memset(&rr, 0, sizeof(rr)); | |
10748 | if (copy_from_user(&rr, arg, size)) | |
10749 | return -EFAULT; | |
992da01a | 10750 | if (!rr.nr || rr.resv || rr.resv2) |
792e3582 PB |
10751 | return -EINVAL; |
10752 | ||
992da01a | 10753 | switch (type) { |
792e3582 PB |
10754 | case IORING_RSRC_FILE: |
10755 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), | |
10756 | rr.nr, u64_to_user_ptr(rr.tags)); | |
634d00df PB |
10757 | case IORING_RSRC_BUFFER: |
10758 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), | |
10759 | rr.nr, u64_to_user_ptr(rr.tags)); | |
792e3582 PB |
10760 | } |
10761 | return -EINVAL; | |
10762 | } | |
10763 | ||
c072481d PB |
10764 | static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx, |
10765 | void __user *arg, unsigned len) | |
fe76421d JA |
10766 | { |
10767 | struct io_uring_task *tctx = current->io_uring; | |
10768 | cpumask_var_t new_mask; | |
10769 | int ret; | |
10770 | ||
10771 | if (!tctx || !tctx->io_wq) | |
10772 | return -EINVAL; | |
10773 | ||
10774 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | |
10775 | return -ENOMEM; | |
10776 | ||
10777 | cpumask_clear(new_mask); | |
10778 | if (len > cpumask_size()) | |
10779 | len = cpumask_size(); | |
10780 | ||
10781 | if (copy_from_user(new_mask, arg, len)) { | |
10782 | free_cpumask_var(new_mask); | |
10783 | return -EFAULT; | |
10784 | } | |
10785 | ||
10786 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); | |
10787 | free_cpumask_var(new_mask); | |
10788 | return ret; | |
10789 | } | |
10790 | ||
c072481d | 10791 | static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx) |
fe76421d JA |
10792 | { |
10793 | struct io_uring_task *tctx = current->io_uring; | |
10794 | ||
10795 | if (!tctx || !tctx->io_wq) | |
10796 | return -EINVAL; | |
10797 | ||
10798 | return io_wq_cpu_affinity(tctx->io_wq, NULL); | |
10799 | } | |
10800 | ||
c072481d PB |
10801 | static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, |
10802 | void __user *arg) | |
b22fa62a | 10803 | __must_hold(&ctx->uring_lock) |
2e480058 | 10804 | { |
b22fa62a | 10805 | struct io_tctx_node *node; |
fa84693b JA |
10806 | struct io_uring_task *tctx = NULL; |
10807 | struct io_sq_data *sqd = NULL; | |
2e480058 JA |
10808 | __u32 new_count[2]; |
10809 | int i, ret; | |
10810 | ||
2e480058 JA |
10811 | if (copy_from_user(new_count, arg, sizeof(new_count))) |
10812 | return -EFAULT; | |
10813 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
10814 | if (new_count[i] > INT_MAX) | |
10815 | return -EINVAL; | |
10816 | ||
fa84693b JA |
10817 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
10818 | sqd = ctx->sq_data; | |
10819 | if (sqd) { | |
009ad9f0 JA |
10820 | /* |
10821 | * Observe the correct sqd->lock -> ctx->uring_lock | |
10822 | * ordering. Fine to drop uring_lock here, we hold | |
10823 | * a ref to the ctx. | |
10824 | */ | |
41d3a6bd | 10825 | refcount_inc(&sqd->refs); |
009ad9f0 | 10826 | mutex_unlock(&ctx->uring_lock); |
fa84693b | 10827 | mutex_lock(&sqd->lock); |
009ad9f0 | 10828 | mutex_lock(&ctx->uring_lock); |
41d3a6bd JA |
10829 | if (sqd->thread) |
10830 | tctx = sqd->thread->io_uring; | |
fa84693b JA |
10831 | } |
10832 | } else { | |
10833 | tctx = current->io_uring; | |
10834 | } | |
10835 | ||
e139a1ec | 10836 | BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits)); |
fa84693b | 10837 | |
bad119b9 PB |
10838 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
10839 | if (new_count[i]) | |
10840 | ctx->iowq_limits[i] = new_count[i]; | |
e139a1ec PB |
10841 | ctx->iowq_limits_set = true; |
10842 | ||
e139a1ec PB |
10843 | if (tctx && tctx->io_wq) { |
10844 | ret = io_wq_max_workers(tctx->io_wq, new_count); | |
10845 | if (ret) | |
10846 | goto err; | |
10847 | } else { | |
10848 | memset(new_count, 0, sizeof(new_count)); | |
10849 | } | |
fa84693b | 10850 | |
41d3a6bd | 10851 | if (sqd) { |
fa84693b | 10852 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
10853 | io_put_sq_data(sqd); |
10854 | } | |
2e480058 JA |
10855 | |
10856 | if (copy_to_user(arg, new_count, sizeof(new_count))) | |
10857 | return -EFAULT; | |
10858 | ||
b22fa62a PB |
10859 | /* that's it for SQPOLL, only the SQPOLL task creates requests */ |
10860 | if (sqd) | |
10861 | return 0; | |
10862 | ||
10863 | /* now propagate the restriction to all registered users */ | |
10864 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
10865 | struct io_uring_task *tctx = node->task->io_uring; | |
10866 | ||
10867 | if (WARN_ON_ONCE(!tctx->io_wq)) | |
10868 | continue; | |
10869 | ||
10870 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
10871 | new_count[i] = ctx->iowq_limits[i]; | |
10872 | /* ignore errors, it always returns zero anyway */ | |
10873 | (void)io_wq_max_workers(tctx->io_wq, new_count); | |
10874 | } | |
2e480058 | 10875 | return 0; |
fa84693b | 10876 | err: |
41d3a6bd | 10877 | if (sqd) { |
fa84693b | 10878 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
10879 | io_put_sq_data(sqd); |
10880 | } | |
fa84693b | 10881 | return ret; |
2e480058 JA |
10882 | } |
10883 | ||
071698e1 JA |
10884 | static bool io_register_op_must_quiesce(int op) |
10885 | { | |
10886 | switch (op) { | |
bd54b6fe BM |
10887 | case IORING_REGISTER_BUFFERS: |
10888 | case IORING_UNREGISTER_BUFFERS: | |
f4f7d21c | 10889 | case IORING_REGISTER_FILES: |
071698e1 JA |
10890 | case IORING_UNREGISTER_FILES: |
10891 | case IORING_REGISTER_FILES_UPDATE: | |
10892 | case IORING_REGISTER_PROBE: | |
10893 | case IORING_REGISTER_PERSONALITY: | |
10894 | case IORING_UNREGISTER_PERSONALITY: | |
992da01a PB |
10895 | case IORING_REGISTER_FILES2: |
10896 | case IORING_REGISTER_FILES_UPDATE2: | |
10897 | case IORING_REGISTER_BUFFERS2: | |
10898 | case IORING_REGISTER_BUFFERS_UPDATE: | |
fe76421d JA |
10899 | case IORING_REGISTER_IOWQ_AFF: |
10900 | case IORING_UNREGISTER_IOWQ_AFF: | |
2e480058 | 10901 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
071698e1 JA |
10902 | return false; |
10903 | default: | |
10904 | return true; | |
10905 | } | |
10906 | } | |
10907 | ||
c072481d | 10908 | static __cold int io_ctx_quiesce(struct io_ring_ctx *ctx) |
e73c5c7c PB |
10909 | { |
10910 | long ret; | |
10911 | ||
10912 | percpu_ref_kill(&ctx->refs); | |
10913 | ||
10914 | /* | |
10915 | * Drop uring mutex before waiting for references to exit. If another | |
10916 | * thread is currently inside io_uring_enter() it might need to grab the | |
10917 | * uring_lock to make progress. If we hold it here across the drain | |
10918 | * wait, then we can deadlock. It's safe to drop the mutex here, since | |
10919 | * no new references will come in after we've killed the percpu ref. | |
10920 | */ | |
10921 | mutex_unlock(&ctx->uring_lock); | |
10922 | do { | |
37f0e767 PB |
10923 | ret = wait_for_completion_interruptible_timeout(&ctx->ref_comp, HZ); |
10924 | if (ret) { | |
10925 | ret = min(0L, ret); | |
e73c5c7c | 10926 | break; |
37f0e767 PB |
10927 | } |
10928 | ||
e73c5c7c | 10929 | ret = io_run_task_work_sig(); |
37f0e767 | 10930 | io_req_caches_free(ctx); |
e73c5c7c PB |
10931 | } while (ret >= 0); |
10932 | mutex_lock(&ctx->uring_lock); | |
10933 | ||
10934 | if (ret) | |
10935 | io_refs_resurrect(&ctx->refs, &ctx->ref_comp); | |
10936 | return ret; | |
10937 | } | |
10938 | ||
edafccee JA |
10939 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
10940 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
10941 | __releases(ctx->uring_lock) |
10942 | __acquires(ctx->uring_lock) | |
edafccee JA |
10943 | { |
10944 | int ret; | |
10945 | ||
35fa71a0 JA |
10946 | /* |
10947 | * We're inside the ring mutex, if the ref is already dying, then | |
10948 | * someone else killed the ctx or is already going through | |
10949 | * io_uring_register(). | |
10950 | */ | |
10951 | if (percpu_ref_is_dying(&ctx->refs)) | |
10952 | return -ENXIO; | |
10953 | ||
75c4021a PB |
10954 | if (ctx->restricted) { |
10955 | if (opcode >= IORING_REGISTER_LAST) | |
10956 | return -EINVAL; | |
10957 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); | |
10958 | if (!test_bit(opcode, ctx->restrictions.register_op)) | |
10959 | return -EACCES; | |
10960 | } | |
10961 | ||
071698e1 | 10962 | if (io_register_op_must_quiesce(opcode)) { |
e73c5c7c PB |
10963 | ret = io_ctx_quiesce(ctx); |
10964 | if (ret) | |
f70865db | 10965 | return ret; |
05f3fb3c | 10966 | } |
edafccee JA |
10967 | |
10968 | switch (opcode) { | |
10969 | case IORING_REGISTER_BUFFERS: | |
634d00df | 10970 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
edafccee JA |
10971 | break; |
10972 | case IORING_UNREGISTER_BUFFERS: | |
10973 | ret = -EINVAL; | |
10974 | if (arg || nr_args) | |
10975 | break; | |
0a96bbe4 | 10976 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 10977 | break; |
6b06314c | 10978 | case IORING_REGISTER_FILES: |
792e3582 | 10979 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
6b06314c JA |
10980 | break; |
10981 | case IORING_UNREGISTER_FILES: | |
10982 | ret = -EINVAL; | |
10983 | if (arg || nr_args) | |
10984 | break; | |
10985 | ret = io_sqe_files_unregister(ctx); | |
10986 | break; | |
c3a31e60 | 10987 | case IORING_REGISTER_FILES_UPDATE: |
c3bdad02 | 10988 | ret = io_register_files_update(ctx, arg, nr_args); |
c3a31e60 | 10989 | break; |
9b402849 | 10990 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 10991 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
10992 | ret = -EINVAL; |
10993 | if (nr_args != 1) | |
10994 | break; | |
10995 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
10996 | if (ret) |
10997 | break; | |
10998 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
10999 | ctx->eventfd_async = 1; | |
11000 | else | |
11001 | ctx->eventfd_async = 0; | |
9b402849 JA |
11002 | break; |
11003 | case IORING_UNREGISTER_EVENTFD: | |
11004 | ret = -EINVAL; | |
11005 | if (arg || nr_args) | |
11006 | break; | |
11007 | ret = io_eventfd_unregister(ctx); | |
11008 | break; | |
66f4af93 JA |
11009 | case IORING_REGISTER_PROBE: |
11010 | ret = -EINVAL; | |
11011 | if (!arg || nr_args > 256) | |
11012 | break; | |
11013 | ret = io_probe(ctx, arg, nr_args); | |
11014 | break; | |
071698e1 JA |
11015 | case IORING_REGISTER_PERSONALITY: |
11016 | ret = -EINVAL; | |
11017 | if (arg || nr_args) | |
11018 | break; | |
11019 | ret = io_register_personality(ctx); | |
11020 | break; | |
11021 | case IORING_UNREGISTER_PERSONALITY: | |
11022 | ret = -EINVAL; | |
11023 | if (arg) | |
11024 | break; | |
11025 | ret = io_unregister_personality(ctx, nr_args); | |
11026 | break; | |
7e84e1c7 SG |
11027 | case IORING_REGISTER_ENABLE_RINGS: |
11028 | ret = -EINVAL; | |
11029 | if (arg || nr_args) | |
11030 | break; | |
11031 | ret = io_register_enable_rings(ctx); | |
11032 | break; | |
21b55dbc SG |
11033 | case IORING_REGISTER_RESTRICTIONS: |
11034 | ret = io_register_restrictions(ctx, arg, nr_args); | |
11035 | break; | |
992da01a PB |
11036 | case IORING_REGISTER_FILES2: |
11037 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); | |
11038 | break; | |
11039 | case IORING_REGISTER_FILES_UPDATE2: | |
11040 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
11041 | IORING_RSRC_FILE); | |
11042 | break; | |
11043 | case IORING_REGISTER_BUFFERS2: | |
11044 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); | |
792e3582 | 11045 | break; |
992da01a PB |
11046 | case IORING_REGISTER_BUFFERS_UPDATE: |
11047 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
11048 | IORING_RSRC_BUFFER); | |
c3bdad02 | 11049 | break; |
fe76421d JA |
11050 | case IORING_REGISTER_IOWQ_AFF: |
11051 | ret = -EINVAL; | |
11052 | if (!arg || !nr_args) | |
11053 | break; | |
11054 | ret = io_register_iowq_aff(ctx, arg, nr_args); | |
11055 | break; | |
11056 | case IORING_UNREGISTER_IOWQ_AFF: | |
11057 | ret = -EINVAL; | |
11058 | if (arg || nr_args) | |
11059 | break; | |
11060 | ret = io_unregister_iowq_aff(ctx); | |
11061 | break; | |
2e480058 JA |
11062 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
11063 | ret = -EINVAL; | |
11064 | if (!arg || nr_args != 2) | |
11065 | break; | |
11066 | ret = io_register_iowq_max_workers(ctx, arg); | |
11067 | break; | |
edafccee JA |
11068 | default: |
11069 | ret = -EINVAL; | |
11070 | break; | |
11071 | } | |
11072 | ||
071698e1 | 11073 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 11074 | /* bring the ctx back to life */ |
05f3fb3c | 11075 | percpu_ref_reinit(&ctx->refs); |
0f158b4c | 11076 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 11077 | } |
edafccee JA |
11078 | return ret; |
11079 | } | |
11080 | ||
11081 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
11082 | void __user *, arg, unsigned int, nr_args) | |
11083 | { | |
11084 | struct io_ring_ctx *ctx; | |
11085 | long ret = -EBADF; | |
11086 | struct fd f; | |
11087 | ||
11088 | f = fdget(fd); | |
11089 | if (!f.file) | |
11090 | return -EBADF; | |
11091 | ||
11092 | ret = -EOPNOTSUPP; | |
11093 | if (f.file->f_op != &io_uring_fops) | |
11094 | goto out_fput; | |
11095 | ||
11096 | ctx = f.file->private_data; | |
11097 | ||
b6c23dd5 PB |
11098 | io_run_task_work(); |
11099 | ||
edafccee JA |
11100 | mutex_lock(&ctx->uring_lock); |
11101 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
11102 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
11103 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
11104 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
11105 | out_fput: |
11106 | fdput(f); | |
11107 | return ret; | |
11108 | } | |
11109 | ||
2b188cc1 JA |
11110 | static int __init io_uring_init(void) |
11111 | { | |
d7f62e82 SM |
11112 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
11113 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
11114 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
11115 | } while (0) | |
11116 | ||
11117 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
11118 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
11119 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
11120 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
11121 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
11122 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
11123 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
11124 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
11125 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
11126 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 11127 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
11128 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
11129 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
11130 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
11131 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
11132 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
11133 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
11134 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
11135 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
11136 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
11137 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
11138 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
11139 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
11140 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
11141 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
11142 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 11143 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
11144 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
11145 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
16340eab | 11146 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
d7f62e82 | 11147 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
7d67af2c | 11148 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
b9445598 | 11149 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
d7f62e82 | 11150 | |
b0d658ec PB |
11151 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
11152 | sizeof(struct io_uring_rsrc_update)); | |
11153 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > | |
11154 | sizeof(struct io_uring_rsrc_update2)); | |
90499ad0 PB |
11155 | |
11156 | /* ->buf_index is u16 */ | |
11157 | BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); | |
11158 | ||
b0d658ec PB |
11159 | /* should fit into one byte */ |
11160 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); | |
68fe256a PB |
11161 | BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8)); |
11162 | BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS); | |
b0d658ec | 11163 | |
d3656344 | 11164 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
32c2d33e | 11165 | BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int)); |
16340eab | 11166 | |
91f245d5 JA |
11167 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
11168 | SLAB_ACCOUNT); | |
2b188cc1 JA |
11169 | return 0; |
11170 | }; | |
11171 | __initcall(io_uring_init); |