Commit | Line | Data |
---|---|---|
2b188cc1 JA |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Shared application/kernel submission and completion ring pairs, for | |
4 | * supporting fast/efficient IO. | |
5 | * | |
6 | * A note on the read/write ordering memory barriers that are matched between | |
1e84b97b SB |
7 | * the application and kernel side. |
8 | * | |
9 | * After the application reads the CQ ring tail, it must use an | |
10 | * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses | |
11 | * before writing the tail (using smp_load_acquire to read the tail will | |
12 | * do). It also needs a smp_mb() before updating CQ head (ordering the | |
13 | * entry load(s) with the head store), pairing with an implicit barrier | |
d068b506 | 14 | * through a control-dependency in io_get_cqe (smp_store_release to |
1e84b97b SB |
15 | * store head will do). Failure to do so could lead to reading invalid |
16 | * CQ entries. | |
17 | * | |
18 | * Likewise, the application must use an appropriate smp_wmb() before | |
19 | * writing the SQ tail (ordering SQ entry stores with the tail store), | |
20 | * which pairs with smp_load_acquire in io_get_sqring (smp_store_release | |
21 | * to store the tail will do). And it needs a barrier ordering the SQ | |
22 | * head load before writing new SQ entries (smp_load_acquire to read | |
23 | * head will do). | |
24 | * | |
25 | * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application | |
26 | * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after* | |
27 | * updating the SQ tail; a full memory barrier smp_mb() is needed | |
28 | * between. | |
2b188cc1 JA |
29 | * |
30 | * Also see the examples in the liburing library: | |
31 | * | |
32 | * git://git.kernel.dk/liburing | |
33 | * | |
34 | * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens | |
35 | * from data shared between the kernel and application. This is done both | |
36 | * for ordering purposes, but also to ensure that once a value is loaded from | |
37 | * data that the application could potentially modify, it remains stable. | |
38 | * | |
39 | * Copyright (C) 2018-2019 Jens Axboe | |
c992fe29 | 40 | * Copyright (c) 2018-2019 Christoph Hellwig |
2b188cc1 JA |
41 | */ |
42 | #include <linux/kernel.h> | |
43 | #include <linux/init.h> | |
44 | #include <linux/errno.h> | |
45 | #include <linux/syscalls.h> | |
46 | #include <linux/compat.h> | |
52de1fe1 | 47 | #include <net/compat.h> |
2b188cc1 JA |
48 | #include <linux/refcount.h> |
49 | #include <linux/uio.h> | |
6b47ee6e | 50 | #include <linux/bits.h> |
2b188cc1 JA |
51 | |
52 | #include <linux/sched/signal.h> | |
53 | #include <linux/fs.h> | |
54 | #include <linux/file.h> | |
55 | #include <linux/fdtable.h> | |
56 | #include <linux/mm.h> | |
57 | #include <linux/mman.h> | |
2b188cc1 JA |
58 | #include <linux/percpu.h> |
59 | #include <linux/slab.h> | |
2b188cc1 | 60 | #include <linux/blkdev.h> |
edafccee | 61 | #include <linux/bvec.h> |
2b188cc1 JA |
62 | #include <linux/net.h> |
63 | #include <net/sock.h> | |
64 | #include <net/af_unix.h> | |
6b06314c | 65 | #include <net/scm.h> |
2b188cc1 JA |
66 | #include <linux/anon_inodes.h> |
67 | #include <linux/sched/mm.h> | |
68 | #include <linux/uaccess.h> | |
69 | #include <linux/nospec.h> | |
edafccee JA |
70 | #include <linux/sizes.h> |
71 | #include <linux/hugetlb.h> | |
aa4c3967 | 72 | #include <linux/highmem.h> |
15b71abe JA |
73 | #include <linux/namei.h> |
74 | #include <linux/fsnotify.h> | |
4840e418 | 75 | #include <linux/fadvise.h> |
3e4827b0 | 76 | #include <linux/eventpoll.h> |
7d67af2c | 77 | #include <linux/splice.h> |
b41e9852 | 78 | #include <linux/task_work.h> |
bcf5a063 | 79 | #include <linux/pagemap.h> |
0f212204 | 80 | #include <linux/io_uring.h> |
ef98eb04 | 81 | #include <linux/tracehook.h> |
2b188cc1 | 82 | |
c826bd7a DD |
83 | #define CREATE_TRACE_POINTS |
84 | #include <trace/events/io_uring.h> | |
85 | ||
2b188cc1 JA |
86 | #include <uapi/linux/io_uring.h> |
87 | ||
88 | #include "internal.h" | |
561fb04a | 89 | #include "io-wq.h" |
2b188cc1 | 90 | |
5277deaa | 91 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 92 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
4ce8ad95 | 93 | #define IORING_SQPOLL_CAP_ENTRIES_VALUE 8 |
65e19f54 | 94 | |
187f08c1 | 95 | /* only define max */ |
042b0d85 | 96 | #define IORING_MAX_FIXED_FILES (1U << 15) |
21b55dbc SG |
97 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
98 | IORING_REGISTER_LAST + IORING_OP_LAST) | |
2b188cc1 | 99 | |
187f08c1 | 100 | #define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3) |
2d091d62 PB |
101 | #define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT) |
102 | #define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1) | |
103 | ||
489809e2 PB |
104 | #define IORING_MAX_REG_BUFFERS (1U << 14) |
105 | ||
68fe256a PB |
106 | #define SQE_COMMON_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_LINK | \ |
107 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) | |
108 | ||
109 | #define SQE_VALID_FLAGS (SQE_COMMON_FLAGS|IOSQE_BUFFER_SELECT|IOSQE_IO_DRAIN) | |
110 | ||
c854357b | 111 | #define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \ |
d886e185 PB |
112 | REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS | \ |
113 | REQ_F_ASYNC_DATA) | |
b16fed66 | 114 | |
09899b19 PB |
115 | #define IO_TCTX_REFS_CACHE_NR (1U << 10) |
116 | ||
2b188cc1 JA |
117 | struct io_uring { |
118 | u32 head ____cacheline_aligned_in_smp; | |
119 | u32 tail ____cacheline_aligned_in_smp; | |
120 | }; | |
121 | ||
1e84b97b | 122 | /* |
75b28aff HV |
123 | * This data is shared with the application through the mmap at offsets |
124 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
125 | * |
126 | * The offsets to the member fields are published through struct | |
127 | * io_sqring_offsets when calling io_uring_setup. | |
128 | */ | |
75b28aff | 129 | struct io_rings { |
1e84b97b SB |
130 | /* |
131 | * Head and tail offsets into the ring; the offsets need to be | |
132 | * masked to get valid indices. | |
133 | * | |
75b28aff HV |
134 | * The kernel controls head of the sq ring and the tail of the cq ring, |
135 | * and the application controls tail of the sq ring and the head of the | |
136 | * cq ring. | |
1e84b97b | 137 | */ |
75b28aff | 138 | struct io_uring sq, cq; |
1e84b97b | 139 | /* |
75b28aff | 140 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
141 | * ring_entries - 1) |
142 | */ | |
75b28aff HV |
143 | u32 sq_ring_mask, cq_ring_mask; |
144 | /* Ring sizes (constant, power of 2) */ | |
145 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
146 | /* |
147 | * Number of invalid entries dropped by the kernel due to | |
148 | * invalid index stored in array | |
149 | * | |
150 | * Written by the kernel, shouldn't be modified by the | |
151 | * application (i.e. get number of "new events" by comparing to | |
152 | * cached value). | |
153 | * | |
154 | * After a new SQ head value was read by the application this | |
155 | * counter includes all submissions that were dropped reaching | |
156 | * the new SQ head (and possibly more). | |
157 | */ | |
75b28aff | 158 | u32 sq_dropped; |
1e84b97b | 159 | /* |
0d9b5b3a | 160 | * Runtime SQ flags |
1e84b97b SB |
161 | * |
162 | * Written by the kernel, shouldn't be modified by the | |
163 | * application. | |
164 | * | |
165 | * The application needs a full memory barrier before checking | |
166 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
167 | */ | |
75b28aff | 168 | u32 sq_flags; |
0d9b5b3a SG |
169 | /* |
170 | * Runtime CQ flags | |
171 | * | |
172 | * Written by the application, shouldn't be modified by the | |
173 | * kernel. | |
174 | */ | |
fe7e3257 | 175 | u32 cq_flags; |
1e84b97b SB |
176 | /* |
177 | * Number of completion events lost because the queue was full; | |
178 | * this should be avoided by the application by making sure | |
0b4295b5 | 179 | * there are not more requests pending than there is space in |
1e84b97b SB |
180 | * the completion queue. |
181 | * | |
182 | * Written by the kernel, shouldn't be modified by the | |
183 | * application (i.e. get number of "new events" by comparing to | |
184 | * cached value). | |
185 | * | |
186 | * As completion events come in out of order this counter is not | |
187 | * ordered with any other data. | |
188 | */ | |
75b28aff | 189 | u32 cq_overflow; |
1e84b97b SB |
190 | /* |
191 | * Ring buffer of completion events. | |
192 | * | |
193 | * The kernel writes completion events fresh every time they are | |
194 | * produced, so the application is allowed to modify pending | |
195 | * entries. | |
196 | */ | |
75b28aff | 197 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
198 | }; |
199 | ||
45d189c6 | 200 | enum io_uring_cmd_flags { |
51aac424 | 201 | IO_URING_F_COMPLETE_DEFER = 1, |
3b44b371 | 202 | IO_URING_F_UNLOCKED = 2, |
51aac424 PB |
203 | /* int's last bit, sign checks are usually faster than a bit test */ |
204 | IO_URING_F_NONBLOCK = INT_MIN, | |
45d189c6 PB |
205 | }; |
206 | ||
edafccee JA |
207 | struct io_mapped_ubuf { |
208 | u64 ubuf; | |
4751f53d | 209 | u64 ubuf_end; |
edafccee | 210 | unsigned int nr_bvecs; |
de293938 | 211 | unsigned long acct_pages; |
41edf1a5 | 212 | struct bio_vec bvec[]; |
edafccee JA |
213 | }; |
214 | ||
50238531 BM |
215 | struct io_ring_ctx; |
216 | ||
6c2450ae PB |
217 | struct io_overflow_cqe { |
218 | struct io_uring_cqe cqe; | |
219 | struct list_head list; | |
220 | }; | |
221 | ||
a04b0ac0 PB |
222 | struct io_fixed_file { |
223 | /* file * with additional FFS_* flags */ | |
224 | unsigned long file_ptr; | |
225 | }; | |
226 | ||
269bbe5f BM |
227 | struct io_rsrc_put { |
228 | struct list_head list; | |
b60c8dce | 229 | u64 tag; |
50238531 BM |
230 | union { |
231 | void *rsrc; | |
232 | struct file *file; | |
bd54b6fe | 233 | struct io_mapped_ubuf *buf; |
50238531 | 234 | }; |
269bbe5f BM |
235 | }; |
236 | ||
aeca241b | 237 | struct io_file_table { |
042b0d85 | 238 | struct io_fixed_file *files; |
31b51510 JA |
239 | }; |
240 | ||
b895c9a6 | 241 | struct io_rsrc_node { |
05589553 XW |
242 | struct percpu_ref refs; |
243 | struct list_head node; | |
269bbe5f | 244 | struct list_head rsrc_list; |
b895c9a6 | 245 | struct io_rsrc_data *rsrc_data; |
4a38aed2 | 246 | struct llist_node llist; |
e297822b | 247 | bool done; |
05589553 XW |
248 | }; |
249 | ||
40ae0ff7 PB |
250 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
251 | ||
b895c9a6 | 252 | struct io_rsrc_data { |
05f3fb3c JA |
253 | struct io_ring_ctx *ctx; |
254 | ||
2d091d62 PB |
255 | u64 **tags; |
256 | unsigned int nr; | |
40ae0ff7 | 257 | rsrc_put_fn *do_put; |
3e942498 | 258 | atomic_t refs; |
05f3fb3c | 259 | struct completion done; |
8bad28d8 | 260 | bool quiesce; |
05f3fb3c JA |
261 | }; |
262 | ||
5a2e745d JA |
263 | struct io_buffer { |
264 | struct list_head list; | |
265 | __u64 addr; | |
d1f82808 | 266 | __u32 len; |
5a2e745d JA |
267 | __u16 bid; |
268 | }; | |
269 | ||
21b55dbc SG |
270 | struct io_restriction { |
271 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
272 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
273 | u8 sqe_flags_allowed; | |
274 | u8 sqe_flags_required; | |
7e84e1c7 | 275 | bool registered; |
21b55dbc SG |
276 | }; |
277 | ||
37d1e2e3 JA |
278 | enum { |
279 | IO_SQ_THREAD_SHOULD_STOP = 0, | |
280 | IO_SQ_THREAD_SHOULD_PARK, | |
281 | }; | |
282 | ||
534ca6d6 JA |
283 | struct io_sq_data { |
284 | refcount_t refs; | |
9e138a48 | 285 | atomic_t park_pending; |
09a6f4ef | 286 | struct mutex lock; |
69fb2131 JA |
287 | |
288 | /* ctx's that are using this sqd */ | |
289 | struct list_head ctx_list; | |
69fb2131 | 290 | |
534ca6d6 JA |
291 | struct task_struct *thread; |
292 | struct wait_queue_head wait; | |
08369246 XW |
293 | |
294 | unsigned sq_thread_idle; | |
37d1e2e3 JA |
295 | int sq_cpu; |
296 | pid_t task_pid; | |
5c2469e0 | 297 | pid_t task_tgid; |
37d1e2e3 JA |
298 | |
299 | unsigned long state; | |
37d1e2e3 | 300 | struct completion exited; |
534ca6d6 JA |
301 | }; |
302 | ||
6dd0be1e | 303 | #define IO_COMPL_BATCH 32 |
6ff119a6 | 304 | #define IO_REQ_CACHE_SIZE 32 |
bf019da7 | 305 | #define IO_REQ_ALLOC_BATCH 8 |
258b29a9 | 306 | |
a1ab7b35 PB |
307 | struct io_submit_link { |
308 | struct io_kiocb *head; | |
309 | struct io_kiocb *last; | |
310 | }; | |
311 | ||
258b29a9 | 312 | struct io_submit_state { |
5a158c6b PB |
313 | /* inline/task_work completion list, under ->uring_lock */ |
314 | struct io_wq_work_node free_list; | |
315 | /* batch completion logic */ | |
316 | struct io_wq_work_list compl_reqs; | |
a1ab7b35 | 317 | struct io_submit_link link; |
258b29a9 | 318 | |
258b29a9 | 319 | bool plug_started; |
4b628aeb | 320 | bool need_plug; |
5ca7a8b3 | 321 | unsigned short submit_nr; |
5a158c6b | 322 | struct blk_plug plug; |
258b29a9 PB |
323 | }; |
324 | ||
2b188cc1 | 325 | struct io_ring_ctx { |
b52ecf8c | 326 | /* const or read-mostly hot data */ |
2b188cc1 JA |
327 | struct { |
328 | struct percpu_ref refs; | |
2b188cc1 | 329 | |
b52ecf8c | 330 | struct io_rings *rings; |
2b188cc1 | 331 | unsigned int flags; |
e1d85334 | 332 | unsigned int compat: 1; |
e1d85334 RD |
333 | unsigned int drain_next: 1; |
334 | unsigned int eventfd_async: 1; | |
21b55dbc | 335 | unsigned int restricted: 1; |
f18ee4cf | 336 | unsigned int off_timeout_used: 1; |
10c66904 | 337 | unsigned int drain_active: 1; |
b52ecf8c | 338 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 339 | |
7f1129d2 | 340 | /* submission data */ |
b52ecf8c | 341 | struct { |
0499e582 PB |
342 | struct mutex uring_lock; |
343 | ||
75b28aff HV |
344 | /* |
345 | * Ring buffer of indices into array of io_uring_sqe, which is | |
346 | * mmapped by the application using the IORING_OFF_SQES offset. | |
347 | * | |
348 | * This indirection could e.g. be used to assign fixed | |
349 | * io_uring_sqe entries to operations and only submit them to | |
350 | * the queue when needed. | |
351 | * | |
352 | * The kernel modifies neither the indices array nor the entries | |
353 | * array. | |
354 | */ | |
355 | u32 *sq_array; | |
c7af47cf | 356 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
357 | unsigned cached_sq_head; |
358 | unsigned sq_entries; | |
de0617e4 | 359 | struct list_head defer_list; |
7f1129d2 PB |
360 | |
361 | /* | |
362 | * Fixed resources fast path, should be accessed only under | |
363 | * uring_lock, and updated through io_uring_register(2) | |
364 | */ | |
365 | struct io_rsrc_node *rsrc_node; | |
ab409402 | 366 | int rsrc_cached_refs; |
7f1129d2 PB |
367 | struct io_file_table file_table; |
368 | unsigned nr_user_files; | |
369 | unsigned nr_user_bufs; | |
370 | struct io_mapped_ubuf **user_bufs; | |
371 | ||
372 | struct io_submit_state submit_state; | |
5262f567 | 373 | struct list_head timeout_list; |
ef9dd637 | 374 | struct list_head ltimeout_list; |
1d7bb1d5 | 375 | struct list_head cq_overflow_list; |
7f1129d2 PB |
376 | struct xarray io_buffers; |
377 | struct xarray personalities; | |
378 | u32 pers_next; | |
379 | unsigned sq_thread_idle; | |
2b188cc1 JA |
380 | } ____cacheline_aligned_in_smp; |
381 | ||
d0acdee2 | 382 | /* IRQ completion list, under ->completion_lock */ |
c2b6c6bc | 383 | struct io_wq_work_list locked_free_list; |
d0acdee2 | 384 | unsigned int locked_free_nr; |
3c1a2ead | 385 | |
7c30f36a | 386 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
534ca6d6 JA |
387 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
388 | ||
90554200 | 389 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 390 | struct list_head sqd_list; |
75b28aff | 391 | |
5ed7a37d PB |
392 | unsigned long check_cq_overflow; |
393 | ||
206aefde JA |
394 | struct { |
395 | unsigned cached_cq_tail; | |
396 | unsigned cq_entries; | |
0499e582 | 397 | struct eventfd_ctx *cq_ev_fd; |
0499e582 PB |
398 | struct wait_queue_head cq_wait; |
399 | unsigned cq_extra; | |
400 | atomic_t cq_timeouts; | |
0499e582 | 401 | unsigned cq_last_tm_flush; |
206aefde | 402 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 403 | |
2b188cc1 JA |
404 | struct { |
405 | spinlock_t completion_lock; | |
e94f141b | 406 | |
89850fce JA |
407 | spinlock_t timeout_lock; |
408 | ||
def596e9 | 409 | /* |
540e32a0 | 410 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
411 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
412 | * For SQPOLL, only the single threaded io_sq_thread() will | |
413 | * manipulate the list, hence no extra locking is needed there. | |
414 | */ | |
5eef4e87 | 415 | struct io_wq_work_list iopoll_list; |
78076bb6 JA |
416 | struct hlist_head *cancel_hash; |
417 | unsigned cancel_hash_bits; | |
915b3dde | 418 | bool poll_multi_queue; |
2b188cc1 | 419 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 420 | |
21b55dbc | 421 | struct io_restriction restrictions; |
3c1a2ead | 422 | |
b13a8918 PB |
423 | /* slow path rsrc auxilary data, used by update/register */ |
424 | struct { | |
425 | struct io_rsrc_node *rsrc_backup_node; | |
426 | struct io_mapped_ubuf *dummy_ubuf; | |
427 | struct io_rsrc_data *file_data; | |
428 | struct io_rsrc_data *buf_data; | |
429 | ||
430 | struct delayed_work rsrc_put_work; | |
431 | struct llist_head rsrc_put_llist; | |
432 | struct list_head rsrc_ref_list; | |
433 | spinlock_t rsrc_ref_lock; | |
434 | }; | |
435 | ||
3c1a2ead | 436 | /* Keep this last, we don't need it for the fast path */ |
b986af7e PB |
437 | struct { |
438 | #if defined(CONFIG_UNIX) | |
439 | struct socket *ring_sock; | |
440 | #endif | |
441 | /* hashed buffered write serialization */ | |
442 | struct io_wq_hash *hash_map; | |
443 | ||
444 | /* Only used for accounting purposes */ | |
445 | struct user_struct *user; | |
446 | struct mm_struct *mm_account; | |
447 | ||
448 | /* ctx exit and cancelation */ | |
9011bf9a PB |
449 | struct llist_head fallback_llist; |
450 | struct delayed_work fallback_work; | |
b986af7e PB |
451 | struct work_struct exit_work; |
452 | struct list_head tctx_list; | |
453 | struct completion ref_comp; | |
e139a1ec PB |
454 | u32 iowq_limits[2]; |
455 | bool iowq_limits_set; | |
b986af7e | 456 | }; |
2b188cc1 JA |
457 | }; |
458 | ||
53e043b2 SM |
459 | struct io_uring_task { |
460 | /* submission side */ | |
09899b19 | 461 | int cached_refs; |
53e043b2 SM |
462 | struct xarray xa; |
463 | struct wait_queue_head wait; | |
ee53fb2b SM |
464 | const struct io_ring_ctx *last; |
465 | struct io_wq *io_wq; | |
53e043b2 | 466 | struct percpu_counter inflight; |
b303fe2e | 467 | atomic_t inflight_tracked; |
53e043b2 | 468 | atomic_t in_idle; |
53e043b2 SM |
469 | |
470 | spinlock_t task_lock; | |
471 | struct io_wq_work_list task_list; | |
53e043b2 | 472 | struct callback_head task_work; |
6294f368 | 473 | bool task_running; |
53e043b2 SM |
474 | }; |
475 | ||
09bb8394 JA |
476 | /* |
477 | * First field must be the file pointer in all the | |
478 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
479 | */ | |
221c5eb2 JA |
480 | struct io_poll_iocb { |
481 | struct file *file; | |
018043be | 482 | struct wait_queue_head *head; |
221c5eb2 | 483 | __poll_t events; |
8c838788 | 484 | bool done; |
221c5eb2 | 485 | bool canceled; |
392edb45 | 486 | struct wait_queue_entry wait; |
221c5eb2 JA |
487 | }; |
488 | ||
9d805892 | 489 | struct io_poll_update { |
018043be | 490 | struct file *file; |
9d805892 PB |
491 | u64 old_user_data; |
492 | u64 new_user_data; | |
493 | __poll_t events; | |
b69de288 JA |
494 | bool update_events; |
495 | bool update_user_data; | |
018043be PB |
496 | }; |
497 | ||
b5dba59e JA |
498 | struct io_close { |
499 | struct file *file; | |
b5dba59e | 500 | int fd; |
7df778be | 501 | u32 file_slot; |
b5dba59e JA |
502 | }; |
503 | ||
ad8a48ac JA |
504 | struct io_timeout_data { |
505 | struct io_kiocb *req; | |
506 | struct hrtimer timer; | |
507 | struct timespec64 ts; | |
508 | enum hrtimer_mode mode; | |
50c1df2b | 509 | u32 flags; |
ad8a48ac JA |
510 | }; |
511 | ||
8ed8d3c3 JA |
512 | struct io_accept { |
513 | struct file *file; | |
514 | struct sockaddr __user *addr; | |
515 | int __user *addr_len; | |
516 | int flags; | |
aaa4db12 | 517 | u32 file_slot; |
09952e3e | 518 | unsigned long nofile; |
8ed8d3c3 JA |
519 | }; |
520 | ||
521 | struct io_sync { | |
522 | struct file *file; | |
523 | loff_t len; | |
524 | loff_t off; | |
525 | int flags; | |
d63d1b5e | 526 | int mode; |
8ed8d3c3 JA |
527 | }; |
528 | ||
fbf23849 JA |
529 | struct io_cancel { |
530 | struct file *file; | |
531 | u64 addr; | |
532 | }; | |
533 | ||
b29472ee JA |
534 | struct io_timeout { |
535 | struct file *file; | |
bfe68a22 PB |
536 | u32 off; |
537 | u32 target_seq; | |
135fcde8 | 538 | struct list_head list; |
90cd7e42 PB |
539 | /* head of the link, used by linked timeouts only */ |
540 | struct io_kiocb *head; | |
89b263f6 JA |
541 | /* for linked completions */ |
542 | struct io_kiocb *prev; | |
b29472ee JA |
543 | }; |
544 | ||
0bdf7a2d PB |
545 | struct io_timeout_rem { |
546 | struct file *file; | |
547 | u64 addr; | |
9c8e11b3 PB |
548 | |
549 | /* timeout update */ | |
550 | struct timespec64 ts; | |
551 | u32 flags; | |
f1042b6c | 552 | bool ltimeout; |
0bdf7a2d PB |
553 | }; |
554 | ||
9adbd45d JA |
555 | struct io_rw { |
556 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
557 | struct kiocb kiocb; | |
558 | u64 addr; | |
559 | u64 len; | |
560 | }; | |
561 | ||
3fbb51c1 JA |
562 | struct io_connect { |
563 | struct file *file; | |
564 | struct sockaddr __user *addr; | |
565 | int addr_len; | |
566 | }; | |
567 | ||
e47293fd JA |
568 | struct io_sr_msg { |
569 | struct file *file; | |
fddaface | 570 | union { |
4af3417a PB |
571 | struct compat_msghdr __user *umsg_compat; |
572 | struct user_msghdr __user *umsg; | |
573 | void __user *buf; | |
fddaface | 574 | }; |
e47293fd | 575 | int msg_flags; |
bcda7baa | 576 | int bgid; |
fddaface | 577 | size_t len; |
e47293fd JA |
578 | }; |
579 | ||
15b71abe JA |
580 | struct io_open { |
581 | struct file *file; | |
582 | int dfd; | |
b9445598 | 583 | u32 file_slot; |
15b71abe | 584 | struct filename *filename; |
c12cedf2 | 585 | struct open_how how; |
4022e7af | 586 | unsigned long nofile; |
15b71abe JA |
587 | }; |
588 | ||
269bbe5f | 589 | struct io_rsrc_update { |
05f3fb3c JA |
590 | struct file *file; |
591 | u64 arg; | |
592 | u32 nr_args; | |
593 | u32 offset; | |
594 | }; | |
595 | ||
4840e418 JA |
596 | struct io_fadvise { |
597 | struct file *file; | |
598 | u64 offset; | |
599 | u32 len; | |
600 | u32 advice; | |
601 | }; | |
602 | ||
c1ca757b JA |
603 | struct io_madvise { |
604 | struct file *file; | |
605 | u64 addr; | |
606 | u32 len; | |
607 | u32 advice; | |
608 | }; | |
609 | ||
3e4827b0 JA |
610 | struct io_epoll { |
611 | struct file *file; | |
612 | int epfd; | |
613 | int op; | |
614 | int fd; | |
615 | struct epoll_event event; | |
e47293fd JA |
616 | }; |
617 | ||
7d67af2c PB |
618 | struct io_splice { |
619 | struct file *file_out; | |
620 | struct file *file_in; | |
621 | loff_t off_out; | |
622 | loff_t off_in; | |
623 | u64 len; | |
624 | unsigned int flags; | |
625 | }; | |
626 | ||
ddf0322d JA |
627 | struct io_provide_buf { |
628 | struct file *file; | |
629 | __u64 addr; | |
38134ada | 630 | __u32 len; |
ddf0322d JA |
631 | __u32 bgid; |
632 | __u16 nbufs; | |
633 | __u16 bid; | |
634 | }; | |
635 | ||
1d9e1288 BM |
636 | struct io_statx { |
637 | struct file *file; | |
638 | int dfd; | |
639 | unsigned int mask; | |
640 | unsigned int flags; | |
e62753e4 | 641 | const char __user *filename; |
1d9e1288 BM |
642 | struct statx __user *buffer; |
643 | }; | |
644 | ||
36f4fa68 JA |
645 | struct io_shutdown { |
646 | struct file *file; | |
647 | int how; | |
648 | }; | |
649 | ||
80a261fd JA |
650 | struct io_rename { |
651 | struct file *file; | |
652 | int old_dfd; | |
653 | int new_dfd; | |
654 | struct filename *oldpath; | |
655 | struct filename *newpath; | |
656 | int flags; | |
657 | }; | |
658 | ||
14a1143b JA |
659 | struct io_unlink { |
660 | struct file *file; | |
661 | int dfd; | |
662 | int flags; | |
663 | struct filename *filename; | |
664 | }; | |
665 | ||
e34a02dc DK |
666 | struct io_mkdir { |
667 | struct file *file; | |
668 | int dfd; | |
669 | umode_t mode; | |
670 | struct filename *filename; | |
671 | }; | |
672 | ||
7a8721f8 DK |
673 | struct io_symlink { |
674 | struct file *file; | |
675 | int new_dfd; | |
676 | struct filename *oldpath; | |
677 | struct filename *newpath; | |
678 | }; | |
679 | ||
cf30da90 DK |
680 | struct io_hardlink { |
681 | struct file *file; | |
682 | int old_dfd; | |
683 | int new_dfd; | |
684 | struct filename *oldpath; | |
685 | struct filename *newpath; | |
686 | int flags; | |
687 | }; | |
688 | ||
f499a021 JA |
689 | struct io_async_connect { |
690 | struct sockaddr_storage address; | |
691 | }; | |
692 | ||
03b1230c JA |
693 | struct io_async_msghdr { |
694 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
695 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
696 | struct iovec *free_iov; | |
03b1230c JA |
697 | struct sockaddr __user *uaddr; |
698 | struct msghdr msg; | |
b537916c | 699 | struct sockaddr_storage addr; |
03b1230c JA |
700 | }; |
701 | ||
538941e2 | 702 | struct io_rw_state { |
ff6165b2 | 703 | struct iov_iter iter; |
cd658695 | 704 | struct iov_iter_state iter_state; |
c88598a9 | 705 | struct iovec fast_iov[UIO_FASTIOV]; |
538941e2 PB |
706 | }; |
707 | ||
708 | struct io_async_rw { | |
709 | struct io_rw_state s; | |
710 | const struct iovec *free_iovec; | |
227c0c96 | 711 | size_t bytes_done; |
bcf5a063 | 712 | struct wait_page_queue wpq; |
f67676d1 JA |
713 | }; |
714 | ||
6b47ee6e PB |
715 | enum { |
716 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
717 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
718 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
719 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
720 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 721 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 722 | |
dddca226 | 723 | /* first byte is taken by user flags, shift it to not overlap */ |
93d2bcd2 | 724 | REQ_F_FAIL_BIT = 8, |
6b47ee6e PB |
725 | REQ_F_INFLIGHT_BIT, |
726 | REQ_F_CUR_POS_BIT, | |
727 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 728 | REQ_F_LINK_TIMEOUT_BIT, |
99bc4c38 | 729 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 730 | REQ_F_POLLED_BIT, |
bcda7baa | 731 | REQ_F_BUFFER_SELECTED_BIT, |
e342c807 | 732 | REQ_F_COMPLETE_INLINE_BIT, |
230d50d4 | 733 | REQ_F_REISSUE_BIT, |
b8e64b53 | 734 | REQ_F_CREDS_BIT, |
20e60a38 | 735 | REQ_F_REFCOUNT_BIT, |
4d13d1a4 | 736 | REQ_F_ARM_LTIMEOUT_BIT, |
d886e185 | 737 | REQ_F_ASYNC_DATA_BIT, |
7b29f92d | 738 | /* keep async read/write and isreg together and in order */ |
35645ac3 | 739 | REQ_F_SUPPORT_NOWAIT_BIT, |
7b29f92d | 740 | REQ_F_ISREG_BIT, |
84557871 JA |
741 | |
742 | /* not a real bit, just to check we're not overflowing the space */ | |
743 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
744 | }; |
745 | ||
746 | enum { | |
747 | /* ctx owns file */ | |
748 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
749 | /* drain existing IO first */ | |
750 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
751 | /* linked sqes */ | |
752 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
753 | /* doesn't sever on completion < 0 */ | |
754 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
755 | /* IOSQE_ASYNC */ | |
756 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
757 | /* IOSQE_BUFFER_SELECT */ |
758 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 759 | |
6b47ee6e | 760 | /* fail rest of links */ |
93d2bcd2 | 761 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
b05a1bcd | 762 | /* on inflight list, should be cancelled and waited on exit reliably */ |
6b47ee6e PB |
763 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
764 | /* read/write uses file position */ | |
765 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
766 | /* must not punt to workers */ | |
767 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 768 | /* has or had linked timeout */ |
6b47ee6e | 769 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
99bc4c38 PB |
770 | /* needs cleanup */ |
771 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
772 | /* already went through poll handler */ |
773 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
774 | /* buffer already selected */ |
775 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
e342c807 PB |
776 | /* completion is deferred through io_comp_state */ |
777 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
230d50d4 JA |
778 | /* caller should reissue async */ |
779 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), | |
35645ac3 PB |
780 | /* supports async reads/writes */ |
781 | REQ_F_SUPPORT_NOWAIT = BIT(REQ_F_SUPPORT_NOWAIT_BIT), | |
7b29f92d JA |
782 | /* regular file */ |
783 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
b8e64b53 PB |
784 | /* has creds assigned */ |
785 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), | |
20e60a38 PB |
786 | /* skip refcounting if not set */ |
787 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), | |
4d13d1a4 PB |
788 | /* there is a linked timeout that has to be armed */ |
789 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT), | |
d886e185 PB |
790 | /* ->async_data allocated */ |
791 | REQ_F_ASYNC_DATA = BIT(REQ_F_ASYNC_DATA_BIT), | |
d7718a9d JA |
792 | }; |
793 | ||
794 | struct async_poll { | |
795 | struct io_poll_iocb poll; | |
807abcb0 | 796 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
797 | }; |
798 | ||
f237c30a | 799 | typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked); |
5b0a6acc | 800 | |
7cbf1722 | 801 | struct io_task_work { |
5b0a6acc PB |
802 | union { |
803 | struct io_wq_work_node node; | |
804 | struct llist_node fallback_node; | |
805 | }; | |
806 | io_req_tw_func_t func; | |
7cbf1722 JA |
807 | }; |
808 | ||
992da01a PB |
809 | enum { |
810 | IORING_RSRC_FILE = 0, | |
811 | IORING_RSRC_BUFFER = 1, | |
812 | }; | |
813 | ||
09bb8394 JA |
814 | /* |
815 | * NOTE! Each of the iocb union members has the file pointer | |
816 | * as the first entry in their struct definition. So you can | |
817 | * access the file pointer through any of the sub-structs, | |
818 | * or directly as just 'ki_filp' in this struct. | |
819 | */ | |
2b188cc1 | 820 | struct io_kiocb { |
221c5eb2 | 821 | union { |
09bb8394 | 822 | struct file *file; |
9adbd45d | 823 | struct io_rw rw; |
221c5eb2 | 824 | struct io_poll_iocb poll; |
9d805892 | 825 | struct io_poll_update poll_update; |
8ed8d3c3 JA |
826 | struct io_accept accept; |
827 | struct io_sync sync; | |
fbf23849 | 828 | struct io_cancel cancel; |
b29472ee | 829 | struct io_timeout timeout; |
0bdf7a2d | 830 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 831 | struct io_connect connect; |
e47293fd | 832 | struct io_sr_msg sr_msg; |
15b71abe | 833 | struct io_open open; |
b5dba59e | 834 | struct io_close close; |
269bbe5f | 835 | struct io_rsrc_update rsrc_update; |
4840e418 | 836 | struct io_fadvise fadvise; |
c1ca757b | 837 | struct io_madvise madvise; |
3e4827b0 | 838 | struct io_epoll epoll; |
7d67af2c | 839 | struct io_splice splice; |
ddf0322d | 840 | struct io_provide_buf pbuf; |
1d9e1288 | 841 | struct io_statx statx; |
36f4fa68 | 842 | struct io_shutdown shutdown; |
80a261fd | 843 | struct io_rename rename; |
14a1143b | 844 | struct io_unlink unlink; |
e34a02dc | 845 | struct io_mkdir mkdir; |
7a8721f8 | 846 | struct io_symlink symlink; |
cf30da90 | 847 | struct io_hardlink hardlink; |
221c5eb2 | 848 | }; |
2b188cc1 | 849 | |
d625c6ee | 850 | u8 opcode; |
65a6543d XW |
851 | /* polled IO has completed */ |
852 | u8 iopoll_completed; | |
4f4eeba8 | 853 | u16 buf_index; |
d17e56eb PB |
854 | unsigned int flags; |
855 | ||
856 | u64 user_data; | |
9cf7c104 | 857 | u32 result; |
d17e56eb | 858 | u32 cflags; |
4f4eeba8 | 859 | |
010e8e6b | 860 | struct io_ring_ctx *ctx; |
010e8e6b | 861 | struct task_struct *task; |
d7718a9d | 862 | |
269bbe5f | 863 | struct percpu_ref *fixed_rsrc_refs; |
d886e185 PB |
864 | /* store used ubuf, so we can prevent reloading */ |
865 | struct io_mapped_ubuf *imu; | |
fcb323cc | 866 | |
7e3709d5 | 867 | /* used by request caches, completion batching and iopoll */ |
ef05d9eb | 868 | struct io_wq_work_node comp_list; |
d17e56eb | 869 | atomic_t refs; |
7e3709d5 | 870 | struct io_kiocb *link; |
5b0a6acc | 871 | struct io_task_work io_task_work; |
010e8e6b PB |
872 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
873 | struct hlist_node hash_node; | |
7e3709d5 | 874 | /* internal polling, see IORING_FEAT_FAST_POLL */ |
010e8e6b | 875 | struct async_poll *apoll; |
d886e185 PB |
876 | /* opcode allocated if it needs to store data for async defer */ |
877 | void *async_data; | |
010e8e6b | 878 | struct io_wq_work work; |
7e3709d5 | 879 | /* custom credentials, valid IFF REQ_F_CREDS is set */ |
fe7e3257 | 880 | const struct cred *creds; |
7e3709d5 | 881 | /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */ |
30d51dd4 | 882 | struct io_buffer *kbuf; |
2b188cc1 | 883 | }; |
05589553 | 884 | |
13bf43f5 PB |
885 | struct io_tctx_node { |
886 | struct list_head ctx_node; | |
887 | struct task_struct *task; | |
13bf43f5 PB |
888 | struct io_ring_ctx *ctx; |
889 | }; | |
890 | ||
27dc8338 PB |
891 | struct io_defer_entry { |
892 | struct list_head list; | |
893 | struct io_kiocb *req; | |
9cf7c104 | 894 | u32 seq; |
2b188cc1 JA |
895 | }; |
896 | ||
d3656344 | 897 | struct io_op_def { |
d3656344 JA |
898 | /* needs req->file assigned */ |
899 | unsigned needs_file : 1; | |
6d63416d PB |
900 | /* should block plug */ |
901 | unsigned plug : 1; | |
d3656344 JA |
902 | /* hash wq insertion if file is a regular file */ |
903 | unsigned hash_reg_file : 1; | |
904 | /* unbound wq insertion if file is a non-regular file */ | |
905 | unsigned unbound_nonreg_file : 1; | |
8a72758c JA |
906 | /* set if opcode supports polled "wait" */ |
907 | unsigned pollin : 1; | |
908 | unsigned pollout : 1; | |
bcda7baa JA |
909 | /* op supports buffer selection */ |
910 | unsigned buffer_select : 1; | |
26f0505a PB |
911 | /* do prep async if is going to be punted */ |
912 | unsigned needs_async_setup : 1; | |
6d63416d PB |
913 | /* opcode is not supported by this kernel */ |
914 | unsigned not_supported : 1; | |
e8c2bc1f JA |
915 | /* size of async data needed, if any */ |
916 | unsigned short async_size; | |
d3656344 JA |
917 | }; |
918 | ||
0918682b | 919 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
920 | [IORING_OP_NOP] = {}, |
921 | [IORING_OP_READV] = { | |
d3656344 JA |
922 | .needs_file = 1, |
923 | .unbound_nonreg_file = 1, | |
8a72758c | 924 | .pollin = 1, |
4d954c25 | 925 | .buffer_select = 1, |
26f0505a | 926 | .needs_async_setup = 1, |
27926b68 | 927 | .plug = 1, |
e8c2bc1f | 928 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 929 | }, |
0463b6c5 | 930 | [IORING_OP_WRITEV] = { |
d3656344 JA |
931 | .needs_file = 1, |
932 | .hash_reg_file = 1, | |
933 | .unbound_nonreg_file = 1, | |
8a72758c | 934 | .pollout = 1, |
26f0505a | 935 | .needs_async_setup = 1, |
27926b68 | 936 | .plug = 1, |
e8c2bc1f | 937 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 938 | }, |
0463b6c5 | 939 | [IORING_OP_FSYNC] = { |
d3656344 JA |
940 | .needs_file = 1, |
941 | }, | |
0463b6c5 | 942 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
943 | .needs_file = 1, |
944 | .unbound_nonreg_file = 1, | |
8a72758c | 945 | .pollin = 1, |
27926b68 | 946 | .plug = 1, |
e8c2bc1f | 947 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 948 | }, |
0463b6c5 | 949 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
950 | .needs_file = 1, |
951 | .hash_reg_file = 1, | |
952 | .unbound_nonreg_file = 1, | |
8a72758c | 953 | .pollout = 1, |
27926b68 | 954 | .plug = 1, |
e8c2bc1f | 955 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 956 | }, |
0463b6c5 | 957 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
958 | .needs_file = 1, |
959 | .unbound_nonreg_file = 1, | |
960 | }, | |
0463b6c5 PB |
961 | [IORING_OP_POLL_REMOVE] = {}, |
962 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 JA |
963 | .needs_file = 1, |
964 | }, | |
0463b6c5 | 965 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
966 | .needs_file = 1, |
967 | .unbound_nonreg_file = 1, | |
8a72758c | 968 | .pollout = 1, |
26f0505a | 969 | .needs_async_setup = 1, |
e8c2bc1f | 970 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 971 | }, |
0463b6c5 | 972 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
973 | .needs_file = 1, |
974 | .unbound_nonreg_file = 1, | |
8a72758c | 975 | .pollin = 1, |
52de1fe1 | 976 | .buffer_select = 1, |
26f0505a | 977 | .needs_async_setup = 1, |
e8c2bc1f | 978 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 979 | }, |
0463b6c5 | 980 | [IORING_OP_TIMEOUT] = { |
e8c2bc1f | 981 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 982 | }, |
9c8e11b3 PB |
983 | [IORING_OP_TIMEOUT_REMOVE] = { |
984 | /* used by timeout updates' prep() */ | |
9c8e11b3 | 985 | }, |
0463b6c5 | 986 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
987 | .needs_file = 1, |
988 | .unbound_nonreg_file = 1, | |
8a72758c | 989 | .pollin = 1, |
d3656344 | 990 | }, |
0463b6c5 PB |
991 | [IORING_OP_ASYNC_CANCEL] = {}, |
992 | [IORING_OP_LINK_TIMEOUT] = { | |
e8c2bc1f | 993 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 994 | }, |
0463b6c5 | 995 | [IORING_OP_CONNECT] = { |
d3656344 JA |
996 | .needs_file = 1, |
997 | .unbound_nonreg_file = 1, | |
8a72758c | 998 | .pollout = 1, |
26f0505a | 999 | .needs_async_setup = 1, |
e8c2bc1f | 1000 | .async_size = sizeof(struct io_async_connect), |
d3656344 | 1001 | }, |
0463b6c5 | 1002 | [IORING_OP_FALLOCATE] = { |
d3656344 | 1003 | .needs_file = 1, |
d3656344 | 1004 | }, |
44526bed JA |
1005 | [IORING_OP_OPENAT] = {}, |
1006 | [IORING_OP_CLOSE] = {}, | |
1007 | [IORING_OP_FILES_UPDATE] = {}, | |
1008 | [IORING_OP_STATX] = {}, | |
0463b6c5 | 1009 | [IORING_OP_READ] = { |
3a6820f2 JA |
1010 | .needs_file = 1, |
1011 | .unbound_nonreg_file = 1, | |
8a72758c | 1012 | .pollin = 1, |
bcda7baa | 1013 | .buffer_select = 1, |
27926b68 | 1014 | .plug = 1, |
e8c2bc1f | 1015 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1016 | }, |
0463b6c5 | 1017 | [IORING_OP_WRITE] = { |
3a6820f2 | 1018 | .needs_file = 1, |
7b3188e7 | 1019 | .hash_reg_file = 1, |
3a6820f2 | 1020 | .unbound_nonreg_file = 1, |
8a72758c | 1021 | .pollout = 1, |
27926b68 | 1022 | .plug = 1, |
e8c2bc1f | 1023 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1024 | }, |
0463b6c5 | 1025 | [IORING_OP_FADVISE] = { |
4840e418 | 1026 | .needs_file = 1, |
c1ca757b | 1027 | }, |
44526bed | 1028 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 1029 | [IORING_OP_SEND] = { |
fddaface JA |
1030 | .needs_file = 1, |
1031 | .unbound_nonreg_file = 1, | |
8a72758c | 1032 | .pollout = 1, |
fddaface | 1033 | }, |
0463b6c5 | 1034 | [IORING_OP_RECV] = { |
fddaface JA |
1035 | .needs_file = 1, |
1036 | .unbound_nonreg_file = 1, | |
8a72758c | 1037 | .pollin = 1, |
bcda7baa | 1038 | .buffer_select = 1, |
fddaface | 1039 | }, |
0463b6c5 | 1040 | [IORING_OP_OPENAT2] = { |
cebdb986 | 1041 | }, |
3e4827b0 JA |
1042 | [IORING_OP_EPOLL_CTL] = { |
1043 | .unbound_nonreg_file = 1, | |
3e4827b0 | 1044 | }, |
7d67af2c PB |
1045 | [IORING_OP_SPLICE] = { |
1046 | .needs_file = 1, | |
1047 | .hash_reg_file = 1, | |
1048 | .unbound_nonreg_file = 1, | |
ddf0322d JA |
1049 | }, |
1050 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 1051 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
1052 | [IORING_OP_TEE] = { |
1053 | .needs_file = 1, | |
1054 | .hash_reg_file = 1, | |
1055 | .unbound_nonreg_file = 1, | |
1056 | }, | |
36f4fa68 JA |
1057 | [IORING_OP_SHUTDOWN] = { |
1058 | .needs_file = 1, | |
1059 | }, | |
44526bed JA |
1060 | [IORING_OP_RENAMEAT] = {}, |
1061 | [IORING_OP_UNLINKAT] = {}, | |
e34a02dc | 1062 | [IORING_OP_MKDIRAT] = {}, |
7a8721f8 | 1063 | [IORING_OP_SYMLINKAT] = {}, |
cf30da90 | 1064 | [IORING_OP_LINKAT] = {}, |
d3656344 JA |
1065 | }; |
1066 | ||
0756a869 PB |
1067 | /* requests with any of those set should undergo io_disarm_next() */ |
1068 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) | |
1069 | ||
7a612350 | 1070 | static bool io_disarm_next(struct io_kiocb *req); |
eef51daa | 1071 | static void io_uring_del_tctx_node(unsigned long index); |
9936c7c2 PB |
1072 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1073 | struct task_struct *task, | |
3dd0c97a | 1074 | bool cancel_all); |
78cc687b | 1075 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); |
1ffc5422 | 1076 | |
d4d19c19 | 1077 | static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
54daa9b2 | 1078 | s32 res, u32 cflags); |
ec9c02ad | 1079 | static void io_put_req(struct io_kiocb *req); |
91c2f697 | 1080 | static void io_put_req_deferred(struct io_kiocb *req); |
c7dae4ba | 1081 | static void io_dismantle_req(struct io_kiocb *req); |
94ae5e77 | 1082 | static void io_queue_linked_timeout(struct io_kiocb *req); |
fdecb662 | 1083 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 1084 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 | 1085 | unsigned nr_args); |
68fb8979 | 1086 | static void io_clean_op(struct io_kiocb *req); |
ac177053 | 1087 | static struct file *io_file_get(struct io_ring_ctx *ctx, |
8371adf5 | 1088 | struct io_kiocb *req, int fd, bool fixed); |
c5eef2b9 | 1089 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1090 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1091 | |
907d1df3 | 1092 | static void io_req_task_queue(struct io_kiocb *req); |
c450178d | 1093 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx); |
179ae0d1 | 1094 | static int io_req_prep_async(struct io_kiocb *req); |
de0617e4 | 1095 | |
b9445598 PB |
1096 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
1097 | unsigned int issue_flags, u32 slot_index); | |
7df778be PB |
1098 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags); |
1099 | ||
f1042b6c | 1100 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer); |
b9445598 | 1101 | |
2b188cc1 JA |
1102 | static struct kmem_cache *req_cachep; |
1103 | ||
0918682b | 1104 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1105 | |
1106 | struct sock *io_uring_get_socket(struct file *file) | |
1107 | { | |
1108 | #if defined(CONFIG_UNIX) | |
1109 | if (file->f_op == &io_uring_fops) { | |
1110 | struct io_ring_ctx *ctx = file->private_data; | |
1111 | ||
1112 | return ctx->ring_sock->sk; | |
1113 | } | |
1114 | #endif | |
1115 | return NULL; | |
1116 | } | |
1117 | EXPORT_SYMBOL(io_uring_get_socket); | |
1118 | ||
f237c30a PB |
1119 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
1120 | { | |
1121 | if (!*locked) { | |
1122 | mutex_lock(&ctx->uring_lock); | |
1123 | *locked = true; | |
1124 | } | |
1125 | } | |
1126 | ||
f2f87370 PB |
1127 | #define io_for_each_link(pos, head) \ |
1128 | for (pos = (head); pos; pos = pos->link) | |
1129 | ||
21c843d5 PB |
1130 | /* |
1131 | * Shamelessly stolen from the mm implementation of page reference checking, | |
1132 | * see commit f958d7b528b1 for details. | |
1133 | */ | |
1134 | #define req_ref_zero_or_close_to_overflow(req) \ | |
1135 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) | |
1136 | ||
1137 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) | |
1138 | { | |
20e60a38 | 1139 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1140 | return atomic_inc_not_zero(&req->refs); |
1141 | } | |
1142 | ||
21c843d5 PB |
1143 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
1144 | { | |
20e60a38 PB |
1145 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
1146 | return true; | |
1147 | ||
21c843d5 PB |
1148 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1149 | return atomic_dec_and_test(&req->refs); | |
1150 | } | |
1151 | ||
1152 | static inline void req_ref_put(struct io_kiocb *req) | |
1153 | { | |
20e60a38 | 1154 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1155 | WARN_ON_ONCE(req_ref_put_and_test(req)); |
1156 | } | |
1157 | ||
1158 | static inline void req_ref_get(struct io_kiocb *req) | |
1159 | { | |
20e60a38 | 1160 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1161 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1162 | atomic_inc(&req->refs); | |
1163 | } | |
1164 | ||
c450178d PB |
1165 | static inline void io_submit_flush_completions(struct io_ring_ctx *ctx) |
1166 | { | |
6f33b0bc | 1167 | if (!wq_list_empty(&ctx->submit_state.compl_reqs)) |
c450178d PB |
1168 | __io_submit_flush_completions(ctx); |
1169 | } | |
1170 | ||
48dcd38d | 1171 | static inline void __io_req_set_refcount(struct io_kiocb *req, int nr) |
20e60a38 PB |
1172 | { |
1173 | if (!(req->flags & REQ_F_REFCOUNT)) { | |
1174 | req->flags |= REQ_F_REFCOUNT; | |
48dcd38d | 1175 | atomic_set(&req->refs, nr); |
20e60a38 PB |
1176 | } |
1177 | } | |
1178 | ||
48dcd38d PB |
1179 | static inline void io_req_set_refcount(struct io_kiocb *req) |
1180 | { | |
1181 | __io_req_set_refcount(req, 1); | |
1182 | } | |
1183 | ||
ab409402 PB |
1184 | #define IO_RSRC_REF_BATCH 100 |
1185 | ||
1186 | static inline void io_req_put_rsrc_locked(struct io_kiocb *req, | |
1187 | struct io_ring_ctx *ctx) | |
1188 | __must_hold(&ctx->uring_lock) | |
36f72fe2 | 1189 | { |
ab409402 PB |
1190 | struct percpu_ref *ref = req->fixed_rsrc_refs; |
1191 | ||
1192 | if (ref) { | |
1193 | if (ref == &ctx->rsrc_node->refs) | |
1194 | ctx->rsrc_cached_refs++; | |
1195 | else | |
1196 | percpu_ref_put(ref); | |
1197 | } | |
1198 | } | |
1199 | ||
1200 | static inline void io_req_put_rsrc(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
1201 | { | |
1202 | if (req->fixed_rsrc_refs) | |
1203 | percpu_ref_put(req->fixed_rsrc_refs); | |
1204 | } | |
1205 | ||
1206 | static __cold void io_rsrc_refs_drop(struct io_ring_ctx *ctx) | |
1207 | __must_hold(&ctx->uring_lock) | |
1208 | { | |
1209 | if (ctx->rsrc_cached_refs) { | |
1210 | percpu_ref_put_many(&ctx->rsrc_node->refs, ctx->rsrc_cached_refs); | |
1211 | ctx->rsrc_cached_refs = 0; | |
1212 | } | |
1213 | } | |
1214 | ||
1215 | static void io_rsrc_refs_refill(struct io_ring_ctx *ctx) | |
1216 | __must_hold(&ctx->uring_lock) | |
1217 | { | |
1218 | ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH; | |
1219 | percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH); | |
1220 | } | |
36f72fe2 | 1221 | |
a46be971 PB |
1222 | static inline void io_req_set_rsrc_node(struct io_kiocb *req, |
1223 | struct io_ring_ctx *ctx) | |
36f72fe2 | 1224 | { |
269bbe5f | 1225 | if (!req->fixed_rsrc_refs) { |
a7f0ed5a | 1226 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
ab409402 PB |
1227 | ctx->rsrc_cached_refs--; |
1228 | if (unlikely(ctx->rsrc_cached_refs < 0)) | |
1229 | io_rsrc_refs_refill(ctx); | |
36f72fe2 PB |
1230 | } |
1231 | } | |
1232 | ||
f70865db PB |
1233 | static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
1234 | { | |
1235 | bool got = percpu_ref_tryget(ref); | |
1236 | ||
1237 | /* already at zero, wait for ->release() */ | |
1238 | if (!got) | |
1239 | wait_for_completion(compl); | |
1240 | percpu_ref_resurrect(ref); | |
1241 | if (got) | |
1242 | percpu_ref_put(ref); | |
1243 | } | |
1244 | ||
3dd0c97a PB |
1245 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
1246 | bool cancel_all) | |
08d23634 PB |
1247 | { |
1248 | struct io_kiocb *req; | |
1249 | ||
68207680 | 1250 | if (task && head->task != task) |
08d23634 | 1251 | return false; |
3dd0c97a | 1252 | if (cancel_all) |
08d23634 PB |
1253 | return true; |
1254 | ||
1255 | io_for_each_link(req, head) { | |
b05a1bcd | 1256 | if (req->flags & REQ_F_INFLIGHT) |
02a13674 | 1257 | return true; |
08d23634 PB |
1258 | } |
1259 | return false; | |
1260 | } | |
1261 | ||
d886e185 PB |
1262 | static inline bool req_has_async_data(struct io_kiocb *req) |
1263 | { | |
1264 | return req->flags & REQ_F_ASYNC_DATA; | |
1265 | } | |
1266 | ||
93d2bcd2 | 1267 | static inline void req_set_fail(struct io_kiocb *req) |
c40f6379 | 1268 | { |
93d2bcd2 | 1269 | req->flags |= REQ_F_FAIL; |
c40f6379 | 1270 | } |
4a38aed2 | 1271 | |
a8295b98 HX |
1272 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
1273 | { | |
1274 | req_set_fail(req); | |
1275 | req->result = res; | |
1276 | } | |
1277 | ||
c072481d | 1278 | static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref) |
2b188cc1 JA |
1279 | { |
1280 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1281 | ||
0f158b4c | 1282 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1283 | } |
1284 | ||
8eb7e2d0 PB |
1285 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1286 | { | |
1287 | return !req->timeout.off; | |
1288 | } | |
1289 | ||
c072481d | 1290 | static __cold void io_fallback_req_func(struct work_struct *work) |
f56165e6 PB |
1291 | { |
1292 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, | |
1293 | fallback_work.work); | |
1294 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); | |
1295 | struct io_kiocb *req, *tmp; | |
f237c30a | 1296 | bool locked = false; |
f56165e6 PB |
1297 | |
1298 | percpu_ref_get(&ctx->refs); | |
1299 | llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node) | |
f237c30a | 1300 | req->io_task_work.func(req, &locked); |
5636c00d | 1301 | |
f237c30a | 1302 | if (locked) { |
c450178d | 1303 | io_submit_flush_completions(ctx); |
f237c30a PB |
1304 | mutex_unlock(&ctx->uring_lock); |
1305 | } | |
f56165e6 PB |
1306 | percpu_ref_put(&ctx->refs); |
1307 | } | |
1308 | ||
c072481d | 1309 | static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
2b188cc1 JA |
1310 | { |
1311 | struct io_ring_ctx *ctx; | |
78076bb6 | 1312 | int hash_bits; |
2b188cc1 JA |
1313 | |
1314 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1315 | if (!ctx) | |
1316 | return NULL; | |
1317 | ||
78076bb6 JA |
1318 | /* |
1319 | * Use 5 bits less than the max cq entries, that should give us around | |
1320 | * 32 entries per hash list if totally full and uniformly spread. | |
1321 | */ | |
1322 | hash_bits = ilog2(p->cq_entries); | |
1323 | hash_bits -= 5; | |
1324 | if (hash_bits <= 0) | |
1325 | hash_bits = 1; | |
1326 | ctx->cancel_hash_bits = hash_bits; | |
1327 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1328 | GFP_KERNEL); | |
1329 | if (!ctx->cancel_hash) | |
1330 | goto err; | |
1331 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1332 | ||
6224843d PB |
1333 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
1334 | if (!ctx->dummy_ubuf) | |
1335 | goto err; | |
1336 | /* set invalid range, so io_import_fixed() fails meeting it */ | |
1337 | ctx->dummy_ubuf->ubuf = -1UL; | |
1338 | ||
21482896 | 1339 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1340 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1341 | goto err; | |
2b188cc1 JA |
1342 | |
1343 | ctx->flags = p->flags; | |
90554200 | 1344 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1345 | INIT_LIST_HEAD(&ctx->sqd_list); |
1d7bb1d5 | 1346 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c | 1347 | init_completion(&ctx->ref_comp); |
9e15c3a0 | 1348 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
61cf9370 | 1349 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
2b188cc1 | 1350 | mutex_init(&ctx->uring_lock); |
311997b3 | 1351 | init_waitqueue_head(&ctx->cq_wait); |
2b188cc1 | 1352 | spin_lock_init(&ctx->completion_lock); |
89850fce | 1353 | spin_lock_init(&ctx->timeout_lock); |
5eef4e87 | 1354 | INIT_WQ_LIST(&ctx->iopoll_list); |
de0617e4 | 1355 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1356 | INIT_LIST_HEAD(&ctx->timeout_list); |
ef9dd637 | 1357 | INIT_LIST_HEAD(&ctx->ltimeout_list); |
d67d2263 BM |
1358 | spin_lock_init(&ctx->rsrc_ref_lock); |
1359 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1360 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1361 | init_llist_head(&ctx->rsrc_put_llist); | |
13bf43f5 | 1362 | INIT_LIST_HEAD(&ctx->tctx_list); |
c2b6c6bc PB |
1363 | ctx->submit_state.free_list.next = NULL; |
1364 | INIT_WQ_LIST(&ctx->locked_free_list); | |
9011bf9a | 1365 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
6f33b0bc | 1366 | INIT_WQ_LIST(&ctx->submit_state.compl_reqs); |
2b188cc1 | 1367 | return ctx; |
206aefde | 1368 | err: |
6224843d | 1369 | kfree(ctx->dummy_ubuf); |
78076bb6 | 1370 | kfree(ctx->cancel_hash); |
206aefde JA |
1371 | kfree(ctx); |
1372 | return NULL; | |
2b188cc1 JA |
1373 | } |
1374 | ||
8f6ed49a PB |
1375 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
1376 | { | |
1377 | struct io_rings *r = ctx->rings; | |
1378 | ||
1379 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); | |
1380 | ctx->cq_extra--; | |
1381 | } | |
1382 | ||
9cf7c104 | 1383 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1384 | { |
2bc9930e JA |
1385 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1386 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1387 | |
8f6ed49a | 1388 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
2bc9930e | 1389 | } |
de0617e4 | 1390 | |
9d858b21 | 1391 | return false; |
de0617e4 JA |
1392 | } |
1393 | ||
35645ac3 PB |
1394 | #define FFS_NOWAIT 0x1UL |
1395 | #define FFS_ISREG 0x2UL | |
1396 | #define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG) | |
c97d8a0f PB |
1397 | |
1398 | static inline bool io_req_ffs_set(struct io_kiocb *req) | |
1399 | { | |
35645ac3 | 1400 | return req->flags & REQ_F_FIXED_FILE; |
c97d8a0f PB |
1401 | } |
1402 | ||
c072481d | 1403 | static inline void io_req_track_inflight(struct io_kiocb *req) |
ce3d5aae | 1404 | { |
ce3d5aae | 1405 | if (!(req->flags & REQ_F_INFLIGHT)) { |
ce3d5aae | 1406 | req->flags |= REQ_F_INFLIGHT; |
b303fe2e | 1407 | atomic_inc(¤t->io_uring->inflight_tracked); |
ce3d5aae PB |
1408 | } |
1409 | } | |
1410 | ||
fd08e530 PB |
1411 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
1412 | { | |
906c6caa PB |
1413 | if (WARN_ON_ONCE(!req->link)) |
1414 | return NULL; | |
1415 | ||
4d13d1a4 PB |
1416 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
1417 | req->flags |= REQ_F_LINK_TIMEOUT; | |
fd08e530 PB |
1418 | |
1419 | /* linked timeouts should have two refs once prep'ed */ | |
48dcd38d | 1420 | io_req_set_refcount(req); |
4d13d1a4 PB |
1421 | __io_req_set_refcount(req->link, 2); |
1422 | return req->link; | |
fd08e530 PB |
1423 | } |
1424 | ||
1425 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) | |
1426 | { | |
4d13d1a4 | 1427 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
fd08e530 PB |
1428 | return NULL; |
1429 | return __io_prep_linked_timeout(req); | |
1430 | } | |
1431 | ||
1e6fa521 JA |
1432 | static void io_prep_async_work(struct io_kiocb *req) |
1433 | { | |
1434 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1435 | struct io_ring_ctx *ctx = req->ctx; |
1436 | ||
b8e64b53 PB |
1437 | if (!(req->flags & REQ_F_CREDS)) { |
1438 | req->flags |= REQ_F_CREDS; | |
c10d1f98 | 1439 | req->creds = get_current_cred(); |
b8e64b53 | 1440 | } |
003e8dcc | 1441 | |
e1d675df PB |
1442 | req->work.list.next = NULL; |
1443 | req->work.flags = 0; | |
feaadc4f PB |
1444 | if (req->flags & REQ_F_FORCE_ASYNC) |
1445 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1446 | ||
1e6fa521 JA |
1447 | if (req->flags & REQ_F_ISREG) { |
1448 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1449 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
4b982bd0 | 1450 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
1e6fa521 JA |
1451 | if (def->unbound_nonreg_file) |
1452 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1453 | } | |
e1d675df PB |
1454 | |
1455 | switch (req->opcode) { | |
1456 | case IORING_OP_SPLICE: | |
1457 | case IORING_OP_TEE: | |
e1d675df PB |
1458 | if (!S_ISREG(file_inode(req->splice.file_in)->i_mode)) |
1459 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1460 | break; | |
1461 | } | |
561fb04a | 1462 | } |
cccf0ee8 | 1463 | |
cbdcb435 | 1464 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1465 | { |
cbdcb435 | 1466 | struct io_kiocb *cur; |
54a91f3b | 1467 | |
44eff40a PB |
1468 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
1469 | struct io_ring_ctx *ctx = req->ctx; | |
1470 | ||
79ebeaee | 1471 | spin_lock(&ctx->completion_lock); |
44eff40a PB |
1472 | io_for_each_link(cur, req) |
1473 | io_prep_async_work(cur); | |
79ebeaee | 1474 | spin_unlock(&ctx->completion_lock); |
44eff40a PB |
1475 | } else { |
1476 | io_for_each_link(cur, req) | |
1477 | io_prep_async_work(cur); | |
1478 | } | |
561fb04a JA |
1479 | } |
1480 | ||
fff4e40e PB |
1481 | static inline void io_req_add_compl_list(struct io_kiocb *req) |
1482 | { | |
1483 | struct io_submit_state *state = &req->ctx->submit_state; | |
1484 | ||
1485 | wq_list_add_tail(&req->comp_list, &state->compl_reqs); | |
1486 | } | |
1487 | ||
00169246 | 1488 | static void io_queue_async_work(struct io_kiocb *req, bool *dont_use) |
561fb04a | 1489 | { |
a197f664 | 1490 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1491 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1492 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1493 | |
3bfe6106 JA |
1494 | BUG_ON(!tctx); |
1495 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1496 | |
cbdcb435 PB |
1497 | /* init ->work of the whole link before punting */ |
1498 | io_prep_async_link(req); | |
991468dc JA |
1499 | |
1500 | /* | |
1501 | * Not expected to happen, but if we do have a bug where this _can_ | |
1502 | * happen, catch it here and ensure the request is marked as | |
1503 | * canceled. That will make io-wq go through the usual work cancel | |
1504 | * procedure rather than attempt to run this request (or create a new | |
1505 | * worker for it). | |
1506 | */ | |
1507 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) | |
1508 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1509 | ||
d07f1e8a PB |
1510 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1511 | &req->work, req->flags); | |
ebf93667 | 1512 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a JA |
1513 | if (link) |
1514 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1515 | } |
1516 | ||
1ee4160c | 1517 | static void io_kill_timeout(struct io_kiocb *req, int status) |
8c855885 | 1518 | __must_hold(&req->ctx->completion_lock) |
89850fce | 1519 | __must_hold(&req->ctx->timeout_lock) |
5262f567 | 1520 | { |
e8c2bc1f | 1521 | struct io_timeout_data *io = req->async_data; |
5262f567 | 1522 | |
fd9c7bc5 | 1523 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
2ae2eb9d PB |
1524 | if (status) |
1525 | req_set_fail(req); | |
01cec8c1 PB |
1526 | atomic_set(&req->ctx->cq_timeouts, |
1527 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1528 | list_del_init(&req->timeout.list); |
d4d19c19 | 1529 | io_cqring_fill_event(req->ctx, req->user_data, status, 0); |
91c2f697 | 1530 | io_put_req_deferred(req); |
5262f567 JA |
1531 | } |
1532 | } | |
1533 | ||
c072481d | 1534 | static __cold void io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1535 | { |
441b8a78 | 1536 | while (!list_empty(&ctx->defer_list)) { |
27dc8338 PB |
1537 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1538 | struct io_defer_entry, list); | |
de0617e4 | 1539 | |
9cf7c104 | 1540 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1541 | break; |
27dc8338 | 1542 | list_del_init(&de->list); |
907d1df3 | 1543 | io_req_task_queue(de->req); |
27dc8338 | 1544 | kfree(de); |
441b8a78 | 1545 | } |
04518945 PB |
1546 | } |
1547 | ||
c072481d | 1548 | static __cold void io_flush_timeouts(struct io_ring_ctx *ctx) |
89850fce | 1549 | __must_hold(&ctx->completion_lock) |
de0617e4 | 1550 | { |
441b8a78 | 1551 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
f010505b | 1552 | |
79ebeaee | 1553 | spin_lock_irq(&ctx->timeout_lock); |
f18ee4cf | 1554 | while (!list_empty(&ctx->timeout_list)) { |
f010505b | 1555 | u32 events_needed, events_got; |
360428f8 | 1556 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1557 | struct io_kiocb, timeout.list); |
de0617e4 | 1558 | |
8eb7e2d0 | 1559 | if (io_is_timeout_noseq(req)) |
360428f8 | 1560 | break; |
f010505b MDG |
1561 | |
1562 | /* | |
1563 | * Since seq can easily wrap around over time, subtract | |
1564 | * the last seq at which timeouts were flushed before comparing. | |
1565 | * Assuming not more than 2^31-1 events have happened since, | |
1566 | * these subtractions won't have wrapped, so we can check if | |
1567 | * target is in [last_seq, current_seq] by comparing the two. | |
1568 | */ | |
1569 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1570 | events_got = seq - ctx->cq_last_tm_flush; | |
1571 | if (events_got < events_needed) | |
360428f8 | 1572 | break; |
bfe68a22 | 1573 | |
135fcde8 | 1574 | list_del_init(&req->timeout.list); |
1ee4160c | 1575 | io_kill_timeout(req, 0); |
f18ee4cf | 1576 | } |
f010505b | 1577 | ctx->cq_last_tm_flush = seq; |
79ebeaee | 1578 | spin_unlock_irq(&ctx->timeout_lock); |
360428f8 | 1579 | } |
5262f567 | 1580 | |
c072481d | 1581 | static __cold void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
360428f8 | 1582 | { |
2335f6f5 PB |
1583 | if (ctx->off_timeout_used) |
1584 | io_flush_timeouts(ctx); | |
1585 | if (ctx->drain_active) | |
1586 | io_queue_deferred(ctx); | |
1587 | } | |
1588 | ||
1589 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) | |
1590 | { | |
1591 | if (unlikely(ctx->off_timeout_used || ctx->drain_active)) | |
1592 | __io_commit_cqring_flush(ctx); | |
ec30e04b PB |
1593 | /* order cqe stores with ring update */ |
1594 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 JA |
1595 | } |
1596 | ||
90554200 JA |
1597 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1598 | { | |
1599 | struct io_rings *r = ctx->rings; | |
1600 | ||
a566c556 | 1601 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries; |
90554200 JA |
1602 | } |
1603 | ||
888aae2e PB |
1604 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1605 | { | |
1606 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1607 | } | |
1608 | ||
d068b506 | 1609 | static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) |
2b188cc1 | 1610 | { |
75b28aff | 1611 | struct io_rings *rings = ctx->rings; |
ea5ab3b5 | 1612 | unsigned tail, mask = ctx->cq_entries - 1; |
2b188cc1 | 1613 | |
115e12e5 SB |
1614 | /* |
1615 | * writes to the cq entry need to come after reading head; the | |
1616 | * control dependency is enough as we're using WRITE_ONCE to | |
1617 | * fill the cq entry | |
1618 | */ | |
a566c556 | 1619 | if (__io_cqring_events(ctx) == ctx->cq_entries) |
2b188cc1 JA |
1620 | return NULL; |
1621 | ||
888aae2e | 1622 | tail = ctx->cached_cq_tail++; |
ea5ab3b5 | 1623 | return &rings->cqes[tail & mask]; |
2b188cc1 JA |
1624 | } |
1625 | ||
f2842ab5 JA |
1626 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1627 | { | |
44c769de | 1628 | if (likely(!ctx->cq_ev_fd)) |
f0b493e6 | 1629 | return false; |
7e55a19c SG |
1630 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1631 | return false; | |
44c769de | 1632 | return !ctx->eventfd_async || io_wq_current_is_worker(); |
f2842ab5 JA |
1633 | } |
1634 | ||
2c5d763c JA |
1635 | /* |
1636 | * This should only get called when at least one event has been posted. | |
1637 | * Some applications rely on the eventfd notification count only changing | |
1638 | * IFF a new CQE has been added to the CQ ring. There's no depedency on | |
1639 | * 1:1 relationship between how many times this function is called (and | |
1640 | * hence the eventfd count) and number of CQEs posted to the CQ ring. | |
1641 | */ | |
b41e9852 | 1642 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1643 | { |
5fd46178 JA |
1644 | /* |
1645 | * wake_up_all() may seem excessive, but io_wake_function() and | |
1646 | * io_should_wake() handle the termination of the loop and only | |
1647 | * wake as many waiters as we need to. | |
1648 | */ | |
1649 | if (wq_has_sleeper(&ctx->cq_wait)) | |
1650 | wake_up_all(&ctx->cq_wait); | |
b41e9852 | 1651 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 JA |
1652 | eventfd_signal(ctx->cq_ev_fd, 1); |
1653 | } | |
1654 | ||
80c18e4a PB |
1655 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1656 | { | |
c57a91fb PB |
1657 | /* see waitqueue_active() comment */ |
1658 | smp_mb(); | |
1659 | ||
80c18e4a | 1660 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
c57a91fb | 1661 | if (waitqueue_active(&ctx->cq_wait)) |
5fd46178 | 1662 | wake_up_all(&ctx->cq_wait); |
80c18e4a PB |
1663 | } |
1664 | if (io_should_trigger_evfd(ctx)) | |
1665 | eventfd_signal(ctx->cq_ev_fd, 1); | |
1666 | } | |
1667 | ||
c4a2ed72 | 1668 | /* Returns true if there are no backlogged entries after the flush */ |
6c2450ae | 1669 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
1d7bb1d5 | 1670 | { |
b18032bb | 1671 | bool all_flushed, posted; |
1d7bb1d5 | 1672 | |
a566c556 | 1673 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
e23de15f | 1674 | return false; |
1d7bb1d5 | 1675 | |
b18032bb | 1676 | posted = false; |
79ebeaee | 1677 | spin_lock(&ctx->completion_lock); |
6c2450ae | 1678 | while (!list_empty(&ctx->cq_overflow_list)) { |
d068b506 | 1679 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
6c2450ae | 1680 | struct io_overflow_cqe *ocqe; |
e6c8aa9a | 1681 | |
1d7bb1d5 JA |
1682 | if (!cqe && !force) |
1683 | break; | |
6c2450ae PB |
1684 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
1685 | struct io_overflow_cqe, list); | |
1686 | if (cqe) | |
1687 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); | |
1688 | else | |
8f6ed49a PB |
1689 | io_account_cq_overflow(ctx); |
1690 | ||
b18032bb | 1691 | posted = true; |
6c2450ae PB |
1692 | list_del(&ocqe->list); |
1693 | kfree(ocqe); | |
1d7bb1d5 JA |
1694 | } |
1695 | ||
09e88404 PB |
1696 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1697 | if (all_flushed) { | |
5ed7a37d | 1698 | clear_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1699 | WRITE_ONCE(ctx->rings->sq_flags, |
1700 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); | |
09e88404 | 1701 | } |
46930143 | 1702 | |
b18032bb JA |
1703 | if (posted) |
1704 | io_commit_cqring(ctx); | |
79ebeaee | 1705 | spin_unlock(&ctx->completion_lock); |
b18032bb JA |
1706 | if (posted) |
1707 | io_cqring_ev_posted(ctx); | |
09e88404 | 1708 | return all_flushed; |
1d7bb1d5 JA |
1709 | } |
1710 | ||
90f67366 | 1711 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
6c503150 | 1712 | { |
ca0a2651 JA |
1713 | bool ret = true; |
1714 | ||
5ed7a37d | 1715 | if (test_bit(0, &ctx->check_cq_overflow)) { |
6c503150 PB |
1716 | /* iopoll syncs against uring_lock, not completion_lock */ |
1717 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1718 | mutex_lock(&ctx->uring_lock); | |
90f67366 | 1719 | ret = __io_cqring_overflow_flush(ctx, false); |
6c503150 PB |
1720 | if (ctx->flags & IORING_SETUP_IOPOLL) |
1721 | mutex_unlock(&ctx->uring_lock); | |
1722 | } | |
ca0a2651 JA |
1723 | |
1724 | return ret; | |
6c503150 PB |
1725 | } |
1726 | ||
6a290a14 PB |
1727 | /* must to be called somewhat shortly after putting a request */ |
1728 | static inline void io_put_task(struct task_struct *task, int nr) | |
1729 | { | |
1730 | struct io_uring_task *tctx = task->io_uring; | |
1731 | ||
e98e49b2 PB |
1732 | if (likely(task == current)) { |
1733 | tctx->cached_refs += nr; | |
1734 | } else { | |
1735 | percpu_counter_sub(&tctx->inflight, nr); | |
1736 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1737 | wake_up(&tctx->wait); | |
1738 | put_task_struct_many(task, nr); | |
1739 | } | |
6a290a14 PB |
1740 | } |
1741 | ||
9a10867a PB |
1742 | static void io_task_refs_refill(struct io_uring_task *tctx) |
1743 | { | |
1744 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; | |
1745 | ||
1746 | percpu_counter_add(&tctx->inflight, refill); | |
1747 | refcount_add(refill, ¤t->usage); | |
1748 | tctx->cached_refs += refill; | |
1749 | } | |
1750 | ||
1751 | static inline void io_get_task_refs(int nr) | |
1752 | { | |
1753 | struct io_uring_task *tctx = current->io_uring; | |
1754 | ||
1755 | tctx->cached_refs -= nr; | |
1756 | if (unlikely(tctx->cached_refs < 0)) | |
1757 | io_task_refs_refill(tctx); | |
1758 | } | |
1759 | ||
d4d19c19 | 1760 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
54daa9b2 | 1761 | s32 res, u32 cflags) |
2b188cc1 | 1762 | { |
cce4b8b0 | 1763 | struct io_overflow_cqe *ocqe; |
2b188cc1 | 1764 | |
cce4b8b0 PB |
1765 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
1766 | if (!ocqe) { | |
1767 | /* | |
1768 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1769 | * or cannot allocate an overflow entry, then we need to drop it | |
1770 | * on the floor. | |
1771 | */ | |
8f6ed49a | 1772 | io_account_cq_overflow(ctx); |
cce4b8b0 | 1773 | return false; |
2b188cc1 | 1774 | } |
cce4b8b0 | 1775 | if (list_empty(&ctx->cq_overflow_list)) { |
5ed7a37d | 1776 | set_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1777 | WRITE_ONCE(ctx->rings->sq_flags, |
1778 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); | |
1779 | ||
cce4b8b0 | 1780 | } |
d4d19c19 | 1781 | ocqe->cqe.user_data = user_data; |
cce4b8b0 PB |
1782 | ocqe->cqe.res = res; |
1783 | ocqe->cqe.flags = cflags; | |
1784 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); | |
1785 | return true; | |
2b188cc1 JA |
1786 | } |
1787 | ||
d4d19c19 | 1788 | static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
54daa9b2 | 1789 | s32 res, u32 cflags) |
2b188cc1 JA |
1790 | { |
1791 | struct io_uring_cqe *cqe; | |
1792 | ||
d4d19c19 | 1793 | trace_io_uring_complete(ctx, user_data, res, cflags); |
51c3ff62 | 1794 | |
2b188cc1 JA |
1795 | /* |
1796 | * If we can't get a cq entry, userspace overflowed the | |
1797 | * submission (by quite a lot). Increment the overflow count in | |
1798 | * the ring. | |
1799 | */ | |
d068b506 | 1800 | cqe = io_get_cqe(ctx); |
1d7bb1d5 | 1801 | if (likely(cqe)) { |
d4d19c19 | 1802 | WRITE_ONCE(cqe->user_data, user_data); |
2b188cc1 | 1803 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1804 | WRITE_ONCE(cqe->flags, cflags); |
8d13326e | 1805 | return true; |
2b188cc1 | 1806 | } |
d4d19c19 | 1807 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
2b188cc1 JA |
1808 | } |
1809 | ||
8d13326e | 1810 | /* not as hot to bloat with inlining */ |
d4d19c19 | 1811 | static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
54daa9b2 | 1812 | s32 res, u32 cflags) |
bcda7baa | 1813 | { |
d4d19c19 | 1814 | return __io_cqring_fill_event(ctx, user_data, res, cflags); |
bcda7baa JA |
1815 | } |
1816 | ||
54daa9b2 PB |
1817 | static void io_req_complete_post(struct io_kiocb *req, s32 res, |
1818 | u32 cflags) | |
2b188cc1 | 1819 | { |
78e19bbe | 1820 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 | 1821 | |
79ebeaee | 1822 | spin_lock(&ctx->completion_lock); |
d4d19c19 | 1823 | __io_cqring_fill_event(ctx, req->user_data, res, cflags); |
c7dae4ba JA |
1824 | /* |
1825 | * If we're the last reference to this request, add to our locked | |
1826 | * free_list cache. | |
1827 | */ | |
de9b4cca | 1828 | if (req_ref_put_and_test(req)) { |
7a612350 | 1829 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
0756a869 | 1830 | if (req->flags & IO_DISARM_MASK) |
7a612350 PB |
1831 | io_disarm_next(req); |
1832 | if (req->link) { | |
1833 | io_req_task_queue(req->link); | |
1834 | req->link = NULL; | |
1835 | } | |
1836 | } | |
ab409402 | 1837 | io_req_put_rsrc(req, ctx); |
c7dae4ba JA |
1838 | io_dismantle_req(req); |
1839 | io_put_task(req->task, 1); | |
c2b6c6bc | 1840 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
d0acdee2 | 1841 | ctx->locked_free_nr++; |
180f829f | 1842 | } |
7a612350 | 1843 | io_commit_cqring(ctx); |
79ebeaee | 1844 | spin_unlock(&ctx->completion_lock); |
a3f34907 | 1845 | io_cqring_ev_posted(ctx); |
4e3d9ff9 JA |
1846 | } |
1847 | ||
54daa9b2 PB |
1848 | static inline void io_req_complete_state(struct io_kiocb *req, s32 res, |
1849 | u32 cflags) | |
229a7b63 | 1850 | { |
a38d68db | 1851 | req->result = res; |
d17e56eb | 1852 | req->cflags = cflags; |
e342c807 | 1853 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
1854 | } |
1855 | ||
889fca73 | 1856 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
54daa9b2 | 1857 | s32 res, u32 cflags) |
bcda7baa | 1858 | { |
889fca73 PB |
1859 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
1860 | io_req_complete_state(req, res, cflags); | |
a38d68db | 1861 | else |
c7dae4ba | 1862 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
1863 | } |
1864 | ||
54daa9b2 | 1865 | static inline void io_req_complete(struct io_kiocb *req, s32 res) |
0ddf92e8 | 1866 | { |
889fca73 | 1867 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
1868 | } |
1869 | ||
54daa9b2 | 1870 | static void io_req_complete_failed(struct io_kiocb *req, s32 res) |
f41db273 | 1871 | { |
93d2bcd2 | 1872 | req_set_fail(req); |
f41db273 PB |
1873 | io_req_complete_post(req, res, 0); |
1874 | } | |
1875 | ||
c6d3d9cb PB |
1876 | static void io_req_complete_fail_submit(struct io_kiocb *req) |
1877 | { | |
1878 | /* | |
1879 | * We don't submit, fail them all, for that replace hardlinks with | |
1880 | * normal links. Extra REQ_F_LINK is tolerated. | |
1881 | */ | |
1882 | req->flags &= ~REQ_F_HARDLINK; | |
1883 | req->flags |= REQ_F_LINK; | |
1884 | io_req_complete_failed(req, req->result); | |
1885 | } | |
1886 | ||
864ea921 PB |
1887 | /* |
1888 | * Don't initialise the fields below on every allocation, but do that in | |
1889 | * advance and keep them valid across allocations. | |
1890 | */ | |
1891 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
1892 | { | |
1893 | req->ctx = ctx; | |
1894 | req->link = NULL; | |
1895 | req->async_data = NULL; | |
1896 | /* not necessary, but safer to zero */ | |
1897 | req->result = 0; | |
1898 | } | |
1899 | ||
dac7a098 | 1900 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
cd0ca2e0 | 1901 | struct io_submit_state *state) |
dac7a098 | 1902 | { |
79ebeaee | 1903 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 1904 | wq_list_splice(&ctx->locked_free_list, &state->free_list); |
d0acdee2 | 1905 | ctx->locked_free_nr = 0; |
79ebeaee | 1906 | spin_unlock(&ctx->completion_lock); |
dac7a098 PB |
1907 | } |
1908 | ||
dd78f492 | 1909 | /* Returns true IFF there are requests in the cache */ |
c7dae4ba | 1910 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 1911 | { |
c7dae4ba | 1912 | struct io_submit_state *state = &ctx->submit_state; |
0ddf92e8 | 1913 | |
c7dae4ba JA |
1914 | /* |
1915 | * If we have more than a batch's worth of requests in our IRQ side | |
1916 | * locked cache, grab the lock and move them over to our submission | |
1917 | * side cache. | |
1918 | */ | |
d0acdee2 | 1919 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
cd0ca2e0 | 1920 | io_flush_cached_locked_reqs(ctx, state); |
c2b6c6bc | 1921 | return !!state->free_list.next; |
0ddf92e8 JA |
1922 | } |
1923 | ||
5d5901a3 PB |
1924 | /* |
1925 | * A request might get retired back into the request caches even before opcode | |
1926 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. | |
1927 | * Because of that, io_alloc_req() should be called only under ->uring_lock | |
1928 | * and with extra caution to not get a request that is still worked on. | |
1929 | */ | |
c072481d | 1930 | static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx) |
5d5901a3 | 1931 | __must_hold(&ctx->uring_lock) |
2b188cc1 | 1932 | { |
e5d1bc0a | 1933 | struct io_submit_state *state = &ctx->submit_state; |
864ea921 | 1934 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
3ab665b7 PB |
1935 | void *reqs[IO_REQ_ALLOC_BATCH]; |
1936 | struct io_kiocb *req; | |
864ea921 | 1937 | int ret, i; |
e5d1bc0a | 1938 | |
c2b6c6bc | 1939 | if (likely(state->free_list.next || io_flush_cached_reqs(ctx))) |
a33ae9ce | 1940 | return true; |
e5d1bc0a | 1941 | |
3ab665b7 | 1942 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs); |
fd6fab2c | 1943 | |
864ea921 PB |
1944 | /* |
1945 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1946 | * retry single alloc to be on the safe side. | |
1947 | */ | |
1948 | if (unlikely(ret <= 0)) { | |
3ab665b7 PB |
1949 | reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
1950 | if (!reqs[0]) | |
a33ae9ce | 1951 | return false; |
864ea921 | 1952 | ret = 1; |
2b188cc1 | 1953 | } |
864ea921 | 1954 | |
37f0e767 | 1955 | percpu_ref_get_many(&ctx->refs, ret); |
3ab665b7 PB |
1956 | for (i = 0; i < ret; i++) { |
1957 | req = reqs[i]; | |
1958 | ||
1959 | io_preinit_req(req, ctx); | |
c2b6c6bc | 1960 | wq_stack_add_head(&req->comp_list, &state->free_list); |
3ab665b7 | 1961 | } |
a33ae9ce PB |
1962 | return true; |
1963 | } | |
1964 | ||
1965 | static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx) | |
1966 | { | |
1967 | if (unlikely(!ctx->submit_state.free_list.next)) | |
1968 | return __io_alloc_req_refill(ctx); | |
1969 | return true; | |
1970 | } | |
1971 | ||
1972 | static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) | |
1973 | { | |
1974 | struct io_wq_work_node *node; | |
1975 | ||
1976 | node = wq_stack_extract(&ctx->submit_state.free_list); | |
c2b6c6bc | 1977 | return container_of(node, struct io_kiocb, comp_list); |
2b188cc1 JA |
1978 | } |
1979 | ||
e1d767f0 | 1980 | static inline void io_put_file(struct file *file) |
8da11c19 | 1981 | { |
e1d767f0 | 1982 | if (file) |
8da11c19 PB |
1983 | fput(file); |
1984 | } | |
1985 | ||
6b639522 | 1986 | static inline void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 1987 | { |
094bae49 | 1988 | unsigned int flags = req->flags; |
929a3af9 | 1989 | |
867f8fa5 | 1990 | if (unlikely(flags & IO_REQ_CLEAN_FLAGS)) |
3a0a6902 | 1991 | io_clean_op(req); |
e1d767f0 PB |
1992 | if (!(flags & REQ_F_FIXED_FILE)) |
1993 | io_put_file(req->file); | |
e65ef56d JA |
1994 | } |
1995 | ||
c072481d | 1996 | static __cold void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 1997 | { |
51a4cc11 | 1998 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 1999 | |
ab409402 | 2000 | io_req_put_rsrc(req, ctx); |
216578e5 | 2001 | io_dismantle_req(req); |
7c660731 | 2002 | io_put_task(req->task, 1); |
c6ca97b3 | 2003 | |
79ebeaee | 2004 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 2005 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
c34b025f | 2006 | ctx->locked_free_nr++; |
79ebeaee | 2007 | spin_unlock(&ctx->completion_lock); |
e65ef56d JA |
2008 | } |
2009 | ||
f2f87370 PB |
2010 | static inline void io_remove_next_linked(struct io_kiocb *req) |
2011 | { | |
2012 | struct io_kiocb *nxt = req->link; | |
2013 | ||
2014 | req->link = nxt->link; | |
2015 | nxt->link = NULL; | |
2016 | } | |
2017 | ||
33cc89a9 PB |
2018 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
2019 | __must_hold(&req->ctx->completion_lock) | |
89b263f6 | 2020 | __must_hold(&req->ctx->timeout_lock) |
2665abfd | 2021 | { |
33cc89a9 | 2022 | struct io_kiocb *link = req->link; |
f2f87370 | 2023 | |
b97e736a | 2024 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
c9abd7ad | 2025 | struct io_timeout_data *io = link->async_data; |
7c86ffee | 2026 | |
f2f87370 | 2027 | io_remove_next_linked(req); |
90cd7e42 | 2028 | link->timeout.head = NULL; |
fd9c7bc5 | 2029 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
ef9dd637 | 2030 | list_del(&link->timeout.list); |
d4d19c19 PB |
2031 | io_cqring_fill_event(link->ctx, link->user_data, |
2032 | -ECANCELED, 0); | |
91c2f697 | 2033 | io_put_req_deferred(link); |
d4729fbd | 2034 | return true; |
c9abd7ad PB |
2035 | } |
2036 | } | |
d4729fbd | 2037 | return false; |
7c86ffee PB |
2038 | } |
2039 | ||
d148ca4b | 2040 | static void io_fail_links(struct io_kiocb *req) |
33cc89a9 | 2041 | __must_hold(&req->ctx->completion_lock) |
9e645e11 | 2042 | { |
33cc89a9 | 2043 | struct io_kiocb *nxt, *link = req->link; |
9e645e11 | 2044 | |
f2f87370 | 2045 | req->link = NULL; |
f2f87370 | 2046 | while (link) { |
a8295b98 HX |
2047 | long res = -ECANCELED; |
2048 | ||
2049 | if (link->flags & REQ_F_FAIL) | |
2050 | res = link->result; | |
2051 | ||
f2f87370 PB |
2052 | nxt = link->link; |
2053 | link->link = NULL; | |
2665abfd | 2054 | |
f2f87370 | 2055 | trace_io_uring_fail_link(req, link); |
a8295b98 | 2056 | io_cqring_fill_event(link->ctx, link->user_data, res, 0); |
91c2f697 | 2057 | io_put_req_deferred(link); |
f2f87370 | 2058 | link = nxt; |
9e645e11 | 2059 | } |
33cc89a9 | 2060 | } |
9e645e11 | 2061 | |
33cc89a9 PB |
2062 | static bool io_disarm_next(struct io_kiocb *req) |
2063 | __must_hold(&req->ctx->completion_lock) | |
2064 | { | |
2065 | bool posted = false; | |
2066 | ||
0756a869 PB |
2067 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
2068 | struct io_kiocb *link = req->link; | |
2069 | ||
906c6caa | 2070 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
0756a869 PB |
2071 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
2072 | io_remove_next_linked(req); | |
2073 | io_cqring_fill_event(link->ctx, link->user_data, | |
2074 | -ECANCELED, 0); | |
2075 | io_put_req_deferred(link); | |
2076 | posted = true; | |
2077 | } | |
2078 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { | |
89b263f6 JA |
2079 | struct io_ring_ctx *ctx = req->ctx; |
2080 | ||
2081 | spin_lock_irq(&ctx->timeout_lock); | |
33cc89a9 | 2082 | posted = io_kill_linked_timeout(req); |
89b263f6 JA |
2083 | spin_unlock_irq(&ctx->timeout_lock); |
2084 | } | |
93d2bcd2 | 2085 | if (unlikely((req->flags & REQ_F_FAIL) && |
e4335ed3 | 2086 | !(req->flags & REQ_F_HARDLINK))) { |
33cc89a9 PB |
2087 | posted |= (req->link != NULL); |
2088 | io_fail_links(req); | |
2089 | } | |
2090 | return posted; | |
9e645e11 JA |
2091 | } |
2092 | ||
d81499bf PB |
2093 | static void __io_req_find_next_prep(struct io_kiocb *req) |
2094 | { | |
2095 | struct io_ring_ctx *ctx = req->ctx; | |
2096 | bool posted; | |
2097 | ||
2098 | spin_lock(&ctx->completion_lock); | |
2099 | posted = io_disarm_next(req); | |
2100 | if (posted) | |
2101 | io_commit_cqring(req->ctx); | |
2102 | spin_unlock(&ctx->completion_lock); | |
2103 | if (posted) | |
2104 | io_cqring_ev_posted(ctx); | |
2105 | } | |
2106 | ||
2107 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) | |
c69f8dbe | 2108 | { |
33cc89a9 | 2109 | struct io_kiocb *nxt; |
944e58bf | 2110 | |
d81499bf PB |
2111 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
2112 | return NULL; | |
9e645e11 JA |
2113 | /* |
2114 | * If LINK is set, we have dependent requests in this chain. If we | |
2115 | * didn't fail this request, queue the first one up, moving any other | |
2116 | * dependencies to the next request. In case of failure, fail the rest | |
2117 | * of the chain. | |
2118 | */ | |
d81499bf PB |
2119 | if (unlikely(req->flags & IO_DISARM_MASK)) |
2120 | __io_req_find_next_prep(req); | |
33cc89a9 PB |
2121 | nxt = req->link; |
2122 | req->link = NULL; | |
2123 | return nxt; | |
4d7dd462 | 2124 | } |
9e645e11 | 2125 | |
f237c30a | 2126 | static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked) |
2c32395d PB |
2127 | { |
2128 | if (!ctx) | |
2129 | return; | |
f237c30a | 2130 | if (*locked) { |
c450178d | 2131 | io_submit_flush_completions(ctx); |
2c32395d | 2132 | mutex_unlock(&ctx->uring_lock); |
f237c30a | 2133 | *locked = false; |
2c32395d PB |
2134 | } |
2135 | percpu_ref_put(&ctx->refs); | |
2136 | } | |
2137 | ||
7cbf1722 | 2138 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 2139 | { |
f237c30a | 2140 | bool locked = false; |
ebd0df2e | 2141 | struct io_ring_ctx *ctx = NULL; |
3f18407d PB |
2142 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
2143 | task_work); | |
c40f6379 | 2144 | |
16f72070 | 2145 | while (1) { |
3f18407d PB |
2146 | struct io_wq_work_node *node; |
2147 | ||
c450178d | 2148 | if (!tctx->task_list.first && locked) |
8d4ad41e PB |
2149 | io_submit_flush_completions(ctx); |
2150 | ||
3f18407d | 2151 | spin_lock_irq(&tctx->task_lock); |
c6538be9 | 2152 | node = tctx->task_list.first; |
3f18407d | 2153 | INIT_WQ_LIST(&tctx->task_list); |
6294f368 PB |
2154 | if (!node) |
2155 | tctx->task_running = false; | |
3f18407d | 2156 | spin_unlock_irq(&tctx->task_lock); |
6294f368 PB |
2157 | if (!node) |
2158 | break; | |
3f18407d | 2159 | |
6294f368 | 2160 | do { |
3f18407d PB |
2161 | struct io_wq_work_node *next = node->next; |
2162 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2163 | io_task_work.node); | |
2164 | ||
2165 | if (req->ctx != ctx) { | |
f237c30a | 2166 | ctx_flush_and_put(ctx, &locked); |
3f18407d | 2167 | ctx = req->ctx; |
126180b9 PB |
2168 | /* if not contended, grab and improve batching */ |
2169 | locked = mutex_trylock(&ctx->uring_lock); | |
3f18407d PB |
2170 | percpu_ref_get(&ctx->refs); |
2171 | } | |
f237c30a | 2172 | req->io_task_work.func(req, &locked); |
3f18407d | 2173 | node = next; |
6294f368 PB |
2174 | } while (node); |
2175 | ||
7cbf1722 | 2176 | cond_resched(); |
3f18407d | 2177 | } |
ebd0df2e | 2178 | |
f237c30a | 2179 | ctx_flush_and_put(ctx, &locked); |
7cbf1722 JA |
2180 | } |
2181 | ||
e09ee510 | 2182 | static void io_req_task_work_add(struct io_kiocb *req) |
7cbf1722 | 2183 | { |
c15b79de | 2184 | struct task_struct *tsk = req->task; |
7cbf1722 | 2185 | struct io_uring_task *tctx = tsk->io_uring; |
c15b79de | 2186 | enum task_work_notify_mode notify; |
e09ee510 | 2187 | struct io_wq_work_node *node; |
0b81e80c | 2188 | unsigned long flags; |
6294f368 | 2189 | bool running; |
7cbf1722 JA |
2190 | |
2191 | WARN_ON_ONCE(!tctx); | |
2192 | ||
0b81e80c | 2193 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 | 2194 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
6294f368 PB |
2195 | running = tctx->task_running; |
2196 | if (!running) | |
2197 | tctx->task_running = true; | |
0b81e80c | 2198 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2199 | |
2200 | /* task_work already pending, we're done */ | |
6294f368 | 2201 | if (running) |
e09ee510 | 2202 | return; |
7cbf1722 | 2203 | |
c15b79de PB |
2204 | /* |
2205 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For | |
2206 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2207 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2208 | * will do the job. | |
2209 | */ | |
2210 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; | |
d97ec623 PB |
2211 | if (likely(!task_work_add(tsk, &tctx->task_work, notify))) { |
2212 | if (notify == TWA_NONE) | |
2213 | wake_up_process(tsk); | |
e09ee510 | 2214 | return; |
c15b79de | 2215 | } |
2215bed9 | 2216 | |
0b81e80c | 2217 | spin_lock_irqsave(&tctx->task_lock, flags); |
6294f368 | 2218 | tctx->task_running = false; |
e09ee510 PB |
2219 | node = tctx->task_list.first; |
2220 | INIT_WQ_LIST(&tctx->task_list); | |
0b81e80c | 2221 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 | 2222 | |
e09ee510 PB |
2223 | while (node) { |
2224 | req = container_of(node, struct io_kiocb, io_task_work.node); | |
2225 | node = node->next; | |
2226 | if (llist_add(&req->io_task_work.fallback_node, | |
2227 | &req->ctx->fallback_llist)) | |
2228 | schedule_delayed_work(&req->ctx->fallback_work, 1); | |
2229 | } | |
eab30c4d PB |
2230 | } |
2231 | ||
f237c30a | 2232 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
c40f6379 | 2233 | { |
87ceb6a6 | 2234 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2235 | |
b18a1a45 | 2236 | /* not needed for normal modes, but SQPOLL depends on it */ |
f237c30a | 2237 | io_tw_lock(ctx, locked); |
2593553a | 2238 | io_req_complete_failed(req, req->result); |
c40f6379 JA |
2239 | } |
2240 | ||
f237c30a | 2241 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
c40f6379 JA |
2242 | { |
2243 | struct io_ring_ctx *ctx = req->ctx; | |
2244 | ||
f237c30a | 2245 | io_tw_lock(ctx, locked); |
316319e8 | 2246 | /* req->task == current here, checking PF_EXITING is safe */ |
af066f31 | 2247 | if (likely(!(req->task->flags & PF_EXITING))) |
c5eef2b9 | 2248 | __io_queue_sqe(req); |
81b6d05c | 2249 | else |
2593553a | 2250 | io_req_complete_failed(req, -EFAULT); |
c40f6379 JA |
2251 | } |
2252 | ||
2c4b8eb6 | 2253 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
c40f6379 | 2254 | { |
2c4b8eb6 | 2255 | req->result = ret; |
5b0a6acc | 2256 | req->io_task_work.func = io_req_task_cancel; |
e09ee510 | 2257 | io_req_task_work_add(req); |
c40f6379 JA |
2258 | } |
2259 | ||
2c4b8eb6 | 2260 | static void io_req_task_queue(struct io_kiocb *req) |
a3df7698 | 2261 | { |
5b0a6acc | 2262 | req->io_task_work.func = io_req_task_submit; |
e09ee510 | 2263 | io_req_task_work_add(req); |
a3df7698 PB |
2264 | } |
2265 | ||
773af691 JA |
2266 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
2267 | { | |
2268 | req->io_task_work.func = io_queue_async_work; | |
2269 | io_req_task_work_add(req); | |
2270 | } | |
2271 | ||
f2f87370 | 2272 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2273 | { |
9b5f7bd9 | 2274 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2275 | |
2276 | if (nxt) | |
906a8c3f | 2277 | io_req_task_queue(nxt); |
c69f8dbe JL |
2278 | } |
2279 | ||
c3524383 | 2280 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2281 | { |
c3524383 PB |
2282 | io_queue_next(req); |
2283 | __io_free_req(req); | |
2284 | } | |
8766dd51 | 2285 | |
f237c30a PB |
2286 | static void io_free_req_work(struct io_kiocb *req, bool *locked) |
2287 | { | |
2288 | io_free_req(req); | |
2289 | } | |
2290 | ||
3aa83bfb | 2291 | static void io_free_batch_list(struct io_ring_ctx *ctx, |
1cce17ac | 2292 | struct io_wq_work_node *node) |
3aa83bfb | 2293 | __must_hold(&ctx->uring_lock) |
5af1d13e | 2294 | { |
d4b7a5ef | 2295 | struct task_struct *task = NULL; |
37f0e767 | 2296 | int task_refs = 0; |
5af1d13e | 2297 | |
3aa83bfb PB |
2298 | do { |
2299 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2300 | comp_list); | |
2d6500d4 | 2301 | |
def77acf | 2302 | if (unlikely(req->flags & REQ_F_REFCOUNT)) { |
c1e53a69 | 2303 | node = req->comp_list.next; |
def77acf PB |
2304 | if (!req_ref_put_and_test(req)) |
2305 | continue; | |
c1e53a69 | 2306 | } |
2d6500d4 | 2307 | |
ab409402 | 2308 | io_req_put_rsrc_locked(req, ctx); |
d4b7a5ef PB |
2309 | io_queue_next(req); |
2310 | io_dismantle_req(req); | |
5af1d13e | 2311 | |
d4b7a5ef PB |
2312 | if (req->task != task) { |
2313 | if (task) | |
2314 | io_put_task(task, task_refs); | |
2315 | task = req->task; | |
2316 | task_refs = 0; | |
2317 | } | |
2318 | task_refs++; | |
c1e53a69 | 2319 | node = req->comp_list.next; |
d4b7a5ef | 2320 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
3aa83bfb | 2321 | } while (node); |
d4b7a5ef | 2322 | |
d4b7a5ef PB |
2323 | if (task) |
2324 | io_put_task(task, task_refs); | |
7a743e22 PB |
2325 | } |
2326 | ||
c450178d | 2327 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx) |
a141dd89 | 2328 | __must_hold(&ctx->uring_lock) |
905c172f | 2329 | { |
6f33b0bc | 2330 | struct io_wq_work_node *node, *prev; |
cd0ca2e0 | 2331 | struct io_submit_state *state = &ctx->submit_state; |
905c172f | 2332 | |
79ebeaee | 2333 | spin_lock(&ctx->completion_lock); |
6f33b0bc PB |
2334 | wq_list_for_each(node, prev, &state->compl_reqs) { |
2335 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2336 | comp_list); | |
5182ed2e | 2337 | |
d4d19c19 | 2338 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
d17e56eb | 2339 | req->cflags); |
905c172f PB |
2340 | } |
2341 | io_commit_cqring(ctx); | |
79ebeaee | 2342 | spin_unlock(&ctx->completion_lock); |
905c172f | 2343 | io_cqring_ev_posted(ctx); |
5182ed2e | 2344 | |
1cce17ac | 2345 | io_free_batch_list(ctx, state->compl_reqs.first); |
6f33b0bc | 2346 | INIT_WQ_LIST(&state->compl_reqs); |
7a743e22 PB |
2347 | } |
2348 | ||
ba816ad6 JA |
2349 | /* |
2350 | * Drop reference to request, return next in chain (if there is one) if this | |
2351 | * was the last reference to this request. | |
2352 | */ | |
0d85035a | 2353 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2354 | { |
9b5f7bd9 PB |
2355 | struct io_kiocb *nxt = NULL; |
2356 | ||
de9b4cca | 2357 | if (req_ref_put_and_test(req)) { |
9b5f7bd9 | 2358 | nxt = io_req_find_next(req); |
4d7dd462 | 2359 | __io_free_req(req); |
2a44f467 | 2360 | } |
9b5f7bd9 | 2361 | return nxt; |
2b188cc1 JA |
2362 | } |
2363 | ||
0d85035a | 2364 | static inline void io_put_req(struct io_kiocb *req) |
e65ef56d | 2365 | { |
de9b4cca | 2366 | if (req_ref_put_and_test(req)) |
e65ef56d | 2367 | io_free_req(req); |
2b188cc1 JA |
2368 | } |
2369 | ||
91c2f697 | 2370 | static inline void io_put_req_deferred(struct io_kiocb *req) |
216578e5 | 2371 | { |
91c2f697 | 2372 | if (req_ref_put_and_test(req)) { |
f237c30a | 2373 | req->io_task_work.func = io_free_req_work; |
543af3a1 PB |
2374 | io_req_task_work_add(req); |
2375 | } | |
216578e5 PB |
2376 | } |
2377 | ||
6c503150 | 2378 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2379 | { |
2380 | /* See comment at the top of this file */ | |
2381 | smp_rmb(); | |
e23de15f | 2382 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2383 | } |
2384 | ||
fb5ccc98 PB |
2385 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2386 | { | |
2387 | struct io_rings *rings = ctx->rings; | |
2388 | ||
2389 | /* make sure SQ entry isn't read before tail */ | |
2390 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2391 | } | |
2392 | ||
8ff069bf | 2393 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2394 | { |
8ff069bf | 2395 | unsigned int cflags; |
e94f141b | 2396 | |
bcda7baa JA |
2397 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2398 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2399 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2400 | kfree(kbuf); |
2401 | return cflags; | |
e94f141b JA |
2402 | } |
2403 | ||
8ff069bf | 2404 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2405 | { |
ae421d93 PB |
2406 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
2407 | return 0; | |
30d51dd4 | 2408 | return io_put_kbuf(req, req->kbuf); |
8ff069bf PB |
2409 | } |
2410 | ||
4c6e277c JA |
2411 | static inline bool io_run_task_work(void) |
2412 | { | |
ef98eb04 | 2413 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) { |
4c6e277c | 2414 | __set_current_state(TASK_RUNNING); |
ef98eb04 | 2415 | tracehook_notify_signal(); |
4c6e277c JA |
2416 | return true; |
2417 | } | |
2418 | ||
2419 | return false; | |
bcda7baa JA |
2420 | } |
2421 | ||
5ba3c874 | 2422 | static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) |
def596e9 | 2423 | { |
5eef4e87 | 2424 | struct io_wq_work_node *pos, *start, *prev; |
d729cf9a | 2425 | unsigned int poll_flags = BLK_POLL_NOSLEEP; |
b688f11e | 2426 | DEFINE_IO_COMP_BATCH(iob); |
5ba3c874 | 2427 | int nr_events = 0; |
def596e9 JA |
2428 | |
2429 | /* | |
2430 | * Only spin for completions if we don't have multiple devices hanging | |
87a115fb | 2431 | * off our complete list. |
def596e9 | 2432 | */ |
87a115fb | 2433 | if (ctx->poll_multi_queue || force_nonspin) |
ef99b2d3 | 2434 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2435 | |
5eef4e87 PB |
2436 | wq_list_for_each(pos, start, &ctx->iopoll_list) { |
2437 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
9adbd45d | 2438 | struct kiocb *kiocb = &req->rw.kiocb; |
a2416e1e | 2439 | int ret; |
def596e9 JA |
2440 | |
2441 | /* | |
581f9810 BM |
2442 | * Move completed and retryable entries to our local lists. |
2443 | * If we find a request that requires polling, break out | |
2444 | * and complete those lists first, if we have entries there. | |
def596e9 | 2445 | */ |
e3f721e6 | 2446 | if (READ_ONCE(req->iopoll_completed)) |
def596e9 JA |
2447 | break; |
2448 | ||
b688f11e | 2449 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags); |
a2416e1e PB |
2450 | if (unlikely(ret < 0)) |
2451 | return ret; | |
2452 | else if (ret) | |
ef99b2d3 | 2453 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2454 | |
3aadc23e | 2455 | /* iopoll may have completed current req */ |
b688f11e JA |
2456 | if (!rq_list_empty(iob.req_list) || |
2457 | READ_ONCE(req->iopoll_completed)) | |
e3f721e6 | 2458 | break; |
def596e9 JA |
2459 | } |
2460 | ||
b688f11e JA |
2461 | if (!rq_list_empty(iob.req_list)) |
2462 | iob.complete(&iob); | |
5eef4e87 PB |
2463 | else if (!pos) |
2464 | return 0; | |
def596e9 | 2465 | |
5eef4e87 PB |
2466 | prev = start; |
2467 | wq_list_for_each_resume(pos, prev) { | |
2468 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
2469 | ||
b3fa03fd PB |
2470 | /* order with io_complete_rw_iopoll(), e.g. ->result updates */ |
2471 | if (!smp_load_acquire(&req->iopoll_completed)) | |
e3f721e6 | 2472 | break; |
b3fa03fd | 2473 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
f5ed3bcd | 2474 | io_put_rw_kbuf(req)); |
e3f721e6 PB |
2475 | nr_events++; |
2476 | } | |
def596e9 | 2477 | |
f5ed3bcd PB |
2478 | if (unlikely(!nr_events)) |
2479 | return 0; | |
2480 | ||
2481 | io_commit_cqring(ctx); | |
2482 | io_cqring_ev_posted_iopoll(ctx); | |
1cce17ac | 2483 | pos = start ? start->next : ctx->iopoll_list.first; |
5eef4e87 | 2484 | wq_list_cut(&ctx->iopoll_list, prev, start); |
1cce17ac | 2485 | io_free_batch_list(ctx, pos); |
5ba3c874 | 2486 | return nr_events; |
def596e9 JA |
2487 | } |
2488 | ||
def596e9 JA |
2489 | /* |
2490 | * We can't just wait for polled events to come to us, we have to actively | |
2491 | * find and complete them. | |
2492 | */ | |
c072481d | 2493 | static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2494 | { |
2495 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2496 | return; | |
2497 | ||
2498 | mutex_lock(&ctx->uring_lock); | |
5eef4e87 | 2499 | while (!wq_list_empty(&ctx->iopoll_list)) { |
b2edc0a7 | 2500 | /* let it sleep and repeat later if can't complete a request */ |
5ba3c874 | 2501 | if (io_do_iopoll(ctx, true) == 0) |
b2edc0a7 | 2502 | break; |
08f5439f JA |
2503 | /* |
2504 | * Ensure we allow local-to-the-cpu processing to take place, | |
2505 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2506 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2507 | */ |
3fcee5a6 PB |
2508 | if (need_resched()) { |
2509 | mutex_unlock(&ctx->uring_lock); | |
2510 | cond_resched(); | |
2511 | mutex_lock(&ctx->uring_lock); | |
2512 | } | |
def596e9 JA |
2513 | } |
2514 | mutex_unlock(&ctx->uring_lock); | |
2515 | } | |
2516 | ||
7668b92a | 2517 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2518 | { |
7668b92a | 2519 | unsigned int nr_events = 0; |
e9979b36 | 2520 | int ret = 0; |
500f9fba | 2521 | |
c7849be9 XW |
2522 | /* |
2523 | * We disallow the app entering submit/complete with polling, but we | |
2524 | * still need to lock the ring to prevent racing with polled issue | |
2525 | * that got punted to a workqueue. | |
2526 | */ | |
2527 | mutex_lock(&ctx->uring_lock); | |
f39c8a5b PB |
2528 | /* |
2529 | * Don't enter poll loop if we already have events pending. | |
2530 | * If we do, we can potentially be spinning for commands that | |
2531 | * already triggered a CQE (eg in error). | |
2532 | */ | |
5ed7a37d | 2533 | if (test_bit(0, &ctx->check_cq_overflow)) |
f39c8a5b PB |
2534 | __io_cqring_overflow_flush(ctx, false); |
2535 | if (io_cqring_events(ctx)) | |
2536 | goto out; | |
def596e9 | 2537 | do { |
500f9fba JA |
2538 | /* |
2539 | * If a submit got punted to a workqueue, we can have the | |
2540 | * application entering polling for a command before it gets | |
2541 | * issued. That app will hold the uring_lock for the duration | |
2542 | * of the poll right here, so we need to take a breather every | |
2543 | * now and then to ensure that the issue has a chance to add | |
2544 | * the poll to the issued list. Otherwise we can spin here | |
2545 | * forever, while the workqueue is stuck trying to acquire the | |
2546 | * very same mutex. | |
2547 | */ | |
5eef4e87 | 2548 | if (wq_list_empty(&ctx->iopoll_list)) { |
8f487ef2 PB |
2549 | u32 tail = ctx->cached_cq_tail; |
2550 | ||
500f9fba | 2551 | mutex_unlock(&ctx->uring_lock); |
4c6e277c | 2552 | io_run_task_work(); |
500f9fba | 2553 | mutex_lock(&ctx->uring_lock); |
def596e9 | 2554 | |
8f487ef2 PB |
2555 | /* some requests don't go through iopoll_list */ |
2556 | if (tail != ctx->cached_cq_tail || | |
5eef4e87 | 2557 | wq_list_empty(&ctx->iopoll_list)) |
e9979b36 | 2558 | break; |
500f9fba | 2559 | } |
5ba3c874 PB |
2560 | ret = io_do_iopoll(ctx, !min); |
2561 | if (ret < 0) | |
2562 | break; | |
2563 | nr_events += ret; | |
2564 | ret = 0; | |
2565 | } while (nr_events < min && !need_resched()); | |
f39c8a5b | 2566 | out: |
500f9fba | 2567 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2568 | return ret; |
2569 | } | |
2570 | ||
491381ce | 2571 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2572 | { |
491381ce JA |
2573 | /* |
2574 | * Tell lockdep we inherited freeze protection from submission | |
2575 | * thread. | |
2576 | */ | |
2577 | if (req->flags & REQ_F_ISREG) { | |
1c98679d | 2578 | struct super_block *sb = file_inode(req->file)->i_sb; |
2b188cc1 | 2579 | |
1c98679d PB |
2580 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
2581 | sb_end_write(sb); | |
2b188cc1 JA |
2582 | } |
2583 | } | |
2584 | ||
b63534c4 | 2585 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2586 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 | 2587 | { |
ab454438 | 2588 | struct io_async_rw *rw = req->async_data; |
b63534c4 | 2589 | |
d886e185 | 2590 | if (!req_has_async_data(req)) |
ab454438 | 2591 | return !io_req_prep_async(req); |
538941e2 | 2592 | iov_iter_restore(&rw->s.iter, &rw->s.iter_state); |
ab454438 | 2593 | return true; |
b63534c4 | 2594 | } |
b63534c4 | 2595 | |
3e6a0d3c | 2596 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 2597 | { |
355afaeb | 2598 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 2599 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 2600 | |
355afaeb JA |
2601 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2602 | return false; | |
3e6a0d3c JA |
2603 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
2604 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 2605 | return false; |
7c977a58 JA |
2606 | /* |
2607 | * If ref is dying, we might be running poll reap from the exit work. | |
2608 | * Don't attempt to reissue from that path, just let it fail with | |
2609 | * -EAGAIN. | |
2610 | */ | |
3e6a0d3c JA |
2611 | if (percpu_ref_is_dying(&ctx->refs)) |
2612 | return false; | |
ef046888 JA |
2613 | /* |
2614 | * Play it safe and assume not safe to re-import and reissue if we're | |
2615 | * not in the original thread group (or in task context). | |
2616 | */ | |
2617 | if (!same_thread_group(req->task, current) || !in_task()) | |
2618 | return false; | |
3e6a0d3c JA |
2619 | return true; |
2620 | } | |
e82ad485 | 2621 | #else |
a1ff1e3f | 2622 | static bool io_resubmit_prep(struct io_kiocb *req) |
e82ad485 JA |
2623 | { |
2624 | return false; | |
2625 | } | |
e82ad485 | 2626 | static bool io_rw_should_reissue(struct io_kiocb *req) |
3e6a0d3c | 2627 | { |
b63534c4 JA |
2628 | return false; |
2629 | } | |
3e6a0d3c | 2630 | #endif |
b63534c4 | 2631 | |
8ef12efe | 2632 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
a1d7c393 | 2633 | { |
b65c128f PB |
2634 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
2635 | kiocb_end_write(req); | |
258f3a7f | 2636 | if (unlikely(res != req->result)) { |
9532b99b PB |
2637 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && |
2638 | io_rw_should_reissue(req)) { | |
2639 | req->flags |= REQ_F_REISSUE; | |
8ef12efe | 2640 | return true; |
9532b99b | 2641 | } |
93d2bcd2 | 2642 | req_set_fail(req); |
8ef12efe | 2643 | req->result = res; |
9532b99b | 2644 | } |
8ef12efe JA |
2645 | return false; |
2646 | } | |
2647 | ||
f237c30a | 2648 | static void io_req_task_complete(struct io_kiocb *req, bool *locked) |
8ef12efe | 2649 | { |
126180b9 | 2650 | unsigned int cflags = io_put_rw_kbuf(req); |
54daa9b2 | 2651 | int res = req->result; |
126180b9 PB |
2652 | |
2653 | if (*locked) { | |
126180b9 | 2654 | io_req_complete_state(req, res, cflags); |
fff4e40e | 2655 | io_req_add_compl_list(req); |
126180b9 PB |
2656 | } else { |
2657 | io_req_complete_post(req, res, cflags); | |
2658 | } | |
8ef12efe JA |
2659 | } |
2660 | ||
2661 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, | |
2662 | unsigned int issue_flags) | |
2663 | { | |
2664 | if (__io_complete_rw_common(req, res)) | |
2665 | return; | |
63637853 | 2666 | __io_req_complete(req, issue_flags, req->result, io_put_rw_kbuf(req)); |
ba816ad6 JA |
2667 | } |
2668 | ||
6b19b766 | 2669 | static void io_complete_rw(struct kiocb *kiocb, long res) |
ba816ad6 | 2670 | { |
9adbd45d | 2671 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2672 | |
8ef12efe JA |
2673 | if (__io_complete_rw_common(req, res)) |
2674 | return; | |
2675 | req->result = res; | |
2676 | req->io_task_work.func = io_req_task_complete; | |
2677 | io_req_task_work_add(req); | |
2b188cc1 JA |
2678 | } |
2679 | ||
6b19b766 | 2680 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res) |
def596e9 | 2681 | { |
9adbd45d | 2682 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2683 | |
491381ce JA |
2684 | if (kiocb->ki_flags & IOCB_WRITE) |
2685 | kiocb_end_write(req); | |
9532b99b | 2686 | if (unlikely(res != req->result)) { |
b66ceaf3 PB |
2687 | if (res == -EAGAIN && io_rw_should_reissue(req)) { |
2688 | req->flags |= REQ_F_REISSUE; | |
2689 | return; | |
9532b99b | 2690 | } |
258f3a7f | 2691 | req->result = res; |
8c130827 | 2692 | } |
bbde017a | 2693 | |
b3fa03fd PB |
2694 | /* order with io_iopoll_complete() checking ->iopoll_completed */ |
2695 | smp_store_release(&req->iopoll_completed, 1); | |
def596e9 JA |
2696 | } |
2697 | ||
2698 | /* | |
2699 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2700 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
f39c8a5b | 2701 | * find it from a io_do_iopoll() thread before the issuer is done |
def596e9 JA |
2702 | * accessing the kiocb cookie. |
2703 | */ | |
9882131c | 2704 | static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags) |
def596e9 JA |
2705 | { |
2706 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 2707 | const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
cb3d8972 PB |
2708 | |
2709 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
3b44b371 | 2710 | if (unlikely(needs_lock)) |
cb3d8972 | 2711 | mutex_lock(&ctx->uring_lock); |
def596e9 JA |
2712 | |
2713 | /* | |
2714 | * Track whether we have multiple files in our lists. This will impact | |
2715 | * how we do polling eventually, not spinning if we're on potentially | |
2716 | * different devices. | |
2717 | */ | |
5eef4e87 | 2718 | if (wq_list_empty(&ctx->iopoll_list)) { |
915b3dde HX |
2719 | ctx->poll_multi_queue = false; |
2720 | } else if (!ctx->poll_multi_queue) { | |
def596e9 JA |
2721 | struct io_kiocb *list_req; |
2722 | ||
5eef4e87 PB |
2723 | list_req = container_of(ctx->iopoll_list.first, struct io_kiocb, |
2724 | comp_list); | |
30da1b45 | 2725 | if (list_req->file != req->file) |
915b3dde | 2726 | ctx->poll_multi_queue = true; |
def596e9 JA |
2727 | } |
2728 | ||
2729 | /* | |
2730 | * For fast devices, IO may have already completed. If it has, add | |
2731 | * it to the front so we find it first. | |
2732 | */ | |
65a6543d | 2733 | if (READ_ONCE(req->iopoll_completed)) |
5eef4e87 | 2734 | wq_list_add_head(&req->comp_list, &ctx->iopoll_list); |
def596e9 | 2735 | else |
5eef4e87 | 2736 | wq_list_add_tail(&req->comp_list, &ctx->iopoll_list); |
bdcd3eab | 2737 | |
3b44b371 | 2738 | if (unlikely(needs_lock)) { |
cb3d8972 PB |
2739 | /* |
2740 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle | |
2741 | * in sq thread task context or in io worker task context. If | |
2742 | * current task context is sq thread, we don't need to check | |
2743 | * whether should wake up sq thread. | |
2744 | */ | |
2745 | if ((ctx->flags & IORING_SETUP_SQPOLL) && | |
2746 | wq_has_sleeper(&ctx->sq_data->wait)) | |
2747 | wake_up(&ctx->sq_data->wait); | |
2748 | ||
2749 | mutex_unlock(&ctx->uring_lock); | |
2750 | } | |
def596e9 JA |
2751 | } |
2752 | ||
4503b767 JA |
2753 | static bool io_bdev_nowait(struct block_device *bdev) |
2754 | { | |
9ba0d0c8 | 2755 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2756 | } |
2757 | ||
2b188cc1 JA |
2758 | /* |
2759 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2760 | * any file. For now, just ensure that anything potentially problematic is done | |
2761 | * inline. | |
2762 | */ | |
88459b50 | 2763 | static bool __io_file_supports_nowait(struct file *file, umode_t mode) |
2b188cc1 | 2764 | { |
4503b767 | 2765 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2766 | if (IS_ENABLED(CONFIG_BLOCK) && |
2767 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2768 | return true; |
2769 | return false; | |
2770 | } | |
976517f1 | 2771 | if (S_ISSOCK(mode)) |
2b188cc1 | 2772 | return true; |
4503b767 | 2773 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2774 | if (IS_ENABLED(CONFIG_BLOCK) && |
2775 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2776 | file->f_op != &io_uring_fops) |
2777 | return true; | |
2778 | return false; | |
2779 | } | |
2b188cc1 | 2780 | |
c5b85625 JA |
2781 | /* any ->read/write should understand O_NONBLOCK */ |
2782 | if (file->f_flags & O_NONBLOCK) | |
2783 | return true; | |
35645ac3 | 2784 | return file->f_mode & FMODE_NOWAIT; |
2b188cc1 | 2785 | } |
c5b85625 | 2786 | |
88459b50 PB |
2787 | /* |
2788 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2789 | * any file. For now, just ensure that anything potentially problematic is done | |
2790 | * inline. | |
2791 | */ | |
2792 | static unsigned int io_file_get_flags(struct file *file) | |
2793 | { | |
2794 | umode_t mode = file_inode(file)->i_mode; | |
2795 | unsigned int res = 0; | |
af197f50 | 2796 | |
88459b50 PB |
2797 | if (S_ISREG(mode)) |
2798 | res |= FFS_ISREG; | |
2799 | if (__io_file_supports_nowait(file, mode)) | |
2800 | res |= FFS_NOWAIT; | |
2801 | return res; | |
2b188cc1 JA |
2802 | } |
2803 | ||
35645ac3 | 2804 | static inline bool io_file_supports_nowait(struct io_kiocb *req) |
7b29f92d | 2805 | { |
88459b50 | 2806 | return req->flags & REQ_F_SUPPORT_NOWAIT; |
7b29f92d JA |
2807 | } |
2808 | ||
b9a6b8f9 | 2809 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 2810 | { |
def596e9 | 2811 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2812 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 2813 | struct file *file = req->file; |
09bb8394 JA |
2814 | unsigned ioprio; |
2815 | int ret; | |
2b188cc1 | 2816 | |
88459b50 PB |
2817 | if (!io_req_ffs_set(req)) |
2818 | req->flags |= io_file_get_flags(file) << REQ_F_SUPPORT_NOWAIT_BIT; | |
491381ce | 2819 | |
2b188cc1 | 2820 | kiocb->ki_pos = READ_ONCE(sqe->off); |
75c668cd | 2821 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
ba04291e | 2822 | req->flags |= REQ_F_CUR_POS; |
75c668cd | 2823 | kiocb->ki_pos = file->f_pos; |
ba04291e | 2824 | } |
5cb03d63 | 2825 | kiocb->ki_flags = iocb_flags(file); |
3e577dcd PB |
2826 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
2827 | if (unlikely(ret)) | |
2828 | return ret; | |
2b188cc1 | 2829 | |
5d329e12 JA |
2830 | /* |
2831 | * If the file is marked O_NONBLOCK, still allow retry for it if it | |
2832 | * supports async. Otherwise it's impossible to use O_NONBLOCK files | |
2833 | * reliably. If not, or it IOCB_NOWAIT is set, don't retry. | |
2834 | */ | |
2835 | if ((kiocb->ki_flags & IOCB_NOWAIT) || | |
35645ac3 | 2836 | ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req))) |
75c668cd PB |
2837 | req->flags |= REQ_F_NOWAIT; |
2838 | ||
def596e9 | 2839 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
5cb03d63 | 2840 | if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll) |
09bb8394 | 2841 | return -EOPNOTSUPP; |
2b188cc1 | 2842 | |
394918eb | 2843 | kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE; |
def596e9 | 2844 | kiocb->ki_complete = io_complete_rw_iopoll; |
65a6543d | 2845 | req->iopoll_completed = 0; |
def596e9 | 2846 | } else { |
09bb8394 JA |
2847 | if (kiocb->ki_flags & IOCB_HIPRI) |
2848 | return -EINVAL; | |
def596e9 JA |
2849 | kiocb->ki_complete = io_complete_rw; |
2850 | } | |
9adbd45d | 2851 | |
fb27274a PB |
2852 | ioprio = READ_ONCE(sqe->ioprio); |
2853 | if (ioprio) { | |
2854 | ret = ioprio_check_cap(ioprio); | |
2855 | if (ret) | |
2856 | return ret; | |
2857 | ||
2858 | kiocb->ki_ioprio = ioprio; | |
2859 | } else { | |
2860 | kiocb->ki_ioprio = get_current_ioprio(); | |
eae071c9 PB |
2861 | } |
2862 | ||
578c0ee2 | 2863 | req->imu = NULL; |
3529d8c2 JA |
2864 | req->rw.addr = READ_ONCE(sqe->addr); |
2865 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2866 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2867 | return 0; |
2b188cc1 JA |
2868 | } |
2869 | ||
2870 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2871 | { | |
2872 | switch (ret) { | |
2873 | case -EIOCBQUEUED: | |
2874 | break; | |
2875 | case -ERESTARTSYS: | |
2876 | case -ERESTARTNOINTR: | |
2877 | case -ERESTARTNOHAND: | |
2878 | case -ERESTART_RESTARTBLOCK: | |
2879 | /* | |
2880 | * We can't just restart the syscall, since previously | |
2881 | * submitted sqes may already be in progress. Just fail this | |
2882 | * IO with EINTR. | |
2883 | */ | |
2884 | ret = -EINTR; | |
df561f66 | 2885 | fallthrough; |
2b188cc1 | 2886 | default: |
6b19b766 | 2887 | kiocb->ki_complete(kiocb, ret); |
2b188cc1 JA |
2888 | } |
2889 | } | |
2890 | ||
a1d7c393 | 2891 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
889fca73 | 2892 | unsigned int issue_flags) |
ba816ad6 | 2893 | { |
ba04291e | 2894 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2895 | struct io_async_rw *io = req->async_data; |
ba04291e | 2896 | |
227c0c96 | 2897 | /* add previously done IO, if any */ |
d886e185 | 2898 | if (req_has_async_data(req) && io->bytes_done > 0) { |
227c0c96 | 2899 | if (ret < 0) |
e8c2bc1f | 2900 | ret = io->bytes_done; |
227c0c96 | 2901 | else |
e8c2bc1f | 2902 | ret += io->bytes_done; |
227c0c96 JA |
2903 | } |
2904 | ||
ba04291e JA |
2905 | if (req->flags & REQ_F_CUR_POS) |
2906 | req->file->f_pos = kiocb->ki_pos; | |
b66ceaf3 | 2907 | if (ret >= 0 && (kiocb->ki_complete == io_complete_rw)) |
889fca73 | 2908 | __io_complete_rw(req, ret, 0, issue_flags); |
ba816ad6 JA |
2909 | else |
2910 | io_rw_done(kiocb, ret); | |
97284637 | 2911 | |
b66ceaf3 | 2912 | if (req->flags & REQ_F_REISSUE) { |
97284637 | 2913 | req->flags &= ~REQ_F_REISSUE; |
a7be7c23 | 2914 | if (io_resubmit_prep(req)) { |
773af691 | 2915 | io_req_task_queue_reissue(req); |
8c130827 | 2916 | } else { |
b66ceaf3 PB |
2917 | unsigned int cflags = io_put_rw_kbuf(req); |
2918 | struct io_ring_ctx *ctx = req->ctx; | |
2919 | ||
93d2bcd2 | 2920 | req_set_fail(req); |
3b44b371 | 2921 | if (issue_flags & IO_URING_F_UNLOCKED) { |
b66ceaf3 PB |
2922 | mutex_lock(&ctx->uring_lock); |
2923 | __io_req_complete(req, issue_flags, ret, cflags); | |
2924 | mutex_unlock(&ctx->uring_lock); | |
2925 | } else { | |
2926 | __io_req_complete(req, issue_flags, ret, cflags); | |
2927 | } | |
97284637 PB |
2928 | } |
2929 | } | |
ba816ad6 JA |
2930 | } |
2931 | ||
eae071c9 PB |
2932 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
2933 | struct io_mapped_ubuf *imu) | |
edafccee | 2934 | { |
9adbd45d | 2935 | size_t len = req->rw.len; |
75769e3f | 2936 | u64 buf_end, buf_addr = req->rw.addr; |
edafccee | 2937 | size_t offset; |
edafccee | 2938 | |
75769e3f | 2939 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
edafccee JA |
2940 | return -EFAULT; |
2941 | /* not inside the mapped region */ | |
4751f53d | 2942 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
edafccee JA |
2943 | return -EFAULT; |
2944 | ||
2945 | /* | |
2946 | * May not be a start of buffer, set size appropriately | |
2947 | * and advance us to the beginning. | |
2948 | */ | |
2949 | offset = buf_addr - imu->ubuf; | |
2950 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
2951 | |
2952 | if (offset) { | |
2953 | /* | |
2954 | * Don't use iov_iter_advance() here, as it's really slow for | |
2955 | * using the latter parts of a big fixed buffer - it iterates | |
2956 | * over each segment manually. We can cheat a bit here, because | |
2957 | * we know that: | |
2958 | * | |
2959 | * 1) it's a BVEC iter, we set it up | |
2960 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
2961 | * first and last bvec | |
2962 | * | |
2963 | * So just find our index, and adjust the iterator afterwards. | |
2964 | * If the offset is within the first bvec (or the whole first | |
2965 | * bvec, just use iov_iter_advance(). This makes it easier | |
2966 | * since we can just skip the first segment, which may not | |
2967 | * be PAGE_SIZE aligned. | |
2968 | */ | |
2969 | const struct bio_vec *bvec = imu->bvec; | |
2970 | ||
2971 | if (offset <= bvec->bv_len) { | |
2972 | iov_iter_advance(iter, offset); | |
2973 | } else { | |
2974 | unsigned long seg_skip; | |
2975 | ||
2976 | /* skip first vec */ | |
2977 | offset -= bvec->bv_len; | |
2978 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
2979 | ||
2980 | iter->bvec = bvec + seg_skip; | |
2981 | iter->nr_segs -= seg_skip; | |
99c79f66 | 2982 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 2983 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
2984 | } |
2985 | } | |
2986 | ||
847595de | 2987 | return 0; |
edafccee JA |
2988 | } |
2989 | ||
eae071c9 PB |
2990 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
2991 | { | |
eae071c9 PB |
2992 | struct io_mapped_ubuf *imu = req->imu; |
2993 | u16 index, buf_index = req->buf_index; | |
2994 | ||
2995 | if (likely(!imu)) { | |
578c0ee2 PB |
2996 | struct io_ring_ctx *ctx = req->ctx; |
2997 | ||
eae071c9 PB |
2998 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
2999 | return -EFAULT; | |
578c0ee2 | 3000 | io_req_set_rsrc_node(req, ctx); |
eae071c9 PB |
3001 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
3002 | imu = READ_ONCE(ctx->user_bufs[index]); | |
3003 | req->imu = imu; | |
3004 | } | |
3005 | return __io_import_fixed(req, rw, iter, imu); | |
3006 | } | |
3007 | ||
bcda7baa JA |
3008 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
3009 | { | |
3010 | if (needs_lock) | |
3011 | mutex_unlock(&ctx->uring_lock); | |
3012 | } | |
3013 | ||
3014 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
3015 | { | |
3016 | /* | |
3017 | * "Normal" inline submissions always hold the uring_lock, since we | |
3018 | * grab it from the system call. Same is true for the SQPOLL offload. | |
3019 | * The only exception is when we've detached the request and issue it | |
3020 | * from an async worker thread, grab the lock for that case. | |
3021 | */ | |
3022 | if (needs_lock) | |
3023 | mutex_lock(&ctx->uring_lock); | |
3024 | } | |
3025 | ||
3026 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
51aac424 | 3027 | int bgid, unsigned int issue_flags) |
bcda7baa | 3028 | { |
30d51dd4 | 3029 | struct io_buffer *kbuf = req->kbuf; |
bcda7baa | 3030 | struct io_buffer *head; |
3b44b371 | 3031 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
bcda7baa JA |
3032 | |
3033 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3034 | return kbuf; | |
3035 | ||
3036 | io_ring_submit_lock(req->ctx, needs_lock); | |
3037 | ||
3038 | lockdep_assert_held(&req->ctx->uring_lock); | |
3039 | ||
9e15c3a0 | 3040 | head = xa_load(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
3041 | if (head) { |
3042 | if (!list_empty(&head->list)) { | |
3043 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
3044 | list); | |
3045 | list_del(&kbuf->list); | |
3046 | } else { | |
3047 | kbuf = head; | |
9e15c3a0 | 3048 | xa_erase(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
3049 | } |
3050 | if (*len > kbuf->len) | |
3051 | *len = kbuf->len; | |
30d51dd4 PB |
3052 | req->flags |= REQ_F_BUFFER_SELECTED; |
3053 | req->kbuf = kbuf; | |
bcda7baa JA |
3054 | } else { |
3055 | kbuf = ERR_PTR(-ENOBUFS); | |
3056 | } | |
3057 | ||
3058 | io_ring_submit_unlock(req->ctx, needs_lock); | |
bcda7baa JA |
3059 | return kbuf; |
3060 | } | |
3061 | ||
4d954c25 | 3062 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
51aac424 | 3063 | unsigned int issue_flags) |
4d954c25 JA |
3064 | { |
3065 | struct io_buffer *kbuf; | |
4f4eeba8 | 3066 | u16 bgid; |
4d954c25 | 3067 | |
4f4eeba8 | 3068 | bgid = req->buf_index; |
51aac424 | 3069 | kbuf = io_buffer_select(req, len, bgid, issue_flags); |
4d954c25 JA |
3070 | if (IS_ERR(kbuf)) |
3071 | return kbuf; | |
4d954c25 JA |
3072 | return u64_to_user_ptr(kbuf->addr); |
3073 | } | |
3074 | ||
3075 | #ifdef CONFIG_COMPAT | |
3076 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3077 | unsigned int issue_flags) |
4d954c25 JA |
3078 | { |
3079 | struct compat_iovec __user *uiov; | |
3080 | compat_ssize_t clen; | |
3081 | void __user *buf; | |
3082 | ssize_t len; | |
3083 | ||
3084 | uiov = u64_to_user_ptr(req->rw.addr); | |
3085 | if (!access_ok(uiov, sizeof(*uiov))) | |
3086 | return -EFAULT; | |
3087 | if (__get_user(clen, &uiov->iov_len)) | |
3088 | return -EFAULT; | |
3089 | if (clen < 0) | |
3090 | return -EINVAL; | |
3091 | ||
3092 | len = clen; | |
51aac424 | 3093 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3094 | if (IS_ERR(buf)) |
3095 | return PTR_ERR(buf); | |
3096 | iov[0].iov_base = buf; | |
3097 | iov[0].iov_len = (compat_size_t) len; | |
3098 | return 0; | |
3099 | } | |
3100 | #endif | |
3101 | ||
3102 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3103 | unsigned int issue_flags) |
4d954c25 JA |
3104 | { |
3105 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3106 | void __user *buf; | |
3107 | ssize_t len; | |
3108 | ||
3109 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3110 | return -EFAULT; | |
3111 | ||
3112 | len = iov[0].iov_len; | |
3113 | if (len < 0) | |
3114 | return -EINVAL; | |
51aac424 | 3115 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3116 | if (IS_ERR(buf)) |
3117 | return PTR_ERR(buf); | |
3118 | iov[0].iov_base = buf; | |
3119 | iov[0].iov_len = len; | |
3120 | return 0; | |
3121 | } | |
3122 | ||
3123 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3124 | unsigned int issue_flags) |
4d954c25 | 3125 | { |
dddb3e26 | 3126 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
30d51dd4 | 3127 | struct io_buffer *kbuf = req->kbuf; |
dddb3e26 | 3128 | |
dddb3e26 JA |
3129 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
3130 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3131 | return 0; |
dddb3e26 | 3132 | } |
dd201662 | 3133 | if (req->rw.len != 1) |
4d954c25 JA |
3134 | return -EINVAL; |
3135 | ||
3136 | #ifdef CONFIG_COMPAT | |
3137 | if (req->ctx->compat) | |
51aac424 | 3138 | return io_compat_import(req, iov, issue_flags); |
4d954c25 JA |
3139 | #endif |
3140 | ||
51aac424 | 3141 | return __io_iov_buffer_select(req, iov, issue_flags); |
4d954c25 JA |
3142 | } |
3143 | ||
caa8fe6e PB |
3144 | static struct iovec *__io_import_iovec(int rw, struct io_kiocb *req, |
3145 | struct io_rw_state *s, | |
3146 | unsigned int issue_flags) | |
2b188cc1 | 3147 | { |
5e49c973 | 3148 | struct iov_iter *iter = &s->iter; |
847595de | 3149 | u8 opcode = req->opcode; |
caa8fe6e | 3150 | struct iovec *iovec; |
d1d681b0 PB |
3151 | void __user *buf; |
3152 | size_t sqe_len; | |
4d954c25 | 3153 | ssize_t ret; |
edafccee | 3154 | |
caa8fe6e PB |
3155 | BUILD_BUG_ON(ERR_PTR(0) != NULL); |
3156 | ||
3157 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) | |
3158 | return ERR_PTR(io_import_fixed(req, rw, iter)); | |
2b188cc1 | 3159 | |
bcda7baa | 3160 | /* buffer index only valid with fixed read/write, or buffer select */ |
d1d681b0 | 3161 | if (unlikely(req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))) |
caa8fe6e | 3162 | return ERR_PTR(-EINVAL); |
9adbd45d | 3163 | |
d1d681b0 PB |
3164 | buf = u64_to_user_ptr(req->rw.addr); |
3165 | sqe_len = req->rw.len; | |
9adbd45d | 3166 | |
3a6820f2 | 3167 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3168 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 3169 | buf = io_rw_buffer_select(req, &sqe_len, issue_flags); |
867a23ea | 3170 | if (IS_ERR(buf)) |
898df244 | 3171 | return ERR_CAST(buf); |
3f9d6441 | 3172 | req->rw.len = sqe_len; |
bcda7baa JA |
3173 | } |
3174 | ||
5e49c973 | 3175 | ret = import_single_range(rw, buf, sqe_len, s->fast_iov, iter); |
caa8fe6e | 3176 | return ERR_PTR(ret); |
3a6820f2 JA |
3177 | } |
3178 | ||
caa8fe6e | 3179 | iovec = s->fast_iov; |
4d954c25 | 3180 | if (req->flags & REQ_F_BUFFER_SELECT) { |
caa8fe6e | 3181 | ret = io_iov_buffer_select(req, iovec, issue_flags); |
847595de | 3182 | if (!ret) |
caa8fe6e PB |
3183 | iov_iter_init(iter, rw, iovec, 1, iovec->iov_len); |
3184 | return ERR_PTR(ret); | |
4d954c25 JA |
3185 | } |
3186 | ||
caa8fe6e | 3187 | ret = __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, &iovec, iter, |
89cd35c5 | 3188 | req->ctx->compat); |
caa8fe6e PB |
3189 | if (unlikely(ret < 0)) |
3190 | return ERR_PTR(ret); | |
3191 | return iovec; | |
2b188cc1 JA |
3192 | } |
3193 | ||
5e49c973 PB |
3194 | static inline int io_import_iovec(int rw, struct io_kiocb *req, |
3195 | struct iovec **iovec, struct io_rw_state *s, | |
3196 | unsigned int issue_flags) | |
3197 | { | |
caa8fe6e PB |
3198 | *iovec = __io_import_iovec(rw, req, s, issue_flags); |
3199 | if (unlikely(IS_ERR(*iovec))) | |
3200 | return PTR_ERR(*iovec); | |
5e49c973 | 3201 | |
5e49c973 | 3202 | iov_iter_save_state(&s->iter, &s->iter_state); |
caa8fe6e | 3203 | return 0; |
2b188cc1 JA |
3204 | } |
3205 | ||
0fef9483 JA |
3206 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3207 | { | |
5b09e37e | 3208 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3209 | } |
3210 | ||
31b51510 | 3211 | /* |
32960613 JA |
3212 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3213 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3214 | */ |
4017eb91 | 3215 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3216 | { |
4017eb91 JA |
3217 | struct kiocb *kiocb = &req->rw.kiocb; |
3218 | struct file *file = req->file; | |
32960613 JA |
3219 | ssize_t ret = 0; |
3220 | ||
3221 | /* | |
3222 | * Don't support polled IO through this interface, and we can't | |
3223 | * support non-blocking either. For the latter, this just causes | |
3224 | * the kiocb to be handled from an async context. | |
3225 | */ | |
3226 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3227 | return -EOPNOTSUPP; | |
35645ac3 PB |
3228 | if ((kiocb->ki_flags & IOCB_NOWAIT) && |
3229 | !(kiocb->ki_filp->f_flags & O_NONBLOCK)) | |
32960613 JA |
3230 | return -EAGAIN; |
3231 | ||
3232 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3233 | struct iovec iovec; |
32960613 JA |
3234 | ssize_t nr; |
3235 | ||
311ae9e1 PB |
3236 | if (!iov_iter_is_bvec(iter)) { |
3237 | iovec = iov_iter_iovec(iter); | |
3238 | } else { | |
4017eb91 JA |
3239 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3240 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3241 | } |
3242 | ||
32960613 JA |
3243 | if (rw == READ) { |
3244 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3245 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3246 | } else { |
3247 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3248 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3249 | } |
3250 | ||
3251 | if (nr < 0) { | |
3252 | if (!ret) | |
3253 | ret = nr; | |
3254 | break; | |
3255 | } | |
16c8d2df JA |
3256 | if (!iov_iter_is_bvec(iter)) { |
3257 | iov_iter_advance(iter, nr); | |
3258 | } else { | |
3259 | req->rw.len -= nr; | |
3260 | req->rw.addr += nr; | |
3261 | } | |
32960613 JA |
3262 | ret += nr; |
3263 | if (nr != iovec.iov_len) | |
3264 | break; | |
32960613 JA |
3265 | } |
3266 | ||
3267 | return ret; | |
3268 | } | |
3269 | ||
ff6165b2 JA |
3270 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3271 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3272 | { |
e8c2bc1f | 3273 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3274 | |
538941e2 | 3275 | memcpy(&rw->s.iter, iter, sizeof(*iter)); |
afb87658 | 3276 | rw->free_iovec = iovec; |
227c0c96 | 3277 | rw->bytes_done = 0; |
ff6165b2 | 3278 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3279 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3280 | return; |
b64e3444 | 3281 | if (!iovec) { |
ff6165b2 JA |
3282 | unsigned iov_off = 0; |
3283 | ||
538941e2 | 3284 | rw->s.iter.iov = rw->s.fast_iov; |
ff6165b2 JA |
3285 | if (iter->iov != fast_iov) { |
3286 | iov_off = iter->iov - fast_iov; | |
538941e2 | 3287 | rw->s.iter.iov += iov_off; |
ff6165b2 | 3288 | } |
538941e2 PB |
3289 | if (rw->s.fast_iov != fast_iov) |
3290 | memcpy(rw->s.fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3291 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3292 | } else { |
3293 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3294 | } |
3295 | } | |
3296 | ||
8d4af685 | 3297 | static inline bool io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3298 | { |
e8c2bc1f JA |
3299 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3300 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
d886e185 PB |
3301 | if (req->async_data) { |
3302 | req->flags |= REQ_F_ASYNC_DATA; | |
3303 | return false; | |
3304 | } | |
3305 | return true; | |
3d9932a8 XW |
3306 | } |
3307 | ||
ff6165b2 | 3308 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
c88598a9 | 3309 | struct io_rw_state *s, bool force) |
b7bb4f7d | 3310 | { |
26f0505a | 3311 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
74566df3 | 3312 | return 0; |
d886e185 | 3313 | if (!req_has_async_data(req)) { |
cd658695 JA |
3314 | struct io_async_rw *iorw; |
3315 | ||
6cb78689 | 3316 | if (io_alloc_async_data(req)) { |
6bf985dc | 3317 | kfree(iovec); |
5d204bcf | 3318 | return -ENOMEM; |
6bf985dc | 3319 | } |
b7bb4f7d | 3320 | |
c88598a9 | 3321 | io_req_map_rw(req, iovec, s->fast_iov, &s->iter); |
cd658695 JA |
3322 | iorw = req->async_data; |
3323 | /* we've copied and mapped the iter, ensure state is saved */ | |
538941e2 | 3324 | iov_iter_save_state(&iorw->s.iter, &iorw->s.iter_state); |
5d204bcf | 3325 | } |
b7bb4f7d | 3326 | return 0; |
f67676d1 JA |
3327 | } |
3328 | ||
73debe68 | 3329 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3330 | { |
e8c2bc1f | 3331 | struct io_async_rw *iorw = req->async_data; |
5e49c973 | 3332 | struct iovec *iov; |
847595de | 3333 | int ret; |
c3e330a4 | 3334 | |
51aac424 | 3335 | /* submission path, ->uring_lock should already be taken */ |
3b44b371 | 3336 | ret = io_import_iovec(rw, req, &iov, &iorw->s, 0); |
c3e330a4 PB |
3337 | if (unlikely(ret < 0)) |
3338 | return ret; | |
3339 | ||
ab0b196c PB |
3340 | iorw->bytes_done = 0; |
3341 | iorw->free_iovec = iov; | |
3342 | if (iov) | |
3343 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3344 | return 0; |
3345 | } | |
3346 | ||
73debe68 | 3347 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3348 | { |
3529d8c2 JA |
3349 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3350 | return -EBADF; | |
b9a6b8f9 | 3351 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3352 | } |
3353 | ||
c1dd91d1 | 3354 | /* |
ffdc8dab | 3355 | * This is our waitqueue callback handler, registered through __folio_lock_async() |
c1dd91d1 JA |
3356 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
3357 | * This gets called when the page is unlocked, and we generally expect that to | |
3358 | * happen when the page IO is completed and the page is now uptodate. This will | |
3359 | * queue a task_work based retry of the operation, attempting to copy the data | |
3360 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3361 | * do a thread based blocking retry of the operation. That's the unexpected | |
3362 | * slow path. | |
3363 | */ | |
bcf5a063 JA |
3364 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3365 | int sync, void *arg) | |
3366 | { | |
3367 | struct wait_page_queue *wpq; | |
3368 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3369 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3370 | |
3371 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3372 | ||
cdc8fcb4 LT |
3373 | if (!wake_page_match(wpq, key)) |
3374 | return 0; | |
3375 | ||
c8d317aa | 3376 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 | 3377 | list_del_init(&wait->entry); |
921b9054 | 3378 | io_req_task_queue(req); |
bcf5a063 JA |
3379 | return 1; |
3380 | } | |
3381 | ||
c1dd91d1 JA |
3382 | /* |
3383 | * This controls whether a given IO request should be armed for async page | |
3384 | * based retry. If we return false here, the request is handed to the async | |
3385 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3386 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3387 | * will either succeed because the page is now uptodate and unlocked, or it | |
3388 | * will register a callback when the page is unlocked at IO completion. Through | |
3389 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3390 | * That retry will attempt the buffered read again. The retry will generally | |
3391 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3392 | * async worker threads for a blocking retry. | |
3393 | */ | |
227c0c96 | 3394 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3395 | { |
e8c2bc1f JA |
3396 | struct io_async_rw *rw = req->async_data; |
3397 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3398 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3399 | |
bcf5a063 JA |
3400 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3401 | if (req->flags & REQ_F_NOWAIT) | |
3402 | return false; | |
f67676d1 | 3403 | |
227c0c96 | 3404 | /* Only for buffered IO */ |
3b2a4439 | 3405 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3406 | return false; |
3b2a4439 | 3407 | |
bcf5a063 JA |
3408 | /* |
3409 | * just use poll if we can, and don't attempt if the fs doesn't | |
3410 | * support callback based unlocks | |
3411 | */ | |
3412 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3413 | return false; | |
f67676d1 | 3414 | |
3b2a4439 JA |
3415 | wait->wait.func = io_async_buf_func; |
3416 | wait->wait.private = req; | |
3417 | wait->wait.flags = 0; | |
3418 | INIT_LIST_HEAD(&wait->wait.entry); | |
3419 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3420 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3421 | kiocb->ki_waitq = wait; |
3b2a4439 | 3422 | return true; |
bcf5a063 JA |
3423 | } |
3424 | ||
aeab9506 | 3425 | static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
bcf5a063 | 3426 | { |
607b6fb8 | 3427 | if (likely(req->file->f_op->read_iter)) |
bcf5a063 | 3428 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
2dd2111d | 3429 | else if (req->file->f_op->read) |
4017eb91 | 3430 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3431 | else |
3432 | return -EINVAL; | |
f67676d1 JA |
3433 | } |
3434 | ||
7db30437 ML |
3435 | static bool need_read_all(struct io_kiocb *req) |
3436 | { | |
3437 | return req->flags & REQ_F_ISREG || | |
3438 | S_ISBLK(file_inode(req->file)->i_mode); | |
3439 | } | |
3440 | ||
889fca73 | 3441 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 3442 | { |
607b6fb8 | 3443 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 3444 | struct iovec *iovec; |
9adbd45d | 3445 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 3446 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
d886e185 | 3447 | struct io_async_rw *rw; |
cd658695 | 3448 | ssize_t ret, ret2; |
ff6165b2 | 3449 | |
607b6fb8 PB |
3450 | if (!req_has_async_data(req)) { |
3451 | ret = io_import_iovec(READ, req, &iovec, s, issue_flags); | |
3452 | if (unlikely(ret < 0)) | |
3453 | return ret; | |
3454 | } else { | |
d886e185 | 3455 | rw = req->async_data; |
c88598a9 | 3456 | s = &rw->s; |
cd658695 JA |
3457 | /* |
3458 | * We come here from an earlier attempt, restore our state to | |
3459 | * match in case it doesn't. It's cheap enough that we don't | |
3460 | * need to make this conditional. | |
3461 | */ | |
c88598a9 | 3462 | iov_iter_restore(&s->iter, &s->iter_state); |
2846c481 | 3463 | iovec = NULL; |
2846c481 | 3464 | } |
c88598a9 | 3465 | req->result = iov_iter_count(&s->iter); |
2b188cc1 | 3466 | |
607b6fb8 PB |
3467 | if (force_nonblock) { |
3468 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 3469 | if (unlikely(!io_file_supports_nowait(req))) { |
607b6fb8 PB |
3470 | ret = io_setup_async_rw(req, iovec, s, true); |
3471 | return ret ?: -EAGAIN; | |
3472 | } | |
a88fc400 | 3473 | kiocb->ki_flags |= IOCB_NOWAIT; |
607b6fb8 PB |
3474 | } else { |
3475 | /* Ensure we clear previously set non-block flag */ | |
3476 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
6713e7a6 | 3477 | } |
9e645e11 | 3478 | |
cd658695 | 3479 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), req->result); |
5ea5dd45 PB |
3480 | if (unlikely(ret)) { |
3481 | kfree(iovec); | |
3482 | return ret; | |
3483 | } | |
2b188cc1 | 3484 | |
c88598a9 | 3485 | ret = io_iter_do_read(req, &s->iter); |
32960613 | 3486 | |
230d50d4 | 3487 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
6ad7f233 | 3488 | req->flags &= ~REQ_F_REISSUE; |
eefdf30f JA |
3489 | /* IOPOLL retry should happen for io-wq threads */ |
3490 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3491 | goto done; |
75c668cd PB |
3492 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3493 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3494 | goto done; |
f38c7e3a | 3495 | ret = 0; |
230d50d4 JA |
3496 | } else if (ret == -EIOCBQUEUED) { |
3497 | goto out_free; | |
f80a50a6 | 3498 | } else if (ret == req->result || ret <= 0 || !force_nonblock || |
7db30437 | 3499 | (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) { |
7335e3bf | 3500 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3501 | goto done; |
227c0c96 JA |
3502 | } |
3503 | ||
cd658695 JA |
3504 | /* |
3505 | * Don't depend on the iter state matching what was consumed, or being | |
3506 | * untouched in case of error. Restore it and we'll advance it | |
3507 | * manually if we need to. | |
3508 | */ | |
c88598a9 | 3509 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 3510 | |
c88598a9 | 3511 | ret2 = io_setup_async_rw(req, iovec, s, true); |
6bf985dc PB |
3512 | if (ret2) |
3513 | return ret2; | |
3514 | ||
fe1cdd55 | 3515 | iovec = NULL; |
e8c2bc1f | 3516 | rw = req->async_data; |
c88598a9 | 3517 | s = &rw->s; |
cd658695 JA |
3518 | /* |
3519 | * Now use our persistent iterator and state, if we aren't already. | |
3520 | * We've restored and mapped the iter to match. | |
3521 | */ | |
227c0c96 | 3522 | |
b23df91b | 3523 | do { |
cd658695 JA |
3524 | /* |
3525 | * We end up here because of a partial read, either from | |
3526 | * above or inside this loop. Advance the iter by the bytes | |
3527 | * that were consumed. | |
3528 | */ | |
c88598a9 PB |
3529 | iov_iter_advance(&s->iter, ret); |
3530 | if (!iov_iter_count(&s->iter)) | |
cd658695 | 3531 | break; |
b23df91b | 3532 | rw->bytes_done += ret; |
c88598a9 | 3533 | iov_iter_save_state(&s->iter, &s->iter_state); |
cd658695 | 3534 | |
b23df91b PB |
3535 | /* if we can retry, do so with the callbacks armed */ |
3536 | if (!io_rw_should_retry(req)) { | |
3537 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3538 | return -EAGAIN; | |
3539 | } | |
3540 | ||
3541 | /* | |
3542 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3543 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3544 | * desired page gets unlocked. We can also get a partial read | |
3545 | * here, and if we do, then just retry at the new offset. | |
3546 | */ | |
c88598a9 | 3547 | ret = io_iter_do_read(req, &s->iter); |
b23df91b PB |
3548 | if (ret == -EIOCBQUEUED) |
3549 | return 0; | |
227c0c96 | 3550 | /* we got some bytes, but not all. retry. */ |
b5b0ecb7 | 3551 | kiocb->ki_flags &= ~IOCB_WAITQ; |
c88598a9 | 3552 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 3553 | } while (ret > 0); |
227c0c96 | 3554 | done: |
889fca73 | 3555 | kiocb_done(kiocb, ret, issue_flags); |
fe1cdd55 PB |
3556 | out_free: |
3557 | /* it's faster to check here then delegate to kfree */ | |
3558 | if (iovec) | |
3559 | kfree(iovec); | |
5ea5dd45 | 3560 | return 0; |
2b188cc1 JA |
3561 | } |
3562 | ||
73debe68 | 3563 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3564 | { |
3529d8c2 JA |
3565 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3566 | return -EBADF; | |
3884b83d | 3567 | req->rw.kiocb.ki_hint = ki_hint_validate(file_write_hint(req->file)); |
b9a6b8f9 | 3568 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3569 | } |
3570 | ||
889fca73 | 3571 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 3572 | { |
607b6fb8 | 3573 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 3574 | struct iovec *iovec; |
9adbd45d | 3575 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 3576 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
cd658695 | 3577 | ssize_t ret, ret2; |
2b188cc1 | 3578 | |
607b6fb8 | 3579 | if (!req_has_async_data(req)) { |
5e49c973 PB |
3580 | ret = io_import_iovec(WRITE, req, &iovec, s, issue_flags); |
3581 | if (unlikely(ret < 0)) | |
2846c481 | 3582 | return ret; |
607b6fb8 PB |
3583 | } else { |
3584 | struct io_async_rw *rw = req->async_data; | |
3585 | ||
3586 | s = &rw->s; | |
3587 | iov_iter_restore(&s->iter, &s->iter_state); | |
2846c481 | 3588 | iovec = NULL; |
2846c481 | 3589 | } |
c88598a9 | 3590 | req->result = iov_iter_count(&s->iter); |
2b188cc1 | 3591 | |
607b6fb8 PB |
3592 | if (force_nonblock) { |
3593 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 3594 | if (unlikely(!io_file_supports_nowait(req))) |
607b6fb8 | 3595 | goto copy_iov; |
fd6c2e4c | 3596 | |
607b6fb8 PB |
3597 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3598 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3599 | (req->flags & REQ_F_ISREG)) | |
3600 | goto copy_iov; | |
31b51510 | 3601 | |
607b6fb8 PB |
3602 | kiocb->ki_flags |= IOCB_NOWAIT; |
3603 | } else { | |
3604 | /* Ensure we clear previously set non-block flag */ | |
3605 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
3606 | } | |
31b51510 | 3607 | |
cd658695 | 3608 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), req->result); |
fa15bafb PB |
3609 | if (unlikely(ret)) |
3610 | goto out_free; | |
4ed734b0 | 3611 | |
fa15bafb PB |
3612 | /* |
3613 | * Open-code file_start_write here to grab freeze protection, | |
3614 | * which will be released by another thread in | |
3615 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3616 | * released so that it doesn't complain about the held lock when | |
3617 | * we return to userspace. | |
3618 | */ | |
3619 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3620 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3621 | __sb_writers_release(file_inode(req->file)->i_sb, |
3622 | SB_FREEZE_WRITE); | |
3623 | } | |
3624 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3625 | |
35645ac3 | 3626 | if (likely(req->file->f_op->write_iter)) |
c88598a9 | 3627 | ret2 = call_write_iter(req->file, kiocb, &s->iter); |
2dd2111d | 3628 | else if (req->file->f_op->write) |
c88598a9 | 3629 | ret2 = loop_rw_iter(WRITE, req, &s->iter); |
2dd2111d GH |
3630 | else |
3631 | ret2 = -EINVAL; | |
4ed734b0 | 3632 | |
6ad7f233 PB |
3633 | if (req->flags & REQ_F_REISSUE) { |
3634 | req->flags &= ~REQ_F_REISSUE; | |
230d50d4 | 3635 | ret2 = -EAGAIN; |
6ad7f233 | 3636 | } |
230d50d4 | 3637 | |
fa15bafb PB |
3638 | /* |
3639 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3640 | * retry them without IOCB_NOWAIT. | |
3641 | */ | |
3642 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3643 | ret2 = -EAGAIN; | |
75c668cd PB |
3644 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3645 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3646 | goto done; |
fa15bafb | 3647 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f | 3648 | /* IOPOLL retry should happen for io-wq threads */ |
b10841c9 | 3649 | if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL)) |
eefdf30f | 3650 | goto copy_iov; |
355afaeb | 3651 | done: |
889fca73 | 3652 | kiocb_done(kiocb, ret2, issue_flags); |
fa15bafb | 3653 | } else { |
f67676d1 | 3654 | copy_iov: |
c88598a9 PB |
3655 | iov_iter_restore(&s->iter, &s->iter_state); |
3656 | ret = io_setup_async_rw(req, iovec, s, false); | |
6bf985dc | 3657 | return ret ?: -EAGAIN; |
2b188cc1 | 3658 | } |
31b51510 | 3659 | out_free: |
f261c168 | 3660 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3661 | if (iovec) |
6f2cc166 | 3662 | kfree(iovec); |
2b188cc1 JA |
3663 | return ret; |
3664 | } | |
3665 | ||
80a261fd JA |
3666 | static int io_renameat_prep(struct io_kiocb *req, |
3667 | const struct io_uring_sqe *sqe) | |
3668 | { | |
3669 | struct io_rename *ren = &req->rename; | |
3670 | const char __user *oldf, *newf; | |
3671 | ||
ed7eb259 JA |
3672 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3673 | return -EINVAL; | |
26578cda | 3674 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
ed7eb259 | 3675 | return -EINVAL; |
80a261fd JA |
3676 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3677 | return -EBADF; | |
3678 | ||
3679 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3680 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3681 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3682 | ren->new_dfd = READ_ONCE(sqe->len); | |
3683 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3684 | ||
3685 | ren->oldpath = getname(oldf); | |
3686 | if (IS_ERR(ren->oldpath)) | |
3687 | return PTR_ERR(ren->oldpath); | |
3688 | ||
3689 | ren->newpath = getname(newf); | |
3690 | if (IS_ERR(ren->newpath)) { | |
3691 | putname(ren->oldpath); | |
3692 | return PTR_ERR(ren->newpath); | |
3693 | } | |
3694 | ||
3695 | req->flags |= REQ_F_NEED_CLEANUP; | |
3696 | return 0; | |
3697 | } | |
3698 | ||
45d189c6 | 3699 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
3700 | { |
3701 | struct io_rename *ren = &req->rename; | |
3702 | int ret; | |
3703 | ||
45d189c6 | 3704 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
3705 | return -EAGAIN; |
3706 | ||
3707 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3708 | ren->newpath, ren->flags); | |
3709 | ||
3710 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3711 | if (ret < 0) | |
93d2bcd2 | 3712 | req_set_fail(req); |
80a261fd JA |
3713 | io_req_complete(req, ret); |
3714 | return 0; | |
3715 | } | |
3716 | ||
14a1143b JA |
3717 | static int io_unlinkat_prep(struct io_kiocb *req, |
3718 | const struct io_uring_sqe *sqe) | |
3719 | { | |
3720 | struct io_unlink *un = &req->unlink; | |
3721 | const char __user *fname; | |
3722 | ||
22634bc5 JA |
3723 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3724 | return -EINVAL; | |
26578cda PB |
3725 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
3726 | sqe->splice_fd_in) | |
22634bc5 | 3727 | return -EINVAL; |
14a1143b JA |
3728 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3729 | return -EBADF; | |
3730 | ||
3731 | un->dfd = READ_ONCE(sqe->fd); | |
3732 | ||
3733 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3734 | if (un->flags & ~AT_REMOVEDIR) | |
3735 | return -EINVAL; | |
3736 | ||
3737 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3738 | un->filename = getname(fname); | |
3739 | if (IS_ERR(un->filename)) | |
3740 | return PTR_ERR(un->filename); | |
3741 | ||
3742 | req->flags |= REQ_F_NEED_CLEANUP; | |
3743 | return 0; | |
3744 | } | |
3745 | ||
45d189c6 | 3746 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
3747 | { |
3748 | struct io_unlink *un = &req->unlink; | |
3749 | int ret; | |
3750 | ||
45d189c6 | 3751 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
3752 | return -EAGAIN; |
3753 | ||
3754 | if (un->flags & AT_REMOVEDIR) | |
3755 | ret = do_rmdir(un->dfd, un->filename); | |
3756 | else | |
3757 | ret = do_unlinkat(un->dfd, un->filename); | |
3758 | ||
3759 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3760 | if (ret < 0) | |
93d2bcd2 | 3761 | req_set_fail(req); |
14a1143b JA |
3762 | io_req_complete(req, ret); |
3763 | return 0; | |
3764 | } | |
3765 | ||
e34a02dc DK |
3766 | static int io_mkdirat_prep(struct io_kiocb *req, |
3767 | const struct io_uring_sqe *sqe) | |
3768 | { | |
3769 | struct io_mkdir *mkd = &req->mkdir; | |
3770 | const char __user *fname; | |
3771 | ||
3772 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3773 | return -EINVAL; | |
3774 | if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index || | |
3775 | sqe->splice_fd_in) | |
3776 | return -EINVAL; | |
3777 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3778 | return -EBADF; | |
3779 | ||
3780 | mkd->dfd = READ_ONCE(sqe->fd); | |
3781 | mkd->mode = READ_ONCE(sqe->len); | |
3782 | ||
3783 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3784 | mkd->filename = getname(fname); | |
3785 | if (IS_ERR(mkd->filename)) | |
3786 | return PTR_ERR(mkd->filename); | |
3787 | ||
3788 | req->flags |= REQ_F_NEED_CLEANUP; | |
3789 | return 0; | |
3790 | } | |
3791 | ||
04f34081 | 3792 | static int io_mkdirat(struct io_kiocb *req, unsigned int issue_flags) |
e34a02dc DK |
3793 | { |
3794 | struct io_mkdir *mkd = &req->mkdir; | |
3795 | int ret; | |
3796 | ||
3797 | if (issue_flags & IO_URING_F_NONBLOCK) | |
3798 | return -EAGAIN; | |
3799 | ||
3800 | ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode); | |
3801 | ||
3802 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3803 | if (ret < 0) | |
3804 | req_set_fail(req); | |
3805 | io_req_complete(req, ret); | |
3806 | return 0; | |
3807 | } | |
3808 | ||
7a8721f8 DK |
3809 | static int io_symlinkat_prep(struct io_kiocb *req, |
3810 | const struct io_uring_sqe *sqe) | |
3811 | { | |
3812 | struct io_symlink *sl = &req->symlink; | |
3813 | const char __user *oldpath, *newpath; | |
3814 | ||
3815 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3816 | return -EINVAL; | |
3817 | if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index || | |
3818 | sqe->splice_fd_in) | |
3819 | return -EINVAL; | |
3820 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3821 | return -EBADF; | |
3822 | ||
3823 | sl->new_dfd = READ_ONCE(sqe->fd); | |
3824 | oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3825 | newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3826 | ||
3827 | sl->oldpath = getname(oldpath); | |
3828 | if (IS_ERR(sl->oldpath)) | |
3829 | return PTR_ERR(sl->oldpath); | |
3830 | ||
3831 | sl->newpath = getname(newpath); | |
3832 | if (IS_ERR(sl->newpath)) { | |
3833 | putname(sl->oldpath); | |
3834 | return PTR_ERR(sl->newpath); | |
3835 | } | |
3836 | ||
3837 | req->flags |= REQ_F_NEED_CLEANUP; | |
3838 | return 0; | |
3839 | } | |
3840 | ||
04f34081 | 3841 | static int io_symlinkat(struct io_kiocb *req, unsigned int issue_flags) |
7a8721f8 DK |
3842 | { |
3843 | struct io_symlink *sl = &req->symlink; | |
3844 | int ret; | |
3845 | ||
3846 | if (issue_flags & IO_URING_F_NONBLOCK) | |
3847 | return -EAGAIN; | |
3848 | ||
3849 | ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath); | |
3850 | ||
3851 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3852 | if (ret < 0) | |
3853 | req_set_fail(req); | |
3854 | io_req_complete(req, ret); | |
3855 | return 0; | |
3856 | } | |
3857 | ||
cf30da90 DK |
3858 | static int io_linkat_prep(struct io_kiocb *req, |
3859 | const struct io_uring_sqe *sqe) | |
3860 | { | |
3861 | struct io_hardlink *lnk = &req->hardlink; | |
3862 | const char __user *oldf, *newf; | |
3863 | ||
3864 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3865 | return -EINVAL; | |
3866 | if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) | |
3867 | return -EINVAL; | |
3868 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3869 | return -EBADF; | |
3870 | ||
3871 | lnk->old_dfd = READ_ONCE(sqe->fd); | |
3872 | lnk->new_dfd = READ_ONCE(sqe->len); | |
3873 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3874 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3875 | lnk->flags = READ_ONCE(sqe->hardlink_flags); | |
3876 | ||
3877 | lnk->oldpath = getname(oldf); | |
3878 | if (IS_ERR(lnk->oldpath)) | |
3879 | return PTR_ERR(lnk->oldpath); | |
3880 | ||
3881 | lnk->newpath = getname(newf); | |
3882 | if (IS_ERR(lnk->newpath)) { | |
3883 | putname(lnk->oldpath); | |
3884 | return PTR_ERR(lnk->newpath); | |
3885 | } | |
3886 | ||
3887 | req->flags |= REQ_F_NEED_CLEANUP; | |
3888 | return 0; | |
3889 | } | |
3890 | ||
04f34081 | 3891 | static int io_linkat(struct io_kiocb *req, unsigned int issue_flags) |
cf30da90 DK |
3892 | { |
3893 | struct io_hardlink *lnk = &req->hardlink; | |
3894 | int ret; | |
3895 | ||
3896 | if (issue_flags & IO_URING_F_NONBLOCK) | |
3897 | return -EAGAIN; | |
3898 | ||
3899 | ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd, | |
3900 | lnk->newpath, lnk->flags); | |
3901 | ||
3902 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3903 | if (ret < 0) | |
3904 | req_set_fail(req); | |
3905 | io_req_complete(req, ret); | |
3906 | return 0; | |
3907 | } | |
3908 | ||
36f4fa68 JA |
3909 | static int io_shutdown_prep(struct io_kiocb *req, |
3910 | const struct io_uring_sqe *sqe) | |
3911 | { | |
3912 | #if defined(CONFIG_NET) | |
3913 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3914 | return -EINVAL; | |
26578cda PB |
3915 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
3916 | sqe->buf_index || sqe->splice_fd_in)) | |
36f4fa68 JA |
3917 | return -EINVAL; |
3918 | ||
3919 | req->shutdown.how = READ_ONCE(sqe->len); | |
3920 | return 0; | |
3921 | #else | |
3922 | return -EOPNOTSUPP; | |
3923 | #endif | |
3924 | } | |
3925 | ||
45d189c6 | 3926 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
3927 | { |
3928 | #if defined(CONFIG_NET) | |
3929 | struct socket *sock; | |
3930 | int ret; | |
3931 | ||
45d189c6 | 3932 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
3933 | return -EAGAIN; |
3934 | ||
48aba79b | 3935 | sock = sock_from_file(req->file); |
36f4fa68 | 3936 | if (unlikely(!sock)) |
48aba79b | 3937 | return -ENOTSOCK; |
36f4fa68 JA |
3938 | |
3939 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d | 3940 | if (ret < 0) |
93d2bcd2 | 3941 | req_set_fail(req); |
36f4fa68 JA |
3942 | io_req_complete(req, ret); |
3943 | return 0; | |
3944 | #else | |
3945 | return -EOPNOTSUPP; | |
3946 | #endif | |
3947 | } | |
3948 | ||
f2a8d5c7 PB |
3949 | static int __io_splice_prep(struct io_kiocb *req, |
3950 | const struct io_uring_sqe *sqe) | |
7d67af2c | 3951 | { |
fe7e3257 | 3952 | struct io_splice *sp = &req->splice; |
7d67af2c | 3953 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
7d67af2c | 3954 | |
3232dd02 PB |
3955 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3956 | return -EINVAL; | |
7d67af2c PB |
3957 | |
3958 | sp->file_in = NULL; | |
7d67af2c PB |
3959 | sp->len = READ_ONCE(sqe->len); |
3960 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3961 | ||
3962 | if (unlikely(sp->flags & ~valid_flags)) | |
3963 | return -EINVAL; | |
3964 | ||
62906e89 | 3965 | sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in), |
8371adf5 PB |
3966 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
3967 | if (!sp->file_in) | |
3968 | return -EBADF; | |
7d67af2c | 3969 | req->flags |= REQ_F_NEED_CLEANUP; |
7d67af2c PB |
3970 | return 0; |
3971 | } | |
3972 | ||
f2a8d5c7 PB |
3973 | static int io_tee_prep(struct io_kiocb *req, |
3974 | const struct io_uring_sqe *sqe) | |
3975 | { | |
3976 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3977 | return -EINVAL; | |
3978 | return __io_splice_prep(req, sqe); | |
3979 | } | |
3980 | ||
45d189c6 | 3981 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
3982 | { |
3983 | struct io_splice *sp = &req->splice; | |
3984 | struct file *in = sp->file_in; | |
3985 | struct file *out = sp->file_out; | |
3986 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3987 | long ret = 0; | |
3988 | ||
45d189c6 | 3989 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
3990 | return -EAGAIN; |
3991 | if (sp->len) | |
3992 | ret = do_tee(in, out, sp->len, flags); | |
3993 | ||
e1d767f0 PB |
3994 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
3995 | io_put_file(in); | |
f2a8d5c7 PB |
3996 | req->flags &= ~REQ_F_NEED_CLEANUP; |
3997 | ||
f2a8d5c7 | 3998 | if (ret != sp->len) |
93d2bcd2 | 3999 | req_set_fail(req); |
e1e16097 | 4000 | io_req_complete(req, ret); |
f2a8d5c7 PB |
4001 | return 0; |
4002 | } | |
4003 | ||
4004 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4005 | { | |
fe7e3257 | 4006 | struct io_splice *sp = &req->splice; |
f2a8d5c7 PB |
4007 | |
4008 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
4009 | sp->off_out = READ_ONCE(sqe->off); | |
4010 | return __io_splice_prep(req, sqe); | |
4011 | } | |
4012 | ||
45d189c6 | 4013 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
4014 | { |
4015 | struct io_splice *sp = &req->splice; | |
4016 | struct file *in = sp->file_in; | |
4017 | struct file *out = sp->file_out; | |
4018 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
4019 | loff_t *poff_in, *poff_out; | |
c9687426 | 4020 | long ret = 0; |
7d67af2c | 4021 | |
45d189c6 | 4022 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 4023 | return -EAGAIN; |
7d67af2c PB |
4024 | |
4025 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
4026 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 4027 | |
948a7749 | 4028 | if (sp->len) |
c9687426 | 4029 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c | 4030 | |
e1d767f0 PB |
4031 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4032 | io_put_file(in); | |
7d67af2c PB |
4033 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4034 | ||
7d67af2c | 4035 | if (ret != sp->len) |
93d2bcd2 | 4036 | req_set_fail(req); |
e1e16097 | 4037 | io_req_complete(req, ret); |
7d67af2c PB |
4038 | return 0; |
4039 | } | |
4040 | ||
2b188cc1 JA |
4041 | /* |
4042 | * IORING_OP_NOP just posts a completion event, nothing else. | |
4043 | */ | |
889fca73 | 4044 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
4045 | { |
4046 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 4047 | |
def596e9 JA |
4048 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4049 | return -EINVAL; | |
4050 | ||
889fca73 | 4051 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
4052 | return 0; |
4053 | } | |
4054 | ||
1155c76a | 4055 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 4056 | { |
6b06314c | 4057 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 4058 | |
09bb8394 JA |
4059 | if (!req->file) |
4060 | return -EBADF; | |
c992fe29 | 4061 | |
6b06314c | 4062 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 4063 | return -EINVAL; |
26578cda PB |
4064 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4065 | sqe->splice_fd_in)) | |
c992fe29 CH |
4066 | return -EINVAL; |
4067 | ||
8ed8d3c3 JA |
4068 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
4069 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
4070 | return -EINVAL; | |
4071 | ||
4072 | req->sync.off = READ_ONCE(sqe->off); | |
4073 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
4074 | return 0; |
4075 | } | |
4076 | ||
45d189c6 | 4077 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4078 | { |
8ed8d3c3 | 4079 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
4080 | int ret; |
4081 | ||
ac45abc0 | 4082 | /* fsync always requires a blocking context */ |
45d189c6 | 4083 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4084 | return -EAGAIN; |
4085 | ||
9adbd45d | 4086 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
4087 | end > 0 ? end : LLONG_MAX, |
4088 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
4089 | if (ret < 0) | |
93d2bcd2 | 4090 | req_set_fail(req); |
e1e16097 | 4091 | io_req_complete(req, ret); |
c992fe29 CH |
4092 | return 0; |
4093 | } | |
4094 | ||
d63d1b5e JA |
4095 | static int io_fallocate_prep(struct io_kiocb *req, |
4096 | const struct io_uring_sqe *sqe) | |
4097 | { | |
26578cda PB |
4098 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
4099 | sqe->splice_fd_in) | |
d63d1b5e | 4100 | return -EINVAL; |
3232dd02 PB |
4101 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4102 | return -EINVAL; | |
d63d1b5e JA |
4103 | |
4104 | req->sync.off = READ_ONCE(sqe->off); | |
4105 | req->sync.len = READ_ONCE(sqe->addr); | |
4106 | req->sync.mode = READ_ONCE(sqe->len); | |
4107 | return 0; | |
4108 | } | |
4109 | ||
45d189c6 | 4110 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 4111 | { |
ac45abc0 PB |
4112 | int ret; |
4113 | ||
d63d1b5e | 4114 | /* fallocate always requiring blocking context */ |
45d189c6 | 4115 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 4116 | return -EAGAIN; |
ac45abc0 PB |
4117 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
4118 | req->sync.len); | |
ac45abc0 | 4119 | if (ret < 0) |
93d2bcd2 | 4120 | req_set_fail(req); |
e1e16097 | 4121 | io_req_complete(req, ret); |
5d17b4a4 JA |
4122 | return 0; |
4123 | } | |
4124 | ||
ec65fea5 | 4125 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 4126 | { |
f8748881 | 4127 | const char __user *fname; |
15b71abe | 4128 | int ret; |
b7bb4f7d | 4129 | |
d3fddf6d PB |
4130 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4131 | return -EINVAL; | |
b9445598 | 4132 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 4133 | return -EINVAL; |
ec65fea5 | 4134 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 4135 | return -EBADF; |
03b1230c | 4136 | |
ec65fea5 PB |
4137 | /* open.how should be already initialised */ |
4138 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 4139 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 4140 | |
25e72d10 PB |
4141 | req->open.dfd = READ_ONCE(sqe->fd); |
4142 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 4143 | req->open.filename = getname(fname); |
15b71abe JA |
4144 | if (IS_ERR(req->open.filename)) { |
4145 | ret = PTR_ERR(req->open.filename); | |
4146 | req->open.filename = NULL; | |
4147 | return ret; | |
4148 | } | |
b9445598 PB |
4149 | |
4150 | req->open.file_slot = READ_ONCE(sqe->file_index); | |
4151 | if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) | |
4152 | return -EINVAL; | |
4153 | ||
4022e7af | 4154 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 4155 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 4156 | return 0; |
03b1230c JA |
4157 | } |
4158 | ||
ec65fea5 PB |
4159 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4160 | { | |
d3fddf6d PB |
4161 | u64 mode = READ_ONCE(sqe->len); |
4162 | u64 flags = READ_ONCE(sqe->open_flags); | |
ec65fea5 | 4163 | |
ec65fea5 PB |
4164 | req->open.how = build_open_how(flags, mode); |
4165 | return __io_openat_prep(req, sqe); | |
4166 | } | |
4167 | ||
cebdb986 | 4168 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 4169 | { |
cebdb986 | 4170 | struct open_how __user *how; |
cebdb986 | 4171 | size_t len; |
0fa03c62 JA |
4172 | int ret; |
4173 | ||
cebdb986 JA |
4174 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4175 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
4176 | if (len < OPEN_HOW_SIZE_VER0) |
4177 | return -EINVAL; | |
3529d8c2 | 4178 | |
cebdb986 JA |
4179 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
4180 | len); | |
4181 | if (ret) | |
4182 | return ret; | |
3529d8c2 | 4183 | |
ec65fea5 | 4184 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
4185 | } |
4186 | ||
45d189c6 | 4187 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
4188 | { |
4189 | struct open_flags op; | |
15b71abe | 4190 | struct file *file; |
b9445598 PB |
4191 | bool resolve_nonblock, nonblock_set; |
4192 | bool fixed = !!req->open.file_slot; | |
15b71abe JA |
4193 | int ret; |
4194 | ||
cebdb986 | 4195 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
4196 | if (ret) |
4197 | goto err; | |
3a81fd02 JA |
4198 | nonblock_set = op.open_flag & O_NONBLOCK; |
4199 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 4200 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
4201 | /* |
4202 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
4203 | * it'll always -EAGAIN | |
4204 | */ | |
4205 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
4206 | return -EAGAIN; | |
4207 | op.lookup_flags |= LOOKUP_CACHED; | |
4208 | op.open_flag |= O_NONBLOCK; | |
4209 | } | |
15b71abe | 4210 | |
b9445598 PB |
4211 | if (!fixed) { |
4212 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); | |
4213 | if (ret < 0) | |
4214 | goto err; | |
4215 | } | |
15b71abe JA |
4216 | |
4217 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
12dcb58a | 4218 | if (IS_ERR(file)) { |
944d1444 | 4219 | /* |
12dcb58a PB |
4220 | * We could hang on to this 'fd' on retrying, but seems like |
4221 | * marginal gain for something that is now known to be a slower | |
4222 | * path. So just put it, and we'll get a new one when we retry. | |
944d1444 | 4223 | */ |
b9445598 PB |
4224 | if (!fixed) |
4225 | put_unused_fd(ret); | |
3a81fd02 | 4226 | |
15b71abe | 4227 | ret = PTR_ERR(file); |
12dcb58a PB |
4228 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
4229 | if (ret == -EAGAIN && | |
4230 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) | |
4231 | return -EAGAIN; | |
4232 | goto err; | |
15b71abe | 4233 | } |
12dcb58a PB |
4234 | |
4235 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) | |
4236 | file->f_flags &= ~O_NONBLOCK; | |
4237 | fsnotify_open(file); | |
b9445598 PB |
4238 | |
4239 | if (!fixed) | |
4240 | fd_install(ret, file); | |
4241 | else | |
4242 | ret = io_install_fixed_file(req, file, issue_flags, | |
4243 | req->open.file_slot - 1); | |
15b71abe JA |
4244 | err: |
4245 | putname(req->open.filename); | |
8fef80bf | 4246 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe | 4247 | if (ret < 0) |
93d2bcd2 | 4248 | req_set_fail(req); |
0bdf3398 | 4249 | __io_req_complete(req, issue_flags, ret, 0); |
15b71abe JA |
4250 | return 0; |
4251 | } | |
4252 | ||
45d189c6 | 4253 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 4254 | { |
e45cff58 | 4255 | return io_openat2(req, issue_flags); |
cebdb986 JA |
4256 | } |
4257 | ||
067524e9 JA |
4258 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4259 | const struct io_uring_sqe *sqe) | |
4260 | { | |
4261 | struct io_provide_buf *p = &req->pbuf; | |
4262 | u64 tmp; | |
4263 | ||
26578cda PB |
4264 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
4265 | sqe->splice_fd_in) | |
067524e9 JA |
4266 | return -EINVAL; |
4267 | ||
4268 | tmp = READ_ONCE(sqe->fd); | |
4269 | if (!tmp || tmp > USHRT_MAX) | |
4270 | return -EINVAL; | |
4271 | ||
4272 | memset(p, 0, sizeof(*p)); | |
4273 | p->nbufs = tmp; | |
4274 | p->bgid = READ_ONCE(sqe->buf_group); | |
4275 | return 0; | |
4276 | } | |
4277 | ||
4278 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
4279 | int bgid, unsigned nbufs) | |
4280 | { | |
4281 | unsigned i = 0; | |
4282 | ||
4283 | /* shouldn't happen */ | |
4284 | if (!nbufs) | |
4285 | return 0; | |
4286 | ||
4287 | /* the head kbuf is the list itself */ | |
4288 | while (!list_empty(&buf->list)) { | |
4289 | struct io_buffer *nxt; | |
4290 | ||
4291 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
4292 | list_del(&nxt->list); | |
4293 | kfree(nxt); | |
4294 | if (++i == nbufs) | |
4295 | return i; | |
4296 | } | |
4297 | i++; | |
4298 | kfree(buf); | |
9e15c3a0 | 4299 | xa_erase(&ctx->io_buffers, bgid); |
067524e9 JA |
4300 | |
4301 | return i; | |
4302 | } | |
4303 | ||
889fca73 | 4304 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
4305 | { |
4306 | struct io_provide_buf *p = &req->pbuf; | |
4307 | struct io_ring_ctx *ctx = req->ctx; | |
4308 | struct io_buffer *head; | |
4309 | int ret = 0; | |
3b44b371 | 4310 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
067524e9 | 4311 | |
3b44b371 | 4312 | io_ring_submit_lock(ctx, needs_lock); |
067524e9 JA |
4313 | |
4314 | lockdep_assert_held(&ctx->uring_lock); | |
4315 | ||
4316 | ret = -ENOENT; | |
9e15c3a0 | 4317 | head = xa_load(&ctx->io_buffers, p->bgid); |
067524e9 JA |
4318 | if (head) |
4319 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 | 4320 | if (ret < 0) |
93d2bcd2 | 4321 | req_set_fail(req); |
067524e9 | 4322 | |
9fb8cb49 PB |
4323 | /* complete before unlock, IOPOLL may need the lock */ |
4324 | __io_req_complete(req, issue_flags, ret, 0); | |
3b44b371 | 4325 | io_ring_submit_unlock(ctx, needs_lock); |
067524e9 JA |
4326 | return 0; |
4327 | } | |
4328 | ||
ddf0322d JA |
4329 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4330 | const struct io_uring_sqe *sqe) | |
4331 | { | |
38134ada | 4332 | unsigned long size, tmp_check; |
ddf0322d JA |
4333 | struct io_provide_buf *p = &req->pbuf; |
4334 | u64 tmp; | |
4335 | ||
26578cda | 4336 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
ddf0322d JA |
4337 | return -EINVAL; |
4338 | ||
4339 | tmp = READ_ONCE(sqe->fd); | |
4340 | if (!tmp || tmp > USHRT_MAX) | |
4341 | return -E2BIG; | |
4342 | p->nbufs = tmp; | |
4343 | p->addr = READ_ONCE(sqe->addr); | |
4344 | p->len = READ_ONCE(sqe->len); | |
4345 | ||
38134ada PB |
4346 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
4347 | &size)) | |
4348 | return -EOVERFLOW; | |
4349 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) | |
4350 | return -EOVERFLOW; | |
4351 | ||
d81269fe PB |
4352 | size = (unsigned long)p->len * p->nbufs; |
4353 | if (!access_ok(u64_to_user_ptr(p->addr), size)) | |
ddf0322d JA |
4354 | return -EFAULT; |
4355 | ||
4356 | p->bgid = READ_ONCE(sqe->buf_group); | |
4357 | tmp = READ_ONCE(sqe->off); | |
4358 | if (tmp > USHRT_MAX) | |
4359 | return -E2BIG; | |
4360 | p->bid = tmp; | |
4361 | return 0; | |
4362 | } | |
4363 | ||
4364 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
4365 | { | |
4366 | struct io_buffer *buf; | |
4367 | u64 addr = pbuf->addr; | |
4368 | int i, bid = pbuf->bid; | |
4369 | ||
4370 | for (i = 0; i < pbuf->nbufs; i++) { | |
9990da93 | 4371 | buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT); |
ddf0322d JA |
4372 | if (!buf) |
4373 | break; | |
4374 | ||
4375 | buf->addr = addr; | |
d1f82808 | 4376 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
ddf0322d JA |
4377 | buf->bid = bid; |
4378 | addr += pbuf->len; | |
4379 | bid++; | |
4380 | if (!*head) { | |
4381 | INIT_LIST_HEAD(&buf->list); | |
4382 | *head = buf; | |
4383 | } else { | |
4384 | list_add_tail(&buf->list, &(*head)->list); | |
4385 | } | |
4386 | } | |
4387 | ||
4388 | return i ? i : -ENOMEM; | |
4389 | } | |
4390 | ||
889fca73 | 4391 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4392 | { |
4393 | struct io_provide_buf *p = &req->pbuf; | |
4394 | struct io_ring_ctx *ctx = req->ctx; | |
4395 | struct io_buffer *head, *list; | |
4396 | int ret = 0; | |
3b44b371 | 4397 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
ddf0322d | 4398 | |
3b44b371 | 4399 | io_ring_submit_lock(ctx, needs_lock); |
ddf0322d JA |
4400 | |
4401 | lockdep_assert_held(&ctx->uring_lock); | |
4402 | ||
9e15c3a0 | 4403 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
ddf0322d JA |
4404 | |
4405 | ret = io_add_buffers(p, &head); | |
9e15c3a0 JA |
4406 | if (ret >= 0 && !list) { |
4407 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); | |
4408 | if (ret < 0) | |
067524e9 | 4409 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d | 4410 | } |
ddf0322d | 4411 | if (ret < 0) |
93d2bcd2 | 4412 | req_set_fail(req); |
9fb8cb49 PB |
4413 | /* complete before unlock, IOPOLL may need the lock */ |
4414 | __io_req_complete(req, issue_flags, ret, 0); | |
3b44b371 | 4415 | io_ring_submit_unlock(ctx, needs_lock); |
ddf0322d | 4416 | return 0; |
cebdb986 JA |
4417 | } |
4418 | ||
3e4827b0 JA |
4419 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4420 | const struct io_uring_sqe *sqe) | |
4421 | { | |
4422 | #if defined(CONFIG_EPOLL) | |
26578cda | 4423 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
3e4827b0 | 4424 | return -EINVAL; |
2d74d042 | 4425 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4426 | return -EINVAL; |
3e4827b0 JA |
4427 | |
4428 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4429 | req->epoll.op = READ_ONCE(sqe->len); | |
4430 | req->epoll.fd = READ_ONCE(sqe->off); | |
4431 | ||
4432 | if (ep_op_has_event(req->epoll.op)) { | |
4433 | struct epoll_event __user *ev; | |
4434 | ||
4435 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4436 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4437 | return -EFAULT; | |
4438 | } | |
4439 | ||
4440 | return 0; | |
4441 | #else | |
4442 | return -EOPNOTSUPP; | |
4443 | #endif | |
4444 | } | |
4445 | ||
889fca73 | 4446 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4447 | { |
4448 | #if defined(CONFIG_EPOLL) | |
4449 | struct io_epoll *ie = &req->epoll; | |
4450 | int ret; | |
45d189c6 | 4451 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4452 | |
4453 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4454 | if (force_nonblock && ret == -EAGAIN) | |
4455 | return -EAGAIN; | |
4456 | ||
4457 | if (ret < 0) | |
93d2bcd2 | 4458 | req_set_fail(req); |
889fca73 | 4459 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4460 | return 0; |
4461 | #else | |
4462 | return -EOPNOTSUPP; | |
4463 | #endif | |
4464 | } | |
4465 | ||
c1ca757b JA |
4466 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4467 | { | |
4468 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
26578cda | 4469 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
c1ca757b | 4470 | return -EINVAL; |
3232dd02 PB |
4471 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4472 | return -EINVAL; | |
c1ca757b JA |
4473 | |
4474 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4475 | req->madvise.len = READ_ONCE(sqe->len); | |
4476 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4477 | return 0; | |
4478 | #else | |
4479 | return -EOPNOTSUPP; | |
4480 | #endif | |
4481 | } | |
4482 | ||
45d189c6 | 4483 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4484 | { |
4485 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4486 | struct io_madvise *ma = &req->madvise; | |
4487 | int ret; | |
4488 | ||
45d189c6 | 4489 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4490 | return -EAGAIN; |
4491 | ||
0726b01e | 4492 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b | 4493 | if (ret < 0) |
93d2bcd2 | 4494 | req_set_fail(req); |
e1e16097 | 4495 | io_req_complete(req, ret); |
c1ca757b JA |
4496 | return 0; |
4497 | #else | |
4498 | return -EOPNOTSUPP; | |
4499 | #endif | |
4500 | } | |
4501 | ||
4840e418 JA |
4502 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4503 | { | |
26578cda | 4504 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
4840e418 | 4505 | return -EINVAL; |
3232dd02 PB |
4506 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4507 | return -EINVAL; | |
4840e418 JA |
4508 | |
4509 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4510 | req->fadvise.len = READ_ONCE(sqe->len); | |
4511 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4512 | return 0; | |
4513 | } | |
4514 | ||
45d189c6 | 4515 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4516 | { |
4517 | struct io_fadvise *fa = &req->fadvise; | |
4518 | int ret; | |
4519 | ||
45d189c6 | 4520 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4521 | switch (fa->advice) { |
4522 | case POSIX_FADV_NORMAL: | |
4523 | case POSIX_FADV_RANDOM: | |
4524 | case POSIX_FADV_SEQUENTIAL: | |
4525 | break; | |
4526 | default: | |
4527 | return -EAGAIN; | |
4528 | } | |
4529 | } | |
4840e418 JA |
4530 | |
4531 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4532 | if (ret < 0) | |
93d2bcd2 | 4533 | req_set_fail(req); |
0bdf3398 | 4534 | __io_req_complete(req, issue_flags, ret, 0); |
4840e418 JA |
4535 | return 0; |
4536 | } | |
4537 | ||
eddc7ef5 JA |
4538 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4539 | { | |
2d74d042 | 4540 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4541 | return -EINVAL; |
26578cda | 4542 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
eddc7ef5 | 4543 | return -EINVAL; |
9c280f90 | 4544 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4545 | return -EBADF; |
eddc7ef5 | 4546 | |
1d9e1288 BM |
4547 | req->statx.dfd = READ_ONCE(sqe->fd); |
4548 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4549 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4550 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4551 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4552 | |
4553 | return 0; | |
4554 | } | |
4555 | ||
45d189c6 | 4556 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4557 | { |
1d9e1288 | 4558 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4559 | int ret; |
4560 | ||
59d70013 | 4561 | if (issue_flags & IO_URING_F_NONBLOCK) |
eddc7ef5 JA |
4562 | return -EAGAIN; |
4563 | ||
e62753e4 BM |
4564 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4565 | ctx->buffer); | |
eddc7ef5 | 4566 | |
eddc7ef5 | 4567 | if (ret < 0) |
93d2bcd2 | 4568 | req_set_fail(req); |
e1e16097 | 4569 | io_req_complete(req, ret); |
eddc7ef5 JA |
4570 | return 0; |
4571 | } | |
4572 | ||
b5dba59e JA |
4573 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4574 | { | |
14587a46 | 4575 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4576 | return -EINVAL; |
b5dba59e | 4577 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
7df778be | 4578 | sqe->rw_flags || sqe->buf_index) |
b5dba59e | 4579 | return -EINVAL; |
9c280f90 | 4580 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4581 | return -EBADF; |
b5dba59e JA |
4582 | |
4583 | req->close.fd = READ_ONCE(sqe->fd); | |
7df778be PB |
4584 | req->close.file_slot = READ_ONCE(sqe->file_index); |
4585 | if (req->close.file_slot && req->close.fd) | |
4586 | return -EINVAL; | |
4587 | ||
b5dba59e | 4588 | return 0; |
b5dba59e JA |
4589 | } |
4590 | ||
889fca73 | 4591 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4592 | { |
9eac1904 | 4593 | struct files_struct *files = current->files; |
3af73b28 | 4594 | struct io_close *close = &req->close; |
9eac1904 | 4595 | struct fdtable *fdt; |
a1fde923 PB |
4596 | struct file *file = NULL; |
4597 | int ret = -EBADF; | |
b5dba59e | 4598 | |
7df778be PB |
4599 | if (req->close.file_slot) { |
4600 | ret = io_close_fixed(req, issue_flags); | |
4601 | goto err; | |
4602 | } | |
4603 | ||
9eac1904 JA |
4604 | spin_lock(&files->file_lock); |
4605 | fdt = files_fdtable(files); | |
4606 | if (close->fd >= fdt->max_fds) { | |
4607 | spin_unlock(&files->file_lock); | |
4608 | goto err; | |
4609 | } | |
4610 | file = fdt->fd[close->fd]; | |
a1fde923 | 4611 | if (!file || file->f_op == &io_uring_fops) { |
9eac1904 JA |
4612 | spin_unlock(&files->file_lock); |
4613 | file = NULL; | |
4614 | goto err; | |
3af73b28 | 4615 | } |
b5dba59e JA |
4616 | |
4617 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 4618 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 4619 | spin_unlock(&files->file_lock); |
0bf0eefd | 4620 | return -EAGAIN; |
a2100672 | 4621 | } |
b5dba59e | 4622 | |
9eac1904 JA |
4623 | ret = __close_fd_get_file(close->fd, &file); |
4624 | spin_unlock(&files->file_lock); | |
4625 | if (ret < 0) { | |
4626 | if (ret == -ENOENT) | |
4627 | ret = -EBADF; | |
4628 | goto err; | |
4629 | } | |
4630 | ||
3af73b28 | 4631 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4632 | ret = filp_close(file, current->files); |
4633 | err: | |
3af73b28 | 4634 | if (ret < 0) |
93d2bcd2 | 4635 | req_set_fail(req); |
9eac1904 JA |
4636 | if (file) |
4637 | fput(file); | |
889fca73 | 4638 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 4639 | return 0; |
b5dba59e JA |
4640 | } |
4641 | ||
1155c76a | 4642 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4643 | { |
4644 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 4645 | |
5d17b4a4 JA |
4646 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4647 | return -EINVAL; | |
26578cda PB |
4648 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4649 | sqe->splice_fd_in)) | |
5d17b4a4 JA |
4650 | return -EINVAL; |
4651 | ||
8ed8d3c3 JA |
4652 | req->sync.off = READ_ONCE(sqe->off); |
4653 | req->sync.len = READ_ONCE(sqe->len); | |
4654 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4655 | return 0; |
4656 | } | |
4657 | ||
45d189c6 | 4658 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4659 | { |
8ed8d3c3 JA |
4660 | int ret; |
4661 | ||
ac45abc0 | 4662 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 4663 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4664 | return -EAGAIN; |
4665 | ||
9adbd45d | 4666 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4667 | req->sync.flags); |
4668 | if (ret < 0) | |
93d2bcd2 | 4669 | req_set_fail(req); |
e1e16097 | 4670 | io_req_complete(req, ret); |
5d17b4a4 JA |
4671 | return 0; |
4672 | } | |
4673 | ||
469956e8 | 4674 | #if defined(CONFIG_NET) |
02d27d89 PB |
4675 | static int io_setup_async_msg(struct io_kiocb *req, |
4676 | struct io_async_msghdr *kmsg) | |
4677 | { | |
e8c2bc1f JA |
4678 | struct io_async_msghdr *async_msg = req->async_data; |
4679 | ||
4680 | if (async_msg) | |
02d27d89 | 4681 | return -EAGAIN; |
e8c2bc1f | 4682 | if (io_alloc_async_data(req)) { |
257e84a5 | 4683 | kfree(kmsg->free_iov); |
02d27d89 PB |
4684 | return -ENOMEM; |
4685 | } | |
e8c2bc1f | 4686 | async_msg = req->async_data; |
02d27d89 | 4687 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4688 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 4689 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
4690 | /* if were using fast_iov, set it to the new one */ |
4691 | if (!async_msg->free_iov) | |
4692 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
4693 | ||
02d27d89 PB |
4694 | return -EAGAIN; |
4695 | } | |
4696 | ||
2ae523ed PB |
4697 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4698 | struct io_async_msghdr *iomsg) | |
4699 | { | |
2ae523ed | 4700 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 4701 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 4702 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 4703 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
4704 | } |
4705 | ||
93642ef8 PB |
4706 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
4707 | { | |
4708 | int ret; | |
4709 | ||
93642ef8 PB |
4710 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
4711 | if (!ret) | |
4712 | req->flags |= REQ_F_NEED_CLEANUP; | |
4713 | return ret; | |
4714 | } | |
4715 | ||
3529d8c2 | 4716 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4717 | { |
e47293fd | 4718 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 4719 | |
d2b6f48b PB |
4720 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4721 | return -EINVAL; | |
4722 | ||
270a5940 | 4723 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4724 | sr->len = READ_ONCE(sqe->len); |
04411806 PB |
4725 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4726 | if (sr->msg_flags & MSG_DONTWAIT) | |
4727 | req->flags |= REQ_F_NOWAIT; | |
3529d8c2 | 4728 | |
d8768362 JA |
4729 | #ifdef CONFIG_COMPAT |
4730 | if (req->ctx->compat) | |
4731 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4732 | #endif | |
93642ef8 | 4733 | return 0; |
03b1230c JA |
4734 | } |
4735 | ||
889fca73 | 4736 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4737 | { |
6b754c8b | 4738 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4739 | struct socket *sock; |
7a7cacba | 4740 | unsigned flags; |
0031275d | 4741 | int min_ret = 0; |
0fa03c62 JA |
4742 | int ret; |
4743 | ||
dba4a925 | 4744 | sock = sock_from_file(req->file); |
7a7cacba | 4745 | if (unlikely(!sock)) |
dba4a925 | 4746 | return -ENOTSOCK; |
3529d8c2 | 4747 | |
d886e185 PB |
4748 | if (req_has_async_data(req)) { |
4749 | kmsg = req->async_data; | |
4750 | } else { | |
7a7cacba PB |
4751 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
4752 | if (ret) | |
4753 | return ret; | |
4754 | kmsg = &iomsg; | |
0fa03c62 | 4755 | } |
0fa03c62 | 4756 | |
04411806 PB |
4757 | flags = req->sr_msg.msg_flags; |
4758 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4759 | flags |= MSG_DONTWAIT; |
0031275d SM |
4760 | if (flags & MSG_WAITALL) |
4761 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4762 | ||
7a7cacba | 4763 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
45d189c6 | 4764 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4765 | return io_setup_async_msg(req, kmsg); |
4766 | if (ret == -ERESTARTSYS) | |
4767 | ret = -EINTR; | |
0fa03c62 | 4768 | |
257e84a5 PB |
4769 | /* fast path, check for non-NULL to avoid function call */ |
4770 | if (kmsg->free_iov) | |
4771 | kfree(kmsg->free_iov); | |
99bc4c38 | 4772 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 4773 | if (ret < min_ret) |
93d2bcd2 | 4774 | req_set_fail(req); |
889fca73 | 4775 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 4776 | return 0; |
03b1230c | 4777 | } |
aa1fa28f | 4778 | |
889fca73 | 4779 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4780 | { |
7a7cacba PB |
4781 | struct io_sr_msg *sr = &req->sr_msg; |
4782 | struct msghdr msg; | |
4783 | struct iovec iov; | |
fddaface | 4784 | struct socket *sock; |
7a7cacba | 4785 | unsigned flags; |
0031275d | 4786 | int min_ret = 0; |
fddaface JA |
4787 | int ret; |
4788 | ||
dba4a925 | 4789 | sock = sock_from_file(req->file); |
7a7cacba | 4790 | if (unlikely(!sock)) |
dba4a925 | 4791 | return -ENOTSOCK; |
fddaface | 4792 | |
7a7cacba PB |
4793 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4794 | if (unlikely(ret)) | |
14db8411 | 4795 | return ret; |
fddaface | 4796 | |
7a7cacba PB |
4797 | msg.msg_name = NULL; |
4798 | msg.msg_control = NULL; | |
4799 | msg.msg_controllen = 0; | |
4800 | msg.msg_namelen = 0; | |
fddaface | 4801 | |
04411806 PB |
4802 | flags = req->sr_msg.msg_flags; |
4803 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4804 | flags |= MSG_DONTWAIT; |
0031275d SM |
4805 | if (flags & MSG_WAITALL) |
4806 | min_ret = iov_iter_count(&msg.msg_iter); | |
4807 | ||
7a7cacba PB |
4808 | msg.msg_flags = flags; |
4809 | ret = sock_sendmsg(sock, &msg); | |
45d189c6 | 4810 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4811 | return -EAGAIN; |
4812 | if (ret == -ERESTARTSYS) | |
4813 | ret = -EINTR; | |
fddaface | 4814 | |
0031275d | 4815 | if (ret < min_ret) |
93d2bcd2 | 4816 | req_set_fail(req); |
889fca73 | 4817 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 4818 | return 0; |
fddaface JA |
4819 | } |
4820 | ||
1400e697 PB |
4821 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4822 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4823 | { |
4824 | struct io_sr_msg *sr = &req->sr_msg; | |
4825 | struct iovec __user *uiov; | |
4826 | size_t iov_len; | |
4827 | int ret; | |
4828 | ||
1400e697 PB |
4829 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4830 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4831 | if (ret) |
4832 | return ret; | |
4833 | ||
4834 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4835 | if (iov_len > 1) | |
4836 | return -EINVAL; | |
5476dfed | 4837 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4838 | return -EFAULT; |
5476dfed | 4839 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 4840 | iomsg->free_iov = NULL; |
52de1fe1 | 4841 | } else { |
257e84a5 | 4842 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4843 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 4844 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 4845 | false); |
52de1fe1 JA |
4846 | if (ret > 0) |
4847 | ret = 0; | |
4848 | } | |
4849 | ||
4850 | return ret; | |
4851 | } | |
4852 | ||
4853 | #ifdef CONFIG_COMPAT | |
4854 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4855 | struct io_async_msghdr *iomsg) |
52de1fe1 | 4856 | { |
52de1fe1 JA |
4857 | struct io_sr_msg *sr = &req->sr_msg; |
4858 | struct compat_iovec __user *uiov; | |
4859 | compat_uptr_t ptr; | |
4860 | compat_size_t len; | |
4861 | int ret; | |
4862 | ||
4af3417a PB |
4863 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
4864 | &ptr, &len); | |
52de1fe1 JA |
4865 | if (ret) |
4866 | return ret; | |
4867 | ||
4868 | uiov = compat_ptr(ptr); | |
4869 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4870 | compat_ssize_t clen; | |
4871 | ||
4872 | if (len > 1) | |
4873 | return -EINVAL; | |
4874 | if (!access_ok(uiov, sizeof(*uiov))) | |
4875 | return -EFAULT; | |
4876 | if (__get_user(clen, &uiov->iov_len)) | |
4877 | return -EFAULT; | |
4878 | if (clen < 0) | |
4879 | return -EINVAL; | |
2d280bc8 | 4880 | sr->len = clen; |
257e84a5 | 4881 | iomsg->free_iov = NULL; |
52de1fe1 | 4882 | } else { |
257e84a5 | 4883 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4884 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 4885 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 4886 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
4887 | if (ret < 0) |
4888 | return ret; | |
4889 | } | |
4890 | ||
4891 | return 0; | |
4892 | } | |
4893 | #endif | |
4894 | ||
1400e697 PB |
4895 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4896 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4897 | { |
1400e697 | 4898 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
4899 | |
4900 | #ifdef CONFIG_COMPAT | |
4901 | if (req->ctx->compat) | |
1400e697 | 4902 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4903 | #endif |
52de1fe1 | 4904 | |
1400e697 | 4905 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4906 | } |
4907 | ||
bcda7baa | 4908 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
51aac424 | 4909 | unsigned int issue_flags) |
bcda7baa JA |
4910 | { |
4911 | struct io_sr_msg *sr = &req->sr_msg; | |
bcda7baa | 4912 | |
51aac424 | 4913 | return io_buffer_select(req, &sr->len, sr->bgid, issue_flags); |
fddaface JA |
4914 | } |
4915 | ||
7fbb1b54 PB |
4916 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4917 | { | |
30d51dd4 | 4918 | return io_put_kbuf(req, req->kbuf); |
7fbb1b54 PB |
4919 | } |
4920 | ||
93642ef8 | 4921 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 4922 | { |
99bc4c38 | 4923 | int ret; |
3529d8c2 | 4924 | |
93642ef8 PB |
4925 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
4926 | if (!ret) | |
4927 | req->flags |= REQ_F_NEED_CLEANUP; | |
4928 | return ret; | |
4929 | } | |
4930 | ||
4931 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4932 | { | |
4933 | struct io_sr_msg *sr = &req->sr_msg; | |
4934 | ||
d2b6f48b PB |
4935 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4936 | return -EINVAL; | |
4937 | ||
270a5940 | 4938 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4939 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4940 | sr->bgid = READ_ONCE(sqe->buf_group); |
04411806 PB |
4941 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4942 | if (sr->msg_flags & MSG_DONTWAIT) | |
4943 | req->flags |= REQ_F_NOWAIT; | |
06b76d44 | 4944 | |
d8768362 JA |
4945 | #ifdef CONFIG_COMPAT |
4946 | if (req->ctx->compat) | |
4947 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4948 | #endif | |
93642ef8 | 4949 | return 0; |
aa1fa28f JA |
4950 | } |
4951 | ||
889fca73 | 4952 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4953 | { |
6b754c8b | 4954 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4955 | struct socket *sock; |
7fbb1b54 | 4956 | struct io_buffer *kbuf; |
7a7cacba | 4957 | unsigned flags; |
0031275d | 4958 | int min_ret = 0; |
52de1fe1 | 4959 | int ret, cflags = 0; |
45d189c6 | 4960 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 4961 | |
dba4a925 | 4962 | sock = sock_from_file(req->file); |
7a7cacba | 4963 | if (unlikely(!sock)) |
dba4a925 | 4964 | return -ENOTSOCK; |
3529d8c2 | 4965 | |
d886e185 PB |
4966 | if (req_has_async_data(req)) { |
4967 | kmsg = req->async_data; | |
4968 | } else { | |
7a7cacba PB |
4969 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
4970 | if (ret) | |
681fda8d | 4971 | return ret; |
7a7cacba PB |
4972 | kmsg = &iomsg; |
4973 | } | |
03b1230c | 4974 | |
bc02ef33 | 4975 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 4976 | kbuf = io_recv_buffer_select(req, issue_flags); |
bc02ef33 | 4977 | if (IS_ERR(kbuf)) |
52de1fe1 | 4978 | return PTR_ERR(kbuf); |
7a7cacba | 4979 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
4980 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
4981 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
4982 | 1, req->sr_msg.len); |
4983 | } | |
52de1fe1 | 4984 | |
04411806 PB |
4985 | flags = req->sr_msg.msg_flags; |
4986 | if (force_nonblock) | |
7a7cacba | 4987 | flags |= MSG_DONTWAIT; |
0031275d SM |
4988 | if (flags & MSG_WAITALL) |
4989 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4990 | ||
7a7cacba PB |
4991 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4992 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4993 | if (force_nonblock && ret == -EAGAIN) |
4994 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4995 | if (ret == -ERESTARTSYS) |
4996 | ret = -EINTR; | |
03b1230c | 4997 | |
7fbb1b54 PB |
4998 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4999 | cflags = io_put_recv_kbuf(req); | |
257e84a5 PB |
5000 | /* fast path, check for non-NULL to avoid function call */ |
5001 | if (kmsg->free_iov) | |
5002 | kfree(kmsg->free_iov); | |
99bc4c38 | 5003 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 5004 | if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
93d2bcd2 | 5005 | req_set_fail(req); |
889fca73 | 5006 | __io_req_complete(req, issue_flags, ret, cflags); |
03b1230c | 5007 | return 0; |
0fa03c62 | 5008 | } |
5d17b4a4 | 5009 | |
889fca73 | 5010 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 5011 | { |
6b754c8b | 5012 | struct io_buffer *kbuf; |
7a7cacba PB |
5013 | struct io_sr_msg *sr = &req->sr_msg; |
5014 | struct msghdr msg; | |
5015 | void __user *buf = sr->buf; | |
fddaface | 5016 | struct socket *sock; |
7a7cacba PB |
5017 | struct iovec iov; |
5018 | unsigned flags; | |
0031275d | 5019 | int min_ret = 0; |
bcda7baa | 5020 | int ret, cflags = 0; |
45d189c6 | 5021 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 5022 | |
dba4a925 | 5023 | sock = sock_from_file(req->file); |
7a7cacba | 5024 | if (unlikely(!sock)) |
dba4a925 | 5025 | return -ENOTSOCK; |
fddaface | 5026 | |
bc02ef33 | 5027 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 5028 | kbuf = io_recv_buffer_select(req, issue_flags); |
bcda7baa JA |
5029 | if (IS_ERR(kbuf)) |
5030 | return PTR_ERR(kbuf); | |
7a7cacba | 5031 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 5032 | } |
bcda7baa | 5033 | |
7a7cacba | 5034 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
5035 | if (unlikely(ret)) |
5036 | goto out_free; | |
fddaface | 5037 | |
7a7cacba PB |
5038 | msg.msg_name = NULL; |
5039 | msg.msg_control = NULL; | |
5040 | msg.msg_controllen = 0; | |
5041 | msg.msg_namelen = 0; | |
5042 | msg.msg_iocb = NULL; | |
5043 | msg.msg_flags = 0; | |
fddaface | 5044 | |
04411806 PB |
5045 | flags = req->sr_msg.msg_flags; |
5046 | if (force_nonblock) | |
7a7cacba | 5047 | flags |= MSG_DONTWAIT; |
0031275d SM |
5048 | if (flags & MSG_WAITALL) |
5049 | min_ret = iov_iter_count(&msg.msg_iter); | |
5050 | ||
7a7cacba PB |
5051 | ret = sock_recvmsg(sock, &msg, flags); |
5052 | if (force_nonblock && ret == -EAGAIN) | |
5053 | return -EAGAIN; | |
5054 | if (ret == -ERESTARTSYS) | |
5055 | ret = -EINTR; | |
14c32eee | 5056 | out_free: |
7fbb1b54 PB |
5057 | if (req->flags & REQ_F_BUFFER_SELECTED) |
5058 | cflags = io_put_recv_kbuf(req); | |
0031275d | 5059 | if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
93d2bcd2 | 5060 | req_set_fail(req); |
889fca73 | 5061 | __io_req_complete(req, issue_flags, ret, cflags); |
fddaface | 5062 | return 0; |
fddaface JA |
5063 | } |
5064 | ||
3529d8c2 | 5065 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 5066 | { |
8ed8d3c3 JA |
5067 | struct io_accept *accept = &req->accept; |
5068 | ||
14587a46 | 5069 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 5070 | return -EINVAL; |
aaa4db12 | 5071 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
5072 | return -EINVAL; |
5073 | ||
d55e5f5b JA |
5074 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5075 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 5076 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 5077 | accept->nofile = rlimit(RLIMIT_NOFILE); |
a7083ad5 | 5078 | |
aaa4db12 PB |
5079 | accept->file_slot = READ_ONCE(sqe->file_index); |
5080 | if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) || | |
5081 | (accept->flags & SOCK_CLOEXEC))) | |
5082 | return -EINVAL; | |
a7083ad5 PB |
5083 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
5084 | return -EINVAL; | |
5085 | if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) | |
5086 | accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; | |
8ed8d3c3 | 5087 | return 0; |
8ed8d3c3 | 5088 | } |
17f2fe35 | 5089 | |
889fca73 | 5090 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
5091 | { |
5092 | struct io_accept *accept = &req->accept; | |
45d189c6 | 5093 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 5094 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
aaa4db12 | 5095 | bool fixed = !!accept->file_slot; |
a7083ad5 PB |
5096 | struct file *file; |
5097 | int ret, fd; | |
8ed8d3c3 | 5098 | |
e697deed JX |
5099 | if (req->file->f_flags & O_NONBLOCK) |
5100 | req->flags |= REQ_F_NOWAIT; | |
5101 | ||
aaa4db12 PB |
5102 | if (!fixed) { |
5103 | fd = __get_unused_fd_flags(accept->flags, accept->nofile); | |
5104 | if (unlikely(fd < 0)) | |
5105 | return fd; | |
5106 | } | |
a7083ad5 PB |
5107 | file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, |
5108 | accept->flags); | |
5109 | if (IS_ERR(file)) { | |
aaa4db12 PB |
5110 | if (!fixed) |
5111 | put_unused_fd(fd); | |
a7083ad5 PB |
5112 | ret = PTR_ERR(file); |
5113 | if (ret == -EAGAIN && force_nonblock) | |
5114 | return -EAGAIN; | |
ac45abc0 PB |
5115 | if (ret == -ERESTARTSYS) |
5116 | ret = -EINTR; | |
93d2bcd2 | 5117 | req_set_fail(req); |
aaa4db12 | 5118 | } else if (!fixed) { |
a7083ad5 PB |
5119 | fd_install(fd, file); |
5120 | ret = fd; | |
aaa4db12 PB |
5121 | } else { |
5122 | ret = io_install_fixed_file(req, file, issue_flags, | |
5123 | accept->file_slot - 1); | |
ac45abc0 | 5124 | } |
889fca73 | 5125 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 5126 | return 0; |
8ed8d3c3 JA |
5127 | } |
5128 | ||
93642ef8 PB |
5129 | static int io_connect_prep_async(struct io_kiocb *req) |
5130 | { | |
5131 | struct io_async_connect *io = req->async_data; | |
5132 | struct io_connect *conn = &req->connect; | |
5133 | ||
5134 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
5135 | } | |
5136 | ||
3529d8c2 | 5137 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 5138 | { |
3529d8c2 | 5139 | struct io_connect *conn = &req->connect; |
f499a021 | 5140 | |
14587a46 | 5141 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 | 5142 | return -EINVAL; |
26578cda PB |
5143 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
5144 | sqe->splice_fd_in) | |
3fbb51c1 JA |
5145 | return -EINVAL; |
5146 | ||
3529d8c2 JA |
5147 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5148 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 5149 | return 0; |
f499a021 JA |
5150 | } |
5151 | ||
889fca73 | 5152 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 5153 | { |
e8c2bc1f | 5154 | struct io_async_connect __io, *io; |
f8e85cf2 | 5155 | unsigned file_flags; |
3fbb51c1 | 5156 | int ret; |
45d189c6 | 5157 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 5158 | |
d886e185 | 5159 | if (req_has_async_data(req)) { |
e8c2bc1f | 5160 | io = req->async_data; |
f499a021 | 5161 | } else { |
3529d8c2 JA |
5162 | ret = move_addr_to_kernel(req->connect.addr, |
5163 | req->connect.addr_len, | |
e8c2bc1f | 5164 | &__io.address); |
f499a021 JA |
5165 | if (ret) |
5166 | goto out; | |
5167 | io = &__io; | |
5168 | } | |
5169 | ||
3fbb51c1 JA |
5170 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
5171 | ||
e8c2bc1f | 5172 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 5173 | req->connect.addr_len, file_flags); |
87f80d62 | 5174 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
d886e185 | 5175 | if (req_has_async_data(req)) |
b7bb4f7d | 5176 | return -EAGAIN; |
e8c2bc1f | 5177 | if (io_alloc_async_data(req)) { |
f499a021 JA |
5178 | ret = -ENOMEM; |
5179 | goto out; | |
5180 | } | |
e8c2bc1f | 5181 | memcpy(req->async_data, &__io, sizeof(__io)); |
f8e85cf2 | 5182 | return -EAGAIN; |
f499a021 | 5183 | } |
f8e85cf2 JA |
5184 | if (ret == -ERESTARTSYS) |
5185 | ret = -EINTR; | |
f499a021 | 5186 | out: |
4e88d6e7 | 5187 | if (ret < 0) |
93d2bcd2 | 5188 | req_set_fail(req); |
889fca73 | 5189 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 5190 | return 0; |
469956e8 Y |
5191 | } |
5192 | #else /* !CONFIG_NET */ | |
99a10081 JA |
5193 | #define IO_NETOP_FN(op) \ |
5194 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
5195 | { \ | |
5196 | return -EOPNOTSUPP; \ | |
5197 | } | |
5198 | ||
5199 | #define IO_NETOP_PREP(op) \ | |
5200 | IO_NETOP_FN(op) \ | |
5201 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
5202 | { \ | |
5203 | return -EOPNOTSUPP; \ | |
5204 | } \ | |
5205 | ||
5206 | #define IO_NETOP_PREP_ASYNC(op) \ | |
5207 | IO_NETOP_PREP(op) \ | |
5208 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
5209 | { \ | |
5210 | return -EOPNOTSUPP; \ | |
5211 | } | |
5212 | ||
5213 | IO_NETOP_PREP_ASYNC(sendmsg); | |
5214 | IO_NETOP_PREP_ASYNC(recvmsg); | |
5215 | IO_NETOP_PREP_ASYNC(connect); | |
5216 | IO_NETOP_PREP(accept); | |
5217 | IO_NETOP_FN(send); | |
5218 | IO_NETOP_FN(recv); | |
469956e8 | 5219 | #endif /* CONFIG_NET */ |
f8e85cf2 | 5220 | |
d7718a9d JA |
5221 | struct io_poll_table { |
5222 | struct poll_table_struct pt; | |
5223 | struct io_kiocb *req; | |
68b11e8b | 5224 | int nr_entries; |
d7718a9d JA |
5225 | int error; |
5226 | }; | |
ce593a6c | 5227 | |
d7718a9d | 5228 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
5b0a6acc | 5229 | __poll_t mask, io_req_tw_func_t func) |
d7718a9d | 5230 | { |
d7718a9d JA |
5231 | /* for instances that support it check for an event match first: */ |
5232 | if (mask && !(mask & poll->events)) | |
5233 | return 0; | |
5234 | ||
5235 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
5236 | ||
5237 | list_del_init(&poll->wait.entry); | |
5238 | ||
d7718a9d | 5239 | req->result = mask; |
5b0a6acc | 5240 | req->io_task_work.func = func; |
6d816e08 | 5241 | |
d7718a9d | 5242 | /* |
e3aabf95 JA |
5243 | * If this fails, then the task is exiting. When a task exits, the |
5244 | * work gets canceled, so just cancel this request as well instead | |
5245 | * of executing it. We can't safely execute it anyway, as we may not | |
5246 | * have the needed state needed for it anyway. | |
d7718a9d | 5247 | */ |
e09ee510 | 5248 | io_req_task_work_add(req); |
d7718a9d JA |
5249 | return 1; |
5250 | } | |
5251 | ||
74ce6ce4 JA |
5252 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
5253 | __acquires(&req->ctx->completion_lock) | |
5254 | { | |
5255 | struct io_ring_ctx *ctx = req->ctx; | |
5256 | ||
316319e8 | 5257 | /* req->task == current here, checking PF_EXITING is safe */ |
e09ee510 PB |
5258 | if (unlikely(req->task->flags & PF_EXITING)) |
5259 | WRITE_ONCE(poll->canceled, true); | |
5260 | ||
74ce6ce4 JA |
5261 | if (!req->result && !READ_ONCE(poll->canceled)) { |
5262 | struct poll_table_struct pt = { ._key = poll->events }; | |
5263 | ||
5264 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
5265 | } | |
5266 | ||
79ebeaee | 5267 | spin_lock(&ctx->completion_lock); |
74ce6ce4 JA |
5268 | if (!req->result && !READ_ONCE(poll->canceled)) { |
5269 | add_wait_queue(poll->head, &poll->wait); | |
5270 | return true; | |
5271 | } | |
5272 | ||
5273 | return false; | |
5274 | } | |
5275 | ||
d4e7cd36 | 5276 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5277 | { |
e8c2bc1f | 5278 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5279 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5280 | return req->async_data; |
d4e7cd36 JA |
5281 | return req->apoll->double_poll; |
5282 | } | |
5283 | ||
5284 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5285 | { | |
5286 | if (req->opcode == IORING_OP_POLL_ADD) | |
5287 | return &req->poll; | |
5288 | return &req->apoll->poll; | |
5289 | } | |
5290 | ||
5291 | static void io_poll_remove_double(struct io_kiocb *req) | |
e07785b0 | 5292 | __must_hold(&req->ctx->completion_lock) |
d4e7cd36 JA |
5293 | { |
5294 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
5295 | |
5296 | lockdep_assert_held(&req->ctx->completion_lock); | |
5297 | ||
5298 | if (poll && poll->head) { | |
5299 | struct wait_queue_head *head = poll->head; | |
5300 | ||
79ebeaee | 5301 | spin_lock_irq(&head->lock); |
18bceab1 JA |
5302 | list_del_init(&poll->wait.entry); |
5303 | if (poll->wait.private) | |
de9b4cca | 5304 | req_ref_put(req); |
18bceab1 | 5305 | poll->head = NULL; |
79ebeaee | 5306 | spin_unlock_irq(&head->lock); |
18bceab1 JA |
5307 | } |
5308 | } | |
5309 | ||
31efe48e | 5310 | static bool __io_poll_complete(struct io_kiocb *req, __poll_t mask) |
e07785b0 | 5311 | __must_hold(&req->ctx->completion_lock) |
18bceab1 JA |
5312 | { |
5313 | struct io_ring_ctx *ctx = req->ctx; | |
88e41cf9 | 5314 | unsigned flags = IORING_CQE_F_MORE; |
e27414be | 5315 | int error; |
18bceab1 | 5316 | |
e27414be | 5317 | if (READ_ONCE(req->poll.canceled)) { |
45ab03b1 | 5318 | error = -ECANCELED; |
88e41cf9 | 5319 | req->poll.events |= EPOLLONESHOT; |
e27414be | 5320 | } else { |
5082620f | 5321 | error = mangle_poll(mask); |
e27414be | 5322 | } |
b69de288 JA |
5323 | if (req->poll.events & EPOLLONESHOT) |
5324 | flags = 0; | |
a62682f9 HX |
5325 | if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) { |
5326 | req->poll.events |= EPOLLONESHOT; | |
88e41cf9 | 5327 | flags = 0; |
a62682f9 | 5328 | } |
7b289c38 HX |
5329 | if (flags & IORING_CQE_F_MORE) |
5330 | ctx->cq_extra++; | |
18bceab1 | 5331 | |
88e41cf9 | 5332 | return !(flags & IORING_CQE_F_MORE); |
18bceab1 JA |
5333 | } |
5334 | ||
f237c30a | 5335 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 JA |
5336 | { |
5337 | struct io_ring_ctx *ctx = req->ctx; | |
dd221f46 | 5338 | struct io_kiocb *nxt; |
18bceab1 JA |
5339 | |
5340 | if (io_poll_rewait(req, &req->poll)) { | |
79ebeaee | 5341 | spin_unlock(&ctx->completion_lock); |
dd221f46 | 5342 | } else { |
f40b964a | 5343 | bool done; |
18bceab1 | 5344 | |
5b7aa38d HX |
5345 | if (req->poll.done) { |
5346 | spin_unlock(&ctx->completion_lock); | |
5347 | return; | |
5348 | } | |
31efe48e | 5349 | done = __io_poll_complete(req, req->result); |
88e41cf9 | 5350 | if (done) { |
a890d01e | 5351 | io_poll_remove_double(req); |
88e41cf9 | 5352 | hash_del(&req->hash_node); |
bd99c71b | 5353 | req->poll.done = true; |
f40b964a | 5354 | } else { |
88e41cf9 JA |
5355 | req->result = 0; |
5356 | add_wait_queue(req->poll.head, &req->poll.wait); | |
5357 | } | |
31efe48e | 5358 | io_commit_cqring(ctx); |
79ebeaee | 5359 | spin_unlock(&ctx->completion_lock); |
dd221f46 | 5360 | io_cqring_ev_posted(ctx); |
18bceab1 | 5361 | |
88e41cf9 JA |
5362 | if (done) { |
5363 | nxt = io_put_req_find_next(req); | |
5364 | if (nxt) | |
f237c30a | 5365 | io_req_task_submit(nxt, locked); |
88e41cf9 | 5366 | } |
dd221f46 | 5367 | } |
18bceab1 JA |
5368 | } |
5369 | ||
5370 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
5371 | int sync, void *key) | |
5372 | { | |
5373 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 5374 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 | 5375 | __poll_t mask = key_to_poll(key); |
79ebeaee | 5376 | unsigned long flags; |
18bceab1 JA |
5377 | |
5378 | /* for instances that support it check for an event match first: */ | |
5379 | if (mask && !(mask & poll->events)) | |
5380 | return 0; | |
88e41cf9 JA |
5381 | if (!(poll->events & EPOLLONESHOT)) |
5382 | return poll->wait.func(&poll->wait, mode, sync, key); | |
18bceab1 | 5383 | |
8706e04e JA |
5384 | list_del_init(&wait->entry); |
5385 | ||
9ce85ef2 | 5386 | if (poll->head) { |
18bceab1 JA |
5387 | bool done; |
5388 | ||
79ebeaee | 5389 | spin_lock_irqsave(&poll->head->lock, flags); |
807abcb0 | 5390 | done = list_empty(&poll->wait.entry); |
18bceab1 | 5391 | if (!done) |
807abcb0 | 5392 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
5393 | /* make sure double remove sees this as being gone */ |
5394 | wait->private = NULL; | |
79ebeaee | 5395 | spin_unlock_irqrestore(&poll->head->lock, flags); |
c8b5e260 JA |
5396 | if (!done) { |
5397 | /* use wait func handler, so it matches the rq type */ | |
5398 | poll->wait.func(&poll->wait, mode, sync, key); | |
5399 | } | |
18bceab1 | 5400 | } |
de9b4cca | 5401 | req_ref_put(req); |
18bceab1 JA |
5402 | return 1; |
5403 | } | |
5404 | ||
5405 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
5406 | wait_queue_func_t wake_func) | |
5407 | { | |
5408 | poll->head = NULL; | |
5409 | poll->done = false; | |
5410 | poll->canceled = false; | |
464dca61 JA |
5411 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
5412 | /* mask in events that we always want/need */ | |
5413 | poll->events = events | IO_POLL_UNMASK; | |
18bceab1 JA |
5414 | INIT_LIST_HEAD(&poll->wait.entry); |
5415 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
5416 | } | |
5417 | ||
5418 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
5419 | struct wait_queue_head *head, |
5420 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
5421 | { |
5422 | struct io_kiocb *req = pt->req; | |
5423 | ||
5424 | /* | |
68b11e8b PB |
5425 | * The file being polled uses multiple waitqueues for poll handling |
5426 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb | |
5427 | * if this happens. | |
18bceab1 | 5428 | */ |
68b11e8b | 5429 | if (unlikely(pt->nr_entries)) { |
58852d4d PB |
5430 | struct io_poll_iocb *poll_one = poll; |
5431 | ||
23a65db8 PB |
5432 | /* double add on the same waitqueue head, ignore */ |
5433 | if (poll_one->head == head) | |
5434 | return; | |
18bceab1 | 5435 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5436 | if (*poll_ptr) { |
23a65db8 PB |
5437 | if ((*poll_ptr)->head == head) |
5438 | return; | |
18bceab1 JA |
5439 | pt->error = -EINVAL; |
5440 | return; | |
5441 | } | |
ea6a693d JA |
5442 | /* |
5443 | * Can't handle multishot for double wait for now, turn it | |
5444 | * into one-shot mode. | |
5445 | */ | |
7a274727 PB |
5446 | if (!(poll_one->events & EPOLLONESHOT)) |
5447 | poll_one->events |= EPOLLONESHOT; | |
18bceab1 JA |
5448 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
5449 | if (!poll) { | |
5450 | pt->error = -ENOMEM; | |
5451 | return; | |
5452 | } | |
58852d4d | 5453 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
de9b4cca | 5454 | req_ref_get(req); |
18bceab1 | 5455 | poll->wait.private = req; |
d886e185 | 5456 | |
807abcb0 | 5457 | *poll_ptr = poll; |
d886e185 PB |
5458 | if (req->opcode == IORING_OP_POLL_ADD) |
5459 | req->flags |= REQ_F_ASYNC_DATA; | |
18bceab1 JA |
5460 | } |
5461 | ||
68b11e8b | 5462 | pt->nr_entries++; |
18bceab1 | 5463 | poll->head = head; |
a31eb4a2 JX |
5464 | |
5465 | if (poll->events & EPOLLEXCLUSIVE) | |
5466 | add_wait_queue_exclusive(head, &poll->wait); | |
5467 | else | |
5468 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5469 | } |
5470 | ||
5471 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5472 | struct poll_table_struct *p) | |
5473 | { | |
5474 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5475 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5476 | |
807abcb0 | 5477 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5478 | } |
5479 | ||
f237c30a | 5480 | static void io_async_task_func(struct io_kiocb *req, bool *locked) |
d7718a9d | 5481 | { |
d7718a9d JA |
5482 | struct async_poll *apoll = req->apoll; |
5483 | struct io_ring_ctx *ctx = req->ctx; | |
5484 | ||
236daeae | 5485 | trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data); |
d7718a9d | 5486 | |
74ce6ce4 | 5487 | if (io_poll_rewait(req, &apoll->poll)) { |
79ebeaee | 5488 | spin_unlock(&ctx->completion_lock); |
74ce6ce4 | 5489 | return; |
d7718a9d JA |
5490 | } |
5491 | ||
0ea13b44 | 5492 | hash_del(&req->hash_node); |
d4e7cd36 | 5493 | io_poll_remove_double(req); |
bd99c71b | 5494 | apoll->poll.done = true; |
79ebeaee | 5495 | spin_unlock(&ctx->completion_lock); |
74ce6ce4 | 5496 | |
0be0b0e3 | 5497 | if (!READ_ONCE(apoll->poll.canceled)) |
f237c30a | 5498 | io_req_task_submit(req, locked); |
0be0b0e3 | 5499 | else |
2593553a | 5500 | io_req_complete_failed(req, -ECANCELED); |
d7718a9d JA |
5501 | } |
5502 | ||
5503 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5504 | void *key) | |
5505 | { | |
5506 | struct io_kiocb *req = wait->private; | |
5507 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5508 | ||
5509 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5510 | key_to_poll(key)); | |
5511 | ||
5512 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5513 | } | |
5514 | ||
5515 | static void io_poll_req_insert(struct io_kiocb *req) | |
5516 | { | |
5517 | struct io_ring_ctx *ctx = req->ctx; | |
5518 | struct hlist_head *list; | |
5519 | ||
5520 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5521 | hlist_add_head(&req->hash_node, list); | |
5522 | } | |
5523 | ||
5524 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5525 | struct io_poll_iocb *poll, | |
5526 | struct io_poll_table *ipt, __poll_t mask, | |
5527 | wait_queue_func_t wake_func) | |
5528 | __acquires(&ctx->completion_lock) | |
5529 | { | |
5530 | struct io_ring_ctx *ctx = req->ctx; | |
5531 | bool cancel = false; | |
5532 | ||
4d52f338 | 5533 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5534 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5535 | poll->file = req->file; |
18bceab1 | 5536 | poll->wait.private = req; |
d7718a9d JA |
5537 | |
5538 | ipt->pt._key = mask; | |
5539 | ipt->req = req; | |
68b11e8b PB |
5540 | ipt->error = 0; |
5541 | ipt->nr_entries = 0; | |
d7718a9d | 5542 | |
d7718a9d | 5543 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
68b11e8b PB |
5544 | if (unlikely(!ipt->nr_entries) && !ipt->error) |
5545 | ipt->error = -EINVAL; | |
d7718a9d | 5546 | |
79ebeaee | 5547 | spin_lock(&ctx->completion_lock); |
a890d01e | 5548 | if (ipt->error || (mask && (poll->events & EPOLLONESHOT))) |
46fee9ab | 5549 | io_poll_remove_double(req); |
d7718a9d | 5550 | if (likely(poll->head)) { |
79ebeaee | 5551 | spin_lock_irq(&poll->head->lock); |
d7718a9d JA |
5552 | if (unlikely(list_empty(&poll->wait.entry))) { |
5553 | if (ipt->error) | |
5554 | cancel = true; | |
5555 | ipt->error = 0; | |
5556 | mask = 0; | |
5557 | } | |
88e41cf9 | 5558 | if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error) |
d7718a9d JA |
5559 | list_del_init(&poll->wait.entry); |
5560 | else if (cancel) | |
5561 | WRITE_ONCE(poll->canceled, true); | |
5562 | else if (!poll->done) /* actually waiting for an event */ | |
5563 | io_poll_req_insert(req); | |
79ebeaee | 5564 | spin_unlock_irq(&poll->head->lock); |
d7718a9d JA |
5565 | } |
5566 | ||
5567 | return mask; | |
5568 | } | |
5569 | ||
59b735ae OL |
5570 | enum { |
5571 | IO_APOLL_OK, | |
5572 | IO_APOLL_ABORTED, | |
5573 | IO_APOLL_READY | |
5574 | }; | |
5575 | ||
5576 | static int io_arm_poll_handler(struct io_kiocb *req) | |
d7718a9d JA |
5577 | { |
5578 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5579 | struct io_ring_ctx *ctx = req->ctx; | |
5580 | struct async_poll *apoll; | |
5581 | struct io_poll_table ipt; | |
b2d9c3da | 5582 | __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI; |
d7718a9d | 5583 | |
b2d9c3da PB |
5584 | if (!def->pollin && !def->pollout) |
5585 | return IO_APOLL_ABORTED; | |
658d0a40 PB |
5586 | if (!file_can_poll(req->file) || (req->flags & REQ_F_POLLED)) |
5587 | return IO_APOLL_ABORTED; | |
b2d9c3da PB |
5588 | |
5589 | if (def->pollin) { | |
b2d9c3da PB |
5590 | mask |= POLLIN | POLLRDNORM; |
5591 | ||
5592 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5593 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5594 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5595 | mask &= ~POLLIN; | |
5596 | } else { | |
b2d9c3da PB |
5597 | mask |= POLLOUT | POLLWRNORM; |
5598 | } | |
5599 | ||
d7718a9d JA |
5600 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
5601 | if (unlikely(!apoll)) | |
59b735ae | 5602 | return IO_APOLL_ABORTED; |
807abcb0 | 5603 | apoll->double_poll = NULL; |
d7718a9d | 5604 | req->apoll = apoll; |
b2d9c3da | 5605 | req->flags |= REQ_F_POLLED; |
d7718a9d | 5606 | ipt.pt._qproc = io_async_queue_proc; |
48dcd38d | 5607 | io_req_set_refcount(req); |
d7718a9d JA |
5608 | |
5609 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5610 | io_async_wake); | |
79ebeaee | 5611 | spin_unlock(&ctx->completion_lock); |
41a5169c HX |
5612 | if (ret || ipt.error) |
5613 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; | |
5614 | ||
236daeae OL |
5615 | trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data, |
5616 | mask, apoll->poll.events); | |
59b735ae | 5617 | return IO_APOLL_OK; |
d7718a9d JA |
5618 | } |
5619 | ||
5620 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
b2e720ac | 5621 | struct io_poll_iocb *poll, bool do_cancel) |
e07785b0 | 5622 | __must_hold(&req->ctx->completion_lock) |
221c5eb2 | 5623 | { |
b41e9852 | 5624 | bool do_complete = false; |
221c5eb2 | 5625 | |
5082620f JA |
5626 | if (!poll->head) |
5627 | return false; | |
79ebeaee | 5628 | spin_lock_irq(&poll->head->lock); |
b2e720ac JA |
5629 | if (do_cancel) |
5630 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5631 | if (!list_empty(&poll->wait.entry)) { |
5632 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5633 | do_complete = true; |
221c5eb2 | 5634 | } |
79ebeaee | 5635 | spin_unlock_irq(&poll->head->lock); |
3bfa5bcb | 5636 | hash_del(&req->hash_node); |
d7718a9d JA |
5637 | return do_complete; |
5638 | } | |
5639 | ||
5d709043 | 5640 | static bool io_poll_remove_one(struct io_kiocb *req) |
e07785b0 | 5641 | __must_hold(&req->ctx->completion_lock) |
d7718a9d JA |
5642 | { |
5643 | bool do_complete; | |
5644 | ||
d4e7cd36 | 5645 | io_poll_remove_double(req); |
e31001a3 | 5646 | do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true); |
d4e7cd36 | 5647 | |
b41e9852 | 5648 | if (do_complete) { |
d4d19c19 | 5649 | io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0); |
b41e9852 | 5650 | io_commit_cqring(req->ctx); |
93d2bcd2 | 5651 | req_set_fail(req); |
91c2f697 | 5652 | io_put_req_deferred(req); |
5d709043 | 5653 | } |
b41e9852 | 5654 | return do_complete; |
221c5eb2 JA |
5655 | } |
5656 | ||
76e1b642 JA |
5657 | /* |
5658 | * Returns true if we found and killed one or more poll requests | |
5659 | */ | |
c072481d PB |
5660 | static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, |
5661 | struct task_struct *tsk, bool cancel_all) | |
221c5eb2 | 5662 | { |
78076bb6 | 5663 | struct hlist_node *tmp; |
221c5eb2 | 5664 | struct io_kiocb *req; |
8e2e1faf | 5665 | int posted = 0, i; |
221c5eb2 | 5666 | |
79ebeaee | 5667 | spin_lock(&ctx->completion_lock); |
78076bb6 JA |
5668 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5669 | struct hlist_head *list; | |
5670 | ||
5671 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5672 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
3dd0c97a | 5673 | if (io_match_task(req, tsk, cancel_all)) |
f3606e3a JA |
5674 | posted += io_poll_remove_one(req); |
5675 | } | |
221c5eb2 | 5676 | } |
79ebeaee | 5677 | spin_unlock(&ctx->completion_lock); |
b41e9852 | 5678 | |
8e2e1faf JA |
5679 | if (posted) |
5680 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5681 | |
5682 | return posted != 0; | |
221c5eb2 JA |
5683 | } |
5684 | ||
9ba5fac8 PB |
5685 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5686 | bool poll_only) | |
e07785b0 | 5687 | __must_hold(&ctx->completion_lock) |
47f46768 | 5688 | { |
78076bb6 | 5689 | struct hlist_head *list; |
47f46768 JA |
5690 | struct io_kiocb *req; |
5691 | ||
78076bb6 JA |
5692 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5693 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5694 | if (sqe_addr != req->user_data) |
5695 | continue; | |
9ba5fac8 PB |
5696 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
5697 | continue; | |
b2cb805f | 5698 | return req; |
47f46768 | 5699 | } |
b2cb805f JA |
5700 | return NULL; |
5701 | } | |
5702 | ||
9ba5fac8 PB |
5703 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5704 | bool poll_only) | |
e07785b0 | 5705 | __must_hold(&ctx->completion_lock) |
b2cb805f JA |
5706 | { |
5707 | struct io_kiocb *req; | |
5708 | ||
9ba5fac8 | 5709 | req = io_poll_find(ctx, sqe_addr, poll_only); |
b2cb805f JA |
5710 | if (!req) |
5711 | return -ENOENT; | |
5712 | if (io_poll_remove_one(req)) | |
5713 | return 0; | |
5714 | ||
5715 | return -EALREADY; | |
47f46768 JA |
5716 | } |
5717 | ||
9096af3e PB |
5718 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
5719 | unsigned int flags) | |
5720 | { | |
5721 | u32 events; | |
47f46768 | 5722 | |
9096af3e PB |
5723 | events = READ_ONCE(sqe->poll32_events); |
5724 | #ifdef __BIG_ENDIAN | |
5725 | events = swahw32(events); | |
5726 | #endif | |
5727 | if (!(flags & IORING_POLL_ADD_MULTI)) | |
5728 | events |= EPOLLONESHOT; | |
5729 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); | |
47f46768 JA |
5730 | } |
5731 | ||
c5de0036 | 5732 | static int io_poll_update_prep(struct io_kiocb *req, |
3529d8c2 | 5733 | const struct io_uring_sqe *sqe) |
0969e783 | 5734 | { |
c5de0036 PB |
5735 | struct io_poll_update *upd = &req->poll_update; |
5736 | u32 flags; | |
5737 | ||
0969e783 JA |
5738 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5739 | return -EINVAL; | |
26578cda | 5740 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
c5de0036 PB |
5741 | return -EINVAL; |
5742 | flags = READ_ONCE(sqe->len); | |
5743 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | | |
5744 | IORING_POLL_ADD_MULTI)) | |
5745 | return -EINVAL; | |
5746 | /* meaningless without update */ | |
5747 | if (flags == IORING_POLL_ADD_MULTI) | |
0969e783 JA |
5748 | return -EINVAL; |
5749 | ||
c5de0036 PB |
5750 | upd->old_user_data = READ_ONCE(sqe->addr); |
5751 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; | |
5752 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; | |
221c5eb2 | 5753 | |
c5de0036 PB |
5754 | upd->new_user_data = READ_ONCE(sqe->off); |
5755 | if (!upd->update_user_data && upd->new_user_data) | |
5756 | return -EINVAL; | |
5757 | if (upd->update_events) | |
5758 | upd->events = io_poll_parse_events(sqe, flags); | |
5759 | else if (sqe->poll32_events) | |
5760 | return -EINVAL; | |
221c5eb2 | 5761 | |
221c5eb2 JA |
5762 | return 0; |
5763 | } | |
5764 | ||
221c5eb2 JA |
5765 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5766 | void *key) | |
5767 | { | |
c2f2eb7d JA |
5768 | struct io_kiocb *req = wait->private; |
5769 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5770 | |
d7718a9d | 5771 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5772 | } |
5773 | ||
221c5eb2 JA |
5774 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5775 | struct poll_table_struct *p) | |
5776 | { | |
5777 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5778 | ||
e8c2bc1f | 5779 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5780 | } |
5781 | ||
3529d8c2 | 5782 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5783 | { |
5784 | struct io_poll_iocb *poll = &req->poll; | |
c5de0036 | 5785 | u32 flags; |
221c5eb2 JA |
5786 | |
5787 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5788 | return -EINVAL; | |
c5de0036 | 5789 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
88e41cf9 JA |
5790 | return -EINVAL; |
5791 | flags = READ_ONCE(sqe->len); | |
c5de0036 | 5792 | if (flags & ~IORING_POLL_ADD_MULTI) |
221c5eb2 JA |
5793 | return -EINVAL; |
5794 | ||
48dcd38d | 5795 | io_req_set_refcount(req); |
c5de0036 | 5796 | poll->events = io_poll_parse_events(sqe, flags); |
0969e783 JA |
5797 | return 0; |
5798 | } | |
5799 | ||
61e98203 | 5800 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
5801 | { |
5802 | struct io_poll_iocb *poll = &req->poll; | |
5803 | struct io_ring_ctx *ctx = req->ctx; | |
5804 | struct io_poll_table ipt; | |
0969e783 | 5805 | __poll_t mask; |
5b7aa38d | 5806 | bool done; |
0969e783 | 5807 | |
d7718a9d | 5808 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5809 | |
d7718a9d JA |
5810 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5811 | io_poll_wake); | |
221c5eb2 | 5812 | |
8c838788 | 5813 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5814 | ipt.error = 0; |
eb6e6f06 PB |
5815 | done = __io_poll_complete(req, mask); |
5816 | io_commit_cqring(req->ctx); | |
221c5eb2 | 5817 | } |
79ebeaee | 5818 | spin_unlock(&ctx->completion_lock); |
221c5eb2 | 5819 | |
8c838788 JA |
5820 | if (mask) { |
5821 | io_cqring_ev_posted(ctx); | |
5b7aa38d | 5822 | if (done) |
88e41cf9 | 5823 | io_put_req(req); |
221c5eb2 | 5824 | } |
8c838788 | 5825 | return ipt.error; |
221c5eb2 JA |
5826 | } |
5827 | ||
c5de0036 | 5828 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
b69de288 JA |
5829 | { |
5830 | struct io_ring_ctx *ctx = req->ctx; | |
5831 | struct io_kiocb *preq; | |
cb3b200e | 5832 | bool completing; |
b69de288 JA |
5833 | int ret; |
5834 | ||
79ebeaee | 5835 | spin_lock(&ctx->completion_lock); |
9ba5fac8 | 5836 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
b69de288 JA |
5837 | if (!preq) { |
5838 | ret = -ENOENT; | |
5839 | goto err; | |
b69de288 | 5840 | } |
cb3b200e | 5841 | |
c5de0036 PB |
5842 | if (!req->poll_update.update_events && !req->poll_update.update_user_data) { |
5843 | completing = true; | |
5844 | ret = io_poll_remove_one(preq) ? 0 : -EALREADY; | |
5845 | goto err; | |
5846 | } | |
5847 | ||
cb3b200e JA |
5848 | /* |
5849 | * Don't allow racy completion with singleshot, as we cannot safely | |
5850 | * update those. For multishot, if we're racing with completion, just | |
5851 | * let completion re-add it. | |
5852 | */ | |
5853 | completing = !__io_poll_remove_one(preq, &preq->poll, false); | |
5854 | if (completing && (preq->poll.events & EPOLLONESHOT)) { | |
5855 | ret = -EALREADY; | |
5856 | goto err; | |
b69de288 JA |
5857 | } |
5858 | /* we now have a detached poll request. reissue. */ | |
5859 | ret = 0; | |
5860 | err: | |
b69de288 | 5861 | if (ret < 0) { |
79ebeaee | 5862 | spin_unlock(&ctx->completion_lock); |
93d2bcd2 | 5863 | req_set_fail(req); |
b69de288 JA |
5864 | io_req_complete(req, ret); |
5865 | return 0; | |
5866 | } | |
5867 | /* only mask one event flags, keep behavior flags */ | |
9d805892 | 5868 | if (req->poll_update.update_events) { |
b69de288 | 5869 | preq->poll.events &= ~0xffff; |
9d805892 | 5870 | preq->poll.events |= req->poll_update.events & 0xffff; |
b69de288 JA |
5871 | preq->poll.events |= IO_POLL_UNMASK; |
5872 | } | |
9d805892 PB |
5873 | if (req->poll_update.update_user_data) |
5874 | preq->user_data = req->poll_update.new_user_data; | |
79ebeaee | 5875 | spin_unlock(&ctx->completion_lock); |
cb3b200e | 5876 | |
b69de288 JA |
5877 | /* complete update request, we're done with it */ |
5878 | io_req_complete(req, ret); | |
5879 | ||
cb3b200e | 5880 | if (!completing) { |
c5de0036 | 5881 | ret = io_poll_add(preq, issue_flags); |
cb3b200e | 5882 | if (ret < 0) { |
93d2bcd2 | 5883 | req_set_fail(preq); |
cb3b200e JA |
5884 | io_req_complete(preq, ret); |
5885 | } | |
b69de288 JA |
5886 | } |
5887 | return 0; | |
5888 | } | |
5889 | ||
f237c30a | 5890 | static void io_req_task_timeout(struct io_kiocb *req, bool *locked) |
89850fce | 5891 | { |
6224590d PB |
5892 | struct io_timeout_data *data = req->async_data; |
5893 | ||
5894 | if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS)) | |
5895 | req_set_fail(req); | |
505657bc | 5896 | io_req_complete_post(req, -ETIME, 0); |
89850fce JA |
5897 | } |
5898 | ||
5262f567 JA |
5899 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5900 | { | |
ad8a48ac JA |
5901 | struct io_timeout_data *data = container_of(timer, |
5902 | struct io_timeout_data, timer); | |
5903 | struct io_kiocb *req = data->req; | |
5904 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5905 | unsigned long flags; |
5906 | ||
89850fce | 5907 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
a71976f3 | 5908 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5909 | atomic_set(&req->ctx->cq_timeouts, |
5910 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
89850fce | 5911 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
01cec8c1 | 5912 | |
89850fce JA |
5913 | req->io_task_work.func = io_req_task_timeout; |
5914 | io_req_task_work_add(req); | |
5262f567 JA |
5915 | return HRTIMER_NORESTART; |
5916 | } | |
5917 | ||
fbd15848 PB |
5918 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5919 | __u64 user_data) | |
89850fce | 5920 | __must_hold(&ctx->timeout_lock) |
f254ac04 | 5921 | { |
fbd15848 | 5922 | struct io_timeout_data *io; |
47f46768 | 5923 | struct io_kiocb *req; |
fd9c7bc5 | 5924 | bool found = false; |
f254ac04 | 5925 | |
135fcde8 | 5926 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
fd9c7bc5 PB |
5927 | found = user_data == req->user_data; |
5928 | if (found) | |
47f46768 | 5929 | break; |
47f46768 | 5930 | } |
fd9c7bc5 PB |
5931 | if (!found) |
5932 | return ERR_PTR(-ENOENT); | |
fbd15848 PB |
5933 | |
5934 | io = req->async_data; | |
fd9c7bc5 | 5935 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
fbd15848 | 5936 | return ERR_PTR(-EALREADY); |
a71976f3 | 5937 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5938 | return req; |
5939 | } | |
47f46768 | 5940 | |
fbd15848 | 5941 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
ec3c3d0f | 5942 | __must_hold(&ctx->completion_lock) |
89850fce | 5943 | __must_hold(&ctx->timeout_lock) |
fbd15848 PB |
5944 | { |
5945 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5946 | ||
5947 | if (IS_ERR(req)) | |
5948 | return PTR_ERR(req); | |
f254ac04 | 5949 | |
93d2bcd2 | 5950 | req_set_fail(req); |
d4d19c19 | 5951 | io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0); |
91c2f697 | 5952 | io_put_req_deferred(req); |
f254ac04 JA |
5953 | return 0; |
5954 | } | |
5955 | ||
50c1df2b JA |
5956 | static clockid_t io_timeout_get_clock(struct io_timeout_data *data) |
5957 | { | |
5958 | switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) { | |
5959 | case IORING_TIMEOUT_BOOTTIME: | |
5960 | return CLOCK_BOOTTIME; | |
5961 | case IORING_TIMEOUT_REALTIME: | |
5962 | return CLOCK_REALTIME; | |
5963 | default: | |
5964 | /* can't happen, vetted at prep time */ | |
5965 | WARN_ON_ONCE(1); | |
5966 | fallthrough; | |
5967 | case 0: | |
5968 | return CLOCK_MONOTONIC; | |
5969 | } | |
5970 | } | |
5971 | ||
f1042b6c PB |
5972 | static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5973 | struct timespec64 *ts, enum hrtimer_mode mode) | |
5974 | __must_hold(&ctx->timeout_lock) | |
5975 | { | |
5976 | struct io_timeout_data *io; | |
5977 | struct io_kiocb *req; | |
5978 | bool found = false; | |
5979 | ||
5980 | list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) { | |
5981 | found = user_data == req->user_data; | |
5982 | if (found) | |
5983 | break; | |
5984 | } | |
5985 | if (!found) | |
5986 | return -ENOENT; | |
5987 | ||
5988 | io = req->async_data; | |
5989 | if (hrtimer_try_to_cancel(&io->timer) == -1) | |
5990 | return -EALREADY; | |
5991 | hrtimer_init(&io->timer, io_timeout_get_clock(io), mode); | |
5992 | io->timer.function = io_link_timeout_fn; | |
5993 | hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode); | |
5994 | return 0; | |
5995 | } | |
5996 | ||
9c8e11b3 PB |
5997 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5998 | struct timespec64 *ts, enum hrtimer_mode mode) | |
89850fce | 5999 | __must_hold(&ctx->timeout_lock) |
47f46768 | 6000 | { |
9c8e11b3 PB |
6001 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
6002 | struct io_timeout_data *data; | |
47f46768 | 6003 | |
9c8e11b3 PB |
6004 | if (IS_ERR(req)) |
6005 | return PTR_ERR(req); | |
47f46768 | 6006 | |
9c8e11b3 PB |
6007 | req->timeout.off = 0; /* noseq */ |
6008 | data = req->async_data; | |
6009 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
50c1df2b | 6010 | hrtimer_init(&data->timer, io_timeout_get_clock(data), mode); |
9c8e11b3 PB |
6011 | data->timer.function = io_timeout_fn; |
6012 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
6013 | return 0; | |
47f46768 JA |
6014 | } |
6015 | ||
3529d8c2 JA |
6016 | static int io_timeout_remove_prep(struct io_kiocb *req, |
6017 | const struct io_uring_sqe *sqe) | |
b29472ee | 6018 | { |
9c8e11b3 PB |
6019 | struct io_timeout_rem *tr = &req->timeout_rem; |
6020 | ||
b29472ee JA |
6021 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
6022 | return -EINVAL; | |
61710e43 DA |
6023 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6024 | return -EINVAL; | |
26578cda | 6025 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
b29472ee JA |
6026 | return -EINVAL; |
6027 | ||
f1042b6c | 6028 | tr->ltimeout = false; |
9c8e11b3 PB |
6029 | tr->addr = READ_ONCE(sqe->addr); |
6030 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
f1042b6c PB |
6031 | if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) { |
6032 | if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
6033 | return -EINVAL; | |
6034 | if (tr->flags & IORING_LINK_TIMEOUT_UPDATE) | |
6035 | tr->ltimeout = true; | |
6036 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS)) | |
9c8e11b3 PB |
6037 | return -EINVAL; |
6038 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
6039 | return -EFAULT; | |
6040 | } else if (tr->flags) { | |
6041 | /* timeout removal doesn't support flags */ | |
b29472ee | 6042 | return -EINVAL; |
9c8e11b3 | 6043 | } |
b29472ee | 6044 | |
b29472ee JA |
6045 | return 0; |
6046 | } | |
6047 | ||
8662daec PB |
6048 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
6049 | { | |
6050 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
6051 | : HRTIMER_MODE_REL; | |
6052 | } | |
6053 | ||
11365043 JA |
6054 | /* |
6055 | * Remove or update an existing timeout command | |
6056 | */ | |
61e98203 | 6057 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 6058 | { |
9c8e11b3 | 6059 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 6060 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 6061 | int ret; |
11365043 | 6062 | |
ec3c3d0f PB |
6063 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
6064 | spin_lock(&ctx->completion_lock); | |
6065 | spin_lock_irq(&ctx->timeout_lock); | |
9c8e11b3 | 6066 | ret = io_timeout_cancel(ctx, tr->addr); |
ec3c3d0f PB |
6067 | spin_unlock_irq(&ctx->timeout_lock); |
6068 | spin_unlock(&ctx->completion_lock); | |
6069 | } else { | |
f1042b6c PB |
6070 | enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags); |
6071 | ||
ec3c3d0f | 6072 | spin_lock_irq(&ctx->timeout_lock); |
f1042b6c PB |
6073 | if (tr->ltimeout) |
6074 | ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
6075 | else | |
6076 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
ec3c3d0f PB |
6077 | spin_unlock_irq(&ctx->timeout_lock); |
6078 | } | |
11365043 | 6079 | |
4e88d6e7 | 6080 | if (ret < 0) |
93d2bcd2 | 6081 | req_set_fail(req); |
505657bc | 6082 | io_req_complete_post(req, ret, 0); |
11365043 | 6083 | return 0; |
5262f567 JA |
6084 | } |
6085 | ||
3529d8c2 | 6086 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 6087 | bool is_timeout_link) |
5262f567 | 6088 | { |
ad8a48ac | 6089 | struct io_timeout_data *data; |
a41525ab | 6090 | unsigned flags; |
56080b02 | 6091 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 6092 | |
ad8a48ac | 6093 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 6094 | return -EINVAL; |
26578cda PB |
6095 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
6096 | sqe->splice_fd_in) | |
a41525ab | 6097 | return -EINVAL; |
56080b02 | 6098 | if (off && is_timeout_link) |
2d28390a | 6099 | return -EINVAL; |
a41525ab | 6100 | flags = READ_ONCE(sqe->timeout_flags); |
6224590d PB |
6101 | if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK | |
6102 | IORING_TIMEOUT_ETIME_SUCCESS)) | |
50c1df2b JA |
6103 | return -EINVAL; |
6104 | /* more than one clock specified is invalid, obviously */ | |
6105 | if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
5262f567 | 6106 | return -EINVAL; |
bdf20073 | 6107 | |
ef9dd637 | 6108 | INIT_LIST_HEAD(&req->timeout.list); |
bfe68a22 | 6109 | req->timeout.off = off; |
f18ee4cf PB |
6110 | if (unlikely(off && !req->ctx->off_timeout_used)) |
6111 | req->ctx->off_timeout_used = true; | |
26a61679 | 6112 | |
d6a644a7 PB |
6113 | if (WARN_ON_ONCE(req_has_async_data(req))) |
6114 | return -EFAULT; | |
6115 | if (io_alloc_async_data(req)) | |
26a61679 JA |
6116 | return -ENOMEM; |
6117 | ||
e8c2bc1f | 6118 | data = req->async_data; |
ad8a48ac | 6119 | data->req = req; |
50c1df2b | 6120 | data->flags = flags; |
ad8a48ac JA |
6121 | |
6122 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
6123 | return -EFAULT; |
6124 | ||
8662daec | 6125 | data->mode = io_translate_timeout_mode(flags); |
50c1df2b | 6126 | hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode); |
b97e736a PB |
6127 | |
6128 | if (is_timeout_link) { | |
6129 | struct io_submit_link *link = &req->ctx->submit_state.link; | |
6130 | ||
6131 | if (!link->head) | |
6132 | return -EINVAL; | |
6133 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) | |
6134 | return -EINVAL; | |
4d13d1a4 PB |
6135 | req->timeout.head = link->last; |
6136 | link->last->flags |= REQ_F_ARM_LTIMEOUT; | |
b97e736a | 6137 | } |
ad8a48ac JA |
6138 | return 0; |
6139 | } | |
6140 | ||
61e98203 | 6141 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 6142 | { |
ad8a48ac | 6143 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 6144 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 6145 | struct list_head *entry; |
bfe68a22 | 6146 | u32 tail, off = req->timeout.off; |
ad8a48ac | 6147 | |
89850fce | 6148 | spin_lock_irq(&ctx->timeout_lock); |
93bd25bb | 6149 | |
5262f567 JA |
6150 | /* |
6151 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
6152 | * timeout event to be satisfied. If it isn't set, then this is |
6153 | * a pure timeout request, sequence isn't used. | |
5262f567 | 6154 | */ |
8eb7e2d0 | 6155 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
6156 | entry = ctx->timeout_list.prev; |
6157 | goto add; | |
6158 | } | |
5262f567 | 6159 | |
bfe68a22 PB |
6160 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
6161 | req->timeout.target_seq = tail + off; | |
5262f567 | 6162 | |
f010505b MDG |
6163 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
6164 | * This is safe because ->completion_lock is held, and submissions | |
6165 | * and completions are never mixed in the same ->completion_lock section. | |
6166 | */ | |
6167 | ctx->cq_last_tm_flush = tail; | |
6168 | ||
5262f567 JA |
6169 | /* |
6170 | * Insertion sort, ensuring the first entry in the list is always | |
6171 | * the one we need first. | |
6172 | */ | |
5262f567 | 6173 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
6174 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
6175 | timeout.list); | |
5262f567 | 6176 | |
8eb7e2d0 | 6177 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 6178 | continue; |
bfe68a22 PB |
6179 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
6180 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
6181 | break; |
6182 | } | |
93bd25bb | 6183 | add: |
135fcde8 | 6184 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
6185 | data->timer.function = io_timeout_fn; |
6186 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
89850fce | 6187 | spin_unlock_irq(&ctx->timeout_lock); |
5262f567 JA |
6188 | return 0; |
6189 | } | |
5262f567 | 6190 | |
f458dd84 PB |
6191 | struct io_cancel_data { |
6192 | struct io_ring_ctx *ctx; | |
6193 | u64 user_data; | |
6194 | }; | |
6195 | ||
62755e35 JA |
6196 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
6197 | { | |
6198 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 6199 | struct io_cancel_data *cd = data; |
62755e35 | 6200 | |
f458dd84 | 6201 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
62755e35 JA |
6202 | } |
6203 | ||
f458dd84 PB |
6204 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
6205 | struct io_ring_ctx *ctx) | |
62755e35 | 6206 | { |
f458dd84 | 6207 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
62755e35 | 6208 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
6209 | int ret = 0; |
6210 | ||
f458dd84 | 6211 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
6212 | return -ENOENT; |
6213 | ||
f458dd84 | 6214 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
62755e35 JA |
6215 | switch (cancel_ret) { |
6216 | case IO_WQ_CANCEL_OK: | |
6217 | ret = 0; | |
6218 | break; | |
6219 | case IO_WQ_CANCEL_RUNNING: | |
6220 | ret = -EALREADY; | |
6221 | break; | |
6222 | case IO_WQ_CANCEL_NOTFOUND: | |
6223 | ret = -ENOENT; | |
6224 | break; | |
6225 | } | |
6226 | ||
e977d6d3 JA |
6227 | return ret; |
6228 | } | |
6229 | ||
8cb01fac | 6230 | static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr) |
47f46768 | 6231 | { |
8cb01fac | 6232 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 JA |
6233 | int ret; |
6234 | ||
dadebc35 | 6235 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
8cb01fac | 6236 | |
f458dd84 | 6237 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
df9727af | 6238 | if (ret != -ENOENT) |
8cb01fac | 6239 | return ret; |
505657bc PB |
6240 | |
6241 | spin_lock(&ctx->completion_lock); | |
79ebeaee | 6242 | spin_lock_irq(&ctx->timeout_lock); |
47f46768 | 6243 | ret = io_timeout_cancel(ctx, sqe_addr); |
79ebeaee | 6244 | spin_unlock_irq(&ctx->timeout_lock); |
47f46768 | 6245 | if (ret != -ENOENT) |
505657bc PB |
6246 | goto out; |
6247 | ret = io_poll_cancel(ctx, sqe_addr, false); | |
6248 | out: | |
6249 | spin_unlock(&ctx->completion_lock); | |
6250 | return ret; | |
47f46768 JA |
6251 | } |
6252 | ||
3529d8c2 JA |
6253 | static int io_async_cancel_prep(struct io_kiocb *req, |
6254 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 6255 | { |
fbf23849 | 6256 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 6257 | return -EINVAL; |
61710e43 DA |
6258 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6259 | return -EINVAL; | |
26578cda PB |
6260 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
6261 | sqe->splice_fd_in) | |
e977d6d3 JA |
6262 | return -EINVAL; |
6263 | ||
fbf23849 JA |
6264 | req->cancel.addr = READ_ONCE(sqe->addr); |
6265 | return 0; | |
6266 | } | |
6267 | ||
61e98203 | 6268 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6269 | { |
6270 | struct io_ring_ctx *ctx = req->ctx; | |
58f99373 | 6271 | u64 sqe_addr = req->cancel.addr; |
3b44b371 | 6272 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
58f99373 PB |
6273 | struct io_tctx_node *node; |
6274 | int ret; | |
6275 | ||
8cb01fac | 6276 | ret = io_try_cancel_userdata(req, sqe_addr); |
58f99373 PB |
6277 | if (ret != -ENOENT) |
6278 | goto done; | |
58f99373 PB |
6279 | |
6280 | /* slow path, try all io-wq's */ | |
3b44b371 | 6281 | io_ring_submit_lock(ctx, needs_lock); |
58f99373 PB |
6282 | ret = -ENOENT; |
6283 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
6284 | struct io_uring_task *tctx = node->task->io_uring; | |
fbf23849 | 6285 | |
58f99373 PB |
6286 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
6287 | if (ret != -ENOENT) | |
6288 | break; | |
6289 | } | |
3b44b371 | 6290 | io_ring_submit_unlock(ctx, needs_lock); |
58f99373 | 6291 | done: |
58f99373 | 6292 | if (ret < 0) |
93d2bcd2 | 6293 | req_set_fail(req); |
505657bc | 6294 | io_req_complete_post(req, ret, 0); |
5262f567 JA |
6295 | return 0; |
6296 | } | |
6297 | ||
269bbe5f | 6298 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
6299 | const struct io_uring_sqe *sqe) |
6300 | { | |
61710e43 DA |
6301 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6302 | return -EINVAL; | |
26578cda | 6303 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
05f3fb3c JA |
6304 | return -EINVAL; |
6305 | ||
269bbe5f BM |
6306 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
6307 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
6308 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 6309 | return -EINVAL; |
269bbe5f | 6310 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
6311 | return 0; |
6312 | } | |
6313 | ||
889fca73 | 6314 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6315 | { |
6316 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 6317 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
c3bdad02 | 6318 | struct io_uring_rsrc_update2 up; |
05f3fb3c | 6319 | int ret; |
fbf23849 | 6320 | |
269bbe5f BM |
6321 | up.offset = req->rsrc_update.offset; |
6322 | up.data = req->rsrc_update.arg; | |
c3bdad02 PB |
6323 | up.nr = 0; |
6324 | up.tags = 0; | |
615cee49 | 6325 | up.resv = 0; |
05f3fb3c | 6326 | |
3b44b371 | 6327 | io_ring_submit_lock(ctx, needs_lock); |
fdecb662 | 6328 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
98f0b3b4 | 6329 | &up, req->rsrc_update.nr_args); |
3b44b371 | 6330 | io_ring_submit_unlock(ctx, needs_lock); |
05f3fb3c JA |
6331 | |
6332 | if (ret < 0) | |
93d2bcd2 | 6333 | req_set_fail(req); |
889fca73 | 6334 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
6335 | return 0; |
6336 | } | |
6337 | ||
bfe76559 | 6338 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 6339 | { |
d625c6ee | 6340 | switch (req->opcode) { |
e781573e | 6341 | case IORING_OP_NOP: |
bfe76559 | 6342 | return 0; |
f67676d1 JA |
6343 | case IORING_OP_READV: |
6344 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 6345 | case IORING_OP_READ: |
bfe76559 | 6346 | return io_read_prep(req, sqe); |
f67676d1 JA |
6347 | case IORING_OP_WRITEV: |
6348 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 6349 | case IORING_OP_WRITE: |
bfe76559 | 6350 | return io_write_prep(req, sqe); |
0969e783 | 6351 | case IORING_OP_POLL_ADD: |
bfe76559 | 6352 | return io_poll_add_prep(req, sqe); |
0969e783 | 6353 | case IORING_OP_POLL_REMOVE: |
c5de0036 | 6354 | return io_poll_update_prep(req, sqe); |
8ed8d3c3 | 6355 | case IORING_OP_FSYNC: |
1155c76a | 6356 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 6357 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 6358 | return io_sfr_prep(req, sqe); |
03b1230c | 6359 | case IORING_OP_SENDMSG: |
fddaface | 6360 | case IORING_OP_SEND: |
bfe76559 | 6361 | return io_sendmsg_prep(req, sqe); |
03b1230c | 6362 | case IORING_OP_RECVMSG: |
fddaface | 6363 | case IORING_OP_RECV: |
bfe76559 | 6364 | return io_recvmsg_prep(req, sqe); |
f499a021 | 6365 | case IORING_OP_CONNECT: |
bfe76559 | 6366 | return io_connect_prep(req, sqe); |
2d28390a | 6367 | case IORING_OP_TIMEOUT: |
bfe76559 | 6368 | return io_timeout_prep(req, sqe, false); |
b29472ee | 6369 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 6370 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 6371 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 6372 | return io_async_cancel_prep(req, sqe); |
2d28390a | 6373 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 6374 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 6375 | case IORING_OP_ACCEPT: |
bfe76559 | 6376 | return io_accept_prep(req, sqe); |
d63d1b5e | 6377 | case IORING_OP_FALLOCATE: |
bfe76559 | 6378 | return io_fallocate_prep(req, sqe); |
15b71abe | 6379 | case IORING_OP_OPENAT: |
bfe76559 | 6380 | return io_openat_prep(req, sqe); |
b5dba59e | 6381 | case IORING_OP_CLOSE: |
bfe76559 | 6382 | return io_close_prep(req, sqe); |
05f3fb3c | 6383 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 6384 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6385 | case IORING_OP_STATX: |
bfe76559 | 6386 | return io_statx_prep(req, sqe); |
4840e418 | 6387 | case IORING_OP_FADVISE: |
bfe76559 | 6388 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6389 | case IORING_OP_MADVISE: |
bfe76559 | 6390 | return io_madvise_prep(req, sqe); |
cebdb986 | 6391 | case IORING_OP_OPENAT2: |
bfe76559 | 6392 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6393 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6394 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6395 | case IORING_OP_SPLICE: |
bfe76559 | 6396 | return io_splice_prep(req, sqe); |
ddf0322d | 6397 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6398 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6399 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6400 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6401 | case IORING_OP_TEE: |
bfe76559 | 6402 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6403 | case IORING_OP_SHUTDOWN: |
6404 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6405 | case IORING_OP_RENAMEAT: |
6406 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6407 | case IORING_OP_UNLINKAT: |
6408 | return io_unlinkat_prep(req, sqe); | |
e34a02dc DK |
6409 | case IORING_OP_MKDIRAT: |
6410 | return io_mkdirat_prep(req, sqe); | |
7a8721f8 DK |
6411 | case IORING_OP_SYMLINKAT: |
6412 | return io_symlinkat_prep(req, sqe); | |
cf30da90 DK |
6413 | case IORING_OP_LINKAT: |
6414 | return io_linkat_prep(req, sqe); | |
f67676d1 JA |
6415 | } |
6416 | ||
bfe76559 PB |
6417 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6418 | req->opcode); | |
bd54b6fe | 6419 | return -EINVAL; |
bfe76559 PB |
6420 | } |
6421 | ||
93642ef8 | 6422 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 6423 | { |
b7e298d2 PB |
6424 | if (!io_op_defs[req->opcode].needs_async_setup) |
6425 | return 0; | |
d886e185 | 6426 | if (WARN_ON_ONCE(req_has_async_data(req))) |
b7e298d2 PB |
6427 | return -EFAULT; |
6428 | if (io_alloc_async_data(req)) | |
6429 | return -EAGAIN; | |
6430 | ||
93642ef8 PB |
6431 | switch (req->opcode) { |
6432 | case IORING_OP_READV: | |
93642ef8 PB |
6433 | return io_rw_prep_async(req, READ); |
6434 | case IORING_OP_WRITEV: | |
93642ef8 PB |
6435 | return io_rw_prep_async(req, WRITE); |
6436 | case IORING_OP_SENDMSG: | |
93642ef8 PB |
6437 | return io_sendmsg_prep_async(req); |
6438 | case IORING_OP_RECVMSG: | |
93642ef8 PB |
6439 | return io_recvmsg_prep_async(req); |
6440 | case IORING_OP_CONNECT: | |
6441 | return io_connect_prep_async(req); | |
6442 | } | |
b7e298d2 PB |
6443 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
6444 | req->opcode); | |
6445 | return -EFAULT; | |
f67676d1 JA |
6446 | } |
6447 | ||
9cf7c104 PB |
6448 | static u32 io_get_sequence(struct io_kiocb *req) |
6449 | { | |
a3dbdf54 | 6450 | u32 seq = req->ctx->cached_sq_head; |
9cf7c104 | 6451 | |
a3dbdf54 PB |
6452 | /* need original cached_sq_head, but it was increased for each req */ |
6453 | io_for_each_link(req, req) | |
6454 | seq--; | |
6455 | return seq; | |
9cf7c104 PB |
6456 | } |
6457 | ||
c072481d | 6458 | static __cold void io_drain_req(struct io_kiocb *req) |
de0617e4 | 6459 | { |
a197f664 | 6460 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6461 | struct io_defer_entry *de; |
f67676d1 | 6462 | int ret; |
e0eb71dc | 6463 | u32 seq = io_get_sequence(req); |
3c19966d | 6464 | |
9d858b21 | 6465 | /* Still need defer if there is pending req in defer list. */ |
5e371265 | 6466 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) { |
e0eb71dc | 6467 | queue: |
10c66904 | 6468 | ctx->drain_active = false; |
e0eb71dc PB |
6469 | io_req_task_queue(req); |
6470 | return; | |
10c66904 | 6471 | } |
9cf7c104 | 6472 | |
b7e298d2 | 6473 | ret = io_req_prep_async(req); |
e0eb71dc PB |
6474 | if (ret) { |
6475 | fail: | |
6476 | io_req_complete_failed(req, ret); | |
6477 | return; | |
6478 | } | |
cbdcb435 | 6479 | io_prep_async_link(req); |
27dc8338 | 6480 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
76cc33d7 | 6481 | if (!de) { |
1b48773f | 6482 | ret = -ENOMEM; |
e0eb71dc | 6483 | goto fail; |
76cc33d7 | 6484 | } |
2d28390a | 6485 | |
79ebeaee | 6486 | spin_lock(&ctx->completion_lock); |
9cf7c104 | 6487 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
79ebeaee | 6488 | spin_unlock(&ctx->completion_lock); |
27dc8338 | 6489 | kfree(de); |
e0eb71dc | 6490 | goto queue; |
de0617e4 JA |
6491 | } |
6492 | ||
915967f6 | 6493 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 6494 | de->req = req; |
9cf7c104 | 6495 | de->seq = seq; |
27dc8338 | 6496 | list_add_tail(&de->list, &ctx->defer_list); |
79ebeaee | 6497 | spin_unlock(&ctx->completion_lock); |
de0617e4 JA |
6498 | } |
6499 | ||
68fb8979 | 6500 | static void io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6501 | { |
0e1b6fe3 | 6502 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
30d51dd4 PB |
6503 | kfree(req->kbuf); |
6504 | req->kbuf = NULL; | |
99bc4c38 PB |
6505 | } |
6506 | ||
0e1b6fe3 PB |
6507 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6508 | switch (req->opcode) { | |
6509 | case IORING_OP_READV: | |
6510 | case IORING_OP_READ_FIXED: | |
6511 | case IORING_OP_READ: | |
6512 | case IORING_OP_WRITEV: | |
6513 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6514 | case IORING_OP_WRITE: { |
6515 | struct io_async_rw *io = req->async_data; | |
1dacb4df PB |
6516 | |
6517 | kfree(io->free_iovec); | |
0e1b6fe3 | 6518 | break; |
e8c2bc1f | 6519 | } |
0e1b6fe3 | 6520 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6521 | case IORING_OP_SENDMSG: { |
6522 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
6523 | |
6524 | kfree(io->free_iov); | |
0e1b6fe3 | 6525 | break; |
e8c2bc1f | 6526 | } |
0e1b6fe3 PB |
6527 | case IORING_OP_SPLICE: |
6528 | case IORING_OP_TEE: | |
e1d767f0 PB |
6529 | if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED)) |
6530 | io_put_file(req->splice.file_in); | |
0e1b6fe3 | 6531 | break; |
f3cd4850 JA |
6532 | case IORING_OP_OPENAT: |
6533 | case IORING_OP_OPENAT2: | |
6534 | if (req->open.filename) | |
6535 | putname(req->open.filename); | |
6536 | break; | |
80a261fd JA |
6537 | case IORING_OP_RENAMEAT: |
6538 | putname(req->rename.oldpath); | |
6539 | putname(req->rename.newpath); | |
6540 | break; | |
14a1143b JA |
6541 | case IORING_OP_UNLINKAT: |
6542 | putname(req->unlink.filename); | |
6543 | break; | |
e34a02dc DK |
6544 | case IORING_OP_MKDIRAT: |
6545 | putname(req->mkdir.filename); | |
6546 | break; | |
7a8721f8 DK |
6547 | case IORING_OP_SYMLINKAT: |
6548 | putname(req->symlink.oldpath); | |
6549 | putname(req->symlink.newpath); | |
6550 | break; | |
cf30da90 DK |
6551 | case IORING_OP_LINKAT: |
6552 | putname(req->hardlink.oldpath); | |
6553 | putname(req->hardlink.newpath); | |
6554 | break; | |
0e1b6fe3 | 6555 | } |
99bc4c38 | 6556 | } |
75652a30 JA |
6557 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
6558 | kfree(req->apoll->double_poll); | |
6559 | kfree(req->apoll); | |
6560 | req->apoll = NULL; | |
6561 | } | |
3a0a6902 PB |
6562 | if (req->flags & REQ_F_INFLIGHT) { |
6563 | struct io_uring_task *tctx = req->task->io_uring; | |
6564 | ||
6565 | atomic_dec(&tctx->inflight_tracked); | |
3a0a6902 | 6566 | } |
c854357b | 6567 | if (req->flags & REQ_F_CREDS) |
b8e64b53 | 6568 | put_cred(req->creds); |
d886e185 PB |
6569 | if (req->flags & REQ_F_ASYNC_DATA) { |
6570 | kfree(req->async_data); | |
6571 | req->async_data = NULL; | |
6572 | } | |
c854357b | 6573 | req->flags &= ~IO_REQ_CLEAN_FLAGS; |
99bc4c38 PB |
6574 | } |
6575 | ||
889fca73 | 6576 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 6577 | { |
5730b27e | 6578 | const struct cred *creds = NULL; |
d625c6ee | 6579 | int ret; |
2b188cc1 | 6580 | |
6878b40e | 6581 | if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred())) |
c10d1f98 | 6582 | creds = override_creds(req->creds); |
5730b27e | 6583 | |
d625c6ee | 6584 | switch (req->opcode) { |
2b188cc1 | 6585 | case IORING_OP_NOP: |
889fca73 | 6586 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
6587 | break; |
6588 | case IORING_OP_READV: | |
edafccee | 6589 | case IORING_OP_READ_FIXED: |
3a6820f2 | 6590 | case IORING_OP_READ: |
889fca73 | 6591 | ret = io_read(req, issue_flags); |
edafccee | 6592 | break; |
3529d8c2 | 6593 | case IORING_OP_WRITEV: |
edafccee | 6594 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 6595 | case IORING_OP_WRITE: |
889fca73 | 6596 | ret = io_write(req, issue_flags); |
2b188cc1 | 6597 | break; |
c992fe29 | 6598 | case IORING_OP_FSYNC: |
45d189c6 | 6599 | ret = io_fsync(req, issue_flags); |
c992fe29 | 6600 | break; |
221c5eb2 | 6601 | case IORING_OP_POLL_ADD: |
61e98203 | 6602 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
6603 | break; |
6604 | case IORING_OP_POLL_REMOVE: | |
c5de0036 | 6605 | ret = io_poll_update(req, issue_flags); |
221c5eb2 | 6606 | break; |
5d17b4a4 | 6607 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 6608 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 6609 | break; |
0fa03c62 | 6610 | case IORING_OP_SENDMSG: |
889fca73 | 6611 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 6612 | break; |
fddaface | 6613 | case IORING_OP_SEND: |
889fca73 | 6614 | ret = io_send(req, issue_flags); |
0fa03c62 | 6615 | break; |
aa1fa28f | 6616 | case IORING_OP_RECVMSG: |
889fca73 | 6617 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 6618 | break; |
fddaface | 6619 | case IORING_OP_RECV: |
889fca73 | 6620 | ret = io_recv(req, issue_flags); |
aa1fa28f | 6621 | break; |
5262f567 | 6622 | case IORING_OP_TIMEOUT: |
61e98203 | 6623 | ret = io_timeout(req, issue_flags); |
5262f567 | 6624 | break; |
11365043 | 6625 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 6626 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 6627 | break; |
17f2fe35 | 6628 | case IORING_OP_ACCEPT: |
889fca73 | 6629 | ret = io_accept(req, issue_flags); |
17f2fe35 | 6630 | break; |
f8e85cf2 | 6631 | case IORING_OP_CONNECT: |
889fca73 | 6632 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 6633 | break; |
62755e35 | 6634 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 6635 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 6636 | break; |
d63d1b5e | 6637 | case IORING_OP_FALLOCATE: |
45d189c6 | 6638 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 6639 | break; |
15b71abe | 6640 | case IORING_OP_OPENAT: |
45d189c6 | 6641 | ret = io_openat(req, issue_flags); |
15b71abe | 6642 | break; |
b5dba59e | 6643 | case IORING_OP_CLOSE: |
889fca73 | 6644 | ret = io_close(req, issue_flags); |
b5dba59e | 6645 | break; |
05f3fb3c | 6646 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6647 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6648 | break; |
eddc7ef5 | 6649 | case IORING_OP_STATX: |
45d189c6 | 6650 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6651 | break; |
4840e418 | 6652 | case IORING_OP_FADVISE: |
45d189c6 | 6653 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6654 | break; |
c1ca757b | 6655 | case IORING_OP_MADVISE: |
45d189c6 | 6656 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6657 | break; |
cebdb986 | 6658 | case IORING_OP_OPENAT2: |
45d189c6 | 6659 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6660 | break; |
3e4827b0 | 6661 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6662 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6663 | break; |
7d67af2c | 6664 | case IORING_OP_SPLICE: |
45d189c6 | 6665 | ret = io_splice(req, issue_flags); |
7d67af2c | 6666 | break; |
ddf0322d | 6667 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6668 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6669 | break; |
067524e9 | 6670 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6671 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6672 | break; |
f2a8d5c7 | 6673 | case IORING_OP_TEE: |
45d189c6 | 6674 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6675 | break; |
36f4fa68 | 6676 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6677 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6678 | break; |
80a261fd | 6679 | case IORING_OP_RENAMEAT: |
45d189c6 | 6680 | ret = io_renameat(req, issue_flags); |
80a261fd | 6681 | break; |
14a1143b | 6682 | case IORING_OP_UNLINKAT: |
45d189c6 | 6683 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6684 | break; |
e34a02dc DK |
6685 | case IORING_OP_MKDIRAT: |
6686 | ret = io_mkdirat(req, issue_flags); | |
6687 | break; | |
7a8721f8 DK |
6688 | case IORING_OP_SYMLINKAT: |
6689 | ret = io_symlinkat(req, issue_flags); | |
6690 | break; | |
cf30da90 DK |
6691 | case IORING_OP_LINKAT: |
6692 | ret = io_linkat(req, issue_flags); | |
6693 | break; | |
2b188cc1 JA |
6694 | default: |
6695 | ret = -EINVAL; | |
6696 | break; | |
6697 | } | |
6698 | ||
5730b27e JA |
6699 | if (creds) |
6700 | revert_creds(creds); | |
def596e9 JA |
6701 | if (ret) |
6702 | return ret; | |
b532576e | 6703 | /* If the op doesn't have a file, we're not polling for it */ |
9983028e | 6704 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
9882131c | 6705 | io_iopoll_req_issued(req, issue_flags); |
def596e9 JA |
6706 | |
6707 | return 0; | |
2b188cc1 JA |
6708 | } |
6709 | ||
ebc11b6c PB |
6710 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
6711 | { | |
6712 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6713 | ||
6714 | req = io_put_req_find_next(req); | |
6715 | return req ? &req->work : NULL; | |
6716 | } | |
6717 | ||
5280f7e5 | 6718 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6719 | { |
6720 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
d01905db PB |
6721 | unsigned int issue_flags = IO_URING_F_UNLOCKED; |
6722 | bool needs_poll = false; | |
6df1db6b | 6723 | struct io_kiocb *timeout; |
561fb04a | 6724 | int ret = 0; |
2b188cc1 | 6725 | |
48dcd38d PB |
6726 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
6727 | if (!(req->flags & REQ_F_REFCOUNT)) | |
6728 | __io_req_set_refcount(req, 2); | |
6729 | else | |
6730 | req_ref_get(req); | |
5d5901a3 | 6731 | |
6df1db6b PB |
6732 | timeout = io_prep_linked_timeout(req); |
6733 | if (timeout) | |
6734 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6735 | |
dadebc35 | 6736 | /* either cancelled or io-wq is dying, so don't touch tctx->iowq */ |
d01905db PB |
6737 | if (work->flags & IO_WQ_WORK_CANCEL) { |
6738 | io_req_task_queue_fail(req, -ECANCELED); | |
6739 | return; | |
6740 | } | |
31b51510 | 6741 | |
d01905db | 6742 | if (req->flags & REQ_F_FORCE_ASYNC) { |
afb7f56f PB |
6743 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
6744 | bool opcode_poll = def->pollin || def->pollout; | |
6745 | ||
6746 | if (opcode_poll && file_can_poll(req->file)) { | |
6747 | needs_poll = true; | |
d01905db | 6748 | issue_flags |= IO_URING_F_NONBLOCK; |
afb7f56f | 6749 | } |
561fb04a | 6750 | } |
31b51510 | 6751 | |
d01905db PB |
6752 | do { |
6753 | ret = io_issue_sqe(req, issue_flags); | |
6754 | if (ret != -EAGAIN) | |
6755 | break; | |
6756 | /* | |
6757 | * We can get EAGAIN for iopolled IO even though we're | |
6758 | * forcing a sync submission from here, since we can't | |
6759 | * wait for request slots on the block side. | |
6760 | */ | |
6761 | if (!needs_poll) { | |
6762 | cond_resched(); | |
6763 | continue; | |
90fa0288 HX |
6764 | } |
6765 | ||
d01905db PB |
6766 | if (io_arm_poll_handler(req) == IO_APOLL_OK) |
6767 | return; | |
6768 | /* aborted or ready, in either case retry blocking */ | |
6769 | needs_poll = false; | |
6770 | issue_flags &= ~IO_URING_F_NONBLOCK; | |
6771 | } while (1); | |
31b51510 | 6772 | |
a3df7698 | 6773 | /* avoid locking problems by failing it from a clean context */ |
5d5901a3 | 6774 | if (ret) |
a3df7698 | 6775 | io_req_task_queue_fail(req, ret); |
2b188cc1 JA |
6776 | } |
6777 | ||
aeca241b | 6778 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
042b0d85 | 6779 | unsigned i) |
65e19f54 | 6780 | { |
042b0d85 | 6781 | return &table->files[i]; |
dafecf19 PB |
6782 | } |
6783 | ||
65e19f54 JA |
6784 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6785 | int index) | |
6786 | { | |
aeca241b | 6787 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
65e19f54 | 6788 | |
a04b0ac0 | 6789 | return (struct file *) (slot->file_ptr & FFS_MASK); |
65e19f54 JA |
6790 | } |
6791 | ||
a04b0ac0 | 6792 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
9a321c98 PB |
6793 | { |
6794 | unsigned long file_ptr = (unsigned long) file; | |
6795 | ||
88459b50 | 6796 | file_ptr |= io_file_get_flags(file); |
a04b0ac0 | 6797 | file_slot->file_ptr = file_ptr; |
65e19f54 JA |
6798 | } |
6799 | ||
ac177053 PB |
6800 | static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx, |
6801 | struct io_kiocb *req, int fd) | |
09bb8394 | 6802 | { |
8da11c19 | 6803 | struct file *file; |
ac177053 | 6804 | unsigned long file_ptr; |
09bb8394 | 6805 | |
ac177053 PB |
6806 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
6807 | return NULL; | |
6808 | fd = array_index_nospec(fd, ctx->nr_user_files); | |
6809 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; | |
6810 | file = (struct file *) (file_ptr & FFS_MASK); | |
6811 | file_ptr &= ~FFS_MASK; | |
6812 | /* mask in overlapping REQ_F and FFS bits */ | |
35645ac3 | 6813 | req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT); |
a46be971 | 6814 | io_req_set_rsrc_node(req, ctx); |
ac177053 PB |
6815 | return file; |
6816 | } | |
d44f554e | 6817 | |
ac177053 | 6818 | static struct file *io_file_get_normal(struct io_ring_ctx *ctx, |
ac177053 PB |
6819 | struct io_kiocb *req, int fd) |
6820 | { | |
62906e89 | 6821 | struct file *file = fget(fd); |
ac177053 PB |
6822 | |
6823 | trace_io_uring_file_get(ctx, fd); | |
09bb8394 | 6824 | |
ac177053 PB |
6825 | /* we don't allow fixed io_uring files */ |
6826 | if (file && unlikely(file->f_op == &io_uring_fops)) | |
6827 | io_req_track_inflight(req); | |
8371adf5 | 6828 | return file; |
09bb8394 JA |
6829 | } |
6830 | ||
ac177053 | 6831 | static inline struct file *io_file_get(struct io_ring_ctx *ctx, |
ac177053 PB |
6832 | struct io_kiocb *req, int fd, bool fixed) |
6833 | { | |
6834 | if (fixed) | |
6835 | return io_file_get_fixed(ctx, req, fd); | |
6836 | else | |
62906e89 | 6837 | return io_file_get_normal(ctx, req, fd); |
ac177053 PB |
6838 | } |
6839 | ||
f237c30a | 6840 | static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked) |
89b263f6 JA |
6841 | { |
6842 | struct io_kiocb *prev = req->timeout.prev; | |
8cb01fac | 6843 | int ret; |
89b263f6 JA |
6844 | |
6845 | if (prev) { | |
8cb01fac | 6846 | ret = io_try_cancel_userdata(req, prev->user_data); |
505657bc | 6847 | io_req_complete_post(req, ret ?: -ETIME, 0); |
89b263f6 | 6848 | io_put_req(prev); |
89b263f6 JA |
6849 | } else { |
6850 | io_req_complete_post(req, -ETIME, 0); | |
6851 | } | |
6852 | } | |
6853 | ||
2665abfd | 6854 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6855 | { |
ad8a48ac JA |
6856 | struct io_timeout_data *data = container_of(timer, |
6857 | struct io_timeout_data, timer); | |
90cd7e42 | 6858 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6859 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6860 | unsigned long flags; |
2665abfd | 6861 | |
89b263f6 | 6862 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
90cd7e42 PB |
6863 | prev = req->timeout.head; |
6864 | req->timeout.head = NULL; | |
2665abfd JA |
6865 | |
6866 | /* | |
6867 | * We don't expect the list to be empty, that will only happen if we | |
6868 | * race with the completion of the linked work. | |
6869 | */ | |
447c19f3 | 6870 | if (prev) { |
f2f87370 | 6871 | io_remove_next_linked(prev); |
447c19f3 PB |
6872 | if (!req_ref_inc_not_zero(prev)) |
6873 | prev = NULL; | |
6874 | } | |
ef9dd637 | 6875 | list_del(&req->timeout.list); |
89b263f6 JA |
6876 | req->timeout.prev = prev; |
6877 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); | |
2665abfd | 6878 | |
89b263f6 JA |
6879 | req->io_task_work.func = io_req_task_link_timeout; |
6880 | io_req_task_work_add(req); | |
2665abfd JA |
6881 | return HRTIMER_NORESTART; |
6882 | } | |
6883 | ||
de968c18 | 6884 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6885 | { |
de968c18 PB |
6886 | struct io_ring_ctx *ctx = req->ctx; |
6887 | ||
89b263f6 | 6888 | spin_lock_irq(&ctx->timeout_lock); |
76a46e06 | 6889 | /* |
f2f87370 PB |
6890 | * If the back reference is NULL, then our linked request finished |
6891 | * before we got a chance to setup the timer | |
76a46e06 | 6892 | */ |
90cd7e42 | 6893 | if (req->timeout.head) { |
e8c2bc1f | 6894 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6895 | |
ad8a48ac JA |
6896 | data->timer.function = io_link_timeout_fn; |
6897 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6898 | data->mode); | |
ef9dd637 | 6899 | list_add_tail(&req->timeout.list, &ctx->ltimeout_list); |
2665abfd | 6900 | } |
89b263f6 | 6901 | spin_unlock_irq(&ctx->timeout_lock); |
2665abfd | 6902 | /* drop submission reference */ |
76a46e06 JA |
6903 | io_put_req(req); |
6904 | } | |
2665abfd | 6905 | |
d475a9a6 PB |
6906 | static void io_queue_sqe_arm_apoll(struct io_kiocb *req) |
6907 | __must_hold(&req->ctx->uring_lock) | |
6908 | { | |
6909 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); | |
6910 | ||
6911 | switch (io_arm_poll_handler(req)) { | |
6912 | case IO_APOLL_READY: | |
6913 | if (linked_timeout) { | |
8d1f0177 | 6914 | io_queue_linked_timeout(linked_timeout); |
d475a9a6 PB |
6915 | linked_timeout = NULL; |
6916 | } | |
6917 | io_req_task_queue(req); | |
6918 | break; | |
6919 | case IO_APOLL_ABORTED: | |
6920 | /* | |
6921 | * Queued up for async execution, worker will release | |
6922 | * submit reference when the iocb is actually submitted. | |
6923 | */ | |
6924 | io_queue_async_work(req, NULL); | |
6925 | break; | |
6926 | } | |
6927 | ||
6928 | if (linked_timeout) | |
6929 | io_queue_linked_timeout(linked_timeout); | |
6930 | } | |
6931 | ||
6932 | static inline void __io_queue_sqe(struct io_kiocb *req) | |
282cdc86 | 6933 | __must_hold(&req->ctx->uring_lock) |
2b188cc1 | 6934 | { |
906c6caa | 6935 | struct io_kiocb *linked_timeout; |
e0c5c576 | 6936 | int ret; |
2b188cc1 | 6937 | |
c5eef2b9 | 6938 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 6939 | |
fff4e40e PB |
6940 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
6941 | io_req_add_compl_list(req); | |
d9f9d284 | 6942 | return; |
fff4e40e | 6943 | } |
491381ce JA |
6944 | /* |
6945 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6946 | * doesn't support non-blocking read/write attempts | |
6947 | */ | |
1840038e | 6948 | if (likely(!ret)) { |
906c6caa PB |
6949 | linked_timeout = io_prep_linked_timeout(req); |
6950 | if (linked_timeout) | |
6951 | io_queue_linked_timeout(linked_timeout); | |
1840038e | 6952 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
d475a9a6 | 6953 | io_queue_sqe_arm_apoll(req); |
0d63c148 | 6954 | } else { |
f41db273 | 6955 | io_req_complete_failed(req, ret); |
9e645e11 | 6956 | } |
2b188cc1 JA |
6957 | } |
6958 | ||
4652fe3f | 6959 | static void io_queue_sqe_fallback(struct io_kiocb *req) |
282cdc86 | 6960 | __must_hold(&req->ctx->uring_lock) |
4fe2c963 | 6961 | { |
4652fe3f | 6962 | if (req->flags & REQ_F_FAIL) { |
c6d3d9cb | 6963 | io_req_complete_fail_submit(req); |
e0eb71dc PB |
6964 | } else if (unlikely(req->ctx->drain_active)) { |
6965 | io_drain_req(req); | |
76cc33d7 PB |
6966 | } else { |
6967 | int ret = io_req_prep_async(req); | |
6968 | ||
6969 | if (unlikely(ret)) | |
6970 | io_req_complete_failed(req, ret); | |
6971 | else | |
f237c30a | 6972 | io_queue_async_work(req, NULL); |
ce35a47a | 6973 | } |
4fe2c963 JL |
6974 | } |
6975 | ||
4652fe3f PB |
6976 | static inline void io_queue_sqe(struct io_kiocb *req) |
6977 | __must_hold(&req->ctx->uring_lock) | |
6978 | { | |
6979 | if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) | |
6980 | __io_queue_sqe(req); | |
6981 | else | |
6982 | io_queue_sqe_fallback(req); | |
6983 | } | |
6984 | ||
b16fed66 PB |
6985 | /* |
6986 | * Check SQE restrictions (opcode and flags). | |
6987 | * | |
6988 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6989 | */ | |
6990 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6991 | struct io_kiocb *req, | |
6992 | unsigned int sqe_flags) | |
4fe2c963 | 6993 | { |
b16fed66 PB |
6994 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
6995 | return false; | |
6996 | ||
6997 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6998 | ctx->restrictions.sqe_flags_required) | |
6999 | return false; | |
7000 | ||
7001 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
7002 | ctx->restrictions.sqe_flags_required)) | |
7003 | return false; | |
7004 | ||
7005 | return true; | |
4fe2c963 JL |
7006 | } |
7007 | ||
22b2ca31 PB |
7008 | static void io_init_req_drain(struct io_kiocb *req) |
7009 | { | |
7010 | struct io_ring_ctx *ctx = req->ctx; | |
7011 | struct io_kiocb *head = ctx->submit_state.link.head; | |
7012 | ||
7013 | ctx->drain_active = true; | |
7014 | if (head) { | |
7015 | /* | |
7016 | * If we need to drain a request in the middle of a link, drain | |
7017 | * the head request and the next request/link after the current | |
7018 | * link. Considering sequential execution of links, | |
7019 | * IOSQE_IO_DRAIN will be maintained for every request of our | |
7020 | * link. | |
7021 | */ | |
7022 | head->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC; | |
7023 | ctx->drain_next = true; | |
7024 | } | |
7025 | } | |
7026 | ||
b16fed66 PB |
7027 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
7028 | const struct io_uring_sqe *sqe) | |
282cdc86 | 7029 | __must_hold(&ctx->uring_lock) |
b16fed66 | 7030 | { |
b16fed66 | 7031 | unsigned int sqe_flags; |
fc0ae024 | 7032 | int personality; |
4a04d1d1 | 7033 | u8 opcode; |
b16fed66 | 7034 | |
864ea921 | 7035 | /* req is partially pre-initialised, see io_preinit_req() */ |
4a04d1d1 | 7036 | req->opcode = opcode = READ_ONCE(sqe->opcode); |
b16fed66 PB |
7037 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
7038 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
7039 | req->user_data = READ_ONCE(sqe->user_data); | |
b16fed66 | 7040 | req->file = NULL; |
b16fed66 | 7041 | req->fixed_rsrc_refs = NULL; |
b16fed66 | 7042 | req->task = current; |
b16fed66 | 7043 | |
4a04d1d1 PB |
7044 | if (unlikely(opcode >= IORING_OP_LAST)) { |
7045 | req->opcode = 0; | |
b16fed66 | 7046 | return -EINVAL; |
4a04d1d1 | 7047 | } |
68fe256a PB |
7048 | if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) { |
7049 | /* enforce forwards compatibility on users */ | |
7050 | if (sqe_flags & ~SQE_VALID_FLAGS) | |
7051 | return -EINVAL; | |
7052 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
4a04d1d1 | 7053 | !io_op_defs[opcode].buffer_select) |
68fe256a | 7054 | return -EOPNOTSUPP; |
22b2ca31 PB |
7055 | if (sqe_flags & IOSQE_IO_DRAIN) |
7056 | io_init_req_drain(req); | |
2a56a9bd PB |
7057 | } |
7058 | if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) { | |
7059 | if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags)) | |
7060 | return -EACCES; | |
7061 | /* knock it to the slow queue path, will be drained there */ | |
7062 | if (ctx->drain_active) | |
7063 | req->flags |= REQ_F_FORCE_ASYNC; | |
7064 | /* if there is no link, we're at "next" request and need to drain */ | |
7065 | if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) { | |
7066 | ctx->drain_next = false; | |
7067 | ctx->drain_active = true; | |
22b2ca31 | 7068 | req->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC; |
2a56a9bd | 7069 | } |
68fe256a | 7070 | } |
b16fed66 | 7071 | |
4a04d1d1 | 7072 | if (io_op_defs[opcode].needs_file) { |
6d63416d PB |
7073 | struct io_submit_state *state = &ctx->submit_state; |
7074 | ||
7075 | /* | |
7076 | * Plug now if we have more than 2 IO left after this, and the | |
7077 | * target is potentially a read/write to block based storage. | |
7078 | */ | |
4a04d1d1 | 7079 | if (state->need_plug && io_op_defs[opcode].plug) { |
6d63416d PB |
7080 | state->plug_started = true; |
7081 | state->need_plug = false; | |
5ca7a8b3 | 7082 | blk_start_plug_nr_ios(&state->plug, state->submit_nr); |
6d63416d PB |
7083 | } |
7084 | ||
62906e89 | 7085 | req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd), |
ac177053 | 7086 | (sqe_flags & IOSQE_FIXED_FILE)); |
b16fed66 | 7087 | if (unlikely(!req->file)) |
fc0ae024 | 7088 | return -EBADF; |
b16fed66 | 7089 | } |
863e0560 | 7090 | |
003e8dcc JA |
7091 | personality = READ_ONCE(sqe->personality); |
7092 | if (personality) { | |
c10d1f98 PB |
7093 | req->creds = xa_load(&ctx->personalities, personality); |
7094 | if (!req->creds) | |
003e8dcc | 7095 | return -EINVAL; |
c10d1f98 | 7096 | get_cred(req->creds); |
b8e64b53 | 7097 | req->flags |= REQ_F_CREDS; |
003e8dcc | 7098 | } |
b16fed66 | 7099 | |
fc0ae024 | 7100 | return io_req_prep(req, sqe); |
b16fed66 PB |
7101 | } |
7102 | ||
a6b8cadc | 7103 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 7104 | const struct io_uring_sqe *sqe) |
282cdc86 | 7105 | __must_hold(&ctx->uring_lock) |
9e645e11 | 7106 | { |
a1ab7b35 | 7107 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 7108 | int ret; |
9e645e11 | 7109 | |
a6b8cadc PB |
7110 | ret = io_init_req(ctx, req, sqe); |
7111 | if (unlikely(ret)) { | |
a87acfde JA |
7112 | trace_io_uring_req_failed(sqe, ret); |
7113 | ||
a8295b98 | 7114 | /* fail even hard links since we don't submit */ |
de59bc10 | 7115 | if (link->head) { |
a8295b98 HX |
7116 | /* |
7117 | * we can judge a link req is failed or cancelled by if | |
7118 | * REQ_F_FAIL is set, but the head is an exception since | |
7119 | * it may be set REQ_F_FAIL because of other req's failure | |
7120 | * so let's leverage req->result to distinguish if a head | |
7121 | * is set REQ_F_FAIL because of its failure or other req's | |
7122 | * failure so that we can set the correct ret code for it. | |
7123 | * init result here to avoid affecting the normal path. | |
7124 | */ | |
7125 | if (!(link->head->flags & REQ_F_FAIL)) | |
7126 | req_fail_link_node(link->head, -ECANCELED); | |
7127 | } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { | |
7128 | /* | |
7129 | * the current req is a normal req, we should return | |
7130 | * error and thus break the submittion loop. | |
7131 | */ | |
7132 | io_req_complete_failed(req, ret); | |
7133 | return ret; | |
de59bc10 | 7134 | } |
a8295b98 | 7135 | req_fail_link_node(req, ret); |
a6b8cadc | 7136 | } |
441b8a78 | 7137 | |
be7053b7 | 7138 | /* don't need @sqe from now on */ |
236daeae OL |
7139 | trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data, |
7140 | req->flags, true, | |
7141 | ctx->flags & IORING_SETUP_SQPOLL); | |
a6b8cadc | 7142 | |
9e645e11 JA |
7143 | /* |
7144 | * If we already have a head request, queue this one for async | |
7145 | * submittal once the head completes. If we don't have a head but | |
7146 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
7147 | * submitted sync once the chain is complete. If none of those | |
7148 | * conditions are true (normal request), then just queue it. | |
7149 | */ | |
863e0560 PB |
7150 | if (link->head) { |
7151 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 7152 | |
a8295b98 HX |
7153 | if (!(req->flags & REQ_F_FAIL)) { |
7154 | ret = io_req_prep_async(req); | |
7155 | if (unlikely(ret)) { | |
7156 | req_fail_link_node(req, ret); | |
7157 | if (!(head->flags & REQ_F_FAIL)) | |
7158 | req_fail_link_node(head, -ECANCELED); | |
7159 | } | |
7160 | } | |
9d76377f | 7161 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 7162 | link->last->link = req; |
863e0560 | 7163 | link->last = req; |
32fe525b | 7164 | |
f15a3431 PB |
7165 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) |
7166 | return 0; | |
32fe525b | 7167 | /* last request of a link, enqueue the link */ |
f15a3431 PB |
7168 | link->head = NULL; |
7169 | req = head; | |
7170 | } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { | |
7171 | link->head = req; | |
7172 | link->last = req; | |
7173 | return 0; | |
9e645e11 | 7174 | } |
2e6e1fde | 7175 | |
f15a3431 | 7176 | io_queue_sqe(req); |
1d4240cc | 7177 | return 0; |
9e645e11 JA |
7178 | } |
7179 | ||
9a56a232 JA |
7180 | /* |
7181 | * Batched submission is done, ensure local IO is flushed out. | |
7182 | */ | |
553deffd | 7183 | static void io_submit_state_end(struct io_ring_ctx *ctx) |
9a56a232 | 7184 | { |
553deffd PB |
7185 | struct io_submit_state *state = &ctx->submit_state; |
7186 | ||
a1ab7b35 | 7187 | if (state->link.head) |
de59bc10 | 7188 | io_queue_sqe(state->link.head); |
553deffd | 7189 | /* flush only after queuing links as they can generate completions */ |
c450178d | 7190 | io_submit_flush_completions(ctx); |
27926b68 JA |
7191 | if (state->plug_started) |
7192 | blk_finish_plug(&state->plug); | |
9a56a232 JA |
7193 | } |
7194 | ||
7195 | /* | |
7196 | * Start submission side cache. | |
7197 | */ | |
7198 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 7199 | unsigned int max_ios) |
9a56a232 | 7200 | { |
27926b68 | 7201 | state->plug_started = false; |
4b628aeb | 7202 | state->need_plug = max_ios > 2; |
5ca7a8b3 | 7203 | state->submit_nr = max_ios; |
a1ab7b35 PB |
7204 | /* set only head, no need to init link_last in advance */ |
7205 | state->link.head = NULL; | |
9a56a232 JA |
7206 | } |
7207 | ||
2b188cc1 JA |
7208 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
7209 | { | |
75b28aff | 7210 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 7211 | |
caf582c6 PB |
7212 | /* |
7213 | * Ensure any loads from the SQEs are done at this point, | |
7214 | * since once we write the new head, the application could | |
7215 | * write new data to them. | |
7216 | */ | |
7217 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
7218 | } |
7219 | ||
2b188cc1 | 7220 | /* |
dd9ae8a0 | 7221 | * Fetch an sqe, if one is available. Note this returns a pointer to memory |
2b188cc1 JA |
7222 | * that is mapped by userspace. This means that care needs to be taken to |
7223 | * ensure that reads are stable, as we cannot rely on userspace always | |
7224 | * being a good citizen. If members of the sqe are validated and then later | |
7225 | * used, it's important that those reads are done through READ_ONCE() to | |
7226 | * prevent a re-load down the line. | |
7227 | */ | |
709b302f | 7228 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 7229 | { |
ea5ab3b5 | 7230 | unsigned head, mask = ctx->sq_entries - 1; |
17d3aeb3 | 7231 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
2b188cc1 JA |
7232 | |
7233 | /* | |
7234 | * The cached sq head (or cq tail) serves two purposes: | |
7235 | * | |
7236 | * 1) allows us to batch the cost of updating the user visible | |
7237 | * head updates. | |
7238 | * 2) allows the kernel side to track the head on its own, even | |
7239 | * though the application is the one updating it. | |
7240 | */ | |
17d3aeb3 | 7241 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
709b302f PB |
7242 | if (likely(head < ctx->sq_entries)) |
7243 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
7244 | |
7245 | /* drop invalid entries */ | |
15641e42 PB |
7246 | ctx->cq_extra--; |
7247 | WRITE_ONCE(ctx->rings->sq_dropped, | |
7248 | READ_ONCE(ctx->rings->sq_dropped) + 1); | |
709b302f PB |
7249 | return NULL; |
7250 | } | |
7251 | ||
0f212204 | 7252 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
282cdc86 | 7253 | __must_hold(&ctx->uring_lock) |
6c271ce2 | 7254 | { |
69629809 | 7255 | unsigned int entries = io_sqring_entries(ctx); |
46c4e16a | 7256 | int submitted = 0; |
6c271ce2 | 7257 | |
51d48dab | 7258 | if (unlikely(!entries)) |
69629809 | 7259 | return 0; |
ee7d46d9 | 7260 | /* make sure SQ entry isn't read before tail */ |
69629809 | 7261 | nr = min3(nr, ctx->sq_entries, entries); |
9a10867a | 7262 | io_get_task_refs(nr); |
6c271ce2 | 7263 | |
ba88ff11 | 7264 | io_submit_state_start(&ctx->submit_state, nr); |
69629809 | 7265 | do { |
3529d8c2 | 7266 | const struct io_uring_sqe *sqe; |
196be95c | 7267 | struct io_kiocb *req; |
fb5ccc98 | 7268 | |
a33ae9ce | 7269 | if (unlikely(!io_alloc_req_refill(ctx))) { |
196be95c PB |
7270 | if (!submitted) |
7271 | submitted = -EAGAIN; | |
fb5ccc98 | 7272 | break; |
196be95c | 7273 | } |
a33ae9ce | 7274 | req = io_alloc_req(ctx); |
4fccfcbb PB |
7275 | sqe = io_get_sqe(ctx); |
7276 | if (unlikely(!sqe)) { | |
c2b6c6bc | 7277 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
4fccfcbb PB |
7278 | break; |
7279 | } | |
d3656344 JA |
7280 | /* will complete beyond this point, count as submitted */ |
7281 | submitted++; | |
a1ab7b35 | 7282 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 7283 | break; |
69629809 | 7284 | } while (submitted < nr); |
6c271ce2 | 7285 | |
9466f437 PB |
7286 | if (unlikely(submitted != nr)) { |
7287 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 | 7288 | int unused = nr - ref_used; |
9466f437 | 7289 | |
09899b19 | 7290 | current->io_uring->cached_refs += unused; |
9466f437 | 7291 | } |
6c271ce2 | 7292 | |
553deffd | 7293 | io_submit_state_end(ctx); |
ae9428ca PB |
7294 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
7295 | io_commit_sqring(ctx); | |
7296 | ||
6c271ce2 JA |
7297 | return submitted; |
7298 | } | |
7299 | ||
e4b6d902 PB |
7300 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
7301 | { | |
7302 | return READ_ONCE(sqd->state); | |
7303 | } | |
7304 | ||
23b3628e XW |
7305 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
7306 | { | |
7307 | /* Tell userspace we may need a wakeup call */ | |
79ebeaee | 7308 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7309 | WRITE_ONCE(ctx->rings->sq_flags, |
7310 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7311 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7312 | } |
7313 | ||
7314 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
7315 | { | |
79ebeaee | 7316 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7317 | WRITE_ONCE(ctx->rings->sq_flags, |
7318 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7319 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7320 | } |
7321 | ||
08369246 | 7322 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 7323 | { |
c8d1ba58 | 7324 | unsigned int to_submit; |
bdcd3eab | 7325 | int ret = 0; |
6c271ce2 | 7326 | |
c8d1ba58 | 7327 | to_submit = io_sqring_entries(ctx); |
e95eee2d | 7328 | /* if we're handling multiple rings, cap submit size for fairness */ |
4ce8ad95 OL |
7329 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
7330 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; | |
e95eee2d | 7331 | |
5eef4e87 | 7332 | if (!wq_list_empty(&ctx->iopoll_list) || to_submit) { |
948e1947 PB |
7333 | const struct cred *creds = NULL; |
7334 | ||
7335 | if (ctx->sq_creds != current_cred()) | |
7336 | creds = override_creds(ctx->sq_creds); | |
a4c0b3de | 7337 | |
c8d1ba58 | 7338 | mutex_lock(&ctx->uring_lock); |
5eef4e87 | 7339 | if (!wq_list_empty(&ctx->iopoll_list)) |
5ba3c874 | 7340 | io_do_iopoll(ctx, true); |
906a3c6f | 7341 | |
3b763ba1 PB |
7342 | /* |
7343 | * Don't submit if refs are dying, good for io_uring_register(), | |
7344 | * but also it is relied upon by io_ring_exit_work() | |
7345 | */ | |
0298ef96 PB |
7346 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
7347 | !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
08369246 | 7348 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 | 7349 | mutex_unlock(&ctx->uring_lock); |
6c271ce2 | 7350 | |
acfb381d PB |
7351 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
7352 | wake_up(&ctx->sqo_sq_wait); | |
948e1947 PB |
7353 | if (creds) |
7354 | revert_creds(creds); | |
acfb381d | 7355 | } |
6c271ce2 | 7356 | |
08369246 XW |
7357 | return ret; |
7358 | } | |
6c271ce2 | 7359 | |
c072481d | 7360 | static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
08369246 XW |
7361 | { |
7362 | struct io_ring_ctx *ctx; | |
7363 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 7364 | |
c9dca27d PB |
7365 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7366 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); | |
08369246 | 7367 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 7368 | } |
6c271ce2 | 7369 | |
e4b6d902 PB |
7370 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
7371 | { | |
7372 | bool did_sig = false; | |
7373 | struct ksignal ksig; | |
7374 | ||
7375 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || | |
7376 | signal_pending(current)) { | |
7377 | mutex_unlock(&sqd->lock); | |
7378 | if (signal_pending(current)) | |
7379 | did_sig = get_signal(&ksig); | |
7380 | cond_resched(); | |
7381 | mutex_lock(&sqd->lock); | |
7382 | } | |
e4b6d902 PB |
7383 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
7384 | } | |
7385 | ||
c8d1ba58 JA |
7386 | static int io_sq_thread(void *data) |
7387 | { | |
69fb2131 JA |
7388 | struct io_sq_data *sqd = data; |
7389 | struct io_ring_ctx *ctx; | |
a0d9205f | 7390 | unsigned long timeout = 0; |
37d1e2e3 | 7391 | char buf[TASK_COMM_LEN]; |
08369246 | 7392 | DEFINE_WAIT(wait); |
6c271ce2 | 7393 | |
696ee88a | 7394 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
37d1e2e3 | 7395 | set_task_comm(current, buf); |
37d1e2e3 JA |
7396 | |
7397 | if (sqd->sq_cpu != -1) | |
7398 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
7399 | else | |
7400 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
7401 | current->flags |= PF_NO_SETAFFINITY; | |
7402 | ||
09a6f4ef | 7403 | mutex_lock(&sqd->lock); |
e4b6d902 | 7404 | while (1) { |
1a924a80 | 7405 | bool cap_entries, sqt_spin = false; |
c1edbf5f | 7406 | |
e4b6d902 PB |
7407 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
7408 | if (io_sqd_handle_event(sqd)) | |
c7d95613 | 7409 | break; |
08369246 XW |
7410 | timeout = jiffies + sqd->sq_thread_idle; |
7411 | } | |
e4b6d902 | 7412 | |
e95eee2d | 7413 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 7414 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
948e1947 | 7415 | int ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a | 7416 | |
5eef4e87 | 7417 | if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list))) |
08369246 | 7418 | sqt_spin = true; |
69fb2131 | 7419 | } |
dd432ea5 PB |
7420 | if (io_run_task_work()) |
7421 | sqt_spin = true; | |
6c271ce2 | 7422 | |
08369246 | 7423 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 7424 | cond_resched(); |
08369246 XW |
7425 | if (sqt_spin) |
7426 | timeout = jiffies + sqd->sq_thread_idle; | |
7427 | continue; | |
7428 | } | |
7429 | ||
08369246 | 7430 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
dd432ea5 | 7431 | if (!io_sqd_events_pending(sqd) && !current->task_works) { |
1a924a80 PB |
7432 | bool needs_sched = true; |
7433 | ||
724cb4f9 | 7434 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
aaa9f0f4 PB |
7435 | io_ring_set_wakeup_flag(ctx); |
7436 | ||
724cb4f9 | 7437 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
5eef4e87 | 7438 | !wq_list_empty(&ctx->iopoll_list)) { |
724cb4f9 HX |
7439 | needs_sched = false; |
7440 | break; | |
7441 | } | |
7442 | if (io_sqring_entries(ctx)) { | |
7443 | needs_sched = false; | |
7444 | break; | |
7445 | } | |
7446 | } | |
7447 | ||
7448 | if (needs_sched) { | |
7449 | mutex_unlock(&sqd->lock); | |
7450 | schedule(); | |
7451 | mutex_lock(&sqd->lock); | |
7452 | } | |
69fb2131 JA |
7453 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7454 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 7455 | } |
08369246 XW |
7456 | |
7457 | finish_wait(&sqd->wait, &wait); | |
7458 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 | 7459 | } |
28cea78a | 7460 | |
78cc687b | 7461 | io_uring_cancel_generic(true, sqd); |
37d1e2e3 | 7462 | sqd->thread = NULL; |
05962f95 | 7463 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
5f3f26f9 | 7464 | io_ring_set_wakeup_flag(ctx); |
521d6a73 | 7465 | io_run_task_work(); |
734551df PB |
7466 | mutex_unlock(&sqd->lock); |
7467 | ||
37d1e2e3 JA |
7468 | complete(&sqd->exited); |
7469 | do_exit(0); | |
6c271ce2 JA |
7470 | } |
7471 | ||
bda52162 JA |
7472 | struct io_wait_queue { |
7473 | struct wait_queue_entry wq; | |
7474 | struct io_ring_ctx *ctx; | |
5fd46178 | 7475 | unsigned cq_tail; |
bda52162 JA |
7476 | unsigned nr_timeouts; |
7477 | }; | |
7478 | ||
6c503150 | 7479 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
7480 | { |
7481 | struct io_ring_ctx *ctx = iowq->ctx; | |
5fd46178 | 7482 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
bda52162 JA |
7483 | |
7484 | /* | |
d195a66e | 7485 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
7486 | * started waiting. For timeouts, we always want to return to userspace, |
7487 | * regardless of event count. | |
7488 | */ | |
5fd46178 | 7489 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
bda52162 JA |
7490 | } |
7491 | ||
7492 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
7493 | int wake_flags, void *key) | |
7494 | { | |
7495 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
7496 | wq); | |
7497 | ||
6c503150 PB |
7498 | /* |
7499 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
7500 | * the task, and the next invocation will do it. | |
7501 | */ | |
5ed7a37d | 7502 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow)) |
6c503150 PB |
7503 | return autoremove_wake_function(curr, mode, wake_flags, key); |
7504 | return -1; | |
bda52162 JA |
7505 | } |
7506 | ||
af9c1a44 JA |
7507 | static int io_run_task_work_sig(void) |
7508 | { | |
7509 | if (io_run_task_work()) | |
7510 | return 1; | |
7511 | if (!signal_pending(current)) | |
7512 | return 0; | |
0b8cfa97 | 7513 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
792ee0f6 | 7514 | return -ERESTARTSYS; |
af9c1a44 JA |
7515 | return -EINTR; |
7516 | } | |
7517 | ||
eeb60b9a PB |
7518 | /* when returns >0, the caller should retry */ |
7519 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
7520 | struct io_wait_queue *iowq, | |
7521 | signed long *timeout) | |
7522 | { | |
7523 | int ret; | |
7524 | ||
7525 | /* make sure we run task_work before checking for signals */ | |
7526 | ret = io_run_task_work_sig(); | |
7527 | if (ret || io_should_wake(iowq)) | |
7528 | return ret; | |
7529 | /* let the caller flush overflows, retry */ | |
5ed7a37d | 7530 | if (test_bit(0, &ctx->check_cq_overflow)) |
eeb60b9a PB |
7531 | return 1; |
7532 | ||
7533 | *timeout = schedule_timeout(*timeout); | |
7534 | return !*timeout ? -ETIME : 1; | |
7535 | } | |
7536 | ||
2b188cc1 JA |
7537 | /* |
7538 | * Wait until events become available, if we don't already have some. The | |
7539 | * application must reap them itself, as they reside on the shared cq ring. | |
7540 | */ | |
7541 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
7542 | const sigset_t __user *sig, size_t sigsz, |
7543 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 7544 | { |
90291099 | 7545 | struct io_wait_queue iowq; |
75b28aff | 7546 | struct io_rings *rings = ctx->rings; |
c1d5a224 PB |
7547 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
7548 | int ret; | |
2b188cc1 | 7549 | |
b41e9852 | 7550 | do { |
90f67366 | 7551 | io_cqring_overflow_flush(ctx); |
6c503150 | 7552 | if (io_cqring_events(ctx) >= min_events) |
b41e9852 | 7553 | return 0; |
4c6e277c | 7554 | if (!io_run_task_work()) |
b41e9852 | 7555 | break; |
b41e9852 | 7556 | } while (1); |
2b188cc1 | 7557 | |
44df58d4 XW |
7558 | if (uts) { |
7559 | struct timespec64 ts; | |
7560 | ||
7561 | if (get_timespec64(&ts, uts)) | |
7562 | return -EFAULT; | |
7563 | timeout = timespec64_to_jiffies(&ts); | |
7564 | } | |
7565 | ||
2b188cc1 | 7566 | if (sig) { |
9e75ad5d AB |
7567 | #ifdef CONFIG_COMPAT |
7568 | if (in_compat_syscall()) | |
7569 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 7570 | sigsz); |
9e75ad5d AB |
7571 | else |
7572 | #endif | |
b772434b | 7573 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 7574 | |
2b188cc1 JA |
7575 | if (ret) |
7576 | return ret; | |
7577 | } | |
7578 | ||
90291099 PB |
7579 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
7580 | iowq.wq.private = current; | |
7581 | INIT_LIST_HEAD(&iowq.wq.entry); | |
7582 | iowq.ctx = ctx; | |
bda52162 | 7583 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
5fd46178 | 7584 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
90291099 | 7585 | |
c826bd7a | 7586 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 7587 | do { |
ca0a2651 | 7588 | /* if we can't even flush overflow, don't wait for more */ |
90f67366 | 7589 | if (!io_cqring_overflow_flush(ctx)) { |
ca0a2651 JA |
7590 | ret = -EBUSY; |
7591 | break; | |
7592 | } | |
311997b3 | 7593 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
bda52162 | 7594 | TASK_INTERRUPTIBLE); |
eeb60b9a | 7595 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
311997b3 | 7596 | finish_wait(&ctx->cq_wait, &iowq.wq); |
ca0a2651 | 7597 | cond_resched(); |
eeb60b9a | 7598 | } while (ret > 0); |
bda52162 | 7599 | |
b7db41c9 | 7600 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7601 | |
75b28aff | 7602 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7603 | } |
7604 | ||
9123c8ff | 7605 | static void io_free_page_table(void **table, size_t size) |
05f3fb3c | 7606 | { |
9123c8ff | 7607 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
05f3fb3c | 7608 | |
846a4ef2 | 7609 | for (i = 0; i < nr_tables; i++) |
9123c8ff PB |
7610 | kfree(table[i]); |
7611 | kfree(table); | |
7612 | } | |
7613 | ||
c072481d | 7614 | static __cold void **io_alloc_page_table(size_t size) |
9123c8ff PB |
7615 | { |
7616 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); | |
7617 | size_t init_size = size; | |
7618 | void **table; | |
7619 | ||
0bea96f5 | 7620 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7621 | if (!table) |
7622 | return NULL; | |
7623 | ||
7624 | for (i = 0; i < nr_tables; i++) { | |
27f6b318 | 7625 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
9123c8ff | 7626 | |
0bea96f5 | 7627 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7628 | if (!table[i]) { |
7629 | io_free_page_table(table, init_size); | |
7630 | return NULL; | |
7631 | } | |
7632 | size -= this_size; | |
7633 | } | |
7634 | return table; | |
05f3fb3c JA |
7635 | } |
7636 | ||
28a9fe25 | 7637 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
1642b445 | 7638 | { |
28a9fe25 PB |
7639 | percpu_ref_exit(&ref_node->refs); |
7640 | kfree(ref_node); | |
1642b445 PB |
7641 | } |
7642 | ||
c072481d | 7643 | static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
b9bd2bea PB |
7644 | { |
7645 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); | |
7646 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; | |
7647 | unsigned long flags; | |
7648 | bool first_add = false; | |
7649 | ||
7650 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); | |
7651 | node->done = true; | |
7652 | ||
7653 | while (!list_empty(&ctx->rsrc_ref_list)) { | |
7654 | node = list_first_entry(&ctx->rsrc_ref_list, | |
7655 | struct io_rsrc_node, node); | |
7656 | /* recycle ref nodes in order */ | |
7657 | if (!node->done) | |
7658 | break; | |
7659 | list_del(&node->node); | |
7660 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); | |
7661 | } | |
7662 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); | |
7663 | ||
7664 | if (first_add) | |
7665 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ); | |
7666 | } | |
7667 | ||
7668 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx) | |
7669 | { | |
7670 | struct io_rsrc_node *ref_node; | |
7671 | ||
7672 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); | |
7673 | if (!ref_node) | |
7674 | return NULL; | |
7675 | ||
7676 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, | |
7677 | 0, GFP_KERNEL)) { | |
7678 | kfree(ref_node); | |
7679 | return NULL; | |
7680 | } | |
7681 | INIT_LIST_HEAD(&ref_node->node); | |
7682 | INIT_LIST_HEAD(&ref_node->rsrc_list); | |
7683 | ref_node->done = false; | |
7684 | return ref_node; | |
7685 | } | |
7686 | ||
a7f0ed5a PB |
7687 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
7688 | struct io_rsrc_data *data_to_kill) | |
ab409402 | 7689 | __must_hold(&ctx->uring_lock) |
6b06314c | 7690 | { |
a7f0ed5a PB |
7691 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
7692 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); | |
6b06314c | 7693 | |
ab409402 PB |
7694 | io_rsrc_refs_drop(ctx); |
7695 | ||
a7f0ed5a PB |
7696 | if (data_to_kill) { |
7697 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; | |
82fbcfa9 | 7698 | |
a7f0ed5a | 7699 | rsrc_node->rsrc_data = data_to_kill; |
4956b9ea | 7700 | spin_lock_irq(&ctx->rsrc_ref_lock); |
a7f0ed5a | 7701 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
4956b9ea | 7702 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
82fbcfa9 | 7703 | |
3e942498 | 7704 | atomic_inc(&data_to_kill->refs); |
a7f0ed5a PB |
7705 | percpu_ref_kill(&rsrc_node->refs); |
7706 | ctx->rsrc_node = NULL; | |
7707 | } | |
6b06314c | 7708 | |
a7f0ed5a PB |
7709 | if (!ctx->rsrc_node) { |
7710 | ctx->rsrc_node = ctx->rsrc_backup_node; | |
7711 | ctx->rsrc_backup_node = NULL; | |
7712 | } | |
8bad28d8 HX |
7713 | } |
7714 | ||
a7f0ed5a | 7715 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
8dd03afe PB |
7716 | { |
7717 | if (ctx->rsrc_backup_node) | |
7718 | return 0; | |
b895c9a6 | 7719 | ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx); |
8dd03afe | 7720 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
8bad28d8 HX |
7721 | } |
7722 | ||
c072481d PB |
7723 | static __cold int io_rsrc_ref_quiesce(struct io_rsrc_data *data, |
7724 | struct io_ring_ctx *ctx) | |
8bad28d8 HX |
7725 | { |
7726 | int ret; | |
05589553 | 7727 | |
215c3902 | 7728 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
8bad28d8 HX |
7729 | if (data->quiesce) |
7730 | return -ENXIO; | |
05589553 | 7731 | |
8bad28d8 | 7732 | data->quiesce = true; |
1ffc5422 | 7733 | do { |
a7f0ed5a | 7734 | ret = io_rsrc_node_switch_start(ctx); |
8dd03afe | 7735 | if (ret) |
f2303b1f | 7736 | break; |
a7f0ed5a | 7737 | io_rsrc_node_switch(ctx, data); |
f2303b1f | 7738 | |
3e942498 PB |
7739 | /* kill initial ref, already quiesced if zero */ |
7740 | if (atomic_dec_and_test(&data->refs)) | |
7741 | break; | |
c018db4a | 7742 | mutex_unlock(&ctx->uring_lock); |
8bad28d8 | 7743 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 | 7744 | ret = wait_for_completion_interruptible(&data->done); |
c018db4a JA |
7745 | if (!ret) { |
7746 | mutex_lock(&ctx->uring_lock); | |
1ffc5422 | 7747 | break; |
c018db4a | 7748 | } |
8bad28d8 | 7749 | |
3e942498 PB |
7750 | atomic_inc(&data->refs); |
7751 | /* wait for all works potentially completing data->done */ | |
7752 | flush_delayed_work(&ctx->rsrc_put_work); | |
cb5e1b81 | 7753 | reinit_completion(&data->done); |
8dd03afe | 7754 | |
1ffc5422 | 7755 | ret = io_run_task_work_sig(); |
8bad28d8 | 7756 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 7757 | } while (ret >= 0); |
8bad28d8 | 7758 | data->quiesce = false; |
05f3fb3c | 7759 | |
8bad28d8 | 7760 | return ret; |
d7954b2b BM |
7761 | } |
7762 | ||
2d091d62 PB |
7763 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
7764 | { | |
7765 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; | |
7766 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; | |
7767 | ||
7768 | return &data->tags[table_idx][off]; | |
7769 | } | |
7770 | ||
44b31f2f | 7771 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
1ad555c6 | 7772 | { |
2d091d62 PB |
7773 | size_t size = data->nr * sizeof(data->tags[0][0]); |
7774 | ||
7775 | if (data->tags) | |
7776 | io_free_page_table((void **)data->tags, size); | |
44b31f2f PB |
7777 | kfree(data); |
7778 | } | |
7779 | ||
c072481d PB |
7780 | static __cold int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
7781 | u64 __user *utags, unsigned nr, | |
7782 | struct io_rsrc_data **pdata) | |
1ad555c6 | 7783 | { |
b895c9a6 | 7784 | struct io_rsrc_data *data; |
2d091d62 | 7785 | int ret = -ENOMEM; |
d878c816 | 7786 | unsigned i; |
1ad555c6 BM |
7787 | |
7788 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7789 | if (!data) | |
d878c816 | 7790 | return -ENOMEM; |
2d091d62 | 7791 | data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0])); |
b60c8dce | 7792 | if (!data->tags) { |
1ad555c6 | 7793 | kfree(data); |
d878c816 PB |
7794 | return -ENOMEM; |
7795 | } | |
2d091d62 PB |
7796 | |
7797 | data->nr = nr; | |
7798 | data->ctx = ctx; | |
7799 | data->do_put = do_put; | |
d878c816 | 7800 | if (utags) { |
2d091d62 | 7801 | ret = -EFAULT; |
d878c816 | 7802 | for (i = 0; i < nr; i++) { |
fdd1dc31 CIK |
7803 | u64 *tag_slot = io_get_tag_slot(data, i); |
7804 | ||
7805 | if (copy_from_user(tag_slot, &utags[i], | |
7806 | sizeof(*tag_slot))) | |
2d091d62 | 7807 | goto fail; |
d878c816 | 7808 | } |
1ad555c6 | 7809 | } |
b60c8dce | 7810 | |
3e942498 | 7811 | atomic_set(&data->refs, 1); |
1ad555c6 | 7812 | init_completion(&data->done); |
d878c816 PB |
7813 | *pdata = data; |
7814 | return 0; | |
2d091d62 PB |
7815 | fail: |
7816 | io_rsrc_data_free(data); | |
7817 | return ret; | |
1ad555c6 BM |
7818 | } |
7819 | ||
9123c8ff PB |
7820 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
7821 | { | |
0bea96f5 PB |
7822 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
7823 | GFP_KERNEL_ACCOUNT); | |
9123c8ff PB |
7824 | return !!table->files; |
7825 | } | |
7826 | ||
042b0d85 | 7827 | static void io_free_file_tables(struct io_file_table *table) |
9123c8ff | 7828 | { |
042b0d85 | 7829 | kvfree(table->files); |
9123c8ff PB |
7830 | table->files = NULL; |
7831 | } | |
7832 | ||
fff4db76 | 7833 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
1ad555c6 | 7834 | { |
fff4db76 PB |
7835 | #if defined(CONFIG_UNIX) |
7836 | if (ctx->ring_sock) { | |
7837 | struct sock *sock = ctx->ring_sock->sk; | |
7838 | struct sk_buff *skb; | |
7839 | ||
7840 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
7841 | kfree_skb(skb); | |
7842 | } | |
7843 | #else | |
7844 | int i; | |
7845 | ||
7846 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7847 | struct file *file; | |
7848 | ||
7849 | file = io_file_from_index(ctx, i); | |
7850 | if (file) | |
7851 | fput(file); | |
7852 | } | |
7853 | #endif | |
042b0d85 | 7854 | io_free_file_tables(&ctx->file_table); |
44b31f2f | 7855 | io_rsrc_data_free(ctx->file_data); |
fff4db76 PB |
7856 | ctx->file_data = NULL; |
7857 | ctx->nr_user_files = 0; | |
1ad555c6 BM |
7858 | } |
7859 | ||
d7954b2b BM |
7860 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7861 | { | |
d7954b2b BM |
7862 | int ret; |
7863 | ||
08480400 | 7864 | if (!ctx->file_data) |
d7954b2b | 7865 | return -ENXIO; |
08480400 PB |
7866 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
7867 | if (!ret) | |
7868 | __io_sqe_files_unregister(ctx); | |
7869 | return ret; | |
6b06314c JA |
7870 | } |
7871 | ||
37d1e2e3 | 7872 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
09a6f4ef | 7873 | __releases(&sqd->lock) |
37d1e2e3 | 7874 | { |
521d6a73 PB |
7875 | WARN_ON_ONCE(sqd->thread == current); |
7876 | ||
9e138a48 PB |
7877 | /* |
7878 | * Do the dance but not conditional clear_bit() because it'd race with | |
7879 | * other threads incrementing park_pending and setting the bit. | |
7880 | */ | |
37d1e2e3 | 7881 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
9e138a48 PB |
7882 | if (atomic_dec_return(&sqd->park_pending)) |
7883 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
09a6f4ef | 7884 | mutex_unlock(&sqd->lock); |
37d1e2e3 JA |
7885 | } |
7886 | ||
86e0d676 | 7887 | static void io_sq_thread_park(struct io_sq_data *sqd) |
09a6f4ef | 7888 | __acquires(&sqd->lock) |
37d1e2e3 | 7889 | { |
521d6a73 PB |
7890 | WARN_ON_ONCE(sqd->thread == current); |
7891 | ||
9e138a48 | 7892 | atomic_inc(&sqd->park_pending); |
86e0d676 | 7893 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
09a6f4ef | 7894 | mutex_lock(&sqd->lock); |
05962f95 | 7895 | if (sqd->thread) |
86e0d676 | 7896 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
7897 | } |
7898 | ||
7899 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
7900 | { | |
521d6a73 | 7901 | WARN_ON_ONCE(sqd->thread == current); |
88885f66 | 7902 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
521d6a73 | 7903 | |
05962f95 | 7904 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
88885f66 | 7905 | mutex_lock(&sqd->lock); |
e8f98f24 JA |
7906 | if (sqd->thread) |
7907 | wake_up_process(sqd->thread); | |
09a6f4ef | 7908 | mutex_unlock(&sqd->lock); |
05962f95 | 7909 | wait_for_completion(&sqd->exited); |
37d1e2e3 JA |
7910 | } |
7911 | ||
534ca6d6 | 7912 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7913 | { |
534ca6d6 | 7914 | if (refcount_dec_and_test(&sqd->refs)) { |
9e138a48 PB |
7915 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
7916 | ||
37d1e2e3 JA |
7917 | io_sq_thread_stop(sqd); |
7918 | kfree(sqd); | |
7919 | } | |
7920 | } | |
7921 | ||
7922 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
7923 | { | |
7924 | struct io_sq_data *sqd = ctx->sq_data; | |
7925 | ||
7926 | if (sqd) { | |
05962f95 | 7927 | io_sq_thread_park(sqd); |
521d6a73 | 7928 | list_del_init(&ctx->sqd_list); |
37d1e2e3 | 7929 | io_sqd_update_thread_idle(sqd); |
05962f95 | 7930 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
7931 | |
7932 | io_put_sq_data(sqd); | |
7933 | ctx->sq_data = NULL; | |
534ca6d6 JA |
7934 | } |
7935 | } | |
7936 | ||
aa06165d JA |
7937 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7938 | { | |
7939 | struct io_ring_ctx *ctx_attach; | |
7940 | struct io_sq_data *sqd; | |
7941 | struct fd f; | |
7942 | ||
7943 | f = fdget(p->wq_fd); | |
7944 | if (!f.file) | |
7945 | return ERR_PTR(-ENXIO); | |
7946 | if (f.file->f_op != &io_uring_fops) { | |
7947 | fdput(f); | |
7948 | return ERR_PTR(-EINVAL); | |
7949 | } | |
7950 | ||
7951 | ctx_attach = f.file->private_data; | |
7952 | sqd = ctx_attach->sq_data; | |
7953 | if (!sqd) { | |
7954 | fdput(f); | |
7955 | return ERR_PTR(-EINVAL); | |
7956 | } | |
5c2469e0 JA |
7957 | if (sqd->task_tgid != current->tgid) { |
7958 | fdput(f); | |
7959 | return ERR_PTR(-EPERM); | |
7960 | } | |
aa06165d JA |
7961 | |
7962 | refcount_inc(&sqd->refs); | |
7963 | fdput(f); | |
7964 | return sqd; | |
7965 | } | |
7966 | ||
26984fbf PB |
7967 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
7968 | bool *attached) | |
534ca6d6 JA |
7969 | { |
7970 | struct io_sq_data *sqd; | |
7971 | ||
26984fbf | 7972 | *attached = false; |
5c2469e0 JA |
7973 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
7974 | sqd = io_attach_sq_data(p); | |
26984fbf PB |
7975 | if (!IS_ERR(sqd)) { |
7976 | *attached = true; | |
5c2469e0 | 7977 | return sqd; |
26984fbf | 7978 | } |
5c2469e0 JA |
7979 | /* fall through for EPERM case, setup new sqd/task */ |
7980 | if (PTR_ERR(sqd) != -EPERM) | |
7981 | return sqd; | |
7982 | } | |
aa06165d | 7983 | |
534ca6d6 JA |
7984 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7985 | if (!sqd) | |
7986 | return ERR_PTR(-ENOMEM); | |
7987 | ||
9e138a48 | 7988 | atomic_set(&sqd->park_pending, 0); |
534ca6d6 | 7989 | refcount_set(&sqd->refs, 1); |
69fb2131 | 7990 | INIT_LIST_HEAD(&sqd->ctx_list); |
09a6f4ef | 7991 | mutex_init(&sqd->lock); |
534ca6d6 | 7992 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 7993 | init_completion(&sqd->exited); |
534ca6d6 JA |
7994 | return sqd; |
7995 | } | |
7996 | ||
6b06314c | 7997 | #if defined(CONFIG_UNIX) |
6b06314c JA |
7998 | /* |
7999 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
8000 | * the io_uring can be safely unregistered on process exit, even if we have | |
8001 | * loops in the file referencing. | |
8002 | */ | |
8003 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
8004 | { | |
8005 | struct sock *sk = ctx->ring_sock->sk; | |
8006 | struct scm_fp_list *fpl; | |
8007 | struct sk_buff *skb; | |
08a45173 | 8008 | int i, nr_files; |
6b06314c | 8009 | |
6b06314c JA |
8010 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
8011 | if (!fpl) | |
8012 | return -ENOMEM; | |
8013 | ||
8014 | skb = alloc_skb(0, GFP_KERNEL); | |
8015 | if (!skb) { | |
8016 | kfree(fpl); | |
8017 | return -ENOMEM; | |
8018 | } | |
8019 | ||
8020 | skb->sk = sk; | |
6b06314c | 8021 | |
08a45173 | 8022 | nr_files = 0; |
62e398be | 8023 | fpl->user = get_uid(current_user()); |
6b06314c | 8024 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
8025 | struct file *file = io_file_from_index(ctx, i + offset); |
8026 | ||
8027 | if (!file) | |
08a45173 | 8028 | continue; |
65e19f54 | 8029 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
8030 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
8031 | nr_files++; | |
6b06314c JA |
8032 | } |
8033 | ||
08a45173 JA |
8034 | if (nr_files) { |
8035 | fpl->max = SCM_MAX_FD; | |
8036 | fpl->count = nr_files; | |
8037 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 8038 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
8039 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
8040 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 8041 | |
08a45173 JA |
8042 | for (i = 0; i < nr_files; i++) |
8043 | fput(fpl->fp[i]); | |
8044 | } else { | |
8045 | kfree_skb(skb); | |
8046 | kfree(fpl); | |
8047 | } | |
6b06314c JA |
8048 | |
8049 | return 0; | |
8050 | } | |
8051 | ||
8052 | /* | |
8053 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
8054 | * causes regular reference counting to break down. We rely on the UNIX | |
8055 | * garbage collection to take care of this problem for us. | |
8056 | */ | |
8057 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8058 | { | |
8059 | unsigned left, total; | |
8060 | int ret = 0; | |
8061 | ||
8062 | total = 0; | |
8063 | left = ctx->nr_user_files; | |
8064 | while (left) { | |
8065 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
8066 | |
8067 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
8068 | if (ret) | |
8069 | break; | |
8070 | left -= this_files; | |
8071 | total += this_files; | |
8072 | } | |
8073 | ||
8074 | if (!ret) | |
8075 | return 0; | |
8076 | ||
8077 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
8078 | struct file *file = io_file_from_index(ctx, total); |
8079 | ||
8080 | if (file) | |
8081 | fput(file); | |
6b06314c JA |
8082 | total++; |
8083 | } | |
8084 | ||
8085 | return ret; | |
8086 | } | |
8087 | #else | |
8088 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8089 | { | |
8090 | return 0; | |
8091 | } | |
8092 | #endif | |
8093 | ||
47e90392 | 8094 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 8095 | { |
50238531 | 8096 | struct file *file = prsrc->file; |
05f3fb3c JA |
8097 | #if defined(CONFIG_UNIX) |
8098 | struct sock *sock = ctx->ring_sock->sk; | |
8099 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
8100 | struct sk_buff *skb; | |
8101 | int i; | |
8102 | ||
8103 | __skb_queue_head_init(&list); | |
8104 | ||
8105 | /* | |
8106 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
8107 | * remove this entry and rearrange the file array. | |
8108 | */ | |
8109 | skb = skb_dequeue(head); | |
8110 | while (skb) { | |
8111 | struct scm_fp_list *fp; | |
8112 | ||
8113 | fp = UNIXCB(skb).fp; | |
8114 | for (i = 0; i < fp->count; i++) { | |
8115 | int left; | |
8116 | ||
8117 | if (fp->fp[i] != file) | |
8118 | continue; | |
8119 | ||
8120 | unix_notinflight(fp->user, fp->fp[i]); | |
8121 | left = fp->count - 1 - i; | |
8122 | if (left) { | |
8123 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
8124 | left * sizeof(struct file *)); | |
8125 | } | |
8126 | fp->count--; | |
8127 | if (!fp->count) { | |
8128 | kfree_skb(skb); | |
8129 | skb = NULL; | |
8130 | } else { | |
8131 | __skb_queue_tail(&list, skb); | |
8132 | } | |
8133 | fput(file); | |
8134 | file = NULL; | |
8135 | break; | |
8136 | } | |
8137 | ||
8138 | if (!file) | |
8139 | break; | |
8140 | ||
8141 | __skb_queue_tail(&list, skb); | |
8142 | ||
8143 | skb = skb_dequeue(head); | |
8144 | } | |
8145 | ||
8146 | if (skb_peek(&list)) { | |
8147 | spin_lock_irq(&head->lock); | |
8148 | while ((skb = __skb_dequeue(&list)) != NULL) | |
8149 | __skb_queue_tail(head, skb); | |
8150 | spin_unlock_irq(&head->lock); | |
8151 | } | |
8152 | #else | |
8153 | fput(file); | |
8154 | #endif | |
8155 | } | |
8156 | ||
b895c9a6 | 8157 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
65e19f54 | 8158 | { |
b895c9a6 | 8159 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
269bbe5f BM |
8160 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
8161 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 8162 | |
269bbe5f BM |
8163 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
8164 | list_del(&prsrc->list); | |
b60c8dce PB |
8165 | |
8166 | if (prsrc->tag) { | |
8167 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; | |
b60c8dce PB |
8168 | |
8169 | io_ring_submit_lock(ctx, lock_ring); | |
79ebeaee | 8170 | spin_lock(&ctx->completion_lock); |
b60c8dce | 8171 | io_cqring_fill_event(ctx, prsrc->tag, 0, 0); |
2840f710 | 8172 | ctx->cq_extra++; |
b60c8dce | 8173 | io_commit_cqring(ctx); |
79ebeaee | 8174 | spin_unlock(&ctx->completion_lock); |
b60c8dce PB |
8175 | io_cqring_ev_posted(ctx); |
8176 | io_ring_submit_unlock(ctx, lock_ring); | |
8177 | } | |
8178 | ||
40ae0ff7 | 8179 | rsrc_data->do_put(ctx, prsrc); |
269bbe5f | 8180 | kfree(prsrc); |
65e19f54 | 8181 | } |
05589553 | 8182 | |
28a9fe25 | 8183 | io_rsrc_node_destroy(ref_node); |
3e942498 PB |
8184 | if (atomic_dec_and_test(&rsrc_data->refs)) |
8185 | complete(&rsrc_data->done); | |
2faf852d | 8186 | } |
65e19f54 | 8187 | |
269bbe5f | 8188 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
8189 | { |
8190 | struct io_ring_ctx *ctx; | |
8191 | struct llist_node *node; | |
8192 | ||
269bbe5f BM |
8193 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
8194 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
8195 | |
8196 | while (node) { | |
b895c9a6 | 8197 | struct io_rsrc_node *ref_node; |
4a38aed2 JA |
8198 | struct llist_node *next = node->next; |
8199 | ||
b895c9a6 | 8200 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
269bbe5f | 8201 | __io_rsrc_put_work(ref_node); |
4a38aed2 JA |
8202 | node = next; |
8203 | } | |
8204 | } | |
8205 | ||
6b06314c | 8206 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
792e3582 | 8207 | unsigned nr_args, u64 __user *tags) |
6b06314c JA |
8208 | { |
8209 | __s32 __user *fds = (__s32 __user *) arg; | |
05f3fb3c | 8210 | struct file *file; |
f3baed39 | 8211 | int fd, ret; |
846a4ef2 | 8212 | unsigned i; |
6b06314c | 8213 | |
05f3fb3c | 8214 | if (ctx->file_data) |
6b06314c JA |
8215 | return -EBUSY; |
8216 | if (!nr_args) | |
8217 | return -EINVAL; | |
8218 | if (nr_args > IORING_MAX_FIXED_FILES) | |
8219 | return -EMFILE; | |
3a1b8a4e PB |
8220 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
8221 | return -EMFILE; | |
a7f0ed5a | 8222 | ret = io_rsrc_node_switch_start(ctx); |
f3baed39 PB |
8223 | if (ret) |
8224 | return ret; | |
d878c816 PB |
8225 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
8226 | &ctx->file_data); | |
8227 | if (ret) | |
8228 | return ret; | |
6b06314c | 8229 | |
f3baed39 | 8230 | ret = -ENOMEM; |
aeca241b | 8231 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
1ad555c6 | 8232 | goto out_free; |
65e19f54 | 8233 | |
08a45173 | 8234 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
d878c816 | 8235 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
600cf3f8 PB |
8236 | ret = -EFAULT; |
8237 | goto out_fput; | |
8238 | } | |
08a45173 | 8239 | /* allow sparse sets */ |
792e3582 PB |
8240 | if (fd == -1) { |
8241 | ret = -EINVAL; | |
2d091d62 | 8242 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
792e3582 | 8243 | goto out_fput; |
08a45173 | 8244 | continue; |
792e3582 | 8245 | } |
6b06314c | 8246 | |
05f3fb3c | 8247 | file = fget(fd); |
6b06314c | 8248 | ret = -EBADF; |
792e3582 | 8249 | if (unlikely(!file)) |
600cf3f8 | 8250 | goto out_fput; |
05f3fb3c | 8251 | |
6b06314c JA |
8252 | /* |
8253 | * Don't allow io_uring instances to be registered. If UNIX | |
8254 | * isn't enabled, then this causes a reference cycle and this | |
8255 | * instance can never get freed. If UNIX is enabled we'll | |
8256 | * handle it just fine, but there's still no point in allowing | |
8257 | * a ring fd as it doesn't support regular read/write anyway. | |
8258 | */ | |
05f3fb3c JA |
8259 | if (file->f_op == &io_uring_fops) { |
8260 | fput(file); | |
600cf3f8 | 8261 | goto out_fput; |
6b06314c | 8262 | } |
aeca241b | 8263 | io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file); |
6b06314c JA |
8264 | } |
8265 | ||
6b06314c | 8266 | ret = io_sqe_files_scm(ctx); |
05589553 | 8267 | if (ret) { |
08480400 | 8268 | __io_sqe_files_unregister(ctx); |
05589553 XW |
8269 | return ret; |
8270 | } | |
6b06314c | 8271 | |
a7f0ed5a | 8272 | io_rsrc_node_switch(ctx, NULL); |
6b06314c | 8273 | return ret; |
600cf3f8 PB |
8274 | out_fput: |
8275 | for (i = 0; i < ctx->nr_user_files; i++) { | |
8276 | file = io_file_from_index(ctx, i); | |
8277 | if (file) | |
8278 | fput(file); | |
8279 | } | |
042b0d85 | 8280 | io_free_file_tables(&ctx->file_table); |
600cf3f8 | 8281 | ctx->nr_user_files = 0; |
600cf3f8 | 8282 | out_free: |
44b31f2f | 8283 | io_rsrc_data_free(ctx->file_data); |
55cbc256 | 8284 | ctx->file_data = NULL; |
6b06314c JA |
8285 | return ret; |
8286 | } | |
8287 | ||
c3a31e60 JA |
8288 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
8289 | int index) | |
8290 | { | |
8291 | #if defined(CONFIG_UNIX) | |
8292 | struct sock *sock = ctx->ring_sock->sk; | |
8293 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
8294 | struct sk_buff *skb; | |
8295 | ||
8296 | /* | |
8297 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
8298 | * file set. If there's no room, fall back to allocating a new skb | |
8299 | * and filling it in. | |
8300 | */ | |
8301 | spin_lock_irq(&head->lock); | |
8302 | skb = skb_peek(head); | |
8303 | if (skb) { | |
8304 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
8305 | ||
8306 | if (fpl->count < SCM_MAX_FD) { | |
8307 | __skb_unlink(skb, head); | |
8308 | spin_unlock_irq(&head->lock); | |
8309 | fpl->fp[fpl->count] = get_file(file); | |
8310 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
8311 | fpl->count++; | |
8312 | spin_lock_irq(&head->lock); | |
8313 | __skb_queue_head(head, skb); | |
8314 | } else { | |
8315 | skb = NULL; | |
8316 | } | |
8317 | } | |
8318 | spin_unlock_irq(&head->lock); | |
8319 | ||
8320 | if (skb) { | |
8321 | fput(file); | |
8322 | return 0; | |
8323 | } | |
8324 | ||
8325 | return __io_sqe_files_scm(ctx, 1, index); | |
8326 | #else | |
8327 | return 0; | |
8328 | #endif | |
8329 | } | |
8330 | ||
9c7b0ba8 PB |
8331 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
8332 | struct io_rsrc_node *node, void *rsrc) | |
8333 | { | |
8334 | struct io_rsrc_put *prsrc; | |
8335 | ||
8336 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); | |
8337 | if (!prsrc) | |
8338 | return -ENOMEM; | |
8339 | ||
8340 | prsrc->tag = *io_get_tag_slot(data, idx); | |
8341 | prsrc->rsrc = rsrc; | |
8342 | list_add(&prsrc->list, &node->rsrc_list); | |
8343 | return 0; | |
8344 | } | |
8345 | ||
b9445598 PB |
8346 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
8347 | unsigned int issue_flags, u32 slot_index) | |
8348 | { | |
8349 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 8350 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
9c7b0ba8 | 8351 | bool needs_switch = false; |
b9445598 PB |
8352 | struct io_fixed_file *file_slot; |
8353 | int ret = -EBADF; | |
8354 | ||
3b44b371 | 8355 | io_ring_submit_lock(ctx, needs_lock); |
b9445598 PB |
8356 | if (file->f_op == &io_uring_fops) |
8357 | goto err; | |
8358 | ret = -ENXIO; | |
8359 | if (!ctx->file_data) | |
8360 | goto err; | |
8361 | ret = -EINVAL; | |
8362 | if (slot_index >= ctx->nr_user_files) | |
8363 | goto err; | |
8364 | ||
8365 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); | |
8366 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); | |
9c7b0ba8 PB |
8367 | |
8368 | if (file_slot->file_ptr) { | |
8369 | struct file *old_file; | |
8370 | ||
8371 | ret = io_rsrc_node_switch_start(ctx); | |
8372 | if (ret) | |
8373 | goto err; | |
8374 | ||
8375 | old_file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8376 | ret = io_queue_rsrc_removal(ctx->file_data, slot_index, | |
8377 | ctx->rsrc_node, old_file); | |
8378 | if (ret) | |
8379 | goto err; | |
8380 | file_slot->file_ptr = 0; | |
8381 | needs_switch = true; | |
8382 | } | |
b9445598 PB |
8383 | |
8384 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; | |
8385 | io_fixed_file_set(file_slot, file); | |
8386 | ret = io_sqe_file_register(ctx, file, slot_index); | |
8387 | if (ret) { | |
8388 | file_slot->file_ptr = 0; | |
8389 | goto err; | |
8390 | } | |
8391 | ||
8392 | ret = 0; | |
8393 | err: | |
9c7b0ba8 PB |
8394 | if (needs_switch) |
8395 | io_rsrc_node_switch(ctx, ctx->file_data); | |
3b44b371 | 8396 | io_ring_submit_unlock(ctx, needs_lock); |
b9445598 PB |
8397 | if (ret) |
8398 | fput(file); | |
8399 | return ret; | |
8400 | } | |
8401 | ||
7df778be PB |
8402 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) |
8403 | { | |
8404 | unsigned int offset = req->close.file_slot - 1; | |
8405 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 8406 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
7df778be PB |
8407 | struct io_fixed_file *file_slot; |
8408 | struct file *file; | |
8409 | int ret, i; | |
8410 | ||
3b44b371 | 8411 | io_ring_submit_lock(ctx, needs_lock); |
7df778be PB |
8412 | ret = -ENXIO; |
8413 | if (unlikely(!ctx->file_data)) | |
8414 | goto out; | |
8415 | ret = -EINVAL; | |
8416 | if (offset >= ctx->nr_user_files) | |
8417 | goto out; | |
8418 | ret = io_rsrc_node_switch_start(ctx); | |
8419 | if (ret) | |
8420 | goto out; | |
8421 | ||
8422 | i = array_index_nospec(offset, ctx->nr_user_files); | |
8423 | file_slot = io_fixed_file_slot(&ctx->file_table, i); | |
8424 | ret = -EBADF; | |
8425 | if (!file_slot->file_ptr) | |
8426 | goto out; | |
8427 | ||
8428 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8429 | ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file); | |
8430 | if (ret) | |
8431 | goto out; | |
8432 | ||
8433 | file_slot->file_ptr = 0; | |
8434 | io_rsrc_node_switch(ctx, ctx->file_data); | |
8435 | ret = 0; | |
8436 | out: | |
3b44b371 | 8437 | io_ring_submit_unlock(ctx, needs_lock); |
7df778be PB |
8438 | return ret; |
8439 | } | |
8440 | ||
05f3fb3c | 8441 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
c3bdad02 | 8442 | struct io_uring_rsrc_update2 *up, |
05f3fb3c JA |
8443 | unsigned nr_args) |
8444 | { | |
c3bdad02 | 8445 | u64 __user *tags = u64_to_user_ptr(up->tags); |
98f0b3b4 | 8446 | __s32 __user *fds = u64_to_user_ptr(up->data); |
b895c9a6 | 8447 | struct io_rsrc_data *data = ctx->file_data; |
a04b0ac0 PB |
8448 | struct io_fixed_file *file_slot; |
8449 | struct file *file; | |
98f0b3b4 PB |
8450 | int fd, i, err = 0; |
8451 | unsigned int done; | |
05589553 | 8452 | bool needs_switch = false; |
c3a31e60 | 8453 | |
98f0b3b4 PB |
8454 | if (!ctx->file_data) |
8455 | return -ENXIO; | |
8456 | if (up->offset + nr_args > ctx->nr_user_files) | |
c3a31e60 JA |
8457 | return -EINVAL; |
8458 | ||
67973b93 | 8459 | for (done = 0; done < nr_args; done++) { |
c3bdad02 PB |
8460 | u64 tag = 0; |
8461 | ||
8462 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || | |
8463 | copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
c3a31e60 JA |
8464 | err = -EFAULT; |
8465 | break; | |
8466 | } | |
c3bdad02 PB |
8467 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
8468 | err = -EINVAL; | |
8469 | break; | |
8470 | } | |
4e0377a1 | 8471 | if (fd == IORING_REGISTER_FILES_SKIP) |
8472 | continue; | |
8473 | ||
67973b93 | 8474 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
aeca241b | 8475 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
ea64ec02 | 8476 | |
a04b0ac0 PB |
8477 | if (file_slot->file_ptr) { |
8478 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
b60c8dce PB |
8479 | err = io_queue_rsrc_removal(data, up->offset + done, |
8480 | ctx->rsrc_node, file); | |
a5318d3c HD |
8481 | if (err) |
8482 | break; | |
a04b0ac0 | 8483 | file_slot->file_ptr = 0; |
05589553 | 8484 | needs_switch = true; |
c3a31e60 JA |
8485 | } |
8486 | if (fd != -1) { | |
c3a31e60 JA |
8487 | file = fget(fd); |
8488 | if (!file) { | |
8489 | err = -EBADF; | |
8490 | break; | |
8491 | } | |
8492 | /* | |
8493 | * Don't allow io_uring instances to be registered. If | |
8494 | * UNIX isn't enabled, then this causes a reference | |
8495 | * cycle and this instance can never get freed. If UNIX | |
8496 | * is enabled we'll handle it just fine, but there's | |
8497 | * still no point in allowing a ring fd as it doesn't | |
8498 | * support regular read/write anyway. | |
8499 | */ | |
8500 | if (file->f_op == &io_uring_fops) { | |
8501 | fput(file); | |
8502 | err = -EBADF; | |
8503 | break; | |
8504 | } | |
2d091d62 | 8505 | *io_get_tag_slot(data, up->offset + done) = tag; |
9a321c98 | 8506 | io_fixed_file_set(file_slot, file); |
c3a31e60 | 8507 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 8508 | if (err) { |
a04b0ac0 | 8509 | file_slot->file_ptr = 0; |
f3bd9dae | 8510 | fput(file); |
c3a31e60 | 8511 | break; |
f3bd9dae | 8512 | } |
c3a31e60 | 8513 | } |
05f3fb3c JA |
8514 | } |
8515 | ||
a7f0ed5a PB |
8516 | if (needs_switch) |
8517 | io_rsrc_node_switch(ctx, data); | |
c3a31e60 JA |
8518 | return done ? done : err; |
8519 | } | |
05589553 | 8520 | |
685fe7fe JA |
8521 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
8522 | struct task_struct *task) | |
24369c2e | 8523 | { |
e941894e | 8524 | struct io_wq_hash *hash; |
24369c2e | 8525 | struct io_wq_data data; |
24369c2e | 8526 | unsigned int concurrency; |
24369c2e | 8527 | |
362a9e65 | 8528 | mutex_lock(&ctx->uring_lock); |
e941894e JA |
8529 | hash = ctx->hash_map; |
8530 | if (!hash) { | |
8531 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
362a9e65 YY |
8532 | if (!hash) { |
8533 | mutex_unlock(&ctx->uring_lock); | |
e941894e | 8534 | return ERR_PTR(-ENOMEM); |
362a9e65 | 8535 | } |
e941894e JA |
8536 | refcount_set(&hash->refs, 1); |
8537 | init_waitqueue_head(&hash->wait); | |
8538 | ctx->hash_map = hash; | |
24369c2e | 8539 | } |
362a9e65 | 8540 | mutex_unlock(&ctx->uring_lock); |
24369c2e | 8541 | |
e941894e | 8542 | data.hash = hash; |
685fe7fe | 8543 | data.task = task; |
ebc11b6c | 8544 | data.free_work = io_wq_free_work; |
f5fa38c5 | 8545 | data.do_work = io_wq_submit_work; |
24369c2e | 8546 | |
d25e3a3d JA |
8547 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
8548 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 8549 | |
5aa75ed5 | 8550 | return io_wq_create(concurrency, &data); |
24369c2e PB |
8551 | } |
8552 | ||
c072481d PB |
8553 | static __cold int io_uring_alloc_task_context(struct task_struct *task, |
8554 | struct io_ring_ctx *ctx) | |
0f212204 JA |
8555 | { |
8556 | struct io_uring_task *tctx; | |
d8a6df10 | 8557 | int ret; |
0f212204 | 8558 | |
09899b19 | 8559 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
0f212204 JA |
8560 | if (unlikely(!tctx)) |
8561 | return -ENOMEM; | |
8562 | ||
d8a6df10 JA |
8563 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
8564 | if (unlikely(ret)) { | |
8565 | kfree(tctx); | |
8566 | return ret; | |
8567 | } | |
8568 | ||
685fe7fe | 8569 | tctx->io_wq = io_init_wq_offload(ctx, task); |
5aa75ed5 JA |
8570 | if (IS_ERR(tctx->io_wq)) { |
8571 | ret = PTR_ERR(tctx->io_wq); | |
8572 | percpu_counter_destroy(&tctx->inflight); | |
8573 | kfree(tctx); | |
8574 | return ret; | |
8575 | } | |
8576 | ||
0f212204 JA |
8577 | xa_init(&tctx->xa); |
8578 | init_waitqueue_head(&tctx->wait); | |
fdaf083c | 8579 | atomic_set(&tctx->in_idle, 0); |
b303fe2e | 8580 | atomic_set(&tctx->inflight_tracked, 0); |
0f212204 | 8581 | task->io_uring = tctx; |
7cbf1722 JA |
8582 | spin_lock_init(&tctx->task_lock); |
8583 | INIT_WQ_LIST(&tctx->task_list); | |
7cbf1722 | 8584 | init_task_work(&tctx->task_work, tctx_task_work); |
0f212204 JA |
8585 | return 0; |
8586 | } | |
8587 | ||
8588 | void __io_uring_free(struct task_struct *tsk) | |
8589 | { | |
8590 | struct io_uring_task *tctx = tsk->io_uring; | |
8591 | ||
8592 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e | 8593 | WARN_ON_ONCE(tctx->io_wq); |
09899b19 | 8594 | WARN_ON_ONCE(tctx->cached_refs); |
ef8eaa4e | 8595 | |
d8a6df10 | 8596 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
8597 | kfree(tctx); |
8598 | tsk->io_uring = NULL; | |
8599 | } | |
8600 | ||
c072481d PB |
8601 | static __cold int io_sq_offload_create(struct io_ring_ctx *ctx, |
8602 | struct io_uring_params *p) | |
2b188cc1 JA |
8603 | { |
8604 | int ret; | |
8605 | ||
d25e3a3d JA |
8606 | /* Retain compatibility with failing for an invalid attach attempt */ |
8607 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
8608 | IORING_SETUP_ATTACH_WQ) { | |
8609 | struct fd f; | |
8610 | ||
8611 | f = fdget(p->wq_fd); | |
8612 | if (!f.file) | |
8613 | return -ENXIO; | |
0cc936f7 JA |
8614 | if (f.file->f_op != &io_uring_fops) { |
8615 | fdput(f); | |
f2a48dd0 | 8616 | return -EINVAL; |
0cc936f7 JA |
8617 | } |
8618 | fdput(f); | |
d25e3a3d | 8619 | } |
6c271ce2 | 8620 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 8621 | struct task_struct *tsk; |
534ca6d6 | 8622 | struct io_sq_data *sqd; |
26984fbf | 8623 | bool attached; |
534ca6d6 | 8624 | |
26984fbf | 8625 | sqd = io_get_sq_data(p, &attached); |
534ca6d6 JA |
8626 | if (IS_ERR(sqd)) { |
8627 | ret = PTR_ERR(sqd); | |
8628 | goto err; | |
8629 | } | |
69fb2131 | 8630 | |
7c30f36a | 8631 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 8632 | ctx->sq_data = sqd; |
917257da JA |
8633 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
8634 | if (!ctx->sq_thread_idle) | |
8635 | ctx->sq_thread_idle = HZ; | |
8636 | ||
78d7f6ba | 8637 | io_sq_thread_park(sqd); |
de75a3d3 PB |
8638 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
8639 | io_sqd_update_thread_idle(sqd); | |
26984fbf | 8640 | /* don't attach to a dying SQPOLL thread, would be racy */ |
f2a48dd0 | 8641 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
78d7f6ba PB |
8642 | io_sq_thread_unpark(sqd); |
8643 | ||
de75a3d3 PB |
8644 | if (ret < 0) |
8645 | goto err; | |
8646 | if (attached) | |
5aa75ed5 | 8647 | return 0; |
aa06165d | 8648 | |
6c271ce2 | 8649 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 8650 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 8651 | |
917257da | 8652 | ret = -EINVAL; |
f2a48dd0 | 8653 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
e8f98f24 | 8654 | goto err_sqpoll; |
37d1e2e3 | 8655 | sqd->sq_cpu = cpu; |
6c271ce2 | 8656 | } else { |
37d1e2e3 | 8657 | sqd->sq_cpu = -1; |
6c271ce2 | 8658 | } |
37d1e2e3 JA |
8659 | |
8660 | sqd->task_pid = current->pid; | |
5c2469e0 | 8661 | sqd->task_tgid = current->tgid; |
46fe18b1 JA |
8662 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
8663 | if (IS_ERR(tsk)) { | |
8664 | ret = PTR_ERR(tsk); | |
e8f98f24 | 8665 | goto err_sqpoll; |
6c271ce2 | 8666 | } |
97a73a0f | 8667 | |
46fe18b1 | 8668 | sqd->thread = tsk; |
97a73a0f | 8669 | ret = io_uring_alloc_task_context(tsk, ctx); |
46fe18b1 | 8670 | wake_up_new_task(tsk); |
0f212204 JA |
8671 | if (ret) |
8672 | goto err; | |
6c271ce2 JA |
8673 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
8674 | /* Can't have SQ_AFF without SQPOLL */ | |
8675 | ret = -EINVAL; | |
8676 | goto err; | |
8677 | } | |
8678 | ||
2b188cc1 | 8679 | return 0; |
f2a48dd0 PB |
8680 | err_sqpoll: |
8681 | complete(&ctx->sq_data->exited); | |
2b188cc1 | 8682 | err: |
37d1e2e3 | 8683 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
8684 | return ret; |
8685 | } | |
8686 | ||
a087e2b5 BM |
8687 | static inline void __io_unaccount_mem(struct user_struct *user, |
8688 | unsigned long nr_pages) | |
2b188cc1 JA |
8689 | { |
8690 | atomic_long_sub(nr_pages, &user->locked_vm); | |
8691 | } | |
8692 | ||
a087e2b5 BM |
8693 | static inline int __io_account_mem(struct user_struct *user, |
8694 | unsigned long nr_pages) | |
2b188cc1 JA |
8695 | { |
8696 | unsigned long page_limit, cur_pages, new_pages; | |
8697 | ||
8698 | /* Don't allow more pages than we can safely lock */ | |
8699 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
8700 | ||
8701 | do { | |
8702 | cur_pages = atomic_long_read(&user->locked_vm); | |
8703 | new_pages = cur_pages + nr_pages; | |
8704 | if (new_pages > page_limit) | |
8705 | return -ENOMEM; | |
8706 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
8707 | new_pages) != cur_pages); | |
8708 | ||
8709 | return 0; | |
8710 | } | |
8711 | ||
26bfa89e | 8712 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8713 | { |
62e398be | 8714 | if (ctx->user) |
a087e2b5 | 8715 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 8716 | |
26bfa89e JA |
8717 | if (ctx->mm_account) |
8718 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8719 | } |
8720 | ||
26bfa89e | 8721 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8722 | { |
30975825 BM |
8723 | int ret; |
8724 | ||
62e398be | 8725 | if (ctx->user) { |
30975825 BM |
8726 | ret = __io_account_mem(ctx->user, nr_pages); |
8727 | if (ret) | |
8728 | return ret; | |
8729 | } | |
8730 | ||
26bfa89e JA |
8731 | if (ctx->mm_account) |
8732 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8733 | |
8734 | return 0; | |
8735 | } | |
8736 | ||
2b188cc1 JA |
8737 | static void io_mem_free(void *ptr) |
8738 | { | |
52e04ef4 MR |
8739 | struct page *page; |
8740 | ||
8741 | if (!ptr) | |
8742 | return; | |
2b188cc1 | 8743 | |
52e04ef4 | 8744 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8745 | if (put_page_testzero(page)) |
8746 | free_compound_page(page); | |
8747 | } | |
8748 | ||
8749 | static void *io_mem_alloc(size_t size) | |
8750 | { | |
8751 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
26bfa89e | 8752 | __GFP_NORETRY | __GFP_ACCOUNT; |
2b188cc1 JA |
8753 | |
8754 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8755 | } | |
8756 | ||
75b28aff HV |
8757 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8758 | size_t *sq_offset) | |
8759 | { | |
8760 | struct io_rings *rings; | |
8761 | size_t off, sq_array_size; | |
8762 | ||
8763 | off = struct_size(rings, cqes, cq_entries); | |
8764 | if (off == SIZE_MAX) | |
8765 | return SIZE_MAX; | |
8766 | ||
8767 | #ifdef CONFIG_SMP | |
8768 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8769 | if (off == 0) | |
8770 | return SIZE_MAX; | |
8771 | #endif | |
8772 | ||
b36200f5 DV |
8773 | if (sq_offset) |
8774 | *sq_offset = off; | |
8775 | ||
75b28aff HV |
8776 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8777 | if (sq_array_size == SIZE_MAX) | |
8778 | return SIZE_MAX; | |
8779 | ||
8780 | if (check_add_overflow(off, sq_array_size, &off)) | |
8781 | return SIZE_MAX; | |
8782 | ||
75b28aff HV |
8783 | return off; |
8784 | } | |
8785 | ||
41edf1a5 | 8786 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot) |
7f61a1e9 | 8787 | { |
41edf1a5 | 8788 | struct io_mapped_ubuf *imu = *slot; |
7f61a1e9 PB |
8789 | unsigned int i; |
8790 | ||
6224843d PB |
8791 | if (imu != ctx->dummy_ubuf) { |
8792 | for (i = 0; i < imu->nr_bvecs; i++) | |
8793 | unpin_user_page(imu->bvec[i].bv_page); | |
8794 | if (imu->acct_pages) | |
8795 | io_unaccount_mem(ctx, imu->acct_pages); | |
8796 | kvfree(imu); | |
8797 | } | |
41edf1a5 | 8798 | *slot = NULL; |
7f61a1e9 PB |
8799 | } |
8800 | ||
bd54b6fe | 8801 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
edafccee | 8802 | { |
634d00df PB |
8803 | io_buffer_unmap(ctx, &prsrc->buf); |
8804 | prsrc->buf = NULL; | |
bd54b6fe | 8805 | } |
edafccee | 8806 | |
bd54b6fe BM |
8807 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
8808 | { | |
8809 | unsigned int i; | |
edafccee | 8810 | |
7f61a1e9 PB |
8811 | for (i = 0; i < ctx->nr_user_bufs; i++) |
8812 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); | |
edafccee | 8813 | kfree(ctx->user_bufs); |
bb6659cc | 8814 | io_rsrc_data_free(ctx->buf_data); |
edafccee | 8815 | ctx->user_bufs = NULL; |
bd54b6fe | 8816 | ctx->buf_data = NULL; |
edafccee | 8817 | ctx->nr_user_bufs = 0; |
bd54b6fe BM |
8818 | } |
8819 | ||
0a96bbe4 | 8820 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee | 8821 | { |
bd54b6fe | 8822 | int ret; |
edafccee | 8823 | |
bd54b6fe | 8824 | if (!ctx->buf_data) |
edafccee JA |
8825 | return -ENXIO; |
8826 | ||
bd54b6fe BM |
8827 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
8828 | if (!ret) | |
8829 | __io_sqe_buffers_unregister(ctx); | |
8830 | return ret; | |
edafccee JA |
8831 | } |
8832 | ||
8833 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8834 | void __user *arg, unsigned index) | |
8835 | { | |
8836 | struct iovec __user *src; | |
8837 | ||
8838 | #ifdef CONFIG_COMPAT | |
8839 | if (ctx->compat) { | |
8840 | struct compat_iovec __user *ciovs; | |
8841 | struct compat_iovec ciov; | |
8842 | ||
8843 | ciovs = (struct compat_iovec __user *) arg; | |
8844 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8845 | return -EFAULT; | |
8846 | ||
d55e5f5b | 8847 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8848 | dst->iov_len = ciov.iov_len; |
8849 | return 0; | |
8850 | } | |
8851 | #endif | |
8852 | src = (struct iovec __user *) arg; | |
8853 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8854 | return -EFAULT; | |
8855 | return 0; | |
8856 | } | |
8857 | ||
de293938 JA |
8858 | /* |
8859 | * Not super efficient, but this is just a registration time. And we do cache | |
8860 | * the last compound head, so generally we'll only do a full search if we don't | |
8861 | * match that one. | |
8862 | * | |
8863 | * We check if the given compound head page has already been accounted, to | |
8864 | * avoid double accounting it. This allows us to account the full size of the | |
8865 | * page, not just the constituent pages of a huge page. | |
8866 | */ | |
8867 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8868 | int nr_pages, struct page *hpage) | |
8869 | { | |
8870 | int i, j; | |
8871 | ||
8872 | /* check current page array */ | |
8873 | for (i = 0; i < nr_pages; i++) { | |
8874 | if (!PageCompound(pages[i])) | |
8875 | continue; | |
8876 | if (compound_head(pages[i]) == hpage) | |
8877 | return true; | |
8878 | } | |
8879 | ||
8880 | /* check previously registered pages */ | |
8881 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
41edf1a5 | 8882 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
de293938 JA |
8883 | |
8884 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8885 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8886 | continue; | |
8887 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8888 | return true; | |
8889 | } | |
8890 | } | |
8891 | ||
8892 | return false; | |
8893 | } | |
8894 | ||
8895 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8896 | int nr_pages, struct io_mapped_ubuf *imu, | |
8897 | struct page **last_hpage) | |
8898 | { | |
8899 | int i, ret; | |
8900 | ||
216e5835 | 8901 | imu->acct_pages = 0; |
de293938 JA |
8902 | for (i = 0; i < nr_pages; i++) { |
8903 | if (!PageCompound(pages[i])) { | |
8904 | imu->acct_pages++; | |
8905 | } else { | |
8906 | struct page *hpage; | |
8907 | ||
8908 | hpage = compound_head(pages[i]); | |
8909 | if (hpage == *last_hpage) | |
8910 | continue; | |
8911 | *last_hpage = hpage; | |
8912 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8913 | continue; | |
8914 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8915 | } | |
8916 | } | |
8917 | ||
8918 | if (!imu->acct_pages) | |
8919 | return 0; | |
8920 | ||
26bfa89e | 8921 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
8922 | if (ret) |
8923 | imu->acct_pages = 0; | |
8924 | return ret; | |
8925 | } | |
8926 | ||
0a96bbe4 | 8927 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
41edf1a5 | 8928 | struct io_mapped_ubuf **pimu, |
0a96bbe4 | 8929 | struct page **last_hpage) |
edafccee | 8930 | { |
41edf1a5 | 8931 | struct io_mapped_ubuf *imu = NULL; |
edafccee JA |
8932 | struct vm_area_struct **vmas = NULL; |
8933 | struct page **pages = NULL; | |
0a96bbe4 BM |
8934 | unsigned long off, start, end, ubuf; |
8935 | size_t size; | |
8936 | int ret, pret, nr_pages, i; | |
8937 | ||
6224843d PB |
8938 | if (!iov->iov_base) { |
8939 | *pimu = ctx->dummy_ubuf; | |
8940 | return 0; | |
8941 | } | |
8942 | ||
0a96bbe4 BM |
8943 | ubuf = (unsigned long) iov->iov_base; |
8944 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8945 | start = ubuf >> PAGE_SHIFT; | |
8946 | nr_pages = end - start; | |
8947 | ||
41edf1a5 | 8948 | *pimu = NULL; |
0a96bbe4 BM |
8949 | ret = -ENOMEM; |
8950 | ||
8951 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8952 | if (!pages) | |
8953 | goto done; | |
8954 | ||
8955 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8956 | GFP_KERNEL); | |
8957 | if (!vmas) | |
8958 | goto done; | |
edafccee | 8959 | |
41edf1a5 | 8960 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
a2b4198c | 8961 | if (!imu) |
0a96bbe4 BM |
8962 | goto done; |
8963 | ||
8964 | ret = 0; | |
8965 | mmap_read_lock(current->mm); | |
8966 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8967 | pages, vmas); | |
8968 | if (pret == nr_pages) { | |
8969 | /* don't support file backed memory */ | |
8970 | for (i = 0; i < nr_pages; i++) { | |
8971 | struct vm_area_struct *vma = vmas[i]; | |
8972 | ||
40dad765 PB |
8973 | if (vma_is_shmem(vma)) |
8974 | continue; | |
0a96bbe4 BM |
8975 | if (vma->vm_file && |
8976 | !is_file_hugepages(vma->vm_file)) { | |
8977 | ret = -EOPNOTSUPP; | |
8978 | break; | |
8979 | } | |
8980 | } | |
8981 | } else { | |
8982 | ret = pret < 0 ? pret : -EFAULT; | |
8983 | } | |
8984 | mmap_read_unlock(current->mm); | |
8985 | if (ret) { | |
8986 | /* | |
8987 | * if we did partial map, or found file backed vmas, | |
8988 | * release any pages we did get | |
8989 | */ | |
8990 | if (pret > 0) | |
8991 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
8992 | goto done; |
8993 | } | |
8994 | ||
8995 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
8996 | if (ret) { | |
8997 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
8998 | goto done; |
8999 | } | |
9000 | ||
9001 | off = ubuf & ~PAGE_MASK; | |
9002 | size = iov->iov_len; | |
9003 | for (i = 0; i < nr_pages; i++) { | |
9004 | size_t vec_len; | |
9005 | ||
9006 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
9007 | imu->bvec[i].bv_page = pages[i]; | |
9008 | imu->bvec[i].bv_len = vec_len; | |
9009 | imu->bvec[i].bv_offset = off; | |
9010 | off = 0; | |
9011 | size -= vec_len; | |
9012 | } | |
9013 | /* store original address for later verification */ | |
9014 | imu->ubuf = ubuf; | |
4751f53d | 9015 | imu->ubuf_end = ubuf + iov->iov_len; |
0a96bbe4 | 9016 | imu->nr_bvecs = nr_pages; |
41edf1a5 | 9017 | *pimu = imu; |
0a96bbe4 BM |
9018 | ret = 0; |
9019 | done: | |
41edf1a5 PB |
9020 | if (ret) |
9021 | kvfree(imu); | |
0a96bbe4 BM |
9022 | kvfree(pages); |
9023 | kvfree(vmas); | |
9024 | return ret; | |
9025 | } | |
9026 | ||
2b358604 | 9027 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 9028 | { |
87094465 PB |
9029 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
9030 | return ctx->user_bufs ? 0 : -ENOMEM; | |
2b358604 | 9031 | } |
edafccee | 9032 | |
2b358604 BM |
9033 | static int io_buffer_validate(struct iovec *iov) |
9034 | { | |
50e96989 PB |
9035 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
9036 | ||
2b358604 BM |
9037 | /* |
9038 | * Don't impose further limits on the size and buffer | |
9039 | * constraints here, we'll -EINVAL later when IO is | |
9040 | * submitted if they are wrong. | |
9041 | */ | |
6224843d PB |
9042 | if (!iov->iov_base) |
9043 | return iov->iov_len ? -EFAULT : 0; | |
9044 | if (!iov->iov_len) | |
2b358604 | 9045 | return -EFAULT; |
edafccee | 9046 | |
2b358604 BM |
9047 | /* arbitrary limit, but we need something */ |
9048 | if (iov->iov_len > SZ_1G) | |
9049 | return -EFAULT; | |
edafccee | 9050 | |
50e96989 PB |
9051 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
9052 | return -EOVERFLOW; | |
9053 | ||
2b358604 BM |
9054 | return 0; |
9055 | } | |
edafccee | 9056 | |
2b358604 | 9057 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
634d00df | 9058 | unsigned int nr_args, u64 __user *tags) |
2b358604 | 9059 | { |
bd54b6fe BM |
9060 | struct page *last_hpage = NULL; |
9061 | struct io_rsrc_data *data; | |
2b358604 BM |
9062 | int i, ret; |
9063 | struct iovec iov; | |
edafccee | 9064 | |
87094465 PB |
9065 | if (ctx->user_bufs) |
9066 | return -EBUSY; | |
489809e2 | 9067 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
87094465 | 9068 | return -EINVAL; |
bd54b6fe | 9069 | ret = io_rsrc_node_switch_start(ctx); |
2b358604 BM |
9070 | if (ret) |
9071 | return ret; | |
d878c816 PB |
9072 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
9073 | if (ret) | |
9074 | return ret; | |
bd54b6fe BM |
9075 | ret = io_buffers_map_alloc(ctx, nr_args); |
9076 | if (ret) { | |
bb6659cc | 9077 | io_rsrc_data_free(data); |
bd54b6fe BM |
9078 | return ret; |
9079 | } | |
edafccee | 9080 | |
87094465 | 9081 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
edafccee JA |
9082 | ret = io_copy_iov(ctx, &iov, arg, i); |
9083 | if (ret) | |
0a96bbe4 | 9084 | break; |
2b358604 BM |
9085 | ret = io_buffer_validate(&iov); |
9086 | if (ret) | |
0a96bbe4 | 9087 | break; |
2d091d62 | 9088 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
cf3770e7 CIK |
9089 | ret = -EINVAL; |
9090 | break; | |
9091 | } | |
edafccee | 9092 | |
41edf1a5 PB |
9093 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
9094 | &last_hpage); | |
0a96bbe4 BM |
9095 | if (ret) |
9096 | break; | |
edafccee | 9097 | } |
0a96bbe4 | 9098 | |
bd54b6fe | 9099 | WARN_ON_ONCE(ctx->buf_data); |
0a96bbe4 | 9100 | |
bd54b6fe BM |
9101 | ctx->buf_data = data; |
9102 | if (ret) | |
9103 | __io_sqe_buffers_unregister(ctx); | |
9104 | else | |
9105 | io_rsrc_node_switch(ctx, NULL); | |
edafccee JA |
9106 | return ret; |
9107 | } | |
9108 | ||
634d00df PB |
9109 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
9110 | struct io_uring_rsrc_update2 *up, | |
9111 | unsigned int nr_args) | |
9112 | { | |
9113 | u64 __user *tags = u64_to_user_ptr(up->tags); | |
9114 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); | |
634d00df PB |
9115 | struct page *last_hpage = NULL; |
9116 | bool needs_switch = false; | |
9117 | __u32 done; | |
9118 | int i, err; | |
9119 | ||
9120 | if (!ctx->buf_data) | |
9121 | return -ENXIO; | |
9122 | if (up->offset + nr_args > ctx->nr_user_bufs) | |
9123 | return -EINVAL; | |
9124 | ||
9125 | for (done = 0; done < nr_args; done++) { | |
0b8c0e7c PB |
9126 | struct io_mapped_ubuf *imu; |
9127 | int offset = up->offset + done; | |
634d00df PB |
9128 | u64 tag = 0; |
9129 | ||
9130 | err = io_copy_iov(ctx, &iov, iovs, done); | |
9131 | if (err) | |
9132 | break; | |
9133 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { | |
9134 | err = -EFAULT; | |
9135 | break; | |
9136 | } | |
0b8c0e7c PB |
9137 | err = io_buffer_validate(&iov); |
9138 | if (err) | |
9139 | break; | |
cf3770e7 CIK |
9140 | if (!iov.iov_base && tag) { |
9141 | err = -EINVAL; | |
9142 | break; | |
9143 | } | |
0b8c0e7c PB |
9144 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
9145 | if (err) | |
9146 | break; | |
634d00df | 9147 | |
0b8c0e7c | 9148 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
6224843d | 9149 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
0b8c0e7c PB |
9150 | err = io_queue_rsrc_removal(ctx->buf_data, offset, |
9151 | ctx->rsrc_node, ctx->user_bufs[i]); | |
9152 | if (unlikely(err)) { | |
9153 | io_buffer_unmap(ctx, &imu); | |
634d00df | 9154 | break; |
0b8c0e7c | 9155 | } |
634d00df PB |
9156 | ctx->user_bufs[i] = NULL; |
9157 | needs_switch = true; | |
9158 | } | |
9159 | ||
0b8c0e7c | 9160 | ctx->user_bufs[i] = imu; |
2d091d62 | 9161 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
634d00df PB |
9162 | } |
9163 | ||
9164 | if (needs_switch) | |
9165 | io_rsrc_node_switch(ctx, ctx->buf_data); | |
9166 | return done ? done : err; | |
9167 | } | |
9168 | ||
9b402849 JA |
9169 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
9170 | { | |
9171 | __s32 __user *fds = arg; | |
9172 | int fd; | |
9173 | ||
9174 | if (ctx->cq_ev_fd) | |
9175 | return -EBUSY; | |
9176 | ||
9177 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
9178 | return -EFAULT; | |
9179 | ||
9180 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
9181 | if (IS_ERR(ctx->cq_ev_fd)) { | |
9182 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
fe7e3257 | 9183 | |
9b402849 JA |
9184 | ctx->cq_ev_fd = NULL; |
9185 | return ret; | |
9186 | } | |
9187 | ||
9188 | return 0; | |
9189 | } | |
9190 | ||
9191 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
9192 | { | |
9193 | if (ctx->cq_ev_fd) { | |
9194 | eventfd_ctx_put(ctx->cq_ev_fd); | |
9195 | ctx->cq_ev_fd = NULL; | |
9196 | return 0; | |
9197 | } | |
9198 | ||
9199 | return -ENXIO; | |
9200 | } | |
9201 | ||
5a2e745d JA |
9202 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
9203 | { | |
9e15c3a0 JA |
9204 | struct io_buffer *buf; |
9205 | unsigned long index; | |
9206 | ||
8bab4c09 | 9207 | xa_for_each(&ctx->io_buffers, index, buf) { |
9e15c3a0 | 9208 | __io_remove_buffers(ctx, buf, index, -1U); |
8bab4c09 JA |
9209 | cond_resched(); |
9210 | } | |
5a2e745d JA |
9211 | } |
9212 | ||
4010fec4 | 9213 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9214 | { |
cd0ca2e0 | 9215 | struct io_submit_state *state = &ctx->submit_state; |
37f0e767 | 9216 | int nr = 0; |
bf019da7 | 9217 | |
9a4fdbd8 | 9218 | mutex_lock(&ctx->uring_lock); |
cd0ca2e0 | 9219 | io_flush_cached_locked_reqs(ctx, state); |
9a4fdbd8 | 9220 | |
c2b6c6bc PB |
9221 | while (state->free_list.next) { |
9222 | struct io_wq_work_node *node; | |
9223 | struct io_kiocb *req; | |
9a4fdbd8 | 9224 | |
c2b6c6bc PB |
9225 | node = wq_stack_extract(&state->free_list); |
9226 | req = container_of(node, struct io_kiocb, comp_list); | |
9227 | kmem_cache_free(req_cachep, req); | |
37f0e767 | 9228 | nr++; |
c2b6c6bc | 9229 | } |
37f0e767 PB |
9230 | if (nr) |
9231 | percpu_ref_put_many(&ctx->refs, nr); | |
9a4fdbd8 JA |
9232 | mutex_unlock(&ctx->uring_lock); |
9233 | } | |
9234 | ||
43597aac | 9235 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
2b188cc1 | 9236 | { |
43597aac | 9237 | if (data && !atomic_dec_and_test(&data->refs)) |
bd54b6fe | 9238 | wait_for_completion(&data->done); |
bd54b6fe | 9239 | } |
04fc6c80 | 9240 | |
c072481d | 9241 | static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9242 | { |
37d1e2e3 | 9243 | io_sq_thread_finish(ctx); |
2aede0e4 | 9244 | |
37d1e2e3 | 9245 | if (ctx->mm_account) { |
2aede0e4 JA |
9246 | mmdrop(ctx->mm_account); |
9247 | ctx->mm_account = NULL; | |
30975825 | 9248 | } |
def596e9 | 9249 | |
ab409402 | 9250 | io_rsrc_refs_drop(ctx); |
43597aac PB |
9251 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
9252 | io_wait_rsrc_data(ctx->buf_data); | |
9253 | io_wait_rsrc_data(ctx->file_data); | |
9254 | ||
8bad28d8 | 9255 | mutex_lock(&ctx->uring_lock); |
43597aac | 9256 | if (ctx->buf_data) |
bd54b6fe | 9257 | __io_sqe_buffers_unregister(ctx); |
43597aac | 9258 | if (ctx->file_data) |
08480400 | 9259 | __io_sqe_files_unregister(ctx); |
c4ea060e PB |
9260 | if (ctx->rings) |
9261 | __io_cqring_overflow_flush(ctx, true); | |
8bad28d8 | 9262 | mutex_unlock(&ctx->uring_lock); |
9b402849 | 9263 | io_eventfd_unregister(ctx); |
5a2e745d | 9264 | io_destroy_buffers(ctx); |
07db298a PB |
9265 | if (ctx->sq_creds) |
9266 | put_cred(ctx->sq_creds); | |
def596e9 | 9267 | |
a7f0ed5a PB |
9268 | /* there are no registered resources left, nobody uses it */ |
9269 | if (ctx->rsrc_node) | |
9270 | io_rsrc_node_destroy(ctx->rsrc_node); | |
8dd03afe | 9271 | if (ctx->rsrc_backup_node) |
b895c9a6 | 9272 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
a7f0ed5a | 9273 | flush_delayed_work(&ctx->rsrc_put_work); |
756ab7c0 | 9274 | flush_delayed_work(&ctx->fallback_work); |
a7f0ed5a PB |
9275 | |
9276 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); | |
9277 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); | |
def596e9 | 9278 | |
2b188cc1 | 9279 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
9280 | if (ctx->ring_sock) { |
9281 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 9282 | sock_release(ctx->ring_sock); |
355e8d26 | 9283 | } |
2b188cc1 | 9284 | #endif |
ef9dd637 | 9285 | WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); |
2b188cc1 | 9286 | |
75b28aff | 9287 | io_mem_free(ctx->rings); |
2b188cc1 | 9288 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
9289 | |
9290 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 9291 | free_uid(ctx->user); |
4010fec4 | 9292 | io_req_caches_free(ctx); |
e941894e JA |
9293 | if (ctx->hash_map) |
9294 | io_wq_put_hash(ctx->hash_map); | |
78076bb6 | 9295 | kfree(ctx->cancel_hash); |
6224843d | 9296 | kfree(ctx->dummy_ubuf); |
2b188cc1 JA |
9297 | kfree(ctx); |
9298 | } | |
9299 | ||
9300 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
9301 | { | |
9302 | struct io_ring_ctx *ctx = file->private_data; | |
9303 | __poll_t mask = 0; | |
9304 | ||
d60aa65b | 9305 | poll_wait(file, &ctx->cq_wait, wait); |
4f7067c3 SB |
9306 | /* |
9307 | * synchronizes with barrier from wq_has_sleeper call in | |
9308 | * io_commit_cqring | |
9309 | */ | |
2b188cc1 | 9310 | smp_rmb(); |
90554200 | 9311 | if (!io_sqring_full(ctx)) |
2b188cc1 | 9312 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
9313 | |
9314 | /* | |
9315 | * Don't flush cqring overflow list here, just do a simple check. | |
9316 | * Otherwise there could possible be ABBA deadlock: | |
9317 | * CPU0 CPU1 | |
9318 | * ---- ---- | |
9319 | * lock(&ctx->uring_lock); | |
9320 | * lock(&ep->mtx); | |
9321 | * lock(&ctx->uring_lock); | |
9322 | * lock(&ep->mtx); | |
9323 | * | |
9324 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
9325 | * pushs them to do the flush. | |
9326 | */ | |
5ed7a37d | 9327 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
2b188cc1 JA |
9328 | mask |= EPOLLIN | EPOLLRDNORM; |
9329 | ||
9330 | return mask; | |
9331 | } | |
9332 | ||
0bead8cd | 9333 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 9334 | { |
4379bf8b | 9335 | const struct cred *creds; |
071698e1 | 9336 | |
61cf9370 | 9337 | creds = xa_erase(&ctx->personalities, id); |
4379bf8b JA |
9338 | if (creds) { |
9339 | put_cred(creds); | |
0bead8cd | 9340 | return 0; |
1e6fa521 | 9341 | } |
0bead8cd YD |
9342 | |
9343 | return -EINVAL; | |
9344 | } | |
9345 | ||
d56d938b PB |
9346 | struct io_tctx_exit { |
9347 | struct callback_head task_work; | |
9348 | struct completion completion; | |
baf186c4 | 9349 | struct io_ring_ctx *ctx; |
d56d938b PB |
9350 | }; |
9351 | ||
c072481d | 9352 | static __cold void io_tctx_exit_cb(struct callback_head *cb) |
d56d938b PB |
9353 | { |
9354 | struct io_uring_task *tctx = current->io_uring; | |
9355 | struct io_tctx_exit *work; | |
9356 | ||
9357 | work = container_of(cb, struct io_tctx_exit, task_work); | |
9358 | /* | |
9359 | * When @in_idle, we're in cancellation and it's racy to remove the | |
9360 | * node. It'll be removed by the end of cancellation, just ignore it. | |
9361 | */ | |
9362 | if (!atomic_read(&tctx->in_idle)) | |
eef51daa | 9363 | io_uring_del_tctx_node((unsigned long)work->ctx); |
d56d938b PB |
9364 | complete(&work->completion); |
9365 | } | |
9366 | ||
c072481d | 9367 | static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
28090c13 PB |
9368 | { |
9369 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
9370 | ||
9371 | return req->ctx == data; | |
9372 | } | |
9373 | ||
c072481d | 9374 | static __cold void io_ring_exit_work(struct work_struct *work) |
85faa7b8 | 9375 | { |
d56d938b | 9376 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 9377 | unsigned long timeout = jiffies + HZ * 60 * 5; |
58d3be2c | 9378 | unsigned long interval = HZ / 20; |
d56d938b PB |
9379 | struct io_tctx_exit exit; |
9380 | struct io_tctx_node *node; | |
9381 | int ret; | |
85faa7b8 | 9382 | |
56952e91 JA |
9383 | /* |
9384 | * If we're doing polled IO and end up having requests being | |
9385 | * submitted async (out-of-line), then completions can come in while | |
9386 | * we're waiting for refs to drop. We need to reap these manually, | |
9387 | * as nobody else will be looking for them. | |
9388 | */ | |
b2edc0a7 | 9389 | do { |
3dd0c97a | 9390 | io_uring_try_cancel_requests(ctx, NULL, true); |
28090c13 PB |
9391 | if (ctx->sq_data) { |
9392 | struct io_sq_data *sqd = ctx->sq_data; | |
9393 | struct task_struct *tsk; | |
9394 | ||
9395 | io_sq_thread_park(sqd); | |
9396 | tsk = sqd->thread; | |
9397 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) | |
9398 | io_wq_cancel_cb(tsk->io_uring->io_wq, | |
9399 | io_cancel_ctx_cb, ctx, true); | |
9400 | io_sq_thread_unpark(sqd); | |
9401 | } | |
b5bb3a24 | 9402 | |
37f0e767 PB |
9403 | io_req_caches_free(ctx); |
9404 | ||
58d3be2c PB |
9405 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
9406 | /* there is little hope left, don't run it too often */ | |
9407 | interval = HZ * 60; | |
9408 | } | |
9409 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); | |
d56d938b | 9410 | |
7f00651a PB |
9411 | init_completion(&exit.completion); |
9412 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
9413 | exit.ctx = ctx; | |
89b5066e PB |
9414 | /* |
9415 | * Some may use context even when all refs and requests have been put, | |
9416 | * and they are free to do so while still holding uring_lock or | |
5b0a6acc | 9417 | * completion_lock, see io_req_task_submit(). Apart from other work, |
89b5066e PB |
9418 | * this lock/unlock section also waits them to finish. |
9419 | */ | |
d56d938b PB |
9420 | mutex_lock(&ctx->uring_lock); |
9421 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
9422 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
9423 | ||
d56d938b PB |
9424 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
9425 | ctx_node); | |
7f00651a PB |
9426 | /* don't spin on a single task if cancellation failed */ |
9427 | list_rotate_left(&ctx->tctx_list); | |
d56d938b PB |
9428 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
9429 | if (WARN_ON_ONCE(ret)) | |
9430 | continue; | |
d56d938b PB |
9431 | |
9432 | mutex_unlock(&ctx->uring_lock); | |
9433 | wait_for_completion(&exit.completion); | |
d56d938b PB |
9434 | mutex_lock(&ctx->uring_lock); |
9435 | } | |
9436 | mutex_unlock(&ctx->uring_lock); | |
79ebeaee JA |
9437 | spin_lock(&ctx->completion_lock); |
9438 | spin_unlock(&ctx->completion_lock); | |
d56d938b | 9439 | |
85faa7b8 JA |
9440 | io_ring_ctx_free(ctx); |
9441 | } | |
9442 | ||
80c4cbdb | 9443 | /* Returns true if we found and killed one or more timeouts */ |
c072481d PB |
9444 | static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx, |
9445 | struct task_struct *tsk, bool cancel_all) | |
80c4cbdb PB |
9446 | { |
9447 | struct io_kiocb *req, *tmp; | |
9448 | int canceled = 0; | |
9449 | ||
79ebeaee JA |
9450 | spin_lock(&ctx->completion_lock); |
9451 | spin_lock_irq(&ctx->timeout_lock); | |
80c4cbdb | 9452 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
3dd0c97a | 9453 | if (io_match_task(req, tsk, cancel_all)) { |
80c4cbdb PB |
9454 | io_kill_timeout(req, -ECANCELED); |
9455 | canceled++; | |
9456 | } | |
9457 | } | |
79ebeaee | 9458 | spin_unlock_irq(&ctx->timeout_lock); |
51520426 PB |
9459 | if (canceled != 0) |
9460 | io_commit_cqring(ctx); | |
79ebeaee | 9461 | spin_unlock(&ctx->completion_lock); |
80c4cbdb PB |
9462 | if (canceled != 0) |
9463 | io_cqring_ev_posted(ctx); | |
9464 | return canceled != 0; | |
9465 | } | |
9466 | ||
c072481d | 9467 | static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
2b188cc1 | 9468 | { |
61cf9370 MWO |
9469 | unsigned long index; |
9470 | struct creds *creds; | |
9471 | ||
2b188cc1 JA |
9472 | mutex_lock(&ctx->uring_lock); |
9473 | percpu_ref_kill(&ctx->refs); | |
634578f8 | 9474 | if (ctx->rings) |
6c2450ae | 9475 | __io_cqring_overflow_flush(ctx, true); |
61cf9370 MWO |
9476 | xa_for_each(&ctx->personalities, index, creds) |
9477 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
9478 | mutex_unlock(&ctx->uring_lock); |
9479 | ||
3dd0c97a PB |
9480 | io_kill_timeouts(ctx, NULL, true); |
9481 | io_poll_remove_all(ctx, NULL, true); | |
561fb04a | 9482 | |
15dff286 | 9483 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 9484 | io_iopoll_try_reap_events(ctx); |
309fc03a | 9485 | |
85faa7b8 | 9486 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
9487 | /* |
9488 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
9489 | * if we're exiting a ton of rings at the same time. It just adds | |
9490 | * noise and overhead, there's no discernable change in runtime | |
9491 | * over using system_wq. | |
9492 | */ | |
9493 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
9494 | } |
9495 | ||
9496 | static int io_uring_release(struct inode *inode, struct file *file) | |
9497 | { | |
9498 | struct io_ring_ctx *ctx = file->private_data; | |
9499 | ||
9500 | file->private_data = NULL; | |
9501 | io_ring_ctx_wait_and_kill(ctx); | |
9502 | return 0; | |
9503 | } | |
9504 | ||
f6edbabb PB |
9505 | struct io_task_cancel { |
9506 | struct task_struct *task; | |
3dd0c97a | 9507 | bool all; |
f6edbabb | 9508 | }; |
f254ac04 | 9509 | |
f6edbabb | 9510 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 9511 | { |
9a472ef7 | 9512 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 9513 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
9514 | bool ret; |
9515 | ||
3dd0c97a | 9516 | if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
9517 | struct io_ring_ctx *ctx = req->ctx; |
9518 | ||
9519 | /* protect against races with linked timeouts */ | |
79ebeaee | 9520 | spin_lock(&ctx->completion_lock); |
3dd0c97a | 9521 | ret = io_match_task(req, cancel->task, cancel->all); |
79ebeaee | 9522 | spin_unlock(&ctx->completion_lock); |
9a472ef7 | 9523 | } else { |
3dd0c97a | 9524 | ret = io_match_task(req, cancel->task, cancel->all); |
9a472ef7 PB |
9525 | } |
9526 | return ret; | |
b711d4ea JA |
9527 | } |
9528 | ||
c072481d PB |
9529 | static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
9530 | struct task_struct *task, | |
9531 | bool cancel_all) | |
b7ddce3c | 9532 | { |
e1915f76 | 9533 | struct io_defer_entry *de; |
b7ddce3c PB |
9534 | LIST_HEAD(list); |
9535 | ||
79ebeaee | 9536 | spin_lock(&ctx->completion_lock); |
b7ddce3c | 9537 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
3dd0c97a | 9538 | if (io_match_task(de->req, task, cancel_all)) { |
b7ddce3c PB |
9539 | list_cut_position(&list, &ctx->defer_list, &de->list); |
9540 | break; | |
9541 | } | |
9542 | } | |
79ebeaee | 9543 | spin_unlock(&ctx->completion_lock); |
e1915f76 PB |
9544 | if (list_empty(&list)) |
9545 | return false; | |
b7ddce3c PB |
9546 | |
9547 | while (!list_empty(&list)) { | |
9548 | de = list_first_entry(&list, struct io_defer_entry, list); | |
9549 | list_del_init(&de->list); | |
f41db273 | 9550 | io_req_complete_failed(de->req, -ECANCELED); |
b7ddce3c PB |
9551 | kfree(de); |
9552 | } | |
e1915f76 | 9553 | return true; |
b7ddce3c PB |
9554 | } |
9555 | ||
c072481d | 9556 | static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
1b00764f PB |
9557 | { |
9558 | struct io_tctx_node *node; | |
9559 | enum io_wq_cancel cret; | |
9560 | bool ret = false; | |
9561 | ||
9562 | mutex_lock(&ctx->uring_lock); | |
9563 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
9564 | struct io_uring_task *tctx = node->task->io_uring; | |
9565 | ||
9566 | /* | |
9567 | * io_wq will stay alive while we hold uring_lock, because it's | |
9568 | * killed after ctx nodes, which requires to take the lock. | |
9569 | */ | |
9570 | if (!tctx || !tctx->io_wq) | |
9571 | continue; | |
9572 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
9573 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9574 | } | |
9575 | mutex_unlock(&ctx->uring_lock); | |
9576 | ||
9577 | return ret; | |
9578 | } | |
9579 | ||
c072481d PB |
9580 | static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
9581 | struct task_struct *task, | |
9582 | bool cancel_all) | |
9936c7c2 | 9583 | { |
3dd0c97a | 9584 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
1b00764f | 9585 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 PB |
9586 | |
9587 | while (1) { | |
9588 | enum io_wq_cancel cret; | |
9589 | bool ret = false; | |
9590 | ||
1b00764f PB |
9591 | if (!task) { |
9592 | ret |= io_uring_try_cancel_iowq(ctx); | |
9593 | } else if (tctx && tctx->io_wq) { | |
9594 | /* | |
9595 | * Cancels requests of all rings, not only @ctx, but | |
9596 | * it's fine as the task is in exit/exec. | |
9597 | */ | |
5aa75ed5 | 9598 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
9599 | &cancel, true); |
9600 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9601 | } | |
9602 | ||
9603 | /* SQPOLL thread does its own polling */ | |
3dd0c97a | 9604 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
d052d1d6 | 9605 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
5eef4e87 | 9606 | while (!wq_list_empty(&ctx->iopoll_list)) { |
9936c7c2 PB |
9607 | io_iopoll_try_reap_events(ctx); |
9608 | ret = true; | |
9609 | } | |
9610 | } | |
9611 | ||
3dd0c97a PB |
9612 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
9613 | ret |= io_poll_remove_all(ctx, task, cancel_all); | |
9614 | ret |= io_kill_timeouts(ctx, task, cancel_all); | |
e5dc480d PB |
9615 | if (task) |
9616 | ret |= io_run_task_work(); | |
9936c7c2 PB |
9617 | if (!ret) |
9618 | break; | |
9619 | cond_resched(); | |
9620 | } | |
9621 | } | |
9622 | ||
eef51daa | 9623 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
0f212204 | 9624 | { |
236434c3 | 9625 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 9626 | struct io_tctx_node *node; |
a528b04e | 9627 | int ret; |
236434c3 MWO |
9628 | |
9629 | if (unlikely(!tctx)) { | |
5aa75ed5 | 9630 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
9631 | if (unlikely(ret)) |
9632 | return ret; | |
e139a1ec | 9633 | |
236434c3 | 9634 | tctx = current->io_uring; |
e139a1ec PB |
9635 | if (ctx->iowq_limits_set) { |
9636 | unsigned int limits[2] = { ctx->iowq_limits[0], | |
9637 | ctx->iowq_limits[1], }; | |
9638 | ||
9639 | ret = io_wq_max_workers(tctx->io_wq, limits); | |
9640 | if (ret) | |
9641 | return ret; | |
9642 | } | |
0f212204 | 9643 | } |
cf27f3b1 PB |
9644 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
9645 | node = kmalloc(sizeof(*node), GFP_KERNEL); | |
9646 | if (!node) | |
9647 | return -ENOMEM; | |
9648 | node->ctx = ctx; | |
9649 | node->task = current; | |
13bf43f5 | 9650 | |
cf27f3b1 PB |
9651 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
9652 | node, GFP_KERNEL)); | |
9653 | if (ret) { | |
9654 | kfree(node); | |
9655 | return ret; | |
0f212204 | 9656 | } |
cf27f3b1 PB |
9657 | |
9658 | mutex_lock(&ctx->uring_lock); | |
9659 | list_add(&node->ctx_node, &ctx->tctx_list); | |
9660 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 9661 | } |
cf27f3b1 | 9662 | tctx->last = ctx; |
0f212204 JA |
9663 | return 0; |
9664 | } | |
9665 | ||
cf27f3b1 PB |
9666 | /* |
9667 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
9668 | */ | |
eef51daa | 9669 | static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
cf27f3b1 PB |
9670 | { |
9671 | struct io_uring_task *tctx = current->io_uring; | |
9672 | ||
9673 | if (likely(tctx && tctx->last == ctx)) | |
9674 | return 0; | |
eef51daa | 9675 | return __io_uring_add_tctx_node(ctx); |
cf27f3b1 PB |
9676 | } |
9677 | ||
0f212204 JA |
9678 | /* |
9679 | * Remove this io_uring_file -> task mapping. | |
9680 | */ | |
c072481d | 9681 | static __cold void io_uring_del_tctx_node(unsigned long index) |
0f212204 JA |
9682 | { |
9683 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 9684 | struct io_tctx_node *node; |
2941267b | 9685 | |
eebd2e37 PB |
9686 | if (!tctx) |
9687 | return; | |
13bf43f5 PB |
9688 | node = xa_erase(&tctx->xa, index); |
9689 | if (!node) | |
2941267b | 9690 | return; |
0f212204 | 9691 | |
13bf43f5 PB |
9692 | WARN_ON_ONCE(current != node->task); |
9693 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
9694 | ||
9695 | mutex_lock(&node->ctx->uring_lock); | |
9696 | list_del(&node->ctx_node); | |
9697 | mutex_unlock(&node->ctx->uring_lock); | |
9698 | ||
baf186c4 | 9699 | if (tctx->last == node->ctx) |
0f212204 | 9700 | tctx->last = NULL; |
13bf43f5 | 9701 | kfree(node); |
0f212204 JA |
9702 | } |
9703 | ||
c072481d | 9704 | static __cold void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 9705 | { |
ba5ef6dc | 9706 | struct io_wq *wq = tctx->io_wq; |
13bf43f5 | 9707 | struct io_tctx_node *node; |
de7f1d9e PB |
9708 | unsigned long index; |
9709 | ||
8bab4c09 | 9710 | xa_for_each(&tctx->xa, index, node) { |
eef51daa | 9711 | io_uring_del_tctx_node(index); |
8bab4c09 JA |
9712 | cond_resched(); |
9713 | } | |
b16ef427 ME |
9714 | if (wq) { |
9715 | /* | |
9716 | * Must be after io_uring_del_task_file() (removes nodes under | |
9717 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). | |
9718 | */ | |
ba5ef6dc | 9719 | io_wq_put_and_exit(wq); |
dadebc35 | 9720 | tctx->io_wq = NULL; |
b16ef427 | 9721 | } |
de7f1d9e PB |
9722 | } |
9723 | ||
3f48cf18 | 9724 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
521d6a73 | 9725 | { |
3f48cf18 PB |
9726 | if (tracked) |
9727 | return atomic_read(&tctx->inflight_tracked); | |
521d6a73 PB |
9728 | return percpu_counter_sum(&tctx->inflight); |
9729 | } | |
9730 | ||
c072481d | 9731 | static __cold void io_uring_drop_tctx_refs(struct task_struct *task) |
09899b19 PB |
9732 | { |
9733 | struct io_uring_task *tctx = task->io_uring; | |
9734 | unsigned int refs = tctx->cached_refs; | |
9735 | ||
e9dbe221 PB |
9736 | if (refs) { |
9737 | tctx->cached_refs = 0; | |
9738 | percpu_counter_sub(&tctx->inflight, refs); | |
9739 | put_task_struct_many(task, refs); | |
9740 | } | |
09899b19 PB |
9741 | } |
9742 | ||
78cc687b PB |
9743 | /* |
9744 | * Find any io_uring ctx that this task has registered or done IO on, and cancel | |
9745 | * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation. | |
9746 | */ | |
c072481d PB |
9747 | static __cold void io_uring_cancel_generic(bool cancel_all, |
9748 | struct io_sq_data *sqd) | |
0e9ddb39 | 9749 | { |
521d6a73 | 9750 | struct io_uring_task *tctx = current->io_uring; |
734551df | 9751 | struct io_ring_ctx *ctx; |
0e9ddb39 PB |
9752 | s64 inflight; |
9753 | DEFINE_WAIT(wait); | |
fdaf083c | 9754 | |
78cc687b PB |
9755 | WARN_ON_ONCE(sqd && sqd->thread != current); |
9756 | ||
6d042ffb PO |
9757 | if (!current->io_uring) |
9758 | return; | |
17a91051 PB |
9759 | if (tctx->io_wq) |
9760 | io_wq_exit_start(tctx->io_wq); | |
9761 | ||
0e9ddb39 PB |
9762 | atomic_inc(&tctx->in_idle); |
9763 | do { | |
e9dbe221 | 9764 | io_uring_drop_tctx_refs(current); |
0e9ddb39 | 9765 | /* read completions before cancelations */ |
78cc687b | 9766 | inflight = tctx_inflight(tctx, !cancel_all); |
0e9ddb39 PB |
9767 | if (!inflight) |
9768 | break; | |
fdaf083c | 9769 | |
78cc687b PB |
9770 | if (!sqd) { |
9771 | struct io_tctx_node *node; | |
9772 | unsigned long index; | |
0f212204 | 9773 | |
78cc687b PB |
9774 | xa_for_each(&tctx->xa, index, node) { |
9775 | /* sqpoll task will cancel all its requests */ | |
9776 | if (node->ctx->sq_data) | |
9777 | continue; | |
9778 | io_uring_try_cancel_requests(node->ctx, current, | |
9779 | cancel_all); | |
9780 | } | |
9781 | } else { | |
9782 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
9783 | io_uring_try_cancel_requests(ctx, current, | |
9784 | cancel_all); | |
9785 | } | |
17a91051 | 9786 | |
0f212204 | 9787 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
e9dbe221 | 9788 | io_uring_drop_tctx_refs(current); |
0f212204 | 9789 | /* |
a1bb3cd5 PB |
9790 | * If we've seen completions, retry without waiting. This |
9791 | * avoids a race where a completion comes in before we did | |
9792 | * prepare_to_wait(). | |
0f212204 | 9793 | */ |
3dd0c97a | 9794 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
a1bb3cd5 | 9795 | schedule(); |
f57555ed | 9796 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 9797 | } while (1); |
fdaf083c | 9798 | atomic_dec(&tctx->in_idle); |
de7f1d9e | 9799 | |
8452d4a6 | 9800 | io_uring_clean_tctx(tctx); |
3dd0c97a | 9801 | if (cancel_all) { |
3f48cf18 PB |
9802 | /* for exec all current's requests should be gone, kill tctx */ |
9803 | __io_uring_free(current); | |
9804 | } | |
44e728b8 PB |
9805 | } |
9806 | ||
f552a27a | 9807 | void __io_uring_cancel(bool cancel_all) |
78cc687b | 9808 | { |
f552a27a | 9809 | io_uring_cancel_generic(cancel_all, NULL); |
78cc687b PB |
9810 | } |
9811 | ||
6c5c240e RP |
9812 | static void *io_uring_validate_mmap_request(struct file *file, |
9813 | loff_t pgoff, size_t sz) | |
2b188cc1 | 9814 | { |
2b188cc1 | 9815 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 9816 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
9817 | struct page *page; |
9818 | void *ptr; | |
9819 | ||
9820 | switch (offset) { | |
9821 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
9822 | case IORING_OFF_CQ_RING: |
9823 | ptr = ctx->rings; | |
2b188cc1 JA |
9824 | break; |
9825 | case IORING_OFF_SQES: | |
9826 | ptr = ctx->sq_sqes; | |
9827 | break; | |
2b188cc1 | 9828 | default: |
6c5c240e | 9829 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
9830 | } |
9831 | ||
9832 | page = virt_to_head_page(ptr); | |
a50b854e | 9833 | if (sz > page_size(page)) |
6c5c240e RP |
9834 | return ERR_PTR(-EINVAL); |
9835 | ||
9836 | return ptr; | |
9837 | } | |
9838 | ||
9839 | #ifdef CONFIG_MMU | |
9840 | ||
c072481d | 9841 | static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
6c5c240e RP |
9842 | { |
9843 | size_t sz = vma->vm_end - vma->vm_start; | |
9844 | unsigned long pfn; | |
9845 | void *ptr; | |
9846 | ||
9847 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
9848 | if (IS_ERR(ptr)) | |
9849 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9850 | |
9851 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9852 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9853 | } | |
9854 | ||
6c5c240e RP |
9855 | #else /* !CONFIG_MMU */ |
9856 | ||
9857 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9858 | { | |
9859 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9860 | } | |
9861 | ||
9862 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9863 | { | |
9864 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9865 | } | |
9866 | ||
9867 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9868 | unsigned long addr, unsigned long len, | |
9869 | unsigned long pgoff, unsigned long flags) | |
9870 | { | |
9871 | void *ptr; | |
9872 | ||
9873 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9874 | if (IS_ERR(ptr)) | |
9875 | return PTR_ERR(ptr); | |
9876 | ||
9877 | return (unsigned long) ptr; | |
9878 | } | |
9879 | ||
9880 | #endif /* !CONFIG_MMU */ | |
9881 | ||
d9d05217 | 9882 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 JA |
9883 | { |
9884 | DEFINE_WAIT(wait); | |
9885 | ||
9886 | do { | |
9887 | if (!io_sqring_full(ctx)) | |
9888 | break; | |
90554200 JA |
9889 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
9890 | ||
9891 | if (!io_sqring_full(ctx)) | |
9892 | break; | |
90554200 JA |
9893 | schedule(); |
9894 | } while (!signal_pending(current)); | |
9895 | ||
9896 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
5199328a | 9897 | return 0; |
90554200 JA |
9898 | } |
9899 | ||
c73ebb68 HX |
9900 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9901 | struct __kernel_timespec __user **ts, | |
9902 | const sigset_t __user **sig) | |
9903 | { | |
9904 | struct io_uring_getevents_arg arg; | |
9905 | ||
9906 | /* | |
9907 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9908 | * is just a pointer to the sigset_t. | |
9909 | */ | |
9910 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9911 | *sig = (const sigset_t __user *) argp; | |
9912 | *ts = NULL; | |
9913 | return 0; | |
9914 | } | |
9915 | ||
9916 | /* | |
9917 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9918 | * timespec and sigset_t pointers if good. | |
9919 | */ | |
9920 | if (*argsz != sizeof(arg)) | |
9921 | return -EINVAL; | |
9922 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9923 | return -EFAULT; | |
9924 | *sig = u64_to_user_ptr(arg.sigmask); | |
9925 | *argsz = arg.sigmask_sz; | |
9926 | *ts = u64_to_user_ptr(arg.ts); | |
9927 | return 0; | |
9928 | } | |
9929 | ||
2b188cc1 | 9930 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9931 | u32, min_complete, u32, flags, const void __user *, argp, |
9932 | size_t, argsz) | |
2b188cc1 JA |
9933 | { |
9934 | struct io_ring_ctx *ctx; | |
2b188cc1 JA |
9935 | int submitted = 0; |
9936 | struct fd f; | |
33f993da | 9937 | long ret; |
2b188cc1 | 9938 | |
4c6e277c | 9939 | io_run_task_work(); |
b41e9852 | 9940 | |
33f993da PB |
9941 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
9942 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) | |
2b188cc1 JA |
9943 | return -EINVAL; |
9944 | ||
9945 | f = fdget(fd); | |
33f993da | 9946 | if (unlikely(!f.file)) |
2b188cc1 JA |
9947 | return -EBADF; |
9948 | ||
9949 | ret = -EOPNOTSUPP; | |
33f993da | 9950 | if (unlikely(f.file->f_op != &io_uring_fops)) |
2b188cc1 JA |
9951 | goto out_fput; |
9952 | ||
9953 | ret = -ENXIO; | |
9954 | ctx = f.file->private_data; | |
33f993da | 9955 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
2b188cc1 JA |
9956 | goto out_fput; |
9957 | ||
7e84e1c7 | 9958 | ret = -EBADFD; |
33f993da | 9959 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
9960 | goto out; |
9961 | ||
6c271ce2 JA |
9962 | /* |
9963 | * For SQ polling, the thread will do all submissions and completions. | |
9964 | * Just return the requested submit count, and wake the thread if | |
9965 | * we were asked to. | |
9966 | */ | |
b2a9eada | 9967 | ret = 0; |
6c271ce2 | 9968 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
90f67366 | 9969 | io_cqring_overflow_flush(ctx); |
89448c47 | 9970 | |
21f96522 JA |
9971 | if (unlikely(ctx->sq_data->thread == NULL)) { |
9972 | ret = -EOWNERDEAD; | |
04147488 | 9973 | goto out; |
21f96522 | 9974 | } |
6c271ce2 | 9975 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9976 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9977 | if (flags & IORING_ENTER_SQ_WAIT) { |
9978 | ret = io_sqpoll_wait_sq(ctx); | |
9979 | if (ret) | |
9980 | goto out; | |
9981 | } | |
6c271ce2 | 9982 | submitted = to_submit; |
b2a9eada | 9983 | } else if (to_submit) { |
eef51daa | 9984 | ret = io_uring_add_tctx_node(ctx); |
0f212204 JA |
9985 | if (unlikely(ret)) |
9986 | goto out; | |
2b188cc1 | 9987 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9988 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9989 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9990 | |
9991 | if (submitted != to_submit) | |
9992 | goto out; | |
2b188cc1 JA |
9993 | } |
9994 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9995 | const sigset_t __user *sig; |
9996 | struct __kernel_timespec __user *ts; | |
9997 | ||
9998 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9999 | if (unlikely(ret)) | |
10000 | goto out; | |
10001 | ||
2b188cc1 JA |
10002 | min_complete = min(min_complete, ctx->cq_entries); |
10003 | ||
32b2244a XW |
10004 | /* |
10005 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
10006 | * space applications don't need to do io completion events | |
10007 | * polling again, they can rely on io_sq_thread to do polling | |
10008 | * work, which can reduce cpu usage and uring_lock contention. | |
10009 | */ | |
10010 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
10011 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 10012 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 10013 | } else { |
c73ebb68 | 10014 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 10015 | } |
2b188cc1 JA |
10016 | } |
10017 | ||
7c504e65 | 10018 | out: |
6805b32e | 10019 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
10020 | out_fput: |
10021 | fdput(f); | |
10022 | return submitted ? submitted : ret; | |
10023 | } | |
10024 | ||
bebdb65e | 10025 | #ifdef CONFIG_PROC_FS |
c072481d | 10026 | static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id, |
61cf9370 | 10027 | const struct cred *cred) |
87ce955b | 10028 | { |
87ce955b JA |
10029 | struct user_namespace *uns = seq_user_ns(m); |
10030 | struct group_info *gi; | |
10031 | kernel_cap_t cap; | |
10032 | unsigned __capi; | |
10033 | int g; | |
10034 | ||
10035 | seq_printf(m, "%5d\n", id); | |
10036 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
10037 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
10038 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
10039 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
10040 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
10041 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
10042 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
10043 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
10044 | seq_puts(m, "\n\tGroups:\t"); | |
10045 | gi = cred->group_info; | |
10046 | for (g = 0; g < gi->ngroups; g++) { | |
10047 | seq_put_decimal_ull(m, g ? " " : "", | |
10048 | from_kgid_munged(uns, gi->gid[g])); | |
10049 | } | |
10050 | seq_puts(m, "\n\tCapEff:\t"); | |
10051 | cap = cred->cap_effective; | |
10052 | CAP_FOR_EACH_U32(__capi) | |
10053 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
10054 | seq_putc(m, '\n'); | |
10055 | return 0; | |
10056 | } | |
10057 | ||
c072481d PB |
10058 | static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, |
10059 | struct seq_file *m) | |
87ce955b | 10060 | { |
dbbe9c64 | 10061 | struct io_sq_data *sq = NULL; |
83f84356 HX |
10062 | struct io_overflow_cqe *ocqe; |
10063 | struct io_rings *r = ctx->rings; | |
10064 | unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1; | |
83f84356 HX |
10065 | unsigned int sq_head = READ_ONCE(r->sq.head); |
10066 | unsigned int sq_tail = READ_ONCE(r->sq.tail); | |
10067 | unsigned int cq_head = READ_ONCE(r->cq.head); | |
10068 | unsigned int cq_tail = READ_ONCE(r->cq.tail); | |
f75d1183 | 10069 | unsigned int sq_entries, cq_entries; |
fad8e0de | 10070 | bool has_lock; |
83f84356 HX |
10071 | unsigned int i; |
10072 | ||
10073 | /* | |
10074 | * we may get imprecise sqe and cqe info if uring is actively running | |
10075 | * since we get cached_sq_head and cached_cq_tail without uring_lock | |
10076 | * and sq_tail and cq_head are changed by userspace. But it's ok since | |
10077 | * we usually use these info when it is stuck. | |
10078 | */ | |
f75d1183 JA |
10079 | seq_printf(m, "SqMask:\t\t0x%x\n", sq_mask); |
10080 | seq_printf(m, "SqHead:\t%u\n", sq_head); | |
10081 | seq_printf(m, "SqTail:\t%u\n", sq_tail); | |
10082 | seq_printf(m, "CachedSqHead:\t%u\n", ctx->cached_sq_head); | |
10083 | seq_printf(m, "CqMask:\t0x%x\n", cq_mask); | |
10084 | seq_printf(m, "CqHead:\t%u\n", cq_head); | |
10085 | seq_printf(m, "CqTail:\t%u\n", cq_tail); | |
10086 | seq_printf(m, "CachedCqTail:\t%u\n", ctx->cached_cq_tail); | |
10087 | seq_printf(m, "SQEs:\t%u\n", sq_tail - ctx->cached_sq_head); | |
10088 | sq_entries = min(sq_tail - sq_head, ctx->sq_entries); | |
10089 | for (i = 0; i < sq_entries; i++) { | |
10090 | unsigned int entry = i + sq_head; | |
10091 | unsigned int sq_idx = READ_ONCE(ctx->sq_array[entry & sq_mask]); | |
10092 | struct io_uring_sqe *sqe = &ctx->sq_sqes[sq_idx]; | |
10093 | ||
10094 | if (sq_idx > sq_mask) | |
10095 | continue; | |
10096 | sqe = &ctx->sq_sqes[sq_idx]; | |
10097 | seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n", | |
10098 | sq_idx, sqe->opcode, sqe->fd, sqe->flags, | |
10099 | sqe->user_data); | |
83f84356 | 10100 | } |
f75d1183 JA |
10101 | seq_printf(m, "CQEs:\t%u\n", cq_tail - cq_head); |
10102 | cq_entries = min(cq_tail - cq_head, ctx->cq_entries); | |
10103 | for (i = 0; i < cq_entries; i++) { | |
10104 | unsigned int entry = i + cq_head; | |
10105 | struct io_uring_cqe *cqe = &r->cqes[entry & cq_mask]; | |
83f84356 HX |
10106 | |
10107 | seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n", | |
f75d1183 JA |
10108 | entry & cq_mask, cqe->user_data, cqe->res, |
10109 | cqe->flags); | |
83f84356 | 10110 | } |
87ce955b | 10111 | |
fad8e0de JA |
10112 | /* |
10113 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
10114 | * since fdinfo case grabs it in the opposite direction of normal use | |
10115 | * cases. If we fail to get the lock, we just don't iterate any | |
10116 | * structures that could be going away outside the io_uring mutex. | |
10117 | */ | |
10118 | has_lock = mutex_trylock(&ctx->uring_lock); | |
10119 | ||
5f3f26f9 | 10120 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 10121 | sq = ctx->sq_data; |
5f3f26f9 JA |
10122 | if (!sq->thread) |
10123 | sq = NULL; | |
10124 | } | |
dbbe9c64 JQ |
10125 | |
10126 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
10127 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 10128 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 10129 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
7b29f92d | 10130 | struct file *f = io_file_from_index(ctx, i); |
87ce955b | 10131 | |
87ce955b JA |
10132 | if (f) |
10133 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
10134 | else | |
10135 | seq_printf(m, "%5u: <none>\n", i); | |
10136 | } | |
10137 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 10138 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
41edf1a5 | 10139 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
4751f53d | 10140 | unsigned int len = buf->ubuf_end - buf->ubuf; |
87ce955b | 10141 | |
4751f53d | 10142 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
87ce955b | 10143 | } |
61cf9370 MWO |
10144 | if (has_lock && !xa_empty(&ctx->personalities)) { |
10145 | unsigned long index; | |
10146 | const struct cred *cred; | |
10147 | ||
87ce955b | 10148 | seq_printf(m, "Personalities:\n"); |
61cf9370 MWO |
10149 | xa_for_each(&ctx->personalities, index, cred) |
10150 | io_uring_show_cred(m, index, cred); | |
87ce955b | 10151 | } |
83f84356 HX |
10152 | if (has_lock) |
10153 | mutex_unlock(&ctx->uring_lock); | |
10154 | ||
10155 | seq_puts(m, "PollList:\n"); | |
79ebeaee | 10156 | spin_lock(&ctx->completion_lock); |
d7718a9d JA |
10157 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
10158 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
10159 | struct io_kiocb *req; | |
10160 | ||
10161 | hlist_for_each_entry(req, list, hash_node) | |
10162 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
10163 | req->task->task_works != NULL); | |
10164 | } | |
83f84356 HX |
10165 | |
10166 | seq_puts(m, "CqOverflowList:\n"); | |
10167 | list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) { | |
10168 | struct io_uring_cqe *cqe = &ocqe->cqe; | |
10169 | ||
10170 | seq_printf(m, " user_data=%llu, res=%d, flags=%x\n", | |
10171 | cqe->user_data, cqe->res, cqe->flags); | |
10172 | ||
10173 | } | |
10174 | ||
79ebeaee | 10175 | spin_unlock(&ctx->completion_lock); |
87ce955b JA |
10176 | } |
10177 | ||
c072481d | 10178 | static __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
87ce955b JA |
10179 | { |
10180 | struct io_ring_ctx *ctx = f->private_data; | |
10181 | ||
10182 | if (percpu_ref_tryget(&ctx->refs)) { | |
10183 | __io_uring_show_fdinfo(ctx, m); | |
10184 | percpu_ref_put(&ctx->refs); | |
10185 | } | |
10186 | } | |
bebdb65e | 10187 | #endif |
87ce955b | 10188 | |
2b188cc1 JA |
10189 | static const struct file_operations io_uring_fops = { |
10190 | .release = io_uring_release, | |
10191 | .mmap = io_uring_mmap, | |
6c5c240e RP |
10192 | #ifndef CONFIG_MMU |
10193 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
10194 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
10195 | #endif | |
2b188cc1 | 10196 | .poll = io_uring_poll, |
bebdb65e | 10197 | #ifdef CONFIG_PROC_FS |
87ce955b | 10198 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 10199 | #endif |
2b188cc1 JA |
10200 | }; |
10201 | ||
c072481d PB |
10202 | static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
10203 | struct io_uring_params *p) | |
2b188cc1 | 10204 | { |
75b28aff HV |
10205 | struct io_rings *rings; |
10206 | size_t size, sq_array_offset; | |
2b188cc1 | 10207 | |
bd740481 JA |
10208 | /* make sure these are sane, as we already accounted them */ |
10209 | ctx->sq_entries = p->sq_entries; | |
10210 | ctx->cq_entries = p->cq_entries; | |
10211 | ||
75b28aff HV |
10212 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
10213 | if (size == SIZE_MAX) | |
10214 | return -EOVERFLOW; | |
10215 | ||
10216 | rings = io_mem_alloc(size); | |
10217 | if (!rings) | |
2b188cc1 JA |
10218 | return -ENOMEM; |
10219 | ||
75b28aff HV |
10220 | ctx->rings = rings; |
10221 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
10222 | rings->sq_ring_mask = p->sq_entries - 1; | |
10223 | rings->cq_ring_mask = p->cq_entries - 1; | |
10224 | rings->sq_ring_entries = p->sq_entries; | |
10225 | rings->cq_ring_entries = p->cq_entries; | |
2b188cc1 JA |
10226 | |
10227 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
10228 | if (size == SIZE_MAX) { |
10229 | io_mem_free(ctx->rings); | |
10230 | ctx->rings = NULL; | |
2b188cc1 | 10231 | return -EOVERFLOW; |
eb065d30 | 10232 | } |
2b188cc1 JA |
10233 | |
10234 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
10235 | if (!ctx->sq_sqes) { |
10236 | io_mem_free(ctx->rings); | |
10237 | ctx->rings = NULL; | |
2b188cc1 | 10238 | return -ENOMEM; |
eb065d30 | 10239 | } |
2b188cc1 | 10240 | |
2b188cc1 JA |
10241 | return 0; |
10242 | } | |
10243 | ||
9faadcc8 PB |
10244 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
10245 | { | |
10246 | int ret, fd; | |
10247 | ||
10248 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
10249 | if (fd < 0) | |
10250 | return fd; | |
10251 | ||
eef51daa | 10252 | ret = io_uring_add_tctx_node(ctx); |
9faadcc8 PB |
10253 | if (ret) { |
10254 | put_unused_fd(fd); | |
10255 | return ret; | |
10256 | } | |
10257 | fd_install(fd, file); | |
10258 | return fd; | |
10259 | } | |
10260 | ||
2b188cc1 JA |
10261 | /* |
10262 | * Allocate an anonymous fd, this is what constitutes the application | |
10263 | * visible backing of an io_uring instance. The application mmaps this | |
10264 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
10265 | * we have to tie this fd to a socket for file garbage collection purposes. | |
10266 | */ | |
9faadcc8 | 10267 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
10268 | { |
10269 | struct file *file; | |
9faadcc8 | 10270 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
10271 | int ret; |
10272 | ||
2b188cc1 JA |
10273 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
10274 | &ctx->ring_sock); | |
10275 | if (ret) | |
9faadcc8 | 10276 | return ERR_PTR(ret); |
2b188cc1 JA |
10277 | #endif |
10278 | ||
2b188cc1 JA |
10279 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
10280 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 10281 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
10282 | if (IS_ERR(file)) { |
10283 | sock_release(ctx->ring_sock); | |
10284 | ctx->ring_sock = NULL; | |
10285 | } else { | |
10286 | ctx->ring_sock->file = file; | |
0f212204 | 10287 | } |
2b188cc1 | 10288 | #endif |
9faadcc8 | 10289 | return file; |
2b188cc1 JA |
10290 | } |
10291 | ||
c072481d PB |
10292 | static __cold int io_uring_create(unsigned entries, struct io_uring_params *p, |
10293 | struct io_uring_params __user *params) | |
2b188cc1 | 10294 | { |
2b188cc1 | 10295 | struct io_ring_ctx *ctx; |
9faadcc8 | 10296 | struct file *file; |
2b188cc1 JA |
10297 | int ret; |
10298 | ||
8110c1a6 | 10299 | if (!entries) |
2b188cc1 | 10300 | return -EINVAL; |
8110c1a6 JA |
10301 | if (entries > IORING_MAX_ENTRIES) { |
10302 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
10303 | return -EINVAL; | |
10304 | entries = IORING_MAX_ENTRIES; | |
10305 | } | |
2b188cc1 JA |
10306 | |
10307 | /* | |
10308 | * Use twice as many entries for the CQ ring. It's possible for the | |
10309 | * application to drive a higher depth than the size of the SQ ring, | |
10310 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
10311 | * some flexibility in overcommitting a bit. If the application has |
10312 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
10313 | * of CQ ring entries manually. | |
2b188cc1 JA |
10314 | */ |
10315 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
10316 | if (p->flags & IORING_SETUP_CQSIZE) { |
10317 | /* | |
10318 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
10319 | * to a power-of-two, if it isn't already. We do NOT impose | |
10320 | * any cq vs sq ring sizing. | |
10321 | */ | |
eb2667b3 | 10322 | if (!p->cq_entries) |
33a107f0 | 10323 | return -EINVAL; |
8110c1a6 JA |
10324 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
10325 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
10326 | return -EINVAL; | |
10327 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
10328 | } | |
eb2667b3 JQ |
10329 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
10330 | if (p->cq_entries < p->sq_entries) | |
10331 | return -EINVAL; | |
33a107f0 JA |
10332 | } else { |
10333 | p->cq_entries = 2 * p->sq_entries; | |
10334 | } | |
2b188cc1 | 10335 | |
2b188cc1 | 10336 | ctx = io_ring_ctx_alloc(p); |
62e398be | 10337 | if (!ctx) |
2b188cc1 | 10338 | return -ENOMEM; |
2b188cc1 | 10339 | ctx->compat = in_compat_syscall(); |
62e398be JA |
10340 | if (!capable(CAP_IPC_LOCK)) |
10341 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
10342 | |
10343 | /* | |
10344 | * This is just grabbed for accounting purposes. When a process exits, | |
10345 | * the mm is exited and dropped before the files, hence we need to hang | |
10346 | * on to this mm purely for the purposes of being able to unaccount | |
10347 | * memory (locked/pinned vm). It's not used for anything else. | |
10348 | */ | |
6b7898eb | 10349 | mmgrab(current->mm); |
2aede0e4 | 10350 | ctx->mm_account = current->mm; |
6b7898eb | 10351 | |
2b188cc1 JA |
10352 | ret = io_allocate_scq_urings(ctx, p); |
10353 | if (ret) | |
10354 | goto err; | |
10355 | ||
7e84e1c7 | 10356 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
10357 | if (ret) |
10358 | goto err; | |
eae071c9 | 10359 | /* always set a rsrc node */ |
47b228ce PB |
10360 | ret = io_rsrc_node_switch_start(ctx); |
10361 | if (ret) | |
10362 | goto err; | |
eae071c9 | 10363 | io_rsrc_node_switch(ctx, NULL); |
2b188cc1 | 10364 | |
2b188cc1 | 10365 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
10366 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
10367 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
10368 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
10369 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
10370 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
10371 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
10372 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
10373 | |
10374 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
10375 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
10376 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
10377 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
10378 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
10379 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
10380 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 10381 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 10382 | |
7f13657d XW |
10383 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
10384 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 10385 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 10386 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9690557e PB |
10387 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
10388 | IORING_FEAT_RSRC_TAGS; | |
7f13657d XW |
10389 | |
10390 | if (copy_to_user(params, p, sizeof(*p))) { | |
10391 | ret = -EFAULT; | |
10392 | goto err; | |
10393 | } | |
d1719f70 | 10394 | |
9faadcc8 PB |
10395 | file = io_uring_get_file(ctx); |
10396 | if (IS_ERR(file)) { | |
10397 | ret = PTR_ERR(file); | |
10398 | goto err; | |
10399 | } | |
10400 | ||
044c1ab3 JA |
10401 | /* |
10402 | * Install ring fd as the very last thing, so we don't risk someone | |
10403 | * having closed it before we finish setup | |
10404 | */ | |
9faadcc8 PB |
10405 | ret = io_uring_install_fd(ctx, file); |
10406 | if (ret < 0) { | |
10407 | /* fput will clean it up */ | |
10408 | fput(file); | |
10409 | return ret; | |
10410 | } | |
044c1ab3 | 10411 | |
c826bd7a | 10412 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
10413 | return ret; |
10414 | err: | |
10415 | io_ring_ctx_wait_and_kill(ctx); | |
10416 | return ret; | |
10417 | } | |
10418 | ||
10419 | /* | |
10420 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
10421 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
10422 | * params structure passed in. | |
10423 | */ | |
10424 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
10425 | { | |
10426 | struct io_uring_params p; | |
2b188cc1 JA |
10427 | int i; |
10428 | ||
10429 | if (copy_from_user(&p, params, sizeof(p))) | |
10430 | return -EFAULT; | |
10431 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
10432 | if (p.resv[i]) | |
10433 | return -EINVAL; | |
10434 | } | |
10435 | ||
6c271ce2 | 10436 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 10437 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
10438 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
10439 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
10440 | return -EINVAL; |
10441 | ||
7f13657d | 10442 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
10443 | } |
10444 | ||
10445 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
10446 | struct io_uring_params __user *, params) | |
10447 | { | |
10448 | return io_uring_setup(entries, params); | |
10449 | } | |
10450 | ||
c072481d PB |
10451 | static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg, |
10452 | unsigned nr_args) | |
66f4af93 JA |
10453 | { |
10454 | struct io_uring_probe *p; | |
10455 | size_t size; | |
10456 | int i, ret; | |
10457 | ||
10458 | size = struct_size(p, ops, nr_args); | |
10459 | if (size == SIZE_MAX) | |
10460 | return -EOVERFLOW; | |
10461 | p = kzalloc(size, GFP_KERNEL); | |
10462 | if (!p) | |
10463 | return -ENOMEM; | |
10464 | ||
10465 | ret = -EFAULT; | |
10466 | if (copy_from_user(p, arg, size)) | |
10467 | goto out; | |
10468 | ret = -EINVAL; | |
10469 | if (memchr_inv(p, 0, size)) | |
10470 | goto out; | |
10471 | ||
10472 | p->last_op = IORING_OP_LAST - 1; | |
10473 | if (nr_args > IORING_OP_LAST) | |
10474 | nr_args = IORING_OP_LAST; | |
10475 | ||
10476 | for (i = 0; i < nr_args; i++) { | |
10477 | p->ops[i].op = i; | |
10478 | if (!io_op_defs[i].not_supported) | |
10479 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
10480 | } | |
10481 | p->ops_len = i; | |
10482 | ||
10483 | ret = 0; | |
10484 | if (copy_to_user(arg, p, size)) | |
10485 | ret = -EFAULT; | |
10486 | out: | |
10487 | kfree(p); | |
10488 | return ret; | |
10489 | } | |
10490 | ||
071698e1 JA |
10491 | static int io_register_personality(struct io_ring_ctx *ctx) |
10492 | { | |
4379bf8b | 10493 | const struct cred *creds; |
61cf9370 | 10494 | u32 id; |
1e6fa521 | 10495 | int ret; |
071698e1 | 10496 | |
4379bf8b | 10497 | creds = get_current_cred(); |
1e6fa521 | 10498 | |
61cf9370 MWO |
10499 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
10500 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); | |
a30f895a JA |
10501 | if (ret < 0) { |
10502 | put_cred(creds); | |
10503 | return ret; | |
10504 | } | |
10505 | return id; | |
071698e1 JA |
10506 | } |
10507 | ||
c072481d PB |
10508 | static __cold int io_register_restrictions(struct io_ring_ctx *ctx, |
10509 | void __user *arg, unsigned int nr_args) | |
21b55dbc SG |
10510 | { |
10511 | struct io_uring_restriction *res; | |
10512 | size_t size; | |
10513 | int i, ret; | |
10514 | ||
7e84e1c7 SG |
10515 | /* Restrictions allowed only if rings started disabled */ |
10516 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10517 | return -EBADFD; | |
10518 | ||
21b55dbc | 10519 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 10520 | if (ctx->restrictions.registered) |
21b55dbc SG |
10521 | return -EBUSY; |
10522 | ||
10523 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
10524 | return -EINVAL; | |
10525 | ||
10526 | size = array_size(nr_args, sizeof(*res)); | |
10527 | if (size == SIZE_MAX) | |
10528 | return -EOVERFLOW; | |
10529 | ||
10530 | res = memdup_user(arg, size); | |
10531 | if (IS_ERR(res)) | |
10532 | return PTR_ERR(res); | |
10533 | ||
10534 | ret = 0; | |
10535 | ||
10536 | for (i = 0; i < nr_args; i++) { | |
10537 | switch (res[i].opcode) { | |
10538 | case IORING_RESTRICTION_REGISTER_OP: | |
10539 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
10540 | ret = -EINVAL; | |
10541 | goto out; | |
10542 | } | |
10543 | ||
10544 | __set_bit(res[i].register_op, | |
10545 | ctx->restrictions.register_op); | |
10546 | break; | |
10547 | case IORING_RESTRICTION_SQE_OP: | |
10548 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
10549 | ret = -EINVAL; | |
10550 | goto out; | |
10551 | } | |
10552 | ||
10553 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
10554 | break; | |
10555 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
10556 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
10557 | break; | |
10558 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
10559 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
10560 | break; | |
10561 | default: | |
10562 | ret = -EINVAL; | |
10563 | goto out; | |
10564 | } | |
10565 | } | |
10566 | ||
10567 | out: | |
10568 | /* Reset all restrictions if an error happened */ | |
10569 | if (ret != 0) | |
10570 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
10571 | else | |
7e84e1c7 | 10572 | ctx->restrictions.registered = true; |
21b55dbc SG |
10573 | |
10574 | kfree(res); | |
10575 | return ret; | |
10576 | } | |
10577 | ||
7e84e1c7 SG |
10578 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
10579 | { | |
10580 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10581 | return -EBADFD; | |
10582 | ||
10583 | if (ctx->restrictions.registered) | |
10584 | ctx->restricted = 1; | |
10585 | ||
0298ef96 PB |
10586 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
10587 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) | |
10588 | wake_up(&ctx->sq_data->wait); | |
7e84e1c7 SG |
10589 | return 0; |
10590 | } | |
10591 | ||
fdecb662 | 10592 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 10593 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 PB |
10594 | unsigned nr_args) |
10595 | { | |
10596 | __u32 tmp; | |
10597 | int err; | |
10598 | ||
c3bdad02 PB |
10599 | if (up->resv) |
10600 | return -EINVAL; | |
98f0b3b4 PB |
10601 | if (check_add_overflow(up->offset, nr_args, &tmp)) |
10602 | return -EOVERFLOW; | |
10603 | err = io_rsrc_node_switch_start(ctx); | |
10604 | if (err) | |
10605 | return err; | |
10606 | ||
fdecb662 PB |
10607 | switch (type) { |
10608 | case IORING_RSRC_FILE: | |
98f0b3b4 | 10609 | return __io_sqe_files_update(ctx, up, nr_args); |
634d00df PB |
10610 | case IORING_RSRC_BUFFER: |
10611 | return __io_sqe_buffers_update(ctx, up, nr_args); | |
98f0b3b4 PB |
10612 | } |
10613 | return -EINVAL; | |
10614 | } | |
10615 | ||
c3bdad02 PB |
10616 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
10617 | unsigned nr_args) | |
98f0b3b4 | 10618 | { |
c3bdad02 | 10619 | struct io_uring_rsrc_update2 up; |
98f0b3b4 PB |
10620 | |
10621 | if (!nr_args) | |
10622 | return -EINVAL; | |
c3bdad02 PB |
10623 | memset(&up, 0, sizeof(up)); |
10624 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) | |
10625 | return -EFAULT; | |
10626 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); | |
10627 | } | |
10628 | ||
10629 | static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
992da01a | 10630 | unsigned size, unsigned type) |
c3bdad02 PB |
10631 | { |
10632 | struct io_uring_rsrc_update2 up; | |
10633 | ||
10634 | if (size != sizeof(up)) | |
10635 | return -EINVAL; | |
98f0b3b4 PB |
10636 | if (copy_from_user(&up, arg, sizeof(up))) |
10637 | return -EFAULT; | |
992da01a | 10638 | if (!up.nr || up.resv) |
98f0b3b4 | 10639 | return -EINVAL; |
992da01a | 10640 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
98f0b3b4 PB |
10641 | } |
10642 | ||
c072481d | 10643 | static __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
992da01a | 10644 | unsigned int size, unsigned int type) |
792e3582 PB |
10645 | { |
10646 | struct io_uring_rsrc_register rr; | |
10647 | ||
10648 | /* keep it extendible */ | |
10649 | if (size != sizeof(rr)) | |
10650 | return -EINVAL; | |
10651 | ||
10652 | memset(&rr, 0, sizeof(rr)); | |
10653 | if (copy_from_user(&rr, arg, size)) | |
10654 | return -EFAULT; | |
992da01a | 10655 | if (!rr.nr || rr.resv || rr.resv2) |
792e3582 PB |
10656 | return -EINVAL; |
10657 | ||
992da01a | 10658 | switch (type) { |
792e3582 PB |
10659 | case IORING_RSRC_FILE: |
10660 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), | |
10661 | rr.nr, u64_to_user_ptr(rr.tags)); | |
634d00df PB |
10662 | case IORING_RSRC_BUFFER: |
10663 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), | |
10664 | rr.nr, u64_to_user_ptr(rr.tags)); | |
792e3582 PB |
10665 | } |
10666 | return -EINVAL; | |
10667 | } | |
10668 | ||
c072481d PB |
10669 | static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx, |
10670 | void __user *arg, unsigned len) | |
fe76421d JA |
10671 | { |
10672 | struct io_uring_task *tctx = current->io_uring; | |
10673 | cpumask_var_t new_mask; | |
10674 | int ret; | |
10675 | ||
10676 | if (!tctx || !tctx->io_wq) | |
10677 | return -EINVAL; | |
10678 | ||
10679 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | |
10680 | return -ENOMEM; | |
10681 | ||
10682 | cpumask_clear(new_mask); | |
10683 | if (len > cpumask_size()) | |
10684 | len = cpumask_size(); | |
10685 | ||
10686 | if (copy_from_user(new_mask, arg, len)) { | |
10687 | free_cpumask_var(new_mask); | |
10688 | return -EFAULT; | |
10689 | } | |
10690 | ||
10691 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); | |
10692 | free_cpumask_var(new_mask); | |
10693 | return ret; | |
10694 | } | |
10695 | ||
c072481d | 10696 | static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx) |
fe76421d JA |
10697 | { |
10698 | struct io_uring_task *tctx = current->io_uring; | |
10699 | ||
10700 | if (!tctx || !tctx->io_wq) | |
10701 | return -EINVAL; | |
10702 | ||
10703 | return io_wq_cpu_affinity(tctx->io_wq, NULL); | |
10704 | } | |
10705 | ||
c072481d PB |
10706 | static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, |
10707 | void __user *arg) | |
b22fa62a | 10708 | __must_hold(&ctx->uring_lock) |
2e480058 | 10709 | { |
b22fa62a | 10710 | struct io_tctx_node *node; |
fa84693b JA |
10711 | struct io_uring_task *tctx = NULL; |
10712 | struct io_sq_data *sqd = NULL; | |
2e480058 JA |
10713 | __u32 new_count[2]; |
10714 | int i, ret; | |
10715 | ||
2e480058 JA |
10716 | if (copy_from_user(new_count, arg, sizeof(new_count))) |
10717 | return -EFAULT; | |
10718 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
10719 | if (new_count[i] > INT_MAX) | |
10720 | return -EINVAL; | |
10721 | ||
fa84693b JA |
10722 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
10723 | sqd = ctx->sq_data; | |
10724 | if (sqd) { | |
009ad9f0 JA |
10725 | /* |
10726 | * Observe the correct sqd->lock -> ctx->uring_lock | |
10727 | * ordering. Fine to drop uring_lock here, we hold | |
10728 | * a ref to the ctx. | |
10729 | */ | |
41d3a6bd | 10730 | refcount_inc(&sqd->refs); |
009ad9f0 | 10731 | mutex_unlock(&ctx->uring_lock); |
fa84693b | 10732 | mutex_lock(&sqd->lock); |
009ad9f0 | 10733 | mutex_lock(&ctx->uring_lock); |
41d3a6bd JA |
10734 | if (sqd->thread) |
10735 | tctx = sqd->thread->io_uring; | |
fa84693b JA |
10736 | } |
10737 | } else { | |
10738 | tctx = current->io_uring; | |
10739 | } | |
10740 | ||
e139a1ec | 10741 | BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits)); |
fa84693b | 10742 | |
e139a1ec PB |
10743 | memcpy(ctx->iowq_limits, new_count, sizeof(new_count)); |
10744 | ctx->iowq_limits_set = true; | |
10745 | ||
10746 | ret = -EINVAL; | |
10747 | if (tctx && tctx->io_wq) { | |
10748 | ret = io_wq_max_workers(tctx->io_wq, new_count); | |
10749 | if (ret) | |
10750 | goto err; | |
10751 | } else { | |
10752 | memset(new_count, 0, sizeof(new_count)); | |
10753 | } | |
fa84693b | 10754 | |
41d3a6bd | 10755 | if (sqd) { |
fa84693b | 10756 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
10757 | io_put_sq_data(sqd); |
10758 | } | |
2e480058 JA |
10759 | |
10760 | if (copy_to_user(arg, new_count, sizeof(new_count))) | |
10761 | return -EFAULT; | |
10762 | ||
b22fa62a PB |
10763 | /* that's it for SQPOLL, only the SQPOLL task creates requests */ |
10764 | if (sqd) | |
10765 | return 0; | |
10766 | ||
10767 | /* now propagate the restriction to all registered users */ | |
10768 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
10769 | struct io_uring_task *tctx = node->task->io_uring; | |
10770 | ||
10771 | if (WARN_ON_ONCE(!tctx->io_wq)) | |
10772 | continue; | |
10773 | ||
10774 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
10775 | new_count[i] = ctx->iowq_limits[i]; | |
10776 | /* ignore errors, it always returns zero anyway */ | |
10777 | (void)io_wq_max_workers(tctx->io_wq, new_count); | |
10778 | } | |
2e480058 | 10779 | return 0; |
fa84693b | 10780 | err: |
41d3a6bd | 10781 | if (sqd) { |
fa84693b | 10782 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
10783 | io_put_sq_data(sqd); |
10784 | } | |
fa84693b | 10785 | return ret; |
2e480058 JA |
10786 | } |
10787 | ||
071698e1 JA |
10788 | static bool io_register_op_must_quiesce(int op) |
10789 | { | |
10790 | switch (op) { | |
bd54b6fe BM |
10791 | case IORING_REGISTER_BUFFERS: |
10792 | case IORING_UNREGISTER_BUFFERS: | |
f4f7d21c | 10793 | case IORING_REGISTER_FILES: |
071698e1 JA |
10794 | case IORING_UNREGISTER_FILES: |
10795 | case IORING_REGISTER_FILES_UPDATE: | |
10796 | case IORING_REGISTER_PROBE: | |
10797 | case IORING_REGISTER_PERSONALITY: | |
10798 | case IORING_UNREGISTER_PERSONALITY: | |
992da01a PB |
10799 | case IORING_REGISTER_FILES2: |
10800 | case IORING_REGISTER_FILES_UPDATE2: | |
10801 | case IORING_REGISTER_BUFFERS2: | |
10802 | case IORING_REGISTER_BUFFERS_UPDATE: | |
fe76421d JA |
10803 | case IORING_REGISTER_IOWQ_AFF: |
10804 | case IORING_UNREGISTER_IOWQ_AFF: | |
2e480058 | 10805 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
071698e1 JA |
10806 | return false; |
10807 | default: | |
10808 | return true; | |
10809 | } | |
10810 | } | |
10811 | ||
c072481d | 10812 | static __cold int io_ctx_quiesce(struct io_ring_ctx *ctx) |
e73c5c7c PB |
10813 | { |
10814 | long ret; | |
10815 | ||
10816 | percpu_ref_kill(&ctx->refs); | |
10817 | ||
10818 | /* | |
10819 | * Drop uring mutex before waiting for references to exit. If another | |
10820 | * thread is currently inside io_uring_enter() it might need to grab the | |
10821 | * uring_lock to make progress. If we hold it here across the drain | |
10822 | * wait, then we can deadlock. It's safe to drop the mutex here, since | |
10823 | * no new references will come in after we've killed the percpu ref. | |
10824 | */ | |
10825 | mutex_unlock(&ctx->uring_lock); | |
10826 | do { | |
37f0e767 PB |
10827 | ret = wait_for_completion_interruptible_timeout(&ctx->ref_comp, HZ); |
10828 | if (ret) { | |
10829 | ret = min(0L, ret); | |
e73c5c7c | 10830 | break; |
37f0e767 PB |
10831 | } |
10832 | ||
e73c5c7c | 10833 | ret = io_run_task_work_sig(); |
37f0e767 | 10834 | io_req_caches_free(ctx); |
e73c5c7c PB |
10835 | } while (ret >= 0); |
10836 | mutex_lock(&ctx->uring_lock); | |
10837 | ||
10838 | if (ret) | |
10839 | io_refs_resurrect(&ctx->refs, &ctx->ref_comp); | |
10840 | return ret; | |
10841 | } | |
10842 | ||
edafccee JA |
10843 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
10844 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
10845 | __releases(ctx->uring_lock) |
10846 | __acquires(ctx->uring_lock) | |
edafccee JA |
10847 | { |
10848 | int ret; | |
10849 | ||
35fa71a0 JA |
10850 | /* |
10851 | * We're inside the ring mutex, if the ref is already dying, then | |
10852 | * someone else killed the ctx or is already going through | |
10853 | * io_uring_register(). | |
10854 | */ | |
10855 | if (percpu_ref_is_dying(&ctx->refs)) | |
10856 | return -ENXIO; | |
10857 | ||
75c4021a PB |
10858 | if (ctx->restricted) { |
10859 | if (opcode >= IORING_REGISTER_LAST) | |
10860 | return -EINVAL; | |
10861 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); | |
10862 | if (!test_bit(opcode, ctx->restrictions.register_op)) | |
10863 | return -EACCES; | |
10864 | } | |
10865 | ||
071698e1 | 10866 | if (io_register_op_must_quiesce(opcode)) { |
e73c5c7c PB |
10867 | ret = io_ctx_quiesce(ctx); |
10868 | if (ret) | |
f70865db | 10869 | return ret; |
05f3fb3c | 10870 | } |
edafccee JA |
10871 | |
10872 | switch (opcode) { | |
10873 | case IORING_REGISTER_BUFFERS: | |
634d00df | 10874 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
edafccee JA |
10875 | break; |
10876 | case IORING_UNREGISTER_BUFFERS: | |
10877 | ret = -EINVAL; | |
10878 | if (arg || nr_args) | |
10879 | break; | |
0a96bbe4 | 10880 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 10881 | break; |
6b06314c | 10882 | case IORING_REGISTER_FILES: |
792e3582 | 10883 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
6b06314c JA |
10884 | break; |
10885 | case IORING_UNREGISTER_FILES: | |
10886 | ret = -EINVAL; | |
10887 | if (arg || nr_args) | |
10888 | break; | |
10889 | ret = io_sqe_files_unregister(ctx); | |
10890 | break; | |
c3a31e60 | 10891 | case IORING_REGISTER_FILES_UPDATE: |
c3bdad02 | 10892 | ret = io_register_files_update(ctx, arg, nr_args); |
c3a31e60 | 10893 | break; |
9b402849 | 10894 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 10895 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
10896 | ret = -EINVAL; |
10897 | if (nr_args != 1) | |
10898 | break; | |
10899 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
10900 | if (ret) |
10901 | break; | |
10902 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
10903 | ctx->eventfd_async = 1; | |
10904 | else | |
10905 | ctx->eventfd_async = 0; | |
9b402849 JA |
10906 | break; |
10907 | case IORING_UNREGISTER_EVENTFD: | |
10908 | ret = -EINVAL; | |
10909 | if (arg || nr_args) | |
10910 | break; | |
10911 | ret = io_eventfd_unregister(ctx); | |
10912 | break; | |
66f4af93 JA |
10913 | case IORING_REGISTER_PROBE: |
10914 | ret = -EINVAL; | |
10915 | if (!arg || nr_args > 256) | |
10916 | break; | |
10917 | ret = io_probe(ctx, arg, nr_args); | |
10918 | break; | |
071698e1 JA |
10919 | case IORING_REGISTER_PERSONALITY: |
10920 | ret = -EINVAL; | |
10921 | if (arg || nr_args) | |
10922 | break; | |
10923 | ret = io_register_personality(ctx); | |
10924 | break; | |
10925 | case IORING_UNREGISTER_PERSONALITY: | |
10926 | ret = -EINVAL; | |
10927 | if (arg) | |
10928 | break; | |
10929 | ret = io_unregister_personality(ctx, nr_args); | |
10930 | break; | |
7e84e1c7 SG |
10931 | case IORING_REGISTER_ENABLE_RINGS: |
10932 | ret = -EINVAL; | |
10933 | if (arg || nr_args) | |
10934 | break; | |
10935 | ret = io_register_enable_rings(ctx); | |
10936 | break; | |
21b55dbc SG |
10937 | case IORING_REGISTER_RESTRICTIONS: |
10938 | ret = io_register_restrictions(ctx, arg, nr_args); | |
10939 | break; | |
992da01a PB |
10940 | case IORING_REGISTER_FILES2: |
10941 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); | |
10942 | break; | |
10943 | case IORING_REGISTER_FILES_UPDATE2: | |
10944 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
10945 | IORING_RSRC_FILE); | |
10946 | break; | |
10947 | case IORING_REGISTER_BUFFERS2: | |
10948 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); | |
792e3582 | 10949 | break; |
992da01a PB |
10950 | case IORING_REGISTER_BUFFERS_UPDATE: |
10951 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
10952 | IORING_RSRC_BUFFER); | |
c3bdad02 | 10953 | break; |
fe76421d JA |
10954 | case IORING_REGISTER_IOWQ_AFF: |
10955 | ret = -EINVAL; | |
10956 | if (!arg || !nr_args) | |
10957 | break; | |
10958 | ret = io_register_iowq_aff(ctx, arg, nr_args); | |
10959 | break; | |
10960 | case IORING_UNREGISTER_IOWQ_AFF: | |
10961 | ret = -EINVAL; | |
10962 | if (arg || nr_args) | |
10963 | break; | |
10964 | ret = io_unregister_iowq_aff(ctx); | |
10965 | break; | |
2e480058 JA |
10966 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
10967 | ret = -EINVAL; | |
10968 | if (!arg || nr_args != 2) | |
10969 | break; | |
10970 | ret = io_register_iowq_max_workers(ctx, arg); | |
10971 | break; | |
edafccee JA |
10972 | default: |
10973 | ret = -EINVAL; | |
10974 | break; | |
10975 | } | |
10976 | ||
071698e1 | 10977 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 10978 | /* bring the ctx back to life */ |
05f3fb3c | 10979 | percpu_ref_reinit(&ctx->refs); |
0f158b4c | 10980 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 10981 | } |
edafccee JA |
10982 | return ret; |
10983 | } | |
10984 | ||
10985 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
10986 | void __user *, arg, unsigned int, nr_args) | |
10987 | { | |
10988 | struct io_ring_ctx *ctx; | |
10989 | long ret = -EBADF; | |
10990 | struct fd f; | |
10991 | ||
10992 | f = fdget(fd); | |
10993 | if (!f.file) | |
10994 | return -EBADF; | |
10995 | ||
10996 | ret = -EOPNOTSUPP; | |
10997 | if (f.file->f_op != &io_uring_fops) | |
10998 | goto out_fput; | |
10999 | ||
11000 | ctx = f.file->private_data; | |
11001 | ||
b6c23dd5 PB |
11002 | io_run_task_work(); |
11003 | ||
edafccee JA |
11004 | mutex_lock(&ctx->uring_lock); |
11005 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
11006 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
11007 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
11008 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
11009 | out_fput: |
11010 | fdput(f); | |
11011 | return ret; | |
11012 | } | |
11013 | ||
2b188cc1 JA |
11014 | static int __init io_uring_init(void) |
11015 | { | |
d7f62e82 SM |
11016 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
11017 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
11018 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
11019 | } while (0) | |
11020 | ||
11021 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
11022 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
11023 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
11024 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
11025 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
11026 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
11027 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
11028 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
11029 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
11030 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 11031 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
11032 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
11033 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
11034 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
11035 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
11036 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
11037 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
11038 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
11039 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
11040 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
11041 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
11042 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
11043 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
11044 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
11045 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
11046 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 11047 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
11048 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
11049 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
16340eab | 11050 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
d7f62e82 | 11051 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
7d67af2c | 11052 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
b9445598 | 11053 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
d7f62e82 | 11054 | |
b0d658ec PB |
11055 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
11056 | sizeof(struct io_uring_rsrc_update)); | |
11057 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > | |
11058 | sizeof(struct io_uring_rsrc_update2)); | |
90499ad0 PB |
11059 | |
11060 | /* ->buf_index is u16 */ | |
11061 | BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); | |
11062 | ||
b0d658ec PB |
11063 | /* should fit into one byte */ |
11064 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); | |
68fe256a PB |
11065 | BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8)); |
11066 | BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS); | |
b0d658ec | 11067 | |
d3656344 | 11068 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
32c2d33e | 11069 | BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int)); |
16340eab | 11070 | |
91f245d5 JA |
11071 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
11072 | SLAB_ACCOUNT); | |
2b188cc1 JA |
11073 | return 0; |
11074 | }; | |
11075 | __initcall(io_uring_init); |