]>
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> | |
edce22e1 | 60 | #include <linux/blk-mq.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> |
5bd2182d | 81 | #include <linux/audit.h> |
cdc1404a | 82 | #include <linux/security.h> |
2b188cc1 | 83 | |
c826bd7a DD |
84 | #define CREATE_TRACE_POINTS |
85 | #include <trace/events/io_uring.h> | |
86 | ||
2b188cc1 JA |
87 | #include <uapi/linux/io_uring.h> |
88 | ||
89 | #include "internal.h" | |
561fb04a | 90 | #include "io-wq.h" |
2b188cc1 | 91 | |
5277deaa | 92 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 93 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
4ce8ad95 | 94 | #define IORING_SQPOLL_CAP_ENTRIES_VALUE 8 |
65e19f54 | 95 | |
187f08c1 | 96 | /* only define max */ |
042b0d85 | 97 | #define IORING_MAX_FIXED_FILES (1U << 15) |
21b55dbc SG |
98 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
99 | IORING_REGISTER_LAST + IORING_OP_LAST) | |
2b188cc1 | 100 | |
187f08c1 | 101 | #define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3) |
2d091d62 PB |
102 | #define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT) |
103 | #define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1) | |
104 | ||
489809e2 PB |
105 | #define IORING_MAX_REG_BUFFERS (1U << 14) |
106 | ||
68fe256a PB |
107 | #define SQE_COMMON_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_LINK | \ |
108 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) | |
109 | ||
5562a8d7 PB |
110 | #define SQE_VALID_FLAGS (SQE_COMMON_FLAGS | IOSQE_BUFFER_SELECT | \ |
111 | IOSQE_IO_DRAIN | IOSQE_CQE_SKIP_SUCCESS) | |
68fe256a | 112 | |
c854357b | 113 | #define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \ |
d5361233 | 114 | REQ_F_POLLED | REQ_F_CREDS | REQ_F_ASYNC_DATA) |
b16fed66 | 115 | |
a538be5b PB |
116 | #define IO_REQ_CLEAN_SLOW_FLAGS (REQ_F_REFCOUNT | REQ_F_LINK | REQ_F_HARDLINK |\ |
117 | IO_REQ_CLEAN_FLAGS) | |
118 | ||
09899b19 PB |
119 | #define IO_TCTX_REFS_CACHE_NR (1U << 10) |
120 | ||
2b188cc1 JA |
121 | struct io_uring { |
122 | u32 head ____cacheline_aligned_in_smp; | |
123 | u32 tail ____cacheline_aligned_in_smp; | |
124 | }; | |
125 | ||
1e84b97b | 126 | /* |
75b28aff HV |
127 | * This data is shared with the application through the mmap at offsets |
128 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
129 | * |
130 | * The offsets to the member fields are published through struct | |
131 | * io_sqring_offsets when calling io_uring_setup. | |
132 | */ | |
75b28aff | 133 | struct io_rings { |
1e84b97b SB |
134 | /* |
135 | * Head and tail offsets into the ring; the offsets need to be | |
136 | * masked to get valid indices. | |
137 | * | |
75b28aff HV |
138 | * The kernel controls head of the sq ring and the tail of the cq ring, |
139 | * and the application controls tail of the sq ring and the head of the | |
140 | * cq ring. | |
1e84b97b | 141 | */ |
75b28aff | 142 | struct io_uring sq, cq; |
1e84b97b | 143 | /* |
75b28aff | 144 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
145 | * ring_entries - 1) |
146 | */ | |
75b28aff HV |
147 | u32 sq_ring_mask, cq_ring_mask; |
148 | /* Ring sizes (constant, power of 2) */ | |
149 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
150 | /* |
151 | * Number of invalid entries dropped by the kernel due to | |
152 | * invalid index stored in array | |
153 | * | |
154 | * Written by the kernel, shouldn't be modified by the | |
155 | * application (i.e. get number of "new events" by comparing to | |
156 | * cached value). | |
157 | * | |
158 | * After a new SQ head value was read by the application this | |
159 | * counter includes all submissions that were dropped reaching | |
160 | * the new SQ head (and possibly more). | |
161 | */ | |
75b28aff | 162 | u32 sq_dropped; |
1e84b97b | 163 | /* |
0d9b5b3a | 164 | * Runtime SQ flags |
1e84b97b SB |
165 | * |
166 | * Written by the kernel, shouldn't be modified by the | |
167 | * application. | |
168 | * | |
169 | * The application needs a full memory barrier before checking | |
170 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
171 | */ | |
75b28aff | 172 | u32 sq_flags; |
0d9b5b3a SG |
173 | /* |
174 | * Runtime CQ flags | |
175 | * | |
176 | * Written by the application, shouldn't be modified by the | |
177 | * kernel. | |
178 | */ | |
fe7e3257 | 179 | u32 cq_flags; |
1e84b97b SB |
180 | /* |
181 | * Number of completion events lost because the queue was full; | |
182 | * this should be avoided by the application by making sure | |
0b4295b5 | 183 | * there are not more requests pending than there is space in |
1e84b97b SB |
184 | * the completion queue. |
185 | * | |
186 | * Written by the kernel, shouldn't be modified by the | |
187 | * application (i.e. get number of "new events" by comparing to | |
188 | * cached value). | |
189 | * | |
190 | * As completion events come in out of order this counter is not | |
191 | * ordered with any other data. | |
192 | */ | |
75b28aff | 193 | u32 cq_overflow; |
1e84b97b SB |
194 | /* |
195 | * Ring buffer of completion events. | |
196 | * | |
197 | * The kernel writes completion events fresh every time they are | |
198 | * produced, so the application is allowed to modify pending | |
199 | * entries. | |
200 | */ | |
75b28aff | 201 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
202 | }; |
203 | ||
45d189c6 | 204 | enum io_uring_cmd_flags { |
51aac424 | 205 | IO_URING_F_COMPLETE_DEFER = 1, |
3b44b371 | 206 | IO_URING_F_UNLOCKED = 2, |
51aac424 PB |
207 | /* int's last bit, sign checks are usually faster than a bit test */ |
208 | IO_URING_F_NONBLOCK = INT_MIN, | |
45d189c6 PB |
209 | }; |
210 | ||
edafccee JA |
211 | struct io_mapped_ubuf { |
212 | u64 ubuf; | |
4751f53d | 213 | u64 ubuf_end; |
edafccee | 214 | unsigned int nr_bvecs; |
de293938 | 215 | unsigned long acct_pages; |
41edf1a5 | 216 | struct bio_vec bvec[]; |
edafccee JA |
217 | }; |
218 | ||
50238531 BM |
219 | struct io_ring_ctx; |
220 | ||
6c2450ae PB |
221 | struct io_overflow_cqe { |
222 | struct io_uring_cqe cqe; | |
223 | struct list_head list; | |
224 | }; | |
225 | ||
a04b0ac0 PB |
226 | struct io_fixed_file { |
227 | /* file * with additional FFS_* flags */ | |
228 | unsigned long file_ptr; | |
229 | }; | |
230 | ||
269bbe5f BM |
231 | struct io_rsrc_put { |
232 | struct list_head list; | |
b60c8dce | 233 | u64 tag; |
50238531 BM |
234 | union { |
235 | void *rsrc; | |
236 | struct file *file; | |
bd54b6fe | 237 | struct io_mapped_ubuf *buf; |
50238531 | 238 | }; |
269bbe5f BM |
239 | }; |
240 | ||
aeca241b | 241 | struct io_file_table { |
042b0d85 | 242 | struct io_fixed_file *files; |
31b51510 JA |
243 | }; |
244 | ||
b895c9a6 | 245 | struct io_rsrc_node { |
05589553 XW |
246 | struct percpu_ref refs; |
247 | struct list_head node; | |
269bbe5f | 248 | struct list_head rsrc_list; |
b895c9a6 | 249 | struct io_rsrc_data *rsrc_data; |
4a38aed2 | 250 | struct llist_node llist; |
e297822b | 251 | bool done; |
05589553 XW |
252 | }; |
253 | ||
40ae0ff7 PB |
254 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
255 | ||
b895c9a6 | 256 | struct io_rsrc_data { |
05f3fb3c JA |
257 | struct io_ring_ctx *ctx; |
258 | ||
2d091d62 PB |
259 | u64 **tags; |
260 | unsigned int nr; | |
40ae0ff7 | 261 | rsrc_put_fn *do_put; |
3e942498 | 262 | atomic_t refs; |
05f3fb3c | 263 | struct completion done; |
8bad28d8 | 264 | bool quiesce; |
05f3fb3c JA |
265 | }; |
266 | ||
dbc7d452 JA |
267 | struct io_buffer_list { |
268 | struct list_head list; | |
269 | struct list_head buf_list; | |
270 | __u16 bgid; | |
271 | }; | |
272 | ||
5a2e745d JA |
273 | struct io_buffer { |
274 | struct list_head list; | |
275 | __u64 addr; | |
d1f82808 | 276 | __u32 len; |
5a2e745d | 277 | __u16 bid; |
b1c62645 | 278 | __u16 bgid; |
5a2e745d JA |
279 | }; |
280 | ||
21b55dbc SG |
281 | struct io_restriction { |
282 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
283 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
284 | u8 sqe_flags_allowed; | |
285 | u8 sqe_flags_required; | |
7e84e1c7 | 286 | bool registered; |
21b55dbc SG |
287 | }; |
288 | ||
37d1e2e3 JA |
289 | enum { |
290 | IO_SQ_THREAD_SHOULD_STOP = 0, | |
291 | IO_SQ_THREAD_SHOULD_PARK, | |
292 | }; | |
293 | ||
534ca6d6 JA |
294 | struct io_sq_data { |
295 | refcount_t refs; | |
9e138a48 | 296 | atomic_t park_pending; |
09a6f4ef | 297 | struct mutex lock; |
69fb2131 JA |
298 | |
299 | /* ctx's that are using this sqd */ | |
300 | struct list_head ctx_list; | |
69fb2131 | 301 | |
534ca6d6 JA |
302 | struct task_struct *thread; |
303 | struct wait_queue_head wait; | |
08369246 XW |
304 | |
305 | unsigned sq_thread_idle; | |
37d1e2e3 JA |
306 | int sq_cpu; |
307 | pid_t task_pid; | |
5c2469e0 | 308 | pid_t task_tgid; |
37d1e2e3 JA |
309 | |
310 | unsigned long state; | |
37d1e2e3 | 311 | struct completion exited; |
534ca6d6 JA |
312 | }; |
313 | ||
6dd0be1e | 314 | #define IO_COMPL_BATCH 32 |
6ff119a6 | 315 | #define IO_REQ_CACHE_SIZE 32 |
bf019da7 | 316 | #define IO_REQ_ALLOC_BATCH 8 |
258b29a9 | 317 | |
a1ab7b35 PB |
318 | struct io_submit_link { |
319 | struct io_kiocb *head; | |
320 | struct io_kiocb *last; | |
321 | }; | |
322 | ||
258b29a9 | 323 | struct io_submit_state { |
5a158c6b PB |
324 | /* inline/task_work completion list, under ->uring_lock */ |
325 | struct io_wq_work_node free_list; | |
326 | /* batch completion logic */ | |
327 | struct io_wq_work_list compl_reqs; | |
a1ab7b35 | 328 | struct io_submit_link link; |
258b29a9 | 329 | |
258b29a9 | 330 | bool plug_started; |
4b628aeb | 331 | bool need_plug; |
3d4aeb9f | 332 | bool flush_cqes; |
5ca7a8b3 | 333 | unsigned short submit_nr; |
5a158c6b | 334 | struct blk_plug plug; |
258b29a9 PB |
335 | }; |
336 | ||
77bc59b4 UA |
337 | struct io_ev_fd { |
338 | struct eventfd_ctx *cq_ev_fd; | |
c75312dd | 339 | unsigned int eventfd_async: 1; |
77bc59b4 UA |
340 | struct rcu_head rcu; |
341 | }; | |
342 | ||
dbc7d452 JA |
343 | #define IO_BUFFERS_HASH_BITS 5 |
344 | ||
2b188cc1 | 345 | struct io_ring_ctx { |
b52ecf8c | 346 | /* const or read-mostly hot data */ |
2b188cc1 JA |
347 | struct { |
348 | struct percpu_ref refs; | |
2b188cc1 | 349 | |
b52ecf8c | 350 | struct io_rings *rings; |
2b188cc1 | 351 | unsigned int flags; |
e1d85334 | 352 | unsigned int compat: 1; |
e1d85334 | 353 | unsigned int drain_next: 1; |
21b55dbc | 354 | unsigned int restricted: 1; |
f18ee4cf | 355 | unsigned int off_timeout_used: 1; |
10c66904 | 356 | unsigned int drain_active: 1; |
5562a8d7 | 357 | unsigned int drain_disabled: 1; |
9aa8dfde | 358 | unsigned int has_evfd: 1; |
773697b6 | 359 | unsigned int syscall_iopoll: 1; |
b52ecf8c | 360 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 361 | |
7f1129d2 | 362 | /* submission data */ |
b52ecf8c | 363 | struct { |
0499e582 PB |
364 | struct mutex uring_lock; |
365 | ||
75b28aff HV |
366 | /* |
367 | * Ring buffer of indices into array of io_uring_sqe, which is | |
368 | * mmapped by the application using the IORING_OFF_SQES offset. | |
369 | * | |
370 | * This indirection could e.g. be used to assign fixed | |
371 | * io_uring_sqe entries to operations and only submit them to | |
372 | * the queue when needed. | |
373 | * | |
374 | * The kernel modifies neither the indices array nor the entries | |
375 | * array. | |
376 | */ | |
377 | u32 *sq_array; | |
c7af47cf | 378 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
379 | unsigned cached_sq_head; |
380 | unsigned sq_entries; | |
de0617e4 | 381 | struct list_head defer_list; |
7f1129d2 PB |
382 | |
383 | /* | |
384 | * Fixed resources fast path, should be accessed only under | |
385 | * uring_lock, and updated through io_uring_register(2) | |
386 | */ | |
387 | struct io_rsrc_node *rsrc_node; | |
ab409402 | 388 | int rsrc_cached_refs; |
7f1129d2 PB |
389 | struct io_file_table file_table; |
390 | unsigned nr_user_files; | |
391 | unsigned nr_user_bufs; | |
392 | struct io_mapped_ubuf **user_bufs; | |
393 | ||
394 | struct io_submit_state submit_state; | |
5262f567 | 395 | struct list_head timeout_list; |
ef9dd637 | 396 | struct list_head ltimeout_list; |
1d7bb1d5 | 397 | struct list_head cq_overflow_list; |
dbc7d452 | 398 | struct list_head *io_buffers; |
cc3cec83 | 399 | struct list_head io_buffers_cache; |
4d9237e3 | 400 | struct list_head apoll_cache; |
7f1129d2 PB |
401 | struct xarray personalities; |
402 | u32 pers_next; | |
403 | unsigned sq_thread_idle; | |
2b188cc1 JA |
404 | } ____cacheline_aligned_in_smp; |
405 | ||
d0acdee2 | 406 | /* IRQ completion list, under ->completion_lock */ |
c2b6c6bc | 407 | struct io_wq_work_list locked_free_list; |
d0acdee2 | 408 | unsigned int locked_free_nr; |
3c1a2ead | 409 | |
7c30f36a | 410 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
534ca6d6 JA |
411 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
412 | ||
90554200 | 413 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 414 | struct list_head sqd_list; |
75b28aff | 415 | |
5ed7a37d PB |
416 | unsigned long check_cq_overflow; |
417 | ||
206aefde | 418 | struct { |
d8da428b PB |
419 | /* |
420 | * We cache a range of free CQEs we can use, once exhausted it | |
421 | * should go through a slower range setup, see __io_get_cqe() | |
422 | */ | |
423 | struct io_uring_cqe *cqe_cached; | |
424 | struct io_uring_cqe *cqe_sentinel; | |
425 | ||
206aefde JA |
426 | unsigned cached_cq_tail; |
427 | unsigned cq_entries; | |
77bc59b4 | 428 | struct io_ev_fd __rcu *io_ev_fd; |
0499e582 PB |
429 | struct wait_queue_head cq_wait; |
430 | unsigned cq_extra; | |
431 | atomic_t cq_timeouts; | |
0499e582 | 432 | unsigned cq_last_tm_flush; |
206aefde | 433 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 434 | |
2b188cc1 JA |
435 | struct { |
436 | spinlock_t completion_lock; | |
e94f141b | 437 | |
89850fce JA |
438 | spinlock_t timeout_lock; |
439 | ||
def596e9 | 440 | /* |
540e32a0 | 441 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
442 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
443 | * For SQPOLL, only the single threaded io_sq_thread() will | |
444 | * manipulate the list, hence no extra locking is needed there. | |
445 | */ | |
5eef4e87 | 446 | struct io_wq_work_list iopoll_list; |
78076bb6 JA |
447 | struct hlist_head *cancel_hash; |
448 | unsigned cancel_hash_bits; | |
915b3dde | 449 | bool poll_multi_queue; |
cc3cec83 JA |
450 | |
451 | struct list_head io_buffers_comp; | |
2b188cc1 | 452 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 453 | |
21b55dbc | 454 | struct io_restriction restrictions; |
3c1a2ead | 455 | |
b13a8918 PB |
456 | /* slow path rsrc auxilary data, used by update/register */ |
457 | struct { | |
458 | struct io_rsrc_node *rsrc_backup_node; | |
459 | struct io_mapped_ubuf *dummy_ubuf; | |
460 | struct io_rsrc_data *file_data; | |
461 | struct io_rsrc_data *buf_data; | |
462 | ||
463 | struct delayed_work rsrc_put_work; | |
464 | struct llist_head rsrc_put_llist; | |
465 | struct list_head rsrc_ref_list; | |
466 | spinlock_t rsrc_ref_lock; | |
cc3cec83 JA |
467 | |
468 | struct list_head io_buffers_pages; | |
b13a8918 PB |
469 | }; |
470 | ||
3c1a2ead | 471 | /* Keep this last, we don't need it for the fast path */ |
b986af7e PB |
472 | struct { |
473 | #if defined(CONFIG_UNIX) | |
474 | struct socket *ring_sock; | |
475 | #endif | |
476 | /* hashed buffered write serialization */ | |
477 | struct io_wq_hash *hash_map; | |
478 | ||
479 | /* Only used for accounting purposes */ | |
480 | struct user_struct *user; | |
481 | struct mm_struct *mm_account; | |
482 | ||
483 | /* ctx exit and cancelation */ | |
9011bf9a PB |
484 | struct llist_head fallback_llist; |
485 | struct delayed_work fallback_work; | |
b986af7e PB |
486 | struct work_struct exit_work; |
487 | struct list_head tctx_list; | |
488 | struct completion ref_comp; | |
e139a1ec PB |
489 | u32 iowq_limits[2]; |
490 | bool iowq_limits_set; | |
b986af7e | 491 | }; |
2b188cc1 JA |
492 | }; |
493 | ||
e7a6c00d JA |
494 | /* |
495 | * Arbitrary limit, can be raised if need be | |
496 | */ | |
497 | #define IO_RINGFD_REG_MAX 16 | |
498 | ||
53e043b2 SM |
499 | struct io_uring_task { |
500 | /* submission side */ | |
09899b19 | 501 | int cached_refs; |
53e043b2 SM |
502 | struct xarray xa; |
503 | struct wait_queue_head wait; | |
ee53fb2b SM |
504 | const struct io_ring_ctx *last; |
505 | struct io_wq *io_wq; | |
53e043b2 SM |
506 | struct percpu_counter inflight; |
507 | atomic_t in_idle; | |
53e043b2 SM |
508 | |
509 | spinlock_t task_lock; | |
510 | struct io_wq_work_list task_list; | |
4813c377 | 511 | struct io_wq_work_list prior_task_list; |
53e043b2 | 512 | struct callback_head task_work; |
e7a6c00d | 513 | struct file **registered_rings; |
6294f368 | 514 | bool task_running; |
53e043b2 SM |
515 | }; |
516 | ||
09bb8394 JA |
517 | /* |
518 | * First field must be the file pointer in all the | |
519 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
520 | */ | |
221c5eb2 JA |
521 | struct io_poll_iocb { |
522 | struct file *file; | |
018043be | 523 | struct wait_queue_head *head; |
221c5eb2 | 524 | __poll_t events; |
392edb45 | 525 | struct wait_queue_entry wait; |
221c5eb2 JA |
526 | }; |
527 | ||
9d805892 | 528 | struct io_poll_update { |
018043be | 529 | struct file *file; |
9d805892 PB |
530 | u64 old_user_data; |
531 | u64 new_user_data; | |
532 | __poll_t events; | |
b69de288 JA |
533 | bool update_events; |
534 | bool update_user_data; | |
018043be PB |
535 | }; |
536 | ||
b5dba59e JA |
537 | struct io_close { |
538 | struct file *file; | |
b5dba59e | 539 | int fd; |
7df778be | 540 | u32 file_slot; |
b5dba59e JA |
541 | }; |
542 | ||
ad8a48ac JA |
543 | struct io_timeout_data { |
544 | struct io_kiocb *req; | |
545 | struct hrtimer timer; | |
546 | struct timespec64 ts; | |
547 | enum hrtimer_mode mode; | |
50c1df2b | 548 | u32 flags; |
ad8a48ac JA |
549 | }; |
550 | ||
8ed8d3c3 JA |
551 | struct io_accept { |
552 | struct file *file; | |
553 | struct sockaddr __user *addr; | |
554 | int __user *addr_len; | |
555 | int flags; | |
aaa4db12 | 556 | u32 file_slot; |
09952e3e | 557 | unsigned long nofile; |
8ed8d3c3 JA |
558 | }; |
559 | ||
560 | struct io_sync { | |
561 | struct file *file; | |
562 | loff_t len; | |
563 | loff_t off; | |
564 | int flags; | |
d63d1b5e | 565 | int mode; |
8ed8d3c3 JA |
566 | }; |
567 | ||
fbf23849 JA |
568 | struct io_cancel { |
569 | struct file *file; | |
570 | u64 addr; | |
571 | }; | |
572 | ||
b29472ee JA |
573 | struct io_timeout { |
574 | struct file *file; | |
bfe68a22 PB |
575 | u32 off; |
576 | u32 target_seq; | |
135fcde8 | 577 | struct list_head list; |
90cd7e42 PB |
578 | /* head of the link, used by linked timeouts only */ |
579 | struct io_kiocb *head; | |
89b263f6 JA |
580 | /* for linked completions */ |
581 | struct io_kiocb *prev; | |
b29472ee JA |
582 | }; |
583 | ||
0bdf7a2d PB |
584 | struct io_timeout_rem { |
585 | struct file *file; | |
586 | u64 addr; | |
9c8e11b3 PB |
587 | |
588 | /* timeout update */ | |
589 | struct timespec64 ts; | |
590 | u32 flags; | |
f1042b6c | 591 | bool ltimeout; |
0bdf7a2d PB |
592 | }; |
593 | ||
9adbd45d JA |
594 | struct io_rw { |
595 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
596 | struct kiocb kiocb; | |
597 | u64 addr; | |
584b0180 JA |
598 | u32 len; |
599 | u32 flags; | |
9adbd45d JA |
600 | }; |
601 | ||
3fbb51c1 JA |
602 | struct io_connect { |
603 | struct file *file; | |
604 | struct sockaddr __user *addr; | |
605 | int addr_len; | |
606 | }; | |
607 | ||
e47293fd JA |
608 | struct io_sr_msg { |
609 | struct file *file; | |
fddaface | 610 | union { |
4af3417a PB |
611 | struct compat_msghdr __user *umsg_compat; |
612 | struct user_msghdr __user *umsg; | |
613 | void __user *buf; | |
fddaface | 614 | }; |
e47293fd | 615 | int msg_flags; |
bcda7baa | 616 | int bgid; |
fddaface | 617 | size_t len; |
7ba89d2a | 618 | size_t done_io; |
e47293fd JA |
619 | }; |
620 | ||
15b71abe JA |
621 | struct io_open { |
622 | struct file *file; | |
623 | int dfd; | |
b9445598 | 624 | u32 file_slot; |
15b71abe | 625 | struct filename *filename; |
c12cedf2 | 626 | struct open_how how; |
4022e7af | 627 | unsigned long nofile; |
15b71abe JA |
628 | }; |
629 | ||
269bbe5f | 630 | struct io_rsrc_update { |
05f3fb3c JA |
631 | struct file *file; |
632 | u64 arg; | |
633 | u32 nr_args; | |
634 | u32 offset; | |
635 | }; | |
636 | ||
4840e418 JA |
637 | struct io_fadvise { |
638 | struct file *file; | |
639 | u64 offset; | |
640 | u32 len; | |
641 | u32 advice; | |
642 | }; | |
643 | ||
c1ca757b JA |
644 | struct io_madvise { |
645 | struct file *file; | |
646 | u64 addr; | |
647 | u32 len; | |
648 | u32 advice; | |
649 | }; | |
650 | ||
3e4827b0 JA |
651 | struct io_epoll { |
652 | struct file *file; | |
653 | int epfd; | |
654 | int op; | |
655 | int fd; | |
656 | struct epoll_event event; | |
e47293fd JA |
657 | }; |
658 | ||
7d67af2c PB |
659 | struct io_splice { |
660 | struct file *file_out; | |
7d67af2c PB |
661 | loff_t off_out; |
662 | loff_t off_in; | |
663 | u64 len; | |
a3e4bc23 | 664 | int splice_fd_in; |
7d67af2c PB |
665 | unsigned int flags; |
666 | }; | |
667 | ||
ddf0322d JA |
668 | struct io_provide_buf { |
669 | struct file *file; | |
670 | __u64 addr; | |
38134ada | 671 | __u32 len; |
ddf0322d JA |
672 | __u32 bgid; |
673 | __u16 nbufs; | |
674 | __u16 bid; | |
675 | }; | |
676 | ||
1d9e1288 BM |
677 | struct io_statx { |
678 | struct file *file; | |
679 | int dfd; | |
680 | unsigned int mask; | |
681 | unsigned int flags; | |
1b6fe6e0 | 682 | struct filename *filename; |
1d9e1288 BM |
683 | struct statx __user *buffer; |
684 | }; | |
685 | ||
36f4fa68 JA |
686 | struct io_shutdown { |
687 | struct file *file; | |
688 | int how; | |
689 | }; | |
690 | ||
80a261fd JA |
691 | struct io_rename { |
692 | struct file *file; | |
693 | int old_dfd; | |
694 | int new_dfd; | |
695 | struct filename *oldpath; | |
696 | struct filename *newpath; | |
697 | int flags; | |
698 | }; | |
699 | ||
14a1143b JA |
700 | struct io_unlink { |
701 | struct file *file; | |
702 | int dfd; | |
703 | int flags; | |
704 | struct filename *filename; | |
705 | }; | |
706 | ||
e34a02dc DK |
707 | struct io_mkdir { |
708 | struct file *file; | |
709 | int dfd; | |
710 | umode_t mode; | |
711 | struct filename *filename; | |
712 | }; | |
713 | ||
7a8721f8 DK |
714 | struct io_symlink { |
715 | struct file *file; | |
716 | int new_dfd; | |
717 | struct filename *oldpath; | |
718 | struct filename *newpath; | |
719 | }; | |
720 | ||
cf30da90 DK |
721 | struct io_hardlink { |
722 | struct file *file; | |
723 | int old_dfd; | |
724 | int new_dfd; | |
725 | struct filename *oldpath; | |
726 | struct filename *newpath; | |
727 | int flags; | |
728 | }; | |
729 | ||
4f57f06c JA |
730 | struct io_msg { |
731 | struct file *file; | |
732 | u64 user_data; | |
733 | u32 len; | |
734 | }; | |
735 | ||
f499a021 JA |
736 | struct io_async_connect { |
737 | struct sockaddr_storage address; | |
738 | }; | |
739 | ||
03b1230c JA |
740 | struct io_async_msghdr { |
741 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
742 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
743 | struct iovec *free_iov; | |
03b1230c JA |
744 | struct sockaddr __user *uaddr; |
745 | struct msghdr msg; | |
b537916c | 746 | struct sockaddr_storage addr; |
03b1230c JA |
747 | }; |
748 | ||
538941e2 | 749 | struct io_rw_state { |
ff6165b2 | 750 | struct iov_iter iter; |
cd658695 | 751 | struct iov_iter_state iter_state; |
c88598a9 | 752 | struct iovec fast_iov[UIO_FASTIOV]; |
538941e2 PB |
753 | }; |
754 | ||
755 | struct io_async_rw { | |
756 | struct io_rw_state s; | |
757 | const struct iovec *free_iovec; | |
227c0c96 | 758 | size_t bytes_done; |
bcf5a063 | 759 | struct wait_page_queue wpq; |
f67676d1 JA |
760 | }; |
761 | ||
6b47ee6e PB |
762 | enum { |
763 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
764 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
765 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
766 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
767 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 768 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
04c76b41 | 769 | REQ_F_CQE_SKIP_BIT = IOSQE_CQE_SKIP_SUCCESS_BIT, |
6b47ee6e | 770 | |
dddca226 | 771 | /* first byte is taken by user flags, shift it to not overlap */ |
93d2bcd2 | 772 | REQ_F_FAIL_BIT = 8, |
6b47ee6e PB |
773 | REQ_F_INFLIGHT_BIT, |
774 | REQ_F_CUR_POS_BIT, | |
775 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 776 | REQ_F_LINK_TIMEOUT_BIT, |
99bc4c38 | 777 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 778 | REQ_F_POLLED_BIT, |
bcda7baa | 779 | REQ_F_BUFFER_SELECTED_BIT, |
e342c807 | 780 | REQ_F_COMPLETE_INLINE_BIT, |
230d50d4 | 781 | REQ_F_REISSUE_BIT, |
b8e64b53 | 782 | REQ_F_CREDS_BIT, |
20e60a38 | 783 | REQ_F_REFCOUNT_BIT, |
4d13d1a4 | 784 | REQ_F_ARM_LTIMEOUT_BIT, |
d886e185 | 785 | REQ_F_ASYNC_DATA_BIT, |
04c76b41 | 786 | REQ_F_SKIP_LINK_CQES_BIT, |
91eac1c6 JA |
787 | REQ_F_SINGLE_POLL_BIT, |
788 | REQ_F_DOUBLE_POLL_BIT, | |
8a3e8ee5 | 789 | REQ_F_PARTIAL_IO_BIT, |
7b29f92d | 790 | /* keep async read/write and isreg together and in order */ |
35645ac3 | 791 | REQ_F_SUPPORT_NOWAIT_BIT, |
7b29f92d | 792 | REQ_F_ISREG_BIT, |
84557871 JA |
793 | |
794 | /* not a real bit, just to check we're not overflowing the space */ | |
795 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
796 | }; |
797 | ||
798 | enum { | |
799 | /* ctx owns file */ | |
800 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
801 | /* drain existing IO first */ | |
802 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
803 | /* linked sqes */ | |
804 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
805 | /* doesn't sever on completion < 0 */ | |
806 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
807 | /* IOSQE_ASYNC */ | |
808 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
809 | /* IOSQE_BUFFER_SELECT */ |
810 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
04c76b41 PB |
811 | /* IOSQE_CQE_SKIP_SUCCESS */ |
812 | REQ_F_CQE_SKIP = BIT(REQ_F_CQE_SKIP_BIT), | |
6b47ee6e | 813 | |
6b47ee6e | 814 | /* fail rest of links */ |
93d2bcd2 | 815 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
b05a1bcd | 816 | /* on inflight list, should be cancelled and waited on exit reliably */ |
6b47ee6e PB |
817 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
818 | /* read/write uses file position */ | |
819 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
820 | /* must not punt to workers */ | |
821 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 822 | /* has or had linked timeout */ |
6b47ee6e | 823 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
99bc4c38 PB |
824 | /* needs cleanup */ |
825 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
826 | /* already went through poll handler */ |
827 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
828 | /* buffer already selected */ |
829 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
e342c807 PB |
830 | /* completion is deferred through io_comp_state */ |
831 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
230d50d4 JA |
832 | /* caller should reissue async */ |
833 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), | |
35645ac3 PB |
834 | /* supports async reads/writes */ |
835 | REQ_F_SUPPORT_NOWAIT = BIT(REQ_F_SUPPORT_NOWAIT_BIT), | |
7b29f92d JA |
836 | /* regular file */ |
837 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
b8e64b53 PB |
838 | /* has creds assigned */ |
839 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), | |
20e60a38 PB |
840 | /* skip refcounting if not set */ |
841 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), | |
4d13d1a4 PB |
842 | /* there is a linked timeout that has to be armed */ |
843 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT), | |
d886e185 PB |
844 | /* ->async_data allocated */ |
845 | REQ_F_ASYNC_DATA = BIT(REQ_F_ASYNC_DATA_BIT), | |
04c76b41 PB |
846 | /* don't post CQEs while failing linked requests */ |
847 | REQ_F_SKIP_LINK_CQES = BIT(REQ_F_SKIP_LINK_CQES_BIT), | |
91eac1c6 JA |
848 | /* single poll may be active */ |
849 | REQ_F_SINGLE_POLL = BIT(REQ_F_SINGLE_POLL_BIT), | |
850 | /* double poll may active */ | |
851 | REQ_F_DOUBLE_POLL = BIT(REQ_F_DOUBLE_POLL_BIT), | |
8a3e8ee5 JA |
852 | /* request has already done partial IO */ |
853 | REQ_F_PARTIAL_IO = BIT(REQ_F_PARTIAL_IO_BIT), | |
d7718a9d JA |
854 | }; |
855 | ||
856 | struct async_poll { | |
857 | struct io_poll_iocb poll; | |
807abcb0 | 858 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
859 | }; |
860 | ||
f237c30a | 861 | typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked); |
5b0a6acc | 862 | |
7cbf1722 | 863 | struct io_task_work { |
5b0a6acc PB |
864 | union { |
865 | struct io_wq_work_node node; | |
866 | struct llist_node fallback_node; | |
867 | }; | |
868 | io_req_tw_func_t func; | |
7cbf1722 JA |
869 | }; |
870 | ||
992da01a PB |
871 | enum { |
872 | IORING_RSRC_FILE = 0, | |
873 | IORING_RSRC_BUFFER = 1, | |
874 | }; | |
875 | ||
cef216fc PB |
876 | struct io_cqe { |
877 | __u64 user_data; | |
878 | __s32 res; | |
879 | /* fd initially, then cflags for completion */ | |
880 | union { | |
881 | __u32 flags; | |
882 | int fd; | |
883 | }; | |
884 | }; | |
885 | ||
09bb8394 JA |
886 | /* |
887 | * NOTE! Each of the iocb union members has the file pointer | |
888 | * as the first entry in their struct definition. So you can | |
889 | * access the file pointer through any of the sub-structs, | |
63c36549 | 890 | * or directly as just 'file' in this struct. |
09bb8394 | 891 | */ |
2b188cc1 | 892 | struct io_kiocb { |
221c5eb2 | 893 | union { |
09bb8394 | 894 | struct file *file; |
9adbd45d | 895 | struct io_rw rw; |
221c5eb2 | 896 | struct io_poll_iocb poll; |
9d805892 | 897 | struct io_poll_update poll_update; |
8ed8d3c3 JA |
898 | struct io_accept accept; |
899 | struct io_sync sync; | |
fbf23849 | 900 | struct io_cancel cancel; |
b29472ee | 901 | struct io_timeout timeout; |
0bdf7a2d | 902 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 903 | struct io_connect connect; |
e47293fd | 904 | struct io_sr_msg sr_msg; |
15b71abe | 905 | struct io_open open; |
b5dba59e | 906 | struct io_close close; |
269bbe5f | 907 | struct io_rsrc_update rsrc_update; |
4840e418 | 908 | struct io_fadvise fadvise; |
c1ca757b | 909 | struct io_madvise madvise; |
3e4827b0 | 910 | struct io_epoll epoll; |
7d67af2c | 911 | struct io_splice splice; |
ddf0322d | 912 | struct io_provide_buf pbuf; |
1d9e1288 | 913 | struct io_statx statx; |
36f4fa68 | 914 | struct io_shutdown shutdown; |
80a261fd | 915 | struct io_rename rename; |
14a1143b | 916 | struct io_unlink unlink; |
e34a02dc | 917 | struct io_mkdir mkdir; |
7a8721f8 | 918 | struct io_symlink symlink; |
cf30da90 | 919 | struct io_hardlink hardlink; |
4f57f06c | 920 | struct io_msg msg; |
221c5eb2 | 921 | }; |
2b188cc1 | 922 | |
d625c6ee | 923 | u8 opcode; |
65a6543d XW |
924 | /* polled IO has completed */ |
925 | u8 iopoll_completed; | |
4f4eeba8 | 926 | u16 buf_index; |
d17e56eb PB |
927 | unsigned int flags; |
928 | ||
cef216fc | 929 | struct io_cqe cqe; |
4f4eeba8 | 930 | |
010e8e6b | 931 | struct io_ring_ctx *ctx; |
010e8e6b | 932 | struct task_struct *task; |
d7718a9d | 933 | |
269bbe5f | 934 | struct percpu_ref *fixed_rsrc_refs; |
d886e185 PB |
935 | /* store used ubuf, so we can prevent reloading */ |
936 | struct io_mapped_ubuf *imu; | |
fcb323cc | 937 | |
2804ecd8 JA |
938 | union { |
939 | /* used by request caches, completion batching and iopoll */ | |
940 | struct io_wq_work_node comp_list; | |
941 | /* cache ->apoll->events */ | |
942 | int apoll_events; | |
943 | }; | |
d17e56eb | 944 | atomic_t refs; |
521d61fc | 945 | atomic_t poll_refs; |
5b0a6acc | 946 | struct io_task_work io_task_work; |
010e8e6b PB |
947 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
948 | struct hlist_node hash_node; | |
7e3709d5 | 949 | /* internal polling, see IORING_FEAT_FAST_POLL */ |
010e8e6b | 950 | struct async_poll *apoll; |
d886e185 PB |
951 | /* opcode allocated if it needs to store data for async defer */ |
952 | void *async_data; | |
7e3709d5 | 953 | /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */ |
30d51dd4 | 954 | struct io_buffer *kbuf; |
41cdcc22 | 955 | /* linked requests, IFF REQ_F_HARDLINK or REQ_F_LINK are set */ |
34d2bfe7 | 956 | struct io_kiocb *link; |
41cdcc22 | 957 | /* custom credentials, valid IFF REQ_F_CREDS is set */ |
521d61fc JA |
958 | const struct cred *creds; |
959 | struct io_wq_work work; | |
2b188cc1 | 960 | }; |
05589553 | 961 | |
13bf43f5 PB |
962 | struct io_tctx_node { |
963 | struct list_head ctx_node; | |
964 | struct task_struct *task; | |
13bf43f5 PB |
965 | struct io_ring_ctx *ctx; |
966 | }; | |
967 | ||
27dc8338 PB |
968 | struct io_defer_entry { |
969 | struct list_head list; | |
970 | struct io_kiocb *req; | |
9cf7c104 | 971 | u32 seq; |
2b188cc1 JA |
972 | }; |
973 | ||
d3656344 | 974 | struct io_op_def { |
d3656344 JA |
975 | /* needs req->file assigned */ |
976 | unsigned needs_file : 1; | |
6d63416d PB |
977 | /* should block plug */ |
978 | unsigned plug : 1; | |
d3656344 JA |
979 | /* hash wq insertion if file is a regular file */ |
980 | unsigned hash_reg_file : 1; | |
981 | /* unbound wq insertion if file is a non-regular file */ | |
982 | unsigned unbound_nonreg_file : 1; | |
8a72758c JA |
983 | /* set if opcode supports polled "wait" */ |
984 | unsigned pollin : 1; | |
985 | unsigned pollout : 1; | |
52dd8640 | 986 | unsigned poll_exclusive : 1; |
bcda7baa JA |
987 | /* op supports buffer selection */ |
988 | unsigned buffer_select : 1; | |
26f0505a PB |
989 | /* do prep async if is going to be punted */ |
990 | unsigned needs_async_setup : 1; | |
6d63416d PB |
991 | /* opcode is not supported by this kernel */ |
992 | unsigned not_supported : 1; | |
5bd2182d PM |
993 | /* skip auditing */ |
994 | unsigned audit_skip : 1; | |
e8c2bc1f JA |
995 | /* size of async data needed, if any */ |
996 | unsigned short async_size; | |
d3656344 JA |
997 | }; |
998 | ||
0918682b | 999 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
1000 | [IORING_OP_NOP] = {}, |
1001 | [IORING_OP_READV] = { | |
d3656344 JA |
1002 | .needs_file = 1, |
1003 | .unbound_nonreg_file = 1, | |
8a72758c | 1004 | .pollin = 1, |
4d954c25 | 1005 | .buffer_select = 1, |
26f0505a | 1006 | .needs_async_setup = 1, |
27926b68 | 1007 | .plug = 1, |
5bd2182d | 1008 | .audit_skip = 1, |
e8c2bc1f | 1009 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 1010 | }, |
0463b6c5 | 1011 | [IORING_OP_WRITEV] = { |
d3656344 JA |
1012 | .needs_file = 1, |
1013 | .hash_reg_file = 1, | |
1014 | .unbound_nonreg_file = 1, | |
8a72758c | 1015 | .pollout = 1, |
26f0505a | 1016 | .needs_async_setup = 1, |
27926b68 | 1017 | .plug = 1, |
5bd2182d | 1018 | .audit_skip = 1, |
e8c2bc1f | 1019 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 1020 | }, |
0463b6c5 | 1021 | [IORING_OP_FSYNC] = { |
d3656344 | 1022 | .needs_file = 1, |
5bd2182d | 1023 | .audit_skip = 1, |
d3656344 | 1024 | }, |
0463b6c5 | 1025 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
1026 | .needs_file = 1, |
1027 | .unbound_nonreg_file = 1, | |
8a72758c | 1028 | .pollin = 1, |
27926b68 | 1029 | .plug = 1, |
5bd2182d | 1030 | .audit_skip = 1, |
e8c2bc1f | 1031 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 1032 | }, |
0463b6c5 | 1033 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
1034 | .needs_file = 1, |
1035 | .hash_reg_file = 1, | |
1036 | .unbound_nonreg_file = 1, | |
8a72758c | 1037 | .pollout = 1, |
27926b68 | 1038 | .plug = 1, |
5bd2182d | 1039 | .audit_skip = 1, |
e8c2bc1f | 1040 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 1041 | }, |
0463b6c5 | 1042 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
1043 | .needs_file = 1, |
1044 | .unbound_nonreg_file = 1, | |
5bd2182d PM |
1045 | .audit_skip = 1, |
1046 | }, | |
1047 | [IORING_OP_POLL_REMOVE] = { | |
1048 | .audit_skip = 1, | |
d3656344 | 1049 | }, |
0463b6c5 | 1050 | [IORING_OP_SYNC_FILE_RANGE] = { |
d3656344 | 1051 | .needs_file = 1, |
5bd2182d | 1052 | .audit_skip = 1, |
d3656344 | 1053 | }, |
0463b6c5 | 1054 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
1055 | .needs_file = 1, |
1056 | .unbound_nonreg_file = 1, | |
8a72758c | 1057 | .pollout = 1, |
26f0505a | 1058 | .needs_async_setup = 1, |
e8c2bc1f | 1059 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 1060 | }, |
0463b6c5 | 1061 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
1062 | .needs_file = 1, |
1063 | .unbound_nonreg_file = 1, | |
8a72758c | 1064 | .pollin = 1, |
52de1fe1 | 1065 | .buffer_select = 1, |
26f0505a | 1066 | .needs_async_setup = 1, |
e8c2bc1f | 1067 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 1068 | }, |
0463b6c5 | 1069 | [IORING_OP_TIMEOUT] = { |
5bd2182d | 1070 | .audit_skip = 1, |
e8c2bc1f | 1071 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 1072 | }, |
9c8e11b3 PB |
1073 | [IORING_OP_TIMEOUT_REMOVE] = { |
1074 | /* used by timeout updates' prep() */ | |
5bd2182d | 1075 | .audit_skip = 1, |
9c8e11b3 | 1076 | }, |
0463b6c5 | 1077 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
1078 | .needs_file = 1, |
1079 | .unbound_nonreg_file = 1, | |
8a72758c | 1080 | .pollin = 1, |
52dd8640 | 1081 | .poll_exclusive = 1, |
d3656344 | 1082 | }, |
5bd2182d PM |
1083 | [IORING_OP_ASYNC_CANCEL] = { |
1084 | .audit_skip = 1, | |
1085 | }, | |
0463b6c5 | 1086 | [IORING_OP_LINK_TIMEOUT] = { |
5bd2182d | 1087 | .audit_skip = 1, |
e8c2bc1f | 1088 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 1089 | }, |
0463b6c5 | 1090 | [IORING_OP_CONNECT] = { |
d3656344 JA |
1091 | .needs_file = 1, |
1092 | .unbound_nonreg_file = 1, | |
8a72758c | 1093 | .pollout = 1, |
26f0505a | 1094 | .needs_async_setup = 1, |
e8c2bc1f | 1095 | .async_size = sizeof(struct io_async_connect), |
d3656344 | 1096 | }, |
0463b6c5 | 1097 | [IORING_OP_FALLOCATE] = { |
d3656344 | 1098 | .needs_file = 1, |
d3656344 | 1099 | }, |
44526bed JA |
1100 | [IORING_OP_OPENAT] = {}, |
1101 | [IORING_OP_CLOSE] = {}, | |
5bd2182d PM |
1102 | [IORING_OP_FILES_UPDATE] = { |
1103 | .audit_skip = 1, | |
1104 | }, | |
1105 | [IORING_OP_STATX] = { | |
1106 | .audit_skip = 1, | |
1107 | }, | |
0463b6c5 | 1108 | [IORING_OP_READ] = { |
3a6820f2 JA |
1109 | .needs_file = 1, |
1110 | .unbound_nonreg_file = 1, | |
8a72758c | 1111 | .pollin = 1, |
bcda7baa | 1112 | .buffer_select = 1, |
27926b68 | 1113 | .plug = 1, |
5bd2182d | 1114 | .audit_skip = 1, |
e8c2bc1f | 1115 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1116 | }, |
0463b6c5 | 1117 | [IORING_OP_WRITE] = { |
3a6820f2 | 1118 | .needs_file = 1, |
7b3188e7 | 1119 | .hash_reg_file = 1, |
3a6820f2 | 1120 | .unbound_nonreg_file = 1, |
8a72758c | 1121 | .pollout = 1, |
27926b68 | 1122 | .plug = 1, |
5bd2182d | 1123 | .audit_skip = 1, |
e8c2bc1f | 1124 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1125 | }, |
0463b6c5 | 1126 | [IORING_OP_FADVISE] = { |
4840e418 | 1127 | .needs_file = 1, |
5bd2182d | 1128 | .audit_skip = 1, |
c1ca757b | 1129 | }, |
44526bed | 1130 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 1131 | [IORING_OP_SEND] = { |
fddaface JA |
1132 | .needs_file = 1, |
1133 | .unbound_nonreg_file = 1, | |
8a72758c | 1134 | .pollout = 1, |
5bd2182d | 1135 | .audit_skip = 1, |
fddaface | 1136 | }, |
0463b6c5 | 1137 | [IORING_OP_RECV] = { |
fddaface JA |
1138 | .needs_file = 1, |
1139 | .unbound_nonreg_file = 1, | |
8a72758c | 1140 | .pollin = 1, |
bcda7baa | 1141 | .buffer_select = 1, |
5bd2182d | 1142 | .audit_skip = 1, |
fddaface | 1143 | }, |
0463b6c5 | 1144 | [IORING_OP_OPENAT2] = { |
cebdb986 | 1145 | }, |
3e4827b0 JA |
1146 | [IORING_OP_EPOLL_CTL] = { |
1147 | .unbound_nonreg_file = 1, | |
5bd2182d | 1148 | .audit_skip = 1, |
3e4827b0 | 1149 | }, |
7d67af2c PB |
1150 | [IORING_OP_SPLICE] = { |
1151 | .needs_file = 1, | |
1152 | .hash_reg_file = 1, | |
1153 | .unbound_nonreg_file = 1, | |
5bd2182d PM |
1154 | .audit_skip = 1, |
1155 | }, | |
1156 | [IORING_OP_PROVIDE_BUFFERS] = { | |
1157 | .audit_skip = 1, | |
1158 | }, | |
1159 | [IORING_OP_REMOVE_BUFFERS] = { | |
1160 | .audit_skip = 1, | |
ddf0322d | 1161 | }, |
f2a8d5c7 PB |
1162 | [IORING_OP_TEE] = { |
1163 | .needs_file = 1, | |
1164 | .hash_reg_file = 1, | |
1165 | .unbound_nonreg_file = 1, | |
5bd2182d | 1166 | .audit_skip = 1, |
f2a8d5c7 | 1167 | }, |
36f4fa68 JA |
1168 | [IORING_OP_SHUTDOWN] = { |
1169 | .needs_file = 1, | |
1170 | }, | |
44526bed JA |
1171 | [IORING_OP_RENAMEAT] = {}, |
1172 | [IORING_OP_UNLINKAT] = {}, | |
e34a02dc | 1173 | [IORING_OP_MKDIRAT] = {}, |
7a8721f8 | 1174 | [IORING_OP_SYMLINKAT] = {}, |
cf30da90 | 1175 | [IORING_OP_LINKAT] = {}, |
4f57f06c JA |
1176 | [IORING_OP_MSG_RING] = { |
1177 | .needs_file = 1, | |
1178 | }, | |
d3656344 JA |
1179 | }; |
1180 | ||
0756a869 PB |
1181 | /* requests with any of those set should undergo io_disarm_next() */ |
1182 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) | |
da1a08c5 | 1183 | #define IO_REQ_LINK_FLAGS (REQ_F_LINK | REQ_F_HARDLINK) |
0756a869 | 1184 | |
7a612350 | 1185 | static bool io_disarm_next(struct io_kiocb *req); |
eef51daa | 1186 | static void io_uring_del_tctx_node(unsigned long index); |
9936c7c2 PB |
1187 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1188 | struct task_struct *task, | |
3dd0c97a | 1189 | bool cancel_all); |
78cc687b | 1190 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); |
1ffc5422 | 1191 | |
4e118cd9 | 1192 | static void __io_req_complete_post(struct io_kiocb *req, s32 res, u32 cflags); |
c7dae4ba | 1193 | static void io_dismantle_req(struct io_kiocb *req); |
94ae5e77 | 1194 | static void io_queue_linked_timeout(struct io_kiocb *req); |
fdecb662 | 1195 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 1196 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 | 1197 | unsigned nr_args); |
68fb8979 | 1198 | static void io_clean_op(struct io_kiocb *req); |
5106dd6e JA |
1199 | static inline struct file *io_file_get_fixed(struct io_kiocb *req, int fd, |
1200 | unsigned issue_flags); | |
1201 | static inline struct file *io_file_get_normal(struct io_kiocb *req, int fd); | |
d5361233 JA |
1202 | static void io_drop_inflight_file(struct io_kiocb *req); |
1203 | static bool io_assign_file(struct io_kiocb *req, unsigned int issue_flags); | |
cbc2e203 | 1204 | static void io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1205 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1206 | |
907d1df3 | 1207 | static void io_req_task_queue(struct io_kiocb *req); |
c450178d | 1208 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx); |
179ae0d1 | 1209 | static int io_req_prep_async(struct io_kiocb *req); |
de0617e4 | 1210 | |
b9445598 PB |
1211 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
1212 | unsigned int issue_flags, u32 slot_index); | |
7df778be PB |
1213 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags); |
1214 | ||
f1042b6c | 1215 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer); |
9aa8dfde | 1216 | static void io_eventfd_signal(struct io_ring_ctx *ctx); |
4e118cd9 | 1217 | static void io_req_tw_post_queue(struct io_kiocb *req, s32 res, u32 cflags); |
b9445598 | 1218 | |
2b188cc1 JA |
1219 | static struct kmem_cache *req_cachep; |
1220 | ||
0918682b | 1221 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1222 | |
1223 | struct sock *io_uring_get_socket(struct file *file) | |
1224 | { | |
1225 | #if defined(CONFIG_UNIX) | |
1226 | if (file->f_op == &io_uring_fops) { | |
1227 | struct io_ring_ctx *ctx = file->private_data; | |
1228 | ||
1229 | return ctx->ring_sock->sk; | |
1230 | } | |
1231 | #endif | |
1232 | return NULL; | |
1233 | } | |
1234 | EXPORT_SYMBOL(io_uring_get_socket); | |
1235 | ||
1f59bc0f PB |
1236 | #if defined(CONFIG_UNIX) |
1237 | static inline bool io_file_need_scm(struct file *filp) | |
1238 | { | |
1239 | return !!unix_get_socket(filp); | |
1240 | } | |
1241 | #else | |
1242 | static inline bool io_file_need_scm(struct file *filp) | |
1243 | { | |
1244 | return 0; | |
1245 | } | |
1246 | #endif | |
1247 | ||
f8929630 PB |
1248 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, unsigned issue_flags) |
1249 | { | |
1250 | lockdep_assert_held(&ctx->uring_lock); | |
1251 | if (issue_flags & IO_URING_F_UNLOCKED) | |
1252 | mutex_unlock(&ctx->uring_lock); | |
1253 | } | |
1254 | ||
1255 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, unsigned issue_flags) | |
1256 | { | |
1257 | /* | |
1258 | * "Normal" inline submissions always hold the uring_lock, since we | |
1259 | * grab it from the system call. Same is true for the SQPOLL offload. | |
1260 | * The only exception is when we've detached the request and issue it | |
1261 | * from an async worker thread, grab the lock for that case. | |
1262 | */ | |
1263 | if (issue_flags & IO_URING_F_UNLOCKED) | |
1264 | mutex_lock(&ctx->uring_lock); | |
1265 | lockdep_assert_held(&ctx->uring_lock); | |
1266 | } | |
1267 | ||
f237c30a PB |
1268 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
1269 | { | |
1270 | if (!*locked) { | |
1271 | mutex_lock(&ctx->uring_lock); | |
1272 | *locked = true; | |
1273 | } | |
1274 | } | |
1275 | ||
f2f87370 PB |
1276 | #define io_for_each_link(pos, head) \ |
1277 | for (pos = (head); pos; pos = pos->link) | |
1278 | ||
21c843d5 PB |
1279 | /* |
1280 | * Shamelessly stolen from the mm implementation of page reference checking, | |
1281 | * see commit f958d7b528b1 for details. | |
1282 | */ | |
1283 | #define req_ref_zero_or_close_to_overflow(req) \ | |
1284 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) | |
1285 | ||
1286 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) | |
1287 | { | |
20e60a38 | 1288 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1289 | return atomic_inc_not_zero(&req->refs); |
1290 | } | |
1291 | ||
21c843d5 PB |
1292 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
1293 | { | |
20e60a38 PB |
1294 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
1295 | return true; | |
1296 | ||
21c843d5 PB |
1297 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1298 | return atomic_dec_and_test(&req->refs); | |
1299 | } | |
1300 | ||
21c843d5 PB |
1301 | static inline void req_ref_get(struct io_kiocb *req) |
1302 | { | |
20e60a38 | 1303 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1304 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1305 | atomic_inc(&req->refs); | |
1306 | } | |
1307 | ||
c450178d PB |
1308 | static inline void io_submit_flush_completions(struct io_ring_ctx *ctx) |
1309 | { | |
6f33b0bc | 1310 | if (!wq_list_empty(&ctx->submit_state.compl_reqs)) |
c450178d PB |
1311 | __io_submit_flush_completions(ctx); |
1312 | } | |
1313 | ||
48dcd38d | 1314 | static inline void __io_req_set_refcount(struct io_kiocb *req, int nr) |
20e60a38 PB |
1315 | { |
1316 | if (!(req->flags & REQ_F_REFCOUNT)) { | |
1317 | req->flags |= REQ_F_REFCOUNT; | |
48dcd38d | 1318 | atomic_set(&req->refs, nr); |
20e60a38 PB |
1319 | } |
1320 | } | |
1321 | ||
48dcd38d PB |
1322 | static inline void io_req_set_refcount(struct io_kiocb *req) |
1323 | { | |
1324 | __io_req_set_refcount(req, 1); | |
1325 | } | |
1326 | ||
ab409402 PB |
1327 | #define IO_RSRC_REF_BATCH 100 |
1328 | ||
1329 | static inline void io_req_put_rsrc_locked(struct io_kiocb *req, | |
1330 | struct io_ring_ctx *ctx) | |
1331 | __must_hold(&ctx->uring_lock) | |
36f72fe2 | 1332 | { |
ab409402 PB |
1333 | struct percpu_ref *ref = req->fixed_rsrc_refs; |
1334 | ||
1335 | if (ref) { | |
1336 | if (ref == &ctx->rsrc_node->refs) | |
1337 | ctx->rsrc_cached_refs++; | |
1338 | else | |
1339 | percpu_ref_put(ref); | |
1340 | } | |
1341 | } | |
1342 | ||
1343 | static inline void io_req_put_rsrc(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
1344 | { | |
1345 | if (req->fixed_rsrc_refs) | |
1346 | percpu_ref_put(req->fixed_rsrc_refs); | |
1347 | } | |
1348 | ||
1349 | static __cold void io_rsrc_refs_drop(struct io_ring_ctx *ctx) | |
1350 | __must_hold(&ctx->uring_lock) | |
1351 | { | |
1352 | if (ctx->rsrc_cached_refs) { | |
1353 | percpu_ref_put_many(&ctx->rsrc_node->refs, ctx->rsrc_cached_refs); | |
1354 | ctx->rsrc_cached_refs = 0; | |
1355 | } | |
1356 | } | |
1357 | ||
1358 | static void io_rsrc_refs_refill(struct io_ring_ctx *ctx) | |
1359 | __must_hold(&ctx->uring_lock) | |
1360 | { | |
1361 | ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH; | |
1362 | percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH); | |
1363 | } | |
36f72fe2 | 1364 | |
a46be971 | 1365 | static inline void io_req_set_rsrc_node(struct io_kiocb *req, |
5106dd6e JA |
1366 | struct io_ring_ctx *ctx, |
1367 | unsigned int issue_flags) | |
36f72fe2 | 1368 | { |
269bbe5f | 1369 | if (!req->fixed_rsrc_refs) { |
a7f0ed5a | 1370 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
5106dd6e JA |
1371 | |
1372 | if (!(issue_flags & IO_URING_F_UNLOCKED)) { | |
1373 | lockdep_assert_held(&ctx->uring_lock); | |
1374 | ctx->rsrc_cached_refs--; | |
1375 | if (unlikely(ctx->rsrc_cached_refs < 0)) | |
1376 | io_rsrc_refs_refill(ctx); | |
1377 | } else { | |
1378 | percpu_ref_get(req->fixed_rsrc_refs); | |
1379 | } | |
36f72fe2 PB |
1380 | } |
1381 | } | |
1382 | ||
cc3cec83 | 1383 | static unsigned int __io_put_kbuf(struct io_kiocb *req, struct list_head *list) |
3648e526 | 1384 | { |
d1fd1c20 | 1385 | struct io_buffer *kbuf = req->kbuf; |
3648e526 HX |
1386 | unsigned int cflags; |
1387 | ||
cc3cec83 | 1388 | cflags = IORING_CQE_F_BUFFER | (kbuf->bid << IORING_CQE_BUFFER_SHIFT); |
3648e526 | 1389 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
cc3cec83 | 1390 | list_add(&kbuf->list, list); |
d1fd1c20 | 1391 | req->kbuf = NULL; |
3648e526 HX |
1392 | return cflags; |
1393 | } | |
1394 | ||
cc3cec83 | 1395 | static inline unsigned int io_put_kbuf_comp(struct io_kiocb *req) |
3648e526 | 1396 | { |
8197b053 PB |
1397 | lockdep_assert_held(&req->ctx->completion_lock); |
1398 | ||
3648e526 HX |
1399 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
1400 | return 0; | |
cc3cec83 JA |
1401 | return __io_put_kbuf(req, &req->ctx->io_buffers_comp); |
1402 | } | |
1403 | ||
1404 | static inline unsigned int io_put_kbuf(struct io_kiocb *req, | |
1405 | unsigned issue_flags) | |
1406 | { | |
1407 | unsigned int cflags; | |
1408 | ||
1409 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) | |
1410 | return 0; | |
1411 | ||
1412 | /* | |
1413 | * We can add this buffer back to two lists: | |
1414 | * | |
1415 | * 1) The io_buffers_cache list. This one is protected by the | |
1416 | * ctx->uring_lock. If we already hold this lock, add back to this | |
1417 | * list as we can grab it from issue as well. | |
1418 | * 2) The io_buffers_comp list. This one is protected by the | |
1419 | * ctx->completion_lock. | |
1420 | * | |
1421 | * We migrate buffers from the comp_list to the issue cache list | |
1422 | * when we need one. | |
1423 | */ | |
1424 | if (issue_flags & IO_URING_F_UNLOCKED) { | |
1425 | struct io_ring_ctx *ctx = req->ctx; | |
1426 | ||
1427 | spin_lock(&ctx->completion_lock); | |
1428 | cflags = __io_put_kbuf(req, &ctx->io_buffers_comp); | |
1429 | spin_unlock(&ctx->completion_lock); | |
1430 | } else { | |
ab0ac095 PB |
1431 | lockdep_assert_held(&req->ctx->uring_lock); |
1432 | ||
cc3cec83 JA |
1433 | cflags = __io_put_kbuf(req, &req->ctx->io_buffers_cache); |
1434 | } | |
1435 | ||
1436 | return cflags; | |
3648e526 HX |
1437 | } |
1438 | ||
dbc7d452 JA |
1439 | static struct io_buffer_list *io_buffer_get_list(struct io_ring_ctx *ctx, |
1440 | unsigned int bgid) | |
1441 | { | |
1442 | struct list_head *hash_list; | |
1443 | struct io_buffer_list *bl; | |
1444 | ||
1445 | hash_list = &ctx->io_buffers[hash_32(bgid, IO_BUFFERS_HASH_BITS)]; | |
1446 | list_for_each_entry(bl, hash_list, list) | |
1447 | if (bl->bgid == bgid || bgid == -1U) | |
1448 | return bl; | |
1449 | ||
1450 | return NULL; | |
1451 | } | |
1452 | ||
4d55f238 | 1453 | static void io_kbuf_recycle(struct io_kiocb *req, unsigned issue_flags) |
b1c62645 JA |
1454 | { |
1455 | struct io_ring_ctx *ctx = req->ctx; | |
dbc7d452 JA |
1456 | struct io_buffer_list *bl; |
1457 | struct io_buffer *buf; | |
b1c62645 JA |
1458 | |
1459 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) | |
1460 | return; | |
8a3e8ee5 JA |
1461 | /* don't recycle if we already did IO to this buffer */ |
1462 | if (req->flags & REQ_F_PARTIAL_IO) | |
1463 | return; | |
b1c62645 | 1464 | |
f8929630 | 1465 | io_ring_submit_lock(ctx, issue_flags); |
b1c62645 JA |
1466 | |
1467 | buf = req->kbuf; | |
dbc7d452 JA |
1468 | bl = io_buffer_get_list(ctx, buf->bgid); |
1469 | list_add(&buf->list, &bl->buf_list); | |
b1c62645 JA |
1470 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
1471 | req->kbuf = NULL; | |
4d55f238 | 1472 | |
f8929630 | 1473 | io_ring_submit_unlock(ctx, issue_flags); |
b1c62645 JA |
1474 | } |
1475 | ||
3dd0c97a PB |
1476 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
1477 | bool cancel_all) | |
6af3f48b | 1478 | __must_hold(&req->ctx->timeout_lock) |
08d23634 | 1479 | { |
68207680 | 1480 | if (task && head->task != task) |
08d23634 | 1481 | return false; |
d5361233 | 1482 | return cancel_all; |
6af3f48b PB |
1483 | } |
1484 | ||
1485 | /* | |
1486 | * As io_match_task() but protected against racing with linked timeouts. | |
1487 | * User must not hold timeout_lock. | |
1488 | */ | |
1489 | static bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task, | |
1490 | bool cancel_all) | |
1491 | { | |
6af3f48b PB |
1492 | if (task && head->task != task) |
1493 | return false; | |
d5361233 | 1494 | return cancel_all; |
6af3f48b PB |
1495 | } |
1496 | ||
d886e185 PB |
1497 | static inline bool req_has_async_data(struct io_kiocb *req) |
1498 | { | |
1499 | return req->flags & REQ_F_ASYNC_DATA; | |
1500 | } | |
1501 | ||
93d2bcd2 | 1502 | static inline void req_set_fail(struct io_kiocb *req) |
c40f6379 | 1503 | { |
93d2bcd2 | 1504 | req->flags |= REQ_F_FAIL; |
04c76b41 PB |
1505 | if (req->flags & REQ_F_CQE_SKIP) { |
1506 | req->flags &= ~REQ_F_CQE_SKIP; | |
1507 | req->flags |= REQ_F_SKIP_LINK_CQES; | |
1508 | } | |
c40f6379 | 1509 | } |
4a38aed2 | 1510 | |
a8295b98 HX |
1511 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
1512 | { | |
1513 | req_set_fail(req); | |
cef216fc | 1514 | req->cqe.res = res; |
a8295b98 HX |
1515 | } |
1516 | ||
fa05457a PB |
1517 | static inline void io_req_add_to_cache(struct io_kiocb *req, struct io_ring_ctx *ctx) |
1518 | { | |
1519 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); | |
1520 | } | |
1521 | ||
c072481d | 1522 | static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref) |
2b188cc1 JA |
1523 | { |
1524 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1525 | ||
0f158b4c | 1526 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1527 | } |
1528 | ||
8eb7e2d0 PB |
1529 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1530 | { | |
1531 | return !req->timeout.off; | |
1532 | } | |
1533 | ||
c072481d | 1534 | static __cold void io_fallback_req_func(struct work_struct *work) |
f56165e6 PB |
1535 | { |
1536 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, | |
1537 | fallback_work.work); | |
1538 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); | |
1539 | struct io_kiocb *req, *tmp; | |
f237c30a | 1540 | bool locked = false; |
f56165e6 PB |
1541 | |
1542 | percpu_ref_get(&ctx->refs); | |
1543 | llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node) | |
f237c30a | 1544 | req->io_task_work.func(req, &locked); |
5636c00d | 1545 | |
f237c30a | 1546 | if (locked) { |
c450178d | 1547 | io_submit_flush_completions(ctx); |
f237c30a PB |
1548 | mutex_unlock(&ctx->uring_lock); |
1549 | } | |
f56165e6 PB |
1550 | percpu_ref_put(&ctx->refs); |
1551 | } | |
1552 | ||
c072481d | 1553 | static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
2b188cc1 JA |
1554 | { |
1555 | struct io_ring_ctx *ctx; | |
dbc7d452 | 1556 | int i, hash_bits; |
2b188cc1 JA |
1557 | |
1558 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1559 | if (!ctx) | |
1560 | return NULL; | |
1561 | ||
78076bb6 JA |
1562 | /* |
1563 | * Use 5 bits less than the max cq entries, that should give us around | |
1564 | * 32 entries per hash list if totally full and uniformly spread. | |
1565 | */ | |
1566 | hash_bits = ilog2(p->cq_entries); | |
1567 | hash_bits -= 5; | |
1568 | if (hash_bits <= 0) | |
1569 | hash_bits = 1; | |
1570 | ctx->cancel_hash_bits = hash_bits; | |
1571 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1572 | GFP_KERNEL); | |
1573 | if (!ctx->cancel_hash) | |
1574 | goto err; | |
1575 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1576 | ||
6224843d PB |
1577 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
1578 | if (!ctx->dummy_ubuf) | |
1579 | goto err; | |
1580 | /* set invalid range, so io_import_fixed() fails meeting it */ | |
1581 | ctx->dummy_ubuf->ubuf = -1UL; | |
1582 | ||
dbc7d452 JA |
1583 | ctx->io_buffers = kcalloc(1U << IO_BUFFERS_HASH_BITS, |
1584 | sizeof(struct list_head), GFP_KERNEL); | |
1585 | if (!ctx->io_buffers) | |
1586 | goto err; | |
1587 | for (i = 0; i < (1U << IO_BUFFERS_HASH_BITS); i++) | |
1588 | INIT_LIST_HEAD(&ctx->io_buffers[i]); | |
1589 | ||
21482896 | 1590 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1591 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1592 | goto err; | |
2b188cc1 JA |
1593 | |
1594 | ctx->flags = p->flags; | |
90554200 | 1595 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1596 | INIT_LIST_HEAD(&ctx->sqd_list); |
1d7bb1d5 | 1597 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
cc3cec83 | 1598 | INIT_LIST_HEAD(&ctx->io_buffers_cache); |
4d9237e3 | 1599 | INIT_LIST_HEAD(&ctx->apoll_cache); |
0f158b4c | 1600 | init_completion(&ctx->ref_comp); |
61cf9370 | 1601 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
2b188cc1 | 1602 | mutex_init(&ctx->uring_lock); |
311997b3 | 1603 | init_waitqueue_head(&ctx->cq_wait); |
2b188cc1 | 1604 | spin_lock_init(&ctx->completion_lock); |
89850fce | 1605 | spin_lock_init(&ctx->timeout_lock); |
5eef4e87 | 1606 | INIT_WQ_LIST(&ctx->iopoll_list); |
cc3cec83 JA |
1607 | INIT_LIST_HEAD(&ctx->io_buffers_pages); |
1608 | INIT_LIST_HEAD(&ctx->io_buffers_comp); | |
de0617e4 | 1609 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1610 | INIT_LIST_HEAD(&ctx->timeout_list); |
ef9dd637 | 1611 | INIT_LIST_HEAD(&ctx->ltimeout_list); |
d67d2263 BM |
1612 | spin_lock_init(&ctx->rsrc_ref_lock); |
1613 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1614 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1615 | init_llist_head(&ctx->rsrc_put_llist); | |
13bf43f5 | 1616 | INIT_LIST_HEAD(&ctx->tctx_list); |
c2b6c6bc PB |
1617 | ctx->submit_state.free_list.next = NULL; |
1618 | INIT_WQ_LIST(&ctx->locked_free_list); | |
9011bf9a | 1619 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
6f33b0bc | 1620 | INIT_WQ_LIST(&ctx->submit_state.compl_reqs); |
2b188cc1 | 1621 | return ctx; |
206aefde | 1622 | err: |
6224843d | 1623 | kfree(ctx->dummy_ubuf); |
78076bb6 | 1624 | kfree(ctx->cancel_hash); |
dbc7d452 | 1625 | kfree(ctx->io_buffers); |
206aefde JA |
1626 | kfree(ctx); |
1627 | return NULL; | |
2b188cc1 JA |
1628 | } |
1629 | ||
8f6ed49a PB |
1630 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
1631 | { | |
1632 | struct io_rings *r = ctx->rings; | |
1633 | ||
1634 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); | |
1635 | ctx->cq_extra--; | |
1636 | } | |
1637 | ||
9cf7c104 | 1638 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1639 | { |
2bc9930e JA |
1640 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1641 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1642 | |
8f6ed49a | 1643 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
2bc9930e | 1644 | } |
de0617e4 | 1645 | |
9d858b21 | 1646 | return false; |
de0617e4 JA |
1647 | } |
1648 | ||
35645ac3 PB |
1649 | #define FFS_NOWAIT 0x1UL |
1650 | #define FFS_ISREG 0x2UL | |
1651 | #define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG) | |
c97d8a0f PB |
1652 | |
1653 | static inline bool io_req_ffs_set(struct io_kiocb *req) | |
1654 | { | |
35645ac3 | 1655 | return req->flags & REQ_F_FIXED_FILE; |
c97d8a0f PB |
1656 | } |
1657 | ||
fd08e530 PB |
1658 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
1659 | { | |
906c6caa PB |
1660 | if (WARN_ON_ONCE(!req->link)) |
1661 | return NULL; | |
1662 | ||
4d13d1a4 PB |
1663 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
1664 | req->flags |= REQ_F_LINK_TIMEOUT; | |
fd08e530 PB |
1665 | |
1666 | /* linked timeouts should have two refs once prep'ed */ | |
48dcd38d | 1667 | io_req_set_refcount(req); |
4d13d1a4 PB |
1668 | __io_req_set_refcount(req->link, 2); |
1669 | return req->link; | |
fd08e530 PB |
1670 | } |
1671 | ||
1672 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) | |
1673 | { | |
4d13d1a4 | 1674 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
fd08e530 PB |
1675 | return NULL; |
1676 | return __io_prep_linked_timeout(req); | |
1677 | } | |
1678 | ||
cb2d344c PB |
1679 | static noinline void __io_arm_ltimeout(struct io_kiocb *req) |
1680 | { | |
1681 | io_queue_linked_timeout(__io_prep_linked_timeout(req)); | |
1682 | } | |
1683 | ||
1684 | static inline void io_arm_ltimeout(struct io_kiocb *req) | |
1685 | { | |
1686 | if (unlikely(req->flags & REQ_F_ARM_LTIMEOUT)) | |
1687 | __io_arm_ltimeout(req); | |
1688 | } | |
1689 | ||
1e6fa521 JA |
1690 | static void io_prep_async_work(struct io_kiocb *req) |
1691 | { | |
1692 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1693 | struct io_ring_ctx *ctx = req->ctx; |
1694 | ||
b8e64b53 PB |
1695 | if (!(req->flags & REQ_F_CREDS)) { |
1696 | req->flags |= REQ_F_CREDS; | |
c10d1f98 | 1697 | req->creds = get_current_cred(); |
b8e64b53 | 1698 | } |
003e8dcc | 1699 | |
e1d675df PB |
1700 | req->work.list.next = NULL; |
1701 | req->work.flags = 0; | |
feaadc4f PB |
1702 | if (req->flags & REQ_F_FORCE_ASYNC) |
1703 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1704 | ||
1e6fa521 JA |
1705 | if (req->flags & REQ_F_ISREG) { |
1706 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1707 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
4b982bd0 | 1708 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
1e6fa521 JA |
1709 | if (def->unbound_nonreg_file) |
1710 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1711 | } | |
561fb04a | 1712 | } |
cccf0ee8 | 1713 | |
cbdcb435 | 1714 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1715 | { |
cbdcb435 | 1716 | struct io_kiocb *cur; |
54a91f3b | 1717 | |
44eff40a PB |
1718 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
1719 | struct io_ring_ctx *ctx = req->ctx; | |
1720 | ||
674ee8e1 | 1721 | spin_lock_irq(&ctx->timeout_lock); |
44eff40a PB |
1722 | io_for_each_link(cur, req) |
1723 | io_prep_async_work(cur); | |
674ee8e1 | 1724 | spin_unlock_irq(&ctx->timeout_lock); |
44eff40a PB |
1725 | } else { |
1726 | io_for_each_link(cur, req) | |
1727 | io_prep_async_work(cur); | |
1728 | } | |
561fb04a JA |
1729 | } |
1730 | ||
fff4e40e PB |
1731 | static inline void io_req_add_compl_list(struct io_kiocb *req) |
1732 | { | |
775a1f2f | 1733 | struct io_submit_state *state = &req->ctx->submit_state; |
fff4e40e | 1734 | |
3d4aeb9f | 1735 | if (!(req->flags & REQ_F_CQE_SKIP)) |
775a1f2f | 1736 | state->flush_cqes = true; |
fff4e40e PB |
1737 | wq_list_add_tail(&req->comp_list, &state->compl_reqs); |
1738 | } | |
1739 | ||
77955efb | 1740 | static void io_queue_iowq(struct io_kiocb *req, bool *dont_use) |
561fb04a | 1741 | { |
cbdcb435 | 1742 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1743 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1744 | |
3bfe6106 JA |
1745 | BUG_ON(!tctx); |
1746 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1747 | |
cbdcb435 PB |
1748 | /* init ->work of the whole link before punting */ |
1749 | io_prep_async_link(req); | |
991468dc JA |
1750 | |
1751 | /* | |
1752 | * Not expected to happen, but if we do have a bug where this _can_ | |
1753 | * happen, catch it here and ensure the request is marked as | |
1754 | * canceled. That will make io-wq go through the usual work cancel | |
1755 | * procedure rather than attempt to run this request (or create a new | |
1756 | * worker for it). | |
1757 | */ | |
1758 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) | |
1759 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1760 | ||
971cf9c1 PB |
1761 | trace_io_uring_queue_async_work(req->ctx, req, req->cqe.user_data, |
1762 | req->opcode, req->flags, &req->work, | |
1763 | io_wq_is_hashed(&req->work)); | |
ebf93667 | 1764 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a JA |
1765 | if (link) |
1766 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1767 | } |
1768 | ||
1ee4160c | 1769 | static void io_kill_timeout(struct io_kiocb *req, int status) |
8c855885 | 1770 | __must_hold(&req->ctx->completion_lock) |
89850fce | 1771 | __must_hold(&req->ctx->timeout_lock) |
5262f567 | 1772 | { |
e8c2bc1f | 1773 | struct io_timeout_data *io = req->async_data; |
5262f567 | 1774 | |
fd9c7bc5 | 1775 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
2ae2eb9d PB |
1776 | if (status) |
1777 | req_set_fail(req); | |
01cec8c1 PB |
1778 | atomic_set(&req->ctx->cq_timeouts, |
1779 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1780 | list_del_init(&req->timeout.list); |
4e118cd9 | 1781 | io_req_tw_post_queue(req, status, 0); |
5262f567 JA |
1782 | } |
1783 | } | |
1784 | ||
c072481d | 1785 | static __cold void io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1786 | { |
441b8a78 | 1787 | while (!list_empty(&ctx->defer_list)) { |
27dc8338 PB |
1788 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1789 | struct io_defer_entry, list); | |
de0617e4 | 1790 | |
9cf7c104 | 1791 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1792 | break; |
27dc8338 | 1793 | list_del_init(&de->list); |
907d1df3 | 1794 | io_req_task_queue(de->req); |
27dc8338 | 1795 | kfree(de); |
441b8a78 | 1796 | } |
04518945 PB |
1797 | } |
1798 | ||
c072481d | 1799 | static __cold void io_flush_timeouts(struct io_ring_ctx *ctx) |
89850fce | 1800 | __must_hold(&ctx->completion_lock) |
de0617e4 | 1801 | { |
441b8a78 | 1802 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
e677edbc | 1803 | struct io_kiocb *req, *tmp; |
f010505b | 1804 | |
79ebeaee | 1805 | spin_lock_irq(&ctx->timeout_lock); |
e677edbc | 1806 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
f010505b | 1807 | u32 events_needed, events_got; |
de0617e4 | 1808 | |
8eb7e2d0 | 1809 | if (io_is_timeout_noseq(req)) |
360428f8 | 1810 | break; |
f010505b MDG |
1811 | |
1812 | /* | |
1813 | * Since seq can easily wrap around over time, subtract | |
1814 | * the last seq at which timeouts were flushed before comparing. | |
1815 | * Assuming not more than 2^31-1 events have happened since, | |
1816 | * these subtractions won't have wrapped, so we can check if | |
1817 | * target is in [last_seq, current_seq] by comparing the two. | |
1818 | */ | |
1819 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1820 | events_got = seq - ctx->cq_last_tm_flush; | |
1821 | if (events_got < events_needed) | |
360428f8 | 1822 | break; |
bfe68a22 | 1823 | |
1ee4160c | 1824 | io_kill_timeout(req, 0); |
f18ee4cf | 1825 | } |
f010505b | 1826 | ctx->cq_last_tm_flush = seq; |
79ebeaee | 1827 | spin_unlock_irq(&ctx->timeout_lock); |
360428f8 | 1828 | } |
5262f567 | 1829 | |
9333f6b4 PB |
1830 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) |
1831 | { | |
1832 | /* order cqe stores with ring update */ | |
1833 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
1834 | } | |
1835 | ||
9aa8dfde | 1836 | static void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
360428f8 | 1837 | { |
9aa8dfde PB |
1838 | if (ctx->off_timeout_used || ctx->drain_active) { |
1839 | spin_lock(&ctx->completion_lock); | |
1840 | if (ctx->off_timeout_used) | |
1841 | io_flush_timeouts(ctx); | |
1842 | if (ctx->drain_active) | |
1843 | io_queue_deferred(ctx); | |
1844 | io_commit_cqring(ctx); | |
1845 | spin_unlock(&ctx->completion_lock); | |
1846 | } | |
1847 | if (ctx->has_evfd) | |
1848 | io_eventfd_signal(ctx); | |
de0617e4 JA |
1849 | } |
1850 | ||
90554200 JA |
1851 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1852 | { | |
1853 | struct io_rings *r = ctx->rings; | |
1854 | ||
a566c556 | 1855 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries; |
90554200 JA |
1856 | } |
1857 | ||
888aae2e PB |
1858 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1859 | { | |
1860 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1861 | } | |
1862 | ||
d8da428b PB |
1863 | /* |
1864 | * writes to the cq entry need to come after reading head; the | |
1865 | * control dependency is enough as we're using WRITE_ONCE to | |
1866 | * fill the cq entry | |
1867 | */ | |
1868 | static noinline struct io_uring_cqe *__io_get_cqe(struct io_ring_ctx *ctx) | |
2b188cc1 | 1869 | { |
75b28aff | 1870 | struct io_rings *rings = ctx->rings; |
d8da428b PB |
1871 | unsigned int off = ctx->cached_cq_tail & (ctx->cq_entries - 1); |
1872 | unsigned int free, queued, len; | |
1873 | ||
1874 | /* userspace may cheat modifying the tail, be safe and do min */ | |
1875 | queued = min(__io_cqring_events(ctx), ctx->cq_entries); | |
1876 | free = ctx->cq_entries - queued; | |
1877 | /* we need a contiguous range, limit based on the current array offset */ | |
1878 | len = min(free, ctx->cq_entries - off); | |
1879 | if (!len) | |
2b188cc1 JA |
1880 | return NULL; |
1881 | ||
d8da428b PB |
1882 | ctx->cached_cq_tail++; |
1883 | ctx->cqe_cached = &rings->cqes[off]; | |
1884 | ctx->cqe_sentinel = ctx->cqe_cached + len; | |
1885 | return ctx->cqe_cached++; | |
1886 | } | |
1887 | ||
1888 | static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) | |
1889 | { | |
1890 | if (likely(ctx->cqe_cached < ctx->cqe_sentinel)) { | |
1891 | ctx->cached_cq_tail++; | |
1892 | return ctx->cqe_cached++; | |
1893 | } | |
1894 | return __io_get_cqe(ctx); | |
2b188cc1 JA |
1895 | } |
1896 | ||
77bc59b4 | 1897 | static void io_eventfd_signal(struct io_ring_ctx *ctx) |
f2842ab5 | 1898 | { |
77bc59b4 UA |
1899 | struct io_ev_fd *ev_fd; |
1900 | ||
77bc59b4 UA |
1901 | rcu_read_lock(); |
1902 | /* | |
1903 | * rcu_dereference ctx->io_ev_fd once and use it for both for checking | |
1904 | * and eventfd_signal | |
1905 | */ | |
1906 | ev_fd = rcu_dereference(ctx->io_ev_fd); | |
1907 | ||
1908 | /* | |
1909 | * Check again if ev_fd exists incase an io_eventfd_unregister call | |
1910 | * completed between the NULL check of ctx->io_ev_fd at the start of | |
1911 | * the function and rcu_read_lock. | |
1912 | */ | |
1913 | if (unlikely(!ev_fd)) | |
1914 | goto out; | |
7e55a19c | 1915 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
77bc59b4 UA |
1916 | goto out; |
1917 | ||
c75312dd | 1918 | if (!ev_fd->eventfd_async || io_wq_current_is_worker()) |
77bc59b4 | 1919 | eventfd_signal(ev_fd->cq_ev_fd, 1); |
77bc59b4 UA |
1920 | out: |
1921 | rcu_read_unlock(); | |
f2842ab5 JA |
1922 | } |
1923 | ||
9aa8dfde PB |
1924 | static inline void io_cqring_wake(struct io_ring_ctx *ctx) |
1925 | { | |
1926 | /* | |
1927 | * wake_up_all() may seem excessive, but io_wake_function() and | |
1928 | * io_should_wake() handle the termination of the loop and only | |
1929 | * wake as many waiters as we need to. | |
1930 | */ | |
1931 | if (wq_has_sleeper(&ctx->cq_wait)) | |
1932 | wake_up_all(&ctx->cq_wait); | |
1933 | } | |
1934 | ||
2c5d763c JA |
1935 | /* |
1936 | * This should only get called when at least one event has been posted. | |
1937 | * Some applications rely on the eventfd notification count only changing | |
1938 | * IFF a new CQE has been added to the CQ ring. There's no depedency on | |
1939 | * 1:1 relationship between how many times this function is called (and | |
1940 | * hence the eventfd count) and number of CQEs posted to the CQ ring. | |
1941 | */ | |
66fc25ca | 1942 | static inline void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1943 | { |
9aa8dfde PB |
1944 | if (unlikely(ctx->off_timeout_used || ctx->drain_active || |
1945 | ctx->has_evfd)) | |
9333f6b4 PB |
1946 | __io_commit_cqring_flush(ctx); |
1947 | ||
9aa8dfde | 1948 | io_cqring_wake(ctx); |
1d7bb1d5 JA |
1949 | } |
1950 | ||
80c18e4a PB |
1951 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1952 | { | |
9aa8dfde PB |
1953 | if (unlikely(ctx->off_timeout_used || ctx->drain_active || |
1954 | ctx->has_evfd)) | |
9333f6b4 PB |
1955 | __io_commit_cqring_flush(ctx); |
1956 | ||
9aa8dfde PB |
1957 | if (ctx->flags & IORING_SETUP_SQPOLL) |
1958 | io_cqring_wake(ctx); | |
80c18e4a PB |
1959 | } |
1960 | ||
c4a2ed72 | 1961 | /* Returns true if there are no backlogged entries after the flush */ |
6c2450ae | 1962 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
1d7bb1d5 | 1963 | { |
b18032bb | 1964 | bool all_flushed, posted; |
1d7bb1d5 | 1965 | |
a566c556 | 1966 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
e23de15f | 1967 | return false; |
1d7bb1d5 | 1968 | |
b18032bb | 1969 | posted = false; |
79ebeaee | 1970 | spin_lock(&ctx->completion_lock); |
6c2450ae | 1971 | while (!list_empty(&ctx->cq_overflow_list)) { |
d068b506 | 1972 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
6c2450ae | 1973 | struct io_overflow_cqe *ocqe; |
e6c8aa9a | 1974 | |
1d7bb1d5 JA |
1975 | if (!cqe && !force) |
1976 | break; | |
6c2450ae PB |
1977 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
1978 | struct io_overflow_cqe, list); | |
1979 | if (cqe) | |
1980 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); | |
1981 | else | |
8f6ed49a PB |
1982 | io_account_cq_overflow(ctx); |
1983 | ||
b18032bb | 1984 | posted = true; |
6c2450ae PB |
1985 | list_del(&ocqe->list); |
1986 | kfree(ocqe); | |
1d7bb1d5 JA |
1987 | } |
1988 | ||
09e88404 PB |
1989 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1990 | if (all_flushed) { | |
5ed7a37d | 1991 | clear_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1992 | WRITE_ONCE(ctx->rings->sq_flags, |
1993 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); | |
09e88404 | 1994 | } |
46930143 | 1995 | |
60053be8 | 1996 | io_commit_cqring(ctx); |
79ebeaee | 1997 | spin_unlock(&ctx->completion_lock); |
b18032bb JA |
1998 | if (posted) |
1999 | io_cqring_ev_posted(ctx); | |
09e88404 | 2000 | return all_flushed; |
1d7bb1d5 JA |
2001 | } |
2002 | ||
90f67366 | 2003 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
6c503150 | 2004 | { |
ca0a2651 JA |
2005 | bool ret = true; |
2006 | ||
5ed7a37d | 2007 | if (test_bit(0, &ctx->check_cq_overflow)) { |
6c503150 PB |
2008 | /* iopoll syncs against uring_lock, not completion_lock */ |
2009 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
2010 | mutex_lock(&ctx->uring_lock); | |
90f67366 | 2011 | ret = __io_cqring_overflow_flush(ctx, false); |
6c503150 PB |
2012 | if (ctx->flags & IORING_SETUP_IOPOLL) |
2013 | mutex_unlock(&ctx->uring_lock); | |
2014 | } | |
ca0a2651 JA |
2015 | |
2016 | return ret; | |
6c503150 PB |
2017 | } |
2018 | ||
9d170164 | 2019 | static void __io_put_task(struct task_struct *task, int nr) |
6a290a14 PB |
2020 | { |
2021 | struct io_uring_task *tctx = task->io_uring; | |
2022 | ||
9d170164 PB |
2023 | percpu_counter_sub(&tctx->inflight, nr); |
2024 | if (unlikely(atomic_read(&tctx->in_idle))) | |
2025 | wake_up(&tctx->wait); | |
2026 | put_task_struct_many(task, nr); | |
2027 | } | |
2028 | ||
2029 | /* must to be called somewhat shortly after putting a request */ | |
2030 | static inline void io_put_task(struct task_struct *task, int nr) | |
2031 | { | |
2032 | if (likely(task == current)) | |
2033 | task->io_uring->cached_refs += nr; | |
2034 | else | |
2035 | __io_put_task(task, nr); | |
6a290a14 PB |
2036 | } |
2037 | ||
9a10867a PB |
2038 | static void io_task_refs_refill(struct io_uring_task *tctx) |
2039 | { | |
2040 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; | |
2041 | ||
2042 | percpu_counter_add(&tctx->inflight, refill); | |
2043 | refcount_add(refill, ¤t->usage); | |
2044 | tctx->cached_refs += refill; | |
2045 | } | |
2046 | ||
2047 | static inline void io_get_task_refs(int nr) | |
2048 | { | |
2049 | struct io_uring_task *tctx = current->io_uring; | |
2050 | ||
2051 | tctx->cached_refs -= nr; | |
2052 | if (unlikely(tctx->cached_refs < 0)) | |
2053 | io_task_refs_refill(tctx); | |
2054 | } | |
2055 | ||
3cc7fdb9 PB |
2056 | static __cold void io_uring_drop_tctx_refs(struct task_struct *task) |
2057 | { | |
2058 | struct io_uring_task *tctx = task->io_uring; | |
2059 | unsigned int refs = tctx->cached_refs; | |
2060 | ||
2061 | if (refs) { | |
2062 | tctx->cached_refs = 0; | |
2063 | percpu_counter_sub(&tctx->inflight, refs); | |
2064 | put_task_struct_many(task, refs); | |
2065 | } | |
2066 | } | |
2067 | ||
d4d19c19 | 2068 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
54daa9b2 | 2069 | s32 res, u32 cflags) |
2b188cc1 | 2070 | { |
cce4b8b0 | 2071 | struct io_overflow_cqe *ocqe; |
2b188cc1 | 2072 | |
cce4b8b0 PB |
2073 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
2074 | if (!ocqe) { | |
2075 | /* | |
2076 | * If we're in ring overflow flush mode, or in task cancel mode, | |
2077 | * or cannot allocate an overflow entry, then we need to drop it | |
2078 | * on the floor. | |
2079 | */ | |
8f6ed49a | 2080 | io_account_cq_overflow(ctx); |
cce4b8b0 | 2081 | return false; |
2b188cc1 | 2082 | } |
cce4b8b0 | 2083 | if (list_empty(&ctx->cq_overflow_list)) { |
5ed7a37d | 2084 | set_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
2085 | WRITE_ONCE(ctx->rings->sq_flags, |
2086 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); | |
2087 | ||
cce4b8b0 | 2088 | } |
d4d19c19 | 2089 | ocqe->cqe.user_data = user_data; |
cce4b8b0 PB |
2090 | ocqe->cqe.res = res; |
2091 | ocqe->cqe.flags = cflags; | |
2092 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); | |
2093 | return true; | |
2b188cc1 JA |
2094 | } |
2095 | ||
ae4da189 | 2096 | static inline bool __io_fill_cqe(struct io_ring_ctx *ctx, u64 user_data, |
913a571a | 2097 | s32 res, u32 cflags) |
2b188cc1 JA |
2098 | { |
2099 | struct io_uring_cqe *cqe; | |
2100 | ||
2101 | /* | |
2102 | * If we can't get a cq entry, userspace overflowed the | |
2103 | * submission (by quite a lot). Increment the overflow count in | |
2104 | * the ring. | |
2105 | */ | |
d068b506 | 2106 | cqe = io_get_cqe(ctx); |
1d7bb1d5 | 2107 | if (likely(cqe)) { |
d4d19c19 | 2108 | WRITE_ONCE(cqe->user_data, user_data); |
2b188cc1 | 2109 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 2110 | WRITE_ONCE(cqe->flags, cflags); |
8d13326e | 2111 | return true; |
2b188cc1 | 2112 | } |
d4d19c19 | 2113 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
2b188cc1 JA |
2114 | } |
2115 | ||
90e7c35f PB |
2116 | static inline bool __io_fill_cqe_req_filled(struct io_ring_ctx *ctx, |
2117 | struct io_kiocb *req) | |
2118 | { | |
2119 | struct io_uring_cqe *cqe; | |
2120 | ||
2121 | trace_io_uring_complete(req->ctx, req, req->cqe.user_data, | |
2122 | req->cqe.res, req->cqe.flags); | |
2123 | ||
2124 | /* | |
2125 | * If we can't get a cq entry, userspace overflowed the | |
2126 | * submission (by quite a lot). Increment the overflow count in | |
2127 | * the ring. | |
2128 | */ | |
2129 | cqe = io_get_cqe(ctx); | |
2130 | if (likely(cqe)) { | |
2131 | memcpy(cqe, &req->cqe, sizeof(*cqe)); | |
2132 | return true; | |
2133 | } | |
2134 | return io_cqring_event_overflow(ctx, req->cqe.user_data, | |
2135 | req->cqe.res, req->cqe.flags); | |
2136 | } | |
2137 | ||
ae4da189 | 2138 | static inline bool __io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags) |
d5ec1dfa | 2139 | { |
cef216fc PB |
2140 | trace_io_uring_complete(req->ctx, req, req->cqe.user_data, res, cflags); |
2141 | return __io_fill_cqe(req->ctx, req->cqe.user_data, res, cflags); | |
d5ec1dfa SR |
2142 | } |
2143 | ||
913a571a PB |
2144 | static noinline bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data, |
2145 | s32 res, u32 cflags) | |
bcda7baa | 2146 | { |
913a571a | 2147 | ctx->cq_extra++; |
502c87d6 | 2148 | trace_io_uring_complete(ctx, NULL, user_data, res, cflags); |
ae4da189 | 2149 | return __io_fill_cqe(ctx, user_data, res, cflags); |
bcda7baa JA |
2150 | } |
2151 | ||
a37fae8a HX |
2152 | static void __io_req_complete_post(struct io_kiocb *req, s32 res, |
2153 | u32 cflags) | |
2b188cc1 | 2154 | { |
78e19bbe | 2155 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 | 2156 | |
04c76b41 | 2157 | if (!(req->flags & REQ_F_CQE_SKIP)) |
ae4da189 | 2158 | __io_fill_cqe_req(req, res, cflags); |
c7dae4ba JA |
2159 | /* |
2160 | * If we're the last reference to this request, add to our locked | |
2161 | * free_list cache. | |
2162 | */ | |
de9b4cca | 2163 | if (req_ref_put_and_test(req)) { |
da1a08c5 | 2164 | if (req->flags & IO_REQ_LINK_FLAGS) { |
0756a869 | 2165 | if (req->flags & IO_DISARM_MASK) |
7a612350 PB |
2166 | io_disarm_next(req); |
2167 | if (req->link) { | |
2168 | io_req_task_queue(req->link); | |
2169 | req->link = NULL; | |
2170 | } | |
2171 | } | |
ab409402 | 2172 | io_req_put_rsrc(req, ctx); |
8197b053 PB |
2173 | /* |
2174 | * Selected buffer deallocation in io_clean_op() assumes that | |
2175 | * we don't hold ->completion_lock. Clean them here to avoid | |
2176 | * deadlocks. | |
2177 | */ | |
2178 | io_put_kbuf_comp(req); | |
c7dae4ba JA |
2179 | io_dismantle_req(req); |
2180 | io_put_task(req->task, 1); | |
c2b6c6bc | 2181 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
d0acdee2 | 2182 | ctx->locked_free_nr++; |
180f829f | 2183 | } |
a37fae8a HX |
2184 | } |
2185 | ||
2186 | static void io_req_complete_post(struct io_kiocb *req, s32 res, | |
2187 | u32 cflags) | |
2188 | { | |
2189 | struct io_ring_ctx *ctx = req->ctx; | |
2190 | ||
2191 | spin_lock(&ctx->completion_lock); | |
2192 | __io_req_complete_post(req, res, cflags); | |
7a612350 | 2193 | io_commit_cqring(ctx); |
79ebeaee | 2194 | spin_unlock(&ctx->completion_lock); |
a3f34907 | 2195 | io_cqring_ev_posted(ctx); |
4e3d9ff9 JA |
2196 | } |
2197 | ||
54daa9b2 PB |
2198 | static inline void io_req_complete_state(struct io_kiocb *req, s32 res, |
2199 | u32 cflags) | |
229a7b63 | 2200 | { |
cef216fc PB |
2201 | req->cqe.res = res; |
2202 | req->cqe.flags = cflags; | |
e342c807 | 2203 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
2204 | } |
2205 | ||
889fca73 | 2206 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
54daa9b2 | 2207 | s32 res, u32 cflags) |
bcda7baa | 2208 | { |
889fca73 PB |
2209 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
2210 | io_req_complete_state(req, res, cflags); | |
a38d68db | 2211 | else |
c7dae4ba | 2212 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
2213 | } |
2214 | ||
54daa9b2 | 2215 | static inline void io_req_complete(struct io_kiocb *req, s32 res) |
0ddf92e8 | 2216 | { |
889fca73 | 2217 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
2218 | } |
2219 | ||
54daa9b2 | 2220 | static void io_req_complete_failed(struct io_kiocb *req, s32 res) |
f41db273 | 2221 | { |
93d2bcd2 | 2222 | req_set_fail(req); |
ab0ac095 | 2223 | io_req_complete_post(req, res, io_put_kbuf(req, IO_URING_F_UNLOCKED)); |
f41db273 PB |
2224 | } |
2225 | ||
c6d3d9cb PB |
2226 | static void io_req_complete_fail_submit(struct io_kiocb *req) |
2227 | { | |
2228 | /* | |
2229 | * We don't submit, fail them all, for that replace hardlinks with | |
2230 | * normal links. Extra REQ_F_LINK is tolerated. | |
2231 | */ | |
2232 | req->flags &= ~REQ_F_HARDLINK; | |
2233 | req->flags |= REQ_F_LINK; | |
cef216fc | 2234 | io_req_complete_failed(req, req->cqe.res); |
c6d3d9cb PB |
2235 | } |
2236 | ||
864ea921 PB |
2237 | /* |
2238 | * Don't initialise the fields below on every allocation, but do that in | |
2239 | * advance and keep them valid across allocations. | |
2240 | */ | |
2241 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
2242 | { | |
2243 | req->ctx = ctx; | |
2244 | req->link = NULL; | |
2245 | req->async_data = NULL; | |
2246 | /* not necessary, but safer to zero */ | |
cef216fc | 2247 | req->cqe.res = 0; |
864ea921 PB |
2248 | } |
2249 | ||
dac7a098 | 2250 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
cd0ca2e0 | 2251 | struct io_submit_state *state) |
dac7a098 | 2252 | { |
79ebeaee | 2253 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 2254 | wq_list_splice(&ctx->locked_free_list, &state->free_list); |
d0acdee2 | 2255 | ctx->locked_free_nr = 0; |
79ebeaee | 2256 | spin_unlock(&ctx->completion_lock); |
dac7a098 PB |
2257 | } |
2258 | ||
88ab95be PB |
2259 | static inline bool io_req_cache_empty(struct io_ring_ctx *ctx) |
2260 | { | |
2261 | return !ctx->submit_state.free_list.next; | |
2262 | } | |
2263 | ||
5d5901a3 PB |
2264 | /* |
2265 | * A request might get retired back into the request caches even before opcode | |
2266 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. | |
2267 | * Because of that, io_alloc_req() should be called only under ->uring_lock | |
2268 | * and with extra caution to not get a request that is still worked on. | |
2269 | */ | |
c072481d | 2270 | static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx) |
5d5901a3 | 2271 | __must_hold(&ctx->uring_lock) |
2b188cc1 | 2272 | { |
864ea921 | 2273 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
3ab665b7 | 2274 | void *reqs[IO_REQ_ALLOC_BATCH]; |
864ea921 | 2275 | int ret, i; |
e5d1bc0a | 2276 | |
23a5c43b PB |
2277 | /* |
2278 | * If we have more than a batch's worth of requests in our IRQ side | |
2279 | * locked cache, grab the lock and move them over to our submission | |
2280 | * side cache. | |
2281 | */ | |
2282 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) { | |
2283 | io_flush_cached_locked_reqs(ctx, &ctx->submit_state); | |
88ab95be | 2284 | if (!io_req_cache_empty(ctx)) |
23a5c43b PB |
2285 | return true; |
2286 | } | |
e5d1bc0a | 2287 | |
3ab665b7 | 2288 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs); |
fd6fab2c | 2289 | |
864ea921 PB |
2290 | /* |
2291 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
2292 | * retry single alloc to be on the safe side. | |
2293 | */ | |
2294 | if (unlikely(ret <= 0)) { | |
3ab665b7 PB |
2295 | reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
2296 | if (!reqs[0]) | |
a33ae9ce | 2297 | return false; |
864ea921 | 2298 | ret = 1; |
2b188cc1 | 2299 | } |
864ea921 | 2300 | |
37f0e767 | 2301 | percpu_ref_get_many(&ctx->refs, ret); |
3ab665b7 | 2302 | for (i = 0; i < ret; i++) { |
23a5c43b | 2303 | struct io_kiocb *req = reqs[i]; |
3ab665b7 PB |
2304 | |
2305 | io_preinit_req(req, ctx); | |
fa05457a | 2306 | io_req_add_to_cache(req, ctx); |
3ab665b7 | 2307 | } |
a33ae9ce PB |
2308 | return true; |
2309 | } | |
2310 | ||
2311 | static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx) | |
2312 | { | |
88ab95be | 2313 | if (unlikely(io_req_cache_empty(ctx))) |
a33ae9ce PB |
2314 | return __io_alloc_req_refill(ctx); |
2315 | return true; | |
2316 | } | |
2317 | ||
2318 | static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) | |
2319 | { | |
2320 | struct io_wq_work_node *node; | |
2321 | ||
2322 | node = wq_stack_extract(&ctx->submit_state.free_list); | |
c2b6c6bc | 2323 | return container_of(node, struct io_kiocb, comp_list); |
2b188cc1 JA |
2324 | } |
2325 | ||
e1d767f0 | 2326 | static inline void io_put_file(struct file *file) |
8da11c19 | 2327 | { |
e1d767f0 | 2328 | if (file) |
8da11c19 PB |
2329 | fput(file); |
2330 | } | |
2331 | ||
6b639522 | 2332 | static inline void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 2333 | { |
094bae49 | 2334 | unsigned int flags = req->flags; |
929a3af9 | 2335 | |
867f8fa5 | 2336 | if (unlikely(flags & IO_REQ_CLEAN_FLAGS)) |
3a0a6902 | 2337 | io_clean_op(req); |
e1d767f0 PB |
2338 | if (!(flags & REQ_F_FIXED_FILE)) |
2339 | io_put_file(req->file); | |
e65ef56d JA |
2340 | } |
2341 | ||
f5c6cf2a | 2342 | static __cold void io_free_req(struct io_kiocb *req) |
c6ca97b3 | 2343 | { |
51a4cc11 | 2344 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 2345 | |
ab409402 | 2346 | io_req_put_rsrc(req, ctx); |
216578e5 | 2347 | io_dismantle_req(req); |
7c660731 | 2348 | io_put_task(req->task, 1); |
c6ca97b3 | 2349 | |
79ebeaee | 2350 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 2351 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
c34b025f | 2352 | ctx->locked_free_nr++; |
79ebeaee | 2353 | spin_unlock(&ctx->completion_lock); |
e65ef56d JA |
2354 | } |
2355 | ||
f2f87370 PB |
2356 | static inline void io_remove_next_linked(struct io_kiocb *req) |
2357 | { | |
2358 | struct io_kiocb *nxt = req->link; | |
2359 | ||
2360 | req->link = nxt->link; | |
2361 | nxt->link = NULL; | |
2362 | } | |
2363 | ||
33cc89a9 PB |
2364 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
2365 | __must_hold(&req->ctx->completion_lock) | |
89b263f6 | 2366 | __must_hold(&req->ctx->timeout_lock) |
2665abfd | 2367 | { |
33cc89a9 | 2368 | struct io_kiocb *link = req->link; |
f2f87370 | 2369 | |
b97e736a | 2370 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
c9abd7ad | 2371 | struct io_timeout_data *io = link->async_data; |
7c86ffee | 2372 | |
f2f87370 | 2373 | io_remove_next_linked(req); |
90cd7e42 | 2374 | link->timeout.head = NULL; |
fd9c7bc5 | 2375 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
ef9dd637 | 2376 | list_del(&link->timeout.list); |
4e118cd9 | 2377 | io_req_tw_post_queue(link, -ECANCELED, 0); |
d4729fbd | 2378 | return true; |
c9abd7ad PB |
2379 | } |
2380 | } | |
d4729fbd | 2381 | return false; |
7c86ffee PB |
2382 | } |
2383 | ||
d148ca4b | 2384 | static void io_fail_links(struct io_kiocb *req) |
33cc89a9 | 2385 | __must_hold(&req->ctx->completion_lock) |
9e645e11 | 2386 | { |
33cc89a9 | 2387 | struct io_kiocb *nxt, *link = req->link; |
04c76b41 | 2388 | bool ignore_cqes = req->flags & REQ_F_SKIP_LINK_CQES; |
9e645e11 | 2389 | |
f2f87370 | 2390 | req->link = NULL; |
f2f87370 | 2391 | while (link) { |
a8295b98 HX |
2392 | long res = -ECANCELED; |
2393 | ||
2394 | if (link->flags & REQ_F_FAIL) | |
cef216fc | 2395 | res = link->cqe.res; |
a8295b98 | 2396 | |
f2f87370 PB |
2397 | nxt = link->link; |
2398 | link->link = NULL; | |
2665abfd | 2399 | |
cef216fc | 2400 | trace_io_uring_fail_link(req->ctx, req, req->cqe.user_data, |
502c87d6 SR |
2401 | req->opcode, link); |
2402 | ||
4e118cd9 PB |
2403 | if (ignore_cqes) |
2404 | link->flags |= REQ_F_CQE_SKIP; | |
2405 | else | |
04c76b41 | 2406 | link->flags &= ~REQ_F_CQE_SKIP; |
4e118cd9 | 2407 | __io_req_complete_post(link, res, 0); |
f2f87370 | 2408 | link = nxt; |
9e645e11 | 2409 | } |
33cc89a9 | 2410 | } |
9e645e11 | 2411 | |
33cc89a9 PB |
2412 | static bool io_disarm_next(struct io_kiocb *req) |
2413 | __must_hold(&req->ctx->completion_lock) | |
2414 | { | |
2415 | bool posted = false; | |
2416 | ||
0756a869 PB |
2417 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
2418 | struct io_kiocb *link = req->link; | |
2419 | ||
906c6caa | 2420 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
0756a869 PB |
2421 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
2422 | io_remove_next_linked(req); | |
4e118cd9 | 2423 | io_req_tw_post_queue(link, -ECANCELED, 0); |
0756a869 PB |
2424 | posted = true; |
2425 | } | |
2426 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { | |
89b263f6 JA |
2427 | struct io_ring_ctx *ctx = req->ctx; |
2428 | ||
2429 | spin_lock_irq(&ctx->timeout_lock); | |
33cc89a9 | 2430 | posted = io_kill_linked_timeout(req); |
89b263f6 JA |
2431 | spin_unlock_irq(&ctx->timeout_lock); |
2432 | } | |
93d2bcd2 | 2433 | if (unlikely((req->flags & REQ_F_FAIL) && |
e4335ed3 | 2434 | !(req->flags & REQ_F_HARDLINK))) { |
33cc89a9 PB |
2435 | posted |= (req->link != NULL); |
2436 | io_fail_links(req); | |
2437 | } | |
2438 | return posted; | |
9e645e11 JA |
2439 | } |
2440 | ||
d81499bf PB |
2441 | static void __io_req_find_next_prep(struct io_kiocb *req) |
2442 | { | |
2443 | struct io_ring_ctx *ctx = req->ctx; | |
2444 | bool posted; | |
2445 | ||
2446 | spin_lock(&ctx->completion_lock); | |
2447 | posted = io_disarm_next(req); | |
60053be8 | 2448 | io_commit_cqring(ctx); |
d81499bf PB |
2449 | spin_unlock(&ctx->completion_lock); |
2450 | if (posted) | |
2451 | io_cqring_ev_posted(ctx); | |
2452 | } | |
2453 | ||
2454 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) | |
c69f8dbe | 2455 | { |
33cc89a9 | 2456 | struct io_kiocb *nxt; |
944e58bf | 2457 | |
9e645e11 JA |
2458 | /* |
2459 | * If LINK is set, we have dependent requests in this chain. If we | |
2460 | * didn't fail this request, queue the first one up, moving any other | |
2461 | * dependencies to the next request. In case of failure, fail the rest | |
2462 | * of the chain. | |
2463 | */ | |
d81499bf PB |
2464 | if (unlikely(req->flags & IO_DISARM_MASK)) |
2465 | __io_req_find_next_prep(req); | |
33cc89a9 PB |
2466 | nxt = req->link; |
2467 | req->link = NULL; | |
2468 | return nxt; | |
4d7dd462 | 2469 | } |
9e645e11 | 2470 | |
f237c30a | 2471 | static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked) |
2c32395d PB |
2472 | { |
2473 | if (!ctx) | |
2474 | return; | |
f237c30a | 2475 | if (*locked) { |
c450178d | 2476 | io_submit_flush_completions(ctx); |
2c32395d | 2477 | mutex_unlock(&ctx->uring_lock); |
f237c30a | 2478 | *locked = false; |
2c32395d PB |
2479 | } |
2480 | percpu_ref_put(&ctx->refs); | |
2481 | } | |
2482 | ||
f28c240e HX |
2483 | static inline void ctx_commit_and_unlock(struct io_ring_ctx *ctx) |
2484 | { | |
2485 | io_commit_cqring(ctx); | |
2486 | spin_unlock(&ctx->completion_lock); | |
2487 | io_cqring_ev_posted(ctx); | |
2488 | } | |
2489 | ||
2490 | static void handle_prev_tw_list(struct io_wq_work_node *node, | |
2491 | struct io_ring_ctx **ctx, bool *uring_locked) | |
2492 | { | |
2493 | if (*ctx && !*uring_locked) | |
2494 | spin_lock(&(*ctx)->completion_lock); | |
2495 | ||
2496 | do { | |
2497 | struct io_wq_work_node *next = node->next; | |
2498 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2499 | io_task_work.node); | |
2500 | ||
34d2bfe7 JA |
2501 | prefetch(container_of(next, struct io_kiocb, io_task_work.node)); |
2502 | ||
f28c240e HX |
2503 | if (req->ctx != *ctx) { |
2504 | if (unlikely(!*uring_locked && *ctx)) | |
2505 | ctx_commit_and_unlock(*ctx); | |
2506 | ||
2507 | ctx_flush_and_put(*ctx, uring_locked); | |
2508 | *ctx = req->ctx; | |
2509 | /* if not contended, grab and improve batching */ | |
2510 | *uring_locked = mutex_trylock(&(*ctx)->uring_lock); | |
2511 | percpu_ref_get(&(*ctx)->refs); | |
2512 | if (unlikely(!*uring_locked)) | |
2513 | spin_lock(&(*ctx)->completion_lock); | |
2514 | } | |
2515 | if (likely(*uring_locked)) | |
2516 | req->io_task_work.func(req, uring_locked); | |
2517 | else | |
cef216fc | 2518 | __io_req_complete_post(req, req->cqe.res, |
cc3cec83 | 2519 | io_put_kbuf_comp(req)); |
f28c240e HX |
2520 | node = next; |
2521 | } while (node); | |
2522 | ||
2523 | if (unlikely(!*uring_locked)) | |
2524 | ctx_commit_and_unlock(*ctx); | |
2525 | } | |
2526 | ||
2527 | static void handle_tw_list(struct io_wq_work_node *node, | |
2528 | struct io_ring_ctx **ctx, bool *locked) | |
9f8d032a HX |
2529 | { |
2530 | do { | |
2531 | struct io_wq_work_node *next = node->next; | |
2532 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2533 | io_task_work.node); | |
2534 | ||
34d2bfe7 JA |
2535 | prefetch(container_of(next, struct io_kiocb, io_task_work.node)); |
2536 | ||
9f8d032a HX |
2537 | if (req->ctx != *ctx) { |
2538 | ctx_flush_and_put(*ctx, locked); | |
2539 | *ctx = req->ctx; | |
2540 | /* if not contended, grab and improve batching */ | |
2541 | *locked = mutex_trylock(&(*ctx)->uring_lock); | |
2542 | percpu_ref_get(&(*ctx)->refs); | |
2543 | } | |
2544 | req->io_task_work.func(req, locked); | |
2545 | node = next; | |
2546 | } while (node); | |
2547 | } | |
2548 | ||
7cbf1722 | 2549 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 2550 | { |
f28c240e | 2551 | bool uring_locked = false; |
ebd0df2e | 2552 | struct io_ring_ctx *ctx = NULL; |
3f18407d PB |
2553 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
2554 | task_work); | |
c40f6379 | 2555 | |
16f72070 | 2556 | while (1) { |
f28c240e | 2557 | struct io_wq_work_node *node1, *node2; |
3f18407d PB |
2558 | |
2559 | spin_lock_irq(&tctx->task_lock); | |
f28c240e HX |
2560 | node1 = tctx->prior_task_list.first; |
2561 | node2 = tctx->task_list.first; | |
3f18407d | 2562 | INIT_WQ_LIST(&tctx->task_list); |
f28c240e HX |
2563 | INIT_WQ_LIST(&tctx->prior_task_list); |
2564 | if (!node2 && !node1) | |
6294f368 | 2565 | tctx->task_running = false; |
3f18407d | 2566 | spin_unlock_irq(&tctx->task_lock); |
f28c240e | 2567 | if (!node2 && !node1) |
6294f368 | 2568 | break; |
3f18407d | 2569 | |
f28c240e HX |
2570 | if (node1) |
2571 | handle_prev_tw_list(node1, &ctx, &uring_locked); | |
f28c240e HX |
2572 | if (node2) |
2573 | handle_tw_list(node2, &ctx, &uring_locked); | |
7cbf1722 | 2574 | cond_resched(); |
68ca8fc0 PB |
2575 | |
2576 | if (!tctx->task_list.first && | |
2577 | !tctx->prior_task_list.first && uring_locked) | |
2578 | io_submit_flush_completions(ctx); | |
3f18407d | 2579 | } |
ebd0df2e | 2580 | |
f28c240e | 2581 | ctx_flush_and_put(ctx, &uring_locked); |
3cc7fdb9 PB |
2582 | |
2583 | /* relaxed read is enough as only the task itself sets ->in_idle */ | |
2584 | if (unlikely(atomic_read(&tctx->in_idle))) | |
2585 | io_uring_drop_tctx_refs(current); | |
7cbf1722 JA |
2586 | } |
2587 | ||
4813c377 | 2588 | static void io_req_task_work_add(struct io_kiocb *req, bool priority) |
7cbf1722 | 2589 | { |
c15b79de | 2590 | struct task_struct *tsk = req->task; |
7cbf1722 | 2591 | struct io_uring_task *tctx = tsk->io_uring; |
c15b79de | 2592 | enum task_work_notify_mode notify; |
e09ee510 | 2593 | struct io_wq_work_node *node; |
0b81e80c | 2594 | unsigned long flags; |
6294f368 | 2595 | bool running; |
7cbf1722 JA |
2596 | |
2597 | WARN_ON_ONCE(!tctx); | |
2598 | ||
d5361233 JA |
2599 | io_drop_inflight_file(req); |
2600 | ||
0b81e80c | 2601 | spin_lock_irqsave(&tctx->task_lock, flags); |
4813c377 HX |
2602 | if (priority) |
2603 | wq_list_add_tail(&req->io_task_work.node, &tctx->prior_task_list); | |
2604 | else | |
2605 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); | |
6294f368 PB |
2606 | running = tctx->task_running; |
2607 | if (!running) | |
2608 | tctx->task_running = true; | |
0b81e80c | 2609 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2610 | |
2611 | /* task_work already pending, we're done */ | |
6294f368 | 2612 | if (running) |
e09ee510 | 2613 | return; |
7cbf1722 | 2614 | |
c15b79de PB |
2615 | /* |
2616 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For | |
2617 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2618 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2619 | * will do the job. | |
2620 | */ | |
2621 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; | |
d97ec623 PB |
2622 | if (likely(!task_work_add(tsk, &tctx->task_work, notify))) { |
2623 | if (notify == TWA_NONE) | |
2624 | wake_up_process(tsk); | |
e09ee510 | 2625 | return; |
c15b79de | 2626 | } |
2215bed9 | 2627 | |
0b81e80c | 2628 | spin_lock_irqsave(&tctx->task_lock, flags); |
6294f368 | 2629 | tctx->task_running = false; |
4813c377 | 2630 | node = wq_list_merge(&tctx->prior_task_list, &tctx->task_list); |
0b81e80c | 2631 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 | 2632 | |
e09ee510 PB |
2633 | while (node) { |
2634 | req = container_of(node, struct io_kiocb, io_task_work.node); | |
2635 | node = node->next; | |
2636 | if (llist_add(&req->io_task_work.fallback_node, | |
2637 | &req->ctx->fallback_llist)) | |
2638 | schedule_delayed_work(&req->ctx->fallback_work, 1); | |
2639 | } | |
eab30c4d PB |
2640 | } |
2641 | ||
4e118cd9 PB |
2642 | static void io_req_tw_post(struct io_kiocb *req, bool *locked) |
2643 | { | |
2644 | io_req_complete_post(req, req->cqe.res, req->cqe.flags); | |
2645 | } | |
2646 | ||
2647 | static void io_req_tw_post_queue(struct io_kiocb *req, s32 res, u32 cflags) | |
2648 | { | |
2649 | req->cqe.res = res; | |
2650 | req->cqe.flags = cflags; | |
2651 | req->io_task_work.func = io_req_tw_post; | |
2652 | io_req_task_work_add(req, false); | |
2653 | } | |
2654 | ||
f237c30a | 2655 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
c40f6379 | 2656 | { |
b18a1a45 | 2657 | /* not needed for normal modes, but SQPOLL depends on it */ |
971cf9c1 | 2658 | io_tw_lock(req->ctx, locked); |
cef216fc | 2659 | io_req_complete_failed(req, req->cqe.res); |
c40f6379 JA |
2660 | } |
2661 | ||
f237c30a | 2662 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
c40f6379 | 2663 | { |
971cf9c1 | 2664 | io_tw_lock(req->ctx, locked); |
316319e8 | 2665 | /* req->task == current here, checking PF_EXITING is safe */ |
af066f31 | 2666 | if (likely(!(req->task->flags & PF_EXITING))) |
cbc2e203 | 2667 | io_queue_sqe(req); |
81b6d05c | 2668 | else |
2593553a | 2669 | io_req_complete_failed(req, -EFAULT); |
c40f6379 JA |
2670 | } |
2671 | ||
2c4b8eb6 | 2672 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
c40f6379 | 2673 | { |
cef216fc | 2674 | req->cqe.res = ret; |
5b0a6acc | 2675 | req->io_task_work.func = io_req_task_cancel; |
4813c377 | 2676 | io_req_task_work_add(req, false); |
c40f6379 JA |
2677 | } |
2678 | ||
2c4b8eb6 | 2679 | static void io_req_task_queue(struct io_kiocb *req) |
a3df7698 | 2680 | { |
5b0a6acc | 2681 | req->io_task_work.func = io_req_task_submit; |
4813c377 | 2682 | io_req_task_work_add(req, false); |
a3df7698 PB |
2683 | } |
2684 | ||
773af691 JA |
2685 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
2686 | { | |
77955efb | 2687 | req->io_task_work.func = io_queue_iowq; |
4813c377 | 2688 | io_req_task_work_add(req, false); |
773af691 JA |
2689 | } |
2690 | ||
57859f4d | 2691 | static void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2692 | { |
57859f4d | 2693 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf | 2694 | |
57859f4d PB |
2695 | if (nxt) |
2696 | io_req_task_queue(nxt); | |
c69f8dbe JL |
2697 | } |
2698 | ||
3aa83bfb | 2699 | static void io_free_batch_list(struct io_ring_ctx *ctx, |
1cce17ac | 2700 | struct io_wq_work_node *node) |
3aa83bfb | 2701 | __must_hold(&ctx->uring_lock) |
5af1d13e | 2702 | { |
d4b7a5ef | 2703 | struct task_struct *task = NULL; |
37f0e767 | 2704 | int task_refs = 0; |
5af1d13e | 2705 | |
3aa83bfb PB |
2706 | do { |
2707 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2708 | comp_list); | |
2d6500d4 | 2709 | |
a538be5b PB |
2710 | if (unlikely(req->flags & IO_REQ_CLEAN_SLOW_FLAGS)) { |
2711 | if (req->flags & REQ_F_REFCOUNT) { | |
2712 | node = req->comp_list.next; | |
2713 | if (!req_ref_put_and_test(req)) | |
2714 | continue; | |
2715 | } | |
b605a7fa PB |
2716 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
2717 | struct async_poll *apoll = req->apoll; | |
2718 | ||
2719 | if (apoll->double_poll) | |
2720 | kfree(apoll->double_poll); | |
2721 | list_add(&apoll->poll.wait.entry, | |
2722 | &ctx->apoll_cache); | |
2723 | req->flags &= ~REQ_F_POLLED; | |
2724 | } | |
da1a08c5 | 2725 | if (req->flags & IO_REQ_LINK_FLAGS) |
57859f4d | 2726 | io_queue_next(req); |
a538be5b PB |
2727 | if (unlikely(req->flags & IO_REQ_CLEAN_FLAGS)) |
2728 | io_clean_op(req); | |
c1e53a69 | 2729 | } |
a538be5b PB |
2730 | if (!(req->flags & REQ_F_FIXED_FILE)) |
2731 | io_put_file(req->file); | |
2d6500d4 | 2732 | |
ab409402 | 2733 | io_req_put_rsrc_locked(req, ctx); |
5af1d13e | 2734 | |
d4b7a5ef PB |
2735 | if (req->task != task) { |
2736 | if (task) | |
2737 | io_put_task(task, task_refs); | |
2738 | task = req->task; | |
2739 | task_refs = 0; | |
2740 | } | |
2741 | task_refs++; | |
c1e53a69 | 2742 | node = req->comp_list.next; |
fa05457a | 2743 | io_req_add_to_cache(req, ctx); |
3aa83bfb | 2744 | } while (node); |
d4b7a5ef | 2745 | |
d4b7a5ef PB |
2746 | if (task) |
2747 | io_put_task(task, task_refs); | |
7a743e22 PB |
2748 | } |
2749 | ||
c450178d | 2750 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx) |
a141dd89 | 2751 | __must_hold(&ctx->uring_lock) |
905c172f | 2752 | { |
6f33b0bc | 2753 | struct io_wq_work_node *node, *prev; |
cd0ca2e0 | 2754 | struct io_submit_state *state = &ctx->submit_state; |
905c172f | 2755 | |
3d4aeb9f PB |
2756 | if (state->flush_cqes) { |
2757 | spin_lock(&ctx->completion_lock); | |
2758 | wq_list_for_each(node, prev, &state->compl_reqs) { | |
2759 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
6f33b0bc | 2760 | comp_list); |
5182ed2e | 2761 | |
3d4aeb9f | 2762 | if (!(req->flags & REQ_F_CQE_SKIP)) |
90e7c35f | 2763 | __io_fill_cqe_req_filled(ctx, req); |
3d4aeb9f PB |
2764 | } |
2765 | ||
2766 | io_commit_cqring(ctx); | |
2767 | spin_unlock(&ctx->completion_lock); | |
2768 | io_cqring_ev_posted(ctx); | |
2769 | state->flush_cqes = false; | |
905c172f | 2770 | } |
5182ed2e | 2771 | |
1cce17ac | 2772 | io_free_batch_list(ctx, state->compl_reqs.first); |
6f33b0bc | 2773 | INIT_WQ_LIST(&state->compl_reqs); |
7a743e22 PB |
2774 | } |
2775 | ||
ba816ad6 JA |
2776 | /* |
2777 | * Drop reference to request, return next in chain (if there is one) if this | |
2778 | * was the last reference to this request. | |
2779 | */ | |
0d85035a | 2780 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2781 | { |
9b5f7bd9 PB |
2782 | struct io_kiocb *nxt = NULL; |
2783 | ||
de9b4cca | 2784 | if (req_ref_put_and_test(req)) { |
da1a08c5 | 2785 | if (unlikely(req->flags & IO_REQ_LINK_FLAGS)) |
7819a1f6 | 2786 | nxt = io_req_find_next(req); |
f5c6cf2a | 2787 | io_free_req(req); |
2a44f467 | 2788 | } |
9b5f7bd9 | 2789 | return nxt; |
2b188cc1 JA |
2790 | } |
2791 | ||
0d85035a | 2792 | static inline void io_put_req(struct io_kiocb *req) |
e65ef56d | 2793 | { |
f5c6cf2a PB |
2794 | if (req_ref_put_and_test(req)) { |
2795 | io_queue_next(req); | |
e65ef56d | 2796 | io_free_req(req); |
f5c6cf2a | 2797 | } |
2b188cc1 JA |
2798 | } |
2799 | ||
6c503150 | 2800 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2801 | { |
2802 | /* See comment at the top of this file */ | |
2803 | smp_rmb(); | |
e23de15f | 2804 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2805 | } |
2806 | ||
fb5ccc98 PB |
2807 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2808 | { | |
2809 | struct io_rings *rings = ctx->rings; | |
2810 | ||
2811 | /* make sure SQ entry isn't read before tail */ | |
2812 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2813 | } | |
2814 | ||
4c6e277c JA |
2815 | static inline bool io_run_task_work(void) |
2816 | { | |
7f62d40d | 2817 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || task_work_pending(current)) { |
4c6e277c | 2818 | __set_current_state(TASK_RUNNING); |
7c5d8fa6 EB |
2819 | clear_notify_signal(); |
2820 | if (task_work_pending(current)) | |
2821 | task_work_run(); | |
4c6e277c JA |
2822 | return true; |
2823 | } | |
2824 | ||
2825 | return false; | |
bcda7baa JA |
2826 | } |
2827 | ||
5ba3c874 | 2828 | static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) |
def596e9 | 2829 | { |
5eef4e87 | 2830 | struct io_wq_work_node *pos, *start, *prev; |
d729cf9a | 2831 | unsigned int poll_flags = BLK_POLL_NOSLEEP; |
b688f11e | 2832 | DEFINE_IO_COMP_BATCH(iob); |
5ba3c874 | 2833 | int nr_events = 0; |
def596e9 JA |
2834 | |
2835 | /* | |
2836 | * Only spin for completions if we don't have multiple devices hanging | |
87a115fb | 2837 | * off our complete list. |
def596e9 | 2838 | */ |
87a115fb | 2839 | if (ctx->poll_multi_queue || force_nonspin) |
ef99b2d3 | 2840 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2841 | |
5eef4e87 PB |
2842 | wq_list_for_each(pos, start, &ctx->iopoll_list) { |
2843 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
9adbd45d | 2844 | struct kiocb *kiocb = &req->rw.kiocb; |
a2416e1e | 2845 | int ret; |
def596e9 JA |
2846 | |
2847 | /* | |
581f9810 BM |
2848 | * Move completed and retryable entries to our local lists. |
2849 | * If we find a request that requires polling, break out | |
2850 | * and complete those lists first, if we have entries there. | |
def596e9 | 2851 | */ |
e3f721e6 | 2852 | if (READ_ONCE(req->iopoll_completed)) |
def596e9 JA |
2853 | break; |
2854 | ||
b688f11e | 2855 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags); |
a2416e1e PB |
2856 | if (unlikely(ret < 0)) |
2857 | return ret; | |
2858 | else if (ret) | |
ef99b2d3 | 2859 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2860 | |
3aadc23e | 2861 | /* iopoll may have completed current req */ |
b688f11e JA |
2862 | if (!rq_list_empty(iob.req_list) || |
2863 | READ_ONCE(req->iopoll_completed)) | |
e3f721e6 | 2864 | break; |
def596e9 JA |
2865 | } |
2866 | ||
b688f11e JA |
2867 | if (!rq_list_empty(iob.req_list)) |
2868 | iob.complete(&iob); | |
5eef4e87 PB |
2869 | else if (!pos) |
2870 | return 0; | |
def596e9 | 2871 | |
5eef4e87 PB |
2872 | prev = start; |
2873 | wq_list_for_each_resume(pos, prev) { | |
2874 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
2875 | ||
b3fa03fd PB |
2876 | /* order with io_complete_rw_iopoll(), e.g. ->result updates */ |
2877 | if (!smp_load_acquire(&req->iopoll_completed)) | |
e3f721e6 | 2878 | break; |
c0713540 | 2879 | nr_events++; |
83a13a41 PB |
2880 | if (unlikely(req->flags & REQ_F_CQE_SKIP)) |
2881 | continue; | |
cef216fc | 2882 | __io_fill_cqe_req(req, req->cqe.res, io_put_kbuf(req, 0)); |
e3f721e6 | 2883 | } |
def596e9 | 2884 | |
f5ed3bcd PB |
2885 | if (unlikely(!nr_events)) |
2886 | return 0; | |
2887 | ||
2888 | io_commit_cqring(ctx); | |
2889 | io_cqring_ev_posted_iopoll(ctx); | |
1cce17ac | 2890 | pos = start ? start->next : ctx->iopoll_list.first; |
5eef4e87 | 2891 | wq_list_cut(&ctx->iopoll_list, prev, start); |
1cce17ac | 2892 | io_free_batch_list(ctx, pos); |
5ba3c874 | 2893 | return nr_events; |
def596e9 JA |
2894 | } |
2895 | ||
def596e9 JA |
2896 | /* |
2897 | * We can't just wait for polled events to come to us, we have to actively | |
2898 | * find and complete them. | |
2899 | */ | |
c072481d | 2900 | static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2901 | { |
2902 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2903 | return; | |
2904 | ||
2905 | mutex_lock(&ctx->uring_lock); | |
5eef4e87 | 2906 | while (!wq_list_empty(&ctx->iopoll_list)) { |
b2edc0a7 | 2907 | /* let it sleep and repeat later if can't complete a request */ |
5ba3c874 | 2908 | if (io_do_iopoll(ctx, true) == 0) |
b2edc0a7 | 2909 | break; |
08f5439f JA |
2910 | /* |
2911 | * Ensure we allow local-to-the-cpu processing to take place, | |
2912 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2913 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2914 | */ |
3fcee5a6 PB |
2915 | if (need_resched()) { |
2916 | mutex_unlock(&ctx->uring_lock); | |
2917 | cond_resched(); | |
2918 | mutex_lock(&ctx->uring_lock); | |
2919 | } | |
def596e9 JA |
2920 | } |
2921 | mutex_unlock(&ctx->uring_lock); | |
2922 | } | |
2923 | ||
7668b92a | 2924 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2925 | { |
7668b92a | 2926 | unsigned int nr_events = 0; |
e9979b36 | 2927 | int ret = 0; |
500f9fba | 2928 | |
f39c8a5b PB |
2929 | /* |
2930 | * Don't enter poll loop if we already have events pending. | |
2931 | * If we do, we can potentially be spinning for commands that | |
2932 | * already triggered a CQE (eg in error). | |
2933 | */ | |
5ed7a37d | 2934 | if (test_bit(0, &ctx->check_cq_overflow)) |
f39c8a5b PB |
2935 | __io_cqring_overflow_flush(ctx, false); |
2936 | if (io_cqring_events(ctx)) | |
d487b43c | 2937 | return 0; |
def596e9 | 2938 | do { |
500f9fba JA |
2939 | /* |
2940 | * If a submit got punted to a workqueue, we can have the | |
2941 | * application entering polling for a command before it gets | |
2942 | * issued. That app will hold the uring_lock for the duration | |
2943 | * of the poll right here, so we need to take a breather every | |
2944 | * now and then to ensure that the issue has a chance to add | |
2945 | * the poll to the issued list. Otherwise we can spin here | |
2946 | * forever, while the workqueue is stuck trying to acquire the | |
2947 | * very same mutex. | |
2948 | */ | |
5eef4e87 | 2949 | if (wq_list_empty(&ctx->iopoll_list)) { |
8f487ef2 PB |
2950 | u32 tail = ctx->cached_cq_tail; |
2951 | ||
500f9fba | 2952 | mutex_unlock(&ctx->uring_lock); |
4c6e277c | 2953 | io_run_task_work(); |
500f9fba | 2954 | mutex_lock(&ctx->uring_lock); |
def596e9 | 2955 | |
8f487ef2 PB |
2956 | /* some requests don't go through iopoll_list */ |
2957 | if (tail != ctx->cached_cq_tail || | |
5eef4e87 | 2958 | wq_list_empty(&ctx->iopoll_list)) |
e9979b36 | 2959 | break; |
500f9fba | 2960 | } |
5ba3c874 PB |
2961 | ret = io_do_iopoll(ctx, !min); |
2962 | if (ret < 0) | |
2963 | break; | |
2964 | nr_events += ret; | |
2965 | ret = 0; | |
2966 | } while (nr_events < min && !need_resched()); | |
d487b43c | 2967 | |
def596e9 JA |
2968 | return ret; |
2969 | } | |
2970 | ||
491381ce | 2971 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2972 | { |
491381ce JA |
2973 | /* |
2974 | * Tell lockdep we inherited freeze protection from submission | |
2975 | * thread. | |
2976 | */ | |
2977 | if (req->flags & REQ_F_ISREG) { | |
1c98679d | 2978 | struct super_block *sb = file_inode(req->file)->i_sb; |
2b188cc1 | 2979 | |
1c98679d PB |
2980 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
2981 | sb_end_write(sb); | |
2b188cc1 JA |
2982 | } |
2983 | } | |
2984 | ||
b63534c4 | 2985 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2986 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 | 2987 | { |
ab454438 | 2988 | struct io_async_rw *rw = req->async_data; |
b63534c4 | 2989 | |
d886e185 | 2990 | if (!req_has_async_data(req)) |
ab454438 | 2991 | return !io_req_prep_async(req); |
538941e2 | 2992 | iov_iter_restore(&rw->s.iter, &rw->s.iter_state); |
ab454438 | 2993 | return true; |
b63534c4 | 2994 | } |
b63534c4 | 2995 | |
3e6a0d3c | 2996 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 2997 | { |
355afaeb | 2998 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 2999 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 3000 | |
355afaeb JA |
3001 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
3002 | return false; | |
3e6a0d3c JA |
3003 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
3004 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 3005 | return false; |
7c977a58 JA |
3006 | /* |
3007 | * If ref is dying, we might be running poll reap from the exit work. | |
3008 | * Don't attempt to reissue from that path, just let it fail with | |
3009 | * -EAGAIN. | |
3010 | */ | |
3e6a0d3c JA |
3011 | if (percpu_ref_is_dying(&ctx->refs)) |
3012 | return false; | |
ef046888 JA |
3013 | /* |
3014 | * Play it safe and assume not safe to re-import and reissue if we're | |
3015 | * not in the original thread group (or in task context). | |
3016 | */ | |
3017 | if (!same_thread_group(req->task, current) || !in_task()) | |
3018 | return false; | |
3e6a0d3c JA |
3019 | return true; |
3020 | } | |
e82ad485 | 3021 | #else |
a1ff1e3f | 3022 | static bool io_resubmit_prep(struct io_kiocb *req) |
e82ad485 JA |
3023 | { |
3024 | return false; | |
3025 | } | |
e82ad485 | 3026 | static bool io_rw_should_reissue(struct io_kiocb *req) |
3e6a0d3c | 3027 | { |
b63534c4 JA |
3028 | return false; |
3029 | } | |
3e6a0d3c | 3030 | #endif |
b63534c4 | 3031 | |
8ef12efe | 3032 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
a1d7c393 | 3033 | { |
f63cf519 | 3034 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) { |
b65c128f | 3035 | kiocb_end_write(req); |
f63cf519 JA |
3036 | fsnotify_modify(req->file); |
3037 | } else { | |
3038 | fsnotify_access(req->file); | |
3039 | } | |
cef216fc | 3040 | if (unlikely(res != req->cqe.res)) { |
9532b99b PB |
3041 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && |
3042 | io_rw_should_reissue(req)) { | |
3043 | req->flags |= REQ_F_REISSUE; | |
8ef12efe | 3044 | return true; |
9532b99b | 3045 | } |
93d2bcd2 | 3046 | req_set_fail(req); |
cef216fc | 3047 | req->cqe.res = res; |
9532b99b | 3048 | } |
8ef12efe JA |
3049 | return false; |
3050 | } | |
3051 | ||
cc8e9ba7 | 3052 | static inline void io_req_task_complete(struct io_kiocb *req, bool *locked) |
8ef12efe | 3053 | { |
cef216fc | 3054 | int res = req->cqe.res; |
126180b9 PB |
3055 | |
3056 | if (*locked) { | |
cc3cec83 | 3057 | io_req_complete_state(req, res, io_put_kbuf(req, 0)); |
fff4e40e | 3058 | io_req_add_compl_list(req); |
126180b9 | 3059 | } else { |
cc3cec83 JA |
3060 | io_req_complete_post(req, res, |
3061 | io_put_kbuf(req, IO_URING_F_UNLOCKED)); | |
126180b9 | 3062 | } |
8ef12efe JA |
3063 | } |
3064 | ||
00f6e68b | 3065 | static void __io_complete_rw(struct io_kiocb *req, long res, |
8ef12efe JA |
3066 | unsigned int issue_flags) |
3067 | { | |
3068 | if (__io_complete_rw_common(req, res)) | |
3069 | return; | |
cef216fc | 3070 | __io_req_complete(req, issue_flags, req->cqe.res, |
cc3cec83 | 3071 | io_put_kbuf(req, issue_flags)); |
ba816ad6 JA |
3072 | } |
3073 | ||
6b19b766 | 3074 | static void io_complete_rw(struct kiocb *kiocb, long res) |
ba816ad6 | 3075 | { |
9adbd45d | 3076 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 3077 | |
8ef12efe JA |
3078 | if (__io_complete_rw_common(req, res)) |
3079 | return; | |
cef216fc | 3080 | req->cqe.res = res; |
8ef12efe | 3081 | req->io_task_work.func = io_req_task_complete; |
f28c240e | 3082 | io_req_task_work_add(req, !!(req->ctx->flags & IORING_SETUP_SQPOLL)); |
2b188cc1 JA |
3083 | } |
3084 | ||
6b19b766 | 3085 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res) |
def596e9 | 3086 | { |
9adbd45d | 3087 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 3088 | |
491381ce JA |
3089 | if (kiocb->ki_flags & IOCB_WRITE) |
3090 | kiocb_end_write(req); | |
cef216fc | 3091 | if (unlikely(res != req->cqe.res)) { |
b66ceaf3 PB |
3092 | if (res == -EAGAIN && io_rw_should_reissue(req)) { |
3093 | req->flags |= REQ_F_REISSUE; | |
3094 | return; | |
9532b99b | 3095 | } |
cef216fc | 3096 | req->cqe.res = res; |
8c130827 | 3097 | } |
bbde017a | 3098 | |
b3fa03fd PB |
3099 | /* order with io_iopoll_complete() checking ->iopoll_completed */ |
3100 | smp_store_release(&req->iopoll_completed, 1); | |
def596e9 JA |
3101 | } |
3102 | ||
3103 | /* | |
3104 | * After the iocb has been issued, it's safe to be found on the poll list. | |
3105 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
f39c8a5b | 3106 | * find it from a io_do_iopoll() thread before the issuer is done |
def596e9 JA |
3107 | * accessing the kiocb cookie. |
3108 | */ | |
9882131c | 3109 | static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags) |
def596e9 JA |
3110 | { |
3111 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 3112 | const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
cb3d8972 PB |
3113 | |
3114 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
3b44b371 | 3115 | if (unlikely(needs_lock)) |
cb3d8972 | 3116 | mutex_lock(&ctx->uring_lock); |
def596e9 JA |
3117 | |
3118 | /* | |
3119 | * Track whether we have multiple files in our lists. This will impact | |
3120 | * how we do polling eventually, not spinning if we're on potentially | |
3121 | * different devices. | |
3122 | */ | |
5eef4e87 | 3123 | if (wq_list_empty(&ctx->iopoll_list)) { |
915b3dde HX |
3124 | ctx->poll_multi_queue = false; |
3125 | } else if (!ctx->poll_multi_queue) { | |
def596e9 JA |
3126 | struct io_kiocb *list_req; |
3127 | ||
5eef4e87 PB |
3128 | list_req = container_of(ctx->iopoll_list.first, struct io_kiocb, |
3129 | comp_list); | |
30da1b45 | 3130 | if (list_req->file != req->file) |
915b3dde | 3131 | ctx->poll_multi_queue = true; |
def596e9 JA |
3132 | } |
3133 | ||
3134 | /* | |
3135 | * For fast devices, IO may have already completed. If it has, add | |
3136 | * it to the front so we find it first. | |
3137 | */ | |
65a6543d | 3138 | if (READ_ONCE(req->iopoll_completed)) |
5eef4e87 | 3139 | wq_list_add_head(&req->comp_list, &ctx->iopoll_list); |
def596e9 | 3140 | else |
5eef4e87 | 3141 | wq_list_add_tail(&req->comp_list, &ctx->iopoll_list); |
bdcd3eab | 3142 | |
3b44b371 | 3143 | if (unlikely(needs_lock)) { |
cb3d8972 PB |
3144 | /* |
3145 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle | |
3146 | * in sq thread task context or in io worker task context. If | |
3147 | * current task context is sq thread, we don't need to check | |
3148 | * whether should wake up sq thread. | |
3149 | */ | |
3150 | if ((ctx->flags & IORING_SETUP_SQPOLL) && | |
3151 | wq_has_sleeper(&ctx->sq_data->wait)) | |
3152 | wake_up(&ctx->sq_data->wait); | |
3153 | ||
3154 | mutex_unlock(&ctx->uring_lock); | |
3155 | } | |
def596e9 JA |
3156 | } |
3157 | ||
4503b767 JA |
3158 | static bool io_bdev_nowait(struct block_device *bdev) |
3159 | { | |
9ba0d0c8 | 3160 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
3161 | } |
3162 | ||
2b188cc1 JA |
3163 | /* |
3164 | * If we tracked the file through the SCM inflight mechanism, we could support | |
3165 | * any file. For now, just ensure that anything potentially problematic is done | |
3166 | * inline. | |
3167 | */ | |
88459b50 | 3168 | static bool __io_file_supports_nowait(struct file *file, umode_t mode) |
2b188cc1 | 3169 | { |
4503b767 | 3170 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
3171 | if (IS_ENABLED(CONFIG_BLOCK) && |
3172 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
3173 | return true; |
3174 | return false; | |
3175 | } | |
976517f1 | 3176 | if (S_ISSOCK(mode)) |
2b188cc1 | 3177 | return true; |
4503b767 | 3178 | if (S_ISREG(mode)) { |
4e7b5671 CH |
3179 | if (IS_ENABLED(CONFIG_BLOCK) && |
3180 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
3181 | file->f_op != &io_uring_fops) |
3182 | return true; | |
3183 | return false; | |
3184 | } | |
2b188cc1 | 3185 | |
c5b85625 JA |
3186 | /* any ->read/write should understand O_NONBLOCK */ |
3187 | if (file->f_flags & O_NONBLOCK) | |
3188 | return true; | |
35645ac3 | 3189 | return file->f_mode & FMODE_NOWAIT; |
2b188cc1 | 3190 | } |
c5b85625 | 3191 | |
88459b50 PB |
3192 | /* |
3193 | * If we tracked the file through the SCM inflight mechanism, we could support | |
3194 | * any file. For now, just ensure that anything potentially problematic is done | |
3195 | * inline. | |
3196 | */ | |
3197 | static unsigned int io_file_get_flags(struct file *file) | |
3198 | { | |
3199 | umode_t mode = file_inode(file)->i_mode; | |
3200 | unsigned int res = 0; | |
af197f50 | 3201 | |
88459b50 PB |
3202 | if (S_ISREG(mode)) |
3203 | res |= FFS_ISREG; | |
3204 | if (__io_file_supports_nowait(file, mode)) | |
3205 | res |= FFS_NOWAIT; | |
3206 | return res; | |
2b188cc1 JA |
3207 | } |
3208 | ||
35645ac3 | 3209 | static inline bool io_file_supports_nowait(struct io_kiocb *req) |
7b29f92d | 3210 | { |
88459b50 | 3211 | return req->flags & REQ_F_SUPPORT_NOWAIT; |
7b29f92d JA |
3212 | } |
3213 | ||
b9a6b8f9 | 3214 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 3215 | { |
9adbd45d | 3216 | struct kiocb *kiocb = &req->rw.kiocb; |
09bb8394 JA |
3217 | unsigned ioprio; |
3218 | int ret; | |
2b188cc1 | 3219 | |
2b188cc1 | 3220 | kiocb->ki_pos = READ_ONCE(sqe->off); |
9adbd45d | 3221 | |
fb27274a PB |
3222 | ioprio = READ_ONCE(sqe->ioprio); |
3223 | if (ioprio) { | |
3224 | ret = ioprio_check_cap(ioprio); | |
3225 | if (ret) | |
3226 | return ret; | |
3227 | ||
3228 | kiocb->ki_ioprio = ioprio; | |
3229 | } else { | |
3230 | kiocb->ki_ioprio = get_current_ioprio(); | |
eae071c9 PB |
3231 | } |
3232 | ||
578c0ee2 | 3233 | req->imu = NULL; |
3529d8c2 JA |
3234 | req->rw.addr = READ_ONCE(sqe->addr); |
3235 | req->rw.len = READ_ONCE(sqe->len); | |
584b0180 | 3236 | req->rw.flags = READ_ONCE(sqe->rw_flags); |
4f4eeba8 | 3237 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 3238 | return 0; |
2b188cc1 JA |
3239 | } |
3240 | ||
3241 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
3242 | { | |
3243 | switch (ret) { | |
3244 | case -EIOCBQUEUED: | |
3245 | break; | |
3246 | case -ERESTARTSYS: | |
3247 | case -ERESTARTNOINTR: | |
3248 | case -ERESTARTNOHAND: | |
3249 | case -ERESTART_RESTARTBLOCK: | |
3250 | /* | |
3251 | * We can't just restart the syscall, since previously | |
3252 | * submitted sqes may already be in progress. Just fail this | |
3253 | * IO with EINTR. | |
3254 | */ | |
3255 | ret = -EINTR; | |
df561f66 | 3256 | fallthrough; |
2b188cc1 | 3257 | default: |
6b19b766 | 3258 | kiocb->ki_complete(kiocb, ret); |
2b188cc1 JA |
3259 | } |
3260 | } | |
3261 | ||
b4aec400 | 3262 | static inline loff_t *io_kiocb_update_pos(struct io_kiocb *req) |
d34e1e5b DY |
3263 | { |
3264 | struct kiocb *kiocb = &req->rw.kiocb; | |
3265 | ||
6f83ab22 JA |
3266 | if (kiocb->ki_pos != -1) |
3267 | return &kiocb->ki_pos; | |
3268 | ||
3269 | if (!(req->file->f_mode & FMODE_STREAM)) { | |
3270 | req->flags |= REQ_F_CUR_POS; | |
3271 | kiocb->ki_pos = req->file->f_pos; | |
3272 | return &kiocb->ki_pos; | |
d34e1e5b | 3273 | } |
6f83ab22 JA |
3274 | |
3275 | kiocb->ki_pos = 0; | |
3276 | return NULL; | |
d34e1e5b DY |
3277 | } |
3278 | ||
2ea537ca | 3279 | static void kiocb_done(struct io_kiocb *req, ssize_t ret, |
889fca73 | 3280 | unsigned int issue_flags) |
ba816ad6 | 3281 | { |
e8c2bc1f | 3282 | struct io_async_rw *io = req->async_data; |
ba04291e | 3283 | |
227c0c96 | 3284 | /* add previously done IO, if any */ |
d886e185 | 3285 | if (req_has_async_data(req) && io->bytes_done > 0) { |
227c0c96 | 3286 | if (ret < 0) |
e8c2bc1f | 3287 | ret = io->bytes_done; |
227c0c96 | 3288 | else |
e8c2bc1f | 3289 | ret += io->bytes_done; |
227c0c96 JA |
3290 | } |
3291 | ||
ba04291e | 3292 | if (req->flags & REQ_F_CUR_POS) |
2ea537ca PB |
3293 | req->file->f_pos = req->rw.kiocb.ki_pos; |
3294 | if (ret >= 0 && (req->rw.kiocb.ki_complete == io_complete_rw)) | |
00f6e68b | 3295 | __io_complete_rw(req, ret, issue_flags); |
ba816ad6 | 3296 | else |
2ea537ca | 3297 | io_rw_done(&req->rw.kiocb, ret); |
97284637 | 3298 | |
b66ceaf3 | 3299 | if (req->flags & REQ_F_REISSUE) { |
97284637 | 3300 | req->flags &= ~REQ_F_REISSUE; |
b91ef187 | 3301 | if (io_resubmit_prep(req)) |
773af691 | 3302 | io_req_task_queue_reissue(req); |
b91ef187 PB |
3303 | else |
3304 | io_req_task_queue_fail(req, ret); | |
97284637 | 3305 | } |
ba816ad6 JA |
3306 | } |
3307 | ||
eae071c9 PB |
3308 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
3309 | struct io_mapped_ubuf *imu) | |
edafccee | 3310 | { |
9adbd45d | 3311 | size_t len = req->rw.len; |
75769e3f | 3312 | u64 buf_end, buf_addr = req->rw.addr; |
edafccee | 3313 | size_t offset; |
edafccee | 3314 | |
75769e3f | 3315 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
edafccee JA |
3316 | return -EFAULT; |
3317 | /* not inside the mapped region */ | |
4751f53d | 3318 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
edafccee JA |
3319 | return -EFAULT; |
3320 | ||
3321 | /* | |
3322 | * May not be a start of buffer, set size appropriately | |
3323 | * and advance us to the beginning. | |
3324 | */ | |
3325 | offset = buf_addr - imu->ubuf; | |
3326 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
3327 | |
3328 | if (offset) { | |
3329 | /* | |
3330 | * Don't use iov_iter_advance() here, as it's really slow for | |
3331 | * using the latter parts of a big fixed buffer - it iterates | |
3332 | * over each segment manually. We can cheat a bit here, because | |
3333 | * we know that: | |
3334 | * | |
3335 | * 1) it's a BVEC iter, we set it up | |
3336 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
3337 | * first and last bvec | |
3338 | * | |
3339 | * So just find our index, and adjust the iterator afterwards. | |
3340 | * If the offset is within the first bvec (or the whole first | |
3341 | * bvec, just use iov_iter_advance(). This makes it easier | |
3342 | * since we can just skip the first segment, which may not | |
3343 | * be PAGE_SIZE aligned. | |
3344 | */ | |
3345 | const struct bio_vec *bvec = imu->bvec; | |
3346 | ||
3347 | if (offset <= bvec->bv_len) { | |
3348 | iov_iter_advance(iter, offset); | |
3349 | } else { | |
3350 | unsigned long seg_skip; | |
3351 | ||
3352 | /* skip first vec */ | |
3353 | offset -= bvec->bv_len; | |
3354 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
3355 | ||
3356 | iter->bvec = bvec + seg_skip; | |
3357 | iter->nr_segs -= seg_skip; | |
99c79f66 | 3358 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 3359 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
3360 | } |
3361 | } | |
3362 | ||
847595de | 3363 | return 0; |
edafccee JA |
3364 | } |
3365 | ||
5106dd6e JA |
3366 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
3367 | unsigned int issue_flags) | |
eae071c9 | 3368 | { |
eae071c9 PB |
3369 | struct io_mapped_ubuf *imu = req->imu; |
3370 | u16 index, buf_index = req->buf_index; | |
3371 | ||
3372 | if (likely(!imu)) { | |
578c0ee2 PB |
3373 | struct io_ring_ctx *ctx = req->ctx; |
3374 | ||
eae071c9 PB |
3375 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
3376 | return -EFAULT; | |
5106dd6e | 3377 | io_req_set_rsrc_node(req, ctx, issue_flags); |
eae071c9 PB |
3378 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
3379 | imu = READ_ONCE(ctx->user_bufs[index]); | |
3380 | req->imu = imu; | |
3381 | } | |
3382 | return __io_import_fixed(req, rw, iter, imu); | |
3383 | } | |
3384 | ||
dbc7d452 JA |
3385 | static void io_buffer_add_list(struct io_ring_ctx *ctx, |
3386 | struct io_buffer_list *bl, unsigned int bgid) | |
3387 | { | |
3388 | struct list_head *list; | |
3389 | ||
3390 | list = &ctx->io_buffers[hash_32(bgid, IO_BUFFERS_HASH_BITS)]; | |
3391 | INIT_LIST_HEAD(&bl->buf_list); | |
3392 | bl->bgid = bgid; | |
3393 | list_add(&bl->list, list); | |
3394 | } | |
3395 | ||
bcda7baa | 3396 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
51aac424 | 3397 | int bgid, unsigned int issue_flags) |
bcda7baa | 3398 | { |
30d51dd4 | 3399 | struct io_buffer *kbuf = req->kbuf; |
dbc7d452 JA |
3400 | struct io_ring_ctx *ctx = req->ctx; |
3401 | struct io_buffer_list *bl; | |
bcda7baa JA |
3402 | |
3403 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3404 | return kbuf; | |
3405 | ||
f8929630 | 3406 | io_ring_submit_lock(req->ctx, issue_flags); |
bcda7baa | 3407 | |
dbc7d452 JA |
3408 | bl = io_buffer_get_list(ctx, bgid); |
3409 | if (bl && !list_empty(&bl->buf_list)) { | |
3410 | kbuf = list_first_entry(&bl->buf_list, struct io_buffer, list); | |
3411 | list_del(&kbuf->list); | |
bcda7baa JA |
3412 | if (*len > kbuf->len) |
3413 | *len = kbuf->len; | |
30d51dd4 PB |
3414 | req->flags |= REQ_F_BUFFER_SELECTED; |
3415 | req->kbuf = kbuf; | |
bcda7baa JA |
3416 | } else { |
3417 | kbuf = ERR_PTR(-ENOBUFS); | |
3418 | } | |
3419 | ||
f8929630 | 3420 | io_ring_submit_unlock(req->ctx, issue_flags); |
bcda7baa JA |
3421 | return kbuf; |
3422 | } | |
3423 | ||
4d954c25 | 3424 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
51aac424 | 3425 | unsigned int issue_flags) |
4d954c25 JA |
3426 | { |
3427 | struct io_buffer *kbuf; | |
4f4eeba8 | 3428 | u16 bgid; |
4d954c25 | 3429 | |
4f4eeba8 | 3430 | bgid = req->buf_index; |
51aac424 | 3431 | kbuf = io_buffer_select(req, len, bgid, issue_flags); |
4d954c25 JA |
3432 | if (IS_ERR(kbuf)) |
3433 | return kbuf; | |
4d954c25 JA |
3434 | return u64_to_user_ptr(kbuf->addr); |
3435 | } | |
3436 | ||
3437 | #ifdef CONFIG_COMPAT | |
3438 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3439 | unsigned int issue_flags) |
4d954c25 JA |
3440 | { |
3441 | struct compat_iovec __user *uiov; | |
3442 | compat_ssize_t clen; | |
3443 | void __user *buf; | |
3444 | ssize_t len; | |
3445 | ||
3446 | uiov = u64_to_user_ptr(req->rw.addr); | |
3447 | if (!access_ok(uiov, sizeof(*uiov))) | |
3448 | return -EFAULT; | |
3449 | if (__get_user(clen, &uiov->iov_len)) | |
3450 | return -EFAULT; | |
3451 | if (clen < 0) | |
3452 | return -EINVAL; | |
3453 | ||
3454 | len = clen; | |
51aac424 | 3455 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3456 | if (IS_ERR(buf)) |
3457 | return PTR_ERR(buf); | |
3458 | iov[0].iov_base = buf; | |
3459 | iov[0].iov_len = (compat_size_t) len; | |
3460 | return 0; | |
3461 | } | |
3462 | #endif | |
3463 | ||
3464 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3465 | unsigned int issue_flags) |
4d954c25 JA |
3466 | { |
3467 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3468 | void __user *buf; | |
3469 | ssize_t len; | |
3470 | ||
3471 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3472 | return -EFAULT; | |
3473 | ||
3474 | len = iov[0].iov_len; | |
3475 | if (len < 0) | |
3476 | return -EINVAL; | |
51aac424 | 3477 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3478 | if (IS_ERR(buf)) |
3479 | return PTR_ERR(buf); | |
3480 | iov[0].iov_base = buf; | |
3481 | iov[0].iov_len = len; | |
3482 | return 0; | |
3483 | } | |
3484 | ||
3485 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3486 | unsigned int issue_flags) |
4d954c25 | 3487 | { |
dddb3e26 | 3488 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
30d51dd4 | 3489 | struct io_buffer *kbuf = req->kbuf; |
dddb3e26 | 3490 | |
dddb3e26 JA |
3491 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
3492 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3493 | return 0; |
dddb3e26 | 3494 | } |
dd201662 | 3495 | if (req->rw.len != 1) |
4d954c25 JA |
3496 | return -EINVAL; |
3497 | ||
3498 | #ifdef CONFIG_COMPAT | |
3499 | if (req->ctx->compat) | |
51aac424 | 3500 | return io_compat_import(req, iov, issue_flags); |
4d954c25 JA |
3501 | #endif |
3502 | ||
51aac424 | 3503 | return __io_iov_buffer_select(req, iov, issue_flags); |
4d954c25 JA |
3504 | } |
3505 | ||
caa8fe6e PB |
3506 | static struct iovec *__io_import_iovec(int rw, struct io_kiocb *req, |
3507 | struct io_rw_state *s, | |
3508 | unsigned int issue_flags) | |
2b188cc1 | 3509 | { |
5e49c973 | 3510 | struct iov_iter *iter = &s->iter; |
847595de | 3511 | u8 opcode = req->opcode; |
caa8fe6e | 3512 | struct iovec *iovec; |
d1d681b0 PB |
3513 | void __user *buf; |
3514 | size_t sqe_len; | |
4d954c25 | 3515 | ssize_t ret; |
edafccee | 3516 | |
f3251183 | 3517 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
5106dd6e | 3518 | ret = io_import_fixed(req, rw, iter, issue_flags); |
f3251183 PB |
3519 | if (ret) |
3520 | return ERR_PTR(ret); | |
3521 | return NULL; | |
3522 | } | |
2b188cc1 | 3523 | |
bcda7baa | 3524 | /* buffer index only valid with fixed read/write, or buffer select */ |
d1d681b0 | 3525 | if (unlikely(req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))) |
caa8fe6e | 3526 | return ERR_PTR(-EINVAL); |
9adbd45d | 3527 | |
d1d681b0 PB |
3528 | buf = u64_to_user_ptr(req->rw.addr); |
3529 | sqe_len = req->rw.len; | |
9adbd45d | 3530 | |
3a6820f2 | 3531 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3532 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 3533 | buf = io_rw_buffer_select(req, &sqe_len, issue_flags); |
867a23ea | 3534 | if (IS_ERR(buf)) |
898df244 | 3535 | return ERR_CAST(buf); |
3f9d6441 | 3536 | req->rw.len = sqe_len; |
bcda7baa JA |
3537 | } |
3538 | ||
5e49c973 | 3539 | ret = import_single_range(rw, buf, sqe_len, s->fast_iov, iter); |
f3251183 PB |
3540 | if (ret) |
3541 | return ERR_PTR(ret); | |
3542 | return NULL; | |
3a6820f2 JA |
3543 | } |
3544 | ||
caa8fe6e | 3545 | iovec = s->fast_iov; |
4d954c25 | 3546 | if (req->flags & REQ_F_BUFFER_SELECT) { |
caa8fe6e | 3547 | ret = io_iov_buffer_select(req, iovec, issue_flags); |
f3251183 PB |
3548 | if (ret) |
3549 | return ERR_PTR(ret); | |
3550 | iov_iter_init(iter, rw, iovec, 1, iovec->iov_len); | |
3551 | return NULL; | |
4d954c25 JA |
3552 | } |
3553 | ||
caa8fe6e | 3554 | ret = __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, &iovec, iter, |
89cd35c5 | 3555 | req->ctx->compat); |
caa8fe6e PB |
3556 | if (unlikely(ret < 0)) |
3557 | return ERR_PTR(ret); | |
3558 | return iovec; | |
2b188cc1 JA |
3559 | } |
3560 | ||
5e49c973 PB |
3561 | static inline int io_import_iovec(int rw, struct io_kiocb *req, |
3562 | struct iovec **iovec, struct io_rw_state *s, | |
3563 | unsigned int issue_flags) | |
3564 | { | |
caa8fe6e PB |
3565 | *iovec = __io_import_iovec(rw, req, s, issue_flags); |
3566 | if (unlikely(IS_ERR(*iovec))) | |
3567 | return PTR_ERR(*iovec); | |
5e49c973 | 3568 | |
5e49c973 | 3569 | iov_iter_save_state(&s->iter, &s->iter_state); |
caa8fe6e | 3570 | return 0; |
2b188cc1 JA |
3571 | } |
3572 | ||
0fef9483 JA |
3573 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3574 | { | |
5b09e37e | 3575 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3576 | } |
3577 | ||
31b51510 | 3578 | /* |
32960613 JA |
3579 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3580 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3581 | */ |
4017eb91 | 3582 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3583 | { |
4017eb91 JA |
3584 | struct kiocb *kiocb = &req->rw.kiocb; |
3585 | struct file *file = req->file; | |
32960613 | 3586 | ssize_t ret = 0; |
af9c45ec | 3587 | loff_t *ppos; |
32960613 JA |
3588 | |
3589 | /* | |
3590 | * Don't support polled IO through this interface, and we can't | |
3591 | * support non-blocking either. For the latter, this just causes | |
3592 | * the kiocb to be handled from an async context. | |
3593 | */ | |
3594 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3595 | return -EOPNOTSUPP; | |
35645ac3 PB |
3596 | if ((kiocb->ki_flags & IOCB_NOWAIT) && |
3597 | !(kiocb->ki_filp->f_flags & O_NONBLOCK)) | |
32960613 JA |
3598 | return -EAGAIN; |
3599 | ||
af9c45ec DY |
3600 | ppos = io_kiocb_ppos(kiocb); |
3601 | ||
32960613 | 3602 | while (iov_iter_count(iter)) { |
311ae9e1 | 3603 | struct iovec iovec; |
32960613 JA |
3604 | ssize_t nr; |
3605 | ||
311ae9e1 PB |
3606 | if (!iov_iter_is_bvec(iter)) { |
3607 | iovec = iov_iter_iovec(iter); | |
3608 | } else { | |
4017eb91 JA |
3609 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3610 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3611 | } |
3612 | ||
32960613 JA |
3613 | if (rw == READ) { |
3614 | nr = file->f_op->read(file, iovec.iov_base, | |
af9c45ec | 3615 | iovec.iov_len, ppos); |
32960613 JA |
3616 | } else { |
3617 | nr = file->f_op->write(file, iovec.iov_base, | |
af9c45ec | 3618 | iovec.iov_len, ppos); |
32960613 JA |
3619 | } |
3620 | ||
3621 | if (nr < 0) { | |
3622 | if (!ret) | |
3623 | ret = nr; | |
3624 | break; | |
3625 | } | |
5e929367 | 3626 | ret += nr; |
16c8d2df JA |
3627 | if (!iov_iter_is_bvec(iter)) { |
3628 | iov_iter_advance(iter, nr); | |
3629 | } else { | |
16c8d2df | 3630 | req->rw.addr += nr; |
5e929367 JA |
3631 | req->rw.len -= nr; |
3632 | if (!req->rw.len) | |
3633 | break; | |
16c8d2df | 3634 | } |
32960613 JA |
3635 | if (nr != iovec.iov_len) |
3636 | break; | |
32960613 JA |
3637 | } |
3638 | ||
3639 | return ret; | |
3640 | } | |
3641 | ||
ff6165b2 JA |
3642 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3643 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3644 | { |
e8c2bc1f | 3645 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3646 | |
538941e2 | 3647 | memcpy(&rw->s.iter, iter, sizeof(*iter)); |
afb87658 | 3648 | rw->free_iovec = iovec; |
227c0c96 | 3649 | rw->bytes_done = 0; |
ff6165b2 | 3650 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3651 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3652 | return; |
b64e3444 | 3653 | if (!iovec) { |
ff6165b2 JA |
3654 | unsigned iov_off = 0; |
3655 | ||
538941e2 | 3656 | rw->s.iter.iov = rw->s.fast_iov; |
ff6165b2 JA |
3657 | if (iter->iov != fast_iov) { |
3658 | iov_off = iter->iov - fast_iov; | |
538941e2 | 3659 | rw->s.iter.iov += iov_off; |
ff6165b2 | 3660 | } |
538941e2 PB |
3661 | if (rw->s.fast_iov != fast_iov) |
3662 | memcpy(rw->s.fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3663 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3664 | } else { |
3665 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3666 | } |
3667 | } | |
3668 | ||
8d4af685 | 3669 | static inline bool io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3670 | { |
e8c2bc1f JA |
3671 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3672 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
d886e185 PB |
3673 | if (req->async_data) { |
3674 | req->flags |= REQ_F_ASYNC_DATA; | |
3675 | return false; | |
3676 | } | |
3677 | return true; | |
3d9932a8 XW |
3678 | } |
3679 | ||
ff6165b2 | 3680 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
c88598a9 | 3681 | struct io_rw_state *s, bool force) |
b7bb4f7d | 3682 | { |
26f0505a | 3683 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
74566df3 | 3684 | return 0; |
d886e185 | 3685 | if (!req_has_async_data(req)) { |
cd658695 JA |
3686 | struct io_async_rw *iorw; |
3687 | ||
6cb78689 | 3688 | if (io_alloc_async_data(req)) { |
6bf985dc | 3689 | kfree(iovec); |
5d204bcf | 3690 | return -ENOMEM; |
6bf985dc | 3691 | } |
b7bb4f7d | 3692 | |
c88598a9 | 3693 | io_req_map_rw(req, iovec, s->fast_iov, &s->iter); |
cd658695 JA |
3694 | iorw = req->async_data; |
3695 | /* we've copied and mapped the iter, ensure state is saved */ | |
538941e2 | 3696 | iov_iter_save_state(&iorw->s.iter, &iorw->s.iter_state); |
5d204bcf | 3697 | } |
b7bb4f7d | 3698 | return 0; |
f67676d1 JA |
3699 | } |
3700 | ||
73debe68 | 3701 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3702 | { |
e8c2bc1f | 3703 | struct io_async_rw *iorw = req->async_data; |
5e49c973 | 3704 | struct iovec *iov; |
847595de | 3705 | int ret; |
c3e330a4 | 3706 | |
51aac424 | 3707 | /* submission path, ->uring_lock should already be taken */ |
3b44b371 | 3708 | ret = io_import_iovec(rw, req, &iov, &iorw->s, 0); |
c3e330a4 PB |
3709 | if (unlikely(ret < 0)) |
3710 | return ret; | |
3711 | ||
ab0b196c PB |
3712 | iorw->bytes_done = 0; |
3713 | iorw->free_iovec = iov; | |
3714 | if (iov) | |
3715 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3716 | return 0; |
3717 | } | |
3718 | ||
c1dd91d1 | 3719 | /* |
ffdc8dab | 3720 | * This is our waitqueue callback handler, registered through __folio_lock_async() |
c1dd91d1 JA |
3721 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
3722 | * This gets called when the page is unlocked, and we generally expect that to | |
3723 | * happen when the page IO is completed and the page is now uptodate. This will | |
3724 | * queue a task_work based retry of the operation, attempting to copy the data | |
3725 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3726 | * do a thread based blocking retry of the operation. That's the unexpected | |
3727 | * slow path. | |
3728 | */ | |
bcf5a063 JA |
3729 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3730 | int sync, void *arg) | |
3731 | { | |
3732 | struct wait_page_queue *wpq; | |
3733 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3734 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3735 | |
3736 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3737 | ||
cdc8fcb4 LT |
3738 | if (!wake_page_match(wpq, key)) |
3739 | return 0; | |
3740 | ||
c8d317aa | 3741 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 | 3742 | list_del_init(&wait->entry); |
921b9054 | 3743 | io_req_task_queue(req); |
bcf5a063 JA |
3744 | return 1; |
3745 | } | |
3746 | ||
c1dd91d1 JA |
3747 | /* |
3748 | * This controls whether a given IO request should be armed for async page | |
3749 | * based retry. If we return false here, the request is handed to the async | |
3750 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3751 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3752 | * will either succeed because the page is now uptodate and unlocked, or it | |
3753 | * will register a callback when the page is unlocked at IO completion. Through | |
3754 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3755 | * That retry will attempt the buffered read again. The retry will generally | |
3756 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3757 | * async worker threads for a blocking retry. | |
3758 | */ | |
227c0c96 | 3759 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3760 | { |
e8c2bc1f JA |
3761 | struct io_async_rw *rw = req->async_data; |
3762 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3763 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3764 | |
bcf5a063 JA |
3765 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3766 | if (req->flags & REQ_F_NOWAIT) | |
3767 | return false; | |
f67676d1 | 3768 | |
227c0c96 | 3769 | /* Only for buffered IO */ |
3b2a4439 | 3770 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3771 | return false; |
3b2a4439 | 3772 | |
bcf5a063 JA |
3773 | /* |
3774 | * just use poll if we can, and don't attempt if the fs doesn't | |
3775 | * support callback based unlocks | |
3776 | */ | |
3777 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3778 | return false; | |
f67676d1 | 3779 | |
3b2a4439 JA |
3780 | wait->wait.func = io_async_buf_func; |
3781 | wait->wait.private = req; | |
3782 | wait->wait.flags = 0; | |
3783 | INIT_LIST_HEAD(&wait->wait.entry); | |
3784 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3785 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3786 | kiocb->ki_waitq = wait; |
3b2a4439 | 3787 | return true; |
bcf5a063 JA |
3788 | } |
3789 | ||
aeab9506 | 3790 | static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
bcf5a063 | 3791 | { |
607b6fb8 | 3792 | if (likely(req->file->f_op->read_iter)) |
bcf5a063 | 3793 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
2dd2111d | 3794 | else if (req->file->f_op->read) |
4017eb91 | 3795 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3796 | else |
3797 | return -EINVAL; | |
f67676d1 JA |
3798 | } |
3799 | ||
7db30437 ML |
3800 | static bool need_read_all(struct io_kiocb *req) |
3801 | { | |
3802 | return req->flags & REQ_F_ISREG || | |
3803 | S_ISBLK(file_inode(req->file)->i_mode); | |
3804 | } | |
3805 | ||
584b0180 JA |
3806 | static int io_rw_init_file(struct io_kiocb *req, fmode_t mode) |
3807 | { | |
3808 | struct kiocb *kiocb = &req->rw.kiocb; | |
3809 | struct io_ring_ctx *ctx = req->ctx; | |
3810 | struct file *file = req->file; | |
3811 | int ret; | |
3812 | ||
3813 | if (unlikely(!file || !(file->f_mode & mode))) | |
3814 | return -EBADF; | |
3815 | ||
3816 | if (!io_req_ffs_set(req)) | |
3817 | req->flags |= io_file_get_flags(file) << REQ_F_SUPPORT_NOWAIT_BIT; | |
3818 | ||
3819 | kiocb->ki_flags = iocb_flags(file); | |
3820 | ret = kiocb_set_rw_flags(kiocb, req->rw.flags); | |
3821 | if (unlikely(ret)) | |
3822 | return ret; | |
3823 | ||
3824 | /* | |
3825 | * If the file is marked O_NONBLOCK, still allow retry for it if it | |
3826 | * supports async. Otherwise it's impossible to use O_NONBLOCK files | |
3827 | * reliably. If not, or it IOCB_NOWAIT is set, don't retry. | |
3828 | */ | |
3829 | if ((kiocb->ki_flags & IOCB_NOWAIT) || | |
3830 | ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req))) | |
3831 | req->flags |= REQ_F_NOWAIT; | |
3832 | ||
3833 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
3834 | if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll) | |
3835 | return -EOPNOTSUPP; | |
3836 | ||
3837 | kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE; | |
3838 | kiocb->ki_complete = io_complete_rw_iopoll; | |
3839 | req->iopoll_completed = 0; | |
3840 | } else { | |
3841 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3842 | return -EINVAL; | |
3843 | kiocb->ki_complete = io_complete_rw; | |
3844 | } | |
3845 | ||
3846 | return 0; | |
3847 | } | |
3848 | ||
889fca73 | 3849 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 3850 | { |
607b6fb8 | 3851 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 3852 | struct iovec *iovec; |
9adbd45d | 3853 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 3854 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
d886e185 | 3855 | struct io_async_rw *rw; |
cd658695 | 3856 | ssize_t ret, ret2; |
b4aec400 | 3857 | loff_t *ppos; |
ff6165b2 | 3858 | |
607b6fb8 PB |
3859 | if (!req_has_async_data(req)) { |
3860 | ret = io_import_iovec(READ, req, &iovec, s, issue_flags); | |
3861 | if (unlikely(ret < 0)) | |
3862 | return ret; | |
3863 | } else { | |
2be2eb02 JA |
3864 | /* |
3865 | * Safe and required to re-import if we're using provided | |
3866 | * buffers, as we dropped the selected one before retry. | |
3867 | */ | |
3868 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
3869 | ret = io_import_iovec(READ, req, &iovec, s, issue_flags); | |
3870 | if (unlikely(ret < 0)) | |
3871 | return ret; | |
3872 | } | |
3873 | ||
d886e185 | 3874 | rw = req->async_data; |
c88598a9 | 3875 | s = &rw->s; |
cd658695 JA |
3876 | /* |
3877 | * We come here from an earlier attempt, restore our state to | |
3878 | * match in case it doesn't. It's cheap enough that we don't | |
3879 | * need to make this conditional. | |
3880 | */ | |
c88598a9 | 3881 | iov_iter_restore(&s->iter, &s->iter_state); |
2846c481 | 3882 | iovec = NULL; |
2846c481 | 3883 | } |
584b0180 | 3884 | ret = io_rw_init_file(req, FMODE_READ); |
323b190b JA |
3885 | if (unlikely(ret)) { |
3886 | kfree(iovec); | |
584b0180 | 3887 | return ret; |
323b190b | 3888 | } |
cef216fc | 3889 | req->cqe.res = iov_iter_count(&s->iter); |
2b188cc1 | 3890 | |
607b6fb8 PB |
3891 | if (force_nonblock) { |
3892 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 3893 | if (unlikely(!io_file_supports_nowait(req))) { |
607b6fb8 PB |
3894 | ret = io_setup_async_rw(req, iovec, s, true); |
3895 | return ret ?: -EAGAIN; | |
3896 | } | |
a88fc400 | 3897 | kiocb->ki_flags |= IOCB_NOWAIT; |
607b6fb8 PB |
3898 | } else { |
3899 | /* Ensure we clear previously set non-block flag */ | |
3900 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
6713e7a6 | 3901 | } |
9e645e11 | 3902 | |
b4aec400 | 3903 | ppos = io_kiocb_update_pos(req); |
d34e1e5b | 3904 | |
cef216fc | 3905 | ret = rw_verify_area(READ, req->file, ppos, req->cqe.res); |
5ea5dd45 PB |
3906 | if (unlikely(ret)) { |
3907 | kfree(iovec); | |
3908 | return ret; | |
3909 | } | |
2b188cc1 | 3910 | |
c88598a9 | 3911 | ret = io_iter_do_read(req, &s->iter); |
32960613 | 3912 | |
230d50d4 | 3913 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
6ad7f233 | 3914 | req->flags &= ~REQ_F_REISSUE; |
9af177ee JA |
3915 | /* if we can poll, just do that */ |
3916 | if (req->opcode == IORING_OP_READ && file_can_poll(req->file)) | |
3917 | return -EAGAIN; | |
eefdf30f JA |
3918 | /* IOPOLL retry should happen for io-wq threads */ |
3919 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3920 | goto done; |
75c668cd PB |
3921 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3922 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3923 | goto done; |
f38c7e3a | 3924 | ret = 0; |
230d50d4 JA |
3925 | } else if (ret == -EIOCBQUEUED) { |
3926 | goto out_free; | |
cef216fc | 3927 | } else if (ret == req->cqe.res || ret <= 0 || !force_nonblock || |
7db30437 | 3928 | (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) { |
7335e3bf | 3929 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3930 | goto done; |
227c0c96 JA |
3931 | } |
3932 | ||
cd658695 JA |
3933 | /* |
3934 | * Don't depend on the iter state matching what was consumed, or being | |
3935 | * untouched in case of error. Restore it and we'll advance it | |
3936 | * manually if we need to. | |
3937 | */ | |
c88598a9 | 3938 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 3939 | |
c88598a9 | 3940 | ret2 = io_setup_async_rw(req, iovec, s, true); |
6bf985dc PB |
3941 | if (ret2) |
3942 | return ret2; | |
3943 | ||
fe1cdd55 | 3944 | iovec = NULL; |
e8c2bc1f | 3945 | rw = req->async_data; |
c88598a9 | 3946 | s = &rw->s; |
cd658695 JA |
3947 | /* |
3948 | * Now use our persistent iterator and state, if we aren't already. | |
3949 | * We've restored and mapped the iter to match. | |
3950 | */ | |
227c0c96 | 3951 | |
b23df91b | 3952 | do { |
cd658695 JA |
3953 | /* |
3954 | * We end up here because of a partial read, either from | |
3955 | * above or inside this loop. Advance the iter by the bytes | |
3956 | * that were consumed. | |
3957 | */ | |
c88598a9 PB |
3958 | iov_iter_advance(&s->iter, ret); |
3959 | if (!iov_iter_count(&s->iter)) | |
cd658695 | 3960 | break; |
b23df91b | 3961 | rw->bytes_done += ret; |
c88598a9 | 3962 | iov_iter_save_state(&s->iter, &s->iter_state); |
cd658695 | 3963 | |
b23df91b PB |
3964 | /* if we can retry, do so with the callbacks armed */ |
3965 | if (!io_rw_should_retry(req)) { | |
3966 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3967 | return -EAGAIN; | |
3968 | } | |
3969 | ||
3970 | /* | |
3971 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3972 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3973 | * desired page gets unlocked. We can also get a partial read | |
3974 | * here, and if we do, then just retry at the new offset. | |
3975 | */ | |
c88598a9 | 3976 | ret = io_iter_do_read(req, &s->iter); |
b23df91b PB |
3977 | if (ret == -EIOCBQUEUED) |
3978 | return 0; | |
227c0c96 | 3979 | /* we got some bytes, but not all. retry. */ |
b5b0ecb7 | 3980 | kiocb->ki_flags &= ~IOCB_WAITQ; |
c88598a9 | 3981 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 3982 | } while (ret > 0); |
227c0c96 | 3983 | done: |
2ea537ca | 3984 | kiocb_done(req, ret, issue_flags); |
fe1cdd55 PB |
3985 | out_free: |
3986 | /* it's faster to check here then delegate to kfree */ | |
3987 | if (iovec) | |
3988 | kfree(iovec); | |
5ea5dd45 | 3989 | return 0; |
2b188cc1 JA |
3990 | } |
3991 | ||
889fca73 | 3992 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 3993 | { |
607b6fb8 | 3994 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 3995 | struct iovec *iovec; |
9adbd45d | 3996 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 3997 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
cd658695 | 3998 | ssize_t ret, ret2; |
b4aec400 | 3999 | loff_t *ppos; |
2b188cc1 | 4000 | |
607b6fb8 | 4001 | if (!req_has_async_data(req)) { |
5e49c973 PB |
4002 | ret = io_import_iovec(WRITE, req, &iovec, s, issue_flags); |
4003 | if (unlikely(ret < 0)) | |
2846c481 | 4004 | return ret; |
607b6fb8 PB |
4005 | } else { |
4006 | struct io_async_rw *rw = req->async_data; | |
4007 | ||
4008 | s = &rw->s; | |
4009 | iov_iter_restore(&s->iter, &s->iter_state); | |
2846c481 | 4010 | iovec = NULL; |
2846c481 | 4011 | } |
584b0180 | 4012 | ret = io_rw_init_file(req, FMODE_WRITE); |
323b190b JA |
4013 | if (unlikely(ret)) { |
4014 | kfree(iovec); | |
584b0180 | 4015 | return ret; |
323b190b | 4016 | } |
cef216fc | 4017 | req->cqe.res = iov_iter_count(&s->iter); |
2b188cc1 | 4018 | |
607b6fb8 PB |
4019 | if (force_nonblock) { |
4020 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 4021 | if (unlikely(!io_file_supports_nowait(req))) |
607b6fb8 | 4022 | goto copy_iov; |
fd6c2e4c | 4023 | |
607b6fb8 PB |
4024 | /* file path doesn't support NOWAIT for non-direct_IO */ |
4025 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
4026 | (req->flags & REQ_F_ISREG)) | |
4027 | goto copy_iov; | |
31b51510 | 4028 | |
607b6fb8 PB |
4029 | kiocb->ki_flags |= IOCB_NOWAIT; |
4030 | } else { | |
4031 | /* Ensure we clear previously set non-block flag */ | |
4032 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
4033 | } | |
31b51510 | 4034 | |
b4aec400 | 4035 | ppos = io_kiocb_update_pos(req); |
d34e1e5b | 4036 | |
cef216fc | 4037 | ret = rw_verify_area(WRITE, req->file, ppos, req->cqe.res); |
fa15bafb PB |
4038 | if (unlikely(ret)) |
4039 | goto out_free; | |
4ed734b0 | 4040 | |
fa15bafb PB |
4041 | /* |
4042 | * Open-code file_start_write here to grab freeze protection, | |
4043 | * which will be released by another thread in | |
4044 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
4045 | * released so that it doesn't complain about the held lock when | |
4046 | * we return to userspace. | |
4047 | */ | |
4048 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 4049 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
4050 | __sb_writers_release(file_inode(req->file)->i_sb, |
4051 | SB_FREEZE_WRITE); | |
4052 | } | |
4053 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 4054 | |
35645ac3 | 4055 | if (likely(req->file->f_op->write_iter)) |
c88598a9 | 4056 | ret2 = call_write_iter(req->file, kiocb, &s->iter); |
2dd2111d | 4057 | else if (req->file->f_op->write) |
c88598a9 | 4058 | ret2 = loop_rw_iter(WRITE, req, &s->iter); |
2dd2111d GH |
4059 | else |
4060 | ret2 = -EINVAL; | |
4ed734b0 | 4061 | |
6ad7f233 PB |
4062 | if (req->flags & REQ_F_REISSUE) { |
4063 | req->flags &= ~REQ_F_REISSUE; | |
230d50d4 | 4064 | ret2 = -EAGAIN; |
6ad7f233 | 4065 | } |
230d50d4 | 4066 | |
fa15bafb PB |
4067 | /* |
4068 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
4069 | * retry them without IOCB_NOWAIT. | |
4070 | */ | |
4071 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
4072 | ret2 = -EAGAIN; | |
75c668cd PB |
4073 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
4074 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 4075 | goto done; |
fa15bafb | 4076 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f | 4077 | /* IOPOLL retry should happen for io-wq threads */ |
b10841c9 | 4078 | if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL)) |
eefdf30f | 4079 | goto copy_iov; |
355afaeb | 4080 | done: |
2ea537ca | 4081 | kiocb_done(req, ret2, issue_flags); |
fa15bafb | 4082 | } else { |
f67676d1 | 4083 | copy_iov: |
c88598a9 PB |
4084 | iov_iter_restore(&s->iter, &s->iter_state); |
4085 | ret = io_setup_async_rw(req, iovec, s, false); | |
6bf985dc | 4086 | return ret ?: -EAGAIN; |
2b188cc1 | 4087 | } |
31b51510 | 4088 | out_free: |
f261c168 | 4089 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 4090 | if (iovec) |
6f2cc166 | 4091 | kfree(iovec); |
2b188cc1 JA |
4092 | return ret; |
4093 | } | |
4094 | ||
80a261fd JA |
4095 | static int io_renameat_prep(struct io_kiocb *req, |
4096 | const struct io_uring_sqe *sqe) | |
4097 | { | |
4098 | struct io_rename *ren = &req->rename; | |
4099 | const char __user *oldf, *newf; | |
4100 | ||
ed7eb259 JA |
4101 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4102 | return -EINVAL; | |
26578cda | 4103 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
ed7eb259 | 4104 | return -EINVAL; |
80a261fd JA |
4105 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
4106 | return -EBADF; | |
4107 | ||
4108 | ren->old_dfd = READ_ONCE(sqe->fd); | |
4109 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4110 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
4111 | ren->new_dfd = READ_ONCE(sqe->len); | |
4112 | ren->flags = READ_ONCE(sqe->rename_flags); | |
4113 | ||
4114 | ren->oldpath = getname(oldf); | |
4115 | if (IS_ERR(ren->oldpath)) | |
4116 | return PTR_ERR(ren->oldpath); | |
4117 | ||
4118 | ren->newpath = getname(newf); | |
4119 | if (IS_ERR(ren->newpath)) { | |
4120 | putname(ren->oldpath); | |
4121 | return PTR_ERR(ren->newpath); | |
4122 | } | |
4123 | ||
4124 | req->flags |= REQ_F_NEED_CLEANUP; | |
4125 | return 0; | |
4126 | } | |
4127 | ||
45d189c6 | 4128 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
4129 | { |
4130 | struct io_rename *ren = &req->rename; | |
4131 | int ret; | |
4132 | ||
45d189c6 | 4133 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
4134 | return -EAGAIN; |
4135 | ||
4136 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
4137 | ren->newpath, ren->flags); | |
4138 | ||
4139 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4140 | if (ret < 0) | |
93d2bcd2 | 4141 | req_set_fail(req); |
80a261fd JA |
4142 | io_req_complete(req, ret); |
4143 | return 0; | |
4144 | } | |
4145 | ||
14a1143b JA |
4146 | static int io_unlinkat_prep(struct io_kiocb *req, |
4147 | const struct io_uring_sqe *sqe) | |
4148 | { | |
4149 | struct io_unlink *un = &req->unlink; | |
4150 | const char __user *fname; | |
4151 | ||
22634bc5 JA |
4152 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4153 | return -EINVAL; | |
26578cda PB |
4154 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
4155 | sqe->splice_fd_in) | |
22634bc5 | 4156 | return -EINVAL; |
14a1143b JA |
4157 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
4158 | return -EBADF; | |
4159 | ||
4160 | un->dfd = READ_ONCE(sqe->fd); | |
4161 | ||
4162 | un->flags = READ_ONCE(sqe->unlink_flags); | |
4163 | if (un->flags & ~AT_REMOVEDIR) | |
4164 | return -EINVAL; | |
4165 | ||
4166 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4167 | un->filename = getname(fname); | |
4168 | if (IS_ERR(un->filename)) | |
4169 | return PTR_ERR(un->filename); | |
4170 | ||
4171 | req->flags |= REQ_F_NEED_CLEANUP; | |
4172 | return 0; | |
4173 | } | |
4174 | ||
45d189c6 | 4175 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
4176 | { |
4177 | struct io_unlink *un = &req->unlink; | |
4178 | int ret; | |
4179 | ||
45d189c6 | 4180 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
4181 | return -EAGAIN; |
4182 | ||
4183 | if (un->flags & AT_REMOVEDIR) | |
4184 | ret = do_rmdir(un->dfd, un->filename); | |
4185 | else | |
4186 | ret = do_unlinkat(un->dfd, un->filename); | |
4187 | ||
4188 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4189 | if (ret < 0) | |
93d2bcd2 | 4190 | req_set_fail(req); |
14a1143b JA |
4191 | io_req_complete(req, ret); |
4192 | return 0; | |
4193 | } | |
4194 | ||
e34a02dc DK |
4195 | static int io_mkdirat_prep(struct io_kiocb *req, |
4196 | const struct io_uring_sqe *sqe) | |
4197 | { | |
4198 | struct io_mkdir *mkd = &req->mkdir; | |
4199 | const char __user *fname; | |
4200 | ||
4201 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4202 | return -EINVAL; | |
4203 | if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index || | |
4204 | sqe->splice_fd_in) | |
4205 | return -EINVAL; | |
4206 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4207 | return -EBADF; | |
4208 | ||
4209 | mkd->dfd = READ_ONCE(sqe->fd); | |
4210 | mkd->mode = READ_ONCE(sqe->len); | |
4211 | ||
4212 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4213 | mkd->filename = getname(fname); | |
4214 | if (IS_ERR(mkd->filename)) | |
4215 | return PTR_ERR(mkd->filename); | |
4216 | ||
4217 | req->flags |= REQ_F_NEED_CLEANUP; | |
4218 | return 0; | |
4219 | } | |
4220 | ||
04f34081 | 4221 | static int io_mkdirat(struct io_kiocb *req, unsigned int issue_flags) |
e34a02dc DK |
4222 | { |
4223 | struct io_mkdir *mkd = &req->mkdir; | |
4224 | int ret; | |
4225 | ||
4226 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4227 | return -EAGAIN; | |
4228 | ||
4229 | ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode); | |
4230 | ||
4231 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4232 | if (ret < 0) | |
4233 | req_set_fail(req); | |
4234 | io_req_complete(req, ret); | |
4235 | return 0; | |
4236 | } | |
4237 | ||
7a8721f8 DK |
4238 | static int io_symlinkat_prep(struct io_kiocb *req, |
4239 | const struct io_uring_sqe *sqe) | |
4240 | { | |
4241 | struct io_symlink *sl = &req->symlink; | |
4242 | const char __user *oldpath, *newpath; | |
4243 | ||
4244 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4245 | return -EINVAL; | |
4246 | if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index || | |
4247 | sqe->splice_fd_in) | |
4248 | return -EINVAL; | |
4249 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4250 | return -EBADF; | |
4251 | ||
4252 | sl->new_dfd = READ_ONCE(sqe->fd); | |
4253 | oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4254 | newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
4255 | ||
4256 | sl->oldpath = getname(oldpath); | |
4257 | if (IS_ERR(sl->oldpath)) | |
4258 | return PTR_ERR(sl->oldpath); | |
4259 | ||
4260 | sl->newpath = getname(newpath); | |
4261 | if (IS_ERR(sl->newpath)) { | |
4262 | putname(sl->oldpath); | |
4263 | return PTR_ERR(sl->newpath); | |
4264 | } | |
4265 | ||
4266 | req->flags |= REQ_F_NEED_CLEANUP; | |
4267 | return 0; | |
4268 | } | |
4269 | ||
04f34081 | 4270 | static int io_symlinkat(struct io_kiocb *req, unsigned int issue_flags) |
7a8721f8 DK |
4271 | { |
4272 | struct io_symlink *sl = &req->symlink; | |
4273 | int ret; | |
4274 | ||
4275 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4276 | return -EAGAIN; | |
4277 | ||
4278 | ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath); | |
4279 | ||
4280 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4281 | if (ret < 0) | |
4282 | req_set_fail(req); | |
4283 | io_req_complete(req, ret); | |
4284 | return 0; | |
4285 | } | |
4286 | ||
cf30da90 DK |
4287 | static int io_linkat_prep(struct io_kiocb *req, |
4288 | const struct io_uring_sqe *sqe) | |
4289 | { | |
4290 | struct io_hardlink *lnk = &req->hardlink; | |
4291 | const char __user *oldf, *newf; | |
4292 | ||
4293 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4294 | return -EINVAL; | |
4295 | if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) | |
4296 | return -EINVAL; | |
4297 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4298 | return -EBADF; | |
4299 | ||
4300 | lnk->old_dfd = READ_ONCE(sqe->fd); | |
4301 | lnk->new_dfd = READ_ONCE(sqe->len); | |
4302 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4303 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
4304 | lnk->flags = READ_ONCE(sqe->hardlink_flags); | |
4305 | ||
4306 | lnk->oldpath = getname(oldf); | |
4307 | if (IS_ERR(lnk->oldpath)) | |
4308 | return PTR_ERR(lnk->oldpath); | |
4309 | ||
4310 | lnk->newpath = getname(newf); | |
4311 | if (IS_ERR(lnk->newpath)) { | |
4312 | putname(lnk->oldpath); | |
4313 | return PTR_ERR(lnk->newpath); | |
4314 | } | |
4315 | ||
4316 | req->flags |= REQ_F_NEED_CLEANUP; | |
4317 | return 0; | |
4318 | } | |
4319 | ||
04f34081 | 4320 | static int io_linkat(struct io_kiocb *req, unsigned int issue_flags) |
cf30da90 DK |
4321 | { |
4322 | struct io_hardlink *lnk = &req->hardlink; | |
4323 | int ret; | |
4324 | ||
4325 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4326 | return -EAGAIN; | |
4327 | ||
4328 | ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd, | |
4329 | lnk->newpath, lnk->flags); | |
4330 | ||
4331 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4332 | if (ret < 0) | |
4333 | req_set_fail(req); | |
4334 | io_req_complete(req, ret); | |
4335 | return 0; | |
4336 | } | |
4337 | ||
36f4fa68 JA |
4338 | static int io_shutdown_prep(struct io_kiocb *req, |
4339 | const struct io_uring_sqe *sqe) | |
4340 | { | |
4341 | #if defined(CONFIG_NET) | |
4342 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4343 | return -EINVAL; | |
26578cda PB |
4344 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
4345 | sqe->buf_index || sqe->splice_fd_in)) | |
36f4fa68 JA |
4346 | return -EINVAL; |
4347 | ||
4348 | req->shutdown.how = READ_ONCE(sqe->len); | |
4349 | return 0; | |
4350 | #else | |
4351 | return -EOPNOTSUPP; | |
4352 | #endif | |
4353 | } | |
4354 | ||
45d189c6 | 4355 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
4356 | { |
4357 | #if defined(CONFIG_NET) | |
4358 | struct socket *sock; | |
4359 | int ret; | |
4360 | ||
45d189c6 | 4361 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
4362 | return -EAGAIN; |
4363 | ||
48aba79b | 4364 | sock = sock_from_file(req->file); |
36f4fa68 | 4365 | if (unlikely(!sock)) |
48aba79b | 4366 | return -ENOTSOCK; |
36f4fa68 JA |
4367 | |
4368 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d | 4369 | if (ret < 0) |
93d2bcd2 | 4370 | req_set_fail(req); |
36f4fa68 JA |
4371 | io_req_complete(req, ret); |
4372 | return 0; | |
4373 | #else | |
4374 | return -EOPNOTSUPP; | |
4375 | #endif | |
4376 | } | |
4377 | ||
f2a8d5c7 PB |
4378 | static int __io_splice_prep(struct io_kiocb *req, |
4379 | const struct io_uring_sqe *sqe) | |
7d67af2c | 4380 | { |
fe7e3257 | 4381 | struct io_splice *sp = &req->splice; |
7d67af2c | 4382 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
7d67af2c | 4383 | |
3232dd02 PB |
4384 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4385 | return -EINVAL; | |
7d67af2c | 4386 | |
7d67af2c PB |
4387 | sp->len = READ_ONCE(sqe->len); |
4388 | sp->flags = READ_ONCE(sqe->splice_flags); | |
7d67af2c PB |
4389 | if (unlikely(sp->flags & ~valid_flags)) |
4390 | return -EINVAL; | |
a3e4bc23 | 4391 | sp->splice_fd_in = READ_ONCE(sqe->splice_fd_in); |
7d67af2c PB |
4392 | return 0; |
4393 | } | |
4394 | ||
f2a8d5c7 PB |
4395 | static int io_tee_prep(struct io_kiocb *req, |
4396 | const struct io_uring_sqe *sqe) | |
4397 | { | |
4398 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
4399 | return -EINVAL; | |
4400 | return __io_splice_prep(req, sqe); | |
4401 | } | |
4402 | ||
45d189c6 | 4403 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
4404 | { |
4405 | struct io_splice *sp = &req->splice; | |
f2a8d5c7 PB |
4406 | struct file *out = sp->file_out; |
4407 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
a3e4bc23 | 4408 | struct file *in; |
f2a8d5c7 PB |
4409 | long ret = 0; |
4410 | ||
45d189c6 | 4411 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 | 4412 | return -EAGAIN; |
a3e4bc23 | 4413 | |
5106dd6e | 4414 | if (sp->flags & SPLICE_F_FD_IN_FIXED) |
e9419766 | 4415 | in = io_file_get_fixed(req, sp->splice_fd_in, issue_flags); |
5106dd6e JA |
4416 | else |
4417 | in = io_file_get_normal(req, sp->splice_fd_in); | |
a3e4bc23 JA |
4418 | if (!in) { |
4419 | ret = -EBADF; | |
4420 | goto done; | |
4421 | } | |
4422 | ||
f2a8d5c7 PB |
4423 | if (sp->len) |
4424 | ret = do_tee(in, out, sp->len, flags); | |
4425 | ||
e1d767f0 PB |
4426 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4427 | io_put_file(in); | |
a3e4bc23 | 4428 | done: |
f2a8d5c7 | 4429 | if (ret != sp->len) |
93d2bcd2 | 4430 | req_set_fail(req); |
e1e16097 | 4431 | io_req_complete(req, ret); |
f2a8d5c7 PB |
4432 | return 0; |
4433 | } | |
4434 | ||
4435 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4436 | { | |
fe7e3257 | 4437 | struct io_splice *sp = &req->splice; |
f2a8d5c7 PB |
4438 | |
4439 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
4440 | sp->off_out = READ_ONCE(sqe->off); | |
4441 | return __io_splice_prep(req, sqe); | |
4442 | } | |
4443 | ||
45d189c6 | 4444 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
4445 | { |
4446 | struct io_splice *sp = &req->splice; | |
7d67af2c PB |
4447 | struct file *out = sp->file_out; |
4448 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
4449 | loff_t *poff_in, *poff_out; | |
a3e4bc23 | 4450 | struct file *in; |
c9687426 | 4451 | long ret = 0; |
7d67af2c | 4452 | |
45d189c6 | 4453 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 4454 | return -EAGAIN; |
7d67af2c | 4455 | |
5106dd6e | 4456 | if (sp->flags & SPLICE_F_FD_IN_FIXED) |
e9419766 | 4457 | in = io_file_get_fixed(req, sp->splice_fd_in, issue_flags); |
5106dd6e JA |
4458 | else |
4459 | in = io_file_get_normal(req, sp->splice_fd_in); | |
a3e4bc23 JA |
4460 | if (!in) { |
4461 | ret = -EBADF; | |
4462 | goto done; | |
4463 | } | |
4464 | ||
7d67af2c PB |
4465 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
4466 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 4467 | |
948a7749 | 4468 | if (sp->len) |
c9687426 | 4469 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c | 4470 | |
e1d767f0 PB |
4471 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4472 | io_put_file(in); | |
a3e4bc23 | 4473 | done: |
7d67af2c | 4474 | if (ret != sp->len) |
93d2bcd2 | 4475 | req_set_fail(req); |
e1e16097 | 4476 | io_req_complete(req, ret); |
7d67af2c PB |
4477 | return 0; |
4478 | } | |
4479 | ||
2b188cc1 JA |
4480 | /* |
4481 | * IORING_OP_NOP just posts a completion event, nothing else. | |
4482 | */ | |
889fca73 | 4483 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
4484 | { |
4485 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 4486 | |
def596e9 JA |
4487 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4488 | return -EINVAL; | |
4489 | ||
889fca73 | 4490 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
4491 | return 0; |
4492 | } | |
4493 | ||
4f57f06c JA |
4494 | static int io_msg_ring_prep(struct io_kiocb *req, |
4495 | const struct io_uring_sqe *sqe) | |
4496 | { | |
f3b6a41e JA |
4497 | if (unlikely(sqe->addr || sqe->ioprio || sqe->rw_flags || |
4498 | sqe->splice_fd_in || sqe->buf_index || sqe->personality)) | |
4f57f06c JA |
4499 | return -EINVAL; |
4500 | ||
4f57f06c JA |
4501 | req->msg.user_data = READ_ONCE(sqe->off); |
4502 | req->msg.len = READ_ONCE(sqe->len); | |
4503 | return 0; | |
4504 | } | |
4505 | ||
4506 | static int io_msg_ring(struct io_kiocb *req, unsigned int issue_flags) | |
4507 | { | |
4508 | struct io_ring_ctx *target_ctx; | |
4509 | struct io_msg *msg = &req->msg; | |
4f57f06c | 4510 | bool filled; |
3f1d52ab | 4511 | int ret; |
4f57f06c | 4512 | |
3f1d52ab JA |
4513 | ret = -EBADFD; |
4514 | if (req->file->f_op != &io_uring_fops) | |
4515 | goto done; | |
4f57f06c | 4516 | |
3f1d52ab | 4517 | ret = -EOVERFLOW; |
4f57f06c JA |
4518 | target_ctx = req->file->private_data; |
4519 | ||
4520 | spin_lock(&target_ctx->completion_lock); | |
7ef66d18 | 4521 | filled = io_fill_cqe_aux(target_ctx, msg->user_data, msg->len, 0); |
4f57f06c JA |
4522 | io_commit_cqring(target_ctx); |
4523 | spin_unlock(&target_ctx->completion_lock); | |
4524 | ||
4525 | if (filled) { | |
4526 | io_cqring_ev_posted(target_ctx); | |
4527 | ret = 0; | |
4528 | } | |
4529 | ||
3f1d52ab | 4530 | done: |
9666d420 JA |
4531 | if (ret < 0) |
4532 | req_set_fail(req); | |
4f57f06c JA |
4533 | __io_req_complete(req, issue_flags, ret, 0); |
4534 | return 0; | |
4535 | } | |
4536 | ||
1155c76a | 4537 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 4538 | { |
6b06314c | 4539 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 4540 | |
6b06314c | 4541 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 4542 | return -EINVAL; |
26578cda PB |
4543 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4544 | sqe->splice_fd_in)) | |
c992fe29 CH |
4545 | return -EINVAL; |
4546 | ||
8ed8d3c3 JA |
4547 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
4548 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
4549 | return -EINVAL; | |
4550 | ||
4551 | req->sync.off = READ_ONCE(sqe->off); | |
4552 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
4553 | return 0; |
4554 | } | |
4555 | ||
45d189c6 | 4556 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4557 | { |
8ed8d3c3 | 4558 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
4559 | int ret; |
4560 | ||
ac45abc0 | 4561 | /* fsync always requires a blocking context */ |
45d189c6 | 4562 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4563 | return -EAGAIN; |
4564 | ||
9adbd45d | 4565 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
4566 | end > 0 ? end : LLONG_MAX, |
4567 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
4568 | if (ret < 0) | |
93d2bcd2 | 4569 | req_set_fail(req); |
e1e16097 | 4570 | io_req_complete(req, ret); |
c992fe29 CH |
4571 | return 0; |
4572 | } | |
4573 | ||
d63d1b5e JA |
4574 | static int io_fallocate_prep(struct io_kiocb *req, |
4575 | const struct io_uring_sqe *sqe) | |
4576 | { | |
26578cda PB |
4577 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
4578 | sqe->splice_fd_in) | |
d63d1b5e | 4579 | return -EINVAL; |
3232dd02 PB |
4580 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4581 | return -EINVAL; | |
d63d1b5e JA |
4582 | |
4583 | req->sync.off = READ_ONCE(sqe->off); | |
4584 | req->sync.len = READ_ONCE(sqe->addr); | |
4585 | req->sync.mode = READ_ONCE(sqe->len); | |
4586 | return 0; | |
4587 | } | |
4588 | ||
45d189c6 | 4589 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 4590 | { |
ac45abc0 PB |
4591 | int ret; |
4592 | ||
d63d1b5e | 4593 | /* fallocate always requiring blocking context */ |
45d189c6 | 4594 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 4595 | return -EAGAIN; |
ac45abc0 PB |
4596 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
4597 | req->sync.len); | |
ac45abc0 | 4598 | if (ret < 0) |
93d2bcd2 | 4599 | req_set_fail(req); |
f63cf519 JA |
4600 | else |
4601 | fsnotify_modify(req->file); | |
e1e16097 | 4602 | io_req_complete(req, ret); |
5d17b4a4 JA |
4603 | return 0; |
4604 | } | |
4605 | ||
ec65fea5 | 4606 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 4607 | { |
f8748881 | 4608 | const char __user *fname; |
15b71abe | 4609 | int ret; |
b7bb4f7d | 4610 | |
d3fddf6d PB |
4611 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4612 | return -EINVAL; | |
b9445598 | 4613 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 4614 | return -EINVAL; |
ec65fea5 | 4615 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 4616 | return -EBADF; |
03b1230c | 4617 | |
ec65fea5 PB |
4618 | /* open.how should be already initialised */ |
4619 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 4620 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 4621 | |
25e72d10 PB |
4622 | req->open.dfd = READ_ONCE(sqe->fd); |
4623 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 4624 | req->open.filename = getname(fname); |
15b71abe JA |
4625 | if (IS_ERR(req->open.filename)) { |
4626 | ret = PTR_ERR(req->open.filename); | |
4627 | req->open.filename = NULL; | |
4628 | return ret; | |
4629 | } | |
b9445598 PB |
4630 | |
4631 | req->open.file_slot = READ_ONCE(sqe->file_index); | |
4632 | if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) | |
4633 | return -EINVAL; | |
4634 | ||
4022e7af | 4635 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 4636 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 4637 | return 0; |
03b1230c JA |
4638 | } |
4639 | ||
ec65fea5 PB |
4640 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4641 | { | |
d3fddf6d PB |
4642 | u64 mode = READ_ONCE(sqe->len); |
4643 | u64 flags = READ_ONCE(sqe->open_flags); | |
ec65fea5 | 4644 | |
ec65fea5 PB |
4645 | req->open.how = build_open_how(flags, mode); |
4646 | return __io_openat_prep(req, sqe); | |
4647 | } | |
4648 | ||
cebdb986 | 4649 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 4650 | { |
cebdb986 | 4651 | struct open_how __user *how; |
cebdb986 | 4652 | size_t len; |
0fa03c62 JA |
4653 | int ret; |
4654 | ||
cebdb986 JA |
4655 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4656 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
4657 | if (len < OPEN_HOW_SIZE_VER0) |
4658 | return -EINVAL; | |
3529d8c2 | 4659 | |
cebdb986 JA |
4660 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
4661 | len); | |
4662 | if (ret) | |
4663 | return ret; | |
3529d8c2 | 4664 | |
ec65fea5 | 4665 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
4666 | } |
4667 | ||
45d189c6 | 4668 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
4669 | { |
4670 | struct open_flags op; | |
15b71abe | 4671 | struct file *file; |
b9445598 PB |
4672 | bool resolve_nonblock, nonblock_set; |
4673 | bool fixed = !!req->open.file_slot; | |
15b71abe JA |
4674 | int ret; |
4675 | ||
cebdb986 | 4676 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
4677 | if (ret) |
4678 | goto err; | |
3a81fd02 JA |
4679 | nonblock_set = op.open_flag & O_NONBLOCK; |
4680 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 4681 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
4682 | /* |
4683 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
4684 | * it'll always -EAGAIN | |
4685 | */ | |
4686 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
4687 | return -EAGAIN; | |
4688 | op.lookup_flags |= LOOKUP_CACHED; | |
4689 | op.open_flag |= O_NONBLOCK; | |
4690 | } | |
15b71abe | 4691 | |
b9445598 PB |
4692 | if (!fixed) { |
4693 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); | |
4694 | if (ret < 0) | |
4695 | goto err; | |
4696 | } | |
15b71abe JA |
4697 | |
4698 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
12dcb58a | 4699 | if (IS_ERR(file)) { |
944d1444 | 4700 | /* |
12dcb58a PB |
4701 | * We could hang on to this 'fd' on retrying, but seems like |
4702 | * marginal gain for something that is now known to be a slower | |
4703 | * path. So just put it, and we'll get a new one when we retry. | |
944d1444 | 4704 | */ |
b9445598 PB |
4705 | if (!fixed) |
4706 | put_unused_fd(ret); | |
3a81fd02 | 4707 | |
15b71abe | 4708 | ret = PTR_ERR(file); |
12dcb58a PB |
4709 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
4710 | if (ret == -EAGAIN && | |
4711 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) | |
4712 | return -EAGAIN; | |
4713 | goto err; | |
15b71abe | 4714 | } |
12dcb58a PB |
4715 | |
4716 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) | |
4717 | file->f_flags &= ~O_NONBLOCK; | |
4718 | fsnotify_open(file); | |
b9445598 PB |
4719 | |
4720 | if (!fixed) | |
4721 | fd_install(ret, file); | |
4722 | else | |
4723 | ret = io_install_fixed_file(req, file, issue_flags, | |
4724 | req->open.file_slot - 1); | |
15b71abe JA |
4725 | err: |
4726 | putname(req->open.filename); | |
8fef80bf | 4727 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe | 4728 | if (ret < 0) |
93d2bcd2 | 4729 | req_set_fail(req); |
0bdf3398 | 4730 | __io_req_complete(req, issue_flags, ret, 0); |
15b71abe JA |
4731 | return 0; |
4732 | } | |
4733 | ||
45d189c6 | 4734 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 4735 | { |
e45cff58 | 4736 | return io_openat2(req, issue_flags); |
cebdb986 JA |
4737 | } |
4738 | ||
067524e9 JA |
4739 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4740 | const struct io_uring_sqe *sqe) | |
4741 | { | |
4742 | struct io_provide_buf *p = &req->pbuf; | |
4743 | u64 tmp; | |
4744 | ||
26578cda PB |
4745 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
4746 | sqe->splice_fd_in) | |
067524e9 JA |
4747 | return -EINVAL; |
4748 | ||
4749 | tmp = READ_ONCE(sqe->fd); | |
4750 | if (!tmp || tmp > USHRT_MAX) | |
4751 | return -EINVAL; | |
4752 | ||
4753 | memset(p, 0, sizeof(*p)); | |
4754 | p->nbufs = tmp; | |
4755 | p->bgid = READ_ONCE(sqe->buf_group); | |
4756 | return 0; | |
4757 | } | |
4758 | ||
dbc7d452 JA |
4759 | static int __io_remove_buffers(struct io_ring_ctx *ctx, |
4760 | struct io_buffer_list *bl, unsigned nbufs) | |
067524e9 JA |
4761 | { |
4762 | unsigned i = 0; | |
4763 | ||
4764 | /* shouldn't happen */ | |
4765 | if (!nbufs) | |
4766 | return 0; | |
4767 | ||
4768 | /* the head kbuf is the list itself */ | |
dbc7d452 | 4769 | while (!list_empty(&bl->buf_list)) { |
067524e9 JA |
4770 | struct io_buffer *nxt; |
4771 | ||
dbc7d452 | 4772 | nxt = list_first_entry(&bl->buf_list, struct io_buffer, list); |
067524e9 | 4773 | list_del(&nxt->list); |
067524e9 JA |
4774 | if (++i == nbufs) |
4775 | return i; | |
1d0254e6 | 4776 | cond_resched(); |
067524e9 JA |
4777 | } |
4778 | i++; | |
067524e9 JA |
4779 | |
4780 | return i; | |
4781 | } | |
4782 | ||
889fca73 | 4783 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
4784 | { |
4785 | struct io_provide_buf *p = &req->pbuf; | |
4786 | struct io_ring_ctx *ctx = req->ctx; | |
dbc7d452 | 4787 | struct io_buffer_list *bl; |
067524e9 JA |
4788 | int ret = 0; |
4789 | ||
f8929630 | 4790 | io_ring_submit_lock(ctx, issue_flags); |
067524e9 JA |
4791 | |
4792 | ret = -ENOENT; | |
dbc7d452 JA |
4793 | bl = io_buffer_get_list(ctx, p->bgid); |
4794 | if (bl) | |
4795 | ret = __io_remove_buffers(ctx, bl, p->nbufs); | |
067524e9 | 4796 | if (ret < 0) |
93d2bcd2 | 4797 | req_set_fail(req); |
067524e9 | 4798 | |
9fb8cb49 PB |
4799 | /* complete before unlock, IOPOLL may need the lock */ |
4800 | __io_req_complete(req, issue_flags, ret, 0); | |
f8929630 | 4801 | io_ring_submit_unlock(ctx, issue_flags); |
067524e9 JA |
4802 | return 0; |
4803 | } | |
4804 | ||
ddf0322d JA |
4805 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4806 | const struct io_uring_sqe *sqe) | |
4807 | { | |
38134ada | 4808 | unsigned long size, tmp_check; |
ddf0322d JA |
4809 | struct io_provide_buf *p = &req->pbuf; |
4810 | u64 tmp; | |
4811 | ||
26578cda | 4812 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
ddf0322d JA |
4813 | return -EINVAL; |
4814 | ||
4815 | tmp = READ_ONCE(sqe->fd); | |
4816 | if (!tmp || tmp > USHRT_MAX) | |
4817 | return -E2BIG; | |
4818 | p->nbufs = tmp; | |
4819 | p->addr = READ_ONCE(sqe->addr); | |
4820 | p->len = READ_ONCE(sqe->len); | |
4821 | ||
38134ada PB |
4822 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
4823 | &size)) | |
4824 | return -EOVERFLOW; | |
4825 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) | |
4826 | return -EOVERFLOW; | |
4827 | ||
d81269fe PB |
4828 | size = (unsigned long)p->len * p->nbufs; |
4829 | if (!access_ok(u64_to_user_ptr(p->addr), size)) | |
ddf0322d JA |
4830 | return -EFAULT; |
4831 | ||
4832 | p->bgid = READ_ONCE(sqe->buf_group); | |
4833 | tmp = READ_ONCE(sqe->off); | |
4834 | if (tmp > USHRT_MAX) | |
4835 | return -E2BIG; | |
4836 | p->bid = tmp; | |
4837 | return 0; | |
4838 | } | |
4839 | ||
cc3cec83 JA |
4840 | static int io_refill_buffer_cache(struct io_ring_ctx *ctx) |
4841 | { | |
4842 | struct io_buffer *buf; | |
4843 | struct page *page; | |
4844 | int bufs_in_page; | |
4845 | ||
4846 | /* | |
4847 | * Completions that don't happen inline (eg not under uring_lock) will | |
4848 | * add to ->io_buffers_comp. If we don't have any free buffers, check | |
4849 | * the completion list and splice those entries first. | |
4850 | */ | |
4851 | if (!list_empty_careful(&ctx->io_buffers_comp)) { | |
4852 | spin_lock(&ctx->completion_lock); | |
4853 | if (!list_empty(&ctx->io_buffers_comp)) { | |
4854 | list_splice_init(&ctx->io_buffers_comp, | |
4855 | &ctx->io_buffers_cache); | |
4856 | spin_unlock(&ctx->completion_lock); | |
4857 | return 0; | |
4858 | } | |
4859 | spin_unlock(&ctx->completion_lock); | |
4860 | } | |
4861 | ||
4862 | /* | |
4863 | * No free buffers and no completion entries either. Allocate a new | |
4864 | * page worth of buffer entries and add those to our freelist. | |
4865 | */ | |
4866 | page = alloc_page(GFP_KERNEL_ACCOUNT); | |
4867 | if (!page) | |
4868 | return -ENOMEM; | |
4869 | ||
4870 | list_add(&page->lru, &ctx->io_buffers_pages); | |
4871 | ||
4872 | buf = page_address(page); | |
4873 | bufs_in_page = PAGE_SIZE / sizeof(*buf); | |
4874 | while (bufs_in_page) { | |
4875 | list_add_tail(&buf->list, &ctx->io_buffers_cache); | |
4876 | buf++; | |
4877 | bufs_in_page--; | |
4878 | } | |
4879 | ||
4880 | return 0; | |
4881 | } | |
4882 | ||
4883 | static int io_add_buffers(struct io_ring_ctx *ctx, struct io_provide_buf *pbuf, | |
dbc7d452 | 4884 | struct io_buffer_list *bl) |
ddf0322d JA |
4885 | { |
4886 | struct io_buffer *buf; | |
4887 | u64 addr = pbuf->addr; | |
4888 | int i, bid = pbuf->bid; | |
4889 | ||
4890 | for (i = 0; i < pbuf->nbufs; i++) { | |
cc3cec83 JA |
4891 | if (list_empty(&ctx->io_buffers_cache) && |
4892 | io_refill_buffer_cache(ctx)) | |
ddf0322d | 4893 | break; |
cc3cec83 JA |
4894 | buf = list_first_entry(&ctx->io_buffers_cache, struct io_buffer, |
4895 | list); | |
dbc7d452 | 4896 | list_move_tail(&buf->list, &bl->buf_list); |
ddf0322d | 4897 | buf->addr = addr; |
d1f82808 | 4898 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
ddf0322d | 4899 | buf->bid = bid; |
b1c62645 | 4900 | buf->bgid = pbuf->bgid; |
ddf0322d JA |
4901 | addr += pbuf->len; |
4902 | bid++; | |
f240762f | 4903 | cond_resched(); |
ddf0322d JA |
4904 | } |
4905 | ||
dbc7d452 | 4906 | return i ? 0 : -ENOMEM; |
ddf0322d JA |
4907 | } |
4908 | ||
889fca73 | 4909 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4910 | { |
4911 | struct io_provide_buf *p = &req->pbuf; | |
4912 | struct io_ring_ctx *ctx = req->ctx; | |
dbc7d452 | 4913 | struct io_buffer_list *bl; |
ddf0322d | 4914 | int ret = 0; |
ddf0322d | 4915 | |
f8929630 | 4916 | io_ring_submit_lock(ctx, issue_flags); |
ddf0322d | 4917 | |
dbc7d452 JA |
4918 | bl = io_buffer_get_list(ctx, p->bgid); |
4919 | if (unlikely(!bl)) { | |
4920 | bl = kmalloc(sizeof(*bl), GFP_KERNEL); | |
4921 | if (!bl) { | |
4922 | ret = -ENOMEM; | |
4923 | goto err; | |
4924 | } | |
4925 | io_buffer_add_list(ctx, bl, p->bgid); | |
ddf0322d | 4926 | } |
dbc7d452 JA |
4927 | |
4928 | ret = io_add_buffers(ctx, p, bl); | |
4929 | err: | |
ddf0322d | 4930 | if (ret < 0) |
93d2bcd2 | 4931 | req_set_fail(req); |
9fb8cb49 PB |
4932 | /* complete before unlock, IOPOLL may need the lock */ |
4933 | __io_req_complete(req, issue_flags, ret, 0); | |
f8929630 | 4934 | io_ring_submit_unlock(ctx, issue_flags); |
ddf0322d | 4935 | return 0; |
cebdb986 JA |
4936 | } |
4937 | ||
3e4827b0 JA |
4938 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4939 | const struct io_uring_sqe *sqe) | |
4940 | { | |
4941 | #if defined(CONFIG_EPOLL) | |
26578cda | 4942 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
3e4827b0 | 4943 | return -EINVAL; |
2d74d042 | 4944 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4945 | return -EINVAL; |
3e4827b0 JA |
4946 | |
4947 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4948 | req->epoll.op = READ_ONCE(sqe->len); | |
4949 | req->epoll.fd = READ_ONCE(sqe->off); | |
4950 | ||
4951 | if (ep_op_has_event(req->epoll.op)) { | |
4952 | struct epoll_event __user *ev; | |
4953 | ||
4954 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4955 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4956 | return -EFAULT; | |
4957 | } | |
4958 | ||
4959 | return 0; | |
4960 | #else | |
4961 | return -EOPNOTSUPP; | |
4962 | #endif | |
4963 | } | |
4964 | ||
889fca73 | 4965 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4966 | { |
4967 | #if defined(CONFIG_EPOLL) | |
4968 | struct io_epoll *ie = &req->epoll; | |
4969 | int ret; | |
45d189c6 | 4970 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4971 | |
4972 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4973 | if (force_nonblock && ret == -EAGAIN) | |
4974 | return -EAGAIN; | |
4975 | ||
4976 | if (ret < 0) | |
93d2bcd2 | 4977 | req_set_fail(req); |
889fca73 | 4978 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4979 | return 0; |
4980 | #else | |
4981 | return -EOPNOTSUPP; | |
4982 | #endif | |
4983 | } | |
4984 | ||
c1ca757b JA |
4985 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4986 | { | |
4987 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
26578cda | 4988 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
c1ca757b | 4989 | return -EINVAL; |
3232dd02 PB |
4990 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4991 | return -EINVAL; | |
c1ca757b JA |
4992 | |
4993 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4994 | req->madvise.len = READ_ONCE(sqe->len); | |
4995 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4996 | return 0; | |
4997 | #else | |
4998 | return -EOPNOTSUPP; | |
4999 | #endif | |
5000 | } | |
5001 | ||
45d189c6 | 5002 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
5003 | { |
5004 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
5005 | struct io_madvise *ma = &req->madvise; | |
5006 | int ret; | |
5007 | ||
45d189c6 | 5008 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
5009 | return -EAGAIN; |
5010 | ||
0726b01e | 5011 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b | 5012 | if (ret < 0) |
93d2bcd2 | 5013 | req_set_fail(req); |
e1e16097 | 5014 | io_req_complete(req, ret); |
c1ca757b JA |
5015 | return 0; |
5016 | #else | |
5017 | return -EOPNOTSUPP; | |
5018 | #endif | |
5019 | } | |
5020 | ||
4840e418 JA |
5021 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5022 | { | |
26578cda | 5023 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
4840e418 | 5024 | return -EINVAL; |
3232dd02 PB |
5025 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5026 | return -EINVAL; | |
4840e418 JA |
5027 | |
5028 | req->fadvise.offset = READ_ONCE(sqe->off); | |
5029 | req->fadvise.len = READ_ONCE(sqe->len); | |
5030 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
5031 | return 0; | |
5032 | } | |
5033 | ||
45d189c6 | 5034 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
5035 | { |
5036 | struct io_fadvise *fa = &req->fadvise; | |
5037 | int ret; | |
5038 | ||
45d189c6 | 5039 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
5040 | switch (fa->advice) { |
5041 | case POSIX_FADV_NORMAL: | |
5042 | case POSIX_FADV_RANDOM: | |
5043 | case POSIX_FADV_SEQUENTIAL: | |
5044 | break; | |
5045 | default: | |
5046 | return -EAGAIN; | |
5047 | } | |
5048 | } | |
4840e418 JA |
5049 | |
5050 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
5051 | if (ret < 0) | |
93d2bcd2 | 5052 | req_set_fail(req); |
0bdf3398 | 5053 | __io_req_complete(req, issue_flags, ret, 0); |
4840e418 JA |
5054 | return 0; |
5055 | } | |
5056 | ||
eddc7ef5 JA |
5057 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5058 | { | |
1b6fe6e0 SR |
5059 | const char __user *path; |
5060 | ||
2d74d042 | 5061 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 5062 | return -EINVAL; |
26578cda | 5063 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
eddc7ef5 | 5064 | return -EINVAL; |
9c280f90 | 5065 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 5066 | return -EBADF; |
eddc7ef5 | 5067 | |
1d9e1288 BM |
5068 | req->statx.dfd = READ_ONCE(sqe->fd); |
5069 | req->statx.mask = READ_ONCE(sqe->len); | |
1b6fe6e0 | 5070 | path = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
5071 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
5072 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 | 5073 | |
1b6fe6e0 SR |
5074 | req->statx.filename = getname_flags(path, |
5075 | getname_statx_lookup_flags(req->statx.flags), | |
5076 | NULL); | |
5077 | ||
5078 | if (IS_ERR(req->statx.filename)) { | |
5079 | int ret = PTR_ERR(req->statx.filename); | |
5080 | ||
5081 | req->statx.filename = NULL; | |
5082 | return ret; | |
5083 | } | |
5084 | ||
5085 | req->flags |= REQ_F_NEED_CLEANUP; | |
eddc7ef5 JA |
5086 | return 0; |
5087 | } | |
5088 | ||
45d189c6 | 5089 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 5090 | { |
1d9e1288 | 5091 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
5092 | int ret; |
5093 | ||
59d70013 | 5094 | if (issue_flags & IO_URING_F_NONBLOCK) |
eddc7ef5 JA |
5095 | return -EAGAIN; |
5096 | ||
e62753e4 BM |
5097 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
5098 | ctx->buffer); | |
eddc7ef5 | 5099 | |
eddc7ef5 | 5100 | if (ret < 0) |
93d2bcd2 | 5101 | req_set_fail(req); |
e1e16097 | 5102 | io_req_complete(req, ret); |
eddc7ef5 JA |
5103 | return 0; |
5104 | } | |
5105 | ||
b5dba59e JA |
5106 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5107 | { | |
14587a46 | 5108 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 5109 | return -EINVAL; |
b5dba59e | 5110 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
7df778be | 5111 | sqe->rw_flags || sqe->buf_index) |
b5dba59e | 5112 | return -EINVAL; |
9c280f90 | 5113 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 5114 | return -EBADF; |
b5dba59e JA |
5115 | |
5116 | req->close.fd = READ_ONCE(sqe->fd); | |
7df778be PB |
5117 | req->close.file_slot = READ_ONCE(sqe->file_index); |
5118 | if (req->close.file_slot && req->close.fd) | |
5119 | return -EINVAL; | |
5120 | ||
b5dba59e | 5121 | return 0; |
b5dba59e JA |
5122 | } |
5123 | ||
889fca73 | 5124 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 5125 | { |
9eac1904 | 5126 | struct files_struct *files = current->files; |
3af73b28 | 5127 | struct io_close *close = &req->close; |
9eac1904 | 5128 | struct fdtable *fdt; |
a1fde923 PB |
5129 | struct file *file = NULL; |
5130 | int ret = -EBADF; | |
b5dba59e | 5131 | |
7df778be PB |
5132 | if (req->close.file_slot) { |
5133 | ret = io_close_fixed(req, issue_flags); | |
5134 | goto err; | |
5135 | } | |
5136 | ||
9eac1904 JA |
5137 | spin_lock(&files->file_lock); |
5138 | fdt = files_fdtable(files); | |
5139 | if (close->fd >= fdt->max_fds) { | |
5140 | spin_unlock(&files->file_lock); | |
5141 | goto err; | |
5142 | } | |
5143 | file = fdt->fd[close->fd]; | |
a1fde923 | 5144 | if (!file || file->f_op == &io_uring_fops) { |
9eac1904 JA |
5145 | spin_unlock(&files->file_lock); |
5146 | file = NULL; | |
5147 | goto err; | |
3af73b28 | 5148 | } |
b5dba59e JA |
5149 | |
5150 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 5151 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 5152 | spin_unlock(&files->file_lock); |
0bf0eefd | 5153 | return -EAGAIN; |
a2100672 | 5154 | } |
b5dba59e | 5155 | |
9eac1904 JA |
5156 | ret = __close_fd_get_file(close->fd, &file); |
5157 | spin_unlock(&files->file_lock); | |
5158 | if (ret < 0) { | |
5159 | if (ret == -ENOENT) | |
5160 | ret = -EBADF; | |
5161 | goto err; | |
5162 | } | |
5163 | ||
3af73b28 | 5164 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
5165 | ret = filp_close(file, current->files); |
5166 | err: | |
3af73b28 | 5167 | if (ret < 0) |
93d2bcd2 | 5168 | req_set_fail(req); |
9eac1904 JA |
5169 | if (file) |
5170 | fput(file); | |
889fca73 | 5171 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 5172 | return 0; |
b5dba59e JA |
5173 | } |
5174 | ||
1155c76a | 5175 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
5176 | { |
5177 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 5178 | |
5d17b4a4 JA |
5179 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
5180 | return -EINVAL; | |
26578cda PB |
5181 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
5182 | sqe->splice_fd_in)) | |
5d17b4a4 JA |
5183 | return -EINVAL; |
5184 | ||
8ed8d3c3 JA |
5185 | req->sync.off = READ_ONCE(sqe->off); |
5186 | req->sync.len = READ_ONCE(sqe->len); | |
5187 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
5188 | return 0; |
5189 | } | |
5190 | ||
45d189c6 | 5191 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 5192 | { |
8ed8d3c3 JA |
5193 | int ret; |
5194 | ||
ac45abc0 | 5195 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 5196 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
5197 | return -EAGAIN; |
5198 | ||
9adbd45d | 5199 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
5200 | req->sync.flags); |
5201 | if (ret < 0) | |
93d2bcd2 | 5202 | req_set_fail(req); |
e1e16097 | 5203 | io_req_complete(req, ret); |
5d17b4a4 JA |
5204 | return 0; |
5205 | } | |
5206 | ||
469956e8 | 5207 | #if defined(CONFIG_NET) |
02d27d89 PB |
5208 | static int io_setup_async_msg(struct io_kiocb *req, |
5209 | struct io_async_msghdr *kmsg) | |
5210 | { | |
e8c2bc1f JA |
5211 | struct io_async_msghdr *async_msg = req->async_data; |
5212 | ||
5213 | if (async_msg) | |
02d27d89 | 5214 | return -EAGAIN; |
e8c2bc1f | 5215 | if (io_alloc_async_data(req)) { |
257e84a5 | 5216 | kfree(kmsg->free_iov); |
02d27d89 PB |
5217 | return -ENOMEM; |
5218 | } | |
e8c2bc1f | 5219 | async_msg = req->async_data; |
02d27d89 | 5220 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 5221 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 5222 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
5223 | /* if were using fast_iov, set it to the new one */ |
5224 | if (!async_msg->free_iov) | |
5225 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
5226 | ||
02d27d89 PB |
5227 | return -EAGAIN; |
5228 | } | |
5229 | ||
2ae523ed PB |
5230 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
5231 | struct io_async_msghdr *iomsg) | |
5232 | { | |
2ae523ed | 5233 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 5234 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 5235 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 5236 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
5237 | } |
5238 | ||
93642ef8 PB |
5239 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
5240 | { | |
5241 | int ret; | |
5242 | ||
93642ef8 PB |
5243 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
5244 | if (!ret) | |
5245 | req->flags |= REQ_F_NEED_CLEANUP; | |
5246 | return ret; | |
5247 | } | |
5248 | ||
3529d8c2 | 5249 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 5250 | { |
e47293fd | 5251 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 5252 | |
d2b6f48b PB |
5253 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5254 | return -EINVAL; | |
5255 | ||
270a5940 | 5256 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 5257 | sr->len = READ_ONCE(sqe->len); |
04411806 PB |
5258 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
5259 | if (sr->msg_flags & MSG_DONTWAIT) | |
5260 | req->flags |= REQ_F_NOWAIT; | |
3529d8c2 | 5261 | |
d8768362 JA |
5262 | #ifdef CONFIG_COMPAT |
5263 | if (req->ctx->compat) | |
5264 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
5265 | #endif | |
93642ef8 | 5266 | return 0; |
03b1230c JA |
5267 | } |
5268 | ||
889fca73 | 5269 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 5270 | { |
6b754c8b | 5271 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 5272 | struct socket *sock; |
7a7cacba | 5273 | unsigned flags; |
0031275d | 5274 | int min_ret = 0; |
0fa03c62 JA |
5275 | int ret; |
5276 | ||
dba4a925 | 5277 | sock = sock_from_file(req->file); |
7a7cacba | 5278 | if (unlikely(!sock)) |
dba4a925 | 5279 | return -ENOTSOCK; |
3529d8c2 | 5280 | |
d886e185 PB |
5281 | if (req_has_async_data(req)) { |
5282 | kmsg = req->async_data; | |
5283 | } else { | |
7a7cacba PB |
5284 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
5285 | if (ret) | |
5286 | return ret; | |
5287 | kmsg = &iomsg; | |
0fa03c62 | 5288 | } |
0fa03c62 | 5289 | |
04411806 PB |
5290 | flags = req->sr_msg.msg_flags; |
5291 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 5292 | flags |= MSG_DONTWAIT; |
0031275d SM |
5293 | if (flags & MSG_WAITALL) |
5294 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
5295 | ||
7a7cacba | 5296 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
0fa03c62 | 5297 | |
7297ce3d PB |
5298 | if (ret < min_ret) { |
5299 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) | |
5300 | return io_setup_async_msg(req, kmsg); | |
5301 | if (ret == -ERESTARTSYS) | |
5302 | ret = -EINTR; | |
5303 | req_set_fail(req); | |
5304 | } | |
257e84a5 PB |
5305 | /* fast path, check for non-NULL to avoid function call */ |
5306 | if (kmsg->free_iov) | |
5307 | kfree(kmsg->free_iov); | |
99bc4c38 | 5308 | req->flags &= ~REQ_F_NEED_CLEANUP; |
889fca73 | 5309 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 5310 | return 0; |
03b1230c | 5311 | } |
aa1fa28f | 5312 | |
889fca73 | 5313 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 5314 | { |
7a7cacba PB |
5315 | struct io_sr_msg *sr = &req->sr_msg; |
5316 | struct msghdr msg; | |
5317 | struct iovec iov; | |
fddaface | 5318 | struct socket *sock; |
7a7cacba | 5319 | unsigned flags; |
0031275d | 5320 | int min_ret = 0; |
fddaface JA |
5321 | int ret; |
5322 | ||
dba4a925 | 5323 | sock = sock_from_file(req->file); |
7a7cacba | 5324 | if (unlikely(!sock)) |
dba4a925 | 5325 | return -ENOTSOCK; |
fddaface | 5326 | |
7a7cacba PB |
5327 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
5328 | if (unlikely(ret)) | |
14db8411 | 5329 | return ret; |
fddaface | 5330 | |
7a7cacba PB |
5331 | msg.msg_name = NULL; |
5332 | msg.msg_control = NULL; | |
5333 | msg.msg_controllen = 0; | |
5334 | msg.msg_namelen = 0; | |
fddaface | 5335 | |
04411806 PB |
5336 | flags = req->sr_msg.msg_flags; |
5337 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 5338 | flags |= MSG_DONTWAIT; |
0031275d SM |
5339 | if (flags & MSG_WAITALL) |
5340 | min_ret = iov_iter_count(&msg.msg_iter); | |
5341 | ||
7a7cacba PB |
5342 | msg.msg_flags = flags; |
5343 | ret = sock_sendmsg(sock, &msg); | |
7297ce3d PB |
5344 | if (ret < min_ret) { |
5345 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) | |
5346 | return -EAGAIN; | |
5347 | if (ret == -ERESTARTSYS) | |
5348 | ret = -EINTR; | |
93d2bcd2 | 5349 | req_set_fail(req); |
7297ce3d | 5350 | } |
889fca73 | 5351 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 5352 | return 0; |
fddaface JA |
5353 | } |
5354 | ||
1400e697 PB |
5355 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
5356 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
5357 | { |
5358 | struct io_sr_msg *sr = &req->sr_msg; | |
5359 | struct iovec __user *uiov; | |
5360 | size_t iov_len; | |
5361 | int ret; | |
5362 | ||
1400e697 PB |
5363 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
5364 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
5365 | if (ret) |
5366 | return ret; | |
5367 | ||
5368 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
5369 | if (iov_len > 1) | |
5370 | return -EINVAL; | |
5476dfed | 5371 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 5372 | return -EFAULT; |
5476dfed | 5373 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 5374 | iomsg->free_iov = NULL; |
52de1fe1 | 5375 | } else { |
257e84a5 | 5376 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 5377 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 5378 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 5379 | false); |
52de1fe1 JA |
5380 | if (ret > 0) |
5381 | ret = 0; | |
5382 | } | |
5383 | ||
5384 | return ret; | |
5385 | } | |
5386 | ||
5387 | #ifdef CONFIG_COMPAT | |
5388 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 5389 | struct io_async_msghdr *iomsg) |
52de1fe1 | 5390 | { |
52de1fe1 JA |
5391 | struct io_sr_msg *sr = &req->sr_msg; |
5392 | struct compat_iovec __user *uiov; | |
5393 | compat_uptr_t ptr; | |
5394 | compat_size_t len; | |
5395 | int ret; | |
5396 | ||
4af3417a PB |
5397 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
5398 | &ptr, &len); | |
52de1fe1 JA |
5399 | if (ret) |
5400 | return ret; | |
5401 | ||
5402 | uiov = compat_ptr(ptr); | |
5403 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
5404 | compat_ssize_t clen; | |
5405 | ||
5406 | if (len > 1) | |
5407 | return -EINVAL; | |
5408 | if (!access_ok(uiov, sizeof(*uiov))) | |
5409 | return -EFAULT; | |
5410 | if (__get_user(clen, &uiov->iov_len)) | |
5411 | return -EFAULT; | |
5412 | if (clen < 0) | |
5413 | return -EINVAL; | |
2d280bc8 | 5414 | sr->len = clen; |
257e84a5 | 5415 | iomsg->free_iov = NULL; |
52de1fe1 | 5416 | } else { |
257e84a5 | 5417 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 5418 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 5419 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 5420 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
5421 | if (ret < 0) |
5422 | return ret; | |
5423 | } | |
5424 | ||
5425 | return 0; | |
5426 | } | |
5427 | #endif | |
5428 | ||
1400e697 PB |
5429 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
5430 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 5431 | { |
1400e697 | 5432 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
5433 | |
5434 | #ifdef CONFIG_COMPAT | |
5435 | if (req->ctx->compat) | |
1400e697 | 5436 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 5437 | #endif |
52de1fe1 | 5438 | |
1400e697 | 5439 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
5440 | } |
5441 | ||
bcda7baa | 5442 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
51aac424 | 5443 | unsigned int issue_flags) |
bcda7baa JA |
5444 | { |
5445 | struct io_sr_msg *sr = &req->sr_msg; | |
bcda7baa | 5446 | |
51aac424 | 5447 | return io_buffer_select(req, &sr->len, sr->bgid, issue_flags); |
fddaface JA |
5448 | } |
5449 | ||
93642ef8 | 5450 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 5451 | { |
99bc4c38 | 5452 | int ret; |
3529d8c2 | 5453 | |
93642ef8 PB |
5454 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
5455 | if (!ret) | |
5456 | req->flags |= REQ_F_NEED_CLEANUP; | |
5457 | return ret; | |
5458 | } | |
5459 | ||
5460 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
5461 | { | |
5462 | struct io_sr_msg *sr = &req->sr_msg; | |
5463 | ||
d2b6f48b PB |
5464 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5465 | return -EINVAL; | |
5466 | ||
270a5940 | 5467 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 5468 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 5469 | sr->bgid = READ_ONCE(sqe->buf_group); |
04411806 PB |
5470 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
5471 | if (sr->msg_flags & MSG_DONTWAIT) | |
5472 | req->flags |= REQ_F_NOWAIT; | |
06b76d44 | 5473 | |
d8768362 JA |
5474 | #ifdef CONFIG_COMPAT |
5475 | if (req->ctx->compat) | |
5476 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
5477 | #endif | |
7ba89d2a | 5478 | sr->done_io = 0; |
93642ef8 | 5479 | return 0; |
aa1fa28f JA |
5480 | } |
5481 | ||
7ba89d2a JA |
5482 | static bool io_net_retry(struct socket *sock, int flags) |
5483 | { | |
5484 | if (!(flags & MSG_WAITALL)) | |
5485 | return false; | |
5486 | return sock->type == SOCK_STREAM || sock->type == SOCK_SEQPACKET; | |
5487 | } | |
5488 | ||
889fca73 | 5489 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 5490 | { |
6b754c8b | 5491 | struct io_async_msghdr iomsg, *kmsg; |
7ba89d2a | 5492 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 5493 | struct socket *sock; |
7fbb1b54 | 5494 | struct io_buffer *kbuf; |
7a7cacba | 5495 | unsigned flags; |
d1fd1c20 | 5496 | int ret, min_ret = 0; |
45d189c6 | 5497 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 5498 | |
dba4a925 | 5499 | sock = sock_from_file(req->file); |
7a7cacba | 5500 | if (unlikely(!sock)) |
dba4a925 | 5501 | return -ENOTSOCK; |
3529d8c2 | 5502 | |
d886e185 PB |
5503 | if (req_has_async_data(req)) { |
5504 | kmsg = req->async_data; | |
5505 | } else { | |
7a7cacba PB |
5506 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
5507 | if (ret) | |
681fda8d | 5508 | return ret; |
7a7cacba PB |
5509 | kmsg = &iomsg; |
5510 | } | |
03b1230c | 5511 | |
bc02ef33 | 5512 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 5513 | kbuf = io_recv_buffer_select(req, issue_flags); |
bc02ef33 | 5514 | if (IS_ERR(kbuf)) |
52de1fe1 | 5515 | return PTR_ERR(kbuf); |
7a7cacba | 5516 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
5517 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
5518 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
5519 | 1, req->sr_msg.len); |
5520 | } | |
52de1fe1 | 5521 | |
04411806 PB |
5522 | flags = req->sr_msg.msg_flags; |
5523 | if (force_nonblock) | |
7a7cacba | 5524 | flags |= MSG_DONTWAIT; |
0031275d SM |
5525 | if (flags & MSG_WAITALL) |
5526 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
5527 | ||
7a7cacba PB |
5528 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
5529 | kmsg->uaddr, flags); | |
7297ce3d PB |
5530 | if (ret < min_ret) { |
5531 | if (ret == -EAGAIN && force_nonblock) | |
5532 | return io_setup_async_msg(req, kmsg); | |
5533 | if (ret == -ERESTARTSYS) | |
5534 | ret = -EINTR; | |
7ba89d2a JA |
5535 | if (ret > 0 && io_net_retry(sock, flags)) { |
5536 | sr->done_io += ret; | |
8a3e8ee5 | 5537 | req->flags |= REQ_F_PARTIAL_IO; |
7ba89d2a JA |
5538 | return io_setup_async_msg(req, kmsg); |
5539 | } | |
7297ce3d PB |
5540 | req_set_fail(req); |
5541 | } else if ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { | |
5542 | req_set_fail(req); | |
5543 | } | |
03b1230c | 5544 | |
257e84a5 PB |
5545 | /* fast path, check for non-NULL to avoid function call */ |
5546 | if (kmsg->free_iov) | |
5547 | kfree(kmsg->free_iov); | |
99bc4c38 | 5548 | req->flags &= ~REQ_F_NEED_CLEANUP; |
7ba89d2a JA |
5549 | if (ret >= 0) |
5550 | ret += sr->done_io; | |
5551 | else if (sr->done_io) | |
5552 | ret = sr->done_io; | |
cc3cec83 | 5553 | __io_req_complete(req, issue_flags, ret, io_put_kbuf(req, issue_flags)); |
03b1230c | 5554 | return 0; |
0fa03c62 | 5555 | } |
5d17b4a4 | 5556 | |
889fca73 | 5557 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 5558 | { |
6b754c8b | 5559 | struct io_buffer *kbuf; |
7a7cacba PB |
5560 | struct io_sr_msg *sr = &req->sr_msg; |
5561 | struct msghdr msg; | |
5562 | void __user *buf = sr->buf; | |
fddaface | 5563 | struct socket *sock; |
7a7cacba PB |
5564 | struct iovec iov; |
5565 | unsigned flags; | |
d1fd1c20 | 5566 | int ret, min_ret = 0; |
45d189c6 | 5567 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 5568 | |
dba4a925 | 5569 | sock = sock_from_file(req->file); |
7a7cacba | 5570 | if (unlikely(!sock)) |
dba4a925 | 5571 | return -ENOTSOCK; |
fddaface | 5572 | |
bc02ef33 | 5573 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 5574 | kbuf = io_recv_buffer_select(req, issue_flags); |
bcda7baa JA |
5575 | if (IS_ERR(kbuf)) |
5576 | return PTR_ERR(kbuf); | |
7a7cacba | 5577 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 5578 | } |
bcda7baa | 5579 | |
7a7cacba | 5580 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
5581 | if (unlikely(ret)) |
5582 | goto out_free; | |
fddaface | 5583 | |
7a7cacba PB |
5584 | msg.msg_name = NULL; |
5585 | msg.msg_control = NULL; | |
5586 | msg.msg_controllen = 0; | |
5587 | msg.msg_namelen = 0; | |
5588 | msg.msg_iocb = NULL; | |
5589 | msg.msg_flags = 0; | |
fddaface | 5590 | |
04411806 PB |
5591 | flags = req->sr_msg.msg_flags; |
5592 | if (force_nonblock) | |
7a7cacba | 5593 | flags |= MSG_DONTWAIT; |
0031275d SM |
5594 | if (flags & MSG_WAITALL) |
5595 | min_ret = iov_iter_count(&msg.msg_iter); | |
5596 | ||
7a7cacba | 5597 | ret = sock_recvmsg(sock, &msg, flags); |
7297ce3d PB |
5598 | if (ret < min_ret) { |
5599 | if (ret == -EAGAIN && force_nonblock) | |
5600 | return -EAGAIN; | |
5601 | if (ret == -ERESTARTSYS) | |
5602 | ret = -EINTR; | |
7ba89d2a JA |
5603 | if (ret > 0 && io_net_retry(sock, flags)) { |
5604 | sr->len -= ret; | |
5605 | sr->buf += ret; | |
5606 | sr->done_io += ret; | |
8a3e8ee5 | 5607 | req->flags |= REQ_F_PARTIAL_IO; |
7ba89d2a JA |
5608 | return -EAGAIN; |
5609 | } | |
7297ce3d PB |
5610 | req_set_fail(req); |
5611 | } else if ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { | |
0d7c1153 | 5612 | out_free: |
93d2bcd2 | 5613 | req_set_fail(req); |
7297ce3d | 5614 | } |
cc3cec83 | 5615 | |
7ba89d2a JA |
5616 | if (ret >= 0) |
5617 | ret += sr->done_io; | |
5618 | else if (sr->done_io) | |
5619 | ret = sr->done_io; | |
cc3cec83 | 5620 | __io_req_complete(req, issue_flags, ret, io_put_kbuf(req, issue_flags)); |
fddaface | 5621 | return 0; |
fddaface JA |
5622 | } |
5623 | ||
3529d8c2 | 5624 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 5625 | { |
8ed8d3c3 JA |
5626 | struct io_accept *accept = &req->accept; |
5627 | ||
14587a46 | 5628 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 5629 | return -EINVAL; |
aaa4db12 | 5630 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
5631 | return -EINVAL; |
5632 | ||
d55e5f5b JA |
5633 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5634 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 5635 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 5636 | accept->nofile = rlimit(RLIMIT_NOFILE); |
a7083ad5 | 5637 | |
aaa4db12 | 5638 | accept->file_slot = READ_ONCE(sqe->file_index); |
adf3a9e9 | 5639 | if (accept->file_slot && (accept->flags & SOCK_CLOEXEC)) |
aaa4db12 | 5640 | return -EINVAL; |
a7083ad5 PB |
5641 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
5642 | return -EINVAL; | |
5643 | if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) | |
5644 | accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; | |
8ed8d3c3 | 5645 | return 0; |
8ed8d3c3 | 5646 | } |
17f2fe35 | 5647 | |
889fca73 | 5648 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
5649 | { |
5650 | struct io_accept *accept = &req->accept; | |
45d189c6 | 5651 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 5652 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
aaa4db12 | 5653 | bool fixed = !!accept->file_slot; |
a7083ad5 PB |
5654 | struct file *file; |
5655 | int ret, fd; | |
8ed8d3c3 | 5656 | |
aaa4db12 PB |
5657 | if (!fixed) { |
5658 | fd = __get_unused_fd_flags(accept->flags, accept->nofile); | |
5659 | if (unlikely(fd < 0)) | |
5660 | return fd; | |
5661 | } | |
a7083ad5 PB |
5662 | file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, |
5663 | accept->flags); | |
5664 | if (IS_ERR(file)) { | |
aaa4db12 PB |
5665 | if (!fixed) |
5666 | put_unused_fd(fd); | |
a7083ad5 PB |
5667 | ret = PTR_ERR(file); |
5668 | if (ret == -EAGAIN && force_nonblock) | |
5669 | return -EAGAIN; | |
ac45abc0 PB |
5670 | if (ret == -ERESTARTSYS) |
5671 | ret = -EINTR; | |
93d2bcd2 | 5672 | req_set_fail(req); |
aaa4db12 | 5673 | } else if (!fixed) { |
a7083ad5 PB |
5674 | fd_install(fd, file); |
5675 | ret = fd; | |
aaa4db12 PB |
5676 | } else { |
5677 | ret = io_install_fixed_file(req, file, issue_flags, | |
5678 | accept->file_slot - 1); | |
ac45abc0 | 5679 | } |
889fca73 | 5680 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 5681 | return 0; |
8ed8d3c3 JA |
5682 | } |
5683 | ||
93642ef8 PB |
5684 | static int io_connect_prep_async(struct io_kiocb *req) |
5685 | { | |
5686 | struct io_async_connect *io = req->async_data; | |
5687 | struct io_connect *conn = &req->connect; | |
5688 | ||
5689 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
5690 | } | |
5691 | ||
3529d8c2 | 5692 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 5693 | { |
3529d8c2 | 5694 | struct io_connect *conn = &req->connect; |
f499a021 | 5695 | |
14587a46 | 5696 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 | 5697 | return -EINVAL; |
26578cda PB |
5698 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
5699 | sqe->splice_fd_in) | |
3fbb51c1 JA |
5700 | return -EINVAL; |
5701 | ||
3529d8c2 JA |
5702 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5703 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 5704 | return 0; |
f499a021 JA |
5705 | } |
5706 | ||
889fca73 | 5707 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 5708 | { |
e8c2bc1f | 5709 | struct io_async_connect __io, *io; |
f8e85cf2 | 5710 | unsigned file_flags; |
3fbb51c1 | 5711 | int ret; |
45d189c6 | 5712 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 5713 | |
d886e185 | 5714 | if (req_has_async_data(req)) { |
e8c2bc1f | 5715 | io = req->async_data; |
f499a021 | 5716 | } else { |
3529d8c2 JA |
5717 | ret = move_addr_to_kernel(req->connect.addr, |
5718 | req->connect.addr_len, | |
e8c2bc1f | 5719 | &__io.address); |
f499a021 JA |
5720 | if (ret) |
5721 | goto out; | |
5722 | io = &__io; | |
5723 | } | |
5724 | ||
3fbb51c1 JA |
5725 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
5726 | ||
e8c2bc1f | 5727 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 5728 | req->connect.addr_len, file_flags); |
87f80d62 | 5729 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
d886e185 | 5730 | if (req_has_async_data(req)) |
b7bb4f7d | 5731 | return -EAGAIN; |
e8c2bc1f | 5732 | if (io_alloc_async_data(req)) { |
f499a021 JA |
5733 | ret = -ENOMEM; |
5734 | goto out; | |
5735 | } | |
e8c2bc1f | 5736 | memcpy(req->async_data, &__io, sizeof(__io)); |
f8e85cf2 | 5737 | return -EAGAIN; |
f499a021 | 5738 | } |
f8e85cf2 JA |
5739 | if (ret == -ERESTARTSYS) |
5740 | ret = -EINTR; | |
f499a021 | 5741 | out: |
4e88d6e7 | 5742 | if (ret < 0) |
93d2bcd2 | 5743 | req_set_fail(req); |
889fca73 | 5744 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 5745 | return 0; |
469956e8 Y |
5746 | } |
5747 | #else /* !CONFIG_NET */ | |
99a10081 JA |
5748 | #define IO_NETOP_FN(op) \ |
5749 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
5750 | { \ | |
5751 | return -EOPNOTSUPP; \ | |
5752 | } | |
5753 | ||
5754 | #define IO_NETOP_PREP(op) \ | |
5755 | IO_NETOP_FN(op) \ | |
5756 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
5757 | { \ | |
5758 | return -EOPNOTSUPP; \ | |
5759 | } \ | |
5760 | ||
5761 | #define IO_NETOP_PREP_ASYNC(op) \ | |
5762 | IO_NETOP_PREP(op) \ | |
5763 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
5764 | { \ | |
5765 | return -EOPNOTSUPP; \ | |
5766 | } | |
5767 | ||
5768 | IO_NETOP_PREP_ASYNC(sendmsg); | |
5769 | IO_NETOP_PREP_ASYNC(recvmsg); | |
5770 | IO_NETOP_PREP_ASYNC(connect); | |
5771 | IO_NETOP_PREP(accept); | |
5772 | IO_NETOP_FN(send); | |
5773 | IO_NETOP_FN(recv); | |
469956e8 | 5774 | #endif /* CONFIG_NET */ |
f8e85cf2 | 5775 | |
d7718a9d JA |
5776 | struct io_poll_table { |
5777 | struct poll_table_struct pt; | |
5778 | struct io_kiocb *req; | |
68b11e8b | 5779 | int nr_entries; |
d7718a9d JA |
5780 | int error; |
5781 | }; | |
ce593a6c | 5782 | |
aa43477b | 5783 | #define IO_POLL_CANCEL_FLAG BIT(31) |
e2c0cb7c | 5784 | #define IO_POLL_REF_MASK GENMASK(30, 0) |
6d816e08 | 5785 | |
aa43477b PB |
5786 | /* |
5787 | * If refs part of ->poll_refs (see IO_POLL_REF_MASK) is 0, it's free. We can | |
5788 | * bump it and acquire ownership. It's disallowed to modify requests while not | |
5789 | * owning it, that prevents from races for enqueueing task_work's and b/w | |
5790 | * arming poll and wakeups. | |
5791 | */ | |
5792 | static inline bool io_poll_get_ownership(struct io_kiocb *req) | |
5793 | { | |
5794 | return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK); | |
d7718a9d JA |
5795 | } |
5796 | ||
aa43477b | 5797 | static void io_poll_mark_cancelled(struct io_kiocb *req) |
74ce6ce4 | 5798 | { |
aa43477b | 5799 | atomic_or(IO_POLL_CANCEL_FLAG, &req->poll_refs); |
74ce6ce4 JA |
5800 | } |
5801 | ||
d4e7cd36 | 5802 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5803 | { |
e8c2bc1f | 5804 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5805 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5806 | return req->async_data; |
d4e7cd36 JA |
5807 | return req->apoll->double_poll; |
5808 | } | |
5809 | ||
5810 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5811 | { | |
5812 | if (req->opcode == IORING_OP_POLL_ADD) | |
5813 | return &req->poll; | |
5814 | return &req->apoll->poll; | |
5815 | } | |
5816 | ||
5641897a | 5817 | static void io_poll_req_insert(struct io_kiocb *req) |
d4e7cd36 | 5818 | { |
5641897a PB |
5819 | struct io_ring_ctx *ctx = req->ctx; |
5820 | struct hlist_head *list; | |
18bceab1 | 5821 | |
cef216fc | 5822 | list = &ctx->cancel_hash[hash_long(req->cqe.user_data, ctx->cancel_hash_bits)]; |
5641897a | 5823 | hlist_add_head(&req->hash_node, list); |
18bceab1 JA |
5824 | } |
5825 | ||
5641897a PB |
5826 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
5827 | wait_queue_func_t wake_func) | |
18bceab1 | 5828 | { |
5641897a | 5829 | poll->head = NULL; |
5641897a PB |
5830 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
5831 | /* mask in events that we always want/need */ | |
5832 | poll->events = events | IO_POLL_UNMASK; | |
5833 | INIT_LIST_HEAD(&poll->wait.entry); | |
5834 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
18bceab1 JA |
5835 | } |
5836 | ||
aa43477b | 5837 | static inline void io_poll_remove_entry(struct io_poll_iocb *poll) |
18bceab1 | 5838 | { |
791f3465 | 5839 | struct wait_queue_head *head = smp_load_acquire(&poll->head); |
18bceab1 | 5840 | |
791f3465 PB |
5841 | if (head) { |
5842 | spin_lock_irq(&head->lock); | |
5843 | list_del_init(&poll->wait.entry); | |
5844 | poll->head = NULL; | |
5845 | spin_unlock_irq(&head->lock); | |
5846 | } | |
aa43477b | 5847 | } |
18bceab1 | 5848 | |
aa43477b PB |
5849 | static void io_poll_remove_entries(struct io_kiocb *req) |
5850 | { | |
91eac1c6 JA |
5851 | /* |
5852 | * Nothing to do if neither of those flags are set. Avoid dipping | |
5853 | * into the poll/apoll/double cachelines if we can. | |
5854 | */ | |
5855 | if (!(req->flags & (REQ_F_SINGLE_POLL | REQ_F_DOUBLE_POLL))) | |
5856 | return; | |
18bceab1 | 5857 | |
791f3465 PB |
5858 | /* |
5859 | * While we hold the waitqueue lock and the waitqueue is nonempty, | |
5860 | * wake_up_pollfree() will wait for us. However, taking the waitqueue | |
5861 | * lock in the first place can race with the waitqueue being freed. | |
5862 | * | |
5863 | * We solve this as eventpoll does: by taking advantage of the fact that | |
5864 | * all users of wake_up_pollfree() will RCU-delay the actual free. If | |
5865 | * we enter rcu_read_lock() and see that the pointer to the queue is | |
5866 | * non-NULL, we can then lock it without the memory being freed out from | |
5867 | * under us. | |
5868 | * | |
5869 | * Keep holding rcu_read_lock() as long as we hold the queue lock, in | |
5870 | * case the caller deletes the entry from the queue, leaving it empty. | |
5871 | * In that case, only RCU prevents the queue memory from being freed. | |
5872 | */ | |
5873 | rcu_read_lock(); | |
91eac1c6 JA |
5874 | if (req->flags & REQ_F_SINGLE_POLL) |
5875 | io_poll_remove_entry(io_poll_get_single(req)); | |
5876 | if (req->flags & REQ_F_DOUBLE_POLL) | |
5877 | io_poll_remove_entry(io_poll_get_double(req)); | |
791f3465 | 5878 | rcu_read_unlock(); |
18bceab1 JA |
5879 | } |
5880 | ||
aa43477b PB |
5881 | /* |
5882 | * All poll tw should go through this. Checks for poll events, manages | |
5883 | * references, does rewait, etc. | |
5884 | * | |
5885 | * Returns a negative error on failure. >0 when no action require, which is | |
5886 | * either spurious wakeup or multishot CQE is served. 0 when it's done with | |
cef216fc | 5887 | * the request, then the mask is stored in req->cqe.res. |
aa43477b | 5888 | */ |
5106dd6e | 5889 | static int io_poll_check_events(struct io_kiocb *req, bool locked) |
18bceab1 | 5890 | { |
74ce6ce4 | 5891 | struct io_ring_ctx *ctx = req->ctx; |
aa43477b | 5892 | int v; |
18bceab1 | 5893 | |
316319e8 | 5894 | /* req->task == current here, checking PF_EXITING is safe */ |
e09ee510 | 5895 | if (unlikely(req->task->flags & PF_EXITING)) |
f2219057 | 5896 | return -ECANCELED; |
18bceab1 | 5897 | |
aa43477b PB |
5898 | do { |
5899 | v = atomic_read(&req->poll_refs); | |
74ce6ce4 | 5900 | |
aa43477b PB |
5901 | /* tw handler should be the owner, and so have some references */ |
5902 | if (WARN_ON_ONCE(!(v & IO_POLL_REF_MASK))) | |
5903 | return 0; | |
5904 | if (v & IO_POLL_CANCEL_FLAG) | |
5905 | return -ECANCELED; | |
8706e04e | 5906 | |
cef216fc | 5907 | if (!req->cqe.res) { |
2804ecd8 | 5908 | struct poll_table_struct pt = { ._key = req->apoll_events }; |
cce64ef0 | 5909 | unsigned flags = locked ? 0 : IO_URING_F_UNLOCKED; |
18bceab1 | 5910 | |
cce64ef0 | 5911 | if (unlikely(!io_assign_file(req, flags))) |
7179c3ce | 5912 | return -EBADF; |
cef216fc | 5913 | req->cqe.res = vfs_poll(req->file, &pt) & req->apoll_events; |
c8b5e260 | 5914 | } |
74ce6ce4 | 5915 | |
aa43477b | 5916 | /* multishot, just fill an CQE and proceed */ |
cef216fc PB |
5917 | if (req->cqe.res && !(req->apoll_events & EPOLLONESHOT)) { |
5918 | __poll_t mask = mangle_poll(req->cqe.res & req->apoll_events); | |
aa43477b | 5919 | bool filled; |
18bceab1 | 5920 | |
aa43477b | 5921 | spin_lock(&ctx->completion_lock); |
cef216fc | 5922 | filled = io_fill_cqe_aux(ctx, req->cqe.user_data, mask, |
aa43477b PB |
5923 | IORING_CQE_F_MORE); |
5924 | io_commit_cqring(ctx); | |
5925 | spin_unlock(&ctx->completion_lock); | |
5926 | if (unlikely(!filled)) | |
5927 | return -ECANCELED; | |
5928 | io_cqring_ev_posted(ctx); | |
cef216fc | 5929 | } else if (req->cqe.res) { |
aa43477b PB |
5930 | return 0; |
5931 | } | |
18bceab1 | 5932 | |
aa43477b PB |
5933 | /* |
5934 | * Release all references, retry if someone tried to restart | |
5935 | * task_work while we were executing it. | |
5936 | */ | |
5937 | } while (atomic_sub_return(v & IO_POLL_REF_MASK, &req->poll_refs)); | |
18bceab1 | 5938 | |
18bceab1 JA |
5939 | return 1; |
5940 | } | |
5941 | ||
aa43477b | 5942 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 | 5943 | { |
18bceab1 | 5944 | struct io_ring_ctx *ctx = req->ctx; |
aa43477b | 5945 | int ret; |
18bceab1 | 5946 | |
5106dd6e | 5947 | ret = io_poll_check_events(req, *locked); |
aa43477b PB |
5948 | if (ret > 0) |
5949 | return; | |
5950 | ||
5951 | if (!ret) { | |
cef216fc | 5952 | req->cqe.res = mangle_poll(req->cqe.res & req->poll.events); |
e27414be | 5953 | } else { |
cef216fc | 5954 | req->cqe.res = ret; |
aa43477b | 5955 | req_set_fail(req); |
a62682f9 | 5956 | } |
aa43477b PB |
5957 | |
5958 | io_poll_remove_entries(req); | |
5959 | spin_lock(&ctx->completion_lock); | |
5960 | hash_del(&req->hash_node); | |
cef216fc | 5961 | __io_req_complete_post(req, req->cqe.res, 0); |
aa43477b PB |
5962 | io_commit_cqring(ctx); |
5963 | spin_unlock(&ctx->completion_lock); | |
5964 | io_cqring_ev_posted(ctx); | |
18bceab1 JA |
5965 | } |
5966 | ||
aa43477b | 5967 | static void io_apoll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 JA |
5968 | { |
5969 | struct io_ring_ctx *ctx = req->ctx; | |
aa43477b | 5970 | int ret; |
18bceab1 | 5971 | |
5106dd6e | 5972 | ret = io_poll_check_events(req, *locked); |
aa43477b PB |
5973 | if (ret > 0) |
5974 | return; | |
18bceab1 | 5975 | |
aa43477b PB |
5976 | io_poll_remove_entries(req); |
5977 | spin_lock(&ctx->completion_lock); | |
5978 | hash_del(&req->hash_node); | |
5979 | spin_unlock(&ctx->completion_lock); | |
18bceab1 | 5980 | |
aa43477b PB |
5981 | if (!ret) |
5982 | io_req_task_submit(req, locked); | |
5983 | else | |
5984 | io_req_complete_failed(req, ret); | |
18bceab1 JA |
5985 | } |
5986 | ||
81459350 | 5987 | static void __io_poll_execute(struct io_kiocb *req, int mask, int events) |
aa43477b | 5988 | { |
cef216fc | 5989 | req->cqe.res = mask; |
81459350 JA |
5990 | /* |
5991 | * This is useful for poll that is armed on behalf of another | |
5992 | * request, and where the wakeup path could be on a different | |
5993 | * CPU. We want to avoid pulling in req->apoll->events for that | |
5994 | * case. | |
5995 | */ | |
2804ecd8 | 5996 | req->apoll_events = events; |
aa43477b PB |
5997 | if (req->opcode == IORING_OP_POLL_ADD) |
5998 | req->io_task_work.func = io_poll_task_func; | |
5999 | else | |
6000 | req->io_task_work.func = io_apoll_task_func; | |
6001 | ||
cef216fc | 6002 | trace_io_uring_task_add(req->ctx, req, req->cqe.user_data, req->opcode, mask); |
aa43477b PB |
6003 | io_req_task_work_add(req, false); |
6004 | } | |
6005 | ||
81459350 | 6006 | static inline void io_poll_execute(struct io_kiocb *req, int res, int events) |
aa43477b PB |
6007 | { |
6008 | if (io_poll_get_ownership(req)) | |
81459350 | 6009 | __io_poll_execute(req, res, events); |
aa43477b PB |
6010 | } |
6011 | ||
6012 | static void io_poll_cancel_req(struct io_kiocb *req) | |
6013 | { | |
6014 | io_poll_mark_cancelled(req); | |
6015 | /* kick tw, which should complete the request */ | |
81459350 | 6016 | io_poll_execute(req, 0, 0); |
aa43477b PB |
6017 | } |
6018 | ||
d89a4fac JA |
6019 | #define wqe_to_req(wait) ((void *)((unsigned long) (wait)->private & ~1)) |
6020 | #define wqe_is_double(wait) ((unsigned long) (wait)->private & 1) | |
6021 | ||
aa43477b PB |
6022 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
6023 | void *key) | |
18bceab1 | 6024 | { |
d89a4fac | 6025 | struct io_kiocb *req = wqe_to_req(wait); |
aa43477b PB |
6026 | struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb, |
6027 | wait); | |
18bceab1 JA |
6028 | __poll_t mask = key_to_poll(key); |
6029 | ||
791f3465 PB |
6030 | if (unlikely(mask & POLLFREE)) { |
6031 | io_poll_mark_cancelled(req); | |
6032 | /* we have to kick tw in case it's not already */ | |
81459350 | 6033 | io_poll_execute(req, 0, poll->events); |
791f3465 PB |
6034 | |
6035 | /* | |
6036 | * If the waitqueue is being freed early but someone is already | |
6037 | * holds ownership over it, we have to tear down the request as | |
6038 | * best we can. That means immediately removing the request from | |
6039 | * its waitqueue and preventing all further accesses to the | |
6040 | * waitqueue via the request. | |
6041 | */ | |
6042 | list_del_init(&poll->wait.entry); | |
6043 | ||
6044 | /* | |
6045 | * Careful: this *must* be the last step, since as soon | |
6046 | * as req->head is NULL'ed out, the request can be | |
6047 | * completed and freed, since aio_poll_complete_work() | |
6048 | * will no longer need to take the waitqueue lock. | |
6049 | */ | |
6050 | smp_store_release(&poll->head, NULL); | |
6051 | return 1; | |
6052 | } | |
6053 | ||
aa43477b | 6054 | /* for instances that support it check for an event match first */ |
18bceab1 JA |
6055 | if (mask && !(mask & poll->events)) |
6056 | return 0; | |
6057 | ||
eb0089d6 PB |
6058 | if (io_poll_get_ownership(req)) { |
6059 | /* optional, saves extra locking for removal in tw handler */ | |
6060 | if (mask && poll->events & EPOLLONESHOT) { | |
6061 | list_del_init(&poll->wait.entry); | |
6062 | poll->head = NULL; | |
d89a4fac JA |
6063 | if (wqe_is_double(wait)) |
6064 | req->flags &= ~REQ_F_DOUBLE_POLL; | |
6065 | else | |
6066 | req->flags &= ~REQ_F_SINGLE_POLL; | |
eb0089d6 | 6067 | } |
81459350 | 6068 | __io_poll_execute(req, mask, poll->events); |
eb0089d6 | 6069 | } |
18bceab1 | 6070 | return 1; |
18bceab1 JA |
6071 | } |
6072 | ||
6073 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
6074 | struct wait_queue_head *head, |
6075 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
6076 | { |
6077 | struct io_kiocb *req = pt->req; | |
d89a4fac | 6078 | unsigned long wqe_private = (unsigned long) req; |
18bceab1 JA |
6079 | |
6080 | /* | |
68b11e8b PB |
6081 | * The file being polled uses multiple waitqueues for poll handling |
6082 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb | |
6083 | * if this happens. | |
18bceab1 | 6084 | */ |
68b11e8b | 6085 | if (unlikely(pt->nr_entries)) { |
aa43477b | 6086 | struct io_poll_iocb *first = poll; |
58852d4d | 6087 | |
23a65db8 | 6088 | /* double add on the same waitqueue head, ignore */ |
aa43477b | 6089 | if (first->head == head) |
23a65db8 | 6090 | return; |
18bceab1 | 6091 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 6092 | if (*poll_ptr) { |
23a65db8 PB |
6093 | if ((*poll_ptr)->head == head) |
6094 | return; | |
18bceab1 JA |
6095 | pt->error = -EINVAL; |
6096 | return; | |
6097 | } | |
aa43477b | 6098 | |
18bceab1 JA |
6099 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
6100 | if (!poll) { | |
6101 | pt->error = -ENOMEM; | |
6102 | return; | |
6103 | } | |
d89a4fac JA |
6104 | /* mark as double wq entry */ |
6105 | wqe_private |= 1; | |
91eac1c6 | 6106 | req->flags |= REQ_F_DOUBLE_POLL; |
aa43477b | 6107 | io_init_poll_iocb(poll, first->events, first->wait.func); |
807abcb0 | 6108 | *poll_ptr = poll; |
d886e185 PB |
6109 | if (req->opcode == IORING_OP_POLL_ADD) |
6110 | req->flags |= REQ_F_ASYNC_DATA; | |
18bceab1 JA |
6111 | } |
6112 | ||
91eac1c6 | 6113 | req->flags |= REQ_F_SINGLE_POLL; |
68b11e8b | 6114 | pt->nr_entries++; |
18bceab1 | 6115 | poll->head = head; |
d89a4fac | 6116 | poll->wait.private = (void *) wqe_private; |
a31eb4a2 JX |
6117 | |
6118 | if (poll->events & EPOLLEXCLUSIVE) | |
6119 | add_wait_queue_exclusive(head, &poll->wait); | |
6120 | else | |
6121 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
6122 | } |
6123 | ||
aa43477b | 6124 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
18bceab1 JA |
6125 | struct poll_table_struct *p) |
6126 | { | |
6127 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
d7718a9d | 6128 | |
aa43477b PB |
6129 | __io_queue_proc(&pt->req->poll, pt, head, |
6130 | (struct io_poll_iocb **) &pt->req->async_data); | |
d7718a9d JA |
6131 | } |
6132 | ||
aa43477b PB |
6133 | static int __io_arm_poll_handler(struct io_kiocb *req, |
6134 | struct io_poll_iocb *poll, | |
6135 | struct io_poll_table *ipt, __poll_t mask) | |
d7718a9d JA |
6136 | { |
6137 | struct io_ring_ctx *ctx = req->ctx; | |
aa43477b | 6138 | int v; |
d7718a9d | 6139 | |
4d52f338 | 6140 | INIT_HLIST_NODE(&req->hash_node); |
aa43477b | 6141 | io_init_poll_iocb(poll, mask, io_poll_wake); |
b90cd197 | 6142 | poll->file = req->file; |
d7718a9d JA |
6143 | |
6144 | ipt->pt._key = mask; | |
6145 | ipt->req = req; | |
68b11e8b PB |
6146 | ipt->error = 0; |
6147 | ipt->nr_entries = 0; | |
d7718a9d | 6148 | |
aa43477b PB |
6149 | /* |
6150 | * Take the ownership to delay any tw execution up until we're done | |
6151 | * with poll arming. see io_poll_get_ownership(). | |
6152 | */ | |
6153 | atomic_set(&req->poll_refs, 1); | |
d7718a9d | 6154 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
aa43477b PB |
6155 | |
6156 | if (mask && (poll->events & EPOLLONESHOT)) { | |
6157 | io_poll_remove_entries(req); | |
6158 | /* no one else has access to the req, forget about the ref */ | |
6159 | return mask; | |
6160 | } | |
6161 | if (!mask && unlikely(ipt->error || !ipt->nr_entries)) { | |
6162 | io_poll_remove_entries(req); | |
6163 | if (!ipt->error) | |
6164 | ipt->error = -EINVAL; | |
6165 | return 0; | |
6166 | } | |
d7718a9d | 6167 | |
79ebeaee | 6168 | spin_lock(&ctx->completion_lock); |
aa43477b PB |
6169 | io_poll_req_insert(req); |
6170 | spin_unlock(&ctx->completion_lock); | |
6171 | ||
6172 | if (mask) { | |
6173 | /* can't multishot if failed, just queue the event we've got */ | |
6174 | if (unlikely(ipt->error || !ipt->nr_entries)) | |
6175 | poll->events |= EPOLLONESHOT; | |
81459350 | 6176 | __io_poll_execute(req, mask, poll->events); |
aa43477b | 6177 | return 0; |
d7718a9d JA |
6178 | } |
6179 | ||
aa43477b PB |
6180 | /* |
6181 | * Release ownership. If someone tried to queue a tw while it was | |
6182 | * locked, kick it off for them. | |
6183 | */ | |
6184 | v = atomic_dec_return(&req->poll_refs); | |
6185 | if (unlikely(v & IO_POLL_REF_MASK)) | |
81459350 | 6186 | __io_poll_execute(req, 0, poll->events); |
aa43477b PB |
6187 | return 0; |
6188 | } | |
6189 | ||
6190 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
6191 | struct poll_table_struct *p) | |
6192 | { | |
6193 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
6194 | struct async_poll *apoll = pt->req->apoll; | |
6195 | ||
6196 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); | |
d7718a9d JA |
6197 | } |
6198 | ||
59b735ae OL |
6199 | enum { |
6200 | IO_APOLL_OK, | |
6201 | IO_APOLL_ABORTED, | |
6202 | IO_APOLL_READY | |
6203 | }; | |
6204 | ||
4d9237e3 | 6205 | static int io_arm_poll_handler(struct io_kiocb *req, unsigned issue_flags) |
d7718a9d JA |
6206 | { |
6207 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
6208 | struct io_ring_ctx *ctx = req->ctx; | |
6209 | struct async_poll *apoll; | |
6210 | struct io_poll_table ipt; | |
aa43477b PB |
6211 | __poll_t mask = EPOLLONESHOT | POLLERR | POLLPRI; |
6212 | int ret; | |
d7718a9d | 6213 | |
b2d9c3da PB |
6214 | if (!def->pollin && !def->pollout) |
6215 | return IO_APOLL_ABORTED; | |
658d0a40 PB |
6216 | if (!file_can_poll(req->file) || (req->flags & REQ_F_POLLED)) |
6217 | return IO_APOLL_ABORTED; | |
b2d9c3da PB |
6218 | |
6219 | if (def->pollin) { | |
b2d9c3da PB |
6220 | mask |= POLLIN | POLLRDNORM; |
6221 | ||
6222 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
6223 | if ((req->opcode == IORING_OP_RECVMSG) && | |
6224 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
6225 | mask &= ~POLLIN; | |
6226 | } else { | |
b2d9c3da PB |
6227 | mask |= POLLOUT | POLLWRNORM; |
6228 | } | |
52dd8640 DY |
6229 | if (def->poll_exclusive) |
6230 | mask |= EPOLLEXCLUSIVE; | |
4d9237e3 JA |
6231 | if (!(issue_flags & IO_URING_F_UNLOCKED) && |
6232 | !list_empty(&ctx->apoll_cache)) { | |
6233 | apoll = list_first_entry(&ctx->apoll_cache, struct async_poll, | |
6234 | poll.wait.entry); | |
6235 | list_del_init(&apoll->poll.wait.entry); | |
6236 | } else { | |
6237 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
6238 | if (unlikely(!apoll)) | |
6239 | return IO_APOLL_ABORTED; | |
6240 | } | |
807abcb0 | 6241 | apoll->double_poll = NULL; |
d7718a9d | 6242 | req->apoll = apoll; |
b2d9c3da | 6243 | req->flags |= REQ_F_POLLED; |
d7718a9d JA |
6244 | ipt.pt._qproc = io_async_queue_proc; |
6245 | ||
4d55f238 | 6246 | io_kbuf_recycle(req, issue_flags); |
abdad709 | 6247 | |
aa43477b | 6248 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask); |
41a5169c HX |
6249 | if (ret || ipt.error) |
6250 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; | |
6251 | ||
cef216fc | 6252 | trace_io_uring_poll_arm(ctx, req, req->cqe.user_data, req->opcode, |
236daeae | 6253 | mask, apoll->poll.events); |
59b735ae | 6254 | return IO_APOLL_OK; |
d7718a9d JA |
6255 | } |
6256 | ||
76e1b642 JA |
6257 | /* |
6258 | * Returns true if we found and killed one or more poll requests | |
6259 | */ | |
c072481d PB |
6260 | static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, |
6261 | struct task_struct *tsk, bool cancel_all) | |
221c5eb2 | 6262 | { |
78076bb6 | 6263 | struct hlist_node *tmp; |
221c5eb2 | 6264 | struct io_kiocb *req; |
aa43477b PB |
6265 | bool found = false; |
6266 | int i; | |
221c5eb2 | 6267 | |
79ebeaee | 6268 | spin_lock(&ctx->completion_lock); |
78076bb6 JA |
6269 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
6270 | struct hlist_head *list; | |
6271 | ||
6272 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 6273 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
42a7b4ed | 6274 | if (io_match_task_safe(req, tsk, cancel_all)) { |
61bc84c4 | 6275 | hlist_del_init(&req->hash_node); |
aa43477b PB |
6276 | io_poll_cancel_req(req); |
6277 | found = true; | |
6278 | } | |
f3606e3a | 6279 | } |
221c5eb2 | 6280 | } |
79ebeaee | 6281 | spin_unlock(&ctx->completion_lock); |
aa43477b | 6282 | return found; |
221c5eb2 JA |
6283 | } |
6284 | ||
9ba5fac8 PB |
6285 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
6286 | bool poll_only) | |
e07785b0 | 6287 | __must_hold(&ctx->completion_lock) |
47f46768 | 6288 | { |
78076bb6 | 6289 | struct hlist_head *list; |
47f46768 JA |
6290 | struct io_kiocb *req; |
6291 | ||
78076bb6 JA |
6292 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
6293 | hlist_for_each_entry(req, list, hash_node) { | |
cef216fc | 6294 | if (sqe_addr != req->cqe.user_data) |
b41e9852 | 6295 | continue; |
9ba5fac8 PB |
6296 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
6297 | continue; | |
b2cb805f | 6298 | return req; |
47f46768 | 6299 | } |
b2cb805f JA |
6300 | return NULL; |
6301 | } | |
6302 | ||
aa43477b PB |
6303 | static bool io_poll_disarm(struct io_kiocb *req) |
6304 | __must_hold(&ctx->completion_lock) | |
6305 | { | |
6306 | if (!io_poll_get_ownership(req)) | |
6307 | return false; | |
6308 | io_poll_remove_entries(req); | |
6309 | hash_del(&req->hash_node); | |
6310 | return true; | |
6311 | } | |
6312 | ||
9ba5fac8 PB |
6313 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
6314 | bool poll_only) | |
e07785b0 | 6315 | __must_hold(&ctx->completion_lock) |
b2cb805f | 6316 | { |
aa43477b | 6317 | struct io_kiocb *req = io_poll_find(ctx, sqe_addr, poll_only); |
b2cb805f | 6318 | |
b2cb805f JA |
6319 | if (!req) |
6320 | return -ENOENT; | |
aa43477b PB |
6321 | io_poll_cancel_req(req); |
6322 | return 0; | |
47f46768 JA |
6323 | } |
6324 | ||
9096af3e PB |
6325 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
6326 | unsigned int flags) | |
6327 | { | |
6328 | u32 events; | |
47f46768 | 6329 | |
9096af3e PB |
6330 | events = READ_ONCE(sqe->poll32_events); |
6331 | #ifdef __BIG_ENDIAN | |
6332 | events = swahw32(events); | |
6333 | #endif | |
6334 | if (!(flags & IORING_POLL_ADD_MULTI)) | |
6335 | events |= EPOLLONESHOT; | |
6336 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); | |
47f46768 JA |
6337 | } |
6338 | ||
c5de0036 | 6339 | static int io_poll_update_prep(struct io_kiocb *req, |
3529d8c2 | 6340 | const struct io_uring_sqe *sqe) |
0969e783 | 6341 | { |
c5de0036 PB |
6342 | struct io_poll_update *upd = &req->poll_update; |
6343 | u32 flags; | |
6344 | ||
0969e783 JA |
6345 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
6346 | return -EINVAL; | |
26578cda | 6347 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
c5de0036 PB |
6348 | return -EINVAL; |
6349 | flags = READ_ONCE(sqe->len); | |
6350 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | | |
6351 | IORING_POLL_ADD_MULTI)) | |
6352 | return -EINVAL; | |
6353 | /* meaningless without update */ | |
6354 | if (flags == IORING_POLL_ADD_MULTI) | |
0969e783 JA |
6355 | return -EINVAL; |
6356 | ||
c5de0036 PB |
6357 | upd->old_user_data = READ_ONCE(sqe->addr); |
6358 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; | |
6359 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; | |
221c5eb2 | 6360 | |
c5de0036 PB |
6361 | upd->new_user_data = READ_ONCE(sqe->off); |
6362 | if (!upd->update_user_data && upd->new_user_data) | |
6363 | return -EINVAL; | |
6364 | if (upd->update_events) | |
6365 | upd->events = io_poll_parse_events(sqe, flags); | |
6366 | else if (sqe->poll32_events) | |
6367 | return -EINVAL; | |
221c5eb2 | 6368 | |
221c5eb2 JA |
6369 | return 0; |
6370 | } | |
6371 | ||
3529d8c2 | 6372 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
6373 | { |
6374 | struct io_poll_iocb *poll = &req->poll; | |
c5de0036 | 6375 | u32 flags; |
221c5eb2 JA |
6376 | |
6377 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
6378 | return -EINVAL; | |
c5de0036 | 6379 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
88e41cf9 JA |
6380 | return -EINVAL; |
6381 | flags = READ_ONCE(sqe->len); | |
c5de0036 | 6382 | if (flags & ~IORING_POLL_ADD_MULTI) |
221c5eb2 | 6383 | return -EINVAL; |
04c76b41 PB |
6384 | if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP)) |
6385 | return -EINVAL; | |
221c5eb2 | 6386 | |
48dcd38d | 6387 | io_req_set_refcount(req); |
2804ecd8 | 6388 | req->apoll_events = poll->events = io_poll_parse_events(sqe, flags); |
0969e783 JA |
6389 | return 0; |
6390 | } | |
6391 | ||
61e98203 | 6392 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
6393 | { |
6394 | struct io_poll_iocb *poll = &req->poll; | |
0969e783 | 6395 | struct io_poll_table ipt; |
aa43477b | 6396 | int ret; |
0969e783 | 6397 | |
d7718a9d | 6398 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 6399 | |
aa43477b PB |
6400 | ret = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events); |
6401 | ret = ret ?: ipt.error; | |
6402 | if (ret) | |
6403 | __io_req_complete(req, issue_flags, ret, 0); | |
6404 | return 0; | |
221c5eb2 JA |
6405 | } |
6406 | ||
c5de0036 | 6407 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
b69de288 JA |
6408 | { |
6409 | struct io_ring_ctx *ctx = req->ctx; | |
6410 | struct io_kiocb *preq; | |
2bbb146d | 6411 | int ret2, ret = 0; |
cc8e9ba7 | 6412 | bool locked; |
b69de288 | 6413 | |
79ebeaee | 6414 | spin_lock(&ctx->completion_lock); |
9ba5fac8 | 6415 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
aa43477b | 6416 | if (!preq || !io_poll_disarm(preq)) { |
79ebeaee | 6417 | spin_unlock(&ctx->completion_lock); |
aa43477b | 6418 | ret = preq ? -EALREADY : -ENOENT; |
2bbb146d | 6419 | goto out; |
b69de288 | 6420 | } |
79ebeaee | 6421 | spin_unlock(&ctx->completion_lock); |
cb3b200e | 6422 | |
2bbb146d PB |
6423 | if (req->poll_update.update_events || req->poll_update.update_user_data) { |
6424 | /* only mask one event flags, keep behavior flags */ | |
6425 | if (req->poll_update.update_events) { | |
6426 | preq->poll.events &= ~0xffff; | |
6427 | preq->poll.events |= req->poll_update.events & 0xffff; | |
6428 | preq->poll.events |= IO_POLL_UNMASK; | |
cb3b200e | 6429 | } |
2bbb146d | 6430 | if (req->poll_update.update_user_data) |
cef216fc | 6431 | preq->cqe.user_data = req->poll_update.new_user_data; |
b69de288 | 6432 | |
2bbb146d PB |
6433 | ret2 = io_poll_add(preq, issue_flags); |
6434 | /* successfully updated, don't complete poll request */ | |
6435 | if (!ret2) | |
6436 | goto out; | |
b69de288 | 6437 | } |
6224590d | 6438 | |
2bbb146d | 6439 | req_set_fail(preq); |
cef216fc | 6440 | preq->cqe.res = -ECANCELED; |
cc8e9ba7 PB |
6441 | locked = !(issue_flags & IO_URING_F_UNLOCKED); |
6442 | io_req_task_complete(preq, &locked); | |
2bbb146d PB |
6443 | out: |
6444 | if (ret < 0) | |
6224590d | 6445 | req_set_fail(req); |
2bbb146d | 6446 | /* complete update request, we're done with it */ |
cc8e9ba7 | 6447 | __io_req_complete(req, issue_flags, ret, 0); |
b69de288 | 6448 | return 0; |
89850fce JA |
6449 | } |
6450 | ||
5262f567 JA |
6451 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
6452 | { | |
ad8a48ac JA |
6453 | struct io_timeout_data *data = container_of(timer, |
6454 | struct io_timeout_data, timer); | |
6455 | struct io_kiocb *req = data->req; | |
6456 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
6457 | unsigned long flags; |
6458 | ||
89850fce | 6459 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
a71976f3 | 6460 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
6461 | atomic_set(&req->ctx->cq_timeouts, |
6462 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
89850fce | 6463 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
01cec8c1 | 6464 | |
a90c8bf6 PB |
6465 | if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS)) |
6466 | req_set_fail(req); | |
6467 | ||
cef216fc | 6468 | req->cqe.res = -ETIME; |
a90c8bf6 | 6469 | req->io_task_work.func = io_req_task_complete; |
4813c377 | 6470 | io_req_task_work_add(req, false); |
5262f567 JA |
6471 | return HRTIMER_NORESTART; |
6472 | } | |
6473 | ||
fbd15848 PB |
6474 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
6475 | __u64 user_data) | |
89850fce | 6476 | __must_hold(&ctx->timeout_lock) |
f254ac04 | 6477 | { |
fbd15848 | 6478 | struct io_timeout_data *io; |
47f46768 | 6479 | struct io_kiocb *req; |
fd9c7bc5 | 6480 | bool found = false; |
f254ac04 | 6481 | |
135fcde8 | 6482 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
cef216fc | 6483 | found = user_data == req->cqe.user_data; |
fd9c7bc5 | 6484 | if (found) |
47f46768 | 6485 | break; |
47f46768 | 6486 | } |
fd9c7bc5 PB |
6487 | if (!found) |
6488 | return ERR_PTR(-ENOENT); | |
fbd15848 PB |
6489 | |
6490 | io = req->async_data; | |
fd9c7bc5 | 6491 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
fbd15848 | 6492 | return ERR_PTR(-EALREADY); |
a71976f3 | 6493 | list_del_init(&req->timeout.list); |
fbd15848 PB |
6494 | return req; |
6495 | } | |
47f46768 | 6496 | |
fbd15848 | 6497 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
ec3c3d0f | 6498 | __must_hold(&ctx->completion_lock) |
89850fce | 6499 | __must_hold(&ctx->timeout_lock) |
fbd15848 PB |
6500 | { |
6501 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
6502 | ||
6503 | if (IS_ERR(req)) | |
6504 | return PTR_ERR(req); | |
6695490d | 6505 | io_req_task_queue_fail(req, -ECANCELED); |
f254ac04 JA |
6506 | return 0; |
6507 | } | |
6508 | ||
50c1df2b JA |
6509 | static clockid_t io_timeout_get_clock(struct io_timeout_data *data) |
6510 | { | |
6511 | switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) { | |
6512 | case IORING_TIMEOUT_BOOTTIME: | |
6513 | return CLOCK_BOOTTIME; | |
6514 | case IORING_TIMEOUT_REALTIME: | |
6515 | return CLOCK_REALTIME; | |
6516 | default: | |
6517 | /* can't happen, vetted at prep time */ | |
6518 | WARN_ON_ONCE(1); | |
6519 | fallthrough; | |
6520 | case 0: | |
6521 | return CLOCK_MONOTONIC; | |
6522 | } | |
6523 | } | |
6524 | ||
f1042b6c PB |
6525 | static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
6526 | struct timespec64 *ts, enum hrtimer_mode mode) | |
6527 | __must_hold(&ctx->timeout_lock) | |
6528 | { | |
6529 | struct io_timeout_data *io; | |
6530 | struct io_kiocb *req; | |
6531 | bool found = false; | |
6532 | ||
6533 | list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) { | |
cef216fc | 6534 | found = user_data == req->cqe.user_data; |
f1042b6c PB |
6535 | if (found) |
6536 | break; | |
6537 | } | |
6538 | if (!found) | |
6539 | return -ENOENT; | |
6540 | ||
6541 | io = req->async_data; | |
6542 | if (hrtimer_try_to_cancel(&io->timer) == -1) | |
6543 | return -EALREADY; | |
6544 | hrtimer_init(&io->timer, io_timeout_get_clock(io), mode); | |
6545 | io->timer.function = io_link_timeout_fn; | |
6546 | hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode); | |
6547 | return 0; | |
6548 | } | |
6549 | ||
9c8e11b3 PB |
6550 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
6551 | struct timespec64 *ts, enum hrtimer_mode mode) | |
89850fce | 6552 | __must_hold(&ctx->timeout_lock) |
47f46768 | 6553 | { |
9c8e11b3 PB |
6554 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
6555 | struct io_timeout_data *data; | |
47f46768 | 6556 | |
9c8e11b3 PB |
6557 | if (IS_ERR(req)) |
6558 | return PTR_ERR(req); | |
47f46768 | 6559 | |
9c8e11b3 PB |
6560 | req->timeout.off = 0; /* noseq */ |
6561 | data = req->async_data; | |
6562 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
50c1df2b | 6563 | hrtimer_init(&data->timer, io_timeout_get_clock(data), mode); |
9c8e11b3 PB |
6564 | data->timer.function = io_timeout_fn; |
6565 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
6566 | return 0; | |
47f46768 JA |
6567 | } |
6568 | ||
3529d8c2 JA |
6569 | static int io_timeout_remove_prep(struct io_kiocb *req, |
6570 | const struct io_uring_sqe *sqe) | |
b29472ee | 6571 | { |
9c8e11b3 PB |
6572 | struct io_timeout_rem *tr = &req->timeout_rem; |
6573 | ||
b29472ee JA |
6574 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
6575 | return -EINVAL; | |
61710e43 DA |
6576 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6577 | return -EINVAL; | |
26578cda | 6578 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
b29472ee JA |
6579 | return -EINVAL; |
6580 | ||
f1042b6c | 6581 | tr->ltimeout = false; |
9c8e11b3 PB |
6582 | tr->addr = READ_ONCE(sqe->addr); |
6583 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
f1042b6c PB |
6584 | if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) { |
6585 | if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
6586 | return -EINVAL; | |
6587 | if (tr->flags & IORING_LINK_TIMEOUT_UPDATE) | |
6588 | tr->ltimeout = true; | |
6589 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS)) | |
9c8e11b3 PB |
6590 | return -EINVAL; |
6591 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
6592 | return -EFAULT; | |
2087009c YB |
6593 | if (tr->ts.tv_sec < 0 || tr->ts.tv_nsec < 0) |
6594 | return -EINVAL; | |
9c8e11b3 PB |
6595 | } else if (tr->flags) { |
6596 | /* timeout removal doesn't support flags */ | |
b29472ee | 6597 | return -EINVAL; |
9c8e11b3 | 6598 | } |
b29472ee | 6599 | |
b29472ee JA |
6600 | return 0; |
6601 | } | |
6602 | ||
8662daec PB |
6603 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
6604 | { | |
6605 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
6606 | : HRTIMER_MODE_REL; | |
6607 | } | |
6608 | ||
11365043 JA |
6609 | /* |
6610 | * Remove or update an existing timeout command | |
6611 | */ | |
61e98203 | 6612 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 6613 | { |
9c8e11b3 | 6614 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 6615 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 6616 | int ret; |
11365043 | 6617 | |
ec3c3d0f PB |
6618 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
6619 | spin_lock(&ctx->completion_lock); | |
6620 | spin_lock_irq(&ctx->timeout_lock); | |
9c8e11b3 | 6621 | ret = io_timeout_cancel(ctx, tr->addr); |
ec3c3d0f PB |
6622 | spin_unlock_irq(&ctx->timeout_lock); |
6623 | spin_unlock(&ctx->completion_lock); | |
6624 | } else { | |
f1042b6c PB |
6625 | enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags); |
6626 | ||
ec3c3d0f | 6627 | spin_lock_irq(&ctx->timeout_lock); |
f1042b6c PB |
6628 | if (tr->ltimeout) |
6629 | ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
6630 | else | |
6631 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
ec3c3d0f PB |
6632 | spin_unlock_irq(&ctx->timeout_lock); |
6633 | } | |
11365043 | 6634 | |
4e88d6e7 | 6635 | if (ret < 0) |
93d2bcd2 | 6636 | req_set_fail(req); |
505657bc | 6637 | io_req_complete_post(req, ret, 0); |
11365043 | 6638 | return 0; |
5262f567 JA |
6639 | } |
6640 | ||
3529d8c2 | 6641 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 6642 | bool is_timeout_link) |
5262f567 | 6643 | { |
ad8a48ac | 6644 | struct io_timeout_data *data; |
a41525ab | 6645 | unsigned flags; |
56080b02 | 6646 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 6647 | |
ad8a48ac | 6648 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 6649 | return -EINVAL; |
26578cda PB |
6650 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
6651 | sqe->splice_fd_in) | |
a41525ab | 6652 | return -EINVAL; |
56080b02 | 6653 | if (off && is_timeout_link) |
2d28390a | 6654 | return -EINVAL; |
a41525ab | 6655 | flags = READ_ONCE(sqe->timeout_flags); |
6224590d PB |
6656 | if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK | |
6657 | IORING_TIMEOUT_ETIME_SUCCESS)) | |
50c1df2b JA |
6658 | return -EINVAL; |
6659 | /* more than one clock specified is invalid, obviously */ | |
6660 | if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
5262f567 | 6661 | return -EINVAL; |
bdf20073 | 6662 | |
ef9dd637 | 6663 | INIT_LIST_HEAD(&req->timeout.list); |
bfe68a22 | 6664 | req->timeout.off = off; |
f18ee4cf PB |
6665 | if (unlikely(off && !req->ctx->off_timeout_used)) |
6666 | req->ctx->off_timeout_used = true; | |
26a61679 | 6667 | |
d6a644a7 PB |
6668 | if (WARN_ON_ONCE(req_has_async_data(req))) |
6669 | return -EFAULT; | |
6670 | if (io_alloc_async_data(req)) | |
26a61679 JA |
6671 | return -ENOMEM; |
6672 | ||
e8c2bc1f | 6673 | data = req->async_data; |
ad8a48ac | 6674 | data->req = req; |
50c1df2b | 6675 | data->flags = flags; |
ad8a48ac JA |
6676 | |
6677 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
6678 | return -EFAULT; |
6679 | ||
f6223ff7 YB |
6680 | if (data->ts.tv_sec < 0 || data->ts.tv_nsec < 0) |
6681 | return -EINVAL; | |
6682 | ||
e677edbc | 6683 | INIT_LIST_HEAD(&req->timeout.list); |
8662daec | 6684 | data->mode = io_translate_timeout_mode(flags); |
50c1df2b | 6685 | hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode); |
b97e736a PB |
6686 | |
6687 | if (is_timeout_link) { | |
6688 | struct io_submit_link *link = &req->ctx->submit_state.link; | |
6689 | ||
6690 | if (!link->head) | |
6691 | return -EINVAL; | |
6692 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) | |
6693 | return -EINVAL; | |
4d13d1a4 PB |
6694 | req->timeout.head = link->last; |
6695 | link->last->flags |= REQ_F_ARM_LTIMEOUT; | |
b97e736a | 6696 | } |
ad8a48ac JA |
6697 | return 0; |
6698 | } | |
6699 | ||
61e98203 | 6700 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 6701 | { |
ad8a48ac | 6702 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 6703 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 6704 | struct list_head *entry; |
bfe68a22 | 6705 | u32 tail, off = req->timeout.off; |
ad8a48ac | 6706 | |
89850fce | 6707 | spin_lock_irq(&ctx->timeout_lock); |
93bd25bb | 6708 | |
5262f567 JA |
6709 | /* |
6710 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
6711 | * timeout event to be satisfied. If it isn't set, then this is |
6712 | * a pure timeout request, sequence isn't used. | |
5262f567 | 6713 | */ |
8eb7e2d0 | 6714 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
6715 | entry = ctx->timeout_list.prev; |
6716 | goto add; | |
6717 | } | |
5262f567 | 6718 | |
bfe68a22 PB |
6719 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
6720 | req->timeout.target_seq = tail + off; | |
5262f567 | 6721 | |
f010505b MDG |
6722 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
6723 | * This is safe because ->completion_lock is held, and submissions | |
6724 | * and completions are never mixed in the same ->completion_lock section. | |
6725 | */ | |
6726 | ctx->cq_last_tm_flush = tail; | |
6727 | ||
5262f567 JA |
6728 | /* |
6729 | * Insertion sort, ensuring the first entry in the list is always | |
6730 | * the one we need first. | |
6731 | */ | |
5262f567 | 6732 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
6733 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
6734 | timeout.list); | |
5262f567 | 6735 | |
8eb7e2d0 | 6736 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 6737 | continue; |
bfe68a22 PB |
6738 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
6739 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
6740 | break; |
6741 | } | |
93bd25bb | 6742 | add: |
135fcde8 | 6743 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
6744 | data->timer.function = io_timeout_fn; |
6745 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
89850fce | 6746 | spin_unlock_irq(&ctx->timeout_lock); |
5262f567 JA |
6747 | return 0; |
6748 | } | |
5262f567 | 6749 | |
f458dd84 PB |
6750 | struct io_cancel_data { |
6751 | struct io_ring_ctx *ctx; | |
6752 | u64 user_data; | |
6753 | }; | |
6754 | ||
62755e35 JA |
6755 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
6756 | { | |
6757 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 6758 | struct io_cancel_data *cd = data; |
62755e35 | 6759 | |
cef216fc | 6760 | return req->ctx == cd->ctx && req->cqe.user_data == cd->user_data; |
62755e35 JA |
6761 | } |
6762 | ||
f458dd84 PB |
6763 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
6764 | struct io_ring_ctx *ctx) | |
62755e35 | 6765 | { |
f458dd84 | 6766 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
62755e35 | 6767 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
6768 | int ret = 0; |
6769 | ||
f458dd84 | 6770 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
6771 | return -ENOENT; |
6772 | ||
f458dd84 | 6773 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
62755e35 JA |
6774 | switch (cancel_ret) { |
6775 | case IO_WQ_CANCEL_OK: | |
6776 | ret = 0; | |
6777 | break; | |
6778 | case IO_WQ_CANCEL_RUNNING: | |
6779 | ret = -EALREADY; | |
6780 | break; | |
6781 | case IO_WQ_CANCEL_NOTFOUND: | |
6782 | ret = -ENOENT; | |
6783 | break; | |
6784 | } | |
6785 | ||
e977d6d3 JA |
6786 | return ret; |
6787 | } | |
6788 | ||
8cb01fac | 6789 | static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr) |
47f46768 | 6790 | { |
8cb01fac | 6791 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 JA |
6792 | int ret; |
6793 | ||
dadebc35 | 6794 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
8cb01fac | 6795 | |
f458dd84 | 6796 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
ccbf7261 JA |
6797 | /* |
6798 | * Fall-through even for -EALREADY, as we may have poll armed | |
6799 | * that need unarming. | |
6800 | */ | |
6801 | if (!ret) | |
6802 | return 0; | |
505657bc PB |
6803 | |
6804 | spin_lock(&ctx->completion_lock); | |
ccbf7261 JA |
6805 | ret = io_poll_cancel(ctx, sqe_addr, false); |
6806 | if (ret != -ENOENT) | |
6807 | goto out; | |
6808 | ||
79ebeaee | 6809 | spin_lock_irq(&ctx->timeout_lock); |
47f46768 | 6810 | ret = io_timeout_cancel(ctx, sqe_addr); |
79ebeaee | 6811 | spin_unlock_irq(&ctx->timeout_lock); |
505657bc PB |
6812 | out: |
6813 | spin_unlock(&ctx->completion_lock); | |
6814 | return ret; | |
47f46768 JA |
6815 | } |
6816 | ||
3529d8c2 JA |
6817 | static int io_async_cancel_prep(struct io_kiocb *req, |
6818 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 6819 | { |
fbf23849 | 6820 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 6821 | return -EINVAL; |
61710e43 DA |
6822 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6823 | return -EINVAL; | |
26578cda PB |
6824 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
6825 | sqe->splice_fd_in) | |
e977d6d3 JA |
6826 | return -EINVAL; |
6827 | ||
fbf23849 JA |
6828 | req->cancel.addr = READ_ONCE(sqe->addr); |
6829 | return 0; | |
6830 | } | |
6831 | ||
61e98203 | 6832 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6833 | { |
6834 | struct io_ring_ctx *ctx = req->ctx; | |
58f99373 PB |
6835 | u64 sqe_addr = req->cancel.addr; |
6836 | struct io_tctx_node *node; | |
6837 | int ret; | |
6838 | ||
8cb01fac | 6839 | ret = io_try_cancel_userdata(req, sqe_addr); |
58f99373 PB |
6840 | if (ret != -ENOENT) |
6841 | goto done; | |
58f99373 PB |
6842 | |
6843 | /* slow path, try all io-wq's */ | |
f8929630 | 6844 | io_ring_submit_lock(ctx, issue_flags); |
58f99373 PB |
6845 | ret = -ENOENT; |
6846 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
6847 | struct io_uring_task *tctx = node->task->io_uring; | |
fbf23849 | 6848 | |
58f99373 PB |
6849 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
6850 | if (ret != -ENOENT) | |
6851 | break; | |
6852 | } | |
f8929630 | 6853 | io_ring_submit_unlock(ctx, issue_flags); |
58f99373 | 6854 | done: |
58f99373 | 6855 | if (ret < 0) |
93d2bcd2 | 6856 | req_set_fail(req); |
505657bc | 6857 | io_req_complete_post(req, ret, 0); |
5262f567 JA |
6858 | return 0; |
6859 | } | |
6860 | ||
269bbe5f | 6861 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
6862 | const struct io_uring_sqe *sqe) |
6863 | { | |
61710e43 DA |
6864 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6865 | return -EINVAL; | |
26578cda | 6866 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
05f3fb3c JA |
6867 | return -EINVAL; |
6868 | ||
269bbe5f BM |
6869 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
6870 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
6871 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 6872 | return -EINVAL; |
269bbe5f | 6873 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
6874 | return 0; |
6875 | } | |
6876 | ||
889fca73 | 6877 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6878 | { |
6879 | struct io_ring_ctx *ctx = req->ctx; | |
c3bdad02 | 6880 | struct io_uring_rsrc_update2 up; |
05f3fb3c | 6881 | int ret; |
fbf23849 | 6882 | |
269bbe5f BM |
6883 | up.offset = req->rsrc_update.offset; |
6884 | up.data = req->rsrc_update.arg; | |
c3bdad02 PB |
6885 | up.nr = 0; |
6886 | up.tags = 0; | |
615cee49 | 6887 | up.resv = 0; |
d8a3ba9c | 6888 | up.resv2 = 0; |
05f3fb3c | 6889 | |
f8929630 | 6890 | io_ring_submit_lock(ctx, issue_flags); |
fdecb662 | 6891 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
98f0b3b4 | 6892 | &up, req->rsrc_update.nr_args); |
f8929630 | 6893 | io_ring_submit_unlock(ctx, issue_flags); |
05f3fb3c JA |
6894 | |
6895 | if (ret < 0) | |
93d2bcd2 | 6896 | req_set_fail(req); |
889fca73 | 6897 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
6898 | return 0; |
6899 | } | |
6900 | ||
bfe76559 | 6901 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 6902 | { |
d625c6ee | 6903 | switch (req->opcode) { |
e781573e | 6904 | case IORING_OP_NOP: |
bfe76559 | 6905 | return 0; |
f67676d1 JA |
6906 | case IORING_OP_READV: |
6907 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 6908 | case IORING_OP_READ: |
f67676d1 JA |
6909 | case IORING_OP_WRITEV: |
6910 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 6911 | case IORING_OP_WRITE: |
584b0180 | 6912 | return io_prep_rw(req, sqe); |
0969e783 | 6913 | case IORING_OP_POLL_ADD: |
bfe76559 | 6914 | return io_poll_add_prep(req, sqe); |
0969e783 | 6915 | case IORING_OP_POLL_REMOVE: |
c5de0036 | 6916 | return io_poll_update_prep(req, sqe); |
8ed8d3c3 | 6917 | case IORING_OP_FSYNC: |
1155c76a | 6918 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 6919 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 6920 | return io_sfr_prep(req, sqe); |
03b1230c | 6921 | case IORING_OP_SENDMSG: |
fddaface | 6922 | case IORING_OP_SEND: |
bfe76559 | 6923 | return io_sendmsg_prep(req, sqe); |
03b1230c | 6924 | case IORING_OP_RECVMSG: |
fddaface | 6925 | case IORING_OP_RECV: |
bfe76559 | 6926 | return io_recvmsg_prep(req, sqe); |
f499a021 | 6927 | case IORING_OP_CONNECT: |
bfe76559 | 6928 | return io_connect_prep(req, sqe); |
2d28390a | 6929 | case IORING_OP_TIMEOUT: |
bfe76559 | 6930 | return io_timeout_prep(req, sqe, false); |
b29472ee | 6931 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 6932 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 6933 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 6934 | return io_async_cancel_prep(req, sqe); |
2d28390a | 6935 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 6936 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 6937 | case IORING_OP_ACCEPT: |
bfe76559 | 6938 | return io_accept_prep(req, sqe); |
d63d1b5e | 6939 | case IORING_OP_FALLOCATE: |
bfe76559 | 6940 | return io_fallocate_prep(req, sqe); |
15b71abe | 6941 | case IORING_OP_OPENAT: |
bfe76559 | 6942 | return io_openat_prep(req, sqe); |
b5dba59e | 6943 | case IORING_OP_CLOSE: |
bfe76559 | 6944 | return io_close_prep(req, sqe); |
05f3fb3c | 6945 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 6946 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6947 | case IORING_OP_STATX: |
bfe76559 | 6948 | return io_statx_prep(req, sqe); |
4840e418 | 6949 | case IORING_OP_FADVISE: |
bfe76559 | 6950 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6951 | case IORING_OP_MADVISE: |
bfe76559 | 6952 | return io_madvise_prep(req, sqe); |
cebdb986 | 6953 | case IORING_OP_OPENAT2: |
bfe76559 | 6954 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6955 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6956 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6957 | case IORING_OP_SPLICE: |
bfe76559 | 6958 | return io_splice_prep(req, sqe); |
ddf0322d | 6959 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6960 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6961 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6962 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6963 | case IORING_OP_TEE: |
bfe76559 | 6964 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6965 | case IORING_OP_SHUTDOWN: |
6966 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6967 | case IORING_OP_RENAMEAT: |
6968 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6969 | case IORING_OP_UNLINKAT: |
6970 | return io_unlinkat_prep(req, sqe); | |
e34a02dc DK |
6971 | case IORING_OP_MKDIRAT: |
6972 | return io_mkdirat_prep(req, sqe); | |
7a8721f8 DK |
6973 | case IORING_OP_SYMLINKAT: |
6974 | return io_symlinkat_prep(req, sqe); | |
cf30da90 DK |
6975 | case IORING_OP_LINKAT: |
6976 | return io_linkat_prep(req, sqe); | |
4f57f06c JA |
6977 | case IORING_OP_MSG_RING: |
6978 | return io_msg_ring_prep(req, sqe); | |
f67676d1 JA |
6979 | } |
6980 | ||
bfe76559 PB |
6981 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6982 | req->opcode); | |
bd54b6fe | 6983 | return -EINVAL; |
bfe76559 PB |
6984 | } |
6985 | ||
93642ef8 | 6986 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 6987 | { |
b7e298d2 PB |
6988 | if (!io_op_defs[req->opcode].needs_async_setup) |
6989 | return 0; | |
d886e185 | 6990 | if (WARN_ON_ONCE(req_has_async_data(req))) |
b7e298d2 PB |
6991 | return -EFAULT; |
6992 | if (io_alloc_async_data(req)) | |
6993 | return -EAGAIN; | |
6994 | ||
93642ef8 PB |
6995 | switch (req->opcode) { |
6996 | case IORING_OP_READV: | |
93642ef8 PB |
6997 | return io_rw_prep_async(req, READ); |
6998 | case IORING_OP_WRITEV: | |
93642ef8 PB |
6999 | return io_rw_prep_async(req, WRITE); |
7000 | case IORING_OP_SENDMSG: | |
93642ef8 PB |
7001 | return io_sendmsg_prep_async(req); |
7002 | case IORING_OP_RECVMSG: | |
93642ef8 PB |
7003 | return io_recvmsg_prep_async(req); |
7004 | case IORING_OP_CONNECT: | |
7005 | return io_connect_prep_async(req); | |
7006 | } | |
b7e298d2 PB |
7007 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
7008 | req->opcode); | |
7009 | return -EFAULT; | |
f67676d1 JA |
7010 | } |
7011 | ||
9cf7c104 PB |
7012 | static u32 io_get_sequence(struct io_kiocb *req) |
7013 | { | |
a3dbdf54 | 7014 | u32 seq = req->ctx->cached_sq_head; |
963c6abb | 7015 | struct io_kiocb *cur; |
9cf7c104 | 7016 | |
a3dbdf54 | 7017 | /* need original cached_sq_head, but it was increased for each req */ |
963c6abb | 7018 | io_for_each_link(cur, req) |
a3dbdf54 PB |
7019 | seq--; |
7020 | return seq; | |
9cf7c104 PB |
7021 | } |
7022 | ||
c072481d | 7023 | static __cold void io_drain_req(struct io_kiocb *req) |
de0617e4 | 7024 | { |
a197f664 | 7025 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 7026 | struct io_defer_entry *de; |
f67676d1 | 7027 | int ret; |
e0eb71dc | 7028 | u32 seq = io_get_sequence(req); |
3c19966d | 7029 | |
9d858b21 | 7030 | /* Still need defer if there is pending req in defer list. */ |
e302f104 | 7031 | spin_lock(&ctx->completion_lock); |
5e371265 | 7032 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) { |
e302f104 | 7033 | spin_unlock(&ctx->completion_lock); |
e0eb71dc | 7034 | queue: |
10c66904 | 7035 | ctx->drain_active = false; |
e0eb71dc PB |
7036 | io_req_task_queue(req); |
7037 | return; | |
10c66904 | 7038 | } |
e302f104 | 7039 | spin_unlock(&ctx->completion_lock); |
9cf7c104 | 7040 | |
b7e298d2 | 7041 | ret = io_req_prep_async(req); |
e0eb71dc PB |
7042 | if (ret) { |
7043 | fail: | |
7044 | io_req_complete_failed(req, ret); | |
7045 | return; | |
7046 | } | |
cbdcb435 | 7047 | io_prep_async_link(req); |
27dc8338 | 7048 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
76cc33d7 | 7049 | if (!de) { |
1b48773f | 7050 | ret = -ENOMEM; |
e0eb71dc | 7051 | goto fail; |
76cc33d7 | 7052 | } |
2d28390a | 7053 | |
79ebeaee | 7054 | spin_lock(&ctx->completion_lock); |
9cf7c104 | 7055 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
79ebeaee | 7056 | spin_unlock(&ctx->completion_lock); |
27dc8338 | 7057 | kfree(de); |
e0eb71dc | 7058 | goto queue; |
de0617e4 JA |
7059 | } |
7060 | ||
cef216fc | 7061 | trace_io_uring_defer(ctx, req, req->cqe.user_data, req->opcode); |
27dc8338 | 7062 | de->req = req; |
9cf7c104 | 7063 | de->seq = seq; |
27dc8338 | 7064 | list_add_tail(&de->list, &ctx->defer_list); |
79ebeaee | 7065 | spin_unlock(&ctx->completion_lock); |
de0617e4 JA |
7066 | } |
7067 | ||
68fb8979 | 7068 | static void io_clean_op(struct io_kiocb *req) |
99bc4c38 | 7069 | { |
8197b053 PB |
7070 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
7071 | spin_lock(&req->ctx->completion_lock); | |
cc3cec83 | 7072 | io_put_kbuf_comp(req); |
8197b053 PB |
7073 | spin_unlock(&req->ctx->completion_lock); |
7074 | } | |
99bc4c38 | 7075 | |
0e1b6fe3 PB |
7076 | if (req->flags & REQ_F_NEED_CLEANUP) { |
7077 | switch (req->opcode) { | |
7078 | case IORING_OP_READV: | |
7079 | case IORING_OP_READ_FIXED: | |
7080 | case IORING_OP_READ: | |
7081 | case IORING_OP_WRITEV: | |
7082 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
7083 | case IORING_OP_WRITE: { |
7084 | struct io_async_rw *io = req->async_data; | |
1dacb4df PB |
7085 | |
7086 | kfree(io->free_iovec); | |
0e1b6fe3 | 7087 | break; |
e8c2bc1f | 7088 | } |
0e1b6fe3 | 7089 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
7090 | case IORING_OP_SENDMSG: { |
7091 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
7092 | |
7093 | kfree(io->free_iov); | |
0e1b6fe3 | 7094 | break; |
e8c2bc1f | 7095 | } |
f3cd4850 JA |
7096 | case IORING_OP_OPENAT: |
7097 | case IORING_OP_OPENAT2: | |
7098 | if (req->open.filename) | |
7099 | putname(req->open.filename); | |
7100 | break; | |
80a261fd JA |
7101 | case IORING_OP_RENAMEAT: |
7102 | putname(req->rename.oldpath); | |
7103 | putname(req->rename.newpath); | |
7104 | break; | |
14a1143b JA |
7105 | case IORING_OP_UNLINKAT: |
7106 | putname(req->unlink.filename); | |
7107 | break; | |
e34a02dc DK |
7108 | case IORING_OP_MKDIRAT: |
7109 | putname(req->mkdir.filename); | |
7110 | break; | |
7a8721f8 DK |
7111 | case IORING_OP_SYMLINKAT: |
7112 | putname(req->symlink.oldpath); | |
7113 | putname(req->symlink.newpath); | |
7114 | break; | |
cf30da90 DK |
7115 | case IORING_OP_LINKAT: |
7116 | putname(req->hardlink.oldpath); | |
7117 | putname(req->hardlink.newpath); | |
7118 | break; | |
1b6fe6e0 SR |
7119 | case IORING_OP_STATX: |
7120 | if (req->statx.filename) | |
7121 | putname(req->statx.filename); | |
7122 | break; | |
0e1b6fe3 | 7123 | } |
99bc4c38 | 7124 | } |
75652a30 JA |
7125 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
7126 | kfree(req->apoll->double_poll); | |
7127 | kfree(req->apoll); | |
7128 | req->apoll = NULL; | |
7129 | } | |
c854357b | 7130 | if (req->flags & REQ_F_CREDS) |
b8e64b53 | 7131 | put_cred(req->creds); |
d886e185 PB |
7132 | if (req->flags & REQ_F_ASYNC_DATA) { |
7133 | kfree(req->async_data); | |
7134 | req->async_data = NULL; | |
7135 | } | |
c854357b | 7136 | req->flags &= ~IO_REQ_CLEAN_FLAGS; |
99bc4c38 PB |
7137 | } |
7138 | ||
6bf9c47a JA |
7139 | static bool io_assign_file(struct io_kiocb *req, unsigned int issue_flags) |
7140 | { | |
7141 | if (req->file || !io_op_defs[req->opcode].needs_file) | |
7142 | return true; | |
7143 | ||
7144 | if (req->flags & REQ_F_FIXED_FILE) | |
cef216fc | 7145 | req->file = io_file_get_fixed(req, req->cqe.fd, issue_flags); |
6bf9c47a | 7146 | else |
cef216fc | 7147 | req->file = io_file_get_normal(req, req->cqe.fd); |
6bf9c47a JA |
7148 | if (req->file) |
7149 | return true; | |
7150 | ||
7151 | req_set_fail(req); | |
cef216fc | 7152 | req->cqe.res = -EBADF; |
6bf9c47a JA |
7153 | return false; |
7154 | } | |
7155 | ||
889fca73 | 7156 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 7157 | { |
5730b27e | 7158 | const struct cred *creds = NULL; |
d625c6ee | 7159 | int ret; |
2b188cc1 | 7160 | |
70152140 JA |
7161 | if (unlikely(!io_assign_file(req, issue_flags))) |
7162 | return -EBADF; | |
7163 | ||
6878b40e | 7164 | if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred())) |
c10d1f98 | 7165 | creds = override_creds(req->creds); |
5730b27e | 7166 | |
5bd2182d PM |
7167 | if (!io_op_defs[req->opcode].audit_skip) |
7168 | audit_uring_entry(req->opcode); | |
7169 | ||
d625c6ee | 7170 | switch (req->opcode) { |
2b188cc1 | 7171 | case IORING_OP_NOP: |
889fca73 | 7172 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
7173 | break; |
7174 | case IORING_OP_READV: | |
edafccee | 7175 | case IORING_OP_READ_FIXED: |
3a6820f2 | 7176 | case IORING_OP_READ: |
889fca73 | 7177 | ret = io_read(req, issue_flags); |
edafccee | 7178 | break; |
3529d8c2 | 7179 | case IORING_OP_WRITEV: |
edafccee | 7180 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 7181 | case IORING_OP_WRITE: |
889fca73 | 7182 | ret = io_write(req, issue_flags); |
2b188cc1 | 7183 | break; |
c992fe29 | 7184 | case IORING_OP_FSYNC: |
45d189c6 | 7185 | ret = io_fsync(req, issue_flags); |
c992fe29 | 7186 | break; |
221c5eb2 | 7187 | case IORING_OP_POLL_ADD: |
61e98203 | 7188 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
7189 | break; |
7190 | case IORING_OP_POLL_REMOVE: | |
c5de0036 | 7191 | ret = io_poll_update(req, issue_flags); |
221c5eb2 | 7192 | break; |
5d17b4a4 | 7193 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 7194 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 7195 | break; |
0fa03c62 | 7196 | case IORING_OP_SENDMSG: |
889fca73 | 7197 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 7198 | break; |
fddaface | 7199 | case IORING_OP_SEND: |
889fca73 | 7200 | ret = io_send(req, issue_flags); |
0fa03c62 | 7201 | break; |
aa1fa28f | 7202 | case IORING_OP_RECVMSG: |
889fca73 | 7203 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 7204 | break; |
fddaface | 7205 | case IORING_OP_RECV: |
889fca73 | 7206 | ret = io_recv(req, issue_flags); |
aa1fa28f | 7207 | break; |
5262f567 | 7208 | case IORING_OP_TIMEOUT: |
61e98203 | 7209 | ret = io_timeout(req, issue_flags); |
5262f567 | 7210 | break; |
11365043 | 7211 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 7212 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 7213 | break; |
17f2fe35 | 7214 | case IORING_OP_ACCEPT: |
889fca73 | 7215 | ret = io_accept(req, issue_flags); |
17f2fe35 | 7216 | break; |
f8e85cf2 | 7217 | case IORING_OP_CONNECT: |
889fca73 | 7218 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 7219 | break; |
62755e35 | 7220 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 7221 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 7222 | break; |
d63d1b5e | 7223 | case IORING_OP_FALLOCATE: |
45d189c6 | 7224 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 7225 | break; |
15b71abe | 7226 | case IORING_OP_OPENAT: |
45d189c6 | 7227 | ret = io_openat(req, issue_flags); |
15b71abe | 7228 | break; |
b5dba59e | 7229 | case IORING_OP_CLOSE: |
889fca73 | 7230 | ret = io_close(req, issue_flags); |
b5dba59e | 7231 | break; |
05f3fb3c | 7232 | case IORING_OP_FILES_UPDATE: |
889fca73 | 7233 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 7234 | break; |
eddc7ef5 | 7235 | case IORING_OP_STATX: |
45d189c6 | 7236 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 7237 | break; |
4840e418 | 7238 | case IORING_OP_FADVISE: |
45d189c6 | 7239 | ret = io_fadvise(req, issue_flags); |
4840e418 | 7240 | break; |
c1ca757b | 7241 | case IORING_OP_MADVISE: |
45d189c6 | 7242 | ret = io_madvise(req, issue_flags); |
c1ca757b | 7243 | break; |
cebdb986 | 7244 | case IORING_OP_OPENAT2: |
45d189c6 | 7245 | ret = io_openat2(req, issue_flags); |
cebdb986 | 7246 | break; |
3e4827b0 | 7247 | case IORING_OP_EPOLL_CTL: |
889fca73 | 7248 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 7249 | break; |
7d67af2c | 7250 | case IORING_OP_SPLICE: |
45d189c6 | 7251 | ret = io_splice(req, issue_flags); |
7d67af2c | 7252 | break; |
ddf0322d | 7253 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 7254 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 7255 | break; |
067524e9 | 7256 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 7257 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 7258 | break; |
f2a8d5c7 | 7259 | case IORING_OP_TEE: |
45d189c6 | 7260 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 7261 | break; |
36f4fa68 | 7262 | case IORING_OP_SHUTDOWN: |
45d189c6 | 7263 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 7264 | break; |
80a261fd | 7265 | case IORING_OP_RENAMEAT: |
45d189c6 | 7266 | ret = io_renameat(req, issue_flags); |
80a261fd | 7267 | break; |
14a1143b | 7268 | case IORING_OP_UNLINKAT: |
45d189c6 | 7269 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 7270 | break; |
e34a02dc DK |
7271 | case IORING_OP_MKDIRAT: |
7272 | ret = io_mkdirat(req, issue_flags); | |
7273 | break; | |
7a8721f8 DK |
7274 | case IORING_OP_SYMLINKAT: |
7275 | ret = io_symlinkat(req, issue_flags); | |
7276 | break; | |
cf30da90 DK |
7277 | case IORING_OP_LINKAT: |
7278 | ret = io_linkat(req, issue_flags); | |
7279 | break; | |
4f57f06c JA |
7280 | case IORING_OP_MSG_RING: |
7281 | ret = io_msg_ring(req, issue_flags); | |
7282 | break; | |
2b188cc1 JA |
7283 | default: |
7284 | ret = -EINVAL; | |
7285 | break; | |
7286 | } | |
7287 | ||
5bd2182d PM |
7288 | if (!io_op_defs[req->opcode].audit_skip) |
7289 | audit_uring_exit(!ret, ret); | |
7290 | ||
5730b27e JA |
7291 | if (creds) |
7292 | revert_creds(creds); | |
def596e9 JA |
7293 | if (ret) |
7294 | return ret; | |
b532576e | 7295 | /* If the op doesn't have a file, we're not polling for it */ |
9983028e | 7296 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
9882131c | 7297 | io_iopoll_req_issued(req, issue_flags); |
def596e9 JA |
7298 | |
7299 | return 0; | |
2b188cc1 JA |
7300 | } |
7301 | ||
ebc11b6c PB |
7302 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
7303 | { | |
7304 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
7305 | ||
7306 | req = io_put_req_find_next(req); | |
7307 | return req ? &req->work : NULL; | |
7308 | } | |
7309 | ||
5280f7e5 | 7310 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
7311 | { |
7312 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6bf9c47a | 7313 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
d01905db PB |
7314 | unsigned int issue_flags = IO_URING_F_UNLOCKED; |
7315 | bool needs_poll = false; | |
6bf9c47a | 7316 | int ret = 0, err = -ECANCELED; |
2b188cc1 | 7317 | |
48dcd38d PB |
7318 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
7319 | if (!(req->flags & REQ_F_REFCOUNT)) | |
7320 | __io_req_set_refcount(req, 2); | |
7321 | else | |
7322 | req_ref_get(req); | |
5d5901a3 | 7323 | |
cb2d344c | 7324 | io_arm_ltimeout(req); |
6bf9c47a | 7325 | |
dadebc35 | 7326 | /* either cancelled or io-wq is dying, so don't touch tctx->iowq */ |
d01905db | 7327 | if (work->flags & IO_WQ_WORK_CANCEL) { |
0f8da75b | 7328 | fail: |
6bf9c47a | 7329 | io_req_task_queue_fail(req, err); |
d01905db PB |
7330 | return; |
7331 | } | |
0f8da75b PB |
7332 | if (!io_assign_file(req, issue_flags)) { |
7333 | err = -EBADF; | |
7334 | work->flags |= IO_WQ_WORK_CANCEL; | |
7335 | goto fail; | |
7336 | } | |
31b51510 | 7337 | |
d01905db | 7338 | if (req->flags & REQ_F_FORCE_ASYNC) { |
afb7f56f PB |
7339 | bool opcode_poll = def->pollin || def->pollout; |
7340 | ||
7341 | if (opcode_poll && file_can_poll(req->file)) { | |
7342 | needs_poll = true; | |
d01905db | 7343 | issue_flags |= IO_URING_F_NONBLOCK; |
afb7f56f | 7344 | } |
561fb04a | 7345 | } |
31b51510 | 7346 | |
d01905db PB |
7347 | do { |
7348 | ret = io_issue_sqe(req, issue_flags); | |
7349 | if (ret != -EAGAIN) | |
7350 | break; | |
7351 | /* | |
7352 | * We can get EAGAIN for iopolled IO even though we're | |
7353 | * forcing a sync submission from here, since we can't | |
7354 | * wait for request slots on the block side. | |
7355 | */ | |
7356 | if (!needs_poll) { | |
7357 | cond_resched(); | |
7358 | continue; | |
90fa0288 HX |
7359 | } |
7360 | ||
4d9237e3 | 7361 | if (io_arm_poll_handler(req, issue_flags) == IO_APOLL_OK) |
d01905db PB |
7362 | return; |
7363 | /* aborted or ready, in either case retry blocking */ | |
7364 | needs_poll = false; | |
7365 | issue_flags &= ~IO_URING_F_NONBLOCK; | |
7366 | } while (1); | |
31b51510 | 7367 | |
a3df7698 | 7368 | /* avoid locking problems by failing it from a clean context */ |
5d5901a3 | 7369 | if (ret) |
a3df7698 | 7370 | io_req_task_queue_fail(req, ret); |
2b188cc1 JA |
7371 | } |
7372 | ||
aeca241b | 7373 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
042b0d85 | 7374 | unsigned i) |
65e19f54 | 7375 | { |
042b0d85 | 7376 | return &table->files[i]; |
dafecf19 PB |
7377 | } |
7378 | ||
65e19f54 JA |
7379 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
7380 | int index) | |
7381 | { | |
aeca241b | 7382 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
65e19f54 | 7383 | |
a04b0ac0 | 7384 | return (struct file *) (slot->file_ptr & FFS_MASK); |
65e19f54 JA |
7385 | } |
7386 | ||
a04b0ac0 | 7387 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
9a321c98 PB |
7388 | { |
7389 | unsigned long file_ptr = (unsigned long) file; | |
7390 | ||
88459b50 | 7391 | file_ptr |= io_file_get_flags(file); |
a04b0ac0 | 7392 | file_slot->file_ptr = file_ptr; |
65e19f54 JA |
7393 | } |
7394 | ||
5106dd6e JA |
7395 | static inline struct file *io_file_get_fixed(struct io_kiocb *req, int fd, |
7396 | unsigned int issue_flags) | |
09bb8394 | 7397 | { |
5106dd6e JA |
7398 | struct io_ring_ctx *ctx = req->ctx; |
7399 | struct file *file = NULL; | |
ac177053 | 7400 | unsigned long file_ptr; |
09bb8394 | 7401 | |
5106dd6e JA |
7402 | if (issue_flags & IO_URING_F_UNLOCKED) |
7403 | mutex_lock(&ctx->uring_lock); | |
7404 | ||
ac177053 | 7405 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
5106dd6e | 7406 | goto out; |
ac177053 PB |
7407 | fd = array_index_nospec(fd, ctx->nr_user_files); |
7408 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; | |
7409 | file = (struct file *) (file_ptr & FFS_MASK); | |
7410 | file_ptr &= ~FFS_MASK; | |
7411 | /* mask in overlapping REQ_F and FFS bits */ | |
35645ac3 | 7412 | req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT); |
5106dd6e JA |
7413 | io_req_set_rsrc_node(req, ctx, 0); |
7414 | out: | |
7415 | if (issue_flags & IO_URING_F_UNLOCKED) | |
7416 | mutex_unlock(&ctx->uring_lock); | |
ac177053 PB |
7417 | return file; |
7418 | } | |
d44f554e | 7419 | |
d5361233 JA |
7420 | /* |
7421 | * Drop the file for requeue operations. Only used of req->file is the | |
7422 | * io_uring descriptor itself. | |
7423 | */ | |
7424 | static void io_drop_inflight_file(struct io_kiocb *req) | |
7425 | { | |
7426 | if (unlikely(req->flags & REQ_F_INFLIGHT)) { | |
7427 | fput(req->file); | |
7428 | req->file = NULL; | |
7429 | req->flags &= ~REQ_F_INFLIGHT; | |
7430 | } | |
7431 | } | |
7432 | ||
5106dd6e | 7433 | static struct file *io_file_get_normal(struct io_kiocb *req, int fd) |
ac177053 | 7434 | { |
62906e89 | 7435 | struct file *file = fget(fd); |
ac177053 | 7436 | |
cef216fc | 7437 | trace_io_uring_file_get(req->ctx, req, req->cqe.user_data, fd); |
09bb8394 | 7438 | |
ac177053 | 7439 | /* we don't allow fixed io_uring files */ |
d5361233 JA |
7440 | if (file && file->f_op == &io_uring_fops) |
7441 | req->flags |= REQ_F_INFLIGHT; | |
8371adf5 | 7442 | return file; |
09bb8394 JA |
7443 | } |
7444 | ||
f237c30a | 7445 | static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked) |
89b263f6 JA |
7446 | { |
7447 | struct io_kiocb *prev = req->timeout.prev; | |
617a8948 | 7448 | int ret = -ENOENT; |
89b263f6 JA |
7449 | |
7450 | if (prev) { | |
617a8948 | 7451 | if (!(req->task->flags & PF_EXITING)) |
cef216fc | 7452 | ret = io_try_cancel_userdata(req, prev->cqe.user_data); |
505657bc | 7453 | io_req_complete_post(req, ret ?: -ETIME, 0); |
89b263f6 | 7454 | io_put_req(prev); |
89b263f6 JA |
7455 | } else { |
7456 | io_req_complete_post(req, -ETIME, 0); | |
7457 | } | |
7458 | } | |
7459 | ||
2665abfd | 7460 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 7461 | { |
ad8a48ac JA |
7462 | struct io_timeout_data *data = container_of(timer, |
7463 | struct io_timeout_data, timer); | |
90cd7e42 | 7464 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 7465 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 7466 | unsigned long flags; |
2665abfd | 7467 | |
89b263f6 | 7468 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
90cd7e42 PB |
7469 | prev = req->timeout.head; |
7470 | req->timeout.head = NULL; | |
2665abfd JA |
7471 | |
7472 | /* | |
7473 | * We don't expect the list to be empty, that will only happen if we | |
7474 | * race with the completion of the linked work. | |
7475 | */ | |
447c19f3 | 7476 | if (prev) { |
f2f87370 | 7477 | io_remove_next_linked(prev); |
447c19f3 PB |
7478 | if (!req_ref_inc_not_zero(prev)) |
7479 | prev = NULL; | |
7480 | } | |
ef9dd637 | 7481 | list_del(&req->timeout.list); |
89b263f6 JA |
7482 | req->timeout.prev = prev; |
7483 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); | |
2665abfd | 7484 | |
89b263f6 | 7485 | req->io_task_work.func = io_req_task_link_timeout; |
4813c377 | 7486 | io_req_task_work_add(req, false); |
2665abfd JA |
7487 | return HRTIMER_NORESTART; |
7488 | } | |
7489 | ||
de968c18 | 7490 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 7491 | { |
de968c18 PB |
7492 | struct io_ring_ctx *ctx = req->ctx; |
7493 | ||
89b263f6 | 7494 | spin_lock_irq(&ctx->timeout_lock); |
76a46e06 | 7495 | /* |
f2f87370 PB |
7496 | * If the back reference is NULL, then our linked request finished |
7497 | * before we got a chance to setup the timer | |
76a46e06 | 7498 | */ |
90cd7e42 | 7499 | if (req->timeout.head) { |
e8c2bc1f | 7500 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 7501 | |
ad8a48ac JA |
7502 | data->timer.function = io_link_timeout_fn; |
7503 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
7504 | data->mode); | |
ef9dd637 | 7505 | list_add_tail(&req->timeout.list, &ctx->ltimeout_list); |
2665abfd | 7506 | } |
89b263f6 | 7507 | spin_unlock_irq(&ctx->timeout_lock); |
2665abfd | 7508 | /* drop submission reference */ |
76a46e06 JA |
7509 | io_put_req(req); |
7510 | } | |
2665abfd | 7511 | |
7bfa9bad | 7512 | static void io_queue_async(struct io_kiocb *req, int ret) |
d475a9a6 PB |
7513 | __must_hold(&req->ctx->uring_lock) |
7514 | { | |
7bfa9bad PB |
7515 | struct io_kiocb *linked_timeout; |
7516 | ||
7517 | if (ret != -EAGAIN || (req->flags & REQ_F_NOWAIT)) { | |
7518 | io_req_complete_failed(req, ret); | |
7519 | return; | |
7520 | } | |
7521 | ||
7522 | linked_timeout = io_prep_linked_timeout(req); | |
d475a9a6 | 7523 | |
4d9237e3 | 7524 | switch (io_arm_poll_handler(req, 0)) { |
d475a9a6 | 7525 | case IO_APOLL_READY: |
d475a9a6 PB |
7526 | io_req_task_queue(req); |
7527 | break; | |
7528 | case IO_APOLL_ABORTED: | |
7529 | /* | |
7530 | * Queued up for async execution, worker will release | |
7531 | * submit reference when the iocb is actually submitted. | |
7532 | */ | |
77955efb | 7533 | io_queue_iowq(req, NULL); |
d475a9a6 | 7534 | break; |
b1c62645 | 7535 | case IO_APOLL_OK: |
b1c62645 | 7536 | break; |
d475a9a6 PB |
7537 | } |
7538 | ||
7539 | if (linked_timeout) | |
7540 | io_queue_linked_timeout(linked_timeout); | |
7541 | } | |
7542 | ||
cbc2e203 | 7543 | static inline void io_queue_sqe(struct io_kiocb *req) |
282cdc86 | 7544 | __must_hold(&req->ctx->uring_lock) |
2b188cc1 | 7545 | { |
e0c5c576 | 7546 | int ret; |
2b188cc1 | 7547 | |
c5eef2b9 | 7548 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 7549 | |
fff4e40e PB |
7550 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
7551 | io_req_add_compl_list(req); | |
d9f9d284 | 7552 | return; |
fff4e40e | 7553 | } |
491381ce JA |
7554 | /* |
7555 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
7556 | * doesn't support non-blocking read/write attempts | |
7557 | */ | |
7bfa9bad | 7558 | if (likely(!ret)) |
cb2d344c | 7559 | io_arm_ltimeout(req); |
7bfa9bad PB |
7560 | else |
7561 | io_queue_async(req, ret); | |
2b188cc1 JA |
7562 | } |
7563 | ||
4652fe3f | 7564 | static void io_queue_sqe_fallback(struct io_kiocb *req) |
282cdc86 | 7565 | __must_hold(&req->ctx->uring_lock) |
4fe2c963 | 7566 | { |
4652fe3f | 7567 | if (req->flags & REQ_F_FAIL) { |
c6d3d9cb | 7568 | io_req_complete_fail_submit(req); |
e0eb71dc PB |
7569 | } else if (unlikely(req->ctx->drain_active)) { |
7570 | io_drain_req(req); | |
76cc33d7 PB |
7571 | } else { |
7572 | int ret = io_req_prep_async(req); | |
7573 | ||
7574 | if (unlikely(ret)) | |
7575 | io_req_complete_failed(req, ret); | |
7576 | else | |
77955efb | 7577 | io_queue_iowq(req, NULL); |
ce35a47a | 7578 | } |
4fe2c963 JL |
7579 | } |
7580 | ||
b16fed66 PB |
7581 | /* |
7582 | * Check SQE restrictions (opcode and flags). | |
7583 | * | |
7584 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
7585 | */ | |
7586 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
7587 | struct io_kiocb *req, | |
7588 | unsigned int sqe_flags) | |
4fe2c963 | 7589 | { |
b16fed66 PB |
7590 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
7591 | return false; | |
7592 | ||
7593 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
7594 | ctx->restrictions.sqe_flags_required) | |
7595 | return false; | |
7596 | ||
7597 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
7598 | ctx->restrictions.sqe_flags_required)) | |
7599 | return false; | |
7600 | ||
7601 | return true; | |
4fe2c963 JL |
7602 | } |
7603 | ||
22b2ca31 PB |
7604 | static void io_init_req_drain(struct io_kiocb *req) |
7605 | { | |
7606 | struct io_ring_ctx *ctx = req->ctx; | |
7607 | struct io_kiocb *head = ctx->submit_state.link.head; | |
7608 | ||
7609 | ctx->drain_active = true; | |
7610 | if (head) { | |
7611 | /* | |
7612 | * If we need to drain a request in the middle of a link, drain | |
7613 | * the head request and the next request/link after the current | |
7614 | * link. Considering sequential execution of links, | |
b6c7db32 | 7615 | * REQ_F_IO_DRAIN will be maintained for every request of our |
22b2ca31 PB |
7616 | * link. |
7617 | */ | |
b6c7db32 | 7618 | head->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; |
22b2ca31 PB |
7619 | ctx->drain_next = true; |
7620 | } | |
7621 | } | |
7622 | ||
b16fed66 PB |
7623 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
7624 | const struct io_uring_sqe *sqe) | |
282cdc86 | 7625 | __must_hold(&ctx->uring_lock) |
b16fed66 | 7626 | { |
b16fed66 | 7627 | unsigned int sqe_flags; |
fc0ae024 | 7628 | int personality; |
4a04d1d1 | 7629 | u8 opcode; |
b16fed66 | 7630 | |
864ea921 | 7631 | /* req is partially pre-initialised, see io_preinit_req() */ |
4a04d1d1 | 7632 | req->opcode = opcode = READ_ONCE(sqe->opcode); |
b16fed66 PB |
7633 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
7634 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
cef216fc | 7635 | req->cqe.user_data = READ_ONCE(sqe->user_data); |
b16fed66 | 7636 | req->file = NULL; |
b16fed66 | 7637 | req->fixed_rsrc_refs = NULL; |
b16fed66 | 7638 | req->task = current; |
b16fed66 | 7639 | |
4a04d1d1 PB |
7640 | if (unlikely(opcode >= IORING_OP_LAST)) { |
7641 | req->opcode = 0; | |
b16fed66 | 7642 | return -EINVAL; |
4a04d1d1 | 7643 | } |
68fe256a PB |
7644 | if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) { |
7645 | /* enforce forwards compatibility on users */ | |
7646 | if (sqe_flags & ~SQE_VALID_FLAGS) | |
7647 | return -EINVAL; | |
7648 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
4a04d1d1 | 7649 | !io_op_defs[opcode].buffer_select) |
68fe256a | 7650 | return -EOPNOTSUPP; |
5562a8d7 PB |
7651 | if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS) |
7652 | ctx->drain_disabled = true; | |
7653 | if (sqe_flags & IOSQE_IO_DRAIN) { | |
7654 | if (ctx->drain_disabled) | |
7655 | return -EOPNOTSUPP; | |
22b2ca31 | 7656 | io_init_req_drain(req); |
5562a8d7 | 7657 | } |
2a56a9bd PB |
7658 | } |
7659 | if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) { | |
7660 | if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags)) | |
7661 | return -EACCES; | |
7662 | /* knock it to the slow queue path, will be drained there */ | |
7663 | if (ctx->drain_active) | |
7664 | req->flags |= REQ_F_FORCE_ASYNC; | |
7665 | /* if there is no link, we're at "next" request and need to drain */ | |
7666 | if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) { | |
7667 | ctx->drain_next = false; | |
7668 | ctx->drain_active = true; | |
b6c7db32 | 7669 | req->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; |
2a56a9bd | 7670 | } |
68fe256a | 7671 | } |
b16fed66 | 7672 | |
4a04d1d1 | 7673 | if (io_op_defs[opcode].needs_file) { |
6d63416d PB |
7674 | struct io_submit_state *state = &ctx->submit_state; |
7675 | ||
cef216fc | 7676 | req->cqe.fd = READ_ONCE(sqe->fd); |
6bf9c47a | 7677 | |
6d63416d PB |
7678 | /* |
7679 | * Plug now if we have more than 2 IO left after this, and the | |
7680 | * target is potentially a read/write to block based storage. | |
7681 | */ | |
4a04d1d1 | 7682 | if (state->need_plug && io_op_defs[opcode].plug) { |
6d63416d PB |
7683 | state->plug_started = true; |
7684 | state->need_plug = false; | |
5ca7a8b3 | 7685 | blk_start_plug_nr_ios(&state->plug, state->submit_nr); |
6d63416d | 7686 | } |
b16fed66 | 7687 | } |
863e0560 | 7688 | |
003e8dcc JA |
7689 | personality = READ_ONCE(sqe->personality); |
7690 | if (personality) { | |
cdab10bf LT |
7691 | int ret; |
7692 | ||
c10d1f98 PB |
7693 | req->creds = xa_load(&ctx->personalities, personality); |
7694 | if (!req->creds) | |
003e8dcc | 7695 | return -EINVAL; |
c10d1f98 | 7696 | get_cred(req->creds); |
cdc1404a PM |
7697 | ret = security_uring_override_creds(req->creds); |
7698 | if (ret) { | |
7699 | put_cred(req->creds); | |
7700 | return ret; | |
7701 | } | |
b8e64b53 | 7702 | req->flags |= REQ_F_CREDS; |
003e8dcc | 7703 | } |
b16fed66 | 7704 | |
fc0ae024 | 7705 | return io_req_prep(req, sqe); |
b16fed66 PB |
7706 | } |
7707 | ||
a6b8cadc | 7708 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 7709 | const struct io_uring_sqe *sqe) |
282cdc86 | 7710 | __must_hold(&ctx->uring_lock) |
9e645e11 | 7711 | { |
a1ab7b35 | 7712 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 7713 | int ret; |
9e645e11 | 7714 | |
a6b8cadc PB |
7715 | ret = io_init_req(ctx, req, sqe); |
7716 | if (unlikely(ret)) { | |
502c87d6 | 7717 | trace_io_uring_req_failed(sqe, ctx, req, ret); |
a87acfde | 7718 | |
a8295b98 | 7719 | /* fail even hard links since we don't submit */ |
de59bc10 | 7720 | if (link->head) { |
a8295b98 HX |
7721 | /* |
7722 | * we can judge a link req is failed or cancelled by if | |
7723 | * REQ_F_FAIL is set, but the head is an exception since | |
7724 | * it may be set REQ_F_FAIL because of other req's failure | |
cef216fc | 7725 | * so let's leverage req->cqe.res to distinguish if a head |
a8295b98 HX |
7726 | * is set REQ_F_FAIL because of its failure or other req's |
7727 | * failure so that we can set the correct ret code for it. | |
7728 | * init result here to avoid affecting the normal path. | |
7729 | */ | |
7730 | if (!(link->head->flags & REQ_F_FAIL)) | |
7731 | req_fail_link_node(link->head, -ECANCELED); | |
da1a08c5 | 7732 | } else if (!(req->flags & IO_REQ_LINK_FLAGS)) { |
a8295b98 HX |
7733 | /* |
7734 | * the current req is a normal req, we should return | |
7735 | * error and thus break the submittion loop. | |
7736 | */ | |
7737 | io_req_complete_failed(req, ret); | |
7738 | return ret; | |
de59bc10 | 7739 | } |
a8295b98 | 7740 | req_fail_link_node(req, ret); |
a6b8cadc | 7741 | } |
441b8a78 | 7742 | |
be7053b7 | 7743 | /* don't need @sqe from now on */ |
cef216fc | 7744 | trace_io_uring_submit_sqe(ctx, req, req->cqe.user_data, req->opcode, |
236daeae OL |
7745 | req->flags, true, |
7746 | ctx->flags & IORING_SETUP_SQPOLL); | |
a6b8cadc | 7747 | |
9e645e11 JA |
7748 | /* |
7749 | * If we already have a head request, queue this one for async | |
7750 | * submittal once the head completes. If we don't have a head but | |
7751 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
7752 | * submitted sync once the chain is complete. If none of those | |
7753 | * conditions are true (normal request), then just queue it. | |
7754 | */ | |
863e0560 PB |
7755 | if (link->head) { |
7756 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 7757 | |
a8295b98 HX |
7758 | if (!(req->flags & REQ_F_FAIL)) { |
7759 | ret = io_req_prep_async(req); | |
7760 | if (unlikely(ret)) { | |
7761 | req_fail_link_node(req, ret); | |
7762 | if (!(head->flags & REQ_F_FAIL)) | |
7763 | req_fail_link_node(head, -ECANCELED); | |
7764 | } | |
7765 | } | |
9d76377f | 7766 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 7767 | link->last->link = req; |
863e0560 | 7768 | link->last = req; |
32fe525b | 7769 | |
da1a08c5 | 7770 | if (req->flags & IO_REQ_LINK_FLAGS) |
f15a3431 | 7771 | return 0; |
32fe525b | 7772 | /* last request of a link, enqueue the link */ |
f15a3431 PB |
7773 | link->head = NULL; |
7774 | req = head; | |
da1a08c5 | 7775 | } else if (req->flags & IO_REQ_LINK_FLAGS) { |
f15a3431 PB |
7776 | link->head = req; |
7777 | link->last = req; | |
7778 | return 0; | |
9e645e11 | 7779 | } |
2e6e1fde | 7780 | |
cbc2e203 PB |
7781 | if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) |
7782 | io_queue_sqe(req); | |
7783 | else | |
7784 | io_queue_sqe_fallback(req); | |
7785 | ||
1d4240cc | 7786 | return 0; |
9e645e11 JA |
7787 | } |
7788 | ||
9a56a232 JA |
7789 | /* |
7790 | * Batched submission is done, ensure local IO is flushed out. | |
7791 | */ | |
553deffd | 7792 | static void io_submit_state_end(struct io_ring_ctx *ctx) |
9a56a232 | 7793 | { |
553deffd PB |
7794 | struct io_submit_state *state = &ctx->submit_state; |
7795 | ||
e126391c PB |
7796 | if (unlikely(state->link.head)) |
7797 | io_queue_sqe_fallback(state->link.head); | |
553deffd | 7798 | /* flush only after queuing links as they can generate completions */ |
c450178d | 7799 | io_submit_flush_completions(ctx); |
27926b68 JA |
7800 | if (state->plug_started) |
7801 | blk_finish_plug(&state->plug); | |
9a56a232 JA |
7802 | } |
7803 | ||
7804 | /* | |
7805 | * Start submission side cache. | |
7806 | */ | |
7807 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 7808 | unsigned int max_ios) |
9a56a232 | 7809 | { |
27926b68 | 7810 | state->plug_started = false; |
4b628aeb | 7811 | state->need_plug = max_ios > 2; |
5ca7a8b3 | 7812 | state->submit_nr = max_ios; |
a1ab7b35 PB |
7813 | /* set only head, no need to init link_last in advance */ |
7814 | state->link.head = NULL; | |
9a56a232 JA |
7815 | } |
7816 | ||
2b188cc1 JA |
7817 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
7818 | { | |
75b28aff | 7819 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 7820 | |
caf582c6 PB |
7821 | /* |
7822 | * Ensure any loads from the SQEs are done at this point, | |
7823 | * since once we write the new head, the application could | |
7824 | * write new data to them. | |
7825 | */ | |
7826 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
7827 | } |
7828 | ||
2b188cc1 | 7829 | /* |
dd9ae8a0 | 7830 | * Fetch an sqe, if one is available. Note this returns a pointer to memory |
2b188cc1 JA |
7831 | * that is mapped by userspace. This means that care needs to be taken to |
7832 | * ensure that reads are stable, as we cannot rely on userspace always | |
7833 | * being a good citizen. If members of the sqe are validated and then later | |
7834 | * used, it's important that those reads are done through READ_ONCE() to | |
7835 | * prevent a re-load down the line. | |
7836 | */ | |
709b302f | 7837 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 7838 | { |
ea5ab3b5 | 7839 | unsigned head, mask = ctx->sq_entries - 1; |
17d3aeb3 | 7840 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
2b188cc1 JA |
7841 | |
7842 | /* | |
7843 | * The cached sq head (or cq tail) serves two purposes: | |
7844 | * | |
7845 | * 1) allows us to batch the cost of updating the user visible | |
7846 | * head updates. | |
7847 | * 2) allows the kernel side to track the head on its own, even | |
7848 | * though the application is the one updating it. | |
7849 | */ | |
17d3aeb3 | 7850 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
709b302f PB |
7851 | if (likely(head < ctx->sq_entries)) |
7852 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
7853 | |
7854 | /* drop invalid entries */ | |
15641e42 PB |
7855 | ctx->cq_extra--; |
7856 | WRITE_ONCE(ctx->rings->sq_dropped, | |
7857 | READ_ONCE(ctx->rings->sq_dropped) + 1); | |
709b302f PB |
7858 | return NULL; |
7859 | } | |
7860 | ||
0f212204 | 7861 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
282cdc86 | 7862 | __must_hold(&ctx->uring_lock) |
6c271ce2 | 7863 | { |
69629809 | 7864 | unsigned int entries = io_sqring_entries(ctx); |
8e6971a8 PB |
7865 | unsigned int left; |
7866 | int ret; | |
6c271ce2 | 7867 | |
51d48dab | 7868 | if (unlikely(!entries)) |
69629809 | 7869 | return 0; |
ee7d46d9 | 7870 | /* make sure SQ entry isn't read before tail */ |
8e6971a8 PB |
7871 | ret = left = min3(nr, ctx->sq_entries, entries); |
7872 | io_get_task_refs(left); | |
7873 | io_submit_state_start(&ctx->submit_state, left); | |
6c271ce2 | 7874 | |
69629809 | 7875 | do { |
3529d8c2 | 7876 | const struct io_uring_sqe *sqe; |
196be95c | 7877 | struct io_kiocb *req; |
fb5ccc98 | 7878 | |
8e6971a8 | 7879 | if (unlikely(!io_alloc_req_refill(ctx))) |
fb5ccc98 | 7880 | break; |
a33ae9ce | 7881 | req = io_alloc_req(ctx); |
4fccfcbb PB |
7882 | sqe = io_get_sqe(ctx); |
7883 | if (unlikely(!sqe)) { | |
fa05457a | 7884 | io_req_add_to_cache(req, ctx); |
4fccfcbb PB |
7885 | break; |
7886 | } | |
1cd15904 PB |
7887 | |
7888 | /* | |
7889 | * Continue submitting even for sqe failure if the | |
7890 | * ring was setup with IORING_SETUP_SUBMIT_ALL | |
7891 | */ | |
7892 | if (unlikely(io_submit_sqe(ctx, req, sqe)) && | |
7893 | !(ctx->flags & IORING_SETUP_SUBMIT_ALL)) { | |
7894 | left--; | |
7895 | break; | |
bcbb7bf6 | 7896 | } |
1cd15904 | 7897 | } while (--left); |
9466f437 | 7898 | |
8e6971a8 PB |
7899 | if (unlikely(left)) { |
7900 | ret -= left; | |
7901 | /* try again if it submitted nothing and can't allocate a req */ | |
7902 | if (!ret && io_req_cache_empty(ctx)) | |
7903 | ret = -EAGAIN; | |
7904 | current->io_uring->cached_refs += left; | |
9466f437 | 7905 | } |
6c271ce2 | 7906 | |
553deffd | 7907 | io_submit_state_end(ctx); |
ae9428ca PB |
7908 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
7909 | io_commit_sqring(ctx); | |
8e6971a8 | 7910 | return ret; |
6c271ce2 JA |
7911 | } |
7912 | ||
e4b6d902 PB |
7913 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
7914 | { | |
7915 | return READ_ONCE(sqd->state); | |
7916 | } | |
7917 | ||
23b3628e XW |
7918 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
7919 | { | |
7920 | /* Tell userspace we may need a wakeup call */ | |
79ebeaee | 7921 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7922 | WRITE_ONCE(ctx->rings->sq_flags, |
7923 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7924 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7925 | } |
7926 | ||
7927 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
7928 | { | |
79ebeaee | 7929 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7930 | WRITE_ONCE(ctx->rings->sq_flags, |
7931 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7932 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7933 | } |
7934 | ||
08369246 | 7935 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 7936 | { |
c8d1ba58 | 7937 | unsigned int to_submit; |
bdcd3eab | 7938 | int ret = 0; |
6c271ce2 | 7939 | |
c8d1ba58 | 7940 | to_submit = io_sqring_entries(ctx); |
e95eee2d | 7941 | /* if we're handling multiple rings, cap submit size for fairness */ |
4ce8ad95 OL |
7942 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
7943 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; | |
e95eee2d | 7944 | |
5eef4e87 | 7945 | if (!wq_list_empty(&ctx->iopoll_list) || to_submit) { |
948e1947 PB |
7946 | const struct cred *creds = NULL; |
7947 | ||
7948 | if (ctx->sq_creds != current_cred()) | |
7949 | creds = override_creds(ctx->sq_creds); | |
a4c0b3de | 7950 | |
c8d1ba58 | 7951 | mutex_lock(&ctx->uring_lock); |
5eef4e87 | 7952 | if (!wq_list_empty(&ctx->iopoll_list)) |
5ba3c874 | 7953 | io_do_iopoll(ctx, true); |
906a3c6f | 7954 | |
3b763ba1 PB |
7955 | /* |
7956 | * Don't submit if refs are dying, good for io_uring_register(), | |
7957 | * but also it is relied upon by io_ring_exit_work() | |
7958 | */ | |
0298ef96 PB |
7959 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
7960 | !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
08369246 | 7961 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 | 7962 | mutex_unlock(&ctx->uring_lock); |
cb318216 | 7963 | |
acfb381d PB |
7964 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
7965 | wake_up(&ctx->sqo_sq_wait); | |
948e1947 PB |
7966 | if (creds) |
7967 | revert_creds(creds); | |
acfb381d | 7968 | } |
6c271ce2 | 7969 | |
08369246 XW |
7970 | return ret; |
7971 | } | |
6c271ce2 | 7972 | |
c072481d | 7973 | static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
08369246 XW |
7974 | { |
7975 | struct io_ring_ctx *ctx; | |
7976 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 7977 | |
c9dca27d PB |
7978 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7979 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); | |
08369246 | 7980 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 7981 | } |
6c271ce2 | 7982 | |
e4b6d902 PB |
7983 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
7984 | { | |
7985 | bool did_sig = false; | |
7986 | struct ksignal ksig; | |
7987 | ||
7988 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || | |
7989 | signal_pending(current)) { | |
7990 | mutex_unlock(&sqd->lock); | |
7991 | if (signal_pending(current)) | |
7992 | did_sig = get_signal(&ksig); | |
7993 | cond_resched(); | |
7994 | mutex_lock(&sqd->lock); | |
7995 | } | |
e4b6d902 PB |
7996 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
7997 | } | |
7998 | ||
c8d1ba58 JA |
7999 | static int io_sq_thread(void *data) |
8000 | { | |
69fb2131 JA |
8001 | struct io_sq_data *sqd = data; |
8002 | struct io_ring_ctx *ctx; | |
a0d9205f | 8003 | unsigned long timeout = 0; |
37d1e2e3 | 8004 | char buf[TASK_COMM_LEN]; |
08369246 | 8005 | DEFINE_WAIT(wait); |
6c271ce2 | 8006 | |
696ee88a | 8007 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
37d1e2e3 | 8008 | set_task_comm(current, buf); |
37d1e2e3 JA |
8009 | |
8010 | if (sqd->sq_cpu != -1) | |
8011 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
8012 | else | |
8013 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
8014 | current->flags |= PF_NO_SETAFFINITY; | |
8015 | ||
5bd2182d PM |
8016 | audit_alloc_kernel(current); |
8017 | ||
09a6f4ef | 8018 | mutex_lock(&sqd->lock); |
e4b6d902 | 8019 | while (1) { |
1a924a80 | 8020 | bool cap_entries, sqt_spin = false; |
c1edbf5f | 8021 | |
e4b6d902 PB |
8022 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
8023 | if (io_sqd_handle_event(sqd)) | |
c7d95613 | 8024 | break; |
08369246 XW |
8025 | timeout = jiffies + sqd->sq_thread_idle; |
8026 | } | |
e4b6d902 | 8027 | |
e95eee2d | 8028 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 8029 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
948e1947 | 8030 | int ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a | 8031 | |
5eef4e87 | 8032 | if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list))) |
08369246 | 8033 | sqt_spin = true; |
69fb2131 | 8034 | } |
dd432ea5 PB |
8035 | if (io_run_task_work()) |
8036 | sqt_spin = true; | |
6c271ce2 | 8037 | |
08369246 | 8038 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 8039 | cond_resched(); |
08369246 XW |
8040 | if (sqt_spin) |
8041 | timeout = jiffies + sqd->sq_thread_idle; | |
8042 | continue; | |
8043 | } | |
8044 | ||
08369246 | 8045 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
7f62d40d | 8046 | if (!io_sqd_events_pending(sqd) && !task_work_pending(current)) { |
1a924a80 PB |
8047 | bool needs_sched = true; |
8048 | ||
724cb4f9 | 8049 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
aaa9f0f4 PB |
8050 | io_ring_set_wakeup_flag(ctx); |
8051 | ||
724cb4f9 | 8052 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
5eef4e87 | 8053 | !wq_list_empty(&ctx->iopoll_list)) { |
724cb4f9 HX |
8054 | needs_sched = false; |
8055 | break; | |
8056 | } | |
649bb75d AK |
8057 | |
8058 | /* | |
8059 | * Ensure the store of the wakeup flag is not | |
8060 | * reordered with the load of the SQ tail | |
8061 | */ | |
8062 | smp_mb(); | |
8063 | ||
724cb4f9 HX |
8064 | if (io_sqring_entries(ctx)) { |
8065 | needs_sched = false; | |
8066 | break; | |
8067 | } | |
8068 | } | |
8069 | ||
8070 | if (needs_sched) { | |
8071 | mutex_unlock(&sqd->lock); | |
8072 | schedule(); | |
8073 | mutex_lock(&sqd->lock); | |
8074 | } | |
69fb2131 JA |
8075 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
8076 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 8077 | } |
08369246 XW |
8078 | |
8079 | finish_wait(&sqd->wait, &wait); | |
8080 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 | 8081 | } |
28cea78a | 8082 | |
78cc687b | 8083 | io_uring_cancel_generic(true, sqd); |
37d1e2e3 | 8084 | sqd->thread = NULL; |
05962f95 | 8085 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
5f3f26f9 | 8086 | io_ring_set_wakeup_flag(ctx); |
521d6a73 | 8087 | io_run_task_work(); |
734551df PB |
8088 | mutex_unlock(&sqd->lock); |
8089 | ||
5bd2182d PM |
8090 | audit_free(current); |
8091 | ||
37d1e2e3 JA |
8092 | complete(&sqd->exited); |
8093 | do_exit(0); | |
6c271ce2 JA |
8094 | } |
8095 | ||
bda52162 JA |
8096 | struct io_wait_queue { |
8097 | struct wait_queue_entry wq; | |
8098 | struct io_ring_ctx *ctx; | |
5fd46178 | 8099 | unsigned cq_tail; |
bda52162 JA |
8100 | unsigned nr_timeouts; |
8101 | }; | |
8102 | ||
6c503150 | 8103 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
8104 | { |
8105 | struct io_ring_ctx *ctx = iowq->ctx; | |
5fd46178 | 8106 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
bda52162 JA |
8107 | |
8108 | /* | |
d195a66e | 8109 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
8110 | * started waiting. For timeouts, we always want to return to userspace, |
8111 | * regardless of event count. | |
8112 | */ | |
5fd46178 | 8113 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
bda52162 JA |
8114 | } |
8115 | ||
8116 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
8117 | int wake_flags, void *key) | |
8118 | { | |
8119 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
8120 | wq); | |
8121 | ||
6c503150 PB |
8122 | /* |
8123 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
8124 | * the task, and the next invocation will do it. | |
8125 | */ | |
5ed7a37d | 8126 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow)) |
6c503150 PB |
8127 | return autoremove_wake_function(curr, mode, wake_flags, key); |
8128 | return -1; | |
bda52162 JA |
8129 | } |
8130 | ||
af9c1a44 JA |
8131 | static int io_run_task_work_sig(void) |
8132 | { | |
8133 | if (io_run_task_work()) | |
8134 | return 1; | |
0b8cfa97 | 8135 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
792ee0f6 | 8136 | return -ERESTARTSYS; |
c5020bc8 OL |
8137 | if (task_sigpending(current)) |
8138 | return -EINTR; | |
8139 | return 0; | |
af9c1a44 JA |
8140 | } |
8141 | ||
eeb60b9a PB |
8142 | /* when returns >0, the caller should retry */ |
8143 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
8144 | struct io_wait_queue *iowq, | |
22833966 | 8145 | ktime_t timeout) |
eeb60b9a PB |
8146 | { |
8147 | int ret; | |
8148 | ||
8149 | /* make sure we run task_work before checking for signals */ | |
8150 | ret = io_run_task_work_sig(); | |
8151 | if (ret || io_should_wake(iowq)) | |
8152 | return ret; | |
8153 | /* let the caller flush overflows, retry */ | |
5ed7a37d | 8154 | if (test_bit(0, &ctx->check_cq_overflow)) |
eeb60b9a PB |
8155 | return 1; |
8156 | ||
22833966 JA |
8157 | if (!schedule_hrtimeout(&timeout, HRTIMER_MODE_ABS)) |
8158 | return -ETIME; | |
8159 | return 1; | |
eeb60b9a PB |
8160 | } |
8161 | ||
2b188cc1 JA |
8162 | /* |
8163 | * Wait until events become available, if we don't already have some. The | |
8164 | * application must reap them itself, as they reside on the shared cq ring. | |
8165 | */ | |
8166 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
8167 | const sigset_t __user *sig, size_t sigsz, |
8168 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 8169 | { |
90291099 | 8170 | struct io_wait_queue iowq; |
75b28aff | 8171 | struct io_rings *rings = ctx->rings; |
22833966 | 8172 | ktime_t timeout = KTIME_MAX; |
c1d5a224 | 8173 | int ret; |
2b188cc1 | 8174 | |
b41e9852 | 8175 | do { |
90f67366 | 8176 | io_cqring_overflow_flush(ctx); |
6c503150 | 8177 | if (io_cqring_events(ctx) >= min_events) |
b41e9852 | 8178 | return 0; |
4c6e277c | 8179 | if (!io_run_task_work()) |
b41e9852 | 8180 | break; |
b41e9852 | 8181 | } while (1); |
2b188cc1 JA |
8182 | |
8183 | if (sig) { | |
9e75ad5d AB |
8184 | #ifdef CONFIG_COMPAT |
8185 | if (in_compat_syscall()) | |
8186 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 8187 | sigsz); |
9e75ad5d AB |
8188 | else |
8189 | #endif | |
b772434b | 8190 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 8191 | |
2b188cc1 JA |
8192 | if (ret) |
8193 | return ret; | |
8194 | } | |
8195 | ||
950e79dd OL |
8196 | if (uts) { |
8197 | struct timespec64 ts; | |
8198 | ||
8199 | if (get_timespec64(&ts, uts)) | |
8200 | return -EFAULT; | |
8201 | timeout = ktime_add_ns(timespec64_to_ktime(ts), ktime_get_ns()); | |
8202 | } | |
8203 | ||
90291099 PB |
8204 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
8205 | iowq.wq.private = current; | |
8206 | INIT_LIST_HEAD(&iowq.wq.entry); | |
8207 | iowq.ctx = ctx; | |
bda52162 | 8208 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
5fd46178 | 8209 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
90291099 | 8210 | |
c826bd7a | 8211 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 8212 | do { |
ca0a2651 | 8213 | /* if we can't even flush overflow, don't wait for more */ |
90f67366 | 8214 | if (!io_cqring_overflow_flush(ctx)) { |
ca0a2651 JA |
8215 | ret = -EBUSY; |
8216 | break; | |
8217 | } | |
311997b3 | 8218 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
bda52162 | 8219 | TASK_INTERRUPTIBLE); |
22833966 | 8220 | ret = io_cqring_wait_schedule(ctx, &iowq, timeout); |
ca0a2651 | 8221 | cond_resched(); |
eeb60b9a | 8222 | } while (ret > 0); |
bda52162 | 8223 | |
b4f20bb4 | 8224 | finish_wait(&ctx->cq_wait, &iowq.wq); |
b7db41c9 | 8225 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 8226 | |
75b28aff | 8227 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
8228 | } |
8229 | ||
9123c8ff | 8230 | static void io_free_page_table(void **table, size_t size) |
05f3fb3c | 8231 | { |
9123c8ff | 8232 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
05f3fb3c | 8233 | |
846a4ef2 | 8234 | for (i = 0; i < nr_tables; i++) |
9123c8ff PB |
8235 | kfree(table[i]); |
8236 | kfree(table); | |
8237 | } | |
8238 | ||
c072481d | 8239 | static __cold void **io_alloc_page_table(size_t size) |
9123c8ff PB |
8240 | { |
8241 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); | |
8242 | size_t init_size = size; | |
8243 | void **table; | |
8244 | ||
0bea96f5 | 8245 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
8246 | if (!table) |
8247 | return NULL; | |
8248 | ||
8249 | for (i = 0; i < nr_tables; i++) { | |
27f6b318 | 8250 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
9123c8ff | 8251 | |
0bea96f5 | 8252 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
8253 | if (!table[i]) { |
8254 | io_free_page_table(table, init_size); | |
8255 | return NULL; | |
8256 | } | |
8257 | size -= this_size; | |
8258 | } | |
8259 | return table; | |
05f3fb3c JA |
8260 | } |
8261 | ||
28a9fe25 | 8262 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
1642b445 | 8263 | { |
28a9fe25 PB |
8264 | percpu_ref_exit(&ref_node->refs); |
8265 | kfree(ref_node); | |
1642b445 PB |
8266 | } |
8267 | ||
c072481d | 8268 | static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
b9bd2bea PB |
8269 | { |
8270 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); | |
8271 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; | |
8272 | unsigned long flags; | |
8273 | bool first_add = false; | |
b36a2050 | 8274 | unsigned long delay = HZ; |
b9bd2bea PB |
8275 | |
8276 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); | |
8277 | node->done = true; | |
8278 | ||
b36a2050 DY |
8279 | /* if we are mid-quiesce then do not delay */ |
8280 | if (node->rsrc_data->quiesce) | |
8281 | delay = 0; | |
8282 | ||
b9bd2bea PB |
8283 | while (!list_empty(&ctx->rsrc_ref_list)) { |
8284 | node = list_first_entry(&ctx->rsrc_ref_list, | |
8285 | struct io_rsrc_node, node); | |
8286 | /* recycle ref nodes in order */ | |
8287 | if (!node->done) | |
8288 | break; | |
8289 | list_del(&node->node); | |
8290 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); | |
8291 | } | |
8292 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); | |
8293 | ||
8294 | if (first_add) | |
b36a2050 | 8295 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
b9bd2bea PB |
8296 | } |
8297 | ||
f6133fbd | 8298 | static struct io_rsrc_node *io_rsrc_node_alloc(void) |
b9bd2bea PB |
8299 | { |
8300 | struct io_rsrc_node *ref_node; | |
8301 | ||
8302 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); | |
8303 | if (!ref_node) | |
8304 | return NULL; | |
8305 | ||
8306 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, | |
8307 | 0, GFP_KERNEL)) { | |
8308 | kfree(ref_node); | |
8309 | return NULL; | |
8310 | } | |
8311 | INIT_LIST_HEAD(&ref_node->node); | |
8312 | INIT_LIST_HEAD(&ref_node->rsrc_list); | |
8313 | ref_node->done = false; | |
8314 | return ref_node; | |
8315 | } | |
8316 | ||
a7f0ed5a PB |
8317 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
8318 | struct io_rsrc_data *data_to_kill) | |
ab409402 | 8319 | __must_hold(&ctx->uring_lock) |
6b06314c | 8320 | { |
a7f0ed5a PB |
8321 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
8322 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); | |
6b06314c | 8323 | |
ab409402 PB |
8324 | io_rsrc_refs_drop(ctx); |
8325 | ||
a7f0ed5a PB |
8326 | if (data_to_kill) { |
8327 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; | |
82fbcfa9 | 8328 | |
a7f0ed5a | 8329 | rsrc_node->rsrc_data = data_to_kill; |
4956b9ea | 8330 | spin_lock_irq(&ctx->rsrc_ref_lock); |
a7f0ed5a | 8331 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
4956b9ea | 8332 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
82fbcfa9 | 8333 | |
3e942498 | 8334 | atomic_inc(&data_to_kill->refs); |
a7f0ed5a PB |
8335 | percpu_ref_kill(&rsrc_node->refs); |
8336 | ctx->rsrc_node = NULL; | |
8337 | } | |
6b06314c | 8338 | |
a7f0ed5a PB |
8339 | if (!ctx->rsrc_node) { |
8340 | ctx->rsrc_node = ctx->rsrc_backup_node; | |
8341 | ctx->rsrc_backup_node = NULL; | |
8342 | } | |
8bad28d8 HX |
8343 | } |
8344 | ||
a7f0ed5a | 8345 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
8dd03afe PB |
8346 | { |
8347 | if (ctx->rsrc_backup_node) | |
8348 | return 0; | |
f6133fbd | 8349 | ctx->rsrc_backup_node = io_rsrc_node_alloc(); |
8dd03afe | 8350 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
8bad28d8 HX |
8351 | } |
8352 | ||
c072481d PB |
8353 | static __cold int io_rsrc_ref_quiesce(struct io_rsrc_data *data, |
8354 | struct io_ring_ctx *ctx) | |
8bad28d8 HX |
8355 | { |
8356 | int ret; | |
05589553 | 8357 | |
215c3902 | 8358 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
8bad28d8 HX |
8359 | if (data->quiesce) |
8360 | return -ENXIO; | |
05589553 | 8361 | |
8bad28d8 | 8362 | data->quiesce = true; |
1ffc5422 | 8363 | do { |
a7f0ed5a | 8364 | ret = io_rsrc_node_switch_start(ctx); |
8dd03afe | 8365 | if (ret) |
f2303b1f | 8366 | break; |
a7f0ed5a | 8367 | io_rsrc_node_switch(ctx, data); |
f2303b1f | 8368 | |
3e942498 PB |
8369 | /* kill initial ref, already quiesced if zero */ |
8370 | if (atomic_dec_and_test(&data->refs)) | |
8371 | break; | |
c018db4a | 8372 | mutex_unlock(&ctx->uring_lock); |
8bad28d8 | 8373 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 | 8374 | ret = wait_for_completion_interruptible(&data->done); |
c018db4a JA |
8375 | if (!ret) { |
8376 | mutex_lock(&ctx->uring_lock); | |
80912cef DY |
8377 | if (atomic_read(&data->refs) > 0) { |
8378 | /* | |
8379 | * it has been revived by another thread while | |
8380 | * we were unlocked | |
8381 | */ | |
8382 | mutex_unlock(&ctx->uring_lock); | |
8383 | } else { | |
8384 | break; | |
8385 | } | |
c018db4a | 8386 | } |
8bad28d8 | 8387 | |
3e942498 PB |
8388 | atomic_inc(&data->refs); |
8389 | /* wait for all works potentially completing data->done */ | |
8390 | flush_delayed_work(&ctx->rsrc_put_work); | |
cb5e1b81 | 8391 | reinit_completion(&data->done); |
8dd03afe | 8392 | |
1ffc5422 | 8393 | ret = io_run_task_work_sig(); |
8bad28d8 | 8394 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 8395 | } while (ret >= 0); |
8bad28d8 | 8396 | data->quiesce = false; |
05f3fb3c | 8397 | |
8bad28d8 | 8398 | return ret; |
d7954b2b BM |
8399 | } |
8400 | ||
2d091d62 PB |
8401 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
8402 | { | |
8403 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; | |
8404 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; | |
8405 | ||
8406 | return &data->tags[table_idx][off]; | |
8407 | } | |
8408 | ||
44b31f2f | 8409 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
1ad555c6 | 8410 | { |
2d091d62 PB |
8411 | size_t size = data->nr * sizeof(data->tags[0][0]); |
8412 | ||
8413 | if (data->tags) | |
8414 | io_free_page_table((void **)data->tags, size); | |
44b31f2f PB |
8415 | kfree(data); |
8416 | } | |
8417 | ||
c072481d PB |
8418 | static __cold int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
8419 | u64 __user *utags, unsigned nr, | |
8420 | struct io_rsrc_data **pdata) | |
1ad555c6 | 8421 | { |
b895c9a6 | 8422 | struct io_rsrc_data *data; |
2d091d62 | 8423 | int ret = -ENOMEM; |
d878c816 | 8424 | unsigned i; |
1ad555c6 BM |
8425 | |
8426 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
8427 | if (!data) | |
d878c816 | 8428 | return -ENOMEM; |
2d091d62 | 8429 | data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0])); |
b60c8dce | 8430 | if (!data->tags) { |
1ad555c6 | 8431 | kfree(data); |
d878c816 PB |
8432 | return -ENOMEM; |
8433 | } | |
2d091d62 PB |
8434 | |
8435 | data->nr = nr; | |
8436 | data->ctx = ctx; | |
8437 | data->do_put = do_put; | |
d878c816 | 8438 | if (utags) { |
2d091d62 | 8439 | ret = -EFAULT; |
d878c816 | 8440 | for (i = 0; i < nr; i++) { |
fdd1dc31 CIK |
8441 | u64 *tag_slot = io_get_tag_slot(data, i); |
8442 | ||
8443 | if (copy_from_user(tag_slot, &utags[i], | |
8444 | sizeof(*tag_slot))) | |
2d091d62 | 8445 | goto fail; |
d878c816 | 8446 | } |
1ad555c6 | 8447 | } |
b60c8dce | 8448 | |
3e942498 | 8449 | atomic_set(&data->refs, 1); |
1ad555c6 | 8450 | init_completion(&data->done); |
d878c816 PB |
8451 | *pdata = data; |
8452 | return 0; | |
2d091d62 PB |
8453 | fail: |
8454 | io_rsrc_data_free(data); | |
8455 | return ret; | |
1ad555c6 BM |
8456 | } |
8457 | ||
9123c8ff PB |
8458 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
8459 | { | |
0bea96f5 PB |
8460 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
8461 | GFP_KERNEL_ACCOUNT); | |
9123c8ff PB |
8462 | return !!table->files; |
8463 | } | |
8464 | ||
042b0d85 | 8465 | static void io_free_file_tables(struct io_file_table *table) |
9123c8ff | 8466 | { |
042b0d85 | 8467 | kvfree(table->files); |
9123c8ff PB |
8468 | table->files = NULL; |
8469 | } | |
8470 | ||
fff4db76 | 8471 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
1ad555c6 | 8472 | { |
1f59bc0f PB |
8473 | int i; |
8474 | ||
8475 | for (i = 0; i < ctx->nr_user_files; i++) { | |
8476 | struct file *file = io_file_from_index(ctx, i); | |
8477 | ||
8478 | if (!file || io_file_need_scm(file)) | |
8479 | continue; | |
8480 | io_fixed_file_slot(&ctx->file_table, i)->file_ptr = 0; | |
8481 | fput(file); | |
8482 | } | |
8483 | ||
fff4db76 PB |
8484 | #if defined(CONFIG_UNIX) |
8485 | if (ctx->ring_sock) { | |
8486 | struct sock *sock = ctx->ring_sock->sk; | |
8487 | struct sk_buff *skb; | |
8488 | ||
8489 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
8490 | kfree_skb(skb); | |
8491 | } | |
fff4db76 | 8492 | #endif |
042b0d85 | 8493 | io_free_file_tables(&ctx->file_table); |
44b31f2f | 8494 | io_rsrc_data_free(ctx->file_data); |
fff4db76 PB |
8495 | ctx->file_data = NULL; |
8496 | ctx->nr_user_files = 0; | |
1ad555c6 BM |
8497 | } |
8498 | ||
d7954b2b BM |
8499 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
8500 | { | |
d7954b2b BM |
8501 | int ret; |
8502 | ||
08480400 | 8503 | if (!ctx->file_data) |
d7954b2b | 8504 | return -ENXIO; |
08480400 PB |
8505 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
8506 | if (!ret) | |
8507 | __io_sqe_files_unregister(ctx); | |
8508 | return ret; | |
6b06314c JA |
8509 | } |
8510 | ||
37d1e2e3 | 8511 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
09a6f4ef | 8512 | __releases(&sqd->lock) |
37d1e2e3 | 8513 | { |
521d6a73 PB |
8514 | WARN_ON_ONCE(sqd->thread == current); |
8515 | ||
9e138a48 PB |
8516 | /* |
8517 | * Do the dance but not conditional clear_bit() because it'd race with | |
8518 | * other threads incrementing park_pending and setting the bit. | |
8519 | */ | |
37d1e2e3 | 8520 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
9e138a48 PB |
8521 | if (atomic_dec_return(&sqd->park_pending)) |
8522 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
09a6f4ef | 8523 | mutex_unlock(&sqd->lock); |
37d1e2e3 JA |
8524 | } |
8525 | ||
86e0d676 | 8526 | static void io_sq_thread_park(struct io_sq_data *sqd) |
09a6f4ef | 8527 | __acquires(&sqd->lock) |
37d1e2e3 | 8528 | { |
521d6a73 PB |
8529 | WARN_ON_ONCE(sqd->thread == current); |
8530 | ||
9e138a48 | 8531 | atomic_inc(&sqd->park_pending); |
86e0d676 | 8532 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
09a6f4ef | 8533 | mutex_lock(&sqd->lock); |
05962f95 | 8534 | if (sqd->thread) |
86e0d676 | 8535 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
8536 | } |
8537 | ||
8538 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
8539 | { | |
521d6a73 | 8540 | WARN_ON_ONCE(sqd->thread == current); |
88885f66 | 8541 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
521d6a73 | 8542 | |
05962f95 | 8543 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
88885f66 | 8544 | mutex_lock(&sqd->lock); |
e8f98f24 JA |
8545 | if (sqd->thread) |
8546 | wake_up_process(sqd->thread); | |
09a6f4ef | 8547 | mutex_unlock(&sqd->lock); |
05962f95 | 8548 | wait_for_completion(&sqd->exited); |
37d1e2e3 JA |
8549 | } |
8550 | ||
534ca6d6 | 8551 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 8552 | { |
534ca6d6 | 8553 | if (refcount_dec_and_test(&sqd->refs)) { |
9e138a48 PB |
8554 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
8555 | ||
37d1e2e3 JA |
8556 | io_sq_thread_stop(sqd); |
8557 | kfree(sqd); | |
8558 | } | |
8559 | } | |
8560 | ||
8561 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
8562 | { | |
8563 | struct io_sq_data *sqd = ctx->sq_data; | |
8564 | ||
8565 | if (sqd) { | |
05962f95 | 8566 | io_sq_thread_park(sqd); |
521d6a73 | 8567 | list_del_init(&ctx->sqd_list); |
37d1e2e3 | 8568 | io_sqd_update_thread_idle(sqd); |
05962f95 | 8569 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
8570 | |
8571 | io_put_sq_data(sqd); | |
8572 | ctx->sq_data = NULL; | |
534ca6d6 JA |
8573 | } |
8574 | } | |
8575 | ||
aa06165d JA |
8576 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
8577 | { | |
8578 | struct io_ring_ctx *ctx_attach; | |
8579 | struct io_sq_data *sqd; | |
8580 | struct fd f; | |
8581 | ||
8582 | f = fdget(p->wq_fd); | |
8583 | if (!f.file) | |
8584 | return ERR_PTR(-ENXIO); | |
8585 | if (f.file->f_op != &io_uring_fops) { | |
8586 | fdput(f); | |
8587 | return ERR_PTR(-EINVAL); | |
8588 | } | |
8589 | ||
8590 | ctx_attach = f.file->private_data; | |
8591 | sqd = ctx_attach->sq_data; | |
8592 | if (!sqd) { | |
8593 | fdput(f); | |
8594 | return ERR_PTR(-EINVAL); | |
8595 | } | |
5c2469e0 JA |
8596 | if (sqd->task_tgid != current->tgid) { |
8597 | fdput(f); | |
8598 | return ERR_PTR(-EPERM); | |
8599 | } | |
aa06165d JA |
8600 | |
8601 | refcount_inc(&sqd->refs); | |
8602 | fdput(f); | |
8603 | return sqd; | |
8604 | } | |
8605 | ||
26984fbf PB |
8606 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
8607 | bool *attached) | |
534ca6d6 JA |
8608 | { |
8609 | struct io_sq_data *sqd; | |
8610 | ||
26984fbf | 8611 | *attached = false; |
5c2469e0 JA |
8612 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
8613 | sqd = io_attach_sq_data(p); | |
26984fbf PB |
8614 | if (!IS_ERR(sqd)) { |
8615 | *attached = true; | |
5c2469e0 | 8616 | return sqd; |
26984fbf | 8617 | } |
5c2469e0 JA |
8618 | /* fall through for EPERM case, setup new sqd/task */ |
8619 | if (PTR_ERR(sqd) != -EPERM) | |
8620 | return sqd; | |
8621 | } | |
aa06165d | 8622 | |
534ca6d6 JA |
8623 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
8624 | if (!sqd) | |
8625 | return ERR_PTR(-ENOMEM); | |
8626 | ||
9e138a48 | 8627 | atomic_set(&sqd->park_pending, 0); |
534ca6d6 | 8628 | refcount_set(&sqd->refs, 1); |
69fb2131 | 8629 | INIT_LIST_HEAD(&sqd->ctx_list); |
09a6f4ef | 8630 | mutex_init(&sqd->lock); |
534ca6d6 | 8631 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 8632 | init_completion(&sqd->exited); |
534ca6d6 JA |
8633 | return sqd; |
8634 | } | |
8635 | ||
6b06314c JA |
8636 | /* |
8637 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
8638 | * the io_uring can be safely unregistered on process exit, even if we have | |
1f59bc0f PB |
8639 | * loops in the file referencing. We account only files that can hold other |
8640 | * files because otherwise they can't form a loop and so are not interesting | |
8641 | * for GC. | |
6b06314c | 8642 | */ |
8b3171bd | 8643 | static int io_scm_file_account(struct io_ring_ctx *ctx, struct file *file) |
6b06314c | 8644 | { |
73b25d3b | 8645 | #if defined(CONFIG_UNIX) |
6b06314c | 8646 | struct sock *sk = ctx->ring_sock->sk; |
73b25d3b | 8647 | struct sk_buff_head *head = &sk->sk_receive_queue; |
6b06314c JA |
8648 | struct scm_fp_list *fpl; |
8649 | struct sk_buff *skb; | |
6b06314c | 8650 | |
73b25d3b PB |
8651 | if (likely(!io_file_need_scm(file))) |
8652 | return 0; | |
8653 | ||
8654 | /* | |
8655 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
8656 | * file set. If there's no room, fall back to allocating a new skb | |
8657 | * and filling it in. | |
8658 | */ | |
8659 | spin_lock_irq(&head->lock); | |
8660 | skb = skb_peek(head); | |
8661 | if (skb && UNIXCB(skb).fp->count < SCM_MAX_FD) | |
8662 | __skb_unlink(skb, head); | |
8663 | else | |
8664 | skb = NULL; | |
8665 | spin_unlock_irq(&head->lock); | |
6b06314c | 8666 | |
6b06314c | 8667 | if (!skb) { |
73b25d3b PB |
8668 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
8669 | if (!fpl) | |
8670 | return -ENOMEM; | |
6b06314c | 8671 | |
73b25d3b PB |
8672 | skb = alloc_skb(0, GFP_KERNEL); |
8673 | if (!skb) { | |
8674 | kfree(fpl); | |
8675 | return -ENOMEM; | |
8676 | } | |
6b06314c | 8677 | |
73b25d3b PB |
8678 | fpl->user = get_uid(current_user()); |
8679 | fpl->max = SCM_MAX_FD; | |
8680 | fpl->count = 0; | |
dca58c6a | 8681 | |
73b25d3b PB |
8682 | UNIXCB(skb).fp = fpl; |
8683 | skb->sk = sk; | |
8684 | skb->destructor = unix_destruct_scm; | |
8685 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); | |
8686 | } | |
8687 | ||
8688 | fpl = UNIXCB(skb).fp; | |
8689 | fpl->fp[fpl->count++] = get_file(file); | |
8690 | unix_inflight(fpl->user, file); | |
8691 | skb_queue_head(head, skb); | |
dca58c6a | 8692 | fput(file); |
73b25d3b | 8693 | #endif |
6b06314c JA |
8694 | return 0; |
8695 | } | |
6b06314c | 8696 | |
47e90392 | 8697 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 8698 | { |
50238531 | 8699 | struct file *file = prsrc->file; |
05f3fb3c JA |
8700 | #if defined(CONFIG_UNIX) |
8701 | struct sock *sock = ctx->ring_sock->sk; | |
8702 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
8703 | struct sk_buff *skb; | |
8704 | int i; | |
8705 | ||
1f59bc0f PB |
8706 | if (!io_file_need_scm(file)) { |
8707 | fput(file); | |
8708 | return; | |
8709 | } | |
8710 | ||
05f3fb3c JA |
8711 | __skb_queue_head_init(&list); |
8712 | ||
8713 | /* | |
8714 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
8715 | * remove this entry and rearrange the file array. | |
8716 | */ | |
8717 | skb = skb_dequeue(head); | |
8718 | while (skb) { | |
8719 | struct scm_fp_list *fp; | |
8720 | ||
8721 | fp = UNIXCB(skb).fp; | |
8722 | for (i = 0; i < fp->count; i++) { | |
8723 | int left; | |
8724 | ||
8725 | if (fp->fp[i] != file) | |
8726 | continue; | |
8727 | ||
8728 | unix_notinflight(fp->user, fp->fp[i]); | |
8729 | left = fp->count - 1 - i; | |
8730 | if (left) { | |
8731 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
8732 | left * sizeof(struct file *)); | |
8733 | } | |
8734 | fp->count--; | |
8735 | if (!fp->count) { | |
8736 | kfree_skb(skb); | |
8737 | skb = NULL; | |
8738 | } else { | |
8739 | __skb_queue_tail(&list, skb); | |
8740 | } | |
8741 | fput(file); | |
8742 | file = NULL; | |
8743 | break; | |
8744 | } | |
8745 | ||
8746 | if (!file) | |
8747 | break; | |
8748 | ||
8749 | __skb_queue_tail(&list, skb); | |
8750 | ||
8751 | skb = skb_dequeue(head); | |
8752 | } | |
8753 | ||
8754 | if (skb_peek(&list)) { | |
8755 | spin_lock_irq(&head->lock); | |
8756 | while ((skb = __skb_dequeue(&list)) != NULL) | |
8757 | __skb_queue_tail(head, skb); | |
8758 | spin_unlock_irq(&head->lock); | |
8759 | } | |
8760 | #else | |
8761 | fput(file); | |
8762 | #endif | |
8763 | } | |
8764 | ||
b895c9a6 | 8765 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
65e19f54 | 8766 | { |
b895c9a6 | 8767 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
269bbe5f BM |
8768 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
8769 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 8770 | |
269bbe5f BM |
8771 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
8772 | list_del(&prsrc->list); | |
b60c8dce PB |
8773 | |
8774 | if (prsrc->tag) { | |
f8929630 PB |
8775 | if (ctx->flags & IORING_SETUP_IOPOLL) |
8776 | mutex_lock(&ctx->uring_lock); | |
b60c8dce | 8777 | |
79ebeaee | 8778 | spin_lock(&ctx->completion_lock); |
913a571a | 8779 | io_fill_cqe_aux(ctx, prsrc->tag, 0, 0); |
b60c8dce | 8780 | io_commit_cqring(ctx); |
79ebeaee | 8781 | spin_unlock(&ctx->completion_lock); |
b60c8dce | 8782 | io_cqring_ev_posted(ctx); |
f8929630 PB |
8783 | |
8784 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
8785 | mutex_unlock(&ctx->uring_lock); | |
b60c8dce PB |
8786 | } |
8787 | ||
40ae0ff7 | 8788 | rsrc_data->do_put(ctx, prsrc); |
269bbe5f | 8789 | kfree(prsrc); |
65e19f54 | 8790 | } |
05589553 | 8791 | |
28a9fe25 | 8792 | io_rsrc_node_destroy(ref_node); |
3e942498 PB |
8793 | if (atomic_dec_and_test(&rsrc_data->refs)) |
8794 | complete(&rsrc_data->done); | |
2faf852d | 8795 | } |
65e19f54 | 8796 | |
269bbe5f | 8797 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
8798 | { |
8799 | struct io_ring_ctx *ctx; | |
8800 | struct llist_node *node; | |
8801 | ||
269bbe5f BM |
8802 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
8803 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
8804 | |
8805 | while (node) { | |
b895c9a6 | 8806 | struct io_rsrc_node *ref_node; |
4a38aed2 JA |
8807 | struct llist_node *next = node->next; |
8808 | ||
b895c9a6 | 8809 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
269bbe5f | 8810 | __io_rsrc_put_work(ref_node); |
4a38aed2 JA |
8811 | node = next; |
8812 | } | |
8813 | } | |
8814 | ||
6b06314c | 8815 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
792e3582 | 8816 | unsigned nr_args, u64 __user *tags) |
6b06314c JA |
8817 | { |
8818 | __s32 __user *fds = (__s32 __user *) arg; | |
05f3fb3c | 8819 | struct file *file; |
f3baed39 | 8820 | int fd, ret; |
846a4ef2 | 8821 | unsigned i; |
6b06314c | 8822 | |
05f3fb3c | 8823 | if (ctx->file_data) |
6b06314c JA |
8824 | return -EBUSY; |
8825 | if (!nr_args) | |
8826 | return -EINVAL; | |
8827 | if (nr_args > IORING_MAX_FIXED_FILES) | |
8828 | return -EMFILE; | |
3a1b8a4e PB |
8829 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
8830 | return -EMFILE; | |
a7f0ed5a | 8831 | ret = io_rsrc_node_switch_start(ctx); |
f3baed39 PB |
8832 | if (ret) |
8833 | return ret; | |
d878c816 PB |
8834 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
8835 | &ctx->file_data); | |
8836 | if (ret) | |
8837 | return ret; | |
6b06314c | 8838 | |
a03a2a20 PB |
8839 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) { |
8840 | io_rsrc_data_free(ctx->file_data); | |
8841 | ctx->file_data = NULL; | |
8842 | return -ENOMEM; | |
8843 | } | |
65e19f54 | 8844 | |
08a45173 | 8845 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
a03a2a20 PB |
8846 | struct io_fixed_file *file_slot; |
8847 | ||
d878c816 | 8848 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
600cf3f8 | 8849 | ret = -EFAULT; |
a03a2a20 | 8850 | goto fail; |
600cf3f8 | 8851 | } |
08a45173 | 8852 | /* allow sparse sets */ |
792e3582 PB |
8853 | if (fd == -1) { |
8854 | ret = -EINVAL; | |
2d091d62 | 8855 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
a03a2a20 | 8856 | goto fail; |
08a45173 | 8857 | continue; |
792e3582 | 8858 | } |
6b06314c | 8859 | |
05f3fb3c | 8860 | file = fget(fd); |
6b06314c | 8861 | ret = -EBADF; |
792e3582 | 8862 | if (unlikely(!file)) |
a03a2a20 | 8863 | goto fail; |
05f3fb3c | 8864 | |
6b06314c JA |
8865 | /* |
8866 | * Don't allow io_uring instances to be registered. If UNIX | |
8867 | * isn't enabled, then this causes a reference cycle and this | |
8868 | * instance can never get freed. If UNIX is enabled we'll | |
8869 | * handle it just fine, but there's still no point in allowing | |
8870 | * a ring fd as it doesn't support regular read/write anyway. | |
8871 | */ | |
05f3fb3c JA |
8872 | if (file->f_op == &io_uring_fops) { |
8873 | fput(file); | |
a03a2a20 PB |
8874 | goto fail; |
8875 | } | |
8b3171bd | 8876 | ret = io_scm_file_account(ctx, file); |
a03a2a20 | 8877 | if (ret) { |
a03a2a20 PB |
8878 | fput(file); |
8879 | goto fail; | |
6b06314c | 8880 | } |
e390510a PB |
8881 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
8882 | io_fixed_file_set(file_slot, file); | |
05589553 | 8883 | } |
6b06314c | 8884 | |
a7f0ed5a | 8885 | io_rsrc_node_switch(ctx, NULL); |
a03a2a20 PB |
8886 | return 0; |
8887 | fail: | |
8888 | __io_sqe_files_unregister(ctx); | |
6b06314c JA |
8889 | return ret; |
8890 | } | |
8891 | ||
9c7b0ba8 PB |
8892 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
8893 | struct io_rsrc_node *node, void *rsrc) | |
8894 | { | |
8f0a2480 | 8895 | u64 *tag_slot = io_get_tag_slot(data, idx); |
9c7b0ba8 PB |
8896 | struct io_rsrc_put *prsrc; |
8897 | ||
8898 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); | |
8899 | if (!prsrc) | |
8900 | return -ENOMEM; | |
8901 | ||
8f0a2480 PB |
8902 | prsrc->tag = *tag_slot; |
8903 | *tag_slot = 0; | |
9c7b0ba8 PB |
8904 | prsrc->rsrc = rsrc; |
8905 | list_add(&prsrc->list, &node->rsrc_list); | |
8906 | return 0; | |
8907 | } | |
8908 | ||
b9445598 PB |
8909 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
8910 | unsigned int issue_flags, u32 slot_index) | |
8911 | { | |
8912 | struct io_ring_ctx *ctx = req->ctx; | |
9c7b0ba8 | 8913 | bool needs_switch = false; |
b9445598 PB |
8914 | struct io_fixed_file *file_slot; |
8915 | int ret = -EBADF; | |
8916 | ||
f8929630 | 8917 | io_ring_submit_lock(ctx, issue_flags); |
b9445598 PB |
8918 | if (file->f_op == &io_uring_fops) |
8919 | goto err; | |
8920 | ret = -ENXIO; | |
8921 | if (!ctx->file_data) | |
8922 | goto err; | |
8923 | ret = -EINVAL; | |
8924 | if (slot_index >= ctx->nr_user_files) | |
8925 | goto err; | |
8926 | ||
8927 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); | |
8928 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); | |
9c7b0ba8 PB |
8929 | |
8930 | if (file_slot->file_ptr) { | |
8931 | struct file *old_file; | |
8932 | ||
8933 | ret = io_rsrc_node_switch_start(ctx); | |
8934 | if (ret) | |
8935 | goto err; | |
8936 | ||
8937 | old_file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8938 | ret = io_queue_rsrc_removal(ctx->file_data, slot_index, | |
8939 | ctx->rsrc_node, old_file); | |
8940 | if (ret) | |
8941 | goto err; | |
8942 | file_slot->file_ptr = 0; | |
8943 | needs_switch = true; | |
8944 | } | |
b9445598 | 8945 | |
8b3171bd | 8946 | ret = io_scm_file_account(ctx, file); |
e390510a PB |
8947 | if (!ret) { |
8948 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; | |
8949 | io_fixed_file_set(file_slot, file); | |
b9445598 | 8950 | } |
b9445598 | 8951 | err: |
9c7b0ba8 PB |
8952 | if (needs_switch) |
8953 | io_rsrc_node_switch(ctx, ctx->file_data); | |
f8929630 | 8954 | io_ring_submit_unlock(ctx, issue_flags); |
b9445598 PB |
8955 | if (ret) |
8956 | fput(file); | |
8957 | return ret; | |
8958 | } | |
8959 | ||
7df778be PB |
8960 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) |
8961 | { | |
8962 | unsigned int offset = req->close.file_slot - 1; | |
8963 | struct io_ring_ctx *ctx = req->ctx; | |
8964 | struct io_fixed_file *file_slot; | |
8965 | struct file *file; | |
4cdd158b | 8966 | int ret; |
7df778be | 8967 | |
f8929630 | 8968 | io_ring_submit_lock(ctx, issue_flags); |
7df778be PB |
8969 | ret = -ENXIO; |
8970 | if (unlikely(!ctx->file_data)) | |
8971 | goto out; | |
8972 | ret = -EINVAL; | |
8973 | if (offset >= ctx->nr_user_files) | |
8974 | goto out; | |
8975 | ret = io_rsrc_node_switch_start(ctx); | |
8976 | if (ret) | |
8977 | goto out; | |
8978 | ||
4cdd158b PB |
8979 | offset = array_index_nospec(offset, ctx->nr_user_files); |
8980 | file_slot = io_fixed_file_slot(&ctx->file_table, offset); | |
7df778be PB |
8981 | ret = -EBADF; |
8982 | if (!file_slot->file_ptr) | |
8983 | goto out; | |
8984 | ||
8985 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8986 | ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file); | |
8987 | if (ret) | |
8988 | goto out; | |
8989 | ||
8990 | file_slot->file_ptr = 0; | |
8991 | io_rsrc_node_switch(ctx, ctx->file_data); | |
8992 | ret = 0; | |
8993 | out: | |
f8929630 | 8994 | io_ring_submit_unlock(ctx, issue_flags); |
7df778be PB |
8995 | return ret; |
8996 | } | |
8997 | ||
05f3fb3c | 8998 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
c3bdad02 | 8999 | struct io_uring_rsrc_update2 *up, |
05f3fb3c JA |
9000 | unsigned nr_args) |
9001 | { | |
c3bdad02 | 9002 | u64 __user *tags = u64_to_user_ptr(up->tags); |
98f0b3b4 | 9003 | __s32 __user *fds = u64_to_user_ptr(up->data); |
b895c9a6 | 9004 | struct io_rsrc_data *data = ctx->file_data; |
a04b0ac0 PB |
9005 | struct io_fixed_file *file_slot; |
9006 | struct file *file; | |
98f0b3b4 PB |
9007 | int fd, i, err = 0; |
9008 | unsigned int done; | |
05589553 | 9009 | bool needs_switch = false; |
c3a31e60 | 9010 | |
98f0b3b4 PB |
9011 | if (!ctx->file_data) |
9012 | return -ENXIO; | |
9013 | if (up->offset + nr_args > ctx->nr_user_files) | |
c3a31e60 JA |
9014 | return -EINVAL; |
9015 | ||
67973b93 | 9016 | for (done = 0; done < nr_args; done++) { |
c3bdad02 PB |
9017 | u64 tag = 0; |
9018 | ||
9019 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || | |
9020 | copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
c3a31e60 JA |
9021 | err = -EFAULT; |
9022 | break; | |
9023 | } | |
c3bdad02 PB |
9024 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
9025 | err = -EINVAL; | |
9026 | break; | |
9027 | } | |
4e0377a1 | 9028 | if (fd == IORING_REGISTER_FILES_SKIP) |
9029 | continue; | |
9030 | ||
67973b93 | 9031 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
aeca241b | 9032 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
ea64ec02 | 9033 | |
a04b0ac0 PB |
9034 | if (file_slot->file_ptr) { |
9035 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
4cdd158b | 9036 | err = io_queue_rsrc_removal(data, i, ctx->rsrc_node, file); |
a5318d3c HD |
9037 | if (err) |
9038 | break; | |
a04b0ac0 | 9039 | file_slot->file_ptr = 0; |
05589553 | 9040 | needs_switch = true; |
c3a31e60 JA |
9041 | } |
9042 | if (fd != -1) { | |
c3a31e60 JA |
9043 | file = fget(fd); |
9044 | if (!file) { | |
9045 | err = -EBADF; | |
9046 | break; | |
9047 | } | |
9048 | /* | |
9049 | * Don't allow io_uring instances to be registered. If | |
9050 | * UNIX isn't enabled, then this causes a reference | |
9051 | * cycle and this instance can never get freed. If UNIX | |
9052 | * is enabled we'll handle it just fine, but there's | |
9053 | * still no point in allowing a ring fd as it doesn't | |
9054 | * support regular read/write anyway. | |
9055 | */ | |
9056 | if (file->f_op == &io_uring_fops) { | |
9057 | fput(file); | |
9058 | err = -EBADF; | |
9059 | break; | |
9060 | } | |
8b3171bd | 9061 | err = io_scm_file_account(ctx, file); |
f3bd9dae YY |
9062 | if (err) { |
9063 | fput(file); | |
c3a31e60 | 9064 | break; |
f3bd9dae | 9065 | } |
e390510a PB |
9066 | *io_get_tag_slot(data, i) = tag; |
9067 | io_fixed_file_set(file_slot, file); | |
c3a31e60 | 9068 | } |
05f3fb3c JA |
9069 | } |
9070 | ||
a7f0ed5a PB |
9071 | if (needs_switch) |
9072 | io_rsrc_node_switch(ctx, data); | |
c3a31e60 JA |
9073 | return done ? done : err; |
9074 | } | |
05589553 | 9075 | |
685fe7fe JA |
9076 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
9077 | struct task_struct *task) | |
24369c2e | 9078 | { |
e941894e | 9079 | struct io_wq_hash *hash; |
24369c2e | 9080 | struct io_wq_data data; |
24369c2e | 9081 | unsigned int concurrency; |
24369c2e | 9082 | |
362a9e65 | 9083 | mutex_lock(&ctx->uring_lock); |
e941894e JA |
9084 | hash = ctx->hash_map; |
9085 | if (!hash) { | |
9086 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
362a9e65 YY |
9087 | if (!hash) { |
9088 | mutex_unlock(&ctx->uring_lock); | |
e941894e | 9089 | return ERR_PTR(-ENOMEM); |
362a9e65 | 9090 | } |
e941894e JA |
9091 | refcount_set(&hash->refs, 1); |
9092 | init_waitqueue_head(&hash->wait); | |
9093 | ctx->hash_map = hash; | |
24369c2e | 9094 | } |
362a9e65 | 9095 | mutex_unlock(&ctx->uring_lock); |
24369c2e | 9096 | |
e941894e | 9097 | data.hash = hash; |
685fe7fe | 9098 | data.task = task; |
ebc11b6c | 9099 | data.free_work = io_wq_free_work; |
f5fa38c5 | 9100 | data.do_work = io_wq_submit_work; |
24369c2e | 9101 | |
d25e3a3d JA |
9102 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
9103 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 9104 | |
5aa75ed5 | 9105 | return io_wq_create(concurrency, &data); |
24369c2e PB |
9106 | } |
9107 | ||
c072481d PB |
9108 | static __cold int io_uring_alloc_task_context(struct task_struct *task, |
9109 | struct io_ring_ctx *ctx) | |
0f212204 JA |
9110 | { |
9111 | struct io_uring_task *tctx; | |
d8a6df10 | 9112 | int ret; |
0f212204 | 9113 | |
09899b19 | 9114 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
0f212204 JA |
9115 | if (unlikely(!tctx)) |
9116 | return -ENOMEM; | |
9117 | ||
e7a6c00d JA |
9118 | tctx->registered_rings = kcalloc(IO_RINGFD_REG_MAX, |
9119 | sizeof(struct file *), GFP_KERNEL); | |
9120 | if (unlikely(!tctx->registered_rings)) { | |
9121 | kfree(tctx); | |
9122 | return -ENOMEM; | |
9123 | } | |
9124 | ||
d8a6df10 JA |
9125 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
9126 | if (unlikely(ret)) { | |
e7a6c00d | 9127 | kfree(tctx->registered_rings); |
d8a6df10 JA |
9128 | kfree(tctx); |
9129 | return ret; | |
9130 | } | |
9131 | ||
685fe7fe | 9132 | tctx->io_wq = io_init_wq_offload(ctx, task); |
5aa75ed5 JA |
9133 | if (IS_ERR(tctx->io_wq)) { |
9134 | ret = PTR_ERR(tctx->io_wq); | |
9135 | percpu_counter_destroy(&tctx->inflight); | |
e7a6c00d | 9136 | kfree(tctx->registered_rings); |
5aa75ed5 JA |
9137 | kfree(tctx); |
9138 | return ret; | |
9139 | } | |
9140 | ||
0f212204 JA |
9141 | xa_init(&tctx->xa); |
9142 | init_waitqueue_head(&tctx->wait); | |
fdaf083c | 9143 | atomic_set(&tctx->in_idle, 0); |
0f212204 | 9144 | task->io_uring = tctx; |
7cbf1722 JA |
9145 | spin_lock_init(&tctx->task_lock); |
9146 | INIT_WQ_LIST(&tctx->task_list); | |
4813c377 | 9147 | INIT_WQ_LIST(&tctx->prior_task_list); |
7cbf1722 | 9148 | init_task_work(&tctx->task_work, tctx_task_work); |
0f212204 JA |
9149 | return 0; |
9150 | } | |
9151 | ||
9152 | void __io_uring_free(struct task_struct *tsk) | |
9153 | { | |
9154 | struct io_uring_task *tctx = tsk->io_uring; | |
9155 | ||
9156 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e | 9157 | WARN_ON_ONCE(tctx->io_wq); |
09899b19 | 9158 | WARN_ON_ONCE(tctx->cached_refs); |
ef8eaa4e | 9159 | |
e7a6c00d | 9160 | kfree(tctx->registered_rings); |
d8a6df10 | 9161 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
9162 | kfree(tctx); |
9163 | tsk->io_uring = NULL; | |
9164 | } | |
9165 | ||
c072481d PB |
9166 | static __cold int io_sq_offload_create(struct io_ring_ctx *ctx, |
9167 | struct io_uring_params *p) | |
2b188cc1 JA |
9168 | { |
9169 | int ret; | |
9170 | ||
d25e3a3d JA |
9171 | /* Retain compatibility with failing for an invalid attach attempt */ |
9172 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
9173 | IORING_SETUP_ATTACH_WQ) { | |
9174 | struct fd f; | |
9175 | ||
9176 | f = fdget(p->wq_fd); | |
9177 | if (!f.file) | |
9178 | return -ENXIO; | |
0cc936f7 JA |
9179 | if (f.file->f_op != &io_uring_fops) { |
9180 | fdput(f); | |
f2a48dd0 | 9181 | return -EINVAL; |
0cc936f7 JA |
9182 | } |
9183 | fdput(f); | |
d25e3a3d | 9184 | } |
6c271ce2 | 9185 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 9186 | struct task_struct *tsk; |
534ca6d6 | 9187 | struct io_sq_data *sqd; |
26984fbf | 9188 | bool attached; |
534ca6d6 | 9189 | |
cdc1404a PM |
9190 | ret = security_uring_sqpoll(); |
9191 | if (ret) | |
9192 | return ret; | |
9193 | ||
26984fbf | 9194 | sqd = io_get_sq_data(p, &attached); |
534ca6d6 JA |
9195 | if (IS_ERR(sqd)) { |
9196 | ret = PTR_ERR(sqd); | |
9197 | goto err; | |
9198 | } | |
69fb2131 | 9199 | |
7c30f36a | 9200 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 9201 | ctx->sq_data = sqd; |
917257da JA |
9202 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
9203 | if (!ctx->sq_thread_idle) | |
9204 | ctx->sq_thread_idle = HZ; | |
9205 | ||
78d7f6ba | 9206 | io_sq_thread_park(sqd); |
de75a3d3 PB |
9207 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
9208 | io_sqd_update_thread_idle(sqd); | |
26984fbf | 9209 | /* don't attach to a dying SQPOLL thread, would be racy */ |
f2a48dd0 | 9210 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
78d7f6ba PB |
9211 | io_sq_thread_unpark(sqd); |
9212 | ||
de75a3d3 PB |
9213 | if (ret < 0) |
9214 | goto err; | |
9215 | if (attached) | |
5aa75ed5 | 9216 | return 0; |
aa06165d | 9217 | |
6c271ce2 | 9218 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 9219 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 9220 | |
917257da | 9221 | ret = -EINVAL; |
f2a48dd0 | 9222 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
e8f98f24 | 9223 | goto err_sqpoll; |
37d1e2e3 | 9224 | sqd->sq_cpu = cpu; |
6c271ce2 | 9225 | } else { |
37d1e2e3 | 9226 | sqd->sq_cpu = -1; |
6c271ce2 | 9227 | } |
37d1e2e3 JA |
9228 | |
9229 | sqd->task_pid = current->pid; | |
5c2469e0 | 9230 | sqd->task_tgid = current->tgid; |
46fe18b1 JA |
9231 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
9232 | if (IS_ERR(tsk)) { | |
9233 | ret = PTR_ERR(tsk); | |
e8f98f24 | 9234 | goto err_sqpoll; |
6c271ce2 | 9235 | } |
97a73a0f | 9236 | |
46fe18b1 | 9237 | sqd->thread = tsk; |
97a73a0f | 9238 | ret = io_uring_alloc_task_context(tsk, ctx); |
46fe18b1 | 9239 | wake_up_new_task(tsk); |
0f212204 JA |
9240 | if (ret) |
9241 | goto err; | |
6c271ce2 JA |
9242 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
9243 | /* Can't have SQ_AFF without SQPOLL */ | |
9244 | ret = -EINVAL; | |
9245 | goto err; | |
9246 | } | |
9247 | ||
2b188cc1 | 9248 | return 0; |
f2a48dd0 PB |
9249 | err_sqpoll: |
9250 | complete(&ctx->sq_data->exited); | |
2b188cc1 | 9251 | err: |
37d1e2e3 | 9252 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
9253 | return ret; |
9254 | } | |
9255 | ||
a087e2b5 BM |
9256 | static inline void __io_unaccount_mem(struct user_struct *user, |
9257 | unsigned long nr_pages) | |
2b188cc1 JA |
9258 | { |
9259 | atomic_long_sub(nr_pages, &user->locked_vm); | |
9260 | } | |
9261 | ||
a087e2b5 BM |
9262 | static inline int __io_account_mem(struct user_struct *user, |
9263 | unsigned long nr_pages) | |
2b188cc1 JA |
9264 | { |
9265 | unsigned long page_limit, cur_pages, new_pages; | |
9266 | ||
9267 | /* Don't allow more pages than we can safely lock */ | |
9268 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
9269 | ||
9270 | do { | |
9271 | cur_pages = atomic_long_read(&user->locked_vm); | |
9272 | new_pages = cur_pages + nr_pages; | |
9273 | if (new_pages > page_limit) | |
9274 | return -ENOMEM; | |
9275 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
9276 | new_pages) != cur_pages); | |
9277 | ||
9278 | return 0; | |
9279 | } | |
9280 | ||
26bfa89e | 9281 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 9282 | { |
62e398be | 9283 | if (ctx->user) |
a087e2b5 | 9284 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 9285 | |
26bfa89e JA |
9286 | if (ctx->mm_account) |
9287 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
9288 | } |
9289 | ||
26bfa89e | 9290 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 9291 | { |
30975825 BM |
9292 | int ret; |
9293 | ||
62e398be | 9294 | if (ctx->user) { |
30975825 BM |
9295 | ret = __io_account_mem(ctx->user, nr_pages); |
9296 | if (ret) | |
9297 | return ret; | |
9298 | } | |
9299 | ||
26bfa89e JA |
9300 | if (ctx->mm_account) |
9301 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
9302 | |
9303 | return 0; | |
9304 | } | |
9305 | ||
2b188cc1 JA |
9306 | static void io_mem_free(void *ptr) |
9307 | { | |
52e04ef4 MR |
9308 | struct page *page; |
9309 | ||
9310 | if (!ptr) | |
9311 | return; | |
2b188cc1 | 9312 | |
52e04ef4 | 9313 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
9314 | if (put_page_testzero(page)) |
9315 | free_compound_page(page); | |
9316 | } | |
9317 | ||
9318 | static void *io_mem_alloc(size_t size) | |
9319 | { | |
0a3f1e0b | 9320 | gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP; |
2b188cc1 | 9321 | |
0a3f1e0b | 9322 | return (void *) __get_free_pages(gfp, get_order(size)); |
2b188cc1 JA |
9323 | } |
9324 | ||
75b28aff HV |
9325 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
9326 | size_t *sq_offset) | |
9327 | { | |
9328 | struct io_rings *rings; | |
9329 | size_t off, sq_array_size; | |
9330 | ||
9331 | off = struct_size(rings, cqes, cq_entries); | |
9332 | if (off == SIZE_MAX) | |
9333 | return SIZE_MAX; | |
9334 | ||
9335 | #ifdef CONFIG_SMP | |
9336 | off = ALIGN(off, SMP_CACHE_BYTES); | |
9337 | if (off == 0) | |
9338 | return SIZE_MAX; | |
9339 | #endif | |
9340 | ||
b36200f5 DV |
9341 | if (sq_offset) |
9342 | *sq_offset = off; | |
9343 | ||
75b28aff HV |
9344 | sq_array_size = array_size(sizeof(u32), sq_entries); |
9345 | if (sq_array_size == SIZE_MAX) | |
9346 | return SIZE_MAX; | |
9347 | ||
9348 | if (check_add_overflow(off, sq_array_size, &off)) | |
9349 | return SIZE_MAX; | |
9350 | ||
75b28aff HV |
9351 | return off; |
9352 | } | |
9353 | ||
41edf1a5 | 9354 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot) |
7f61a1e9 | 9355 | { |
41edf1a5 | 9356 | struct io_mapped_ubuf *imu = *slot; |
7f61a1e9 PB |
9357 | unsigned int i; |
9358 | ||
6224843d PB |
9359 | if (imu != ctx->dummy_ubuf) { |
9360 | for (i = 0; i < imu->nr_bvecs; i++) | |
9361 | unpin_user_page(imu->bvec[i].bv_page); | |
9362 | if (imu->acct_pages) | |
9363 | io_unaccount_mem(ctx, imu->acct_pages); | |
9364 | kvfree(imu); | |
9365 | } | |
41edf1a5 | 9366 | *slot = NULL; |
7f61a1e9 PB |
9367 | } |
9368 | ||
bd54b6fe | 9369 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
edafccee | 9370 | { |
634d00df PB |
9371 | io_buffer_unmap(ctx, &prsrc->buf); |
9372 | prsrc->buf = NULL; | |
bd54b6fe | 9373 | } |
edafccee | 9374 | |
bd54b6fe BM |
9375 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
9376 | { | |
9377 | unsigned int i; | |
edafccee | 9378 | |
7f61a1e9 PB |
9379 | for (i = 0; i < ctx->nr_user_bufs; i++) |
9380 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); | |
edafccee | 9381 | kfree(ctx->user_bufs); |
bb6659cc | 9382 | io_rsrc_data_free(ctx->buf_data); |
edafccee | 9383 | ctx->user_bufs = NULL; |
bd54b6fe | 9384 | ctx->buf_data = NULL; |
edafccee | 9385 | ctx->nr_user_bufs = 0; |
bd54b6fe BM |
9386 | } |
9387 | ||
0a96bbe4 | 9388 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee | 9389 | { |
bd54b6fe | 9390 | int ret; |
edafccee | 9391 | |
bd54b6fe | 9392 | if (!ctx->buf_data) |
edafccee JA |
9393 | return -ENXIO; |
9394 | ||
bd54b6fe BM |
9395 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
9396 | if (!ret) | |
9397 | __io_sqe_buffers_unregister(ctx); | |
9398 | return ret; | |
edafccee JA |
9399 | } |
9400 | ||
9401 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
9402 | void __user *arg, unsigned index) | |
9403 | { | |
9404 | struct iovec __user *src; | |
9405 | ||
9406 | #ifdef CONFIG_COMPAT | |
9407 | if (ctx->compat) { | |
9408 | struct compat_iovec __user *ciovs; | |
9409 | struct compat_iovec ciov; | |
9410 | ||
9411 | ciovs = (struct compat_iovec __user *) arg; | |
9412 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
9413 | return -EFAULT; | |
9414 | ||
d55e5f5b | 9415 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
9416 | dst->iov_len = ciov.iov_len; |
9417 | return 0; | |
9418 | } | |
9419 | #endif | |
9420 | src = (struct iovec __user *) arg; | |
9421 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
9422 | return -EFAULT; | |
9423 | return 0; | |
9424 | } | |
9425 | ||
de293938 JA |
9426 | /* |
9427 | * Not super efficient, but this is just a registration time. And we do cache | |
9428 | * the last compound head, so generally we'll only do a full search if we don't | |
9429 | * match that one. | |
9430 | * | |
9431 | * We check if the given compound head page has already been accounted, to | |
9432 | * avoid double accounting it. This allows us to account the full size of the | |
9433 | * page, not just the constituent pages of a huge page. | |
9434 | */ | |
9435 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
9436 | int nr_pages, struct page *hpage) | |
9437 | { | |
9438 | int i, j; | |
9439 | ||
9440 | /* check current page array */ | |
9441 | for (i = 0; i < nr_pages; i++) { | |
9442 | if (!PageCompound(pages[i])) | |
9443 | continue; | |
9444 | if (compound_head(pages[i]) == hpage) | |
9445 | return true; | |
9446 | } | |
9447 | ||
9448 | /* check previously registered pages */ | |
9449 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
41edf1a5 | 9450 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
de293938 JA |
9451 | |
9452 | for (j = 0; j < imu->nr_bvecs; j++) { | |
9453 | if (!PageCompound(imu->bvec[j].bv_page)) | |
9454 | continue; | |
9455 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
9456 | return true; | |
9457 | } | |
9458 | } | |
9459 | ||
9460 | return false; | |
9461 | } | |
9462 | ||
9463 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
9464 | int nr_pages, struct io_mapped_ubuf *imu, | |
9465 | struct page **last_hpage) | |
9466 | { | |
9467 | int i, ret; | |
9468 | ||
216e5835 | 9469 | imu->acct_pages = 0; |
de293938 JA |
9470 | for (i = 0; i < nr_pages; i++) { |
9471 | if (!PageCompound(pages[i])) { | |
9472 | imu->acct_pages++; | |
9473 | } else { | |
9474 | struct page *hpage; | |
9475 | ||
9476 | hpage = compound_head(pages[i]); | |
9477 | if (hpage == *last_hpage) | |
9478 | continue; | |
9479 | *last_hpage = hpage; | |
9480 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
9481 | continue; | |
9482 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
9483 | } | |
9484 | } | |
9485 | ||
9486 | if (!imu->acct_pages) | |
9487 | return 0; | |
9488 | ||
26bfa89e | 9489 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
9490 | if (ret) |
9491 | imu->acct_pages = 0; | |
9492 | return ret; | |
9493 | } | |
9494 | ||
0a96bbe4 | 9495 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
41edf1a5 | 9496 | struct io_mapped_ubuf **pimu, |
0a96bbe4 | 9497 | struct page **last_hpage) |
edafccee | 9498 | { |
41edf1a5 | 9499 | struct io_mapped_ubuf *imu = NULL; |
edafccee JA |
9500 | struct vm_area_struct **vmas = NULL; |
9501 | struct page **pages = NULL; | |
0a96bbe4 BM |
9502 | unsigned long off, start, end, ubuf; |
9503 | size_t size; | |
9504 | int ret, pret, nr_pages, i; | |
9505 | ||
6224843d PB |
9506 | if (!iov->iov_base) { |
9507 | *pimu = ctx->dummy_ubuf; | |
9508 | return 0; | |
9509 | } | |
9510 | ||
0a96bbe4 BM |
9511 | ubuf = (unsigned long) iov->iov_base; |
9512 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
9513 | start = ubuf >> PAGE_SHIFT; | |
9514 | nr_pages = end - start; | |
9515 | ||
41edf1a5 | 9516 | *pimu = NULL; |
0a96bbe4 BM |
9517 | ret = -ENOMEM; |
9518 | ||
9519 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
9520 | if (!pages) | |
9521 | goto done; | |
9522 | ||
9523 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
9524 | GFP_KERNEL); | |
9525 | if (!vmas) | |
9526 | goto done; | |
edafccee | 9527 | |
41edf1a5 | 9528 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
a2b4198c | 9529 | if (!imu) |
0a96bbe4 BM |
9530 | goto done; |
9531 | ||
9532 | ret = 0; | |
9533 | mmap_read_lock(current->mm); | |
9534 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
9535 | pages, vmas); | |
9536 | if (pret == nr_pages) { | |
9537 | /* don't support file backed memory */ | |
9538 | for (i = 0; i < nr_pages; i++) { | |
9539 | struct vm_area_struct *vma = vmas[i]; | |
9540 | ||
40dad765 PB |
9541 | if (vma_is_shmem(vma)) |
9542 | continue; | |
0a96bbe4 BM |
9543 | if (vma->vm_file && |
9544 | !is_file_hugepages(vma->vm_file)) { | |
9545 | ret = -EOPNOTSUPP; | |
9546 | break; | |
9547 | } | |
9548 | } | |
9549 | } else { | |
9550 | ret = pret < 0 ? pret : -EFAULT; | |
9551 | } | |
9552 | mmap_read_unlock(current->mm); | |
9553 | if (ret) { | |
9554 | /* | |
9555 | * if we did partial map, or found file backed vmas, | |
9556 | * release any pages we did get | |
9557 | */ | |
9558 | if (pret > 0) | |
9559 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9560 | goto done; |
9561 | } | |
9562 | ||
9563 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
9564 | if (ret) { | |
9565 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9566 | goto done; |
9567 | } | |
9568 | ||
9569 | off = ubuf & ~PAGE_MASK; | |
9570 | size = iov->iov_len; | |
9571 | for (i = 0; i < nr_pages; i++) { | |
9572 | size_t vec_len; | |
9573 | ||
9574 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
9575 | imu->bvec[i].bv_page = pages[i]; | |
9576 | imu->bvec[i].bv_len = vec_len; | |
9577 | imu->bvec[i].bv_offset = off; | |
9578 | off = 0; | |
9579 | size -= vec_len; | |
9580 | } | |
9581 | /* store original address for later verification */ | |
9582 | imu->ubuf = ubuf; | |
4751f53d | 9583 | imu->ubuf_end = ubuf + iov->iov_len; |
0a96bbe4 | 9584 | imu->nr_bvecs = nr_pages; |
41edf1a5 | 9585 | *pimu = imu; |
0a96bbe4 BM |
9586 | ret = 0; |
9587 | done: | |
41edf1a5 PB |
9588 | if (ret) |
9589 | kvfree(imu); | |
0a96bbe4 BM |
9590 | kvfree(pages); |
9591 | kvfree(vmas); | |
9592 | return ret; | |
9593 | } | |
9594 | ||
2b358604 | 9595 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 9596 | { |
87094465 PB |
9597 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
9598 | return ctx->user_bufs ? 0 : -ENOMEM; | |
2b358604 | 9599 | } |
edafccee | 9600 | |
2b358604 BM |
9601 | static int io_buffer_validate(struct iovec *iov) |
9602 | { | |
50e96989 PB |
9603 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
9604 | ||
2b358604 BM |
9605 | /* |
9606 | * Don't impose further limits on the size and buffer | |
9607 | * constraints here, we'll -EINVAL later when IO is | |
9608 | * submitted if they are wrong. | |
9609 | */ | |
6224843d PB |
9610 | if (!iov->iov_base) |
9611 | return iov->iov_len ? -EFAULT : 0; | |
9612 | if (!iov->iov_len) | |
2b358604 | 9613 | return -EFAULT; |
edafccee | 9614 | |
2b358604 BM |
9615 | /* arbitrary limit, but we need something */ |
9616 | if (iov->iov_len > SZ_1G) | |
9617 | return -EFAULT; | |
edafccee | 9618 | |
50e96989 PB |
9619 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
9620 | return -EOVERFLOW; | |
9621 | ||
2b358604 BM |
9622 | return 0; |
9623 | } | |
edafccee | 9624 | |
2b358604 | 9625 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
634d00df | 9626 | unsigned int nr_args, u64 __user *tags) |
2b358604 | 9627 | { |
bd54b6fe BM |
9628 | struct page *last_hpage = NULL; |
9629 | struct io_rsrc_data *data; | |
2b358604 BM |
9630 | int i, ret; |
9631 | struct iovec iov; | |
edafccee | 9632 | |
87094465 PB |
9633 | if (ctx->user_bufs) |
9634 | return -EBUSY; | |
489809e2 | 9635 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
87094465 | 9636 | return -EINVAL; |
bd54b6fe | 9637 | ret = io_rsrc_node_switch_start(ctx); |
2b358604 BM |
9638 | if (ret) |
9639 | return ret; | |
d878c816 PB |
9640 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
9641 | if (ret) | |
9642 | return ret; | |
bd54b6fe BM |
9643 | ret = io_buffers_map_alloc(ctx, nr_args); |
9644 | if (ret) { | |
bb6659cc | 9645 | io_rsrc_data_free(data); |
bd54b6fe BM |
9646 | return ret; |
9647 | } | |
edafccee | 9648 | |
87094465 | 9649 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
edafccee JA |
9650 | ret = io_copy_iov(ctx, &iov, arg, i); |
9651 | if (ret) | |
0a96bbe4 | 9652 | break; |
2b358604 BM |
9653 | ret = io_buffer_validate(&iov); |
9654 | if (ret) | |
0a96bbe4 | 9655 | break; |
2d091d62 | 9656 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
cf3770e7 CIK |
9657 | ret = -EINVAL; |
9658 | break; | |
9659 | } | |
edafccee | 9660 | |
41edf1a5 PB |
9661 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
9662 | &last_hpage); | |
0a96bbe4 BM |
9663 | if (ret) |
9664 | break; | |
edafccee | 9665 | } |
0a96bbe4 | 9666 | |
bd54b6fe | 9667 | WARN_ON_ONCE(ctx->buf_data); |
0a96bbe4 | 9668 | |
bd54b6fe BM |
9669 | ctx->buf_data = data; |
9670 | if (ret) | |
9671 | __io_sqe_buffers_unregister(ctx); | |
9672 | else | |
9673 | io_rsrc_node_switch(ctx, NULL); | |
edafccee JA |
9674 | return ret; |
9675 | } | |
9676 | ||
634d00df PB |
9677 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
9678 | struct io_uring_rsrc_update2 *up, | |
9679 | unsigned int nr_args) | |
9680 | { | |
9681 | u64 __user *tags = u64_to_user_ptr(up->tags); | |
9682 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); | |
634d00df PB |
9683 | struct page *last_hpage = NULL; |
9684 | bool needs_switch = false; | |
9685 | __u32 done; | |
9686 | int i, err; | |
9687 | ||
9688 | if (!ctx->buf_data) | |
9689 | return -ENXIO; | |
9690 | if (up->offset + nr_args > ctx->nr_user_bufs) | |
9691 | return -EINVAL; | |
9692 | ||
9693 | for (done = 0; done < nr_args; done++) { | |
0b8c0e7c PB |
9694 | struct io_mapped_ubuf *imu; |
9695 | int offset = up->offset + done; | |
634d00df PB |
9696 | u64 tag = 0; |
9697 | ||
9698 | err = io_copy_iov(ctx, &iov, iovs, done); | |
9699 | if (err) | |
9700 | break; | |
9701 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { | |
9702 | err = -EFAULT; | |
9703 | break; | |
9704 | } | |
0b8c0e7c PB |
9705 | err = io_buffer_validate(&iov); |
9706 | if (err) | |
9707 | break; | |
cf3770e7 CIK |
9708 | if (!iov.iov_base && tag) { |
9709 | err = -EINVAL; | |
9710 | break; | |
9711 | } | |
0b8c0e7c PB |
9712 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
9713 | if (err) | |
9714 | break; | |
634d00df | 9715 | |
0b8c0e7c | 9716 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
6224843d | 9717 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
4cdd158b | 9718 | err = io_queue_rsrc_removal(ctx->buf_data, i, |
0b8c0e7c PB |
9719 | ctx->rsrc_node, ctx->user_bufs[i]); |
9720 | if (unlikely(err)) { | |
9721 | io_buffer_unmap(ctx, &imu); | |
634d00df | 9722 | break; |
0b8c0e7c | 9723 | } |
634d00df PB |
9724 | ctx->user_bufs[i] = NULL; |
9725 | needs_switch = true; | |
9726 | } | |
9727 | ||
0b8c0e7c | 9728 | ctx->user_bufs[i] = imu; |
2d091d62 | 9729 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
634d00df PB |
9730 | } |
9731 | ||
9732 | if (needs_switch) | |
9733 | io_rsrc_node_switch(ctx, ctx->buf_data); | |
9734 | return done ? done : err; | |
9735 | } | |
9736 | ||
c75312dd UA |
9737 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg, |
9738 | unsigned int eventfd_async) | |
9b402849 | 9739 | { |
77bc59b4 | 9740 | struct io_ev_fd *ev_fd; |
9b402849 | 9741 | __s32 __user *fds = arg; |
f0a4e62b | 9742 | int fd; |
9b402849 | 9743 | |
77bc59b4 UA |
9744 | ev_fd = rcu_dereference_protected(ctx->io_ev_fd, |
9745 | lockdep_is_held(&ctx->uring_lock)); | |
9746 | if (ev_fd) | |
9b402849 JA |
9747 | return -EBUSY; |
9748 | ||
9749 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
9750 | return -EFAULT; | |
9751 | ||
77bc59b4 UA |
9752 | ev_fd = kmalloc(sizeof(*ev_fd), GFP_KERNEL); |
9753 | if (!ev_fd) | |
9754 | return -ENOMEM; | |
fe7e3257 | 9755 | |
77bc59b4 UA |
9756 | ev_fd->cq_ev_fd = eventfd_ctx_fdget(fd); |
9757 | if (IS_ERR(ev_fd->cq_ev_fd)) { | |
f0a4e62b | 9758 | int ret = PTR_ERR(ev_fd->cq_ev_fd); |
77bc59b4 | 9759 | kfree(ev_fd); |
9b402849 JA |
9760 | return ret; |
9761 | } | |
c75312dd | 9762 | ev_fd->eventfd_async = eventfd_async; |
9aa8dfde | 9763 | ctx->has_evfd = true; |
77bc59b4 | 9764 | rcu_assign_pointer(ctx->io_ev_fd, ev_fd); |
f0a4e62b | 9765 | return 0; |
77bc59b4 UA |
9766 | } |
9767 | ||
9768 | static void io_eventfd_put(struct rcu_head *rcu) | |
9769 | { | |
9770 | struct io_ev_fd *ev_fd = container_of(rcu, struct io_ev_fd, rcu); | |
9771 | ||
9772 | eventfd_ctx_put(ev_fd->cq_ev_fd); | |
9773 | kfree(ev_fd); | |
9b402849 JA |
9774 | } |
9775 | ||
9776 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
9777 | { | |
77bc59b4 UA |
9778 | struct io_ev_fd *ev_fd; |
9779 | ||
9780 | ev_fd = rcu_dereference_protected(ctx->io_ev_fd, | |
9781 | lockdep_is_held(&ctx->uring_lock)); | |
9782 | if (ev_fd) { | |
9aa8dfde | 9783 | ctx->has_evfd = false; |
77bc59b4 UA |
9784 | rcu_assign_pointer(ctx->io_ev_fd, NULL); |
9785 | call_rcu(&ev_fd->rcu, io_eventfd_put); | |
9b402849 JA |
9786 | return 0; |
9787 | } | |
9788 | ||
9789 | return -ENXIO; | |
9790 | } | |
9791 | ||
5a2e745d JA |
9792 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
9793 | { | |
dbc7d452 JA |
9794 | int i; |
9795 | ||
9796 | for (i = 0; i < (1U << IO_BUFFERS_HASH_BITS); i++) { | |
9797 | struct list_head *list = &ctx->io_buffers[i]; | |
9e15c3a0 | 9798 | |
dbc7d452 JA |
9799 | while (!list_empty(list)) { |
9800 | struct io_buffer_list *bl; | |
9801 | ||
9802 | bl = list_first_entry(list, struct io_buffer_list, list); | |
9803 | __io_remove_buffers(ctx, bl, -1U); | |
9804 | list_del(&bl->list); | |
9805 | kfree(bl); | |
9806 | } | |
9807 | } | |
cc3cec83 JA |
9808 | |
9809 | while (!list_empty(&ctx->io_buffers_pages)) { | |
9810 | struct page *page; | |
9811 | ||
9812 | page = list_first_entry(&ctx->io_buffers_pages, struct page, lru); | |
9813 | list_del_init(&page->lru); | |
9814 | __free_page(page); | |
9815 | } | |
5a2e745d JA |
9816 | } |
9817 | ||
4010fec4 | 9818 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9819 | { |
cd0ca2e0 | 9820 | struct io_submit_state *state = &ctx->submit_state; |
37f0e767 | 9821 | int nr = 0; |
bf019da7 | 9822 | |
9a4fdbd8 | 9823 | mutex_lock(&ctx->uring_lock); |
cd0ca2e0 | 9824 | io_flush_cached_locked_reqs(ctx, state); |
9a4fdbd8 | 9825 | |
88ab95be | 9826 | while (!io_req_cache_empty(ctx)) { |
c2b6c6bc PB |
9827 | struct io_wq_work_node *node; |
9828 | struct io_kiocb *req; | |
9a4fdbd8 | 9829 | |
c2b6c6bc PB |
9830 | node = wq_stack_extract(&state->free_list); |
9831 | req = container_of(node, struct io_kiocb, comp_list); | |
9832 | kmem_cache_free(req_cachep, req); | |
37f0e767 | 9833 | nr++; |
c2b6c6bc | 9834 | } |
37f0e767 PB |
9835 | if (nr) |
9836 | percpu_ref_put_many(&ctx->refs, nr); | |
9a4fdbd8 JA |
9837 | mutex_unlock(&ctx->uring_lock); |
9838 | } | |
9839 | ||
43597aac | 9840 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
2b188cc1 | 9841 | { |
43597aac | 9842 | if (data && !atomic_dec_and_test(&data->refs)) |
bd54b6fe | 9843 | wait_for_completion(&data->done); |
bd54b6fe | 9844 | } |
04fc6c80 | 9845 | |
4d9237e3 JA |
9846 | static void io_flush_apoll_cache(struct io_ring_ctx *ctx) |
9847 | { | |
9848 | struct async_poll *apoll; | |
9849 | ||
9850 | while (!list_empty(&ctx->apoll_cache)) { | |
9851 | apoll = list_first_entry(&ctx->apoll_cache, struct async_poll, | |
9852 | poll.wait.entry); | |
9853 | list_del(&apoll->poll.wait.entry); | |
9854 | kfree(apoll); | |
9855 | } | |
9856 | } | |
9857 | ||
c072481d | 9858 | static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9859 | { |
37d1e2e3 | 9860 | io_sq_thread_finish(ctx); |
2aede0e4 | 9861 | |
37d1e2e3 | 9862 | if (ctx->mm_account) { |
2aede0e4 JA |
9863 | mmdrop(ctx->mm_account); |
9864 | ctx->mm_account = NULL; | |
30975825 | 9865 | } |
def596e9 | 9866 | |
ab409402 | 9867 | io_rsrc_refs_drop(ctx); |
43597aac PB |
9868 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
9869 | io_wait_rsrc_data(ctx->buf_data); | |
9870 | io_wait_rsrc_data(ctx->file_data); | |
9871 | ||
8bad28d8 | 9872 | mutex_lock(&ctx->uring_lock); |
43597aac | 9873 | if (ctx->buf_data) |
bd54b6fe | 9874 | __io_sqe_buffers_unregister(ctx); |
43597aac | 9875 | if (ctx->file_data) |
08480400 | 9876 | __io_sqe_files_unregister(ctx); |
c4ea060e PB |
9877 | if (ctx->rings) |
9878 | __io_cqring_overflow_flush(ctx, true); | |
9b402849 | 9879 | io_eventfd_unregister(ctx); |
4d9237e3 | 9880 | io_flush_apoll_cache(ctx); |
77bc59b4 | 9881 | mutex_unlock(&ctx->uring_lock); |
5a2e745d | 9882 | io_destroy_buffers(ctx); |
07db298a PB |
9883 | if (ctx->sq_creds) |
9884 | put_cred(ctx->sq_creds); | |
def596e9 | 9885 | |
a7f0ed5a PB |
9886 | /* there are no registered resources left, nobody uses it */ |
9887 | if (ctx->rsrc_node) | |
9888 | io_rsrc_node_destroy(ctx->rsrc_node); | |
8dd03afe | 9889 | if (ctx->rsrc_backup_node) |
b895c9a6 | 9890 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
a7f0ed5a | 9891 | flush_delayed_work(&ctx->rsrc_put_work); |
756ab7c0 | 9892 | flush_delayed_work(&ctx->fallback_work); |
a7f0ed5a PB |
9893 | |
9894 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); | |
9895 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); | |
def596e9 | 9896 | |
2b188cc1 | 9897 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
9898 | if (ctx->ring_sock) { |
9899 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 9900 | sock_release(ctx->ring_sock); |
355e8d26 | 9901 | } |
2b188cc1 | 9902 | #endif |
ef9dd637 | 9903 | WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); |
2b188cc1 | 9904 | |
75b28aff | 9905 | io_mem_free(ctx->rings); |
2b188cc1 | 9906 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
9907 | |
9908 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 9909 | free_uid(ctx->user); |
4010fec4 | 9910 | io_req_caches_free(ctx); |
e941894e JA |
9911 | if (ctx->hash_map) |
9912 | io_wq_put_hash(ctx->hash_map); | |
78076bb6 | 9913 | kfree(ctx->cancel_hash); |
6224843d | 9914 | kfree(ctx->dummy_ubuf); |
dbc7d452 | 9915 | kfree(ctx->io_buffers); |
2b188cc1 JA |
9916 | kfree(ctx); |
9917 | } | |
9918 | ||
9919 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
9920 | { | |
9921 | struct io_ring_ctx *ctx = file->private_data; | |
9922 | __poll_t mask = 0; | |
9923 | ||
d60aa65b | 9924 | poll_wait(file, &ctx->cq_wait, wait); |
4f7067c3 SB |
9925 | /* |
9926 | * synchronizes with barrier from wq_has_sleeper call in | |
9927 | * io_commit_cqring | |
9928 | */ | |
2b188cc1 | 9929 | smp_rmb(); |
90554200 | 9930 | if (!io_sqring_full(ctx)) |
2b188cc1 | 9931 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
9932 | |
9933 | /* | |
9934 | * Don't flush cqring overflow list here, just do a simple check. | |
9935 | * Otherwise there could possible be ABBA deadlock: | |
9936 | * CPU0 CPU1 | |
9937 | * ---- ---- | |
9938 | * lock(&ctx->uring_lock); | |
9939 | * lock(&ep->mtx); | |
9940 | * lock(&ctx->uring_lock); | |
9941 | * lock(&ep->mtx); | |
9942 | * | |
9943 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
9944 | * pushs them to do the flush. | |
9945 | */ | |
5ed7a37d | 9946 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
2b188cc1 JA |
9947 | mask |= EPOLLIN | EPOLLRDNORM; |
9948 | ||
9949 | return mask; | |
9950 | } | |
9951 | ||
0bead8cd | 9952 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 9953 | { |
4379bf8b | 9954 | const struct cred *creds; |
071698e1 | 9955 | |
61cf9370 | 9956 | creds = xa_erase(&ctx->personalities, id); |
4379bf8b JA |
9957 | if (creds) { |
9958 | put_cred(creds); | |
0bead8cd | 9959 | return 0; |
1e6fa521 | 9960 | } |
0bead8cd YD |
9961 | |
9962 | return -EINVAL; | |
9963 | } | |
9964 | ||
d56d938b PB |
9965 | struct io_tctx_exit { |
9966 | struct callback_head task_work; | |
9967 | struct completion completion; | |
baf186c4 | 9968 | struct io_ring_ctx *ctx; |
d56d938b PB |
9969 | }; |
9970 | ||
c072481d | 9971 | static __cold void io_tctx_exit_cb(struct callback_head *cb) |
d56d938b PB |
9972 | { |
9973 | struct io_uring_task *tctx = current->io_uring; | |
9974 | struct io_tctx_exit *work; | |
9975 | ||
9976 | work = container_of(cb, struct io_tctx_exit, task_work); | |
9977 | /* | |
9978 | * When @in_idle, we're in cancellation and it's racy to remove the | |
9979 | * node. It'll be removed by the end of cancellation, just ignore it. | |
9980 | */ | |
9981 | if (!atomic_read(&tctx->in_idle)) | |
eef51daa | 9982 | io_uring_del_tctx_node((unsigned long)work->ctx); |
d56d938b PB |
9983 | complete(&work->completion); |
9984 | } | |
9985 | ||
c072481d | 9986 | static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
28090c13 PB |
9987 | { |
9988 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
9989 | ||
9990 | return req->ctx == data; | |
9991 | } | |
9992 | ||
c072481d | 9993 | static __cold void io_ring_exit_work(struct work_struct *work) |
85faa7b8 | 9994 | { |
d56d938b | 9995 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 9996 | unsigned long timeout = jiffies + HZ * 60 * 5; |
58d3be2c | 9997 | unsigned long interval = HZ / 20; |
d56d938b PB |
9998 | struct io_tctx_exit exit; |
9999 | struct io_tctx_node *node; | |
10000 | int ret; | |
85faa7b8 | 10001 | |
56952e91 JA |
10002 | /* |
10003 | * If we're doing polled IO and end up having requests being | |
10004 | * submitted async (out-of-line), then completions can come in while | |
10005 | * we're waiting for refs to drop. We need to reap these manually, | |
10006 | * as nobody else will be looking for them. | |
10007 | */ | |
b2edc0a7 | 10008 | do { |
3dd0c97a | 10009 | io_uring_try_cancel_requests(ctx, NULL, true); |
28090c13 PB |
10010 | if (ctx->sq_data) { |
10011 | struct io_sq_data *sqd = ctx->sq_data; | |
10012 | struct task_struct *tsk; | |
10013 | ||
10014 | io_sq_thread_park(sqd); | |
10015 | tsk = sqd->thread; | |
10016 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) | |
10017 | io_wq_cancel_cb(tsk->io_uring->io_wq, | |
10018 | io_cancel_ctx_cb, ctx, true); | |
10019 | io_sq_thread_unpark(sqd); | |
10020 | } | |
b5bb3a24 | 10021 | |
37f0e767 PB |
10022 | io_req_caches_free(ctx); |
10023 | ||
58d3be2c PB |
10024 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
10025 | /* there is little hope left, don't run it too often */ | |
10026 | interval = HZ * 60; | |
10027 | } | |
10028 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); | |
d56d938b | 10029 | |
7f00651a PB |
10030 | init_completion(&exit.completion); |
10031 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
10032 | exit.ctx = ctx; | |
89b5066e PB |
10033 | /* |
10034 | * Some may use context even when all refs and requests have been put, | |
10035 | * and they are free to do so while still holding uring_lock or | |
5b0a6acc | 10036 | * completion_lock, see io_req_task_submit(). Apart from other work, |
89b5066e PB |
10037 | * this lock/unlock section also waits them to finish. |
10038 | */ | |
d56d938b PB |
10039 | mutex_lock(&ctx->uring_lock); |
10040 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
10041 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
10042 | ||
d56d938b PB |
10043 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
10044 | ctx_node); | |
7f00651a PB |
10045 | /* don't spin on a single task if cancellation failed */ |
10046 | list_rotate_left(&ctx->tctx_list); | |
d56d938b PB |
10047 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
10048 | if (WARN_ON_ONCE(ret)) | |
10049 | continue; | |
d56d938b PB |
10050 | |
10051 | mutex_unlock(&ctx->uring_lock); | |
10052 | wait_for_completion(&exit.completion); | |
d56d938b PB |
10053 | mutex_lock(&ctx->uring_lock); |
10054 | } | |
10055 | mutex_unlock(&ctx->uring_lock); | |
79ebeaee JA |
10056 | spin_lock(&ctx->completion_lock); |
10057 | spin_unlock(&ctx->completion_lock); | |
d56d938b | 10058 | |
85faa7b8 JA |
10059 | io_ring_ctx_free(ctx); |
10060 | } | |
10061 | ||
80c4cbdb | 10062 | /* Returns true if we found and killed one or more timeouts */ |
c072481d PB |
10063 | static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx, |
10064 | struct task_struct *tsk, bool cancel_all) | |
80c4cbdb PB |
10065 | { |
10066 | struct io_kiocb *req, *tmp; | |
10067 | int canceled = 0; | |
10068 | ||
79ebeaee JA |
10069 | spin_lock(&ctx->completion_lock); |
10070 | spin_lock_irq(&ctx->timeout_lock); | |
80c4cbdb | 10071 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
3dd0c97a | 10072 | if (io_match_task(req, tsk, cancel_all)) { |
80c4cbdb PB |
10073 | io_kill_timeout(req, -ECANCELED); |
10074 | canceled++; | |
10075 | } | |
10076 | } | |
79ebeaee | 10077 | spin_unlock_irq(&ctx->timeout_lock); |
60053be8 | 10078 | io_commit_cqring(ctx); |
79ebeaee | 10079 | spin_unlock(&ctx->completion_lock); |
80c4cbdb PB |
10080 | if (canceled != 0) |
10081 | io_cqring_ev_posted(ctx); | |
10082 | return canceled != 0; | |
10083 | } | |
10084 | ||
c072481d | 10085 | static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
2b188cc1 | 10086 | { |
61cf9370 MWO |
10087 | unsigned long index; |
10088 | struct creds *creds; | |
10089 | ||
2b188cc1 JA |
10090 | mutex_lock(&ctx->uring_lock); |
10091 | percpu_ref_kill(&ctx->refs); | |
634578f8 | 10092 | if (ctx->rings) |
6c2450ae | 10093 | __io_cqring_overflow_flush(ctx, true); |
61cf9370 MWO |
10094 | xa_for_each(&ctx->personalities, index, creds) |
10095 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
10096 | mutex_unlock(&ctx->uring_lock); |
10097 | ||
60053be8 PB |
10098 | /* failed during ring init, it couldn't have issued any requests */ |
10099 | if (ctx->rings) { | |
10100 | io_kill_timeouts(ctx, NULL, true); | |
10101 | io_poll_remove_all(ctx, NULL, true); | |
10102 | /* if we failed setting up the ctx, we might not have any rings */ | |
10103 | io_iopoll_try_reap_events(ctx); | |
10104 | } | |
309fc03a | 10105 | |
85faa7b8 | 10106 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
10107 | /* |
10108 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
10109 | * if we're exiting a ton of rings at the same time. It just adds | |
10110 | * noise and overhead, there's no discernable change in runtime | |
10111 | * over using system_wq. | |
10112 | */ | |
10113 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
10114 | } |
10115 | ||
10116 | static int io_uring_release(struct inode *inode, struct file *file) | |
10117 | { | |
10118 | struct io_ring_ctx *ctx = file->private_data; | |
10119 | ||
10120 | file->private_data = NULL; | |
10121 | io_ring_ctx_wait_and_kill(ctx); | |
10122 | return 0; | |
10123 | } | |
10124 | ||
f6edbabb PB |
10125 | struct io_task_cancel { |
10126 | struct task_struct *task; | |
3dd0c97a | 10127 | bool all; |
f6edbabb | 10128 | }; |
f254ac04 | 10129 | |
f6edbabb | 10130 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 10131 | { |
9a472ef7 | 10132 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 10133 | struct io_task_cancel *cancel = data; |
9a472ef7 | 10134 | |
6af3f48b | 10135 | return io_match_task_safe(req, cancel->task, cancel->all); |
b711d4ea JA |
10136 | } |
10137 | ||
c072481d PB |
10138 | static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
10139 | struct task_struct *task, | |
10140 | bool cancel_all) | |
b7ddce3c | 10141 | { |
e1915f76 | 10142 | struct io_defer_entry *de; |
b7ddce3c PB |
10143 | LIST_HEAD(list); |
10144 | ||
79ebeaee | 10145 | spin_lock(&ctx->completion_lock); |
b7ddce3c | 10146 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
6af3f48b | 10147 | if (io_match_task_safe(de->req, task, cancel_all)) { |
b7ddce3c PB |
10148 | list_cut_position(&list, &ctx->defer_list, &de->list); |
10149 | break; | |
10150 | } | |
10151 | } | |
79ebeaee | 10152 | spin_unlock(&ctx->completion_lock); |
e1915f76 PB |
10153 | if (list_empty(&list)) |
10154 | return false; | |
b7ddce3c PB |
10155 | |
10156 | while (!list_empty(&list)) { | |
10157 | de = list_first_entry(&list, struct io_defer_entry, list); | |
10158 | list_del_init(&de->list); | |
f41db273 | 10159 | io_req_complete_failed(de->req, -ECANCELED); |
b7ddce3c PB |
10160 | kfree(de); |
10161 | } | |
e1915f76 | 10162 | return true; |
b7ddce3c PB |
10163 | } |
10164 | ||
c072481d | 10165 | static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
1b00764f PB |
10166 | { |
10167 | struct io_tctx_node *node; | |
10168 | enum io_wq_cancel cret; | |
10169 | bool ret = false; | |
10170 | ||
10171 | mutex_lock(&ctx->uring_lock); | |
10172 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
10173 | struct io_uring_task *tctx = node->task->io_uring; | |
10174 | ||
10175 | /* | |
10176 | * io_wq will stay alive while we hold uring_lock, because it's | |
10177 | * killed after ctx nodes, which requires to take the lock. | |
10178 | */ | |
10179 | if (!tctx || !tctx->io_wq) | |
10180 | continue; | |
10181 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
10182 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
10183 | } | |
10184 | mutex_unlock(&ctx->uring_lock); | |
10185 | ||
10186 | return ret; | |
10187 | } | |
10188 | ||
c072481d PB |
10189 | static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
10190 | struct task_struct *task, | |
10191 | bool cancel_all) | |
9936c7c2 | 10192 | { |
3dd0c97a | 10193 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
1b00764f | 10194 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 | 10195 | |
60053be8 PB |
10196 | /* failed during ring init, it couldn't have issued any requests */ |
10197 | if (!ctx->rings) | |
10198 | return; | |
10199 | ||
9936c7c2 PB |
10200 | while (1) { |
10201 | enum io_wq_cancel cret; | |
10202 | bool ret = false; | |
10203 | ||
1b00764f PB |
10204 | if (!task) { |
10205 | ret |= io_uring_try_cancel_iowq(ctx); | |
10206 | } else if (tctx && tctx->io_wq) { | |
10207 | /* | |
10208 | * Cancels requests of all rings, not only @ctx, but | |
10209 | * it's fine as the task is in exit/exec. | |
10210 | */ | |
5aa75ed5 | 10211 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
10212 | &cancel, true); |
10213 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
10214 | } | |
10215 | ||
10216 | /* SQPOLL thread does its own polling */ | |
3dd0c97a | 10217 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
d052d1d6 | 10218 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
5eef4e87 | 10219 | while (!wq_list_empty(&ctx->iopoll_list)) { |
9936c7c2 PB |
10220 | io_iopoll_try_reap_events(ctx); |
10221 | ret = true; | |
10222 | } | |
10223 | } | |
10224 | ||
3dd0c97a PB |
10225 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
10226 | ret |= io_poll_remove_all(ctx, task, cancel_all); | |
10227 | ret |= io_kill_timeouts(ctx, task, cancel_all); | |
e5dc480d PB |
10228 | if (task) |
10229 | ret |= io_run_task_work(); | |
9936c7c2 PB |
10230 | if (!ret) |
10231 | break; | |
10232 | cond_resched(); | |
10233 | } | |
10234 | } | |
10235 | ||
eef51daa | 10236 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
0f212204 | 10237 | { |
236434c3 | 10238 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 10239 | struct io_tctx_node *node; |
a528b04e | 10240 | int ret; |
236434c3 MWO |
10241 | |
10242 | if (unlikely(!tctx)) { | |
5aa75ed5 | 10243 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
10244 | if (unlikely(ret)) |
10245 | return ret; | |
e139a1ec | 10246 | |
236434c3 | 10247 | tctx = current->io_uring; |
e139a1ec PB |
10248 | if (ctx->iowq_limits_set) { |
10249 | unsigned int limits[2] = { ctx->iowq_limits[0], | |
10250 | ctx->iowq_limits[1], }; | |
10251 | ||
10252 | ret = io_wq_max_workers(tctx->io_wq, limits); | |
10253 | if (ret) | |
10254 | return ret; | |
10255 | } | |
0f212204 | 10256 | } |
cf27f3b1 PB |
10257 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
10258 | node = kmalloc(sizeof(*node), GFP_KERNEL); | |
10259 | if (!node) | |
10260 | return -ENOMEM; | |
10261 | node->ctx = ctx; | |
10262 | node->task = current; | |
13bf43f5 | 10263 | |
cf27f3b1 PB |
10264 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
10265 | node, GFP_KERNEL)); | |
10266 | if (ret) { | |
10267 | kfree(node); | |
10268 | return ret; | |
0f212204 | 10269 | } |
cf27f3b1 PB |
10270 | |
10271 | mutex_lock(&ctx->uring_lock); | |
10272 | list_add(&node->ctx_node, &ctx->tctx_list); | |
10273 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 10274 | } |
cf27f3b1 | 10275 | tctx->last = ctx; |
0f212204 JA |
10276 | return 0; |
10277 | } | |
10278 | ||
cf27f3b1 PB |
10279 | /* |
10280 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
10281 | */ | |
eef51daa | 10282 | static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
cf27f3b1 PB |
10283 | { |
10284 | struct io_uring_task *tctx = current->io_uring; | |
10285 | ||
10286 | if (likely(tctx && tctx->last == ctx)) | |
10287 | return 0; | |
eef51daa | 10288 | return __io_uring_add_tctx_node(ctx); |
cf27f3b1 PB |
10289 | } |
10290 | ||
0f212204 JA |
10291 | /* |
10292 | * Remove this io_uring_file -> task mapping. | |
10293 | */ | |
c072481d | 10294 | static __cold void io_uring_del_tctx_node(unsigned long index) |
0f212204 JA |
10295 | { |
10296 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 10297 | struct io_tctx_node *node; |
2941267b | 10298 | |
eebd2e37 PB |
10299 | if (!tctx) |
10300 | return; | |
13bf43f5 PB |
10301 | node = xa_erase(&tctx->xa, index); |
10302 | if (!node) | |
2941267b | 10303 | return; |
0f212204 | 10304 | |
13bf43f5 PB |
10305 | WARN_ON_ONCE(current != node->task); |
10306 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
10307 | ||
10308 | mutex_lock(&node->ctx->uring_lock); | |
10309 | list_del(&node->ctx_node); | |
10310 | mutex_unlock(&node->ctx->uring_lock); | |
10311 | ||
baf186c4 | 10312 | if (tctx->last == node->ctx) |
0f212204 | 10313 | tctx->last = NULL; |
13bf43f5 | 10314 | kfree(node); |
0f212204 JA |
10315 | } |
10316 | ||
c072481d | 10317 | static __cold void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 10318 | { |
ba5ef6dc | 10319 | struct io_wq *wq = tctx->io_wq; |
13bf43f5 | 10320 | struct io_tctx_node *node; |
de7f1d9e PB |
10321 | unsigned long index; |
10322 | ||
8bab4c09 | 10323 | xa_for_each(&tctx->xa, index, node) { |
eef51daa | 10324 | io_uring_del_tctx_node(index); |
8bab4c09 JA |
10325 | cond_resched(); |
10326 | } | |
b16ef427 ME |
10327 | if (wq) { |
10328 | /* | |
f6f9b278 | 10329 | * Must be after io_uring_del_tctx_node() (removes nodes under |
b16ef427 ME |
10330 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). |
10331 | */ | |
ba5ef6dc | 10332 | io_wq_put_and_exit(wq); |
dadebc35 | 10333 | tctx->io_wq = NULL; |
b16ef427 | 10334 | } |
de7f1d9e PB |
10335 | } |
10336 | ||
3f48cf18 | 10337 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
521d6a73 | 10338 | { |
3f48cf18 | 10339 | if (tracked) |
d5361233 | 10340 | return 0; |
521d6a73 PB |
10341 | return percpu_counter_sum(&tctx->inflight); |
10342 | } | |
10343 | ||
78cc687b PB |
10344 | /* |
10345 | * Find any io_uring ctx that this task has registered or done IO on, and cancel | |
78a78060 | 10346 | * requests. @sqd should be not-null IFF it's an SQPOLL thread cancellation. |
78cc687b | 10347 | */ |
c072481d PB |
10348 | static __cold void io_uring_cancel_generic(bool cancel_all, |
10349 | struct io_sq_data *sqd) | |
0e9ddb39 | 10350 | { |
521d6a73 | 10351 | struct io_uring_task *tctx = current->io_uring; |
734551df | 10352 | struct io_ring_ctx *ctx; |
0e9ddb39 PB |
10353 | s64 inflight; |
10354 | DEFINE_WAIT(wait); | |
fdaf083c | 10355 | |
78cc687b PB |
10356 | WARN_ON_ONCE(sqd && sqd->thread != current); |
10357 | ||
6d042ffb PO |
10358 | if (!current->io_uring) |
10359 | return; | |
17a91051 PB |
10360 | if (tctx->io_wq) |
10361 | io_wq_exit_start(tctx->io_wq); | |
10362 | ||
0e9ddb39 PB |
10363 | atomic_inc(&tctx->in_idle); |
10364 | do { | |
e9dbe221 | 10365 | io_uring_drop_tctx_refs(current); |
0e9ddb39 | 10366 | /* read completions before cancelations */ |
78cc687b | 10367 | inflight = tctx_inflight(tctx, !cancel_all); |
0e9ddb39 PB |
10368 | if (!inflight) |
10369 | break; | |
fdaf083c | 10370 | |
78cc687b PB |
10371 | if (!sqd) { |
10372 | struct io_tctx_node *node; | |
10373 | unsigned long index; | |
0f212204 | 10374 | |
78cc687b PB |
10375 | xa_for_each(&tctx->xa, index, node) { |
10376 | /* sqpoll task will cancel all its requests */ | |
10377 | if (node->ctx->sq_data) | |
10378 | continue; | |
10379 | io_uring_try_cancel_requests(node->ctx, current, | |
10380 | cancel_all); | |
10381 | } | |
10382 | } else { | |
10383 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
10384 | io_uring_try_cancel_requests(ctx, current, | |
10385 | cancel_all); | |
10386 | } | |
17a91051 | 10387 | |
78a78060 JA |
10388 | prepare_to_wait(&tctx->wait, &wait, TASK_INTERRUPTIBLE); |
10389 | io_run_task_work(); | |
e9dbe221 | 10390 | io_uring_drop_tctx_refs(current); |
78a78060 | 10391 | |
0f212204 | 10392 | /* |
a1bb3cd5 PB |
10393 | * If we've seen completions, retry without waiting. This |
10394 | * avoids a race where a completion comes in before we did | |
10395 | * prepare_to_wait(). | |
0f212204 | 10396 | */ |
3dd0c97a | 10397 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
a1bb3cd5 | 10398 | schedule(); |
f57555ed | 10399 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 10400 | } while (1); |
de7f1d9e | 10401 | |
8452d4a6 | 10402 | io_uring_clean_tctx(tctx); |
3dd0c97a | 10403 | if (cancel_all) { |
3cc7fdb9 PB |
10404 | /* |
10405 | * We shouldn't run task_works after cancel, so just leave | |
10406 | * ->in_idle set for normal exit. | |
10407 | */ | |
10408 | atomic_dec(&tctx->in_idle); | |
3f48cf18 PB |
10409 | /* for exec all current's requests should be gone, kill tctx */ |
10410 | __io_uring_free(current); | |
10411 | } | |
44e728b8 PB |
10412 | } |
10413 | ||
f552a27a | 10414 | void __io_uring_cancel(bool cancel_all) |
78cc687b | 10415 | { |
f552a27a | 10416 | io_uring_cancel_generic(cancel_all, NULL); |
78cc687b PB |
10417 | } |
10418 | ||
e7a6c00d JA |
10419 | void io_uring_unreg_ringfd(void) |
10420 | { | |
10421 | struct io_uring_task *tctx = current->io_uring; | |
10422 | int i; | |
10423 | ||
10424 | for (i = 0; i < IO_RINGFD_REG_MAX; i++) { | |
10425 | if (tctx->registered_rings[i]) { | |
10426 | fput(tctx->registered_rings[i]); | |
10427 | tctx->registered_rings[i] = NULL; | |
10428 | } | |
10429 | } | |
10430 | } | |
10431 | ||
10432 | static int io_ring_add_registered_fd(struct io_uring_task *tctx, int fd, | |
10433 | int start, int end) | |
10434 | { | |
10435 | struct file *file; | |
10436 | int offset; | |
10437 | ||
10438 | for (offset = start; offset < end; offset++) { | |
10439 | offset = array_index_nospec(offset, IO_RINGFD_REG_MAX); | |
10440 | if (tctx->registered_rings[offset]) | |
10441 | continue; | |
10442 | ||
10443 | file = fget(fd); | |
10444 | if (!file) { | |
10445 | return -EBADF; | |
10446 | } else if (file->f_op != &io_uring_fops) { | |
10447 | fput(file); | |
10448 | return -EOPNOTSUPP; | |
10449 | } | |
10450 | tctx->registered_rings[offset] = file; | |
10451 | return offset; | |
10452 | } | |
10453 | ||
10454 | return -EBUSY; | |
10455 | } | |
10456 | ||
10457 | /* | |
10458 | * Register a ring fd to avoid fdget/fdput for each io_uring_enter() | |
10459 | * invocation. User passes in an array of struct io_uring_rsrc_update | |
10460 | * with ->data set to the ring_fd, and ->offset given for the desired | |
10461 | * index. If no index is desired, application may set ->offset == -1U | |
10462 | * and we'll find an available index. Returns number of entries | |
10463 | * successfully processed, or < 0 on error if none were processed. | |
10464 | */ | |
10465 | static int io_ringfd_register(struct io_ring_ctx *ctx, void __user *__arg, | |
10466 | unsigned nr_args) | |
10467 | { | |
10468 | struct io_uring_rsrc_update __user *arg = __arg; | |
10469 | struct io_uring_rsrc_update reg; | |
10470 | struct io_uring_task *tctx; | |
10471 | int ret, i; | |
10472 | ||
10473 | if (!nr_args || nr_args > IO_RINGFD_REG_MAX) | |
10474 | return -EINVAL; | |
10475 | ||
10476 | mutex_unlock(&ctx->uring_lock); | |
10477 | ret = io_uring_add_tctx_node(ctx); | |
10478 | mutex_lock(&ctx->uring_lock); | |
10479 | if (ret) | |
10480 | return ret; | |
10481 | ||
10482 | tctx = current->io_uring; | |
10483 | for (i = 0; i < nr_args; i++) { | |
10484 | int start, end; | |
10485 | ||
10486 | if (copy_from_user(®, &arg[i], sizeof(reg))) { | |
10487 | ret = -EFAULT; | |
10488 | break; | |
10489 | } | |
10490 | ||
6fb53cf8 DY |
10491 | if (reg.resv) { |
10492 | ret = -EINVAL; | |
10493 | break; | |
10494 | } | |
10495 | ||
e7a6c00d JA |
10496 | if (reg.offset == -1U) { |
10497 | start = 0; | |
10498 | end = IO_RINGFD_REG_MAX; | |
10499 | } else { | |
10500 | if (reg.offset >= IO_RINGFD_REG_MAX) { | |
10501 | ret = -EINVAL; | |
10502 | break; | |
10503 | } | |
10504 | start = reg.offset; | |
10505 | end = start + 1; | |
10506 | } | |
10507 | ||
10508 | ret = io_ring_add_registered_fd(tctx, reg.data, start, end); | |
10509 | if (ret < 0) | |
10510 | break; | |
10511 | ||
10512 | reg.offset = ret; | |
10513 | if (copy_to_user(&arg[i], ®, sizeof(reg))) { | |
10514 | fput(tctx->registered_rings[reg.offset]); | |
10515 | tctx->registered_rings[reg.offset] = NULL; | |
10516 | ret = -EFAULT; | |
10517 | break; | |
10518 | } | |
10519 | } | |
10520 | ||
10521 | return i ? i : ret; | |
10522 | } | |
10523 | ||
10524 | static int io_ringfd_unregister(struct io_ring_ctx *ctx, void __user *__arg, | |
10525 | unsigned nr_args) | |
10526 | { | |
10527 | struct io_uring_rsrc_update __user *arg = __arg; | |
10528 | struct io_uring_task *tctx = current->io_uring; | |
10529 | struct io_uring_rsrc_update reg; | |
10530 | int ret = 0, i; | |
10531 | ||
10532 | if (!nr_args || nr_args > IO_RINGFD_REG_MAX) | |
10533 | return -EINVAL; | |
10534 | if (!tctx) | |
10535 | return 0; | |
10536 | ||
10537 | for (i = 0; i < nr_args; i++) { | |
10538 | if (copy_from_user(®, &arg[i], sizeof(reg))) { | |
10539 | ret = -EFAULT; | |
10540 | break; | |
10541 | } | |
6fb53cf8 | 10542 | if (reg.resv || reg.offset >= IO_RINGFD_REG_MAX) { |
e7a6c00d JA |
10543 | ret = -EINVAL; |
10544 | break; | |
10545 | } | |
10546 | ||
10547 | reg.offset = array_index_nospec(reg.offset, IO_RINGFD_REG_MAX); | |
10548 | if (tctx->registered_rings[reg.offset]) { | |
10549 | fput(tctx->registered_rings[reg.offset]); | |
10550 | tctx->registered_rings[reg.offset] = NULL; | |
10551 | } | |
10552 | } | |
10553 | ||
10554 | return i ? i : ret; | |
10555 | } | |
10556 | ||
6c5c240e RP |
10557 | static void *io_uring_validate_mmap_request(struct file *file, |
10558 | loff_t pgoff, size_t sz) | |
2b188cc1 | 10559 | { |
2b188cc1 | 10560 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 10561 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
10562 | struct page *page; |
10563 | void *ptr; | |
10564 | ||
10565 | switch (offset) { | |
10566 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
10567 | case IORING_OFF_CQ_RING: |
10568 | ptr = ctx->rings; | |
2b188cc1 JA |
10569 | break; |
10570 | case IORING_OFF_SQES: | |
10571 | ptr = ctx->sq_sqes; | |
10572 | break; | |
2b188cc1 | 10573 | default: |
6c5c240e | 10574 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
10575 | } |
10576 | ||
10577 | page = virt_to_head_page(ptr); | |
a50b854e | 10578 | if (sz > page_size(page)) |
6c5c240e RP |
10579 | return ERR_PTR(-EINVAL); |
10580 | ||
10581 | return ptr; | |
10582 | } | |
10583 | ||
10584 | #ifdef CONFIG_MMU | |
10585 | ||
c072481d | 10586 | static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
6c5c240e RP |
10587 | { |
10588 | size_t sz = vma->vm_end - vma->vm_start; | |
10589 | unsigned long pfn; | |
10590 | void *ptr; | |
10591 | ||
10592 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
10593 | if (IS_ERR(ptr)) | |
10594 | return PTR_ERR(ptr); | |
2b188cc1 JA |
10595 | |
10596 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
10597 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
10598 | } | |
10599 | ||
6c5c240e RP |
10600 | #else /* !CONFIG_MMU */ |
10601 | ||
10602 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
10603 | { | |
10604 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
10605 | } | |
10606 | ||
10607 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
10608 | { | |
10609 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
10610 | } | |
10611 | ||
10612 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
10613 | unsigned long addr, unsigned long len, | |
10614 | unsigned long pgoff, unsigned long flags) | |
10615 | { | |
10616 | void *ptr; | |
10617 | ||
10618 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
10619 | if (IS_ERR(ptr)) | |
10620 | return PTR_ERR(ptr); | |
10621 | ||
10622 | return (unsigned long) ptr; | |
10623 | } | |
10624 | ||
10625 | #endif /* !CONFIG_MMU */ | |
10626 | ||
d9d05217 | 10627 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 JA |
10628 | { |
10629 | DEFINE_WAIT(wait); | |
10630 | ||
10631 | do { | |
10632 | if (!io_sqring_full(ctx)) | |
10633 | break; | |
90554200 JA |
10634 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
10635 | ||
10636 | if (!io_sqring_full(ctx)) | |
10637 | break; | |
90554200 JA |
10638 | schedule(); |
10639 | } while (!signal_pending(current)); | |
10640 | ||
10641 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
5199328a | 10642 | return 0; |
90554200 JA |
10643 | } |
10644 | ||
f81440d3 PB |
10645 | static int io_validate_ext_arg(unsigned flags, const void __user *argp, size_t argsz) |
10646 | { | |
10647 | if (flags & IORING_ENTER_EXT_ARG) { | |
10648 | struct io_uring_getevents_arg arg; | |
10649 | ||
10650 | if (argsz != sizeof(arg)) | |
10651 | return -EINVAL; | |
10652 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
10653 | return -EFAULT; | |
10654 | } | |
10655 | return 0; | |
10656 | } | |
10657 | ||
c73ebb68 HX |
10658 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
10659 | struct __kernel_timespec __user **ts, | |
10660 | const sigset_t __user **sig) | |
10661 | { | |
10662 | struct io_uring_getevents_arg arg; | |
10663 | ||
10664 | /* | |
10665 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
10666 | * is just a pointer to the sigset_t. | |
10667 | */ | |
10668 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
10669 | *sig = (const sigset_t __user *) argp; | |
10670 | *ts = NULL; | |
10671 | return 0; | |
10672 | } | |
10673 | ||
10674 | /* | |
10675 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
10676 | * timespec and sigset_t pointers if good. | |
10677 | */ | |
10678 | if (*argsz != sizeof(arg)) | |
10679 | return -EINVAL; | |
10680 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
10681 | return -EFAULT; | |
d2347b96 DY |
10682 | if (arg.pad) |
10683 | return -EINVAL; | |
c73ebb68 HX |
10684 | *sig = u64_to_user_ptr(arg.sigmask); |
10685 | *argsz = arg.sigmask_sz; | |
10686 | *ts = u64_to_user_ptr(arg.ts); | |
10687 | return 0; | |
10688 | } | |
10689 | ||
2b188cc1 | 10690 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
10691 | u32, min_complete, u32, flags, const void __user *, argp, |
10692 | size_t, argsz) | |
2b188cc1 JA |
10693 | { |
10694 | struct io_ring_ctx *ctx; | |
2b188cc1 JA |
10695 | int submitted = 0; |
10696 | struct fd f; | |
33f993da | 10697 | long ret; |
2b188cc1 | 10698 | |
4c6e277c | 10699 | io_run_task_work(); |
b41e9852 | 10700 | |
33f993da | 10701 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
e7a6c00d JA |
10702 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG | |
10703 | IORING_ENTER_REGISTERED_RING))) | |
2b188cc1 JA |
10704 | return -EINVAL; |
10705 | ||
e7a6c00d JA |
10706 | /* |
10707 | * Ring fd has been registered via IORING_REGISTER_RING_FDS, we | |
10708 | * need only dereference our task private array to find it. | |
10709 | */ | |
10710 | if (flags & IORING_ENTER_REGISTERED_RING) { | |
10711 | struct io_uring_task *tctx = current->io_uring; | |
10712 | ||
10713 | if (!tctx || fd >= IO_RINGFD_REG_MAX) | |
10714 | return -EINVAL; | |
10715 | fd = array_index_nospec(fd, IO_RINGFD_REG_MAX); | |
10716 | f.file = tctx->registered_rings[fd]; | |
10717 | if (unlikely(!f.file)) | |
10718 | return -EBADF; | |
10719 | } else { | |
10720 | f = fdget(fd); | |
10721 | if (unlikely(!f.file)) | |
10722 | return -EBADF; | |
10723 | } | |
2b188cc1 JA |
10724 | |
10725 | ret = -EOPNOTSUPP; | |
33f993da | 10726 | if (unlikely(f.file->f_op != &io_uring_fops)) |
2b188cc1 JA |
10727 | goto out_fput; |
10728 | ||
10729 | ret = -ENXIO; | |
10730 | ctx = f.file->private_data; | |
33f993da | 10731 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
2b188cc1 JA |
10732 | goto out_fput; |
10733 | ||
7e84e1c7 | 10734 | ret = -EBADFD; |
33f993da | 10735 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
10736 | goto out; |
10737 | ||
6c271ce2 JA |
10738 | /* |
10739 | * For SQ polling, the thread will do all submissions and completions. | |
10740 | * Just return the requested submit count, and wake the thread if | |
10741 | * we were asked to. | |
10742 | */ | |
b2a9eada | 10743 | ret = 0; |
6c271ce2 | 10744 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
90f67366 | 10745 | io_cqring_overflow_flush(ctx); |
89448c47 | 10746 | |
21f96522 JA |
10747 | if (unlikely(ctx->sq_data->thread == NULL)) { |
10748 | ret = -EOWNERDEAD; | |
04147488 | 10749 | goto out; |
21f96522 | 10750 | } |
6c271ce2 | 10751 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 10752 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
10753 | if (flags & IORING_ENTER_SQ_WAIT) { |
10754 | ret = io_sqpoll_wait_sq(ctx); | |
10755 | if (ret) | |
10756 | goto out; | |
10757 | } | |
6c271ce2 | 10758 | submitted = to_submit; |
b2a9eada | 10759 | } else if (to_submit) { |
eef51daa | 10760 | ret = io_uring_add_tctx_node(ctx); |
0f212204 JA |
10761 | if (unlikely(ret)) |
10762 | goto out; | |
d487b43c | 10763 | |
2b188cc1 | 10764 | mutex_lock(&ctx->uring_lock); |
0f212204 | 10765 | submitted = io_submit_sqes(ctx, to_submit); |
d487b43c PB |
10766 | if (submitted != to_submit) { |
10767 | mutex_unlock(&ctx->uring_lock); | |
7c504e65 | 10768 | goto out; |
d487b43c PB |
10769 | } |
10770 | if ((flags & IORING_ENTER_GETEVENTS) && ctx->syscall_iopoll) | |
10771 | goto iopoll_locked; | |
10772 | mutex_unlock(&ctx->uring_lock); | |
2b188cc1 JA |
10773 | } |
10774 | if (flags & IORING_ENTER_GETEVENTS) { | |
773697b6 | 10775 | if (ctx->syscall_iopoll) { |
d487b43c PB |
10776 | /* |
10777 | * We disallow the app entering submit/complete with | |
10778 | * polling, but we still need to lock the ring to | |
10779 | * prevent racing with polled issue that got punted to | |
10780 | * a workqueue. | |
10781 | */ | |
10782 | mutex_lock(&ctx->uring_lock); | |
10783 | iopoll_locked: | |
f81440d3 | 10784 | ret = io_validate_ext_arg(flags, argp, argsz); |
d487b43c PB |
10785 | if (likely(!ret)) { |
10786 | min_complete = min(min_complete, ctx->cq_entries); | |
10787 | ret = io_iopoll_check(ctx, min_complete); | |
10788 | } | |
10789 | mutex_unlock(&ctx->uring_lock); | |
def596e9 | 10790 | } else { |
f81440d3 PB |
10791 | const sigset_t __user *sig; |
10792 | struct __kernel_timespec __user *ts; | |
10793 | ||
10794 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
10795 | if (unlikely(ret)) | |
10796 | goto out; | |
d487b43c | 10797 | min_complete = min(min_complete, ctx->cq_entries); |
c73ebb68 | 10798 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 10799 | } |
2b188cc1 JA |
10800 | } |
10801 | ||
7c504e65 | 10802 | out: |
6805b32e | 10803 | percpu_ref_put(&ctx->refs); |
2b188cc1 | 10804 | out_fput: |
e7a6c00d JA |
10805 | if (!(flags & IORING_ENTER_REGISTERED_RING)) |
10806 | fdput(f); | |
2b188cc1 JA |
10807 | return submitted ? submitted : ret; |
10808 | } | |
10809 | ||
bebdb65e | 10810 | #ifdef CONFIG_PROC_FS |
c072481d | 10811 | static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id, |
61cf9370 | 10812 | const struct cred *cred) |
87ce955b | 10813 | { |
87ce955b JA |
10814 | struct user_namespace *uns = seq_user_ns(m); |
10815 | struct group_info *gi; | |
10816 | kernel_cap_t cap; | |
10817 | unsigned __capi; | |
10818 | int g; | |
10819 | ||
10820 | seq_printf(m, "%5d\n", id); | |
10821 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
10822 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
10823 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
10824 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
10825 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
10826 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
10827 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
10828 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
10829 | seq_puts(m, "\n\tGroups:\t"); | |
10830 | gi = cred->group_info; | |
10831 | for (g = 0; g < gi->ngroups; g++) { | |
10832 | seq_put_decimal_ull(m, g ? " " : "", | |
10833 | from_kgid_munged(uns, gi->gid[g])); | |
10834 | } | |
10835 | seq_puts(m, "\n\tCapEff:\t"); | |
10836 | cap = cred->cap_effective; | |
10837 | CAP_FOR_EACH_U32(__capi) | |
10838 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
10839 | seq_putc(m, '\n'); | |
10840 | return 0; | |
10841 | } | |
10842 | ||
c072481d PB |
10843 | static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, |
10844 | struct seq_file *m) | |
87ce955b | 10845 | { |
dbbe9c64 | 10846 | struct io_sq_data *sq = NULL; |
83f84356 HX |
10847 | struct io_overflow_cqe *ocqe; |
10848 | struct io_rings *r = ctx->rings; | |
10849 | unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1; | |
83f84356 HX |
10850 | unsigned int sq_head = READ_ONCE(r->sq.head); |
10851 | unsigned int sq_tail = READ_ONCE(r->sq.tail); | |
10852 | unsigned int cq_head = READ_ONCE(r->cq.head); | |
10853 | unsigned int cq_tail = READ_ONCE(r->cq.tail); | |
f75d1183 | 10854 | unsigned int sq_entries, cq_entries; |
fad8e0de | 10855 | bool has_lock; |
83f84356 HX |
10856 | unsigned int i; |
10857 | ||
10858 | /* | |
10859 | * we may get imprecise sqe and cqe info if uring is actively running | |
10860 | * since we get cached_sq_head and cached_cq_tail without uring_lock | |
10861 | * and sq_tail and cq_head are changed by userspace. But it's ok since | |
10862 | * we usually use these info when it is stuck. | |
10863 | */ | |
c0235652 | 10864 | seq_printf(m, "SqMask:\t0x%x\n", sq_mask); |
f75d1183 JA |
10865 | seq_printf(m, "SqHead:\t%u\n", sq_head); |
10866 | seq_printf(m, "SqTail:\t%u\n", sq_tail); | |
10867 | seq_printf(m, "CachedSqHead:\t%u\n", ctx->cached_sq_head); | |
10868 | seq_printf(m, "CqMask:\t0x%x\n", cq_mask); | |
10869 | seq_printf(m, "CqHead:\t%u\n", cq_head); | |
10870 | seq_printf(m, "CqTail:\t%u\n", cq_tail); | |
10871 | seq_printf(m, "CachedCqTail:\t%u\n", ctx->cached_cq_tail); | |
10872 | seq_printf(m, "SQEs:\t%u\n", sq_tail - ctx->cached_sq_head); | |
10873 | sq_entries = min(sq_tail - sq_head, ctx->sq_entries); | |
10874 | for (i = 0; i < sq_entries; i++) { | |
10875 | unsigned int entry = i + sq_head; | |
10876 | unsigned int sq_idx = READ_ONCE(ctx->sq_array[entry & sq_mask]); | |
a1957780 | 10877 | struct io_uring_sqe *sqe; |
f75d1183 JA |
10878 | |
10879 | if (sq_idx > sq_mask) | |
10880 | continue; | |
10881 | sqe = &ctx->sq_sqes[sq_idx]; | |
10882 | seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n", | |
10883 | sq_idx, sqe->opcode, sqe->fd, sqe->flags, | |
10884 | sqe->user_data); | |
83f84356 | 10885 | } |
f75d1183 JA |
10886 | seq_printf(m, "CQEs:\t%u\n", cq_tail - cq_head); |
10887 | cq_entries = min(cq_tail - cq_head, ctx->cq_entries); | |
10888 | for (i = 0; i < cq_entries; i++) { | |
10889 | unsigned int entry = i + cq_head; | |
10890 | struct io_uring_cqe *cqe = &r->cqes[entry & cq_mask]; | |
83f84356 HX |
10891 | |
10892 | seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n", | |
f75d1183 JA |
10893 | entry & cq_mask, cqe->user_data, cqe->res, |
10894 | cqe->flags); | |
83f84356 | 10895 | } |
87ce955b | 10896 | |
fad8e0de JA |
10897 | /* |
10898 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
10899 | * since fdinfo case grabs it in the opposite direction of normal use | |
10900 | * cases. If we fail to get the lock, we just don't iterate any | |
10901 | * structures that could be going away outside the io_uring mutex. | |
10902 | */ | |
10903 | has_lock = mutex_trylock(&ctx->uring_lock); | |
10904 | ||
5f3f26f9 | 10905 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 10906 | sq = ctx->sq_data; |
5f3f26f9 JA |
10907 | if (!sq->thread) |
10908 | sq = NULL; | |
10909 | } | |
dbbe9c64 JQ |
10910 | |
10911 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
10912 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 10913 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 10914 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
7b29f92d | 10915 | struct file *f = io_file_from_index(ctx, i); |
87ce955b | 10916 | |
87ce955b JA |
10917 | if (f) |
10918 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
10919 | else | |
10920 | seq_printf(m, "%5u: <none>\n", i); | |
10921 | } | |
10922 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 10923 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
41edf1a5 | 10924 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
4751f53d | 10925 | unsigned int len = buf->ubuf_end - buf->ubuf; |
87ce955b | 10926 | |
4751f53d | 10927 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
87ce955b | 10928 | } |
61cf9370 MWO |
10929 | if (has_lock && !xa_empty(&ctx->personalities)) { |
10930 | unsigned long index; | |
10931 | const struct cred *cred; | |
10932 | ||
87ce955b | 10933 | seq_printf(m, "Personalities:\n"); |
61cf9370 MWO |
10934 | xa_for_each(&ctx->personalities, index, cred) |
10935 | io_uring_show_cred(m, index, cred); | |
87ce955b | 10936 | } |
83f84356 HX |
10937 | if (has_lock) |
10938 | mutex_unlock(&ctx->uring_lock); | |
10939 | ||
10940 | seq_puts(m, "PollList:\n"); | |
79ebeaee | 10941 | spin_lock(&ctx->completion_lock); |
d7718a9d JA |
10942 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
10943 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
10944 | struct io_kiocb *req; | |
10945 | ||
10946 | hlist_for_each_entry(req, list, hash_node) | |
10947 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
7f62d40d | 10948 | task_work_pending(req->task)); |
d7718a9d | 10949 | } |
83f84356 HX |
10950 | |
10951 | seq_puts(m, "CqOverflowList:\n"); | |
10952 | list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) { | |
10953 | struct io_uring_cqe *cqe = &ocqe->cqe; | |
10954 | ||
10955 | seq_printf(m, " user_data=%llu, res=%d, flags=%x\n", | |
10956 | cqe->user_data, cqe->res, cqe->flags); | |
10957 | ||
10958 | } | |
10959 | ||
79ebeaee | 10960 | spin_unlock(&ctx->completion_lock); |
87ce955b JA |
10961 | } |
10962 | ||
c072481d | 10963 | static __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
87ce955b JA |
10964 | { |
10965 | struct io_ring_ctx *ctx = f->private_data; | |
10966 | ||
10967 | if (percpu_ref_tryget(&ctx->refs)) { | |
10968 | __io_uring_show_fdinfo(ctx, m); | |
10969 | percpu_ref_put(&ctx->refs); | |
10970 | } | |
10971 | } | |
bebdb65e | 10972 | #endif |
87ce955b | 10973 | |
2b188cc1 JA |
10974 | static const struct file_operations io_uring_fops = { |
10975 | .release = io_uring_release, | |
10976 | .mmap = io_uring_mmap, | |
6c5c240e RP |
10977 | #ifndef CONFIG_MMU |
10978 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
10979 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
10980 | #endif | |
2b188cc1 | 10981 | .poll = io_uring_poll, |
bebdb65e | 10982 | #ifdef CONFIG_PROC_FS |
87ce955b | 10983 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 10984 | #endif |
2b188cc1 JA |
10985 | }; |
10986 | ||
c072481d PB |
10987 | static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
10988 | struct io_uring_params *p) | |
2b188cc1 | 10989 | { |
75b28aff HV |
10990 | struct io_rings *rings; |
10991 | size_t size, sq_array_offset; | |
2b188cc1 | 10992 | |
bd740481 JA |
10993 | /* make sure these are sane, as we already accounted them */ |
10994 | ctx->sq_entries = p->sq_entries; | |
10995 | ctx->cq_entries = p->cq_entries; | |
10996 | ||
75b28aff HV |
10997 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
10998 | if (size == SIZE_MAX) | |
10999 | return -EOVERFLOW; | |
11000 | ||
11001 | rings = io_mem_alloc(size); | |
11002 | if (!rings) | |
2b188cc1 JA |
11003 | return -ENOMEM; |
11004 | ||
75b28aff HV |
11005 | ctx->rings = rings; |
11006 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
11007 | rings->sq_ring_mask = p->sq_entries - 1; | |
11008 | rings->cq_ring_mask = p->cq_entries - 1; | |
11009 | rings->sq_ring_entries = p->sq_entries; | |
11010 | rings->cq_ring_entries = p->cq_entries; | |
2b188cc1 JA |
11011 | |
11012 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
11013 | if (size == SIZE_MAX) { |
11014 | io_mem_free(ctx->rings); | |
11015 | ctx->rings = NULL; | |
2b188cc1 | 11016 | return -EOVERFLOW; |
eb065d30 | 11017 | } |
2b188cc1 JA |
11018 | |
11019 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
11020 | if (!ctx->sq_sqes) { |
11021 | io_mem_free(ctx->rings); | |
11022 | ctx->rings = NULL; | |
2b188cc1 | 11023 | return -ENOMEM; |
eb065d30 | 11024 | } |
2b188cc1 | 11025 | |
2b188cc1 JA |
11026 | return 0; |
11027 | } | |
11028 | ||
9faadcc8 PB |
11029 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
11030 | { | |
11031 | int ret, fd; | |
11032 | ||
11033 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
11034 | if (fd < 0) | |
11035 | return fd; | |
11036 | ||
eef51daa | 11037 | ret = io_uring_add_tctx_node(ctx); |
9faadcc8 PB |
11038 | if (ret) { |
11039 | put_unused_fd(fd); | |
11040 | return ret; | |
11041 | } | |
11042 | fd_install(fd, file); | |
11043 | return fd; | |
11044 | } | |
11045 | ||
2b188cc1 JA |
11046 | /* |
11047 | * Allocate an anonymous fd, this is what constitutes the application | |
11048 | * visible backing of an io_uring instance. The application mmaps this | |
11049 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
11050 | * we have to tie this fd to a socket for file garbage collection purposes. | |
11051 | */ | |
9faadcc8 | 11052 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
11053 | { |
11054 | struct file *file; | |
9faadcc8 | 11055 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
11056 | int ret; |
11057 | ||
2b188cc1 JA |
11058 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
11059 | &ctx->ring_sock); | |
11060 | if (ret) | |
9faadcc8 | 11061 | return ERR_PTR(ret); |
2b188cc1 JA |
11062 | #endif |
11063 | ||
91a9ab7c PM |
11064 | file = anon_inode_getfile_secure("[io_uring]", &io_uring_fops, ctx, |
11065 | O_RDWR | O_CLOEXEC, NULL); | |
2b188cc1 | 11066 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
11067 | if (IS_ERR(file)) { |
11068 | sock_release(ctx->ring_sock); | |
11069 | ctx->ring_sock = NULL; | |
11070 | } else { | |
11071 | ctx->ring_sock->file = file; | |
0f212204 | 11072 | } |
2b188cc1 | 11073 | #endif |
9faadcc8 | 11074 | return file; |
2b188cc1 JA |
11075 | } |
11076 | ||
c072481d PB |
11077 | static __cold int io_uring_create(unsigned entries, struct io_uring_params *p, |
11078 | struct io_uring_params __user *params) | |
2b188cc1 | 11079 | { |
2b188cc1 | 11080 | struct io_ring_ctx *ctx; |
9faadcc8 | 11081 | struct file *file; |
2b188cc1 JA |
11082 | int ret; |
11083 | ||
8110c1a6 | 11084 | if (!entries) |
2b188cc1 | 11085 | return -EINVAL; |
8110c1a6 JA |
11086 | if (entries > IORING_MAX_ENTRIES) { |
11087 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
11088 | return -EINVAL; | |
11089 | entries = IORING_MAX_ENTRIES; | |
11090 | } | |
2b188cc1 JA |
11091 | |
11092 | /* | |
11093 | * Use twice as many entries for the CQ ring. It's possible for the | |
11094 | * application to drive a higher depth than the size of the SQ ring, | |
11095 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
11096 | * some flexibility in overcommitting a bit. If the application has |
11097 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
11098 | * of CQ ring entries manually. | |
2b188cc1 JA |
11099 | */ |
11100 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
11101 | if (p->flags & IORING_SETUP_CQSIZE) { |
11102 | /* | |
11103 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
11104 | * to a power-of-two, if it isn't already. We do NOT impose | |
11105 | * any cq vs sq ring sizing. | |
11106 | */ | |
eb2667b3 | 11107 | if (!p->cq_entries) |
33a107f0 | 11108 | return -EINVAL; |
8110c1a6 JA |
11109 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
11110 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
11111 | return -EINVAL; | |
11112 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
11113 | } | |
eb2667b3 JQ |
11114 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
11115 | if (p->cq_entries < p->sq_entries) | |
11116 | return -EINVAL; | |
33a107f0 JA |
11117 | } else { |
11118 | p->cq_entries = 2 * p->sq_entries; | |
11119 | } | |
2b188cc1 | 11120 | |
2b188cc1 | 11121 | ctx = io_ring_ctx_alloc(p); |
62e398be | 11122 | if (!ctx) |
2b188cc1 | 11123 | return -ENOMEM; |
773697b6 PB |
11124 | |
11125 | /* | |
11126 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
11127 | * space applications don't need to do io completion events | |
11128 | * polling again, they can rely on io_sq_thread to do polling | |
11129 | * work, which can reduce cpu usage and uring_lock contention. | |
11130 | */ | |
11131 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
11132 | !(ctx->flags & IORING_SETUP_SQPOLL)) | |
11133 | ctx->syscall_iopoll = 1; | |
11134 | ||
2b188cc1 | 11135 | ctx->compat = in_compat_syscall(); |
62e398be JA |
11136 | if (!capable(CAP_IPC_LOCK)) |
11137 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
11138 | |
11139 | /* | |
11140 | * This is just grabbed for accounting purposes. When a process exits, | |
11141 | * the mm is exited and dropped before the files, hence we need to hang | |
11142 | * on to this mm purely for the purposes of being able to unaccount | |
11143 | * memory (locked/pinned vm). It's not used for anything else. | |
11144 | */ | |
6b7898eb | 11145 | mmgrab(current->mm); |
2aede0e4 | 11146 | ctx->mm_account = current->mm; |
6b7898eb | 11147 | |
2b188cc1 JA |
11148 | ret = io_allocate_scq_urings(ctx, p); |
11149 | if (ret) | |
11150 | goto err; | |
11151 | ||
7e84e1c7 | 11152 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
11153 | if (ret) |
11154 | goto err; | |
eae071c9 | 11155 | /* always set a rsrc node */ |
47b228ce PB |
11156 | ret = io_rsrc_node_switch_start(ctx); |
11157 | if (ret) | |
11158 | goto err; | |
eae071c9 | 11159 | io_rsrc_node_switch(ctx, NULL); |
2b188cc1 | 11160 | |
2b188cc1 | 11161 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
11162 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
11163 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
11164 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
11165 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
11166 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
11167 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
11168 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
11169 | |
11170 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
11171 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
11172 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
11173 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
11174 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
11175 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
11176 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 11177 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 11178 | |
7f13657d XW |
11179 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
11180 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 11181 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 11182 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9690557e | 11183 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
c4212f3e JA |
11184 | IORING_FEAT_RSRC_TAGS | IORING_FEAT_CQE_SKIP | |
11185 | IORING_FEAT_LINKED_FILE; | |
7f13657d XW |
11186 | |
11187 | if (copy_to_user(params, p, sizeof(*p))) { | |
11188 | ret = -EFAULT; | |
11189 | goto err; | |
11190 | } | |
d1719f70 | 11191 | |
9faadcc8 PB |
11192 | file = io_uring_get_file(ctx); |
11193 | if (IS_ERR(file)) { | |
11194 | ret = PTR_ERR(file); | |
11195 | goto err; | |
11196 | } | |
11197 | ||
044c1ab3 JA |
11198 | /* |
11199 | * Install ring fd as the very last thing, so we don't risk someone | |
11200 | * having closed it before we finish setup | |
11201 | */ | |
9faadcc8 PB |
11202 | ret = io_uring_install_fd(ctx, file); |
11203 | if (ret < 0) { | |
11204 | /* fput will clean it up */ | |
11205 | fput(file); | |
11206 | return ret; | |
11207 | } | |
044c1ab3 | 11208 | |
c826bd7a | 11209 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
11210 | return ret; |
11211 | err: | |
11212 | io_ring_ctx_wait_and_kill(ctx); | |
11213 | return ret; | |
11214 | } | |
11215 | ||
11216 | /* | |
11217 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
11218 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
11219 | * params structure passed in. | |
11220 | */ | |
11221 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
11222 | { | |
11223 | struct io_uring_params p; | |
2b188cc1 JA |
11224 | int i; |
11225 | ||
11226 | if (copy_from_user(&p, params, sizeof(p))) | |
11227 | return -EFAULT; | |
11228 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
11229 | if (p.resv[i]) | |
11230 | return -EINVAL; | |
11231 | } | |
11232 | ||
6c271ce2 | 11233 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 11234 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 | 11235 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
bcbb7bf6 | 11236 | IORING_SETUP_R_DISABLED | IORING_SETUP_SUBMIT_ALL)) |
2b188cc1 JA |
11237 | return -EINVAL; |
11238 | ||
7f13657d | 11239 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
11240 | } |
11241 | ||
11242 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
11243 | struct io_uring_params __user *, params) | |
11244 | { | |
11245 | return io_uring_setup(entries, params); | |
11246 | } | |
11247 | ||
c072481d PB |
11248 | static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg, |
11249 | unsigned nr_args) | |
66f4af93 JA |
11250 | { |
11251 | struct io_uring_probe *p; | |
11252 | size_t size; | |
11253 | int i, ret; | |
11254 | ||
11255 | size = struct_size(p, ops, nr_args); | |
11256 | if (size == SIZE_MAX) | |
11257 | return -EOVERFLOW; | |
11258 | p = kzalloc(size, GFP_KERNEL); | |
11259 | if (!p) | |
11260 | return -ENOMEM; | |
11261 | ||
11262 | ret = -EFAULT; | |
11263 | if (copy_from_user(p, arg, size)) | |
11264 | goto out; | |
11265 | ret = -EINVAL; | |
11266 | if (memchr_inv(p, 0, size)) | |
11267 | goto out; | |
11268 | ||
11269 | p->last_op = IORING_OP_LAST - 1; | |
11270 | if (nr_args > IORING_OP_LAST) | |
11271 | nr_args = IORING_OP_LAST; | |
11272 | ||
11273 | for (i = 0; i < nr_args; i++) { | |
11274 | p->ops[i].op = i; | |
11275 | if (!io_op_defs[i].not_supported) | |
11276 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
11277 | } | |
11278 | p->ops_len = i; | |
11279 | ||
11280 | ret = 0; | |
11281 | if (copy_to_user(arg, p, size)) | |
11282 | ret = -EFAULT; | |
11283 | out: | |
11284 | kfree(p); | |
11285 | return ret; | |
11286 | } | |
11287 | ||
071698e1 JA |
11288 | static int io_register_personality(struct io_ring_ctx *ctx) |
11289 | { | |
4379bf8b | 11290 | const struct cred *creds; |
61cf9370 | 11291 | u32 id; |
1e6fa521 | 11292 | int ret; |
071698e1 | 11293 | |
4379bf8b | 11294 | creds = get_current_cred(); |
1e6fa521 | 11295 | |
61cf9370 MWO |
11296 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
11297 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); | |
a30f895a JA |
11298 | if (ret < 0) { |
11299 | put_cred(creds); | |
11300 | return ret; | |
11301 | } | |
11302 | return id; | |
071698e1 JA |
11303 | } |
11304 | ||
c072481d PB |
11305 | static __cold int io_register_restrictions(struct io_ring_ctx *ctx, |
11306 | void __user *arg, unsigned int nr_args) | |
21b55dbc SG |
11307 | { |
11308 | struct io_uring_restriction *res; | |
11309 | size_t size; | |
11310 | int i, ret; | |
11311 | ||
7e84e1c7 SG |
11312 | /* Restrictions allowed only if rings started disabled */ |
11313 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
11314 | return -EBADFD; | |
11315 | ||
21b55dbc | 11316 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 11317 | if (ctx->restrictions.registered) |
21b55dbc SG |
11318 | return -EBUSY; |
11319 | ||
11320 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
11321 | return -EINVAL; | |
11322 | ||
11323 | size = array_size(nr_args, sizeof(*res)); | |
11324 | if (size == SIZE_MAX) | |
11325 | return -EOVERFLOW; | |
11326 | ||
11327 | res = memdup_user(arg, size); | |
11328 | if (IS_ERR(res)) | |
11329 | return PTR_ERR(res); | |
11330 | ||
11331 | ret = 0; | |
11332 | ||
11333 | for (i = 0; i < nr_args; i++) { | |
11334 | switch (res[i].opcode) { | |
11335 | case IORING_RESTRICTION_REGISTER_OP: | |
11336 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
11337 | ret = -EINVAL; | |
11338 | goto out; | |
11339 | } | |
11340 | ||
11341 | __set_bit(res[i].register_op, | |
11342 | ctx->restrictions.register_op); | |
11343 | break; | |
11344 | case IORING_RESTRICTION_SQE_OP: | |
11345 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
11346 | ret = -EINVAL; | |
11347 | goto out; | |
11348 | } | |
11349 | ||
11350 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
11351 | break; | |
11352 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
11353 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
11354 | break; | |
11355 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
11356 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
11357 | break; | |
11358 | default: | |
11359 | ret = -EINVAL; | |
11360 | goto out; | |
11361 | } | |
11362 | } | |
11363 | ||
11364 | out: | |
11365 | /* Reset all restrictions if an error happened */ | |
11366 | if (ret != 0) | |
11367 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
11368 | else | |
7e84e1c7 | 11369 | ctx->restrictions.registered = true; |
21b55dbc SG |
11370 | |
11371 | kfree(res); | |
11372 | return ret; | |
11373 | } | |
11374 | ||
7e84e1c7 SG |
11375 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
11376 | { | |
11377 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
11378 | return -EBADFD; | |
11379 | ||
11380 | if (ctx->restrictions.registered) | |
11381 | ctx->restricted = 1; | |
11382 | ||
0298ef96 PB |
11383 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
11384 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) | |
11385 | wake_up(&ctx->sq_data->wait); | |
7e84e1c7 SG |
11386 | return 0; |
11387 | } | |
11388 | ||
fdecb662 | 11389 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 11390 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 PB |
11391 | unsigned nr_args) |
11392 | { | |
11393 | __u32 tmp; | |
11394 | int err; | |
11395 | ||
11396 | if (check_add_overflow(up->offset, nr_args, &tmp)) | |
11397 | return -EOVERFLOW; | |
11398 | err = io_rsrc_node_switch_start(ctx); | |
11399 | if (err) | |
11400 | return err; | |
11401 | ||
fdecb662 PB |
11402 | switch (type) { |
11403 | case IORING_RSRC_FILE: | |
98f0b3b4 | 11404 | return __io_sqe_files_update(ctx, up, nr_args); |
634d00df PB |
11405 | case IORING_RSRC_BUFFER: |
11406 | return __io_sqe_buffers_update(ctx, up, nr_args); | |
98f0b3b4 PB |
11407 | } |
11408 | return -EINVAL; | |
11409 | } | |
11410 | ||
c3bdad02 PB |
11411 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
11412 | unsigned nr_args) | |
98f0b3b4 | 11413 | { |
c3bdad02 | 11414 | struct io_uring_rsrc_update2 up; |
98f0b3b4 PB |
11415 | |
11416 | if (!nr_args) | |
11417 | return -EINVAL; | |
c3bdad02 PB |
11418 | memset(&up, 0, sizeof(up)); |
11419 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) | |
11420 | return -EFAULT; | |
d8a3ba9c | 11421 | if (up.resv || up.resv2) |
565c5e61 | 11422 | return -EINVAL; |
c3bdad02 PB |
11423 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); |
11424 | } | |
11425 | ||
11426 | static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
992da01a | 11427 | unsigned size, unsigned type) |
c3bdad02 PB |
11428 | { |
11429 | struct io_uring_rsrc_update2 up; | |
11430 | ||
11431 | if (size != sizeof(up)) | |
11432 | return -EINVAL; | |
98f0b3b4 PB |
11433 | if (copy_from_user(&up, arg, sizeof(up))) |
11434 | return -EFAULT; | |
d8a3ba9c | 11435 | if (!up.nr || up.resv || up.resv2) |
98f0b3b4 | 11436 | return -EINVAL; |
992da01a | 11437 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
98f0b3b4 PB |
11438 | } |
11439 | ||
c072481d | 11440 | static __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
992da01a | 11441 | unsigned int size, unsigned int type) |
792e3582 PB |
11442 | { |
11443 | struct io_uring_rsrc_register rr; | |
11444 | ||
11445 | /* keep it extendible */ | |
11446 | if (size != sizeof(rr)) | |
11447 | return -EINVAL; | |
11448 | ||
11449 | memset(&rr, 0, sizeof(rr)); | |
11450 | if (copy_from_user(&rr, arg, size)) | |
11451 | return -EFAULT; | |
992da01a | 11452 | if (!rr.nr || rr.resv || rr.resv2) |
792e3582 PB |
11453 | return -EINVAL; |
11454 | ||
992da01a | 11455 | switch (type) { |
792e3582 PB |
11456 | case IORING_RSRC_FILE: |
11457 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), | |
11458 | rr.nr, u64_to_user_ptr(rr.tags)); | |
634d00df PB |
11459 | case IORING_RSRC_BUFFER: |
11460 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), | |
11461 | rr.nr, u64_to_user_ptr(rr.tags)); | |
792e3582 PB |
11462 | } |
11463 | return -EINVAL; | |
11464 | } | |
11465 | ||
c072481d PB |
11466 | static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx, |
11467 | void __user *arg, unsigned len) | |
fe76421d JA |
11468 | { |
11469 | struct io_uring_task *tctx = current->io_uring; | |
11470 | cpumask_var_t new_mask; | |
11471 | int ret; | |
11472 | ||
11473 | if (!tctx || !tctx->io_wq) | |
11474 | return -EINVAL; | |
11475 | ||
11476 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | |
11477 | return -ENOMEM; | |
11478 | ||
11479 | cpumask_clear(new_mask); | |
11480 | if (len > cpumask_size()) | |
11481 | len = cpumask_size(); | |
11482 | ||
0f5e4b83 ES |
11483 | if (in_compat_syscall()) { |
11484 | ret = compat_get_bitmap(cpumask_bits(new_mask), | |
11485 | (const compat_ulong_t __user *)arg, | |
11486 | len * 8 /* CHAR_BIT */); | |
11487 | } else { | |
11488 | ret = copy_from_user(new_mask, arg, len); | |
11489 | } | |
11490 | ||
11491 | if (ret) { | |
fe76421d JA |
11492 | free_cpumask_var(new_mask); |
11493 | return -EFAULT; | |
11494 | } | |
11495 | ||
11496 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); | |
11497 | free_cpumask_var(new_mask); | |
11498 | return ret; | |
11499 | } | |
11500 | ||
c072481d | 11501 | static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx) |
fe76421d JA |
11502 | { |
11503 | struct io_uring_task *tctx = current->io_uring; | |
11504 | ||
11505 | if (!tctx || !tctx->io_wq) | |
11506 | return -EINVAL; | |
11507 | ||
11508 | return io_wq_cpu_affinity(tctx->io_wq, NULL); | |
11509 | } | |
11510 | ||
c072481d PB |
11511 | static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, |
11512 | void __user *arg) | |
b22fa62a | 11513 | __must_hold(&ctx->uring_lock) |
2e480058 | 11514 | { |
b22fa62a | 11515 | struct io_tctx_node *node; |
fa84693b JA |
11516 | struct io_uring_task *tctx = NULL; |
11517 | struct io_sq_data *sqd = NULL; | |
2e480058 JA |
11518 | __u32 new_count[2]; |
11519 | int i, ret; | |
11520 | ||
2e480058 JA |
11521 | if (copy_from_user(new_count, arg, sizeof(new_count))) |
11522 | return -EFAULT; | |
11523 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
11524 | if (new_count[i] > INT_MAX) | |
11525 | return -EINVAL; | |
11526 | ||
fa84693b JA |
11527 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
11528 | sqd = ctx->sq_data; | |
11529 | if (sqd) { | |
009ad9f0 JA |
11530 | /* |
11531 | * Observe the correct sqd->lock -> ctx->uring_lock | |
11532 | * ordering. Fine to drop uring_lock here, we hold | |
11533 | * a ref to the ctx. | |
11534 | */ | |
41d3a6bd | 11535 | refcount_inc(&sqd->refs); |
009ad9f0 | 11536 | mutex_unlock(&ctx->uring_lock); |
fa84693b | 11537 | mutex_lock(&sqd->lock); |
009ad9f0 | 11538 | mutex_lock(&ctx->uring_lock); |
41d3a6bd JA |
11539 | if (sqd->thread) |
11540 | tctx = sqd->thread->io_uring; | |
fa84693b JA |
11541 | } |
11542 | } else { | |
11543 | tctx = current->io_uring; | |
11544 | } | |
11545 | ||
e139a1ec | 11546 | BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits)); |
fa84693b | 11547 | |
bad119b9 PB |
11548 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
11549 | if (new_count[i]) | |
11550 | ctx->iowq_limits[i] = new_count[i]; | |
e139a1ec PB |
11551 | ctx->iowq_limits_set = true; |
11552 | ||
e139a1ec PB |
11553 | if (tctx && tctx->io_wq) { |
11554 | ret = io_wq_max_workers(tctx->io_wq, new_count); | |
11555 | if (ret) | |
11556 | goto err; | |
11557 | } else { | |
11558 | memset(new_count, 0, sizeof(new_count)); | |
11559 | } | |
fa84693b | 11560 | |
41d3a6bd | 11561 | if (sqd) { |
fa84693b | 11562 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
11563 | io_put_sq_data(sqd); |
11564 | } | |
2e480058 JA |
11565 | |
11566 | if (copy_to_user(arg, new_count, sizeof(new_count))) | |
11567 | return -EFAULT; | |
11568 | ||
b22fa62a PB |
11569 | /* that's it for SQPOLL, only the SQPOLL task creates requests */ |
11570 | if (sqd) | |
11571 | return 0; | |
11572 | ||
11573 | /* now propagate the restriction to all registered users */ | |
11574 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
11575 | struct io_uring_task *tctx = node->task->io_uring; | |
11576 | ||
11577 | if (WARN_ON_ONCE(!tctx->io_wq)) | |
11578 | continue; | |
11579 | ||
11580 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
11581 | new_count[i] = ctx->iowq_limits[i]; | |
11582 | /* ignore errors, it always returns zero anyway */ | |
11583 | (void)io_wq_max_workers(tctx->io_wq, new_count); | |
11584 | } | |
2e480058 | 11585 | return 0; |
fa84693b | 11586 | err: |
41d3a6bd | 11587 | if (sqd) { |
fa84693b | 11588 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
11589 | io_put_sq_data(sqd); |
11590 | } | |
fa84693b | 11591 | return ret; |
2e480058 JA |
11592 | } |
11593 | ||
edafccee JA |
11594 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
11595 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
11596 | __releases(ctx->uring_lock) |
11597 | __acquires(ctx->uring_lock) | |
edafccee JA |
11598 | { |
11599 | int ret; | |
11600 | ||
35fa71a0 JA |
11601 | /* |
11602 | * We're inside the ring mutex, if the ref is already dying, then | |
11603 | * someone else killed the ctx or is already going through | |
11604 | * io_uring_register(). | |
11605 | */ | |
11606 | if (percpu_ref_is_dying(&ctx->refs)) | |
11607 | return -ENXIO; | |
11608 | ||
75c4021a PB |
11609 | if (ctx->restricted) { |
11610 | if (opcode >= IORING_REGISTER_LAST) | |
11611 | return -EINVAL; | |
11612 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); | |
11613 | if (!test_bit(opcode, ctx->restrictions.register_op)) | |
11614 | return -EACCES; | |
11615 | } | |
11616 | ||
edafccee JA |
11617 | switch (opcode) { |
11618 | case IORING_REGISTER_BUFFERS: | |
634d00df | 11619 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
edafccee JA |
11620 | break; |
11621 | case IORING_UNREGISTER_BUFFERS: | |
11622 | ret = -EINVAL; | |
11623 | if (arg || nr_args) | |
11624 | break; | |
0a96bbe4 | 11625 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 11626 | break; |
6b06314c | 11627 | case IORING_REGISTER_FILES: |
792e3582 | 11628 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
6b06314c JA |
11629 | break; |
11630 | case IORING_UNREGISTER_FILES: | |
11631 | ret = -EINVAL; | |
11632 | if (arg || nr_args) | |
11633 | break; | |
11634 | ret = io_sqe_files_unregister(ctx); | |
11635 | break; | |
c3a31e60 | 11636 | case IORING_REGISTER_FILES_UPDATE: |
c3bdad02 | 11637 | ret = io_register_files_update(ctx, arg, nr_args); |
c3a31e60 | 11638 | break; |
9b402849 JA |
11639 | case IORING_REGISTER_EVENTFD: |
11640 | ret = -EINVAL; | |
11641 | if (nr_args != 1) | |
11642 | break; | |
c75312dd UA |
11643 | ret = io_eventfd_register(ctx, arg, 0); |
11644 | break; | |
11645 | case IORING_REGISTER_EVENTFD_ASYNC: | |
11646 | ret = -EINVAL; | |
11647 | if (nr_args != 1) | |
f2842ab5 | 11648 | break; |
c75312dd | 11649 | ret = io_eventfd_register(ctx, arg, 1); |
9b402849 JA |
11650 | break; |
11651 | case IORING_UNREGISTER_EVENTFD: | |
11652 | ret = -EINVAL; | |
11653 | if (arg || nr_args) | |
11654 | break; | |
11655 | ret = io_eventfd_unregister(ctx); | |
11656 | break; | |
66f4af93 JA |
11657 | case IORING_REGISTER_PROBE: |
11658 | ret = -EINVAL; | |
11659 | if (!arg || nr_args > 256) | |
11660 | break; | |
11661 | ret = io_probe(ctx, arg, nr_args); | |
11662 | break; | |
071698e1 JA |
11663 | case IORING_REGISTER_PERSONALITY: |
11664 | ret = -EINVAL; | |
11665 | if (arg || nr_args) | |
11666 | break; | |
11667 | ret = io_register_personality(ctx); | |
11668 | break; | |
11669 | case IORING_UNREGISTER_PERSONALITY: | |
11670 | ret = -EINVAL; | |
11671 | if (arg) | |
11672 | break; | |
11673 | ret = io_unregister_personality(ctx, nr_args); | |
11674 | break; | |
7e84e1c7 SG |
11675 | case IORING_REGISTER_ENABLE_RINGS: |
11676 | ret = -EINVAL; | |
11677 | if (arg || nr_args) | |
11678 | break; | |
11679 | ret = io_register_enable_rings(ctx); | |
11680 | break; | |
21b55dbc SG |
11681 | case IORING_REGISTER_RESTRICTIONS: |
11682 | ret = io_register_restrictions(ctx, arg, nr_args); | |
11683 | break; | |
992da01a PB |
11684 | case IORING_REGISTER_FILES2: |
11685 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); | |
11686 | break; | |
11687 | case IORING_REGISTER_FILES_UPDATE2: | |
11688 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
11689 | IORING_RSRC_FILE); | |
11690 | break; | |
11691 | case IORING_REGISTER_BUFFERS2: | |
11692 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); | |
792e3582 | 11693 | break; |
992da01a PB |
11694 | case IORING_REGISTER_BUFFERS_UPDATE: |
11695 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
11696 | IORING_RSRC_BUFFER); | |
c3bdad02 | 11697 | break; |
fe76421d JA |
11698 | case IORING_REGISTER_IOWQ_AFF: |
11699 | ret = -EINVAL; | |
11700 | if (!arg || !nr_args) | |
11701 | break; | |
11702 | ret = io_register_iowq_aff(ctx, arg, nr_args); | |
11703 | break; | |
11704 | case IORING_UNREGISTER_IOWQ_AFF: | |
11705 | ret = -EINVAL; | |
11706 | if (arg || nr_args) | |
11707 | break; | |
11708 | ret = io_unregister_iowq_aff(ctx); | |
11709 | break; | |
2e480058 JA |
11710 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
11711 | ret = -EINVAL; | |
11712 | if (!arg || nr_args != 2) | |
11713 | break; | |
11714 | ret = io_register_iowq_max_workers(ctx, arg); | |
11715 | break; | |
e7a6c00d JA |
11716 | case IORING_REGISTER_RING_FDS: |
11717 | ret = io_ringfd_register(ctx, arg, nr_args); | |
11718 | break; | |
11719 | case IORING_UNREGISTER_RING_FDS: | |
11720 | ret = io_ringfd_unregister(ctx, arg, nr_args); | |
11721 | break; | |
edafccee JA |
11722 | default: |
11723 | ret = -EINVAL; | |
11724 | break; | |
11725 | } | |
11726 | ||
edafccee JA |
11727 | return ret; |
11728 | } | |
11729 | ||
11730 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
11731 | void __user *, arg, unsigned int, nr_args) | |
11732 | { | |
11733 | struct io_ring_ctx *ctx; | |
11734 | long ret = -EBADF; | |
11735 | struct fd f; | |
11736 | ||
11737 | f = fdget(fd); | |
11738 | if (!f.file) | |
11739 | return -EBADF; | |
11740 | ||
11741 | ret = -EOPNOTSUPP; | |
11742 | if (f.file->f_op != &io_uring_fops) | |
11743 | goto out_fput; | |
11744 | ||
11745 | ctx = f.file->private_data; | |
11746 | ||
b6c23dd5 PB |
11747 | io_run_task_work(); |
11748 | ||
edafccee JA |
11749 | mutex_lock(&ctx->uring_lock); |
11750 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
11751 | mutex_unlock(&ctx->uring_lock); | |
2757be22 | 11752 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, ret); |
edafccee JA |
11753 | out_fput: |
11754 | fdput(f); | |
11755 | return ret; | |
11756 | } | |
11757 | ||
2b188cc1 JA |
11758 | static int __init io_uring_init(void) |
11759 | { | |
d7f62e82 SM |
11760 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
11761 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
11762 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
11763 | } while (0) | |
11764 | ||
11765 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
11766 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
11767 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
11768 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
11769 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
11770 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
11771 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
11772 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
11773 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
11774 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 11775 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
11776 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
11777 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
11778 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
11779 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
11780 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
11781 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
11782 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
11783 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
11784 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
11785 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
11786 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
11787 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
11788 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
11789 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
11790 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 11791 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
11792 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
11793 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
16340eab | 11794 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
d7f62e82 | 11795 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
7d67af2c | 11796 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
b9445598 | 11797 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
d7f62e82 | 11798 | |
b0d658ec PB |
11799 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
11800 | sizeof(struct io_uring_rsrc_update)); | |
11801 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > | |
11802 | sizeof(struct io_uring_rsrc_update2)); | |
90499ad0 PB |
11803 | |
11804 | /* ->buf_index is u16 */ | |
11805 | BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); | |
11806 | ||
b0d658ec PB |
11807 | /* should fit into one byte */ |
11808 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); | |
68fe256a PB |
11809 | BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8)); |
11810 | BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS); | |
b0d658ec | 11811 | |
d3656344 | 11812 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
32c2d33e | 11813 | BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int)); |
16340eab | 11814 | |
91f245d5 JA |
11815 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
11816 | SLAB_ACCOUNT); | |
2b188cc1 JA |
11817 | return 0; |
11818 | }; | |
11819 | __initcall(io_uring_init); |