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 | */ | |
3a4b89a2 | 172 | atomic_t 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 | ||
5e45690a JA |
226 | /* |
227 | * FFS_SCM is only available on 64-bit archs, for 32-bit we just define it as 0 | |
228 | * and define IO_URING_SCM_ALL. For this case, we use SCM for all files as we | |
229 | * can't safely always dereference the file when the task has exited and ring | |
230 | * cleanup is done. If a file is tracked and part of SCM, then unix gc on | |
231 | * process exit may reap it before __io_sqe_files_unregister() is run. | |
232 | */ | |
233 | #define FFS_NOWAIT 0x1UL | |
234 | #define FFS_ISREG 0x2UL | |
235 | #if defined(CONFIG_64BIT) | |
236 | #define FFS_SCM 0x4UL | |
237 | #else | |
238 | #define IO_URING_SCM_ALL | |
239 | #define FFS_SCM 0x0UL | |
240 | #endif | |
241 | #define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG|FFS_SCM) | |
242 | ||
a04b0ac0 PB |
243 | struct io_fixed_file { |
244 | /* file * with additional FFS_* flags */ | |
245 | unsigned long file_ptr; | |
246 | }; | |
247 | ||
269bbe5f BM |
248 | struct io_rsrc_put { |
249 | struct list_head list; | |
b60c8dce | 250 | u64 tag; |
50238531 BM |
251 | union { |
252 | void *rsrc; | |
253 | struct file *file; | |
bd54b6fe | 254 | struct io_mapped_ubuf *buf; |
50238531 | 255 | }; |
269bbe5f BM |
256 | }; |
257 | ||
aeca241b | 258 | struct io_file_table { |
042b0d85 | 259 | struct io_fixed_file *files; |
31b51510 JA |
260 | }; |
261 | ||
b895c9a6 | 262 | struct io_rsrc_node { |
05589553 XW |
263 | struct percpu_ref refs; |
264 | struct list_head node; | |
269bbe5f | 265 | struct list_head rsrc_list; |
b895c9a6 | 266 | struct io_rsrc_data *rsrc_data; |
4a38aed2 | 267 | struct llist_node llist; |
e297822b | 268 | bool done; |
05589553 XW |
269 | }; |
270 | ||
40ae0ff7 PB |
271 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
272 | ||
b895c9a6 | 273 | struct io_rsrc_data { |
05f3fb3c JA |
274 | struct io_ring_ctx *ctx; |
275 | ||
2d091d62 PB |
276 | u64 **tags; |
277 | unsigned int nr; | |
40ae0ff7 | 278 | rsrc_put_fn *do_put; |
3e942498 | 279 | atomic_t refs; |
05f3fb3c | 280 | struct completion done; |
8bad28d8 | 281 | bool quiesce; |
05f3fb3c JA |
282 | }; |
283 | ||
dbc7d452 JA |
284 | struct io_buffer_list { |
285 | struct list_head list; | |
286 | struct list_head buf_list; | |
287 | __u16 bgid; | |
288 | }; | |
289 | ||
5a2e745d JA |
290 | struct io_buffer { |
291 | struct list_head list; | |
292 | __u64 addr; | |
d1f82808 | 293 | __u32 len; |
5a2e745d | 294 | __u16 bid; |
b1c62645 | 295 | __u16 bgid; |
5a2e745d JA |
296 | }; |
297 | ||
21b55dbc SG |
298 | struct io_restriction { |
299 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
300 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
301 | u8 sqe_flags_allowed; | |
302 | u8 sqe_flags_required; | |
7e84e1c7 | 303 | bool registered; |
21b55dbc SG |
304 | }; |
305 | ||
37d1e2e3 JA |
306 | enum { |
307 | IO_SQ_THREAD_SHOULD_STOP = 0, | |
308 | IO_SQ_THREAD_SHOULD_PARK, | |
309 | }; | |
310 | ||
534ca6d6 JA |
311 | struct io_sq_data { |
312 | refcount_t refs; | |
9e138a48 | 313 | atomic_t park_pending; |
09a6f4ef | 314 | struct mutex lock; |
69fb2131 JA |
315 | |
316 | /* ctx's that are using this sqd */ | |
317 | struct list_head ctx_list; | |
69fb2131 | 318 | |
534ca6d6 JA |
319 | struct task_struct *thread; |
320 | struct wait_queue_head wait; | |
08369246 XW |
321 | |
322 | unsigned sq_thread_idle; | |
37d1e2e3 JA |
323 | int sq_cpu; |
324 | pid_t task_pid; | |
5c2469e0 | 325 | pid_t task_tgid; |
37d1e2e3 JA |
326 | |
327 | unsigned long state; | |
37d1e2e3 | 328 | struct completion exited; |
534ca6d6 JA |
329 | }; |
330 | ||
6dd0be1e | 331 | #define IO_COMPL_BATCH 32 |
6ff119a6 | 332 | #define IO_REQ_CACHE_SIZE 32 |
bf019da7 | 333 | #define IO_REQ_ALLOC_BATCH 8 |
258b29a9 | 334 | |
a1ab7b35 PB |
335 | struct io_submit_link { |
336 | struct io_kiocb *head; | |
337 | struct io_kiocb *last; | |
338 | }; | |
339 | ||
258b29a9 | 340 | struct io_submit_state { |
5a158c6b PB |
341 | /* inline/task_work completion list, under ->uring_lock */ |
342 | struct io_wq_work_node free_list; | |
343 | /* batch completion logic */ | |
344 | struct io_wq_work_list compl_reqs; | |
a1ab7b35 | 345 | struct io_submit_link link; |
258b29a9 | 346 | |
258b29a9 | 347 | bool plug_started; |
4b628aeb | 348 | bool need_plug; |
3d4aeb9f | 349 | bool flush_cqes; |
5ca7a8b3 | 350 | unsigned short submit_nr; |
5a158c6b | 351 | struct blk_plug plug; |
258b29a9 PB |
352 | }; |
353 | ||
77bc59b4 UA |
354 | struct io_ev_fd { |
355 | struct eventfd_ctx *cq_ev_fd; | |
c75312dd | 356 | unsigned int eventfd_async: 1; |
77bc59b4 UA |
357 | struct rcu_head rcu; |
358 | }; | |
359 | ||
dbc7d452 JA |
360 | #define IO_BUFFERS_HASH_BITS 5 |
361 | ||
2b188cc1 | 362 | struct io_ring_ctx { |
b52ecf8c | 363 | /* const or read-mostly hot data */ |
2b188cc1 JA |
364 | struct { |
365 | struct percpu_ref refs; | |
2b188cc1 | 366 | |
b52ecf8c | 367 | struct io_rings *rings; |
2b188cc1 | 368 | unsigned int flags; |
9f010507 | 369 | enum task_work_notify_mode notify_method; |
e1d85334 | 370 | unsigned int compat: 1; |
e1d85334 | 371 | unsigned int drain_next: 1; |
21b55dbc | 372 | unsigned int restricted: 1; |
f18ee4cf | 373 | unsigned int off_timeout_used: 1; |
10c66904 | 374 | unsigned int drain_active: 1; |
5562a8d7 | 375 | unsigned int drain_disabled: 1; |
9aa8dfde | 376 | unsigned int has_evfd: 1; |
773697b6 | 377 | unsigned int syscall_iopoll: 1; |
b52ecf8c | 378 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 379 | |
7f1129d2 | 380 | /* submission data */ |
b52ecf8c | 381 | struct { |
0499e582 PB |
382 | struct mutex uring_lock; |
383 | ||
75b28aff HV |
384 | /* |
385 | * Ring buffer of indices into array of io_uring_sqe, which is | |
386 | * mmapped by the application using the IORING_OFF_SQES offset. | |
387 | * | |
388 | * This indirection could e.g. be used to assign fixed | |
389 | * io_uring_sqe entries to operations and only submit them to | |
390 | * the queue when needed. | |
391 | * | |
392 | * The kernel modifies neither the indices array nor the entries | |
393 | * array. | |
394 | */ | |
395 | u32 *sq_array; | |
c7af47cf | 396 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
397 | unsigned cached_sq_head; |
398 | unsigned sq_entries; | |
de0617e4 | 399 | struct list_head defer_list; |
7f1129d2 PB |
400 | |
401 | /* | |
402 | * Fixed resources fast path, should be accessed only under | |
403 | * uring_lock, and updated through io_uring_register(2) | |
404 | */ | |
405 | struct io_rsrc_node *rsrc_node; | |
ab409402 | 406 | int rsrc_cached_refs; |
8e29da69 | 407 | atomic_t cancel_seq; |
7f1129d2 PB |
408 | struct io_file_table file_table; |
409 | unsigned nr_user_files; | |
410 | unsigned nr_user_bufs; | |
411 | struct io_mapped_ubuf **user_bufs; | |
412 | ||
413 | struct io_submit_state submit_state; | |
5262f567 | 414 | struct list_head timeout_list; |
ef9dd637 | 415 | struct list_head ltimeout_list; |
1d7bb1d5 | 416 | struct list_head cq_overflow_list; |
dbc7d452 | 417 | struct list_head *io_buffers; |
cc3cec83 | 418 | struct list_head io_buffers_cache; |
4d9237e3 | 419 | struct list_head apoll_cache; |
7f1129d2 PB |
420 | struct xarray personalities; |
421 | u32 pers_next; | |
422 | unsigned sq_thread_idle; | |
2b188cc1 JA |
423 | } ____cacheline_aligned_in_smp; |
424 | ||
d0acdee2 | 425 | /* IRQ completion list, under ->completion_lock */ |
c2b6c6bc | 426 | struct io_wq_work_list locked_free_list; |
d0acdee2 | 427 | unsigned int locked_free_nr; |
3c1a2ead | 428 | |
7c30f36a | 429 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
534ca6d6 JA |
430 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
431 | ||
90554200 | 432 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 433 | struct list_head sqd_list; |
75b28aff | 434 | |
10988a0a | 435 | unsigned long check_cq; |
5ed7a37d | 436 | |
206aefde | 437 | struct { |
d8da428b PB |
438 | /* |
439 | * We cache a range of free CQEs we can use, once exhausted it | |
440 | * should go through a slower range setup, see __io_get_cqe() | |
441 | */ | |
442 | struct io_uring_cqe *cqe_cached; | |
443 | struct io_uring_cqe *cqe_sentinel; | |
444 | ||
206aefde JA |
445 | unsigned cached_cq_tail; |
446 | unsigned cq_entries; | |
77bc59b4 | 447 | struct io_ev_fd __rcu *io_ev_fd; |
0499e582 PB |
448 | struct wait_queue_head cq_wait; |
449 | unsigned cq_extra; | |
450 | atomic_t cq_timeouts; | |
0499e582 | 451 | unsigned cq_last_tm_flush; |
206aefde | 452 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 453 | |
2b188cc1 JA |
454 | struct { |
455 | spinlock_t completion_lock; | |
e94f141b | 456 | |
89850fce JA |
457 | spinlock_t timeout_lock; |
458 | ||
def596e9 | 459 | /* |
540e32a0 | 460 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
461 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
462 | * For SQPOLL, only the single threaded io_sq_thread() will | |
463 | * manipulate the list, hence no extra locking is needed there. | |
464 | */ | |
5eef4e87 | 465 | struct io_wq_work_list iopoll_list; |
78076bb6 JA |
466 | struct hlist_head *cancel_hash; |
467 | unsigned cancel_hash_bits; | |
915b3dde | 468 | bool poll_multi_queue; |
cc3cec83 JA |
469 | |
470 | struct list_head io_buffers_comp; | |
2b188cc1 | 471 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 472 | |
21b55dbc | 473 | struct io_restriction restrictions; |
3c1a2ead | 474 | |
b13a8918 PB |
475 | /* slow path rsrc auxilary data, used by update/register */ |
476 | struct { | |
477 | struct io_rsrc_node *rsrc_backup_node; | |
478 | struct io_mapped_ubuf *dummy_ubuf; | |
479 | struct io_rsrc_data *file_data; | |
480 | struct io_rsrc_data *buf_data; | |
481 | ||
482 | struct delayed_work rsrc_put_work; | |
483 | struct llist_head rsrc_put_llist; | |
484 | struct list_head rsrc_ref_list; | |
485 | spinlock_t rsrc_ref_lock; | |
cc3cec83 JA |
486 | |
487 | struct list_head io_buffers_pages; | |
b13a8918 PB |
488 | }; |
489 | ||
3c1a2ead | 490 | /* Keep this last, we don't need it for the fast path */ |
b986af7e PB |
491 | struct { |
492 | #if defined(CONFIG_UNIX) | |
493 | struct socket *ring_sock; | |
494 | #endif | |
495 | /* hashed buffered write serialization */ | |
496 | struct io_wq_hash *hash_map; | |
497 | ||
498 | /* Only used for accounting purposes */ | |
499 | struct user_struct *user; | |
500 | struct mm_struct *mm_account; | |
501 | ||
502 | /* ctx exit and cancelation */ | |
9011bf9a PB |
503 | struct llist_head fallback_llist; |
504 | struct delayed_work fallback_work; | |
b986af7e PB |
505 | struct work_struct exit_work; |
506 | struct list_head tctx_list; | |
507 | struct completion ref_comp; | |
e139a1ec PB |
508 | u32 iowq_limits[2]; |
509 | bool iowq_limits_set; | |
b986af7e | 510 | }; |
2b188cc1 JA |
511 | }; |
512 | ||
e7a6c00d JA |
513 | /* |
514 | * Arbitrary limit, can be raised if need be | |
515 | */ | |
516 | #define IO_RINGFD_REG_MAX 16 | |
517 | ||
53e043b2 SM |
518 | struct io_uring_task { |
519 | /* submission side */ | |
09899b19 | 520 | int cached_refs; |
53e043b2 SM |
521 | struct xarray xa; |
522 | struct wait_queue_head wait; | |
ee53fb2b SM |
523 | const struct io_ring_ctx *last; |
524 | struct io_wq *io_wq; | |
53e043b2 SM |
525 | struct percpu_counter inflight; |
526 | atomic_t in_idle; | |
53e043b2 SM |
527 | |
528 | spinlock_t task_lock; | |
529 | struct io_wq_work_list task_list; | |
4813c377 | 530 | struct io_wq_work_list prior_task_list; |
53e043b2 | 531 | struct callback_head task_work; |
e7a6c00d | 532 | struct file **registered_rings; |
6294f368 | 533 | bool task_running; |
53e043b2 SM |
534 | }; |
535 | ||
09bb8394 JA |
536 | /* |
537 | * First field must be the file pointer in all the | |
538 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
539 | */ | |
221c5eb2 JA |
540 | struct io_poll_iocb { |
541 | struct file *file; | |
018043be | 542 | struct wait_queue_head *head; |
221c5eb2 | 543 | __poll_t events; |
392edb45 | 544 | struct wait_queue_entry wait; |
221c5eb2 JA |
545 | }; |
546 | ||
9d805892 | 547 | struct io_poll_update { |
018043be | 548 | struct file *file; |
9d805892 PB |
549 | u64 old_user_data; |
550 | u64 new_user_data; | |
551 | __poll_t events; | |
b69de288 JA |
552 | bool update_events; |
553 | bool update_user_data; | |
018043be PB |
554 | }; |
555 | ||
b5dba59e JA |
556 | struct io_close { |
557 | struct file *file; | |
b5dba59e | 558 | int fd; |
7df778be | 559 | u32 file_slot; |
b5dba59e JA |
560 | }; |
561 | ||
ad8a48ac JA |
562 | struct io_timeout_data { |
563 | struct io_kiocb *req; | |
564 | struct hrtimer timer; | |
565 | struct timespec64 ts; | |
566 | enum hrtimer_mode mode; | |
50c1df2b | 567 | u32 flags; |
ad8a48ac JA |
568 | }; |
569 | ||
8ed8d3c3 JA |
570 | struct io_accept { |
571 | struct file *file; | |
572 | struct sockaddr __user *addr; | |
573 | int __user *addr_len; | |
574 | int flags; | |
aaa4db12 | 575 | u32 file_slot; |
09952e3e | 576 | unsigned long nofile; |
8ed8d3c3 JA |
577 | }; |
578 | ||
579 | struct io_sync { | |
580 | struct file *file; | |
581 | loff_t len; | |
582 | loff_t off; | |
583 | int flags; | |
d63d1b5e | 584 | int mode; |
8ed8d3c3 JA |
585 | }; |
586 | ||
fbf23849 JA |
587 | struct io_cancel { |
588 | struct file *file; | |
589 | u64 addr; | |
8e29da69 | 590 | u32 flags; |
4bf94615 | 591 | s32 fd; |
fbf23849 JA |
592 | }; |
593 | ||
b29472ee JA |
594 | struct io_timeout { |
595 | struct file *file; | |
bfe68a22 PB |
596 | u32 off; |
597 | u32 target_seq; | |
135fcde8 | 598 | struct list_head list; |
90cd7e42 PB |
599 | /* head of the link, used by linked timeouts only */ |
600 | struct io_kiocb *head; | |
89b263f6 JA |
601 | /* for linked completions */ |
602 | struct io_kiocb *prev; | |
b29472ee JA |
603 | }; |
604 | ||
0bdf7a2d PB |
605 | struct io_timeout_rem { |
606 | struct file *file; | |
607 | u64 addr; | |
9c8e11b3 PB |
608 | |
609 | /* timeout update */ | |
610 | struct timespec64 ts; | |
611 | u32 flags; | |
f1042b6c | 612 | bool ltimeout; |
0bdf7a2d PB |
613 | }; |
614 | ||
9adbd45d JA |
615 | struct io_rw { |
616 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
617 | struct kiocb kiocb; | |
618 | u64 addr; | |
584b0180 JA |
619 | u32 len; |
620 | u32 flags; | |
9adbd45d JA |
621 | }; |
622 | ||
3fbb51c1 JA |
623 | struct io_connect { |
624 | struct file *file; | |
625 | struct sockaddr __user *addr; | |
626 | int addr_len; | |
627 | }; | |
628 | ||
e47293fd JA |
629 | struct io_sr_msg { |
630 | struct file *file; | |
fddaface | 631 | union { |
4af3417a PB |
632 | struct compat_msghdr __user *umsg_compat; |
633 | struct user_msghdr __user *umsg; | |
634 | void __user *buf; | |
fddaface | 635 | }; |
e47293fd | 636 | int msg_flags; |
bcda7baa | 637 | int bgid; |
fddaface | 638 | size_t len; |
7ba89d2a | 639 | size_t done_io; |
e47293fd JA |
640 | }; |
641 | ||
15b71abe JA |
642 | struct io_open { |
643 | struct file *file; | |
644 | int dfd; | |
b9445598 | 645 | u32 file_slot; |
15b71abe | 646 | struct filename *filename; |
c12cedf2 | 647 | struct open_how how; |
4022e7af | 648 | unsigned long nofile; |
15b71abe JA |
649 | }; |
650 | ||
269bbe5f | 651 | struct io_rsrc_update { |
05f3fb3c JA |
652 | struct file *file; |
653 | u64 arg; | |
654 | u32 nr_args; | |
655 | u32 offset; | |
656 | }; | |
657 | ||
4840e418 JA |
658 | struct io_fadvise { |
659 | struct file *file; | |
660 | u64 offset; | |
661 | u32 len; | |
662 | u32 advice; | |
663 | }; | |
664 | ||
c1ca757b JA |
665 | struct io_madvise { |
666 | struct file *file; | |
667 | u64 addr; | |
668 | u32 len; | |
669 | u32 advice; | |
670 | }; | |
671 | ||
3e4827b0 JA |
672 | struct io_epoll { |
673 | struct file *file; | |
674 | int epfd; | |
675 | int op; | |
676 | int fd; | |
677 | struct epoll_event event; | |
e47293fd JA |
678 | }; |
679 | ||
7d67af2c PB |
680 | struct io_splice { |
681 | struct file *file_out; | |
7d67af2c PB |
682 | loff_t off_out; |
683 | loff_t off_in; | |
684 | u64 len; | |
a3e4bc23 | 685 | int splice_fd_in; |
7d67af2c PB |
686 | unsigned int flags; |
687 | }; | |
688 | ||
ddf0322d JA |
689 | struct io_provide_buf { |
690 | struct file *file; | |
691 | __u64 addr; | |
38134ada | 692 | __u32 len; |
ddf0322d JA |
693 | __u32 bgid; |
694 | __u16 nbufs; | |
695 | __u16 bid; | |
696 | }; | |
697 | ||
1d9e1288 BM |
698 | struct io_statx { |
699 | struct file *file; | |
700 | int dfd; | |
701 | unsigned int mask; | |
702 | unsigned int flags; | |
1b6fe6e0 | 703 | struct filename *filename; |
1d9e1288 BM |
704 | struct statx __user *buffer; |
705 | }; | |
706 | ||
36f4fa68 JA |
707 | struct io_shutdown { |
708 | struct file *file; | |
709 | int how; | |
710 | }; | |
711 | ||
80a261fd JA |
712 | struct io_rename { |
713 | struct file *file; | |
714 | int old_dfd; | |
715 | int new_dfd; | |
716 | struct filename *oldpath; | |
717 | struct filename *newpath; | |
718 | int flags; | |
719 | }; | |
720 | ||
14a1143b JA |
721 | struct io_unlink { |
722 | struct file *file; | |
723 | int dfd; | |
724 | int flags; | |
725 | struct filename *filename; | |
726 | }; | |
727 | ||
e34a02dc DK |
728 | struct io_mkdir { |
729 | struct file *file; | |
730 | int dfd; | |
731 | umode_t mode; | |
732 | struct filename *filename; | |
733 | }; | |
734 | ||
7a8721f8 DK |
735 | struct io_symlink { |
736 | struct file *file; | |
737 | int new_dfd; | |
738 | struct filename *oldpath; | |
739 | struct filename *newpath; | |
740 | }; | |
741 | ||
cf30da90 DK |
742 | struct io_hardlink { |
743 | struct file *file; | |
744 | int old_dfd; | |
745 | int new_dfd; | |
746 | struct filename *oldpath; | |
747 | struct filename *newpath; | |
748 | int flags; | |
749 | }; | |
750 | ||
4f57f06c JA |
751 | struct io_msg { |
752 | struct file *file; | |
753 | u64 user_data; | |
754 | u32 len; | |
755 | }; | |
756 | ||
f499a021 JA |
757 | struct io_async_connect { |
758 | struct sockaddr_storage address; | |
759 | }; | |
760 | ||
03b1230c JA |
761 | struct io_async_msghdr { |
762 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
763 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
764 | struct iovec *free_iov; | |
03b1230c JA |
765 | struct sockaddr __user *uaddr; |
766 | struct msghdr msg; | |
b537916c | 767 | struct sockaddr_storage addr; |
03b1230c JA |
768 | }; |
769 | ||
538941e2 | 770 | struct io_rw_state { |
ff6165b2 | 771 | struct iov_iter iter; |
cd658695 | 772 | struct iov_iter_state iter_state; |
c88598a9 | 773 | struct iovec fast_iov[UIO_FASTIOV]; |
538941e2 PB |
774 | }; |
775 | ||
776 | struct io_async_rw { | |
777 | struct io_rw_state s; | |
778 | const struct iovec *free_iovec; | |
227c0c96 | 779 | size_t bytes_done; |
bcf5a063 | 780 | struct wait_page_queue wpq; |
f67676d1 JA |
781 | }; |
782 | ||
6b47ee6e PB |
783 | enum { |
784 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
785 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
786 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
787 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
788 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 789 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
04c76b41 | 790 | REQ_F_CQE_SKIP_BIT = IOSQE_CQE_SKIP_SUCCESS_BIT, |
6b47ee6e | 791 | |
dddca226 | 792 | /* first byte is taken by user flags, shift it to not overlap */ |
93d2bcd2 | 793 | REQ_F_FAIL_BIT = 8, |
6b47ee6e PB |
794 | REQ_F_INFLIGHT_BIT, |
795 | REQ_F_CUR_POS_BIT, | |
796 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 797 | REQ_F_LINK_TIMEOUT_BIT, |
99bc4c38 | 798 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 799 | REQ_F_POLLED_BIT, |
bcda7baa | 800 | REQ_F_BUFFER_SELECTED_BIT, |
e342c807 | 801 | REQ_F_COMPLETE_INLINE_BIT, |
230d50d4 | 802 | REQ_F_REISSUE_BIT, |
b8e64b53 | 803 | REQ_F_CREDS_BIT, |
20e60a38 | 804 | REQ_F_REFCOUNT_BIT, |
4d13d1a4 | 805 | REQ_F_ARM_LTIMEOUT_BIT, |
d886e185 | 806 | REQ_F_ASYNC_DATA_BIT, |
04c76b41 | 807 | REQ_F_SKIP_LINK_CQES_BIT, |
91eac1c6 JA |
808 | REQ_F_SINGLE_POLL_BIT, |
809 | REQ_F_DOUBLE_POLL_BIT, | |
8a3e8ee5 | 810 | REQ_F_PARTIAL_IO_BIT, |
7b29f92d | 811 | /* keep async read/write and isreg together and in order */ |
35645ac3 | 812 | REQ_F_SUPPORT_NOWAIT_BIT, |
7b29f92d | 813 | REQ_F_ISREG_BIT, |
84557871 JA |
814 | |
815 | /* not a real bit, just to check we're not overflowing the space */ | |
816 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
817 | }; |
818 | ||
819 | enum { | |
820 | /* ctx owns file */ | |
821 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
822 | /* drain existing IO first */ | |
823 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
824 | /* linked sqes */ | |
825 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
826 | /* doesn't sever on completion < 0 */ | |
827 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
828 | /* IOSQE_ASYNC */ | |
829 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
830 | /* IOSQE_BUFFER_SELECT */ |
831 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
04c76b41 PB |
832 | /* IOSQE_CQE_SKIP_SUCCESS */ |
833 | REQ_F_CQE_SKIP = BIT(REQ_F_CQE_SKIP_BIT), | |
6b47ee6e | 834 | |
6b47ee6e | 835 | /* fail rest of links */ |
93d2bcd2 | 836 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
b05a1bcd | 837 | /* on inflight list, should be cancelled and waited on exit reliably */ |
6b47ee6e PB |
838 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
839 | /* read/write uses file position */ | |
840 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
841 | /* must not punt to workers */ | |
842 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 843 | /* has or had linked timeout */ |
6b47ee6e | 844 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
99bc4c38 PB |
845 | /* needs cleanup */ |
846 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
847 | /* already went through poll handler */ |
848 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
849 | /* buffer already selected */ |
850 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
e342c807 PB |
851 | /* completion is deferred through io_comp_state */ |
852 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
230d50d4 JA |
853 | /* caller should reissue async */ |
854 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), | |
35645ac3 PB |
855 | /* supports async reads/writes */ |
856 | REQ_F_SUPPORT_NOWAIT = BIT(REQ_F_SUPPORT_NOWAIT_BIT), | |
7b29f92d JA |
857 | /* regular file */ |
858 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
b8e64b53 PB |
859 | /* has creds assigned */ |
860 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), | |
20e60a38 PB |
861 | /* skip refcounting if not set */ |
862 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), | |
4d13d1a4 PB |
863 | /* there is a linked timeout that has to be armed */ |
864 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT), | |
d886e185 PB |
865 | /* ->async_data allocated */ |
866 | REQ_F_ASYNC_DATA = BIT(REQ_F_ASYNC_DATA_BIT), | |
04c76b41 PB |
867 | /* don't post CQEs while failing linked requests */ |
868 | REQ_F_SKIP_LINK_CQES = BIT(REQ_F_SKIP_LINK_CQES_BIT), | |
91eac1c6 JA |
869 | /* single poll may be active */ |
870 | REQ_F_SINGLE_POLL = BIT(REQ_F_SINGLE_POLL_BIT), | |
871 | /* double poll may active */ | |
872 | REQ_F_DOUBLE_POLL = BIT(REQ_F_DOUBLE_POLL_BIT), | |
8a3e8ee5 JA |
873 | /* request has already done partial IO */ |
874 | REQ_F_PARTIAL_IO = BIT(REQ_F_PARTIAL_IO_BIT), | |
d7718a9d JA |
875 | }; |
876 | ||
877 | struct async_poll { | |
878 | struct io_poll_iocb poll; | |
807abcb0 | 879 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
880 | }; |
881 | ||
f237c30a | 882 | typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked); |
5b0a6acc | 883 | |
7cbf1722 | 884 | struct io_task_work { |
5b0a6acc PB |
885 | union { |
886 | struct io_wq_work_node node; | |
887 | struct llist_node fallback_node; | |
888 | }; | |
889 | io_req_tw_func_t func; | |
7cbf1722 JA |
890 | }; |
891 | ||
992da01a PB |
892 | enum { |
893 | IORING_RSRC_FILE = 0, | |
894 | IORING_RSRC_BUFFER = 1, | |
895 | }; | |
896 | ||
cef216fc PB |
897 | struct io_cqe { |
898 | __u64 user_data; | |
899 | __s32 res; | |
900 | /* fd initially, then cflags for completion */ | |
901 | union { | |
902 | __u32 flags; | |
903 | int fd; | |
904 | }; | |
905 | }; | |
906 | ||
10988a0a DY |
907 | enum { |
908 | IO_CHECK_CQ_OVERFLOW_BIT, | |
155bc950 | 909 | IO_CHECK_CQ_DROPPED_BIT, |
10988a0a DY |
910 | }; |
911 | ||
09bb8394 JA |
912 | /* |
913 | * NOTE! Each of the iocb union members has the file pointer | |
914 | * as the first entry in their struct definition. So you can | |
915 | * access the file pointer through any of the sub-structs, | |
63c36549 | 916 | * or directly as just 'file' in this struct. |
09bb8394 | 917 | */ |
2b188cc1 | 918 | struct io_kiocb { |
221c5eb2 | 919 | union { |
09bb8394 | 920 | struct file *file; |
9adbd45d | 921 | struct io_rw rw; |
221c5eb2 | 922 | struct io_poll_iocb poll; |
9d805892 | 923 | struct io_poll_update poll_update; |
8ed8d3c3 JA |
924 | struct io_accept accept; |
925 | struct io_sync sync; | |
fbf23849 | 926 | struct io_cancel cancel; |
b29472ee | 927 | struct io_timeout timeout; |
0bdf7a2d | 928 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 929 | struct io_connect connect; |
e47293fd | 930 | struct io_sr_msg sr_msg; |
15b71abe | 931 | struct io_open open; |
b5dba59e | 932 | struct io_close close; |
269bbe5f | 933 | struct io_rsrc_update rsrc_update; |
4840e418 | 934 | struct io_fadvise fadvise; |
c1ca757b | 935 | struct io_madvise madvise; |
3e4827b0 | 936 | struct io_epoll epoll; |
7d67af2c | 937 | struct io_splice splice; |
ddf0322d | 938 | struct io_provide_buf pbuf; |
1d9e1288 | 939 | struct io_statx statx; |
36f4fa68 | 940 | struct io_shutdown shutdown; |
80a261fd | 941 | struct io_rename rename; |
14a1143b | 942 | struct io_unlink unlink; |
e34a02dc | 943 | struct io_mkdir mkdir; |
7a8721f8 | 944 | struct io_symlink symlink; |
cf30da90 | 945 | struct io_hardlink hardlink; |
4f57f06c | 946 | struct io_msg msg; |
221c5eb2 | 947 | }; |
2b188cc1 | 948 | |
d625c6ee | 949 | u8 opcode; |
65a6543d XW |
950 | /* polled IO has completed */ |
951 | u8 iopoll_completed; | |
4f4eeba8 | 952 | u16 buf_index; |
d17e56eb PB |
953 | unsigned int flags; |
954 | ||
cef216fc | 955 | struct io_cqe cqe; |
4f4eeba8 | 956 | |
010e8e6b | 957 | struct io_ring_ctx *ctx; |
010e8e6b | 958 | struct task_struct *task; |
d7718a9d | 959 | |
c1bdf8ed | 960 | struct io_rsrc_node *rsrc_node; |
d886e185 PB |
961 | /* store used ubuf, so we can prevent reloading */ |
962 | struct io_mapped_ubuf *imu; | |
fcb323cc | 963 | |
2804ecd8 JA |
964 | union { |
965 | /* used by request caches, completion batching and iopoll */ | |
966 | struct io_wq_work_node comp_list; | |
967 | /* cache ->apoll->events */ | |
968 | int apoll_events; | |
969 | }; | |
d17e56eb | 970 | atomic_t refs; |
521d61fc | 971 | atomic_t poll_refs; |
5b0a6acc | 972 | struct io_task_work io_task_work; |
010e8e6b PB |
973 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
974 | struct hlist_node hash_node; | |
7e3709d5 | 975 | /* internal polling, see IORING_FEAT_FAST_POLL */ |
010e8e6b | 976 | struct async_poll *apoll; |
d886e185 PB |
977 | /* opcode allocated if it needs to store data for async defer */ |
978 | void *async_data; | |
7e3709d5 | 979 | /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */ |
30d51dd4 | 980 | struct io_buffer *kbuf; |
41cdcc22 | 981 | /* linked requests, IFF REQ_F_HARDLINK or REQ_F_LINK are set */ |
34d2bfe7 | 982 | struct io_kiocb *link; |
41cdcc22 | 983 | /* custom credentials, valid IFF REQ_F_CREDS is set */ |
521d61fc JA |
984 | const struct cred *creds; |
985 | struct io_wq_work work; | |
2b188cc1 | 986 | }; |
05589553 | 987 | |
13bf43f5 PB |
988 | struct io_tctx_node { |
989 | struct list_head ctx_node; | |
990 | struct task_struct *task; | |
13bf43f5 PB |
991 | struct io_ring_ctx *ctx; |
992 | }; | |
993 | ||
27dc8338 PB |
994 | struct io_defer_entry { |
995 | struct list_head list; | |
996 | struct io_kiocb *req; | |
9cf7c104 | 997 | u32 seq; |
2b188cc1 JA |
998 | }; |
999 | ||
b21432b4 JA |
1000 | struct io_cancel_data { |
1001 | struct io_ring_ctx *ctx; | |
4bf94615 JA |
1002 | union { |
1003 | u64 data; | |
1004 | struct file *file; | |
1005 | }; | |
8e29da69 JA |
1006 | u32 flags; |
1007 | int seq; | |
b21432b4 JA |
1008 | }; |
1009 | ||
d3656344 | 1010 | struct io_op_def { |
d3656344 JA |
1011 | /* needs req->file assigned */ |
1012 | unsigned needs_file : 1; | |
6d63416d PB |
1013 | /* should block plug */ |
1014 | unsigned plug : 1; | |
d3656344 JA |
1015 | /* hash wq insertion if file is a regular file */ |
1016 | unsigned hash_reg_file : 1; | |
1017 | /* unbound wq insertion if file is a non-regular file */ | |
1018 | unsigned unbound_nonreg_file : 1; | |
8a72758c JA |
1019 | /* set if opcode supports polled "wait" */ |
1020 | unsigned pollin : 1; | |
1021 | unsigned pollout : 1; | |
52dd8640 | 1022 | unsigned poll_exclusive : 1; |
bcda7baa JA |
1023 | /* op supports buffer selection */ |
1024 | unsigned buffer_select : 1; | |
26f0505a PB |
1025 | /* do prep async if is going to be punted */ |
1026 | unsigned needs_async_setup : 1; | |
6d63416d PB |
1027 | /* opcode is not supported by this kernel */ |
1028 | unsigned not_supported : 1; | |
5bd2182d PM |
1029 | /* skip auditing */ |
1030 | unsigned audit_skip : 1; | |
e8c2bc1f JA |
1031 | /* size of async data needed, if any */ |
1032 | unsigned short async_size; | |
d3656344 JA |
1033 | }; |
1034 | ||
0918682b | 1035 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
1036 | [IORING_OP_NOP] = {}, |
1037 | [IORING_OP_READV] = { | |
d3656344 JA |
1038 | .needs_file = 1, |
1039 | .unbound_nonreg_file = 1, | |
8a72758c | 1040 | .pollin = 1, |
4d954c25 | 1041 | .buffer_select = 1, |
26f0505a | 1042 | .needs_async_setup = 1, |
27926b68 | 1043 | .plug = 1, |
5bd2182d | 1044 | .audit_skip = 1, |
e8c2bc1f | 1045 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 1046 | }, |
0463b6c5 | 1047 | [IORING_OP_WRITEV] = { |
d3656344 JA |
1048 | .needs_file = 1, |
1049 | .hash_reg_file = 1, | |
1050 | .unbound_nonreg_file = 1, | |
8a72758c | 1051 | .pollout = 1, |
26f0505a | 1052 | .needs_async_setup = 1, |
27926b68 | 1053 | .plug = 1, |
5bd2182d | 1054 | .audit_skip = 1, |
e8c2bc1f | 1055 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 1056 | }, |
0463b6c5 | 1057 | [IORING_OP_FSYNC] = { |
d3656344 | 1058 | .needs_file = 1, |
5bd2182d | 1059 | .audit_skip = 1, |
d3656344 | 1060 | }, |
0463b6c5 | 1061 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
1062 | .needs_file = 1, |
1063 | .unbound_nonreg_file = 1, | |
8a72758c | 1064 | .pollin = 1, |
27926b68 | 1065 | .plug = 1, |
5bd2182d | 1066 | .audit_skip = 1, |
e8c2bc1f | 1067 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 1068 | }, |
0463b6c5 | 1069 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
1070 | .needs_file = 1, |
1071 | .hash_reg_file = 1, | |
1072 | .unbound_nonreg_file = 1, | |
8a72758c | 1073 | .pollout = 1, |
27926b68 | 1074 | .plug = 1, |
5bd2182d | 1075 | .audit_skip = 1, |
e8c2bc1f | 1076 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 1077 | }, |
0463b6c5 | 1078 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
1079 | .needs_file = 1, |
1080 | .unbound_nonreg_file = 1, | |
5bd2182d PM |
1081 | .audit_skip = 1, |
1082 | }, | |
1083 | [IORING_OP_POLL_REMOVE] = { | |
1084 | .audit_skip = 1, | |
d3656344 | 1085 | }, |
0463b6c5 | 1086 | [IORING_OP_SYNC_FILE_RANGE] = { |
d3656344 | 1087 | .needs_file = 1, |
5bd2182d | 1088 | .audit_skip = 1, |
d3656344 | 1089 | }, |
0463b6c5 | 1090 | [IORING_OP_SENDMSG] = { |
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_msghdr), |
d3656344 | 1096 | }, |
0463b6c5 | 1097 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
1098 | .needs_file = 1, |
1099 | .unbound_nonreg_file = 1, | |
8a72758c | 1100 | .pollin = 1, |
52de1fe1 | 1101 | .buffer_select = 1, |
26f0505a | 1102 | .needs_async_setup = 1, |
e8c2bc1f | 1103 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 1104 | }, |
0463b6c5 | 1105 | [IORING_OP_TIMEOUT] = { |
5bd2182d | 1106 | .audit_skip = 1, |
e8c2bc1f | 1107 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 1108 | }, |
9c8e11b3 PB |
1109 | [IORING_OP_TIMEOUT_REMOVE] = { |
1110 | /* used by timeout updates' prep() */ | |
5bd2182d | 1111 | .audit_skip = 1, |
9c8e11b3 | 1112 | }, |
0463b6c5 | 1113 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
1114 | .needs_file = 1, |
1115 | .unbound_nonreg_file = 1, | |
8a72758c | 1116 | .pollin = 1, |
52dd8640 | 1117 | .poll_exclusive = 1, |
d3656344 | 1118 | }, |
5bd2182d PM |
1119 | [IORING_OP_ASYNC_CANCEL] = { |
1120 | .audit_skip = 1, | |
1121 | }, | |
0463b6c5 | 1122 | [IORING_OP_LINK_TIMEOUT] = { |
5bd2182d | 1123 | .audit_skip = 1, |
e8c2bc1f | 1124 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 1125 | }, |
0463b6c5 | 1126 | [IORING_OP_CONNECT] = { |
d3656344 JA |
1127 | .needs_file = 1, |
1128 | .unbound_nonreg_file = 1, | |
8a72758c | 1129 | .pollout = 1, |
26f0505a | 1130 | .needs_async_setup = 1, |
e8c2bc1f | 1131 | .async_size = sizeof(struct io_async_connect), |
d3656344 | 1132 | }, |
0463b6c5 | 1133 | [IORING_OP_FALLOCATE] = { |
d3656344 | 1134 | .needs_file = 1, |
d3656344 | 1135 | }, |
44526bed JA |
1136 | [IORING_OP_OPENAT] = {}, |
1137 | [IORING_OP_CLOSE] = {}, | |
5bd2182d PM |
1138 | [IORING_OP_FILES_UPDATE] = { |
1139 | .audit_skip = 1, | |
1140 | }, | |
1141 | [IORING_OP_STATX] = { | |
1142 | .audit_skip = 1, | |
1143 | }, | |
0463b6c5 | 1144 | [IORING_OP_READ] = { |
3a6820f2 JA |
1145 | .needs_file = 1, |
1146 | .unbound_nonreg_file = 1, | |
8a72758c | 1147 | .pollin = 1, |
bcda7baa | 1148 | .buffer_select = 1, |
27926b68 | 1149 | .plug = 1, |
5bd2182d | 1150 | .audit_skip = 1, |
e8c2bc1f | 1151 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1152 | }, |
0463b6c5 | 1153 | [IORING_OP_WRITE] = { |
3a6820f2 | 1154 | .needs_file = 1, |
7b3188e7 | 1155 | .hash_reg_file = 1, |
3a6820f2 | 1156 | .unbound_nonreg_file = 1, |
8a72758c | 1157 | .pollout = 1, |
27926b68 | 1158 | .plug = 1, |
5bd2182d | 1159 | .audit_skip = 1, |
e8c2bc1f | 1160 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1161 | }, |
0463b6c5 | 1162 | [IORING_OP_FADVISE] = { |
4840e418 | 1163 | .needs_file = 1, |
5bd2182d | 1164 | .audit_skip = 1, |
c1ca757b | 1165 | }, |
44526bed | 1166 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 1167 | [IORING_OP_SEND] = { |
fddaface JA |
1168 | .needs_file = 1, |
1169 | .unbound_nonreg_file = 1, | |
8a72758c | 1170 | .pollout = 1, |
5bd2182d | 1171 | .audit_skip = 1, |
fddaface | 1172 | }, |
0463b6c5 | 1173 | [IORING_OP_RECV] = { |
fddaface JA |
1174 | .needs_file = 1, |
1175 | .unbound_nonreg_file = 1, | |
8a72758c | 1176 | .pollin = 1, |
bcda7baa | 1177 | .buffer_select = 1, |
5bd2182d | 1178 | .audit_skip = 1, |
fddaface | 1179 | }, |
0463b6c5 | 1180 | [IORING_OP_OPENAT2] = { |
cebdb986 | 1181 | }, |
3e4827b0 JA |
1182 | [IORING_OP_EPOLL_CTL] = { |
1183 | .unbound_nonreg_file = 1, | |
5bd2182d | 1184 | .audit_skip = 1, |
3e4827b0 | 1185 | }, |
7d67af2c PB |
1186 | [IORING_OP_SPLICE] = { |
1187 | .needs_file = 1, | |
1188 | .hash_reg_file = 1, | |
1189 | .unbound_nonreg_file = 1, | |
5bd2182d PM |
1190 | .audit_skip = 1, |
1191 | }, | |
1192 | [IORING_OP_PROVIDE_BUFFERS] = { | |
1193 | .audit_skip = 1, | |
1194 | }, | |
1195 | [IORING_OP_REMOVE_BUFFERS] = { | |
1196 | .audit_skip = 1, | |
ddf0322d | 1197 | }, |
f2a8d5c7 PB |
1198 | [IORING_OP_TEE] = { |
1199 | .needs_file = 1, | |
1200 | .hash_reg_file = 1, | |
1201 | .unbound_nonreg_file = 1, | |
5bd2182d | 1202 | .audit_skip = 1, |
f2a8d5c7 | 1203 | }, |
36f4fa68 JA |
1204 | [IORING_OP_SHUTDOWN] = { |
1205 | .needs_file = 1, | |
1206 | }, | |
44526bed JA |
1207 | [IORING_OP_RENAMEAT] = {}, |
1208 | [IORING_OP_UNLINKAT] = {}, | |
e34a02dc | 1209 | [IORING_OP_MKDIRAT] = {}, |
7a8721f8 | 1210 | [IORING_OP_SYMLINKAT] = {}, |
cf30da90 | 1211 | [IORING_OP_LINKAT] = {}, |
4f57f06c JA |
1212 | [IORING_OP_MSG_RING] = { |
1213 | .needs_file = 1, | |
1214 | }, | |
d3656344 JA |
1215 | }; |
1216 | ||
0756a869 PB |
1217 | /* requests with any of those set should undergo io_disarm_next() */ |
1218 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) | |
da1a08c5 | 1219 | #define IO_REQ_LINK_FLAGS (REQ_F_LINK | REQ_F_HARDLINK) |
0756a869 | 1220 | |
7a612350 | 1221 | static bool io_disarm_next(struct io_kiocb *req); |
eef51daa | 1222 | static void io_uring_del_tctx_node(unsigned long index); |
9936c7c2 PB |
1223 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1224 | struct task_struct *task, | |
3dd0c97a | 1225 | bool cancel_all); |
78cc687b | 1226 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); |
1ffc5422 | 1227 | |
4e118cd9 | 1228 | static void __io_req_complete_post(struct io_kiocb *req, s32 res, u32 cflags); |
c7dae4ba | 1229 | static void io_dismantle_req(struct io_kiocb *req); |
94ae5e77 | 1230 | static void io_queue_linked_timeout(struct io_kiocb *req); |
fdecb662 | 1231 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 1232 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 | 1233 | unsigned nr_args); |
68fb8979 | 1234 | static void io_clean_op(struct io_kiocb *req); |
5106dd6e JA |
1235 | static inline struct file *io_file_get_fixed(struct io_kiocb *req, int fd, |
1236 | unsigned issue_flags); | |
1237 | static inline struct file *io_file_get_normal(struct io_kiocb *req, int fd); | |
d5361233 JA |
1238 | static void io_drop_inflight_file(struct io_kiocb *req); |
1239 | static bool io_assign_file(struct io_kiocb *req, unsigned int issue_flags); | |
cbc2e203 | 1240 | static void io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1241 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1242 | |
907d1df3 | 1243 | static void io_req_task_queue(struct io_kiocb *req); |
c450178d | 1244 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx); |
179ae0d1 | 1245 | static int io_req_prep_async(struct io_kiocb *req); |
de0617e4 | 1246 | |
b9445598 PB |
1247 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
1248 | unsigned int issue_flags, u32 slot_index); | |
7df778be PB |
1249 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags); |
1250 | ||
f1042b6c | 1251 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer); |
9aa8dfde | 1252 | static void io_eventfd_signal(struct io_ring_ctx *ctx); |
4e118cd9 | 1253 | static void io_req_tw_post_queue(struct io_kiocb *req, s32 res, u32 cflags); |
b9445598 | 1254 | |
2b188cc1 JA |
1255 | static struct kmem_cache *req_cachep; |
1256 | ||
0918682b | 1257 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1258 | |
1259 | struct sock *io_uring_get_socket(struct file *file) | |
1260 | { | |
1261 | #if defined(CONFIG_UNIX) | |
1262 | if (file->f_op == &io_uring_fops) { | |
1263 | struct io_ring_ctx *ctx = file->private_data; | |
1264 | ||
1265 | return ctx->ring_sock->sk; | |
1266 | } | |
1267 | #endif | |
1268 | return NULL; | |
1269 | } | |
1270 | EXPORT_SYMBOL(io_uring_get_socket); | |
1271 | ||
1f59bc0f PB |
1272 | #if defined(CONFIG_UNIX) |
1273 | static inline bool io_file_need_scm(struct file *filp) | |
1274 | { | |
5e45690a JA |
1275 | #if defined(IO_URING_SCM_ALL) |
1276 | return true; | |
1277 | #else | |
1f59bc0f | 1278 | return !!unix_get_socket(filp); |
5e45690a | 1279 | #endif |
1f59bc0f PB |
1280 | } |
1281 | #else | |
1282 | static inline bool io_file_need_scm(struct file *filp) | |
1283 | { | |
5e45690a | 1284 | return false; |
1f59bc0f PB |
1285 | } |
1286 | #endif | |
1287 | ||
f8929630 PB |
1288 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, unsigned issue_flags) |
1289 | { | |
1290 | lockdep_assert_held(&ctx->uring_lock); | |
1291 | if (issue_flags & IO_URING_F_UNLOCKED) | |
1292 | mutex_unlock(&ctx->uring_lock); | |
1293 | } | |
1294 | ||
1295 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, unsigned issue_flags) | |
1296 | { | |
1297 | /* | |
1298 | * "Normal" inline submissions always hold the uring_lock, since we | |
1299 | * grab it from the system call. Same is true for the SQPOLL offload. | |
1300 | * The only exception is when we've detached the request and issue it | |
1301 | * from an async worker thread, grab the lock for that case. | |
1302 | */ | |
1303 | if (issue_flags & IO_URING_F_UNLOCKED) | |
1304 | mutex_lock(&ctx->uring_lock); | |
1305 | lockdep_assert_held(&ctx->uring_lock); | |
1306 | } | |
1307 | ||
f237c30a PB |
1308 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
1309 | { | |
1310 | if (!*locked) { | |
1311 | mutex_lock(&ctx->uring_lock); | |
1312 | *locked = true; | |
1313 | } | |
1314 | } | |
1315 | ||
f2f87370 PB |
1316 | #define io_for_each_link(pos, head) \ |
1317 | for (pos = (head); pos; pos = pos->link) | |
1318 | ||
21c843d5 PB |
1319 | /* |
1320 | * Shamelessly stolen from the mm implementation of page reference checking, | |
1321 | * see commit f958d7b528b1 for details. | |
1322 | */ | |
1323 | #define req_ref_zero_or_close_to_overflow(req) \ | |
1324 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) | |
1325 | ||
1326 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) | |
1327 | { | |
20e60a38 | 1328 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1329 | return atomic_inc_not_zero(&req->refs); |
1330 | } | |
1331 | ||
21c843d5 PB |
1332 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
1333 | { | |
20e60a38 PB |
1334 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
1335 | return true; | |
1336 | ||
21c843d5 PB |
1337 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1338 | return atomic_dec_and_test(&req->refs); | |
1339 | } | |
1340 | ||
21c843d5 PB |
1341 | static inline void req_ref_get(struct io_kiocb *req) |
1342 | { | |
20e60a38 | 1343 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1344 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1345 | atomic_inc(&req->refs); | |
1346 | } | |
1347 | ||
c450178d PB |
1348 | static inline void io_submit_flush_completions(struct io_ring_ctx *ctx) |
1349 | { | |
6f33b0bc | 1350 | if (!wq_list_empty(&ctx->submit_state.compl_reqs)) |
c450178d PB |
1351 | __io_submit_flush_completions(ctx); |
1352 | } | |
1353 | ||
48dcd38d | 1354 | static inline void __io_req_set_refcount(struct io_kiocb *req, int nr) |
20e60a38 PB |
1355 | { |
1356 | if (!(req->flags & REQ_F_REFCOUNT)) { | |
1357 | req->flags |= REQ_F_REFCOUNT; | |
48dcd38d | 1358 | atomic_set(&req->refs, nr); |
20e60a38 PB |
1359 | } |
1360 | } | |
1361 | ||
48dcd38d PB |
1362 | static inline void io_req_set_refcount(struct io_kiocb *req) |
1363 | { | |
1364 | __io_req_set_refcount(req, 1); | |
1365 | } | |
1366 | ||
ab409402 PB |
1367 | #define IO_RSRC_REF_BATCH 100 |
1368 | ||
25a15d3c PB |
1369 | static void io_rsrc_put_node(struct io_rsrc_node *node, int nr) |
1370 | { | |
1371 | percpu_ref_put_many(&node->refs, nr); | |
1372 | } | |
1373 | ||
ab409402 PB |
1374 | static inline void io_req_put_rsrc_locked(struct io_kiocb *req, |
1375 | struct io_ring_ctx *ctx) | |
1376 | __must_hold(&ctx->uring_lock) | |
36f72fe2 | 1377 | { |
c1bdf8ed | 1378 | struct io_rsrc_node *node = req->rsrc_node; |
ab409402 | 1379 | |
c1bdf8ed PB |
1380 | if (node) { |
1381 | if (node == ctx->rsrc_node) | |
ab409402 PB |
1382 | ctx->rsrc_cached_refs++; |
1383 | else | |
25a15d3c | 1384 | io_rsrc_put_node(node, 1); |
ab409402 PB |
1385 | } |
1386 | } | |
1387 | ||
7ac1edc4 | 1388 | static inline void io_req_put_rsrc(struct io_kiocb *req) |
ab409402 | 1389 | { |
c1bdf8ed | 1390 | if (req->rsrc_node) |
25a15d3c | 1391 | io_rsrc_put_node(req->rsrc_node, 1); |
ab409402 PB |
1392 | } |
1393 | ||
1394 | static __cold void io_rsrc_refs_drop(struct io_ring_ctx *ctx) | |
1395 | __must_hold(&ctx->uring_lock) | |
1396 | { | |
1397 | if (ctx->rsrc_cached_refs) { | |
25a15d3c | 1398 | io_rsrc_put_node(ctx->rsrc_node, ctx->rsrc_cached_refs); |
ab409402 PB |
1399 | ctx->rsrc_cached_refs = 0; |
1400 | } | |
1401 | } | |
1402 | ||
1403 | static void io_rsrc_refs_refill(struct io_ring_ctx *ctx) | |
1404 | __must_hold(&ctx->uring_lock) | |
1405 | { | |
1406 | ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH; | |
1407 | percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH); | |
1408 | } | |
36f72fe2 | 1409 | |
a46be971 | 1410 | static inline void io_req_set_rsrc_node(struct io_kiocb *req, |
5106dd6e JA |
1411 | struct io_ring_ctx *ctx, |
1412 | unsigned int issue_flags) | |
36f72fe2 | 1413 | { |
c1bdf8ed PB |
1414 | if (!req->rsrc_node) { |
1415 | req->rsrc_node = ctx->rsrc_node; | |
5106dd6e JA |
1416 | |
1417 | if (!(issue_flags & IO_URING_F_UNLOCKED)) { | |
1418 | lockdep_assert_held(&ctx->uring_lock); | |
1419 | ctx->rsrc_cached_refs--; | |
1420 | if (unlikely(ctx->rsrc_cached_refs < 0)) | |
1421 | io_rsrc_refs_refill(ctx); | |
1422 | } else { | |
c1bdf8ed | 1423 | percpu_ref_get(&req->rsrc_node->refs); |
5106dd6e | 1424 | } |
36f72fe2 PB |
1425 | } |
1426 | } | |
1427 | ||
cc3cec83 | 1428 | static unsigned int __io_put_kbuf(struct io_kiocb *req, struct list_head *list) |
3648e526 | 1429 | { |
d1fd1c20 | 1430 | struct io_buffer *kbuf = req->kbuf; |
3648e526 HX |
1431 | unsigned int cflags; |
1432 | ||
cc3cec83 | 1433 | cflags = IORING_CQE_F_BUFFER | (kbuf->bid << IORING_CQE_BUFFER_SHIFT); |
3648e526 | 1434 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
cc3cec83 | 1435 | list_add(&kbuf->list, list); |
d1fd1c20 | 1436 | req->kbuf = NULL; |
3648e526 HX |
1437 | return cflags; |
1438 | } | |
1439 | ||
cc3cec83 | 1440 | static inline unsigned int io_put_kbuf_comp(struct io_kiocb *req) |
3648e526 | 1441 | { |
8197b053 PB |
1442 | lockdep_assert_held(&req->ctx->completion_lock); |
1443 | ||
3648e526 HX |
1444 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
1445 | return 0; | |
cc3cec83 JA |
1446 | return __io_put_kbuf(req, &req->ctx->io_buffers_comp); |
1447 | } | |
1448 | ||
1449 | static inline unsigned int io_put_kbuf(struct io_kiocb *req, | |
1450 | unsigned issue_flags) | |
1451 | { | |
1452 | unsigned int cflags; | |
1453 | ||
1454 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) | |
1455 | return 0; | |
1456 | ||
1457 | /* | |
1458 | * We can add this buffer back to two lists: | |
1459 | * | |
1460 | * 1) The io_buffers_cache list. This one is protected by the | |
1461 | * ctx->uring_lock. If we already hold this lock, add back to this | |
1462 | * list as we can grab it from issue as well. | |
1463 | * 2) The io_buffers_comp list. This one is protected by the | |
1464 | * ctx->completion_lock. | |
1465 | * | |
1466 | * We migrate buffers from the comp_list to the issue cache list | |
1467 | * when we need one. | |
1468 | */ | |
1469 | if (issue_flags & IO_URING_F_UNLOCKED) { | |
1470 | struct io_ring_ctx *ctx = req->ctx; | |
1471 | ||
1472 | spin_lock(&ctx->completion_lock); | |
1473 | cflags = __io_put_kbuf(req, &ctx->io_buffers_comp); | |
1474 | spin_unlock(&ctx->completion_lock); | |
1475 | } else { | |
ab0ac095 PB |
1476 | lockdep_assert_held(&req->ctx->uring_lock); |
1477 | ||
cc3cec83 JA |
1478 | cflags = __io_put_kbuf(req, &req->ctx->io_buffers_cache); |
1479 | } | |
1480 | ||
1481 | return cflags; | |
3648e526 HX |
1482 | } |
1483 | ||
dbc7d452 JA |
1484 | static struct io_buffer_list *io_buffer_get_list(struct io_ring_ctx *ctx, |
1485 | unsigned int bgid) | |
1486 | { | |
1487 | struct list_head *hash_list; | |
1488 | struct io_buffer_list *bl; | |
1489 | ||
1490 | hash_list = &ctx->io_buffers[hash_32(bgid, IO_BUFFERS_HASH_BITS)]; | |
1491 | list_for_each_entry(bl, hash_list, list) | |
1492 | if (bl->bgid == bgid || bgid == -1U) | |
1493 | return bl; | |
1494 | ||
1495 | return NULL; | |
1496 | } | |
1497 | ||
4d55f238 | 1498 | static void io_kbuf_recycle(struct io_kiocb *req, unsigned issue_flags) |
b1c62645 JA |
1499 | { |
1500 | struct io_ring_ctx *ctx = req->ctx; | |
dbc7d452 JA |
1501 | struct io_buffer_list *bl; |
1502 | struct io_buffer *buf; | |
b1c62645 JA |
1503 | |
1504 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) | |
1505 | return; | |
8a3e8ee5 JA |
1506 | /* don't recycle if we already did IO to this buffer */ |
1507 | if (req->flags & REQ_F_PARTIAL_IO) | |
1508 | return; | |
b1c62645 | 1509 | |
f8929630 | 1510 | io_ring_submit_lock(ctx, issue_flags); |
b1c62645 JA |
1511 | |
1512 | buf = req->kbuf; | |
dbc7d452 JA |
1513 | bl = io_buffer_get_list(ctx, buf->bgid); |
1514 | list_add(&buf->list, &bl->buf_list); | |
b1c62645 JA |
1515 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
1516 | req->kbuf = NULL; | |
4d55f238 | 1517 | |
f8929630 | 1518 | io_ring_submit_unlock(ctx, issue_flags); |
b1c62645 JA |
1519 | } |
1520 | ||
3dd0c97a PB |
1521 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
1522 | bool cancel_all) | |
6af3f48b | 1523 | __must_hold(&req->ctx->timeout_lock) |
08d23634 | 1524 | { |
68207680 | 1525 | if (task && head->task != task) |
08d23634 | 1526 | return false; |
d5361233 | 1527 | return cancel_all; |
6af3f48b PB |
1528 | } |
1529 | ||
1530 | /* | |
1531 | * As io_match_task() but protected against racing with linked timeouts. | |
1532 | * User must not hold timeout_lock. | |
1533 | */ | |
1534 | static bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task, | |
1535 | bool cancel_all) | |
1536 | { | |
6af3f48b PB |
1537 | if (task && head->task != task) |
1538 | return false; | |
d5361233 | 1539 | return cancel_all; |
6af3f48b PB |
1540 | } |
1541 | ||
d886e185 PB |
1542 | static inline bool req_has_async_data(struct io_kiocb *req) |
1543 | { | |
1544 | return req->flags & REQ_F_ASYNC_DATA; | |
1545 | } | |
1546 | ||
93d2bcd2 | 1547 | static inline void req_set_fail(struct io_kiocb *req) |
c40f6379 | 1548 | { |
93d2bcd2 | 1549 | req->flags |= REQ_F_FAIL; |
04c76b41 PB |
1550 | if (req->flags & REQ_F_CQE_SKIP) { |
1551 | req->flags &= ~REQ_F_CQE_SKIP; | |
1552 | req->flags |= REQ_F_SKIP_LINK_CQES; | |
1553 | } | |
c40f6379 | 1554 | } |
4a38aed2 | 1555 | |
a8295b98 HX |
1556 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
1557 | { | |
1558 | req_set_fail(req); | |
cef216fc | 1559 | req->cqe.res = res; |
a8295b98 HX |
1560 | } |
1561 | ||
fa05457a PB |
1562 | static inline void io_req_add_to_cache(struct io_kiocb *req, struct io_ring_ctx *ctx) |
1563 | { | |
1564 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); | |
1565 | } | |
1566 | ||
c072481d | 1567 | static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref) |
2b188cc1 JA |
1568 | { |
1569 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1570 | ||
0f158b4c | 1571 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1572 | } |
1573 | ||
8eb7e2d0 PB |
1574 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1575 | { | |
1576 | return !req->timeout.off; | |
1577 | } | |
1578 | ||
c072481d | 1579 | static __cold void io_fallback_req_func(struct work_struct *work) |
f56165e6 PB |
1580 | { |
1581 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, | |
1582 | fallback_work.work); | |
1583 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); | |
1584 | struct io_kiocb *req, *tmp; | |
f237c30a | 1585 | bool locked = false; |
f56165e6 PB |
1586 | |
1587 | percpu_ref_get(&ctx->refs); | |
1588 | llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node) | |
f237c30a | 1589 | req->io_task_work.func(req, &locked); |
5636c00d | 1590 | |
f237c30a | 1591 | if (locked) { |
c450178d | 1592 | io_submit_flush_completions(ctx); |
f237c30a PB |
1593 | mutex_unlock(&ctx->uring_lock); |
1594 | } | |
f56165e6 PB |
1595 | percpu_ref_put(&ctx->refs); |
1596 | } | |
1597 | ||
c072481d | 1598 | static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
2b188cc1 JA |
1599 | { |
1600 | struct io_ring_ctx *ctx; | |
dbc7d452 | 1601 | int i, hash_bits; |
2b188cc1 JA |
1602 | |
1603 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1604 | if (!ctx) | |
1605 | return NULL; | |
1606 | ||
78076bb6 JA |
1607 | /* |
1608 | * Use 5 bits less than the max cq entries, that should give us around | |
1609 | * 32 entries per hash list if totally full and uniformly spread. | |
1610 | */ | |
1611 | hash_bits = ilog2(p->cq_entries); | |
1612 | hash_bits -= 5; | |
1613 | if (hash_bits <= 0) | |
1614 | hash_bits = 1; | |
1615 | ctx->cancel_hash_bits = hash_bits; | |
1616 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1617 | GFP_KERNEL); | |
1618 | if (!ctx->cancel_hash) | |
1619 | goto err; | |
1620 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1621 | ||
6224843d PB |
1622 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
1623 | if (!ctx->dummy_ubuf) | |
1624 | goto err; | |
1625 | /* set invalid range, so io_import_fixed() fails meeting it */ | |
1626 | ctx->dummy_ubuf->ubuf = -1UL; | |
1627 | ||
dbc7d452 JA |
1628 | ctx->io_buffers = kcalloc(1U << IO_BUFFERS_HASH_BITS, |
1629 | sizeof(struct list_head), GFP_KERNEL); | |
1630 | if (!ctx->io_buffers) | |
1631 | goto err; | |
1632 | for (i = 0; i < (1U << IO_BUFFERS_HASH_BITS); i++) | |
1633 | INIT_LIST_HEAD(&ctx->io_buffers[i]); | |
1634 | ||
21482896 | 1635 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1636 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1637 | goto err; | |
2b188cc1 JA |
1638 | |
1639 | ctx->flags = p->flags; | |
90554200 | 1640 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1641 | INIT_LIST_HEAD(&ctx->sqd_list); |
1d7bb1d5 | 1642 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
cc3cec83 | 1643 | INIT_LIST_HEAD(&ctx->io_buffers_cache); |
4d9237e3 | 1644 | INIT_LIST_HEAD(&ctx->apoll_cache); |
0f158b4c | 1645 | init_completion(&ctx->ref_comp); |
61cf9370 | 1646 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
2b188cc1 | 1647 | mutex_init(&ctx->uring_lock); |
311997b3 | 1648 | init_waitqueue_head(&ctx->cq_wait); |
2b188cc1 | 1649 | spin_lock_init(&ctx->completion_lock); |
89850fce | 1650 | spin_lock_init(&ctx->timeout_lock); |
5eef4e87 | 1651 | INIT_WQ_LIST(&ctx->iopoll_list); |
cc3cec83 JA |
1652 | INIT_LIST_HEAD(&ctx->io_buffers_pages); |
1653 | INIT_LIST_HEAD(&ctx->io_buffers_comp); | |
de0617e4 | 1654 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1655 | INIT_LIST_HEAD(&ctx->timeout_list); |
ef9dd637 | 1656 | INIT_LIST_HEAD(&ctx->ltimeout_list); |
d67d2263 BM |
1657 | spin_lock_init(&ctx->rsrc_ref_lock); |
1658 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1659 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1660 | init_llist_head(&ctx->rsrc_put_llist); | |
13bf43f5 | 1661 | INIT_LIST_HEAD(&ctx->tctx_list); |
c2b6c6bc PB |
1662 | ctx->submit_state.free_list.next = NULL; |
1663 | INIT_WQ_LIST(&ctx->locked_free_list); | |
9011bf9a | 1664 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
6f33b0bc | 1665 | INIT_WQ_LIST(&ctx->submit_state.compl_reqs); |
2b188cc1 | 1666 | return ctx; |
206aefde | 1667 | err: |
6224843d | 1668 | kfree(ctx->dummy_ubuf); |
78076bb6 | 1669 | kfree(ctx->cancel_hash); |
dbc7d452 | 1670 | kfree(ctx->io_buffers); |
206aefde JA |
1671 | kfree(ctx); |
1672 | return NULL; | |
2b188cc1 JA |
1673 | } |
1674 | ||
8f6ed49a PB |
1675 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
1676 | { | |
1677 | struct io_rings *r = ctx->rings; | |
1678 | ||
1679 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); | |
1680 | ctx->cq_extra--; | |
1681 | } | |
1682 | ||
9cf7c104 | 1683 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1684 | { |
2bc9930e JA |
1685 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1686 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1687 | |
8f6ed49a | 1688 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
2bc9930e | 1689 | } |
de0617e4 | 1690 | |
9d858b21 | 1691 | return false; |
de0617e4 JA |
1692 | } |
1693 | ||
c97d8a0f PB |
1694 | static inline bool io_req_ffs_set(struct io_kiocb *req) |
1695 | { | |
35645ac3 | 1696 | return req->flags & REQ_F_FIXED_FILE; |
c97d8a0f PB |
1697 | } |
1698 | ||
fd08e530 PB |
1699 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
1700 | { | |
906c6caa PB |
1701 | if (WARN_ON_ONCE(!req->link)) |
1702 | return NULL; | |
1703 | ||
4d13d1a4 PB |
1704 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
1705 | req->flags |= REQ_F_LINK_TIMEOUT; | |
fd08e530 PB |
1706 | |
1707 | /* linked timeouts should have two refs once prep'ed */ | |
48dcd38d | 1708 | io_req_set_refcount(req); |
4d13d1a4 PB |
1709 | __io_req_set_refcount(req->link, 2); |
1710 | return req->link; | |
fd08e530 PB |
1711 | } |
1712 | ||
1713 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) | |
1714 | { | |
4d13d1a4 | 1715 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
fd08e530 PB |
1716 | return NULL; |
1717 | return __io_prep_linked_timeout(req); | |
1718 | } | |
1719 | ||
cb2d344c PB |
1720 | static noinline void __io_arm_ltimeout(struct io_kiocb *req) |
1721 | { | |
1722 | io_queue_linked_timeout(__io_prep_linked_timeout(req)); | |
1723 | } | |
1724 | ||
1725 | static inline void io_arm_ltimeout(struct io_kiocb *req) | |
1726 | { | |
1727 | if (unlikely(req->flags & REQ_F_ARM_LTIMEOUT)) | |
1728 | __io_arm_ltimeout(req); | |
1729 | } | |
1730 | ||
1e6fa521 JA |
1731 | static void io_prep_async_work(struct io_kiocb *req) |
1732 | { | |
1733 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1734 | struct io_ring_ctx *ctx = req->ctx; |
1735 | ||
b8e64b53 PB |
1736 | if (!(req->flags & REQ_F_CREDS)) { |
1737 | req->flags |= REQ_F_CREDS; | |
c10d1f98 | 1738 | req->creds = get_current_cred(); |
b8e64b53 | 1739 | } |
003e8dcc | 1740 | |
e1d675df PB |
1741 | req->work.list.next = NULL; |
1742 | req->work.flags = 0; | |
8e29da69 | 1743 | req->work.cancel_seq = atomic_read(&ctx->cancel_seq); |
feaadc4f PB |
1744 | if (req->flags & REQ_F_FORCE_ASYNC) |
1745 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1746 | ||
1e6fa521 JA |
1747 | if (req->flags & REQ_F_ISREG) { |
1748 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1749 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
4b982bd0 | 1750 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
1e6fa521 JA |
1751 | if (def->unbound_nonreg_file) |
1752 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1753 | } | |
561fb04a | 1754 | } |
cccf0ee8 | 1755 | |
cbdcb435 | 1756 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1757 | { |
cbdcb435 | 1758 | struct io_kiocb *cur; |
54a91f3b | 1759 | |
44eff40a PB |
1760 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
1761 | struct io_ring_ctx *ctx = req->ctx; | |
1762 | ||
674ee8e1 | 1763 | spin_lock_irq(&ctx->timeout_lock); |
44eff40a PB |
1764 | io_for_each_link(cur, req) |
1765 | io_prep_async_work(cur); | |
674ee8e1 | 1766 | spin_unlock_irq(&ctx->timeout_lock); |
44eff40a PB |
1767 | } else { |
1768 | io_for_each_link(cur, req) | |
1769 | io_prep_async_work(cur); | |
1770 | } | |
561fb04a JA |
1771 | } |
1772 | ||
fff4e40e PB |
1773 | static inline void io_req_add_compl_list(struct io_kiocb *req) |
1774 | { | |
775a1f2f | 1775 | struct io_submit_state *state = &req->ctx->submit_state; |
fff4e40e | 1776 | |
3d4aeb9f | 1777 | if (!(req->flags & REQ_F_CQE_SKIP)) |
775a1f2f | 1778 | state->flush_cqes = true; |
fff4e40e PB |
1779 | wq_list_add_tail(&req->comp_list, &state->compl_reqs); |
1780 | } | |
1781 | ||
77955efb | 1782 | static void io_queue_iowq(struct io_kiocb *req, bool *dont_use) |
561fb04a | 1783 | { |
cbdcb435 | 1784 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1785 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1786 | |
3bfe6106 JA |
1787 | BUG_ON(!tctx); |
1788 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1789 | |
cbdcb435 PB |
1790 | /* init ->work of the whole link before punting */ |
1791 | io_prep_async_link(req); | |
991468dc JA |
1792 | |
1793 | /* | |
1794 | * Not expected to happen, but if we do have a bug where this _can_ | |
1795 | * happen, catch it here and ensure the request is marked as | |
1796 | * canceled. That will make io-wq go through the usual work cancel | |
1797 | * procedure rather than attempt to run this request (or create a new | |
1798 | * worker for it). | |
1799 | */ | |
1800 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) | |
1801 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1802 | ||
971cf9c1 PB |
1803 | trace_io_uring_queue_async_work(req->ctx, req, req->cqe.user_data, |
1804 | req->opcode, req->flags, &req->work, | |
1805 | io_wq_is_hashed(&req->work)); | |
ebf93667 | 1806 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a JA |
1807 | if (link) |
1808 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1809 | } |
1810 | ||
1ee4160c | 1811 | static void io_kill_timeout(struct io_kiocb *req, int status) |
8c855885 | 1812 | __must_hold(&req->ctx->completion_lock) |
89850fce | 1813 | __must_hold(&req->ctx->timeout_lock) |
5262f567 | 1814 | { |
e8c2bc1f | 1815 | struct io_timeout_data *io = req->async_data; |
5262f567 | 1816 | |
fd9c7bc5 | 1817 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
2ae2eb9d PB |
1818 | if (status) |
1819 | req_set_fail(req); | |
01cec8c1 PB |
1820 | atomic_set(&req->ctx->cq_timeouts, |
1821 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1822 | list_del_init(&req->timeout.list); |
4e118cd9 | 1823 | io_req_tw_post_queue(req, status, 0); |
5262f567 JA |
1824 | } |
1825 | } | |
1826 | ||
c072481d | 1827 | static __cold void io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1828 | { |
441b8a78 | 1829 | while (!list_empty(&ctx->defer_list)) { |
27dc8338 PB |
1830 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1831 | struct io_defer_entry, list); | |
de0617e4 | 1832 | |
9cf7c104 | 1833 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1834 | break; |
27dc8338 | 1835 | list_del_init(&de->list); |
907d1df3 | 1836 | io_req_task_queue(de->req); |
27dc8338 | 1837 | kfree(de); |
441b8a78 | 1838 | } |
04518945 PB |
1839 | } |
1840 | ||
c072481d | 1841 | static __cold void io_flush_timeouts(struct io_ring_ctx *ctx) |
89850fce | 1842 | __must_hold(&ctx->completion_lock) |
de0617e4 | 1843 | { |
441b8a78 | 1844 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
e677edbc | 1845 | struct io_kiocb *req, *tmp; |
f010505b | 1846 | |
79ebeaee | 1847 | spin_lock_irq(&ctx->timeout_lock); |
e677edbc | 1848 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
f010505b | 1849 | u32 events_needed, events_got; |
de0617e4 | 1850 | |
8eb7e2d0 | 1851 | if (io_is_timeout_noseq(req)) |
360428f8 | 1852 | break; |
f010505b MDG |
1853 | |
1854 | /* | |
1855 | * Since seq can easily wrap around over time, subtract | |
1856 | * the last seq at which timeouts were flushed before comparing. | |
1857 | * Assuming not more than 2^31-1 events have happened since, | |
1858 | * these subtractions won't have wrapped, so we can check if | |
1859 | * target is in [last_seq, current_seq] by comparing the two. | |
1860 | */ | |
1861 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1862 | events_got = seq - ctx->cq_last_tm_flush; | |
1863 | if (events_got < events_needed) | |
360428f8 | 1864 | break; |
bfe68a22 | 1865 | |
1ee4160c | 1866 | io_kill_timeout(req, 0); |
f18ee4cf | 1867 | } |
f010505b | 1868 | ctx->cq_last_tm_flush = seq; |
79ebeaee | 1869 | spin_unlock_irq(&ctx->timeout_lock); |
360428f8 | 1870 | } |
5262f567 | 1871 | |
9333f6b4 PB |
1872 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) |
1873 | { | |
1874 | /* order cqe stores with ring update */ | |
1875 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
1876 | } | |
1877 | ||
9aa8dfde | 1878 | static void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
360428f8 | 1879 | { |
9aa8dfde PB |
1880 | if (ctx->off_timeout_used || ctx->drain_active) { |
1881 | spin_lock(&ctx->completion_lock); | |
1882 | if (ctx->off_timeout_used) | |
1883 | io_flush_timeouts(ctx); | |
1884 | if (ctx->drain_active) | |
1885 | io_queue_deferred(ctx); | |
1886 | io_commit_cqring(ctx); | |
1887 | spin_unlock(&ctx->completion_lock); | |
1888 | } | |
1889 | if (ctx->has_evfd) | |
1890 | io_eventfd_signal(ctx); | |
de0617e4 JA |
1891 | } |
1892 | ||
90554200 JA |
1893 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1894 | { | |
1895 | struct io_rings *r = ctx->rings; | |
1896 | ||
a566c556 | 1897 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries; |
90554200 JA |
1898 | } |
1899 | ||
888aae2e PB |
1900 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1901 | { | |
1902 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1903 | } | |
1904 | ||
d8da428b PB |
1905 | /* |
1906 | * writes to the cq entry need to come after reading head; the | |
1907 | * control dependency is enough as we're using WRITE_ONCE to | |
1908 | * fill the cq entry | |
1909 | */ | |
1910 | static noinline struct io_uring_cqe *__io_get_cqe(struct io_ring_ctx *ctx) | |
2b188cc1 | 1911 | { |
75b28aff | 1912 | struct io_rings *rings = ctx->rings; |
d8da428b PB |
1913 | unsigned int off = ctx->cached_cq_tail & (ctx->cq_entries - 1); |
1914 | unsigned int free, queued, len; | |
1915 | ||
1916 | /* userspace may cheat modifying the tail, be safe and do min */ | |
1917 | queued = min(__io_cqring_events(ctx), ctx->cq_entries); | |
1918 | free = ctx->cq_entries - queued; | |
1919 | /* we need a contiguous range, limit based on the current array offset */ | |
1920 | len = min(free, ctx->cq_entries - off); | |
1921 | if (!len) | |
2b188cc1 JA |
1922 | return NULL; |
1923 | ||
d8da428b PB |
1924 | ctx->cached_cq_tail++; |
1925 | ctx->cqe_cached = &rings->cqes[off]; | |
1926 | ctx->cqe_sentinel = ctx->cqe_cached + len; | |
1927 | return ctx->cqe_cached++; | |
1928 | } | |
1929 | ||
1930 | static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) | |
1931 | { | |
1932 | if (likely(ctx->cqe_cached < ctx->cqe_sentinel)) { | |
1933 | ctx->cached_cq_tail++; | |
1934 | return ctx->cqe_cached++; | |
1935 | } | |
1936 | return __io_get_cqe(ctx); | |
2b188cc1 JA |
1937 | } |
1938 | ||
77bc59b4 | 1939 | static void io_eventfd_signal(struct io_ring_ctx *ctx) |
f2842ab5 | 1940 | { |
77bc59b4 UA |
1941 | struct io_ev_fd *ev_fd; |
1942 | ||
77bc59b4 UA |
1943 | rcu_read_lock(); |
1944 | /* | |
1945 | * rcu_dereference ctx->io_ev_fd once and use it for both for checking | |
1946 | * and eventfd_signal | |
1947 | */ | |
1948 | ev_fd = rcu_dereference(ctx->io_ev_fd); | |
1949 | ||
1950 | /* | |
1951 | * Check again if ev_fd exists incase an io_eventfd_unregister call | |
1952 | * completed between the NULL check of ctx->io_ev_fd at the start of | |
1953 | * the function and rcu_read_lock. | |
1954 | */ | |
1955 | if (unlikely(!ev_fd)) | |
1956 | goto out; | |
7e55a19c | 1957 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
77bc59b4 UA |
1958 | goto out; |
1959 | ||
c75312dd | 1960 | if (!ev_fd->eventfd_async || io_wq_current_is_worker()) |
77bc59b4 | 1961 | eventfd_signal(ev_fd->cq_ev_fd, 1); |
77bc59b4 UA |
1962 | out: |
1963 | rcu_read_unlock(); | |
f2842ab5 JA |
1964 | } |
1965 | ||
9aa8dfde PB |
1966 | static inline void io_cqring_wake(struct io_ring_ctx *ctx) |
1967 | { | |
1968 | /* | |
1969 | * wake_up_all() may seem excessive, but io_wake_function() and | |
1970 | * io_should_wake() handle the termination of the loop and only | |
1971 | * wake as many waiters as we need to. | |
1972 | */ | |
1973 | if (wq_has_sleeper(&ctx->cq_wait)) | |
1974 | wake_up_all(&ctx->cq_wait); | |
1975 | } | |
1976 | ||
2c5d763c JA |
1977 | /* |
1978 | * This should only get called when at least one event has been posted. | |
1979 | * Some applications rely on the eventfd notification count only changing | |
1980 | * IFF a new CQE has been added to the CQ ring. There's no depedency on | |
1981 | * 1:1 relationship between how many times this function is called (and | |
1982 | * hence the eventfd count) and number of CQEs posted to the CQ ring. | |
1983 | */ | |
66fc25ca | 1984 | static inline void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1985 | { |
9aa8dfde PB |
1986 | if (unlikely(ctx->off_timeout_used || ctx->drain_active || |
1987 | ctx->has_evfd)) | |
9333f6b4 PB |
1988 | __io_commit_cqring_flush(ctx); |
1989 | ||
9aa8dfde | 1990 | io_cqring_wake(ctx); |
1d7bb1d5 JA |
1991 | } |
1992 | ||
80c18e4a PB |
1993 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1994 | { | |
9aa8dfde PB |
1995 | if (unlikely(ctx->off_timeout_used || ctx->drain_active || |
1996 | ctx->has_evfd)) | |
9333f6b4 PB |
1997 | __io_commit_cqring_flush(ctx); |
1998 | ||
9aa8dfde PB |
1999 | if (ctx->flags & IORING_SETUP_SQPOLL) |
2000 | io_cqring_wake(ctx); | |
80c18e4a PB |
2001 | } |
2002 | ||
c4a2ed72 | 2003 | /* Returns true if there are no backlogged entries after the flush */ |
6c2450ae | 2004 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
1d7bb1d5 | 2005 | { |
b18032bb | 2006 | bool all_flushed, posted; |
1d7bb1d5 | 2007 | |
a566c556 | 2008 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
e23de15f | 2009 | return false; |
1d7bb1d5 | 2010 | |
b18032bb | 2011 | posted = false; |
79ebeaee | 2012 | spin_lock(&ctx->completion_lock); |
6c2450ae | 2013 | while (!list_empty(&ctx->cq_overflow_list)) { |
d068b506 | 2014 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
6c2450ae | 2015 | struct io_overflow_cqe *ocqe; |
e6c8aa9a | 2016 | |
1d7bb1d5 JA |
2017 | if (!cqe && !force) |
2018 | break; | |
6c2450ae PB |
2019 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
2020 | struct io_overflow_cqe, list); | |
2021 | if (cqe) | |
2022 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); | |
2023 | else | |
8f6ed49a PB |
2024 | io_account_cq_overflow(ctx); |
2025 | ||
b18032bb | 2026 | posted = true; |
6c2450ae PB |
2027 | list_del(&ocqe->list); |
2028 | kfree(ocqe); | |
1d7bb1d5 JA |
2029 | } |
2030 | ||
09e88404 PB |
2031 | all_flushed = list_empty(&ctx->cq_overflow_list); |
2032 | if (all_flushed) { | |
10988a0a | 2033 | clear_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq); |
3a4b89a2 | 2034 | atomic_andnot(IORING_SQ_CQ_OVERFLOW, &ctx->rings->sq_flags); |
09e88404 | 2035 | } |
46930143 | 2036 | |
60053be8 | 2037 | io_commit_cqring(ctx); |
79ebeaee | 2038 | spin_unlock(&ctx->completion_lock); |
b18032bb JA |
2039 | if (posted) |
2040 | io_cqring_ev_posted(ctx); | |
09e88404 | 2041 | return all_flushed; |
1d7bb1d5 JA |
2042 | } |
2043 | ||
90f67366 | 2044 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
6c503150 | 2045 | { |
ca0a2651 JA |
2046 | bool ret = true; |
2047 | ||
10988a0a | 2048 | if (test_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq)) { |
6c503150 PB |
2049 | /* iopoll syncs against uring_lock, not completion_lock */ |
2050 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
2051 | mutex_lock(&ctx->uring_lock); | |
90f67366 | 2052 | ret = __io_cqring_overflow_flush(ctx, false); |
6c503150 PB |
2053 | if (ctx->flags & IORING_SETUP_IOPOLL) |
2054 | mutex_unlock(&ctx->uring_lock); | |
2055 | } | |
ca0a2651 JA |
2056 | |
2057 | return ret; | |
6c503150 PB |
2058 | } |
2059 | ||
9d170164 | 2060 | static void __io_put_task(struct task_struct *task, int nr) |
6a290a14 PB |
2061 | { |
2062 | struct io_uring_task *tctx = task->io_uring; | |
2063 | ||
9d170164 PB |
2064 | percpu_counter_sub(&tctx->inflight, nr); |
2065 | if (unlikely(atomic_read(&tctx->in_idle))) | |
2066 | wake_up(&tctx->wait); | |
2067 | put_task_struct_many(task, nr); | |
2068 | } | |
2069 | ||
2070 | /* must to be called somewhat shortly after putting a request */ | |
2071 | static inline void io_put_task(struct task_struct *task, int nr) | |
2072 | { | |
2073 | if (likely(task == current)) | |
2074 | task->io_uring->cached_refs += nr; | |
2075 | else | |
2076 | __io_put_task(task, nr); | |
6a290a14 PB |
2077 | } |
2078 | ||
9a10867a PB |
2079 | static void io_task_refs_refill(struct io_uring_task *tctx) |
2080 | { | |
2081 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; | |
2082 | ||
2083 | percpu_counter_add(&tctx->inflight, refill); | |
2084 | refcount_add(refill, ¤t->usage); | |
2085 | tctx->cached_refs += refill; | |
2086 | } | |
2087 | ||
2088 | static inline void io_get_task_refs(int nr) | |
2089 | { | |
2090 | struct io_uring_task *tctx = current->io_uring; | |
2091 | ||
2092 | tctx->cached_refs -= nr; | |
2093 | if (unlikely(tctx->cached_refs < 0)) | |
2094 | io_task_refs_refill(tctx); | |
2095 | } | |
2096 | ||
3cc7fdb9 PB |
2097 | static __cold void io_uring_drop_tctx_refs(struct task_struct *task) |
2098 | { | |
2099 | struct io_uring_task *tctx = task->io_uring; | |
2100 | unsigned int refs = tctx->cached_refs; | |
2101 | ||
2102 | if (refs) { | |
2103 | tctx->cached_refs = 0; | |
2104 | percpu_counter_sub(&tctx->inflight, refs); | |
2105 | put_task_struct_many(task, refs); | |
2106 | } | |
2107 | } | |
2108 | ||
d4d19c19 | 2109 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
54daa9b2 | 2110 | s32 res, u32 cflags) |
2b188cc1 | 2111 | { |
cce4b8b0 | 2112 | struct io_overflow_cqe *ocqe; |
2b188cc1 | 2113 | |
cce4b8b0 | 2114 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
08dcd028 | 2115 | trace_io_uring_cqe_overflow(ctx, user_data, res, cflags, ocqe); |
cce4b8b0 PB |
2116 | if (!ocqe) { |
2117 | /* | |
2118 | * If we're in ring overflow flush mode, or in task cancel mode, | |
2119 | * or cannot allocate an overflow entry, then we need to drop it | |
2120 | * on the floor. | |
2121 | */ | |
8f6ed49a | 2122 | io_account_cq_overflow(ctx); |
155bc950 | 2123 | set_bit(IO_CHECK_CQ_DROPPED_BIT, &ctx->check_cq); |
cce4b8b0 | 2124 | return false; |
2b188cc1 | 2125 | } |
cce4b8b0 | 2126 | if (list_empty(&ctx->cq_overflow_list)) { |
10988a0a | 2127 | set_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq); |
3a4b89a2 | 2128 | atomic_or(IORING_SQ_CQ_OVERFLOW, &ctx->rings->sq_flags); |
20c0b380 | 2129 | |
cce4b8b0 | 2130 | } |
d4d19c19 | 2131 | ocqe->cqe.user_data = user_data; |
cce4b8b0 PB |
2132 | ocqe->cqe.res = res; |
2133 | ocqe->cqe.flags = cflags; | |
2134 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); | |
2135 | return true; | |
2b188cc1 JA |
2136 | } |
2137 | ||
ae4da189 | 2138 | static inline bool __io_fill_cqe(struct io_ring_ctx *ctx, u64 user_data, |
913a571a | 2139 | s32 res, u32 cflags) |
2b188cc1 JA |
2140 | { |
2141 | struct io_uring_cqe *cqe; | |
2142 | ||
2143 | /* | |
2144 | * If we can't get a cq entry, userspace overflowed the | |
2145 | * submission (by quite a lot). Increment the overflow count in | |
2146 | * the ring. | |
2147 | */ | |
d068b506 | 2148 | cqe = io_get_cqe(ctx); |
1d7bb1d5 | 2149 | if (likely(cqe)) { |
d4d19c19 | 2150 | WRITE_ONCE(cqe->user_data, user_data); |
2b188cc1 | 2151 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 2152 | WRITE_ONCE(cqe->flags, cflags); |
8d13326e | 2153 | return true; |
2b188cc1 | 2154 | } |
d4d19c19 | 2155 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
2b188cc1 JA |
2156 | } |
2157 | ||
90e7c35f PB |
2158 | static inline bool __io_fill_cqe_req_filled(struct io_ring_ctx *ctx, |
2159 | struct io_kiocb *req) | |
2160 | { | |
2161 | struct io_uring_cqe *cqe; | |
2162 | ||
2163 | trace_io_uring_complete(req->ctx, req, req->cqe.user_data, | |
2164 | req->cqe.res, req->cqe.flags); | |
2165 | ||
2166 | /* | |
2167 | * If we can't get a cq entry, userspace overflowed the | |
2168 | * submission (by quite a lot). Increment the overflow count in | |
2169 | * the ring. | |
2170 | */ | |
2171 | cqe = io_get_cqe(ctx); | |
2172 | if (likely(cqe)) { | |
2173 | memcpy(cqe, &req->cqe, sizeof(*cqe)); | |
2174 | return true; | |
2175 | } | |
2176 | return io_cqring_event_overflow(ctx, req->cqe.user_data, | |
2177 | req->cqe.res, req->cqe.flags); | |
2178 | } | |
2179 | ||
ae4da189 | 2180 | static inline bool __io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags) |
d5ec1dfa | 2181 | { |
cef216fc PB |
2182 | trace_io_uring_complete(req->ctx, req, req->cqe.user_data, res, cflags); |
2183 | return __io_fill_cqe(req->ctx, req->cqe.user_data, res, cflags); | |
d5ec1dfa SR |
2184 | } |
2185 | ||
913a571a PB |
2186 | static noinline bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data, |
2187 | s32 res, u32 cflags) | |
bcda7baa | 2188 | { |
913a571a | 2189 | ctx->cq_extra++; |
502c87d6 | 2190 | trace_io_uring_complete(ctx, NULL, user_data, res, cflags); |
ae4da189 | 2191 | return __io_fill_cqe(ctx, user_data, res, cflags); |
bcda7baa JA |
2192 | } |
2193 | ||
a37fae8a HX |
2194 | static void __io_req_complete_post(struct io_kiocb *req, s32 res, |
2195 | u32 cflags) | |
2b188cc1 | 2196 | { |
78e19bbe | 2197 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 | 2198 | |
04c76b41 | 2199 | if (!(req->flags & REQ_F_CQE_SKIP)) |
ae4da189 | 2200 | __io_fill_cqe_req(req, res, cflags); |
c7dae4ba JA |
2201 | /* |
2202 | * If we're the last reference to this request, add to our locked | |
2203 | * free_list cache. | |
2204 | */ | |
de9b4cca | 2205 | if (req_ref_put_and_test(req)) { |
da1a08c5 | 2206 | if (req->flags & IO_REQ_LINK_FLAGS) { |
0756a869 | 2207 | if (req->flags & IO_DISARM_MASK) |
7a612350 PB |
2208 | io_disarm_next(req); |
2209 | if (req->link) { | |
2210 | io_req_task_queue(req->link); | |
2211 | req->link = NULL; | |
2212 | } | |
2213 | } | |
7ac1edc4 | 2214 | io_req_put_rsrc(req); |
8197b053 PB |
2215 | /* |
2216 | * Selected buffer deallocation in io_clean_op() assumes that | |
2217 | * we don't hold ->completion_lock. Clean them here to avoid | |
2218 | * deadlocks. | |
2219 | */ | |
2220 | io_put_kbuf_comp(req); | |
c7dae4ba JA |
2221 | io_dismantle_req(req); |
2222 | io_put_task(req->task, 1); | |
c2b6c6bc | 2223 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
d0acdee2 | 2224 | ctx->locked_free_nr++; |
180f829f | 2225 | } |
a37fae8a HX |
2226 | } |
2227 | ||
2228 | static void io_req_complete_post(struct io_kiocb *req, s32 res, | |
2229 | u32 cflags) | |
2230 | { | |
2231 | struct io_ring_ctx *ctx = req->ctx; | |
2232 | ||
2233 | spin_lock(&ctx->completion_lock); | |
2234 | __io_req_complete_post(req, res, cflags); | |
7a612350 | 2235 | io_commit_cqring(ctx); |
79ebeaee | 2236 | spin_unlock(&ctx->completion_lock); |
a3f34907 | 2237 | io_cqring_ev_posted(ctx); |
4e3d9ff9 JA |
2238 | } |
2239 | ||
54daa9b2 PB |
2240 | static inline void io_req_complete_state(struct io_kiocb *req, s32 res, |
2241 | u32 cflags) | |
229a7b63 | 2242 | { |
cef216fc PB |
2243 | req->cqe.res = res; |
2244 | req->cqe.flags = cflags; | |
e342c807 | 2245 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
2246 | } |
2247 | ||
889fca73 | 2248 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
54daa9b2 | 2249 | s32 res, u32 cflags) |
bcda7baa | 2250 | { |
889fca73 PB |
2251 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
2252 | io_req_complete_state(req, res, cflags); | |
a38d68db | 2253 | else |
c7dae4ba | 2254 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
2255 | } |
2256 | ||
54daa9b2 | 2257 | static inline void io_req_complete(struct io_kiocb *req, s32 res) |
0ddf92e8 | 2258 | { |
889fca73 | 2259 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
2260 | } |
2261 | ||
54daa9b2 | 2262 | static void io_req_complete_failed(struct io_kiocb *req, s32 res) |
f41db273 | 2263 | { |
93d2bcd2 | 2264 | req_set_fail(req); |
ab0ac095 | 2265 | io_req_complete_post(req, res, io_put_kbuf(req, IO_URING_F_UNLOCKED)); |
f41db273 PB |
2266 | } |
2267 | ||
864ea921 PB |
2268 | /* |
2269 | * Don't initialise the fields below on every allocation, but do that in | |
2270 | * advance and keep them valid across allocations. | |
2271 | */ | |
2272 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
2273 | { | |
2274 | req->ctx = ctx; | |
2275 | req->link = NULL; | |
2276 | req->async_data = NULL; | |
2277 | /* not necessary, but safer to zero */ | |
cef216fc | 2278 | req->cqe.res = 0; |
864ea921 PB |
2279 | } |
2280 | ||
dac7a098 | 2281 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
cd0ca2e0 | 2282 | struct io_submit_state *state) |
dac7a098 | 2283 | { |
79ebeaee | 2284 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 2285 | wq_list_splice(&ctx->locked_free_list, &state->free_list); |
d0acdee2 | 2286 | ctx->locked_free_nr = 0; |
79ebeaee | 2287 | spin_unlock(&ctx->completion_lock); |
dac7a098 PB |
2288 | } |
2289 | ||
88ab95be PB |
2290 | static inline bool io_req_cache_empty(struct io_ring_ctx *ctx) |
2291 | { | |
2292 | return !ctx->submit_state.free_list.next; | |
2293 | } | |
2294 | ||
5d5901a3 PB |
2295 | /* |
2296 | * A request might get retired back into the request caches even before opcode | |
2297 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. | |
2298 | * Because of that, io_alloc_req() should be called only under ->uring_lock | |
2299 | * and with extra caution to not get a request that is still worked on. | |
2300 | */ | |
c072481d | 2301 | static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx) |
5d5901a3 | 2302 | __must_hold(&ctx->uring_lock) |
2b188cc1 | 2303 | { |
864ea921 | 2304 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
3ab665b7 | 2305 | void *reqs[IO_REQ_ALLOC_BATCH]; |
864ea921 | 2306 | int ret, i; |
e5d1bc0a | 2307 | |
23a5c43b PB |
2308 | /* |
2309 | * If we have more than a batch's worth of requests in our IRQ side | |
2310 | * locked cache, grab the lock and move them over to our submission | |
2311 | * side cache. | |
2312 | */ | |
a6d97a8a | 2313 | if (data_race(ctx->locked_free_nr) > IO_COMPL_BATCH) { |
23a5c43b | 2314 | io_flush_cached_locked_reqs(ctx, &ctx->submit_state); |
88ab95be | 2315 | if (!io_req_cache_empty(ctx)) |
23a5c43b PB |
2316 | return true; |
2317 | } | |
e5d1bc0a | 2318 | |
3ab665b7 | 2319 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs); |
fd6fab2c | 2320 | |
864ea921 PB |
2321 | /* |
2322 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
2323 | * retry single alloc to be on the safe side. | |
2324 | */ | |
2325 | if (unlikely(ret <= 0)) { | |
3ab665b7 PB |
2326 | reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
2327 | if (!reqs[0]) | |
a33ae9ce | 2328 | return false; |
864ea921 | 2329 | ret = 1; |
2b188cc1 | 2330 | } |
864ea921 | 2331 | |
37f0e767 | 2332 | percpu_ref_get_many(&ctx->refs, ret); |
3ab665b7 | 2333 | for (i = 0; i < ret; i++) { |
23a5c43b | 2334 | struct io_kiocb *req = reqs[i]; |
3ab665b7 PB |
2335 | |
2336 | io_preinit_req(req, ctx); | |
fa05457a | 2337 | io_req_add_to_cache(req, ctx); |
3ab665b7 | 2338 | } |
a33ae9ce PB |
2339 | return true; |
2340 | } | |
2341 | ||
2342 | static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx) | |
2343 | { | |
88ab95be | 2344 | if (unlikely(io_req_cache_empty(ctx))) |
a33ae9ce PB |
2345 | return __io_alloc_req_refill(ctx); |
2346 | return true; | |
2347 | } | |
2348 | ||
2349 | static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) | |
2350 | { | |
2351 | struct io_wq_work_node *node; | |
2352 | ||
2353 | node = wq_stack_extract(&ctx->submit_state.free_list); | |
c2b6c6bc | 2354 | return container_of(node, struct io_kiocb, comp_list); |
2b188cc1 JA |
2355 | } |
2356 | ||
e1d767f0 | 2357 | static inline void io_put_file(struct file *file) |
8da11c19 | 2358 | { |
e1d767f0 | 2359 | if (file) |
8da11c19 PB |
2360 | fput(file); |
2361 | } | |
2362 | ||
6b639522 | 2363 | static inline void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 2364 | { |
094bae49 | 2365 | unsigned int flags = req->flags; |
929a3af9 | 2366 | |
867f8fa5 | 2367 | if (unlikely(flags & IO_REQ_CLEAN_FLAGS)) |
3a0a6902 | 2368 | io_clean_op(req); |
e1d767f0 PB |
2369 | if (!(flags & REQ_F_FIXED_FILE)) |
2370 | io_put_file(req->file); | |
e65ef56d JA |
2371 | } |
2372 | ||
f5c6cf2a | 2373 | static __cold void io_free_req(struct io_kiocb *req) |
c6ca97b3 | 2374 | { |
51a4cc11 | 2375 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 2376 | |
7ac1edc4 | 2377 | io_req_put_rsrc(req); |
216578e5 | 2378 | io_dismantle_req(req); |
7c660731 | 2379 | io_put_task(req->task, 1); |
c6ca97b3 | 2380 | |
79ebeaee | 2381 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 2382 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
c34b025f | 2383 | ctx->locked_free_nr++; |
79ebeaee | 2384 | spin_unlock(&ctx->completion_lock); |
e65ef56d JA |
2385 | } |
2386 | ||
f2f87370 PB |
2387 | static inline void io_remove_next_linked(struct io_kiocb *req) |
2388 | { | |
2389 | struct io_kiocb *nxt = req->link; | |
2390 | ||
2391 | req->link = nxt->link; | |
2392 | nxt->link = NULL; | |
2393 | } | |
2394 | ||
81ec803b | 2395 | static struct io_kiocb *io_disarm_linked_timeout(struct io_kiocb *req) |
33cc89a9 | 2396 | __must_hold(&req->ctx->completion_lock) |
89b263f6 | 2397 | __must_hold(&req->ctx->timeout_lock) |
2665abfd | 2398 | { |
33cc89a9 | 2399 | struct io_kiocb *link = req->link; |
f2f87370 | 2400 | |
b97e736a | 2401 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
c9abd7ad | 2402 | struct io_timeout_data *io = link->async_data; |
7c86ffee | 2403 | |
f2f87370 | 2404 | io_remove_next_linked(req); |
90cd7e42 | 2405 | link->timeout.head = NULL; |
fd9c7bc5 | 2406 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
ef9dd637 | 2407 | list_del(&link->timeout.list); |
81ec803b | 2408 | return link; |
c9abd7ad PB |
2409 | } |
2410 | } | |
81ec803b | 2411 | return NULL; |
7c86ffee PB |
2412 | } |
2413 | ||
d148ca4b | 2414 | static void io_fail_links(struct io_kiocb *req) |
33cc89a9 | 2415 | __must_hold(&req->ctx->completion_lock) |
9e645e11 | 2416 | { |
33cc89a9 | 2417 | struct io_kiocb *nxt, *link = req->link; |
04c76b41 | 2418 | bool ignore_cqes = req->flags & REQ_F_SKIP_LINK_CQES; |
9e645e11 | 2419 | |
f2f87370 | 2420 | req->link = NULL; |
f2f87370 | 2421 | while (link) { |
a8295b98 HX |
2422 | long res = -ECANCELED; |
2423 | ||
2424 | if (link->flags & REQ_F_FAIL) | |
cef216fc | 2425 | res = link->cqe.res; |
a8295b98 | 2426 | |
f2f87370 PB |
2427 | nxt = link->link; |
2428 | link->link = NULL; | |
2665abfd | 2429 | |
cef216fc | 2430 | trace_io_uring_fail_link(req->ctx, req, req->cqe.user_data, |
502c87d6 SR |
2431 | req->opcode, link); |
2432 | ||
4e118cd9 PB |
2433 | if (ignore_cqes) |
2434 | link->flags |= REQ_F_CQE_SKIP; | |
2435 | else | |
04c76b41 | 2436 | link->flags &= ~REQ_F_CQE_SKIP; |
4e118cd9 | 2437 | __io_req_complete_post(link, res, 0); |
f2f87370 | 2438 | link = nxt; |
9e645e11 | 2439 | } |
33cc89a9 | 2440 | } |
9e645e11 | 2441 | |
33cc89a9 PB |
2442 | static bool io_disarm_next(struct io_kiocb *req) |
2443 | __must_hold(&req->ctx->completion_lock) | |
2444 | { | |
81ec803b | 2445 | struct io_kiocb *link = NULL; |
33cc89a9 PB |
2446 | bool posted = false; |
2447 | ||
0756a869 | 2448 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
81ec803b | 2449 | link = req->link; |
906c6caa | 2450 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
0756a869 PB |
2451 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
2452 | io_remove_next_linked(req); | |
4e118cd9 | 2453 | io_req_tw_post_queue(link, -ECANCELED, 0); |
0756a869 PB |
2454 | posted = true; |
2455 | } | |
2456 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { | |
89b263f6 JA |
2457 | struct io_ring_ctx *ctx = req->ctx; |
2458 | ||
2459 | spin_lock_irq(&ctx->timeout_lock); | |
81ec803b | 2460 | link = io_disarm_linked_timeout(req); |
89b263f6 | 2461 | spin_unlock_irq(&ctx->timeout_lock); |
81ec803b PB |
2462 | if (link) { |
2463 | posted = true; | |
2464 | io_req_tw_post_queue(link, -ECANCELED, 0); | |
2465 | } | |
89b263f6 | 2466 | } |
93d2bcd2 | 2467 | if (unlikely((req->flags & REQ_F_FAIL) && |
e4335ed3 | 2468 | !(req->flags & REQ_F_HARDLINK))) { |
33cc89a9 PB |
2469 | posted |= (req->link != NULL); |
2470 | io_fail_links(req); | |
2471 | } | |
2472 | return posted; | |
9e645e11 JA |
2473 | } |
2474 | ||
d81499bf PB |
2475 | static void __io_req_find_next_prep(struct io_kiocb *req) |
2476 | { | |
2477 | struct io_ring_ctx *ctx = req->ctx; | |
2478 | bool posted; | |
2479 | ||
2480 | spin_lock(&ctx->completion_lock); | |
2481 | posted = io_disarm_next(req); | |
60053be8 | 2482 | io_commit_cqring(ctx); |
d81499bf PB |
2483 | spin_unlock(&ctx->completion_lock); |
2484 | if (posted) | |
2485 | io_cqring_ev_posted(ctx); | |
2486 | } | |
2487 | ||
2488 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) | |
c69f8dbe | 2489 | { |
33cc89a9 | 2490 | struct io_kiocb *nxt; |
944e58bf | 2491 | |
9e645e11 JA |
2492 | /* |
2493 | * If LINK is set, we have dependent requests in this chain. If we | |
2494 | * didn't fail this request, queue the first one up, moving any other | |
2495 | * dependencies to the next request. In case of failure, fail the rest | |
2496 | * of the chain. | |
2497 | */ | |
d81499bf PB |
2498 | if (unlikely(req->flags & IO_DISARM_MASK)) |
2499 | __io_req_find_next_prep(req); | |
33cc89a9 PB |
2500 | nxt = req->link; |
2501 | req->link = NULL; | |
2502 | return nxt; | |
4d7dd462 | 2503 | } |
9e645e11 | 2504 | |
f237c30a | 2505 | static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked) |
2c32395d PB |
2506 | { |
2507 | if (!ctx) | |
2508 | return; | |
ef060ea9 JA |
2509 | if (ctx->flags & IORING_SETUP_TASKRUN_FLAG) |
2510 | atomic_andnot(IORING_SQ_TASKRUN, &ctx->rings->sq_flags); | |
f237c30a | 2511 | if (*locked) { |
c450178d | 2512 | io_submit_flush_completions(ctx); |
2c32395d | 2513 | mutex_unlock(&ctx->uring_lock); |
f237c30a | 2514 | *locked = false; |
2c32395d PB |
2515 | } |
2516 | percpu_ref_put(&ctx->refs); | |
2517 | } | |
2518 | ||
f28c240e HX |
2519 | static inline void ctx_commit_and_unlock(struct io_ring_ctx *ctx) |
2520 | { | |
2521 | io_commit_cqring(ctx); | |
2522 | spin_unlock(&ctx->completion_lock); | |
2523 | io_cqring_ev_posted(ctx); | |
2524 | } | |
2525 | ||
2526 | static void handle_prev_tw_list(struct io_wq_work_node *node, | |
2527 | struct io_ring_ctx **ctx, bool *uring_locked) | |
2528 | { | |
2529 | if (*ctx && !*uring_locked) | |
2530 | spin_lock(&(*ctx)->completion_lock); | |
2531 | ||
2532 | do { | |
2533 | struct io_wq_work_node *next = node->next; | |
2534 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2535 | io_task_work.node); | |
2536 | ||
34d2bfe7 JA |
2537 | prefetch(container_of(next, struct io_kiocb, io_task_work.node)); |
2538 | ||
f28c240e HX |
2539 | if (req->ctx != *ctx) { |
2540 | if (unlikely(!*uring_locked && *ctx)) | |
2541 | ctx_commit_and_unlock(*ctx); | |
2542 | ||
2543 | ctx_flush_and_put(*ctx, uring_locked); | |
2544 | *ctx = req->ctx; | |
2545 | /* if not contended, grab and improve batching */ | |
2546 | *uring_locked = mutex_trylock(&(*ctx)->uring_lock); | |
2547 | percpu_ref_get(&(*ctx)->refs); | |
2548 | if (unlikely(!*uring_locked)) | |
2549 | spin_lock(&(*ctx)->completion_lock); | |
2550 | } | |
2551 | if (likely(*uring_locked)) | |
2552 | req->io_task_work.func(req, uring_locked); | |
2553 | else | |
cef216fc | 2554 | __io_req_complete_post(req, req->cqe.res, |
cc3cec83 | 2555 | io_put_kbuf_comp(req)); |
f28c240e HX |
2556 | node = next; |
2557 | } while (node); | |
2558 | ||
2559 | if (unlikely(!*uring_locked)) | |
2560 | ctx_commit_and_unlock(*ctx); | |
2561 | } | |
2562 | ||
2563 | static void handle_tw_list(struct io_wq_work_node *node, | |
2564 | struct io_ring_ctx **ctx, bool *locked) | |
9f8d032a HX |
2565 | { |
2566 | do { | |
2567 | struct io_wq_work_node *next = node->next; | |
2568 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2569 | io_task_work.node); | |
2570 | ||
34d2bfe7 JA |
2571 | prefetch(container_of(next, struct io_kiocb, io_task_work.node)); |
2572 | ||
9f8d032a HX |
2573 | if (req->ctx != *ctx) { |
2574 | ctx_flush_and_put(*ctx, locked); | |
2575 | *ctx = req->ctx; | |
2576 | /* if not contended, grab and improve batching */ | |
2577 | *locked = mutex_trylock(&(*ctx)->uring_lock); | |
2578 | percpu_ref_get(&(*ctx)->refs); | |
2579 | } | |
2580 | req->io_task_work.func(req, locked); | |
2581 | node = next; | |
2582 | } while (node); | |
2583 | } | |
2584 | ||
7cbf1722 | 2585 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 2586 | { |
f28c240e | 2587 | bool uring_locked = false; |
ebd0df2e | 2588 | struct io_ring_ctx *ctx = NULL; |
3f18407d PB |
2589 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
2590 | task_work); | |
c40f6379 | 2591 | |
16f72070 | 2592 | while (1) { |
f28c240e | 2593 | struct io_wq_work_node *node1, *node2; |
3f18407d PB |
2594 | |
2595 | spin_lock_irq(&tctx->task_lock); | |
f28c240e HX |
2596 | node1 = tctx->prior_task_list.first; |
2597 | node2 = tctx->task_list.first; | |
3f18407d | 2598 | INIT_WQ_LIST(&tctx->task_list); |
f28c240e HX |
2599 | INIT_WQ_LIST(&tctx->prior_task_list); |
2600 | if (!node2 && !node1) | |
6294f368 | 2601 | tctx->task_running = false; |
3f18407d | 2602 | spin_unlock_irq(&tctx->task_lock); |
f28c240e | 2603 | if (!node2 && !node1) |
6294f368 | 2604 | break; |
3f18407d | 2605 | |
f28c240e HX |
2606 | if (node1) |
2607 | handle_prev_tw_list(node1, &ctx, &uring_locked); | |
f28c240e HX |
2608 | if (node2) |
2609 | handle_tw_list(node2, &ctx, &uring_locked); | |
7cbf1722 | 2610 | cond_resched(); |
68ca8fc0 | 2611 | |
a6d97a8a PB |
2612 | if (data_race(!tctx->task_list.first) && |
2613 | data_race(!tctx->prior_task_list.first) && uring_locked) | |
68ca8fc0 | 2614 | io_submit_flush_completions(ctx); |
3f18407d | 2615 | } |
ebd0df2e | 2616 | |
f28c240e | 2617 | ctx_flush_and_put(ctx, &uring_locked); |
3cc7fdb9 PB |
2618 | |
2619 | /* relaxed read is enough as only the task itself sets ->in_idle */ | |
2620 | if (unlikely(atomic_read(&tctx->in_idle))) | |
2621 | io_uring_drop_tctx_refs(current); | |
7cbf1722 JA |
2622 | } |
2623 | ||
4813c377 | 2624 | static void io_req_task_work_add(struct io_kiocb *req, bool priority) |
7cbf1722 | 2625 | { |
c15b79de | 2626 | struct task_struct *tsk = req->task; |
9f010507 | 2627 | struct io_ring_ctx *ctx = req->ctx; |
7cbf1722 | 2628 | struct io_uring_task *tctx = tsk->io_uring; |
e09ee510 | 2629 | struct io_wq_work_node *node; |
0b81e80c | 2630 | unsigned long flags; |
6294f368 | 2631 | bool running; |
7cbf1722 JA |
2632 | |
2633 | WARN_ON_ONCE(!tctx); | |
2634 | ||
d5361233 JA |
2635 | io_drop_inflight_file(req); |
2636 | ||
0b81e80c | 2637 | spin_lock_irqsave(&tctx->task_lock, flags); |
4813c377 HX |
2638 | if (priority) |
2639 | wq_list_add_tail(&req->io_task_work.node, &tctx->prior_task_list); | |
2640 | else | |
2641 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); | |
6294f368 PB |
2642 | running = tctx->task_running; |
2643 | if (!running) | |
2644 | tctx->task_running = true; | |
0b81e80c | 2645 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2646 | |
2647 | /* task_work already pending, we're done */ | |
6294f368 | 2648 | if (running) |
e09ee510 | 2649 | return; |
7cbf1722 | 2650 | |
ef060ea9 JA |
2651 | if (ctx->flags & IORING_SETUP_TASKRUN_FLAG) |
2652 | atomic_or(IORING_SQ_TASKRUN, &ctx->rings->sq_flags); | |
2653 | ||
9f010507 | 2654 | if (likely(!task_work_add(tsk, &tctx->task_work, ctx->notify_method))) |
e09ee510 | 2655 | return; |
2215bed9 | 2656 | |
0b81e80c | 2657 | spin_lock_irqsave(&tctx->task_lock, flags); |
6294f368 | 2658 | tctx->task_running = false; |
4813c377 | 2659 | node = wq_list_merge(&tctx->prior_task_list, &tctx->task_list); |
0b81e80c | 2660 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 | 2661 | |
e09ee510 PB |
2662 | while (node) { |
2663 | req = container_of(node, struct io_kiocb, io_task_work.node); | |
2664 | node = node->next; | |
2665 | if (llist_add(&req->io_task_work.fallback_node, | |
2666 | &req->ctx->fallback_llist)) | |
2667 | schedule_delayed_work(&req->ctx->fallback_work, 1); | |
2668 | } | |
eab30c4d PB |
2669 | } |
2670 | ||
4e118cd9 PB |
2671 | static void io_req_tw_post(struct io_kiocb *req, bool *locked) |
2672 | { | |
2673 | io_req_complete_post(req, req->cqe.res, req->cqe.flags); | |
2674 | } | |
2675 | ||
2676 | static void io_req_tw_post_queue(struct io_kiocb *req, s32 res, u32 cflags) | |
2677 | { | |
2678 | req->cqe.res = res; | |
2679 | req->cqe.flags = cflags; | |
2680 | req->io_task_work.func = io_req_tw_post; | |
2681 | io_req_task_work_add(req, false); | |
2682 | } | |
2683 | ||
f237c30a | 2684 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
c40f6379 | 2685 | { |
b18a1a45 | 2686 | /* not needed for normal modes, but SQPOLL depends on it */ |
971cf9c1 | 2687 | io_tw_lock(req->ctx, locked); |
cef216fc | 2688 | io_req_complete_failed(req, req->cqe.res); |
c40f6379 JA |
2689 | } |
2690 | ||
f237c30a | 2691 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
c40f6379 | 2692 | { |
971cf9c1 | 2693 | io_tw_lock(req->ctx, locked); |
316319e8 | 2694 | /* req->task == current here, checking PF_EXITING is safe */ |
af066f31 | 2695 | if (likely(!(req->task->flags & PF_EXITING))) |
cbc2e203 | 2696 | io_queue_sqe(req); |
81b6d05c | 2697 | else |
2593553a | 2698 | io_req_complete_failed(req, -EFAULT); |
c40f6379 JA |
2699 | } |
2700 | ||
2c4b8eb6 | 2701 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
c40f6379 | 2702 | { |
cef216fc | 2703 | req->cqe.res = ret; |
5b0a6acc | 2704 | req->io_task_work.func = io_req_task_cancel; |
4813c377 | 2705 | io_req_task_work_add(req, false); |
c40f6379 JA |
2706 | } |
2707 | ||
2c4b8eb6 | 2708 | static void io_req_task_queue(struct io_kiocb *req) |
a3df7698 | 2709 | { |
5b0a6acc | 2710 | req->io_task_work.func = io_req_task_submit; |
4813c377 | 2711 | io_req_task_work_add(req, false); |
a3df7698 PB |
2712 | } |
2713 | ||
773af691 JA |
2714 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
2715 | { | |
77955efb | 2716 | req->io_task_work.func = io_queue_iowq; |
4813c377 | 2717 | io_req_task_work_add(req, false); |
773af691 JA |
2718 | } |
2719 | ||
57859f4d | 2720 | static void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2721 | { |
57859f4d | 2722 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf | 2723 | |
57859f4d PB |
2724 | if (nxt) |
2725 | io_req_task_queue(nxt); | |
c69f8dbe JL |
2726 | } |
2727 | ||
3aa83bfb | 2728 | static void io_free_batch_list(struct io_ring_ctx *ctx, |
1cce17ac | 2729 | struct io_wq_work_node *node) |
3aa83bfb | 2730 | __must_hold(&ctx->uring_lock) |
5af1d13e | 2731 | { |
d4b7a5ef | 2732 | struct task_struct *task = NULL; |
37f0e767 | 2733 | int task_refs = 0; |
5af1d13e | 2734 | |
3aa83bfb PB |
2735 | do { |
2736 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2737 | comp_list); | |
2d6500d4 | 2738 | |
a538be5b PB |
2739 | if (unlikely(req->flags & IO_REQ_CLEAN_SLOW_FLAGS)) { |
2740 | if (req->flags & REQ_F_REFCOUNT) { | |
2741 | node = req->comp_list.next; | |
2742 | if (!req_ref_put_and_test(req)) | |
2743 | continue; | |
2744 | } | |
b605a7fa PB |
2745 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
2746 | struct async_poll *apoll = req->apoll; | |
2747 | ||
2748 | if (apoll->double_poll) | |
2749 | kfree(apoll->double_poll); | |
2750 | list_add(&apoll->poll.wait.entry, | |
2751 | &ctx->apoll_cache); | |
2752 | req->flags &= ~REQ_F_POLLED; | |
2753 | } | |
da1a08c5 | 2754 | if (req->flags & IO_REQ_LINK_FLAGS) |
57859f4d | 2755 | io_queue_next(req); |
a538be5b PB |
2756 | if (unlikely(req->flags & IO_REQ_CLEAN_FLAGS)) |
2757 | io_clean_op(req); | |
c1e53a69 | 2758 | } |
a538be5b PB |
2759 | if (!(req->flags & REQ_F_FIXED_FILE)) |
2760 | io_put_file(req->file); | |
2d6500d4 | 2761 | |
ab409402 | 2762 | io_req_put_rsrc_locked(req, ctx); |
5af1d13e | 2763 | |
d4b7a5ef PB |
2764 | if (req->task != task) { |
2765 | if (task) | |
2766 | io_put_task(task, task_refs); | |
2767 | task = req->task; | |
2768 | task_refs = 0; | |
2769 | } | |
2770 | task_refs++; | |
c1e53a69 | 2771 | node = req->comp_list.next; |
fa05457a | 2772 | io_req_add_to_cache(req, ctx); |
3aa83bfb | 2773 | } while (node); |
d4b7a5ef | 2774 | |
d4b7a5ef PB |
2775 | if (task) |
2776 | io_put_task(task, task_refs); | |
7a743e22 PB |
2777 | } |
2778 | ||
c450178d | 2779 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx) |
a141dd89 | 2780 | __must_hold(&ctx->uring_lock) |
905c172f | 2781 | { |
6f33b0bc | 2782 | struct io_wq_work_node *node, *prev; |
cd0ca2e0 | 2783 | struct io_submit_state *state = &ctx->submit_state; |
905c172f | 2784 | |
3d4aeb9f PB |
2785 | if (state->flush_cqes) { |
2786 | spin_lock(&ctx->completion_lock); | |
2787 | wq_list_for_each(node, prev, &state->compl_reqs) { | |
2788 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
6f33b0bc | 2789 | comp_list); |
5182ed2e | 2790 | |
3d4aeb9f | 2791 | if (!(req->flags & REQ_F_CQE_SKIP)) |
90e7c35f | 2792 | __io_fill_cqe_req_filled(ctx, req); |
3d4aeb9f PB |
2793 | } |
2794 | ||
2795 | io_commit_cqring(ctx); | |
2796 | spin_unlock(&ctx->completion_lock); | |
2797 | io_cqring_ev_posted(ctx); | |
2798 | state->flush_cqes = false; | |
905c172f | 2799 | } |
5182ed2e | 2800 | |
1cce17ac | 2801 | io_free_batch_list(ctx, state->compl_reqs.first); |
6f33b0bc | 2802 | INIT_WQ_LIST(&state->compl_reqs); |
7a743e22 PB |
2803 | } |
2804 | ||
ba816ad6 JA |
2805 | /* |
2806 | * Drop reference to request, return next in chain (if there is one) if this | |
2807 | * was the last reference to this request. | |
2808 | */ | |
0d85035a | 2809 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2810 | { |
9b5f7bd9 PB |
2811 | struct io_kiocb *nxt = NULL; |
2812 | ||
de9b4cca | 2813 | if (req_ref_put_and_test(req)) { |
da1a08c5 | 2814 | if (unlikely(req->flags & IO_REQ_LINK_FLAGS)) |
7819a1f6 | 2815 | nxt = io_req_find_next(req); |
f5c6cf2a | 2816 | io_free_req(req); |
2a44f467 | 2817 | } |
9b5f7bd9 | 2818 | return nxt; |
2b188cc1 JA |
2819 | } |
2820 | ||
0d85035a | 2821 | static inline void io_put_req(struct io_kiocb *req) |
e65ef56d | 2822 | { |
f5c6cf2a PB |
2823 | if (req_ref_put_and_test(req)) { |
2824 | io_queue_next(req); | |
e65ef56d | 2825 | io_free_req(req); |
f5c6cf2a | 2826 | } |
2b188cc1 JA |
2827 | } |
2828 | ||
6c503150 | 2829 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2830 | { |
2831 | /* See comment at the top of this file */ | |
2832 | smp_rmb(); | |
e23de15f | 2833 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2834 | } |
2835 | ||
fb5ccc98 PB |
2836 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2837 | { | |
2838 | struct io_rings *rings = ctx->rings; | |
2839 | ||
2840 | /* make sure SQ entry isn't read before tail */ | |
2841 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2842 | } | |
2843 | ||
4c6e277c JA |
2844 | static inline bool io_run_task_work(void) |
2845 | { | |
7f62d40d | 2846 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || task_work_pending(current)) { |
4c6e277c | 2847 | __set_current_state(TASK_RUNNING); |
7c5d8fa6 EB |
2848 | clear_notify_signal(); |
2849 | if (task_work_pending(current)) | |
2850 | task_work_run(); | |
4c6e277c JA |
2851 | return true; |
2852 | } | |
2853 | ||
2854 | return false; | |
bcda7baa JA |
2855 | } |
2856 | ||
5ba3c874 | 2857 | static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) |
def596e9 | 2858 | { |
5eef4e87 | 2859 | struct io_wq_work_node *pos, *start, *prev; |
d729cf9a | 2860 | unsigned int poll_flags = BLK_POLL_NOSLEEP; |
b688f11e | 2861 | DEFINE_IO_COMP_BATCH(iob); |
5ba3c874 | 2862 | int nr_events = 0; |
def596e9 JA |
2863 | |
2864 | /* | |
2865 | * Only spin for completions if we don't have multiple devices hanging | |
87a115fb | 2866 | * off our complete list. |
def596e9 | 2867 | */ |
87a115fb | 2868 | if (ctx->poll_multi_queue || force_nonspin) |
ef99b2d3 | 2869 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2870 | |
5eef4e87 PB |
2871 | wq_list_for_each(pos, start, &ctx->iopoll_list) { |
2872 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
9adbd45d | 2873 | struct kiocb *kiocb = &req->rw.kiocb; |
a2416e1e | 2874 | int ret; |
def596e9 JA |
2875 | |
2876 | /* | |
581f9810 BM |
2877 | * Move completed and retryable entries to our local lists. |
2878 | * If we find a request that requires polling, break out | |
2879 | * and complete those lists first, if we have entries there. | |
def596e9 | 2880 | */ |
e3f721e6 | 2881 | if (READ_ONCE(req->iopoll_completed)) |
def596e9 JA |
2882 | break; |
2883 | ||
b688f11e | 2884 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags); |
a2416e1e PB |
2885 | if (unlikely(ret < 0)) |
2886 | return ret; | |
2887 | else if (ret) | |
ef99b2d3 | 2888 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2889 | |
3aadc23e | 2890 | /* iopoll may have completed current req */ |
b688f11e JA |
2891 | if (!rq_list_empty(iob.req_list) || |
2892 | READ_ONCE(req->iopoll_completed)) | |
e3f721e6 | 2893 | break; |
def596e9 JA |
2894 | } |
2895 | ||
b688f11e JA |
2896 | if (!rq_list_empty(iob.req_list)) |
2897 | iob.complete(&iob); | |
5eef4e87 PB |
2898 | else if (!pos) |
2899 | return 0; | |
def596e9 | 2900 | |
5eef4e87 PB |
2901 | prev = start; |
2902 | wq_list_for_each_resume(pos, prev) { | |
2903 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
2904 | ||
b3fa03fd PB |
2905 | /* order with io_complete_rw_iopoll(), e.g. ->result updates */ |
2906 | if (!smp_load_acquire(&req->iopoll_completed)) | |
e3f721e6 | 2907 | break; |
c0713540 | 2908 | nr_events++; |
83a13a41 PB |
2909 | if (unlikely(req->flags & REQ_F_CQE_SKIP)) |
2910 | continue; | |
cef216fc | 2911 | __io_fill_cqe_req(req, req->cqe.res, io_put_kbuf(req, 0)); |
e3f721e6 | 2912 | } |
def596e9 | 2913 | |
f5ed3bcd PB |
2914 | if (unlikely(!nr_events)) |
2915 | return 0; | |
2916 | ||
2917 | io_commit_cqring(ctx); | |
2918 | io_cqring_ev_posted_iopoll(ctx); | |
1cce17ac | 2919 | pos = start ? start->next : ctx->iopoll_list.first; |
5eef4e87 | 2920 | wq_list_cut(&ctx->iopoll_list, prev, start); |
1cce17ac | 2921 | io_free_batch_list(ctx, pos); |
5ba3c874 | 2922 | return nr_events; |
def596e9 JA |
2923 | } |
2924 | ||
def596e9 JA |
2925 | /* |
2926 | * We can't just wait for polled events to come to us, we have to actively | |
2927 | * find and complete them. | |
2928 | */ | |
c072481d | 2929 | static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2930 | { |
2931 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2932 | return; | |
2933 | ||
2934 | mutex_lock(&ctx->uring_lock); | |
5eef4e87 | 2935 | while (!wq_list_empty(&ctx->iopoll_list)) { |
b2edc0a7 | 2936 | /* let it sleep and repeat later if can't complete a request */ |
5ba3c874 | 2937 | if (io_do_iopoll(ctx, true) == 0) |
b2edc0a7 | 2938 | break; |
08f5439f JA |
2939 | /* |
2940 | * Ensure we allow local-to-the-cpu processing to take place, | |
2941 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2942 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2943 | */ |
3fcee5a6 PB |
2944 | if (need_resched()) { |
2945 | mutex_unlock(&ctx->uring_lock); | |
2946 | cond_resched(); | |
2947 | mutex_lock(&ctx->uring_lock); | |
2948 | } | |
def596e9 JA |
2949 | } |
2950 | mutex_unlock(&ctx->uring_lock); | |
2951 | } | |
2952 | ||
7668b92a | 2953 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2954 | { |
7668b92a | 2955 | unsigned int nr_events = 0; |
e9979b36 | 2956 | int ret = 0; |
155bc950 | 2957 | unsigned long check_cq; |
500f9fba | 2958 | |
f39c8a5b PB |
2959 | /* |
2960 | * Don't enter poll loop if we already have events pending. | |
2961 | * If we do, we can potentially be spinning for commands that | |
2962 | * already triggered a CQE (eg in error). | |
2963 | */ | |
155bc950 DY |
2964 | check_cq = READ_ONCE(ctx->check_cq); |
2965 | if (check_cq & BIT(IO_CHECK_CQ_OVERFLOW_BIT)) | |
f39c8a5b PB |
2966 | __io_cqring_overflow_flush(ctx, false); |
2967 | if (io_cqring_events(ctx)) | |
d487b43c | 2968 | return 0; |
155bc950 DY |
2969 | |
2970 | /* | |
2971 | * Similarly do not spin if we have not informed the user of any | |
2972 | * dropped CQE. | |
2973 | */ | |
2974 | if (unlikely(check_cq & BIT(IO_CHECK_CQ_DROPPED_BIT))) | |
2975 | return -EBADR; | |
2976 | ||
def596e9 | 2977 | do { |
500f9fba JA |
2978 | /* |
2979 | * If a submit got punted to a workqueue, we can have the | |
2980 | * application entering polling for a command before it gets | |
2981 | * issued. That app will hold the uring_lock for the duration | |
2982 | * of the poll right here, so we need to take a breather every | |
2983 | * now and then to ensure that the issue has a chance to add | |
2984 | * the poll to the issued list. Otherwise we can spin here | |
2985 | * forever, while the workqueue is stuck trying to acquire the | |
2986 | * very same mutex. | |
2987 | */ | |
5eef4e87 | 2988 | if (wq_list_empty(&ctx->iopoll_list)) { |
8f487ef2 PB |
2989 | u32 tail = ctx->cached_cq_tail; |
2990 | ||
500f9fba | 2991 | mutex_unlock(&ctx->uring_lock); |
4c6e277c | 2992 | io_run_task_work(); |
500f9fba | 2993 | mutex_lock(&ctx->uring_lock); |
def596e9 | 2994 | |
8f487ef2 PB |
2995 | /* some requests don't go through iopoll_list */ |
2996 | if (tail != ctx->cached_cq_tail || | |
5eef4e87 | 2997 | wq_list_empty(&ctx->iopoll_list)) |
e9979b36 | 2998 | break; |
500f9fba | 2999 | } |
5ba3c874 PB |
3000 | ret = io_do_iopoll(ctx, !min); |
3001 | if (ret < 0) | |
3002 | break; | |
3003 | nr_events += ret; | |
3004 | ret = 0; | |
3005 | } while (nr_events < min && !need_resched()); | |
d487b43c | 3006 | |
def596e9 JA |
3007 | return ret; |
3008 | } | |
3009 | ||
491381ce | 3010 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 3011 | { |
491381ce JA |
3012 | /* |
3013 | * Tell lockdep we inherited freeze protection from submission | |
3014 | * thread. | |
3015 | */ | |
3016 | if (req->flags & REQ_F_ISREG) { | |
1c98679d | 3017 | struct super_block *sb = file_inode(req->file)->i_sb; |
2b188cc1 | 3018 | |
1c98679d PB |
3019 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
3020 | sb_end_write(sb); | |
2b188cc1 JA |
3021 | } |
3022 | } | |
3023 | ||
b63534c4 | 3024 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 3025 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 | 3026 | { |
ab454438 | 3027 | struct io_async_rw *rw = req->async_data; |
b63534c4 | 3028 | |
d886e185 | 3029 | if (!req_has_async_data(req)) |
ab454438 | 3030 | return !io_req_prep_async(req); |
538941e2 | 3031 | iov_iter_restore(&rw->s.iter, &rw->s.iter_state); |
ab454438 | 3032 | return true; |
b63534c4 | 3033 | } |
b63534c4 | 3034 | |
3e6a0d3c | 3035 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 3036 | { |
355afaeb | 3037 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 3038 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 3039 | |
355afaeb JA |
3040 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
3041 | return false; | |
3e6a0d3c JA |
3042 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
3043 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 3044 | return false; |
7c977a58 JA |
3045 | /* |
3046 | * If ref is dying, we might be running poll reap from the exit work. | |
3047 | * Don't attempt to reissue from that path, just let it fail with | |
3048 | * -EAGAIN. | |
3049 | */ | |
3e6a0d3c JA |
3050 | if (percpu_ref_is_dying(&ctx->refs)) |
3051 | return false; | |
ef046888 JA |
3052 | /* |
3053 | * Play it safe and assume not safe to re-import and reissue if we're | |
3054 | * not in the original thread group (or in task context). | |
3055 | */ | |
3056 | if (!same_thread_group(req->task, current) || !in_task()) | |
3057 | return false; | |
3e6a0d3c JA |
3058 | return true; |
3059 | } | |
e82ad485 | 3060 | #else |
a1ff1e3f | 3061 | static bool io_resubmit_prep(struct io_kiocb *req) |
e82ad485 JA |
3062 | { |
3063 | return false; | |
3064 | } | |
e82ad485 | 3065 | static bool io_rw_should_reissue(struct io_kiocb *req) |
3e6a0d3c | 3066 | { |
b63534c4 JA |
3067 | return false; |
3068 | } | |
3e6a0d3c | 3069 | #endif |
b63534c4 | 3070 | |
8ef12efe | 3071 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
a1d7c393 | 3072 | { |
f63cf519 | 3073 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) { |
b65c128f | 3074 | kiocb_end_write(req); |
f63cf519 JA |
3075 | fsnotify_modify(req->file); |
3076 | } else { | |
3077 | fsnotify_access(req->file); | |
3078 | } | |
cef216fc | 3079 | if (unlikely(res != req->cqe.res)) { |
9532b99b PB |
3080 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && |
3081 | io_rw_should_reissue(req)) { | |
3082 | req->flags |= REQ_F_REISSUE; | |
8ef12efe | 3083 | return true; |
9532b99b | 3084 | } |
93d2bcd2 | 3085 | req_set_fail(req); |
cef216fc | 3086 | req->cqe.res = res; |
9532b99b | 3087 | } |
8ef12efe JA |
3088 | return false; |
3089 | } | |
3090 | ||
cc8e9ba7 | 3091 | static inline void io_req_task_complete(struct io_kiocb *req, bool *locked) |
8ef12efe | 3092 | { |
cef216fc | 3093 | int res = req->cqe.res; |
126180b9 PB |
3094 | |
3095 | if (*locked) { | |
cc3cec83 | 3096 | io_req_complete_state(req, res, io_put_kbuf(req, 0)); |
fff4e40e | 3097 | io_req_add_compl_list(req); |
126180b9 | 3098 | } else { |
cc3cec83 JA |
3099 | io_req_complete_post(req, res, |
3100 | io_put_kbuf(req, IO_URING_F_UNLOCKED)); | |
126180b9 | 3101 | } |
8ef12efe JA |
3102 | } |
3103 | ||
00f6e68b | 3104 | static void __io_complete_rw(struct io_kiocb *req, long res, |
8ef12efe JA |
3105 | unsigned int issue_flags) |
3106 | { | |
3107 | if (__io_complete_rw_common(req, res)) | |
3108 | return; | |
cef216fc | 3109 | __io_req_complete(req, issue_flags, req->cqe.res, |
cc3cec83 | 3110 | io_put_kbuf(req, issue_flags)); |
ba816ad6 JA |
3111 | } |
3112 | ||
6b19b766 | 3113 | static void io_complete_rw(struct kiocb *kiocb, long res) |
ba816ad6 | 3114 | { |
9adbd45d | 3115 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 3116 | |
8ef12efe JA |
3117 | if (__io_complete_rw_common(req, res)) |
3118 | return; | |
cef216fc | 3119 | req->cqe.res = res; |
8ef12efe | 3120 | req->io_task_work.func = io_req_task_complete; |
f28c240e | 3121 | io_req_task_work_add(req, !!(req->ctx->flags & IORING_SETUP_SQPOLL)); |
2b188cc1 JA |
3122 | } |
3123 | ||
6b19b766 | 3124 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res) |
def596e9 | 3125 | { |
9adbd45d | 3126 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 3127 | |
491381ce JA |
3128 | if (kiocb->ki_flags & IOCB_WRITE) |
3129 | kiocb_end_write(req); | |
cef216fc | 3130 | if (unlikely(res != req->cqe.res)) { |
b66ceaf3 PB |
3131 | if (res == -EAGAIN && io_rw_should_reissue(req)) { |
3132 | req->flags |= REQ_F_REISSUE; | |
3133 | return; | |
9532b99b | 3134 | } |
cef216fc | 3135 | req->cqe.res = res; |
8c130827 | 3136 | } |
bbde017a | 3137 | |
b3fa03fd PB |
3138 | /* order with io_iopoll_complete() checking ->iopoll_completed */ |
3139 | smp_store_release(&req->iopoll_completed, 1); | |
def596e9 JA |
3140 | } |
3141 | ||
3142 | /* | |
3143 | * After the iocb has been issued, it's safe to be found on the poll list. | |
3144 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
f39c8a5b | 3145 | * find it from a io_do_iopoll() thread before the issuer is done |
def596e9 JA |
3146 | * accessing the kiocb cookie. |
3147 | */ | |
9882131c | 3148 | static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags) |
def596e9 JA |
3149 | { |
3150 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 3151 | const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
cb3d8972 PB |
3152 | |
3153 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
3b44b371 | 3154 | if (unlikely(needs_lock)) |
cb3d8972 | 3155 | mutex_lock(&ctx->uring_lock); |
def596e9 JA |
3156 | |
3157 | /* | |
3158 | * Track whether we have multiple files in our lists. This will impact | |
3159 | * how we do polling eventually, not spinning if we're on potentially | |
3160 | * different devices. | |
3161 | */ | |
5eef4e87 | 3162 | if (wq_list_empty(&ctx->iopoll_list)) { |
915b3dde HX |
3163 | ctx->poll_multi_queue = false; |
3164 | } else if (!ctx->poll_multi_queue) { | |
def596e9 JA |
3165 | struct io_kiocb *list_req; |
3166 | ||
5eef4e87 PB |
3167 | list_req = container_of(ctx->iopoll_list.first, struct io_kiocb, |
3168 | comp_list); | |
30da1b45 | 3169 | if (list_req->file != req->file) |
915b3dde | 3170 | ctx->poll_multi_queue = true; |
def596e9 JA |
3171 | } |
3172 | ||
3173 | /* | |
3174 | * For fast devices, IO may have already completed. If it has, add | |
3175 | * it to the front so we find it first. | |
3176 | */ | |
65a6543d | 3177 | if (READ_ONCE(req->iopoll_completed)) |
5eef4e87 | 3178 | wq_list_add_head(&req->comp_list, &ctx->iopoll_list); |
def596e9 | 3179 | else |
5eef4e87 | 3180 | wq_list_add_tail(&req->comp_list, &ctx->iopoll_list); |
bdcd3eab | 3181 | |
3b44b371 | 3182 | if (unlikely(needs_lock)) { |
cb3d8972 PB |
3183 | /* |
3184 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle | |
3185 | * in sq thread task context or in io worker task context. If | |
3186 | * current task context is sq thread, we don't need to check | |
3187 | * whether should wake up sq thread. | |
3188 | */ | |
3189 | if ((ctx->flags & IORING_SETUP_SQPOLL) && | |
3190 | wq_has_sleeper(&ctx->sq_data->wait)) | |
3191 | wake_up(&ctx->sq_data->wait); | |
3192 | ||
3193 | mutex_unlock(&ctx->uring_lock); | |
3194 | } | |
def596e9 JA |
3195 | } |
3196 | ||
4503b767 JA |
3197 | static bool io_bdev_nowait(struct block_device *bdev) |
3198 | { | |
9ba0d0c8 | 3199 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
3200 | } |
3201 | ||
2b188cc1 JA |
3202 | /* |
3203 | * If we tracked the file through the SCM inflight mechanism, we could support | |
3204 | * any file. For now, just ensure that anything potentially problematic is done | |
3205 | * inline. | |
3206 | */ | |
88459b50 | 3207 | static bool __io_file_supports_nowait(struct file *file, umode_t mode) |
2b188cc1 | 3208 | { |
4503b767 | 3209 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
3210 | if (IS_ENABLED(CONFIG_BLOCK) && |
3211 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
3212 | return true; |
3213 | return false; | |
3214 | } | |
976517f1 | 3215 | if (S_ISSOCK(mode)) |
2b188cc1 | 3216 | return true; |
4503b767 | 3217 | if (S_ISREG(mode)) { |
4e7b5671 CH |
3218 | if (IS_ENABLED(CONFIG_BLOCK) && |
3219 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
3220 | file->f_op != &io_uring_fops) |
3221 | return true; | |
3222 | return false; | |
3223 | } | |
2b188cc1 | 3224 | |
c5b85625 JA |
3225 | /* any ->read/write should understand O_NONBLOCK */ |
3226 | if (file->f_flags & O_NONBLOCK) | |
3227 | return true; | |
35645ac3 | 3228 | return file->f_mode & FMODE_NOWAIT; |
2b188cc1 | 3229 | } |
c5b85625 | 3230 | |
88459b50 PB |
3231 | /* |
3232 | * If we tracked the file through the SCM inflight mechanism, we could support | |
3233 | * any file. For now, just ensure that anything potentially problematic is done | |
3234 | * inline. | |
3235 | */ | |
3236 | static unsigned int io_file_get_flags(struct file *file) | |
3237 | { | |
3238 | umode_t mode = file_inode(file)->i_mode; | |
3239 | unsigned int res = 0; | |
af197f50 | 3240 | |
88459b50 PB |
3241 | if (S_ISREG(mode)) |
3242 | res |= FFS_ISREG; | |
3243 | if (__io_file_supports_nowait(file, mode)) | |
3244 | res |= FFS_NOWAIT; | |
5e45690a JA |
3245 | if (io_file_need_scm(file)) |
3246 | res |= FFS_SCM; | |
88459b50 | 3247 | return res; |
2b188cc1 JA |
3248 | } |
3249 | ||
35645ac3 | 3250 | static inline bool io_file_supports_nowait(struct io_kiocb *req) |
7b29f92d | 3251 | { |
88459b50 | 3252 | return req->flags & REQ_F_SUPPORT_NOWAIT; |
7b29f92d JA |
3253 | } |
3254 | ||
b9a6b8f9 | 3255 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 3256 | { |
9adbd45d | 3257 | struct kiocb *kiocb = &req->rw.kiocb; |
09bb8394 JA |
3258 | unsigned ioprio; |
3259 | int ret; | |
2b188cc1 | 3260 | |
2b188cc1 | 3261 | kiocb->ki_pos = READ_ONCE(sqe->off); |
9adbd45d | 3262 | |
fb27274a PB |
3263 | ioprio = READ_ONCE(sqe->ioprio); |
3264 | if (ioprio) { | |
3265 | ret = ioprio_check_cap(ioprio); | |
3266 | if (ret) | |
3267 | return ret; | |
3268 | ||
3269 | kiocb->ki_ioprio = ioprio; | |
3270 | } else { | |
3271 | kiocb->ki_ioprio = get_current_ioprio(); | |
eae071c9 PB |
3272 | } |
3273 | ||
578c0ee2 | 3274 | req->imu = NULL; |
3529d8c2 JA |
3275 | req->rw.addr = READ_ONCE(sqe->addr); |
3276 | req->rw.len = READ_ONCE(sqe->len); | |
584b0180 | 3277 | req->rw.flags = READ_ONCE(sqe->rw_flags); |
4f4eeba8 | 3278 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 3279 | return 0; |
2b188cc1 JA |
3280 | } |
3281 | ||
3282 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
3283 | { | |
3284 | switch (ret) { | |
3285 | case -EIOCBQUEUED: | |
3286 | break; | |
3287 | case -ERESTARTSYS: | |
3288 | case -ERESTARTNOINTR: | |
3289 | case -ERESTARTNOHAND: | |
3290 | case -ERESTART_RESTARTBLOCK: | |
3291 | /* | |
3292 | * We can't just restart the syscall, since previously | |
3293 | * submitted sqes may already be in progress. Just fail this | |
3294 | * IO with EINTR. | |
3295 | */ | |
3296 | ret = -EINTR; | |
df561f66 | 3297 | fallthrough; |
2b188cc1 | 3298 | default: |
6b19b766 | 3299 | kiocb->ki_complete(kiocb, ret); |
2b188cc1 JA |
3300 | } |
3301 | } | |
3302 | ||
b4aec400 | 3303 | static inline loff_t *io_kiocb_update_pos(struct io_kiocb *req) |
d34e1e5b DY |
3304 | { |
3305 | struct kiocb *kiocb = &req->rw.kiocb; | |
3306 | ||
6f83ab22 JA |
3307 | if (kiocb->ki_pos != -1) |
3308 | return &kiocb->ki_pos; | |
3309 | ||
3310 | if (!(req->file->f_mode & FMODE_STREAM)) { | |
3311 | req->flags |= REQ_F_CUR_POS; | |
3312 | kiocb->ki_pos = req->file->f_pos; | |
3313 | return &kiocb->ki_pos; | |
d34e1e5b | 3314 | } |
6f83ab22 JA |
3315 | |
3316 | kiocb->ki_pos = 0; | |
3317 | return NULL; | |
d34e1e5b DY |
3318 | } |
3319 | ||
2ea537ca | 3320 | static void kiocb_done(struct io_kiocb *req, ssize_t ret, |
889fca73 | 3321 | unsigned int issue_flags) |
ba816ad6 | 3322 | { |
e8c2bc1f | 3323 | struct io_async_rw *io = req->async_data; |
ba04291e | 3324 | |
227c0c96 | 3325 | /* add previously done IO, if any */ |
d886e185 | 3326 | if (req_has_async_data(req) && io->bytes_done > 0) { |
227c0c96 | 3327 | if (ret < 0) |
e8c2bc1f | 3328 | ret = io->bytes_done; |
227c0c96 | 3329 | else |
e8c2bc1f | 3330 | ret += io->bytes_done; |
227c0c96 JA |
3331 | } |
3332 | ||
ba04291e | 3333 | if (req->flags & REQ_F_CUR_POS) |
2ea537ca PB |
3334 | req->file->f_pos = req->rw.kiocb.ki_pos; |
3335 | if (ret >= 0 && (req->rw.kiocb.ki_complete == io_complete_rw)) | |
00f6e68b | 3336 | __io_complete_rw(req, ret, issue_flags); |
ba816ad6 | 3337 | else |
2ea537ca | 3338 | io_rw_done(&req->rw.kiocb, ret); |
97284637 | 3339 | |
b66ceaf3 | 3340 | if (req->flags & REQ_F_REISSUE) { |
97284637 | 3341 | req->flags &= ~REQ_F_REISSUE; |
b91ef187 | 3342 | if (io_resubmit_prep(req)) |
773af691 | 3343 | io_req_task_queue_reissue(req); |
b91ef187 PB |
3344 | else |
3345 | io_req_task_queue_fail(req, ret); | |
97284637 | 3346 | } |
ba816ad6 JA |
3347 | } |
3348 | ||
eae071c9 PB |
3349 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
3350 | struct io_mapped_ubuf *imu) | |
edafccee | 3351 | { |
9adbd45d | 3352 | size_t len = req->rw.len; |
75769e3f | 3353 | u64 buf_end, buf_addr = req->rw.addr; |
edafccee | 3354 | size_t offset; |
edafccee | 3355 | |
75769e3f | 3356 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
edafccee JA |
3357 | return -EFAULT; |
3358 | /* not inside the mapped region */ | |
4751f53d | 3359 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
edafccee JA |
3360 | return -EFAULT; |
3361 | ||
3362 | /* | |
3363 | * May not be a start of buffer, set size appropriately | |
3364 | * and advance us to the beginning. | |
3365 | */ | |
3366 | offset = buf_addr - imu->ubuf; | |
3367 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
3368 | |
3369 | if (offset) { | |
3370 | /* | |
3371 | * Don't use iov_iter_advance() here, as it's really slow for | |
3372 | * using the latter parts of a big fixed buffer - it iterates | |
3373 | * over each segment manually. We can cheat a bit here, because | |
3374 | * we know that: | |
3375 | * | |
3376 | * 1) it's a BVEC iter, we set it up | |
3377 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
3378 | * first and last bvec | |
3379 | * | |
3380 | * So just find our index, and adjust the iterator afterwards. | |
3381 | * If the offset is within the first bvec (or the whole first | |
3382 | * bvec, just use iov_iter_advance(). This makes it easier | |
3383 | * since we can just skip the first segment, which may not | |
3384 | * be PAGE_SIZE aligned. | |
3385 | */ | |
3386 | const struct bio_vec *bvec = imu->bvec; | |
3387 | ||
3388 | if (offset <= bvec->bv_len) { | |
3389 | iov_iter_advance(iter, offset); | |
3390 | } else { | |
3391 | unsigned long seg_skip; | |
3392 | ||
3393 | /* skip first vec */ | |
3394 | offset -= bvec->bv_len; | |
3395 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
3396 | ||
3397 | iter->bvec = bvec + seg_skip; | |
3398 | iter->nr_segs -= seg_skip; | |
99c79f66 | 3399 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 3400 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
3401 | } |
3402 | } | |
3403 | ||
847595de | 3404 | return 0; |
edafccee JA |
3405 | } |
3406 | ||
5106dd6e JA |
3407 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
3408 | unsigned int issue_flags) | |
eae071c9 | 3409 | { |
eae071c9 PB |
3410 | struct io_mapped_ubuf *imu = req->imu; |
3411 | u16 index, buf_index = req->buf_index; | |
3412 | ||
3413 | if (likely(!imu)) { | |
578c0ee2 PB |
3414 | struct io_ring_ctx *ctx = req->ctx; |
3415 | ||
eae071c9 PB |
3416 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
3417 | return -EFAULT; | |
5106dd6e | 3418 | io_req_set_rsrc_node(req, ctx, issue_flags); |
eae071c9 PB |
3419 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
3420 | imu = READ_ONCE(ctx->user_bufs[index]); | |
3421 | req->imu = imu; | |
3422 | } | |
3423 | return __io_import_fixed(req, rw, iter, imu); | |
3424 | } | |
3425 | ||
dbc7d452 JA |
3426 | static void io_buffer_add_list(struct io_ring_ctx *ctx, |
3427 | struct io_buffer_list *bl, unsigned int bgid) | |
3428 | { | |
3429 | struct list_head *list; | |
3430 | ||
3431 | list = &ctx->io_buffers[hash_32(bgid, IO_BUFFERS_HASH_BITS)]; | |
3432 | INIT_LIST_HEAD(&bl->buf_list); | |
3433 | bl->bgid = bgid; | |
3434 | list_add(&bl->list, list); | |
3435 | } | |
3436 | ||
bcda7baa | 3437 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
51aac424 | 3438 | int bgid, unsigned int issue_flags) |
bcda7baa | 3439 | { |
30d51dd4 | 3440 | struct io_buffer *kbuf = req->kbuf; |
dbc7d452 JA |
3441 | struct io_ring_ctx *ctx = req->ctx; |
3442 | struct io_buffer_list *bl; | |
bcda7baa JA |
3443 | |
3444 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3445 | return kbuf; | |
3446 | ||
f8929630 | 3447 | io_ring_submit_lock(req->ctx, issue_flags); |
bcda7baa | 3448 | |
dbc7d452 JA |
3449 | bl = io_buffer_get_list(ctx, bgid); |
3450 | if (bl && !list_empty(&bl->buf_list)) { | |
3451 | kbuf = list_first_entry(&bl->buf_list, struct io_buffer, list); | |
3452 | list_del(&kbuf->list); | |
bcda7baa JA |
3453 | if (*len > kbuf->len) |
3454 | *len = kbuf->len; | |
30d51dd4 PB |
3455 | req->flags |= REQ_F_BUFFER_SELECTED; |
3456 | req->kbuf = kbuf; | |
bcda7baa JA |
3457 | } else { |
3458 | kbuf = ERR_PTR(-ENOBUFS); | |
3459 | } | |
3460 | ||
f8929630 | 3461 | io_ring_submit_unlock(req->ctx, issue_flags); |
bcda7baa JA |
3462 | return kbuf; |
3463 | } | |
3464 | ||
4d954c25 | 3465 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
51aac424 | 3466 | unsigned int issue_flags) |
4d954c25 JA |
3467 | { |
3468 | struct io_buffer *kbuf; | |
4f4eeba8 | 3469 | u16 bgid; |
4d954c25 | 3470 | |
4f4eeba8 | 3471 | bgid = req->buf_index; |
51aac424 | 3472 | kbuf = io_buffer_select(req, len, bgid, issue_flags); |
4d954c25 JA |
3473 | if (IS_ERR(kbuf)) |
3474 | return kbuf; | |
4d954c25 JA |
3475 | return u64_to_user_ptr(kbuf->addr); |
3476 | } | |
3477 | ||
3478 | #ifdef CONFIG_COMPAT | |
3479 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3480 | unsigned int issue_flags) |
4d954c25 JA |
3481 | { |
3482 | struct compat_iovec __user *uiov; | |
3483 | compat_ssize_t clen; | |
3484 | void __user *buf; | |
3485 | ssize_t len; | |
3486 | ||
3487 | uiov = u64_to_user_ptr(req->rw.addr); | |
3488 | if (!access_ok(uiov, sizeof(*uiov))) | |
3489 | return -EFAULT; | |
3490 | if (__get_user(clen, &uiov->iov_len)) | |
3491 | return -EFAULT; | |
3492 | if (clen < 0) | |
3493 | return -EINVAL; | |
3494 | ||
3495 | len = clen; | |
51aac424 | 3496 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3497 | if (IS_ERR(buf)) |
3498 | return PTR_ERR(buf); | |
3499 | iov[0].iov_base = buf; | |
3500 | iov[0].iov_len = (compat_size_t) len; | |
3501 | return 0; | |
3502 | } | |
3503 | #endif | |
3504 | ||
3505 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3506 | unsigned int issue_flags) |
4d954c25 JA |
3507 | { |
3508 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3509 | void __user *buf; | |
3510 | ssize_t len; | |
3511 | ||
3512 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3513 | return -EFAULT; | |
3514 | ||
3515 | len = iov[0].iov_len; | |
3516 | if (len < 0) | |
3517 | return -EINVAL; | |
51aac424 | 3518 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3519 | if (IS_ERR(buf)) |
3520 | return PTR_ERR(buf); | |
3521 | iov[0].iov_base = buf; | |
3522 | iov[0].iov_len = len; | |
3523 | return 0; | |
3524 | } | |
3525 | ||
3526 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3527 | unsigned int issue_flags) |
4d954c25 | 3528 | { |
dddb3e26 | 3529 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
30d51dd4 | 3530 | struct io_buffer *kbuf = req->kbuf; |
dddb3e26 | 3531 | |
dddb3e26 JA |
3532 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
3533 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3534 | return 0; |
dddb3e26 | 3535 | } |
dd201662 | 3536 | if (req->rw.len != 1) |
4d954c25 JA |
3537 | return -EINVAL; |
3538 | ||
3539 | #ifdef CONFIG_COMPAT | |
3540 | if (req->ctx->compat) | |
51aac424 | 3541 | return io_compat_import(req, iov, issue_flags); |
4d954c25 JA |
3542 | #endif |
3543 | ||
51aac424 | 3544 | return __io_iov_buffer_select(req, iov, issue_flags); |
4d954c25 JA |
3545 | } |
3546 | ||
caa8fe6e PB |
3547 | static struct iovec *__io_import_iovec(int rw, struct io_kiocb *req, |
3548 | struct io_rw_state *s, | |
3549 | unsigned int issue_flags) | |
2b188cc1 | 3550 | { |
5e49c973 | 3551 | struct iov_iter *iter = &s->iter; |
847595de | 3552 | u8 opcode = req->opcode; |
caa8fe6e | 3553 | struct iovec *iovec; |
d1d681b0 PB |
3554 | void __user *buf; |
3555 | size_t sqe_len; | |
4d954c25 | 3556 | ssize_t ret; |
edafccee | 3557 | |
f3251183 | 3558 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
5106dd6e | 3559 | ret = io_import_fixed(req, rw, iter, issue_flags); |
f3251183 PB |
3560 | if (ret) |
3561 | return ERR_PTR(ret); | |
3562 | return NULL; | |
3563 | } | |
2b188cc1 | 3564 | |
bcda7baa | 3565 | /* buffer index only valid with fixed read/write, or buffer select */ |
d1d681b0 | 3566 | if (unlikely(req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))) |
caa8fe6e | 3567 | return ERR_PTR(-EINVAL); |
9adbd45d | 3568 | |
d1d681b0 PB |
3569 | buf = u64_to_user_ptr(req->rw.addr); |
3570 | sqe_len = req->rw.len; | |
9adbd45d | 3571 | |
3a6820f2 | 3572 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3573 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 3574 | buf = io_rw_buffer_select(req, &sqe_len, issue_flags); |
867a23ea | 3575 | if (IS_ERR(buf)) |
898df244 | 3576 | return ERR_CAST(buf); |
3f9d6441 | 3577 | req->rw.len = sqe_len; |
bcda7baa JA |
3578 | } |
3579 | ||
5e49c973 | 3580 | ret = import_single_range(rw, buf, sqe_len, s->fast_iov, iter); |
f3251183 PB |
3581 | if (ret) |
3582 | return ERR_PTR(ret); | |
3583 | return NULL; | |
3a6820f2 JA |
3584 | } |
3585 | ||
caa8fe6e | 3586 | iovec = s->fast_iov; |
4d954c25 | 3587 | if (req->flags & REQ_F_BUFFER_SELECT) { |
caa8fe6e | 3588 | ret = io_iov_buffer_select(req, iovec, issue_flags); |
f3251183 PB |
3589 | if (ret) |
3590 | return ERR_PTR(ret); | |
3591 | iov_iter_init(iter, rw, iovec, 1, iovec->iov_len); | |
3592 | return NULL; | |
4d954c25 JA |
3593 | } |
3594 | ||
caa8fe6e | 3595 | ret = __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, &iovec, iter, |
89cd35c5 | 3596 | req->ctx->compat); |
caa8fe6e PB |
3597 | if (unlikely(ret < 0)) |
3598 | return ERR_PTR(ret); | |
3599 | return iovec; | |
2b188cc1 JA |
3600 | } |
3601 | ||
5e49c973 PB |
3602 | static inline int io_import_iovec(int rw, struct io_kiocb *req, |
3603 | struct iovec **iovec, struct io_rw_state *s, | |
3604 | unsigned int issue_flags) | |
3605 | { | |
caa8fe6e PB |
3606 | *iovec = __io_import_iovec(rw, req, s, issue_flags); |
3607 | if (unlikely(IS_ERR(*iovec))) | |
3608 | return PTR_ERR(*iovec); | |
5e49c973 | 3609 | |
5e49c973 | 3610 | iov_iter_save_state(&s->iter, &s->iter_state); |
caa8fe6e | 3611 | return 0; |
2b188cc1 JA |
3612 | } |
3613 | ||
0fef9483 JA |
3614 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3615 | { | |
5b09e37e | 3616 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3617 | } |
3618 | ||
31b51510 | 3619 | /* |
32960613 JA |
3620 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3621 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3622 | */ |
4017eb91 | 3623 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3624 | { |
4017eb91 JA |
3625 | struct kiocb *kiocb = &req->rw.kiocb; |
3626 | struct file *file = req->file; | |
32960613 | 3627 | ssize_t ret = 0; |
af9c45ec | 3628 | loff_t *ppos; |
32960613 JA |
3629 | |
3630 | /* | |
3631 | * Don't support polled IO through this interface, and we can't | |
3632 | * support non-blocking either. For the latter, this just causes | |
3633 | * the kiocb to be handled from an async context. | |
3634 | */ | |
3635 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3636 | return -EOPNOTSUPP; | |
35645ac3 PB |
3637 | if ((kiocb->ki_flags & IOCB_NOWAIT) && |
3638 | !(kiocb->ki_filp->f_flags & O_NONBLOCK)) | |
32960613 JA |
3639 | return -EAGAIN; |
3640 | ||
af9c45ec DY |
3641 | ppos = io_kiocb_ppos(kiocb); |
3642 | ||
32960613 | 3643 | while (iov_iter_count(iter)) { |
311ae9e1 | 3644 | struct iovec iovec; |
32960613 JA |
3645 | ssize_t nr; |
3646 | ||
311ae9e1 PB |
3647 | if (!iov_iter_is_bvec(iter)) { |
3648 | iovec = iov_iter_iovec(iter); | |
3649 | } else { | |
4017eb91 JA |
3650 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3651 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3652 | } |
3653 | ||
32960613 JA |
3654 | if (rw == READ) { |
3655 | nr = file->f_op->read(file, iovec.iov_base, | |
af9c45ec | 3656 | iovec.iov_len, ppos); |
32960613 JA |
3657 | } else { |
3658 | nr = file->f_op->write(file, iovec.iov_base, | |
af9c45ec | 3659 | iovec.iov_len, ppos); |
32960613 JA |
3660 | } |
3661 | ||
3662 | if (nr < 0) { | |
3663 | if (!ret) | |
3664 | ret = nr; | |
3665 | break; | |
3666 | } | |
5e929367 | 3667 | ret += nr; |
16c8d2df JA |
3668 | if (!iov_iter_is_bvec(iter)) { |
3669 | iov_iter_advance(iter, nr); | |
3670 | } else { | |
16c8d2df | 3671 | req->rw.addr += nr; |
5e929367 JA |
3672 | req->rw.len -= nr; |
3673 | if (!req->rw.len) | |
3674 | break; | |
16c8d2df | 3675 | } |
32960613 JA |
3676 | if (nr != iovec.iov_len) |
3677 | break; | |
32960613 JA |
3678 | } |
3679 | ||
3680 | return ret; | |
3681 | } | |
3682 | ||
ff6165b2 JA |
3683 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3684 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3685 | { |
e8c2bc1f | 3686 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3687 | |
538941e2 | 3688 | memcpy(&rw->s.iter, iter, sizeof(*iter)); |
afb87658 | 3689 | rw->free_iovec = iovec; |
227c0c96 | 3690 | rw->bytes_done = 0; |
ff6165b2 | 3691 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3692 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3693 | return; |
b64e3444 | 3694 | if (!iovec) { |
ff6165b2 JA |
3695 | unsigned iov_off = 0; |
3696 | ||
538941e2 | 3697 | rw->s.iter.iov = rw->s.fast_iov; |
ff6165b2 JA |
3698 | if (iter->iov != fast_iov) { |
3699 | iov_off = iter->iov - fast_iov; | |
538941e2 | 3700 | rw->s.iter.iov += iov_off; |
ff6165b2 | 3701 | } |
538941e2 PB |
3702 | if (rw->s.fast_iov != fast_iov) |
3703 | memcpy(rw->s.fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3704 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3705 | } else { |
3706 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3707 | } |
3708 | } | |
3709 | ||
8d4af685 | 3710 | static inline bool io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3711 | { |
e8c2bc1f JA |
3712 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3713 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
d886e185 PB |
3714 | if (req->async_data) { |
3715 | req->flags |= REQ_F_ASYNC_DATA; | |
3716 | return false; | |
3717 | } | |
3718 | return true; | |
3d9932a8 XW |
3719 | } |
3720 | ||
ff6165b2 | 3721 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
c88598a9 | 3722 | struct io_rw_state *s, bool force) |
b7bb4f7d | 3723 | { |
26f0505a | 3724 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
74566df3 | 3725 | return 0; |
d886e185 | 3726 | if (!req_has_async_data(req)) { |
cd658695 JA |
3727 | struct io_async_rw *iorw; |
3728 | ||
6cb78689 | 3729 | if (io_alloc_async_data(req)) { |
6bf985dc | 3730 | kfree(iovec); |
5d204bcf | 3731 | return -ENOMEM; |
6bf985dc | 3732 | } |
b7bb4f7d | 3733 | |
c88598a9 | 3734 | io_req_map_rw(req, iovec, s->fast_iov, &s->iter); |
cd658695 JA |
3735 | iorw = req->async_data; |
3736 | /* we've copied and mapped the iter, ensure state is saved */ | |
538941e2 | 3737 | iov_iter_save_state(&iorw->s.iter, &iorw->s.iter_state); |
5d204bcf | 3738 | } |
b7bb4f7d | 3739 | return 0; |
f67676d1 JA |
3740 | } |
3741 | ||
73debe68 | 3742 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3743 | { |
e8c2bc1f | 3744 | struct io_async_rw *iorw = req->async_data; |
5e49c973 | 3745 | struct iovec *iov; |
847595de | 3746 | int ret; |
c3e330a4 | 3747 | |
51aac424 | 3748 | /* submission path, ->uring_lock should already be taken */ |
3b44b371 | 3749 | ret = io_import_iovec(rw, req, &iov, &iorw->s, 0); |
c3e330a4 PB |
3750 | if (unlikely(ret < 0)) |
3751 | return ret; | |
3752 | ||
ab0b196c PB |
3753 | iorw->bytes_done = 0; |
3754 | iorw->free_iovec = iov; | |
3755 | if (iov) | |
3756 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3757 | return 0; |
3758 | } | |
3759 | ||
c1dd91d1 | 3760 | /* |
ffdc8dab | 3761 | * This is our waitqueue callback handler, registered through __folio_lock_async() |
c1dd91d1 JA |
3762 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
3763 | * This gets called when the page is unlocked, and we generally expect that to | |
3764 | * happen when the page IO is completed and the page is now uptodate. This will | |
3765 | * queue a task_work based retry of the operation, attempting to copy the data | |
3766 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3767 | * do a thread based blocking retry of the operation. That's the unexpected | |
3768 | * slow path. | |
3769 | */ | |
bcf5a063 JA |
3770 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3771 | int sync, void *arg) | |
3772 | { | |
3773 | struct wait_page_queue *wpq; | |
3774 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3775 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3776 | |
3777 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3778 | ||
cdc8fcb4 LT |
3779 | if (!wake_page_match(wpq, key)) |
3780 | return 0; | |
3781 | ||
c8d317aa | 3782 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 | 3783 | list_del_init(&wait->entry); |
921b9054 | 3784 | io_req_task_queue(req); |
bcf5a063 JA |
3785 | return 1; |
3786 | } | |
3787 | ||
c1dd91d1 JA |
3788 | /* |
3789 | * This controls whether a given IO request should be armed for async page | |
3790 | * based retry. If we return false here, the request is handed to the async | |
3791 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3792 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3793 | * will either succeed because the page is now uptodate and unlocked, or it | |
3794 | * will register a callback when the page is unlocked at IO completion. Through | |
3795 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3796 | * That retry will attempt the buffered read again. The retry will generally | |
3797 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3798 | * async worker threads for a blocking retry. | |
3799 | */ | |
227c0c96 | 3800 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3801 | { |
e8c2bc1f JA |
3802 | struct io_async_rw *rw = req->async_data; |
3803 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3804 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3805 | |
bcf5a063 JA |
3806 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3807 | if (req->flags & REQ_F_NOWAIT) | |
3808 | return false; | |
f67676d1 | 3809 | |
227c0c96 | 3810 | /* Only for buffered IO */ |
3b2a4439 | 3811 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3812 | return false; |
3b2a4439 | 3813 | |
bcf5a063 JA |
3814 | /* |
3815 | * just use poll if we can, and don't attempt if the fs doesn't | |
3816 | * support callback based unlocks | |
3817 | */ | |
3818 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3819 | return false; | |
f67676d1 | 3820 | |
3b2a4439 JA |
3821 | wait->wait.func = io_async_buf_func; |
3822 | wait->wait.private = req; | |
3823 | wait->wait.flags = 0; | |
3824 | INIT_LIST_HEAD(&wait->wait.entry); | |
3825 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3826 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3827 | kiocb->ki_waitq = wait; |
3b2a4439 | 3828 | return true; |
bcf5a063 JA |
3829 | } |
3830 | ||
aeab9506 | 3831 | static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
bcf5a063 | 3832 | { |
607b6fb8 | 3833 | if (likely(req->file->f_op->read_iter)) |
bcf5a063 | 3834 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
2dd2111d | 3835 | else if (req->file->f_op->read) |
4017eb91 | 3836 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3837 | else |
3838 | return -EINVAL; | |
f67676d1 JA |
3839 | } |
3840 | ||
7db30437 ML |
3841 | static bool need_read_all(struct io_kiocb *req) |
3842 | { | |
3843 | return req->flags & REQ_F_ISREG || | |
3844 | S_ISBLK(file_inode(req->file)->i_mode); | |
3845 | } | |
3846 | ||
584b0180 JA |
3847 | static int io_rw_init_file(struct io_kiocb *req, fmode_t mode) |
3848 | { | |
3849 | struct kiocb *kiocb = &req->rw.kiocb; | |
3850 | struct io_ring_ctx *ctx = req->ctx; | |
3851 | struct file *file = req->file; | |
3852 | int ret; | |
3853 | ||
3854 | if (unlikely(!file || !(file->f_mode & mode))) | |
3855 | return -EBADF; | |
3856 | ||
3857 | if (!io_req_ffs_set(req)) | |
3858 | req->flags |= io_file_get_flags(file) << REQ_F_SUPPORT_NOWAIT_BIT; | |
3859 | ||
3860 | kiocb->ki_flags = iocb_flags(file); | |
3861 | ret = kiocb_set_rw_flags(kiocb, req->rw.flags); | |
3862 | if (unlikely(ret)) | |
3863 | return ret; | |
3864 | ||
3865 | /* | |
3866 | * If the file is marked O_NONBLOCK, still allow retry for it if it | |
3867 | * supports async. Otherwise it's impossible to use O_NONBLOCK files | |
3868 | * reliably. If not, or it IOCB_NOWAIT is set, don't retry. | |
3869 | */ | |
3870 | if ((kiocb->ki_flags & IOCB_NOWAIT) || | |
3871 | ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req))) | |
3872 | req->flags |= REQ_F_NOWAIT; | |
3873 | ||
3874 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
3875 | if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll) | |
3876 | return -EOPNOTSUPP; | |
3877 | ||
3878 | kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE; | |
3879 | kiocb->ki_complete = io_complete_rw_iopoll; | |
3880 | req->iopoll_completed = 0; | |
3881 | } else { | |
3882 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3883 | return -EINVAL; | |
3884 | kiocb->ki_complete = io_complete_rw; | |
3885 | } | |
3886 | ||
3887 | return 0; | |
3888 | } | |
3889 | ||
889fca73 | 3890 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 3891 | { |
607b6fb8 | 3892 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 3893 | struct iovec *iovec; |
9adbd45d | 3894 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 3895 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
d886e185 | 3896 | struct io_async_rw *rw; |
cd658695 | 3897 | ssize_t ret, ret2; |
b4aec400 | 3898 | loff_t *ppos; |
ff6165b2 | 3899 | |
607b6fb8 PB |
3900 | if (!req_has_async_data(req)) { |
3901 | ret = io_import_iovec(READ, req, &iovec, s, issue_flags); | |
3902 | if (unlikely(ret < 0)) | |
3903 | return ret; | |
3904 | } else { | |
2be2eb02 JA |
3905 | /* |
3906 | * Safe and required to re-import if we're using provided | |
3907 | * buffers, as we dropped the selected one before retry. | |
3908 | */ | |
3909 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
3910 | ret = io_import_iovec(READ, req, &iovec, s, issue_flags); | |
3911 | if (unlikely(ret < 0)) | |
3912 | return ret; | |
3913 | } | |
3914 | ||
d886e185 | 3915 | rw = req->async_data; |
c88598a9 | 3916 | s = &rw->s; |
cd658695 JA |
3917 | /* |
3918 | * We come here from an earlier attempt, restore our state to | |
3919 | * match in case it doesn't. It's cheap enough that we don't | |
3920 | * need to make this conditional. | |
3921 | */ | |
c88598a9 | 3922 | iov_iter_restore(&s->iter, &s->iter_state); |
2846c481 | 3923 | iovec = NULL; |
2846c481 | 3924 | } |
584b0180 | 3925 | ret = io_rw_init_file(req, FMODE_READ); |
323b190b JA |
3926 | if (unlikely(ret)) { |
3927 | kfree(iovec); | |
584b0180 | 3928 | return ret; |
323b190b | 3929 | } |
cef216fc | 3930 | req->cqe.res = iov_iter_count(&s->iter); |
2b188cc1 | 3931 | |
607b6fb8 PB |
3932 | if (force_nonblock) { |
3933 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 3934 | if (unlikely(!io_file_supports_nowait(req))) { |
607b6fb8 PB |
3935 | ret = io_setup_async_rw(req, iovec, s, true); |
3936 | return ret ?: -EAGAIN; | |
3937 | } | |
a88fc400 | 3938 | kiocb->ki_flags |= IOCB_NOWAIT; |
607b6fb8 PB |
3939 | } else { |
3940 | /* Ensure we clear previously set non-block flag */ | |
3941 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
6713e7a6 | 3942 | } |
9e645e11 | 3943 | |
b4aec400 | 3944 | ppos = io_kiocb_update_pos(req); |
d34e1e5b | 3945 | |
cef216fc | 3946 | ret = rw_verify_area(READ, req->file, ppos, req->cqe.res); |
5ea5dd45 PB |
3947 | if (unlikely(ret)) { |
3948 | kfree(iovec); | |
3949 | return ret; | |
3950 | } | |
2b188cc1 | 3951 | |
c88598a9 | 3952 | ret = io_iter_do_read(req, &s->iter); |
32960613 | 3953 | |
230d50d4 | 3954 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
6ad7f233 | 3955 | req->flags &= ~REQ_F_REISSUE; |
9af177ee JA |
3956 | /* if we can poll, just do that */ |
3957 | if (req->opcode == IORING_OP_READ && file_can_poll(req->file)) | |
3958 | return -EAGAIN; | |
eefdf30f JA |
3959 | /* IOPOLL retry should happen for io-wq threads */ |
3960 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3961 | goto done; |
75c668cd PB |
3962 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3963 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3964 | goto done; |
f38c7e3a | 3965 | ret = 0; |
230d50d4 JA |
3966 | } else if (ret == -EIOCBQUEUED) { |
3967 | goto out_free; | |
cef216fc | 3968 | } else if (ret == req->cqe.res || ret <= 0 || !force_nonblock || |
7db30437 | 3969 | (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) { |
7335e3bf | 3970 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3971 | goto done; |
227c0c96 JA |
3972 | } |
3973 | ||
cd658695 JA |
3974 | /* |
3975 | * Don't depend on the iter state matching what was consumed, or being | |
3976 | * untouched in case of error. Restore it and we'll advance it | |
3977 | * manually if we need to. | |
3978 | */ | |
c88598a9 | 3979 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 3980 | |
c88598a9 | 3981 | ret2 = io_setup_async_rw(req, iovec, s, true); |
6bf985dc PB |
3982 | if (ret2) |
3983 | return ret2; | |
3984 | ||
fe1cdd55 | 3985 | iovec = NULL; |
e8c2bc1f | 3986 | rw = req->async_data; |
c88598a9 | 3987 | s = &rw->s; |
cd658695 JA |
3988 | /* |
3989 | * Now use our persistent iterator and state, if we aren't already. | |
3990 | * We've restored and mapped the iter to match. | |
3991 | */ | |
227c0c96 | 3992 | |
b23df91b | 3993 | do { |
cd658695 JA |
3994 | /* |
3995 | * We end up here because of a partial read, either from | |
3996 | * above or inside this loop. Advance the iter by the bytes | |
3997 | * that were consumed. | |
3998 | */ | |
c88598a9 PB |
3999 | iov_iter_advance(&s->iter, ret); |
4000 | if (!iov_iter_count(&s->iter)) | |
cd658695 | 4001 | break; |
b23df91b | 4002 | rw->bytes_done += ret; |
c88598a9 | 4003 | iov_iter_save_state(&s->iter, &s->iter_state); |
cd658695 | 4004 | |
b23df91b PB |
4005 | /* if we can retry, do so with the callbacks armed */ |
4006 | if (!io_rw_should_retry(req)) { | |
4007 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
4008 | return -EAGAIN; | |
4009 | } | |
4010 | ||
4011 | /* | |
4012 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
4013 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
4014 | * desired page gets unlocked. We can also get a partial read | |
4015 | * here, and if we do, then just retry at the new offset. | |
4016 | */ | |
c88598a9 | 4017 | ret = io_iter_do_read(req, &s->iter); |
b23df91b PB |
4018 | if (ret == -EIOCBQUEUED) |
4019 | return 0; | |
227c0c96 | 4020 | /* we got some bytes, but not all. retry. */ |
b5b0ecb7 | 4021 | kiocb->ki_flags &= ~IOCB_WAITQ; |
c88598a9 | 4022 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 4023 | } while (ret > 0); |
227c0c96 | 4024 | done: |
2ea537ca | 4025 | kiocb_done(req, ret, issue_flags); |
fe1cdd55 PB |
4026 | out_free: |
4027 | /* it's faster to check here then delegate to kfree */ | |
4028 | if (iovec) | |
4029 | kfree(iovec); | |
5ea5dd45 | 4030 | return 0; |
2b188cc1 JA |
4031 | } |
4032 | ||
889fca73 | 4033 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 4034 | { |
607b6fb8 | 4035 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 4036 | struct iovec *iovec; |
9adbd45d | 4037 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 4038 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
cd658695 | 4039 | ssize_t ret, ret2; |
b4aec400 | 4040 | loff_t *ppos; |
2b188cc1 | 4041 | |
607b6fb8 | 4042 | if (!req_has_async_data(req)) { |
5e49c973 PB |
4043 | ret = io_import_iovec(WRITE, req, &iovec, s, issue_flags); |
4044 | if (unlikely(ret < 0)) | |
2846c481 | 4045 | return ret; |
607b6fb8 PB |
4046 | } else { |
4047 | struct io_async_rw *rw = req->async_data; | |
4048 | ||
4049 | s = &rw->s; | |
4050 | iov_iter_restore(&s->iter, &s->iter_state); | |
2846c481 | 4051 | iovec = NULL; |
2846c481 | 4052 | } |
584b0180 | 4053 | ret = io_rw_init_file(req, FMODE_WRITE); |
323b190b JA |
4054 | if (unlikely(ret)) { |
4055 | kfree(iovec); | |
584b0180 | 4056 | return ret; |
323b190b | 4057 | } |
cef216fc | 4058 | req->cqe.res = iov_iter_count(&s->iter); |
2b188cc1 | 4059 | |
607b6fb8 PB |
4060 | if (force_nonblock) { |
4061 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 4062 | if (unlikely(!io_file_supports_nowait(req))) |
607b6fb8 | 4063 | goto copy_iov; |
fd6c2e4c | 4064 | |
607b6fb8 PB |
4065 | /* file path doesn't support NOWAIT for non-direct_IO */ |
4066 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
4067 | (req->flags & REQ_F_ISREG)) | |
4068 | goto copy_iov; | |
31b51510 | 4069 | |
607b6fb8 PB |
4070 | kiocb->ki_flags |= IOCB_NOWAIT; |
4071 | } else { | |
4072 | /* Ensure we clear previously set non-block flag */ | |
4073 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
4074 | } | |
31b51510 | 4075 | |
b4aec400 | 4076 | ppos = io_kiocb_update_pos(req); |
d34e1e5b | 4077 | |
cef216fc | 4078 | ret = rw_verify_area(WRITE, req->file, ppos, req->cqe.res); |
fa15bafb PB |
4079 | if (unlikely(ret)) |
4080 | goto out_free; | |
4ed734b0 | 4081 | |
fa15bafb PB |
4082 | /* |
4083 | * Open-code file_start_write here to grab freeze protection, | |
4084 | * which will be released by another thread in | |
4085 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
4086 | * released so that it doesn't complain about the held lock when | |
4087 | * we return to userspace. | |
4088 | */ | |
4089 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 4090 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
4091 | __sb_writers_release(file_inode(req->file)->i_sb, |
4092 | SB_FREEZE_WRITE); | |
4093 | } | |
4094 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 4095 | |
35645ac3 | 4096 | if (likely(req->file->f_op->write_iter)) |
c88598a9 | 4097 | ret2 = call_write_iter(req->file, kiocb, &s->iter); |
2dd2111d | 4098 | else if (req->file->f_op->write) |
c88598a9 | 4099 | ret2 = loop_rw_iter(WRITE, req, &s->iter); |
2dd2111d GH |
4100 | else |
4101 | ret2 = -EINVAL; | |
4ed734b0 | 4102 | |
6ad7f233 PB |
4103 | if (req->flags & REQ_F_REISSUE) { |
4104 | req->flags &= ~REQ_F_REISSUE; | |
230d50d4 | 4105 | ret2 = -EAGAIN; |
6ad7f233 | 4106 | } |
230d50d4 | 4107 | |
fa15bafb PB |
4108 | /* |
4109 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
4110 | * retry them without IOCB_NOWAIT. | |
4111 | */ | |
4112 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
4113 | ret2 = -EAGAIN; | |
75c668cd PB |
4114 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
4115 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 4116 | goto done; |
fa15bafb | 4117 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f | 4118 | /* IOPOLL retry should happen for io-wq threads */ |
b10841c9 | 4119 | if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL)) |
eefdf30f | 4120 | goto copy_iov; |
355afaeb | 4121 | done: |
2ea537ca | 4122 | kiocb_done(req, ret2, issue_flags); |
fa15bafb | 4123 | } else { |
f67676d1 | 4124 | copy_iov: |
c88598a9 PB |
4125 | iov_iter_restore(&s->iter, &s->iter_state); |
4126 | ret = io_setup_async_rw(req, iovec, s, false); | |
6bf985dc | 4127 | return ret ?: -EAGAIN; |
2b188cc1 | 4128 | } |
31b51510 | 4129 | out_free: |
f261c168 | 4130 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 4131 | if (iovec) |
6f2cc166 | 4132 | kfree(iovec); |
2b188cc1 JA |
4133 | return ret; |
4134 | } | |
4135 | ||
80a261fd JA |
4136 | static int io_renameat_prep(struct io_kiocb *req, |
4137 | const struct io_uring_sqe *sqe) | |
4138 | { | |
4139 | struct io_rename *ren = &req->rename; | |
4140 | const char __user *oldf, *newf; | |
4141 | ||
ed7eb259 JA |
4142 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4143 | return -EINVAL; | |
26578cda | 4144 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
ed7eb259 | 4145 | return -EINVAL; |
80a261fd JA |
4146 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
4147 | return -EBADF; | |
4148 | ||
4149 | ren->old_dfd = READ_ONCE(sqe->fd); | |
4150 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4151 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
4152 | ren->new_dfd = READ_ONCE(sqe->len); | |
4153 | ren->flags = READ_ONCE(sqe->rename_flags); | |
4154 | ||
4155 | ren->oldpath = getname(oldf); | |
4156 | if (IS_ERR(ren->oldpath)) | |
4157 | return PTR_ERR(ren->oldpath); | |
4158 | ||
4159 | ren->newpath = getname(newf); | |
4160 | if (IS_ERR(ren->newpath)) { | |
4161 | putname(ren->oldpath); | |
4162 | return PTR_ERR(ren->newpath); | |
4163 | } | |
4164 | ||
4165 | req->flags |= REQ_F_NEED_CLEANUP; | |
4166 | return 0; | |
4167 | } | |
4168 | ||
45d189c6 | 4169 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
4170 | { |
4171 | struct io_rename *ren = &req->rename; | |
4172 | int ret; | |
4173 | ||
45d189c6 | 4174 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
4175 | return -EAGAIN; |
4176 | ||
4177 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
4178 | ren->newpath, ren->flags); | |
4179 | ||
4180 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4181 | if (ret < 0) | |
93d2bcd2 | 4182 | req_set_fail(req); |
80a261fd JA |
4183 | io_req_complete(req, ret); |
4184 | return 0; | |
4185 | } | |
4186 | ||
14a1143b JA |
4187 | static int io_unlinkat_prep(struct io_kiocb *req, |
4188 | const struct io_uring_sqe *sqe) | |
4189 | { | |
4190 | struct io_unlink *un = &req->unlink; | |
4191 | const char __user *fname; | |
4192 | ||
22634bc5 JA |
4193 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4194 | return -EINVAL; | |
26578cda PB |
4195 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
4196 | sqe->splice_fd_in) | |
22634bc5 | 4197 | return -EINVAL; |
14a1143b JA |
4198 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
4199 | return -EBADF; | |
4200 | ||
4201 | un->dfd = READ_ONCE(sqe->fd); | |
4202 | ||
4203 | un->flags = READ_ONCE(sqe->unlink_flags); | |
4204 | if (un->flags & ~AT_REMOVEDIR) | |
4205 | return -EINVAL; | |
4206 | ||
4207 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4208 | un->filename = getname(fname); | |
4209 | if (IS_ERR(un->filename)) | |
4210 | return PTR_ERR(un->filename); | |
4211 | ||
4212 | req->flags |= REQ_F_NEED_CLEANUP; | |
4213 | return 0; | |
4214 | } | |
4215 | ||
45d189c6 | 4216 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
4217 | { |
4218 | struct io_unlink *un = &req->unlink; | |
4219 | int ret; | |
4220 | ||
45d189c6 | 4221 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
4222 | return -EAGAIN; |
4223 | ||
4224 | if (un->flags & AT_REMOVEDIR) | |
4225 | ret = do_rmdir(un->dfd, un->filename); | |
4226 | else | |
4227 | ret = do_unlinkat(un->dfd, un->filename); | |
4228 | ||
4229 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4230 | if (ret < 0) | |
93d2bcd2 | 4231 | req_set_fail(req); |
14a1143b JA |
4232 | io_req_complete(req, ret); |
4233 | return 0; | |
4234 | } | |
4235 | ||
e34a02dc DK |
4236 | static int io_mkdirat_prep(struct io_kiocb *req, |
4237 | const struct io_uring_sqe *sqe) | |
4238 | { | |
4239 | struct io_mkdir *mkd = &req->mkdir; | |
4240 | const char __user *fname; | |
4241 | ||
4242 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4243 | return -EINVAL; | |
4244 | if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index || | |
4245 | sqe->splice_fd_in) | |
4246 | return -EINVAL; | |
4247 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4248 | return -EBADF; | |
4249 | ||
4250 | mkd->dfd = READ_ONCE(sqe->fd); | |
4251 | mkd->mode = READ_ONCE(sqe->len); | |
4252 | ||
4253 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4254 | mkd->filename = getname(fname); | |
4255 | if (IS_ERR(mkd->filename)) | |
4256 | return PTR_ERR(mkd->filename); | |
4257 | ||
4258 | req->flags |= REQ_F_NEED_CLEANUP; | |
4259 | return 0; | |
4260 | } | |
4261 | ||
04f34081 | 4262 | static int io_mkdirat(struct io_kiocb *req, unsigned int issue_flags) |
e34a02dc DK |
4263 | { |
4264 | struct io_mkdir *mkd = &req->mkdir; | |
4265 | int ret; | |
4266 | ||
4267 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4268 | return -EAGAIN; | |
4269 | ||
4270 | ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode); | |
4271 | ||
4272 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4273 | if (ret < 0) | |
4274 | req_set_fail(req); | |
4275 | io_req_complete(req, ret); | |
4276 | return 0; | |
4277 | } | |
4278 | ||
7a8721f8 DK |
4279 | static int io_symlinkat_prep(struct io_kiocb *req, |
4280 | const struct io_uring_sqe *sqe) | |
4281 | { | |
4282 | struct io_symlink *sl = &req->symlink; | |
4283 | const char __user *oldpath, *newpath; | |
4284 | ||
4285 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4286 | return -EINVAL; | |
4287 | if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index || | |
4288 | sqe->splice_fd_in) | |
4289 | return -EINVAL; | |
4290 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4291 | return -EBADF; | |
4292 | ||
4293 | sl->new_dfd = READ_ONCE(sqe->fd); | |
4294 | oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4295 | newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
4296 | ||
4297 | sl->oldpath = getname(oldpath); | |
4298 | if (IS_ERR(sl->oldpath)) | |
4299 | return PTR_ERR(sl->oldpath); | |
4300 | ||
4301 | sl->newpath = getname(newpath); | |
4302 | if (IS_ERR(sl->newpath)) { | |
4303 | putname(sl->oldpath); | |
4304 | return PTR_ERR(sl->newpath); | |
4305 | } | |
4306 | ||
4307 | req->flags |= REQ_F_NEED_CLEANUP; | |
4308 | return 0; | |
4309 | } | |
4310 | ||
04f34081 | 4311 | static int io_symlinkat(struct io_kiocb *req, unsigned int issue_flags) |
7a8721f8 DK |
4312 | { |
4313 | struct io_symlink *sl = &req->symlink; | |
4314 | int ret; | |
4315 | ||
4316 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4317 | return -EAGAIN; | |
4318 | ||
4319 | ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath); | |
4320 | ||
4321 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4322 | if (ret < 0) | |
4323 | req_set_fail(req); | |
4324 | io_req_complete(req, ret); | |
4325 | return 0; | |
4326 | } | |
4327 | ||
cf30da90 DK |
4328 | static int io_linkat_prep(struct io_kiocb *req, |
4329 | const struct io_uring_sqe *sqe) | |
4330 | { | |
4331 | struct io_hardlink *lnk = &req->hardlink; | |
4332 | const char __user *oldf, *newf; | |
4333 | ||
4334 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4335 | return -EINVAL; | |
4336 | if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) | |
4337 | return -EINVAL; | |
4338 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4339 | return -EBADF; | |
4340 | ||
4341 | lnk->old_dfd = READ_ONCE(sqe->fd); | |
4342 | lnk->new_dfd = READ_ONCE(sqe->len); | |
4343 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4344 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
4345 | lnk->flags = READ_ONCE(sqe->hardlink_flags); | |
4346 | ||
4347 | lnk->oldpath = getname(oldf); | |
4348 | if (IS_ERR(lnk->oldpath)) | |
4349 | return PTR_ERR(lnk->oldpath); | |
4350 | ||
4351 | lnk->newpath = getname(newf); | |
4352 | if (IS_ERR(lnk->newpath)) { | |
4353 | putname(lnk->oldpath); | |
4354 | return PTR_ERR(lnk->newpath); | |
4355 | } | |
4356 | ||
4357 | req->flags |= REQ_F_NEED_CLEANUP; | |
4358 | return 0; | |
4359 | } | |
4360 | ||
04f34081 | 4361 | static int io_linkat(struct io_kiocb *req, unsigned int issue_flags) |
cf30da90 DK |
4362 | { |
4363 | struct io_hardlink *lnk = &req->hardlink; | |
4364 | int ret; | |
4365 | ||
4366 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4367 | return -EAGAIN; | |
4368 | ||
4369 | ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd, | |
4370 | lnk->newpath, lnk->flags); | |
4371 | ||
4372 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4373 | if (ret < 0) | |
4374 | req_set_fail(req); | |
4375 | io_req_complete(req, ret); | |
4376 | return 0; | |
4377 | } | |
4378 | ||
36f4fa68 JA |
4379 | static int io_shutdown_prep(struct io_kiocb *req, |
4380 | const struct io_uring_sqe *sqe) | |
4381 | { | |
4382 | #if defined(CONFIG_NET) | |
4383 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4384 | return -EINVAL; | |
26578cda PB |
4385 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
4386 | sqe->buf_index || sqe->splice_fd_in)) | |
36f4fa68 JA |
4387 | return -EINVAL; |
4388 | ||
4389 | req->shutdown.how = READ_ONCE(sqe->len); | |
4390 | return 0; | |
4391 | #else | |
4392 | return -EOPNOTSUPP; | |
4393 | #endif | |
4394 | } | |
4395 | ||
45d189c6 | 4396 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
4397 | { |
4398 | #if defined(CONFIG_NET) | |
4399 | struct socket *sock; | |
4400 | int ret; | |
4401 | ||
45d189c6 | 4402 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
4403 | return -EAGAIN; |
4404 | ||
48aba79b | 4405 | sock = sock_from_file(req->file); |
36f4fa68 | 4406 | if (unlikely(!sock)) |
48aba79b | 4407 | return -ENOTSOCK; |
36f4fa68 JA |
4408 | |
4409 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d | 4410 | if (ret < 0) |
93d2bcd2 | 4411 | req_set_fail(req); |
36f4fa68 JA |
4412 | io_req_complete(req, ret); |
4413 | return 0; | |
4414 | #else | |
4415 | return -EOPNOTSUPP; | |
4416 | #endif | |
4417 | } | |
4418 | ||
f2a8d5c7 PB |
4419 | static int __io_splice_prep(struct io_kiocb *req, |
4420 | const struct io_uring_sqe *sqe) | |
7d67af2c | 4421 | { |
fe7e3257 | 4422 | struct io_splice *sp = &req->splice; |
7d67af2c | 4423 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
7d67af2c | 4424 | |
3232dd02 PB |
4425 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4426 | return -EINVAL; | |
7d67af2c | 4427 | |
7d67af2c PB |
4428 | sp->len = READ_ONCE(sqe->len); |
4429 | sp->flags = READ_ONCE(sqe->splice_flags); | |
7d67af2c PB |
4430 | if (unlikely(sp->flags & ~valid_flags)) |
4431 | return -EINVAL; | |
a3e4bc23 | 4432 | sp->splice_fd_in = READ_ONCE(sqe->splice_fd_in); |
7d67af2c PB |
4433 | return 0; |
4434 | } | |
4435 | ||
f2a8d5c7 PB |
4436 | static int io_tee_prep(struct io_kiocb *req, |
4437 | const struct io_uring_sqe *sqe) | |
4438 | { | |
4439 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
4440 | return -EINVAL; | |
4441 | return __io_splice_prep(req, sqe); | |
4442 | } | |
4443 | ||
45d189c6 | 4444 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
4445 | { |
4446 | struct io_splice *sp = &req->splice; | |
f2a8d5c7 PB |
4447 | struct file *out = sp->file_out; |
4448 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
a3e4bc23 | 4449 | struct file *in; |
f2a8d5c7 PB |
4450 | long ret = 0; |
4451 | ||
45d189c6 | 4452 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 | 4453 | return -EAGAIN; |
a3e4bc23 | 4454 | |
5106dd6e | 4455 | if (sp->flags & SPLICE_F_FD_IN_FIXED) |
e9419766 | 4456 | in = io_file_get_fixed(req, sp->splice_fd_in, issue_flags); |
5106dd6e JA |
4457 | else |
4458 | in = io_file_get_normal(req, sp->splice_fd_in); | |
a3e4bc23 JA |
4459 | if (!in) { |
4460 | ret = -EBADF; | |
4461 | goto done; | |
4462 | } | |
4463 | ||
f2a8d5c7 PB |
4464 | if (sp->len) |
4465 | ret = do_tee(in, out, sp->len, flags); | |
4466 | ||
e1d767f0 PB |
4467 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4468 | io_put_file(in); | |
a3e4bc23 | 4469 | done: |
f2a8d5c7 | 4470 | if (ret != sp->len) |
93d2bcd2 | 4471 | req_set_fail(req); |
e1e16097 | 4472 | io_req_complete(req, ret); |
f2a8d5c7 PB |
4473 | return 0; |
4474 | } | |
4475 | ||
4476 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4477 | { | |
fe7e3257 | 4478 | struct io_splice *sp = &req->splice; |
f2a8d5c7 PB |
4479 | |
4480 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
4481 | sp->off_out = READ_ONCE(sqe->off); | |
4482 | return __io_splice_prep(req, sqe); | |
4483 | } | |
4484 | ||
45d189c6 | 4485 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
4486 | { |
4487 | struct io_splice *sp = &req->splice; | |
7d67af2c PB |
4488 | struct file *out = sp->file_out; |
4489 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
4490 | loff_t *poff_in, *poff_out; | |
a3e4bc23 | 4491 | struct file *in; |
c9687426 | 4492 | long ret = 0; |
7d67af2c | 4493 | |
45d189c6 | 4494 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 4495 | return -EAGAIN; |
7d67af2c | 4496 | |
5106dd6e | 4497 | if (sp->flags & SPLICE_F_FD_IN_FIXED) |
e9419766 | 4498 | in = io_file_get_fixed(req, sp->splice_fd_in, issue_flags); |
5106dd6e JA |
4499 | else |
4500 | in = io_file_get_normal(req, sp->splice_fd_in); | |
a3e4bc23 JA |
4501 | if (!in) { |
4502 | ret = -EBADF; | |
4503 | goto done; | |
4504 | } | |
4505 | ||
7d67af2c PB |
4506 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
4507 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 4508 | |
948a7749 | 4509 | if (sp->len) |
c9687426 | 4510 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c | 4511 | |
e1d767f0 PB |
4512 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4513 | io_put_file(in); | |
a3e4bc23 | 4514 | done: |
7d67af2c | 4515 | if (ret != sp->len) |
93d2bcd2 | 4516 | req_set_fail(req); |
e1e16097 | 4517 | io_req_complete(req, ret); |
7d67af2c PB |
4518 | return 0; |
4519 | } | |
4520 | ||
2b188cc1 JA |
4521 | /* |
4522 | * IORING_OP_NOP just posts a completion event, nothing else. | |
4523 | */ | |
889fca73 | 4524 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
4525 | { |
4526 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 4527 | |
def596e9 JA |
4528 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4529 | return -EINVAL; | |
4530 | ||
889fca73 | 4531 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
4532 | return 0; |
4533 | } | |
4534 | ||
4f57f06c JA |
4535 | static int io_msg_ring_prep(struct io_kiocb *req, |
4536 | const struct io_uring_sqe *sqe) | |
4537 | { | |
f3b6a41e JA |
4538 | if (unlikely(sqe->addr || sqe->ioprio || sqe->rw_flags || |
4539 | sqe->splice_fd_in || sqe->buf_index || sqe->personality)) | |
4f57f06c JA |
4540 | return -EINVAL; |
4541 | ||
4f57f06c JA |
4542 | req->msg.user_data = READ_ONCE(sqe->off); |
4543 | req->msg.len = READ_ONCE(sqe->len); | |
4544 | return 0; | |
4545 | } | |
4546 | ||
4547 | static int io_msg_ring(struct io_kiocb *req, unsigned int issue_flags) | |
4548 | { | |
4549 | struct io_ring_ctx *target_ctx; | |
4550 | struct io_msg *msg = &req->msg; | |
4f57f06c | 4551 | bool filled; |
3f1d52ab | 4552 | int ret; |
4f57f06c | 4553 | |
3f1d52ab JA |
4554 | ret = -EBADFD; |
4555 | if (req->file->f_op != &io_uring_fops) | |
4556 | goto done; | |
4f57f06c | 4557 | |
3f1d52ab | 4558 | ret = -EOVERFLOW; |
4f57f06c JA |
4559 | target_ctx = req->file->private_data; |
4560 | ||
4561 | spin_lock(&target_ctx->completion_lock); | |
7ef66d18 | 4562 | filled = io_fill_cqe_aux(target_ctx, msg->user_data, msg->len, 0); |
4f57f06c JA |
4563 | io_commit_cqring(target_ctx); |
4564 | spin_unlock(&target_ctx->completion_lock); | |
4565 | ||
4566 | if (filled) { | |
4567 | io_cqring_ev_posted(target_ctx); | |
4568 | ret = 0; | |
4569 | } | |
4570 | ||
3f1d52ab | 4571 | done: |
9666d420 JA |
4572 | if (ret < 0) |
4573 | req_set_fail(req); | |
4f57f06c JA |
4574 | __io_req_complete(req, issue_flags, ret, 0); |
4575 | return 0; | |
4576 | } | |
4577 | ||
1155c76a | 4578 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 4579 | { |
6b06314c | 4580 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 4581 | |
6b06314c | 4582 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 4583 | return -EINVAL; |
26578cda PB |
4584 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4585 | sqe->splice_fd_in)) | |
c992fe29 CH |
4586 | return -EINVAL; |
4587 | ||
8ed8d3c3 JA |
4588 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
4589 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
4590 | return -EINVAL; | |
4591 | ||
4592 | req->sync.off = READ_ONCE(sqe->off); | |
4593 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
4594 | return 0; |
4595 | } | |
4596 | ||
45d189c6 | 4597 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4598 | { |
8ed8d3c3 | 4599 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
4600 | int ret; |
4601 | ||
ac45abc0 | 4602 | /* fsync always requires a blocking context */ |
45d189c6 | 4603 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4604 | return -EAGAIN; |
4605 | ||
9adbd45d | 4606 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
4607 | end > 0 ? end : LLONG_MAX, |
4608 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
4609 | if (ret < 0) | |
93d2bcd2 | 4610 | req_set_fail(req); |
e1e16097 | 4611 | io_req_complete(req, ret); |
c992fe29 CH |
4612 | return 0; |
4613 | } | |
4614 | ||
d63d1b5e JA |
4615 | static int io_fallocate_prep(struct io_kiocb *req, |
4616 | const struct io_uring_sqe *sqe) | |
4617 | { | |
26578cda PB |
4618 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
4619 | sqe->splice_fd_in) | |
d63d1b5e | 4620 | return -EINVAL; |
3232dd02 PB |
4621 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4622 | return -EINVAL; | |
d63d1b5e JA |
4623 | |
4624 | req->sync.off = READ_ONCE(sqe->off); | |
4625 | req->sync.len = READ_ONCE(sqe->addr); | |
4626 | req->sync.mode = READ_ONCE(sqe->len); | |
4627 | return 0; | |
4628 | } | |
4629 | ||
45d189c6 | 4630 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 4631 | { |
ac45abc0 PB |
4632 | int ret; |
4633 | ||
d63d1b5e | 4634 | /* fallocate always requiring blocking context */ |
45d189c6 | 4635 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 4636 | return -EAGAIN; |
ac45abc0 PB |
4637 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
4638 | req->sync.len); | |
ac45abc0 | 4639 | if (ret < 0) |
93d2bcd2 | 4640 | req_set_fail(req); |
f63cf519 JA |
4641 | else |
4642 | fsnotify_modify(req->file); | |
e1e16097 | 4643 | io_req_complete(req, ret); |
5d17b4a4 JA |
4644 | return 0; |
4645 | } | |
4646 | ||
ec65fea5 | 4647 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 4648 | { |
f8748881 | 4649 | const char __user *fname; |
15b71abe | 4650 | int ret; |
b7bb4f7d | 4651 | |
d3fddf6d PB |
4652 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4653 | return -EINVAL; | |
b9445598 | 4654 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 4655 | return -EINVAL; |
ec65fea5 | 4656 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 4657 | return -EBADF; |
03b1230c | 4658 | |
ec65fea5 PB |
4659 | /* open.how should be already initialised */ |
4660 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 4661 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 4662 | |
25e72d10 PB |
4663 | req->open.dfd = READ_ONCE(sqe->fd); |
4664 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 4665 | req->open.filename = getname(fname); |
15b71abe JA |
4666 | if (IS_ERR(req->open.filename)) { |
4667 | ret = PTR_ERR(req->open.filename); | |
4668 | req->open.filename = NULL; | |
4669 | return ret; | |
4670 | } | |
b9445598 PB |
4671 | |
4672 | req->open.file_slot = READ_ONCE(sqe->file_index); | |
4673 | if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) | |
4674 | return -EINVAL; | |
4675 | ||
4022e7af | 4676 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 4677 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 4678 | return 0; |
03b1230c JA |
4679 | } |
4680 | ||
ec65fea5 PB |
4681 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4682 | { | |
d3fddf6d PB |
4683 | u64 mode = READ_ONCE(sqe->len); |
4684 | u64 flags = READ_ONCE(sqe->open_flags); | |
ec65fea5 | 4685 | |
ec65fea5 PB |
4686 | req->open.how = build_open_how(flags, mode); |
4687 | return __io_openat_prep(req, sqe); | |
4688 | } | |
4689 | ||
cebdb986 | 4690 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 4691 | { |
cebdb986 | 4692 | struct open_how __user *how; |
cebdb986 | 4693 | size_t len; |
0fa03c62 JA |
4694 | int ret; |
4695 | ||
cebdb986 JA |
4696 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4697 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
4698 | if (len < OPEN_HOW_SIZE_VER0) |
4699 | return -EINVAL; | |
3529d8c2 | 4700 | |
cebdb986 JA |
4701 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
4702 | len); | |
4703 | if (ret) | |
4704 | return ret; | |
3529d8c2 | 4705 | |
ec65fea5 | 4706 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
4707 | } |
4708 | ||
45d189c6 | 4709 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
4710 | { |
4711 | struct open_flags op; | |
15b71abe | 4712 | struct file *file; |
b9445598 PB |
4713 | bool resolve_nonblock, nonblock_set; |
4714 | bool fixed = !!req->open.file_slot; | |
15b71abe JA |
4715 | int ret; |
4716 | ||
cebdb986 | 4717 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
4718 | if (ret) |
4719 | goto err; | |
3a81fd02 JA |
4720 | nonblock_set = op.open_flag & O_NONBLOCK; |
4721 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 4722 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
4723 | /* |
4724 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
4725 | * it'll always -EAGAIN | |
4726 | */ | |
4727 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
4728 | return -EAGAIN; | |
4729 | op.lookup_flags |= LOOKUP_CACHED; | |
4730 | op.open_flag |= O_NONBLOCK; | |
4731 | } | |
15b71abe | 4732 | |
b9445598 PB |
4733 | if (!fixed) { |
4734 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); | |
4735 | if (ret < 0) | |
4736 | goto err; | |
4737 | } | |
15b71abe JA |
4738 | |
4739 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
12dcb58a | 4740 | if (IS_ERR(file)) { |
944d1444 | 4741 | /* |
12dcb58a PB |
4742 | * We could hang on to this 'fd' on retrying, but seems like |
4743 | * marginal gain for something that is now known to be a slower | |
4744 | * path. So just put it, and we'll get a new one when we retry. | |
944d1444 | 4745 | */ |
b9445598 PB |
4746 | if (!fixed) |
4747 | put_unused_fd(ret); | |
3a81fd02 | 4748 | |
15b71abe | 4749 | ret = PTR_ERR(file); |
12dcb58a PB |
4750 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
4751 | if (ret == -EAGAIN && | |
4752 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) | |
4753 | return -EAGAIN; | |
4754 | goto err; | |
15b71abe | 4755 | } |
12dcb58a PB |
4756 | |
4757 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) | |
4758 | file->f_flags &= ~O_NONBLOCK; | |
4759 | fsnotify_open(file); | |
b9445598 PB |
4760 | |
4761 | if (!fixed) | |
4762 | fd_install(ret, file); | |
4763 | else | |
4764 | ret = io_install_fixed_file(req, file, issue_flags, | |
4765 | req->open.file_slot - 1); | |
15b71abe JA |
4766 | err: |
4767 | putname(req->open.filename); | |
8fef80bf | 4768 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe | 4769 | if (ret < 0) |
93d2bcd2 | 4770 | req_set_fail(req); |
0bdf3398 | 4771 | __io_req_complete(req, issue_flags, ret, 0); |
15b71abe JA |
4772 | return 0; |
4773 | } | |
4774 | ||
45d189c6 | 4775 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 4776 | { |
e45cff58 | 4777 | return io_openat2(req, issue_flags); |
cebdb986 JA |
4778 | } |
4779 | ||
067524e9 JA |
4780 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4781 | const struct io_uring_sqe *sqe) | |
4782 | { | |
4783 | struct io_provide_buf *p = &req->pbuf; | |
4784 | u64 tmp; | |
4785 | ||
26578cda PB |
4786 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
4787 | sqe->splice_fd_in) | |
067524e9 JA |
4788 | return -EINVAL; |
4789 | ||
4790 | tmp = READ_ONCE(sqe->fd); | |
4791 | if (!tmp || tmp > USHRT_MAX) | |
4792 | return -EINVAL; | |
4793 | ||
4794 | memset(p, 0, sizeof(*p)); | |
4795 | p->nbufs = tmp; | |
4796 | p->bgid = READ_ONCE(sqe->buf_group); | |
4797 | return 0; | |
4798 | } | |
4799 | ||
dbc7d452 JA |
4800 | static int __io_remove_buffers(struct io_ring_ctx *ctx, |
4801 | struct io_buffer_list *bl, unsigned nbufs) | |
067524e9 JA |
4802 | { |
4803 | unsigned i = 0; | |
4804 | ||
4805 | /* shouldn't happen */ | |
4806 | if (!nbufs) | |
4807 | return 0; | |
4808 | ||
4809 | /* the head kbuf is the list itself */ | |
dbc7d452 | 4810 | while (!list_empty(&bl->buf_list)) { |
067524e9 JA |
4811 | struct io_buffer *nxt; |
4812 | ||
dbc7d452 | 4813 | nxt = list_first_entry(&bl->buf_list, struct io_buffer, list); |
067524e9 | 4814 | list_del(&nxt->list); |
067524e9 JA |
4815 | if (++i == nbufs) |
4816 | return i; | |
1d0254e6 | 4817 | cond_resched(); |
067524e9 JA |
4818 | } |
4819 | i++; | |
067524e9 JA |
4820 | |
4821 | return i; | |
4822 | } | |
4823 | ||
889fca73 | 4824 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
4825 | { |
4826 | struct io_provide_buf *p = &req->pbuf; | |
4827 | struct io_ring_ctx *ctx = req->ctx; | |
dbc7d452 | 4828 | struct io_buffer_list *bl; |
067524e9 JA |
4829 | int ret = 0; |
4830 | ||
f8929630 | 4831 | io_ring_submit_lock(ctx, issue_flags); |
067524e9 JA |
4832 | |
4833 | ret = -ENOENT; | |
dbc7d452 JA |
4834 | bl = io_buffer_get_list(ctx, p->bgid); |
4835 | if (bl) | |
4836 | ret = __io_remove_buffers(ctx, bl, p->nbufs); | |
067524e9 | 4837 | if (ret < 0) |
93d2bcd2 | 4838 | req_set_fail(req); |
067524e9 | 4839 | |
9fb8cb49 PB |
4840 | /* complete before unlock, IOPOLL may need the lock */ |
4841 | __io_req_complete(req, issue_flags, ret, 0); | |
f8929630 | 4842 | io_ring_submit_unlock(ctx, issue_flags); |
067524e9 JA |
4843 | return 0; |
4844 | } | |
4845 | ||
ddf0322d JA |
4846 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4847 | const struct io_uring_sqe *sqe) | |
4848 | { | |
38134ada | 4849 | unsigned long size, tmp_check; |
ddf0322d JA |
4850 | struct io_provide_buf *p = &req->pbuf; |
4851 | u64 tmp; | |
4852 | ||
26578cda | 4853 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
ddf0322d JA |
4854 | return -EINVAL; |
4855 | ||
4856 | tmp = READ_ONCE(sqe->fd); | |
4857 | if (!tmp || tmp > USHRT_MAX) | |
4858 | return -E2BIG; | |
4859 | p->nbufs = tmp; | |
4860 | p->addr = READ_ONCE(sqe->addr); | |
4861 | p->len = READ_ONCE(sqe->len); | |
4862 | ||
38134ada PB |
4863 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
4864 | &size)) | |
4865 | return -EOVERFLOW; | |
4866 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) | |
4867 | return -EOVERFLOW; | |
4868 | ||
d81269fe PB |
4869 | size = (unsigned long)p->len * p->nbufs; |
4870 | if (!access_ok(u64_to_user_ptr(p->addr), size)) | |
ddf0322d JA |
4871 | return -EFAULT; |
4872 | ||
4873 | p->bgid = READ_ONCE(sqe->buf_group); | |
4874 | tmp = READ_ONCE(sqe->off); | |
4875 | if (tmp > USHRT_MAX) | |
4876 | return -E2BIG; | |
4877 | p->bid = tmp; | |
4878 | return 0; | |
4879 | } | |
4880 | ||
cc3cec83 JA |
4881 | static int io_refill_buffer_cache(struct io_ring_ctx *ctx) |
4882 | { | |
4883 | struct io_buffer *buf; | |
4884 | struct page *page; | |
4885 | int bufs_in_page; | |
4886 | ||
4887 | /* | |
4888 | * Completions that don't happen inline (eg not under uring_lock) will | |
4889 | * add to ->io_buffers_comp. If we don't have any free buffers, check | |
4890 | * the completion list and splice those entries first. | |
4891 | */ | |
4892 | if (!list_empty_careful(&ctx->io_buffers_comp)) { | |
4893 | spin_lock(&ctx->completion_lock); | |
4894 | if (!list_empty(&ctx->io_buffers_comp)) { | |
4895 | list_splice_init(&ctx->io_buffers_comp, | |
4896 | &ctx->io_buffers_cache); | |
4897 | spin_unlock(&ctx->completion_lock); | |
4898 | return 0; | |
4899 | } | |
4900 | spin_unlock(&ctx->completion_lock); | |
4901 | } | |
4902 | ||
4903 | /* | |
4904 | * No free buffers and no completion entries either. Allocate a new | |
4905 | * page worth of buffer entries and add those to our freelist. | |
4906 | */ | |
4907 | page = alloc_page(GFP_KERNEL_ACCOUNT); | |
4908 | if (!page) | |
4909 | return -ENOMEM; | |
4910 | ||
4911 | list_add(&page->lru, &ctx->io_buffers_pages); | |
4912 | ||
4913 | buf = page_address(page); | |
4914 | bufs_in_page = PAGE_SIZE / sizeof(*buf); | |
4915 | while (bufs_in_page) { | |
4916 | list_add_tail(&buf->list, &ctx->io_buffers_cache); | |
4917 | buf++; | |
4918 | bufs_in_page--; | |
4919 | } | |
4920 | ||
4921 | return 0; | |
4922 | } | |
4923 | ||
4924 | static int io_add_buffers(struct io_ring_ctx *ctx, struct io_provide_buf *pbuf, | |
dbc7d452 | 4925 | struct io_buffer_list *bl) |
ddf0322d JA |
4926 | { |
4927 | struct io_buffer *buf; | |
4928 | u64 addr = pbuf->addr; | |
4929 | int i, bid = pbuf->bid; | |
4930 | ||
4931 | for (i = 0; i < pbuf->nbufs; i++) { | |
cc3cec83 JA |
4932 | if (list_empty(&ctx->io_buffers_cache) && |
4933 | io_refill_buffer_cache(ctx)) | |
ddf0322d | 4934 | break; |
cc3cec83 JA |
4935 | buf = list_first_entry(&ctx->io_buffers_cache, struct io_buffer, |
4936 | list); | |
dbc7d452 | 4937 | list_move_tail(&buf->list, &bl->buf_list); |
ddf0322d | 4938 | buf->addr = addr; |
d1f82808 | 4939 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
ddf0322d | 4940 | buf->bid = bid; |
b1c62645 | 4941 | buf->bgid = pbuf->bgid; |
ddf0322d JA |
4942 | addr += pbuf->len; |
4943 | bid++; | |
f240762f | 4944 | cond_resched(); |
ddf0322d JA |
4945 | } |
4946 | ||
dbc7d452 | 4947 | return i ? 0 : -ENOMEM; |
ddf0322d JA |
4948 | } |
4949 | ||
889fca73 | 4950 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4951 | { |
4952 | struct io_provide_buf *p = &req->pbuf; | |
4953 | struct io_ring_ctx *ctx = req->ctx; | |
dbc7d452 | 4954 | struct io_buffer_list *bl; |
ddf0322d | 4955 | int ret = 0; |
ddf0322d | 4956 | |
f8929630 | 4957 | io_ring_submit_lock(ctx, issue_flags); |
ddf0322d | 4958 | |
dbc7d452 JA |
4959 | bl = io_buffer_get_list(ctx, p->bgid); |
4960 | if (unlikely(!bl)) { | |
4961 | bl = kmalloc(sizeof(*bl), GFP_KERNEL); | |
4962 | if (!bl) { | |
4963 | ret = -ENOMEM; | |
4964 | goto err; | |
4965 | } | |
4966 | io_buffer_add_list(ctx, bl, p->bgid); | |
ddf0322d | 4967 | } |
dbc7d452 JA |
4968 | |
4969 | ret = io_add_buffers(ctx, p, bl); | |
4970 | err: | |
ddf0322d | 4971 | if (ret < 0) |
93d2bcd2 | 4972 | req_set_fail(req); |
9fb8cb49 PB |
4973 | /* complete before unlock, IOPOLL may need the lock */ |
4974 | __io_req_complete(req, issue_flags, ret, 0); | |
f8929630 | 4975 | io_ring_submit_unlock(ctx, issue_flags); |
ddf0322d | 4976 | return 0; |
cebdb986 JA |
4977 | } |
4978 | ||
3e4827b0 JA |
4979 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4980 | const struct io_uring_sqe *sqe) | |
4981 | { | |
4982 | #if defined(CONFIG_EPOLL) | |
26578cda | 4983 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
3e4827b0 | 4984 | return -EINVAL; |
2d74d042 | 4985 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4986 | return -EINVAL; |
3e4827b0 JA |
4987 | |
4988 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4989 | req->epoll.op = READ_ONCE(sqe->len); | |
4990 | req->epoll.fd = READ_ONCE(sqe->off); | |
4991 | ||
4992 | if (ep_op_has_event(req->epoll.op)) { | |
4993 | struct epoll_event __user *ev; | |
4994 | ||
4995 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4996 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4997 | return -EFAULT; | |
4998 | } | |
4999 | ||
5000 | return 0; | |
5001 | #else | |
5002 | return -EOPNOTSUPP; | |
5003 | #endif | |
5004 | } | |
5005 | ||
889fca73 | 5006 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
5007 | { |
5008 | #if defined(CONFIG_EPOLL) | |
5009 | struct io_epoll *ie = &req->epoll; | |
5010 | int ret; | |
45d189c6 | 5011 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
5012 | |
5013 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
5014 | if (force_nonblock && ret == -EAGAIN) | |
5015 | return -EAGAIN; | |
5016 | ||
5017 | if (ret < 0) | |
93d2bcd2 | 5018 | req_set_fail(req); |
889fca73 | 5019 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
5020 | return 0; |
5021 | #else | |
5022 | return -EOPNOTSUPP; | |
5023 | #endif | |
5024 | } | |
5025 | ||
c1ca757b JA |
5026 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5027 | { | |
5028 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
26578cda | 5029 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
c1ca757b | 5030 | return -EINVAL; |
3232dd02 PB |
5031 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5032 | return -EINVAL; | |
c1ca757b JA |
5033 | |
5034 | req->madvise.addr = READ_ONCE(sqe->addr); | |
5035 | req->madvise.len = READ_ONCE(sqe->len); | |
5036 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
5037 | return 0; | |
5038 | #else | |
5039 | return -EOPNOTSUPP; | |
5040 | #endif | |
5041 | } | |
5042 | ||
45d189c6 | 5043 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
5044 | { |
5045 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
5046 | struct io_madvise *ma = &req->madvise; | |
5047 | int ret; | |
5048 | ||
45d189c6 | 5049 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
5050 | return -EAGAIN; |
5051 | ||
0726b01e | 5052 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b | 5053 | if (ret < 0) |
93d2bcd2 | 5054 | req_set_fail(req); |
e1e16097 | 5055 | io_req_complete(req, ret); |
c1ca757b JA |
5056 | return 0; |
5057 | #else | |
5058 | return -EOPNOTSUPP; | |
5059 | #endif | |
5060 | } | |
5061 | ||
4840e418 JA |
5062 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5063 | { | |
26578cda | 5064 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
4840e418 | 5065 | return -EINVAL; |
3232dd02 PB |
5066 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5067 | return -EINVAL; | |
4840e418 JA |
5068 | |
5069 | req->fadvise.offset = READ_ONCE(sqe->off); | |
5070 | req->fadvise.len = READ_ONCE(sqe->len); | |
5071 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
5072 | return 0; | |
5073 | } | |
5074 | ||
45d189c6 | 5075 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
5076 | { |
5077 | struct io_fadvise *fa = &req->fadvise; | |
5078 | int ret; | |
5079 | ||
45d189c6 | 5080 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
5081 | switch (fa->advice) { |
5082 | case POSIX_FADV_NORMAL: | |
5083 | case POSIX_FADV_RANDOM: | |
5084 | case POSIX_FADV_SEQUENTIAL: | |
5085 | break; | |
5086 | default: | |
5087 | return -EAGAIN; | |
5088 | } | |
5089 | } | |
4840e418 JA |
5090 | |
5091 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
5092 | if (ret < 0) | |
93d2bcd2 | 5093 | req_set_fail(req); |
0bdf3398 | 5094 | __io_req_complete(req, issue_flags, ret, 0); |
4840e418 JA |
5095 | return 0; |
5096 | } | |
5097 | ||
eddc7ef5 JA |
5098 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5099 | { | |
1b6fe6e0 SR |
5100 | const char __user *path; |
5101 | ||
2d74d042 | 5102 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 5103 | return -EINVAL; |
26578cda | 5104 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
eddc7ef5 | 5105 | return -EINVAL; |
9c280f90 | 5106 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 5107 | return -EBADF; |
eddc7ef5 | 5108 | |
1d9e1288 BM |
5109 | req->statx.dfd = READ_ONCE(sqe->fd); |
5110 | req->statx.mask = READ_ONCE(sqe->len); | |
1b6fe6e0 | 5111 | path = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
5112 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
5113 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 | 5114 | |
1b6fe6e0 SR |
5115 | req->statx.filename = getname_flags(path, |
5116 | getname_statx_lookup_flags(req->statx.flags), | |
5117 | NULL); | |
5118 | ||
5119 | if (IS_ERR(req->statx.filename)) { | |
5120 | int ret = PTR_ERR(req->statx.filename); | |
5121 | ||
5122 | req->statx.filename = NULL; | |
5123 | return ret; | |
5124 | } | |
5125 | ||
5126 | req->flags |= REQ_F_NEED_CLEANUP; | |
eddc7ef5 JA |
5127 | return 0; |
5128 | } | |
5129 | ||
45d189c6 | 5130 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 5131 | { |
1d9e1288 | 5132 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
5133 | int ret; |
5134 | ||
59d70013 | 5135 | if (issue_flags & IO_URING_F_NONBLOCK) |
eddc7ef5 JA |
5136 | return -EAGAIN; |
5137 | ||
e62753e4 BM |
5138 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
5139 | ctx->buffer); | |
eddc7ef5 | 5140 | |
eddc7ef5 | 5141 | if (ret < 0) |
93d2bcd2 | 5142 | req_set_fail(req); |
e1e16097 | 5143 | io_req_complete(req, ret); |
eddc7ef5 JA |
5144 | return 0; |
5145 | } | |
5146 | ||
b5dba59e JA |
5147 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5148 | { | |
14587a46 | 5149 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 5150 | return -EINVAL; |
b5dba59e | 5151 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
7df778be | 5152 | sqe->rw_flags || sqe->buf_index) |
b5dba59e | 5153 | return -EINVAL; |
9c280f90 | 5154 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 5155 | return -EBADF; |
b5dba59e JA |
5156 | |
5157 | req->close.fd = READ_ONCE(sqe->fd); | |
7df778be PB |
5158 | req->close.file_slot = READ_ONCE(sqe->file_index); |
5159 | if (req->close.file_slot && req->close.fd) | |
5160 | return -EINVAL; | |
5161 | ||
b5dba59e | 5162 | return 0; |
b5dba59e JA |
5163 | } |
5164 | ||
889fca73 | 5165 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 5166 | { |
9eac1904 | 5167 | struct files_struct *files = current->files; |
3af73b28 | 5168 | struct io_close *close = &req->close; |
9eac1904 | 5169 | struct fdtable *fdt; |
a1fde923 PB |
5170 | struct file *file = NULL; |
5171 | int ret = -EBADF; | |
b5dba59e | 5172 | |
7df778be PB |
5173 | if (req->close.file_slot) { |
5174 | ret = io_close_fixed(req, issue_flags); | |
5175 | goto err; | |
5176 | } | |
5177 | ||
9eac1904 JA |
5178 | spin_lock(&files->file_lock); |
5179 | fdt = files_fdtable(files); | |
5180 | if (close->fd >= fdt->max_fds) { | |
5181 | spin_unlock(&files->file_lock); | |
5182 | goto err; | |
5183 | } | |
5184 | file = fdt->fd[close->fd]; | |
a1fde923 | 5185 | if (!file || file->f_op == &io_uring_fops) { |
9eac1904 JA |
5186 | spin_unlock(&files->file_lock); |
5187 | file = NULL; | |
5188 | goto err; | |
3af73b28 | 5189 | } |
b5dba59e JA |
5190 | |
5191 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 5192 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 5193 | spin_unlock(&files->file_lock); |
0bf0eefd | 5194 | return -EAGAIN; |
a2100672 | 5195 | } |
b5dba59e | 5196 | |
9eac1904 JA |
5197 | ret = __close_fd_get_file(close->fd, &file); |
5198 | spin_unlock(&files->file_lock); | |
5199 | if (ret < 0) { | |
5200 | if (ret == -ENOENT) | |
5201 | ret = -EBADF; | |
5202 | goto err; | |
5203 | } | |
5204 | ||
3af73b28 | 5205 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
5206 | ret = filp_close(file, current->files); |
5207 | err: | |
3af73b28 | 5208 | if (ret < 0) |
93d2bcd2 | 5209 | req_set_fail(req); |
9eac1904 JA |
5210 | if (file) |
5211 | fput(file); | |
889fca73 | 5212 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 5213 | return 0; |
b5dba59e JA |
5214 | } |
5215 | ||
1155c76a | 5216 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
5217 | { |
5218 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 5219 | |
5d17b4a4 JA |
5220 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
5221 | return -EINVAL; | |
26578cda PB |
5222 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
5223 | sqe->splice_fd_in)) | |
5d17b4a4 JA |
5224 | return -EINVAL; |
5225 | ||
8ed8d3c3 JA |
5226 | req->sync.off = READ_ONCE(sqe->off); |
5227 | req->sync.len = READ_ONCE(sqe->len); | |
5228 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
5229 | return 0; |
5230 | } | |
5231 | ||
45d189c6 | 5232 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 5233 | { |
8ed8d3c3 JA |
5234 | int ret; |
5235 | ||
ac45abc0 | 5236 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 5237 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
5238 | return -EAGAIN; |
5239 | ||
9adbd45d | 5240 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
5241 | req->sync.flags); |
5242 | if (ret < 0) | |
93d2bcd2 | 5243 | req_set_fail(req); |
e1e16097 | 5244 | io_req_complete(req, ret); |
5d17b4a4 JA |
5245 | return 0; |
5246 | } | |
5247 | ||
469956e8 | 5248 | #if defined(CONFIG_NET) |
4c3c0943 JA |
5249 | static bool io_net_retry(struct socket *sock, int flags) |
5250 | { | |
5251 | if (!(flags & MSG_WAITALL)) | |
5252 | return false; | |
5253 | return sock->type == SOCK_STREAM || sock->type == SOCK_SEQPACKET; | |
5254 | } | |
5255 | ||
02d27d89 PB |
5256 | static int io_setup_async_msg(struct io_kiocb *req, |
5257 | struct io_async_msghdr *kmsg) | |
5258 | { | |
e8c2bc1f JA |
5259 | struct io_async_msghdr *async_msg = req->async_data; |
5260 | ||
5261 | if (async_msg) | |
02d27d89 | 5262 | return -EAGAIN; |
e8c2bc1f | 5263 | if (io_alloc_async_data(req)) { |
257e84a5 | 5264 | kfree(kmsg->free_iov); |
02d27d89 PB |
5265 | return -ENOMEM; |
5266 | } | |
e8c2bc1f | 5267 | async_msg = req->async_data; |
02d27d89 | 5268 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 5269 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 5270 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
5271 | /* if were using fast_iov, set it to the new one */ |
5272 | if (!async_msg->free_iov) | |
5273 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
5274 | ||
02d27d89 PB |
5275 | return -EAGAIN; |
5276 | } | |
5277 | ||
2ae523ed PB |
5278 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
5279 | struct io_async_msghdr *iomsg) | |
5280 | { | |
2ae523ed | 5281 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 5282 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 5283 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 5284 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
5285 | } |
5286 | ||
93642ef8 PB |
5287 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
5288 | { | |
5289 | int ret; | |
5290 | ||
93642ef8 PB |
5291 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
5292 | if (!ret) | |
5293 | req->flags |= REQ_F_NEED_CLEANUP; | |
5294 | return ret; | |
5295 | } | |
5296 | ||
3529d8c2 | 5297 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 5298 | { |
e47293fd | 5299 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 5300 | |
d2b6f48b PB |
5301 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5302 | return -EINVAL; | |
5303 | ||
270a5940 | 5304 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 5305 | sr->len = READ_ONCE(sqe->len); |
04411806 PB |
5306 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
5307 | if (sr->msg_flags & MSG_DONTWAIT) | |
5308 | req->flags |= REQ_F_NOWAIT; | |
3529d8c2 | 5309 | |
d8768362 JA |
5310 | #ifdef CONFIG_COMPAT |
5311 | if (req->ctx->compat) | |
5312 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
5313 | #endif | |
4c3c0943 | 5314 | sr->done_io = 0; |
93642ef8 | 5315 | return 0; |
03b1230c JA |
5316 | } |
5317 | ||
889fca73 | 5318 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 5319 | { |
6b754c8b | 5320 | struct io_async_msghdr iomsg, *kmsg; |
4c3c0943 | 5321 | struct io_sr_msg *sr = &req->sr_msg; |
0fa03c62 | 5322 | struct socket *sock; |
7a7cacba | 5323 | unsigned flags; |
0031275d | 5324 | int min_ret = 0; |
0fa03c62 JA |
5325 | int ret; |
5326 | ||
dba4a925 | 5327 | sock = sock_from_file(req->file); |
7a7cacba | 5328 | if (unlikely(!sock)) |
dba4a925 | 5329 | return -ENOTSOCK; |
3529d8c2 | 5330 | |
d886e185 PB |
5331 | if (req_has_async_data(req)) { |
5332 | kmsg = req->async_data; | |
5333 | } else { | |
7a7cacba PB |
5334 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
5335 | if (ret) | |
5336 | return ret; | |
5337 | kmsg = &iomsg; | |
0fa03c62 | 5338 | } |
0fa03c62 | 5339 | |
04411806 PB |
5340 | flags = req->sr_msg.msg_flags; |
5341 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 5342 | flags |= MSG_DONTWAIT; |
0031275d SM |
5343 | if (flags & MSG_WAITALL) |
5344 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
5345 | ||
7a7cacba | 5346 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
0fa03c62 | 5347 | |
7297ce3d PB |
5348 | if (ret < min_ret) { |
5349 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) | |
5350 | return io_setup_async_msg(req, kmsg); | |
5351 | if (ret == -ERESTARTSYS) | |
5352 | ret = -EINTR; | |
4c3c0943 JA |
5353 | if (ret > 0 && io_net_retry(sock, flags)) { |
5354 | sr->done_io += ret; | |
5355 | req->flags |= REQ_F_PARTIAL_IO; | |
5356 | return io_setup_async_msg(req, kmsg); | |
5357 | } | |
7297ce3d PB |
5358 | req_set_fail(req); |
5359 | } | |
257e84a5 PB |
5360 | /* fast path, check for non-NULL to avoid function call */ |
5361 | if (kmsg->free_iov) | |
5362 | kfree(kmsg->free_iov); | |
99bc4c38 | 5363 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4c3c0943 JA |
5364 | if (ret >= 0) |
5365 | ret += sr->done_io; | |
5366 | else if (sr->done_io) | |
5367 | ret = sr->done_io; | |
889fca73 | 5368 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 5369 | return 0; |
03b1230c | 5370 | } |
aa1fa28f | 5371 | |
889fca73 | 5372 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 5373 | { |
7a7cacba PB |
5374 | struct io_sr_msg *sr = &req->sr_msg; |
5375 | struct msghdr msg; | |
5376 | struct iovec iov; | |
fddaface | 5377 | struct socket *sock; |
7a7cacba | 5378 | unsigned flags; |
0031275d | 5379 | int min_ret = 0; |
fddaface JA |
5380 | int ret; |
5381 | ||
dba4a925 | 5382 | sock = sock_from_file(req->file); |
7a7cacba | 5383 | if (unlikely(!sock)) |
dba4a925 | 5384 | return -ENOTSOCK; |
fddaface | 5385 | |
7a7cacba PB |
5386 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
5387 | if (unlikely(ret)) | |
14db8411 | 5388 | return ret; |
fddaface | 5389 | |
7a7cacba PB |
5390 | msg.msg_name = NULL; |
5391 | msg.msg_control = NULL; | |
5392 | msg.msg_controllen = 0; | |
5393 | msg.msg_namelen = 0; | |
fddaface | 5394 | |
04411806 PB |
5395 | flags = req->sr_msg.msg_flags; |
5396 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 5397 | flags |= MSG_DONTWAIT; |
0031275d SM |
5398 | if (flags & MSG_WAITALL) |
5399 | min_ret = iov_iter_count(&msg.msg_iter); | |
5400 | ||
7a7cacba PB |
5401 | msg.msg_flags = flags; |
5402 | ret = sock_sendmsg(sock, &msg); | |
7297ce3d PB |
5403 | if (ret < min_ret) { |
5404 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) | |
5405 | return -EAGAIN; | |
5406 | if (ret == -ERESTARTSYS) | |
5407 | ret = -EINTR; | |
4c3c0943 JA |
5408 | if (ret > 0 && io_net_retry(sock, flags)) { |
5409 | sr->len -= ret; | |
5410 | sr->buf += ret; | |
5411 | sr->done_io += ret; | |
5412 | req->flags |= REQ_F_PARTIAL_IO; | |
5413 | return -EAGAIN; | |
5414 | } | |
93d2bcd2 | 5415 | req_set_fail(req); |
7297ce3d | 5416 | } |
4c3c0943 JA |
5417 | if (ret >= 0) |
5418 | ret += sr->done_io; | |
5419 | else if (sr->done_io) | |
5420 | ret = sr->done_io; | |
889fca73 | 5421 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 5422 | return 0; |
fddaface JA |
5423 | } |
5424 | ||
1400e697 PB |
5425 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
5426 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
5427 | { |
5428 | struct io_sr_msg *sr = &req->sr_msg; | |
5429 | struct iovec __user *uiov; | |
5430 | size_t iov_len; | |
5431 | int ret; | |
5432 | ||
1400e697 PB |
5433 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
5434 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
5435 | if (ret) |
5436 | return ret; | |
5437 | ||
5438 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
5439 | if (iov_len > 1) | |
5440 | return -EINVAL; | |
5476dfed | 5441 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 5442 | return -EFAULT; |
5476dfed | 5443 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 5444 | iomsg->free_iov = NULL; |
52de1fe1 | 5445 | } else { |
257e84a5 | 5446 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 5447 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 5448 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 5449 | false); |
52de1fe1 JA |
5450 | if (ret > 0) |
5451 | ret = 0; | |
5452 | } | |
5453 | ||
5454 | return ret; | |
5455 | } | |
5456 | ||
5457 | #ifdef CONFIG_COMPAT | |
5458 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 5459 | struct io_async_msghdr *iomsg) |
52de1fe1 | 5460 | { |
52de1fe1 JA |
5461 | struct io_sr_msg *sr = &req->sr_msg; |
5462 | struct compat_iovec __user *uiov; | |
5463 | compat_uptr_t ptr; | |
5464 | compat_size_t len; | |
5465 | int ret; | |
5466 | ||
4af3417a PB |
5467 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
5468 | &ptr, &len); | |
52de1fe1 JA |
5469 | if (ret) |
5470 | return ret; | |
5471 | ||
5472 | uiov = compat_ptr(ptr); | |
5473 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
5474 | compat_ssize_t clen; | |
5475 | ||
5476 | if (len > 1) | |
5477 | return -EINVAL; | |
5478 | if (!access_ok(uiov, sizeof(*uiov))) | |
5479 | return -EFAULT; | |
5480 | if (__get_user(clen, &uiov->iov_len)) | |
5481 | return -EFAULT; | |
5482 | if (clen < 0) | |
5483 | return -EINVAL; | |
2d280bc8 | 5484 | sr->len = clen; |
257e84a5 | 5485 | iomsg->free_iov = NULL; |
52de1fe1 | 5486 | } else { |
257e84a5 | 5487 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 5488 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 5489 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 5490 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
5491 | if (ret < 0) |
5492 | return ret; | |
5493 | } | |
5494 | ||
5495 | return 0; | |
5496 | } | |
5497 | #endif | |
5498 | ||
1400e697 PB |
5499 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
5500 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 5501 | { |
1400e697 | 5502 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
5503 | |
5504 | #ifdef CONFIG_COMPAT | |
5505 | if (req->ctx->compat) | |
1400e697 | 5506 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 5507 | #endif |
52de1fe1 | 5508 | |
1400e697 | 5509 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
5510 | } |
5511 | ||
bcda7baa | 5512 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
51aac424 | 5513 | unsigned int issue_flags) |
bcda7baa JA |
5514 | { |
5515 | struct io_sr_msg *sr = &req->sr_msg; | |
bcda7baa | 5516 | |
51aac424 | 5517 | return io_buffer_select(req, &sr->len, sr->bgid, issue_flags); |
fddaface JA |
5518 | } |
5519 | ||
93642ef8 | 5520 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 5521 | { |
99bc4c38 | 5522 | int ret; |
3529d8c2 | 5523 | |
93642ef8 PB |
5524 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
5525 | if (!ret) | |
5526 | req->flags |= REQ_F_NEED_CLEANUP; | |
5527 | return ret; | |
5528 | } | |
5529 | ||
5530 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
5531 | { | |
5532 | struct io_sr_msg *sr = &req->sr_msg; | |
5533 | ||
d2b6f48b PB |
5534 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5535 | return -EINVAL; | |
5536 | ||
270a5940 | 5537 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 5538 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 5539 | sr->bgid = READ_ONCE(sqe->buf_group); |
04411806 PB |
5540 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
5541 | if (sr->msg_flags & MSG_DONTWAIT) | |
5542 | req->flags |= REQ_F_NOWAIT; | |
06b76d44 | 5543 | |
d8768362 JA |
5544 | #ifdef CONFIG_COMPAT |
5545 | if (req->ctx->compat) | |
5546 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
5547 | #endif | |
7ba89d2a | 5548 | sr->done_io = 0; |
93642ef8 | 5549 | return 0; |
aa1fa28f JA |
5550 | } |
5551 | ||
889fca73 | 5552 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 5553 | { |
6b754c8b | 5554 | struct io_async_msghdr iomsg, *kmsg; |
7ba89d2a | 5555 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 5556 | struct socket *sock; |
7fbb1b54 | 5557 | struct io_buffer *kbuf; |
7a7cacba | 5558 | unsigned flags; |
d1fd1c20 | 5559 | int ret, min_ret = 0; |
45d189c6 | 5560 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 5561 | |
dba4a925 | 5562 | sock = sock_from_file(req->file); |
7a7cacba | 5563 | if (unlikely(!sock)) |
dba4a925 | 5564 | return -ENOTSOCK; |
3529d8c2 | 5565 | |
d886e185 PB |
5566 | if (req_has_async_data(req)) { |
5567 | kmsg = req->async_data; | |
5568 | } else { | |
7a7cacba PB |
5569 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
5570 | if (ret) | |
681fda8d | 5571 | return ret; |
7a7cacba PB |
5572 | kmsg = &iomsg; |
5573 | } | |
03b1230c | 5574 | |
bc02ef33 | 5575 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 5576 | kbuf = io_recv_buffer_select(req, issue_flags); |
bc02ef33 | 5577 | if (IS_ERR(kbuf)) |
52de1fe1 | 5578 | return PTR_ERR(kbuf); |
7a7cacba | 5579 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
5580 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
5581 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
5582 | 1, req->sr_msg.len); |
5583 | } | |
52de1fe1 | 5584 | |
04411806 PB |
5585 | flags = req->sr_msg.msg_flags; |
5586 | if (force_nonblock) | |
7a7cacba | 5587 | flags |= MSG_DONTWAIT; |
0031275d SM |
5588 | if (flags & MSG_WAITALL) |
5589 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
5590 | ||
7a7cacba PB |
5591 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
5592 | kmsg->uaddr, flags); | |
7297ce3d PB |
5593 | if (ret < min_ret) { |
5594 | if (ret == -EAGAIN && force_nonblock) | |
5595 | return io_setup_async_msg(req, kmsg); | |
5596 | if (ret == -ERESTARTSYS) | |
5597 | ret = -EINTR; | |
7ba89d2a JA |
5598 | if (ret > 0 && io_net_retry(sock, flags)) { |
5599 | sr->done_io += ret; | |
8a3e8ee5 | 5600 | req->flags |= REQ_F_PARTIAL_IO; |
7ba89d2a JA |
5601 | return io_setup_async_msg(req, kmsg); |
5602 | } | |
7297ce3d PB |
5603 | req_set_fail(req); |
5604 | } else if ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { | |
5605 | req_set_fail(req); | |
5606 | } | |
03b1230c | 5607 | |
257e84a5 PB |
5608 | /* fast path, check for non-NULL to avoid function call */ |
5609 | if (kmsg->free_iov) | |
5610 | kfree(kmsg->free_iov); | |
99bc4c38 | 5611 | req->flags &= ~REQ_F_NEED_CLEANUP; |
7ba89d2a JA |
5612 | if (ret >= 0) |
5613 | ret += sr->done_io; | |
5614 | else if (sr->done_io) | |
5615 | ret = sr->done_io; | |
cc3cec83 | 5616 | __io_req_complete(req, issue_flags, ret, io_put_kbuf(req, issue_flags)); |
03b1230c | 5617 | return 0; |
0fa03c62 | 5618 | } |
5d17b4a4 | 5619 | |
889fca73 | 5620 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 5621 | { |
6b754c8b | 5622 | struct io_buffer *kbuf; |
7a7cacba PB |
5623 | struct io_sr_msg *sr = &req->sr_msg; |
5624 | struct msghdr msg; | |
5625 | void __user *buf = sr->buf; | |
fddaface | 5626 | struct socket *sock; |
7a7cacba PB |
5627 | struct iovec iov; |
5628 | unsigned flags; | |
d1fd1c20 | 5629 | int ret, min_ret = 0; |
45d189c6 | 5630 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 5631 | |
dba4a925 | 5632 | sock = sock_from_file(req->file); |
7a7cacba | 5633 | if (unlikely(!sock)) |
dba4a925 | 5634 | return -ENOTSOCK; |
fddaface | 5635 | |
bc02ef33 | 5636 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 5637 | kbuf = io_recv_buffer_select(req, issue_flags); |
bcda7baa JA |
5638 | if (IS_ERR(kbuf)) |
5639 | return PTR_ERR(kbuf); | |
7a7cacba | 5640 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 5641 | } |
bcda7baa | 5642 | |
7a7cacba | 5643 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
5644 | if (unlikely(ret)) |
5645 | goto out_free; | |
fddaface | 5646 | |
7a7cacba PB |
5647 | msg.msg_name = NULL; |
5648 | msg.msg_control = NULL; | |
5649 | msg.msg_controllen = 0; | |
5650 | msg.msg_namelen = 0; | |
5651 | msg.msg_iocb = NULL; | |
5652 | msg.msg_flags = 0; | |
fddaface | 5653 | |
04411806 PB |
5654 | flags = req->sr_msg.msg_flags; |
5655 | if (force_nonblock) | |
7a7cacba | 5656 | flags |= MSG_DONTWAIT; |
0031275d SM |
5657 | if (flags & MSG_WAITALL) |
5658 | min_ret = iov_iter_count(&msg.msg_iter); | |
5659 | ||
7a7cacba | 5660 | ret = sock_recvmsg(sock, &msg, flags); |
7297ce3d PB |
5661 | if (ret < min_ret) { |
5662 | if (ret == -EAGAIN && force_nonblock) | |
5663 | return -EAGAIN; | |
5664 | if (ret == -ERESTARTSYS) | |
5665 | ret = -EINTR; | |
7ba89d2a JA |
5666 | if (ret > 0 && io_net_retry(sock, flags)) { |
5667 | sr->len -= ret; | |
5668 | sr->buf += ret; | |
5669 | sr->done_io += ret; | |
8a3e8ee5 | 5670 | req->flags |= REQ_F_PARTIAL_IO; |
7ba89d2a JA |
5671 | return -EAGAIN; |
5672 | } | |
7297ce3d PB |
5673 | req_set_fail(req); |
5674 | } else if ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { | |
0d7c1153 | 5675 | out_free: |
93d2bcd2 | 5676 | req_set_fail(req); |
7297ce3d | 5677 | } |
cc3cec83 | 5678 | |
7ba89d2a JA |
5679 | if (ret >= 0) |
5680 | ret += sr->done_io; | |
5681 | else if (sr->done_io) | |
5682 | ret = sr->done_io; | |
cc3cec83 | 5683 | __io_req_complete(req, issue_flags, ret, io_put_kbuf(req, issue_flags)); |
fddaface | 5684 | return 0; |
fddaface JA |
5685 | } |
5686 | ||
3529d8c2 | 5687 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 5688 | { |
8ed8d3c3 JA |
5689 | struct io_accept *accept = &req->accept; |
5690 | ||
14587a46 | 5691 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 5692 | return -EINVAL; |
aaa4db12 | 5693 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
5694 | return -EINVAL; |
5695 | ||
d55e5f5b JA |
5696 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5697 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 5698 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 5699 | accept->nofile = rlimit(RLIMIT_NOFILE); |
a7083ad5 | 5700 | |
aaa4db12 | 5701 | accept->file_slot = READ_ONCE(sqe->file_index); |
adf3a9e9 | 5702 | if (accept->file_slot && (accept->flags & SOCK_CLOEXEC)) |
aaa4db12 | 5703 | return -EINVAL; |
a7083ad5 PB |
5704 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
5705 | return -EINVAL; | |
5706 | if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) | |
5707 | accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; | |
8ed8d3c3 | 5708 | return 0; |
8ed8d3c3 | 5709 | } |
17f2fe35 | 5710 | |
889fca73 | 5711 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
5712 | { |
5713 | struct io_accept *accept = &req->accept; | |
45d189c6 | 5714 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 5715 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
aaa4db12 | 5716 | bool fixed = !!accept->file_slot; |
a7083ad5 PB |
5717 | struct file *file; |
5718 | int ret, fd; | |
8ed8d3c3 | 5719 | |
aaa4db12 PB |
5720 | if (!fixed) { |
5721 | fd = __get_unused_fd_flags(accept->flags, accept->nofile); | |
5722 | if (unlikely(fd < 0)) | |
5723 | return fd; | |
5724 | } | |
a7083ad5 PB |
5725 | file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, |
5726 | accept->flags); | |
5727 | if (IS_ERR(file)) { | |
aaa4db12 PB |
5728 | if (!fixed) |
5729 | put_unused_fd(fd); | |
a7083ad5 PB |
5730 | ret = PTR_ERR(file); |
5731 | if (ret == -EAGAIN && force_nonblock) | |
5732 | return -EAGAIN; | |
ac45abc0 PB |
5733 | if (ret == -ERESTARTSYS) |
5734 | ret = -EINTR; | |
93d2bcd2 | 5735 | req_set_fail(req); |
aaa4db12 | 5736 | } else if (!fixed) { |
a7083ad5 PB |
5737 | fd_install(fd, file); |
5738 | ret = fd; | |
aaa4db12 PB |
5739 | } else { |
5740 | ret = io_install_fixed_file(req, file, issue_flags, | |
5741 | accept->file_slot - 1); | |
ac45abc0 | 5742 | } |
889fca73 | 5743 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 5744 | return 0; |
8ed8d3c3 JA |
5745 | } |
5746 | ||
93642ef8 PB |
5747 | static int io_connect_prep_async(struct io_kiocb *req) |
5748 | { | |
5749 | struct io_async_connect *io = req->async_data; | |
5750 | struct io_connect *conn = &req->connect; | |
5751 | ||
5752 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
5753 | } | |
5754 | ||
3529d8c2 | 5755 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 5756 | { |
3529d8c2 | 5757 | struct io_connect *conn = &req->connect; |
f499a021 | 5758 | |
14587a46 | 5759 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 | 5760 | return -EINVAL; |
26578cda PB |
5761 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
5762 | sqe->splice_fd_in) | |
3fbb51c1 JA |
5763 | return -EINVAL; |
5764 | ||
3529d8c2 JA |
5765 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5766 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 5767 | return 0; |
f499a021 JA |
5768 | } |
5769 | ||
889fca73 | 5770 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 5771 | { |
e8c2bc1f | 5772 | struct io_async_connect __io, *io; |
f8e85cf2 | 5773 | unsigned file_flags; |
3fbb51c1 | 5774 | int ret; |
45d189c6 | 5775 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 5776 | |
d886e185 | 5777 | if (req_has_async_data(req)) { |
e8c2bc1f | 5778 | io = req->async_data; |
f499a021 | 5779 | } else { |
3529d8c2 JA |
5780 | ret = move_addr_to_kernel(req->connect.addr, |
5781 | req->connect.addr_len, | |
e8c2bc1f | 5782 | &__io.address); |
f499a021 JA |
5783 | if (ret) |
5784 | goto out; | |
5785 | io = &__io; | |
5786 | } | |
5787 | ||
3fbb51c1 JA |
5788 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
5789 | ||
e8c2bc1f | 5790 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 5791 | req->connect.addr_len, file_flags); |
87f80d62 | 5792 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
d886e185 | 5793 | if (req_has_async_data(req)) |
b7bb4f7d | 5794 | return -EAGAIN; |
e8c2bc1f | 5795 | if (io_alloc_async_data(req)) { |
f499a021 JA |
5796 | ret = -ENOMEM; |
5797 | goto out; | |
5798 | } | |
e8c2bc1f | 5799 | memcpy(req->async_data, &__io, sizeof(__io)); |
f8e85cf2 | 5800 | return -EAGAIN; |
f499a021 | 5801 | } |
f8e85cf2 JA |
5802 | if (ret == -ERESTARTSYS) |
5803 | ret = -EINTR; | |
f499a021 | 5804 | out: |
4e88d6e7 | 5805 | if (ret < 0) |
93d2bcd2 | 5806 | req_set_fail(req); |
889fca73 | 5807 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 5808 | return 0; |
469956e8 Y |
5809 | } |
5810 | #else /* !CONFIG_NET */ | |
99a10081 JA |
5811 | #define IO_NETOP_FN(op) \ |
5812 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
5813 | { \ | |
5814 | return -EOPNOTSUPP; \ | |
5815 | } | |
5816 | ||
5817 | #define IO_NETOP_PREP(op) \ | |
5818 | IO_NETOP_FN(op) \ | |
5819 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
5820 | { \ | |
5821 | return -EOPNOTSUPP; \ | |
5822 | } \ | |
5823 | ||
5824 | #define IO_NETOP_PREP_ASYNC(op) \ | |
5825 | IO_NETOP_PREP(op) \ | |
5826 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
5827 | { \ | |
5828 | return -EOPNOTSUPP; \ | |
5829 | } | |
5830 | ||
5831 | IO_NETOP_PREP_ASYNC(sendmsg); | |
5832 | IO_NETOP_PREP_ASYNC(recvmsg); | |
5833 | IO_NETOP_PREP_ASYNC(connect); | |
5834 | IO_NETOP_PREP(accept); | |
5835 | IO_NETOP_FN(send); | |
5836 | IO_NETOP_FN(recv); | |
469956e8 | 5837 | #endif /* CONFIG_NET */ |
f8e85cf2 | 5838 | |
d7718a9d JA |
5839 | struct io_poll_table { |
5840 | struct poll_table_struct pt; | |
5841 | struct io_kiocb *req; | |
68b11e8b | 5842 | int nr_entries; |
d7718a9d JA |
5843 | int error; |
5844 | }; | |
ce593a6c | 5845 | |
aa43477b | 5846 | #define IO_POLL_CANCEL_FLAG BIT(31) |
e2c0cb7c | 5847 | #define IO_POLL_REF_MASK GENMASK(30, 0) |
6d816e08 | 5848 | |
aa43477b PB |
5849 | /* |
5850 | * If refs part of ->poll_refs (see IO_POLL_REF_MASK) is 0, it's free. We can | |
5851 | * bump it and acquire ownership. It's disallowed to modify requests while not | |
5852 | * owning it, that prevents from races for enqueueing task_work's and b/w | |
5853 | * arming poll and wakeups. | |
5854 | */ | |
5855 | static inline bool io_poll_get_ownership(struct io_kiocb *req) | |
5856 | { | |
5857 | return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK); | |
d7718a9d JA |
5858 | } |
5859 | ||
aa43477b | 5860 | static void io_poll_mark_cancelled(struct io_kiocb *req) |
74ce6ce4 | 5861 | { |
aa43477b | 5862 | atomic_or(IO_POLL_CANCEL_FLAG, &req->poll_refs); |
74ce6ce4 JA |
5863 | } |
5864 | ||
d4e7cd36 | 5865 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5866 | { |
e8c2bc1f | 5867 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5868 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5869 | return req->async_data; |
d4e7cd36 JA |
5870 | return req->apoll->double_poll; |
5871 | } | |
5872 | ||
5873 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5874 | { | |
5875 | if (req->opcode == IORING_OP_POLL_ADD) | |
5876 | return &req->poll; | |
5877 | return &req->apoll->poll; | |
5878 | } | |
5879 | ||
5641897a | 5880 | static void io_poll_req_insert(struct io_kiocb *req) |
d4e7cd36 | 5881 | { |
5641897a PB |
5882 | struct io_ring_ctx *ctx = req->ctx; |
5883 | struct hlist_head *list; | |
18bceab1 | 5884 | |
cef216fc | 5885 | list = &ctx->cancel_hash[hash_long(req->cqe.user_data, ctx->cancel_hash_bits)]; |
5641897a | 5886 | hlist_add_head(&req->hash_node, list); |
18bceab1 JA |
5887 | } |
5888 | ||
5641897a PB |
5889 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
5890 | wait_queue_func_t wake_func) | |
18bceab1 | 5891 | { |
5641897a | 5892 | poll->head = NULL; |
5641897a PB |
5893 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
5894 | /* mask in events that we always want/need */ | |
5895 | poll->events = events | IO_POLL_UNMASK; | |
5896 | INIT_LIST_HEAD(&poll->wait.entry); | |
5897 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
18bceab1 JA |
5898 | } |
5899 | ||
aa43477b | 5900 | static inline void io_poll_remove_entry(struct io_poll_iocb *poll) |
18bceab1 | 5901 | { |
791f3465 | 5902 | struct wait_queue_head *head = smp_load_acquire(&poll->head); |
18bceab1 | 5903 | |
791f3465 PB |
5904 | if (head) { |
5905 | spin_lock_irq(&head->lock); | |
5906 | list_del_init(&poll->wait.entry); | |
5907 | poll->head = NULL; | |
5908 | spin_unlock_irq(&head->lock); | |
5909 | } | |
aa43477b | 5910 | } |
18bceab1 | 5911 | |
aa43477b PB |
5912 | static void io_poll_remove_entries(struct io_kiocb *req) |
5913 | { | |
91eac1c6 JA |
5914 | /* |
5915 | * Nothing to do if neither of those flags are set. Avoid dipping | |
5916 | * into the poll/apoll/double cachelines if we can. | |
5917 | */ | |
5918 | if (!(req->flags & (REQ_F_SINGLE_POLL | REQ_F_DOUBLE_POLL))) | |
5919 | return; | |
18bceab1 | 5920 | |
791f3465 PB |
5921 | /* |
5922 | * While we hold the waitqueue lock and the waitqueue is nonempty, | |
5923 | * wake_up_pollfree() will wait for us. However, taking the waitqueue | |
5924 | * lock in the first place can race with the waitqueue being freed. | |
5925 | * | |
5926 | * We solve this as eventpoll does: by taking advantage of the fact that | |
5927 | * all users of wake_up_pollfree() will RCU-delay the actual free. If | |
5928 | * we enter rcu_read_lock() and see that the pointer to the queue is | |
5929 | * non-NULL, we can then lock it without the memory being freed out from | |
5930 | * under us. | |
5931 | * | |
5932 | * Keep holding rcu_read_lock() as long as we hold the queue lock, in | |
5933 | * case the caller deletes the entry from the queue, leaving it empty. | |
5934 | * In that case, only RCU prevents the queue memory from being freed. | |
5935 | */ | |
5936 | rcu_read_lock(); | |
91eac1c6 JA |
5937 | if (req->flags & REQ_F_SINGLE_POLL) |
5938 | io_poll_remove_entry(io_poll_get_single(req)); | |
5939 | if (req->flags & REQ_F_DOUBLE_POLL) | |
5940 | io_poll_remove_entry(io_poll_get_double(req)); | |
791f3465 | 5941 | rcu_read_unlock(); |
18bceab1 JA |
5942 | } |
5943 | ||
aa43477b PB |
5944 | /* |
5945 | * All poll tw should go through this. Checks for poll events, manages | |
5946 | * references, does rewait, etc. | |
5947 | * | |
5948 | * Returns a negative error on failure. >0 when no action require, which is | |
5949 | * either spurious wakeup or multishot CQE is served. 0 when it's done with | |
cef216fc | 5950 | * the request, then the mask is stored in req->cqe.res. |
aa43477b | 5951 | */ |
5106dd6e | 5952 | static int io_poll_check_events(struct io_kiocb *req, bool locked) |
18bceab1 | 5953 | { |
74ce6ce4 | 5954 | struct io_ring_ctx *ctx = req->ctx; |
aa43477b | 5955 | int v; |
18bceab1 | 5956 | |
316319e8 | 5957 | /* req->task == current here, checking PF_EXITING is safe */ |
e09ee510 | 5958 | if (unlikely(req->task->flags & PF_EXITING)) |
f2219057 | 5959 | return -ECANCELED; |
18bceab1 | 5960 | |
aa43477b PB |
5961 | do { |
5962 | v = atomic_read(&req->poll_refs); | |
74ce6ce4 | 5963 | |
aa43477b PB |
5964 | /* tw handler should be the owner, and so have some references */ |
5965 | if (WARN_ON_ONCE(!(v & IO_POLL_REF_MASK))) | |
5966 | return 0; | |
5967 | if (v & IO_POLL_CANCEL_FLAG) | |
5968 | return -ECANCELED; | |
8706e04e | 5969 | |
cef216fc | 5970 | if (!req->cqe.res) { |
2804ecd8 | 5971 | struct poll_table_struct pt = { ._key = req->apoll_events }; |
cce64ef0 | 5972 | unsigned flags = locked ? 0 : IO_URING_F_UNLOCKED; |
18bceab1 | 5973 | |
cce64ef0 | 5974 | if (unlikely(!io_assign_file(req, flags))) |
7179c3ce | 5975 | return -EBADF; |
cef216fc | 5976 | req->cqe.res = vfs_poll(req->file, &pt) & req->apoll_events; |
c8b5e260 | 5977 | } |
74ce6ce4 | 5978 | |
aa43477b | 5979 | /* multishot, just fill an CQE and proceed */ |
cef216fc PB |
5980 | if (req->cqe.res && !(req->apoll_events & EPOLLONESHOT)) { |
5981 | __poll_t mask = mangle_poll(req->cqe.res & req->apoll_events); | |
aa43477b | 5982 | bool filled; |
18bceab1 | 5983 | |
aa43477b | 5984 | spin_lock(&ctx->completion_lock); |
cef216fc | 5985 | filled = io_fill_cqe_aux(ctx, req->cqe.user_data, mask, |
aa43477b PB |
5986 | IORING_CQE_F_MORE); |
5987 | io_commit_cqring(ctx); | |
5988 | spin_unlock(&ctx->completion_lock); | |
5989 | if (unlikely(!filled)) | |
5990 | return -ECANCELED; | |
5991 | io_cqring_ev_posted(ctx); | |
cef216fc | 5992 | } else if (req->cqe.res) { |
aa43477b PB |
5993 | return 0; |
5994 | } | |
18bceab1 | 5995 | |
aa43477b PB |
5996 | /* |
5997 | * Release all references, retry if someone tried to restart | |
5998 | * task_work while we were executing it. | |
5999 | */ | |
6000 | } while (atomic_sub_return(v & IO_POLL_REF_MASK, &req->poll_refs)); | |
18bceab1 | 6001 | |
18bceab1 JA |
6002 | return 1; |
6003 | } | |
6004 | ||
aa43477b | 6005 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 | 6006 | { |
18bceab1 | 6007 | struct io_ring_ctx *ctx = req->ctx; |
aa43477b | 6008 | int ret; |
18bceab1 | 6009 | |
5106dd6e | 6010 | ret = io_poll_check_events(req, *locked); |
aa43477b PB |
6011 | if (ret > 0) |
6012 | return; | |
6013 | ||
6014 | if (!ret) { | |
cef216fc | 6015 | req->cqe.res = mangle_poll(req->cqe.res & req->poll.events); |
e27414be | 6016 | } else { |
cef216fc | 6017 | req->cqe.res = ret; |
aa43477b | 6018 | req_set_fail(req); |
a62682f9 | 6019 | } |
aa43477b PB |
6020 | |
6021 | io_poll_remove_entries(req); | |
6022 | spin_lock(&ctx->completion_lock); | |
6023 | hash_del(&req->hash_node); | |
cef216fc | 6024 | __io_req_complete_post(req, req->cqe.res, 0); |
aa43477b PB |
6025 | io_commit_cqring(ctx); |
6026 | spin_unlock(&ctx->completion_lock); | |
6027 | io_cqring_ev_posted(ctx); | |
18bceab1 JA |
6028 | } |
6029 | ||
aa43477b | 6030 | static void io_apoll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 JA |
6031 | { |
6032 | struct io_ring_ctx *ctx = req->ctx; | |
aa43477b | 6033 | int ret; |
18bceab1 | 6034 | |
5106dd6e | 6035 | ret = io_poll_check_events(req, *locked); |
aa43477b PB |
6036 | if (ret > 0) |
6037 | return; | |
18bceab1 | 6038 | |
aa43477b PB |
6039 | io_poll_remove_entries(req); |
6040 | spin_lock(&ctx->completion_lock); | |
6041 | hash_del(&req->hash_node); | |
6042 | spin_unlock(&ctx->completion_lock); | |
18bceab1 | 6043 | |
aa43477b PB |
6044 | if (!ret) |
6045 | io_req_task_submit(req, locked); | |
6046 | else | |
6047 | io_req_complete_failed(req, ret); | |
18bceab1 JA |
6048 | } |
6049 | ||
81459350 | 6050 | static void __io_poll_execute(struct io_kiocb *req, int mask, int events) |
aa43477b | 6051 | { |
cef216fc | 6052 | req->cqe.res = mask; |
81459350 JA |
6053 | /* |
6054 | * This is useful for poll that is armed on behalf of another | |
6055 | * request, and where the wakeup path could be on a different | |
6056 | * CPU. We want to avoid pulling in req->apoll->events for that | |
6057 | * case. | |
6058 | */ | |
2804ecd8 | 6059 | req->apoll_events = events; |
aa43477b PB |
6060 | if (req->opcode == IORING_OP_POLL_ADD) |
6061 | req->io_task_work.func = io_poll_task_func; | |
6062 | else | |
6063 | req->io_task_work.func = io_apoll_task_func; | |
6064 | ||
cef216fc | 6065 | trace_io_uring_task_add(req->ctx, req, req->cqe.user_data, req->opcode, mask); |
aa43477b PB |
6066 | io_req_task_work_add(req, false); |
6067 | } | |
6068 | ||
81459350 | 6069 | static inline void io_poll_execute(struct io_kiocb *req, int res, int events) |
aa43477b PB |
6070 | { |
6071 | if (io_poll_get_ownership(req)) | |
81459350 | 6072 | __io_poll_execute(req, res, events); |
aa43477b PB |
6073 | } |
6074 | ||
6075 | static void io_poll_cancel_req(struct io_kiocb *req) | |
6076 | { | |
6077 | io_poll_mark_cancelled(req); | |
6078 | /* kick tw, which should complete the request */ | |
81459350 | 6079 | io_poll_execute(req, 0, 0); |
aa43477b PB |
6080 | } |
6081 | ||
d89a4fac JA |
6082 | #define wqe_to_req(wait) ((void *)((unsigned long) (wait)->private & ~1)) |
6083 | #define wqe_is_double(wait) ((unsigned long) (wait)->private & 1) | |
6084 | ||
aa43477b PB |
6085 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
6086 | void *key) | |
18bceab1 | 6087 | { |
d89a4fac | 6088 | struct io_kiocb *req = wqe_to_req(wait); |
aa43477b PB |
6089 | struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb, |
6090 | wait); | |
18bceab1 JA |
6091 | __poll_t mask = key_to_poll(key); |
6092 | ||
791f3465 PB |
6093 | if (unlikely(mask & POLLFREE)) { |
6094 | io_poll_mark_cancelled(req); | |
6095 | /* we have to kick tw in case it's not already */ | |
81459350 | 6096 | io_poll_execute(req, 0, poll->events); |
791f3465 PB |
6097 | |
6098 | /* | |
6099 | * If the waitqueue is being freed early but someone is already | |
6100 | * holds ownership over it, we have to tear down the request as | |
6101 | * best we can. That means immediately removing the request from | |
6102 | * its waitqueue and preventing all further accesses to the | |
6103 | * waitqueue via the request. | |
6104 | */ | |
6105 | list_del_init(&poll->wait.entry); | |
6106 | ||
6107 | /* | |
6108 | * Careful: this *must* be the last step, since as soon | |
6109 | * as req->head is NULL'ed out, the request can be | |
6110 | * completed and freed, since aio_poll_complete_work() | |
6111 | * will no longer need to take the waitqueue lock. | |
6112 | */ | |
6113 | smp_store_release(&poll->head, NULL); | |
6114 | return 1; | |
6115 | } | |
6116 | ||
aa43477b | 6117 | /* for instances that support it check for an event match first */ |
18bceab1 JA |
6118 | if (mask && !(mask & poll->events)) |
6119 | return 0; | |
6120 | ||
eb0089d6 PB |
6121 | if (io_poll_get_ownership(req)) { |
6122 | /* optional, saves extra locking for removal in tw handler */ | |
6123 | if (mask && poll->events & EPOLLONESHOT) { | |
6124 | list_del_init(&poll->wait.entry); | |
6125 | poll->head = NULL; | |
d89a4fac JA |
6126 | if (wqe_is_double(wait)) |
6127 | req->flags &= ~REQ_F_DOUBLE_POLL; | |
6128 | else | |
6129 | req->flags &= ~REQ_F_SINGLE_POLL; | |
eb0089d6 | 6130 | } |
81459350 | 6131 | __io_poll_execute(req, mask, poll->events); |
eb0089d6 | 6132 | } |
18bceab1 | 6133 | return 1; |
18bceab1 JA |
6134 | } |
6135 | ||
6136 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
6137 | struct wait_queue_head *head, |
6138 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
6139 | { |
6140 | struct io_kiocb *req = pt->req; | |
d89a4fac | 6141 | unsigned long wqe_private = (unsigned long) req; |
18bceab1 JA |
6142 | |
6143 | /* | |
68b11e8b PB |
6144 | * The file being polled uses multiple waitqueues for poll handling |
6145 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb | |
6146 | * if this happens. | |
18bceab1 | 6147 | */ |
68b11e8b | 6148 | if (unlikely(pt->nr_entries)) { |
aa43477b | 6149 | struct io_poll_iocb *first = poll; |
58852d4d | 6150 | |
23a65db8 | 6151 | /* double add on the same waitqueue head, ignore */ |
aa43477b | 6152 | if (first->head == head) |
23a65db8 | 6153 | return; |
18bceab1 | 6154 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 6155 | if (*poll_ptr) { |
23a65db8 PB |
6156 | if ((*poll_ptr)->head == head) |
6157 | return; | |
18bceab1 JA |
6158 | pt->error = -EINVAL; |
6159 | return; | |
6160 | } | |
aa43477b | 6161 | |
18bceab1 JA |
6162 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
6163 | if (!poll) { | |
6164 | pt->error = -ENOMEM; | |
6165 | return; | |
6166 | } | |
d89a4fac JA |
6167 | /* mark as double wq entry */ |
6168 | wqe_private |= 1; | |
91eac1c6 | 6169 | req->flags |= REQ_F_DOUBLE_POLL; |
aa43477b | 6170 | io_init_poll_iocb(poll, first->events, first->wait.func); |
807abcb0 | 6171 | *poll_ptr = poll; |
d886e185 PB |
6172 | if (req->opcode == IORING_OP_POLL_ADD) |
6173 | req->flags |= REQ_F_ASYNC_DATA; | |
18bceab1 JA |
6174 | } |
6175 | ||
91eac1c6 | 6176 | req->flags |= REQ_F_SINGLE_POLL; |
68b11e8b | 6177 | pt->nr_entries++; |
18bceab1 | 6178 | poll->head = head; |
d89a4fac | 6179 | poll->wait.private = (void *) wqe_private; |
a31eb4a2 JX |
6180 | |
6181 | if (poll->events & EPOLLEXCLUSIVE) | |
6182 | add_wait_queue_exclusive(head, &poll->wait); | |
6183 | else | |
6184 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
6185 | } |
6186 | ||
aa43477b | 6187 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
18bceab1 JA |
6188 | struct poll_table_struct *p) |
6189 | { | |
6190 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
d7718a9d | 6191 | |
aa43477b PB |
6192 | __io_queue_proc(&pt->req->poll, pt, head, |
6193 | (struct io_poll_iocb **) &pt->req->async_data); | |
d7718a9d JA |
6194 | } |
6195 | ||
aa43477b PB |
6196 | static int __io_arm_poll_handler(struct io_kiocb *req, |
6197 | struct io_poll_iocb *poll, | |
6198 | struct io_poll_table *ipt, __poll_t mask) | |
d7718a9d JA |
6199 | { |
6200 | struct io_ring_ctx *ctx = req->ctx; | |
aa43477b | 6201 | int v; |
d7718a9d | 6202 | |
4d52f338 | 6203 | INIT_HLIST_NODE(&req->hash_node); |
8e29da69 | 6204 | req->work.cancel_seq = atomic_read(&ctx->cancel_seq); |
aa43477b | 6205 | io_init_poll_iocb(poll, mask, io_poll_wake); |
b90cd197 | 6206 | poll->file = req->file; |
d7718a9d JA |
6207 | |
6208 | ipt->pt._key = mask; | |
6209 | ipt->req = req; | |
68b11e8b PB |
6210 | ipt->error = 0; |
6211 | ipt->nr_entries = 0; | |
d7718a9d | 6212 | |
aa43477b PB |
6213 | /* |
6214 | * Take the ownership to delay any tw execution up until we're done | |
6215 | * with poll arming. see io_poll_get_ownership(). | |
6216 | */ | |
6217 | atomic_set(&req->poll_refs, 1); | |
d7718a9d | 6218 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
aa43477b PB |
6219 | |
6220 | if (mask && (poll->events & EPOLLONESHOT)) { | |
6221 | io_poll_remove_entries(req); | |
6222 | /* no one else has access to the req, forget about the ref */ | |
6223 | return mask; | |
6224 | } | |
6225 | if (!mask && unlikely(ipt->error || !ipt->nr_entries)) { | |
6226 | io_poll_remove_entries(req); | |
6227 | if (!ipt->error) | |
6228 | ipt->error = -EINVAL; | |
6229 | return 0; | |
6230 | } | |
d7718a9d | 6231 | |
79ebeaee | 6232 | spin_lock(&ctx->completion_lock); |
aa43477b PB |
6233 | io_poll_req_insert(req); |
6234 | spin_unlock(&ctx->completion_lock); | |
6235 | ||
6236 | if (mask) { | |
6237 | /* can't multishot if failed, just queue the event we've got */ | |
6238 | if (unlikely(ipt->error || !ipt->nr_entries)) | |
6239 | poll->events |= EPOLLONESHOT; | |
81459350 | 6240 | __io_poll_execute(req, mask, poll->events); |
aa43477b | 6241 | return 0; |
d7718a9d JA |
6242 | } |
6243 | ||
aa43477b PB |
6244 | /* |
6245 | * Release ownership. If someone tried to queue a tw while it was | |
6246 | * locked, kick it off for them. | |
6247 | */ | |
6248 | v = atomic_dec_return(&req->poll_refs); | |
6249 | if (unlikely(v & IO_POLL_REF_MASK)) | |
81459350 | 6250 | __io_poll_execute(req, 0, poll->events); |
aa43477b PB |
6251 | return 0; |
6252 | } | |
6253 | ||
6254 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
6255 | struct poll_table_struct *p) | |
6256 | { | |
6257 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
6258 | struct async_poll *apoll = pt->req->apoll; | |
6259 | ||
6260 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); | |
d7718a9d JA |
6261 | } |
6262 | ||
59b735ae OL |
6263 | enum { |
6264 | IO_APOLL_OK, | |
6265 | IO_APOLL_ABORTED, | |
6266 | IO_APOLL_READY | |
6267 | }; | |
6268 | ||
4d9237e3 | 6269 | static int io_arm_poll_handler(struct io_kiocb *req, unsigned issue_flags) |
d7718a9d JA |
6270 | { |
6271 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
6272 | struct io_ring_ctx *ctx = req->ctx; | |
6273 | struct async_poll *apoll; | |
6274 | struct io_poll_table ipt; | |
aa43477b PB |
6275 | __poll_t mask = EPOLLONESHOT | POLLERR | POLLPRI; |
6276 | int ret; | |
d7718a9d | 6277 | |
b2d9c3da PB |
6278 | if (!def->pollin && !def->pollout) |
6279 | return IO_APOLL_ABORTED; | |
10c87333 JA |
6280 | if (!file_can_poll(req->file)) |
6281 | return IO_APOLL_ABORTED; | |
6282 | if ((req->flags & (REQ_F_POLLED|REQ_F_PARTIAL_IO)) == REQ_F_POLLED) | |
658d0a40 | 6283 | return IO_APOLL_ABORTED; |
b2d9c3da PB |
6284 | |
6285 | if (def->pollin) { | |
b2d9c3da PB |
6286 | mask |= POLLIN | POLLRDNORM; |
6287 | ||
6288 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
6289 | if ((req->opcode == IORING_OP_RECVMSG) && | |
6290 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
6291 | mask &= ~POLLIN; | |
6292 | } else { | |
b2d9c3da PB |
6293 | mask |= POLLOUT | POLLWRNORM; |
6294 | } | |
52dd8640 DY |
6295 | if (def->poll_exclusive) |
6296 | mask |= EPOLLEXCLUSIVE; | |
10c87333 JA |
6297 | if (req->flags & REQ_F_POLLED) { |
6298 | apoll = req->apoll; | |
6299 | } else if (!(issue_flags & IO_URING_F_UNLOCKED) && | |
6300 | !list_empty(&ctx->apoll_cache)) { | |
4d9237e3 JA |
6301 | apoll = list_first_entry(&ctx->apoll_cache, struct async_poll, |
6302 | poll.wait.entry); | |
6303 | list_del_init(&apoll->poll.wait.entry); | |
6304 | } else { | |
6305 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
6306 | if (unlikely(!apoll)) | |
6307 | return IO_APOLL_ABORTED; | |
6308 | } | |
807abcb0 | 6309 | apoll->double_poll = NULL; |
d7718a9d | 6310 | req->apoll = apoll; |
b2d9c3da | 6311 | req->flags |= REQ_F_POLLED; |
d7718a9d JA |
6312 | ipt.pt._qproc = io_async_queue_proc; |
6313 | ||
4d55f238 | 6314 | io_kbuf_recycle(req, issue_flags); |
abdad709 | 6315 | |
aa43477b | 6316 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask); |
41a5169c HX |
6317 | if (ret || ipt.error) |
6318 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; | |
6319 | ||
cef216fc | 6320 | trace_io_uring_poll_arm(ctx, req, req->cqe.user_data, req->opcode, |
236daeae | 6321 | mask, apoll->poll.events); |
59b735ae | 6322 | return IO_APOLL_OK; |
d7718a9d JA |
6323 | } |
6324 | ||
76e1b642 JA |
6325 | /* |
6326 | * Returns true if we found and killed one or more poll requests | |
6327 | */ | |
c072481d PB |
6328 | static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, |
6329 | struct task_struct *tsk, bool cancel_all) | |
221c5eb2 | 6330 | { |
78076bb6 | 6331 | struct hlist_node *tmp; |
221c5eb2 | 6332 | struct io_kiocb *req; |
aa43477b PB |
6333 | bool found = false; |
6334 | int i; | |
221c5eb2 | 6335 | |
79ebeaee | 6336 | spin_lock(&ctx->completion_lock); |
78076bb6 JA |
6337 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
6338 | struct hlist_head *list; | |
6339 | ||
6340 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 6341 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
42a7b4ed | 6342 | if (io_match_task_safe(req, tsk, cancel_all)) { |
61bc84c4 | 6343 | hlist_del_init(&req->hash_node); |
aa43477b PB |
6344 | io_poll_cancel_req(req); |
6345 | found = true; | |
6346 | } | |
f3606e3a | 6347 | } |
221c5eb2 | 6348 | } |
79ebeaee | 6349 | spin_unlock(&ctx->completion_lock); |
aa43477b | 6350 | return found; |
221c5eb2 JA |
6351 | } |
6352 | ||
b21432b4 JA |
6353 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, bool poll_only, |
6354 | struct io_cancel_data *cd) | |
e07785b0 | 6355 | __must_hold(&ctx->completion_lock) |
47f46768 | 6356 | { |
78076bb6 | 6357 | struct hlist_head *list; |
47f46768 JA |
6358 | struct io_kiocb *req; |
6359 | ||
b21432b4 | 6360 | list = &ctx->cancel_hash[hash_long(cd->data, ctx->cancel_hash_bits)]; |
78076bb6 | 6361 | hlist_for_each_entry(req, list, hash_node) { |
b21432b4 | 6362 | if (cd->data != req->cqe.user_data) |
b41e9852 | 6363 | continue; |
9ba5fac8 PB |
6364 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
6365 | continue; | |
8e29da69 JA |
6366 | if (cd->flags & IORING_ASYNC_CANCEL_ALL) { |
6367 | if (cd->seq == req->work.cancel_seq) | |
6368 | continue; | |
6369 | req->work.cancel_seq = cd->seq; | |
6370 | } | |
b2cb805f | 6371 | return req; |
47f46768 | 6372 | } |
b2cb805f JA |
6373 | return NULL; |
6374 | } | |
6375 | ||
4bf94615 JA |
6376 | static struct io_kiocb *io_poll_file_find(struct io_ring_ctx *ctx, |
6377 | struct io_cancel_data *cd) | |
6378 | __must_hold(&ctx->completion_lock) | |
6379 | { | |
6380 | struct io_kiocb *req; | |
6381 | int i; | |
6382 | ||
6383 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { | |
6384 | struct hlist_head *list; | |
6385 | ||
6386 | list = &ctx->cancel_hash[i]; | |
6387 | hlist_for_each_entry(req, list, hash_node) { | |
970f256e JA |
6388 | if (!(cd->flags & IORING_ASYNC_CANCEL_ANY) && |
6389 | req->file != cd->file) | |
4bf94615 JA |
6390 | continue; |
6391 | if (cd->seq == req->work.cancel_seq) | |
6392 | continue; | |
6393 | req->work.cancel_seq = cd->seq; | |
6394 | return req; | |
6395 | } | |
6396 | } | |
6397 | return NULL; | |
6398 | } | |
6399 | ||
aa43477b PB |
6400 | static bool io_poll_disarm(struct io_kiocb *req) |
6401 | __must_hold(&ctx->completion_lock) | |
6402 | { | |
6403 | if (!io_poll_get_ownership(req)) | |
6404 | return false; | |
6405 | io_poll_remove_entries(req); | |
6406 | hash_del(&req->hash_node); | |
6407 | return true; | |
6408 | } | |
6409 | ||
b21432b4 | 6410 | static int io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd) |
e07785b0 | 6411 | __must_hold(&ctx->completion_lock) |
b2cb805f | 6412 | { |
4bf94615 | 6413 | struct io_kiocb *req; |
b2cb805f | 6414 | |
970f256e | 6415 | if (cd->flags & (IORING_ASYNC_CANCEL_FD|IORING_ASYNC_CANCEL_ANY)) |
4bf94615 JA |
6416 | req = io_poll_file_find(ctx, cd); |
6417 | else | |
6418 | req = io_poll_find(ctx, false, cd); | |
b2cb805f JA |
6419 | if (!req) |
6420 | return -ENOENT; | |
aa43477b PB |
6421 | io_poll_cancel_req(req); |
6422 | return 0; | |
47f46768 JA |
6423 | } |
6424 | ||
9096af3e PB |
6425 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
6426 | unsigned int flags) | |
6427 | { | |
6428 | u32 events; | |
47f46768 | 6429 | |
9096af3e PB |
6430 | events = READ_ONCE(sqe->poll32_events); |
6431 | #ifdef __BIG_ENDIAN | |
6432 | events = swahw32(events); | |
6433 | #endif | |
6434 | if (!(flags & IORING_POLL_ADD_MULTI)) | |
6435 | events |= EPOLLONESHOT; | |
6436 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); | |
47f46768 JA |
6437 | } |
6438 | ||
c5de0036 | 6439 | static int io_poll_update_prep(struct io_kiocb *req, |
3529d8c2 | 6440 | const struct io_uring_sqe *sqe) |
0969e783 | 6441 | { |
c5de0036 PB |
6442 | struct io_poll_update *upd = &req->poll_update; |
6443 | u32 flags; | |
6444 | ||
0969e783 JA |
6445 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
6446 | return -EINVAL; | |
26578cda | 6447 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
c5de0036 PB |
6448 | return -EINVAL; |
6449 | flags = READ_ONCE(sqe->len); | |
6450 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | | |
6451 | IORING_POLL_ADD_MULTI)) | |
6452 | return -EINVAL; | |
6453 | /* meaningless without update */ | |
6454 | if (flags == IORING_POLL_ADD_MULTI) | |
0969e783 JA |
6455 | return -EINVAL; |
6456 | ||
c5de0036 PB |
6457 | upd->old_user_data = READ_ONCE(sqe->addr); |
6458 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; | |
6459 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; | |
221c5eb2 | 6460 | |
c5de0036 PB |
6461 | upd->new_user_data = READ_ONCE(sqe->off); |
6462 | if (!upd->update_user_data && upd->new_user_data) | |
6463 | return -EINVAL; | |
6464 | if (upd->update_events) | |
6465 | upd->events = io_poll_parse_events(sqe, flags); | |
6466 | else if (sqe->poll32_events) | |
6467 | return -EINVAL; | |
221c5eb2 | 6468 | |
221c5eb2 JA |
6469 | return 0; |
6470 | } | |
6471 | ||
3529d8c2 | 6472 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
6473 | { |
6474 | struct io_poll_iocb *poll = &req->poll; | |
c5de0036 | 6475 | u32 flags; |
221c5eb2 JA |
6476 | |
6477 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
6478 | return -EINVAL; | |
c5de0036 | 6479 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
88e41cf9 JA |
6480 | return -EINVAL; |
6481 | flags = READ_ONCE(sqe->len); | |
c5de0036 | 6482 | if (flags & ~IORING_POLL_ADD_MULTI) |
221c5eb2 | 6483 | return -EINVAL; |
04c76b41 PB |
6484 | if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP)) |
6485 | return -EINVAL; | |
221c5eb2 | 6486 | |
48dcd38d | 6487 | io_req_set_refcount(req); |
2804ecd8 | 6488 | req->apoll_events = poll->events = io_poll_parse_events(sqe, flags); |
0969e783 JA |
6489 | return 0; |
6490 | } | |
6491 | ||
61e98203 | 6492 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
6493 | { |
6494 | struct io_poll_iocb *poll = &req->poll; | |
0969e783 | 6495 | struct io_poll_table ipt; |
aa43477b | 6496 | int ret; |
0969e783 | 6497 | |
d7718a9d | 6498 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 6499 | |
aa43477b PB |
6500 | ret = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events); |
6501 | ret = ret ?: ipt.error; | |
6502 | if (ret) | |
6503 | __io_req_complete(req, issue_flags, ret, 0); | |
6504 | return 0; | |
221c5eb2 JA |
6505 | } |
6506 | ||
c5de0036 | 6507 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
b69de288 | 6508 | { |
b21432b4 | 6509 | struct io_cancel_data cd = { .data = req->poll_update.old_user_data, }; |
b69de288 JA |
6510 | struct io_ring_ctx *ctx = req->ctx; |
6511 | struct io_kiocb *preq; | |
2bbb146d | 6512 | int ret2, ret = 0; |
cc8e9ba7 | 6513 | bool locked; |
b69de288 | 6514 | |
79ebeaee | 6515 | spin_lock(&ctx->completion_lock); |
b21432b4 | 6516 | preq = io_poll_find(ctx, true, &cd); |
aa43477b | 6517 | if (!preq || !io_poll_disarm(preq)) { |
79ebeaee | 6518 | spin_unlock(&ctx->completion_lock); |
aa43477b | 6519 | ret = preq ? -EALREADY : -ENOENT; |
2bbb146d | 6520 | goto out; |
b69de288 | 6521 | } |
79ebeaee | 6522 | spin_unlock(&ctx->completion_lock); |
cb3b200e | 6523 | |
2bbb146d PB |
6524 | if (req->poll_update.update_events || req->poll_update.update_user_data) { |
6525 | /* only mask one event flags, keep behavior flags */ | |
6526 | if (req->poll_update.update_events) { | |
6527 | preq->poll.events &= ~0xffff; | |
6528 | preq->poll.events |= req->poll_update.events & 0xffff; | |
6529 | preq->poll.events |= IO_POLL_UNMASK; | |
cb3b200e | 6530 | } |
2bbb146d | 6531 | if (req->poll_update.update_user_data) |
cef216fc | 6532 | preq->cqe.user_data = req->poll_update.new_user_data; |
b69de288 | 6533 | |
2bbb146d PB |
6534 | ret2 = io_poll_add(preq, issue_flags); |
6535 | /* successfully updated, don't complete poll request */ | |
6536 | if (!ret2) | |
6537 | goto out; | |
b69de288 | 6538 | } |
6224590d | 6539 | |
2bbb146d | 6540 | req_set_fail(preq); |
cef216fc | 6541 | preq->cqe.res = -ECANCELED; |
cc8e9ba7 PB |
6542 | locked = !(issue_flags & IO_URING_F_UNLOCKED); |
6543 | io_req_task_complete(preq, &locked); | |
2bbb146d PB |
6544 | out: |
6545 | if (ret < 0) | |
6224590d | 6546 | req_set_fail(req); |
2bbb146d | 6547 | /* complete update request, we're done with it */ |
cc8e9ba7 | 6548 | __io_req_complete(req, issue_flags, ret, 0); |
b69de288 | 6549 | return 0; |
89850fce JA |
6550 | } |
6551 | ||
5262f567 JA |
6552 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
6553 | { | |
ad8a48ac JA |
6554 | struct io_timeout_data *data = container_of(timer, |
6555 | struct io_timeout_data, timer); | |
6556 | struct io_kiocb *req = data->req; | |
6557 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
6558 | unsigned long flags; |
6559 | ||
89850fce | 6560 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
a71976f3 | 6561 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
6562 | atomic_set(&req->ctx->cq_timeouts, |
6563 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
89850fce | 6564 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
01cec8c1 | 6565 | |
a90c8bf6 PB |
6566 | if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS)) |
6567 | req_set_fail(req); | |
6568 | ||
cef216fc | 6569 | req->cqe.res = -ETIME; |
a90c8bf6 | 6570 | req->io_task_work.func = io_req_task_complete; |
4813c377 | 6571 | io_req_task_work_add(req, false); |
5262f567 JA |
6572 | return HRTIMER_NORESTART; |
6573 | } | |
6574 | ||
fbd15848 | 6575 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
b21432b4 | 6576 | struct io_cancel_data *cd) |
89850fce | 6577 | __must_hold(&ctx->timeout_lock) |
f254ac04 | 6578 | { |
fbd15848 | 6579 | struct io_timeout_data *io; |
47f46768 | 6580 | struct io_kiocb *req; |
fd9c7bc5 | 6581 | bool found = false; |
f254ac04 | 6582 | |
135fcde8 | 6583 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
970f256e JA |
6584 | if (!(cd->flags & IORING_ASYNC_CANCEL_ANY) && |
6585 | cd->data != req->cqe.user_data) | |
8e29da69 | 6586 | continue; |
970f256e | 6587 | if (cd->flags & (IORING_ASYNC_CANCEL_ALL|IORING_ASYNC_CANCEL_ANY)) { |
8e29da69 JA |
6588 | if (cd->seq == req->work.cancel_seq) |
6589 | continue; | |
6590 | req->work.cancel_seq = cd->seq; | |
6591 | } | |
6592 | found = true; | |
6593 | break; | |
47f46768 | 6594 | } |
fd9c7bc5 PB |
6595 | if (!found) |
6596 | return ERR_PTR(-ENOENT); | |
fbd15848 PB |
6597 | |
6598 | io = req->async_data; | |
fd9c7bc5 | 6599 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
fbd15848 | 6600 | return ERR_PTR(-EALREADY); |
a71976f3 | 6601 | list_del_init(&req->timeout.list); |
fbd15848 PB |
6602 | return req; |
6603 | } | |
47f46768 | 6604 | |
b21432b4 | 6605 | static int io_timeout_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd) |
ec3c3d0f | 6606 | __must_hold(&ctx->completion_lock) |
fbd15848 | 6607 | { |
3645c200 PB |
6608 | struct io_kiocb *req; |
6609 | ||
6610 | spin_lock_irq(&ctx->timeout_lock); | |
b21432b4 | 6611 | req = io_timeout_extract(ctx, cd); |
3645c200 | 6612 | spin_unlock_irq(&ctx->timeout_lock); |
fbd15848 PB |
6613 | |
6614 | if (IS_ERR(req)) | |
6615 | return PTR_ERR(req); | |
6695490d | 6616 | io_req_task_queue_fail(req, -ECANCELED); |
f254ac04 JA |
6617 | return 0; |
6618 | } | |
6619 | ||
50c1df2b JA |
6620 | static clockid_t io_timeout_get_clock(struct io_timeout_data *data) |
6621 | { | |
6622 | switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) { | |
6623 | case IORING_TIMEOUT_BOOTTIME: | |
6624 | return CLOCK_BOOTTIME; | |
6625 | case IORING_TIMEOUT_REALTIME: | |
6626 | return CLOCK_REALTIME; | |
6627 | default: | |
6628 | /* can't happen, vetted at prep time */ | |
6629 | WARN_ON_ONCE(1); | |
6630 | fallthrough; | |
6631 | case 0: | |
6632 | return CLOCK_MONOTONIC; | |
6633 | } | |
6634 | } | |
6635 | ||
f1042b6c PB |
6636 | static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
6637 | struct timespec64 *ts, enum hrtimer_mode mode) | |
6638 | __must_hold(&ctx->timeout_lock) | |
6639 | { | |
6640 | struct io_timeout_data *io; | |
6641 | struct io_kiocb *req; | |
6642 | bool found = false; | |
6643 | ||
6644 | list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) { | |
cef216fc | 6645 | found = user_data == req->cqe.user_data; |
f1042b6c PB |
6646 | if (found) |
6647 | break; | |
6648 | } | |
6649 | if (!found) | |
6650 | return -ENOENT; | |
6651 | ||
6652 | io = req->async_data; | |
6653 | if (hrtimer_try_to_cancel(&io->timer) == -1) | |
6654 | return -EALREADY; | |
6655 | hrtimer_init(&io->timer, io_timeout_get_clock(io), mode); | |
6656 | io->timer.function = io_link_timeout_fn; | |
6657 | hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode); | |
6658 | return 0; | |
6659 | } | |
6660 | ||
9c8e11b3 PB |
6661 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
6662 | struct timespec64 *ts, enum hrtimer_mode mode) | |
89850fce | 6663 | __must_hold(&ctx->timeout_lock) |
47f46768 | 6664 | { |
b21432b4 JA |
6665 | struct io_cancel_data cd = { .data = user_data, }; |
6666 | struct io_kiocb *req = io_timeout_extract(ctx, &cd); | |
9c8e11b3 | 6667 | struct io_timeout_data *data; |
47f46768 | 6668 | |
9c8e11b3 PB |
6669 | if (IS_ERR(req)) |
6670 | return PTR_ERR(req); | |
47f46768 | 6671 | |
9c8e11b3 PB |
6672 | req->timeout.off = 0; /* noseq */ |
6673 | data = req->async_data; | |
6674 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
50c1df2b | 6675 | hrtimer_init(&data->timer, io_timeout_get_clock(data), mode); |
9c8e11b3 PB |
6676 | data->timer.function = io_timeout_fn; |
6677 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
6678 | return 0; | |
47f46768 JA |
6679 | } |
6680 | ||
3529d8c2 JA |
6681 | static int io_timeout_remove_prep(struct io_kiocb *req, |
6682 | const struct io_uring_sqe *sqe) | |
b29472ee | 6683 | { |
9c8e11b3 PB |
6684 | struct io_timeout_rem *tr = &req->timeout_rem; |
6685 | ||
b29472ee JA |
6686 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
6687 | return -EINVAL; | |
61710e43 DA |
6688 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6689 | return -EINVAL; | |
26578cda | 6690 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
b29472ee JA |
6691 | return -EINVAL; |
6692 | ||
f1042b6c | 6693 | tr->ltimeout = false; |
9c8e11b3 PB |
6694 | tr->addr = READ_ONCE(sqe->addr); |
6695 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
f1042b6c PB |
6696 | if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) { |
6697 | if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
6698 | return -EINVAL; | |
6699 | if (tr->flags & IORING_LINK_TIMEOUT_UPDATE) | |
6700 | tr->ltimeout = true; | |
6701 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS)) | |
9c8e11b3 PB |
6702 | return -EINVAL; |
6703 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
6704 | return -EFAULT; | |
2087009c YB |
6705 | if (tr->ts.tv_sec < 0 || tr->ts.tv_nsec < 0) |
6706 | return -EINVAL; | |
9c8e11b3 PB |
6707 | } else if (tr->flags) { |
6708 | /* timeout removal doesn't support flags */ | |
b29472ee | 6709 | return -EINVAL; |
9c8e11b3 | 6710 | } |
b29472ee | 6711 | |
b29472ee JA |
6712 | return 0; |
6713 | } | |
6714 | ||
8662daec PB |
6715 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
6716 | { | |
6717 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
6718 | : HRTIMER_MODE_REL; | |
6719 | } | |
6720 | ||
11365043 JA |
6721 | /* |
6722 | * Remove or update an existing timeout command | |
6723 | */ | |
61e98203 | 6724 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 6725 | { |
9c8e11b3 | 6726 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 6727 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 6728 | int ret; |
11365043 | 6729 | |
ec3c3d0f | 6730 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
b21432b4 JA |
6731 | struct io_cancel_data cd = { .data = tr->addr, }; |
6732 | ||
ec3c3d0f | 6733 | spin_lock(&ctx->completion_lock); |
b21432b4 | 6734 | ret = io_timeout_cancel(ctx, &cd); |
ec3c3d0f PB |
6735 | spin_unlock(&ctx->completion_lock); |
6736 | } else { | |
f1042b6c PB |
6737 | enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags); |
6738 | ||
ec3c3d0f | 6739 | spin_lock_irq(&ctx->timeout_lock); |
f1042b6c PB |
6740 | if (tr->ltimeout) |
6741 | ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
6742 | else | |
6743 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
ec3c3d0f PB |
6744 | spin_unlock_irq(&ctx->timeout_lock); |
6745 | } | |
11365043 | 6746 | |
4e88d6e7 | 6747 | if (ret < 0) |
93d2bcd2 | 6748 | req_set_fail(req); |
505657bc | 6749 | io_req_complete_post(req, ret, 0); |
11365043 | 6750 | return 0; |
5262f567 JA |
6751 | } |
6752 | ||
3529d8c2 | 6753 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 6754 | bool is_timeout_link) |
5262f567 | 6755 | { |
ad8a48ac | 6756 | struct io_timeout_data *data; |
a41525ab | 6757 | unsigned flags; |
56080b02 | 6758 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 6759 | |
ad8a48ac | 6760 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 6761 | return -EINVAL; |
26578cda PB |
6762 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
6763 | sqe->splice_fd_in) | |
a41525ab | 6764 | return -EINVAL; |
56080b02 | 6765 | if (off && is_timeout_link) |
2d28390a | 6766 | return -EINVAL; |
a41525ab | 6767 | flags = READ_ONCE(sqe->timeout_flags); |
6224590d PB |
6768 | if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK | |
6769 | IORING_TIMEOUT_ETIME_SUCCESS)) | |
50c1df2b JA |
6770 | return -EINVAL; |
6771 | /* more than one clock specified is invalid, obviously */ | |
6772 | if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
5262f567 | 6773 | return -EINVAL; |
bdf20073 | 6774 | |
ef9dd637 | 6775 | INIT_LIST_HEAD(&req->timeout.list); |
bfe68a22 | 6776 | req->timeout.off = off; |
f18ee4cf PB |
6777 | if (unlikely(off && !req->ctx->off_timeout_used)) |
6778 | req->ctx->off_timeout_used = true; | |
26a61679 | 6779 | |
d6a644a7 PB |
6780 | if (WARN_ON_ONCE(req_has_async_data(req))) |
6781 | return -EFAULT; | |
6782 | if (io_alloc_async_data(req)) | |
26a61679 JA |
6783 | return -ENOMEM; |
6784 | ||
e8c2bc1f | 6785 | data = req->async_data; |
ad8a48ac | 6786 | data->req = req; |
50c1df2b | 6787 | data->flags = flags; |
ad8a48ac JA |
6788 | |
6789 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
6790 | return -EFAULT; |
6791 | ||
f6223ff7 YB |
6792 | if (data->ts.tv_sec < 0 || data->ts.tv_nsec < 0) |
6793 | return -EINVAL; | |
6794 | ||
e677edbc | 6795 | INIT_LIST_HEAD(&req->timeout.list); |
8662daec | 6796 | data->mode = io_translate_timeout_mode(flags); |
50c1df2b | 6797 | hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode); |
b97e736a PB |
6798 | |
6799 | if (is_timeout_link) { | |
6800 | struct io_submit_link *link = &req->ctx->submit_state.link; | |
6801 | ||
6802 | if (!link->head) | |
6803 | return -EINVAL; | |
6804 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) | |
6805 | return -EINVAL; | |
4d13d1a4 PB |
6806 | req->timeout.head = link->last; |
6807 | link->last->flags |= REQ_F_ARM_LTIMEOUT; | |
b97e736a | 6808 | } |
ad8a48ac JA |
6809 | return 0; |
6810 | } | |
6811 | ||
61e98203 | 6812 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 6813 | { |
ad8a48ac | 6814 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 6815 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 6816 | struct list_head *entry; |
bfe68a22 | 6817 | u32 tail, off = req->timeout.off; |
ad8a48ac | 6818 | |
89850fce | 6819 | spin_lock_irq(&ctx->timeout_lock); |
93bd25bb | 6820 | |
5262f567 JA |
6821 | /* |
6822 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
6823 | * timeout event to be satisfied. If it isn't set, then this is |
6824 | * a pure timeout request, sequence isn't used. | |
5262f567 | 6825 | */ |
8eb7e2d0 | 6826 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
6827 | entry = ctx->timeout_list.prev; |
6828 | goto add; | |
6829 | } | |
5262f567 | 6830 | |
bfe68a22 PB |
6831 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
6832 | req->timeout.target_seq = tail + off; | |
5262f567 | 6833 | |
f010505b MDG |
6834 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
6835 | * This is safe because ->completion_lock is held, and submissions | |
6836 | * and completions are never mixed in the same ->completion_lock section. | |
6837 | */ | |
6838 | ctx->cq_last_tm_flush = tail; | |
6839 | ||
5262f567 JA |
6840 | /* |
6841 | * Insertion sort, ensuring the first entry in the list is always | |
6842 | * the one we need first. | |
6843 | */ | |
5262f567 | 6844 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
6845 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
6846 | timeout.list); | |
5262f567 | 6847 | |
8eb7e2d0 | 6848 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 6849 | continue; |
bfe68a22 PB |
6850 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
6851 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
6852 | break; |
6853 | } | |
93bd25bb | 6854 | add: |
135fcde8 | 6855 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
6856 | data->timer.function = io_timeout_fn; |
6857 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
89850fce | 6858 | spin_unlock_irq(&ctx->timeout_lock); |
5262f567 JA |
6859 | return 0; |
6860 | } | |
5262f567 | 6861 | |
62755e35 JA |
6862 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
6863 | { | |
6864 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 6865 | struct io_cancel_data *cd = data; |
62755e35 | 6866 | |
8e29da69 JA |
6867 | if (req->ctx != cd->ctx) |
6868 | return false; | |
970f256e JA |
6869 | if (cd->flags & IORING_ASYNC_CANCEL_ANY) { |
6870 | ; | |
6871 | } else if (cd->flags & IORING_ASYNC_CANCEL_FD) { | |
4bf94615 JA |
6872 | if (req->file != cd->file) |
6873 | return false; | |
6874 | } else { | |
6875 | if (req->cqe.user_data != cd->data) | |
6876 | return false; | |
6877 | } | |
970f256e | 6878 | if (cd->flags & (IORING_ASYNC_CANCEL_ALL|IORING_ASYNC_CANCEL_ANY)) { |
8e29da69 JA |
6879 | if (cd->seq == req->work.cancel_seq) |
6880 | return false; | |
6881 | req->work.cancel_seq = cd->seq; | |
6882 | } | |
6883 | return true; | |
62755e35 JA |
6884 | } |
6885 | ||
b21432b4 JA |
6886 | static int io_async_cancel_one(struct io_uring_task *tctx, |
6887 | struct io_cancel_data *cd) | |
62755e35 | 6888 | { |
62755e35 | 6889 | enum io_wq_cancel cancel_ret; |
62755e35 | 6890 | int ret = 0; |
970f256e | 6891 | bool all; |
62755e35 | 6892 | |
f458dd84 | 6893 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
6894 | return -ENOENT; |
6895 | ||
970f256e JA |
6896 | all = cd->flags & (IORING_ASYNC_CANCEL_ALL|IORING_ASYNC_CANCEL_ANY); |
6897 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, cd, all); | |
62755e35 JA |
6898 | switch (cancel_ret) { |
6899 | case IO_WQ_CANCEL_OK: | |
6900 | ret = 0; | |
6901 | break; | |
6902 | case IO_WQ_CANCEL_RUNNING: | |
6903 | ret = -EALREADY; | |
6904 | break; | |
6905 | case IO_WQ_CANCEL_NOTFOUND: | |
6906 | ret = -ENOENT; | |
6907 | break; | |
6908 | } | |
6909 | ||
e977d6d3 JA |
6910 | return ret; |
6911 | } | |
6912 | ||
b21432b4 | 6913 | static int io_try_cancel(struct io_kiocb *req, struct io_cancel_data *cd) |
47f46768 | 6914 | { |
8cb01fac | 6915 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 JA |
6916 | int ret; |
6917 | ||
dadebc35 | 6918 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
8cb01fac | 6919 | |
b21432b4 | 6920 | ret = io_async_cancel_one(req->task->io_uring, cd); |
ccbf7261 JA |
6921 | /* |
6922 | * Fall-through even for -EALREADY, as we may have poll armed | |
6923 | * that need unarming. | |
6924 | */ | |
6925 | if (!ret) | |
6926 | return 0; | |
505657bc PB |
6927 | |
6928 | spin_lock(&ctx->completion_lock); | |
b21432b4 | 6929 | ret = io_poll_cancel(ctx, cd); |
ccbf7261 JA |
6930 | if (ret != -ENOENT) |
6931 | goto out; | |
4bf94615 JA |
6932 | if (!(cd->flags & IORING_ASYNC_CANCEL_FD)) |
6933 | ret = io_timeout_cancel(ctx, cd); | |
505657bc PB |
6934 | out: |
6935 | spin_unlock(&ctx->completion_lock); | |
6936 | return ret; | |
47f46768 JA |
6937 | } |
6938 | ||
970f256e JA |
6939 | #define CANCEL_FLAGS (IORING_ASYNC_CANCEL_ALL | IORING_ASYNC_CANCEL_FD | \ |
6940 | IORING_ASYNC_CANCEL_ANY) | |
6941 | ||
3529d8c2 JA |
6942 | static int io_async_cancel_prep(struct io_kiocb *req, |
6943 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 6944 | { |
fbf23849 | 6945 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 6946 | return -EINVAL; |
4bf94615 | 6947 | if (unlikely(req->flags & REQ_F_BUFFER_SELECT)) |
61710e43 | 6948 | return -EINVAL; |
8e29da69 | 6949 | if (sqe->ioprio || sqe->off || sqe->len || sqe->splice_fd_in) |
e977d6d3 JA |
6950 | return -EINVAL; |
6951 | ||
fbf23849 | 6952 | req->cancel.addr = READ_ONCE(sqe->addr); |
8e29da69 | 6953 | req->cancel.flags = READ_ONCE(sqe->cancel_flags); |
970f256e | 6954 | if (req->cancel.flags & ~CANCEL_FLAGS) |
8e29da69 | 6955 | return -EINVAL; |
970f256e JA |
6956 | if (req->cancel.flags & IORING_ASYNC_CANCEL_FD) { |
6957 | if (req->cancel.flags & IORING_ASYNC_CANCEL_ANY) | |
6958 | return -EINVAL; | |
4bf94615 | 6959 | req->cancel.fd = READ_ONCE(sqe->fd); |
970f256e | 6960 | } |
8e29da69 | 6961 | |
fbf23849 JA |
6962 | return 0; |
6963 | } | |
6964 | ||
8e29da69 JA |
6965 | static int __io_async_cancel(struct io_cancel_data *cd, struct io_kiocb *req, |
6966 | unsigned int issue_flags) | |
fbf23849 | 6967 | { |
970f256e | 6968 | bool all = cd->flags & (IORING_ASYNC_CANCEL_ALL|IORING_ASYNC_CANCEL_ANY); |
8e29da69 | 6969 | struct io_ring_ctx *ctx = cd->ctx; |
58f99373 | 6970 | struct io_tctx_node *node; |
8e29da69 | 6971 | int ret, nr = 0; |
58f99373 | 6972 | |
8e29da69 JA |
6973 | do { |
6974 | ret = io_try_cancel(req, cd); | |
6975 | if (ret == -ENOENT) | |
6976 | break; | |
970f256e | 6977 | if (!all) |
8e29da69 JA |
6978 | return ret; |
6979 | nr++; | |
6980 | } while (1); | |
58f99373 PB |
6981 | |
6982 | /* slow path, try all io-wq's */ | |
f8929630 | 6983 | io_ring_submit_lock(ctx, issue_flags); |
58f99373 PB |
6984 | ret = -ENOENT; |
6985 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
6986 | struct io_uring_task *tctx = node->task->io_uring; | |
fbf23849 | 6987 | |
8e29da69 JA |
6988 | ret = io_async_cancel_one(tctx, cd); |
6989 | if (ret != -ENOENT) { | |
970f256e | 6990 | if (!all) |
8e29da69 JA |
6991 | break; |
6992 | nr++; | |
6993 | } | |
58f99373 | 6994 | } |
f8929630 | 6995 | io_ring_submit_unlock(ctx, issue_flags); |
970f256e | 6996 | return all ? nr : ret; |
8e29da69 JA |
6997 | } |
6998 | ||
6999 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) | |
7000 | { | |
7001 | struct io_cancel_data cd = { | |
7002 | .ctx = req->ctx, | |
7003 | .data = req->cancel.addr, | |
7004 | .flags = req->cancel.flags, | |
7005 | .seq = atomic_inc_return(&req->ctx->cancel_seq), | |
7006 | }; | |
7007 | int ret; | |
7008 | ||
4bf94615 JA |
7009 | if (cd.flags & IORING_ASYNC_CANCEL_FD) { |
7010 | if (req->flags & REQ_F_FIXED_FILE) | |
7011 | req->file = io_file_get_fixed(req, req->cancel.fd, | |
7012 | issue_flags); | |
7013 | else | |
7014 | req->file = io_file_get_normal(req, req->cancel.fd); | |
7015 | if (!req->file) { | |
7016 | ret = -EBADF; | |
7017 | goto done; | |
7018 | } | |
7019 | cd.file = req->file; | |
7020 | } | |
7021 | ||
8e29da69 | 7022 | ret = __io_async_cancel(&cd, req, issue_flags); |
4bf94615 | 7023 | done: |
58f99373 | 7024 | if (ret < 0) |
93d2bcd2 | 7025 | req_set_fail(req); |
505657bc | 7026 | io_req_complete_post(req, ret, 0); |
5262f567 JA |
7027 | return 0; |
7028 | } | |
7029 | ||
269bbe5f | 7030 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
7031 | const struct io_uring_sqe *sqe) |
7032 | { | |
61710e43 DA |
7033 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
7034 | return -EINVAL; | |
26578cda | 7035 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
05f3fb3c JA |
7036 | return -EINVAL; |
7037 | ||
269bbe5f BM |
7038 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
7039 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
7040 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 7041 | return -EINVAL; |
269bbe5f | 7042 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
7043 | return 0; |
7044 | } | |
7045 | ||
889fca73 | 7046 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
7047 | { |
7048 | struct io_ring_ctx *ctx = req->ctx; | |
c3bdad02 | 7049 | struct io_uring_rsrc_update2 up; |
05f3fb3c | 7050 | int ret; |
fbf23849 | 7051 | |
269bbe5f BM |
7052 | up.offset = req->rsrc_update.offset; |
7053 | up.data = req->rsrc_update.arg; | |
c3bdad02 PB |
7054 | up.nr = 0; |
7055 | up.tags = 0; | |
615cee49 | 7056 | up.resv = 0; |
d8a3ba9c | 7057 | up.resv2 = 0; |
05f3fb3c | 7058 | |
f8929630 | 7059 | io_ring_submit_lock(ctx, issue_flags); |
fdecb662 | 7060 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
98f0b3b4 | 7061 | &up, req->rsrc_update.nr_args); |
f8929630 | 7062 | io_ring_submit_unlock(ctx, issue_flags); |
05f3fb3c JA |
7063 | |
7064 | if (ret < 0) | |
93d2bcd2 | 7065 | req_set_fail(req); |
889fca73 | 7066 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
7067 | return 0; |
7068 | } | |
7069 | ||
bfe76559 | 7070 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 7071 | { |
d625c6ee | 7072 | switch (req->opcode) { |
e781573e | 7073 | case IORING_OP_NOP: |
bfe76559 | 7074 | return 0; |
f67676d1 JA |
7075 | case IORING_OP_READV: |
7076 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 7077 | case IORING_OP_READ: |
f67676d1 JA |
7078 | case IORING_OP_WRITEV: |
7079 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 7080 | case IORING_OP_WRITE: |
584b0180 | 7081 | return io_prep_rw(req, sqe); |
0969e783 | 7082 | case IORING_OP_POLL_ADD: |
bfe76559 | 7083 | return io_poll_add_prep(req, sqe); |
0969e783 | 7084 | case IORING_OP_POLL_REMOVE: |
c5de0036 | 7085 | return io_poll_update_prep(req, sqe); |
8ed8d3c3 | 7086 | case IORING_OP_FSYNC: |
1155c76a | 7087 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 7088 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 7089 | return io_sfr_prep(req, sqe); |
03b1230c | 7090 | case IORING_OP_SENDMSG: |
fddaface | 7091 | case IORING_OP_SEND: |
bfe76559 | 7092 | return io_sendmsg_prep(req, sqe); |
03b1230c | 7093 | case IORING_OP_RECVMSG: |
fddaface | 7094 | case IORING_OP_RECV: |
bfe76559 | 7095 | return io_recvmsg_prep(req, sqe); |
f499a021 | 7096 | case IORING_OP_CONNECT: |
bfe76559 | 7097 | return io_connect_prep(req, sqe); |
2d28390a | 7098 | case IORING_OP_TIMEOUT: |
bfe76559 | 7099 | return io_timeout_prep(req, sqe, false); |
b29472ee | 7100 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 7101 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 7102 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 7103 | return io_async_cancel_prep(req, sqe); |
2d28390a | 7104 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 7105 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 7106 | case IORING_OP_ACCEPT: |
bfe76559 | 7107 | return io_accept_prep(req, sqe); |
d63d1b5e | 7108 | case IORING_OP_FALLOCATE: |
bfe76559 | 7109 | return io_fallocate_prep(req, sqe); |
15b71abe | 7110 | case IORING_OP_OPENAT: |
bfe76559 | 7111 | return io_openat_prep(req, sqe); |
b5dba59e | 7112 | case IORING_OP_CLOSE: |
bfe76559 | 7113 | return io_close_prep(req, sqe); |
05f3fb3c | 7114 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 7115 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 7116 | case IORING_OP_STATX: |
bfe76559 | 7117 | return io_statx_prep(req, sqe); |
4840e418 | 7118 | case IORING_OP_FADVISE: |
bfe76559 | 7119 | return io_fadvise_prep(req, sqe); |
c1ca757b | 7120 | case IORING_OP_MADVISE: |
bfe76559 | 7121 | return io_madvise_prep(req, sqe); |
cebdb986 | 7122 | case IORING_OP_OPENAT2: |
bfe76559 | 7123 | return io_openat2_prep(req, sqe); |
3e4827b0 | 7124 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 7125 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 7126 | case IORING_OP_SPLICE: |
bfe76559 | 7127 | return io_splice_prep(req, sqe); |
ddf0322d | 7128 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 7129 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 7130 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 7131 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 7132 | case IORING_OP_TEE: |
bfe76559 | 7133 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
7134 | case IORING_OP_SHUTDOWN: |
7135 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
7136 | case IORING_OP_RENAMEAT: |
7137 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
7138 | case IORING_OP_UNLINKAT: |
7139 | return io_unlinkat_prep(req, sqe); | |
e34a02dc DK |
7140 | case IORING_OP_MKDIRAT: |
7141 | return io_mkdirat_prep(req, sqe); | |
7a8721f8 DK |
7142 | case IORING_OP_SYMLINKAT: |
7143 | return io_symlinkat_prep(req, sqe); | |
cf30da90 DK |
7144 | case IORING_OP_LINKAT: |
7145 | return io_linkat_prep(req, sqe); | |
4f57f06c JA |
7146 | case IORING_OP_MSG_RING: |
7147 | return io_msg_ring_prep(req, sqe); | |
f67676d1 JA |
7148 | } |
7149 | ||
bfe76559 PB |
7150 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
7151 | req->opcode); | |
bd54b6fe | 7152 | return -EINVAL; |
bfe76559 PB |
7153 | } |
7154 | ||
93642ef8 | 7155 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 7156 | { |
b7e298d2 PB |
7157 | if (!io_op_defs[req->opcode].needs_async_setup) |
7158 | return 0; | |
d886e185 | 7159 | if (WARN_ON_ONCE(req_has_async_data(req))) |
b7e298d2 PB |
7160 | return -EFAULT; |
7161 | if (io_alloc_async_data(req)) | |
7162 | return -EAGAIN; | |
7163 | ||
93642ef8 PB |
7164 | switch (req->opcode) { |
7165 | case IORING_OP_READV: | |
93642ef8 PB |
7166 | return io_rw_prep_async(req, READ); |
7167 | case IORING_OP_WRITEV: | |
93642ef8 PB |
7168 | return io_rw_prep_async(req, WRITE); |
7169 | case IORING_OP_SENDMSG: | |
93642ef8 PB |
7170 | return io_sendmsg_prep_async(req); |
7171 | case IORING_OP_RECVMSG: | |
93642ef8 PB |
7172 | return io_recvmsg_prep_async(req); |
7173 | case IORING_OP_CONNECT: | |
7174 | return io_connect_prep_async(req); | |
7175 | } | |
b7e298d2 PB |
7176 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
7177 | req->opcode); | |
7178 | return -EFAULT; | |
f67676d1 JA |
7179 | } |
7180 | ||
9cf7c104 PB |
7181 | static u32 io_get_sequence(struct io_kiocb *req) |
7182 | { | |
a3dbdf54 | 7183 | u32 seq = req->ctx->cached_sq_head; |
963c6abb | 7184 | struct io_kiocb *cur; |
9cf7c104 | 7185 | |
a3dbdf54 | 7186 | /* need original cached_sq_head, but it was increased for each req */ |
963c6abb | 7187 | io_for_each_link(cur, req) |
a3dbdf54 PB |
7188 | seq--; |
7189 | return seq; | |
9cf7c104 PB |
7190 | } |
7191 | ||
c072481d | 7192 | static __cold void io_drain_req(struct io_kiocb *req) |
de0617e4 | 7193 | { |
a197f664 | 7194 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 7195 | struct io_defer_entry *de; |
f67676d1 | 7196 | int ret; |
e0eb71dc | 7197 | u32 seq = io_get_sequence(req); |
3c19966d | 7198 | |
9d858b21 | 7199 | /* Still need defer if there is pending req in defer list. */ |
e302f104 | 7200 | spin_lock(&ctx->completion_lock); |
5e371265 | 7201 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) { |
e302f104 | 7202 | spin_unlock(&ctx->completion_lock); |
e0eb71dc | 7203 | queue: |
10c66904 | 7204 | ctx->drain_active = false; |
e0eb71dc PB |
7205 | io_req_task_queue(req); |
7206 | return; | |
10c66904 | 7207 | } |
e302f104 | 7208 | spin_unlock(&ctx->completion_lock); |
9cf7c104 | 7209 | |
b7e298d2 | 7210 | ret = io_req_prep_async(req); |
e0eb71dc PB |
7211 | if (ret) { |
7212 | fail: | |
7213 | io_req_complete_failed(req, ret); | |
7214 | return; | |
7215 | } | |
cbdcb435 | 7216 | io_prep_async_link(req); |
27dc8338 | 7217 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
76cc33d7 | 7218 | if (!de) { |
1b48773f | 7219 | ret = -ENOMEM; |
e0eb71dc | 7220 | goto fail; |
76cc33d7 | 7221 | } |
2d28390a | 7222 | |
79ebeaee | 7223 | spin_lock(&ctx->completion_lock); |
9cf7c104 | 7224 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
79ebeaee | 7225 | spin_unlock(&ctx->completion_lock); |
27dc8338 | 7226 | kfree(de); |
e0eb71dc | 7227 | goto queue; |
de0617e4 JA |
7228 | } |
7229 | ||
cef216fc | 7230 | trace_io_uring_defer(ctx, req, req->cqe.user_data, req->opcode); |
27dc8338 | 7231 | de->req = req; |
9cf7c104 | 7232 | de->seq = seq; |
27dc8338 | 7233 | list_add_tail(&de->list, &ctx->defer_list); |
79ebeaee | 7234 | spin_unlock(&ctx->completion_lock); |
de0617e4 JA |
7235 | } |
7236 | ||
68fb8979 | 7237 | static void io_clean_op(struct io_kiocb *req) |
99bc4c38 | 7238 | { |
8197b053 PB |
7239 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
7240 | spin_lock(&req->ctx->completion_lock); | |
cc3cec83 | 7241 | io_put_kbuf_comp(req); |
8197b053 PB |
7242 | spin_unlock(&req->ctx->completion_lock); |
7243 | } | |
99bc4c38 | 7244 | |
0e1b6fe3 PB |
7245 | if (req->flags & REQ_F_NEED_CLEANUP) { |
7246 | switch (req->opcode) { | |
7247 | case IORING_OP_READV: | |
7248 | case IORING_OP_READ_FIXED: | |
7249 | case IORING_OP_READ: | |
7250 | case IORING_OP_WRITEV: | |
7251 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
7252 | case IORING_OP_WRITE: { |
7253 | struct io_async_rw *io = req->async_data; | |
1dacb4df PB |
7254 | |
7255 | kfree(io->free_iovec); | |
0e1b6fe3 | 7256 | break; |
e8c2bc1f | 7257 | } |
0e1b6fe3 | 7258 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
7259 | case IORING_OP_SENDMSG: { |
7260 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
7261 | |
7262 | kfree(io->free_iov); | |
0e1b6fe3 | 7263 | break; |
e8c2bc1f | 7264 | } |
f3cd4850 JA |
7265 | case IORING_OP_OPENAT: |
7266 | case IORING_OP_OPENAT2: | |
7267 | if (req->open.filename) | |
7268 | putname(req->open.filename); | |
7269 | break; | |
80a261fd JA |
7270 | case IORING_OP_RENAMEAT: |
7271 | putname(req->rename.oldpath); | |
7272 | putname(req->rename.newpath); | |
7273 | break; | |
14a1143b JA |
7274 | case IORING_OP_UNLINKAT: |
7275 | putname(req->unlink.filename); | |
7276 | break; | |
e34a02dc DK |
7277 | case IORING_OP_MKDIRAT: |
7278 | putname(req->mkdir.filename); | |
7279 | break; | |
7a8721f8 DK |
7280 | case IORING_OP_SYMLINKAT: |
7281 | putname(req->symlink.oldpath); | |
7282 | putname(req->symlink.newpath); | |
7283 | break; | |
cf30da90 DK |
7284 | case IORING_OP_LINKAT: |
7285 | putname(req->hardlink.oldpath); | |
7286 | putname(req->hardlink.newpath); | |
7287 | break; | |
1b6fe6e0 SR |
7288 | case IORING_OP_STATX: |
7289 | if (req->statx.filename) | |
7290 | putname(req->statx.filename); | |
7291 | break; | |
0e1b6fe3 | 7292 | } |
99bc4c38 | 7293 | } |
75652a30 JA |
7294 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
7295 | kfree(req->apoll->double_poll); | |
7296 | kfree(req->apoll); | |
7297 | req->apoll = NULL; | |
7298 | } | |
c854357b | 7299 | if (req->flags & REQ_F_CREDS) |
b8e64b53 | 7300 | put_cred(req->creds); |
d886e185 PB |
7301 | if (req->flags & REQ_F_ASYNC_DATA) { |
7302 | kfree(req->async_data); | |
7303 | req->async_data = NULL; | |
7304 | } | |
c854357b | 7305 | req->flags &= ~IO_REQ_CLEAN_FLAGS; |
99bc4c38 PB |
7306 | } |
7307 | ||
6bf9c47a JA |
7308 | static bool io_assign_file(struct io_kiocb *req, unsigned int issue_flags) |
7309 | { | |
7310 | if (req->file || !io_op_defs[req->opcode].needs_file) | |
7311 | return true; | |
7312 | ||
7313 | if (req->flags & REQ_F_FIXED_FILE) | |
cef216fc | 7314 | req->file = io_file_get_fixed(req, req->cqe.fd, issue_flags); |
6bf9c47a | 7315 | else |
cef216fc | 7316 | req->file = io_file_get_normal(req, req->cqe.fd); |
6bf9c47a | 7317 | |
772f5e00 | 7318 | return !!req->file; |
6bf9c47a JA |
7319 | } |
7320 | ||
889fca73 | 7321 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 7322 | { |
5730b27e | 7323 | const struct cred *creds = NULL; |
d625c6ee | 7324 | int ret; |
2b188cc1 | 7325 | |
70152140 JA |
7326 | if (unlikely(!io_assign_file(req, issue_flags))) |
7327 | return -EBADF; | |
7328 | ||
6878b40e | 7329 | if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred())) |
c10d1f98 | 7330 | creds = override_creds(req->creds); |
5730b27e | 7331 | |
5bd2182d PM |
7332 | if (!io_op_defs[req->opcode].audit_skip) |
7333 | audit_uring_entry(req->opcode); | |
7334 | ||
d625c6ee | 7335 | switch (req->opcode) { |
2b188cc1 | 7336 | case IORING_OP_NOP: |
889fca73 | 7337 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
7338 | break; |
7339 | case IORING_OP_READV: | |
edafccee | 7340 | case IORING_OP_READ_FIXED: |
3a6820f2 | 7341 | case IORING_OP_READ: |
889fca73 | 7342 | ret = io_read(req, issue_flags); |
edafccee | 7343 | break; |
3529d8c2 | 7344 | case IORING_OP_WRITEV: |
edafccee | 7345 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 7346 | case IORING_OP_WRITE: |
889fca73 | 7347 | ret = io_write(req, issue_flags); |
2b188cc1 | 7348 | break; |
c992fe29 | 7349 | case IORING_OP_FSYNC: |
45d189c6 | 7350 | ret = io_fsync(req, issue_flags); |
c992fe29 | 7351 | break; |
221c5eb2 | 7352 | case IORING_OP_POLL_ADD: |
61e98203 | 7353 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
7354 | break; |
7355 | case IORING_OP_POLL_REMOVE: | |
c5de0036 | 7356 | ret = io_poll_update(req, issue_flags); |
221c5eb2 | 7357 | break; |
5d17b4a4 | 7358 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 7359 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 7360 | break; |
0fa03c62 | 7361 | case IORING_OP_SENDMSG: |
889fca73 | 7362 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 7363 | break; |
fddaface | 7364 | case IORING_OP_SEND: |
889fca73 | 7365 | ret = io_send(req, issue_flags); |
0fa03c62 | 7366 | break; |
aa1fa28f | 7367 | case IORING_OP_RECVMSG: |
889fca73 | 7368 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 7369 | break; |
fddaface | 7370 | case IORING_OP_RECV: |
889fca73 | 7371 | ret = io_recv(req, issue_flags); |
aa1fa28f | 7372 | break; |
5262f567 | 7373 | case IORING_OP_TIMEOUT: |
61e98203 | 7374 | ret = io_timeout(req, issue_flags); |
5262f567 | 7375 | break; |
11365043 | 7376 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 7377 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 7378 | break; |
17f2fe35 | 7379 | case IORING_OP_ACCEPT: |
889fca73 | 7380 | ret = io_accept(req, issue_flags); |
17f2fe35 | 7381 | break; |
f8e85cf2 | 7382 | case IORING_OP_CONNECT: |
889fca73 | 7383 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 7384 | break; |
62755e35 | 7385 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 7386 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 7387 | break; |
d63d1b5e | 7388 | case IORING_OP_FALLOCATE: |
45d189c6 | 7389 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 7390 | break; |
15b71abe | 7391 | case IORING_OP_OPENAT: |
45d189c6 | 7392 | ret = io_openat(req, issue_flags); |
15b71abe | 7393 | break; |
b5dba59e | 7394 | case IORING_OP_CLOSE: |
889fca73 | 7395 | ret = io_close(req, issue_flags); |
b5dba59e | 7396 | break; |
05f3fb3c | 7397 | case IORING_OP_FILES_UPDATE: |
889fca73 | 7398 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 7399 | break; |
eddc7ef5 | 7400 | case IORING_OP_STATX: |
45d189c6 | 7401 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 7402 | break; |
4840e418 | 7403 | case IORING_OP_FADVISE: |
45d189c6 | 7404 | ret = io_fadvise(req, issue_flags); |
4840e418 | 7405 | break; |
c1ca757b | 7406 | case IORING_OP_MADVISE: |
45d189c6 | 7407 | ret = io_madvise(req, issue_flags); |
c1ca757b | 7408 | break; |
cebdb986 | 7409 | case IORING_OP_OPENAT2: |
45d189c6 | 7410 | ret = io_openat2(req, issue_flags); |
cebdb986 | 7411 | break; |
3e4827b0 | 7412 | case IORING_OP_EPOLL_CTL: |
889fca73 | 7413 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 7414 | break; |
7d67af2c | 7415 | case IORING_OP_SPLICE: |
45d189c6 | 7416 | ret = io_splice(req, issue_flags); |
7d67af2c | 7417 | break; |
ddf0322d | 7418 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 7419 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 7420 | break; |
067524e9 | 7421 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 7422 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 7423 | break; |
f2a8d5c7 | 7424 | case IORING_OP_TEE: |
45d189c6 | 7425 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 7426 | break; |
36f4fa68 | 7427 | case IORING_OP_SHUTDOWN: |
45d189c6 | 7428 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 7429 | break; |
80a261fd | 7430 | case IORING_OP_RENAMEAT: |
45d189c6 | 7431 | ret = io_renameat(req, issue_flags); |
80a261fd | 7432 | break; |
14a1143b | 7433 | case IORING_OP_UNLINKAT: |
45d189c6 | 7434 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 7435 | break; |
e34a02dc DK |
7436 | case IORING_OP_MKDIRAT: |
7437 | ret = io_mkdirat(req, issue_flags); | |
7438 | break; | |
7a8721f8 DK |
7439 | case IORING_OP_SYMLINKAT: |
7440 | ret = io_symlinkat(req, issue_flags); | |
7441 | break; | |
cf30da90 DK |
7442 | case IORING_OP_LINKAT: |
7443 | ret = io_linkat(req, issue_flags); | |
7444 | break; | |
4f57f06c JA |
7445 | case IORING_OP_MSG_RING: |
7446 | ret = io_msg_ring(req, issue_flags); | |
7447 | break; | |
2b188cc1 JA |
7448 | default: |
7449 | ret = -EINVAL; | |
7450 | break; | |
7451 | } | |
7452 | ||
5bd2182d PM |
7453 | if (!io_op_defs[req->opcode].audit_skip) |
7454 | audit_uring_exit(!ret, ret); | |
7455 | ||
5730b27e JA |
7456 | if (creds) |
7457 | revert_creds(creds); | |
def596e9 JA |
7458 | if (ret) |
7459 | return ret; | |
b532576e | 7460 | /* If the op doesn't have a file, we're not polling for it */ |
9983028e | 7461 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
9882131c | 7462 | io_iopoll_req_issued(req, issue_flags); |
def596e9 JA |
7463 | |
7464 | return 0; | |
2b188cc1 JA |
7465 | } |
7466 | ||
ebc11b6c PB |
7467 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
7468 | { | |
7469 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
7470 | ||
7471 | req = io_put_req_find_next(req); | |
7472 | return req ? &req->work : NULL; | |
7473 | } | |
7474 | ||
5280f7e5 | 7475 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
7476 | { |
7477 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6bf9c47a | 7478 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
d01905db PB |
7479 | unsigned int issue_flags = IO_URING_F_UNLOCKED; |
7480 | bool needs_poll = false; | |
6bf9c47a | 7481 | int ret = 0, err = -ECANCELED; |
2b188cc1 | 7482 | |
48dcd38d PB |
7483 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
7484 | if (!(req->flags & REQ_F_REFCOUNT)) | |
7485 | __io_req_set_refcount(req, 2); | |
7486 | else | |
7487 | req_ref_get(req); | |
5d5901a3 | 7488 | |
cb2d344c | 7489 | io_arm_ltimeout(req); |
6bf9c47a | 7490 | |
dadebc35 | 7491 | /* either cancelled or io-wq is dying, so don't touch tctx->iowq */ |
d01905db | 7492 | if (work->flags & IO_WQ_WORK_CANCEL) { |
0f8da75b | 7493 | fail: |
6bf9c47a | 7494 | io_req_task_queue_fail(req, err); |
d01905db PB |
7495 | return; |
7496 | } | |
0f8da75b PB |
7497 | if (!io_assign_file(req, issue_flags)) { |
7498 | err = -EBADF; | |
7499 | work->flags |= IO_WQ_WORK_CANCEL; | |
7500 | goto fail; | |
7501 | } | |
31b51510 | 7502 | |
d01905db | 7503 | if (req->flags & REQ_F_FORCE_ASYNC) { |
afb7f56f PB |
7504 | bool opcode_poll = def->pollin || def->pollout; |
7505 | ||
7506 | if (opcode_poll && file_can_poll(req->file)) { | |
7507 | needs_poll = true; | |
d01905db | 7508 | issue_flags |= IO_URING_F_NONBLOCK; |
afb7f56f | 7509 | } |
561fb04a | 7510 | } |
31b51510 | 7511 | |
d01905db PB |
7512 | do { |
7513 | ret = io_issue_sqe(req, issue_flags); | |
7514 | if (ret != -EAGAIN) | |
7515 | break; | |
7516 | /* | |
7517 | * We can get EAGAIN for iopolled IO even though we're | |
7518 | * forcing a sync submission from here, since we can't | |
7519 | * wait for request slots on the block side. | |
7520 | */ | |
7521 | if (!needs_poll) { | |
7522 | cond_resched(); | |
7523 | continue; | |
90fa0288 HX |
7524 | } |
7525 | ||
4d9237e3 | 7526 | if (io_arm_poll_handler(req, issue_flags) == IO_APOLL_OK) |
d01905db PB |
7527 | return; |
7528 | /* aborted or ready, in either case retry blocking */ | |
7529 | needs_poll = false; | |
7530 | issue_flags &= ~IO_URING_F_NONBLOCK; | |
7531 | } while (1); | |
31b51510 | 7532 | |
a3df7698 | 7533 | /* avoid locking problems by failing it from a clean context */ |
5d5901a3 | 7534 | if (ret) |
a3df7698 | 7535 | io_req_task_queue_fail(req, ret); |
2b188cc1 JA |
7536 | } |
7537 | ||
aeca241b | 7538 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
042b0d85 | 7539 | unsigned i) |
65e19f54 | 7540 | { |
042b0d85 | 7541 | return &table->files[i]; |
dafecf19 PB |
7542 | } |
7543 | ||
65e19f54 JA |
7544 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
7545 | int index) | |
7546 | { | |
aeca241b | 7547 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
65e19f54 | 7548 | |
a04b0ac0 | 7549 | return (struct file *) (slot->file_ptr & FFS_MASK); |
65e19f54 JA |
7550 | } |
7551 | ||
a04b0ac0 | 7552 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
9a321c98 PB |
7553 | { |
7554 | unsigned long file_ptr = (unsigned long) file; | |
7555 | ||
88459b50 | 7556 | file_ptr |= io_file_get_flags(file); |
a04b0ac0 | 7557 | file_slot->file_ptr = file_ptr; |
65e19f54 JA |
7558 | } |
7559 | ||
5106dd6e JA |
7560 | static inline struct file *io_file_get_fixed(struct io_kiocb *req, int fd, |
7561 | unsigned int issue_flags) | |
09bb8394 | 7562 | { |
5106dd6e JA |
7563 | struct io_ring_ctx *ctx = req->ctx; |
7564 | struct file *file = NULL; | |
ac177053 | 7565 | unsigned long file_ptr; |
09bb8394 | 7566 | |
93f052cb | 7567 | io_ring_submit_lock(ctx, issue_flags); |
5106dd6e | 7568 | |
ac177053 | 7569 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
5106dd6e | 7570 | goto out; |
ac177053 PB |
7571 | fd = array_index_nospec(fd, ctx->nr_user_files); |
7572 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; | |
7573 | file = (struct file *) (file_ptr & FFS_MASK); | |
7574 | file_ptr &= ~FFS_MASK; | |
7575 | /* mask in overlapping REQ_F and FFS bits */ | |
35645ac3 | 7576 | req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT); |
5106dd6e JA |
7577 | io_req_set_rsrc_node(req, ctx, 0); |
7578 | out: | |
93f052cb | 7579 | io_ring_submit_unlock(ctx, issue_flags); |
ac177053 PB |
7580 | return file; |
7581 | } | |
d44f554e | 7582 | |
d5361233 JA |
7583 | /* |
7584 | * Drop the file for requeue operations. Only used of req->file is the | |
7585 | * io_uring descriptor itself. | |
7586 | */ | |
7587 | static void io_drop_inflight_file(struct io_kiocb *req) | |
7588 | { | |
7589 | if (unlikely(req->flags & REQ_F_INFLIGHT)) { | |
7590 | fput(req->file); | |
7591 | req->file = NULL; | |
7592 | req->flags &= ~REQ_F_INFLIGHT; | |
7593 | } | |
7594 | } | |
7595 | ||
5106dd6e | 7596 | static struct file *io_file_get_normal(struct io_kiocb *req, int fd) |
ac177053 | 7597 | { |
62906e89 | 7598 | struct file *file = fget(fd); |
ac177053 | 7599 | |
cef216fc | 7600 | trace_io_uring_file_get(req->ctx, req, req->cqe.user_data, fd); |
09bb8394 | 7601 | |
ac177053 | 7602 | /* we don't allow fixed io_uring files */ |
d5361233 JA |
7603 | if (file && file->f_op == &io_uring_fops) |
7604 | req->flags |= REQ_F_INFLIGHT; | |
8371adf5 | 7605 | return file; |
09bb8394 JA |
7606 | } |
7607 | ||
f237c30a | 7608 | static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked) |
89b263f6 JA |
7609 | { |
7610 | struct io_kiocb *prev = req->timeout.prev; | |
617a8948 | 7611 | int ret = -ENOENT; |
89b263f6 JA |
7612 | |
7613 | if (prev) { | |
b21432b4 JA |
7614 | if (!(req->task->flags & PF_EXITING)) { |
7615 | struct io_cancel_data cd = { | |
7616 | .ctx = req->ctx, | |
7617 | .data = prev->cqe.user_data, | |
7618 | }; | |
7619 | ||
7620 | ret = io_try_cancel(req, &cd); | |
7621 | } | |
505657bc | 7622 | io_req_complete_post(req, ret ?: -ETIME, 0); |
89b263f6 | 7623 | io_put_req(prev); |
89b263f6 JA |
7624 | } else { |
7625 | io_req_complete_post(req, -ETIME, 0); | |
7626 | } | |
7627 | } | |
7628 | ||
2665abfd | 7629 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 7630 | { |
ad8a48ac JA |
7631 | struct io_timeout_data *data = container_of(timer, |
7632 | struct io_timeout_data, timer); | |
90cd7e42 | 7633 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 7634 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 7635 | unsigned long flags; |
2665abfd | 7636 | |
89b263f6 | 7637 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
90cd7e42 PB |
7638 | prev = req->timeout.head; |
7639 | req->timeout.head = NULL; | |
2665abfd JA |
7640 | |
7641 | /* | |
7642 | * We don't expect the list to be empty, that will only happen if we | |
7643 | * race with the completion of the linked work. | |
7644 | */ | |
447c19f3 | 7645 | if (prev) { |
f2f87370 | 7646 | io_remove_next_linked(prev); |
447c19f3 PB |
7647 | if (!req_ref_inc_not_zero(prev)) |
7648 | prev = NULL; | |
7649 | } | |
ef9dd637 | 7650 | list_del(&req->timeout.list); |
89b263f6 JA |
7651 | req->timeout.prev = prev; |
7652 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); | |
2665abfd | 7653 | |
89b263f6 | 7654 | req->io_task_work.func = io_req_task_link_timeout; |
4813c377 | 7655 | io_req_task_work_add(req, false); |
2665abfd JA |
7656 | return HRTIMER_NORESTART; |
7657 | } | |
7658 | ||
de968c18 | 7659 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 7660 | { |
de968c18 PB |
7661 | struct io_ring_ctx *ctx = req->ctx; |
7662 | ||
89b263f6 | 7663 | spin_lock_irq(&ctx->timeout_lock); |
76a46e06 | 7664 | /* |
f2f87370 PB |
7665 | * If the back reference is NULL, then our linked request finished |
7666 | * before we got a chance to setup the timer | |
76a46e06 | 7667 | */ |
90cd7e42 | 7668 | if (req->timeout.head) { |
e8c2bc1f | 7669 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 7670 | |
ad8a48ac JA |
7671 | data->timer.function = io_link_timeout_fn; |
7672 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
7673 | data->mode); | |
ef9dd637 | 7674 | list_add_tail(&req->timeout.list, &ctx->ltimeout_list); |
2665abfd | 7675 | } |
89b263f6 | 7676 | spin_unlock_irq(&ctx->timeout_lock); |
2665abfd | 7677 | /* drop submission reference */ |
76a46e06 JA |
7678 | io_put_req(req); |
7679 | } | |
2665abfd | 7680 | |
7bfa9bad | 7681 | static void io_queue_async(struct io_kiocb *req, int ret) |
d475a9a6 PB |
7682 | __must_hold(&req->ctx->uring_lock) |
7683 | { | |
7bfa9bad PB |
7684 | struct io_kiocb *linked_timeout; |
7685 | ||
7686 | if (ret != -EAGAIN || (req->flags & REQ_F_NOWAIT)) { | |
7687 | io_req_complete_failed(req, ret); | |
7688 | return; | |
7689 | } | |
7690 | ||
7691 | linked_timeout = io_prep_linked_timeout(req); | |
d475a9a6 | 7692 | |
4d9237e3 | 7693 | switch (io_arm_poll_handler(req, 0)) { |
d475a9a6 | 7694 | case IO_APOLL_READY: |
d475a9a6 PB |
7695 | io_req_task_queue(req); |
7696 | break; | |
7697 | case IO_APOLL_ABORTED: | |
7698 | /* | |
7699 | * Queued up for async execution, worker will release | |
7700 | * submit reference when the iocb is actually submitted. | |
7701 | */ | |
77955efb | 7702 | io_queue_iowq(req, NULL); |
d475a9a6 | 7703 | break; |
b1c62645 | 7704 | case IO_APOLL_OK: |
b1c62645 | 7705 | break; |
d475a9a6 PB |
7706 | } |
7707 | ||
7708 | if (linked_timeout) | |
7709 | io_queue_linked_timeout(linked_timeout); | |
7710 | } | |
7711 | ||
cbc2e203 | 7712 | static inline void io_queue_sqe(struct io_kiocb *req) |
282cdc86 | 7713 | __must_hold(&req->ctx->uring_lock) |
2b188cc1 | 7714 | { |
e0c5c576 | 7715 | int ret; |
2b188cc1 | 7716 | |
c5eef2b9 | 7717 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 7718 | |
fff4e40e PB |
7719 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
7720 | io_req_add_compl_list(req); | |
d9f9d284 | 7721 | return; |
fff4e40e | 7722 | } |
491381ce JA |
7723 | /* |
7724 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
7725 | * doesn't support non-blocking read/write attempts | |
7726 | */ | |
7bfa9bad | 7727 | if (likely(!ret)) |
cb2d344c | 7728 | io_arm_ltimeout(req); |
7bfa9bad PB |
7729 | else |
7730 | io_queue_async(req, ret); | |
2b188cc1 JA |
7731 | } |
7732 | ||
4652fe3f | 7733 | static void io_queue_sqe_fallback(struct io_kiocb *req) |
282cdc86 | 7734 | __must_hold(&req->ctx->uring_lock) |
4fe2c963 | 7735 | { |
17b147f6 PB |
7736 | if (unlikely(req->flags & REQ_F_FAIL)) { |
7737 | /* | |
7738 | * We don't submit, fail them all, for that replace hardlinks | |
7739 | * with normal links. Extra REQ_F_LINK is tolerated. | |
7740 | */ | |
7741 | req->flags &= ~REQ_F_HARDLINK; | |
7742 | req->flags |= REQ_F_LINK; | |
7743 | io_req_complete_failed(req, req->cqe.res); | |
e0eb71dc PB |
7744 | } else if (unlikely(req->ctx->drain_active)) { |
7745 | io_drain_req(req); | |
76cc33d7 PB |
7746 | } else { |
7747 | int ret = io_req_prep_async(req); | |
7748 | ||
7749 | if (unlikely(ret)) | |
7750 | io_req_complete_failed(req, ret); | |
7751 | else | |
77955efb | 7752 | io_queue_iowq(req, NULL); |
ce35a47a | 7753 | } |
4fe2c963 JL |
7754 | } |
7755 | ||
b16fed66 PB |
7756 | /* |
7757 | * Check SQE restrictions (opcode and flags). | |
7758 | * | |
7759 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
7760 | */ | |
7761 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
7762 | struct io_kiocb *req, | |
7763 | unsigned int sqe_flags) | |
4fe2c963 | 7764 | { |
b16fed66 PB |
7765 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
7766 | return false; | |
7767 | ||
7768 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
7769 | ctx->restrictions.sqe_flags_required) | |
7770 | return false; | |
7771 | ||
7772 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
7773 | ctx->restrictions.sqe_flags_required)) | |
7774 | return false; | |
7775 | ||
7776 | return true; | |
4fe2c963 JL |
7777 | } |
7778 | ||
22b2ca31 PB |
7779 | static void io_init_req_drain(struct io_kiocb *req) |
7780 | { | |
7781 | struct io_ring_ctx *ctx = req->ctx; | |
7782 | struct io_kiocb *head = ctx->submit_state.link.head; | |
7783 | ||
7784 | ctx->drain_active = true; | |
7785 | if (head) { | |
7786 | /* | |
7787 | * If we need to drain a request in the middle of a link, drain | |
7788 | * the head request and the next request/link after the current | |
7789 | * link. Considering sequential execution of links, | |
b6c7db32 | 7790 | * REQ_F_IO_DRAIN will be maintained for every request of our |
22b2ca31 PB |
7791 | * link. |
7792 | */ | |
b6c7db32 | 7793 | head->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; |
22b2ca31 PB |
7794 | ctx->drain_next = true; |
7795 | } | |
7796 | } | |
7797 | ||
b16fed66 PB |
7798 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
7799 | const struct io_uring_sqe *sqe) | |
282cdc86 | 7800 | __must_hold(&ctx->uring_lock) |
b16fed66 | 7801 | { |
b16fed66 | 7802 | unsigned int sqe_flags; |
fc0ae024 | 7803 | int personality; |
4a04d1d1 | 7804 | u8 opcode; |
b16fed66 | 7805 | |
864ea921 | 7806 | /* req is partially pre-initialised, see io_preinit_req() */ |
4a04d1d1 | 7807 | req->opcode = opcode = READ_ONCE(sqe->opcode); |
b16fed66 PB |
7808 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
7809 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
cef216fc | 7810 | req->cqe.user_data = READ_ONCE(sqe->user_data); |
b16fed66 | 7811 | req->file = NULL; |
c1bdf8ed | 7812 | req->rsrc_node = NULL; |
b16fed66 | 7813 | req->task = current; |
b16fed66 | 7814 | |
4a04d1d1 PB |
7815 | if (unlikely(opcode >= IORING_OP_LAST)) { |
7816 | req->opcode = 0; | |
b16fed66 | 7817 | return -EINVAL; |
4a04d1d1 | 7818 | } |
68fe256a PB |
7819 | if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) { |
7820 | /* enforce forwards compatibility on users */ | |
7821 | if (sqe_flags & ~SQE_VALID_FLAGS) | |
7822 | return -EINVAL; | |
7823 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
4a04d1d1 | 7824 | !io_op_defs[opcode].buffer_select) |
68fe256a | 7825 | return -EOPNOTSUPP; |
5562a8d7 PB |
7826 | if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS) |
7827 | ctx->drain_disabled = true; | |
7828 | if (sqe_flags & IOSQE_IO_DRAIN) { | |
7829 | if (ctx->drain_disabled) | |
7830 | return -EOPNOTSUPP; | |
22b2ca31 | 7831 | io_init_req_drain(req); |
5562a8d7 | 7832 | } |
2a56a9bd PB |
7833 | } |
7834 | if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) { | |
7835 | if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags)) | |
7836 | return -EACCES; | |
7837 | /* knock it to the slow queue path, will be drained there */ | |
7838 | if (ctx->drain_active) | |
7839 | req->flags |= REQ_F_FORCE_ASYNC; | |
7840 | /* if there is no link, we're at "next" request and need to drain */ | |
7841 | if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) { | |
7842 | ctx->drain_next = false; | |
7843 | ctx->drain_active = true; | |
b6c7db32 | 7844 | req->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; |
2a56a9bd | 7845 | } |
68fe256a | 7846 | } |
b16fed66 | 7847 | |
4a04d1d1 | 7848 | if (io_op_defs[opcode].needs_file) { |
6d63416d PB |
7849 | struct io_submit_state *state = &ctx->submit_state; |
7850 | ||
cef216fc | 7851 | req->cqe.fd = READ_ONCE(sqe->fd); |
6bf9c47a | 7852 | |
6d63416d PB |
7853 | /* |
7854 | * Plug now if we have more than 2 IO left after this, and the | |
7855 | * target is potentially a read/write to block based storage. | |
7856 | */ | |
4a04d1d1 | 7857 | if (state->need_plug && io_op_defs[opcode].plug) { |
6d63416d PB |
7858 | state->plug_started = true; |
7859 | state->need_plug = false; | |
5ca7a8b3 | 7860 | blk_start_plug_nr_ios(&state->plug, state->submit_nr); |
6d63416d | 7861 | } |
b16fed66 | 7862 | } |
863e0560 | 7863 | |
003e8dcc JA |
7864 | personality = READ_ONCE(sqe->personality); |
7865 | if (personality) { | |
cdab10bf LT |
7866 | int ret; |
7867 | ||
c10d1f98 PB |
7868 | req->creds = xa_load(&ctx->personalities, personality); |
7869 | if (!req->creds) | |
003e8dcc | 7870 | return -EINVAL; |
c10d1f98 | 7871 | get_cred(req->creds); |
cdc1404a PM |
7872 | ret = security_uring_override_creds(req->creds); |
7873 | if (ret) { | |
7874 | put_cred(req->creds); | |
7875 | return ret; | |
7876 | } | |
b8e64b53 | 7877 | req->flags |= REQ_F_CREDS; |
003e8dcc | 7878 | } |
b16fed66 | 7879 | |
fc0ae024 | 7880 | return io_req_prep(req, sqe); |
b16fed66 PB |
7881 | } |
7882 | ||
df3becde PB |
7883 | static __cold int io_submit_fail_init(const struct io_uring_sqe *sqe, |
7884 | struct io_kiocb *req, int ret) | |
7885 | { | |
7886 | struct io_ring_ctx *ctx = req->ctx; | |
7887 | struct io_submit_link *link = &ctx->submit_state.link; | |
7888 | struct io_kiocb *head = link->head; | |
7889 | ||
7890 | trace_io_uring_req_failed(sqe, ctx, req, ret); | |
7891 | ||
7892 | /* | |
7893 | * Avoid breaking links in the middle as it renders links with SQPOLL | |
7894 | * unusable. Instead of failing eagerly, continue assembling the link if | |
7895 | * applicable and mark the head with REQ_F_FAIL. The link flushing code | |
7896 | * should find the flag and handle the rest. | |
7897 | */ | |
7898 | req_fail_link_node(req, ret); | |
7899 | if (head && !(head->flags & REQ_F_FAIL)) | |
7900 | req_fail_link_node(head, -ECANCELED); | |
7901 | ||
7902 | if (!(req->flags & IO_REQ_LINK_FLAGS)) { | |
7903 | if (head) { | |
7904 | link->last->link = req; | |
7905 | link->head = NULL; | |
7906 | req = head; | |
7907 | } | |
7908 | io_queue_sqe_fallback(req); | |
7909 | return ret; | |
7910 | } | |
7911 | ||
7912 | if (head) | |
7913 | link->last->link = req; | |
7914 | else | |
7915 | link->head = req; | |
7916 | link->last = req; | |
7917 | return 0; | |
7918 | } | |
7919 | ||
7920 | static inline int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, | |
a1ab7b35 | 7921 | const struct io_uring_sqe *sqe) |
282cdc86 | 7922 | __must_hold(&ctx->uring_lock) |
9e645e11 | 7923 | { |
a1ab7b35 | 7924 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 7925 | int ret; |
9e645e11 | 7926 | |
a6b8cadc | 7927 | ret = io_init_req(ctx, req, sqe); |
df3becde PB |
7928 | if (unlikely(ret)) |
7929 | return io_submit_fail_init(sqe, req, ret); | |
441b8a78 | 7930 | |
be7053b7 | 7931 | /* don't need @sqe from now on */ |
cef216fc | 7932 | trace_io_uring_submit_sqe(ctx, req, req->cqe.user_data, req->opcode, |
236daeae OL |
7933 | req->flags, true, |
7934 | ctx->flags & IORING_SETUP_SQPOLL); | |
a6b8cadc | 7935 | |
9e645e11 JA |
7936 | /* |
7937 | * If we already have a head request, queue this one for async | |
7938 | * submittal once the head completes. If we don't have a head but | |
7939 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
7940 | * submitted sync once the chain is complete. If none of those | |
7941 | * conditions are true (normal request), then just queue it. | |
7942 | */ | |
924a07e4 | 7943 | if (unlikely(link->head)) { |
df3becde PB |
7944 | ret = io_req_prep_async(req); |
7945 | if (unlikely(ret)) | |
7946 | return io_submit_fail_init(sqe, req, ret); | |
7947 | ||
7948 | trace_io_uring_link(ctx, req, link->head); | |
f2f87370 | 7949 | link->last->link = req; |
863e0560 | 7950 | link->last = req; |
32fe525b | 7951 | |
da1a08c5 | 7952 | if (req->flags & IO_REQ_LINK_FLAGS) |
f15a3431 | 7953 | return 0; |
df3becde PB |
7954 | /* last request of the link, flush it */ |
7955 | req = link->head; | |
f15a3431 | 7956 | link->head = NULL; |
924a07e4 PB |
7957 | if (req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)) |
7958 | goto fallback; | |
7959 | ||
7960 | } else if (unlikely(req->flags & (IO_REQ_LINK_FLAGS | | |
7961 | REQ_F_FORCE_ASYNC | REQ_F_FAIL))) { | |
7962 | if (req->flags & IO_REQ_LINK_FLAGS) { | |
7963 | link->head = req; | |
7964 | link->last = req; | |
7965 | } else { | |
7966 | fallback: | |
7967 | io_queue_sqe_fallback(req); | |
7968 | } | |
f15a3431 | 7969 | return 0; |
9e645e11 | 7970 | } |
2e6e1fde | 7971 | |
924a07e4 | 7972 | io_queue_sqe(req); |
1d4240cc | 7973 | return 0; |
9e645e11 JA |
7974 | } |
7975 | ||
9a56a232 JA |
7976 | /* |
7977 | * Batched submission is done, ensure local IO is flushed out. | |
7978 | */ | |
553deffd | 7979 | static void io_submit_state_end(struct io_ring_ctx *ctx) |
9a56a232 | 7980 | { |
553deffd PB |
7981 | struct io_submit_state *state = &ctx->submit_state; |
7982 | ||
e126391c PB |
7983 | if (unlikely(state->link.head)) |
7984 | io_queue_sqe_fallback(state->link.head); | |
553deffd | 7985 | /* flush only after queuing links as they can generate completions */ |
c450178d | 7986 | io_submit_flush_completions(ctx); |
27926b68 JA |
7987 | if (state->plug_started) |
7988 | blk_finish_plug(&state->plug); | |
9a56a232 JA |
7989 | } |
7990 | ||
7991 | /* | |
7992 | * Start submission side cache. | |
7993 | */ | |
7994 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 7995 | unsigned int max_ios) |
9a56a232 | 7996 | { |
27926b68 | 7997 | state->plug_started = false; |
4b628aeb | 7998 | state->need_plug = max_ios > 2; |
5ca7a8b3 | 7999 | state->submit_nr = max_ios; |
a1ab7b35 PB |
8000 | /* set only head, no need to init link_last in advance */ |
8001 | state->link.head = NULL; | |
9a56a232 JA |
8002 | } |
8003 | ||
2b188cc1 JA |
8004 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
8005 | { | |
75b28aff | 8006 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 8007 | |
caf582c6 PB |
8008 | /* |
8009 | * Ensure any loads from the SQEs are done at this point, | |
8010 | * since once we write the new head, the application could | |
8011 | * write new data to them. | |
8012 | */ | |
8013 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
8014 | } |
8015 | ||
2b188cc1 | 8016 | /* |
dd9ae8a0 | 8017 | * Fetch an sqe, if one is available. Note this returns a pointer to memory |
2b188cc1 JA |
8018 | * that is mapped by userspace. This means that care needs to be taken to |
8019 | * ensure that reads are stable, as we cannot rely on userspace always | |
8020 | * being a good citizen. If members of the sqe are validated and then later | |
8021 | * used, it's important that those reads are done through READ_ONCE() to | |
8022 | * prevent a re-load down the line. | |
8023 | */ | |
709b302f | 8024 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 8025 | { |
ea5ab3b5 | 8026 | unsigned head, mask = ctx->sq_entries - 1; |
17d3aeb3 | 8027 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
2b188cc1 JA |
8028 | |
8029 | /* | |
8030 | * The cached sq head (or cq tail) serves two purposes: | |
8031 | * | |
8032 | * 1) allows us to batch the cost of updating the user visible | |
8033 | * head updates. | |
8034 | * 2) allows the kernel side to track the head on its own, even | |
8035 | * though the application is the one updating it. | |
8036 | */ | |
17d3aeb3 | 8037 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
709b302f PB |
8038 | if (likely(head < ctx->sq_entries)) |
8039 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
8040 | |
8041 | /* drop invalid entries */ | |
15641e42 PB |
8042 | ctx->cq_extra--; |
8043 | WRITE_ONCE(ctx->rings->sq_dropped, | |
8044 | READ_ONCE(ctx->rings->sq_dropped) + 1); | |
709b302f PB |
8045 | return NULL; |
8046 | } | |
8047 | ||
0f212204 | 8048 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
282cdc86 | 8049 | __must_hold(&ctx->uring_lock) |
6c271ce2 | 8050 | { |
69629809 | 8051 | unsigned int entries = io_sqring_entries(ctx); |
8e6971a8 PB |
8052 | unsigned int left; |
8053 | int ret; | |
6c271ce2 | 8054 | |
51d48dab | 8055 | if (unlikely(!entries)) |
69629809 | 8056 | return 0; |
ee7d46d9 | 8057 | /* make sure SQ entry isn't read before tail */ |
8e6971a8 PB |
8058 | ret = left = min3(nr, ctx->sq_entries, entries); |
8059 | io_get_task_refs(left); | |
8060 | io_submit_state_start(&ctx->submit_state, left); | |
6c271ce2 | 8061 | |
69629809 | 8062 | do { |
3529d8c2 | 8063 | const struct io_uring_sqe *sqe; |
196be95c | 8064 | struct io_kiocb *req; |
fb5ccc98 | 8065 | |
8e6971a8 | 8066 | if (unlikely(!io_alloc_req_refill(ctx))) |
fb5ccc98 | 8067 | break; |
a33ae9ce | 8068 | req = io_alloc_req(ctx); |
4fccfcbb PB |
8069 | sqe = io_get_sqe(ctx); |
8070 | if (unlikely(!sqe)) { | |
fa05457a | 8071 | io_req_add_to_cache(req, ctx); |
4fccfcbb PB |
8072 | break; |
8073 | } | |
1cd15904 PB |
8074 | |
8075 | /* | |
8076 | * Continue submitting even for sqe failure if the | |
8077 | * ring was setup with IORING_SETUP_SUBMIT_ALL | |
8078 | */ | |
8079 | if (unlikely(io_submit_sqe(ctx, req, sqe)) && | |
8080 | !(ctx->flags & IORING_SETUP_SUBMIT_ALL)) { | |
8081 | left--; | |
8082 | break; | |
bcbb7bf6 | 8083 | } |
1cd15904 | 8084 | } while (--left); |
9466f437 | 8085 | |
8e6971a8 PB |
8086 | if (unlikely(left)) { |
8087 | ret -= left; | |
8088 | /* try again if it submitted nothing and can't allocate a req */ | |
8089 | if (!ret && io_req_cache_empty(ctx)) | |
8090 | ret = -EAGAIN; | |
8091 | current->io_uring->cached_refs += left; | |
9466f437 | 8092 | } |
6c271ce2 | 8093 | |
553deffd | 8094 | io_submit_state_end(ctx); |
ae9428ca PB |
8095 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
8096 | io_commit_sqring(ctx); | |
8e6971a8 | 8097 | return ret; |
6c271ce2 JA |
8098 | } |
8099 | ||
e4b6d902 PB |
8100 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
8101 | { | |
8102 | return READ_ONCE(sqd->state); | |
8103 | } | |
8104 | ||
08369246 | 8105 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 8106 | { |
c8d1ba58 | 8107 | unsigned int to_submit; |
bdcd3eab | 8108 | int ret = 0; |
6c271ce2 | 8109 | |
c8d1ba58 | 8110 | to_submit = io_sqring_entries(ctx); |
e95eee2d | 8111 | /* if we're handling multiple rings, cap submit size for fairness */ |
4ce8ad95 OL |
8112 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
8113 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; | |
e95eee2d | 8114 | |
5eef4e87 | 8115 | if (!wq_list_empty(&ctx->iopoll_list) || to_submit) { |
948e1947 PB |
8116 | const struct cred *creds = NULL; |
8117 | ||
8118 | if (ctx->sq_creds != current_cred()) | |
8119 | creds = override_creds(ctx->sq_creds); | |
a4c0b3de | 8120 | |
c8d1ba58 | 8121 | mutex_lock(&ctx->uring_lock); |
5eef4e87 | 8122 | if (!wq_list_empty(&ctx->iopoll_list)) |
5ba3c874 | 8123 | io_do_iopoll(ctx, true); |
906a3c6f | 8124 | |
3b763ba1 PB |
8125 | /* |
8126 | * Don't submit if refs are dying, good for io_uring_register(), | |
8127 | * but also it is relied upon by io_ring_exit_work() | |
8128 | */ | |
0298ef96 PB |
8129 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
8130 | !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
08369246 | 8131 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 | 8132 | mutex_unlock(&ctx->uring_lock); |
cb318216 | 8133 | |
acfb381d PB |
8134 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
8135 | wake_up(&ctx->sqo_sq_wait); | |
948e1947 PB |
8136 | if (creds) |
8137 | revert_creds(creds); | |
acfb381d | 8138 | } |
6c271ce2 | 8139 | |
08369246 XW |
8140 | return ret; |
8141 | } | |
6c271ce2 | 8142 | |
c072481d | 8143 | static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
08369246 XW |
8144 | { |
8145 | struct io_ring_ctx *ctx; | |
8146 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 8147 | |
c9dca27d PB |
8148 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
8149 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); | |
08369246 | 8150 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 8151 | } |
6c271ce2 | 8152 | |
e4b6d902 PB |
8153 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
8154 | { | |
8155 | bool did_sig = false; | |
8156 | struct ksignal ksig; | |
8157 | ||
8158 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || | |
8159 | signal_pending(current)) { | |
8160 | mutex_unlock(&sqd->lock); | |
8161 | if (signal_pending(current)) | |
8162 | did_sig = get_signal(&ksig); | |
8163 | cond_resched(); | |
8164 | mutex_lock(&sqd->lock); | |
8165 | } | |
e4b6d902 PB |
8166 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
8167 | } | |
8168 | ||
c8d1ba58 JA |
8169 | static int io_sq_thread(void *data) |
8170 | { | |
69fb2131 JA |
8171 | struct io_sq_data *sqd = data; |
8172 | struct io_ring_ctx *ctx; | |
a0d9205f | 8173 | unsigned long timeout = 0; |
37d1e2e3 | 8174 | char buf[TASK_COMM_LEN]; |
08369246 | 8175 | DEFINE_WAIT(wait); |
6c271ce2 | 8176 | |
696ee88a | 8177 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
37d1e2e3 | 8178 | set_task_comm(current, buf); |
37d1e2e3 JA |
8179 | |
8180 | if (sqd->sq_cpu != -1) | |
8181 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
8182 | else | |
8183 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
8184 | current->flags |= PF_NO_SETAFFINITY; | |
8185 | ||
5bd2182d PM |
8186 | audit_alloc_kernel(current); |
8187 | ||
09a6f4ef | 8188 | mutex_lock(&sqd->lock); |
e4b6d902 | 8189 | while (1) { |
1a924a80 | 8190 | bool cap_entries, sqt_spin = false; |
c1edbf5f | 8191 | |
e4b6d902 PB |
8192 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
8193 | if (io_sqd_handle_event(sqd)) | |
c7d95613 | 8194 | break; |
08369246 XW |
8195 | timeout = jiffies + sqd->sq_thread_idle; |
8196 | } | |
e4b6d902 | 8197 | |
e95eee2d | 8198 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 8199 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
948e1947 | 8200 | int ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a | 8201 | |
5eef4e87 | 8202 | if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list))) |
08369246 | 8203 | sqt_spin = true; |
69fb2131 | 8204 | } |
dd432ea5 PB |
8205 | if (io_run_task_work()) |
8206 | sqt_spin = true; | |
6c271ce2 | 8207 | |
08369246 | 8208 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 8209 | cond_resched(); |
08369246 XW |
8210 | if (sqt_spin) |
8211 | timeout = jiffies + sqd->sq_thread_idle; | |
8212 | continue; | |
8213 | } | |
8214 | ||
08369246 | 8215 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
7f62d40d | 8216 | if (!io_sqd_events_pending(sqd) && !task_work_pending(current)) { |
1a924a80 PB |
8217 | bool needs_sched = true; |
8218 | ||
724cb4f9 | 8219 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
3a4b89a2 JA |
8220 | atomic_or(IORING_SQ_NEED_WAKEUP, |
8221 | &ctx->rings->sq_flags); | |
724cb4f9 | 8222 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
5eef4e87 | 8223 | !wq_list_empty(&ctx->iopoll_list)) { |
724cb4f9 HX |
8224 | needs_sched = false; |
8225 | break; | |
8226 | } | |
649bb75d AK |
8227 | |
8228 | /* | |
8229 | * Ensure the store of the wakeup flag is not | |
8230 | * reordered with the load of the SQ tail | |
8231 | */ | |
f2e030dd | 8232 | smp_mb__after_atomic(); |
649bb75d | 8233 | |
724cb4f9 HX |
8234 | if (io_sqring_entries(ctx)) { |
8235 | needs_sched = false; | |
8236 | break; | |
8237 | } | |
8238 | } | |
8239 | ||
8240 | if (needs_sched) { | |
8241 | mutex_unlock(&sqd->lock); | |
8242 | schedule(); | |
8243 | mutex_lock(&sqd->lock); | |
8244 | } | |
69fb2131 | 8245 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
3a4b89a2 JA |
8246 | atomic_andnot(IORING_SQ_NEED_WAKEUP, |
8247 | &ctx->rings->sq_flags); | |
6c271ce2 | 8248 | } |
08369246 XW |
8249 | |
8250 | finish_wait(&sqd->wait, &wait); | |
8251 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 | 8252 | } |
28cea78a | 8253 | |
78cc687b | 8254 | io_uring_cancel_generic(true, sqd); |
37d1e2e3 | 8255 | sqd->thread = NULL; |
05962f95 | 8256 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
3a4b89a2 | 8257 | atomic_or(IORING_SQ_NEED_WAKEUP, &ctx->rings->sq_flags); |
521d6a73 | 8258 | io_run_task_work(); |
734551df PB |
8259 | mutex_unlock(&sqd->lock); |
8260 | ||
5bd2182d PM |
8261 | audit_free(current); |
8262 | ||
37d1e2e3 JA |
8263 | complete(&sqd->exited); |
8264 | do_exit(0); | |
6c271ce2 JA |
8265 | } |
8266 | ||
bda52162 JA |
8267 | struct io_wait_queue { |
8268 | struct wait_queue_entry wq; | |
8269 | struct io_ring_ctx *ctx; | |
5fd46178 | 8270 | unsigned cq_tail; |
bda52162 JA |
8271 | unsigned nr_timeouts; |
8272 | }; | |
8273 | ||
6c503150 | 8274 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
8275 | { |
8276 | struct io_ring_ctx *ctx = iowq->ctx; | |
5fd46178 | 8277 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
bda52162 JA |
8278 | |
8279 | /* | |
d195a66e | 8280 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
8281 | * started waiting. For timeouts, we always want to return to userspace, |
8282 | * regardless of event count. | |
8283 | */ | |
5fd46178 | 8284 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
bda52162 JA |
8285 | } |
8286 | ||
8287 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
8288 | int wake_flags, void *key) | |
8289 | { | |
8290 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
8291 | wq); | |
8292 | ||
6c503150 PB |
8293 | /* |
8294 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
8295 | * the task, and the next invocation will do it. | |
8296 | */ | |
10988a0a DY |
8297 | if (io_should_wake(iowq) || |
8298 | test_bit(IO_CHECK_CQ_OVERFLOW_BIT, &iowq->ctx->check_cq)) | |
6c503150 PB |
8299 | return autoremove_wake_function(curr, mode, wake_flags, key); |
8300 | return -1; | |
bda52162 JA |
8301 | } |
8302 | ||
af9c1a44 JA |
8303 | static int io_run_task_work_sig(void) |
8304 | { | |
8305 | if (io_run_task_work()) | |
8306 | return 1; | |
0b8cfa97 | 8307 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
792ee0f6 | 8308 | return -ERESTARTSYS; |
c5020bc8 OL |
8309 | if (task_sigpending(current)) |
8310 | return -EINTR; | |
8311 | return 0; | |
af9c1a44 JA |
8312 | } |
8313 | ||
eeb60b9a PB |
8314 | /* when returns >0, the caller should retry */ |
8315 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
8316 | struct io_wait_queue *iowq, | |
22833966 | 8317 | ktime_t timeout) |
eeb60b9a PB |
8318 | { |
8319 | int ret; | |
155bc950 | 8320 | unsigned long check_cq; |
eeb60b9a PB |
8321 | |
8322 | /* make sure we run task_work before checking for signals */ | |
8323 | ret = io_run_task_work_sig(); | |
8324 | if (ret || io_should_wake(iowq)) | |
8325 | return ret; | |
155bc950 | 8326 | check_cq = READ_ONCE(ctx->check_cq); |
eeb60b9a | 8327 | /* let the caller flush overflows, retry */ |
155bc950 | 8328 | if (check_cq & BIT(IO_CHECK_CQ_OVERFLOW_BIT)) |
eeb60b9a | 8329 | return 1; |
155bc950 DY |
8330 | if (unlikely(check_cq & BIT(IO_CHECK_CQ_DROPPED_BIT))) |
8331 | return -EBADR; | |
22833966 JA |
8332 | if (!schedule_hrtimeout(&timeout, HRTIMER_MODE_ABS)) |
8333 | return -ETIME; | |
8334 | return 1; | |
eeb60b9a PB |
8335 | } |
8336 | ||
2b188cc1 JA |
8337 | /* |
8338 | * Wait until events become available, if we don't already have some. The | |
8339 | * application must reap them itself, as they reside on the shared cq ring. | |
8340 | */ | |
8341 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
8342 | const sigset_t __user *sig, size_t sigsz, |
8343 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 8344 | { |
90291099 | 8345 | struct io_wait_queue iowq; |
75b28aff | 8346 | struct io_rings *rings = ctx->rings; |
22833966 | 8347 | ktime_t timeout = KTIME_MAX; |
c1d5a224 | 8348 | int ret; |
2b188cc1 | 8349 | |
b41e9852 | 8350 | do { |
90f67366 | 8351 | io_cqring_overflow_flush(ctx); |
6c503150 | 8352 | if (io_cqring_events(ctx) >= min_events) |
b41e9852 | 8353 | return 0; |
4c6e277c | 8354 | if (!io_run_task_work()) |
b41e9852 | 8355 | break; |
b41e9852 | 8356 | } while (1); |
2b188cc1 JA |
8357 | |
8358 | if (sig) { | |
9e75ad5d AB |
8359 | #ifdef CONFIG_COMPAT |
8360 | if (in_compat_syscall()) | |
8361 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 8362 | sigsz); |
9e75ad5d AB |
8363 | else |
8364 | #endif | |
b772434b | 8365 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 8366 | |
2b188cc1 JA |
8367 | if (ret) |
8368 | return ret; | |
8369 | } | |
8370 | ||
950e79dd OL |
8371 | if (uts) { |
8372 | struct timespec64 ts; | |
8373 | ||
8374 | if (get_timespec64(&ts, uts)) | |
8375 | return -EFAULT; | |
8376 | timeout = ktime_add_ns(timespec64_to_ktime(ts), ktime_get_ns()); | |
8377 | } | |
8378 | ||
90291099 PB |
8379 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
8380 | iowq.wq.private = current; | |
8381 | INIT_LIST_HEAD(&iowq.wq.entry); | |
8382 | iowq.ctx = ctx; | |
bda52162 | 8383 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
5fd46178 | 8384 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
90291099 | 8385 | |
c826bd7a | 8386 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 8387 | do { |
ca0a2651 | 8388 | /* if we can't even flush overflow, don't wait for more */ |
90f67366 | 8389 | if (!io_cqring_overflow_flush(ctx)) { |
ca0a2651 JA |
8390 | ret = -EBUSY; |
8391 | break; | |
8392 | } | |
311997b3 | 8393 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
bda52162 | 8394 | TASK_INTERRUPTIBLE); |
22833966 | 8395 | ret = io_cqring_wait_schedule(ctx, &iowq, timeout); |
ca0a2651 | 8396 | cond_resched(); |
eeb60b9a | 8397 | } while (ret > 0); |
bda52162 | 8398 | |
b4f20bb4 | 8399 | finish_wait(&ctx->cq_wait, &iowq.wq); |
b7db41c9 | 8400 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 8401 | |
75b28aff | 8402 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
8403 | } |
8404 | ||
9123c8ff | 8405 | static void io_free_page_table(void **table, size_t size) |
05f3fb3c | 8406 | { |
9123c8ff | 8407 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
05f3fb3c | 8408 | |
846a4ef2 | 8409 | for (i = 0; i < nr_tables; i++) |
9123c8ff PB |
8410 | kfree(table[i]); |
8411 | kfree(table); | |
8412 | } | |
8413 | ||
c072481d | 8414 | static __cold void **io_alloc_page_table(size_t size) |
9123c8ff PB |
8415 | { |
8416 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); | |
8417 | size_t init_size = size; | |
8418 | void **table; | |
8419 | ||
0bea96f5 | 8420 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
8421 | if (!table) |
8422 | return NULL; | |
8423 | ||
8424 | for (i = 0; i < nr_tables; i++) { | |
27f6b318 | 8425 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
9123c8ff | 8426 | |
0bea96f5 | 8427 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
8428 | if (!table[i]) { |
8429 | io_free_page_table(table, init_size); | |
8430 | return NULL; | |
8431 | } | |
8432 | size -= this_size; | |
8433 | } | |
8434 | return table; | |
05f3fb3c JA |
8435 | } |
8436 | ||
28a9fe25 | 8437 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
1642b445 | 8438 | { |
28a9fe25 PB |
8439 | percpu_ref_exit(&ref_node->refs); |
8440 | kfree(ref_node); | |
1642b445 PB |
8441 | } |
8442 | ||
c072481d | 8443 | static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
b9bd2bea PB |
8444 | { |
8445 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); | |
8446 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; | |
8447 | unsigned long flags; | |
8448 | bool first_add = false; | |
b36a2050 | 8449 | unsigned long delay = HZ; |
b9bd2bea PB |
8450 | |
8451 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); | |
8452 | node->done = true; | |
8453 | ||
b36a2050 DY |
8454 | /* if we are mid-quiesce then do not delay */ |
8455 | if (node->rsrc_data->quiesce) | |
8456 | delay = 0; | |
8457 | ||
b9bd2bea PB |
8458 | while (!list_empty(&ctx->rsrc_ref_list)) { |
8459 | node = list_first_entry(&ctx->rsrc_ref_list, | |
8460 | struct io_rsrc_node, node); | |
8461 | /* recycle ref nodes in order */ | |
8462 | if (!node->done) | |
8463 | break; | |
8464 | list_del(&node->node); | |
8465 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); | |
8466 | } | |
8467 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); | |
8468 | ||
8469 | if (first_add) | |
b36a2050 | 8470 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
b9bd2bea PB |
8471 | } |
8472 | ||
f6133fbd | 8473 | static struct io_rsrc_node *io_rsrc_node_alloc(void) |
b9bd2bea PB |
8474 | { |
8475 | struct io_rsrc_node *ref_node; | |
8476 | ||
8477 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); | |
8478 | if (!ref_node) | |
8479 | return NULL; | |
8480 | ||
8481 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, | |
8482 | 0, GFP_KERNEL)) { | |
8483 | kfree(ref_node); | |
8484 | return NULL; | |
8485 | } | |
8486 | INIT_LIST_HEAD(&ref_node->node); | |
8487 | INIT_LIST_HEAD(&ref_node->rsrc_list); | |
8488 | ref_node->done = false; | |
8489 | return ref_node; | |
8490 | } | |
8491 | ||
a7f0ed5a PB |
8492 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
8493 | struct io_rsrc_data *data_to_kill) | |
ab409402 | 8494 | __must_hold(&ctx->uring_lock) |
6b06314c | 8495 | { |
a7f0ed5a PB |
8496 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
8497 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); | |
6b06314c | 8498 | |
ab409402 PB |
8499 | io_rsrc_refs_drop(ctx); |
8500 | ||
a7f0ed5a PB |
8501 | if (data_to_kill) { |
8502 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; | |
82fbcfa9 | 8503 | |
a7f0ed5a | 8504 | rsrc_node->rsrc_data = data_to_kill; |
4956b9ea | 8505 | spin_lock_irq(&ctx->rsrc_ref_lock); |
a7f0ed5a | 8506 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
4956b9ea | 8507 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
82fbcfa9 | 8508 | |
3e942498 | 8509 | atomic_inc(&data_to_kill->refs); |
a7f0ed5a PB |
8510 | percpu_ref_kill(&rsrc_node->refs); |
8511 | ctx->rsrc_node = NULL; | |
8512 | } | |
6b06314c | 8513 | |
a7f0ed5a PB |
8514 | if (!ctx->rsrc_node) { |
8515 | ctx->rsrc_node = ctx->rsrc_backup_node; | |
8516 | ctx->rsrc_backup_node = NULL; | |
8517 | } | |
8bad28d8 HX |
8518 | } |
8519 | ||
a7f0ed5a | 8520 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
8dd03afe PB |
8521 | { |
8522 | if (ctx->rsrc_backup_node) | |
8523 | return 0; | |
f6133fbd | 8524 | ctx->rsrc_backup_node = io_rsrc_node_alloc(); |
8dd03afe | 8525 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
8bad28d8 HX |
8526 | } |
8527 | ||
c072481d PB |
8528 | static __cold int io_rsrc_ref_quiesce(struct io_rsrc_data *data, |
8529 | struct io_ring_ctx *ctx) | |
8bad28d8 HX |
8530 | { |
8531 | int ret; | |
05589553 | 8532 | |
215c3902 | 8533 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
8bad28d8 HX |
8534 | if (data->quiesce) |
8535 | return -ENXIO; | |
05589553 | 8536 | |
8bad28d8 | 8537 | data->quiesce = true; |
1ffc5422 | 8538 | do { |
a7f0ed5a | 8539 | ret = io_rsrc_node_switch_start(ctx); |
8dd03afe | 8540 | if (ret) |
f2303b1f | 8541 | break; |
a7f0ed5a | 8542 | io_rsrc_node_switch(ctx, data); |
f2303b1f | 8543 | |
3e942498 PB |
8544 | /* kill initial ref, already quiesced if zero */ |
8545 | if (atomic_dec_and_test(&data->refs)) | |
8546 | break; | |
c018db4a | 8547 | mutex_unlock(&ctx->uring_lock); |
8bad28d8 | 8548 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 | 8549 | ret = wait_for_completion_interruptible(&data->done); |
c018db4a JA |
8550 | if (!ret) { |
8551 | mutex_lock(&ctx->uring_lock); | |
80912cef DY |
8552 | if (atomic_read(&data->refs) > 0) { |
8553 | /* | |
8554 | * it has been revived by another thread while | |
8555 | * we were unlocked | |
8556 | */ | |
8557 | mutex_unlock(&ctx->uring_lock); | |
8558 | } else { | |
8559 | break; | |
8560 | } | |
c018db4a | 8561 | } |
8bad28d8 | 8562 | |
3e942498 PB |
8563 | atomic_inc(&data->refs); |
8564 | /* wait for all works potentially completing data->done */ | |
8565 | flush_delayed_work(&ctx->rsrc_put_work); | |
cb5e1b81 | 8566 | reinit_completion(&data->done); |
8dd03afe | 8567 | |
1ffc5422 | 8568 | ret = io_run_task_work_sig(); |
8bad28d8 | 8569 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 8570 | } while (ret >= 0); |
8bad28d8 | 8571 | data->quiesce = false; |
05f3fb3c | 8572 | |
8bad28d8 | 8573 | return ret; |
d7954b2b BM |
8574 | } |
8575 | ||
2d091d62 PB |
8576 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
8577 | { | |
8578 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; | |
8579 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; | |
8580 | ||
8581 | return &data->tags[table_idx][off]; | |
8582 | } | |
8583 | ||
44b31f2f | 8584 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
1ad555c6 | 8585 | { |
2d091d62 PB |
8586 | size_t size = data->nr * sizeof(data->tags[0][0]); |
8587 | ||
8588 | if (data->tags) | |
8589 | io_free_page_table((void **)data->tags, size); | |
44b31f2f PB |
8590 | kfree(data); |
8591 | } | |
8592 | ||
c072481d PB |
8593 | static __cold int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
8594 | u64 __user *utags, unsigned nr, | |
8595 | struct io_rsrc_data **pdata) | |
1ad555c6 | 8596 | { |
b895c9a6 | 8597 | struct io_rsrc_data *data; |
2d091d62 | 8598 | int ret = -ENOMEM; |
d878c816 | 8599 | unsigned i; |
1ad555c6 BM |
8600 | |
8601 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
8602 | if (!data) | |
d878c816 | 8603 | return -ENOMEM; |
2d091d62 | 8604 | data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0])); |
b60c8dce | 8605 | if (!data->tags) { |
1ad555c6 | 8606 | kfree(data); |
d878c816 PB |
8607 | return -ENOMEM; |
8608 | } | |
2d091d62 PB |
8609 | |
8610 | data->nr = nr; | |
8611 | data->ctx = ctx; | |
8612 | data->do_put = do_put; | |
d878c816 | 8613 | if (utags) { |
2d091d62 | 8614 | ret = -EFAULT; |
d878c816 | 8615 | for (i = 0; i < nr; i++) { |
fdd1dc31 CIK |
8616 | u64 *tag_slot = io_get_tag_slot(data, i); |
8617 | ||
8618 | if (copy_from_user(tag_slot, &utags[i], | |
8619 | sizeof(*tag_slot))) | |
2d091d62 | 8620 | goto fail; |
d878c816 | 8621 | } |
1ad555c6 | 8622 | } |
b60c8dce | 8623 | |
3e942498 | 8624 | atomic_set(&data->refs, 1); |
1ad555c6 | 8625 | init_completion(&data->done); |
d878c816 PB |
8626 | *pdata = data; |
8627 | return 0; | |
2d091d62 PB |
8628 | fail: |
8629 | io_rsrc_data_free(data); | |
8630 | return ret; | |
1ad555c6 BM |
8631 | } |
8632 | ||
9123c8ff PB |
8633 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
8634 | { | |
0bea96f5 PB |
8635 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
8636 | GFP_KERNEL_ACCOUNT); | |
9123c8ff PB |
8637 | return !!table->files; |
8638 | } | |
8639 | ||
042b0d85 | 8640 | static void io_free_file_tables(struct io_file_table *table) |
9123c8ff | 8641 | { |
042b0d85 | 8642 | kvfree(table->files); |
9123c8ff PB |
8643 | table->files = NULL; |
8644 | } | |
8645 | ||
fff4db76 | 8646 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
1ad555c6 | 8647 | { |
69cc1b6f | 8648 | #if !defined(IO_URING_SCM_ALL) |
1f59bc0f PB |
8649 | int i; |
8650 | ||
8651 | for (i = 0; i < ctx->nr_user_files; i++) { | |
8652 | struct file *file = io_file_from_index(ctx, i); | |
8653 | ||
5e45690a JA |
8654 | if (!file) |
8655 | continue; | |
8656 | if (io_fixed_file_slot(&ctx->file_table, i)->file_ptr & FFS_SCM) | |
1f59bc0f | 8657 | continue; |
1f59bc0f PB |
8658 | fput(file); |
8659 | } | |
5e45690a | 8660 | #endif |
1f59bc0f | 8661 | |
fff4db76 PB |
8662 | #if defined(CONFIG_UNIX) |
8663 | if (ctx->ring_sock) { | |
8664 | struct sock *sock = ctx->ring_sock->sk; | |
8665 | struct sk_buff *skb; | |
8666 | ||
8667 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
8668 | kfree_skb(skb); | |
8669 | } | |
fff4db76 | 8670 | #endif |
042b0d85 | 8671 | io_free_file_tables(&ctx->file_table); |
44b31f2f | 8672 | io_rsrc_data_free(ctx->file_data); |
fff4db76 PB |
8673 | ctx->file_data = NULL; |
8674 | ctx->nr_user_files = 0; | |
1ad555c6 BM |
8675 | } |
8676 | ||
d7954b2b BM |
8677 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
8678 | { | |
d7954b2b BM |
8679 | int ret; |
8680 | ||
08480400 | 8681 | if (!ctx->file_data) |
d7954b2b | 8682 | return -ENXIO; |
08480400 PB |
8683 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
8684 | if (!ret) | |
8685 | __io_sqe_files_unregister(ctx); | |
8686 | return ret; | |
6b06314c JA |
8687 | } |
8688 | ||
37d1e2e3 | 8689 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
09a6f4ef | 8690 | __releases(&sqd->lock) |
37d1e2e3 | 8691 | { |
521d6a73 PB |
8692 | WARN_ON_ONCE(sqd->thread == current); |
8693 | ||
9e138a48 PB |
8694 | /* |
8695 | * Do the dance but not conditional clear_bit() because it'd race with | |
8696 | * other threads incrementing park_pending and setting the bit. | |
8697 | */ | |
37d1e2e3 | 8698 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
9e138a48 PB |
8699 | if (atomic_dec_return(&sqd->park_pending)) |
8700 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
09a6f4ef | 8701 | mutex_unlock(&sqd->lock); |
37d1e2e3 JA |
8702 | } |
8703 | ||
86e0d676 | 8704 | static void io_sq_thread_park(struct io_sq_data *sqd) |
09a6f4ef | 8705 | __acquires(&sqd->lock) |
37d1e2e3 | 8706 | { |
521d6a73 PB |
8707 | WARN_ON_ONCE(sqd->thread == current); |
8708 | ||
9e138a48 | 8709 | atomic_inc(&sqd->park_pending); |
86e0d676 | 8710 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
09a6f4ef | 8711 | mutex_lock(&sqd->lock); |
05962f95 | 8712 | if (sqd->thread) |
86e0d676 | 8713 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
8714 | } |
8715 | ||
8716 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
8717 | { | |
521d6a73 | 8718 | WARN_ON_ONCE(sqd->thread == current); |
88885f66 | 8719 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
521d6a73 | 8720 | |
05962f95 | 8721 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
88885f66 | 8722 | mutex_lock(&sqd->lock); |
e8f98f24 JA |
8723 | if (sqd->thread) |
8724 | wake_up_process(sqd->thread); | |
09a6f4ef | 8725 | mutex_unlock(&sqd->lock); |
05962f95 | 8726 | wait_for_completion(&sqd->exited); |
37d1e2e3 JA |
8727 | } |
8728 | ||
534ca6d6 | 8729 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 8730 | { |
534ca6d6 | 8731 | if (refcount_dec_and_test(&sqd->refs)) { |
9e138a48 PB |
8732 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
8733 | ||
37d1e2e3 JA |
8734 | io_sq_thread_stop(sqd); |
8735 | kfree(sqd); | |
8736 | } | |
8737 | } | |
8738 | ||
8739 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
8740 | { | |
8741 | struct io_sq_data *sqd = ctx->sq_data; | |
8742 | ||
8743 | if (sqd) { | |
05962f95 | 8744 | io_sq_thread_park(sqd); |
521d6a73 | 8745 | list_del_init(&ctx->sqd_list); |
37d1e2e3 | 8746 | io_sqd_update_thread_idle(sqd); |
05962f95 | 8747 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
8748 | |
8749 | io_put_sq_data(sqd); | |
8750 | ctx->sq_data = NULL; | |
534ca6d6 JA |
8751 | } |
8752 | } | |
8753 | ||
aa06165d JA |
8754 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
8755 | { | |
8756 | struct io_ring_ctx *ctx_attach; | |
8757 | struct io_sq_data *sqd; | |
8758 | struct fd f; | |
8759 | ||
8760 | f = fdget(p->wq_fd); | |
8761 | if (!f.file) | |
8762 | return ERR_PTR(-ENXIO); | |
8763 | if (f.file->f_op != &io_uring_fops) { | |
8764 | fdput(f); | |
8765 | return ERR_PTR(-EINVAL); | |
8766 | } | |
8767 | ||
8768 | ctx_attach = f.file->private_data; | |
8769 | sqd = ctx_attach->sq_data; | |
8770 | if (!sqd) { | |
8771 | fdput(f); | |
8772 | return ERR_PTR(-EINVAL); | |
8773 | } | |
5c2469e0 JA |
8774 | if (sqd->task_tgid != current->tgid) { |
8775 | fdput(f); | |
8776 | return ERR_PTR(-EPERM); | |
8777 | } | |
aa06165d JA |
8778 | |
8779 | refcount_inc(&sqd->refs); | |
8780 | fdput(f); | |
8781 | return sqd; | |
8782 | } | |
8783 | ||
26984fbf PB |
8784 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
8785 | bool *attached) | |
534ca6d6 JA |
8786 | { |
8787 | struct io_sq_data *sqd; | |
8788 | ||
26984fbf | 8789 | *attached = false; |
5c2469e0 JA |
8790 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
8791 | sqd = io_attach_sq_data(p); | |
26984fbf PB |
8792 | if (!IS_ERR(sqd)) { |
8793 | *attached = true; | |
5c2469e0 | 8794 | return sqd; |
26984fbf | 8795 | } |
5c2469e0 JA |
8796 | /* fall through for EPERM case, setup new sqd/task */ |
8797 | if (PTR_ERR(sqd) != -EPERM) | |
8798 | return sqd; | |
8799 | } | |
aa06165d | 8800 | |
534ca6d6 JA |
8801 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
8802 | if (!sqd) | |
8803 | return ERR_PTR(-ENOMEM); | |
8804 | ||
9e138a48 | 8805 | atomic_set(&sqd->park_pending, 0); |
534ca6d6 | 8806 | refcount_set(&sqd->refs, 1); |
69fb2131 | 8807 | INIT_LIST_HEAD(&sqd->ctx_list); |
09a6f4ef | 8808 | mutex_init(&sqd->lock); |
534ca6d6 | 8809 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 8810 | init_completion(&sqd->exited); |
534ca6d6 JA |
8811 | return sqd; |
8812 | } | |
8813 | ||
6b06314c JA |
8814 | /* |
8815 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
8816 | * the io_uring can be safely unregistered on process exit, even if we have | |
1f59bc0f PB |
8817 | * loops in the file referencing. We account only files that can hold other |
8818 | * files because otherwise they can't form a loop and so are not interesting | |
8819 | * for GC. | |
6b06314c | 8820 | */ |
8b3171bd | 8821 | static int io_scm_file_account(struct io_ring_ctx *ctx, struct file *file) |
6b06314c | 8822 | { |
73b25d3b | 8823 | #if defined(CONFIG_UNIX) |
6b06314c | 8824 | struct sock *sk = ctx->ring_sock->sk; |
73b25d3b | 8825 | struct sk_buff_head *head = &sk->sk_receive_queue; |
6b06314c JA |
8826 | struct scm_fp_list *fpl; |
8827 | struct sk_buff *skb; | |
6b06314c | 8828 | |
73b25d3b PB |
8829 | if (likely(!io_file_need_scm(file))) |
8830 | return 0; | |
8831 | ||
8832 | /* | |
8833 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
8834 | * file set. If there's no room, fall back to allocating a new skb | |
8835 | * and filling it in. | |
8836 | */ | |
8837 | spin_lock_irq(&head->lock); | |
8838 | skb = skb_peek(head); | |
8839 | if (skb && UNIXCB(skb).fp->count < SCM_MAX_FD) | |
8840 | __skb_unlink(skb, head); | |
8841 | else | |
8842 | skb = NULL; | |
8843 | spin_unlock_irq(&head->lock); | |
6b06314c | 8844 | |
6b06314c | 8845 | if (!skb) { |
73b25d3b PB |
8846 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
8847 | if (!fpl) | |
8848 | return -ENOMEM; | |
6b06314c | 8849 | |
73b25d3b PB |
8850 | skb = alloc_skb(0, GFP_KERNEL); |
8851 | if (!skb) { | |
8852 | kfree(fpl); | |
8853 | return -ENOMEM; | |
8854 | } | |
6b06314c | 8855 | |
73b25d3b PB |
8856 | fpl->user = get_uid(current_user()); |
8857 | fpl->max = SCM_MAX_FD; | |
8858 | fpl->count = 0; | |
dca58c6a | 8859 | |
73b25d3b PB |
8860 | UNIXCB(skb).fp = fpl; |
8861 | skb->sk = sk; | |
8862 | skb->destructor = unix_destruct_scm; | |
8863 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); | |
8864 | } | |
8865 | ||
8866 | fpl = UNIXCB(skb).fp; | |
8867 | fpl->fp[fpl->count++] = get_file(file); | |
8868 | unix_inflight(fpl->user, file); | |
8869 | skb_queue_head(head, skb); | |
dca58c6a | 8870 | fput(file); |
73b25d3b | 8871 | #endif |
6b06314c JA |
8872 | return 0; |
8873 | } | |
6b06314c | 8874 | |
47e90392 | 8875 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 8876 | { |
50238531 | 8877 | struct file *file = prsrc->file; |
05f3fb3c JA |
8878 | #if defined(CONFIG_UNIX) |
8879 | struct sock *sock = ctx->ring_sock->sk; | |
8880 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
8881 | struct sk_buff *skb; | |
8882 | int i; | |
8883 | ||
1f59bc0f PB |
8884 | if (!io_file_need_scm(file)) { |
8885 | fput(file); | |
8886 | return; | |
8887 | } | |
8888 | ||
05f3fb3c JA |
8889 | __skb_queue_head_init(&list); |
8890 | ||
8891 | /* | |
8892 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
8893 | * remove this entry and rearrange the file array. | |
8894 | */ | |
8895 | skb = skb_dequeue(head); | |
8896 | while (skb) { | |
8897 | struct scm_fp_list *fp; | |
8898 | ||
8899 | fp = UNIXCB(skb).fp; | |
8900 | for (i = 0; i < fp->count; i++) { | |
8901 | int left; | |
8902 | ||
8903 | if (fp->fp[i] != file) | |
8904 | continue; | |
8905 | ||
8906 | unix_notinflight(fp->user, fp->fp[i]); | |
8907 | left = fp->count - 1 - i; | |
8908 | if (left) { | |
8909 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
8910 | left * sizeof(struct file *)); | |
8911 | } | |
8912 | fp->count--; | |
8913 | if (!fp->count) { | |
8914 | kfree_skb(skb); | |
8915 | skb = NULL; | |
8916 | } else { | |
8917 | __skb_queue_tail(&list, skb); | |
8918 | } | |
8919 | fput(file); | |
8920 | file = NULL; | |
8921 | break; | |
8922 | } | |
8923 | ||
8924 | if (!file) | |
8925 | break; | |
8926 | ||
8927 | __skb_queue_tail(&list, skb); | |
8928 | ||
8929 | skb = skb_dequeue(head); | |
8930 | } | |
8931 | ||
8932 | if (skb_peek(&list)) { | |
8933 | spin_lock_irq(&head->lock); | |
8934 | while ((skb = __skb_dequeue(&list)) != NULL) | |
8935 | __skb_queue_tail(head, skb); | |
8936 | spin_unlock_irq(&head->lock); | |
8937 | } | |
8938 | #else | |
8939 | fput(file); | |
8940 | #endif | |
8941 | } | |
8942 | ||
b895c9a6 | 8943 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
65e19f54 | 8944 | { |
b895c9a6 | 8945 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
269bbe5f BM |
8946 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
8947 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 8948 | |
269bbe5f BM |
8949 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
8950 | list_del(&prsrc->list); | |
b60c8dce PB |
8951 | |
8952 | if (prsrc->tag) { | |
f8929630 PB |
8953 | if (ctx->flags & IORING_SETUP_IOPOLL) |
8954 | mutex_lock(&ctx->uring_lock); | |
b60c8dce | 8955 | |
79ebeaee | 8956 | spin_lock(&ctx->completion_lock); |
913a571a | 8957 | io_fill_cqe_aux(ctx, prsrc->tag, 0, 0); |
b60c8dce | 8958 | io_commit_cqring(ctx); |
79ebeaee | 8959 | spin_unlock(&ctx->completion_lock); |
b60c8dce | 8960 | io_cqring_ev_posted(ctx); |
f8929630 PB |
8961 | |
8962 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
8963 | mutex_unlock(&ctx->uring_lock); | |
b60c8dce PB |
8964 | } |
8965 | ||
40ae0ff7 | 8966 | rsrc_data->do_put(ctx, prsrc); |
269bbe5f | 8967 | kfree(prsrc); |
65e19f54 | 8968 | } |
05589553 | 8969 | |
28a9fe25 | 8970 | io_rsrc_node_destroy(ref_node); |
3e942498 PB |
8971 | if (atomic_dec_and_test(&rsrc_data->refs)) |
8972 | complete(&rsrc_data->done); | |
2faf852d | 8973 | } |
65e19f54 | 8974 | |
269bbe5f | 8975 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
8976 | { |
8977 | struct io_ring_ctx *ctx; | |
8978 | struct llist_node *node; | |
8979 | ||
269bbe5f BM |
8980 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
8981 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
8982 | |
8983 | while (node) { | |
b895c9a6 | 8984 | struct io_rsrc_node *ref_node; |
4a38aed2 JA |
8985 | struct llist_node *next = node->next; |
8986 | ||
b895c9a6 | 8987 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
269bbe5f | 8988 | __io_rsrc_put_work(ref_node); |
4a38aed2 JA |
8989 | node = next; |
8990 | } | |
8991 | } | |
8992 | ||
6b06314c | 8993 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
792e3582 | 8994 | unsigned nr_args, u64 __user *tags) |
6b06314c JA |
8995 | { |
8996 | __s32 __user *fds = (__s32 __user *) arg; | |
05f3fb3c | 8997 | struct file *file; |
f3baed39 | 8998 | int fd, ret; |
846a4ef2 | 8999 | unsigned i; |
6b06314c | 9000 | |
05f3fb3c | 9001 | if (ctx->file_data) |
6b06314c JA |
9002 | return -EBUSY; |
9003 | if (!nr_args) | |
9004 | return -EINVAL; | |
9005 | if (nr_args > IORING_MAX_FIXED_FILES) | |
9006 | return -EMFILE; | |
3a1b8a4e PB |
9007 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
9008 | return -EMFILE; | |
a7f0ed5a | 9009 | ret = io_rsrc_node_switch_start(ctx); |
f3baed39 PB |
9010 | if (ret) |
9011 | return ret; | |
d878c816 PB |
9012 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
9013 | &ctx->file_data); | |
9014 | if (ret) | |
9015 | return ret; | |
6b06314c | 9016 | |
a03a2a20 PB |
9017 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) { |
9018 | io_rsrc_data_free(ctx->file_data); | |
9019 | ctx->file_data = NULL; | |
9020 | return -ENOMEM; | |
9021 | } | |
65e19f54 | 9022 | |
08a45173 | 9023 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
a03a2a20 PB |
9024 | struct io_fixed_file *file_slot; |
9025 | ||
d878c816 | 9026 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
600cf3f8 | 9027 | ret = -EFAULT; |
a03a2a20 | 9028 | goto fail; |
600cf3f8 | 9029 | } |
08a45173 | 9030 | /* allow sparse sets */ |
792e3582 PB |
9031 | if (fd == -1) { |
9032 | ret = -EINVAL; | |
2d091d62 | 9033 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
a03a2a20 | 9034 | goto fail; |
08a45173 | 9035 | continue; |
792e3582 | 9036 | } |
6b06314c | 9037 | |
05f3fb3c | 9038 | file = fget(fd); |
6b06314c | 9039 | ret = -EBADF; |
792e3582 | 9040 | if (unlikely(!file)) |
a03a2a20 | 9041 | goto fail; |
05f3fb3c | 9042 | |
6b06314c JA |
9043 | /* |
9044 | * Don't allow io_uring instances to be registered. If UNIX | |
9045 | * isn't enabled, then this causes a reference cycle and this | |
9046 | * instance can never get freed. If UNIX is enabled we'll | |
9047 | * handle it just fine, but there's still no point in allowing | |
9048 | * a ring fd as it doesn't support regular read/write anyway. | |
9049 | */ | |
05f3fb3c JA |
9050 | if (file->f_op == &io_uring_fops) { |
9051 | fput(file); | |
a03a2a20 PB |
9052 | goto fail; |
9053 | } | |
8b3171bd | 9054 | ret = io_scm_file_account(ctx, file); |
a03a2a20 | 9055 | if (ret) { |
a03a2a20 PB |
9056 | fput(file); |
9057 | goto fail; | |
6b06314c | 9058 | } |
e390510a PB |
9059 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
9060 | io_fixed_file_set(file_slot, file); | |
05589553 | 9061 | } |
6b06314c | 9062 | |
a7f0ed5a | 9063 | io_rsrc_node_switch(ctx, NULL); |
a03a2a20 PB |
9064 | return 0; |
9065 | fail: | |
9066 | __io_sqe_files_unregister(ctx); | |
6b06314c JA |
9067 | return ret; |
9068 | } | |
9069 | ||
9c7b0ba8 PB |
9070 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
9071 | struct io_rsrc_node *node, void *rsrc) | |
9072 | { | |
8f0a2480 | 9073 | u64 *tag_slot = io_get_tag_slot(data, idx); |
9c7b0ba8 PB |
9074 | struct io_rsrc_put *prsrc; |
9075 | ||
9076 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); | |
9077 | if (!prsrc) | |
9078 | return -ENOMEM; | |
9079 | ||
8f0a2480 PB |
9080 | prsrc->tag = *tag_slot; |
9081 | *tag_slot = 0; | |
9c7b0ba8 PB |
9082 | prsrc->rsrc = rsrc; |
9083 | list_add(&prsrc->list, &node->rsrc_list); | |
9084 | return 0; | |
9085 | } | |
9086 | ||
b9445598 PB |
9087 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
9088 | unsigned int issue_flags, u32 slot_index) | |
9089 | { | |
9090 | struct io_ring_ctx *ctx = req->ctx; | |
9c7b0ba8 | 9091 | bool needs_switch = false; |
b9445598 PB |
9092 | struct io_fixed_file *file_slot; |
9093 | int ret = -EBADF; | |
9094 | ||
f8929630 | 9095 | io_ring_submit_lock(ctx, issue_flags); |
b9445598 PB |
9096 | if (file->f_op == &io_uring_fops) |
9097 | goto err; | |
9098 | ret = -ENXIO; | |
9099 | if (!ctx->file_data) | |
9100 | goto err; | |
9101 | ret = -EINVAL; | |
9102 | if (slot_index >= ctx->nr_user_files) | |
9103 | goto err; | |
9104 | ||
9105 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); | |
9106 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); | |
9c7b0ba8 PB |
9107 | |
9108 | if (file_slot->file_ptr) { | |
9109 | struct file *old_file; | |
9110 | ||
9111 | ret = io_rsrc_node_switch_start(ctx); | |
9112 | if (ret) | |
9113 | goto err; | |
9114 | ||
9115 | old_file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
9116 | ret = io_queue_rsrc_removal(ctx->file_data, slot_index, | |
9117 | ctx->rsrc_node, old_file); | |
9118 | if (ret) | |
9119 | goto err; | |
9120 | file_slot->file_ptr = 0; | |
9121 | needs_switch = true; | |
9122 | } | |
b9445598 | 9123 | |
8b3171bd | 9124 | ret = io_scm_file_account(ctx, file); |
e390510a PB |
9125 | if (!ret) { |
9126 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; | |
9127 | io_fixed_file_set(file_slot, file); | |
b9445598 | 9128 | } |
b9445598 | 9129 | err: |
9c7b0ba8 PB |
9130 | if (needs_switch) |
9131 | io_rsrc_node_switch(ctx, ctx->file_data); | |
f8929630 | 9132 | io_ring_submit_unlock(ctx, issue_flags); |
b9445598 PB |
9133 | if (ret) |
9134 | fput(file); | |
9135 | return ret; | |
9136 | } | |
9137 | ||
7df778be PB |
9138 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) |
9139 | { | |
9140 | unsigned int offset = req->close.file_slot - 1; | |
9141 | struct io_ring_ctx *ctx = req->ctx; | |
9142 | struct io_fixed_file *file_slot; | |
9143 | struct file *file; | |
4cdd158b | 9144 | int ret; |
7df778be | 9145 | |
f8929630 | 9146 | io_ring_submit_lock(ctx, issue_flags); |
7df778be PB |
9147 | ret = -ENXIO; |
9148 | if (unlikely(!ctx->file_data)) | |
9149 | goto out; | |
9150 | ret = -EINVAL; | |
9151 | if (offset >= ctx->nr_user_files) | |
9152 | goto out; | |
9153 | ret = io_rsrc_node_switch_start(ctx); | |
9154 | if (ret) | |
9155 | goto out; | |
9156 | ||
4cdd158b PB |
9157 | offset = array_index_nospec(offset, ctx->nr_user_files); |
9158 | file_slot = io_fixed_file_slot(&ctx->file_table, offset); | |
7df778be PB |
9159 | ret = -EBADF; |
9160 | if (!file_slot->file_ptr) | |
9161 | goto out; | |
9162 | ||
9163 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
9164 | ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file); | |
9165 | if (ret) | |
9166 | goto out; | |
9167 | ||
9168 | file_slot->file_ptr = 0; | |
9169 | io_rsrc_node_switch(ctx, ctx->file_data); | |
9170 | ret = 0; | |
9171 | out: | |
f8929630 | 9172 | io_ring_submit_unlock(ctx, issue_flags); |
7df778be PB |
9173 | return ret; |
9174 | } | |
9175 | ||
05f3fb3c | 9176 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
c3bdad02 | 9177 | struct io_uring_rsrc_update2 *up, |
05f3fb3c JA |
9178 | unsigned nr_args) |
9179 | { | |
c3bdad02 | 9180 | u64 __user *tags = u64_to_user_ptr(up->tags); |
98f0b3b4 | 9181 | __s32 __user *fds = u64_to_user_ptr(up->data); |
b895c9a6 | 9182 | struct io_rsrc_data *data = ctx->file_data; |
a04b0ac0 PB |
9183 | struct io_fixed_file *file_slot; |
9184 | struct file *file; | |
98f0b3b4 PB |
9185 | int fd, i, err = 0; |
9186 | unsigned int done; | |
05589553 | 9187 | bool needs_switch = false; |
c3a31e60 | 9188 | |
98f0b3b4 PB |
9189 | if (!ctx->file_data) |
9190 | return -ENXIO; | |
9191 | if (up->offset + nr_args > ctx->nr_user_files) | |
c3a31e60 JA |
9192 | return -EINVAL; |
9193 | ||
67973b93 | 9194 | for (done = 0; done < nr_args; done++) { |
c3bdad02 PB |
9195 | u64 tag = 0; |
9196 | ||
9197 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || | |
9198 | copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
c3a31e60 JA |
9199 | err = -EFAULT; |
9200 | break; | |
9201 | } | |
c3bdad02 PB |
9202 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
9203 | err = -EINVAL; | |
9204 | break; | |
9205 | } | |
4e0377a1 | 9206 | if (fd == IORING_REGISTER_FILES_SKIP) |
9207 | continue; | |
9208 | ||
67973b93 | 9209 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
aeca241b | 9210 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
ea64ec02 | 9211 | |
a04b0ac0 PB |
9212 | if (file_slot->file_ptr) { |
9213 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
4cdd158b | 9214 | err = io_queue_rsrc_removal(data, i, ctx->rsrc_node, file); |
a5318d3c HD |
9215 | if (err) |
9216 | break; | |
a04b0ac0 | 9217 | file_slot->file_ptr = 0; |
05589553 | 9218 | needs_switch = true; |
c3a31e60 JA |
9219 | } |
9220 | if (fd != -1) { | |
c3a31e60 JA |
9221 | file = fget(fd); |
9222 | if (!file) { | |
9223 | err = -EBADF; | |
9224 | break; | |
9225 | } | |
9226 | /* | |
9227 | * Don't allow io_uring instances to be registered. If | |
9228 | * UNIX isn't enabled, then this causes a reference | |
9229 | * cycle and this instance can never get freed. If UNIX | |
9230 | * is enabled we'll handle it just fine, but there's | |
9231 | * still no point in allowing a ring fd as it doesn't | |
9232 | * support regular read/write anyway. | |
9233 | */ | |
9234 | if (file->f_op == &io_uring_fops) { | |
9235 | fput(file); | |
9236 | err = -EBADF; | |
9237 | break; | |
9238 | } | |
8b3171bd | 9239 | err = io_scm_file_account(ctx, file); |
f3bd9dae YY |
9240 | if (err) { |
9241 | fput(file); | |
c3a31e60 | 9242 | break; |
f3bd9dae | 9243 | } |
e390510a PB |
9244 | *io_get_tag_slot(data, i) = tag; |
9245 | io_fixed_file_set(file_slot, file); | |
c3a31e60 | 9246 | } |
05f3fb3c JA |
9247 | } |
9248 | ||
a7f0ed5a PB |
9249 | if (needs_switch) |
9250 | io_rsrc_node_switch(ctx, data); | |
c3a31e60 JA |
9251 | return done ? done : err; |
9252 | } | |
05589553 | 9253 | |
685fe7fe JA |
9254 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
9255 | struct task_struct *task) | |
24369c2e | 9256 | { |
e941894e | 9257 | struct io_wq_hash *hash; |
24369c2e | 9258 | struct io_wq_data data; |
24369c2e | 9259 | unsigned int concurrency; |
24369c2e | 9260 | |
362a9e65 | 9261 | mutex_lock(&ctx->uring_lock); |
e941894e JA |
9262 | hash = ctx->hash_map; |
9263 | if (!hash) { | |
9264 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
362a9e65 YY |
9265 | if (!hash) { |
9266 | mutex_unlock(&ctx->uring_lock); | |
e941894e | 9267 | return ERR_PTR(-ENOMEM); |
362a9e65 | 9268 | } |
e941894e JA |
9269 | refcount_set(&hash->refs, 1); |
9270 | init_waitqueue_head(&hash->wait); | |
9271 | ctx->hash_map = hash; | |
24369c2e | 9272 | } |
362a9e65 | 9273 | mutex_unlock(&ctx->uring_lock); |
24369c2e | 9274 | |
e941894e | 9275 | data.hash = hash; |
685fe7fe | 9276 | data.task = task; |
ebc11b6c | 9277 | data.free_work = io_wq_free_work; |
f5fa38c5 | 9278 | data.do_work = io_wq_submit_work; |
24369c2e | 9279 | |
d25e3a3d JA |
9280 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
9281 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 9282 | |
5aa75ed5 | 9283 | return io_wq_create(concurrency, &data); |
24369c2e PB |
9284 | } |
9285 | ||
c072481d PB |
9286 | static __cold int io_uring_alloc_task_context(struct task_struct *task, |
9287 | struct io_ring_ctx *ctx) | |
0f212204 JA |
9288 | { |
9289 | struct io_uring_task *tctx; | |
d8a6df10 | 9290 | int ret; |
0f212204 | 9291 | |
09899b19 | 9292 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
0f212204 JA |
9293 | if (unlikely(!tctx)) |
9294 | return -ENOMEM; | |
9295 | ||
e7a6c00d JA |
9296 | tctx->registered_rings = kcalloc(IO_RINGFD_REG_MAX, |
9297 | sizeof(struct file *), GFP_KERNEL); | |
9298 | if (unlikely(!tctx->registered_rings)) { | |
9299 | kfree(tctx); | |
9300 | return -ENOMEM; | |
9301 | } | |
9302 | ||
d8a6df10 JA |
9303 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
9304 | if (unlikely(ret)) { | |
e7a6c00d | 9305 | kfree(tctx->registered_rings); |
d8a6df10 JA |
9306 | kfree(tctx); |
9307 | return ret; | |
9308 | } | |
9309 | ||
685fe7fe | 9310 | tctx->io_wq = io_init_wq_offload(ctx, task); |
5aa75ed5 JA |
9311 | if (IS_ERR(tctx->io_wq)) { |
9312 | ret = PTR_ERR(tctx->io_wq); | |
9313 | percpu_counter_destroy(&tctx->inflight); | |
e7a6c00d | 9314 | kfree(tctx->registered_rings); |
5aa75ed5 JA |
9315 | kfree(tctx); |
9316 | return ret; | |
9317 | } | |
9318 | ||
0f212204 JA |
9319 | xa_init(&tctx->xa); |
9320 | init_waitqueue_head(&tctx->wait); | |
fdaf083c | 9321 | atomic_set(&tctx->in_idle, 0); |
0f212204 | 9322 | task->io_uring = tctx; |
7cbf1722 JA |
9323 | spin_lock_init(&tctx->task_lock); |
9324 | INIT_WQ_LIST(&tctx->task_list); | |
4813c377 | 9325 | INIT_WQ_LIST(&tctx->prior_task_list); |
7cbf1722 | 9326 | init_task_work(&tctx->task_work, tctx_task_work); |
0f212204 JA |
9327 | return 0; |
9328 | } | |
9329 | ||
9330 | void __io_uring_free(struct task_struct *tsk) | |
9331 | { | |
9332 | struct io_uring_task *tctx = tsk->io_uring; | |
9333 | ||
9334 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e | 9335 | WARN_ON_ONCE(tctx->io_wq); |
09899b19 | 9336 | WARN_ON_ONCE(tctx->cached_refs); |
ef8eaa4e | 9337 | |
e7a6c00d | 9338 | kfree(tctx->registered_rings); |
d8a6df10 | 9339 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
9340 | kfree(tctx); |
9341 | tsk->io_uring = NULL; | |
9342 | } | |
9343 | ||
c072481d PB |
9344 | static __cold int io_sq_offload_create(struct io_ring_ctx *ctx, |
9345 | struct io_uring_params *p) | |
2b188cc1 JA |
9346 | { |
9347 | int ret; | |
9348 | ||
d25e3a3d JA |
9349 | /* Retain compatibility with failing for an invalid attach attempt */ |
9350 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
9351 | IORING_SETUP_ATTACH_WQ) { | |
9352 | struct fd f; | |
9353 | ||
9354 | f = fdget(p->wq_fd); | |
9355 | if (!f.file) | |
9356 | return -ENXIO; | |
0cc936f7 JA |
9357 | if (f.file->f_op != &io_uring_fops) { |
9358 | fdput(f); | |
f2a48dd0 | 9359 | return -EINVAL; |
0cc936f7 JA |
9360 | } |
9361 | fdput(f); | |
d25e3a3d | 9362 | } |
6c271ce2 | 9363 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 9364 | struct task_struct *tsk; |
534ca6d6 | 9365 | struct io_sq_data *sqd; |
26984fbf | 9366 | bool attached; |
534ca6d6 | 9367 | |
cdc1404a PM |
9368 | ret = security_uring_sqpoll(); |
9369 | if (ret) | |
9370 | return ret; | |
9371 | ||
26984fbf | 9372 | sqd = io_get_sq_data(p, &attached); |
534ca6d6 JA |
9373 | if (IS_ERR(sqd)) { |
9374 | ret = PTR_ERR(sqd); | |
9375 | goto err; | |
9376 | } | |
69fb2131 | 9377 | |
7c30f36a | 9378 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 9379 | ctx->sq_data = sqd; |
917257da JA |
9380 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
9381 | if (!ctx->sq_thread_idle) | |
9382 | ctx->sq_thread_idle = HZ; | |
9383 | ||
78d7f6ba | 9384 | io_sq_thread_park(sqd); |
de75a3d3 PB |
9385 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
9386 | io_sqd_update_thread_idle(sqd); | |
26984fbf | 9387 | /* don't attach to a dying SQPOLL thread, would be racy */ |
f2a48dd0 | 9388 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
78d7f6ba PB |
9389 | io_sq_thread_unpark(sqd); |
9390 | ||
de75a3d3 PB |
9391 | if (ret < 0) |
9392 | goto err; | |
9393 | if (attached) | |
5aa75ed5 | 9394 | return 0; |
aa06165d | 9395 | |
6c271ce2 | 9396 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 9397 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 9398 | |
917257da | 9399 | ret = -EINVAL; |
f2a48dd0 | 9400 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
e8f98f24 | 9401 | goto err_sqpoll; |
37d1e2e3 | 9402 | sqd->sq_cpu = cpu; |
6c271ce2 | 9403 | } else { |
37d1e2e3 | 9404 | sqd->sq_cpu = -1; |
6c271ce2 | 9405 | } |
37d1e2e3 JA |
9406 | |
9407 | sqd->task_pid = current->pid; | |
5c2469e0 | 9408 | sqd->task_tgid = current->tgid; |
46fe18b1 JA |
9409 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
9410 | if (IS_ERR(tsk)) { | |
9411 | ret = PTR_ERR(tsk); | |
e8f98f24 | 9412 | goto err_sqpoll; |
6c271ce2 | 9413 | } |
97a73a0f | 9414 | |
46fe18b1 | 9415 | sqd->thread = tsk; |
97a73a0f | 9416 | ret = io_uring_alloc_task_context(tsk, ctx); |
46fe18b1 | 9417 | wake_up_new_task(tsk); |
0f212204 JA |
9418 | if (ret) |
9419 | goto err; | |
6c271ce2 JA |
9420 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
9421 | /* Can't have SQ_AFF without SQPOLL */ | |
9422 | ret = -EINVAL; | |
9423 | goto err; | |
9424 | } | |
9425 | ||
2b188cc1 | 9426 | return 0; |
f2a48dd0 PB |
9427 | err_sqpoll: |
9428 | complete(&ctx->sq_data->exited); | |
2b188cc1 | 9429 | err: |
37d1e2e3 | 9430 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
9431 | return ret; |
9432 | } | |
9433 | ||
a087e2b5 BM |
9434 | static inline void __io_unaccount_mem(struct user_struct *user, |
9435 | unsigned long nr_pages) | |
2b188cc1 JA |
9436 | { |
9437 | atomic_long_sub(nr_pages, &user->locked_vm); | |
9438 | } | |
9439 | ||
a087e2b5 BM |
9440 | static inline int __io_account_mem(struct user_struct *user, |
9441 | unsigned long nr_pages) | |
2b188cc1 JA |
9442 | { |
9443 | unsigned long page_limit, cur_pages, new_pages; | |
9444 | ||
9445 | /* Don't allow more pages than we can safely lock */ | |
9446 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
9447 | ||
9448 | do { | |
9449 | cur_pages = atomic_long_read(&user->locked_vm); | |
9450 | new_pages = cur_pages + nr_pages; | |
9451 | if (new_pages > page_limit) | |
9452 | return -ENOMEM; | |
9453 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
9454 | new_pages) != cur_pages); | |
9455 | ||
9456 | return 0; | |
9457 | } | |
9458 | ||
26bfa89e | 9459 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 9460 | { |
62e398be | 9461 | if (ctx->user) |
a087e2b5 | 9462 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 9463 | |
26bfa89e JA |
9464 | if (ctx->mm_account) |
9465 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
9466 | } |
9467 | ||
26bfa89e | 9468 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 9469 | { |
30975825 BM |
9470 | int ret; |
9471 | ||
62e398be | 9472 | if (ctx->user) { |
30975825 BM |
9473 | ret = __io_account_mem(ctx->user, nr_pages); |
9474 | if (ret) | |
9475 | return ret; | |
9476 | } | |
9477 | ||
26bfa89e JA |
9478 | if (ctx->mm_account) |
9479 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
9480 | |
9481 | return 0; | |
9482 | } | |
9483 | ||
2b188cc1 JA |
9484 | static void io_mem_free(void *ptr) |
9485 | { | |
52e04ef4 MR |
9486 | struct page *page; |
9487 | ||
9488 | if (!ptr) | |
9489 | return; | |
2b188cc1 | 9490 | |
52e04ef4 | 9491 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
9492 | if (put_page_testzero(page)) |
9493 | free_compound_page(page); | |
9494 | } | |
9495 | ||
9496 | static void *io_mem_alloc(size_t size) | |
9497 | { | |
0a3f1e0b | 9498 | gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP; |
2b188cc1 | 9499 | |
0a3f1e0b | 9500 | return (void *) __get_free_pages(gfp, get_order(size)); |
2b188cc1 JA |
9501 | } |
9502 | ||
75b28aff HV |
9503 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
9504 | size_t *sq_offset) | |
9505 | { | |
9506 | struct io_rings *rings; | |
9507 | size_t off, sq_array_size; | |
9508 | ||
9509 | off = struct_size(rings, cqes, cq_entries); | |
9510 | if (off == SIZE_MAX) | |
9511 | return SIZE_MAX; | |
9512 | ||
9513 | #ifdef CONFIG_SMP | |
9514 | off = ALIGN(off, SMP_CACHE_BYTES); | |
9515 | if (off == 0) | |
9516 | return SIZE_MAX; | |
9517 | #endif | |
9518 | ||
b36200f5 DV |
9519 | if (sq_offset) |
9520 | *sq_offset = off; | |
9521 | ||
75b28aff HV |
9522 | sq_array_size = array_size(sizeof(u32), sq_entries); |
9523 | if (sq_array_size == SIZE_MAX) | |
9524 | return SIZE_MAX; | |
9525 | ||
9526 | if (check_add_overflow(off, sq_array_size, &off)) | |
9527 | return SIZE_MAX; | |
9528 | ||
75b28aff HV |
9529 | return off; |
9530 | } | |
9531 | ||
41edf1a5 | 9532 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot) |
7f61a1e9 | 9533 | { |
41edf1a5 | 9534 | struct io_mapped_ubuf *imu = *slot; |
7f61a1e9 PB |
9535 | unsigned int i; |
9536 | ||
6224843d PB |
9537 | if (imu != ctx->dummy_ubuf) { |
9538 | for (i = 0; i < imu->nr_bvecs; i++) | |
9539 | unpin_user_page(imu->bvec[i].bv_page); | |
9540 | if (imu->acct_pages) | |
9541 | io_unaccount_mem(ctx, imu->acct_pages); | |
9542 | kvfree(imu); | |
9543 | } | |
41edf1a5 | 9544 | *slot = NULL; |
7f61a1e9 PB |
9545 | } |
9546 | ||
bd54b6fe | 9547 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
edafccee | 9548 | { |
634d00df PB |
9549 | io_buffer_unmap(ctx, &prsrc->buf); |
9550 | prsrc->buf = NULL; | |
bd54b6fe | 9551 | } |
edafccee | 9552 | |
bd54b6fe BM |
9553 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
9554 | { | |
9555 | unsigned int i; | |
edafccee | 9556 | |
7f61a1e9 PB |
9557 | for (i = 0; i < ctx->nr_user_bufs; i++) |
9558 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); | |
edafccee | 9559 | kfree(ctx->user_bufs); |
bb6659cc | 9560 | io_rsrc_data_free(ctx->buf_data); |
edafccee | 9561 | ctx->user_bufs = NULL; |
bd54b6fe | 9562 | ctx->buf_data = NULL; |
edafccee | 9563 | ctx->nr_user_bufs = 0; |
bd54b6fe BM |
9564 | } |
9565 | ||
0a96bbe4 | 9566 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee | 9567 | { |
bd54b6fe | 9568 | int ret; |
edafccee | 9569 | |
bd54b6fe | 9570 | if (!ctx->buf_data) |
edafccee JA |
9571 | return -ENXIO; |
9572 | ||
bd54b6fe BM |
9573 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
9574 | if (!ret) | |
9575 | __io_sqe_buffers_unregister(ctx); | |
9576 | return ret; | |
edafccee JA |
9577 | } |
9578 | ||
9579 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
9580 | void __user *arg, unsigned index) | |
9581 | { | |
9582 | struct iovec __user *src; | |
9583 | ||
9584 | #ifdef CONFIG_COMPAT | |
9585 | if (ctx->compat) { | |
9586 | struct compat_iovec __user *ciovs; | |
9587 | struct compat_iovec ciov; | |
9588 | ||
9589 | ciovs = (struct compat_iovec __user *) arg; | |
9590 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
9591 | return -EFAULT; | |
9592 | ||
d55e5f5b | 9593 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
9594 | dst->iov_len = ciov.iov_len; |
9595 | return 0; | |
9596 | } | |
9597 | #endif | |
9598 | src = (struct iovec __user *) arg; | |
9599 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
9600 | return -EFAULT; | |
9601 | return 0; | |
9602 | } | |
9603 | ||
de293938 JA |
9604 | /* |
9605 | * Not super efficient, but this is just a registration time. And we do cache | |
9606 | * the last compound head, so generally we'll only do a full search if we don't | |
9607 | * match that one. | |
9608 | * | |
9609 | * We check if the given compound head page has already been accounted, to | |
9610 | * avoid double accounting it. This allows us to account the full size of the | |
9611 | * page, not just the constituent pages of a huge page. | |
9612 | */ | |
9613 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
9614 | int nr_pages, struct page *hpage) | |
9615 | { | |
9616 | int i, j; | |
9617 | ||
9618 | /* check current page array */ | |
9619 | for (i = 0; i < nr_pages; i++) { | |
9620 | if (!PageCompound(pages[i])) | |
9621 | continue; | |
9622 | if (compound_head(pages[i]) == hpage) | |
9623 | return true; | |
9624 | } | |
9625 | ||
9626 | /* check previously registered pages */ | |
9627 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
41edf1a5 | 9628 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
de293938 JA |
9629 | |
9630 | for (j = 0; j < imu->nr_bvecs; j++) { | |
9631 | if (!PageCompound(imu->bvec[j].bv_page)) | |
9632 | continue; | |
9633 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
9634 | return true; | |
9635 | } | |
9636 | } | |
9637 | ||
9638 | return false; | |
9639 | } | |
9640 | ||
9641 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
9642 | int nr_pages, struct io_mapped_ubuf *imu, | |
9643 | struct page **last_hpage) | |
9644 | { | |
9645 | int i, ret; | |
9646 | ||
216e5835 | 9647 | imu->acct_pages = 0; |
de293938 JA |
9648 | for (i = 0; i < nr_pages; i++) { |
9649 | if (!PageCompound(pages[i])) { | |
9650 | imu->acct_pages++; | |
9651 | } else { | |
9652 | struct page *hpage; | |
9653 | ||
9654 | hpage = compound_head(pages[i]); | |
9655 | if (hpage == *last_hpage) | |
9656 | continue; | |
9657 | *last_hpage = hpage; | |
9658 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
9659 | continue; | |
9660 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
9661 | } | |
9662 | } | |
9663 | ||
9664 | if (!imu->acct_pages) | |
9665 | return 0; | |
9666 | ||
26bfa89e | 9667 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
9668 | if (ret) |
9669 | imu->acct_pages = 0; | |
9670 | return ret; | |
9671 | } | |
9672 | ||
0a96bbe4 | 9673 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
41edf1a5 | 9674 | struct io_mapped_ubuf **pimu, |
0a96bbe4 | 9675 | struct page **last_hpage) |
edafccee | 9676 | { |
41edf1a5 | 9677 | struct io_mapped_ubuf *imu = NULL; |
edafccee JA |
9678 | struct vm_area_struct **vmas = NULL; |
9679 | struct page **pages = NULL; | |
0a96bbe4 BM |
9680 | unsigned long off, start, end, ubuf; |
9681 | size_t size; | |
9682 | int ret, pret, nr_pages, i; | |
9683 | ||
6224843d PB |
9684 | if (!iov->iov_base) { |
9685 | *pimu = ctx->dummy_ubuf; | |
9686 | return 0; | |
9687 | } | |
9688 | ||
0a96bbe4 BM |
9689 | ubuf = (unsigned long) iov->iov_base; |
9690 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
9691 | start = ubuf >> PAGE_SHIFT; | |
9692 | nr_pages = end - start; | |
9693 | ||
41edf1a5 | 9694 | *pimu = NULL; |
0a96bbe4 BM |
9695 | ret = -ENOMEM; |
9696 | ||
9697 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
9698 | if (!pages) | |
9699 | goto done; | |
9700 | ||
9701 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
9702 | GFP_KERNEL); | |
9703 | if (!vmas) | |
9704 | goto done; | |
edafccee | 9705 | |
41edf1a5 | 9706 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
a2b4198c | 9707 | if (!imu) |
0a96bbe4 BM |
9708 | goto done; |
9709 | ||
9710 | ret = 0; | |
9711 | mmap_read_lock(current->mm); | |
9712 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
9713 | pages, vmas); | |
9714 | if (pret == nr_pages) { | |
9715 | /* don't support file backed memory */ | |
9716 | for (i = 0; i < nr_pages; i++) { | |
9717 | struct vm_area_struct *vma = vmas[i]; | |
9718 | ||
40dad765 PB |
9719 | if (vma_is_shmem(vma)) |
9720 | continue; | |
0a96bbe4 BM |
9721 | if (vma->vm_file && |
9722 | !is_file_hugepages(vma->vm_file)) { | |
9723 | ret = -EOPNOTSUPP; | |
9724 | break; | |
9725 | } | |
9726 | } | |
9727 | } else { | |
9728 | ret = pret < 0 ? pret : -EFAULT; | |
9729 | } | |
9730 | mmap_read_unlock(current->mm); | |
9731 | if (ret) { | |
9732 | /* | |
9733 | * if we did partial map, or found file backed vmas, | |
9734 | * release any pages we did get | |
9735 | */ | |
9736 | if (pret > 0) | |
9737 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9738 | goto done; |
9739 | } | |
9740 | ||
9741 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
9742 | if (ret) { | |
9743 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9744 | goto done; |
9745 | } | |
9746 | ||
9747 | off = ubuf & ~PAGE_MASK; | |
9748 | size = iov->iov_len; | |
9749 | for (i = 0; i < nr_pages; i++) { | |
9750 | size_t vec_len; | |
9751 | ||
9752 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
9753 | imu->bvec[i].bv_page = pages[i]; | |
9754 | imu->bvec[i].bv_len = vec_len; | |
9755 | imu->bvec[i].bv_offset = off; | |
9756 | off = 0; | |
9757 | size -= vec_len; | |
9758 | } | |
9759 | /* store original address for later verification */ | |
9760 | imu->ubuf = ubuf; | |
4751f53d | 9761 | imu->ubuf_end = ubuf + iov->iov_len; |
0a96bbe4 | 9762 | imu->nr_bvecs = nr_pages; |
41edf1a5 | 9763 | *pimu = imu; |
0a96bbe4 BM |
9764 | ret = 0; |
9765 | done: | |
41edf1a5 PB |
9766 | if (ret) |
9767 | kvfree(imu); | |
0a96bbe4 BM |
9768 | kvfree(pages); |
9769 | kvfree(vmas); | |
9770 | return ret; | |
9771 | } | |
9772 | ||
2b358604 | 9773 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 9774 | { |
87094465 PB |
9775 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
9776 | return ctx->user_bufs ? 0 : -ENOMEM; | |
2b358604 | 9777 | } |
edafccee | 9778 | |
2b358604 BM |
9779 | static int io_buffer_validate(struct iovec *iov) |
9780 | { | |
50e96989 PB |
9781 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
9782 | ||
2b358604 BM |
9783 | /* |
9784 | * Don't impose further limits on the size and buffer | |
9785 | * constraints here, we'll -EINVAL later when IO is | |
9786 | * submitted if they are wrong. | |
9787 | */ | |
6224843d PB |
9788 | if (!iov->iov_base) |
9789 | return iov->iov_len ? -EFAULT : 0; | |
9790 | if (!iov->iov_len) | |
2b358604 | 9791 | return -EFAULT; |
edafccee | 9792 | |
2b358604 BM |
9793 | /* arbitrary limit, but we need something */ |
9794 | if (iov->iov_len > SZ_1G) | |
9795 | return -EFAULT; | |
edafccee | 9796 | |
50e96989 PB |
9797 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
9798 | return -EOVERFLOW; | |
9799 | ||
2b358604 BM |
9800 | return 0; |
9801 | } | |
edafccee | 9802 | |
2b358604 | 9803 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
634d00df | 9804 | unsigned int nr_args, u64 __user *tags) |
2b358604 | 9805 | { |
bd54b6fe BM |
9806 | struct page *last_hpage = NULL; |
9807 | struct io_rsrc_data *data; | |
2b358604 BM |
9808 | int i, ret; |
9809 | struct iovec iov; | |
edafccee | 9810 | |
87094465 PB |
9811 | if (ctx->user_bufs) |
9812 | return -EBUSY; | |
489809e2 | 9813 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
87094465 | 9814 | return -EINVAL; |
bd54b6fe | 9815 | ret = io_rsrc_node_switch_start(ctx); |
2b358604 BM |
9816 | if (ret) |
9817 | return ret; | |
d878c816 PB |
9818 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
9819 | if (ret) | |
9820 | return ret; | |
bd54b6fe BM |
9821 | ret = io_buffers_map_alloc(ctx, nr_args); |
9822 | if (ret) { | |
bb6659cc | 9823 | io_rsrc_data_free(data); |
bd54b6fe BM |
9824 | return ret; |
9825 | } | |
edafccee | 9826 | |
87094465 | 9827 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
edafccee JA |
9828 | ret = io_copy_iov(ctx, &iov, arg, i); |
9829 | if (ret) | |
0a96bbe4 | 9830 | break; |
2b358604 BM |
9831 | ret = io_buffer_validate(&iov); |
9832 | if (ret) | |
0a96bbe4 | 9833 | break; |
2d091d62 | 9834 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
cf3770e7 CIK |
9835 | ret = -EINVAL; |
9836 | break; | |
9837 | } | |
edafccee | 9838 | |
41edf1a5 PB |
9839 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
9840 | &last_hpage); | |
0a96bbe4 BM |
9841 | if (ret) |
9842 | break; | |
edafccee | 9843 | } |
0a96bbe4 | 9844 | |
bd54b6fe | 9845 | WARN_ON_ONCE(ctx->buf_data); |
0a96bbe4 | 9846 | |
bd54b6fe BM |
9847 | ctx->buf_data = data; |
9848 | if (ret) | |
9849 | __io_sqe_buffers_unregister(ctx); | |
9850 | else | |
9851 | io_rsrc_node_switch(ctx, NULL); | |
edafccee JA |
9852 | return ret; |
9853 | } | |
9854 | ||
634d00df PB |
9855 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
9856 | struct io_uring_rsrc_update2 *up, | |
9857 | unsigned int nr_args) | |
9858 | { | |
9859 | u64 __user *tags = u64_to_user_ptr(up->tags); | |
9860 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); | |
634d00df PB |
9861 | struct page *last_hpage = NULL; |
9862 | bool needs_switch = false; | |
9863 | __u32 done; | |
9864 | int i, err; | |
9865 | ||
9866 | if (!ctx->buf_data) | |
9867 | return -ENXIO; | |
9868 | if (up->offset + nr_args > ctx->nr_user_bufs) | |
9869 | return -EINVAL; | |
9870 | ||
9871 | for (done = 0; done < nr_args; done++) { | |
0b8c0e7c PB |
9872 | struct io_mapped_ubuf *imu; |
9873 | int offset = up->offset + done; | |
634d00df PB |
9874 | u64 tag = 0; |
9875 | ||
9876 | err = io_copy_iov(ctx, &iov, iovs, done); | |
9877 | if (err) | |
9878 | break; | |
9879 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { | |
9880 | err = -EFAULT; | |
9881 | break; | |
9882 | } | |
0b8c0e7c PB |
9883 | err = io_buffer_validate(&iov); |
9884 | if (err) | |
9885 | break; | |
cf3770e7 CIK |
9886 | if (!iov.iov_base && tag) { |
9887 | err = -EINVAL; | |
9888 | break; | |
9889 | } | |
0b8c0e7c PB |
9890 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
9891 | if (err) | |
9892 | break; | |
634d00df | 9893 | |
0b8c0e7c | 9894 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
6224843d | 9895 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
4cdd158b | 9896 | err = io_queue_rsrc_removal(ctx->buf_data, i, |
0b8c0e7c PB |
9897 | ctx->rsrc_node, ctx->user_bufs[i]); |
9898 | if (unlikely(err)) { | |
9899 | io_buffer_unmap(ctx, &imu); | |
634d00df | 9900 | break; |
0b8c0e7c | 9901 | } |
634d00df PB |
9902 | ctx->user_bufs[i] = NULL; |
9903 | needs_switch = true; | |
9904 | } | |
9905 | ||
0b8c0e7c | 9906 | ctx->user_bufs[i] = imu; |
2d091d62 | 9907 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
634d00df PB |
9908 | } |
9909 | ||
9910 | if (needs_switch) | |
9911 | io_rsrc_node_switch(ctx, ctx->buf_data); | |
9912 | return done ? done : err; | |
9913 | } | |
9914 | ||
c75312dd UA |
9915 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg, |
9916 | unsigned int eventfd_async) | |
9b402849 | 9917 | { |
77bc59b4 | 9918 | struct io_ev_fd *ev_fd; |
9b402849 | 9919 | __s32 __user *fds = arg; |
f0a4e62b | 9920 | int fd; |
9b402849 | 9921 | |
77bc59b4 UA |
9922 | ev_fd = rcu_dereference_protected(ctx->io_ev_fd, |
9923 | lockdep_is_held(&ctx->uring_lock)); | |
9924 | if (ev_fd) | |
9b402849 JA |
9925 | return -EBUSY; |
9926 | ||
9927 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
9928 | return -EFAULT; | |
9929 | ||
77bc59b4 UA |
9930 | ev_fd = kmalloc(sizeof(*ev_fd), GFP_KERNEL); |
9931 | if (!ev_fd) | |
9932 | return -ENOMEM; | |
fe7e3257 | 9933 | |
77bc59b4 UA |
9934 | ev_fd->cq_ev_fd = eventfd_ctx_fdget(fd); |
9935 | if (IS_ERR(ev_fd->cq_ev_fd)) { | |
f0a4e62b | 9936 | int ret = PTR_ERR(ev_fd->cq_ev_fd); |
77bc59b4 | 9937 | kfree(ev_fd); |
9b402849 JA |
9938 | return ret; |
9939 | } | |
c75312dd | 9940 | ev_fd->eventfd_async = eventfd_async; |
9aa8dfde | 9941 | ctx->has_evfd = true; |
77bc59b4 | 9942 | rcu_assign_pointer(ctx->io_ev_fd, ev_fd); |
f0a4e62b | 9943 | return 0; |
77bc59b4 UA |
9944 | } |
9945 | ||
9946 | static void io_eventfd_put(struct rcu_head *rcu) | |
9947 | { | |
9948 | struct io_ev_fd *ev_fd = container_of(rcu, struct io_ev_fd, rcu); | |
9949 | ||
9950 | eventfd_ctx_put(ev_fd->cq_ev_fd); | |
9951 | kfree(ev_fd); | |
9b402849 JA |
9952 | } |
9953 | ||
9954 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
9955 | { | |
77bc59b4 UA |
9956 | struct io_ev_fd *ev_fd; |
9957 | ||
9958 | ev_fd = rcu_dereference_protected(ctx->io_ev_fd, | |
9959 | lockdep_is_held(&ctx->uring_lock)); | |
9960 | if (ev_fd) { | |
9aa8dfde | 9961 | ctx->has_evfd = false; |
77bc59b4 UA |
9962 | rcu_assign_pointer(ctx->io_ev_fd, NULL); |
9963 | call_rcu(&ev_fd->rcu, io_eventfd_put); | |
9b402849 JA |
9964 | return 0; |
9965 | } | |
9966 | ||
9967 | return -ENXIO; | |
9968 | } | |
9969 | ||
5a2e745d JA |
9970 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
9971 | { | |
dbc7d452 JA |
9972 | int i; |
9973 | ||
9974 | for (i = 0; i < (1U << IO_BUFFERS_HASH_BITS); i++) { | |
9975 | struct list_head *list = &ctx->io_buffers[i]; | |
9e15c3a0 | 9976 | |
dbc7d452 JA |
9977 | while (!list_empty(list)) { |
9978 | struct io_buffer_list *bl; | |
9979 | ||
9980 | bl = list_first_entry(list, struct io_buffer_list, list); | |
9981 | __io_remove_buffers(ctx, bl, -1U); | |
9982 | list_del(&bl->list); | |
9983 | kfree(bl); | |
9984 | } | |
9985 | } | |
cc3cec83 JA |
9986 | |
9987 | while (!list_empty(&ctx->io_buffers_pages)) { | |
9988 | struct page *page; | |
9989 | ||
9990 | page = list_first_entry(&ctx->io_buffers_pages, struct page, lru); | |
9991 | list_del_init(&page->lru); | |
9992 | __free_page(page); | |
9993 | } | |
5a2e745d JA |
9994 | } |
9995 | ||
4010fec4 | 9996 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9997 | { |
cd0ca2e0 | 9998 | struct io_submit_state *state = &ctx->submit_state; |
37f0e767 | 9999 | int nr = 0; |
bf019da7 | 10000 | |
9a4fdbd8 | 10001 | mutex_lock(&ctx->uring_lock); |
cd0ca2e0 | 10002 | io_flush_cached_locked_reqs(ctx, state); |
9a4fdbd8 | 10003 | |
88ab95be | 10004 | while (!io_req_cache_empty(ctx)) { |
c2b6c6bc PB |
10005 | struct io_wq_work_node *node; |
10006 | struct io_kiocb *req; | |
9a4fdbd8 | 10007 | |
c2b6c6bc PB |
10008 | node = wq_stack_extract(&state->free_list); |
10009 | req = container_of(node, struct io_kiocb, comp_list); | |
10010 | kmem_cache_free(req_cachep, req); | |
37f0e767 | 10011 | nr++; |
c2b6c6bc | 10012 | } |
37f0e767 PB |
10013 | if (nr) |
10014 | percpu_ref_put_many(&ctx->refs, nr); | |
9a4fdbd8 JA |
10015 | mutex_unlock(&ctx->uring_lock); |
10016 | } | |
10017 | ||
43597aac | 10018 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
2b188cc1 | 10019 | { |
43597aac | 10020 | if (data && !atomic_dec_and_test(&data->refs)) |
bd54b6fe | 10021 | wait_for_completion(&data->done); |
bd54b6fe | 10022 | } |
04fc6c80 | 10023 | |
4d9237e3 JA |
10024 | static void io_flush_apoll_cache(struct io_ring_ctx *ctx) |
10025 | { | |
10026 | struct async_poll *apoll; | |
10027 | ||
10028 | while (!list_empty(&ctx->apoll_cache)) { | |
10029 | apoll = list_first_entry(&ctx->apoll_cache, struct async_poll, | |
10030 | poll.wait.entry); | |
10031 | list_del(&apoll->poll.wait.entry); | |
10032 | kfree(apoll); | |
10033 | } | |
10034 | } | |
10035 | ||
c072481d | 10036 | static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) |
2b188cc1 | 10037 | { |
37d1e2e3 | 10038 | io_sq_thread_finish(ctx); |
2aede0e4 | 10039 | |
37d1e2e3 | 10040 | if (ctx->mm_account) { |
2aede0e4 JA |
10041 | mmdrop(ctx->mm_account); |
10042 | ctx->mm_account = NULL; | |
30975825 | 10043 | } |
def596e9 | 10044 | |
ab409402 | 10045 | io_rsrc_refs_drop(ctx); |
43597aac PB |
10046 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
10047 | io_wait_rsrc_data(ctx->buf_data); | |
10048 | io_wait_rsrc_data(ctx->file_data); | |
10049 | ||
8bad28d8 | 10050 | mutex_lock(&ctx->uring_lock); |
43597aac | 10051 | if (ctx->buf_data) |
bd54b6fe | 10052 | __io_sqe_buffers_unregister(ctx); |
43597aac | 10053 | if (ctx->file_data) |
08480400 | 10054 | __io_sqe_files_unregister(ctx); |
c4ea060e PB |
10055 | if (ctx->rings) |
10056 | __io_cqring_overflow_flush(ctx, true); | |
9b402849 | 10057 | io_eventfd_unregister(ctx); |
4d9237e3 | 10058 | io_flush_apoll_cache(ctx); |
77bc59b4 | 10059 | mutex_unlock(&ctx->uring_lock); |
5a2e745d | 10060 | io_destroy_buffers(ctx); |
07db298a PB |
10061 | if (ctx->sq_creds) |
10062 | put_cred(ctx->sq_creds); | |
def596e9 | 10063 | |
a7f0ed5a PB |
10064 | /* there are no registered resources left, nobody uses it */ |
10065 | if (ctx->rsrc_node) | |
10066 | io_rsrc_node_destroy(ctx->rsrc_node); | |
8dd03afe | 10067 | if (ctx->rsrc_backup_node) |
b895c9a6 | 10068 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
a7f0ed5a | 10069 | flush_delayed_work(&ctx->rsrc_put_work); |
756ab7c0 | 10070 | flush_delayed_work(&ctx->fallback_work); |
a7f0ed5a PB |
10071 | |
10072 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); | |
10073 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); | |
def596e9 | 10074 | |
2b188cc1 | 10075 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
10076 | if (ctx->ring_sock) { |
10077 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 10078 | sock_release(ctx->ring_sock); |
355e8d26 | 10079 | } |
2b188cc1 | 10080 | #endif |
ef9dd637 | 10081 | WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); |
2b188cc1 | 10082 | |
75b28aff | 10083 | io_mem_free(ctx->rings); |
2b188cc1 | 10084 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
10085 | |
10086 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 10087 | free_uid(ctx->user); |
4010fec4 | 10088 | io_req_caches_free(ctx); |
e941894e JA |
10089 | if (ctx->hash_map) |
10090 | io_wq_put_hash(ctx->hash_map); | |
78076bb6 | 10091 | kfree(ctx->cancel_hash); |
6224843d | 10092 | kfree(ctx->dummy_ubuf); |
dbc7d452 | 10093 | kfree(ctx->io_buffers); |
2b188cc1 JA |
10094 | kfree(ctx); |
10095 | } | |
10096 | ||
10097 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
10098 | { | |
10099 | struct io_ring_ctx *ctx = file->private_data; | |
10100 | __poll_t mask = 0; | |
10101 | ||
d60aa65b | 10102 | poll_wait(file, &ctx->cq_wait, wait); |
4f7067c3 SB |
10103 | /* |
10104 | * synchronizes with barrier from wq_has_sleeper call in | |
10105 | * io_commit_cqring | |
10106 | */ | |
2b188cc1 | 10107 | smp_rmb(); |
90554200 | 10108 | if (!io_sqring_full(ctx)) |
2b188cc1 | 10109 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
10110 | |
10111 | /* | |
10112 | * Don't flush cqring overflow list here, just do a simple check. | |
10113 | * Otherwise there could possible be ABBA deadlock: | |
10114 | * CPU0 CPU1 | |
10115 | * ---- ---- | |
10116 | * lock(&ctx->uring_lock); | |
10117 | * lock(&ep->mtx); | |
10118 | * lock(&ctx->uring_lock); | |
10119 | * lock(&ep->mtx); | |
10120 | * | |
10121 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
10122 | * pushs them to do the flush. | |
10123 | */ | |
10988a0a DY |
10124 | if (io_cqring_events(ctx) || |
10125 | test_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq)) | |
2b188cc1 JA |
10126 | mask |= EPOLLIN | EPOLLRDNORM; |
10127 | ||
10128 | return mask; | |
10129 | } | |
10130 | ||
0bead8cd | 10131 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 10132 | { |
4379bf8b | 10133 | const struct cred *creds; |
071698e1 | 10134 | |
61cf9370 | 10135 | creds = xa_erase(&ctx->personalities, id); |
4379bf8b JA |
10136 | if (creds) { |
10137 | put_cred(creds); | |
0bead8cd | 10138 | return 0; |
1e6fa521 | 10139 | } |
0bead8cd YD |
10140 | |
10141 | return -EINVAL; | |
10142 | } | |
10143 | ||
d56d938b PB |
10144 | struct io_tctx_exit { |
10145 | struct callback_head task_work; | |
10146 | struct completion completion; | |
baf186c4 | 10147 | struct io_ring_ctx *ctx; |
d56d938b PB |
10148 | }; |
10149 | ||
c072481d | 10150 | static __cold void io_tctx_exit_cb(struct callback_head *cb) |
d56d938b PB |
10151 | { |
10152 | struct io_uring_task *tctx = current->io_uring; | |
10153 | struct io_tctx_exit *work; | |
10154 | ||
10155 | work = container_of(cb, struct io_tctx_exit, task_work); | |
10156 | /* | |
10157 | * When @in_idle, we're in cancellation and it's racy to remove the | |
10158 | * node. It'll be removed by the end of cancellation, just ignore it. | |
10159 | */ | |
10160 | if (!atomic_read(&tctx->in_idle)) | |
eef51daa | 10161 | io_uring_del_tctx_node((unsigned long)work->ctx); |
d56d938b PB |
10162 | complete(&work->completion); |
10163 | } | |
10164 | ||
c072481d | 10165 | static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
28090c13 PB |
10166 | { |
10167 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
10168 | ||
10169 | return req->ctx == data; | |
10170 | } | |
10171 | ||
c072481d | 10172 | static __cold void io_ring_exit_work(struct work_struct *work) |
85faa7b8 | 10173 | { |
d56d938b | 10174 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 10175 | unsigned long timeout = jiffies + HZ * 60 * 5; |
58d3be2c | 10176 | unsigned long interval = HZ / 20; |
d56d938b PB |
10177 | struct io_tctx_exit exit; |
10178 | struct io_tctx_node *node; | |
10179 | int ret; | |
85faa7b8 | 10180 | |
56952e91 JA |
10181 | /* |
10182 | * If we're doing polled IO and end up having requests being | |
10183 | * submitted async (out-of-line), then completions can come in while | |
10184 | * we're waiting for refs to drop. We need to reap these manually, | |
10185 | * as nobody else will be looking for them. | |
10186 | */ | |
b2edc0a7 | 10187 | do { |
3dd0c97a | 10188 | io_uring_try_cancel_requests(ctx, NULL, true); |
28090c13 PB |
10189 | if (ctx->sq_data) { |
10190 | struct io_sq_data *sqd = ctx->sq_data; | |
10191 | struct task_struct *tsk; | |
10192 | ||
10193 | io_sq_thread_park(sqd); | |
10194 | tsk = sqd->thread; | |
10195 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) | |
10196 | io_wq_cancel_cb(tsk->io_uring->io_wq, | |
10197 | io_cancel_ctx_cb, ctx, true); | |
10198 | io_sq_thread_unpark(sqd); | |
10199 | } | |
b5bb3a24 | 10200 | |
37f0e767 PB |
10201 | io_req_caches_free(ctx); |
10202 | ||
58d3be2c PB |
10203 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
10204 | /* there is little hope left, don't run it too often */ | |
10205 | interval = HZ * 60; | |
10206 | } | |
10207 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); | |
d56d938b | 10208 | |
7f00651a PB |
10209 | init_completion(&exit.completion); |
10210 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
10211 | exit.ctx = ctx; | |
89b5066e PB |
10212 | /* |
10213 | * Some may use context even when all refs and requests have been put, | |
10214 | * and they are free to do so while still holding uring_lock or | |
5b0a6acc | 10215 | * completion_lock, see io_req_task_submit(). Apart from other work, |
89b5066e PB |
10216 | * this lock/unlock section also waits them to finish. |
10217 | */ | |
d56d938b PB |
10218 | mutex_lock(&ctx->uring_lock); |
10219 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
10220 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
10221 | ||
d56d938b PB |
10222 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
10223 | ctx_node); | |
7f00651a PB |
10224 | /* don't spin on a single task if cancellation failed */ |
10225 | list_rotate_left(&ctx->tctx_list); | |
d56d938b PB |
10226 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
10227 | if (WARN_ON_ONCE(ret)) | |
10228 | continue; | |
d56d938b PB |
10229 | |
10230 | mutex_unlock(&ctx->uring_lock); | |
10231 | wait_for_completion(&exit.completion); | |
d56d938b PB |
10232 | mutex_lock(&ctx->uring_lock); |
10233 | } | |
10234 | mutex_unlock(&ctx->uring_lock); | |
79ebeaee JA |
10235 | spin_lock(&ctx->completion_lock); |
10236 | spin_unlock(&ctx->completion_lock); | |
d56d938b | 10237 | |
85faa7b8 JA |
10238 | io_ring_ctx_free(ctx); |
10239 | } | |
10240 | ||
80c4cbdb | 10241 | /* Returns true if we found and killed one or more timeouts */ |
c072481d PB |
10242 | static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx, |
10243 | struct task_struct *tsk, bool cancel_all) | |
80c4cbdb PB |
10244 | { |
10245 | struct io_kiocb *req, *tmp; | |
10246 | int canceled = 0; | |
10247 | ||
79ebeaee JA |
10248 | spin_lock(&ctx->completion_lock); |
10249 | spin_lock_irq(&ctx->timeout_lock); | |
80c4cbdb | 10250 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
3dd0c97a | 10251 | if (io_match_task(req, tsk, cancel_all)) { |
80c4cbdb PB |
10252 | io_kill_timeout(req, -ECANCELED); |
10253 | canceled++; | |
10254 | } | |
10255 | } | |
79ebeaee | 10256 | spin_unlock_irq(&ctx->timeout_lock); |
60053be8 | 10257 | io_commit_cqring(ctx); |
79ebeaee | 10258 | spin_unlock(&ctx->completion_lock); |
80c4cbdb PB |
10259 | if (canceled != 0) |
10260 | io_cqring_ev_posted(ctx); | |
10261 | return canceled != 0; | |
10262 | } | |
10263 | ||
c072481d | 10264 | static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
2b188cc1 | 10265 | { |
61cf9370 MWO |
10266 | unsigned long index; |
10267 | struct creds *creds; | |
10268 | ||
2b188cc1 JA |
10269 | mutex_lock(&ctx->uring_lock); |
10270 | percpu_ref_kill(&ctx->refs); | |
634578f8 | 10271 | if (ctx->rings) |
6c2450ae | 10272 | __io_cqring_overflow_flush(ctx, true); |
61cf9370 MWO |
10273 | xa_for_each(&ctx->personalities, index, creds) |
10274 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
10275 | mutex_unlock(&ctx->uring_lock); |
10276 | ||
60053be8 PB |
10277 | /* failed during ring init, it couldn't have issued any requests */ |
10278 | if (ctx->rings) { | |
10279 | io_kill_timeouts(ctx, NULL, true); | |
10280 | io_poll_remove_all(ctx, NULL, true); | |
10281 | /* if we failed setting up the ctx, we might not have any rings */ | |
10282 | io_iopoll_try_reap_events(ctx); | |
10283 | } | |
309fc03a | 10284 | |
85faa7b8 | 10285 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
10286 | /* |
10287 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
10288 | * if we're exiting a ton of rings at the same time. It just adds | |
10289 | * noise and overhead, there's no discernable change in runtime | |
10290 | * over using system_wq. | |
10291 | */ | |
10292 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
10293 | } |
10294 | ||
10295 | static int io_uring_release(struct inode *inode, struct file *file) | |
10296 | { | |
10297 | struct io_ring_ctx *ctx = file->private_data; | |
10298 | ||
10299 | file->private_data = NULL; | |
10300 | io_ring_ctx_wait_and_kill(ctx); | |
10301 | return 0; | |
10302 | } | |
10303 | ||
f6edbabb PB |
10304 | struct io_task_cancel { |
10305 | struct task_struct *task; | |
3dd0c97a | 10306 | bool all; |
f6edbabb | 10307 | }; |
f254ac04 | 10308 | |
f6edbabb | 10309 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 10310 | { |
9a472ef7 | 10311 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 10312 | struct io_task_cancel *cancel = data; |
9a472ef7 | 10313 | |
6af3f48b | 10314 | return io_match_task_safe(req, cancel->task, cancel->all); |
b711d4ea JA |
10315 | } |
10316 | ||
c072481d PB |
10317 | static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
10318 | struct task_struct *task, | |
10319 | bool cancel_all) | |
b7ddce3c | 10320 | { |
e1915f76 | 10321 | struct io_defer_entry *de; |
b7ddce3c PB |
10322 | LIST_HEAD(list); |
10323 | ||
79ebeaee | 10324 | spin_lock(&ctx->completion_lock); |
b7ddce3c | 10325 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
6af3f48b | 10326 | if (io_match_task_safe(de->req, task, cancel_all)) { |
b7ddce3c PB |
10327 | list_cut_position(&list, &ctx->defer_list, &de->list); |
10328 | break; | |
10329 | } | |
10330 | } | |
79ebeaee | 10331 | spin_unlock(&ctx->completion_lock); |
e1915f76 PB |
10332 | if (list_empty(&list)) |
10333 | return false; | |
b7ddce3c PB |
10334 | |
10335 | while (!list_empty(&list)) { | |
10336 | de = list_first_entry(&list, struct io_defer_entry, list); | |
10337 | list_del_init(&de->list); | |
f41db273 | 10338 | io_req_complete_failed(de->req, -ECANCELED); |
b7ddce3c PB |
10339 | kfree(de); |
10340 | } | |
e1915f76 | 10341 | return true; |
b7ddce3c PB |
10342 | } |
10343 | ||
c072481d | 10344 | static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
1b00764f PB |
10345 | { |
10346 | struct io_tctx_node *node; | |
10347 | enum io_wq_cancel cret; | |
10348 | bool ret = false; | |
10349 | ||
10350 | mutex_lock(&ctx->uring_lock); | |
10351 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
10352 | struct io_uring_task *tctx = node->task->io_uring; | |
10353 | ||
10354 | /* | |
10355 | * io_wq will stay alive while we hold uring_lock, because it's | |
10356 | * killed after ctx nodes, which requires to take the lock. | |
10357 | */ | |
10358 | if (!tctx || !tctx->io_wq) | |
10359 | continue; | |
10360 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
10361 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
10362 | } | |
10363 | mutex_unlock(&ctx->uring_lock); | |
10364 | ||
10365 | return ret; | |
10366 | } | |
10367 | ||
c072481d PB |
10368 | static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
10369 | struct task_struct *task, | |
10370 | bool cancel_all) | |
9936c7c2 | 10371 | { |
3dd0c97a | 10372 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
1b00764f | 10373 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 | 10374 | |
60053be8 PB |
10375 | /* failed during ring init, it couldn't have issued any requests */ |
10376 | if (!ctx->rings) | |
10377 | return; | |
10378 | ||
9936c7c2 PB |
10379 | while (1) { |
10380 | enum io_wq_cancel cret; | |
10381 | bool ret = false; | |
10382 | ||
1b00764f PB |
10383 | if (!task) { |
10384 | ret |= io_uring_try_cancel_iowq(ctx); | |
10385 | } else if (tctx && tctx->io_wq) { | |
10386 | /* | |
10387 | * Cancels requests of all rings, not only @ctx, but | |
10388 | * it's fine as the task is in exit/exec. | |
10389 | */ | |
5aa75ed5 | 10390 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
10391 | &cancel, true); |
10392 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
10393 | } | |
10394 | ||
10395 | /* SQPOLL thread does its own polling */ | |
3dd0c97a | 10396 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
d052d1d6 | 10397 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
5eef4e87 | 10398 | while (!wq_list_empty(&ctx->iopoll_list)) { |
9936c7c2 PB |
10399 | io_iopoll_try_reap_events(ctx); |
10400 | ret = true; | |
10401 | } | |
10402 | } | |
10403 | ||
3dd0c97a PB |
10404 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
10405 | ret |= io_poll_remove_all(ctx, task, cancel_all); | |
10406 | ret |= io_kill_timeouts(ctx, task, cancel_all); | |
e5dc480d PB |
10407 | if (task) |
10408 | ret |= io_run_task_work(); | |
9936c7c2 PB |
10409 | if (!ret) |
10410 | break; | |
10411 | cond_resched(); | |
10412 | } | |
10413 | } | |
10414 | ||
eef51daa | 10415 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
0f212204 | 10416 | { |
236434c3 | 10417 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 10418 | struct io_tctx_node *node; |
a528b04e | 10419 | int ret; |
236434c3 MWO |
10420 | |
10421 | if (unlikely(!tctx)) { | |
5aa75ed5 | 10422 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
10423 | if (unlikely(ret)) |
10424 | return ret; | |
e139a1ec | 10425 | |
236434c3 | 10426 | tctx = current->io_uring; |
e139a1ec PB |
10427 | if (ctx->iowq_limits_set) { |
10428 | unsigned int limits[2] = { ctx->iowq_limits[0], | |
10429 | ctx->iowq_limits[1], }; | |
10430 | ||
10431 | ret = io_wq_max_workers(tctx->io_wq, limits); | |
10432 | if (ret) | |
10433 | return ret; | |
10434 | } | |
0f212204 | 10435 | } |
cf27f3b1 PB |
10436 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
10437 | node = kmalloc(sizeof(*node), GFP_KERNEL); | |
10438 | if (!node) | |
10439 | return -ENOMEM; | |
10440 | node->ctx = ctx; | |
10441 | node->task = current; | |
13bf43f5 | 10442 | |
cf27f3b1 PB |
10443 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
10444 | node, GFP_KERNEL)); | |
10445 | if (ret) { | |
10446 | kfree(node); | |
10447 | return ret; | |
0f212204 | 10448 | } |
cf27f3b1 PB |
10449 | |
10450 | mutex_lock(&ctx->uring_lock); | |
10451 | list_add(&node->ctx_node, &ctx->tctx_list); | |
10452 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 10453 | } |
cf27f3b1 | 10454 | tctx->last = ctx; |
0f212204 JA |
10455 | return 0; |
10456 | } | |
10457 | ||
cf27f3b1 PB |
10458 | /* |
10459 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
10460 | */ | |
eef51daa | 10461 | static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
cf27f3b1 PB |
10462 | { |
10463 | struct io_uring_task *tctx = current->io_uring; | |
10464 | ||
10465 | if (likely(tctx && tctx->last == ctx)) | |
10466 | return 0; | |
eef51daa | 10467 | return __io_uring_add_tctx_node(ctx); |
cf27f3b1 PB |
10468 | } |
10469 | ||
0f212204 JA |
10470 | /* |
10471 | * Remove this io_uring_file -> task mapping. | |
10472 | */ | |
c072481d | 10473 | static __cold void io_uring_del_tctx_node(unsigned long index) |
0f212204 JA |
10474 | { |
10475 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 10476 | struct io_tctx_node *node; |
2941267b | 10477 | |
eebd2e37 PB |
10478 | if (!tctx) |
10479 | return; | |
13bf43f5 PB |
10480 | node = xa_erase(&tctx->xa, index); |
10481 | if (!node) | |
2941267b | 10482 | return; |
0f212204 | 10483 | |
13bf43f5 PB |
10484 | WARN_ON_ONCE(current != node->task); |
10485 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
10486 | ||
10487 | mutex_lock(&node->ctx->uring_lock); | |
10488 | list_del(&node->ctx_node); | |
10489 | mutex_unlock(&node->ctx->uring_lock); | |
10490 | ||
baf186c4 | 10491 | if (tctx->last == node->ctx) |
0f212204 | 10492 | tctx->last = NULL; |
13bf43f5 | 10493 | kfree(node); |
0f212204 JA |
10494 | } |
10495 | ||
c072481d | 10496 | static __cold void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 10497 | { |
ba5ef6dc | 10498 | struct io_wq *wq = tctx->io_wq; |
13bf43f5 | 10499 | struct io_tctx_node *node; |
de7f1d9e PB |
10500 | unsigned long index; |
10501 | ||
8bab4c09 | 10502 | xa_for_each(&tctx->xa, index, node) { |
eef51daa | 10503 | io_uring_del_tctx_node(index); |
8bab4c09 JA |
10504 | cond_resched(); |
10505 | } | |
b16ef427 ME |
10506 | if (wq) { |
10507 | /* | |
f6f9b278 | 10508 | * Must be after io_uring_del_tctx_node() (removes nodes under |
b16ef427 ME |
10509 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). |
10510 | */ | |
ba5ef6dc | 10511 | io_wq_put_and_exit(wq); |
dadebc35 | 10512 | tctx->io_wq = NULL; |
b16ef427 | 10513 | } |
de7f1d9e PB |
10514 | } |
10515 | ||
3f48cf18 | 10516 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
521d6a73 | 10517 | { |
3f48cf18 | 10518 | if (tracked) |
d5361233 | 10519 | return 0; |
521d6a73 PB |
10520 | return percpu_counter_sum(&tctx->inflight); |
10521 | } | |
10522 | ||
78cc687b PB |
10523 | /* |
10524 | * Find any io_uring ctx that this task has registered or done IO on, and cancel | |
78a78060 | 10525 | * requests. @sqd should be not-null IFF it's an SQPOLL thread cancellation. |
78cc687b | 10526 | */ |
c072481d PB |
10527 | static __cold void io_uring_cancel_generic(bool cancel_all, |
10528 | struct io_sq_data *sqd) | |
0e9ddb39 | 10529 | { |
521d6a73 | 10530 | struct io_uring_task *tctx = current->io_uring; |
734551df | 10531 | struct io_ring_ctx *ctx; |
0e9ddb39 PB |
10532 | s64 inflight; |
10533 | DEFINE_WAIT(wait); | |
fdaf083c | 10534 | |
78cc687b PB |
10535 | WARN_ON_ONCE(sqd && sqd->thread != current); |
10536 | ||
6d042ffb PO |
10537 | if (!current->io_uring) |
10538 | return; | |
17a91051 PB |
10539 | if (tctx->io_wq) |
10540 | io_wq_exit_start(tctx->io_wq); | |
10541 | ||
0e9ddb39 PB |
10542 | atomic_inc(&tctx->in_idle); |
10543 | do { | |
e9dbe221 | 10544 | io_uring_drop_tctx_refs(current); |
0e9ddb39 | 10545 | /* read completions before cancelations */ |
78cc687b | 10546 | inflight = tctx_inflight(tctx, !cancel_all); |
0e9ddb39 PB |
10547 | if (!inflight) |
10548 | break; | |
fdaf083c | 10549 | |
78cc687b PB |
10550 | if (!sqd) { |
10551 | struct io_tctx_node *node; | |
10552 | unsigned long index; | |
0f212204 | 10553 | |
78cc687b PB |
10554 | xa_for_each(&tctx->xa, index, node) { |
10555 | /* sqpoll task will cancel all its requests */ | |
10556 | if (node->ctx->sq_data) | |
10557 | continue; | |
10558 | io_uring_try_cancel_requests(node->ctx, current, | |
10559 | cancel_all); | |
10560 | } | |
10561 | } else { | |
10562 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
10563 | io_uring_try_cancel_requests(ctx, current, | |
10564 | cancel_all); | |
10565 | } | |
17a91051 | 10566 | |
78a78060 JA |
10567 | prepare_to_wait(&tctx->wait, &wait, TASK_INTERRUPTIBLE); |
10568 | io_run_task_work(); | |
e9dbe221 | 10569 | io_uring_drop_tctx_refs(current); |
78a78060 | 10570 | |
0f212204 | 10571 | /* |
a1bb3cd5 PB |
10572 | * If we've seen completions, retry without waiting. This |
10573 | * avoids a race where a completion comes in before we did | |
10574 | * prepare_to_wait(). | |
0f212204 | 10575 | */ |
3dd0c97a | 10576 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
a1bb3cd5 | 10577 | schedule(); |
f57555ed | 10578 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 10579 | } while (1); |
de7f1d9e | 10580 | |
8452d4a6 | 10581 | io_uring_clean_tctx(tctx); |
3dd0c97a | 10582 | if (cancel_all) { |
3cc7fdb9 PB |
10583 | /* |
10584 | * We shouldn't run task_works after cancel, so just leave | |
10585 | * ->in_idle set for normal exit. | |
10586 | */ | |
10587 | atomic_dec(&tctx->in_idle); | |
3f48cf18 PB |
10588 | /* for exec all current's requests should be gone, kill tctx */ |
10589 | __io_uring_free(current); | |
10590 | } | |
44e728b8 PB |
10591 | } |
10592 | ||
f552a27a | 10593 | void __io_uring_cancel(bool cancel_all) |
78cc687b | 10594 | { |
f552a27a | 10595 | io_uring_cancel_generic(cancel_all, NULL); |
78cc687b PB |
10596 | } |
10597 | ||
e7a6c00d JA |
10598 | void io_uring_unreg_ringfd(void) |
10599 | { | |
10600 | struct io_uring_task *tctx = current->io_uring; | |
10601 | int i; | |
10602 | ||
10603 | for (i = 0; i < IO_RINGFD_REG_MAX; i++) { | |
10604 | if (tctx->registered_rings[i]) { | |
10605 | fput(tctx->registered_rings[i]); | |
10606 | tctx->registered_rings[i] = NULL; | |
10607 | } | |
10608 | } | |
10609 | } | |
10610 | ||
10611 | static int io_ring_add_registered_fd(struct io_uring_task *tctx, int fd, | |
10612 | int start, int end) | |
10613 | { | |
10614 | struct file *file; | |
10615 | int offset; | |
10616 | ||
10617 | for (offset = start; offset < end; offset++) { | |
10618 | offset = array_index_nospec(offset, IO_RINGFD_REG_MAX); | |
10619 | if (tctx->registered_rings[offset]) | |
10620 | continue; | |
10621 | ||
10622 | file = fget(fd); | |
10623 | if (!file) { | |
10624 | return -EBADF; | |
10625 | } else if (file->f_op != &io_uring_fops) { | |
10626 | fput(file); | |
10627 | return -EOPNOTSUPP; | |
10628 | } | |
10629 | tctx->registered_rings[offset] = file; | |
10630 | return offset; | |
10631 | } | |
10632 | ||
10633 | return -EBUSY; | |
10634 | } | |
10635 | ||
10636 | /* | |
10637 | * Register a ring fd to avoid fdget/fdput for each io_uring_enter() | |
10638 | * invocation. User passes in an array of struct io_uring_rsrc_update | |
10639 | * with ->data set to the ring_fd, and ->offset given for the desired | |
10640 | * index. If no index is desired, application may set ->offset == -1U | |
10641 | * and we'll find an available index. Returns number of entries | |
10642 | * successfully processed, or < 0 on error if none were processed. | |
10643 | */ | |
10644 | static int io_ringfd_register(struct io_ring_ctx *ctx, void __user *__arg, | |
10645 | unsigned nr_args) | |
10646 | { | |
10647 | struct io_uring_rsrc_update __user *arg = __arg; | |
10648 | struct io_uring_rsrc_update reg; | |
10649 | struct io_uring_task *tctx; | |
10650 | int ret, i; | |
10651 | ||
10652 | if (!nr_args || nr_args > IO_RINGFD_REG_MAX) | |
10653 | return -EINVAL; | |
10654 | ||
10655 | mutex_unlock(&ctx->uring_lock); | |
10656 | ret = io_uring_add_tctx_node(ctx); | |
10657 | mutex_lock(&ctx->uring_lock); | |
10658 | if (ret) | |
10659 | return ret; | |
10660 | ||
10661 | tctx = current->io_uring; | |
10662 | for (i = 0; i < nr_args; i++) { | |
10663 | int start, end; | |
10664 | ||
10665 | if (copy_from_user(®, &arg[i], sizeof(reg))) { | |
10666 | ret = -EFAULT; | |
10667 | break; | |
10668 | } | |
10669 | ||
6fb53cf8 DY |
10670 | if (reg.resv) { |
10671 | ret = -EINVAL; | |
10672 | break; | |
10673 | } | |
10674 | ||
e7a6c00d JA |
10675 | if (reg.offset == -1U) { |
10676 | start = 0; | |
10677 | end = IO_RINGFD_REG_MAX; | |
10678 | } else { | |
10679 | if (reg.offset >= IO_RINGFD_REG_MAX) { | |
10680 | ret = -EINVAL; | |
10681 | break; | |
10682 | } | |
10683 | start = reg.offset; | |
10684 | end = start + 1; | |
10685 | } | |
10686 | ||
10687 | ret = io_ring_add_registered_fd(tctx, reg.data, start, end); | |
10688 | if (ret < 0) | |
10689 | break; | |
10690 | ||
10691 | reg.offset = ret; | |
10692 | if (copy_to_user(&arg[i], ®, sizeof(reg))) { | |
10693 | fput(tctx->registered_rings[reg.offset]); | |
10694 | tctx->registered_rings[reg.offset] = NULL; | |
10695 | ret = -EFAULT; | |
10696 | break; | |
10697 | } | |
10698 | } | |
10699 | ||
10700 | return i ? i : ret; | |
10701 | } | |
10702 | ||
10703 | static int io_ringfd_unregister(struct io_ring_ctx *ctx, void __user *__arg, | |
10704 | unsigned nr_args) | |
10705 | { | |
10706 | struct io_uring_rsrc_update __user *arg = __arg; | |
10707 | struct io_uring_task *tctx = current->io_uring; | |
10708 | struct io_uring_rsrc_update reg; | |
10709 | int ret = 0, i; | |
10710 | ||
10711 | if (!nr_args || nr_args > IO_RINGFD_REG_MAX) | |
10712 | return -EINVAL; | |
10713 | if (!tctx) | |
10714 | return 0; | |
10715 | ||
10716 | for (i = 0; i < nr_args; i++) { | |
10717 | if (copy_from_user(®, &arg[i], sizeof(reg))) { | |
10718 | ret = -EFAULT; | |
10719 | break; | |
10720 | } | |
6fb53cf8 | 10721 | if (reg.resv || reg.offset >= IO_RINGFD_REG_MAX) { |
e7a6c00d JA |
10722 | ret = -EINVAL; |
10723 | break; | |
10724 | } | |
10725 | ||
10726 | reg.offset = array_index_nospec(reg.offset, IO_RINGFD_REG_MAX); | |
10727 | if (tctx->registered_rings[reg.offset]) { | |
10728 | fput(tctx->registered_rings[reg.offset]); | |
10729 | tctx->registered_rings[reg.offset] = NULL; | |
10730 | } | |
10731 | } | |
10732 | ||
10733 | return i ? i : ret; | |
10734 | } | |
10735 | ||
6c5c240e RP |
10736 | static void *io_uring_validate_mmap_request(struct file *file, |
10737 | loff_t pgoff, size_t sz) | |
2b188cc1 | 10738 | { |
2b188cc1 | 10739 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 10740 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
10741 | struct page *page; |
10742 | void *ptr; | |
10743 | ||
10744 | switch (offset) { | |
10745 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
10746 | case IORING_OFF_CQ_RING: |
10747 | ptr = ctx->rings; | |
2b188cc1 JA |
10748 | break; |
10749 | case IORING_OFF_SQES: | |
10750 | ptr = ctx->sq_sqes; | |
10751 | break; | |
2b188cc1 | 10752 | default: |
6c5c240e | 10753 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
10754 | } |
10755 | ||
10756 | page = virt_to_head_page(ptr); | |
a50b854e | 10757 | if (sz > page_size(page)) |
6c5c240e RP |
10758 | return ERR_PTR(-EINVAL); |
10759 | ||
10760 | return ptr; | |
10761 | } | |
10762 | ||
10763 | #ifdef CONFIG_MMU | |
10764 | ||
c072481d | 10765 | static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
6c5c240e RP |
10766 | { |
10767 | size_t sz = vma->vm_end - vma->vm_start; | |
10768 | unsigned long pfn; | |
10769 | void *ptr; | |
10770 | ||
10771 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
10772 | if (IS_ERR(ptr)) | |
10773 | return PTR_ERR(ptr); | |
2b188cc1 JA |
10774 | |
10775 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
10776 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
10777 | } | |
10778 | ||
6c5c240e RP |
10779 | #else /* !CONFIG_MMU */ |
10780 | ||
10781 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
10782 | { | |
10783 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
10784 | } | |
10785 | ||
10786 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
10787 | { | |
10788 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
10789 | } | |
10790 | ||
10791 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
10792 | unsigned long addr, unsigned long len, | |
10793 | unsigned long pgoff, unsigned long flags) | |
10794 | { | |
10795 | void *ptr; | |
10796 | ||
10797 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
10798 | if (IS_ERR(ptr)) | |
10799 | return PTR_ERR(ptr); | |
10800 | ||
10801 | return (unsigned long) ptr; | |
10802 | } | |
10803 | ||
10804 | #endif /* !CONFIG_MMU */ | |
10805 | ||
d9d05217 | 10806 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 JA |
10807 | { |
10808 | DEFINE_WAIT(wait); | |
10809 | ||
10810 | do { | |
10811 | if (!io_sqring_full(ctx)) | |
10812 | break; | |
90554200 JA |
10813 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
10814 | ||
10815 | if (!io_sqring_full(ctx)) | |
10816 | break; | |
90554200 JA |
10817 | schedule(); |
10818 | } while (!signal_pending(current)); | |
10819 | ||
10820 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
5199328a | 10821 | return 0; |
90554200 JA |
10822 | } |
10823 | ||
f81440d3 PB |
10824 | static int io_validate_ext_arg(unsigned flags, const void __user *argp, size_t argsz) |
10825 | { | |
10826 | if (flags & IORING_ENTER_EXT_ARG) { | |
10827 | struct io_uring_getevents_arg arg; | |
10828 | ||
10829 | if (argsz != sizeof(arg)) | |
10830 | return -EINVAL; | |
10831 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
10832 | return -EFAULT; | |
10833 | } | |
10834 | return 0; | |
10835 | } | |
10836 | ||
c73ebb68 HX |
10837 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
10838 | struct __kernel_timespec __user **ts, | |
10839 | const sigset_t __user **sig) | |
10840 | { | |
10841 | struct io_uring_getevents_arg arg; | |
10842 | ||
10843 | /* | |
10844 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
10845 | * is just a pointer to the sigset_t. | |
10846 | */ | |
10847 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
10848 | *sig = (const sigset_t __user *) argp; | |
10849 | *ts = NULL; | |
10850 | return 0; | |
10851 | } | |
10852 | ||
10853 | /* | |
10854 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
10855 | * timespec and sigset_t pointers if good. | |
10856 | */ | |
10857 | if (*argsz != sizeof(arg)) | |
10858 | return -EINVAL; | |
10859 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
10860 | return -EFAULT; | |
d2347b96 DY |
10861 | if (arg.pad) |
10862 | return -EINVAL; | |
c73ebb68 HX |
10863 | *sig = u64_to_user_ptr(arg.sigmask); |
10864 | *argsz = arg.sigmask_sz; | |
10865 | *ts = u64_to_user_ptr(arg.ts); | |
10866 | return 0; | |
10867 | } | |
10868 | ||
2b188cc1 | 10869 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
10870 | u32, min_complete, u32, flags, const void __user *, argp, |
10871 | size_t, argsz) | |
2b188cc1 JA |
10872 | { |
10873 | struct io_ring_ctx *ctx; | |
2b188cc1 | 10874 | struct fd f; |
33f993da | 10875 | long ret; |
2b188cc1 | 10876 | |
4c6e277c | 10877 | io_run_task_work(); |
b41e9852 | 10878 | |
33f993da | 10879 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
e7a6c00d JA |
10880 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG | |
10881 | IORING_ENTER_REGISTERED_RING))) | |
2b188cc1 JA |
10882 | return -EINVAL; |
10883 | ||
e7a6c00d JA |
10884 | /* |
10885 | * Ring fd has been registered via IORING_REGISTER_RING_FDS, we | |
10886 | * need only dereference our task private array to find it. | |
10887 | */ | |
10888 | if (flags & IORING_ENTER_REGISTERED_RING) { | |
10889 | struct io_uring_task *tctx = current->io_uring; | |
10890 | ||
10891 | if (!tctx || fd >= IO_RINGFD_REG_MAX) | |
10892 | return -EINVAL; | |
10893 | fd = array_index_nospec(fd, IO_RINGFD_REG_MAX); | |
10894 | f.file = tctx->registered_rings[fd]; | |
10895 | if (unlikely(!f.file)) | |
10896 | return -EBADF; | |
10897 | } else { | |
10898 | f = fdget(fd); | |
10899 | if (unlikely(!f.file)) | |
10900 | return -EBADF; | |
10901 | } | |
2b188cc1 JA |
10902 | |
10903 | ret = -EOPNOTSUPP; | |
33f993da | 10904 | if (unlikely(f.file->f_op != &io_uring_fops)) |
2b188cc1 JA |
10905 | goto out_fput; |
10906 | ||
10907 | ret = -ENXIO; | |
10908 | ctx = f.file->private_data; | |
33f993da | 10909 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
2b188cc1 JA |
10910 | goto out_fput; |
10911 | ||
7e84e1c7 | 10912 | ret = -EBADFD; |
33f993da | 10913 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
10914 | goto out; |
10915 | ||
6c271ce2 JA |
10916 | /* |
10917 | * For SQ polling, the thread will do all submissions and completions. | |
10918 | * Just return the requested submit count, and wake the thread if | |
10919 | * we were asked to. | |
10920 | */ | |
b2a9eada | 10921 | ret = 0; |
6c271ce2 | 10922 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
90f67366 | 10923 | io_cqring_overflow_flush(ctx); |
89448c47 | 10924 | |
21f96522 JA |
10925 | if (unlikely(ctx->sq_data->thread == NULL)) { |
10926 | ret = -EOWNERDEAD; | |
04147488 | 10927 | goto out; |
21f96522 | 10928 | } |
6c271ce2 | 10929 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 10930 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
10931 | if (flags & IORING_ENTER_SQ_WAIT) { |
10932 | ret = io_sqpoll_wait_sq(ctx); | |
10933 | if (ret) | |
10934 | goto out; | |
10935 | } | |
3e813c90 | 10936 | ret = to_submit; |
b2a9eada | 10937 | } else if (to_submit) { |
eef51daa | 10938 | ret = io_uring_add_tctx_node(ctx); |
0f212204 JA |
10939 | if (unlikely(ret)) |
10940 | goto out; | |
d487b43c | 10941 | |
2b188cc1 | 10942 | mutex_lock(&ctx->uring_lock); |
3e813c90 DY |
10943 | ret = io_submit_sqes(ctx, to_submit); |
10944 | if (ret != to_submit) { | |
d487b43c | 10945 | mutex_unlock(&ctx->uring_lock); |
7c504e65 | 10946 | goto out; |
d487b43c PB |
10947 | } |
10948 | if ((flags & IORING_ENTER_GETEVENTS) && ctx->syscall_iopoll) | |
10949 | goto iopoll_locked; | |
10950 | mutex_unlock(&ctx->uring_lock); | |
2b188cc1 JA |
10951 | } |
10952 | if (flags & IORING_ENTER_GETEVENTS) { | |
3e813c90 | 10953 | int ret2; |
773697b6 | 10954 | if (ctx->syscall_iopoll) { |
d487b43c PB |
10955 | /* |
10956 | * We disallow the app entering submit/complete with | |
10957 | * polling, but we still need to lock the ring to | |
10958 | * prevent racing with polled issue that got punted to | |
10959 | * a workqueue. | |
10960 | */ | |
10961 | mutex_lock(&ctx->uring_lock); | |
10962 | iopoll_locked: | |
3e813c90 DY |
10963 | ret2 = io_validate_ext_arg(flags, argp, argsz); |
10964 | if (likely(!ret2)) { | |
10965 | min_complete = min(min_complete, | |
10966 | ctx->cq_entries); | |
10967 | ret2 = io_iopoll_check(ctx, min_complete); | |
d487b43c PB |
10968 | } |
10969 | mutex_unlock(&ctx->uring_lock); | |
def596e9 | 10970 | } else { |
f81440d3 PB |
10971 | const sigset_t __user *sig; |
10972 | struct __kernel_timespec __user *ts; | |
10973 | ||
3e813c90 DY |
10974 | ret2 = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); |
10975 | if (likely(!ret2)) { | |
10976 | min_complete = min(min_complete, | |
10977 | ctx->cq_entries); | |
10978 | ret2 = io_cqring_wait(ctx, min_complete, sig, | |
10979 | argsz, ts); | |
10980 | } | |
def596e9 | 10981 | } |
3e813c90 | 10982 | |
155bc950 | 10983 | if (!ret) { |
3e813c90 DY |
10984 | ret = ret2; |
10985 | ||
155bc950 DY |
10986 | /* |
10987 | * EBADR indicates that one or more CQE were dropped. | |
10988 | * Once the user has been informed we can clear the bit | |
10989 | * as they are obviously ok with those drops. | |
10990 | */ | |
10991 | if (unlikely(ret2 == -EBADR)) | |
10992 | clear_bit(IO_CHECK_CQ_DROPPED_BIT, | |
10993 | &ctx->check_cq); | |
10994 | } | |
2b188cc1 JA |
10995 | } |
10996 | ||
7c504e65 | 10997 | out: |
6805b32e | 10998 | percpu_ref_put(&ctx->refs); |
2b188cc1 | 10999 | out_fput: |
e7a6c00d JA |
11000 | if (!(flags & IORING_ENTER_REGISTERED_RING)) |
11001 | fdput(f); | |
3e813c90 | 11002 | return ret; |
2b188cc1 JA |
11003 | } |
11004 | ||
bebdb65e | 11005 | #ifdef CONFIG_PROC_FS |
c072481d | 11006 | static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id, |
61cf9370 | 11007 | const struct cred *cred) |
87ce955b | 11008 | { |
87ce955b JA |
11009 | struct user_namespace *uns = seq_user_ns(m); |
11010 | struct group_info *gi; | |
11011 | kernel_cap_t cap; | |
11012 | unsigned __capi; | |
11013 | int g; | |
11014 | ||
11015 | seq_printf(m, "%5d\n", id); | |
11016 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
11017 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
11018 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
11019 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
11020 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
11021 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
11022 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
11023 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
11024 | seq_puts(m, "\n\tGroups:\t"); | |
11025 | gi = cred->group_info; | |
11026 | for (g = 0; g < gi->ngroups; g++) { | |
11027 | seq_put_decimal_ull(m, g ? " " : "", | |
11028 | from_kgid_munged(uns, gi->gid[g])); | |
11029 | } | |
11030 | seq_puts(m, "\n\tCapEff:\t"); | |
11031 | cap = cred->cap_effective; | |
11032 | CAP_FOR_EACH_U32(__capi) | |
11033 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
11034 | seq_putc(m, '\n'); | |
11035 | return 0; | |
11036 | } | |
11037 | ||
c072481d PB |
11038 | static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, |
11039 | struct seq_file *m) | |
87ce955b | 11040 | { |
dbbe9c64 | 11041 | struct io_sq_data *sq = NULL; |
83f84356 HX |
11042 | struct io_overflow_cqe *ocqe; |
11043 | struct io_rings *r = ctx->rings; | |
11044 | unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1; | |
83f84356 HX |
11045 | unsigned int sq_head = READ_ONCE(r->sq.head); |
11046 | unsigned int sq_tail = READ_ONCE(r->sq.tail); | |
11047 | unsigned int cq_head = READ_ONCE(r->cq.head); | |
11048 | unsigned int cq_tail = READ_ONCE(r->cq.tail); | |
f75d1183 | 11049 | unsigned int sq_entries, cq_entries; |
fad8e0de | 11050 | bool has_lock; |
83f84356 HX |
11051 | unsigned int i; |
11052 | ||
11053 | /* | |
11054 | * we may get imprecise sqe and cqe info if uring is actively running | |
11055 | * since we get cached_sq_head and cached_cq_tail without uring_lock | |
11056 | * and sq_tail and cq_head are changed by userspace. But it's ok since | |
11057 | * we usually use these info when it is stuck. | |
11058 | */ | |
c0235652 | 11059 | seq_printf(m, "SqMask:\t0x%x\n", sq_mask); |
f75d1183 JA |
11060 | seq_printf(m, "SqHead:\t%u\n", sq_head); |
11061 | seq_printf(m, "SqTail:\t%u\n", sq_tail); | |
11062 | seq_printf(m, "CachedSqHead:\t%u\n", ctx->cached_sq_head); | |
11063 | seq_printf(m, "CqMask:\t0x%x\n", cq_mask); | |
11064 | seq_printf(m, "CqHead:\t%u\n", cq_head); | |
11065 | seq_printf(m, "CqTail:\t%u\n", cq_tail); | |
11066 | seq_printf(m, "CachedCqTail:\t%u\n", ctx->cached_cq_tail); | |
11067 | seq_printf(m, "SQEs:\t%u\n", sq_tail - ctx->cached_sq_head); | |
11068 | sq_entries = min(sq_tail - sq_head, ctx->sq_entries); | |
11069 | for (i = 0; i < sq_entries; i++) { | |
11070 | unsigned int entry = i + sq_head; | |
11071 | unsigned int sq_idx = READ_ONCE(ctx->sq_array[entry & sq_mask]); | |
a1957780 | 11072 | struct io_uring_sqe *sqe; |
f75d1183 JA |
11073 | |
11074 | if (sq_idx > sq_mask) | |
11075 | continue; | |
11076 | sqe = &ctx->sq_sqes[sq_idx]; | |
11077 | seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n", | |
11078 | sq_idx, sqe->opcode, sqe->fd, sqe->flags, | |
11079 | sqe->user_data); | |
83f84356 | 11080 | } |
f75d1183 JA |
11081 | seq_printf(m, "CQEs:\t%u\n", cq_tail - cq_head); |
11082 | cq_entries = min(cq_tail - cq_head, ctx->cq_entries); | |
11083 | for (i = 0; i < cq_entries; i++) { | |
11084 | unsigned int entry = i + cq_head; | |
11085 | struct io_uring_cqe *cqe = &r->cqes[entry & cq_mask]; | |
83f84356 HX |
11086 | |
11087 | seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n", | |
f75d1183 JA |
11088 | entry & cq_mask, cqe->user_data, cqe->res, |
11089 | cqe->flags); | |
83f84356 | 11090 | } |
87ce955b | 11091 | |
fad8e0de JA |
11092 | /* |
11093 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
11094 | * since fdinfo case grabs it in the opposite direction of normal use | |
11095 | * cases. If we fail to get the lock, we just don't iterate any | |
11096 | * structures that could be going away outside the io_uring mutex. | |
11097 | */ | |
11098 | has_lock = mutex_trylock(&ctx->uring_lock); | |
11099 | ||
5f3f26f9 | 11100 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 11101 | sq = ctx->sq_data; |
5f3f26f9 JA |
11102 | if (!sq->thread) |
11103 | sq = NULL; | |
11104 | } | |
dbbe9c64 JQ |
11105 | |
11106 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
11107 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 11108 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 11109 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
7b29f92d | 11110 | struct file *f = io_file_from_index(ctx, i); |
87ce955b | 11111 | |
87ce955b JA |
11112 | if (f) |
11113 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
11114 | else | |
11115 | seq_printf(m, "%5u: <none>\n", i); | |
11116 | } | |
11117 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 11118 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
41edf1a5 | 11119 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
4751f53d | 11120 | unsigned int len = buf->ubuf_end - buf->ubuf; |
87ce955b | 11121 | |
4751f53d | 11122 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
87ce955b | 11123 | } |
61cf9370 MWO |
11124 | if (has_lock && !xa_empty(&ctx->personalities)) { |
11125 | unsigned long index; | |
11126 | const struct cred *cred; | |
11127 | ||
87ce955b | 11128 | seq_printf(m, "Personalities:\n"); |
61cf9370 MWO |
11129 | xa_for_each(&ctx->personalities, index, cred) |
11130 | io_uring_show_cred(m, index, cred); | |
87ce955b | 11131 | } |
83f84356 HX |
11132 | if (has_lock) |
11133 | mutex_unlock(&ctx->uring_lock); | |
11134 | ||
11135 | seq_puts(m, "PollList:\n"); | |
79ebeaee | 11136 | spin_lock(&ctx->completion_lock); |
d7718a9d JA |
11137 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
11138 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
11139 | struct io_kiocb *req; | |
11140 | ||
11141 | hlist_for_each_entry(req, list, hash_node) | |
11142 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
7f62d40d | 11143 | task_work_pending(req->task)); |
d7718a9d | 11144 | } |
83f84356 HX |
11145 | |
11146 | seq_puts(m, "CqOverflowList:\n"); | |
11147 | list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) { | |
11148 | struct io_uring_cqe *cqe = &ocqe->cqe; | |
11149 | ||
11150 | seq_printf(m, " user_data=%llu, res=%d, flags=%x\n", | |
11151 | cqe->user_data, cqe->res, cqe->flags); | |
11152 | ||
11153 | } | |
11154 | ||
79ebeaee | 11155 | spin_unlock(&ctx->completion_lock); |
87ce955b JA |
11156 | } |
11157 | ||
c072481d | 11158 | static __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
87ce955b JA |
11159 | { |
11160 | struct io_ring_ctx *ctx = f->private_data; | |
11161 | ||
11162 | if (percpu_ref_tryget(&ctx->refs)) { | |
11163 | __io_uring_show_fdinfo(ctx, m); | |
11164 | percpu_ref_put(&ctx->refs); | |
11165 | } | |
11166 | } | |
bebdb65e | 11167 | #endif |
87ce955b | 11168 | |
2b188cc1 JA |
11169 | static const struct file_operations io_uring_fops = { |
11170 | .release = io_uring_release, | |
11171 | .mmap = io_uring_mmap, | |
6c5c240e RP |
11172 | #ifndef CONFIG_MMU |
11173 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
11174 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
11175 | #endif | |
2b188cc1 | 11176 | .poll = io_uring_poll, |
bebdb65e | 11177 | #ifdef CONFIG_PROC_FS |
87ce955b | 11178 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 11179 | #endif |
2b188cc1 JA |
11180 | }; |
11181 | ||
c072481d PB |
11182 | static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
11183 | struct io_uring_params *p) | |
2b188cc1 | 11184 | { |
75b28aff HV |
11185 | struct io_rings *rings; |
11186 | size_t size, sq_array_offset; | |
2b188cc1 | 11187 | |
bd740481 JA |
11188 | /* make sure these are sane, as we already accounted them */ |
11189 | ctx->sq_entries = p->sq_entries; | |
11190 | ctx->cq_entries = p->cq_entries; | |
11191 | ||
75b28aff HV |
11192 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
11193 | if (size == SIZE_MAX) | |
11194 | return -EOVERFLOW; | |
11195 | ||
11196 | rings = io_mem_alloc(size); | |
11197 | if (!rings) | |
2b188cc1 JA |
11198 | return -ENOMEM; |
11199 | ||
75b28aff HV |
11200 | ctx->rings = rings; |
11201 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
11202 | rings->sq_ring_mask = p->sq_entries - 1; | |
11203 | rings->cq_ring_mask = p->cq_entries - 1; | |
11204 | rings->sq_ring_entries = p->sq_entries; | |
11205 | rings->cq_ring_entries = p->cq_entries; | |
2b188cc1 JA |
11206 | |
11207 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
11208 | if (size == SIZE_MAX) { |
11209 | io_mem_free(ctx->rings); | |
11210 | ctx->rings = NULL; | |
2b188cc1 | 11211 | return -EOVERFLOW; |
eb065d30 | 11212 | } |
2b188cc1 JA |
11213 | |
11214 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
11215 | if (!ctx->sq_sqes) { |
11216 | io_mem_free(ctx->rings); | |
11217 | ctx->rings = NULL; | |
2b188cc1 | 11218 | return -ENOMEM; |
eb065d30 | 11219 | } |
2b188cc1 | 11220 | |
2b188cc1 JA |
11221 | return 0; |
11222 | } | |
11223 | ||
9faadcc8 PB |
11224 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
11225 | { | |
11226 | int ret, fd; | |
11227 | ||
11228 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
11229 | if (fd < 0) | |
11230 | return fd; | |
11231 | ||
eef51daa | 11232 | ret = io_uring_add_tctx_node(ctx); |
9faadcc8 PB |
11233 | if (ret) { |
11234 | put_unused_fd(fd); | |
11235 | return ret; | |
11236 | } | |
11237 | fd_install(fd, file); | |
11238 | return fd; | |
11239 | } | |
11240 | ||
2b188cc1 JA |
11241 | /* |
11242 | * Allocate an anonymous fd, this is what constitutes the application | |
11243 | * visible backing of an io_uring instance. The application mmaps this | |
11244 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
11245 | * we have to tie this fd to a socket for file garbage collection purposes. | |
11246 | */ | |
9faadcc8 | 11247 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
11248 | { |
11249 | struct file *file; | |
9faadcc8 | 11250 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
11251 | int ret; |
11252 | ||
2b188cc1 JA |
11253 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
11254 | &ctx->ring_sock); | |
11255 | if (ret) | |
9faadcc8 | 11256 | return ERR_PTR(ret); |
2b188cc1 JA |
11257 | #endif |
11258 | ||
91a9ab7c PM |
11259 | file = anon_inode_getfile_secure("[io_uring]", &io_uring_fops, ctx, |
11260 | O_RDWR | O_CLOEXEC, NULL); | |
2b188cc1 | 11261 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
11262 | if (IS_ERR(file)) { |
11263 | sock_release(ctx->ring_sock); | |
11264 | ctx->ring_sock = NULL; | |
11265 | } else { | |
11266 | ctx->ring_sock->file = file; | |
0f212204 | 11267 | } |
2b188cc1 | 11268 | #endif |
9faadcc8 | 11269 | return file; |
2b188cc1 JA |
11270 | } |
11271 | ||
c072481d PB |
11272 | static __cold int io_uring_create(unsigned entries, struct io_uring_params *p, |
11273 | struct io_uring_params __user *params) | |
2b188cc1 | 11274 | { |
2b188cc1 | 11275 | struct io_ring_ctx *ctx; |
9faadcc8 | 11276 | struct file *file; |
2b188cc1 JA |
11277 | int ret; |
11278 | ||
8110c1a6 | 11279 | if (!entries) |
2b188cc1 | 11280 | return -EINVAL; |
8110c1a6 JA |
11281 | if (entries > IORING_MAX_ENTRIES) { |
11282 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
11283 | return -EINVAL; | |
11284 | entries = IORING_MAX_ENTRIES; | |
11285 | } | |
2b188cc1 JA |
11286 | |
11287 | /* | |
11288 | * Use twice as many entries for the CQ ring. It's possible for the | |
11289 | * application to drive a higher depth than the size of the SQ ring, | |
11290 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
11291 | * some flexibility in overcommitting a bit. If the application has |
11292 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
11293 | * of CQ ring entries manually. | |
2b188cc1 JA |
11294 | */ |
11295 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
11296 | if (p->flags & IORING_SETUP_CQSIZE) { |
11297 | /* | |
11298 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
11299 | * to a power-of-two, if it isn't already. We do NOT impose | |
11300 | * any cq vs sq ring sizing. | |
11301 | */ | |
eb2667b3 | 11302 | if (!p->cq_entries) |
33a107f0 | 11303 | return -EINVAL; |
8110c1a6 JA |
11304 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
11305 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
11306 | return -EINVAL; | |
11307 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
11308 | } | |
eb2667b3 JQ |
11309 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
11310 | if (p->cq_entries < p->sq_entries) | |
11311 | return -EINVAL; | |
33a107f0 JA |
11312 | } else { |
11313 | p->cq_entries = 2 * p->sq_entries; | |
11314 | } | |
2b188cc1 | 11315 | |
2b188cc1 | 11316 | ctx = io_ring_ctx_alloc(p); |
62e398be | 11317 | if (!ctx) |
2b188cc1 | 11318 | return -ENOMEM; |
773697b6 PB |
11319 | |
11320 | /* | |
11321 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
11322 | * space applications don't need to do io completion events | |
11323 | * polling again, they can rely on io_sq_thread to do polling | |
11324 | * work, which can reduce cpu usage and uring_lock contention. | |
11325 | */ | |
11326 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
11327 | !(ctx->flags & IORING_SETUP_SQPOLL)) | |
11328 | ctx->syscall_iopoll = 1; | |
11329 | ||
2b188cc1 | 11330 | ctx->compat = in_compat_syscall(); |
62e398be JA |
11331 | if (!capable(CAP_IPC_LOCK)) |
11332 | ctx->user = get_uid(current_user()); | |
2aede0e4 | 11333 | |
9f010507 | 11334 | /* |
e1169f06 JA |
11335 | * For SQPOLL, we just need a wakeup, always. For !SQPOLL, if |
11336 | * COOP_TASKRUN is set, then IPIs are never needed by the app. | |
9f010507 | 11337 | */ |
e1169f06 JA |
11338 | ret = -EINVAL; |
11339 | if (ctx->flags & IORING_SETUP_SQPOLL) { | |
11340 | /* IPI related flags don't make sense with SQPOLL */ | |
ef060ea9 JA |
11341 | if (ctx->flags & (IORING_SETUP_COOP_TASKRUN | |
11342 | IORING_SETUP_TASKRUN_FLAG)) | |
e1169f06 | 11343 | goto err; |
9f010507 | 11344 | ctx->notify_method = TWA_SIGNAL_NO_IPI; |
e1169f06 JA |
11345 | } else if (ctx->flags & IORING_SETUP_COOP_TASKRUN) { |
11346 | ctx->notify_method = TWA_SIGNAL_NO_IPI; | |
11347 | } else { | |
ef060ea9 JA |
11348 | if (ctx->flags & IORING_SETUP_TASKRUN_FLAG) |
11349 | goto err; | |
9f010507 | 11350 | ctx->notify_method = TWA_SIGNAL; |
e1169f06 | 11351 | } |
9f010507 | 11352 | |
2aede0e4 JA |
11353 | /* |
11354 | * This is just grabbed for accounting purposes. When a process exits, | |
11355 | * the mm is exited and dropped before the files, hence we need to hang | |
11356 | * on to this mm purely for the purposes of being able to unaccount | |
11357 | * memory (locked/pinned vm). It's not used for anything else. | |
11358 | */ | |
6b7898eb | 11359 | mmgrab(current->mm); |
2aede0e4 | 11360 | ctx->mm_account = current->mm; |
6b7898eb | 11361 | |
2b188cc1 JA |
11362 | ret = io_allocate_scq_urings(ctx, p); |
11363 | if (ret) | |
11364 | goto err; | |
11365 | ||
7e84e1c7 | 11366 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
11367 | if (ret) |
11368 | goto err; | |
eae071c9 | 11369 | /* always set a rsrc node */ |
47b228ce PB |
11370 | ret = io_rsrc_node_switch_start(ctx); |
11371 | if (ret) | |
11372 | goto err; | |
eae071c9 | 11373 | io_rsrc_node_switch(ctx, NULL); |
2b188cc1 | 11374 | |
2b188cc1 | 11375 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
11376 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
11377 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
11378 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
11379 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
11380 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
11381 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
11382 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
11383 | |
11384 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
11385 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
11386 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
11387 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
11388 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
11389 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
11390 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 11391 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 11392 | |
7f13657d XW |
11393 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
11394 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 11395 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 11396 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9690557e | 11397 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
c4212f3e JA |
11398 | IORING_FEAT_RSRC_TAGS | IORING_FEAT_CQE_SKIP | |
11399 | IORING_FEAT_LINKED_FILE; | |
7f13657d XW |
11400 | |
11401 | if (copy_to_user(params, p, sizeof(*p))) { | |
11402 | ret = -EFAULT; | |
11403 | goto err; | |
11404 | } | |
d1719f70 | 11405 | |
9faadcc8 PB |
11406 | file = io_uring_get_file(ctx); |
11407 | if (IS_ERR(file)) { | |
11408 | ret = PTR_ERR(file); | |
11409 | goto err; | |
11410 | } | |
11411 | ||
044c1ab3 JA |
11412 | /* |
11413 | * Install ring fd as the very last thing, so we don't risk someone | |
11414 | * having closed it before we finish setup | |
11415 | */ | |
9faadcc8 PB |
11416 | ret = io_uring_install_fd(ctx, file); |
11417 | if (ret < 0) { | |
11418 | /* fput will clean it up */ | |
11419 | fput(file); | |
11420 | return ret; | |
11421 | } | |
044c1ab3 | 11422 | |
c826bd7a | 11423 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
11424 | return ret; |
11425 | err: | |
11426 | io_ring_ctx_wait_and_kill(ctx); | |
11427 | return ret; | |
11428 | } | |
11429 | ||
11430 | /* | |
11431 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
11432 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
11433 | * params structure passed in. | |
11434 | */ | |
11435 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
11436 | { | |
11437 | struct io_uring_params p; | |
2b188cc1 JA |
11438 | int i; |
11439 | ||
11440 | if (copy_from_user(&p, params, sizeof(p))) | |
11441 | return -EFAULT; | |
11442 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
11443 | if (p.resv[i]) | |
11444 | return -EINVAL; | |
11445 | } | |
11446 | ||
6c271ce2 | 11447 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 11448 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 | 11449 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
e1169f06 | 11450 | IORING_SETUP_R_DISABLED | IORING_SETUP_SUBMIT_ALL | |
ef060ea9 | 11451 | IORING_SETUP_COOP_TASKRUN | IORING_SETUP_TASKRUN_FLAG)) |
2b188cc1 JA |
11452 | return -EINVAL; |
11453 | ||
ef060ea9 | 11454 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
11455 | } |
11456 | ||
11457 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
11458 | struct io_uring_params __user *, params) | |
11459 | { | |
11460 | return io_uring_setup(entries, params); | |
11461 | } | |
11462 | ||
c072481d PB |
11463 | static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg, |
11464 | unsigned nr_args) | |
66f4af93 JA |
11465 | { |
11466 | struct io_uring_probe *p; | |
11467 | size_t size; | |
11468 | int i, ret; | |
11469 | ||
11470 | size = struct_size(p, ops, nr_args); | |
11471 | if (size == SIZE_MAX) | |
11472 | return -EOVERFLOW; | |
11473 | p = kzalloc(size, GFP_KERNEL); | |
11474 | if (!p) | |
11475 | return -ENOMEM; | |
11476 | ||
11477 | ret = -EFAULT; | |
11478 | if (copy_from_user(p, arg, size)) | |
11479 | goto out; | |
11480 | ret = -EINVAL; | |
11481 | if (memchr_inv(p, 0, size)) | |
11482 | goto out; | |
11483 | ||
11484 | p->last_op = IORING_OP_LAST - 1; | |
11485 | if (nr_args > IORING_OP_LAST) | |
11486 | nr_args = IORING_OP_LAST; | |
11487 | ||
11488 | for (i = 0; i < nr_args; i++) { | |
11489 | p->ops[i].op = i; | |
11490 | if (!io_op_defs[i].not_supported) | |
11491 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
11492 | } | |
11493 | p->ops_len = i; | |
11494 | ||
11495 | ret = 0; | |
11496 | if (copy_to_user(arg, p, size)) | |
11497 | ret = -EFAULT; | |
11498 | out: | |
11499 | kfree(p); | |
11500 | return ret; | |
11501 | } | |
11502 | ||
071698e1 JA |
11503 | static int io_register_personality(struct io_ring_ctx *ctx) |
11504 | { | |
4379bf8b | 11505 | const struct cred *creds; |
61cf9370 | 11506 | u32 id; |
1e6fa521 | 11507 | int ret; |
071698e1 | 11508 | |
4379bf8b | 11509 | creds = get_current_cred(); |
1e6fa521 | 11510 | |
61cf9370 MWO |
11511 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
11512 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); | |
a30f895a JA |
11513 | if (ret < 0) { |
11514 | put_cred(creds); | |
11515 | return ret; | |
11516 | } | |
11517 | return id; | |
071698e1 JA |
11518 | } |
11519 | ||
c072481d PB |
11520 | static __cold int io_register_restrictions(struct io_ring_ctx *ctx, |
11521 | void __user *arg, unsigned int nr_args) | |
21b55dbc SG |
11522 | { |
11523 | struct io_uring_restriction *res; | |
11524 | size_t size; | |
11525 | int i, ret; | |
11526 | ||
7e84e1c7 SG |
11527 | /* Restrictions allowed only if rings started disabled */ |
11528 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
11529 | return -EBADFD; | |
11530 | ||
21b55dbc | 11531 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 11532 | if (ctx->restrictions.registered) |
21b55dbc SG |
11533 | return -EBUSY; |
11534 | ||
11535 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
11536 | return -EINVAL; | |
11537 | ||
11538 | size = array_size(nr_args, sizeof(*res)); | |
11539 | if (size == SIZE_MAX) | |
11540 | return -EOVERFLOW; | |
11541 | ||
11542 | res = memdup_user(arg, size); | |
11543 | if (IS_ERR(res)) | |
11544 | return PTR_ERR(res); | |
11545 | ||
11546 | ret = 0; | |
11547 | ||
11548 | for (i = 0; i < nr_args; i++) { | |
11549 | switch (res[i].opcode) { | |
11550 | case IORING_RESTRICTION_REGISTER_OP: | |
11551 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
11552 | ret = -EINVAL; | |
11553 | goto out; | |
11554 | } | |
11555 | ||
11556 | __set_bit(res[i].register_op, | |
11557 | ctx->restrictions.register_op); | |
11558 | break; | |
11559 | case IORING_RESTRICTION_SQE_OP: | |
11560 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
11561 | ret = -EINVAL; | |
11562 | goto out; | |
11563 | } | |
11564 | ||
11565 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
11566 | break; | |
11567 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
11568 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
11569 | break; | |
11570 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
11571 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
11572 | break; | |
11573 | default: | |
11574 | ret = -EINVAL; | |
11575 | goto out; | |
11576 | } | |
11577 | } | |
11578 | ||
11579 | out: | |
11580 | /* Reset all restrictions if an error happened */ | |
11581 | if (ret != 0) | |
11582 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
11583 | else | |
7e84e1c7 | 11584 | ctx->restrictions.registered = true; |
21b55dbc SG |
11585 | |
11586 | kfree(res); | |
11587 | return ret; | |
11588 | } | |
11589 | ||
7e84e1c7 SG |
11590 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
11591 | { | |
11592 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
11593 | return -EBADFD; | |
11594 | ||
11595 | if (ctx->restrictions.registered) | |
11596 | ctx->restricted = 1; | |
11597 | ||
0298ef96 PB |
11598 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
11599 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) | |
11600 | wake_up(&ctx->sq_data->wait); | |
7e84e1c7 SG |
11601 | return 0; |
11602 | } | |
11603 | ||
fdecb662 | 11604 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 11605 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 PB |
11606 | unsigned nr_args) |
11607 | { | |
11608 | __u32 tmp; | |
11609 | int err; | |
11610 | ||
11611 | if (check_add_overflow(up->offset, nr_args, &tmp)) | |
11612 | return -EOVERFLOW; | |
11613 | err = io_rsrc_node_switch_start(ctx); | |
11614 | if (err) | |
11615 | return err; | |
11616 | ||
fdecb662 PB |
11617 | switch (type) { |
11618 | case IORING_RSRC_FILE: | |
98f0b3b4 | 11619 | return __io_sqe_files_update(ctx, up, nr_args); |
634d00df PB |
11620 | case IORING_RSRC_BUFFER: |
11621 | return __io_sqe_buffers_update(ctx, up, nr_args); | |
98f0b3b4 PB |
11622 | } |
11623 | return -EINVAL; | |
11624 | } | |
11625 | ||
c3bdad02 PB |
11626 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
11627 | unsigned nr_args) | |
98f0b3b4 | 11628 | { |
c3bdad02 | 11629 | struct io_uring_rsrc_update2 up; |
98f0b3b4 PB |
11630 | |
11631 | if (!nr_args) | |
11632 | return -EINVAL; | |
c3bdad02 PB |
11633 | memset(&up, 0, sizeof(up)); |
11634 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) | |
11635 | return -EFAULT; | |
d8a3ba9c | 11636 | if (up.resv || up.resv2) |
565c5e61 | 11637 | return -EINVAL; |
c3bdad02 PB |
11638 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); |
11639 | } | |
11640 | ||
11641 | static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
992da01a | 11642 | unsigned size, unsigned type) |
c3bdad02 PB |
11643 | { |
11644 | struct io_uring_rsrc_update2 up; | |
11645 | ||
11646 | if (size != sizeof(up)) | |
11647 | return -EINVAL; | |
98f0b3b4 PB |
11648 | if (copy_from_user(&up, arg, sizeof(up))) |
11649 | return -EFAULT; | |
d8a3ba9c | 11650 | if (!up.nr || up.resv || up.resv2) |
98f0b3b4 | 11651 | return -EINVAL; |
992da01a | 11652 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
98f0b3b4 PB |
11653 | } |
11654 | ||
c072481d | 11655 | static __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
992da01a | 11656 | unsigned int size, unsigned int type) |
792e3582 PB |
11657 | { |
11658 | struct io_uring_rsrc_register rr; | |
11659 | ||
11660 | /* keep it extendible */ | |
11661 | if (size != sizeof(rr)) | |
11662 | return -EINVAL; | |
11663 | ||
11664 | memset(&rr, 0, sizeof(rr)); | |
11665 | if (copy_from_user(&rr, arg, size)) | |
11666 | return -EFAULT; | |
992da01a | 11667 | if (!rr.nr || rr.resv || rr.resv2) |
792e3582 PB |
11668 | return -EINVAL; |
11669 | ||
992da01a | 11670 | switch (type) { |
792e3582 PB |
11671 | case IORING_RSRC_FILE: |
11672 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), | |
11673 | rr.nr, u64_to_user_ptr(rr.tags)); | |
634d00df PB |
11674 | case IORING_RSRC_BUFFER: |
11675 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), | |
11676 | rr.nr, u64_to_user_ptr(rr.tags)); | |
792e3582 PB |
11677 | } |
11678 | return -EINVAL; | |
11679 | } | |
11680 | ||
c072481d PB |
11681 | static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx, |
11682 | void __user *arg, unsigned len) | |
fe76421d JA |
11683 | { |
11684 | struct io_uring_task *tctx = current->io_uring; | |
11685 | cpumask_var_t new_mask; | |
11686 | int ret; | |
11687 | ||
11688 | if (!tctx || !tctx->io_wq) | |
11689 | return -EINVAL; | |
11690 | ||
11691 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | |
11692 | return -ENOMEM; | |
11693 | ||
11694 | cpumask_clear(new_mask); | |
11695 | if (len > cpumask_size()) | |
11696 | len = cpumask_size(); | |
11697 | ||
0f5e4b83 ES |
11698 | if (in_compat_syscall()) { |
11699 | ret = compat_get_bitmap(cpumask_bits(new_mask), | |
11700 | (const compat_ulong_t __user *)arg, | |
11701 | len * 8 /* CHAR_BIT */); | |
11702 | } else { | |
11703 | ret = copy_from_user(new_mask, arg, len); | |
11704 | } | |
11705 | ||
11706 | if (ret) { | |
fe76421d JA |
11707 | free_cpumask_var(new_mask); |
11708 | return -EFAULT; | |
11709 | } | |
11710 | ||
11711 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); | |
11712 | free_cpumask_var(new_mask); | |
11713 | return ret; | |
11714 | } | |
11715 | ||
c072481d | 11716 | static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx) |
fe76421d JA |
11717 | { |
11718 | struct io_uring_task *tctx = current->io_uring; | |
11719 | ||
11720 | if (!tctx || !tctx->io_wq) | |
11721 | return -EINVAL; | |
11722 | ||
11723 | return io_wq_cpu_affinity(tctx->io_wq, NULL); | |
11724 | } | |
11725 | ||
c072481d PB |
11726 | static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, |
11727 | void __user *arg) | |
b22fa62a | 11728 | __must_hold(&ctx->uring_lock) |
2e480058 | 11729 | { |
b22fa62a | 11730 | struct io_tctx_node *node; |
fa84693b JA |
11731 | struct io_uring_task *tctx = NULL; |
11732 | struct io_sq_data *sqd = NULL; | |
2e480058 JA |
11733 | __u32 new_count[2]; |
11734 | int i, ret; | |
11735 | ||
2e480058 JA |
11736 | if (copy_from_user(new_count, arg, sizeof(new_count))) |
11737 | return -EFAULT; | |
11738 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
11739 | if (new_count[i] > INT_MAX) | |
11740 | return -EINVAL; | |
11741 | ||
fa84693b JA |
11742 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
11743 | sqd = ctx->sq_data; | |
11744 | if (sqd) { | |
009ad9f0 JA |
11745 | /* |
11746 | * Observe the correct sqd->lock -> ctx->uring_lock | |
11747 | * ordering. Fine to drop uring_lock here, we hold | |
11748 | * a ref to the ctx. | |
11749 | */ | |
41d3a6bd | 11750 | refcount_inc(&sqd->refs); |
009ad9f0 | 11751 | mutex_unlock(&ctx->uring_lock); |
fa84693b | 11752 | mutex_lock(&sqd->lock); |
009ad9f0 | 11753 | mutex_lock(&ctx->uring_lock); |
41d3a6bd JA |
11754 | if (sqd->thread) |
11755 | tctx = sqd->thread->io_uring; | |
fa84693b JA |
11756 | } |
11757 | } else { | |
11758 | tctx = current->io_uring; | |
11759 | } | |
11760 | ||
e139a1ec | 11761 | BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits)); |
fa84693b | 11762 | |
bad119b9 PB |
11763 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
11764 | if (new_count[i]) | |
11765 | ctx->iowq_limits[i] = new_count[i]; | |
e139a1ec PB |
11766 | ctx->iowq_limits_set = true; |
11767 | ||
e139a1ec PB |
11768 | if (tctx && tctx->io_wq) { |
11769 | ret = io_wq_max_workers(tctx->io_wq, new_count); | |
11770 | if (ret) | |
11771 | goto err; | |
11772 | } else { | |
11773 | memset(new_count, 0, sizeof(new_count)); | |
11774 | } | |
fa84693b | 11775 | |
41d3a6bd | 11776 | if (sqd) { |
fa84693b | 11777 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
11778 | io_put_sq_data(sqd); |
11779 | } | |
2e480058 JA |
11780 | |
11781 | if (copy_to_user(arg, new_count, sizeof(new_count))) | |
11782 | return -EFAULT; | |
11783 | ||
b22fa62a PB |
11784 | /* that's it for SQPOLL, only the SQPOLL task creates requests */ |
11785 | if (sqd) | |
11786 | return 0; | |
11787 | ||
11788 | /* now propagate the restriction to all registered users */ | |
11789 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
11790 | struct io_uring_task *tctx = node->task->io_uring; | |
11791 | ||
11792 | if (WARN_ON_ONCE(!tctx->io_wq)) | |
11793 | continue; | |
11794 | ||
11795 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
11796 | new_count[i] = ctx->iowq_limits[i]; | |
11797 | /* ignore errors, it always returns zero anyway */ | |
11798 | (void)io_wq_max_workers(tctx->io_wq, new_count); | |
11799 | } | |
2e480058 | 11800 | return 0; |
fa84693b | 11801 | err: |
41d3a6bd | 11802 | if (sqd) { |
fa84693b | 11803 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
11804 | io_put_sq_data(sqd); |
11805 | } | |
fa84693b | 11806 | return ret; |
2e480058 JA |
11807 | } |
11808 | ||
edafccee JA |
11809 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
11810 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
11811 | __releases(ctx->uring_lock) |
11812 | __acquires(ctx->uring_lock) | |
edafccee JA |
11813 | { |
11814 | int ret; | |
11815 | ||
35fa71a0 JA |
11816 | /* |
11817 | * We're inside the ring mutex, if the ref is already dying, then | |
11818 | * someone else killed the ctx or is already going through | |
11819 | * io_uring_register(). | |
11820 | */ | |
11821 | if (percpu_ref_is_dying(&ctx->refs)) | |
11822 | return -ENXIO; | |
11823 | ||
75c4021a PB |
11824 | if (ctx->restricted) { |
11825 | if (opcode >= IORING_REGISTER_LAST) | |
11826 | return -EINVAL; | |
11827 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); | |
11828 | if (!test_bit(opcode, ctx->restrictions.register_op)) | |
11829 | return -EACCES; | |
11830 | } | |
11831 | ||
edafccee JA |
11832 | switch (opcode) { |
11833 | case IORING_REGISTER_BUFFERS: | |
634d00df | 11834 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
edafccee JA |
11835 | break; |
11836 | case IORING_UNREGISTER_BUFFERS: | |
11837 | ret = -EINVAL; | |
11838 | if (arg || nr_args) | |
11839 | break; | |
0a96bbe4 | 11840 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 11841 | break; |
6b06314c | 11842 | case IORING_REGISTER_FILES: |
792e3582 | 11843 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
6b06314c JA |
11844 | break; |
11845 | case IORING_UNREGISTER_FILES: | |
11846 | ret = -EINVAL; | |
11847 | if (arg || nr_args) | |
11848 | break; | |
11849 | ret = io_sqe_files_unregister(ctx); | |
11850 | break; | |
c3a31e60 | 11851 | case IORING_REGISTER_FILES_UPDATE: |
c3bdad02 | 11852 | ret = io_register_files_update(ctx, arg, nr_args); |
c3a31e60 | 11853 | break; |
9b402849 JA |
11854 | case IORING_REGISTER_EVENTFD: |
11855 | ret = -EINVAL; | |
11856 | if (nr_args != 1) | |
11857 | break; | |
c75312dd UA |
11858 | ret = io_eventfd_register(ctx, arg, 0); |
11859 | break; | |
11860 | case IORING_REGISTER_EVENTFD_ASYNC: | |
11861 | ret = -EINVAL; | |
11862 | if (nr_args != 1) | |
f2842ab5 | 11863 | break; |
c75312dd | 11864 | ret = io_eventfd_register(ctx, arg, 1); |
9b402849 JA |
11865 | break; |
11866 | case IORING_UNREGISTER_EVENTFD: | |
11867 | ret = -EINVAL; | |
11868 | if (arg || nr_args) | |
11869 | break; | |
11870 | ret = io_eventfd_unregister(ctx); | |
11871 | break; | |
66f4af93 JA |
11872 | case IORING_REGISTER_PROBE: |
11873 | ret = -EINVAL; | |
11874 | if (!arg || nr_args > 256) | |
11875 | break; | |
11876 | ret = io_probe(ctx, arg, nr_args); | |
11877 | break; | |
071698e1 JA |
11878 | case IORING_REGISTER_PERSONALITY: |
11879 | ret = -EINVAL; | |
11880 | if (arg || nr_args) | |
11881 | break; | |
11882 | ret = io_register_personality(ctx); | |
11883 | break; | |
11884 | case IORING_UNREGISTER_PERSONALITY: | |
11885 | ret = -EINVAL; | |
11886 | if (arg) | |
11887 | break; | |
11888 | ret = io_unregister_personality(ctx, nr_args); | |
11889 | break; | |
7e84e1c7 SG |
11890 | case IORING_REGISTER_ENABLE_RINGS: |
11891 | ret = -EINVAL; | |
11892 | if (arg || nr_args) | |
11893 | break; | |
11894 | ret = io_register_enable_rings(ctx); | |
11895 | break; | |
21b55dbc SG |
11896 | case IORING_REGISTER_RESTRICTIONS: |
11897 | ret = io_register_restrictions(ctx, arg, nr_args); | |
11898 | break; | |
992da01a PB |
11899 | case IORING_REGISTER_FILES2: |
11900 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); | |
11901 | break; | |
11902 | case IORING_REGISTER_FILES_UPDATE2: | |
11903 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
11904 | IORING_RSRC_FILE); | |
11905 | break; | |
11906 | case IORING_REGISTER_BUFFERS2: | |
11907 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); | |
792e3582 | 11908 | break; |
992da01a PB |
11909 | case IORING_REGISTER_BUFFERS_UPDATE: |
11910 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
11911 | IORING_RSRC_BUFFER); | |
c3bdad02 | 11912 | break; |
fe76421d JA |
11913 | case IORING_REGISTER_IOWQ_AFF: |
11914 | ret = -EINVAL; | |
11915 | if (!arg || !nr_args) | |
11916 | break; | |
11917 | ret = io_register_iowq_aff(ctx, arg, nr_args); | |
11918 | break; | |
11919 | case IORING_UNREGISTER_IOWQ_AFF: | |
11920 | ret = -EINVAL; | |
11921 | if (arg || nr_args) | |
11922 | break; | |
11923 | ret = io_unregister_iowq_aff(ctx); | |
11924 | break; | |
2e480058 JA |
11925 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
11926 | ret = -EINVAL; | |
11927 | if (!arg || nr_args != 2) | |
11928 | break; | |
11929 | ret = io_register_iowq_max_workers(ctx, arg); | |
11930 | break; | |
e7a6c00d JA |
11931 | case IORING_REGISTER_RING_FDS: |
11932 | ret = io_ringfd_register(ctx, arg, nr_args); | |
11933 | break; | |
11934 | case IORING_UNREGISTER_RING_FDS: | |
11935 | ret = io_ringfd_unregister(ctx, arg, nr_args); | |
11936 | break; | |
edafccee JA |
11937 | default: |
11938 | ret = -EINVAL; | |
11939 | break; | |
11940 | } | |
11941 | ||
edafccee JA |
11942 | return ret; |
11943 | } | |
11944 | ||
11945 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
11946 | void __user *, arg, unsigned int, nr_args) | |
11947 | { | |
11948 | struct io_ring_ctx *ctx; | |
11949 | long ret = -EBADF; | |
11950 | struct fd f; | |
11951 | ||
11952 | f = fdget(fd); | |
11953 | if (!f.file) | |
11954 | return -EBADF; | |
11955 | ||
11956 | ret = -EOPNOTSUPP; | |
11957 | if (f.file->f_op != &io_uring_fops) | |
11958 | goto out_fput; | |
11959 | ||
11960 | ctx = f.file->private_data; | |
11961 | ||
b6c23dd5 PB |
11962 | io_run_task_work(); |
11963 | ||
edafccee JA |
11964 | mutex_lock(&ctx->uring_lock); |
11965 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
11966 | mutex_unlock(&ctx->uring_lock); | |
2757be22 | 11967 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, ret); |
edafccee JA |
11968 | out_fput: |
11969 | fdput(f); | |
11970 | return ret; | |
11971 | } | |
11972 | ||
2b188cc1 JA |
11973 | static int __init io_uring_init(void) |
11974 | { | |
d7f62e82 SM |
11975 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
11976 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
11977 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
11978 | } while (0) | |
11979 | ||
11980 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
11981 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
11982 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
11983 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
11984 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
11985 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
11986 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
11987 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
11988 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
11989 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 11990 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
11991 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
11992 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
11993 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
11994 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
11995 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
11996 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
11997 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
11998 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
11999 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
12000 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
12001 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
12002 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
12003 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
12004 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
12005 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 12006 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
12007 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
12008 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
16340eab | 12009 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
d7f62e82 | 12010 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
7d67af2c | 12011 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
b9445598 | 12012 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
d7f62e82 | 12013 | |
b0d658ec PB |
12014 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
12015 | sizeof(struct io_uring_rsrc_update)); | |
12016 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > | |
12017 | sizeof(struct io_uring_rsrc_update2)); | |
90499ad0 PB |
12018 | |
12019 | /* ->buf_index is u16 */ | |
12020 | BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); | |
12021 | ||
b0d658ec PB |
12022 | /* should fit into one byte */ |
12023 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); | |
68fe256a PB |
12024 | BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8)); |
12025 | BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS); | |
b0d658ec | 12026 | |
d3656344 | 12027 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
32c2d33e | 12028 | BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int)); |
16340eab | 12029 | |
3a4b89a2 JA |
12030 | BUILD_BUG_ON(sizeof(atomic_t) != sizeof(u32)); |
12031 | ||
91f245d5 JA |
12032 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
12033 | SLAB_ACCOUNT); | |
2b188cc1 JA |
12034 | return 0; |
12035 | }; | |
12036 | __initcall(io_uring_init); |