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> |
ef98eb04 | 81 | #include <linux/tracehook.h> |
5bd2182d | 82 | #include <linux/audit.h> |
cdc1404a | 83 | #include <linux/security.h> |
2b188cc1 | 84 | |
c826bd7a DD |
85 | #define CREATE_TRACE_POINTS |
86 | #include <trace/events/io_uring.h> | |
87 | ||
2b188cc1 JA |
88 | #include <uapi/linux/io_uring.h> |
89 | ||
90 | #include "internal.h" | |
561fb04a | 91 | #include "io-wq.h" |
2b188cc1 | 92 | |
5277deaa | 93 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 94 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
4ce8ad95 | 95 | #define IORING_SQPOLL_CAP_ENTRIES_VALUE 8 |
65e19f54 | 96 | |
187f08c1 | 97 | /* only define max */ |
042b0d85 | 98 | #define IORING_MAX_FIXED_FILES (1U << 15) |
21b55dbc SG |
99 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
100 | IORING_REGISTER_LAST + IORING_OP_LAST) | |
2b188cc1 | 101 | |
187f08c1 | 102 | #define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3) |
2d091d62 PB |
103 | #define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT) |
104 | #define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1) | |
105 | ||
489809e2 PB |
106 | #define IORING_MAX_REG_BUFFERS (1U << 14) |
107 | ||
68fe256a PB |
108 | #define SQE_COMMON_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_LINK | \ |
109 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) | |
110 | ||
5562a8d7 PB |
111 | #define SQE_VALID_FLAGS (SQE_COMMON_FLAGS | IOSQE_BUFFER_SELECT | \ |
112 | IOSQE_IO_DRAIN | IOSQE_CQE_SKIP_SUCCESS) | |
68fe256a | 113 | |
c854357b | 114 | #define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \ |
d886e185 PB |
115 | REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS | \ |
116 | REQ_F_ASYNC_DATA) | |
b16fed66 | 117 | |
09899b19 PB |
118 | #define IO_TCTX_REFS_CACHE_NR (1U << 10) |
119 | ||
2b188cc1 JA |
120 | struct io_uring { |
121 | u32 head ____cacheline_aligned_in_smp; | |
122 | u32 tail ____cacheline_aligned_in_smp; | |
123 | }; | |
124 | ||
1e84b97b | 125 | /* |
75b28aff HV |
126 | * This data is shared with the application through the mmap at offsets |
127 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
128 | * |
129 | * The offsets to the member fields are published through struct | |
130 | * io_sqring_offsets when calling io_uring_setup. | |
131 | */ | |
75b28aff | 132 | struct io_rings { |
1e84b97b SB |
133 | /* |
134 | * Head and tail offsets into the ring; the offsets need to be | |
135 | * masked to get valid indices. | |
136 | * | |
75b28aff HV |
137 | * The kernel controls head of the sq ring and the tail of the cq ring, |
138 | * and the application controls tail of the sq ring and the head of the | |
139 | * cq ring. | |
1e84b97b | 140 | */ |
75b28aff | 141 | struct io_uring sq, cq; |
1e84b97b | 142 | /* |
75b28aff | 143 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
144 | * ring_entries - 1) |
145 | */ | |
75b28aff HV |
146 | u32 sq_ring_mask, cq_ring_mask; |
147 | /* Ring sizes (constant, power of 2) */ | |
148 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
149 | /* |
150 | * Number of invalid entries dropped by the kernel due to | |
151 | * invalid index stored in array | |
152 | * | |
153 | * Written by the kernel, shouldn't be modified by the | |
154 | * application (i.e. get number of "new events" by comparing to | |
155 | * cached value). | |
156 | * | |
157 | * After a new SQ head value was read by the application this | |
158 | * counter includes all submissions that were dropped reaching | |
159 | * the new SQ head (and possibly more). | |
160 | */ | |
75b28aff | 161 | u32 sq_dropped; |
1e84b97b | 162 | /* |
0d9b5b3a | 163 | * Runtime SQ flags |
1e84b97b SB |
164 | * |
165 | * Written by the kernel, shouldn't be modified by the | |
166 | * application. | |
167 | * | |
168 | * The application needs a full memory barrier before checking | |
169 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
170 | */ | |
75b28aff | 171 | u32 sq_flags; |
0d9b5b3a SG |
172 | /* |
173 | * Runtime CQ flags | |
174 | * | |
175 | * Written by the application, shouldn't be modified by the | |
176 | * kernel. | |
177 | */ | |
fe7e3257 | 178 | u32 cq_flags; |
1e84b97b SB |
179 | /* |
180 | * Number of completion events lost because the queue was full; | |
181 | * this should be avoided by the application by making sure | |
0b4295b5 | 182 | * there are not more requests pending than there is space in |
1e84b97b SB |
183 | * the completion queue. |
184 | * | |
185 | * Written by the kernel, shouldn't be modified by the | |
186 | * application (i.e. get number of "new events" by comparing to | |
187 | * cached value). | |
188 | * | |
189 | * As completion events come in out of order this counter is not | |
190 | * ordered with any other data. | |
191 | */ | |
75b28aff | 192 | u32 cq_overflow; |
1e84b97b SB |
193 | /* |
194 | * Ring buffer of completion events. | |
195 | * | |
196 | * The kernel writes completion events fresh every time they are | |
197 | * produced, so the application is allowed to modify pending | |
198 | * entries. | |
199 | */ | |
75b28aff | 200 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
201 | }; |
202 | ||
45d189c6 | 203 | enum io_uring_cmd_flags { |
51aac424 | 204 | IO_URING_F_COMPLETE_DEFER = 1, |
3b44b371 | 205 | IO_URING_F_UNLOCKED = 2, |
51aac424 PB |
206 | /* int's last bit, sign checks are usually faster than a bit test */ |
207 | IO_URING_F_NONBLOCK = INT_MIN, | |
45d189c6 PB |
208 | }; |
209 | ||
edafccee JA |
210 | struct io_mapped_ubuf { |
211 | u64 ubuf; | |
4751f53d | 212 | u64 ubuf_end; |
edafccee | 213 | unsigned int nr_bvecs; |
de293938 | 214 | unsigned long acct_pages; |
41edf1a5 | 215 | struct bio_vec bvec[]; |
edafccee JA |
216 | }; |
217 | ||
50238531 BM |
218 | struct io_ring_ctx; |
219 | ||
6c2450ae PB |
220 | struct io_overflow_cqe { |
221 | struct io_uring_cqe cqe; | |
222 | struct list_head list; | |
223 | }; | |
224 | ||
a04b0ac0 PB |
225 | struct io_fixed_file { |
226 | /* file * with additional FFS_* flags */ | |
227 | unsigned long file_ptr; | |
228 | }; | |
229 | ||
269bbe5f BM |
230 | struct io_rsrc_put { |
231 | struct list_head list; | |
b60c8dce | 232 | u64 tag; |
50238531 BM |
233 | union { |
234 | void *rsrc; | |
235 | struct file *file; | |
bd54b6fe | 236 | struct io_mapped_ubuf *buf; |
50238531 | 237 | }; |
269bbe5f BM |
238 | }; |
239 | ||
aeca241b | 240 | struct io_file_table { |
042b0d85 | 241 | struct io_fixed_file *files; |
31b51510 JA |
242 | }; |
243 | ||
b895c9a6 | 244 | struct io_rsrc_node { |
05589553 XW |
245 | struct percpu_ref refs; |
246 | struct list_head node; | |
269bbe5f | 247 | struct list_head rsrc_list; |
b895c9a6 | 248 | struct io_rsrc_data *rsrc_data; |
4a38aed2 | 249 | struct llist_node llist; |
e297822b | 250 | bool done; |
05589553 XW |
251 | }; |
252 | ||
40ae0ff7 PB |
253 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
254 | ||
b895c9a6 | 255 | struct io_rsrc_data { |
05f3fb3c JA |
256 | struct io_ring_ctx *ctx; |
257 | ||
2d091d62 PB |
258 | u64 **tags; |
259 | unsigned int nr; | |
40ae0ff7 | 260 | rsrc_put_fn *do_put; |
3e942498 | 261 | atomic_t refs; |
05f3fb3c | 262 | struct completion done; |
8bad28d8 | 263 | bool quiesce; |
05f3fb3c JA |
264 | }; |
265 | ||
5a2e745d JA |
266 | struct io_buffer { |
267 | struct list_head list; | |
268 | __u64 addr; | |
d1f82808 | 269 | __u32 len; |
5a2e745d JA |
270 | __u16 bid; |
271 | }; | |
272 | ||
21b55dbc SG |
273 | struct io_restriction { |
274 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
275 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
276 | u8 sqe_flags_allowed; | |
277 | u8 sqe_flags_required; | |
7e84e1c7 | 278 | bool registered; |
21b55dbc SG |
279 | }; |
280 | ||
37d1e2e3 JA |
281 | enum { |
282 | IO_SQ_THREAD_SHOULD_STOP = 0, | |
283 | IO_SQ_THREAD_SHOULD_PARK, | |
284 | }; | |
285 | ||
534ca6d6 JA |
286 | struct io_sq_data { |
287 | refcount_t refs; | |
9e138a48 | 288 | atomic_t park_pending; |
09a6f4ef | 289 | struct mutex lock; |
69fb2131 JA |
290 | |
291 | /* ctx's that are using this sqd */ | |
292 | struct list_head ctx_list; | |
69fb2131 | 293 | |
534ca6d6 JA |
294 | struct task_struct *thread; |
295 | struct wait_queue_head wait; | |
08369246 XW |
296 | |
297 | unsigned sq_thread_idle; | |
37d1e2e3 JA |
298 | int sq_cpu; |
299 | pid_t task_pid; | |
5c2469e0 | 300 | pid_t task_tgid; |
37d1e2e3 JA |
301 | |
302 | unsigned long state; | |
37d1e2e3 | 303 | struct completion exited; |
534ca6d6 JA |
304 | }; |
305 | ||
6dd0be1e | 306 | #define IO_COMPL_BATCH 32 |
6ff119a6 | 307 | #define IO_REQ_CACHE_SIZE 32 |
bf019da7 | 308 | #define IO_REQ_ALLOC_BATCH 8 |
258b29a9 | 309 | |
a1ab7b35 PB |
310 | struct io_submit_link { |
311 | struct io_kiocb *head; | |
312 | struct io_kiocb *last; | |
313 | }; | |
314 | ||
258b29a9 | 315 | struct io_submit_state { |
5a158c6b PB |
316 | /* inline/task_work completion list, under ->uring_lock */ |
317 | struct io_wq_work_node free_list; | |
318 | /* batch completion logic */ | |
319 | struct io_wq_work_list compl_reqs; | |
a1ab7b35 | 320 | struct io_submit_link link; |
258b29a9 | 321 | |
258b29a9 | 322 | bool plug_started; |
4b628aeb | 323 | bool need_plug; |
3d4aeb9f | 324 | bool flush_cqes; |
5ca7a8b3 | 325 | unsigned short submit_nr; |
5a158c6b | 326 | struct blk_plug plug; |
258b29a9 PB |
327 | }; |
328 | ||
2b188cc1 | 329 | struct io_ring_ctx { |
b52ecf8c | 330 | /* const or read-mostly hot data */ |
2b188cc1 JA |
331 | struct { |
332 | struct percpu_ref refs; | |
2b188cc1 | 333 | |
b52ecf8c | 334 | struct io_rings *rings; |
2b188cc1 | 335 | unsigned int flags; |
e1d85334 | 336 | unsigned int compat: 1; |
e1d85334 RD |
337 | unsigned int drain_next: 1; |
338 | unsigned int eventfd_async: 1; | |
21b55dbc | 339 | unsigned int restricted: 1; |
f18ee4cf | 340 | unsigned int off_timeout_used: 1; |
10c66904 | 341 | unsigned int drain_active: 1; |
5562a8d7 | 342 | unsigned int drain_disabled: 1; |
b52ecf8c | 343 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 344 | |
7f1129d2 | 345 | /* submission data */ |
b52ecf8c | 346 | struct { |
0499e582 PB |
347 | struct mutex uring_lock; |
348 | ||
75b28aff HV |
349 | /* |
350 | * Ring buffer of indices into array of io_uring_sqe, which is | |
351 | * mmapped by the application using the IORING_OFF_SQES offset. | |
352 | * | |
353 | * This indirection could e.g. be used to assign fixed | |
354 | * io_uring_sqe entries to operations and only submit them to | |
355 | * the queue when needed. | |
356 | * | |
357 | * The kernel modifies neither the indices array nor the entries | |
358 | * array. | |
359 | */ | |
360 | u32 *sq_array; | |
c7af47cf | 361 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
362 | unsigned cached_sq_head; |
363 | unsigned sq_entries; | |
de0617e4 | 364 | struct list_head defer_list; |
7f1129d2 PB |
365 | |
366 | /* | |
367 | * Fixed resources fast path, should be accessed only under | |
368 | * uring_lock, and updated through io_uring_register(2) | |
369 | */ | |
370 | struct io_rsrc_node *rsrc_node; | |
ab409402 | 371 | int rsrc_cached_refs; |
7f1129d2 PB |
372 | struct io_file_table file_table; |
373 | unsigned nr_user_files; | |
374 | unsigned nr_user_bufs; | |
375 | struct io_mapped_ubuf **user_bufs; | |
376 | ||
377 | struct io_submit_state submit_state; | |
5262f567 | 378 | struct list_head timeout_list; |
ef9dd637 | 379 | struct list_head ltimeout_list; |
1d7bb1d5 | 380 | struct list_head cq_overflow_list; |
7f1129d2 PB |
381 | struct xarray io_buffers; |
382 | struct xarray personalities; | |
383 | u32 pers_next; | |
384 | unsigned sq_thread_idle; | |
2b188cc1 JA |
385 | } ____cacheline_aligned_in_smp; |
386 | ||
d0acdee2 | 387 | /* IRQ completion list, under ->completion_lock */ |
c2b6c6bc | 388 | struct io_wq_work_list locked_free_list; |
d0acdee2 | 389 | unsigned int locked_free_nr; |
3c1a2ead | 390 | |
7c30f36a | 391 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
534ca6d6 JA |
392 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
393 | ||
90554200 | 394 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 395 | struct list_head sqd_list; |
75b28aff | 396 | |
5ed7a37d PB |
397 | unsigned long check_cq_overflow; |
398 | ||
206aefde JA |
399 | struct { |
400 | unsigned cached_cq_tail; | |
401 | unsigned cq_entries; | |
0499e582 | 402 | struct eventfd_ctx *cq_ev_fd; |
0499e582 PB |
403 | struct wait_queue_head cq_wait; |
404 | unsigned cq_extra; | |
405 | atomic_t cq_timeouts; | |
0499e582 | 406 | unsigned cq_last_tm_flush; |
206aefde | 407 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 408 | |
2b188cc1 JA |
409 | struct { |
410 | spinlock_t completion_lock; | |
e94f141b | 411 | |
89850fce JA |
412 | spinlock_t timeout_lock; |
413 | ||
def596e9 | 414 | /* |
540e32a0 | 415 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
416 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
417 | * For SQPOLL, only the single threaded io_sq_thread() will | |
418 | * manipulate the list, hence no extra locking is needed there. | |
419 | */ | |
5eef4e87 | 420 | struct io_wq_work_list iopoll_list; |
78076bb6 JA |
421 | struct hlist_head *cancel_hash; |
422 | unsigned cancel_hash_bits; | |
915b3dde | 423 | bool poll_multi_queue; |
2b188cc1 | 424 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 425 | |
21b55dbc | 426 | struct io_restriction restrictions; |
3c1a2ead | 427 | |
b13a8918 PB |
428 | /* slow path rsrc auxilary data, used by update/register */ |
429 | struct { | |
430 | struct io_rsrc_node *rsrc_backup_node; | |
431 | struct io_mapped_ubuf *dummy_ubuf; | |
432 | struct io_rsrc_data *file_data; | |
433 | struct io_rsrc_data *buf_data; | |
434 | ||
435 | struct delayed_work rsrc_put_work; | |
436 | struct llist_head rsrc_put_llist; | |
437 | struct list_head rsrc_ref_list; | |
438 | spinlock_t rsrc_ref_lock; | |
439 | }; | |
440 | ||
3c1a2ead | 441 | /* Keep this last, we don't need it for the fast path */ |
b986af7e PB |
442 | struct { |
443 | #if defined(CONFIG_UNIX) | |
444 | struct socket *ring_sock; | |
445 | #endif | |
446 | /* hashed buffered write serialization */ | |
447 | struct io_wq_hash *hash_map; | |
448 | ||
449 | /* Only used for accounting purposes */ | |
450 | struct user_struct *user; | |
451 | struct mm_struct *mm_account; | |
452 | ||
453 | /* ctx exit and cancelation */ | |
9011bf9a PB |
454 | struct llist_head fallback_llist; |
455 | struct delayed_work fallback_work; | |
b986af7e PB |
456 | struct work_struct exit_work; |
457 | struct list_head tctx_list; | |
458 | struct completion ref_comp; | |
e139a1ec PB |
459 | u32 iowq_limits[2]; |
460 | bool iowq_limits_set; | |
b986af7e | 461 | }; |
2b188cc1 JA |
462 | }; |
463 | ||
53e043b2 SM |
464 | struct io_uring_task { |
465 | /* submission side */ | |
09899b19 | 466 | int cached_refs; |
53e043b2 SM |
467 | struct xarray xa; |
468 | struct wait_queue_head wait; | |
ee53fb2b SM |
469 | const struct io_ring_ctx *last; |
470 | struct io_wq *io_wq; | |
53e043b2 | 471 | struct percpu_counter inflight; |
b303fe2e | 472 | atomic_t inflight_tracked; |
53e043b2 | 473 | atomic_t in_idle; |
53e043b2 SM |
474 | |
475 | spinlock_t task_lock; | |
476 | struct io_wq_work_list task_list; | |
4813c377 | 477 | struct io_wq_work_list prior_task_list; |
53e043b2 | 478 | struct callback_head task_work; |
6294f368 | 479 | bool task_running; |
53e043b2 SM |
480 | }; |
481 | ||
09bb8394 JA |
482 | /* |
483 | * First field must be the file pointer in all the | |
484 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
485 | */ | |
221c5eb2 JA |
486 | struct io_poll_iocb { |
487 | struct file *file; | |
018043be | 488 | struct wait_queue_head *head; |
221c5eb2 | 489 | __poll_t events; |
392edb45 | 490 | struct wait_queue_entry wait; |
221c5eb2 JA |
491 | }; |
492 | ||
9d805892 | 493 | struct io_poll_update { |
018043be | 494 | struct file *file; |
9d805892 PB |
495 | u64 old_user_data; |
496 | u64 new_user_data; | |
497 | __poll_t events; | |
b69de288 JA |
498 | bool update_events; |
499 | bool update_user_data; | |
018043be PB |
500 | }; |
501 | ||
b5dba59e JA |
502 | struct io_close { |
503 | struct file *file; | |
b5dba59e | 504 | int fd; |
7df778be | 505 | u32 file_slot; |
b5dba59e JA |
506 | }; |
507 | ||
ad8a48ac JA |
508 | struct io_timeout_data { |
509 | struct io_kiocb *req; | |
510 | struct hrtimer timer; | |
511 | struct timespec64 ts; | |
512 | enum hrtimer_mode mode; | |
50c1df2b | 513 | u32 flags; |
ad8a48ac JA |
514 | }; |
515 | ||
8ed8d3c3 JA |
516 | struct io_accept { |
517 | struct file *file; | |
518 | struct sockaddr __user *addr; | |
519 | int __user *addr_len; | |
520 | int flags; | |
aaa4db12 | 521 | u32 file_slot; |
09952e3e | 522 | unsigned long nofile; |
8ed8d3c3 JA |
523 | }; |
524 | ||
525 | struct io_sync { | |
526 | struct file *file; | |
527 | loff_t len; | |
528 | loff_t off; | |
529 | int flags; | |
d63d1b5e | 530 | int mode; |
8ed8d3c3 JA |
531 | }; |
532 | ||
fbf23849 JA |
533 | struct io_cancel { |
534 | struct file *file; | |
535 | u64 addr; | |
536 | }; | |
537 | ||
b29472ee JA |
538 | struct io_timeout { |
539 | struct file *file; | |
bfe68a22 PB |
540 | u32 off; |
541 | u32 target_seq; | |
135fcde8 | 542 | struct list_head list; |
90cd7e42 PB |
543 | /* head of the link, used by linked timeouts only */ |
544 | struct io_kiocb *head; | |
89b263f6 JA |
545 | /* for linked completions */ |
546 | struct io_kiocb *prev; | |
b29472ee JA |
547 | }; |
548 | ||
0bdf7a2d PB |
549 | struct io_timeout_rem { |
550 | struct file *file; | |
551 | u64 addr; | |
9c8e11b3 PB |
552 | |
553 | /* timeout update */ | |
554 | struct timespec64 ts; | |
555 | u32 flags; | |
f1042b6c | 556 | bool ltimeout; |
0bdf7a2d PB |
557 | }; |
558 | ||
9adbd45d JA |
559 | struct io_rw { |
560 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
561 | struct kiocb kiocb; | |
562 | u64 addr; | |
563 | u64 len; | |
564 | }; | |
565 | ||
3fbb51c1 JA |
566 | struct io_connect { |
567 | struct file *file; | |
568 | struct sockaddr __user *addr; | |
569 | int addr_len; | |
570 | }; | |
571 | ||
e47293fd JA |
572 | struct io_sr_msg { |
573 | struct file *file; | |
fddaface | 574 | union { |
4af3417a PB |
575 | struct compat_msghdr __user *umsg_compat; |
576 | struct user_msghdr __user *umsg; | |
577 | void __user *buf; | |
fddaface | 578 | }; |
e47293fd | 579 | int msg_flags; |
bcda7baa | 580 | int bgid; |
fddaface | 581 | size_t len; |
e47293fd JA |
582 | }; |
583 | ||
15b71abe JA |
584 | struct io_open { |
585 | struct file *file; | |
586 | int dfd; | |
b9445598 | 587 | u32 file_slot; |
15b71abe | 588 | struct filename *filename; |
c12cedf2 | 589 | struct open_how how; |
4022e7af | 590 | unsigned long nofile; |
15b71abe JA |
591 | }; |
592 | ||
269bbe5f | 593 | struct io_rsrc_update { |
05f3fb3c JA |
594 | struct file *file; |
595 | u64 arg; | |
596 | u32 nr_args; | |
597 | u32 offset; | |
598 | }; | |
599 | ||
4840e418 JA |
600 | struct io_fadvise { |
601 | struct file *file; | |
602 | u64 offset; | |
603 | u32 len; | |
604 | u32 advice; | |
605 | }; | |
606 | ||
c1ca757b JA |
607 | struct io_madvise { |
608 | struct file *file; | |
609 | u64 addr; | |
610 | u32 len; | |
611 | u32 advice; | |
612 | }; | |
613 | ||
3e4827b0 JA |
614 | struct io_epoll { |
615 | struct file *file; | |
616 | int epfd; | |
617 | int op; | |
618 | int fd; | |
619 | struct epoll_event event; | |
e47293fd JA |
620 | }; |
621 | ||
7d67af2c PB |
622 | struct io_splice { |
623 | struct file *file_out; | |
624 | struct file *file_in; | |
625 | loff_t off_out; | |
626 | loff_t off_in; | |
627 | u64 len; | |
628 | unsigned int flags; | |
629 | }; | |
630 | ||
ddf0322d JA |
631 | struct io_provide_buf { |
632 | struct file *file; | |
633 | __u64 addr; | |
38134ada | 634 | __u32 len; |
ddf0322d JA |
635 | __u32 bgid; |
636 | __u16 nbufs; | |
637 | __u16 bid; | |
638 | }; | |
639 | ||
1d9e1288 BM |
640 | struct io_statx { |
641 | struct file *file; | |
642 | int dfd; | |
643 | unsigned int mask; | |
644 | unsigned int flags; | |
e62753e4 | 645 | const char __user *filename; |
1d9e1288 BM |
646 | struct statx __user *buffer; |
647 | }; | |
648 | ||
36f4fa68 JA |
649 | struct io_shutdown { |
650 | struct file *file; | |
651 | int how; | |
652 | }; | |
653 | ||
80a261fd JA |
654 | struct io_rename { |
655 | struct file *file; | |
656 | int old_dfd; | |
657 | int new_dfd; | |
658 | struct filename *oldpath; | |
659 | struct filename *newpath; | |
660 | int flags; | |
661 | }; | |
662 | ||
14a1143b JA |
663 | struct io_unlink { |
664 | struct file *file; | |
665 | int dfd; | |
666 | int flags; | |
667 | struct filename *filename; | |
668 | }; | |
669 | ||
e34a02dc DK |
670 | struct io_mkdir { |
671 | struct file *file; | |
672 | int dfd; | |
673 | umode_t mode; | |
674 | struct filename *filename; | |
675 | }; | |
676 | ||
7a8721f8 DK |
677 | struct io_symlink { |
678 | struct file *file; | |
679 | int new_dfd; | |
680 | struct filename *oldpath; | |
681 | struct filename *newpath; | |
682 | }; | |
683 | ||
cf30da90 DK |
684 | struct io_hardlink { |
685 | struct file *file; | |
686 | int old_dfd; | |
687 | int new_dfd; | |
688 | struct filename *oldpath; | |
689 | struct filename *newpath; | |
690 | int flags; | |
691 | }; | |
692 | ||
f499a021 JA |
693 | struct io_async_connect { |
694 | struct sockaddr_storage address; | |
695 | }; | |
696 | ||
03b1230c JA |
697 | struct io_async_msghdr { |
698 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
699 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
700 | struct iovec *free_iov; | |
03b1230c JA |
701 | struct sockaddr __user *uaddr; |
702 | struct msghdr msg; | |
b537916c | 703 | struct sockaddr_storage addr; |
03b1230c JA |
704 | }; |
705 | ||
538941e2 | 706 | struct io_rw_state { |
ff6165b2 | 707 | struct iov_iter iter; |
cd658695 | 708 | struct iov_iter_state iter_state; |
c88598a9 | 709 | struct iovec fast_iov[UIO_FASTIOV]; |
538941e2 PB |
710 | }; |
711 | ||
712 | struct io_async_rw { | |
713 | struct io_rw_state s; | |
714 | const struct iovec *free_iovec; | |
227c0c96 | 715 | size_t bytes_done; |
bcf5a063 | 716 | struct wait_page_queue wpq; |
f67676d1 JA |
717 | }; |
718 | ||
6b47ee6e PB |
719 | enum { |
720 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
721 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
722 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
723 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
724 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 725 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
04c76b41 | 726 | REQ_F_CQE_SKIP_BIT = IOSQE_CQE_SKIP_SUCCESS_BIT, |
6b47ee6e | 727 | |
dddca226 | 728 | /* first byte is taken by user flags, shift it to not overlap */ |
93d2bcd2 | 729 | REQ_F_FAIL_BIT = 8, |
6b47ee6e PB |
730 | REQ_F_INFLIGHT_BIT, |
731 | REQ_F_CUR_POS_BIT, | |
732 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 733 | REQ_F_LINK_TIMEOUT_BIT, |
99bc4c38 | 734 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 735 | REQ_F_POLLED_BIT, |
bcda7baa | 736 | REQ_F_BUFFER_SELECTED_BIT, |
e342c807 | 737 | REQ_F_COMPLETE_INLINE_BIT, |
230d50d4 | 738 | REQ_F_REISSUE_BIT, |
b8e64b53 | 739 | REQ_F_CREDS_BIT, |
20e60a38 | 740 | REQ_F_REFCOUNT_BIT, |
4d13d1a4 | 741 | REQ_F_ARM_LTIMEOUT_BIT, |
d886e185 | 742 | REQ_F_ASYNC_DATA_BIT, |
04c76b41 | 743 | REQ_F_SKIP_LINK_CQES_BIT, |
7b29f92d | 744 | /* keep async read/write and isreg together and in order */ |
35645ac3 | 745 | REQ_F_SUPPORT_NOWAIT_BIT, |
7b29f92d | 746 | REQ_F_ISREG_BIT, |
84557871 JA |
747 | |
748 | /* not a real bit, just to check we're not overflowing the space */ | |
749 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
750 | }; |
751 | ||
752 | enum { | |
753 | /* ctx owns file */ | |
754 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
755 | /* drain existing IO first */ | |
756 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
757 | /* linked sqes */ | |
758 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
759 | /* doesn't sever on completion < 0 */ | |
760 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
761 | /* IOSQE_ASYNC */ | |
762 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
763 | /* IOSQE_BUFFER_SELECT */ |
764 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
04c76b41 PB |
765 | /* IOSQE_CQE_SKIP_SUCCESS */ |
766 | REQ_F_CQE_SKIP = BIT(REQ_F_CQE_SKIP_BIT), | |
6b47ee6e | 767 | |
6b47ee6e | 768 | /* fail rest of links */ |
93d2bcd2 | 769 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
b05a1bcd | 770 | /* on inflight list, should be cancelled and waited on exit reliably */ |
6b47ee6e PB |
771 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
772 | /* read/write uses file position */ | |
773 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
774 | /* must not punt to workers */ | |
775 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 776 | /* has or had linked timeout */ |
6b47ee6e | 777 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
99bc4c38 PB |
778 | /* needs cleanup */ |
779 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
780 | /* already went through poll handler */ |
781 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
782 | /* buffer already selected */ |
783 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
e342c807 PB |
784 | /* completion is deferred through io_comp_state */ |
785 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
230d50d4 JA |
786 | /* caller should reissue async */ |
787 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), | |
35645ac3 PB |
788 | /* supports async reads/writes */ |
789 | REQ_F_SUPPORT_NOWAIT = BIT(REQ_F_SUPPORT_NOWAIT_BIT), | |
7b29f92d JA |
790 | /* regular file */ |
791 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
b8e64b53 PB |
792 | /* has creds assigned */ |
793 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), | |
20e60a38 PB |
794 | /* skip refcounting if not set */ |
795 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), | |
4d13d1a4 PB |
796 | /* there is a linked timeout that has to be armed */ |
797 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT), | |
d886e185 PB |
798 | /* ->async_data allocated */ |
799 | REQ_F_ASYNC_DATA = BIT(REQ_F_ASYNC_DATA_BIT), | |
04c76b41 PB |
800 | /* don't post CQEs while failing linked requests */ |
801 | REQ_F_SKIP_LINK_CQES = BIT(REQ_F_SKIP_LINK_CQES_BIT), | |
d7718a9d JA |
802 | }; |
803 | ||
804 | struct async_poll { | |
805 | struct io_poll_iocb poll; | |
807abcb0 | 806 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
807 | }; |
808 | ||
f237c30a | 809 | typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked); |
5b0a6acc | 810 | |
7cbf1722 | 811 | struct io_task_work { |
5b0a6acc PB |
812 | union { |
813 | struct io_wq_work_node node; | |
814 | struct llist_node fallback_node; | |
815 | }; | |
816 | io_req_tw_func_t func; | |
7cbf1722 JA |
817 | }; |
818 | ||
992da01a PB |
819 | enum { |
820 | IORING_RSRC_FILE = 0, | |
821 | IORING_RSRC_BUFFER = 1, | |
822 | }; | |
823 | ||
09bb8394 JA |
824 | /* |
825 | * NOTE! Each of the iocb union members has the file pointer | |
826 | * as the first entry in their struct definition. So you can | |
827 | * access the file pointer through any of the sub-structs, | |
828 | * or directly as just 'ki_filp' in this struct. | |
829 | */ | |
2b188cc1 | 830 | struct io_kiocb { |
221c5eb2 | 831 | union { |
09bb8394 | 832 | struct file *file; |
9adbd45d | 833 | struct io_rw rw; |
221c5eb2 | 834 | struct io_poll_iocb poll; |
9d805892 | 835 | struct io_poll_update poll_update; |
8ed8d3c3 JA |
836 | struct io_accept accept; |
837 | struct io_sync sync; | |
fbf23849 | 838 | struct io_cancel cancel; |
b29472ee | 839 | struct io_timeout timeout; |
0bdf7a2d | 840 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 841 | struct io_connect connect; |
e47293fd | 842 | struct io_sr_msg sr_msg; |
15b71abe | 843 | struct io_open open; |
b5dba59e | 844 | struct io_close close; |
269bbe5f | 845 | struct io_rsrc_update rsrc_update; |
4840e418 | 846 | struct io_fadvise fadvise; |
c1ca757b | 847 | struct io_madvise madvise; |
3e4827b0 | 848 | struct io_epoll epoll; |
7d67af2c | 849 | struct io_splice splice; |
ddf0322d | 850 | struct io_provide_buf pbuf; |
1d9e1288 | 851 | struct io_statx statx; |
36f4fa68 | 852 | struct io_shutdown shutdown; |
80a261fd | 853 | struct io_rename rename; |
14a1143b | 854 | struct io_unlink unlink; |
e34a02dc | 855 | struct io_mkdir mkdir; |
7a8721f8 | 856 | struct io_symlink symlink; |
cf30da90 | 857 | struct io_hardlink hardlink; |
221c5eb2 | 858 | }; |
2b188cc1 | 859 | |
d625c6ee | 860 | u8 opcode; |
65a6543d XW |
861 | /* polled IO has completed */ |
862 | u8 iopoll_completed; | |
4f4eeba8 | 863 | u16 buf_index; |
d17e56eb PB |
864 | unsigned int flags; |
865 | ||
866 | u64 user_data; | |
9cf7c104 | 867 | u32 result; |
d17e56eb | 868 | u32 cflags; |
4f4eeba8 | 869 | |
010e8e6b | 870 | struct io_ring_ctx *ctx; |
010e8e6b | 871 | struct task_struct *task; |
d7718a9d | 872 | |
269bbe5f | 873 | struct percpu_ref *fixed_rsrc_refs; |
d886e185 PB |
874 | /* store used ubuf, so we can prevent reloading */ |
875 | struct io_mapped_ubuf *imu; | |
fcb323cc | 876 | |
7e3709d5 | 877 | /* used by request caches, completion batching and iopoll */ |
ef05d9eb | 878 | struct io_wq_work_node comp_list; |
d17e56eb | 879 | atomic_t refs; |
7e3709d5 | 880 | struct io_kiocb *link; |
5b0a6acc | 881 | struct io_task_work io_task_work; |
010e8e6b PB |
882 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
883 | struct hlist_node hash_node; | |
7e3709d5 | 884 | /* internal polling, see IORING_FEAT_FAST_POLL */ |
010e8e6b | 885 | struct async_poll *apoll; |
d886e185 PB |
886 | /* opcode allocated if it needs to store data for async defer */ |
887 | void *async_data; | |
010e8e6b | 888 | struct io_wq_work work; |
7e3709d5 | 889 | /* custom credentials, valid IFF REQ_F_CREDS is set */ |
fe7e3257 | 890 | const struct cred *creds; |
7e3709d5 | 891 | /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */ |
30d51dd4 | 892 | struct io_buffer *kbuf; |
aa43477b | 893 | atomic_t poll_refs; |
2b188cc1 | 894 | }; |
05589553 | 895 | |
13bf43f5 PB |
896 | struct io_tctx_node { |
897 | struct list_head ctx_node; | |
898 | struct task_struct *task; | |
13bf43f5 PB |
899 | struct io_ring_ctx *ctx; |
900 | }; | |
901 | ||
27dc8338 PB |
902 | struct io_defer_entry { |
903 | struct list_head list; | |
904 | struct io_kiocb *req; | |
9cf7c104 | 905 | u32 seq; |
2b188cc1 JA |
906 | }; |
907 | ||
d3656344 | 908 | struct io_op_def { |
d3656344 JA |
909 | /* needs req->file assigned */ |
910 | unsigned needs_file : 1; | |
6d63416d PB |
911 | /* should block plug */ |
912 | unsigned plug : 1; | |
d3656344 JA |
913 | /* hash wq insertion if file is a regular file */ |
914 | unsigned hash_reg_file : 1; | |
915 | /* unbound wq insertion if file is a non-regular file */ | |
916 | unsigned unbound_nonreg_file : 1; | |
8a72758c JA |
917 | /* set if opcode supports polled "wait" */ |
918 | unsigned pollin : 1; | |
919 | unsigned pollout : 1; | |
bcda7baa JA |
920 | /* op supports buffer selection */ |
921 | unsigned buffer_select : 1; | |
26f0505a PB |
922 | /* do prep async if is going to be punted */ |
923 | unsigned needs_async_setup : 1; | |
6d63416d PB |
924 | /* opcode is not supported by this kernel */ |
925 | unsigned not_supported : 1; | |
5bd2182d PM |
926 | /* skip auditing */ |
927 | unsigned audit_skip : 1; | |
e8c2bc1f JA |
928 | /* size of async data needed, if any */ |
929 | unsigned short async_size; | |
d3656344 JA |
930 | }; |
931 | ||
0918682b | 932 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
933 | [IORING_OP_NOP] = {}, |
934 | [IORING_OP_READV] = { | |
d3656344 JA |
935 | .needs_file = 1, |
936 | .unbound_nonreg_file = 1, | |
8a72758c | 937 | .pollin = 1, |
4d954c25 | 938 | .buffer_select = 1, |
26f0505a | 939 | .needs_async_setup = 1, |
27926b68 | 940 | .plug = 1, |
5bd2182d | 941 | .audit_skip = 1, |
e8c2bc1f | 942 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 943 | }, |
0463b6c5 | 944 | [IORING_OP_WRITEV] = { |
d3656344 JA |
945 | .needs_file = 1, |
946 | .hash_reg_file = 1, | |
947 | .unbound_nonreg_file = 1, | |
8a72758c | 948 | .pollout = 1, |
26f0505a | 949 | .needs_async_setup = 1, |
27926b68 | 950 | .plug = 1, |
5bd2182d | 951 | .audit_skip = 1, |
e8c2bc1f | 952 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 953 | }, |
0463b6c5 | 954 | [IORING_OP_FSYNC] = { |
d3656344 | 955 | .needs_file = 1, |
5bd2182d | 956 | .audit_skip = 1, |
d3656344 | 957 | }, |
0463b6c5 | 958 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
959 | .needs_file = 1, |
960 | .unbound_nonreg_file = 1, | |
8a72758c | 961 | .pollin = 1, |
27926b68 | 962 | .plug = 1, |
5bd2182d | 963 | .audit_skip = 1, |
e8c2bc1f | 964 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 965 | }, |
0463b6c5 | 966 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
967 | .needs_file = 1, |
968 | .hash_reg_file = 1, | |
969 | .unbound_nonreg_file = 1, | |
8a72758c | 970 | .pollout = 1, |
27926b68 | 971 | .plug = 1, |
5bd2182d | 972 | .audit_skip = 1, |
e8c2bc1f | 973 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 974 | }, |
0463b6c5 | 975 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
976 | .needs_file = 1, |
977 | .unbound_nonreg_file = 1, | |
5bd2182d PM |
978 | .audit_skip = 1, |
979 | }, | |
980 | [IORING_OP_POLL_REMOVE] = { | |
981 | .audit_skip = 1, | |
d3656344 | 982 | }, |
0463b6c5 | 983 | [IORING_OP_SYNC_FILE_RANGE] = { |
d3656344 | 984 | .needs_file = 1, |
5bd2182d | 985 | .audit_skip = 1, |
d3656344 | 986 | }, |
0463b6c5 | 987 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
988 | .needs_file = 1, |
989 | .unbound_nonreg_file = 1, | |
8a72758c | 990 | .pollout = 1, |
26f0505a | 991 | .needs_async_setup = 1, |
e8c2bc1f | 992 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 993 | }, |
0463b6c5 | 994 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
995 | .needs_file = 1, |
996 | .unbound_nonreg_file = 1, | |
8a72758c | 997 | .pollin = 1, |
52de1fe1 | 998 | .buffer_select = 1, |
26f0505a | 999 | .needs_async_setup = 1, |
e8c2bc1f | 1000 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 1001 | }, |
0463b6c5 | 1002 | [IORING_OP_TIMEOUT] = { |
5bd2182d | 1003 | .audit_skip = 1, |
e8c2bc1f | 1004 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 1005 | }, |
9c8e11b3 PB |
1006 | [IORING_OP_TIMEOUT_REMOVE] = { |
1007 | /* used by timeout updates' prep() */ | |
5bd2182d | 1008 | .audit_skip = 1, |
9c8e11b3 | 1009 | }, |
0463b6c5 | 1010 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
1011 | .needs_file = 1, |
1012 | .unbound_nonreg_file = 1, | |
8a72758c | 1013 | .pollin = 1, |
d3656344 | 1014 | }, |
5bd2182d PM |
1015 | [IORING_OP_ASYNC_CANCEL] = { |
1016 | .audit_skip = 1, | |
1017 | }, | |
0463b6c5 | 1018 | [IORING_OP_LINK_TIMEOUT] = { |
5bd2182d | 1019 | .audit_skip = 1, |
e8c2bc1f | 1020 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 1021 | }, |
0463b6c5 | 1022 | [IORING_OP_CONNECT] = { |
d3656344 JA |
1023 | .needs_file = 1, |
1024 | .unbound_nonreg_file = 1, | |
8a72758c | 1025 | .pollout = 1, |
26f0505a | 1026 | .needs_async_setup = 1, |
e8c2bc1f | 1027 | .async_size = sizeof(struct io_async_connect), |
d3656344 | 1028 | }, |
0463b6c5 | 1029 | [IORING_OP_FALLOCATE] = { |
d3656344 | 1030 | .needs_file = 1, |
d3656344 | 1031 | }, |
44526bed JA |
1032 | [IORING_OP_OPENAT] = {}, |
1033 | [IORING_OP_CLOSE] = {}, | |
5bd2182d PM |
1034 | [IORING_OP_FILES_UPDATE] = { |
1035 | .audit_skip = 1, | |
1036 | }, | |
1037 | [IORING_OP_STATX] = { | |
1038 | .audit_skip = 1, | |
1039 | }, | |
0463b6c5 | 1040 | [IORING_OP_READ] = { |
3a6820f2 JA |
1041 | .needs_file = 1, |
1042 | .unbound_nonreg_file = 1, | |
8a72758c | 1043 | .pollin = 1, |
bcda7baa | 1044 | .buffer_select = 1, |
27926b68 | 1045 | .plug = 1, |
5bd2182d | 1046 | .audit_skip = 1, |
e8c2bc1f | 1047 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1048 | }, |
0463b6c5 | 1049 | [IORING_OP_WRITE] = { |
3a6820f2 | 1050 | .needs_file = 1, |
7b3188e7 | 1051 | .hash_reg_file = 1, |
3a6820f2 | 1052 | .unbound_nonreg_file = 1, |
8a72758c | 1053 | .pollout = 1, |
27926b68 | 1054 | .plug = 1, |
5bd2182d | 1055 | .audit_skip = 1, |
e8c2bc1f | 1056 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1057 | }, |
0463b6c5 | 1058 | [IORING_OP_FADVISE] = { |
4840e418 | 1059 | .needs_file = 1, |
5bd2182d | 1060 | .audit_skip = 1, |
c1ca757b | 1061 | }, |
44526bed | 1062 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 1063 | [IORING_OP_SEND] = { |
fddaface JA |
1064 | .needs_file = 1, |
1065 | .unbound_nonreg_file = 1, | |
8a72758c | 1066 | .pollout = 1, |
5bd2182d | 1067 | .audit_skip = 1, |
fddaface | 1068 | }, |
0463b6c5 | 1069 | [IORING_OP_RECV] = { |
fddaface JA |
1070 | .needs_file = 1, |
1071 | .unbound_nonreg_file = 1, | |
8a72758c | 1072 | .pollin = 1, |
bcda7baa | 1073 | .buffer_select = 1, |
5bd2182d | 1074 | .audit_skip = 1, |
fddaface | 1075 | }, |
0463b6c5 | 1076 | [IORING_OP_OPENAT2] = { |
cebdb986 | 1077 | }, |
3e4827b0 JA |
1078 | [IORING_OP_EPOLL_CTL] = { |
1079 | .unbound_nonreg_file = 1, | |
5bd2182d | 1080 | .audit_skip = 1, |
3e4827b0 | 1081 | }, |
7d67af2c PB |
1082 | [IORING_OP_SPLICE] = { |
1083 | .needs_file = 1, | |
1084 | .hash_reg_file = 1, | |
1085 | .unbound_nonreg_file = 1, | |
5bd2182d PM |
1086 | .audit_skip = 1, |
1087 | }, | |
1088 | [IORING_OP_PROVIDE_BUFFERS] = { | |
1089 | .audit_skip = 1, | |
1090 | }, | |
1091 | [IORING_OP_REMOVE_BUFFERS] = { | |
1092 | .audit_skip = 1, | |
ddf0322d | 1093 | }, |
f2a8d5c7 PB |
1094 | [IORING_OP_TEE] = { |
1095 | .needs_file = 1, | |
1096 | .hash_reg_file = 1, | |
1097 | .unbound_nonreg_file = 1, | |
5bd2182d | 1098 | .audit_skip = 1, |
f2a8d5c7 | 1099 | }, |
36f4fa68 JA |
1100 | [IORING_OP_SHUTDOWN] = { |
1101 | .needs_file = 1, | |
1102 | }, | |
44526bed JA |
1103 | [IORING_OP_RENAMEAT] = {}, |
1104 | [IORING_OP_UNLINKAT] = {}, | |
e34a02dc | 1105 | [IORING_OP_MKDIRAT] = {}, |
7a8721f8 | 1106 | [IORING_OP_SYMLINKAT] = {}, |
cf30da90 | 1107 | [IORING_OP_LINKAT] = {}, |
d3656344 JA |
1108 | }; |
1109 | ||
0756a869 PB |
1110 | /* requests with any of those set should undergo io_disarm_next() */ |
1111 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) | |
1112 | ||
7a612350 | 1113 | static bool io_disarm_next(struct io_kiocb *req); |
eef51daa | 1114 | static void io_uring_del_tctx_node(unsigned long index); |
9936c7c2 PB |
1115 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1116 | struct task_struct *task, | |
3dd0c97a | 1117 | bool cancel_all); |
78cc687b | 1118 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); |
1ffc5422 | 1119 | |
913a571a PB |
1120 | static void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags); |
1121 | ||
ec9c02ad | 1122 | static void io_put_req(struct io_kiocb *req); |
91c2f697 | 1123 | static void io_put_req_deferred(struct io_kiocb *req); |
c7dae4ba | 1124 | static void io_dismantle_req(struct io_kiocb *req); |
94ae5e77 | 1125 | static void io_queue_linked_timeout(struct io_kiocb *req); |
fdecb662 | 1126 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 1127 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 | 1128 | unsigned nr_args); |
68fb8979 | 1129 | static void io_clean_op(struct io_kiocb *req); |
ac177053 | 1130 | static struct file *io_file_get(struct io_ring_ctx *ctx, |
8371adf5 | 1131 | struct io_kiocb *req, int fd, bool fixed); |
c5eef2b9 | 1132 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1133 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1134 | |
907d1df3 | 1135 | static void io_req_task_queue(struct io_kiocb *req); |
c450178d | 1136 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx); |
179ae0d1 | 1137 | static int io_req_prep_async(struct io_kiocb *req); |
de0617e4 | 1138 | |
b9445598 PB |
1139 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
1140 | unsigned int issue_flags, u32 slot_index); | |
7df778be PB |
1141 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags); |
1142 | ||
f1042b6c | 1143 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer); |
b9445598 | 1144 | |
2b188cc1 JA |
1145 | static struct kmem_cache *req_cachep; |
1146 | ||
0918682b | 1147 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1148 | |
1149 | struct sock *io_uring_get_socket(struct file *file) | |
1150 | { | |
1151 | #if defined(CONFIG_UNIX) | |
1152 | if (file->f_op == &io_uring_fops) { | |
1153 | struct io_ring_ctx *ctx = file->private_data; | |
1154 | ||
1155 | return ctx->ring_sock->sk; | |
1156 | } | |
1157 | #endif | |
1158 | return NULL; | |
1159 | } | |
1160 | EXPORT_SYMBOL(io_uring_get_socket); | |
1161 | ||
f237c30a PB |
1162 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
1163 | { | |
1164 | if (!*locked) { | |
1165 | mutex_lock(&ctx->uring_lock); | |
1166 | *locked = true; | |
1167 | } | |
1168 | } | |
1169 | ||
f2f87370 PB |
1170 | #define io_for_each_link(pos, head) \ |
1171 | for (pos = (head); pos; pos = pos->link) | |
1172 | ||
21c843d5 PB |
1173 | /* |
1174 | * Shamelessly stolen from the mm implementation of page reference checking, | |
1175 | * see commit f958d7b528b1 for details. | |
1176 | */ | |
1177 | #define req_ref_zero_or_close_to_overflow(req) \ | |
1178 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) | |
1179 | ||
1180 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) | |
1181 | { | |
20e60a38 | 1182 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1183 | return atomic_inc_not_zero(&req->refs); |
1184 | } | |
1185 | ||
21c843d5 PB |
1186 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
1187 | { | |
20e60a38 PB |
1188 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
1189 | return true; | |
1190 | ||
21c843d5 PB |
1191 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1192 | return atomic_dec_and_test(&req->refs); | |
1193 | } | |
1194 | ||
21c843d5 PB |
1195 | static inline void req_ref_get(struct io_kiocb *req) |
1196 | { | |
20e60a38 | 1197 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1198 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1199 | atomic_inc(&req->refs); | |
1200 | } | |
1201 | ||
c450178d PB |
1202 | static inline void io_submit_flush_completions(struct io_ring_ctx *ctx) |
1203 | { | |
6f33b0bc | 1204 | if (!wq_list_empty(&ctx->submit_state.compl_reqs)) |
c450178d PB |
1205 | __io_submit_flush_completions(ctx); |
1206 | } | |
1207 | ||
48dcd38d | 1208 | static inline void __io_req_set_refcount(struct io_kiocb *req, int nr) |
20e60a38 PB |
1209 | { |
1210 | if (!(req->flags & REQ_F_REFCOUNT)) { | |
1211 | req->flags |= REQ_F_REFCOUNT; | |
48dcd38d | 1212 | atomic_set(&req->refs, nr); |
20e60a38 PB |
1213 | } |
1214 | } | |
1215 | ||
48dcd38d PB |
1216 | static inline void io_req_set_refcount(struct io_kiocb *req) |
1217 | { | |
1218 | __io_req_set_refcount(req, 1); | |
1219 | } | |
1220 | ||
ab409402 PB |
1221 | #define IO_RSRC_REF_BATCH 100 |
1222 | ||
1223 | static inline void io_req_put_rsrc_locked(struct io_kiocb *req, | |
1224 | struct io_ring_ctx *ctx) | |
1225 | __must_hold(&ctx->uring_lock) | |
36f72fe2 | 1226 | { |
ab409402 PB |
1227 | struct percpu_ref *ref = req->fixed_rsrc_refs; |
1228 | ||
1229 | if (ref) { | |
1230 | if (ref == &ctx->rsrc_node->refs) | |
1231 | ctx->rsrc_cached_refs++; | |
1232 | else | |
1233 | percpu_ref_put(ref); | |
1234 | } | |
1235 | } | |
1236 | ||
1237 | static inline void io_req_put_rsrc(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
1238 | { | |
1239 | if (req->fixed_rsrc_refs) | |
1240 | percpu_ref_put(req->fixed_rsrc_refs); | |
1241 | } | |
1242 | ||
1243 | static __cold void io_rsrc_refs_drop(struct io_ring_ctx *ctx) | |
1244 | __must_hold(&ctx->uring_lock) | |
1245 | { | |
1246 | if (ctx->rsrc_cached_refs) { | |
1247 | percpu_ref_put_many(&ctx->rsrc_node->refs, ctx->rsrc_cached_refs); | |
1248 | ctx->rsrc_cached_refs = 0; | |
1249 | } | |
1250 | } | |
1251 | ||
1252 | static void io_rsrc_refs_refill(struct io_ring_ctx *ctx) | |
1253 | __must_hold(&ctx->uring_lock) | |
1254 | { | |
1255 | ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH; | |
1256 | percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH); | |
1257 | } | |
36f72fe2 | 1258 | |
a46be971 PB |
1259 | static inline void io_req_set_rsrc_node(struct io_kiocb *req, |
1260 | struct io_ring_ctx *ctx) | |
36f72fe2 | 1261 | { |
269bbe5f | 1262 | if (!req->fixed_rsrc_refs) { |
a7f0ed5a | 1263 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
ab409402 PB |
1264 | ctx->rsrc_cached_refs--; |
1265 | if (unlikely(ctx->rsrc_cached_refs < 0)) | |
1266 | io_rsrc_refs_refill(ctx); | |
36f72fe2 PB |
1267 | } |
1268 | } | |
1269 | ||
d1fd1c20 | 1270 | static unsigned int __io_put_kbuf(struct io_kiocb *req) |
3648e526 | 1271 | { |
d1fd1c20 | 1272 | struct io_buffer *kbuf = req->kbuf; |
3648e526 HX |
1273 | unsigned int cflags; |
1274 | ||
1275 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; | |
1276 | cflags |= IORING_CQE_F_BUFFER; | |
1277 | req->flags &= ~REQ_F_BUFFER_SELECTED; | |
1278 | kfree(kbuf); | |
d1fd1c20 | 1279 | req->kbuf = NULL; |
3648e526 HX |
1280 | return cflags; |
1281 | } | |
1282 | ||
d1fd1c20 | 1283 | static inline unsigned int io_put_kbuf(struct io_kiocb *req) |
3648e526 HX |
1284 | { |
1285 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) | |
1286 | return 0; | |
d1fd1c20 | 1287 | return __io_put_kbuf(req); |
3648e526 HX |
1288 | } |
1289 | ||
f70865db PB |
1290 | static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
1291 | { | |
1292 | bool got = percpu_ref_tryget(ref); | |
1293 | ||
1294 | /* already at zero, wait for ->release() */ | |
1295 | if (!got) | |
1296 | wait_for_completion(compl); | |
1297 | percpu_ref_resurrect(ref); | |
1298 | if (got) | |
1299 | percpu_ref_put(ref); | |
1300 | } | |
1301 | ||
3dd0c97a PB |
1302 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
1303 | bool cancel_all) | |
6af3f48b | 1304 | __must_hold(&req->ctx->timeout_lock) |
08d23634 PB |
1305 | { |
1306 | struct io_kiocb *req; | |
1307 | ||
68207680 | 1308 | if (task && head->task != task) |
08d23634 | 1309 | return false; |
3dd0c97a | 1310 | if (cancel_all) |
08d23634 PB |
1311 | return true; |
1312 | ||
1313 | io_for_each_link(req, head) { | |
b05a1bcd | 1314 | if (req->flags & REQ_F_INFLIGHT) |
02a13674 | 1315 | return true; |
08d23634 PB |
1316 | } |
1317 | return false; | |
1318 | } | |
1319 | ||
6af3f48b PB |
1320 | static bool io_match_linked(struct io_kiocb *head) |
1321 | { | |
1322 | struct io_kiocb *req; | |
1323 | ||
1324 | io_for_each_link(req, head) { | |
1325 | if (req->flags & REQ_F_INFLIGHT) | |
1326 | return true; | |
1327 | } | |
1328 | return false; | |
1329 | } | |
1330 | ||
1331 | /* | |
1332 | * As io_match_task() but protected against racing with linked timeouts. | |
1333 | * User must not hold timeout_lock. | |
1334 | */ | |
1335 | static bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task, | |
1336 | bool cancel_all) | |
1337 | { | |
1338 | bool matched; | |
1339 | ||
1340 | if (task && head->task != task) | |
1341 | return false; | |
1342 | if (cancel_all) | |
1343 | return true; | |
1344 | ||
1345 | if (head->flags & REQ_F_LINK_TIMEOUT) { | |
1346 | struct io_ring_ctx *ctx = head->ctx; | |
1347 | ||
1348 | /* protect against races with linked timeouts */ | |
1349 | spin_lock_irq(&ctx->timeout_lock); | |
1350 | matched = io_match_linked(head); | |
1351 | spin_unlock_irq(&ctx->timeout_lock); | |
1352 | } else { | |
1353 | matched = io_match_linked(head); | |
1354 | } | |
1355 | return matched; | |
1356 | } | |
1357 | ||
d886e185 PB |
1358 | static inline bool req_has_async_data(struct io_kiocb *req) |
1359 | { | |
1360 | return req->flags & REQ_F_ASYNC_DATA; | |
1361 | } | |
1362 | ||
93d2bcd2 | 1363 | static inline void req_set_fail(struct io_kiocb *req) |
c40f6379 | 1364 | { |
93d2bcd2 | 1365 | req->flags |= REQ_F_FAIL; |
04c76b41 PB |
1366 | if (req->flags & REQ_F_CQE_SKIP) { |
1367 | req->flags &= ~REQ_F_CQE_SKIP; | |
1368 | req->flags |= REQ_F_SKIP_LINK_CQES; | |
1369 | } | |
c40f6379 | 1370 | } |
4a38aed2 | 1371 | |
a8295b98 HX |
1372 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
1373 | { | |
1374 | req_set_fail(req); | |
1375 | req->result = res; | |
1376 | } | |
1377 | ||
c072481d | 1378 | static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref) |
2b188cc1 JA |
1379 | { |
1380 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1381 | ||
0f158b4c | 1382 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1383 | } |
1384 | ||
8eb7e2d0 PB |
1385 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1386 | { | |
1387 | return !req->timeout.off; | |
1388 | } | |
1389 | ||
c072481d | 1390 | static __cold void io_fallback_req_func(struct work_struct *work) |
f56165e6 PB |
1391 | { |
1392 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, | |
1393 | fallback_work.work); | |
1394 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); | |
1395 | struct io_kiocb *req, *tmp; | |
f237c30a | 1396 | bool locked = false; |
f56165e6 PB |
1397 | |
1398 | percpu_ref_get(&ctx->refs); | |
1399 | llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node) | |
f237c30a | 1400 | req->io_task_work.func(req, &locked); |
5636c00d | 1401 | |
f237c30a | 1402 | if (locked) { |
c450178d | 1403 | io_submit_flush_completions(ctx); |
f237c30a PB |
1404 | mutex_unlock(&ctx->uring_lock); |
1405 | } | |
f56165e6 PB |
1406 | percpu_ref_put(&ctx->refs); |
1407 | } | |
1408 | ||
c072481d | 1409 | static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
2b188cc1 JA |
1410 | { |
1411 | struct io_ring_ctx *ctx; | |
78076bb6 | 1412 | int hash_bits; |
2b188cc1 JA |
1413 | |
1414 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1415 | if (!ctx) | |
1416 | return NULL; | |
1417 | ||
78076bb6 JA |
1418 | /* |
1419 | * Use 5 bits less than the max cq entries, that should give us around | |
1420 | * 32 entries per hash list if totally full and uniformly spread. | |
1421 | */ | |
1422 | hash_bits = ilog2(p->cq_entries); | |
1423 | hash_bits -= 5; | |
1424 | if (hash_bits <= 0) | |
1425 | hash_bits = 1; | |
1426 | ctx->cancel_hash_bits = hash_bits; | |
1427 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1428 | GFP_KERNEL); | |
1429 | if (!ctx->cancel_hash) | |
1430 | goto err; | |
1431 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1432 | ||
6224843d PB |
1433 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
1434 | if (!ctx->dummy_ubuf) | |
1435 | goto err; | |
1436 | /* set invalid range, so io_import_fixed() fails meeting it */ | |
1437 | ctx->dummy_ubuf->ubuf = -1UL; | |
1438 | ||
21482896 | 1439 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1440 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1441 | goto err; | |
2b188cc1 JA |
1442 | |
1443 | ctx->flags = p->flags; | |
90554200 | 1444 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1445 | INIT_LIST_HEAD(&ctx->sqd_list); |
1d7bb1d5 | 1446 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c | 1447 | init_completion(&ctx->ref_comp); |
9e15c3a0 | 1448 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
61cf9370 | 1449 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
2b188cc1 | 1450 | mutex_init(&ctx->uring_lock); |
311997b3 | 1451 | init_waitqueue_head(&ctx->cq_wait); |
2b188cc1 | 1452 | spin_lock_init(&ctx->completion_lock); |
89850fce | 1453 | spin_lock_init(&ctx->timeout_lock); |
5eef4e87 | 1454 | INIT_WQ_LIST(&ctx->iopoll_list); |
de0617e4 | 1455 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1456 | INIT_LIST_HEAD(&ctx->timeout_list); |
ef9dd637 | 1457 | INIT_LIST_HEAD(&ctx->ltimeout_list); |
d67d2263 BM |
1458 | spin_lock_init(&ctx->rsrc_ref_lock); |
1459 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1460 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1461 | init_llist_head(&ctx->rsrc_put_llist); | |
13bf43f5 | 1462 | INIT_LIST_HEAD(&ctx->tctx_list); |
c2b6c6bc PB |
1463 | ctx->submit_state.free_list.next = NULL; |
1464 | INIT_WQ_LIST(&ctx->locked_free_list); | |
9011bf9a | 1465 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
6f33b0bc | 1466 | INIT_WQ_LIST(&ctx->submit_state.compl_reqs); |
2b188cc1 | 1467 | return ctx; |
206aefde | 1468 | err: |
6224843d | 1469 | kfree(ctx->dummy_ubuf); |
78076bb6 | 1470 | kfree(ctx->cancel_hash); |
206aefde JA |
1471 | kfree(ctx); |
1472 | return NULL; | |
2b188cc1 JA |
1473 | } |
1474 | ||
8f6ed49a PB |
1475 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
1476 | { | |
1477 | struct io_rings *r = ctx->rings; | |
1478 | ||
1479 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); | |
1480 | ctx->cq_extra--; | |
1481 | } | |
1482 | ||
9cf7c104 | 1483 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1484 | { |
2bc9930e JA |
1485 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1486 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1487 | |
8f6ed49a | 1488 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
2bc9930e | 1489 | } |
de0617e4 | 1490 | |
9d858b21 | 1491 | return false; |
de0617e4 JA |
1492 | } |
1493 | ||
35645ac3 PB |
1494 | #define FFS_NOWAIT 0x1UL |
1495 | #define FFS_ISREG 0x2UL | |
1496 | #define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG) | |
c97d8a0f PB |
1497 | |
1498 | static inline bool io_req_ffs_set(struct io_kiocb *req) | |
1499 | { | |
35645ac3 | 1500 | return req->flags & REQ_F_FIXED_FILE; |
c97d8a0f PB |
1501 | } |
1502 | ||
c072481d | 1503 | static inline void io_req_track_inflight(struct io_kiocb *req) |
ce3d5aae | 1504 | { |
ce3d5aae | 1505 | if (!(req->flags & REQ_F_INFLIGHT)) { |
ce3d5aae | 1506 | req->flags |= REQ_F_INFLIGHT; |
b303fe2e | 1507 | atomic_inc(¤t->io_uring->inflight_tracked); |
ce3d5aae PB |
1508 | } |
1509 | } | |
1510 | ||
fd08e530 PB |
1511 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
1512 | { | |
906c6caa PB |
1513 | if (WARN_ON_ONCE(!req->link)) |
1514 | return NULL; | |
1515 | ||
4d13d1a4 PB |
1516 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
1517 | req->flags |= REQ_F_LINK_TIMEOUT; | |
fd08e530 PB |
1518 | |
1519 | /* linked timeouts should have two refs once prep'ed */ | |
48dcd38d | 1520 | io_req_set_refcount(req); |
4d13d1a4 PB |
1521 | __io_req_set_refcount(req->link, 2); |
1522 | return req->link; | |
fd08e530 PB |
1523 | } |
1524 | ||
1525 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) | |
1526 | { | |
4d13d1a4 | 1527 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
fd08e530 PB |
1528 | return NULL; |
1529 | return __io_prep_linked_timeout(req); | |
1530 | } | |
1531 | ||
1e6fa521 JA |
1532 | static void io_prep_async_work(struct io_kiocb *req) |
1533 | { | |
1534 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1535 | struct io_ring_ctx *ctx = req->ctx; |
1536 | ||
b8e64b53 PB |
1537 | if (!(req->flags & REQ_F_CREDS)) { |
1538 | req->flags |= REQ_F_CREDS; | |
c10d1f98 | 1539 | req->creds = get_current_cred(); |
b8e64b53 | 1540 | } |
003e8dcc | 1541 | |
e1d675df PB |
1542 | req->work.list.next = NULL; |
1543 | req->work.flags = 0; | |
feaadc4f PB |
1544 | if (req->flags & REQ_F_FORCE_ASYNC) |
1545 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1546 | ||
1e6fa521 JA |
1547 | if (req->flags & REQ_F_ISREG) { |
1548 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1549 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
4b982bd0 | 1550 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
1e6fa521 JA |
1551 | if (def->unbound_nonreg_file) |
1552 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1553 | } | |
e1d675df PB |
1554 | |
1555 | switch (req->opcode) { | |
1556 | case IORING_OP_SPLICE: | |
1557 | case IORING_OP_TEE: | |
e1d675df PB |
1558 | if (!S_ISREG(file_inode(req->splice.file_in)->i_mode)) |
1559 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1560 | break; | |
1561 | } | |
561fb04a | 1562 | } |
cccf0ee8 | 1563 | |
cbdcb435 | 1564 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1565 | { |
cbdcb435 | 1566 | struct io_kiocb *cur; |
54a91f3b | 1567 | |
44eff40a PB |
1568 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
1569 | struct io_ring_ctx *ctx = req->ctx; | |
1570 | ||
674ee8e1 | 1571 | spin_lock_irq(&ctx->timeout_lock); |
44eff40a PB |
1572 | io_for_each_link(cur, req) |
1573 | io_prep_async_work(cur); | |
674ee8e1 | 1574 | spin_unlock_irq(&ctx->timeout_lock); |
44eff40a PB |
1575 | } else { |
1576 | io_for_each_link(cur, req) | |
1577 | io_prep_async_work(cur); | |
1578 | } | |
561fb04a JA |
1579 | } |
1580 | ||
fff4e40e PB |
1581 | static inline void io_req_add_compl_list(struct io_kiocb *req) |
1582 | { | |
3d4aeb9f | 1583 | struct io_ring_ctx *ctx = req->ctx; |
33ce2aff | 1584 | struct io_submit_state *state = &ctx->submit_state; |
fff4e40e | 1585 | |
3d4aeb9f PB |
1586 | if (!(req->flags & REQ_F_CQE_SKIP)) |
1587 | ctx->submit_state.flush_cqes = true; | |
fff4e40e PB |
1588 | wq_list_add_tail(&req->comp_list, &state->compl_reqs); |
1589 | } | |
1590 | ||
00169246 | 1591 | static void io_queue_async_work(struct io_kiocb *req, bool *dont_use) |
561fb04a | 1592 | { |
a197f664 | 1593 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1594 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1595 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1596 | |
3bfe6106 JA |
1597 | BUG_ON(!tctx); |
1598 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1599 | |
cbdcb435 PB |
1600 | /* init ->work of the whole link before punting */ |
1601 | io_prep_async_link(req); | |
991468dc JA |
1602 | |
1603 | /* | |
1604 | * Not expected to happen, but if we do have a bug where this _can_ | |
1605 | * happen, catch it here and ensure the request is marked as | |
1606 | * canceled. That will make io-wq go through the usual work cancel | |
1607 | * procedure rather than attempt to run this request (or create a new | |
1608 | * worker for it). | |
1609 | */ | |
1610 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) | |
1611 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1612 | ||
d07f1e8a PB |
1613 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1614 | &req->work, req->flags); | |
ebf93667 | 1615 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a JA |
1616 | if (link) |
1617 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1618 | } |
1619 | ||
1ee4160c | 1620 | static void io_kill_timeout(struct io_kiocb *req, int status) |
8c855885 | 1621 | __must_hold(&req->ctx->completion_lock) |
89850fce | 1622 | __must_hold(&req->ctx->timeout_lock) |
5262f567 | 1623 | { |
e8c2bc1f | 1624 | struct io_timeout_data *io = req->async_data; |
5262f567 | 1625 | |
fd9c7bc5 | 1626 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
2ae2eb9d PB |
1627 | if (status) |
1628 | req_set_fail(req); | |
01cec8c1 PB |
1629 | atomic_set(&req->ctx->cq_timeouts, |
1630 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1631 | list_del_init(&req->timeout.list); |
913a571a | 1632 | io_fill_cqe_req(req, status, 0); |
91c2f697 | 1633 | io_put_req_deferred(req); |
5262f567 JA |
1634 | } |
1635 | } | |
1636 | ||
c072481d | 1637 | static __cold void io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1638 | { |
441b8a78 | 1639 | while (!list_empty(&ctx->defer_list)) { |
27dc8338 PB |
1640 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1641 | struct io_defer_entry, list); | |
de0617e4 | 1642 | |
9cf7c104 | 1643 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1644 | break; |
27dc8338 | 1645 | list_del_init(&de->list); |
907d1df3 | 1646 | io_req_task_queue(de->req); |
27dc8338 | 1647 | kfree(de); |
441b8a78 | 1648 | } |
04518945 PB |
1649 | } |
1650 | ||
c072481d | 1651 | static __cold void io_flush_timeouts(struct io_ring_ctx *ctx) |
89850fce | 1652 | __must_hold(&ctx->completion_lock) |
de0617e4 | 1653 | { |
441b8a78 | 1654 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
f010505b | 1655 | |
79ebeaee | 1656 | spin_lock_irq(&ctx->timeout_lock); |
f18ee4cf | 1657 | while (!list_empty(&ctx->timeout_list)) { |
f010505b | 1658 | u32 events_needed, events_got; |
360428f8 | 1659 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1660 | struct io_kiocb, timeout.list); |
de0617e4 | 1661 | |
8eb7e2d0 | 1662 | if (io_is_timeout_noseq(req)) |
360428f8 | 1663 | break; |
f010505b MDG |
1664 | |
1665 | /* | |
1666 | * Since seq can easily wrap around over time, subtract | |
1667 | * the last seq at which timeouts were flushed before comparing. | |
1668 | * Assuming not more than 2^31-1 events have happened since, | |
1669 | * these subtractions won't have wrapped, so we can check if | |
1670 | * target is in [last_seq, current_seq] by comparing the two. | |
1671 | */ | |
1672 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1673 | events_got = seq - ctx->cq_last_tm_flush; | |
1674 | if (events_got < events_needed) | |
360428f8 | 1675 | break; |
bfe68a22 | 1676 | |
135fcde8 | 1677 | list_del_init(&req->timeout.list); |
1ee4160c | 1678 | io_kill_timeout(req, 0); |
f18ee4cf | 1679 | } |
f010505b | 1680 | ctx->cq_last_tm_flush = seq; |
79ebeaee | 1681 | spin_unlock_irq(&ctx->timeout_lock); |
360428f8 | 1682 | } |
5262f567 | 1683 | |
c072481d | 1684 | static __cold void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
360428f8 | 1685 | { |
2335f6f5 PB |
1686 | if (ctx->off_timeout_used) |
1687 | io_flush_timeouts(ctx); | |
1688 | if (ctx->drain_active) | |
1689 | io_queue_deferred(ctx); | |
1690 | } | |
1691 | ||
1692 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) | |
1693 | { | |
1694 | if (unlikely(ctx->off_timeout_used || ctx->drain_active)) | |
1695 | __io_commit_cqring_flush(ctx); | |
ec30e04b PB |
1696 | /* order cqe stores with ring update */ |
1697 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 JA |
1698 | } |
1699 | ||
90554200 JA |
1700 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1701 | { | |
1702 | struct io_rings *r = ctx->rings; | |
1703 | ||
a566c556 | 1704 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries; |
90554200 JA |
1705 | } |
1706 | ||
888aae2e PB |
1707 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1708 | { | |
1709 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1710 | } | |
1711 | ||
d068b506 | 1712 | static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) |
2b188cc1 | 1713 | { |
75b28aff | 1714 | struct io_rings *rings = ctx->rings; |
ea5ab3b5 | 1715 | unsigned tail, mask = ctx->cq_entries - 1; |
2b188cc1 | 1716 | |
115e12e5 SB |
1717 | /* |
1718 | * writes to the cq entry need to come after reading head; the | |
1719 | * control dependency is enough as we're using WRITE_ONCE to | |
1720 | * fill the cq entry | |
1721 | */ | |
a566c556 | 1722 | if (__io_cqring_events(ctx) == ctx->cq_entries) |
2b188cc1 JA |
1723 | return NULL; |
1724 | ||
888aae2e | 1725 | tail = ctx->cached_cq_tail++; |
ea5ab3b5 | 1726 | return &rings->cqes[tail & mask]; |
2b188cc1 JA |
1727 | } |
1728 | ||
f2842ab5 JA |
1729 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1730 | { | |
44c769de | 1731 | if (likely(!ctx->cq_ev_fd)) |
f0b493e6 | 1732 | return false; |
7e55a19c SG |
1733 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1734 | return false; | |
44c769de | 1735 | return !ctx->eventfd_async || io_wq_current_is_worker(); |
f2842ab5 JA |
1736 | } |
1737 | ||
2c5d763c JA |
1738 | /* |
1739 | * This should only get called when at least one event has been posted. | |
1740 | * Some applications rely on the eventfd notification count only changing | |
1741 | * IFF a new CQE has been added to the CQ ring. There's no depedency on | |
1742 | * 1:1 relationship between how many times this function is called (and | |
1743 | * hence the eventfd count) and number of CQEs posted to the CQ ring. | |
1744 | */ | |
b41e9852 | 1745 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1746 | { |
5fd46178 JA |
1747 | /* |
1748 | * wake_up_all() may seem excessive, but io_wake_function() and | |
1749 | * io_should_wake() handle the termination of the loop and only | |
1750 | * wake as many waiters as we need to. | |
1751 | */ | |
1752 | if (wq_has_sleeper(&ctx->cq_wait)) | |
1753 | wake_up_all(&ctx->cq_wait); | |
b41e9852 | 1754 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 JA |
1755 | eventfd_signal(ctx->cq_ev_fd, 1); |
1756 | } | |
1757 | ||
80c18e4a PB |
1758 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1759 | { | |
c57a91fb PB |
1760 | /* see waitqueue_active() comment */ |
1761 | smp_mb(); | |
1762 | ||
80c18e4a | 1763 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
c57a91fb | 1764 | if (waitqueue_active(&ctx->cq_wait)) |
5fd46178 | 1765 | wake_up_all(&ctx->cq_wait); |
80c18e4a PB |
1766 | } |
1767 | if (io_should_trigger_evfd(ctx)) | |
1768 | eventfd_signal(ctx->cq_ev_fd, 1); | |
1769 | } | |
1770 | ||
c4a2ed72 | 1771 | /* Returns true if there are no backlogged entries after the flush */ |
6c2450ae | 1772 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
1d7bb1d5 | 1773 | { |
b18032bb | 1774 | bool all_flushed, posted; |
1d7bb1d5 | 1775 | |
a566c556 | 1776 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
e23de15f | 1777 | return false; |
1d7bb1d5 | 1778 | |
b18032bb | 1779 | posted = false; |
79ebeaee | 1780 | spin_lock(&ctx->completion_lock); |
6c2450ae | 1781 | while (!list_empty(&ctx->cq_overflow_list)) { |
d068b506 | 1782 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
6c2450ae | 1783 | struct io_overflow_cqe *ocqe; |
e6c8aa9a | 1784 | |
1d7bb1d5 JA |
1785 | if (!cqe && !force) |
1786 | break; | |
6c2450ae PB |
1787 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
1788 | struct io_overflow_cqe, list); | |
1789 | if (cqe) | |
1790 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); | |
1791 | else | |
8f6ed49a PB |
1792 | io_account_cq_overflow(ctx); |
1793 | ||
b18032bb | 1794 | posted = true; |
6c2450ae PB |
1795 | list_del(&ocqe->list); |
1796 | kfree(ocqe); | |
1d7bb1d5 JA |
1797 | } |
1798 | ||
09e88404 PB |
1799 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1800 | if (all_flushed) { | |
5ed7a37d | 1801 | clear_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1802 | WRITE_ONCE(ctx->rings->sq_flags, |
1803 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); | |
09e88404 | 1804 | } |
46930143 | 1805 | |
b18032bb JA |
1806 | if (posted) |
1807 | io_commit_cqring(ctx); | |
79ebeaee | 1808 | spin_unlock(&ctx->completion_lock); |
b18032bb JA |
1809 | if (posted) |
1810 | io_cqring_ev_posted(ctx); | |
09e88404 | 1811 | return all_flushed; |
1d7bb1d5 JA |
1812 | } |
1813 | ||
90f67366 | 1814 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
6c503150 | 1815 | { |
ca0a2651 JA |
1816 | bool ret = true; |
1817 | ||
5ed7a37d | 1818 | if (test_bit(0, &ctx->check_cq_overflow)) { |
6c503150 PB |
1819 | /* iopoll syncs against uring_lock, not completion_lock */ |
1820 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1821 | mutex_lock(&ctx->uring_lock); | |
90f67366 | 1822 | ret = __io_cqring_overflow_flush(ctx, false); |
6c503150 PB |
1823 | if (ctx->flags & IORING_SETUP_IOPOLL) |
1824 | mutex_unlock(&ctx->uring_lock); | |
1825 | } | |
ca0a2651 JA |
1826 | |
1827 | return ret; | |
6c503150 PB |
1828 | } |
1829 | ||
6a290a14 PB |
1830 | /* must to be called somewhat shortly after putting a request */ |
1831 | static inline void io_put_task(struct task_struct *task, int nr) | |
1832 | { | |
1833 | struct io_uring_task *tctx = task->io_uring; | |
1834 | ||
e98e49b2 PB |
1835 | if (likely(task == current)) { |
1836 | tctx->cached_refs += nr; | |
1837 | } else { | |
1838 | percpu_counter_sub(&tctx->inflight, nr); | |
1839 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1840 | wake_up(&tctx->wait); | |
1841 | put_task_struct_many(task, nr); | |
1842 | } | |
6a290a14 PB |
1843 | } |
1844 | ||
9a10867a PB |
1845 | static void io_task_refs_refill(struct io_uring_task *tctx) |
1846 | { | |
1847 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; | |
1848 | ||
1849 | percpu_counter_add(&tctx->inflight, refill); | |
1850 | refcount_add(refill, ¤t->usage); | |
1851 | tctx->cached_refs += refill; | |
1852 | } | |
1853 | ||
1854 | static inline void io_get_task_refs(int nr) | |
1855 | { | |
1856 | struct io_uring_task *tctx = current->io_uring; | |
1857 | ||
1858 | tctx->cached_refs -= nr; | |
1859 | if (unlikely(tctx->cached_refs < 0)) | |
1860 | io_task_refs_refill(tctx); | |
1861 | } | |
1862 | ||
3cc7fdb9 PB |
1863 | static __cold void io_uring_drop_tctx_refs(struct task_struct *task) |
1864 | { | |
1865 | struct io_uring_task *tctx = task->io_uring; | |
1866 | unsigned int refs = tctx->cached_refs; | |
1867 | ||
1868 | if (refs) { | |
1869 | tctx->cached_refs = 0; | |
1870 | percpu_counter_sub(&tctx->inflight, refs); | |
1871 | put_task_struct_many(task, refs); | |
1872 | } | |
1873 | } | |
1874 | ||
d4d19c19 | 1875 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
54daa9b2 | 1876 | s32 res, u32 cflags) |
2b188cc1 | 1877 | { |
cce4b8b0 | 1878 | struct io_overflow_cqe *ocqe; |
2b188cc1 | 1879 | |
cce4b8b0 PB |
1880 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
1881 | if (!ocqe) { | |
1882 | /* | |
1883 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1884 | * or cannot allocate an overflow entry, then we need to drop it | |
1885 | * on the floor. | |
1886 | */ | |
8f6ed49a | 1887 | io_account_cq_overflow(ctx); |
cce4b8b0 | 1888 | return false; |
2b188cc1 | 1889 | } |
cce4b8b0 | 1890 | if (list_empty(&ctx->cq_overflow_list)) { |
5ed7a37d | 1891 | set_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1892 | WRITE_ONCE(ctx->rings->sq_flags, |
1893 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); | |
1894 | ||
cce4b8b0 | 1895 | } |
d4d19c19 | 1896 | ocqe->cqe.user_data = user_data; |
cce4b8b0 PB |
1897 | ocqe->cqe.res = res; |
1898 | ocqe->cqe.flags = cflags; | |
1899 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); | |
1900 | return true; | |
2b188cc1 JA |
1901 | } |
1902 | ||
913a571a PB |
1903 | static inline bool __io_fill_cqe(struct io_ring_ctx *ctx, u64 user_data, |
1904 | s32 res, u32 cflags) | |
2b188cc1 JA |
1905 | { |
1906 | struct io_uring_cqe *cqe; | |
1907 | ||
d4d19c19 | 1908 | trace_io_uring_complete(ctx, user_data, res, cflags); |
51c3ff62 | 1909 | |
2b188cc1 JA |
1910 | /* |
1911 | * If we can't get a cq entry, userspace overflowed the | |
1912 | * submission (by quite a lot). Increment the overflow count in | |
1913 | * the ring. | |
1914 | */ | |
d068b506 | 1915 | cqe = io_get_cqe(ctx); |
1d7bb1d5 | 1916 | if (likely(cqe)) { |
d4d19c19 | 1917 | WRITE_ONCE(cqe->user_data, user_data); |
2b188cc1 | 1918 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1919 | WRITE_ONCE(cqe->flags, cflags); |
8d13326e | 1920 | return true; |
2b188cc1 | 1921 | } |
d4d19c19 | 1922 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
2b188cc1 JA |
1923 | } |
1924 | ||
913a571a | 1925 | static noinline void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags) |
bcda7baa | 1926 | { |
04c76b41 PB |
1927 | if (!(req->flags & REQ_F_CQE_SKIP)) |
1928 | __io_fill_cqe(req->ctx, req->user_data, res, cflags); | |
bcda7baa JA |
1929 | } |
1930 | ||
913a571a PB |
1931 | static noinline bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data, |
1932 | s32 res, u32 cflags) | |
bcda7baa | 1933 | { |
913a571a PB |
1934 | ctx->cq_extra++; |
1935 | return __io_fill_cqe(ctx, user_data, res, cflags); | |
bcda7baa JA |
1936 | } |
1937 | ||
a37fae8a HX |
1938 | static void __io_req_complete_post(struct io_kiocb *req, s32 res, |
1939 | u32 cflags) | |
2b188cc1 | 1940 | { |
78e19bbe | 1941 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 | 1942 | |
04c76b41 PB |
1943 | if (!(req->flags & REQ_F_CQE_SKIP)) |
1944 | __io_fill_cqe(ctx, req->user_data, res, cflags); | |
c7dae4ba JA |
1945 | /* |
1946 | * If we're the last reference to this request, add to our locked | |
1947 | * free_list cache. | |
1948 | */ | |
de9b4cca | 1949 | if (req_ref_put_and_test(req)) { |
7a612350 | 1950 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
0756a869 | 1951 | if (req->flags & IO_DISARM_MASK) |
7a612350 PB |
1952 | io_disarm_next(req); |
1953 | if (req->link) { | |
1954 | io_req_task_queue(req->link); | |
1955 | req->link = NULL; | |
1956 | } | |
1957 | } | |
ab409402 | 1958 | io_req_put_rsrc(req, ctx); |
c7dae4ba JA |
1959 | io_dismantle_req(req); |
1960 | io_put_task(req->task, 1); | |
c2b6c6bc | 1961 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
d0acdee2 | 1962 | ctx->locked_free_nr++; |
180f829f | 1963 | } |
a37fae8a HX |
1964 | } |
1965 | ||
1966 | static void io_req_complete_post(struct io_kiocb *req, s32 res, | |
1967 | u32 cflags) | |
1968 | { | |
1969 | struct io_ring_ctx *ctx = req->ctx; | |
1970 | ||
1971 | spin_lock(&ctx->completion_lock); | |
1972 | __io_req_complete_post(req, res, cflags); | |
7a612350 | 1973 | io_commit_cqring(ctx); |
79ebeaee | 1974 | spin_unlock(&ctx->completion_lock); |
a3f34907 | 1975 | io_cqring_ev_posted(ctx); |
4e3d9ff9 JA |
1976 | } |
1977 | ||
54daa9b2 PB |
1978 | static inline void io_req_complete_state(struct io_kiocb *req, s32 res, |
1979 | u32 cflags) | |
229a7b63 | 1980 | { |
a38d68db | 1981 | req->result = res; |
d17e56eb | 1982 | req->cflags = cflags; |
e342c807 | 1983 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
1984 | } |
1985 | ||
889fca73 | 1986 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
54daa9b2 | 1987 | s32 res, u32 cflags) |
bcda7baa | 1988 | { |
889fca73 PB |
1989 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
1990 | io_req_complete_state(req, res, cflags); | |
a38d68db | 1991 | else |
c7dae4ba | 1992 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
1993 | } |
1994 | ||
54daa9b2 | 1995 | static inline void io_req_complete(struct io_kiocb *req, s32 res) |
0ddf92e8 | 1996 | { |
889fca73 | 1997 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
1998 | } |
1999 | ||
54daa9b2 | 2000 | static void io_req_complete_failed(struct io_kiocb *req, s32 res) |
f41db273 | 2001 | { |
93d2bcd2 | 2002 | req_set_fail(req); |
f41db273 PB |
2003 | io_req_complete_post(req, res, 0); |
2004 | } | |
2005 | ||
c6d3d9cb PB |
2006 | static void io_req_complete_fail_submit(struct io_kiocb *req) |
2007 | { | |
2008 | /* | |
2009 | * We don't submit, fail them all, for that replace hardlinks with | |
2010 | * normal links. Extra REQ_F_LINK is tolerated. | |
2011 | */ | |
2012 | req->flags &= ~REQ_F_HARDLINK; | |
2013 | req->flags |= REQ_F_LINK; | |
2014 | io_req_complete_failed(req, req->result); | |
2015 | } | |
2016 | ||
864ea921 PB |
2017 | /* |
2018 | * Don't initialise the fields below on every allocation, but do that in | |
2019 | * advance and keep them valid across allocations. | |
2020 | */ | |
2021 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
2022 | { | |
2023 | req->ctx = ctx; | |
2024 | req->link = NULL; | |
2025 | req->async_data = NULL; | |
2026 | /* not necessary, but safer to zero */ | |
2027 | req->result = 0; | |
2028 | } | |
2029 | ||
dac7a098 | 2030 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
cd0ca2e0 | 2031 | struct io_submit_state *state) |
dac7a098 | 2032 | { |
79ebeaee | 2033 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 2034 | wq_list_splice(&ctx->locked_free_list, &state->free_list); |
d0acdee2 | 2035 | ctx->locked_free_nr = 0; |
79ebeaee | 2036 | spin_unlock(&ctx->completion_lock); |
dac7a098 PB |
2037 | } |
2038 | ||
dd78f492 | 2039 | /* Returns true IFF there are requests in the cache */ |
c7dae4ba | 2040 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 2041 | { |
c7dae4ba | 2042 | struct io_submit_state *state = &ctx->submit_state; |
0ddf92e8 | 2043 | |
c7dae4ba JA |
2044 | /* |
2045 | * If we have more than a batch's worth of requests in our IRQ side | |
2046 | * locked cache, grab the lock and move them over to our submission | |
2047 | * side cache. | |
2048 | */ | |
d0acdee2 | 2049 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
cd0ca2e0 | 2050 | io_flush_cached_locked_reqs(ctx, state); |
c2b6c6bc | 2051 | return !!state->free_list.next; |
0ddf92e8 JA |
2052 | } |
2053 | ||
5d5901a3 PB |
2054 | /* |
2055 | * A request might get retired back into the request caches even before opcode | |
2056 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. | |
2057 | * Because of that, io_alloc_req() should be called only under ->uring_lock | |
2058 | * and with extra caution to not get a request that is still worked on. | |
2059 | */ | |
c072481d | 2060 | static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx) |
5d5901a3 | 2061 | __must_hold(&ctx->uring_lock) |
2b188cc1 | 2062 | { |
e5d1bc0a | 2063 | struct io_submit_state *state = &ctx->submit_state; |
864ea921 | 2064 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
3ab665b7 PB |
2065 | void *reqs[IO_REQ_ALLOC_BATCH]; |
2066 | struct io_kiocb *req; | |
864ea921 | 2067 | int ret, i; |
e5d1bc0a | 2068 | |
c2b6c6bc | 2069 | if (likely(state->free_list.next || io_flush_cached_reqs(ctx))) |
a33ae9ce | 2070 | return true; |
e5d1bc0a | 2071 | |
3ab665b7 | 2072 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs); |
fd6fab2c | 2073 | |
864ea921 PB |
2074 | /* |
2075 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
2076 | * retry single alloc to be on the safe side. | |
2077 | */ | |
2078 | if (unlikely(ret <= 0)) { | |
3ab665b7 PB |
2079 | reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
2080 | if (!reqs[0]) | |
a33ae9ce | 2081 | return false; |
864ea921 | 2082 | ret = 1; |
2b188cc1 | 2083 | } |
864ea921 | 2084 | |
37f0e767 | 2085 | percpu_ref_get_many(&ctx->refs, ret); |
3ab665b7 PB |
2086 | for (i = 0; i < ret; i++) { |
2087 | req = reqs[i]; | |
2088 | ||
2089 | io_preinit_req(req, ctx); | |
c2b6c6bc | 2090 | wq_stack_add_head(&req->comp_list, &state->free_list); |
3ab665b7 | 2091 | } |
a33ae9ce PB |
2092 | return true; |
2093 | } | |
2094 | ||
2095 | static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx) | |
2096 | { | |
2097 | if (unlikely(!ctx->submit_state.free_list.next)) | |
2098 | return __io_alloc_req_refill(ctx); | |
2099 | return true; | |
2100 | } | |
2101 | ||
2102 | static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) | |
2103 | { | |
2104 | struct io_wq_work_node *node; | |
2105 | ||
2106 | node = wq_stack_extract(&ctx->submit_state.free_list); | |
c2b6c6bc | 2107 | return container_of(node, struct io_kiocb, comp_list); |
2b188cc1 JA |
2108 | } |
2109 | ||
e1d767f0 | 2110 | static inline void io_put_file(struct file *file) |
8da11c19 | 2111 | { |
e1d767f0 | 2112 | if (file) |
8da11c19 PB |
2113 | fput(file); |
2114 | } | |
2115 | ||
6b639522 | 2116 | static inline void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 2117 | { |
094bae49 | 2118 | unsigned int flags = req->flags; |
929a3af9 | 2119 | |
867f8fa5 | 2120 | if (unlikely(flags & IO_REQ_CLEAN_FLAGS)) |
3a0a6902 | 2121 | io_clean_op(req); |
e1d767f0 PB |
2122 | if (!(flags & REQ_F_FIXED_FILE)) |
2123 | io_put_file(req->file); | |
e65ef56d JA |
2124 | } |
2125 | ||
c072481d | 2126 | static __cold void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 2127 | { |
51a4cc11 | 2128 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 2129 | |
ab409402 | 2130 | io_req_put_rsrc(req, ctx); |
216578e5 | 2131 | io_dismantle_req(req); |
7c660731 | 2132 | io_put_task(req->task, 1); |
c6ca97b3 | 2133 | |
79ebeaee | 2134 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 2135 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
c34b025f | 2136 | ctx->locked_free_nr++; |
79ebeaee | 2137 | spin_unlock(&ctx->completion_lock); |
e65ef56d JA |
2138 | } |
2139 | ||
f2f87370 PB |
2140 | static inline void io_remove_next_linked(struct io_kiocb *req) |
2141 | { | |
2142 | struct io_kiocb *nxt = req->link; | |
2143 | ||
2144 | req->link = nxt->link; | |
2145 | nxt->link = NULL; | |
2146 | } | |
2147 | ||
33cc89a9 PB |
2148 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
2149 | __must_hold(&req->ctx->completion_lock) | |
89b263f6 | 2150 | __must_hold(&req->ctx->timeout_lock) |
2665abfd | 2151 | { |
33cc89a9 | 2152 | struct io_kiocb *link = req->link; |
f2f87370 | 2153 | |
b97e736a | 2154 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
c9abd7ad | 2155 | struct io_timeout_data *io = link->async_data; |
7c86ffee | 2156 | |
f2f87370 | 2157 | io_remove_next_linked(req); |
90cd7e42 | 2158 | link->timeout.head = NULL; |
fd9c7bc5 | 2159 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
ef9dd637 | 2160 | list_del(&link->timeout.list); |
04c76b41 | 2161 | /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */ |
913a571a | 2162 | io_fill_cqe_req(link, -ECANCELED, 0); |
91c2f697 | 2163 | io_put_req_deferred(link); |
d4729fbd | 2164 | return true; |
c9abd7ad PB |
2165 | } |
2166 | } | |
d4729fbd | 2167 | return false; |
7c86ffee PB |
2168 | } |
2169 | ||
d148ca4b | 2170 | static void io_fail_links(struct io_kiocb *req) |
33cc89a9 | 2171 | __must_hold(&req->ctx->completion_lock) |
9e645e11 | 2172 | { |
33cc89a9 | 2173 | struct io_kiocb *nxt, *link = req->link; |
04c76b41 | 2174 | bool ignore_cqes = req->flags & REQ_F_SKIP_LINK_CQES; |
9e645e11 | 2175 | |
f2f87370 | 2176 | req->link = NULL; |
f2f87370 | 2177 | while (link) { |
a8295b98 HX |
2178 | long res = -ECANCELED; |
2179 | ||
2180 | if (link->flags & REQ_F_FAIL) | |
2181 | res = link->result; | |
2182 | ||
f2f87370 PB |
2183 | nxt = link->link; |
2184 | link->link = NULL; | |
2665abfd | 2185 | |
f2f87370 | 2186 | trace_io_uring_fail_link(req, link); |
04c76b41 PB |
2187 | if (!ignore_cqes) { |
2188 | link->flags &= ~REQ_F_CQE_SKIP; | |
2189 | io_fill_cqe_req(link, res, 0); | |
2190 | } | |
91c2f697 | 2191 | io_put_req_deferred(link); |
f2f87370 | 2192 | link = nxt; |
9e645e11 | 2193 | } |
33cc89a9 | 2194 | } |
9e645e11 | 2195 | |
33cc89a9 PB |
2196 | static bool io_disarm_next(struct io_kiocb *req) |
2197 | __must_hold(&req->ctx->completion_lock) | |
2198 | { | |
2199 | bool posted = false; | |
2200 | ||
0756a869 PB |
2201 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
2202 | struct io_kiocb *link = req->link; | |
2203 | ||
906c6caa | 2204 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
0756a869 PB |
2205 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
2206 | io_remove_next_linked(req); | |
04c76b41 | 2207 | /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */ |
913a571a | 2208 | io_fill_cqe_req(link, -ECANCELED, 0); |
0756a869 PB |
2209 | io_put_req_deferred(link); |
2210 | posted = true; | |
2211 | } | |
2212 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { | |
89b263f6 JA |
2213 | struct io_ring_ctx *ctx = req->ctx; |
2214 | ||
2215 | spin_lock_irq(&ctx->timeout_lock); | |
33cc89a9 | 2216 | posted = io_kill_linked_timeout(req); |
89b263f6 JA |
2217 | spin_unlock_irq(&ctx->timeout_lock); |
2218 | } | |
93d2bcd2 | 2219 | if (unlikely((req->flags & REQ_F_FAIL) && |
e4335ed3 | 2220 | !(req->flags & REQ_F_HARDLINK))) { |
33cc89a9 PB |
2221 | posted |= (req->link != NULL); |
2222 | io_fail_links(req); | |
2223 | } | |
2224 | return posted; | |
9e645e11 JA |
2225 | } |
2226 | ||
d81499bf PB |
2227 | static void __io_req_find_next_prep(struct io_kiocb *req) |
2228 | { | |
2229 | struct io_ring_ctx *ctx = req->ctx; | |
2230 | bool posted; | |
2231 | ||
2232 | spin_lock(&ctx->completion_lock); | |
2233 | posted = io_disarm_next(req); | |
2234 | if (posted) | |
33ce2aff | 2235 | io_commit_cqring(ctx); |
d81499bf PB |
2236 | spin_unlock(&ctx->completion_lock); |
2237 | if (posted) | |
2238 | io_cqring_ev_posted(ctx); | |
2239 | } | |
2240 | ||
2241 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) | |
c69f8dbe | 2242 | { |
33cc89a9 | 2243 | struct io_kiocb *nxt; |
944e58bf | 2244 | |
d81499bf PB |
2245 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
2246 | return NULL; | |
9e645e11 JA |
2247 | /* |
2248 | * If LINK is set, we have dependent requests in this chain. If we | |
2249 | * didn't fail this request, queue the first one up, moving any other | |
2250 | * dependencies to the next request. In case of failure, fail the rest | |
2251 | * of the chain. | |
2252 | */ | |
d81499bf PB |
2253 | if (unlikely(req->flags & IO_DISARM_MASK)) |
2254 | __io_req_find_next_prep(req); | |
33cc89a9 PB |
2255 | nxt = req->link; |
2256 | req->link = NULL; | |
2257 | return nxt; | |
4d7dd462 | 2258 | } |
9e645e11 | 2259 | |
f237c30a | 2260 | static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked) |
2c32395d PB |
2261 | { |
2262 | if (!ctx) | |
2263 | return; | |
f237c30a | 2264 | if (*locked) { |
c450178d | 2265 | io_submit_flush_completions(ctx); |
2c32395d | 2266 | mutex_unlock(&ctx->uring_lock); |
f237c30a | 2267 | *locked = false; |
2c32395d PB |
2268 | } |
2269 | percpu_ref_put(&ctx->refs); | |
2270 | } | |
2271 | ||
f28c240e HX |
2272 | static inline void ctx_commit_and_unlock(struct io_ring_ctx *ctx) |
2273 | { | |
2274 | io_commit_cqring(ctx); | |
2275 | spin_unlock(&ctx->completion_lock); | |
2276 | io_cqring_ev_posted(ctx); | |
2277 | } | |
2278 | ||
2279 | static void handle_prev_tw_list(struct io_wq_work_node *node, | |
2280 | struct io_ring_ctx **ctx, bool *uring_locked) | |
2281 | { | |
2282 | if (*ctx && !*uring_locked) | |
2283 | spin_lock(&(*ctx)->completion_lock); | |
2284 | ||
2285 | do { | |
2286 | struct io_wq_work_node *next = node->next; | |
2287 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2288 | io_task_work.node); | |
2289 | ||
2290 | if (req->ctx != *ctx) { | |
2291 | if (unlikely(!*uring_locked && *ctx)) | |
2292 | ctx_commit_and_unlock(*ctx); | |
2293 | ||
2294 | ctx_flush_and_put(*ctx, uring_locked); | |
2295 | *ctx = req->ctx; | |
2296 | /* if not contended, grab and improve batching */ | |
2297 | *uring_locked = mutex_trylock(&(*ctx)->uring_lock); | |
2298 | percpu_ref_get(&(*ctx)->refs); | |
2299 | if (unlikely(!*uring_locked)) | |
2300 | spin_lock(&(*ctx)->completion_lock); | |
2301 | } | |
2302 | if (likely(*uring_locked)) | |
2303 | req->io_task_work.func(req, uring_locked); | |
2304 | else | |
2305 | __io_req_complete_post(req, req->result, io_put_kbuf(req)); | |
2306 | node = next; | |
2307 | } while (node); | |
2308 | ||
2309 | if (unlikely(!*uring_locked)) | |
2310 | ctx_commit_and_unlock(*ctx); | |
2311 | } | |
2312 | ||
2313 | static void handle_tw_list(struct io_wq_work_node *node, | |
2314 | struct io_ring_ctx **ctx, bool *locked) | |
9f8d032a HX |
2315 | { |
2316 | do { | |
2317 | struct io_wq_work_node *next = node->next; | |
2318 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2319 | io_task_work.node); | |
2320 | ||
2321 | if (req->ctx != *ctx) { | |
2322 | ctx_flush_and_put(*ctx, locked); | |
2323 | *ctx = req->ctx; | |
2324 | /* if not contended, grab and improve batching */ | |
2325 | *locked = mutex_trylock(&(*ctx)->uring_lock); | |
2326 | percpu_ref_get(&(*ctx)->refs); | |
2327 | } | |
2328 | req->io_task_work.func(req, locked); | |
2329 | node = next; | |
2330 | } while (node); | |
2331 | } | |
2332 | ||
7cbf1722 | 2333 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 2334 | { |
f28c240e | 2335 | bool uring_locked = false; |
ebd0df2e | 2336 | struct io_ring_ctx *ctx = NULL; |
3f18407d PB |
2337 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
2338 | task_work); | |
c40f6379 | 2339 | |
16f72070 | 2340 | while (1) { |
f28c240e | 2341 | struct io_wq_work_node *node1, *node2; |
3f18407d | 2342 | |
f28c240e HX |
2343 | if (!tctx->task_list.first && |
2344 | !tctx->prior_task_list.first && uring_locked) | |
8d4ad41e PB |
2345 | io_submit_flush_completions(ctx); |
2346 | ||
3f18407d | 2347 | spin_lock_irq(&tctx->task_lock); |
f28c240e HX |
2348 | node1 = tctx->prior_task_list.first; |
2349 | node2 = tctx->task_list.first; | |
3f18407d | 2350 | INIT_WQ_LIST(&tctx->task_list); |
f28c240e HX |
2351 | INIT_WQ_LIST(&tctx->prior_task_list); |
2352 | if (!node2 && !node1) | |
6294f368 | 2353 | tctx->task_running = false; |
3f18407d | 2354 | spin_unlock_irq(&tctx->task_lock); |
f28c240e | 2355 | if (!node2 && !node1) |
6294f368 | 2356 | break; |
3f18407d | 2357 | |
f28c240e HX |
2358 | if (node1) |
2359 | handle_prev_tw_list(node1, &ctx, &uring_locked); | |
6294f368 | 2360 | |
f28c240e HX |
2361 | if (node2) |
2362 | handle_tw_list(node2, &ctx, &uring_locked); | |
7cbf1722 | 2363 | cond_resched(); |
3f18407d | 2364 | } |
ebd0df2e | 2365 | |
f28c240e | 2366 | ctx_flush_and_put(ctx, &uring_locked); |
3cc7fdb9 PB |
2367 | |
2368 | /* relaxed read is enough as only the task itself sets ->in_idle */ | |
2369 | if (unlikely(atomic_read(&tctx->in_idle))) | |
2370 | io_uring_drop_tctx_refs(current); | |
7cbf1722 JA |
2371 | } |
2372 | ||
4813c377 | 2373 | static void io_req_task_work_add(struct io_kiocb *req, bool priority) |
7cbf1722 | 2374 | { |
c15b79de | 2375 | struct task_struct *tsk = req->task; |
7cbf1722 | 2376 | struct io_uring_task *tctx = tsk->io_uring; |
c15b79de | 2377 | enum task_work_notify_mode notify; |
e09ee510 | 2378 | struct io_wq_work_node *node; |
0b81e80c | 2379 | unsigned long flags; |
6294f368 | 2380 | bool running; |
7cbf1722 JA |
2381 | |
2382 | WARN_ON_ONCE(!tctx); | |
2383 | ||
0b81e80c | 2384 | spin_lock_irqsave(&tctx->task_lock, flags); |
4813c377 HX |
2385 | if (priority) |
2386 | wq_list_add_tail(&req->io_task_work.node, &tctx->prior_task_list); | |
2387 | else | |
2388 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); | |
6294f368 PB |
2389 | running = tctx->task_running; |
2390 | if (!running) | |
2391 | tctx->task_running = true; | |
0b81e80c | 2392 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2393 | |
2394 | /* task_work already pending, we're done */ | |
6294f368 | 2395 | if (running) |
e09ee510 | 2396 | return; |
7cbf1722 | 2397 | |
c15b79de PB |
2398 | /* |
2399 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For | |
2400 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2401 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2402 | * will do the job. | |
2403 | */ | |
2404 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; | |
d97ec623 PB |
2405 | if (likely(!task_work_add(tsk, &tctx->task_work, notify))) { |
2406 | if (notify == TWA_NONE) | |
2407 | wake_up_process(tsk); | |
e09ee510 | 2408 | return; |
c15b79de | 2409 | } |
2215bed9 | 2410 | |
0b81e80c | 2411 | spin_lock_irqsave(&tctx->task_lock, flags); |
6294f368 | 2412 | tctx->task_running = false; |
4813c377 | 2413 | node = wq_list_merge(&tctx->prior_task_list, &tctx->task_list); |
0b81e80c | 2414 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 | 2415 | |
e09ee510 PB |
2416 | while (node) { |
2417 | req = container_of(node, struct io_kiocb, io_task_work.node); | |
2418 | node = node->next; | |
2419 | if (llist_add(&req->io_task_work.fallback_node, | |
2420 | &req->ctx->fallback_llist)) | |
2421 | schedule_delayed_work(&req->ctx->fallback_work, 1); | |
2422 | } | |
eab30c4d PB |
2423 | } |
2424 | ||
f237c30a | 2425 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
c40f6379 | 2426 | { |
87ceb6a6 | 2427 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2428 | |
b18a1a45 | 2429 | /* not needed for normal modes, but SQPOLL depends on it */ |
f237c30a | 2430 | io_tw_lock(ctx, locked); |
2593553a | 2431 | io_req_complete_failed(req, req->result); |
c40f6379 JA |
2432 | } |
2433 | ||
f237c30a | 2434 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
c40f6379 JA |
2435 | { |
2436 | struct io_ring_ctx *ctx = req->ctx; | |
2437 | ||
f237c30a | 2438 | io_tw_lock(ctx, locked); |
316319e8 | 2439 | /* req->task == current here, checking PF_EXITING is safe */ |
af066f31 | 2440 | if (likely(!(req->task->flags & PF_EXITING))) |
c5eef2b9 | 2441 | __io_queue_sqe(req); |
81b6d05c | 2442 | else |
2593553a | 2443 | io_req_complete_failed(req, -EFAULT); |
c40f6379 JA |
2444 | } |
2445 | ||
2c4b8eb6 | 2446 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
c40f6379 | 2447 | { |
2c4b8eb6 | 2448 | req->result = ret; |
5b0a6acc | 2449 | req->io_task_work.func = io_req_task_cancel; |
4813c377 | 2450 | io_req_task_work_add(req, false); |
c40f6379 JA |
2451 | } |
2452 | ||
2c4b8eb6 | 2453 | static void io_req_task_queue(struct io_kiocb *req) |
a3df7698 | 2454 | { |
5b0a6acc | 2455 | req->io_task_work.func = io_req_task_submit; |
4813c377 | 2456 | io_req_task_work_add(req, false); |
a3df7698 PB |
2457 | } |
2458 | ||
773af691 JA |
2459 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
2460 | { | |
2461 | req->io_task_work.func = io_queue_async_work; | |
4813c377 | 2462 | io_req_task_work_add(req, false); |
773af691 JA |
2463 | } |
2464 | ||
f2f87370 | 2465 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2466 | { |
9b5f7bd9 | 2467 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2468 | |
2469 | if (nxt) | |
906a8c3f | 2470 | io_req_task_queue(nxt); |
c69f8dbe JL |
2471 | } |
2472 | ||
c3524383 | 2473 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2474 | { |
c3524383 PB |
2475 | io_queue_next(req); |
2476 | __io_free_req(req); | |
2477 | } | |
8766dd51 | 2478 | |
f237c30a PB |
2479 | static void io_free_req_work(struct io_kiocb *req, bool *locked) |
2480 | { | |
2481 | io_free_req(req); | |
2482 | } | |
2483 | ||
3aa83bfb | 2484 | static void io_free_batch_list(struct io_ring_ctx *ctx, |
1cce17ac | 2485 | struct io_wq_work_node *node) |
3aa83bfb | 2486 | __must_hold(&ctx->uring_lock) |
5af1d13e | 2487 | { |
d4b7a5ef | 2488 | struct task_struct *task = NULL; |
37f0e767 | 2489 | int task_refs = 0; |
5af1d13e | 2490 | |
3aa83bfb PB |
2491 | do { |
2492 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2493 | comp_list); | |
2d6500d4 | 2494 | |
def77acf | 2495 | if (unlikely(req->flags & REQ_F_REFCOUNT)) { |
c1e53a69 | 2496 | node = req->comp_list.next; |
def77acf PB |
2497 | if (!req_ref_put_and_test(req)) |
2498 | continue; | |
c1e53a69 | 2499 | } |
2d6500d4 | 2500 | |
ab409402 | 2501 | io_req_put_rsrc_locked(req, ctx); |
d4b7a5ef PB |
2502 | io_queue_next(req); |
2503 | io_dismantle_req(req); | |
5af1d13e | 2504 | |
d4b7a5ef PB |
2505 | if (req->task != task) { |
2506 | if (task) | |
2507 | io_put_task(task, task_refs); | |
2508 | task = req->task; | |
2509 | task_refs = 0; | |
2510 | } | |
2511 | task_refs++; | |
c1e53a69 | 2512 | node = req->comp_list.next; |
d4b7a5ef | 2513 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
3aa83bfb | 2514 | } while (node); |
d4b7a5ef | 2515 | |
d4b7a5ef PB |
2516 | if (task) |
2517 | io_put_task(task, task_refs); | |
7a743e22 PB |
2518 | } |
2519 | ||
c450178d | 2520 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx) |
a141dd89 | 2521 | __must_hold(&ctx->uring_lock) |
905c172f | 2522 | { |
6f33b0bc | 2523 | struct io_wq_work_node *node, *prev; |
cd0ca2e0 | 2524 | struct io_submit_state *state = &ctx->submit_state; |
905c172f | 2525 | |
3d4aeb9f PB |
2526 | if (state->flush_cqes) { |
2527 | spin_lock(&ctx->completion_lock); | |
2528 | wq_list_for_each(node, prev, &state->compl_reqs) { | |
2529 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
6f33b0bc | 2530 | comp_list); |
5182ed2e | 2531 | |
3d4aeb9f PB |
2532 | if (!(req->flags & REQ_F_CQE_SKIP)) |
2533 | __io_fill_cqe(ctx, req->user_data, req->result, | |
2534 | req->cflags); | |
2535 | } | |
2536 | ||
2537 | io_commit_cqring(ctx); | |
2538 | spin_unlock(&ctx->completion_lock); | |
2539 | io_cqring_ev_posted(ctx); | |
2540 | state->flush_cqes = false; | |
905c172f | 2541 | } |
5182ed2e | 2542 | |
1cce17ac | 2543 | io_free_batch_list(ctx, state->compl_reqs.first); |
6f33b0bc | 2544 | INIT_WQ_LIST(&state->compl_reqs); |
7a743e22 PB |
2545 | } |
2546 | ||
ba816ad6 JA |
2547 | /* |
2548 | * Drop reference to request, return next in chain (if there is one) if this | |
2549 | * was the last reference to this request. | |
2550 | */ | |
0d85035a | 2551 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2552 | { |
9b5f7bd9 PB |
2553 | struct io_kiocb *nxt = NULL; |
2554 | ||
de9b4cca | 2555 | if (req_ref_put_and_test(req)) { |
9b5f7bd9 | 2556 | nxt = io_req_find_next(req); |
4d7dd462 | 2557 | __io_free_req(req); |
2a44f467 | 2558 | } |
9b5f7bd9 | 2559 | return nxt; |
2b188cc1 JA |
2560 | } |
2561 | ||
0d85035a | 2562 | static inline void io_put_req(struct io_kiocb *req) |
e65ef56d | 2563 | { |
de9b4cca | 2564 | if (req_ref_put_and_test(req)) |
e65ef56d | 2565 | io_free_req(req); |
2b188cc1 JA |
2566 | } |
2567 | ||
91c2f697 | 2568 | static inline void io_put_req_deferred(struct io_kiocb *req) |
216578e5 | 2569 | { |
91c2f697 | 2570 | if (req_ref_put_and_test(req)) { |
f237c30a | 2571 | req->io_task_work.func = io_free_req_work; |
4813c377 | 2572 | io_req_task_work_add(req, false); |
543af3a1 | 2573 | } |
216578e5 PB |
2574 | } |
2575 | ||
6c503150 | 2576 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2577 | { |
2578 | /* See comment at the top of this file */ | |
2579 | smp_rmb(); | |
e23de15f | 2580 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2581 | } |
2582 | ||
fb5ccc98 PB |
2583 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2584 | { | |
2585 | struct io_rings *rings = ctx->rings; | |
2586 | ||
2587 | /* make sure SQ entry isn't read before tail */ | |
2588 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2589 | } | |
2590 | ||
4c6e277c JA |
2591 | static inline bool io_run_task_work(void) |
2592 | { | |
ef98eb04 | 2593 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) { |
4c6e277c | 2594 | __set_current_state(TASK_RUNNING); |
ef98eb04 | 2595 | tracehook_notify_signal(); |
4c6e277c JA |
2596 | return true; |
2597 | } | |
2598 | ||
2599 | return false; | |
bcda7baa JA |
2600 | } |
2601 | ||
5ba3c874 | 2602 | static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) |
def596e9 | 2603 | { |
5eef4e87 | 2604 | struct io_wq_work_node *pos, *start, *prev; |
d729cf9a | 2605 | unsigned int poll_flags = BLK_POLL_NOSLEEP; |
b688f11e | 2606 | DEFINE_IO_COMP_BATCH(iob); |
5ba3c874 | 2607 | int nr_events = 0; |
def596e9 JA |
2608 | |
2609 | /* | |
2610 | * Only spin for completions if we don't have multiple devices hanging | |
87a115fb | 2611 | * off our complete list. |
def596e9 | 2612 | */ |
87a115fb | 2613 | if (ctx->poll_multi_queue || force_nonspin) |
ef99b2d3 | 2614 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2615 | |
5eef4e87 PB |
2616 | wq_list_for_each(pos, start, &ctx->iopoll_list) { |
2617 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
9adbd45d | 2618 | struct kiocb *kiocb = &req->rw.kiocb; |
a2416e1e | 2619 | int ret; |
def596e9 JA |
2620 | |
2621 | /* | |
581f9810 BM |
2622 | * Move completed and retryable entries to our local lists. |
2623 | * If we find a request that requires polling, break out | |
2624 | * and complete those lists first, if we have entries there. | |
def596e9 | 2625 | */ |
e3f721e6 | 2626 | if (READ_ONCE(req->iopoll_completed)) |
def596e9 JA |
2627 | break; |
2628 | ||
b688f11e | 2629 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags); |
a2416e1e PB |
2630 | if (unlikely(ret < 0)) |
2631 | return ret; | |
2632 | else if (ret) | |
ef99b2d3 | 2633 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2634 | |
3aadc23e | 2635 | /* iopoll may have completed current req */ |
b688f11e JA |
2636 | if (!rq_list_empty(iob.req_list) || |
2637 | READ_ONCE(req->iopoll_completed)) | |
e3f721e6 | 2638 | break; |
def596e9 JA |
2639 | } |
2640 | ||
b688f11e JA |
2641 | if (!rq_list_empty(iob.req_list)) |
2642 | iob.complete(&iob); | |
5eef4e87 PB |
2643 | else if (!pos) |
2644 | return 0; | |
def596e9 | 2645 | |
5eef4e87 PB |
2646 | prev = start; |
2647 | wq_list_for_each_resume(pos, prev) { | |
2648 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
2649 | ||
b3fa03fd PB |
2650 | /* order with io_complete_rw_iopoll(), e.g. ->result updates */ |
2651 | if (!smp_load_acquire(&req->iopoll_completed)) | |
e3f721e6 | 2652 | break; |
83a13a41 PB |
2653 | if (unlikely(req->flags & REQ_F_CQE_SKIP)) |
2654 | continue; | |
d1fd1c20 | 2655 | |
83a13a41 | 2656 | __io_fill_cqe(ctx, req->user_data, req->result, io_put_kbuf(req)); |
e3f721e6 PB |
2657 | nr_events++; |
2658 | } | |
def596e9 | 2659 | |
f5ed3bcd PB |
2660 | if (unlikely(!nr_events)) |
2661 | return 0; | |
2662 | ||
2663 | io_commit_cqring(ctx); | |
2664 | io_cqring_ev_posted_iopoll(ctx); | |
1cce17ac | 2665 | pos = start ? start->next : ctx->iopoll_list.first; |
5eef4e87 | 2666 | wq_list_cut(&ctx->iopoll_list, prev, start); |
1cce17ac | 2667 | io_free_batch_list(ctx, pos); |
5ba3c874 | 2668 | return nr_events; |
def596e9 JA |
2669 | } |
2670 | ||
def596e9 JA |
2671 | /* |
2672 | * We can't just wait for polled events to come to us, we have to actively | |
2673 | * find and complete them. | |
2674 | */ | |
c072481d | 2675 | static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2676 | { |
2677 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2678 | return; | |
2679 | ||
2680 | mutex_lock(&ctx->uring_lock); | |
5eef4e87 | 2681 | while (!wq_list_empty(&ctx->iopoll_list)) { |
b2edc0a7 | 2682 | /* let it sleep and repeat later if can't complete a request */ |
5ba3c874 | 2683 | if (io_do_iopoll(ctx, true) == 0) |
b2edc0a7 | 2684 | break; |
08f5439f JA |
2685 | /* |
2686 | * Ensure we allow local-to-the-cpu processing to take place, | |
2687 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2688 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2689 | */ |
3fcee5a6 PB |
2690 | if (need_resched()) { |
2691 | mutex_unlock(&ctx->uring_lock); | |
2692 | cond_resched(); | |
2693 | mutex_lock(&ctx->uring_lock); | |
2694 | } | |
def596e9 JA |
2695 | } |
2696 | mutex_unlock(&ctx->uring_lock); | |
2697 | } | |
2698 | ||
7668b92a | 2699 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2700 | { |
7668b92a | 2701 | unsigned int nr_events = 0; |
e9979b36 | 2702 | int ret = 0; |
500f9fba | 2703 | |
c7849be9 XW |
2704 | /* |
2705 | * We disallow the app entering submit/complete with polling, but we | |
2706 | * still need to lock the ring to prevent racing with polled issue | |
2707 | * that got punted to a workqueue. | |
2708 | */ | |
2709 | mutex_lock(&ctx->uring_lock); | |
f39c8a5b PB |
2710 | /* |
2711 | * Don't enter poll loop if we already have events pending. | |
2712 | * If we do, we can potentially be spinning for commands that | |
2713 | * already triggered a CQE (eg in error). | |
2714 | */ | |
5ed7a37d | 2715 | if (test_bit(0, &ctx->check_cq_overflow)) |
f39c8a5b PB |
2716 | __io_cqring_overflow_flush(ctx, false); |
2717 | if (io_cqring_events(ctx)) | |
2718 | goto out; | |
def596e9 | 2719 | do { |
500f9fba JA |
2720 | /* |
2721 | * If a submit got punted to a workqueue, we can have the | |
2722 | * application entering polling for a command before it gets | |
2723 | * issued. That app will hold the uring_lock for the duration | |
2724 | * of the poll right here, so we need to take a breather every | |
2725 | * now and then to ensure that the issue has a chance to add | |
2726 | * the poll to the issued list. Otherwise we can spin here | |
2727 | * forever, while the workqueue is stuck trying to acquire the | |
2728 | * very same mutex. | |
2729 | */ | |
5eef4e87 | 2730 | if (wq_list_empty(&ctx->iopoll_list)) { |
8f487ef2 PB |
2731 | u32 tail = ctx->cached_cq_tail; |
2732 | ||
500f9fba | 2733 | mutex_unlock(&ctx->uring_lock); |
4c6e277c | 2734 | io_run_task_work(); |
500f9fba | 2735 | mutex_lock(&ctx->uring_lock); |
def596e9 | 2736 | |
8f487ef2 PB |
2737 | /* some requests don't go through iopoll_list */ |
2738 | if (tail != ctx->cached_cq_tail || | |
5eef4e87 | 2739 | wq_list_empty(&ctx->iopoll_list)) |
e9979b36 | 2740 | break; |
500f9fba | 2741 | } |
5ba3c874 PB |
2742 | ret = io_do_iopoll(ctx, !min); |
2743 | if (ret < 0) | |
2744 | break; | |
2745 | nr_events += ret; | |
2746 | ret = 0; | |
2747 | } while (nr_events < min && !need_resched()); | |
f39c8a5b | 2748 | out: |
500f9fba | 2749 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2750 | return ret; |
2751 | } | |
2752 | ||
491381ce | 2753 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2754 | { |
491381ce JA |
2755 | /* |
2756 | * Tell lockdep we inherited freeze protection from submission | |
2757 | * thread. | |
2758 | */ | |
2759 | if (req->flags & REQ_F_ISREG) { | |
1c98679d | 2760 | struct super_block *sb = file_inode(req->file)->i_sb; |
2b188cc1 | 2761 | |
1c98679d PB |
2762 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
2763 | sb_end_write(sb); | |
2b188cc1 JA |
2764 | } |
2765 | } | |
2766 | ||
b63534c4 | 2767 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2768 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 | 2769 | { |
ab454438 | 2770 | struct io_async_rw *rw = req->async_data; |
b63534c4 | 2771 | |
d886e185 | 2772 | if (!req_has_async_data(req)) |
ab454438 | 2773 | return !io_req_prep_async(req); |
538941e2 | 2774 | iov_iter_restore(&rw->s.iter, &rw->s.iter_state); |
ab454438 | 2775 | return true; |
b63534c4 | 2776 | } |
b63534c4 | 2777 | |
3e6a0d3c | 2778 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 2779 | { |
355afaeb | 2780 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 2781 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 2782 | |
355afaeb JA |
2783 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2784 | return false; | |
3e6a0d3c JA |
2785 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
2786 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 2787 | return false; |
7c977a58 JA |
2788 | /* |
2789 | * If ref is dying, we might be running poll reap from the exit work. | |
2790 | * Don't attempt to reissue from that path, just let it fail with | |
2791 | * -EAGAIN. | |
2792 | */ | |
3e6a0d3c JA |
2793 | if (percpu_ref_is_dying(&ctx->refs)) |
2794 | return false; | |
ef046888 JA |
2795 | /* |
2796 | * Play it safe and assume not safe to re-import and reissue if we're | |
2797 | * not in the original thread group (or in task context). | |
2798 | */ | |
2799 | if (!same_thread_group(req->task, current) || !in_task()) | |
2800 | return false; | |
3e6a0d3c JA |
2801 | return true; |
2802 | } | |
e82ad485 | 2803 | #else |
a1ff1e3f | 2804 | static bool io_resubmit_prep(struct io_kiocb *req) |
e82ad485 JA |
2805 | { |
2806 | return false; | |
2807 | } | |
e82ad485 | 2808 | static bool io_rw_should_reissue(struct io_kiocb *req) |
3e6a0d3c | 2809 | { |
b63534c4 JA |
2810 | return false; |
2811 | } | |
3e6a0d3c | 2812 | #endif |
b63534c4 | 2813 | |
8ef12efe | 2814 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
a1d7c393 | 2815 | { |
b65c128f PB |
2816 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
2817 | kiocb_end_write(req); | |
258f3a7f | 2818 | if (unlikely(res != req->result)) { |
9532b99b PB |
2819 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && |
2820 | io_rw_should_reissue(req)) { | |
2821 | req->flags |= REQ_F_REISSUE; | |
8ef12efe | 2822 | return true; |
9532b99b | 2823 | } |
93d2bcd2 | 2824 | req_set_fail(req); |
8ef12efe | 2825 | req->result = res; |
9532b99b | 2826 | } |
8ef12efe JA |
2827 | return false; |
2828 | } | |
2829 | ||
cc8e9ba7 | 2830 | static inline void io_req_task_complete(struct io_kiocb *req, bool *locked) |
8ef12efe | 2831 | { |
d1fd1c20 | 2832 | unsigned int cflags = io_put_kbuf(req); |
54daa9b2 | 2833 | int res = req->result; |
126180b9 PB |
2834 | |
2835 | if (*locked) { | |
126180b9 | 2836 | io_req_complete_state(req, res, cflags); |
fff4e40e | 2837 | io_req_add_compl_list(req); |
126180b9 PB |
2838 | } else { |
2839 | io_req_complete_post(req, res, cflags); | |
2840 | } | |
8ef12efe JA |
2841 | } |
2842 | ||
00f6e68b | 2843 | static void __io_complete_rw(struct io_kiocb *req, long res, |
8ef12efe JA |
2844 | unsigned int issue_flags) |
2845 | { | |
2846 | if (__io_complete_rw_common(req, res)) | |
2847 | return; | |
d1fd1c20 | 2848 | __io_req_complete(req, issue_flags, req->result, io_put_kbuf(req)); |
ba816ad6 JA |
2849 | } |
2850 | ||
6b19b766 | 2851 | static void io_complete_rw(struct kiocb *kiocb, long res) |
ba816ad6 | 2852 | { |
9adbd45d | 2853 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2854 | |
8ef12efe JA |
2855 | if (__io_complete_rw_common(req, res)) |
2856 | return; | |
2857 | req->result = res; | |
2858 | req->io_task_work.func = io_req_task_complete; | |
f28c240e | 2859 | io_req_task_work_add(req, !!(req->ctx->flags & IORING_SETUP_SQPOLL)); |
2b188cc1 JA |
2860 | } |
2861 | ||
6b19b766 | 2862 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res) |
def596e9 | 2863 | { |
9adbd45d | 2864 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2865 | |
491381ce JA |
2866 | if (kiocb->ki_flags & IOCB_WRITE) |
2867 | kiocb_end_write(req); | |
9532b99b | 2868 | if (unlikely(res != req->result)) { |
b66ceaf3 PB |
2869 | if (res == -EAGAIN && io_rw_should_reissue(req)) { |
2870 | req->flags |= REQ_F_REISSUE; | |
2871 | return; | |
9532b99b | 2872 | } |
258f3a7f | 2873 | req->result = res; |
8c130827 | 2874 | } |
bbde017a | 2875 | |
b3fa03fd PB |
2876 | /* order with io_iopoll_complete() checking ->iopoll_completed */ |
2877 | smp_store_release(&req->iopoll_completed, 1); | |
def596e9 JA |
2878 | } |
2879 | ||
2880 | /* | |
2881 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2882 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
f39c8a5b | 2883 | * find it from a io_do_iopoll() thread before the issuer is done |
def596e9 JA |
2884 | * accessing the kiocb cookie. |
2885 | */ | |
9882131c | 2886 | static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags) |
def596e9 JA |
2887 | { |
2888 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 2889 | const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
cb3d8972 PB |
2890 | |
2891 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
3b44b371 | 2892 | if (unlikely(needs_lock)) |
cb3d8972 | 2893 | mutex_lock(&ctx->uring_lock); |
def596e9 JA |
2894 | |
2895 | /* | |
2896 | * Track whether we have multiple files in our lists. This will impact | |
2897 | * how we do polling eventually, not spinning if we're on potentially | |
2898 | * different devices. | |
2899 | */ | |
5eef4e87 | 2900 | if (wq_list_empty(&ctx->iopoll_list)) { |
915b3dde HX |
2901 | ctx->poll_multi_queue = false; |
2902 | } else if (!ctx->poll_multi_queue) { | |
def596e9 JA |
2903 | struct io_kiocb *list_req; |
2904 | ||
5eef4e87 PB |
2905 | list_req = container_of(ctx->iopoll_list.first, struct io_kiocb, |
2906 | comp_list); | |
30da1b45 | 2907 | if (list_req->file != req->file) |
915b3dde | 2908 | ctx->poll_multi_queue = true; |
def596e9 JA |
2909 | } |
2910 | ||
2911 | /* | |
2912 | * For fast devices, IO may have already completed. If it has, add | |
2913 | * it to the front so we find it first. | |
2914 | */ | |
65a6543d | 2915 | if (READ_ONCE(req->iopoll_completed)) |
5eef4e87 | 2916 | wq_list_add_head(&req->comp_list, &ctx->iopoll_list); |
def596e9 | 2917 | else |
5eef4e87 | 2918 | wq_list_add_tail(&req->comp_list, &ctx->iopoll_list); |
bdcd3eab | 2919 | |
3b44b371 | 2920 | if (unlikely(needs_lock)) { |
cb3d8972 PB |
2921 | /* |
2922 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle | |
2923 | * in sq thread task context or in io worker task context. If | |
2924 | * current task context is sq thread, we don't need to check | |
2925 | * whether should wake up sq thread. | |
2926 | */ | |
2927 | if ((ctx->flags & IORING_SETUP_SQPOLL) && | |
2928 | wq_has_sleeper(&ctx->sq_data->wait)) | |
2929 | wake_up(&ctx->sq_data->wait); | |
2930 | ||
2931 | mutex_unlock(&ctx->uring_lock); | |
2932 | } | |
def596e9 JA |
2933 | } |
2934 | ||
4503b767 JA |
2935 | static bool io_bdev_nowait(struct block_device *bdev) |
2936 | { | |
9ba0d0c8 | 2937 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2938 | } |
2939 | ||
2b188cc1 JA |
2940 | /* |
2941 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2942 | * any file. For now, just ensure that anything potentially problematic is done | |
2943 | * inline. | |
2944 | */ | |
88459b50 | 2945 | static bool __io_file_supports_nowait(struct file *file, umode_t mode) |
2b188cc1 | 2946 | { |
4503b767 | 2947 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2948 | if (IS_ENABLED(CONFIG_BLOCK) && |
2949 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2950 | return true; |
2951 | return false; | |
2952 | } | |
976517f1 | 2953 | if (S_ISSOCK(mode)) |
2b188cc1 | 2954 | return true; |
4503b767 | 2955 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2956 | if (IS_ENABLED(CONFIG_BLOCK) && |
2957 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2958 | file->f_op != &io_uring_fops) |
2959 | return true; | |
2960 | return false; | |
2961 | } | |
2b188cc1 | 2962 | |
c5b85625 JA |
2963 | /* any ->read/write should understand O_NONBLOCK */ |
2964 | if (file->f_flags & O_NONBLOCK) | |
2965 | return true; | |
35645ac3 | 2966 | return file->f_mode & FMODE_NOWAIT; |
2b188cc1 | 2967 | } |
c5b85625 | 2968 | |
88459b50 PB |
2969 | /* |
2970 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2971 | * any file. For now, just ensure that anything potentially problematic is done | |
2972 | * inline. | |
2973 | */ | |
2974 | static unsigned int io_file_get_flags(struct file *file) | |
2975 | { | |
2976 | umode_t mode = file_inode(file)->i_mode; | |
2977 | unsigned int res = 0; | |
af197f50 | 2978 | |
88459b50 PB |
2979 | if (S_ISREG(mode)) |
2980 | res |= FFS_ISREG; | |
2981 | if (__io_file_supports_nowait(file, mode)) | |
2982 | res |= FFS_NOWAIT; | |
2983 | return res; | |
2b188cc1 JA |
2984 | } |
2985 | ||
35645ac3 | 2986 | static inline bool io_file_supports_nowait(struct io_kiocb *req) |
7b29f92d | 2987 | { |
88459b50 | 2988 | return req->flags & REQ_F_SUPPORT_NOWAIT; |
7b29f92d JA |
2989 | } |
2990 | ||
b9a6b8f9 | 2991 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 2992 | { |
def596e9 | 2993 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2994 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 2995 | struct file *file = req->file; |
09bb8394 JA |
2996 | unsigned ioprio; |
2997 | int ret; | |
2b188cc1 | 2998 | |
88459b50 PB |
2999 | if (!io_req_ffs_set(req)) |
3000 | req->flags |= io_file_get_flags(file) << REQ_F_SUPPORT_NOWAIT_BIT; | |
491381ce | 3001 | |
2b188cc1 | 3002 | kiocb->ki_pos = READ_ONCE(sqe->off); |
7b9762a5 JA |
3003 | if (kiocb->ki_pos == -1) { |
3004 | if (!(file->f_mode & FMODE_STREAM)) { | |
3005 | req->flags |= REQ_F_CUR_POS; | |
3006 | kiocb->ki_pos = file->f_pos; | |
3007 | } else { | |
3008 | kiocb->ki_pos = 0; | |
3009 | } | |
ba04291e | 3010 | } |
5cb03d63 | 3011 | kiocb->ki_flags = iocb_flags(file); |
3e577dcd PB |
3012 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
3013 | if (unlikely(ret)) | |
3014 | return ret; | |
2b188cc1 | 3015 | |
5d329e12 JA |
3016 | /* |
3017 | * If the file is marked O_NONBLOCK, still allow retry for it if it | |
3018 | * supports async. Otherwise it's impossible to use O_NONBLOCK files | |
3019 | * reliably. If not, or it IOCB_NOWAIT is set, don't retry. | |
3020 | */ | |
3021 | if ((kiocb->ki_flags & IOCB_NOWAIT) || | |
35645ac3 | 3022 | ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req))) |
75c668cd PB |
3023 | req->flags |= REQ_F_NOWAIT; |
3024 | ||
def596e9 | 3025 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
5cb03d63 | 3026 | if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll) |
09bb8394 | 3027 | return -EOPNOTSUPP; |
2b188cc1 | 3028 | |
394918eb | 3029 | kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE; |
def596e9 | 3030 | kiocb->ki_complete = io_complete_rw_iopoll; |
65a6543d | 3031 | req->iopoll_completed = 0; |
def596e9 | 3032 | } else { |
09bb8394 JA |
3033 | if (kiocb->ki_flags & IOCB_HIPRI) |
3034 | return -EINVAL; | |
def596e9 JA |
3035 | kiocb->ki_complete = io_complete_rw; |
3036 | } | |
9adbd45d | 3037 | |
fb27274a PB |
3038 | ioprio = READ_ONCE(sqe->ioprio); |
3039 | if (ioprio) { | |
3040 | ret = ioprio_check_cap(ioprio); | |
3041 | if (ret) | |
3042 | return ret; | |
3043 | ||
3044 | kiocb->ki_ioprio = ioprio; | |
3045 | } else { | |
3046 | kiocb->ki_ioprio = get_current_ioprio(); | |
eae071c9 PB |
3047 | } |
3048 | ||
578c0ee2 | 3049 | req->imu = NULL; |
3529d8c2 JA |
3050 | req->rw.addr = READ_ONCE(sqe->addr); |
3051 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 3052 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 3053 | return 0; |
2b188cc1 JA |
3054 | } |
3055 | ||
3056 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
3057 | { | |
3058 | switch (ret) { | |
3059 | case -EIOCBQUEUED: | |
3060 | break; | |
3061 | case -ERESTARTSYS: | |
3062 | case -ERESTARTNOINTR: | |
3063 | case -ERESTARTNOHAND: | |
3064 | case -ERESTART_RESTARTBLOCK: | |
3065 | /* | |
3066 | * We can't just restart the syscall, since previously | |
3067 | * submitted sqes may already be in progress. Just fail this | |
3068 | * IO with EINTR. | |
3069 | */ | |
3070 | ret = -EINTR; | |
df561f66 | 3071 | fallthrough; |
2b188cc1 | 3072 | default: |
6b19b766 | 3073 | kiocb->ki_complete(kiocb, ret); |
2b188cc1 JA |
3074 | } |
3075 | } | |
3076 | ||
2ea537ca | 3077 | static void kiocb_done(struct io_kiocb *req, ssize_t ret, |
889fca73 | 3078 | unsigned int issue_flags) |
ba816ad6 | 3079 | { |
e8c2bc1f | 3080 | struct io_async_rw *io = req->async_data; |
ba04291e | 3081 | |
227c0c96 | 3082 | /* add previously done IO, if any */ |
d886e185 | 3083 | if (req_has_async_data(req) && io->bytes_done > 0) { |
227c0c96 | 3084 | if (ret < 0) |
e8c2bc1f | 3085 | ret = io->bytes_done; |
227c0c96 | 3086 | else |
e8c2bc1f | 3087 | ret += io->bytes_done; |
227c0c96 JA |
3088 | } |
3089 | ||
ba04291e | 3090 | if (req->flags & REQ_F_CUR_POS) |
2ea537ca PB |
3091 | req->file->f_pos = req->rw.kiocb.ki_pos; |
3092 | if (ret >= 0 && (req->rw.kiocb.ki_complete == io_complete_rw)) | |
00f6e68b | 3093 | __io_complete_rw(req, ret, issue_flags); |
ba816ad6 | 3094 | else |
2ea537ca | 3095 | io_rw_done(&req->rw.kiocb, ret); |
97284637 | 3096 | |
b66ceaf3 | 3097 | if (req->flags & REQ_F_REISSUE) { |
97284637 | 3098 | req->flags &= ~REQ_F_REISSUE; |
a7be7c23 | 3099 | if (io_resubmit_prep(req)) { |
773af691 | 3100 | io_req_task_queue_reissue(req); |
8c130827 | 3101 | } else { |
93d2bcd2 | 3102 | req_set_fail(req); |
06bdea20 PB |
3103 | req->result = ret; |
3104 | req->io_task_work.func = io_req_task_complete; | |
4813c377 | 3105 | io_req_task_work_add(req, false); |
97284637 PB |
3106 | } |
3107 | } | |
ba816ad6 JA |
3108 | } |
3109 | ||
eae071c9 PB |
3110 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
3111 | struct io_mapped_ubuf *imu) | |
edafccee | 3112 | { |
9adbd45d | 3113 | size_t len = req->rw.len; |
75769e3f | 3114 | u64 buf_end, buf_addr = req->rw.addr; |
edafccee | 3115 | size_t offset; |
edafccee | 3116 | |
75769e3f | 3117 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
edafccee JA |
3118 | return -EFAULT; |
3119 | /* not inside the mapped region */ | |
4751f53d | 3120 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
edafccee JA |
3121 | return -EFAULT; |
3122 | ||
3123 | /* | |
3124 | * May not be a start of buffer, set size appropriately | |
3125 | * and advance us to the beginning. | |
3126 | */ | |
3127 | offset = buf_addr - imu->ubuf; | |
3128 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
3129 | |
3130 | if (offset) { | |
3131 | /* | |
3132 | * Don't use iov_iter_advance() here, as it's really slow for | |
3133 | * using the latter parts of a big fixed buffer - it iterates | |
3134 | * over each segment manually. We can cheat a bit here, because | |
3135 | * we know that: | |
3136 | * | |
3137 | * 1) it's a BVEC iter, we set it up | |
3138 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
3139 | * first and last bvec | |
3140 | * | |
3141 | * So just find our index, and adjust the iterator afterwards. | |
3142 | * If the offset is within the first bvec (or the whole first | |
3143 | * bvec, just use iov_iter_advance(). This makes it easier | |
3144 | * since we can just skip the first segment, which may not | |
3145 | * be PAGE_SIZE aligned. | |
3146 | */ | |
3147 | const struct bio_vec *bvec = imu->bvec; | |
3148 | ||
3149 | if (offset <= bvec->bv_len) { | |
3150 | iov_iter_advance(iter, offset); | |
3151 | } else { | |
3152 | unsigned long seg_skip; | |
3153 | ||
3154 | /* skip first vec */ | |
3155 | offset -= bvec->bv_len; | |
3156 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
3157 | ||
3158 | iter->bvec = bvec + seg_skip; | |
3159 | iter->nr_segs -= seg_skip; | |
99c79f66 | 3160 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 3161 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
3162 | } |
3163 | } | |
3164 | ||
847595de | 3165 | return 0; |
edafccee JA |
3166 | } |
3167 | ||
eae071c9 PB |
3168 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
3169 | { | |
eae071c9 PB |
3170 | struct io_mapped_ubuf *imu = req->imu; |
3171 | u16 index, buf_index = req->buf_index; | |
3172 | ||
3173 | if (likely(!imu)) { | |
578c0ee2 PB |
3174 | struct io_ring_ctx *ctx = req->ctx; |
3175 | ||
eae071c9 PB |
3176 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
3177 | return -EFAULT; | |
578c0ee2 | 3178 | io_req_set_rsrc_node(req, ctx); |
eae071c9 PB |
3179 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
3180 | imu = READ_ONCE(ctx->user_bufs[index]); | |
3181 | req->imu = imu; | |
3182 | } | |
3183 | return __io_import_fixed(req, rw, iter, imu); | |
3184 | } | |
3185 | ||
bcda7baa JA |
3186 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
3187 | { | |
3188 | if (needs_lock) | |
3189 | mutex_unlock(&ctx->uring_lock); | |
3190 | } | |
3191 | ||
3192 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
3193 | { | |
3194 | /* | |
3195 | * "Normal" inline submissions always hold the uring_lock, since we | |
3196 | * grab it from the system call. Same is true for the SQPOLL offload. | |
3197 | * The only exception is when we've detached the request and issue it | |
3198 | * from an async worker thread, grab the lock for that case. | |
3199 | */ | |
3200 | if (needs_lock) | |
3201 | mutex_lock(&ctx->uring_lock); | |
3202 | } | |
3203 | ||
3204 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
51aac424 | 3205 | int bgid, unsigned int issue_flags) |
bcda7baa | 3206 | { |
30d51dd4 | 3207 | struct io_buffer *kbuf = req->kbuf; |
bcda7baa | 3208 | struct io_buffer *head; |
3b44b371 | 3209 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
bcda7baa JA |
3210 | |
3211 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3212 | return kbuf; | |
3213 | ||
3214 | io_ring_submit_lock(req->ctx, needs_lock); | |
3215 | ||
3216 | lockdep_assert_held(&req->ctx->uring_lock); | |
3217 | ||
9e15c3a0 | 3218 | head = xa_load(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
3219 | if (head) { |
3220 | if (!list_empty(&head->list)) { | |
3221 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
3222 | list); | |
3223 | list_del(&kbuf->list); | |
3224 | } else { | |
3225 | kbuf = head; | |
9e15c3a0 | 3226 | xa_erase(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
3227 | } |
3228 | if (*len > kbuf->len) | |
3229 | *len = kbuf->len; | |
30d51dd4 PB |
3230 | req->flags |= REQ_F_BUFFER_SELECTED; |
3231 | req->kbuf = kbuf; | |
bcda7baa JA |
3232 | } else { |
3233 | kbuf = ERR_PTR(-ENOBUFS); | |
3234 | } | |
3235 | ||
3236 | io_ring_submit_unlock(req->ctx, needs_lock); | |
bcda7baa JA |
3237 | return kbuf; |
3238 | } | |
3239 | ||
4d954c25 | 3240 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
51aac424 | 3241 | unsigned int issue_flags) |
4d954c25 JA |
3242 | { |
3243 | struct io_buffer *kbuf; | |
4f4eeba8 | 3244 | u16 bgid; |
4d954c25 | 3245 | |
4f4eeba8 | 3246 | bgid = req->buf_index; |
51aac424 | 3247 | kbuf = io_buffer_select(req, len, bgid, issue_flags); |
4d954c25 JA |
3248 | if (IS_ERR(kbuf)) |
3249 | return kbuf; | |
4d954c25 JA |
3250 | return u64_to_user_ptr(kbuf->addr); |
3251 | } | |
3252 | ||
3253 | #ifdef CONFIG_COMPAT | |
3254 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3255 | unsigned int issue_flags) |
4d954c25 JA |
3256 | { |
3257 | struct compat_iovec __user *uiov; | |
3258 | compat_ssize_t clen; | |
3259 | void __user *buf; | |
3260 | ssize_t len; | |
3261 | ||
3262 | uiov = u64_to_user_ptr(req->rw.addr); | |
3263 | if (!access_ok(uiov, sizeof(*uiov))) | |
3264 | return -EFAULT; | |
3265 | if (__get_user(clen, &uiov->iov_len)) | |
3266 | return -EFAULT; | |
3267 | if (clen < 0) | |
3268 | return -EINVAL; | |
3269 | ||
3270 | len = clen; | |
51aac424 | 3271 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3272 | if (IS_ERR(buf)) |
3273 | return PTR_ERR(buf); | |
3274 | iov[0].iov_base = buf; | |
3275 | iov[0].iov_len = (compat_size_t) len; | |
3276 | return 0; | |
3277 | } | |
3278 | #endif | |
3279 | ||
3280 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3281 | unsigned int issue_flags) |
4d954c25 JA |
3282 | { |
3283 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3284 | void __user *buf; | |
3285 | ssize_t len; | |
3286 | ||
3287 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3288 | return -EFAULT; | |
3289 | ||
3290 | len = iov[0].iov_len; | |
3291 | if (len < 0) | |
3292 | return -EINVAL; | |
51aac424 | 3293 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3294 | if (IS_ERR(buf)) |
3295 | return PTR_ERR(buf); | |
3296 | iov[0].iov_base = buf; | |
3297 | iov[0].iov_len = len; | |
3298 | return 0; | |
3299 | } | |
3300 | ||
3301 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3302 | unsigned int issue_flags) |
4d954c25 | 3303 | { |
dddb3e26 | 3304 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
30d51dd4 | 3305 | struct io_buffer *kbuf = req->kbuf; |
dddb3e26 | 3306 | |
dddb3e26 JA |
3307 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
3308 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3309 | return 0; |
dddb3e26 | 3310 | } |
dd201662 | 3311 | if (req->rw.len != 1) |
4d954c25 JA |
3312 | return -EINVAL; |
3313 | ||
3314 | #ifdef CONFIG_COMPAT | |
3315 | if (req->ctx->compat) | |
51aac424 | 3316 | return io_compat_import(req, iov, issue_flags); |
4d954c25 JA |
3317 | #endif |
3318 | ||
51aac424 | 3319 | return __io_iov_buffer_select(req, iov, issue_flags); |
4d954c25 JA |
3320 | } |
3321 | ||
caa8fe6e PB |
3322 | static struct iovec *__io_import_iovec(int rw, struct io_kiocb *req, |
3323 | struct io_rw_state *s, | |
3324 | unsigned int issue_flags) | |
2b188cc1 | 3325 | { |
5e49c973 | 3326 | struct iov_iter *iter = &s->iter; |
847595de | 3327 | u8 opcode = req->opcode; |
caa8fe6e | 3328 | struct iovec *iovec; |
d1d681b0 PB |
3329 | void __user *buf; |
3330 | size_t sqe_len; | |
4d954c25 | 3331 | ssize_t ret; |
edafccee | 3332 | |
f3251183 PB |
3333 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
3334 | ret = io_import_fixed(req, rw, iter); | |
3335 | if (ret) | |
3336 | return ERR_PTR(ret); | |
3337 | return NULL; | |
3338 | } | |
2b188cc1 | 3339 | |
bcda7baa | 3340 | /* buffer index only valid with fixed read/write, or buffer select */ |
d1d681b0 | 3341 | if (unlikely(req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))) |
caa8fe6e | 3342 | return ERR_PTR(-EINVAL); |
9adbd45d | 3343 | |
d1d681b0 PB |
3344 | buf = u64_to_user_ptr(req->rw.addr); |
3345 | sqe_len = req->rw.len; | |
9adbd45d | 3346 | |
3a6820f2 | 3347 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3348 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 3349 | buf = io_rw_buffer_select(req, &sqe_len, issue_flags); |
867a23ea | 3350 | if (IS_ERR(buf)) |
898df244 | 3351 | return ERR_CAST(buf); |
3f9d6441 | 3352 | req->rw.len = sqe_len; |
bcda7baa JA |
3353 | } |
3354 | ||
5e49c973 | 3355 | ret = import_single_range(rw, buf, sqe_len, s->fast_iov, iter); |
f3251183 PB |
3356 | if (ret) |
3357 | return ERR_PTR(ret); | |
3358 | return NULL; | |
3a6820f2 JA |
3359 | } |
3360 | ||
caa8fe6e | 3361 | iovec = s->fast_iov; |
4d954c25 | 3362 | if (req->flags & REQ_F_BUFFER_SELECT) { |
caa8fe6e | 3363 | ret = io_iov_buffer_select(req, iovec, issue_flags); |
f3251183 PB |
3364 | if (ret) |
3365 | return ERR_PTR(ret); | |
3366 | iov_iter_init(iter, rw, iovec, 1, iovec->iov_len); | |
3367 | return NULL; | |
4d954c25 JA |
3368 | } |
3369 | ||
caa8fe6e | 3370 | ret = __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, &iovec, iter, |
89cd35c5 | 3371 | req->ctx->compat); |
caa8fe6e PB |
3372 | if (unlikely(ret < 0)) |
3373 | return ERR_PTR(ret); | |
3374 | return iovec; | |
2b188cc1 JA |
3375 | } |
3376 | ||
5e49c973 PB |
3377 | static inline int io_import_iovec(int rw, struct io_kiocb *req, |
3378 | struct iovec **iovec, struct io_rw_state *s, | |
3379 | unsigned int issue_flags) | |
3380 | { | |
caa8fe6e PB |
3381 | *iovec = __io_import_iovec(rw, req, s, issue_flags); |
3382 | if (unlikely(IS_ERR(*iovec))) | |
3383 | return PTR_ERR(*iovec); | |
5e49c973 | 3384 | |
5e49c973 | 3385 | iov_iter_save_state(&s->iter, &s->iter_state); |
caa8fe6e | 3386 | return 0; |
2b188cc1 JA |
3387 | } |
3388 | ||
0fef9483 JA |
3389 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3390 | { | |
5b09e37e | 3391 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3392 | } |
3393 | ||
31b51510 | 3394 | /* |
32960613 JA |
3395 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3396 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3397 | */ |
4017eb91 | 3398 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3399 | { |
4017eb91 JA |
3400 | struct kiocb *kiocb = &req->rw.kiocb; |
3401 | struct file *file = req->file; | |
32960613 JA |
3402 | ssize_t ret = 0; |
3403 | ||
3404 | /* | |
3405 | * Don't support polled IO through this interface, and we can't | |
3406 | * support non-blocking either. For the latter, this just causes | |
3407 | * the kiocb to be handled from an async context. | |
3408 | */ | |
3409 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3410 | return -EOPNOTSUPP; | |
35645ac3 PB |
3411 | if ((kiocb->ki_flags & IOCB_NOWAIT) && |
3412 | !(kiocb->ki_filp->f_flags & O_NONBLOCK)) | |
32960613 JA |
3413 | return -EAGAIN; |
3414 | ||
3415 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3416 | struct iovec iovec; |
32960613 JA |
3417 | ssize_t nr; |
3418 | ||
311ae9e1 PB |
3419 | if (!iov_iter_is_bvec(iter)) { |
3420 | iovec = iov_iter_iovec(iter); | |
3421 | } else { | |
4017eb91 JA |
3422 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3423 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3424 | } |
3425 | ||
32960613 JA |
3426 | if (rw == READ) { |
3427 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3428 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3429 | } else { |
3430 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3431 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3432 | } |
3433 | ||
3434 | if (nr < 0) { | |
3435 | if (!ret) | |
3436 | ret = nr; | |
3437 | break; | |
3438 | } | |
16c8d2df JA |
3439 | if (!iov_iter_is_bvec(iter)) { |
3440 | iov_iter_advance(iter, nr); | |
3441 | } else { | |
3442 | req->rw.len -= nr; | |
3443 | req->rw.addr += nr; | |
3444 | } | |
32960613 JA |
3445 | ret += nr; |
3446 | if (nr != iovec.iov_len) | |
3447 | break; | |
32960613 JA |
3448 | } |
3449 | ||
3450 | return ret; | |
3451 | } | |
3452 | ||
ff6165b2 JA |
3453 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3454 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3455 | { |
e8c2bc1f | 3456 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3457 | |
538941e2 | 3458 | memcpy(&rw->s.iter, iter, sizeof(*iter)); |
afb87658 | 3459 | rw->free_iovec = iovec; |
227c0c96 | 3460 | rw->bytes_done = 0; |
ff6165b2 | 3461 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3462 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3463 | return; |
b64e3444 | 3464 | if (!iovec) { |
ff6165b2 JA |
3465 | unsigned iov_off = 0; |
3466 | ||
538941e2 | 3467 | rw->s.iter.iov = rw->s.fast_iov; |
ff6165b2 JA |
3468 | if (iter->iov != fast_iov) { |
3469 | iov_off = iter->iov - fast_iov; | |
538941e2 | 3470 | rw->s.iter.iov += iov_off; |
ff6165b2 | 3471 | } |
538941e2 PB |
3472 | if (rw->s.fast_iov != fast_iov) |
3473 | memcpy(rw->s.fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3474 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3475 | } else { |
3476 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3477 | } |
3478 | } | |
3479 | ||
8d4af685 | 3480 | static inline bool io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3481 | { |
e8c2bc1f JA |
3482 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3483 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
d886e185 PB |
3484 | if (req->async_data) { |
3485 | req->flags |= REQ_F_ASYNC_DATA; | |
3486 | return false; | |
3487 | } | |
3488 | return true; | |
3d9932a8 XW |
3489 | } |
3490 | ||
ff6165b2 | 3491 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
c88598a9 | 3492 | struct io_rw_state *s, bool force) |
b7bb4f7d | 3493 | { |
26f0505a | 3494 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
74566df3 | 3495 | return 0; |
d886e185 | 3496 | if (!req_has_async_data(req)) { |
cd658695 JA |
3497 | struct io_async_rw *iorw; |
3498 | ||
6cb78689 | 3499 | if (io_alloc_async_data(req)) { |
6bf985dc | 3500 | kfree(iovec); |
5d204bcf | 3501 | return -ENOMEM; |
6bf985dc | 3502 | } |
b7bb4f7d | 3503 | |
c88598a9 | 3504 | io_req_map_rw(req, iovec, s->fast_iov, &s->iter); |
cd658695 JA |
3505 | iorw = req->async_data; |
3506 | /* we've copied and mapped the iter, ensure state is saved */ | |
538941e2 | 3507 | iov_iter_save_state(&iorw->s.iter, &iorw->s.iter_state); |
5d204bcf | 3508 | } |
b7bb4f7d | 3509 | return 0; |
f67676d1 JA |
3510 | } |
3511 | ||
73debe68 | 3512 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3513 | { |
e8c2bc1f | 3514 | struct io_async_rw *iorw = req->async_data; |
5e49c973 | 3515 | struct iovec *iov; |
847595de | 3516 | int ret; |
c3e330a4 | 3517 | |
51aac424 | 3518 | /* submission path, ->uring_lock should already be taken */ |
3b44b371 | 3519 | ret = io_import_iovec(rw, req, &iov, &iorw->s, 0); |
c3e330a4 PB |
3520 | if (unlikely(ret < 0)) |
3521 | return ret; | |
3522 | ||
ab0b196c PB |
3523 | iorw->bytes_done = 0; |
3524 | iorw->free_iovec = iov; | |
3525 | if (iov) | |
3526 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3527 | return 0; |
3528 | } | |
3529 | ||
73debe68 | 3530 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3531 | { |
3529d8c2 JA |
3532 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3533 | return -EBADF; | |
b9a6b8f9 | 3534 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3535 | } |
3536 | ||
c1dd91d1 | 3537 | /* |
ffdc8dab | 3538 | * This is our waitqueue callback handler, registered through __folio_lock_async() |
c1dd91d1 JA |
3539 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
3540 | * This gets called when the page is unlocked, and we generally expect that to | |
3541 | * happen when the page IO is completed and the page is now uptodate. This will | |
3542 | * queue a task_work based retry of the operation, attempting to copy the data | |
3543 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3544 | * do a thread based blocking retry of the operation. That's the unexpected | |
3545 | * slow path. | |
3546 | */ | |
bcf5a063 JA |
3547 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3548 | int sync, void *arg) | |
3549 | { | |
3550 | struct wait_page_queue *wpq; | |
3551 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3552 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3553 | |
3554 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3555 | ||
cdc8fcb4 LT |
3556 | if (!wake_page_match(wpq, key)) |
3557 | return 0; | |
3558 | ||
c8d317aa | 3559 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 | 3560 | list_del_init(&wait->entry); |
921b9054 | 3561 | io_req_task_queue(req); |
bcf5a063 JA |
3562 | return 1; |
3563 | } | |
3564 | ||
c1dd91d1 JA |
3565 | /* |
3566 | * This controls whether a given IO request should be armed for async page | |
3567 | * based retry. If we return false here, the request is handed to the async | |
3568 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3569 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3570 | * will either succeed because the page is now uptodate and unlocked, or it | |
3571 | * will register a callback when the page is unlocked at IO completion. Through | |
3572 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3573 | * That retry will attempt the buffered read again. The retry will generally | |
3574 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3575 | * async worker threads for a blocking retry. | |
3576 | */ | |
227c0c96 | 3577 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3578 | { |
e8c2bc1f JA |
3579 | struct io_async_rw *rw = req->async_data; |
3580 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3581 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3582 | |
bcf5a063 JA |
3583 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3584 | if (req->flags & REQ_F_NOWAIT) | |
3585 | return false; | |
f67676d1 | 3586 | |
227c0c96 | 3587 | /* Only for buffered IO */ |
3b2a4439 | 3588 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3589 | return false; |
3b2a4439 | 3590 | |
bcf5a063 JA |
3591 | /* |
3592 | * just use poll if we can, and don't attempt if the fs doesn't | |
3593 | * support callback based unlocks | |
3594 | */ | |
3595 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3596 | return false; | |
f67676d1 | 3597 | |
3b2a4439 JA |
3598 | wait->wait.func = io_async_buf_func; |
3599 | wait->wait.private = req; | |
3600 | wait->wait.flags = 0; | |
3601 | INIT_LIST_HEAD(&wait->wait.entry); | |
3602 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3603 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3604 | kiocb->ki_waitq = wait; |
3b2a4439 | 3605 | return true; |
bcf5a063 JA |
3606 | } |
3607 | ||
aeab9506 | 3608 | static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
bcf5a063 | 3609 | { |
607b6fb8 | 3610 | if (likely(req->file->f_op->read_iter)) |
bcf5a063 | 3611 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
2dd2111d | 3612 | else if (req->file->f_op->read) |
4017eb91 | 3613 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3614 | else |
3615 | return -EINVAL; | |
f67676d1 JA |
3616 | } |
3617 | ||
7db30437 ML |
3618 | static bool need_read_all(struct io_kiocb *req) |
3619 | { | |
3620 | return req->flags & REQ_F_ISREG || | |
3621 | S_ISBLK(file_inode(req->file)->i_mode); | |
3622 | } | |
3623 | ||
889fca73 | 3624 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 3625 | { |
607b6fb8 | 3626 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 3627 | struct iovec *iovec; |
9adbd45d | 3628 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 3629 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
d886e185 | 3630 | struct io_async_rw *rw; |
cd658695 | 3631 | ssize_t ret, ret2; |
ff6165b2 | 3632 | |
607b6fb8 PB |
3633 | if (!req_has_async_data(req)) { |
3634 | ret = io_import_iovec(READ, req, &iovec, s, issue_flags); | |
3635 | if (unlikely(ret < 0)) | |
3636 | return ret; | |
3637 | } else { | |
d886e185 | 3638 | rw = req->async_data; |
c88598a9 | 3639 | s = &rw->s; |
cd658695 JA |
3640 | /* |
3641 | * We come here from an earlier attempt, restore our state to | |
3642 | * match in case it doesn't. It's cheap enough that we don't | |
3643 | * need to make this conditional. | |
3644 | */ | |
c88598a9 | 3645 | iov_iter_restore(&s->iter, &s->iter_state); |
2846c481 | 3646 | iovec = NULL; |
2846c481 | 3647 | } |
c88598a9 | 3648 | req->result = iov_iter_count(&s->iter); |
2b188cc1 | 3649 | |
607b6fb8 PB |
3650 | if (force_nonblock) { |
3651 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 3652 | if (unlikely(!io_file_supports_nowait(req))) { |
607b6fb8 PB |
3653 | ret = io_setup_async_rw(req, iovec, s, true); |
3654 | return ret ?: -EAGAIN; | |
3655 | } | |
a88fc400 | 3656 | kiocb->ki_flags |= IOCB_NOWAIT; |
607b6fb8 PB |
3657 | } else { |
3658 | /* Ensure we clear previously set non-block flag */ | |
3659 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
6713e7a6 | 3660 | } |
9e645e11 | 3661 | |
cd658695 | 3662 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), req->result); |
5ea5dd45 PB |
3663 | if (unlikely(ret)) { |
3664 | kfree(iovec); | |
3665 | return ret; | |
3666 | } | |
2b188cc1 | 3667 | |
c88598a9 | 3668 | ret = io_iter_do_read(req, &s->iter); |
32960613 | 3669 | |
230d50d4 | 3670 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
6ad7f233 | 3671 | req->flags &= ~REQ_F_REISSUE; |
eefdf30f JA |
3672 | /* IOPOLL retry should happen for io-wq threads */ |
3673 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3674 | goto done; |
75c668cd PB |
3675 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3676 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3677 | goto done; |
f38c7e3a | 3678 | ret = 0; |
230d50d4 JA |
3679 | } else if (ret == -EIOCBQUEUED) { |
3680 | goto out_free; | |
f80a50a6 | 3681 | } else if (ret == req->result || ret <= 0 || !force_nonblock || |
7db30437 | 3682 | (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) { |
7335e3bf | 3683 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3684 | goto done; |
227c0c96 JA |
3685 | } |
3686 | ||
cd658695 JA |
3687 | /* |
3688 | * Don't depend on the iter state matching what was consumed, or being | |
3689 | * untouched in case of error. Restore it and we'll advance it | |
3690 | * manually if we need to. | |
3691 | */ | |
c88598a9 | 3692 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 3693 | |
c88598a9 | 3694 | ret2 = io_setup_async_rw(req, iovec, s, true); |
6bf985dc PB |
3695 | if (ret2) |
3696 | return ret2; | |
3697 | ||
fe1cdd55 | 3698 | iovec = NULL; |
e8c2bc1f | 3699 | rw = req->async_data; |
c88598a9 | 3700 | s = &rw->s; |
cd658695 JA |
3701 | /* |
3702 | * Now use our persistent iterator and state, if we aren't already. | |
3703 | * We've restored and mapped the iter to match. | |
3704 | */ | |
227c0c96 | 3705 | |
b23df91b | 3706 | do { |
cd658695 JA |
3707 | /* |
3708 | * We end up here because of a partial read, either from | |
3709 | * above or inside this loop. Advance the iter by the bytes | |
3710 | * that were consumed. | |
3711 | */ | |
c88598a9 PB |
3712 | iov_iter_advance(&s->iter, ret); |
3713 | if (!iov_iter_count(&s->iter)) | |
cd658695 | 3714 | break; |
b23df91b | 3715 | rw->bytes_done += ret; |
c88598a9 | 3716 | iov_iter_save_state(&s->iter, &s->iter_state); |
cd658695 | 3717 | |
b23df91b PB |
3718 | /* if we can retry, do so with the callbacks armed */ |
3719 | if (!io_rw_should_retry(req)) { | |
3720 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3721 | return -EAGAIN; | |
3722 | } | |
3723 | ||
3724 | /* | |
3725 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3726 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3727 | * desired page gets unlocked. We can also get a partial read | |
3728 | * here, and if we do, then just retry at the new offset. | |
3729 | */ | |
c88598a9 | 3730 | ret = io_iter_do_read(req, &s->iter); |
b23df91b PB |
3731 | if (ret == -EIOCBQUEUED) |
3732 | return 0; | |
227c0c96 | 3733 | /* we got some bytes, but not all. retry. */ |
b5b0ecb7 | 3734 | kiocb->ki_flags &= ~IOCB_WAITQ; |
c88598a9 | 3735 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 3736 | } while (ret > 0); |
227c0c96 | 3737 | done: |
2ea537ca | 3738 | kiocb_done(req, ret, issue_flags); |
fe1cdd55 PB |
3739 | out_free: |
3740 | /* it's faster to check here then delegate to kfree */ | |
3741 | if (iovec) | |
3742 | kfree(iovec); | |
5ea5dd45 | 3743 | return 0; |
2b188cc1 JA |
3744 | } |
3745 | ||
73debe68 | 3746 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3747 | { |
3529d8c2 JA |
3748 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3749 | return -EBADF; | |
3884b83d | 3750 | req->rw.kiocb.ki_hint = ki_hint_validate(file_write_hint(req->file)); |
b9a6b8f9 | 3751 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3752 | } |
3753 | ||
889fca73 | 3754 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 3755 | { |
607b6fb8 | 3756 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 3757 | struct iovec *iovec; |
9adbd45d | 3758 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 3759 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
cd658695 | 3760 | ssize_t ret, ret2; |
2b188cc1 | 3761 | |
607b6fb8 | 3762 | if (!req_has_async_data(req)) { |
5e49c973 PB |
3763 | ret = io_import_iovec(WRITE, req, &iovec, s, issue_flags); |
3764 | if (unlikely(ret < 0)) | |
2846c481 | 3765 | return ret; |
607b6fb8 PB |
3766 | } else { |
3767 | struct io_async_rw *rw = req->async_data; | |
3768 | ||
3769 | s = &rw->s; | |
3770 | iov_iter_restore(&s->iter, &s->iter_state); | |
2846c481 | 3771 | iovec = NULL; |
2846c481 | 3772 | } |
c88598a9 | 3773 | req->result = iov_iter_count(&s->iter); |
2b188cc1 | 3774 | |
607b6fb8 PB |
3775 | if (force_nonblock) { |
3776 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 3777 | if (unlikely(!io_file_supports_nowait(req))) |
607b6fb8 | 3778 | goto copy_iov; |
fd6c2e4c | 3779 | |
607b6fb8 PB |
3780 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3781 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3782 | (req->flags & REQ_F_ISREG)) | |
3783 | goto copy_iov; | |
31b51510 | 3784 | |
607b6fb8 PB |
3785 | kiocb->ki_flags |= IOCB_NOWAIT; |
3786 | } else { | |
3787 | /* Ensure we clear previously set non-block flag */ | |
3788 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
3789 | } | |
31b51510 | 3790 | |
cd658695 | 3791 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), req->result); |
fa15bafb PB |
3792 | if (unlikely(ret)) |
3793 | goto out_free; | |
4ed734b0 | 3794 | |
fa15bafb PB |
3795 | /* |
3796 | * Open-code file_start_write here to grab freeze protection, | |
3797 | * which will be released by another thread in | |
3798 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3799 | * released so that it doesn't complain about the held lock when | |
3800 | * we return to userspace. | |
3801 | */ | |
3802 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3803 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3804 | __sb_writers_release(file_inode(req->file)->i_sb, |
3805 | SB_FREEZE_WRITE); | |
3806 | } | |
3807 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3808 | |
35645ac3 | 3809 | if (likely(req->file->f_op->write_iter)) |
c88598a9 | 3810 | ret2 = call_write_iter(req->file, kiocb, &s->iter); |
2dd2111d | 3811 | else if (req->file->f_op->write) |
c88598a9 | 3812 | ret2 = loop_rw_iter(WRITE, req, &s->iter); |
2dd2111d GH |
3813 | else |
3814 | ret2 = -EINVAL; | |
4ed734b0 | 3815 | |
6ad7f233 PB |
3816 | if (req->flags & REQ_F_REISSUE) { |
3817 | req->flags &= ~REQ_F_REISSUE; | |
230d50d4 | 3818 | ret2 = -EAGAIN; |
6ad7f233 | 3819 | } |
230d50d4 | 3820 | |
fa15bafb PB |
3821 | /* |
3822 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3823 | * retry them without IOCB_NOWAIT. | |
3824 | */ | |
3825 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3826 | ret2 = -EAGAIN; | |
75c668cd PB |
3827 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3828 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3829 | goto done; |
fa15bafb | 3830 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f | 3831 | /* IOPOLL retry should happen for io-wq threads */ |
b10841c9 | 3832 | if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL)) |
eefdf30f | 3833 | goto copy_iov; |
355afaeb | 3834 | done: |
2ea537ca | 3835 | kiocb_done(req, ret2, issue_flags); |
fa15bafb | 3836 | } else { |
f67676d1 | 3837 | copy_iov: |
c88598a9 PB |
3838 | iov_iter_restore(&s->iter, &s->iter_state); |
3839 | ret = io_setup_async_rw(req, iovec, s, false); | |
6bf985dc | 3840 | return ret ?: -EAGAIN; |
2b188cc1 | 3841 | } |
31b51510 | 3842 | out_free: |
f261c168 | 3843 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3844 | if (iovec) |
6f2cc166 | 3845 | kfree(iovec); |
2b188cc1 JA |
3846 | return ret; |
3847 | } | |
3848 | ||
80a261fd JA |
3849 | static int io_renameat_prep(struct io_kiocb *req, |
3850 | const struct io_uring_sqe *sqe) | |
3851 | { | |
3852 | struct io_rename *ren = &req->rename; | |
3853 | const char __user *oldf, *newf; | |
3854 | ||
ed7eb259 JA |
3855 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3856 | return -EINVAL; | |
26578cda | 3857 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
ed7eb259 | 3858 | return -EINVAL; |
80a261fd JA |
3859 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3860 | return -EBADF; | |
3861 | ||
3862 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3863 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3864 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3865 | ren->new_dfd = READ_ONCE(sqe->len); | |
3866 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3867 | ||
3868 | ren->oldpath = getname(oldf); | |
3869 | if (IS_ERR(ren->oldpath)) | |
3870 | return PTR_ERR(ren->oldpath); | |
3871 | ||
3872 | ren->newpath = getname(newf); | |
3873 | if (IS_ERR(ren->newpath)) { | |
3874 | putname(ren->oldpath); | |
3875 | return PTR_ERR(ren->newpath); | |
3876 | } | |
3877 | ||
3878 | req->flags |= REQ_F_NEED_CLEANUP; | |
3879 | return 0; | |
3880 | } | |
3881 | ||
45d189c6 | 3882 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
3883 | { |
3884 | struct io_rename *ren = &req->rename; | |
3885 | int ret; | |
3886 | ||
45d189c6 | 3887 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
3888 | return -EAGAIN; |
3889 | ||
3890 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3891 | ren->newpath, ren->flags); | |
3892 | ||
3893 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3894 | if (ret < 0) | |
93d2bcd2 | 3895 | req_set_fail(req); |
80a261fd JA |
3896 | io_req_complete(req, ret); |
3897 | return 0; | |
3898 | } | |
3899 | ||
14a1143b JA |
3900 | static int io_unlinkat_prep(struct io_kiocb *req, |
3901 | const struct io_uring_sqe *sqe) | |
3902 | { | |
3903 | struct io_unlink *un = &req->unlink; | |
3904 | const char __user *fname; | |
3905 | ||
22634bc5 JA |
3906 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3907 | return -EINVAL; | |
26578cda PB |
3908 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
3909 | sqe->splice_fd_in) | |
22634bc5 | 3910 | return -EINVAL; |
14a1143b JA |
3911 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3912 | return -EBADF; | |
3913 | ||
3914 | un->dfd = READ_ONCE(sqe->fd); | |
3915 | ||
3916 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3917 | if (un->flags & ~AT_REMOVEDIR) | |
3918 | return -EINVAL; | |
3919 | ||
3920 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3921 | un->filename = getname(fname); | |
3922 | if (IS_ERR(un->filename)) | |
3923 | return PTR_ERR(un->filename); | |
3924 | ||
3925 | req->flags |= REQ_F_NEED_CLEANUP; | |
3926 | return 0; | |
3927 | } | |
3928 | ||
45d189c6 | 3929 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
3930 | { |
3931 | struct io_unlink *un = &req->unlink; | |
3932 | int ret; | |
3933 | ||
45d189c6 | 3934 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
3935 | return -EAGAIN; |
3936 | ||
3937 | if (un->flags & AT_REMOVEDIR) | |
3938 | ret = do_rmdir(un->dfd, un->filename); | |
3939 | else | |
3940 | ret = do_unlinkat(un->dfd, un->filename); | |
3941 | ||
3942 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3943 | if (ret < 0) | |
93d2bcd2 | 3944 | req_set_fail(req); |
14a1143b JA |
3945 | io_req_complete(req, ret); |
3946 | return 0; | |
3947 | } | |
3948 | ||
e34a02dc DK |
3949 | static int io_mkdirat_prep(struct io_kiocb *req, |
3950 | const struct io_uring_sqe *sqe) | |
3951 | { | |
3952 | struct io_mkdir *mkd = &req->mkdir; | |
3953 | const char __user *fname; | |
3954 | ||
3955 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3956 | return -EINVAL; | |
3957 | if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index || | |
3958 | sqe->splice_fd_in) | |
3959 | return -EINVAL; | |
3960 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3961 | return -EBADF; | |
3962 | ||
3963 | mkd->dfd = READ_ONCE(sqe->fd); | |
3964 | mkd->mode = READ_ONCE(sqe->len); | |
3965 | ||
3966 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3967 | mkd->filename = getname(fname); | |
3968 | if (IS_ERR(mkd->filename)) | |
3969 | return PTR_ERR(mkd->filename); | |
3970 | ||
3971 | req->flags |= REQ_F_NEED_CLEANUP; | |
3972 | return 0; | |
3973 | } | |
3974 | ||
04f34081 | 3975 | static int io_mkdirat(struct io_kiocb *req, unsigned int issue_flags) |
e34a02dc DK |
3976 | { |
3977 | struct io_mkdir *mkd = &req->mkdir; | |
3978 | int ret; | |
3979 | ||
3980 | if (issue_flags & IO_URING_F_NONBLOCK) | |
3981 | return -EAGAIN; | |
3982 | ||
3983 | ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode); | |
3984 | ||
3985 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3986 | if (ret < 0) | |
3987 | req_set_fail(req); | |
3988 | io_req_complete(req, ret); | |
3989 | return 0; | |
3990 | } | |
3991 | ||
7a8721f8 DK |
3992 | static int io_symlinkat_prep(struct io_kiocb *req, |
3993 | const struct io_uring_sqe *sqe) | |
3994 | { | |
3995 | struct io_symlink *sl = &req->symlink; | |
3996 | const char __user *oldpath, *newpath; | |
3997 | ||
3998 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3999 | return -EINVAL; | |
4000 | if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index || | |
4001 | sqe->splice_fd_in) | |
4002 | return -EINVAL; | |
4003 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4004 | return -EBADF; | |
4005 | ||
4006 | sl->new_dfd = READ_ONCE(sqe->fd); | |
4007 | oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4008 | newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
4009 | ||
4010 | sl->oldpath = getname(oldpath); | |
4011 | if (IS_ERR(sl->oldpath)) | |
4012 | return PTR_ERR(sl->oldpath); | |
4013 | ||
4014 | sl->newpath = getname(newpath); | |
4015 | if (IS_ERR(sl->newpath)) { | |
4016 | putname(sl->oldpath); | |
4017 | return PTR_ERR(sl->newpath); | |
4018 | } | |
4019 | ||
4020 | req->flags |= REQ_F_NEED_CLEANUP; | |
4021 | return 0; | |
4022 | } | |
4023 | ||
04f34081 | 4024 | static int io_symlinkat(struct io_kiocb *req, unsigned int issue_flags) |
7a8721f8 DK |
4025 | { |
4026 | struct io_symlink *sl = &req->symlink; | |
4027 | int ret; | |
4028 | ||
4029 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4030 | return -EAGAIN; | |
4031 | ||
4032 | ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath); | |
4033 | ||
4034 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4035 | if (ret < 0) | |
4036 | req_set_fail(req); | |
4037 | io_req_complete(req, ret); | |
4038 | return 0; | |
4039 | } | |
4040 | ||
cf30da90 DK |
4041 | static int io_linkat_prep(struct io_kiocb *req, |
4042 | const struct io_uring_sqe *sqe) | |
4043 | { | |
4044 | struct io_hardlink *lnk = &req->hardlink; | |
4045 | const char __user *oldf, *newf; | |
4046 | ||
4047 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4048 | return -EINVAL; | |
4049 | if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) | |
4050 | return -EINVAL; | |
4051 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4052 | return -EBADF; | |
4053 | ||
4054 | lnk->old_dfd = READ_ONCE(sqe->fd); | |
4055 | lnk->new_dfd = READ_ONCE(sqe->len); | |
4056 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4057 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
4058 | lnk->flags = READ_ONCE(sqe->hardlink_flags); | |
4059 | ||
4060 | lnk->oldpath = getname(oldf); | |
4061 | if (IS_ERR(lnk->oldpath)) | |
4062 | return PTR_ERR(lnk->oldpath); | |
4063 | ||
4064 | lnk->newpath = getname(newf); | |
4065 | if (IS_ERR(lnk->newpath)) { | |
4066 | putname(lnk->oldpath); | |
4067 | return PTR_ERR(lnk->newpath); | |
4068 | } | |
4069 | ||
4070 | req->flags |= REQ_F_NEED_CLEANUP; | |
4071 | return 0; | |
4072 | } | |
4073 | ||
04f34081 | 4074 | static int io_linkat(struct io_kiocb *req, unsigned int issue_flags) |
cf30da90 DK |
4075 | { |
4076 | struct io_hardlink *lnk = &req->hardlink; | |
4077 | int ret; | |
4078 | ||
4079 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4080 | return -EAGAIN; | |
4081 | ||
4082 | ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd, | |
4083 | lnk->newpath, lnk->flags); | |
4084 | ||
4085 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4086 | if (ret < 0) | |
4087 | req_set_fail(req); | |
4088 | io_req_complete(req, ret); | |
4089 | return 0; | |
4090 | } | |
4091 | ||
36f4fa68 JA |
4092 | static int io_shutdown_prep(struct io_kiocb *req, |
4093 | const struct io_uring_sqe *sqe) | |
4094 | { | |
4095 | #if defined(CONFIG_NET) | |
4096 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4097 | return -EINVAL; | |
26578cda PB |
4098 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
4099 | sqe->buf_index || sqe->splice_fd_in)) | |
36f4fa68 JA |
4100 | return -EINVAL; |
4101 | ||
4102 | req->shutdown.how = READ_ONCE(sqe->len); | |
4103 | return 0; | |
4104 | #else | |
4105 | return -EOPNOTSUPP; | |
4106 | #endif | |
4107 | } | |
4108 | ||
45d189c6 | 4109 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
4110 | { |
4111 | #if defined(CONFIG_NET) | |
4112 | struct socket *sock; | |
4113 | int ret; | |
4114 | ||
45d189c6 | 4115 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
4116 | return -EAGAIN; |
4117 | ||
48aba79b | 4118 | sock = sock_from_file(req->file); |
36f4fa68 | 4119 | if (unlikely(!sock)) |
48aba79b | 4120 | return -ENOTSOCK; |
36f4fa68 JA |
4121 | |
4122 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d | 4123 | if (ret < 0) |
93d2bcd2 | 4124 | req_set_fail(req); |
36f4fa68 JA |
4125 | io_req_complete(req, ret); |
4126 | return 0; | |
4127 | #else | |
4128 | return -EOPNOTSUPP; | |
4129 | #endif | |
4130 | } | |
4131 | ||
f2a8d5c7 PB |
4132 | static int __io_splice_prep(struct io_kiocb *req, |
4133 | const struct io_uring_sqe *sqe) | |
7d67af2c | 4134 | { |
fe7e3257 | 4135 | struct io_splice *sp = &req->splice; |
7d67af2c | 4136 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
7d67af2c | 4137 | |
3232dd02 PB |
4138 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4139 | return -EINVAL; | |
7d67af2c PB |
4140 | |
4141 | sp->file_in = NULL; | |
7d67af2c PB |
4142 | sp->len = READ_ONCE(sqe->len); |
4143 | sp->flags = READ_ONCE(sqe->splice_flags); | |
4144 | ||
4145 | if (unlikely(sp->flags & ~valid_flags)) | |
4146 | return -EINVAL; | |
4147 | ||
62906e89 | 4148 | sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in), |
8371adf5 PB |
4149 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
4150 | if (!sp->file_in) | |
4151 | return -EBADF; | |
7d67af2c | 4152 | req->flags |= REQ_F_NEED_CLEANUP; |
7d67af2c PB |
4153 | return 0; |
4154 | } | |
4155 | ||
f2a8d5c7 PB |
4156 | static int io_tee_prep(struct io_kiocb *req, |
4157 | const struct io_uring_sqe *sqe) | |
4158 | { | |
4159 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
4160 | return -EINVAL; | |
4161 | return __io_splice_prep(req, sqe); | |
4162 | } | |
4163 | ||
45d189c6 | 4164 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
4165 | { |
4166 | struct io_splice *sp = &req->splice; | |
4167 | struct file *in = sp->file_in; | |
4168 | struct file *out = sp->file_out; | |
4169 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
4170 | long ret = 0; | |
4171 | ||
45d189c6 | 4172 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
4173 | return -EAGAIN; |
4174 | if (sp->len) | |
4175 | ret = do_tee(in, out, sp->len, flags); | |
4176 | ||
e1d767f0 PB |
4177 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4178 | io_put_file(in); | |
f2a8d5c7 PB |
4179 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4180 | ||
f2a8d5c7 | 4181 | if (ret != sp->len) |
93d2bcd2 | 4182 | req_set_fail(req); |
e1e16097 | 4183 | io_req_complete(req, ret); |
f2a8d5c7 PB |
4184 | return 0; |
4185 | } | |
4186 | ||
4187 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4188 | { | |
fe7e3257 | 4189 | struct io_splice *sp = &req->splice; |
f2a8d5c7 PB |
4190 | |
4191 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
4192 | sp->off_out = READ_ONCE(sqe->off); | |
4193 | return __io_splice_prep(req, sqe); | |
4194 | } | |
4195 | ||
45d189c6 | 4196 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
4197 | { |
4198 | struct io_splice *sp = &req->splice; | |
4199 | struct file *in = sp->file_in; | |
4200 | struct file *out = sp->file_out; | |
4201 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
4202 | loff_t *poff_in, *poff_out; | |
c9687426 | 4203 | long ret = 0; |
7d67af2c | 4204 | |
45d189c6 | 4205 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 4206 | return -EAGAIN; |
7d67af2c PB |
4207 | |
4208 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
4209 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 4210 | |
948a7749 | 4211 | if (sp->len) |
c9687426 | 4212 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c | 4213 | |
e1d767f0 PB |
4214 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4215 | io_put_file(in); | |
7d67af2c PB |
4216 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4217 | ||
7d67af2c | 4218 | if (ret != sp->len) |
93d2bcd2 | 4219 | req_set_fail(req); |
e1e16097 | 4220 | io_req_complete(req, ret); |
7d67af2c PB |
4221 | return 0; |
4222 | } | |
4223 | ||
2b188cc1 JA |
4224 | /* |
4225 | * IORING_OP_NOP just posts a completion event, nothing else. | |
4226 | */ | |
889fca73 | 4227 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
4228 | { |
4229 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 4230 | |
def596e9 JA |
4231 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4232 | return -EINVAL; | |
4233 | ||
889fca73 | 4234 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
4235 | return 0; |
4236 | } | |
4237 | ||
1155c76a | 4238 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 4239 | { |
6b06314c | 4240 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 4241 | |
09bb8394 JA |
4242 | if (!req->file) |
4243 | return -EBADF; | |
c992fe29 | 4244 | |
6b06314c | 4245 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 4246 | return -EINVAL; |
26578cda PB |
4247 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4248 | sqe->splice_fd_in)) | |
c992fe29 CH |
4249 | return -EINVAL; |
4250 | ||
8ed8d3c3 JA |
4251 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
4252 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
4253 | return -EINVAL; | |
4254 | ||
4255 | req->sync.off = READ_ONCE(sqe->off); | |
4256 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
4257 | return 0; |
4258 | } | |
4259 | ||
45d189c6 | 4260 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4261 | { |
8ed8d3c3 | 4262 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
4263 | int ret; |
4264 | ||
ac45abc0 | 4265 | /* fsync always requires a blocking context */ |
45d189c6 | 4266 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4267 | return -EAGAIN; |
4268 | ||
9adbd45d | 4269 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
4270 | end > 0 ? end : LLONG_MAX, |
4271 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
4272 | if (ret < 0) | |
93d2bcd2 | 4273 | req_set_fail(req); |
e1e16097 | 4274 | io_req_complete(req, ret); |
c992fe29 CH |
4275 | return 0; |
4276 | } | |
4277 | ||
d63d1b5e JA |
4278 | static int io_fallocate_prep(struct io_kiocb *req, |
4279 | const struct io_uring_sqe *sqe) | |
4280 | { | |
26578cda PB |
4281 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
4282 | sqe->splice_fd_in) | |
d63d1b5e | 4283 | return -EINVAL; |
3232dd02 PB |
4284 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4285 | return -EINVAL; | |
d63d1b5e JA |
4286 | |
4287 | req->sync.off = READ_ONCE(sqe->off); | |
4288 | req->sync.len = READ_ONCE(sqe->addr); | |
4289 | req->sync.mode = READ_ONCE(sqe->len); | |
4290 | return 0; | |
4291 | } | |
4292 | ||
45d189c6 | 4293 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 4294 | { |
ac45abc0 PB |
4295 | int ret; |
4296 | ||
d63d1b5e | 4297 | /* fallocate always requiring blocking context */ |
45d189c6 | 4298 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 4299 | return -EAGAIN; |
ac45abc0 PB |
4300 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
4301 | req->sync.len); | |
ac45abc0 | 4302 | if (ret < 0) |
93d2bcd2 | 4303 | req_set_fail(req); |
e1e16097 | 4304 | io_req_complete(req, ret); |
5d17b4a4 JA |
4305 | return 0; |
4306 | } | |
4307 | ||
ec65fea5 | 4308 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 4309 | { |
f8748881 | 4310 | const char __user *fname; |
15b71abe | 4311 | int ret; |
b7bb4f7d | 4312 | |
d3fddf6d PB |
4313 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4314 | return -EINVAL; | |
b9445598 | 4315 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 4316 | return -EINVAL; |
ec65fea5 | 4317 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 4318 | return -EBADF; |
03b1230c | 4319 | |
ec65fea5 PB |
4320 | /* open.how should be already initialised */ |
4321 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 4322 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 4323 | |
25e72d10 PB |
4324 | req->open.dfd = READ_ONCE(sqe->fd); |
4325 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 4326 | req->open.filename = getname(fname); |
15b71abe JA |
4327 | if (IS_ERR(req->open.filename)) { |
4328 | ret = PTR_ERR(req->open.filename); | |
4329 | req->open.filename = NULL; | |
4330 | return ret; | |
4331 | } | |
b9445598 PB |
4332 | |
4333 | req->open.file_slot = READ_ONCE(sqe->file_index); | |
4334 | if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) | |
4335 | return -EINVAL; | |
4336 | ||
4022e7af | 4337 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 4338 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 4339 | return 0; |
03b1230c JA |
4340 | } |
4341 | ||
ec65fea5 PB |
4342 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4343 | { | |
d3fddf6d PB |
4344 | u64 mode = READ_ONCE(sqe->len); |
4345 | u64 flags = READ_ONCE(sqe->open_flags); | |
ec65fea5 | 4346 | |
ec65fea5 PB |
4347 | req->open.how = build_open_how(flags, mode); |
4348 | return __io_openat_prep(req, sqe); | |
4349 | } | |
4350 | ||
cebdb986 | 4351 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 4352 | { |
cebdb986 | 4353 | struct open_how __user *how; |
cebdb986 | 4354 | size_t len; |
0fa03c62 JA |
4355 | int ret; |
4356 | ||
cebdb986 JA |
4357 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4358 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
4359 | if (len < OPEN_HOW_SIZE_VER0) |
4360 | return -EINVAL; | |
3529d8c2 | 4361 | |
cebdb986 JA |
4362 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
4363 | len); | |
4364 | if (ret) | |
4365 | return ret; | |
3529d8c2 | 4366 | |
ec65fea5 | 4367 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
4368 | } |
4369 | ||
45d189c6 | 4370 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
4371 | { |
4372 | struct open_flags op; | |
15b71abe | 4373 | struct file *file; |
b9445598 PB |
4374 | bool resolve_nonblock, nonblock_set; |
4375 | bool fixed = !!req->open.file_slot; | |
15b71abe JA |
4376 | int ret; |
4377 | ||
cebdb986 | 4378 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
4379 | if (ret) |
4380 | goto err; | |
3a81fd02 JA |
4381 | nonblock_set = op.open_flag & O_NONBLOCK; |
4382 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 4383 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
4384 | /* |
4385 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
4386 | * it'll always -EAGAIN | |
4387 | */ | |
4388 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
4389 | return -EAGAIN; | |
4390 | op.lookup_flags |= LOOKUP_CACHED; | |
4391 | op.open_flag |= O_NONBLOCK; | |
4392 | } | |
15b71abe | 4393 | |
b9445598 PB |
4394 | if (!fixed) { |
4395 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); | |
4396 | if (ret < 0) | |
4397 | goto err; | |
4398 | } | |
15b71abe JA |
4399 | |
4400 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
12dcb58a | 4401 | if (IS_ERR(file)) { |
944d1444 | 4402 | /* |
12dcb58a PB |
4403 | * We could hang on to this 'fd' on retrying, but seems like |
4404 | * marginal gain for something that is now known to be a slower | |
4405 | * path. So just put it, and we'll get a new one when we retry. | |
944d1444 | 4406 | */ |
b9445598 PB |
4407 | if (!fixed) |
4408 | put_unused_fd(ret); | |
3a81fd02 | 4409 | |
15b71abe | 4410 | ret = PTR_ERR(file); |
12dcb58a PB |
4411 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
4412 | if (ret == -EAGAIN && | |
4413 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) | |
4414 | return -EAGAIN; | |
4415 | goto err; | |
15b71abe | 4416 | } |
12dcb58a PB |
4417 | |
4418 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) | |
4419 | file->f_flags &= ~O_NONBLOCK; | |
4420 | fsnotify_open(file); | |
b9445598 PB |
4421 | |
4422 | if (!fixed) | |
4423 | fd_install(ret, file); | |
4424 | else | |
4425 | ret = io_install_fixed_file(req, file, issue_flags, | |
4426 | req->open.file_slot - 1); | |
15b71abe JA |
4427 | err: |
4428 | putname(req->open.filename); | |
8fef80bf | 4429 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe | 4430 | if (ret < 0) |
93d2bcd2 | 4431 | req_set_fail(req); |
0bdf3398 | 4432 | __io_req_complete(req, issue_flags, ret, 0); |
15b71abe JA |
4433 | return 0; |
4434 | } | |
4435 | ||
45d189c6 | 4436 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 4437 | { |
e45cff58 | 4438 | return io_openat2(req, issue_flags); |
cebdb986 JA |
4439 | } |
4440 | ||
067524e9 JA |
4441 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4442 | const struct io_uring_sqe *sqe) | |
4443 | { | |
4444 | struct io_provide_buf *p = &req->pbuf; | |
4445 | u64 tmp; | |
4446 | ||
26578cda PB |
4447 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
4448 | sqe->splice_fd_in) | |
067524e9 JA |
4449 | return -EINVAL; |
4450 | ||
4451 | tmp = READ_ONCE(sqe->fd); | |
4452 | if (!tmp || tmp > USHRT_MAX) | |
4453 | return -EINVAL; | |
4454 | ||
4455 | memset(p, 0, sizeof(*p)); | |
4456 | p->nbufs = tmp; | |
4457 | p->bgid = READ_ONCE(sqe->buf_group); | |
4458 | return 0; | |
4459 | } | |
4460 | ||
4461 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
4462 | int bgid, unsigned nbufs) | |
4463 | { | |
4464 | unsigned i = 0; | |
4465 | ||
4466 | /* shouldn't happen */ | |
4467 | if (!nbufs) | |
4468 | return 0; | |
4469 | ||
4470 | /* the head kbuf is the list itself */ | |
4471 | while (!list_empty(&buf->list)) { | |
4472 | struct io_buffer *nxt; | |
4473 | ||
4474 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
4475 | list_del(&nxt->list); | |
4476 | kfree(nxt); | |
4477 | if (++i == nbufs) | |
4478 | return i; | |
1d0254e6 | 4479 | cond_resched(); |
067524e9 JA |
4480 | } |
4481 | i++; | |
4482 | kfree(buf); | |
9e15c3a0 | 4483 | xa_erase(&ctx->io_buffers, bgid); |
067524e9 JA |
4484 | |
4485 | return i; | |
4486 | } | |
4487 | ||
889fca73 | 4488 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
4489 | { |
4490 | struct io_provide_buf *p = &req->pbuf; | |
4491 | struct io_ring_ctx *ctx = req->ctx; | |
4492 | struct io_buffer *head; | |
4493 | int ret = 0; | |
3b44b371 | 4494 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
067524e9 | 4495 | |
3b44b371 | 4496 | io_ring_submit_lock(ctx, needs_lock); |
067524e9 JA |
4497 | |
4498 | lockdep_assert_held(&ctx->uring_lock); | |
4499 | ||
4500 | ret = -ENOENT; | |
9e15c3a0 | 4501 | head = xa_load(&ctx->io_buffers, p->bgid); |
067524e9 JA |
4502 | if (head) |
4503 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 | 4504 | if (ret < 0) |
93d2bcd2 | 4505 | req_set_fail(req); |
067524e9 | 4506 | |
9fb8cb49 PB |
4507 | /* complete before unlock, IOPOLL may need the lock */ |
4508 | __io_req_complete(req, issue_flags, ret, 0); | |
3b44b371 | 4509 | io_ring_submit_unlock(ctx, needs_lock); |
067524e9 JA |
4510 | return 0; |
4511 | } | |
4512 | ||
ddf0322d JA |
4513 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4514 | const struct io_uring_sqe *sqe) | |
4515 | { | |
38134ada | 4516 | unsigned long size, tmp_check; |
ddf0322d JA |
4517 | struct io_provide_buf *p = &req->pbuf; |
4518 | u64 tmp; | |
4519 | ||
26578cda | 4520 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
ddf0322d JA |
4521 | return -EINVAL; |
4522 | ||
4523 | tmp = READ_ONCE(sqe->fd); | |
4524 | if (!tmp || tmp > USHRT_MAX) | |
4525 | return -E2BIG; | |
4526 | p->nbufs = tmp; | |
4527 | p->addr = READ_ONCE(sqe->addr); | |
4528 | p->len = READ_ONCE(sqe->len); | |
4529 | ||
38134ada PB |
4530 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
4531 | &size)) | |
4532 | return -EOVERFLOW; | |
4533 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) | |
4534 | return -EOVERFLOW; | |
4535 | ||
d81269fe PB |
4536 | size = (unsigned long)p->len * p->nbufs; |
4537 | if (!access_ok(u64_to_user_ptr(p->addr), size)) | |
ddf0322d JA |
4538 | return -EFAULT; |
4539 | ||
4540 | p->bgid = READ_ONCE(sqe->buf_group); | |
4541 | tmp = READ_ONCE(sqe->off); | |
4542 | if (tmp > USHRT_MAX) | |
4543 | return -E2BIG; | |
4544 | p->bid = tmp; | |
4545 | return 0; | |
4546 | } | |
4547 | ||
4548 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
4549 | { | |
4550 | struct io_buffer *buf; | |
4551 | u64 addr = pbuf->addr; | |
4552 | int i, bid = pbuf->bid; | |
4553 | ||
4554 | for (i = 0; i < pbuf->nbufs; i++) { | |
9990da93 | 4555 | buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT); |
ddf0322d JA |
4556 | if (!buf) |
4557 | break; | |
4558 | ||
4559 | buf->addr = addr; | |
d1f82808 | 4560 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
ddf0322d JA |
4561 | buf->bid = bid; |
4562 | addr += pbuf->len; | |
4563 | bid++; | |
4564 | if (!*head) { | |
4565 | INIT_LIST_HEAD(&buf->list); | |
4566 | *head = buf; | |
4567 | } else { | |
4568 | list_add_tail(&buf->list, &(*head)->list); | |
4569 | } | |
4570 | } | |
4571 | ||
4572 | return i ? i : -ENOMEM; | |
4573 | } | |
4574 | ||
889fca73 | 4575 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4576 | { |
4577 | struct io_provide_buf *p = &req->pbuf; | |
4578 | struct io_ring_ctx *ctx = req->ctx; | |
4579 | struct io_buffer *head, *list; | |
4580 | int ret = 0; | |
3b44b371 | 4581 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
ddf0322d | 4582 | |
3b44b371 | 4583 | io_ring_submit_lock(ctx, needs_lock); |
ddf0322d JA |
4584 | |
4585 | lockdep_assert_held(&ctx->uring_lock); | |
4586 | ||
9e15c3a0 | 4587 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
ddf0322d JA |
4588 | |
4589 | ret = io_add_buffers(p, &head); | |
9e15c3a0 JA |
4590 | if (ret >= 0 && !list) { |
4591 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); | |
4592 | if (ret < 0) | |
067524e9 | 4593 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d | 4594 | } |
ddf0322d | 4595 | if (ret < 0) |
93d2bcd2 | 4596 | req_set_fail(req); |
9fb8cb49 PB |
4597 | /* complete before unlock, IOPOLL may need the lock */ |
4598 | __io_req_complete(req, issue_flags, ret, 0); | |
3b44b371 | 4599 | io_ring_submit_unlock(ctx, needs_lock); |
ddf0322d | 4600 | return 0; |
cebdb986 JA |
4601 | } |
4602 | ||
3e4827b0 JA |
4603 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4604 | const struct io_uring_sqe *sqe) | |
4605 | { | |
4606 | #if defined(CONFIG_EPOLL) | |
26578cda | 4607 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
3e4827b0 | 4608 | return -EINVAL; |
2d74d042 | 4609 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4610 | return -EINVAL; |
3e4827b0 JA |
4611 | |
4612 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4613 | req->epoll.op = READ_ONCE(sqe->len); | |
4614 | req->epoll.fd = READ_ONCE(sqe->off); | |
4615 | ||
4616 | if (ep_op_has_event(req->epoll.op)) { | |
4617 | struct epoll_event __user *ev; | |
4618 | ||
4619 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4620 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4621 | return -EFAULT; | |
4622 | } | |
4623 | ||
4624 | return 0; | |
4625 | #else | |
4626 | return -EOPNOTSUPP; | |
4627 | #endif | |
4628 | } | |
4629 | ||
889fca73 | 4630 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4631 | { |
4632 | #if defined(CONFIG_EPOLL) | |
4633 | struct io_epoll *ie = &req->epoll; | |
4634 | int ret; | |
45d189c6 | 4635 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4636 | |
4637 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4638 | if (force_nonblock && ret == -EAGAIN) | |
4639 | return -EAGAIN; | |
4640 | ||
4641 | if (ret < 0) | |
93d2bcd2 | 4642 | req_set_fail(req); |
889fca73 | 4643 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4644 | return 0; |
4645 | #else | |
4646 | return -EOPNOTSUPP; | |
4647 | #endif | |
4648 | } | |
4649 | ||
c1ca757b JA |
4650 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4651 | { | |
4652 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
26578cda | 4653 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
c1ca757b | 4654 | return -EINVAL; |
3232dd02 PB |
4655 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4656 | return -EINVAL; | |
c1ca757b JA |
4657 | |
4658 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4659 | req->madvise.len = READ_ONCE(sqe->len); | |
4660 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4661 | return 0; | |
4662 | #else | |
4663 | return -EOPNOTSUPP; | |
4664 | #endif | |
4665 | } | |
4666 | ||
45d189c6 | 4667 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4668 | { |
4669 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4670 | struct io_madvise *ma = &req->madvise; | |
4671 | int ret; | |
4672 | ||
45d189c6 | 4673 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4674 | return -EAGAIN; |
4675 | ||
0726b01e | 4676 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b | 4677 | if (ret < 0) |
93d2bcd2 | 4678 | req_set_fail(req); |
e1e16097 | 4679 | io_req_complete(req, ret); |
c1ca757b JA |
4680 | return 0; |
4681 | #else | |
4682 | return -EOPNOTSUPP; | |
4683 | #endif | |
4684 | } | |
4685 | ||
4840e418 JA |
4686 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4687 | { | |
26578cda | 4688 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
4840e418 | 4689 | return -EINVAL; |
3232dd02 PB |
4690 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4691 | return -EINVAL; | |
4840e418 JA |
4692 | |
4693 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4694 | req->fadvise.len = READ_ONCE(sqe->len); | |
4695 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4696 | return 0; | |
4697 | } | |
4698 | ||
45d189c6 | 4699 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4700 | { |
4701 | struct io_fadvise *fa = &req->fadvise; | |
4702 | int ret; | |
4703 | ||
45d189c6 | 4704 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4705 | switch (fa->advice) { |
4706 | case POSIX_FADV_NORMAL: | |
4707 | case POSIX_FADV_RANDOM: | |
4708 | case POSIX_FADV_SEQUENTIAL: | |
4709 | break; | |
4710 | default: | |
4711 | return -EAGAIN; | |
4712 | } | |
4713 | } | |
4840e418 JA |
4714 | |
4715 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4716 | if (ret < 0) | |
93d2bcd2 | 4717 | req_set_fail(req); |
0bdf3398 | 4718 | __io_req_complete(req, issue_flags, ret, 0); |
4840e418 JA |
4719 | return 0; |
4720 | } | |
4721 | ||
eddc7ef5 JA |
4722 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4723 | { | |
2d74d042 | 4724 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4725 | return -EINVAL; |
26578cda | 4726 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
eddc7ef5 | 4727 | return -EINVAL; |
9c280f90 | 4728 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4729 | return -EBADF; |
eddc7ef5 | 4730 | |
1d9e1288 BM |
4731 | req->statx.dfd = READ_ONCE(sqe->fd); |
4732 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4733 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4734 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4735 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4736 | |
4737 | return 0; | |
4738 | } | |
4739 | ||
45d189c6 | 4740 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4741 | { |
1d9e1288 | 4742 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4743 | int ret; |
4744 | ||
59d70013 | 4745 | if (issue_flags & IO_URING_F_NONBLOCK) |
eddc7ef5 JA |
4746 | return -EAGAIN; |
4747 | ||
e62753e4 BM |
4748 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4749 | ctx->buffer); | |
eddc7ef5 | 4750 | |
eddc7ef5 | 4751 | if (ret < 0) |
93d2bcd2 | 4752 | req_set_fail(req); |
e1e16097 | 4753 | io_req_complete(req, ret); |
eddc7ef5 JA |
4754 | return 0; |
4755 | } | |
4756 | ||
b5dba59e JA |
4757 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4758 | { | |
14587a46 | 4759 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4760 | return -EINVAL; |
b5dba59e | 4761 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
7df778be | 4762 | sqe->rw_flags || sqe->buf_index) |
b5dba59e | 4763 | return -EINVAL; |
9c280f90 | 4764 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4765 | return -EBADF; |
b5dba59e JA |
4766 | |
4767 | req->close.fd = READ_ONCE(sqe->fd); | |
7df778be PB |
4768 | req->close.file_slot = READ_ONCE(sqe->file_index); |
4769 | if (req->close.file_slot && req->close.fd) | |
4770 | return -EINVAL; | |
4771 | ||
b5dba59e | 4772 | return 0; |
b5dba59e JA |
4773 | } |
4774 | ||
889fca73 | 4775 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4776 | { |
9eac1904 | 4777 | struct files_struct *files = current->files; |
3af73b28 | 4778 | struct io_close *close = &req->close; |
9eac1904 | 4779 | struct fdtable *fdt; |
a1fde923 PB |
4780 | struct file *file = NULL; |
4781 | int ret = -EBADF; | |
b5dba59e | 4782 | |
7df778be PB |
4783 | if (req->close.file_slot) { |
4784 | ret = io_close_fixed(req, issue_flags); | |
4785 | goto err; | |
4786 | } | |
4787 | ||
9eac1904 JA |
4788 | spin_lock(&files->file_lock); |
4789 | fdt = files_fdtable(files); | |
4790 | if (close->fd >= fdt->max_fds) { | |
4791 | spin_unlock(&files->file_lock); | |
4792 | goto err; | |
4793 | } | |
4794 | file = fdt->fd[close->fd]; | |
a1fde923 | 4795 | if (!file || file->f_op == &io_uring_fops) { |
9eac1904 JA |
4796 | spin_unlock(&files->file_lock); |
4797 | file = NULL; | |
4798 | goto err; | |
3af73b28 | 4799 | } |
b5dba59e JA |
4800 | |
4801 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 4802 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 4803 | spin_unlock(&files->file_lock); |
0bf0eefd | 4804 | return -EAGAIN; |
a2100672 | 4805 | } |
b5dba59e | 4806 | |
9eac1904 JA |
4807 | ret = __close_fd_get_file(close->fd, &file); |
4808 | spin_unlock(&files->file_lock); | |
4809 | if (ret < 0) { | |
4810 | if (ret == -ENOENT) | |
4811 | ret = -EBADF; | |
4812 | goto err; | |
4813 | } | |
4814 | ||
3af73b28 | 4815 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4816 | ret = filp_close(file, current->files); |
4817 | err: | |
3af73b28 | 4818 | if (ret < 0) |
93d2bcd2 | 4819 | req_set_fail(req); |
9eac1904 JA |
4820 | if (file) |
4821 | fput(file); | |
889fca73 | 4822 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 4823 | return 0; |
b5dba59e JA |
4824 | } |
4825 | ||
1155c76a | 4826 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4827 | { |
4828 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 4829 | |
5d17b4a4 JA |
4830 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4831 | return -EINVAL; | |
26578cda PB |
4832 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4833 | sqe->splice_fd_in)) | |
5d17b4a4 JA |
4834 | return -EINVAL; |
4835 | ||
8ed8d3c3 JA |
4836 | req->sync.off = READ_ONCE(sqe->off); |
4837 | req->sync.len = READ_ONCE(sqe->len); | |
4838 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4839 | return 0; |
4840 | } | |
4841 | ||
45d189c6 | 4842 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4843 | { |
8ed8d3c3 JA |
4844 | int ret; |
4845 | ||
ac45abc0 | 4846 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 4847 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4848 | return -EAGAIN; |
4849 | ||
9adbd45d | 4850 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4851 | req->sync.flags); |
4852 | if (ret < 0) | |
93d2bcd2 | 4853 | req_set_fail(req); |
e1e16097 | 4854 | io_req_complete(req, ret); |
5d17b4a4 JA |
4855 | return 0; |
4856 | } | |
4857 | ||
469956e8 | 4858 | #if defined(CONFIG_NET) |
02d27d89 PB |
4859 | static int io_setup_async_msg(struct io_kiocb *req, |
4860 | struct io_async_msghdr *kmsg) | |
4861 | { | |
e8c2bc1f JA |
4862 | struct io_async_msghdr *async_msg = req->async_data; |
4863 | ||
4864 | if (async_msg) | |
02d27d89 | 4865 | return -EAGAIN; |
e8c2bc1f | 4866 | if (io_alloc_async_data(req)) { |
257e84a5 | 4867 | kfree(kmsg->free_iov); |
02d27d89 PB |
4868 | return -ENOMEM; |
4869 | } | |
e8c2bc1f | 4870 | async_msg = req->async_data; |
02d27d89 | 4871 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4872 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 4873 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
4874 | /* if were using fast_iov, set it to the new one */ |
4875 | if (!async_msg->free_iov) | |
4876 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
4877 | ||
02d27d89 PB |
4878 | return -EAGAIN; |
4879 | } | |
4880 | ||
2ae523ed PB |
4881 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4882 | struct io_async_msghdr *iomsg) | |
4883 | { | |
2ae523ed | 4884 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 4885 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 4886 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 4887 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
4888 | } |
4889 | ||
93642ef8 PB |
4890 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
4891 | { | |
4892 | int ret; | |
4893 | ||
93642ef8 PB |
4894 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
4895 | if (!ret) | |
4896 | req->flags |= REQ_F_NEED_CLEANUP; | |
4897 | return ret; | |
4898 | } | |
4899 | ||
3529d8c2 | 4900 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4901 | { |
e47293fd | 4902 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 4903 | |
d2b6f48b PB |
4904 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4905 | return -EINVAL; | |
4906 | ||
270a5940 | 4907 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4908 | sr->len = READ_ONCE(sqe->len); |
04411806 PB |
4909 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4910 | if (sr->msg_flags & MSG_DONTWAIT) | |
4911 | req->flags |= REQ_F_NOWAIT; | |
3529d8c2 | 4912 | |
d8768362 JA |
4913 | #ifdef CONFIG_COMPAT |
4914 | if (req->ctx->compat) | |
4915 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4916 | #endif | |
93642ef8 | 4917 | return 0; |
03b1230c JA |
4918 | } |
4919 | ||
889fca73 | 4920 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4921 | { |
6b754c8b | 4922 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4923 | struct socket *sock; |
7a7cacba | 4924 | unsigned flags; |
0031275d | 4925 | int min_ret = 0; |
0fa03c62 JA |
4926 | int ret; |
4927 | ||
dba4a925 | 4928 | sock = sock_from_file(req->file); |
7a7cacba | 4929 | if (unlikely(!sock)) |
dba4a925 | 4930 | return -ENOTSOCK; |
3529d8c2 | 4931 | |
d886e185 PB |
4932 | if (req_has_async_data(req)) { |
4933 | kmsg = req->async_data; | |
4934 | } else { | |
7a7cacba PB |
4935 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
4936 | if (ret) | |
4937 | return ret; | |
4938 | kmsg = &iomsg; | |
0fa03c62 | 4939 | } |
0fa03c62 | 4940 | |
04411806 PB |
4941 | flags = req->sr_msg.msg_flags; |
4942 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4943 | flags |= MSG_DONTWAIT; |
0031275d SM |
4944 | if (flags & MSG_WAITALL) |
4945 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4946 | ||
7a7cacba | 4947 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
0fa03c62 | 4948 | |
7297ce3d PB |
4949 | if (ret < min_ret) { |
4950 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) | |
4951 | return io_setup_async_msg(req, kmsg); | |
4952 | if (ret == -ERESTARTSYS) | |
4953 | ret = -EINTR; | |
4954 | req_set_fail(req); | |
4955 | } | |
257e84a5 PB |
4956 | /* fast path, check for non-NULL to avoid function call */ |
4957 | if (kmsg->free_iov) | |
4958 | kfree(kmsg->free_iov); | |
99bc4c38 | 4959 | req->flags &= ~REQ_F_NEED_CLEANUP; |
889fca73 | 4960 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 4961 | return 0; |
03b1230c | 4962 | } |
aa1fa28f | 4963 | |
889fca73 | 4964 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4965 | { |
7a7cacba PB |
4966 | struct io_sr_msg *sr = &req->sr_msg; |
4967 | struct msghdr msg; | |
4968 | struct iovec iov; | |
fddaface | 4969 | struct socket *sock; |
7a7cacba | 4970 | unsigned flags; |
0031275d | 4971 | int min_ret = 0; |
fddaface JA |
4972 | int ret; |
4973 | ||
dba4a925 | 4974 | sock = sock_from_file(req->file); |
7a7cacba | 4975 | if (unlikely(!sock)) |
dba4a925 | 4976 | return -ENOTSOCK; |
fddaface | 4977 | |
7a7cacba PB |
4978 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4979 | if (unlikely(ret)) | |
14db8411 | 4980 | return ret; |
fddaface | 4981 | |
7a7cacba PB |
4982 | msg.msg_name = NULL; |
4983 | msg.msg_control = NULL; | |
4984 | msg.msg_controllen = 0; | |
4985 | msg.msg_namelen = 0; | |
fddaface | 4986 | |
04411806 PB |
4987 | flags = req->sr_msg.msg_flags; |
4988 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4989 | flags |= MSG_DONTWAIT; |
0031275d SM |
4990 | if (flags & MSG_WAITALL) |
4991 | min_ret = iov_iter_count(&msg.msg_iter); | |
4992 | ||
7a7cacba PB |
4993 | msg.msg_flags = flags; |
4994 | ret = sock_sendmsg(sock, &msg); | |
7297ce3d PB |
4995 | if (ret < min_ret) { |
4996 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) | |
4997 | return -EAGAIN; | |
4998 | if (ret == -ERESTARTSYS) | |
4999 | ret = -EINTR; | |
93d2bcd2 | 5000 | req_set_fail(req); |
7297ce3d | 5001 | } |
889fca73 | 5002 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 5003 | return 0; |
fddaface JA |
5004 | } |
5005 | ||
1400e697 PB |
5006 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
5007 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
5008 | { |
5009 | struct io_sr_msg *sr = &req->sr_msg; | |
5010 | struct iovec __user *uiov; | |
5011 | size_t iov_len; | |
5012 | int ret; | |
5013 | ||
1400e697 PB |
5014 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
5015 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
5016 | if (ret) |
5017 | return ret; | |
5018 | ||
5019 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
5020 | if (iov_len > 1) | |
5021 | return -EINVAL; | |
5476dfed | 5022 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 5023 | return -EFAULT; |
5476dfed | 5024 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 5025 | iomsg->free_iov = NULL; |
52de1fe1 | 5026 | } else { |
257e84a5 | 5027 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 5028 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 5029 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 5030 | false); |
52de1fe1 JA |
5031 | if (ret > 0) |
5032 | ret = 0; | |
5033 | } | |
5034 | ||
5035 | return ret; | |
5036 | } | |
5037 | ||
5038 | #ifdef CONFIG_COMPAT | |
5039 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 5040 | struct io_async_msghdr *iomsg) |
52de1fe1 | 5041 | { |
52de1fe1 JA |
5042 | struct io_sr_msg *sr = &req->sr_msg; |
5043 | struct compat_iovec __user *uiov; | |
5044 | compat_uptr_t ptr; | |
5045 | compat_size_t len; | |
5046 | int ret; | |
5047 | ||
4af3417a PB |
5048 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
5049 | &ptr, &len); | |
52de1fe1 JA |
5050 | if (ret) |
5051 | return ret; | |
5052 | ||
5053 | uiov = compat_ptr(ptr); | |
5054 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
5055 | compat_ssize_t clen; | |
5056 | ||
5057 | if (len > 1) | |
5058 | return -EINVAL; | |
5059 | if (!access_ok(uiov, sizeof(*uiov))) | |
5060 | return -EFAULT; | |
5061 | if (__get_user(clen, &uiov->iov_len)) | |
5062 | return -EFAULT; | |
5063 | if (clen < 0) | |
5064 | return -EINVAL; | |
2d280bc8 | 5065 | sr->len = clen; |
257e84a5 | 5066 | iomsg->free_iov = NULL; |
52de1fe1 | 5067 | } else { |
257e84a5 | 5068 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 5069 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 5070 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 5071 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
5072 | if (ret < 0) |
5073 | return ret; | |
5074 | } | |
5075 | ||
5076 | return 0; | |
5077 | } | |
5078 | #endif | |
5079 | ||
1400e697 PB |
5080 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
5081 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 5082 | { |
1400e697 | 5083 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
5084 | |
5085 | #ifdef CONFIG_COMPAT | |
5086 | if (req->ctx->compat) | |
1400e697 | 5087 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 5088 | #endif |
52de1fe1 | 5089 | |
1400e697 | 5090 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
5091 | } |
5092 | ||
bcda7baa | 5093 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
51aac424 | 5094 | unsigned int issue_flags) |
bcda7baa JA |
5095 | { |
5096 | struct io_sr_msg *sr = &req->sr_msg; | |
bcda7baa | 5097 | |
51aac424 | 5098 | return io_buffer_select(req, &sr->len, sr->bgid, issue_flags); |
fddaface JA |
5099 | } |
5100 | ||
93642ef8 | 5101 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 5102 | { |
99bc4c38 | 5103 | int ret; |
3529d8c2 | 5104 | |
93642ef8 PB |
5105 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
5106 | if (!ret) | |
5107 | req->flags |= REQ_F_NEED_CLEANUP; | |
5108 | return ret; | |
5109 | } | |
5110 | ||
5111 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
5112 | { | |
5113 | struct io_sr_msg *sr = &req->sr_msg; | |
5114 | ||
d2b6f48b PB |
5115 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5116 | return -EINVAL; | |
5117 | ||
270a5940 | 5118 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 5119 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 5120 | sr->bgid = READ_ONCE(sqe->buf_group); |
04411806 PB |
5121 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
5122 | if (sr->msg_flags & MSG_DONTWAIT) | |
5123 | req->flags |= REQ_F_NOWAIT; | |
06b76d44 | 5124 | |
d8768362 JA |
5125 | #ifdef CONFIG_COMPAT |
5126 | if (req->ctx->compat) | |
5127 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
5128 | #endif | |
93642ef8 | 5129 | return 0; |
aa1fa28f JA |
5130 | } |
5131 | ||
889fca73 | 5132 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 5133 | { |
6b754c8b | 5134 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 5135 | struct socket *sock; |
7fbb1b54 | 5136 | struct io_buffer *kbuf; |
7a7cacba | 5137 | unsigned flags; |
d1fd1c20 | 5138 | int ret, min_ret = 0; |
45d189c6 | 5139 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 5140 | |
dba4a925 | 5141 | sock = sock_from_file(req->file); |
7a7cacba | 5142 | if (unlikely(!sock)) |
dba4a925 | 5143 | return -ENOTSOCK; |
3529d8c2 | 5144 | |
d886e185 PB |
5145 | if (req_has_async_data(req)) { |
5146 | kmsg = req->async_data; | |
5147 | } else { | |
7a7cacba PB |
5148 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
5149 | if (ret) | |
681fda8d | 5150 | return ret; |
7a7cacba PB |
5151 | kmsg = &iomsg; |
5152 | } | |
03b1230c | 5153 | |
bc02ef33 | 5154 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 5155 | kbuf = io_recv_buffer_select(req, issue_flags); |
bc02ef33 | 5156 | if (IS_ERR(kbuf)) |
52de1fe1 | 5157 | return PTR_ERR(kbuf); |
7a7cacba | 5158 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
5159 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
5160 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
5161 | 1, req->sr_msg.len); |
5162 | } | |
52de1fe1 | 5163 | |
04411806 PB |
5164 | flags = req->sr_msg.msg_flags; |
5165 | if (force_nonblock) | |
7a7cacba | 5166 | flags |= MSG_DONTWAIT; |
0031275d SM |
5167 | if (flags & MSG_WAITALL) |
5168 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
5169 | ||
7a7cacba PB |
5170 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
5171 | kmsg->uaddr, flags); | |
7297ce3d PB |
5172 | if (ret < min_ret) { |
5173 | if (ret == -EAGAIN && force_nonblock) | |
5174 | return io_setup_async_msg(req, kmsg); | |
5175 | if (ret == -ERESTARTSYS) | |
5176 | ret = -EINTR; | |
5177 | req_set_fail(req); | |
5178 | } else if ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { | |
5179 | req_set_fail(req); | |
5180 | } | |
03b1230c | 5181 | |
257e84a5 PB |
5182 | /* fast path, check for non-NULL to avoid function call */ |
5183 | if (kmsg->free_iov) | |
5184 | kfree(kmsg->free_iov); | |
99bc4c38 | 5185 | req->flags &= ~REQ_F_NEED_CLEANUP; |
d1fd1c20 | 5186 | __io_req_complete(req, issue_flags, ret, io_put_kbuf(req)); |
03b1230c | 5187 | return 0; |
0fa03c62 | 5188 | } |
5d17b4a4 | 5189 | |
889fca73 | 5190 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 5191 | { |
6b754c8b | 5192 | struct io_buffer *kbuf; |
7a7cacba PB |
5193 | struct io_sr_msg *sr = &req->sr_msg; |
5194 | struct msghdr msg; | |
5195 | void __user *buf = sr->buf; | |
fddaface | 5196 | struct socket *sock; |
7a7cacba PB |
5197 | struct iovec iov; |
5198 | unsigned flags; | |
d1fd1c20 | 5199 | int ret, min_ret = 0; |
45d189c6 | 5200 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 5201 | |
dba4a925 | 5202 | sock = sock_from_file(req->file); |
7a7cacba | 5203 | if (unlikely(!sock)) |
dba4a925 | 5204 | return -ENOTSOCK; |
fddaface | 5205 | |
bc02ef33 | 5206 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 5207 | kbuf = io_recv_buffer_select(req, issue_flags); |
bcda7baa JA |
5208 | if (IS_ERR(kbuf)) |
5209 | return PTR_ERR(kbuf); | |
7a7cacba | 5210 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 5211 | } |
bcda7baa | 5212 | |
7a7cacba | 5213 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
5214 | if (unlikely(ret)) |
5215 | goto out_free; | |
fddaface | 5216 | |
7a7cacba PB |
5217 | msg.msg_name = NULL; |
5218 | msg.msg_control = NULL; | |
5219 | msg.msg_controllen = 0; | |
5220 | msg.msg_namelen = 0; | |
5221 | msg.msg_iocb = NULL; | |
5222 | msg.msg_flags = 0; | |
fddaface | 5223 | |
04411806 PB |
5224 | flags = req->sr_msg.msg_flags; |
5225 | if (force_nonblock) | |
7a7cacba | 5226 | flags |= MSG_DONTWAIT; |
0031275d SM |
5227 | if (flags & MSG_WAITALL) |
5228 | min_ret = iov_iter_count(&msg.msg_iter); | |
5229 | ||
7a7cacba | 5230 | ret = sock_recvmsg(sock, &msg, flags); |
14c32eee | 5231 | out_free: |
7297ce3d PB |
5232 | if (ret < min_ret) { |
5233 | if (ret == -EAGAIN && force_nonblock) | |
5234 | return -EAGAIN; | |
5235 | if (ret == -ERESTARTSYS) | |
5236 | ret = -EINTR; | |
5237 | req_set_fail(req); | |
5238 | } else if ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { | |
93d2bcd2 | 5239 | req_set_fail(req); |
7297ce3d | 5240 | } |
d1fd1c20 PB |
5241 | |
5242 | __io_req_complete(req, issue_flags, ret, io_put_kbuf(req)); | |
fddaface | 5243 | return 0; |
fddaface JA |
5244 | } |
5245 | ||
3529d8c2 | 5246 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 5247 | { |
8ed8d3c3 JA |
5248 | struct io_accept *accept = &req->accept; |
5249 | ||
14587a46 | 5250 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 5251 | return -EINVAL; |
aaa4db12 | 5252 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
5253 | return -EINVAL; |
5254 | ||
d55e5f5b JA |
5255 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5256 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 5257 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 5258 | accept->nofile = rlimit(RLIMIT_NOFILE); |
a7083ad5 | 5259 | |
aaa4db12 PB |
5260 | accept->file_slot = READ_ONCE(sqe->file_index); |
5261 | if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) || | |
5262 | (accept->flags & SOCK_CLOEXEC))) | |
5263 | return -EINVAL; | |
a7083ad5 PB |
5264 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
5265 | return -EINVAL; | |
5266 | if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) | |
5267 | accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; | |
8ed8d3c3 | 5268 | return 0; |
8ed8d3c3 | 5269 | } |
17f2fe35 | 5270 | |
889fca73 | 5271 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
5272 | { |
5273 | struct io_accept *accept = &req->accept; | |
45d189c6 | 5274 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 5275 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
aaa4db12 | 5276 | bool fixed = !!accept->file_slot; |
a7083ad5 PB |
5277 | struct file *file; |
5278 | int ret, fd; | |
8ed8d3c3 | 5279 | |
e697deed JX |
5280 | if (req->file->f_flags & O_NONBLOCK) |
5281 | req->flags |= REQ_F_NOWAIT; | |
5282 | ||
aaa4db12 PB |
5283 | if (!fixed) { |
5284 | fd = __get_unused_fd_flags(accept->flags, accept->nofile); | |
5285 | if (unlikely(fd < 0)) | |
5286 | return fd; | |
5287 | } | |
a7083ad5 PB |
5288 | file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, |
5289 | accept->flags); | |
5290 | if (IS_ERR(file)) { | |
aaa4db12 PB |
5291 | if (!fixed) |
5292 | put_unused_fd(fd); | |
a7083ad5 PB |
5293 | ret = PTR_ERR(file); |
5294 | if (ret == -EAGAIN && force_nonblock) | |
5295 | return -EAGAIN; | |
ac45abc0 PB |
5296 | if (ret == -ERESTARTSYS) |
5297 | ret = -EINTR; | |
93d2bcd2 | 5298 | req_set_fail(req); |
aaa4db12 | 5299 | } else if (!fixed) { |
a7083ad5 PB |
5300 | fd_install(fd, file); |
5301 | ret = fd; | |
aaa4db12 PB |
5302 | } else { |
5303 | ret = io_install_fixed_file(req, file, issue_flags, | |
5304 | accept->file_slot - 1); | |
ac45abc0 | 5305 | } |
889fca73 | 5306 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 5307 | return 0; |
8ed8d3c3 JA |
5308 | } |
5309 | ||
93642ef8 PB |
5310 | static int io_connect_prep_async(struct io_kiocb *req) |
5311 | { | |
5312 | struct io_async_connect *io = req->async_data; | |
5313 | struct io_connect *conn = &req->connect; | |
5314 | ||
5315 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
5316 | } | |
5317 | ||
3529d8c2 | 5318 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 5319 | { |
3529d8c2 | 5320 | struct io_connect *conn = &req->connect; |
f499a021 | 5321 | |
14587a46 | 5322 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 | 5323 | return -EINVAL; |
26578cda PB |
5324 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
5325 | sqe->splice_fd_in) | |
3fbb51c1 JA |
5326 | return -EINVAL; |
5327 | ||
3529d8c2 JA |
5328 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5329 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 5330 | return 0; |
f499a021 JA |
5331 | } |
5332 | ||
889fca73 | 5333 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 5334 | { |
e8c2bc1f | 5335 | struct io_async_connect __io, *io; |
f8e85cf2 | 5336 | unsigned file_flags; |
3fbb51c1 | 5337 | int ret; |
45d189c6 | 5338 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 5339 | |
d886e185 | 5340 | if (req_has_async_data(req)) { |
e8c2bc1f | 5341 | io = req->async_data; |
f499a021 | 5342 | } else { |
3529d8c2 JA |
5343 | ret = move_addr_to_kernel(req->connect.addr, |
5344 | req->connect.addr_len, | |
e8c2bc1f | 5345 | &__io.address); |
f499a021 JA |
5346 | if (ret) |
5347 | goto out; | |
5348 | io = &__io; | |
5349 | } | |
5350 | ||
3fbb51c1 JA |
5351 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
5352 | ||
e8c2bc1f | 5353 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 5354 | req->connect.addr_len, file_flags); |
87f80d62 | 5355 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
d886e185 | 5356 | if (req_has_async_data(req)) |
b7bb4f7d | 5357 | return -EAGAIN; |
e8c2bc1f | 5358 | if (io_alloc_async_data(req)) { |
f499a021 JA |
5359 | ret = -ENOMEM; |
5360 | goto out; | |
5361 | } | |
e8c2bc1f | 5362 | memcpy(req->async_data, &__io, sizeof(__io)); |
f8e85cf2 | 5363 | return -EAGAIN; |
f499a021 | 5364 | } |
f8e85cf2 JA |
5365 | if (ret == -ERESTARTSYS) |
5366 | ret = -EINTR; | |
f499a021 | 5367 | out: |
4e88d6e7 | 5368 | if (ret < 0) |
93d2bcd2 | 5369 | req_set_fail(req); |
889fca73 | 5370 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 5371 | return 0; |
469956e8 Y |
5372 | } |
5373 | #else /* !CONFIG_NET */ | |
99a10081 JA |
5374 | #define IO_NETOP_FN(op) \ |
5375 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
5376 | { \ | |
5377 | return -EOPNOTSUPP; \ | |
5378 | } | |
5379 | ||
5380 | #define IO_NETOP_PREP(op) \ | |
5381 | IO_NETOP_FN(op) \ | |
5382 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
5383 | { \ | |
5384 | return -EOPNOTSUPP; \ | |
5385 | } \ | |
5386 | ||
5387 | #define IO_NETOP_PREP_ASYNC(op) \ | |
5388 | IO_NETOP_PREP(op) \ | |
5389 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
5390 | { \ | |
5391 | return -EOPNOTSUPP; \ | |
5392 | } | |
5393 | ||
5394 | IO_NETOP_PREP_ASYNC(sendmsg); | |
5395 | IO_NETOP_PREP_ASYNC(recvmsg); | |
5396 | IO_NETOP_PREP_ASYNC(connect); | |
5397 | IO_NETOP_PREP(accept); | |
5398 | IO_NETOP_FN(send); | |
5399 | IO_NETOP_FN(recv); | |
469956e8 | 5400 | #endif /* CONFIG_NET */ |
f8e85cf2 | 5401 | |
d7718a9d JA |
5402 | struct io_poll_table { |
5403 | struct poll_table_struct pt; | |
5404 | struct io_kiocb *req; | |
68b11e8b | 5405 | int nr_entries; |
d7718a9d JA |
5406 | int error; |
5407 | }; | |
ce593a6c | 5408 | |
aa43477b PB |
5409 | #define IO_POLL_CANCEL_FLAG BIT(31) |
5410 | #define IO_POLL_REF_MASK ((1u << 20)-1) | |
6d816e08 | 5411 | |
aa43477b PB |
5412 | /* |
5413 | * If refs part of ->poll_refs (see IO_POLL_REF_MASK) is 0, it's free. We can | |
5414 | * bump it and acquire ownership. It's disallowed to modify requests while not | |
5415 | * owning it, that prevents from races for enqueueing task_work's and b/w | |
5416 | * arming poll and wakeups. | |
5417 | */ | |
5418 | static inline bool io_poll_get_ownership(struct io_kiocb *req) | |
5419 | { | |
5420 | return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK); | |
d7718a9d JA |
5421 | } |
5422 | ||
aa43477b | 5423 | static void io_poll_mark_cancelled(struct io_kiocb *req) |
74ce6ce4 | 5424 | { |
aa43477b | 5425 | atomic_or(IO_POLL_CANCEL_FLAG, &req->poll_refs); |
74ce6ce4 JA |
5426 | } |
5427 | ||
d4e7cd36 | 5428 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5429 | { |
e8c2bc1f | 5430 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5431 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5432 | return req->async_data; |
d4e7cd36 JA |
5433 | return req->apoll->double_poll; |
5434 | } | |
5435 | ||
5436 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5437 | { | |
5438 | if (req->opcode == IORING_OP_POLL_ADD) | |
5439 | return &req->poll; | |
5440 | return &req->apoll->poll; | |
5441 | } | |
5442 | ||
5641897a | 5443 | static void io_poll_req_insert(struct io_kiocb *req) |
d4e7cd36 | 5444 | { |
5641897a PB |
5445 | struct io_ring_ctx *ctx = req->ctx; |
5446 | struct hlist_head *list; | |
18bceab1 | 5447 | |
5641897a PB |
5448 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
5449 | hlist_add_head(&req->hash_node, list); | |
18bceab1 JA |
5450 | } |
5451 | ||
5641897a PB |
5452 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
5453 | wait_queue_func_t wake_func) | |
18bceab1 | 5454 | { |
5641897a | 5455 | poll->head = NULL; |
5641897a PB |
5456 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
5457 | /* mask in events that we always want/need */ | |
5458 | poll->events = events | IO_POLL_UNMASK; | |
5459 | INIT_LIST_HEAD(&poll->wait.entry); | |
5460 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
18bceab1 JA |
5461 | } |
5462 | ||
aa43477b | 5463 | static inline void io_poll_remove_entry(struct io_poll_iocb *poll) |
18bceab1 | 5464 | { |
791f3465 | 5465 | struct wait_queue_head *head = smp_load_acquire(&poll->head); |
18bceab1 | 5466 | |
791f3465 PB |
5467 | if (head) { |
5468 | spin_lock_irq(&head->lock); | |
5469 | list_del_init(&poll->wait.entry); | |
5470 | poll->head = NULL; | |
5471 | spin_unlock_irq(&head->lock); | |
5472 | } | |
aa43477b | 5473 | } |
18bceab1 | 5474 | |
aa43477b PB |
5475 | static void io_poll_remove_entries(struct io_kiocb *req) |
5476 | { | |
5477 | struct io_poll_iocb *poll = io_poll_get_single(req); | |
5478 | struct io_poll_iocb *poll_double = io_poll_get_double(req); | |
18bceab1 | 5479 | |
791f3465 PB |
5480 | /* |
5481 | * While we hold the waitqueue lock and the waitqueue is nonempty, | |
5482 | * wake_up_pollfree() will wait for us. However, taking the waitqueue | |
5483 | * lock in the first place can race with the waitqueue being freed. | |
5484 | * | |
5485 | * We solve this as eventpoll does: by taking advantage of the fact that | |
5486 | * all users of wake_up_pollfree() will RCU-delay the actual free. If | |
5487 | * we enter rcu_read_lock() and see that the pointer to the queue is | |
5488 | * non-NULL, we can then lock it without the memory being freed out from | |
5489 | * under us. | |
5490 | * | |
5491 | * Keep holding rcu_read_lock() as long as we hold the queue lock, in | |
5492 | * case the caller deletes the entry from the queue, leaving it empty. | |
5493 | * In that case, only RCU prevents the queue memory from being freed. | |
5494 | */ | |
5495 | rcu_read_lock(); | |
5496 | io_poll_remove_entry(poll); | |
5497 | if (poll_double) | |
aa43477b | 5498 | io_poll_remove_entry(poll_double); |
791f3465 | 5499 | rcu_read_unlock(); |
18bceab1 JA |
5500 | } |
5501 | ||
aa43477b PB |
5502 | /* |
5503 | * All poll tw should go through this. Checks for poll events, manages | |
5504 | * references, does rewait, etc. | |
5505 | * | |
5506 | * Returns a negative error on failure. >0 when no action require, which is | |
5507 | * either spurious wakeup or multishot CQE is served. 0 when it's done with | |
5508 | * the request, then the mask is stored in req->result. | |
5509 | */ | |
5510 | static int io_poll_check_events(struct io_kiocb *req) | |
18bceab1 | 5511 | { |
74ce6ce4 | 5512 | struct io_ring_ctx *ctx = req->ctx; |
d4e7cd36 | 5513 | struct io_poll_iocb *poll = io_poll_get_single(req); |
aa43477b | 5514 | int v; |
18bceab1 | 5515 | |
316319e8 | 5516 | /* req->task == current here, checking PF_EXITING is safe */ |
e09ee510 | 5517 | if (unlikely(req->task->flags & PF_EXITING)) |
aa43477b | 5518 | io_poll_mark_cancelled(req); |
18bceab1 | 5519 | |
aa43477b PB |
5520 | do { |
5521 | v = atomic_read(&req->poll_refs); | |
74ce6ce4 | 5522 | |
aa43477b PB |
5523 | /* tw handler should be the owner, and so have some references */ |
5524 | if (WARN_ON_ONCE(!(v & IO_POLL_REF_MASK))) | |
5525 | return 0; | |
5526 | if (v & IO_POLL_CANCEL_FLAG) | |
5527 | return -ECANCELED; | |
8706e04e | 5528 | |
aa43477b PB |
5529 | if (!req->result) { |
5530 | struct poll_table_struct pt = { ._key = poll->events }; | |
18bceab1 | 5531 | |
aa43477b | 5532 | req->result = vfs_poll(req->file, &pt) & poll->events; |
c8b5e260 | 5533 | } |
74ce6ce4 | 5534 | |
aa43477b PB |
5535 | /* multishot, just fill an CQE and proceed */ |
5536 | if (req->result && !(poll->events & EPOLLONESHOT)) { | |
5537 | __poll_t mask = mangle_poll(req->result & poll->events); | |
5538 | bool filled; | |
18bceab1 | 5539 | |
aa43477b PB |
5540 | spin_lock(&ctx->completion_lock); |
5541 | filled = io_fill_cqe_aux(ctx, req->user_data, mask, | |
5542 | IORING_CQE_F_MORE); | |
5543 | io_commit_cqring(ctx); | |
5544 | spin_unlock(&ctx->completion_lock); | |
5545 | if (unlikely(!filled)) | |
5546 | return -ECANCELED; | |
5547 | io_cqring_ev_posted(ctx); | |
5548 | } else if (req->result) { | |
5549 | return 0; | |
5550 | } | |
18bceab1 | 5551 | |
aa43477b PB |
5552 | /* |
5553 | * Release all references, retry if someone tried to restart | |
5554 | * task_work while we were executing it. | |
5555 | */ | |
5556 | } while (atomic_sub_return(v & IO_POLL_REF_MASK, &req->poll_refs)); | |
18bceab1 | 5557 | |
18bceab1 JA |
5558 | return 1; |
5559 | } | |
5560 | ||
aa43477b | 5561 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 | 5562 | { |
18bceab1 | 5563 | struct io_ring_ctx *ctx = req->ctx; |
aa43477b | 5564 | int ret; |
18bceab1 | 5565 | |
aa43477b PB |
5566 | ret = io_poll_check_events(req); |
5567 | if (ret > 0) | |
5568 | return; | |
5569 | ||
5570 | if (!ret) { | |
5571 | req->result = mangle_poll(req->result & req->poll.events); | |
e27414be | 5572 | } else { |
aa43477b PB |
5573 | req->result = ret; |
5574 | req_set_fail(req); | |
a62682f9 | 5575 | } |
aa43477b PB |
5576 | |
5577 | io_poll_remove_entries(req); | |
5578 | spin_lock(&ctx->completion_lock); | |
5579 | hash_del(&req->hash_node); | |
5580 | __io_req_complete_post(req, req->result, 0); | |
5581 | io_commit_cqring(ctx); | |
5582 | spin_unlock(&ctx->completion_lock); | |
5583 | io_cqring_ev_posted(ctx); | |
18bceab1 JA |
5584 | } |
5585 | ||
aa43477b | 5586 | static void io_apoll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 JA |
5587 | { |
5588 | struct io_ring_ctx *ctx = req->ctx; | |
aa43477b | 5589 | int ret; |
18bceab1 | 5590 | |
aa43477b PB |
5591 | ret = io_poll_check_events(req); |
5592 | if (ret > 0) | |
5593 | return; | |
18bceab1 | 5594 | |
aa43477b PB |
5595 | io_poll_remove_entries(req); |
5596 | spin_lock(&ctx->completion_lock); | |
5597 | hash_del(&req->hash_node); | |
5598 | spin_unlock(&ctx->completion_lock); | |
18bceab1 | 5599 | |
aa43477b PB |
5600 | if (!ret) |
5601 | io_req_task_submit(req, locked); | |
5602 | else | |
5603 | io_req_complete_failed(req, ret); | |
18bceab1 JA |
5604 | } |
5605 | ||
aa43477b PB |
5606 | static void __io_poll_execute(struct io_kiocb *req, int mask) |
5607 | { | |
5608 | req->result = mask; | |
5609 | if (req->opcode == IORING_OP_POLL_ADD) | |
5610 | req->io_task_work.func = io_poll_task_func; | |
5611 | else | |
5612 | req->io_task_work.func = io_apoll_task_func; | |
5613 | ||
5614 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
5615 | io_req_task_work_add(req, false); | |
5616 | } | |
5617 | ||
5618 | static inline void io_poll_execute(struct io_kiocb *req, int res) | |
5619 | { | |
5620 | if (io_poll_get_ownership(req)) | |
5621 | __io_poll_execute(req, res); | |
5622 | } | |
5623 | ||
5624 | static void io_poll_cancel_req(struct io_kiocb *req) | |
5625 | { | |
5626 | io_poll_mark_cancelled(req); | |
5627 | /* kick tw, which should complete the request */ | |
5628 | io_poll_execute(req, 0); | |
5629 | } | |
5630 | ||
5631 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5632 | void *key) | |
18bceab1 JA |
5633 | { |
5634 | struct io_kiocb *req = wait->private; | |
aa43477b PB |
5635 | struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb, |
5636 | wait); | |
18bceab1 JA |
5637 | __poll_t mask = key_to_poll(key); |
5638 | ||
791f3465 PB |
5639 | if (unlikely(mask & POLLFREE)) { |
5640 | io_poll_mark_cancelled(req); | |
5641 | /* we have to kick tw in case it's not already */ | |
5642 | io_poll_execute(req, 0); | |
5643 | ||
5644 | /* | |
5645 | * If the waitqueue is being freed early but someone is already | |
5646 | * holds ownership over it, we have to tear down the request as | |
5647 | * best we can. That means immediately removing the request from | |
5648 | * its waitqueue and preventing all further accesses to the | |
5649 | * waitqueue via the request. | |
5650 | */ | |
5651 | list_del_init(&poll->wait.entry); | |
5652 | ||
5653 | /* | |
5654 | * Careful: this *must* be the last step, since as soon | |
5655 | * as req->head is NULL'ed out, the request can be | |
5656 | * completed and freed, since aio_poll_complete_work() | |
5657 | * will no longer need to take the waitqueue lock. | |
5658 | */ | |
5659 | smp_store_release(&poll->head, NULL); | |
5660 | return 1; | |
5661 | } | |
5662 | ||
aa43477b | 5663 | /* for instances that support it check for an event match first */ |
18bceab1 JA |
5664 | if (mask && !(mask & poll->events)) |
5665 | return 0; | |
5666 | ||
eb0089d6 PB |
5667 | if (io_poll_get_ownership(req)) { |
5668 | /* optional, saves extra locking for removal in tw handler */ | |
5669 | if (mask && poll->events & EPOLLONESHOT) { | |
5670 | list_del_init(&poll->wait.entry); | |
5671 | poll->head = NULL; | |
5672 | } | |
aa43477b | 5673 | __io_poll_execute(req, mask); |
eb0089d6 | 5674 | } |
18bceab1 | 5675 | return 1; |
18bceab1 JA |
5676 | } |
5677 | ||
5678 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
5679 | struct wait_queue_head *head, |
5680 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
5681 | { |
5682 | struct io_kiocb *req = pt->req; | |
5683 | ||
5684 | /* | |
68b11e8b PB |
5685 | * The file being polled uses multiple waitqueues for poll handling |
5686 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb | |
5687 | * if this happens. | |
18bceab1 | 5688 | */ |
68b11e8b | 5689 | if (unlikely(pt->nr_entries)) { |
aa43477b | 5690 | struct io_poll_iocb *first = poll; |
58852d4d | 5691 | |
23a65db8 | 5692 | /* double add on the same waitqueue head, ignore */ |
aa43477b | 5693 | if (first->head == head) |
23a65db8 | 5694 | return; |
18bceab1 | 5695 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5696 | if (*poll_ptr) { |
23a65db8 PB |
5697 | if ((*poll_ptr)->head == head) |
5698 | return; | |
18bceab1 JA |
5699 | pt->error = -EINVAL; |
5700 | return; | |
5701 | } | |
aa43477b | 5702 | |
18bceab1 JA |
5703 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
5704 | if (!poll) { | |
5705 | pt->error = -ENOMEM; | |
5706 | return; | |
5707 | } | |
aa43477b | 5708 | io_init_poll_iocb(poll, first->events, first->wait.func); |
807abcb0 | 5709 | *poll_ptr = poll; |
d886e185 PB |
5710 | if (req->opcode == IORING_OP_POLL_ADD) |
5711 | req->flags |= REQ_F_ASYNC_DATA; | |
18bceab1 JA |
5712 | } |
5713 | ||
68b11e8b | 5714 | pt->nr_entries++; |
18bceab1 | 5715 | poll->head = head; |
aa43477b | 5716 | poll->wait.private = req; |
a31eb4a2 JX |
5717 | |
5718 | if (poll->events & EPOLLEXCLUSIVE) | |
5719 | add_wait_queue_exclusive(head, &poll->wait); | |
5720 | else | |
5721 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5722 | } |
5723 | ||
aa43477b | 5724 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
18bceab1 JA |
5725 | struct poll_table_struct *p) |
5726 | { | |
5727 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
d7718a9d | 5728 | |
aa43477b PB |
5729 | __io_queue_proc(&pt->req->poll, pt, head, |
5730 | (struct io_poll_iocb **) &pt->req->async_data); | |
d7718a9d JA |
5731 | } |
5732 | ||
aa43477b PB |
5733 | static int __io_arm_poll_handler(struct io_kiocb *req, |
5734 | struct io_poll_iocb *poll, | |
5735 | struct io_poll_table *ipt, __poll_t mask) | |
d7718a9d JA |
5736 | { |
5737 | struct io_ring_ctx *ctx = req->ctx; | |
aa43477b | 5738 | int v; |
d7718a9d | 5739 | |
4d52f338 | 5740 | INIT_HLIST_NODE(&req->hash_node); |
aa43477b | 5741 | io_init_poll_iocb(poll, mask, io_poll_wake); |
b90cd197 | 5742 | poll->file = req->file; |
18bceab1 | 5743 | poll->wait.private = req; |
d7718a9d JA |
5744 | |
5745 | ipt->pt._key = mask; | |
5746 | ipt->req = req; | |
68b11e8b PB |
5747 | ipt->error = 0; |
5748 | ipt->nr_entries = 0; | |
d7718a9d | 5749 | |
aa43477b PB |
5750 | /* |
5751 | * Take the ownership to delay any tw execution up until we're done | |
5752 | * with poll arming. see io_poll_get_ownership(). | |
5753 | */ | |
5754 | atomic_set(&req->poll_refs, 1); | |
d7718a9d | 5755 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
aa43477b PB |
5756 | |
5757 | if (mask && (poll->events & EPOLLONESHOT)) { | |
5758 | io_poll_remove_entries(req); | |
5759 | /* no one else has access to the req, forget about the ref */ | |
5760 | return mask; | |
5761 | } | |
5762 | if (!mask && unlikely(ipt->error || !ipt->nr_entries)) { | |
5763 | io_poll_remove_entries(req); | |
5764 | if (!ipt->error) | |
5765 | ipt->error = -EINVAL; | |
5766 | return 0; | |
5767 | } | |
d7718a9d | 5768 | |
79ebeaee | 5769 | spin_lock(&ctx->completion_lock); |
aa43477b PB |
5770 | io_poll_req_insert(req); |
5771 | spin_unlock(&ctx->completion_lock); | |
5772 | ||
5773 | if (mask) { | |
5774 | /* can't multishot if failed, just queue the event we've got */ | |
5775 | if (unlikely(ipt->error || !ipt->nr_entries)) | |
5776 | poll->events |= EPOLLONESHOT; | |
5777 | __io_poll_execute(req, mask); | |
5778 | return 0; | |
d7718a9d JA |
5779 | } |
5780 | ||
aa43477b PB |
5781 | /* |
5782 | * Release ownership. If someone tried to queue a tw while it was | |
5783 | * locked, kick it off for them. | |
5784 | */ | |
5785 | v = atomic_dec_return(&req->poll_refs); | |
5786 | if (unlikely(v & IO_POLL_REF_MASK)) | |
5787 | __io_poll_execute(req, 0); | |
5788 | return 0; | |
5789 | } | |
5790 | ||
5791 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5792 | struct poll_table_struct *p) | |
5793 | { | |
5794 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5795 | struct async_poll *apoll = pt->req->apoll; | |
5796 | ||
5797 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); | |
d7718a9d JA |
5798 | } |
5799 | ||
59b735ae OL |
5800 | enum { |
5801 | IO_APOLL_OK, | |
5802 | IO_APOLL_ABORTED, | |
5803 | IO_APOLL_READY | |
5804 | }; | |
5805 | ||
5806 | static int io_arm_poll_handler(struct io_kiocb *req) | |
d7718a9d JA |
5807 | { |
5808 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5809 | struct io_ring_ctx *ctx = req->ctx; | |
5810 | struct async_poll *apoll; | |
5811 | struct io_poll_table ipt; | |
aa43477b PB |
5812 | __poll_t mask = EPOLLONESHOT | POLLERR | POLLPRI; |
5813 | int ret; | |
d7718a9d | 5814 | |
b2d9c3da PB |
5815 | if (!def->pollin && !def->pollout) |
5816 | return IO_APOLL_ABORTED; | |
658d0a40 PB |
5817 | if (!file_can_poll(req->file) || (req->flags & REQ_F_POLLED)) |
5818 | return IO_APOLL_ABORTED; | |
b2d9c3da PB |
5819 | |
5820 | if (def->pollin) { | |
b2d9c3da PB |
5821 | mask |= POLLIN | POLLRDNORM; |
5822 | ||
5823 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5824 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5825 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5826 | mask &= ~POLLIN; | |
5827 | } else { | |
b2d9c3da PB |
5828 | mask |= POLLOUT | POLLWRNORM; |
5829 | } | |
5830 | ||
d7718a9d JA |
5831 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
5832 | if (unlikely(!apoll)) | |
59b735ae | 5833 | return IO_APOLL_ABORTED; |
807abcb0 | 5834 | apoll->double_poll = NULL; |
d7718a9d | 5835 | req->apoll = apoll; |
b2d9c3da | 5836 | req->flags |= REQ_F_POLLED; |
d7718a9d JA |
5837 | ipt.pt._qproc = io_async_queue_proc; |
5838 | ||
aa43477b | 5839 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask); |
41a5169c HX |
5840 | if (ret || ipt.error) |
5841 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; | |
5842 | ||
236daeae OL |
5843 | trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data, |
5844 | mask, apoll->poll.events); | |
59b735ae | 5845 | return IO_APOLL_OK; |
d7718a9d JA |
5846 | } |
5847 | ||
76e1b642 JA |
5848 | /* |
5849 | * Returns true if we found and killed one or more poll requests | |
5850 | */ | |
c072481d PB |
5851 | static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, |
5852 | struct task_struct *tsk, bool cancel_all) | |
221c5eb2 | 5853 | { |
78076bb6 | 5854 | struct hlist_node *tmp; |
221c5eb2 | 5855 | struct io_kiocb *req; |
aa43477b PB |
5856 | bool found = false; |
5857 | int i; | |
221c5eb2 | 5858 | |
79ebeaee | 5859 | spin_lock(&ctx->completion_lock); |
78076bb6 JA |
5860 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5861 | struct hlist_head *list; | |
5862 | ||
5863 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5864 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
42a7b4ed | 5865 | if (io_match_task_safe(req, tsk, cancel_all)) { |
aa43477b PB |
5866 | io_poll_cancel_req(req); |
5867 | found = true; | |
5868 | } | |
f3606e3a | 5869 | } |
221c5eb2 | 5870 | } |
79ebeaee | 5871 | spin_unlock(&ctx->completion_lock); |
aa43477b | 5872 | return found; |
221c5eb2 JA |
5873 | } |
5874 | ||
9ba5fac8 PB |
5875 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5876 | bool poll_only) | |
e07785b0 | 5877 | __must_hold(&ctx->completion_lock) |
47f46768 | 5878 | { |
78076bb6 | 5879 | struct hlist_head *list; |
47f46768 JA |
5880 | struct io_kiocb *req; |
5881 | ||
78076bb6 JA |
5882 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5883 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5884 | if (sqe_addr != req->user_data) |
5885 | continue; | |
9ba5fac8 PB |
5886 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
5887 | continue; | |
b2cb805f | 5888 | return req; |
47f46768 | 5889 | } |
b2cb805f JA |
5890 | return NULL; |
5891 | } | |
5892 | ||
aa43477b PB |
5893 | static bool io_poll_disarm(struct io_kiocb *req) |
5894 | __must_hold(&ctx->completion_lock) | |
5895 | { | |
5896 | if (!io_poll_get_ownership(req)) | |
5897 | return false; | |
5898 | io_poll_remove_entries(req); | |
5899 | hash_del(&req->hash_node); | |
5900 | return true; | |
5901 | } | |
5902 | ||
9ba5fac8 PB |
5903 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5904 | bool poll_only) | |
e07785b0 | 5905 | __must_hold(&ctx->completion_lock) |
b2cb805f | 5906 | { |
aa43477b | 5907 | struct io_kiocb *req = io_poll_find(ctx, sqe_addr, poll_only); |
b2cb805f | 5908 | |
b2cb805f JA |
5909 | if (!req) |
5910 | return -ENOENT; | |
aa43477b PB |
5911 | io_poll_cancel_req(req); |
5912 | return 0; | |
47f46768 JA |
5913 | } |
5914 | ||
9096af3e PB |
5915 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
5916 | unsigned int flags) | |
5917 | { | |
5918 | u32 events; | |
47f46768 | 5919 | |
9096af3e PB |
5920 | events = READ_ONCE(sqe->poll32_events); |
5921 | #ifdef __BIG_ENDIAN | |
5922 | events = swahw32(events); | |
5923 | #endif | |
5924 | if (!(flags & IORING_POLL_ADD_MULTI)) | |
5925 | events |= EPOLLONESHOT; | |
5926 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); | |
47f46768 JA |
5927 | } |
5928 | ||
c5de0036 | 5929 | static int io_poll_update_prep(struct io_kiocb *req, |
3529d8c2 | 5930 | const struct io_uring_sqe *sqe) |
0969e783 | 5931 | { |
c5de0036 PB |
5932 | struct io_poll_update *upd = &req->poll_update; |
5933 | u32 flags; | |
5934 | ||
0969e783 JA |
5935 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5936 | return -EINVAL; | |
26578cda | 5937 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
c5de0036 PB |
5938 | return -EINVAL; |
5939 | flags = READ_ONCE(sqe->len); | |
5940 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | | |
5941 | IORING_POLL_ADD_MULTI)) | |
5942 | return -EINVAL; | |
5943 | /* meaningless without update */ | |
5944 | if (flags == IORING_POLL_ADD_MULTI) | |
0969e783 JA |
5945 | return -EINVAL; |
5946 | ||
c5de0036 PB |
5947 | upd->old_user_data = READ_ONCE(sqe->addr); |
5948 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; | |
5949 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; | |
221c5eb2 | 5950 | |
c5de0036 PB |
5951 | upd->new_user_data = READ_ONCE(sqe->off); |
5952 | if (!upd->update_user_data && upd->new_user_data) | |
5953 | return -EINVAL; | |
5954 | if (upd->update_events) | |
5955 | upd->events = io_poll_parse_events(sqe, flags); | |
5956 | else if (sqe->poll32_events) | |
5957 | return -EINVAL; | |
221c5eb2 | 5958 | |
221c5eb2 JA |
5959 | return 0; |
5960 | } | |
5961 | ||
3529d8c2 | 5962 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5963 | { |
5964 | struct io_poll_iocb *poll = &req->poll; | |
c5de0036 | 5965 | u32 flags; |
221c5eb2 JA |
5966 | |
5967 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5968 | return -EINVAL; | |
c5de0036 | 5969 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
88e41cf9 JA |
5970 | return -EINVAL; |
5971 | flags = READ_ONCE(sqe->len); | |
c5de0036 | 5972 | if (flags & ~IORING_POLL_ADD_MULTI) |
221c5eb2 | 5973 | return -EINVAL; |
04c76b41 PB |
5974 | if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP)) |
5975 | return -EINVAL; | |
221c5eb2 | 5976 | |
48dcd38d | 5977 | io_req_set_refcount(req); |
c5de0036 | 5978 | poll->events = io_poll_parse_events(sqe, flags); |
0969e783 JA |
5979 | return 0; |
5980 | } | |
5981 | ||
61e98203 | 5982 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
5983 | { |
5984 | struct io_poll_iocb *poll = &req->poll; | |
0969e783 | 5985 | struct io_poll_table ipt; |
aa43477b | 5986 | int ret; |
0969e783 | 5987 | |
d7718a9d | 5988 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5989 | |
aa43477b PB |
5990 | ret = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events); |
5991 | ret = ret ?: ipt.error; | |
5992 | if (ret) | |
5993 | __io_req_complete(req, issue_flags, ret, 0); | |
5994 | return 0; | |
221c5eb2 JA |
5995 | } |
5996 | ||
c5de0036 | 5997 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
b69de288 JA |
5998 | { |
5999 | struct io_ring_ctx *ctx = req->ctx; | |
6000 | struct io_kiocb *preq; | |
2bbb146d | 6001 | int ret2, ret = 0; |
cc8e9ba7 | 6002 | bool locked; |
b69de288 | 6003 | |
79ebeaee | 6004 | spin_lock(&ctx->completion_lock); |
9ba5fac8 | 6005 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
aa43477b | 6006 | if (!preq || !io_poll_disarm(preq)) { |
79ebeaee | 6007 | spin_unlock(&ctx->completion_lock); |
aa43477b | 6008 | ret = preq ? -EALREADY : -ENOENT; |
2bbb146d | 6009 | goto out; |
b69de288 | 6010 | } |
79ebeaee | 6011 | spin_unlock(&ctx->completion_lock); |
cb3b200e | 6012 | |
2bbb146d PB |
6013 | if (req->poll_update.update_events || req->poll_update.update_user_data) { |
6014 | /* only mask one event flags, keep behavior flags */ | |
6015 | if (req->poll_update.update_events) { | |
6016 | preq->poll.events &= ~0xffff; | |
6017 | preq->poll.events |= req->poll_update.events & 0xffff; | |
6018 | preq->poll.events |= IO_POLL_UNMASK; | |
cb3b200e | 6019 | } |
2bbb146d PB |
6020 | if (req->poll_update.update_user_data) |
6021 | preq->user_data = req->poll_update.new_user_data; | |
b69de288 | 6022 | |
2bbb146d PB |
6023 | ret2 = io_poll_add(preq, issue_flags); |
6024 | /* successfully updated, don't complete poll request */ | |
6025 | if (!ret2) | |
6026 | goto out; | |
b69de288 | 6027 | } |
6224590d | 6028 | |
2bbb146d | 6029 | req_set_fail(preq); |
cc8e9ba7 PB |
6030 | preq->result = -ECANCELED; |
6031 | locked = !(issue_flags & IO_URING_F_UNLOCKED); | |
6032 | io_req_task_complete(preq, &locked); | |
2bbb146d PB |
6033 | out: |
6034 | if (ret < 0) | |
6224590d | 6035 | req_set_fail(req); |
2bbb146d | 6036 | /* complete update request, we're done with it */ |
cc8e9ba7 | 6037 | __io_req_complete(req, issue_flags, ret, 0); |
b69de288 | 6038 | return 0; |
89850fce JA |
6039 | } |
6040 | ||
5262f567 JA |
6041 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
6042 | { | |
ad8a48ac JA |
6043 | struct io_timeout_data *data = container_of(timer, |
6044 | struct io_timeout_data, timer); | |
6045 | struct io_kiocb *req = data->req; | |
6046 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
6047 | unsigned long flags; |
6048 | ||
89850fce | 6049 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
a71976f3 | 6050 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
6051 | atomic_set(&req->ctx->cq_timeouts, |
6052 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
89850fce | 6053 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
01cec8c1 | 6054 | |
a90c8bf6 PB |
6055 | if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS)) |
6056 | req_set_fail(req); | |
6057 | ||
6058 | req->result = -ETIME; | |
6059 | req->io_task_work.func = io_req_task_complete; | |
4813c377 | 6060 | io_req_task_work_add(req, false); |
5262f567 JA |
6061 | return HRTIMER_NORESTART; |
6062 | } | |
6063 | ||
fbd15848 PB |
6064 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
6065 | __u64 user_data) | |
89850fce | 6066 | __must_hold(&ctx->timeout_lock) |
f254ac04 | 6067 | { |
fbd15848 | 6068 | struct io_timeout_data *io; |
47f46768 | 6069 | struct io_kiocb *req; |
fd9c7bc5 | 6070 | bool found = false; |
f254ac04 | 6071 | |
135fcde8 | 6072 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
fd9c7bc5 PB |
6073 | found = user_data == req->user_data; |
6074 | if (found) | |
47f46768 | 6075 | break; |
47f46768 | 6076 | } |
fd9c7bc5 PB |
6077 | if (!found) |
6078 | return ERR_PTR(-ENOENT); | |
fbd15848 PB |
6079 | |
6080 | io = req->async_data; | |
fd9c7bc5 | 6081 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
fbd15848 | 6082 | return ERR_PTR(-EALREADY); |
a71976f3 | 6083 | list_del_init(&req->timeout.list); |
fbd15848 PB |
6084 | return req; |
6085 | } | |
47f46768 | 6086 | |
fbd15848 | 6087 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
ec3c3d0f | 6088 | __must_hold(&ctx->completion_lock) |
89850fce | 6089 | __must_hold(&ctx->timeout_lock) |
fbd15848 PB |
6090 | { |
6091 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
6092 | ||
6093 | if (IS_ERR(req)) | |
6094 | return PTR_ERR(req); | |
f254ac04 | 6095 | |
93d2bcd2 | 6096 | req_set_fail(req); |
913a571a | 6097 | io_fill_cqe_req(req, -ECANCELED, 0); |
91c2f697 | 6098 | io_put_req_deferred(req); |
f254ac04 JA |
6099 | return 0; |
6100 | } | |
6101 | ||
50c1df2b JA |
6102 | static clockid_t io_timeout_get_clock(struct io_timeout_data *data) |
6103 | { | |
6104 | switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) { | |
6105 | case IORING_TIMEOUT_BOOTTIME: | |
6106 | return CLOCK_BOOTTIME; | |
6107 | case IORING_TIMEOUT_REALTIME: | |
6108 | return CLOCK_REALTIME; | |
6109 | default: | |
6110 | /* can't happen, vetted at prep time */ | |
6111 | WARN_ON_ONCE(1); | |
6112 | fallthrough; | |
6113 | case 0: | |
6114 | return CLOCK_MONOTONIC; | |
6115 | } | |
6116 | } | |
6117 | ||
f1042b6c PB |
6118 | static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
6119 | struct timespec64 *ts, enum hrtimer_mode mode) | |
6120 | __must_hold(&ctx->timeout_lock) | |
6121 | { | |
6122 | struct io_timeout_data *io; | |
6123 | struct io_kiocb *req; | |
6124 | bool found = false; | |
6125 | ||
6126 | list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) { | |
6127 | found = user_data == req->user_data; | |
6128 | if (found) | |
6129 | break; | |
6130 | } | |
6131 | if (!found) | |
6132 | return -ENOENT; | |
6133 | ||
6134 | io = req->async_data; | |
6135 | if (hrtimer_try_to_cancel(&io->timer) == -1) | |
6136 | return -EALREADY; | |
6137 | hrtimer_init(&io->timer, io_timeout_get_clock(io), mode); | |
6138 | io->timer.function = io_link_timeout_fn; | |
6139 | hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode); | |
6140 | return 0; | |
6141 | } | |
6142 | ||
9c8e11b3 PB |
6143 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
6144 | struct timespec64 *ts, enum hrtimer_mode mode) | |
89850fce | 6145 | __must_hold(&ctx->timeout_lock) |
47f46768 | 6146 | { |
9c8e11b3 PB |
6147 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
6148 | struct io_timeout_data *data; | |
47f46768 | 6149 | |
9c8e11b3 PB |
6150 | if (IS_ERR(req)) |
6151 | return PTR_ERR(req); | |
47f46768 | 6152 | |
9c8e11b3 PB |
6153 | req->timeout.off = 0; /* noseq */ |
6154 | data = req->async_data; | |
6155 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
50c1df2b | 6156 | hrtimer_init(&data->timer, io_timeout_get_clock(data), mode); |
9c8e11b3 PB |
6157 | data->timer.function = io_timeout_fn; |
6158 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
6159 | return 0; | |
47f46768 JA |
6160 | } |
6161 | ||
3529d8c2 JA |
6162 | static int io_timeout_remove_prep(struct io_kiocb *req, |
6163 | const struct io_uring_sqe *sqe) | |
b29472ee | 6164 | { |
9c8e11b3 PB |
6165 | struct io_timeout_rem *tr = &req->timeout_rem; |
6166 | ||
b29472ee JA |
6167 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
6168 | return -EINVAL; | |
61710e43 DA |
6169 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6170 | return -EINVAL; | |
26578cda | 6171 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
b29472ee JA |
6172 | return -EINVAL; |
6173 | ||
f1042b6c | 6174 | tr->ltimeout = false; |
9c8e11b3 PB |
6175 | tr->addr = READ_ONCE(sqe->addr); |
6176 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
f1042b6c PB |
6177 | if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) { |
6178 | if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
6179 | return -EINVAL; | |
6180 | if (tr->flags & IORING_LINK_TIMEOUT_UPDATE) | |
6181 | tr->ltimeout = true; | |
6182 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS)) | |
9c8e11b3 PB |
6183 | return -EINVAL; |
6184 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
6185 | return -EFAULT; | |
2087009c YB |
6186 | if (tr->ts.tv_sec < 0 || tr->ts.tv_nsec < 0) |
6187 | return -EINVAL; | |
9c8e11b3 PB |
6188 | } else if (tr->flags) { |
6189 | /* timeout removal doesn't support flags */ | |
b29472ee | 6190 | return -EINVAL; |
9c8e11b3 | 6191 | } |
b29472ee | 6192 | |
b29472ee JA |
6193 | return 0; |
6194 | } | |
6195 | ||
8662daec PB |
6196 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
6197 | { | |
6198 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
6199 | : HRTIMER_MODE_REL; | |
6200 | } | |
6201 | ||
11365043 JA |
6202 | /* |
6203 | * Remove or update an existing timeout command | |
6204 | */ | |
61e98203 | 6205 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 6206 | { |
9c8e11b3 | 6207 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 6208 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 6209 | int ret; |
11365043 | 6210 | |
ec3c3d0f PB |
6211 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
6212 | spin_lock(&ctx->completion_lock); | |
6213 | spin_lock_irq(&ctx->timeout_lock); | |
9c8e11b3 | 6214 | ret = io_timeout_cancel(ctx, tr->addr); |
ec3c3d0f PB |
6215 | spin_unlock_irq(&ctx->timeout_lock); |
6216 | spin_unlock(&ctx->completion_lock); | |
6217 | } else { | |
f1042b6c PB |
6218 | enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags); |
6219 | ||
ec3c3d0f | 6220 | spin_lock_irq(&ctx->timeout_lock); |
f1042b6c PB |
6221 | if (tr->ltimeout) |
6222 | ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
6223 | else | |
6224 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
ec3c3d0f PB |
6225 | spin_unlock_irq(&ctx->timeout_lock); |
6226 | } | |
11365043 | 6227 | |
4e88d6e7 | 6228 | if (ret < 0) |
93d2bcd2 | 6229 | req_set_fail(req); |
505657bc | 6230 | io_req_complete_post(req, ret, 0); |
11365043 | 6231 | return 0; |
5262f567 JA |
6232 | } |
6233 | ||
3529d8c2 | 6234 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 6235 | bool is_timeout_link) |
5262f567 | 6236 | { |
ad8a48ac | 6237 | struct io_timeout_data *data; |
a41525ab | 6238 | unsigned flags; |
56080b02 | 6239 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 6240 | |
ad8a48ac | 6241 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 6242 | return -EINVAL; |
26578cda PB |
6243 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
6244 | sqe->splice_fd_in) | |
a41525ab | 6245 | return -EINVAL; |
56080b02 | 6246 | if (off && is_timeout_link) |
2d28390a | 6247 | return -EINVAL; |
a41525ab | 6248 | flags = READ_ONCE(sqe->timeout_flags); |
6224590d PB |
6249 | if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK | |
6250 | IORING_TIMEOUT_ETIME_SUCCESS)) | |
50c1df2b JA |
6251 | return -EINVAL; |
6252 | /* more than one clock specified is invalid, obviously */ | |
6253 | if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
5262f567 | 6254 | return -EINVAL; |
bdf20073 | 6255 | |
ef9dd637 | 6256 | INIT_LIST_HEAD(&req->timeout.list); |
bfe68a22 | 6257 | req->timeout.off = off; |
f18ee4cf PB |
6258 | if (unlikely(off && !req->ctx->off_timeout_used)) |
6259 | req->ctx->off_timeout_used = true; | |
26a61679 | 6260 | |
d6a644a7 PB |
6261 | if (WARN_ON_ONCE(req_has_async_data(req))) |
6262 | return -EFAULT; | |
6263 | if (io_alloc_async_data(req)) | |
26a61679 JA |
6264 | return -ENOMEM; |
6265 | ||
e8c2bc1f | 6266 | data = req->async_data; |
ad8a48ac | 6267 | data->req = req; |
50c1df2b | 6268 | data->flags = flags; |
ad8a48ac JA |
6269 | |
6270 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
6271 | return -EFAULT; |
6272 | ||
f6223ff7 YB |
6273 | if (data->ts.tv_sec < 0 || data->ts.tv_nsec < 0) |
6274 | return -EINVAL; | |
6275 | ||
8662daec | 6276 | data->mode = io_translate_timeout_mode(flags); |
50c1df2b | 6277 | hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode); |
b97e736a PB |
6278 | |
6279 | if (is_timeout_link) { | |
6280 | struct io_submit_link *link = &req->ctx->submit_state.link; | |
6281 | ||
6282 | if (!link->head) | |
6283 | return -EINVAL; | |
6284 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) | |
6285 | return -EINVAL; | |
4d13d1a4 PB |
6286 | req->timeout.head = link->last; |
6287 | link->last->flags |= REQ_F_ARM_LTIMEOUT; | |
b97e736a | 6288 | } |
ad8a48ac JA |
6289 | return 0; |
6290 | } | |
6291 | ||
61e98203 | 6292 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 6293 | { |
ad8a48ac | 6294 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 6295 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 6296 | struct list_head *entry; |
bfe68a22 | 6297 | u32 tail, off = req->timeout.off; |
ad8a48ac | 6298 | |
89850fce | 6299 | spin_lock_irq(&ctx->timeout_lock); |
93bd25bb | 6300 | |
5262f567 JA |
6301 | /* |
6302 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
6303 | * timeout event to be satisfied. If it isn't set, then this is |
6304 | * a pure timeout request, sequence isn't used. | |
5262f567 | 6305 | */ |
8eb7e2d0 | 6306 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
6307 | entry = ctx->timeout_list.prev; |
6308 | goto add; | |
6309 | } | |
5262f567 | 6310 | |
bfe68a22 PB |
6311 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
6312 | req->timeout.target_seq = tail + off; | |
5262f567 | 6313 | |
f010505b MDG |
6314 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
6315 | * This is safe because ->completion_lock is held, and submissions | |
6316 | * and completions are never mixed in the same ->completion_lock section. | |
6317 | */ | |
6318 | ctx->cq_last_tm_flush = tail; | |
6319 | ||
5262f567 JA |
6320 | /* |
6321 | * Insertion sort, ensuring the first entry in the list is always | |
6322 | * the one we need first. | |
6323 | */ | |
5262f567 | 6324 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
6325 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
6326 | timeout.list); | |
5262f567 | 6327 | |
8eb7e2d0 | 6328 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 6329 | continue; |
bfe68a22 PB |
6330 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
6331 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
6332 | break; |
6333 | } | |
93bd25bb | 6334 | add: |
135fcde8 | 6335 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
6336 | data->timer.function = io_timeout_fn; |
6337 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
89850fce | 6338 | spin_unlock_irq(&ctx->timeout_lock); |
5262f567 JA |
6339 | return 0; |
6340 | } | |
5262f567 | 6341 | |
f458dd84 PB |
6342 | struct io_cancel_data { |
6343 | struct io_ring_ctx *ctx; | |
6344 | u64 user_data; | |
6345 | }; | |
6346 | ||
62755e35 JA |
6347 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
6348 | { | |
6349 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 6350 | struct io_cancel_data *cd = data; |
62755e35 | 6351 | |
f458dd84 | 6352 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
62755e35 JA |
6353 | } |
6354 | ||
f458dd84 PB |
6355 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
6356 | struct io_ring_ctx *ctx) | |
62755e35 | 6357 | { |
f458dd84 | 6358 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
62755e35 | 6359 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
6360 | int ret = 0; |
6361 | ||
f458dd84 | 6362 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
6363 | return -ENOENT; |
6364 | ||
f458dd84 | 6365 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
62755e35 JA |
6366 | switch (cancel_ret) { |
6367 | case IO_WQ_CANCEL_OK: | |
6368 | ret = 0; | |
6369 | break; | |
6370 | case IO_WQ_CANCEL_RUNNING: | |
6371 | ret = -EALREADY; | |
6372 | break; | |
6373 | case IO_WQ_CANCEL_NOTFOUND: | |
6374 | ret = -ENOENT; | |
6375 | break; | |
6376 | } | |
6377 | ||
e977d6d3 JA |
6378 | return ret; |
6379 | } | |
6380 | ||
8cb01fac | 6381 | static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr) |
47f46768 | 6382 | { |
8cb01fac | 6383 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 JA |
6384 | int ret; |
6385 | ||
dadebc35 | 6386 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
8cb01fac | 6387 | |
f458dd84 | 6388 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
ccbf7261 JA |
6389 | /* |
6390 | * Fall-through even for -EALREADY, as we may have poll armed | |
6391 | * that need unarming. | |
6392 | */ | |
6393 | if (!ret) | |
6394 | return 0; | |
505657bc PB |
6395 | |
6396 | spin_lock(&ctx->completion_lock); | |
ccbf7261 JA |
6397 | ret = io_poll_cancel(ctx, sqe_addr, false); |
6398 | if (ret != -ENOENT) | |
6399 | goto out; | |
6400 | ||
79ebeaee | 6401 | spin_lock_irq(&ctx->timeout_lock); |
47f46768 | 6402 | ret = io_timeout_cancel(ctx, sqe_addr); |
79ebeaee | 6403 | spin_unlock_irq(&ctx->timeout_lock); |
505657bc PB |
6404 | out: |
6405 | spin_unlock(&ctx->completion_lock); | |
6406 | return ret; | |
47f46768 JA |
6407 | } |
6408 | ||
3529d8c2 JA |
6409 | static int io_async_cancel_prep(struct io_kiocb *req, |
6410 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 6411 | { |
fbf23849 | 6412 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 6413 | return -EINVAL; |
61710e43 DA |
6414 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6415 | return -EINVAL; | |
26578cda PB |
6416 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
6417 | sqe->splice_fd_in) | |
e977d6d3 JA |
6418 | return -EINVAL; |
6419 | ||
fbf23849 JA |
6420 | req->cancel.addr = READ_ONCE(sqe->addr); |
6421 | return 0; | |
6422 | } | |
6423 | ||
61e98203 | 6424 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6425 | { |
6426 | struct io_ring_ctx *ctx = req->ctx; | |
58f99373 | 6427 | u64 sqe_addr = req->cancel.addr; |
3b44b371 | 6428 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
58f99373 PB |
6429 | struct io_tctx_node *node; |
6430 | int ret; | |
6431 | ||
8cb01fac | 6432 | ret = io_try_cancel_userdata(req, sqe_addr); |
58f99373 PB |
6433 | if (ret != -ENOENT) |
6434 | goto done; | |
58f99373 PB |
6435 | |
6436 | /* slow path, try all io-wq's */ | |
3b44b371 | 6437 | io_ring_submit_lock(ctx, needs_lock); |
58f99373 PB |
6438 | ret = -ENOENT; |
6439 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
6440 | struct io_uring_task *tctx = node->task->io_uring; | |
fbf23849 | 6441 | |
58f99373 PB |
6442 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
6443 | if (ret != -ENOENT) | |
6444 | break; | |
6445 | } | |
3b44b371 | 6446 | io_ring_submit_unlock(ctx, needs_lock); |
58f99373 | 6447 | done: |
58f99373 | 6448 | if (ret < 0) |
93d2bcd2 | 6449 | req_set_fail(req); |
505657bc | 6450 | io_req_complete_post(req, ret, 0); |
5262f567 JA |
6451 | return 0; |
6452 | } | |
6453 | ||
269bbe5f | 6454 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
6455 | const struct io_uring_sqe *sqe) |
6456 | { | |
61710e43 DA |
6457 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6458 | return -EINVAL; | |
26578cda | 6459 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
05f3fb3c JA |
6460 | return -EINVAL; |
6461 | ||
269bbe5f BM |
6462 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
6463 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
6464 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 6465 | return -EINVAL; |
269bbe5f | 6466 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
6467 | return 0; |
6468 | } | |
6469 | ||
889fca73 | 6470 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6471 | { |
6472 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 6473 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
c3bdad02 | 6474 | struct io_uring_rsrc_update2 up; |
05f3fb3c | 6475 | int ret; |
fbf23849 | 6476 | |
269bbe5f BM |
6477 | up.offset = req->rsrc_update.offset; |
6478 | up.data = req->rsrc_update.arg; | |
c3bdad02 PB |
6479 | up.nr = 0; |
6480 | up.tags = 0; | |
615cee49 | 6481 | up.resv = 0; |
05f3fb3c | 6482 | |
3b44b371 | 6483 | io_ring_submit_lock(ctx, needs_lock); |
fdecb662 | 6484 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
98f0b3b4 | 6485 | &up, req->rsrc_update.nr_args); |
3b44b371 | 6486 | io_ring_submit_unlock(ctx, needs_lock); |
05f3fb3c JA |
6487 | |
6488 | if (ret < 0) | |
93d2bcd2 | 6489 | req_set_fail(req); |
889fca73 | 6490 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
6491 | return 0; |
6492 | } | |
6493 | ||
bfe76559 | 6494 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 6495 | { |
d625c6ee | 6496 | switch (req->opcode) { |
e781573e | 6497 | case IORING_OP_NOP: |
bfe76559 | 6498 | return 0; |
f67676d1 JA |
6499 | case IORING_OP_READV: |
6500 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 6501 | case IORING_OP_READ: |
bfe76559 | 6502 | return io_read_prep(req, sqe); |
f67676d1 JA |
6503 | case IORING_OP_WRITEV: |
6504 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 6505 | case IORING_OP_WRITE: |
bfe76559 | 6506 | return io_write_prep(req, sqe); |
0969e783 | 6507 | case IORING_OP_POLL_ADD: |
bfe76559 | 6508 | return io_poll_add_prep(req, sqe); |
0969e783 | 6509 | case IORING_OP_POLL_REMOVE: |
c5de0036 | 6510 | return io_poll_update_prep(req, sqe); |
8ed8d3c3 | 6511 | case IORING_OP_FSYNC: |
1155c76a | 6512 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 6513 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 6514 | return io_sfr_prep(req, sqe); |
03b1230c | 6515 | case IORING_OP_SENDMSG: |
fddaface | 6516 | case IORING_OP_SEND: |
bfe76559 | 6517 | return io_sendmsg_prep(req, sqe); |
03b1230c | 6518 | case IORING_OP_RECVMSG: |
fddaface | 6519 | case IORING_OP_RECV: |
bfe76559 | 6520 | return io_recvmsg_prep(req, sqe); |
f499a021 | 6521 | case IORING_OP_CONNECT: |
bfe76559 | 6522 | return io_connect_prep(req, sqe); |
2d28390a | 6523 | case IORING_OP_TIMEOUT: |
bfe76559 | 6524 | return io_timeout_prep(req, sqe, false); |
b29472ee | 6525 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 6526 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 6527 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 6528 | return io_async_cancel_prep(req, sqe); |
2d28390a | 6529 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 6530 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 6531 | case IORING_OP_ACCEPT: |
bfe76559 | 6532 | return io_accept_prep(req, sqe); |
d63d1b5e | 6533 | case IORING_OP_FALLOCATE: |
bfe76559 | 6534 | return io_fallocate_prep(req, sqe); |
15b71abe | 6535 | case IORING_OP_OPENAT: |
bfe76559 | 6536 | return io_openat_prep(req, sqe); |
b5dba59e | 6537 | case IORING_OP_CLOSE: |
bfe76559 | 6538 | return io_close_prep(req, sqe); |
05f3fb3c | 6539 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 6540 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6541 | case IORING_OP_STATX: |
bfe76559 | 6542 | return io_statx_prep(req, sqe); |
4840e418 | 6543 | case IORING_OP_FADVISE: |
bfe76559 | 6544 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6545 | case IORING_OP_MADVISE: |
bfe76559 | 6546 | return io_madvise_prep(req, sqe); |
cebdb986 | 6547 | case IORING_OP_OPENAT2: |
bfe76559 | 6548 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6549 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6550 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6551 | case IORING_OP_SPLICE: |
bfe76559 | 6552 | return io_splice_prep(req, sqe); |
ddf0322d | 6553 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6554 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6555 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6556 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6557 | case IORING_OP_TEE: |
bfe76559 | 6558 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6559 | case IORING_OP_SHUTDOWN: |
6560 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6561 | case IORING_OP_RENAMEAT: |
6562 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6563 | case IORING_OP_UNLINKAT: |
6564 | return io_unlinkat_prep(req, sqe); | |
e34a02dc DK |
6565 | case IORING_OP_MKDIRAT: |
6566 | return io_mkdirat_prep(req, sqe); | |
7a8721f8 DK |
6567 | case IORING_OP_SYMLINKAT: |
6568 | return io_symlinkat_prep(req, sqe); | |
cf30da90 DK |
6569 | case IORING_OP_LINKAT: |
6570 | return io_linkat_prep(req, sqe); | |
f67676d1 JA |
6571 | } |
6572 | ||
bfe76559 PB |
6573 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6574 | req->opcode); | |
bd54b6fe | 6575 | return -EINVAL; |
bfe76559 PB |
6576 | } |
6577 | ||
93642ef8 | 6578 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 6579 | { |
b7e298d2 PB |
6580 | if (!io_op_defs[req->opcode].needs_async_setup) |
6581 | return 0; | |
d886e185 | 6582 | if (WARN_ON_ONCE(req_has_async_data(req))) |
b7e298d2 PB |
6583 | return -EFAULT; |
6584 | if (io_alloc_async_data(req)) | |
6585 | return -EAGAIN; | |
6586 | ||
93642ef8 PB |
6587 | switch (req->opcode) { |
6588 | case IORING_OP_READV: | |
93642ef8 PB |
6589 | return io_rw_prep_async(req, READ); |
6590 | case IORING_OP_WRITEV: | |
93642ef8 PB |
6591 | return io_rw_prep_async(req, WRITE); |
6592 | case IORING_OP_SENDMSG: | |
93642ef8 PB |
6593 | return io_sendmsg_prep_async(req); |
6594 | case IORING_OP_RECVMSG: | |
93642ef8 PB |
6595 | return io_recvmsg_prep_async(req); |
6596 | case IORING_OP_CONNECT: | |
6597 | return io_connect_prep_async(req); | |
6598 | } | |
b7e298d2 PB |
6599 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
6600 | req->opcode); | |
6601 | return -EFAULT; | |
f67676d1 JA |
6602 | } |
6603 | ||
9cf7c104 PB |
6604 | static u32 io_get_sequence(struct io_kiocb *req) |
6605 | { | |
a3dbdf54 | 6606 | u32 seq = req->ctx->cached_sq_head; |
9cf7c104 | 6607 | |
a3dbdf54 PB |
6608 | /* need original cached_sq_head, but it was increased for each req */ |
6609 | io_for_each_link(req, req) | |
6610 | seq--; | |
6611 | return seq; | |
9cf7c104 PB |
6612 | } |
6613 | ||
c072481d | 6614 | static __cold void io_drain_req(struct io_kiocb *req) |
de0617e4 | 6615 | { |
a197f664 | 6616 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6617 | struct io_defer_entry *de; |
f67676d1 | 6618 | int ret; |
e0eb71dc | 6619 | u32 seq = io_get_sequence(req); |
3c19966d | 6620 | |
9d858b21 | 6621 | /* Still need defer if there is pending req in defer list. */ |
e302f104 | 6622 | spin_lock(&ctx->completion_lock); |
5e371265 | 6623 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) { |
e302f104 | 6624 | spin_unlock(&ctx->completion_lock); |
e0eb71dc | 6625 | queue: |
10c66904 | 6626 | ctx->drain_active = false; |
e0eb71dc PB |
6627 | io_req_task_queue(req); |
6628 | return; | |
10c66904 | 6629 | } |
e302f104 | 6630 | spin_unlock(&ctx->completion_lock); |
9cf7c104 | 6631 | |
b7e298d2 | 6632 | ret = io_req_prep_async(req); |
e0eb71dc PB |
6633 | if (ret) { |
6634 | fail: | |
6635 | io_req_complete_failed(req, ret); | |
6636 | return; | |
6637 | } | |
cbdcb435 | 6638 | io_prep_async_link(req); |
27dc8338 | 6639 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
76cc33d7 | 6640 | if (!de) { |
1b48773f | 6641 | ret = -ENOMEM; |
e0eb71dc | 6642 | goto fail; |
76cc33d7 | 6643 | } |
2d28390a | 6644 | |
79ebeaee | 6645 | spin_lock(&ctx->completion_lock); |
9cf7c104 | 6646 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
79ebeaee | 6647 | spin_unlock(&ctx->completion_lock); |
27dc8338 | 6648 | kfree(de); |
e0eb71dc | 6649 | goto queue; |
de0617e4 JA |
6650 | } |
6651 | ||
915967f6 | 6652 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 6653 | de->req = req; |
9cf7c104 | 6654 | de->seq = seq; |
27dc8338 | 6655 | list_add_tail(&de->list, &ctx->defer_list); |
79ebeaee | 6656 | spin_unlock(&ctx->completion_lock); |
de0617e4 JA |
6657 | } |
6658 | ||
68fb8979 | 6659 | static void io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6660 | { |
d1fd1c20 PB |
6661 | if (req->flags & REQ_F_BUFFER_SELECTED) |
6662 | io_put_kbuf(req); | |
99bc4c38 | 6663 | |
0e1b6fe3 PB |
6664 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6665 | switch (req->opcode) { | |
6666 | case IORING_OP_READV: | |
6667 | case IORING_OP_READ_FIXED: | |
6668 | case IORING_OP_READ: | |
6669 | case IORING_OP_WRITEV: | |
6670 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6671 | case IORING_OP_WRITE: { |
6672 | struct io_async_rw *io = req->async_data; | |
1dacb4df PB |
6673 | |
6674 | kfree(io->free_iovec); | |
0e1b6fe3 | 6675 | break; |
e8c2bc1f | 6676 | } |
0e1b6fe3 | 6677 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6678 | case IORING_OP_SENDMSG: { |
6679 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
6680 | |
6681 | kfree(io->free_iov); | |
0e1b6fe3 | 6682 | break; |
e8c2bc1f | 6683 | } |
0e1b6fe3 PB |
6684 | case IORING_OP_SPLICE: |
6685 | case IORING_OP_TEE: | |
e1d767f0 PB |
6686 | if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED)) |
6687 | io_put_file(req->splice.file_in); | |
0e1b6fe3 | 6688 | break; |
f3cd4850 JA |
6689 | case IORING_OP_OPENAT: |
6690 | case IORING_OP_OPENAT2: | |
6691 | if (req->open.filename) | |
6692 | putname(req->open.filename); | |
6693 | break; | |
80a261fd JA |
6694 | case IORING_OP_RENAMEAT: |
6695 | putname(req->rename.oldpath); | |
6696 | putname(req->rename.newpath); | |
6697 | break; | |
14a1143b JA |
6698 | case IORING_OP_UNLINKAT: |
6699 | putname(req->unlink.filename); | |
6700 | break; | |
e34a02dc DK |
6701 | case IORING_OP_MKDIRAT: |
6702 | putname(req->mkdir.filename); | |
6703 | break; | |
7a8721f8 DK |
6704 | case IORING_OP_SYMLINKAT: |
6705 | putname(req->symlink.oldpath); | |
6706 | putname(req->symlink.newpath); | |
6707 | break; | |
cf30da90 DK |
6708 | case IORING_OP_LINKAT: |
6709 | putname(req->hardlink.oldpath); | |
6710 | putname(req->hardlink.newpath); | |
6711 | break; | |
0e1b6fe3 | 6712 | } |
99bc4c38 | 6713 | } |
75652a30 JA |
6714 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
6715 | kfree(req->apoll->double_poll); | |
6716 | kfree(req->apoll); | |
6717 | req->apoll = NULL; | |
6718 | } | |
3a0a6902 PB |
6719 | if (req->flags & REQ_F_INFLIGHT) { |
6720 | struct io_uring_task *tctx = req->task->io_uring; | |
6721 | ||
6722 | atomic_dec(&tctx->inflight_tracked); | |
3a0a6902 | 6723 | } |
c854357b | 6724 | if (req->flags & REQ_F_CREDS) |
b8e64b53 | 6725 | put_cred(req->creds); |
d886e185 PB |
6726 | if (req->flags & REQ_F_ASYNC_DATA) { |
6727 | kfree(req->async_data); | |
6728 | req->async_data = NULL; | |
6729 | } | |
c854357b | 6730 | req->flags &= ~IO_REQ_CLEAN_FLAGS; |
99bc4c38 PB |
6731 | } |
6732 | ||
889fca73 | 6733 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 6734 | { |
5730b27e | 6735 | const struct cred *creds = NULL; |
d625c6ee | 6736 | int ret; |
2b188cc1 | 6737 | |
6878b40e | 6738 | if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred())) |
c10d1f98 | 6739 | creds = override_creds(req->creds); |
5730b27e | 6740 | |
5bd2182d PM |
6741 | if (!io_op_defs[req->opcode].audit_skip) |
6742 | audit_uring_entry(req->opcode); | |
6743 | ||
d625c6ee | 6744 | switch (req->opcode) { |
2b188cc1 | 6745 | case IORING_OP_NOP: |
889fca73 | 6746 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
6747 | break; |
6748 | case IORING_OP_READV: | |
edafccee | 6749 | case IORING_OP_READ_FIXED: |
3a6820f2 | 6750 | case IORING_OP_READ: |
889fca73 | 6751 | ret = io_read(req, issue_flags); |
edafccee | 6752 | break; |
3529d8c2 | 6753 | case IORING_OP_WRITEV: |
edafccee | 6754 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 6755 | case IORING_OP_WRITE: |
889fca73 | 6756 | ret = io_write(req, issue_flags); |
2b188cc1 | 6757 | break; |
c992fe29 | 6758 | case IORING_OP_FSYNC: |
45d189c6 | 6759 | ret = io_fsync(req, issue_flags); |
c992fe29 | 6760 | break; |
221c5eb2 | 6761 | case IORING_OP_POLL_ADD: |
61e98203 | 6762 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
6763 | break; |
6764 | case IORING_OP_POLL_REMOVE: | |
c5de0036 | 6765 | ret = io_poll_update(req, issue_flags); |
221c5eb2 | 6766 | break; |
5d17b4a4 | 6767 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 6768 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 6769 | break; |
0fa03c62 | 6770 | case IORING_OP_SENDMSG: |
889fca73 | 6771 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 6772 | break; |
fddaface | 6773 | case IORING_OP_SEND: |
889fca73 | 6774 | ret = io_send(req, issue_flags); |
0fa03c62 | 6775 | break; |
aa1fa28f | 6776 | case IORING_OP_RECVMSG: |
889fca73 | 6777 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 6778 | break; |
fddaface | 6779 | case IORING_OP_RECV: |
889fca73 | 6780 | ret = io_recv(req, issue_flags); |
aa1fa28f | 6781 | break; |
5262f567 | 6782 | case IORING_OP_TIMEOUT: |
61e98203 | 6783 | ret = io_timeout(req, issue_flags); |
5262f567 | 6784 | break; |
11365043 | 6785 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 6786 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 6787 | break; |
17f2fe35 | 6788 | case IORING_OP_ACCEPT: |
889fca73 | 6789 | ret = io_accept(req, issue_flags); |
17f2fe35 | 6790 | break; |
f8e85cf2 | 6791 | case IORING_OP_CONNECT: |
889fca73 | 6792 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 6793 | break; |
62755e35 | 6794 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 6795 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 6796 | break; |
d63d1b5e | 6797 | case IORING_OP_FALLOCATE: |
45d189c6 | 6798 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 6799 | break; |
15b71abe | 6800 | case IORING_OP_OPENAT: |
45d189c6 | 6801 | ret = io_openat(req, issue_flags); |
15b71abe | 6802 | break; |
b5dba59e | 6803 | case IORING_OP_CLOSE: |
889fca73 | 6804 | ret = io_close(req, issue_flags); |
b5dba59e | 6805 | break; |
05f3fb3c | 6806 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6807 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6808 | break; |
eddc7ef5 | 6809 | case IORING_OP_STATX: |
45d189c6 | 6810 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6811 | break; |
4840e418 | 6812 | case IORING_OP_FADVISE: |
45d189c6 | 6813 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6814 | break; |
c1ca757b | 6815 | case IORING_OP_MADVISE: |
45d189c6 | 6816 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6817 | break; |
cebdb986 | 6818 | case IORING_OP_OPENAT2: |
45d189c6 | 6819 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6820 | break; |
3e4827b0 | 6821 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6822 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6823 | break; |
7d67af2c | 6824 | case IORING_OP_SPLICE: |
45d189c6 | 6825 | ret = io_splice(req, issue_flags); |
7d67af2c | 6826 | break; |
ddf0322d | 6827 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6828 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6829 | break; |
067524e9 | 6830 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6831 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6832 | break; |
f2a8d5c7 | 6833 | case IORING_OP_TEE: |
45d189c6 | 6834 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6835 | break; |
36f4fa68 | 6836 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6837 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6838 | break; |
80a261fd | 6839 | case IORING_OP_RENAMEAT: |
45d189c6 | 6840 | ret = io_renameat(req, issue_flags); |
80a261fd | 6841 | break; |
14a1143b | 6842 | case IORING_OP_UNLINKAT: |
45d189c6 | 6843 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6844 | break; |
e34a02dc DK |
6845 | case IORING_OP_MKDIRAT: |
6846 | ret = io_mkdirat(req, issue_flags); | |
6847 | break; | |
7a8721f8 DK |
6848 | case IORING_OP_SYMLINKAT: |
6849 | ret = io_symlinkat(req, issue_flags); | |
6850 | break; | |
cf30da90 DK |
6851 | case IORING_OP_LINKAT: |
6852 | ret = io_linkat(req, issue_flags); | |
6853 | break; | |
2b188cc1 JA |
6854 | default: |
6855 | ret = -EINVAL; | |
6856 | break; | |
6857 | } | |
6858 | ||
5bd2182d PM |
6859 | if (!io_op_defs[req->opcode].audit_skip) |
6860 | audit_uring_exit(!ret, ret); | |
6861 | ||
5730b27e JA |
6862 | if (creds) |
6863 | revert_creds(creds); | |
def596e9 JA |
6864 | if (ret) |
6865 | return ret; | |
b532576e | 6866 | /* If the op doesn't have a file, we're not polling for it */ |
9983028e | 6867 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
9882131c | 6868 | io_iopoll_req_issued(req, issue_flags); |
def596e9 JA |
6869 | |
6870 | return 0; | |
2b188cc1 JA |
6871 | } |
6872 | ||
ebc11b6c PB |
6873 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
6874 | { | |
6875 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6876 | ||
6877 | req = io_put_req_find_next(req); | |
6878 | return req ? &req->work : NULL; | |
6879 | } | |
6880 | ||
5280f7e5 | 6881 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6882 | { |
6883 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
d01905db PB |
6884 | unsigned int issue_flags = IO_URING_F_UNLOCKED; |
6885 | bool needs_poll = false; | |
6df1db6b | 6886 | struct io_kiocb *timeout; |
561fb04a | 6887 | int ret = 0; |
2b188cc1 | 6888 | |
48dcd38d PB |
6889 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
6890 | if (!(req->flags & REQ_F_REFCOUNT)) | |
6891 | __io_req_set_refcount(req, 2); | |
6892 | else | |
6893 | req_ref_get(req); | |
5d5901a3 | 6894 | |
6df1db6b PB |
6895 | timeout = io_prep_linked_timeout(req); |
6896 | if (timeout) | |
6897 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6898 | |
dadebc35 | 6899 | /* either cancelled or io-wq is dying, so don't touch tctx->iowq */ |
d01905db PB |
6900 | if (work->flags & IO_WQ_WORK_CANCEL) { |
6901 | io_req_task_queue_fail(req, -ECANCELED); | |
6902 | return; | |
6903 | } | |
31b51510 | 6904 | |
d01905db | 6905 | if (req->flags & REQ_F_FORCE_ASYNC) { |
afb7f56f PB |
6906 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
6907 | bool opcode_poll = def->pollin || def->pollout; | |
6908 | ||
6909 | if (opcode_poll && file_can_poll(req->file)) { | |
6910 | needs_poll = true; | |
d01905db | 6911 | issue_flags |= IO_URING_F_NONBLOCK; |
afb7f56f | 6912 | } |
561fb04a | 6913 | } |
31b51510 | 6914 | |
d01905db PB |
6915 | do { |
6916 | ret = io_issue_sqe(req, issue_flags); | |
6917 | if (ret != -EAGAIN) | |
6918 | break; | |
6919 | /* | |
6920 | * We can get EAGAIN for iopolled IO even though we're | |
6921 | * forcing a sync submission from here, since we can't | |
6922 | * wait for request slots on the block side. | |
6923 | */ | |
6924 | if (!needs_poll) { | |
6925 | cond_resched(); | |
6926 | continue; | |
90fa0288 HX |
6927 | } |
6928 | ||
d01905db PB |
6929 | if (io_arm_poll_handler(req) == IO_APOLL_OK) |
6930 | return; | |
6931 | /* aborted or ready, in either case retry blocking */ | |
6932 | needs_poll = false; | |
6933 | issue_flags &= ~IO_URING_F_NONBLOCK; | |
6934 | } while (1); | |
31b51510 | 6935 | |
a3df7698 | 6936 | /* avoid locking problems by failing it from a clean context */ |
5d5901a3 | 6937 | if (ret) |
a3df7698 | 6938 | io_req_task_queue_fail(req, ret); |
2b188cc1 JA |
6939 | } |
6940 | ||
aeca241b | 6941 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
042b0d85 | 6942 | unsigned i) |
65e19f54 | 6943 | { |
042b0d85 | 6944 | return &table->files[i]; |
dafecf19 PB |
6945 | } |
6946 | ||
65e19f54 JA |
6947 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6948 | int index) | |
6949 | { | |
aeca241b | 6950 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
65e19f54 | 6951 | |
a04b0ac0 | 6952 | return (struct file *) (slot->file_ptr & FFS_MASK); |
65e19f54 JA |
6953 | } |
6954 | ||
a04b0ac0 | 6955 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
9a321c98 PB |
6956 | { |
6957 | unsigned long file_ptr = (unsigned long) file; | |
6958 | ||
88459b50 | 6959 | file_ptr |= io_file_get_flags(file); |
a04b0ac0 | 6960 | file_slot->file_ptr = file_ptr; |
65e19f54 JA |
6961 | } |
6962 | ||
ac177053 PB |
6963 | static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx, |
6964 | struct io_kiocb *req, int fd) | |
09bb8394 | 6965 | { |
8da11c19 | 6966 | struct file *file; |
ac177053 | 6967 | unsigned long file_ptr; |
09bb8394 | 6968 | |
ac177053 PB |
6969 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
6970 | return NULL; | |
6971 | fd = array_index_nospec(fd, ctx->nr_user_files); | |
6972 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; | |
6973 | file = (struct file *) (file_ptr & FFS_MASK); | |
6974 | file_ptr &= ~FFS_MASK; | |
6975 | /* mask in overlapping REQ_F and FFS bits */ | |
35645ac3 | 6976 | req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT); |
a46be971 | 6977 | io_req_set_rsrc_node(req, ctx); |
ac177053 PB |
6978 | return file; |
6979 | } | |
d44f554e | 6980 | |
ac177053 | 6981 | static struct file *io_file_get_normal(struct io_ring_ctx *ctx, |
ac177053 PB |
6982 | struct io_kiocb *req, int fd) |
6983 | { | |
62906e89 | 6984 | struct file *file = fget(fd); |
ac177053 PB |
6985 | |
6986 | trace_io_uring_file_get(ctx, fd); | |
09bb8394 | 6987 | |
ac177053 PB |
6988 | /* we don't allow fixed io_uring files */ |
6989 | if (file && unlikely(file->f_op == &io_uring_fops)) | |
6990 | io_req_track_inflight(req); | |
8371adf5 | 6991 | return file; |
09bb8394 JA |
6992 | } |
6993 | ||
ac177053 | 6994 | static inline struct file *io_file_get(struct io_ring_ctx *ctx, |
ac177053 PB |
6995 | struct io_kiocb *req, int fd, bool fixed) |
6996 | { | |
6997 | if (fixed) | |
6998 | return io_file_get_fixed(ctx, req, fd); | |
6999 | else | |
62906e89 | 7000 | return io_file_get_normal(ctx, req, fd); |
ac177053 PB |
7001 | } |
7002 | ||
f237c30a | 7003 | static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked) |
89b263f6 JA |
7004 | { |
7005 | struct io_kiocb *prev = req->timeout.prev; | |
617a8948 | 7006 | int ret = -ENOENT; |
89b263f6 JA |
7007 | |
7008 | if (prev) { | |
617a8948 PB |
7009 | if (!(req->task->flags & PF_EXITING)) |
7010 | ret = io_try_cancel_userdata(req, prev->user_data); | |
505657bc | 7011 | io_req_complete_post(req, ret ?: -ETIME, 0); |
89b263f6 | 7012 | io_put_req(prev); |
89b263f6 JA |
7013 | } else { |
7014 | io_req_complete_post(req, -ETIME, 0); | |
7015 | } | |
7016 | } | |
7017 | ||
2665abfd | 7018 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 7019 | { |
ad8a48ac JA |
7020 | struct io_timeout_data *data = container_of(timer, |
7021 | struct io_timeout_data, timer); | |
90cd7e42 | 7022 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 7023 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 7024 | unsigned long flags; |
2665abfd | 7025 | |
89b263f6 | 7026 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
90cd7e42 PB |
7027 | prev = req->timeout.head; |
7028 | req->timeout.head = NULL; | |
2665abfd JA |
7029 | |
7030 | /* | |
7031 | * We don't expect the list to be empty, that will only happen if we | |
7032 | * race with the completion of the linked work. | |
7033 | */ | |
447c19f3 | 7034 | if (prev) { |
f2f87370 | 7035 | io_remove_next_linked(prev); |
447c19f3 PB |
7036 | if (!req_ref_inc_not_zero(prev)) |
7037 | prev = NULL; | |
7038 | } | |
ef9dd637 | 7039 | list_del(&req->timeout.list); |
89b263f6 JA |
7040 | req->timeout.prev = prev; |
7041 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); | |
2665abfd | 7042 | |
89b263f6 | 7043 | req->io_task_work.func = io_req_task_link_timeout; |
4813c377 | 7044 | io_req_task_work_add(req, false); |
2665abfd JA |
7045 | return HRTIMER_NORESTART; |
7046 | } | |
7047 | ||
de968c18 | 7048 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 7049 | { |
de968c18 PB |
7050 | struct io_ring_ctx *ctx = req->ctx; |
7051 | ||
89b263f6 | 7052 | spin_lock_irq(&ctx->timeout_lock); |
76a46e06 | 7053 | /* |
f2f87370 PB |
7054 | * If the back reference is NULL, then our linked request finished |
7055 | * before we got a chance to setup the timer | |
76a46e06 | 7056 | */ |
90cd7e42 | 7057 | if (req->timeout.head) { |
e8c2bc1f | 7058 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 7059 | |
ad8a48ac JA |
7060 | data->timer.function = io_link_timeout_fn; |
7061 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
7062 | data->mode); | |
ef9dd637 | 7063 | list_add_tail(&req->timeout.list, &ctx->ltimeout_list); |
2665abfd | 7064 | } |
89b263f6 | 7065 | spin_unlock_irq(&ctx->timeout_lock); |
2665abfd | 7066 | /* drop submission reference */ |
76a46e06 JA |
7067 | io_put_req(req); |
7068 | } | |
2665abfd | 7069 | |
d475a9a6 PB |
7070 | static void io_queue_sqe_arm_apoll(struct io_kiocb *req) |
7071 | __must_hold(&req->ctx->uring_lock) | |
7072 | { | |
7073 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); | |
7074 | ||
7075 | switch (io_arm_poll_handler(req)) { | |
7076 | case IO_APOLL_READY: | |
d475a9a6 PB |
7077 | io_req_task_queue(req); |
7078 | break; | |
7079 | case IO_APOLL_ABORTED: | |
7080 | /* | |
7081 | * Queued up for async execution, worker will release | |
7082 | * submit reference when the iocb is actually submitted. | |
7083 | */ | |
7084 | io_queue_async_work(req, NULL); | |
7085 | break; | |
7086 | } | |
7087 | ||
7088 | if (linked_timeout) | |
7089 | io_queue_linked_timeout(linked_timeout); | |
7090 | } | |
7091 | ||
7092 | static inline void __io_queue_sqe(struct io_kiocb *req) | |
282cdc86 | 7093 | __must_hold(&req->ctx->uring_lock) |
2b188cc1 | 7094 | { |
906c6caa | 7095 | struct io_kiocb *linked_timeout; |
e0c5c576 | 7096 | int ret; |
2b188cc1 | 7097 | |
c5eef2b9 | 7098 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 7099 | |
fff4e40e PB |
7100 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
7101 | io_req_add_compl_list(req); | |
d9f9d284 | 7102 | return; |
fff4e40e | 7103 | } |
491381ce JA |
7104 | /* |
7105 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
7106 | * doesn't support non-blocking read/write attempts | |
7107 | */ | |
1840038e | 7108 | if (likely(!ret)) { |
906c6caa PB |
7109 | linked_timeout = io_prep_linked_timeout(req); |
7110 | if (linked_timeout) | |
7111 | io_queue_linked_timeout(linked_timeout); | |
1840038e | 7112 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
d475a9a6 | 7113 | io_queue_sqe_arm_apoll(req); |
0d63c148 | 7114 | } else { |
f41db273 | 7115 | io_req_complete_failed(req, ret); |
9e645e11 | 7116 | } |
2b188cc1 JA |
7117 | } |
7118 | ||
4652fe3f | 7119 | static void io_queue_sqe_fallback(struct io_kiocb *req) |
282cdc86 | 7120 | __must_hold(&req->ctx->uring_lock) |
4fe2c963 | 7121 | { |
4652fe3f | 7122 | if (req->flags & REQ_F_FAIL) { |
c6d3d9cb | 7123 | io_req_complete_fail_submit(req); |
e0eb71dc PB |
7124 | } else if (unlikely(req->ctx->drain_active)) { |
7125 | io_drain_req(req); | |
76cc33d7 PB |
7126 | } else { |
7127 | int ret = io_req_prep_async(req); | |
7128 | ||
7129 | if (unlikely(ret)) | |
7130 | io_req_complete_failed(req, ret); | |
7131 | else | |
f237c30a | 7132 | io_queue_async_work(req, NULL); |
ce35a47a | 7133 | } |
4fe2c963 JL |
7134 | } |
7135 | ||
4652fe3f PB |
7136 | static inline void io_queue_sqe(struct io_kiocb *req) |
7137 | __must_hold(&req->ctx->uring_lock) | |
7138 | { | |
7139 | if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) | |
7140 | __io_queue_sqe(req); | |
7141 | else | |
7142 | io_queue_sqe_fallback(req); | |
7143 | } | |
7144 | ||
b16fed66 PB |
7145 | /* |
7146 | * Check SQE restrictions (opcode and flags). | |
7147 | * | |
7148 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
7149 | */ | |
7150 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
7151 | struct io_kiocb *req, | |
7152 | unsigned int sqe_flags) | |
4fe2c963 | 7153 | { |
b16fed66 PB |
7154 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
7155 | return false; | |
7156 | ||
7157 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
7158 | ctx->restrictions.sqe_flags_required) | |
7159 | return false; | |
7160 | ||
7161 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
7162 | ctx->restrictions.sqe_flags_required)) | |
7163 | return false; | |
7164 | ||
7165 | return true; | |
4fe2c963 JL |
7166 | } |
7167 | ||
22b2ca31 PB |
7168 | static void io_init_req_drain(struct io_kiocb *req) |
7169 | { | |
7170 | struct io_ring_ctx *ctx = req->ctx; | |
7171 | struct io_kiocb *head = ctx->submit_state.link.head; | |
7172 | ||
7173 | ctx->drain_active = true; | |
7174 | if (head) { | |
7175 | /* | |
7176 | * If we need to drain a request in the middle of a link, drain | |
7177 | * the head request and the next request/link after the current | |
7178 | * link. Considering sequential execution of links, | |
b6c7db32 | 7179 | * REQ_F_IO_DRAIN will be maintained for every request of our |
22b2ca31 PB |
7180 | * link. |
7181 | */ | |
b6c7db32 | 7182 | head->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; |
22b2ca31 PB |
7183 | ctx->drain_next = true; |
7184 | } | |
7185 | } | |
7186 | ||
b16fed66 PB |
7187 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
7188 | const struct io_uring_sqe *sqe) | |
282cdc86 | 7189 | __must_hold(&ctx->uring_lock) |
b16fed66 | 7190 | { |
b16fed66 | 7191 | unsigned int sqe_flags; |
fc0ae024 | 7192 | int personality; |
4a04d1d1 | 7193 | u8 opcode; |
b16fed66 | 7194 | |
864ea921 | 7195 | /* req is partially pre-initialised, see io_preinit_req() */ |
4a04d1d1 | 7196 | req->opcode = opcode = READ_ONCE(sqe->opcode); |
b16fed66 PB |
7197 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
7198 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
7199 | req->user_data = READ_ONCE(sqe->user_data); | |
b16fed66 | 7200 | req->file = NULL; |
b16fed66 | 7201 | req->fixed_rsrc_refs = NULL; |
b16fed66 | 7202 | req->task = current; |
b16fed66 | 7203 | |
4a04d1d1 PB |
7204 | if (unlikely(opcode >= IORING_OP_LAST)) { |
7205 | req->opcode = 0; | |
b16fed66 | 7206 | return -EINVAL; |
4a04d1d1 | 7207 | } |
68fe256a PB |
7208 | if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) { |
7209 | /* enforce forwards compatibility on users */ | |
7210 | if (sqe_flags & ~SQE_VALID_FLAGS) | |
7211 | return -EINVAL; | |
7212 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
4a04d1d1 | 7213 | !io_op_defs[opcode].buffer_select) |
68fe256a | 7214 | return -EOPNOTSUPP; |
5562a8d7 PB |
7215 | if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS) |
7216 | ctx->drain_disabled = true; | |
7217 | if (sqe_flags & IOSQE_IO_DRAIN) { | |
7218 | if (ctx->drain_disabled) | |
7219 | return -EOPNOTSUPP; | |
22b2ca31 | 7220 | io_init_req_drain(req); |
5562a8d7 | 7221 | } |
2a56a9bd PB |
7222 | } |
7223 | if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) { | |
7224 | if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags)) | |
7225 | return -EACCES; | |
7226 | /* knock it to the slow queue path, will be drained there */ | |
7227 | if (ctx->drain_active) | |
7228 | req->flags |= REQ_F_FORCE_ASYNC; | |
7229 | /* if there is no link, we're at "next" request and need to drain */ | |
7230 | if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) { | |
7231 | ctx->drain_next = false; | |
7232 | ctx->drain_active = true; | |
b6c7db32 | 7233 | req->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; |
2a56a9bd | 7234 | } |
68fe256a | 7235 | } |
b16fed66 | 7236 | |
4a04d1d1 | 7237 | if (io_op_defs[opcode].needs_file) { |
6d63416d PB |
7238 | struct io_submit_state *state = &ctx->submit_state; |
7239 | ||
7240 | /* | |
7241 | * Plug now if we have more than 2 IO left after this, and the | |
7242 | * target is potentially a read/write to block based storage. | |
7243 | */ | |
4a04d1d1 | 7244 | if (state->need_plug && io_op_defs[opcode].plug) { |
6d63416d PB |
7245 | state->plug_started = true; |
7246 | state->need_plug = false; | |
5ca7a8b3 | 7247 | blk_start_plug_nr_ios(&state->plug, state->submit_nr); |
6d63416d PB |
7248 | } |
7249 | ||
62906e89 | 7250 | req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd), |
ac177053 | 7251 | (sqe_flags & IOSQE_FIXED_FILE)); |
b16fed66 | 7252 | if (unlikely(!req->file)) |
fc0ae024 | 7253 | return -EBADF; |
b16fed66 | 7254 | } |
863e0560 | 7255 | |
003e8dcc JA |
7256 | personality = READ_ONCE(sqe->personality); |
7257 | if (personality) { | |
cdab10bf LT |
7258 | int ret; |
7259 | ||
c10d1f98 PB |
7260 | req->creds = xa_load(&ctx->personalities, personality); |
7261 | if (!req->creds) | |
003e8dcc | 7262 | return -EINVAL; |
c10d1f98 | 7263 | get_cred(req->creds); |
cdc1404a PM |
7264 | ret = security_uring_override_creds(req->creds); |
7265 | if (ret) { | |
7266 | put_cred(req->creds); | |
7267 | return ret; | |
7268 | } | |
b8e64b53 | 7269 | req->flags |= REQ_F_CREDS; |
003e8dcc | 7270 | } |
b16fed66 | 7271 | |
fc0ae024 | 7272 | return io_req_prep(req, sqe); |
b16fed66 PB |
7273 | } |
7274 | ||
a6b8cadc | 7275 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 7276 | const struct io_uring_sqe *sqe) |
282cdc86 | 7277 | __must_hold(&ctx->uring_lock) |
9e645e11 | 7278 | { |
a1ab7b35 | 7279 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 7280 | int ret; |
9e645e11 | 7281 | |
a6b8cadc PB |
7282 | ret = io_init_req(ctx, req, sqe); |
7283 | if (unlikely(ret)) { | |
a87acfde JA |
7284 | trace_io_uring_req_failed(sqe, ret); |
7285 | ||
a8295b98 | 7286 | /* fail even hard links since we don't submit */ |
de59bc10 | 7287 | if (link->head) { |
a8295b98 HX |
7288 | /* |
7289 | * we can judge a link req is failed or cancelled by if | |
7290 | * REQ_F_FAIL is set, but the head is an exception since | |
7291 | * it may be set REQ_F_FAIL because of other req's failure | |
7292 | * so let's leverage req->result to distinguish if a head | |
7293 | * is set REQ_F_FAIL because of its failure or other req's | |
7294 | * failure so that we can set the correct ret code for it. | |
7295 | * init result here to avoid affecting the normal path. | |
7296 | */ | |
7297 | if (!(link->head->flags & REQ_F_FAIL)) | |
7298 | req_fail_link_node(link->head, -ECANCELED); | |
7299 | } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { | |
7300 | /* | |
7301 | * the current req is a normal req, we should return | |
7302 | * error and thus break the submittion loop. | |
7303 | */ | |
7304 | io_req_complete_failed(req, ret); | |
7305 | return ret; | |
de59bc10 | 7306 | } |
a8295b98 | 7307 | req_fail_link_node(req, ret); |
a6b8cadc | 7308 | } |
441b8a78 | 7309 | |
be7053b7 | 7310 | /* don't need @sqe from now on */ |
236daeae OL |
7311 | trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data, |
7312 | req->flags, true, | |
7313 | ctx->flags & IORING_SETUP_SQPOLL); | |
a6b8cadc | 7314 | |
9e645e11 JA |
7315 | /* |
7316 | * If we already have a head request, queue this one for async | |
7317 | * submittal once the head completes. If we don't have a head but | |
7318 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
7319 | * submitted sync once the chain is complete. If none of those | |
7320 | * conditions are true (normal request), then just queue it. | |
7321 | */ | |
863e0560 PB |
7322 | if (link->head) { |
7323 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 7324 | |
a8295b98 HX |
7325 | if (!(req->flags & REQ_F_FAIL)) { |
7326 | ret = io_req_prep_async(req); | |
7327 | if (unlikely(ret)) { | |
7328 | req_fail_link_node(req, ret); | |
7329 | if (!(head->flags & REQ_F_FAIL)) | |
7330 | req_fail_link_node(head, -ECANCELED); | |
7331 | } | |
7332 | } | |
9d76377f | 7333 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 7334 | link->last->link = req; |
863e0560 | 7335 | link->last = req; |
32fe525b | 7336 | |
f15a3431 PB |
7337 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) |
7338 | return 0; | |
32fe525b | 7339 | /* last request of a link, enqueue the link */ |
f15a3431 PB |
7340 | link->head = NULL; |
7341 | req = head; | |
7342 | } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { | |
7343 | link->head = req; | |
7344 | link->last = req; | |
7345 | return 0; | |
9e645e11 | 7346 | } |
2e6e1fde | 7347 | |
f15a3431 | 7348 | io_queue_sqe(req); |
1d4240cc | 7349 | return 0; |
9e645e11 JA |
7350 | } |
7351 | ||
9a56a232 JA |
7352 | /* |
7353 | * Batched submission is done, ensure local IO is flushed out. | |
7354 | */ | |
553deffd | 7355 | static void io_submit_state_end(struct io_ring_ctx *ctx) |
9a56a232 | 7356 | { |
553deffd PB |
7357 | struct io_submit_state *state = &ctx->submit_state; |
7358 | ||
a1ab7b35 | 7359 | if (state->link.head) |
de59bc10 | 7360 | io_queue_sqe(state->link.head); |
553deffd | 7361 | /* flush only after queuing links as they can generate completions */ |
c450178d | 7362 | io_submit_flush_completions(ctx); |
27926b68 JA |
7363 | if (state->plug_started) |
7364 | blk_finish_plug(&state->plug); | |
9a56a232 JA |
7365 | } |
7366 | ||
7367 | /* | |
7368 | * Start submission side cache. | |
7369 | */ | |
7370 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 7371 | unsigned int max_ios) |
9a56a232 | 7372 | { |
27926b68 | 7373 | state->plug_started = false; |
4b628aeb | 7374 | state->need_plug = max_ios > 2; |
5ca7a8b3 | 7375 | state->submit_nr = max_ios; |
a1ab7b35 PB |
7376 | /* set only head, no need to init link_last in advance */ |
7377 | state->link.head = NULL; | |
9a56a232 JA |
7378 | } |
7379 | ||
2b188cc1 JA |
7380 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
7381 | { | |
75b28aff | 7382 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 7383 | |
caf582c6 PB |
7384 | /* |
7385 | * Ensure any loads from the SQEs are done at this point, | |
7386 | * since once we write the new head, the application could | |
7387 | * write new data to them. | |
7388 | */ | |
7389 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
7390 | } |
7391 | ||
2b188cc1 | 7392 | /* |
dd9ae8a0 | 7393 | * Fetch an sqe, if one is available. Note this returns a pointer to memory |
2b188cc1 JA |
7394 | * that is mapped by userspace. This means that care needs to be taken to |
7395 | * ensure that reads are stable, as we cannot rely on userspace always | |
7396 | * being a good citizen. If members of the sqe are validated and then later | |
7397 | * used, it's important that those reads are done through READ_ONCE() to | |
7398 | * prevent a re-load down the line. | |
7399 | */ | |
709b302f | 7400 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 7401 | { |
ea5ab3b5 | 7402 | unsigned head, mask = ctx->sq_entries - 1; |
17d3aeb3 | 7403 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
2b188cc1 JA |
7404 | |
7405 | /* | |
7406 | * The cached sq head (or cq tail) serves two purposes: | |
7407 | * | |
7408 | * 1) allows us to batch the cost of updating the user visible | |
7409 | * head updates. | |
7410 | * 2) allows the kernel side to track the head on its own, even | |
7411 | * though the application is the one updating it. | |
7412 | */ | |
17d3aeb3 | 7413 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
709b302f PB |
7414 | if (likely(head < ctx->sq_entries)) |
7415 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
7416 | |
7417 | /* drop invalid entries */ | |
15641e42 PB |
7418 | ctx->cq_extra--; |
7419 | WRITE_ONCE(ctx->rings->sq_dropped, | |
7420 | READ_ONCE(ctx->rings->sq_dropped) + 1); | |
709b302f PB |
7421 | return NULL; |
7422 | } | |
7423 | ||
0f212204 | 7424 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
282cdc86 | 7425 | __must_hold(&ctx->uring_lock) |
6c271ce2 | 7426 | { |
69629809 | 7427 | unsigned int entries = io_sqring_entries(ctx); |
46c4e16a | 7428 | int submitted = 0; |
6c271ce2 | 7429 | |
51d48dab | 7430 | if (unlikely(!entries)) |
69629809 | 7431 | return 0; |
ee7d46d9 | 7432 | /* make sure SQ entry isn't read before tail */ |
69629809 | 7433 | nr = min3(nr, ctx->sq_entries, entries); |
9a10867a | 7434 | io_get_task_refs(nr); |
6c271ce2 | 7435 | |
ba88ff11 | 7436 | io_submit_state_start(&ctx->submit_state, nr); |
69629809 | 7437 | do { |
3529d8c2 | 7438 | const struct io_uring_sqe *sqe; |
196be95c | 7439 | struct io_kiocb *req; |
fb5ccc98 | 7440 | |
a33ae9ce | 7441 | if (unlikely(!io_alloc_req_refill(ctx))) { |
196be95c PB |
7442 | if (!submitted) |
7443 | submitted = -EAGAIN; | |
fb5ccc98 | 7444 | break; |
196be95c | 7445 | } |
a33ae9ce | 7446 | req = io_alloc_req(ctx); |
4fccfcbb PB |
7447 | sqe = io_get_sqe(ctx); |
7448 | if (unlikely(!sqe)) { | |
c2b6c6bc | 7449 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
4fccfcbb PB |
7450 | break; |
7451 | } | |
d3656344 JA |
7452 | /* will complete beyond this point, count as submitted */ |
7453 | submitted++; | |
a1ab7b35 | 7454 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 7455 | break; |
69629809 | 7456 | } while (submitted < nr); |
6c271ce2 | 7457 | |
9466f437 PB |
7458 | if (unlikely(submitted != nr)) { |
7459 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 | 7460 | int unused = nr - ref_used; |
9466f437 | 7461 | |
09899b19 | 7462 | current->io_uring->cached_refs += unused; |
9466f437 | 7463 | } |
6c271ce2 | 7464 | |
553deffd | 7465 | io_submit_state_end(ctx); |
ae9428ca PB |
7466 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
7467 | io_commit_sqring(ctx); | |
7468 | ||
6c271ce2 JA |
7469 | return submitted; |
7470 | } | |
7471 | ||
e4b6d902 PB |
7472 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
7473 | { | |
7474 | return READ_ONCE(sqd->state); | |
7475 | } | |
7476 | ||
23b3628e XW |
7477 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
7478 | { | |
7479 | /* Tell userspace we may need a wakeup call */ | |
79ebeaee | 7480 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7481 | WRITE_ONCE(ctx->rings->sq_flags, |
7482 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7483 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7484 | } |
7485 | ||
7486 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
7487 | { | |
79ebeaee | 7488 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7489 | WRITE_ONCE(ctx->rings->sq_flags, |
7490 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7491 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7492 | } |
7493 | ||
08369246 | 7494 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 7495 | { |
c8d1ba58 | 7496 | unsigned int to_submit; |
bdcd3eab | 7497 | int ret = 0; |
6c271ce2 | 7498 | |
c8d1ba58 | 7499 | to_submit = io_sqring_entries(ctx); |
e95eee2d | 7500 | /* if we're handling multiple rings, cap submit size for fairness */ |
4ce8ad95 OL |
7501 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
7502 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; | |
e95eee2d | 7503 | |
5eef4e87 | 7504 | if (!wq_list_empty(&ctx->iopoll_list) || to_submit) { |
948e1947 PB |
7505 | const struct cred *creds = NULL; |
7506 | ||
7507 | if (ctx->sq_creds != current_cred()) | |
7508 | creds = override_creds(ctx->sq_creds); | |
a4c0b3de | 7509 | |
c8d1ba58 | 7510 | mutex_lock(&ctx->uring_lock); |
5eef4e87 | 7511 | if (!wq_list_empty(&ctx->iopoll_list)) |
5ba3c874 | 7512 | io_do_iopoll(ctx, true); |
906a3c6f | 7513 | |
3b763ba1 PB |
7514 | /* |
7515 | * Don't submit if refs are dying, good for io_uring_register(), | |
7516 | * but also it is relied upon by io_ring_exit_work() | |
7517 | */ | |
0298ef96 PB |
7518 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
7519 | !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
08369246 | 7520 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 | 7521 | mutex_unlock(&ctx->uring_lock); |
6c271ce2 | 7522 | |
acfb381d PB |
7523 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
7524 | wake_up(&ctx->sqo_sq_wait); | |
948e1947 PB |
7525 | if (creds) |
7526 | revert_creds(creds); | |
acfb381d | 7527 | } |
6c271ce2 | 7528 | |
08369246 XW |
7529 | return ret; |
7530 | } | |
6c271ce2 | 7531 | |
c072481d | 7532 | static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
08369246 XW |
7533 | { |
7534 | struct io_ring_ctx *ctx; | |
7535 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 7536 | |
c9dca27d PB |
7537 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7538 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); | |
08369246 | 7539 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 7540 | } |
6c271ce2 | 7541 | |
e4b6d902 PB |
7542 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
7543 | { | |
7544 | bool did_sig = false; | |
7545 | struct ksignal ksig; | |
7546 | ||
7547 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || | |
7548 | signal_pending(current)) { | |
7549 | mutex_unlock(&sqd->lock); | |
7550 | if (signal_pending(current)) | |
7551 | did_sig = get_signal(&ksig); | |
7552 | cond_resched(); | |
7553 | mutex_lock(&sqd->lock); | |
7554 | } | |
e4b6d902 PB |
7555 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
7556 | } | |
7557 | ||
c8d1ba58 JA |
7558 | static int io_sq_thread(void *data) |
7559 | { | |
69fb2131 JA |
7560 | struct io_sq_data *sqd = data; |
7561 | struct io_ring_ctx *ctx; | |
a0d9205f | 7562 | unsigned long timeout = 0; |
37d1e2e3 | 7563 | char buf[TASK_COMM_LEN]; |
08369246 | 7564 | DEFINE_WAIT(wait); |
6c271ce2 | 7565 | |
696ee88a | 7566 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
37d1e2e3 | 7567 | set_task_comm(current, buf); |
37d1e2e3 JA |
7568 | |
7569 | if (sqd->sq_cpu != -1) | |
7570 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
7571 | else | |
7572 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
7573 | current->flags |= PF_NO_SETAFFINITY; | |
7574 | ||
5bd2182d PM |
7575 | audit_alloc_kernel(current); |
7576 | ||
09a6f4ef | 7577 | mutex_lock(&sqd->lock); |
e4b6d902 | 7578 | while (1) { |
1a924a80 | 7579 | bool cap_entries, sqt_spin = false; |
c1edbf5f | 7580 | |
e4b6d902 PB |
7581 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
7582 | if (io_sqd_handle_event(sqd)) | |
c7d95613 | 7583 | break; |
08369246 XW |
7584 | timeout = jiffies + sqd->sq_thread_idle; |
7585 | } | |
e4b6d902 | 7586 | |
e95eee2d | 7587 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 7588 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
948e1947 | 7589 | int ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a | 7590 | |
5eef4e87 | 7591 | if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list))) |
08369246 | 7592 | sqt_spin = true; |
69fb2131 | 7593 | } |
dd432ea5 PB |
7594 | if (io_run_task_work()) |
7595 | sqt_spin = true; | |
6c271ce2 | 7596 | |
08369246 | 7597 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 7598 | cond_resched(); |
08369246 XW |
7599 | if (sqt_spin) |
7600 | timeout = jiffies + sqd->sq_thread_idle; | |
7601 | continue; | |
7602 | } | |
7603 | ||
08369246 | 7604 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
dd432ea5 | 7605 | if (!io_sqd_events_pending(sqd) && !current->task_works) { |
1a924a80 PB |
7606 | bool needs_sched = true; |
7607 | ||
724cb4f9 | 7608 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
aaa9f0f4 PB |
7609 | io_ring_set_wakeup_flag(ctx); |
7610 | ||
724cb4f9 | 7611 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
5eef4e87 | 7612 | !wq_list_empty(&ctx->iopoll_list)) { |
724cb4f9 HX |
7613 | needs_sched = false; |
7614 | break; | |
7615 | } | |
7616 | if (io_sqring_entries(ctx)) { | |
7617 | needs_sched = false; | |
7618 | break; | |
7619 | } | |
7620 | } | |
7621 | ||
7622 | if (needs_sched) { | |
7623 | mutex_unlock(&sqd->lock); | |
7624 | schedule(); | |
7625 | mutex_lock(&sqd->lock); | |
7626 | } | |
69fb2131 JA |
7627 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7628 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 7629 | } |
08369246 XW |
7630 | |
7631 | finish_wait(&sqd->wait, &wait); | |
7632 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 | 7633 | } |
28cea78a | 7634 | |
78cc687b | 7635 | io_uring_cancel_generic(true, sqd); |
37d1e2e3 | 7636 | sqd->thread = NULL; |
05962f95 | 7637 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
5f3f26f9 | 7638 | io_ring_set_wakeup_flag(ctx); |
521d6a73 | 7639 | io_run_task_work(); |
734551df PB |
7640 | mutex_unlock(&sqd->lock); |
7641 | ||
5bd2182d PM |
7642 | audit_free(current); |
7643 | ||
37d1e2e3 JA |
7644 | complete(&sqd->exited); |
7645 | do_exit(0); | |
6c271ce2 JA |
7646 | } |
7647 | ||
bda52162 JA |
7648 | struct io_wait_queue { |
7649 | struct wait_queue_entry wq; | |
7650 | struct io_ring_ctx *ctx; | |
5fd46178 | 7651 | unsigned cq_tail; |
bda52162 JA |
7652 | unsigned nr_timeouts; |
7653 | }; | |
7654 | ||
6c503150 | 7655 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
7656 | { |
7657 | struct io_ring_ctx *ctx = iowq->ctx; | |
5fd46178 | 7658 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
bda52162 JA |
7659 | |
7660 | /* | |
d195a66e | 7661 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
7662 | * started waiting. For timeouts, we always want to return to userspace, |
7663 | * regardless of event count. | |
7664 | */ | |
5fd46178 | 7665 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
bda52162 JA |
7666 | } |
7667 | ||
7668 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
7669 | int wake_flags, void *key) | |
7670 | { | |
7671 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
7672 | wq); | |
7673 | ||
6c503150 PB |
7674 | /* |
7675 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
7676 | * the task, and the next invocation will do it. | |
7677 | */ | |
5ed7a37d | 7678 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow)) |
6c503150 PB |
7679 | return autoremove_wake_function(curr, mode, wake_flags, key); |
7680 | return -1; | |
bda52162 JA |
7681 | } |
7682 | ||
af9c1a44 JA |
7683 | static int io_run_task_work_sig(void) |
7684 | { | |
7685 | if (io_run_task_work()) | |
7686 | return 1; | |
7687 | if (!signal_pending(current)) | |
7688 | return 0; | |
0b8cfa97 | 7689 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
792ee0f6 | 7690 | return -ERESTARTSYS; |
af9c1a44 JA |
7691 | return -EINTR; |
7692 | } | |
7693 | ||
eeb60b9a PB |
7694 | /* when returns >0, the caller should retry */ |
7695 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
7696 | struct io_wait_queue *iowq, | |
7697 | signed long *timeout) | |
7698 | { | |
7699 | int ret; | |
7700 | ||
7701 | /* make sure we run task_work before checking for signals */ | |
7702 | ret = io_run_task_work_sig(); | |
7703 | if (ret || io_should_wake(iowq)) | |
7704 | return ret; | |
7705 | /* let the caller flush overflows, retry */ | |
5ed7a37d | 7706 | if (test_bit(0, &ctx->check_cq_overflow)) |
eeb60b9a PB |
7707 | return 1; |
7708 | ||
7709 | *timeout = schedule_timeout(*timeout); | |
7710 | return !*timeout ? -ETIME : 1; | |
7711 | } | |
7712 | ||
2b188cc1 JA |
7713 | /* |
7714 | * Wait until events become available, if we don't already have some. The | |
7715 | * application must reap them itself, as they reside on the shared cq ring. | |
7716 | */ | |
7717 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
7718 | const sigset_t __user *sig, size_t sigsz, |
7719 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 7720 | { |
90291099 | 7721 | struct io_wait_queue iowq; |
75b28aff | 7722 | struct io_rings *rings = ctx->rings; |
c1d5a224 PB |
7723 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
7724 | int ret; | |
2b188cc1 | 7725 | |
b41e9852 | 7726 | do { |
90f67366 | 7727 | io_cqring_overflow_flush(ctx); |
6c503150 | 7728 | if (io_cqring_events(ctx) >= min_events) |
b41e9852 | 7729 | return 0; |
4c6e277c | 7730 | if (!io_run_task_work()) |
b41e9852 | 7731 | break; |
b41e9852 | 7732 | } while (1); |
2b188cc1 | 7733 | |
44df58d4 XW |
7734 | if (uts) { |
7735 | struct timespec64 ts; | |
7736 | ||
7737 | if (get_timespec64(&ts, uts)) | |
7738 | return -EFAULT; | |
7739 | timeout = timespec64_to_jiffies(&ts); | |
7740 | } | |
7741 | ||
2b188cc1 | 7742 | if (sig) { |
9e75ad5d AB |
7743 | #ifdef CONFIG_COMPAT |
7744 | if (in_compat_syscall()) | |
7745 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 7746 | sigsz); |
9e75ad5d AB |
7747 | else |
7748 | #endif | |
b772434b | 7749 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 7750 | |
2b188cc1 JA |
7751 | if (ret) |
7752 | return ret; | |
7753 | } | |
7754 | ||
90291099 PB |
7755 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
7756 | iowq.wq.private = current; | |
7757 | INIT_LIST_HEAD(&iowq.wq.entry); | |
7758 | iowq.ctx = ctx; | |
bda52162 | 7759 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
5fd46178 | 7760 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
90291099 | 7761 | |
c826bd7a | 7762 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 7763 | do { |
ca0a2651 | 7764 | /* if we can't even flush overflow, don't wait for more */ |
90f67366 | 7765 | if (!io_cqring_overflow_flush(ctx)) { |
ca0a2651 JA |
7766 | ret = -EBUSY; |
7767 | break; | |
7768 | } | |
311997b3 | 7769 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
bda52162 | 7770 | TASK_INTERRUPTIBLE); |
eeb60b9a | 7771 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
311997b3 | 7772 | finish_wait(&ctx->cq_wait, &iowq.wq); |
ca0a2651 | 7773 | cond_resched(); |
eeb60b9a | 7774 | } while (ret > 0); |
bda52162 | 7775 | |
b7db41c9 | 7776 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7777 | |
75b28aff | 7778 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7779 | } |
7780 | ||
9123c8ff | 7781 | static void io_free_page_table(void **table, size_t size) |
05f3fb3c | 7782 | { |
9123c8ff | 7783 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
05f3fb3c | 7784 | |
846a4ef2 | 7785 | for (i = 0; i < nr_tables; i++) |
9123c8ff PB |
7786 | kfree(table[i]); |
7787 | kfree(table); | |
7788 | } | |
7789 | ||
c072481d | 7790 | static __cold void **io_alloc_page_table(size_t size) |
9123c8ff PB |
7791 | { |
7792 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); | |
7793 | size_t init_size = size; | |
7794 | void **table; | |
7795 | ||
0bea96f5 | 7796 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7797 | if (!table) |
7798 | return NULL; | |
7799 | ||
7800 | for (i = 0; i < nr_tables; i++) { | |
27f6b318 | 7801 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
9123c8ff | 7802 | |
0bea96f5 | 7803 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7804 | if (!table[i]) { |
7805 | io_free_page_table(table, init_size); | |
7806 | return NULL; | |
7807 | } | |
7808 | size -= this_size; | |
7809 | } | |
7810 | return table; | |
05f3fb3c JA |
7811 | } |
7812 | ||
28a9fe25 | 7813 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
1642b445 | 7814 | { |
28a9fe25 PB |
7815 | percpu_ref_exit(&ref_node->refs); |
7816 | kfree(ref_node); | |
1642b445 PB |
7817 | } |
7818 | ||
c072481d | 7819 | static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
b9bd2bea PB |
7820 | { |
7821 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); | |
7822 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; | |
7823 | unsigned long flags; | |
7824 | bool first_add = false; | |
b36a2050 | 7825 | unsigned long delay = HZ; |
b9bd2bea PB |
7826 | |
7827 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); | |
7828 | node->done = true; | |
7829 | ||
b36a2050 DY |
7830 | /* if we are mid-quiesce then do not delay */ |
7831 | if (node->rsrc_data->quiesce) | |
7832 | delay = 0; | |
7833 | ||
b9bd2bea PB |
7834 | while (!list_empty(&ctx->rsrc_ref_list)) { |
7835 | node = list_first_entry(&ctx->rsrc_ref_list, | |
7836 | struct io_rsrc_node, node); | |
7837 | /* recycle ref nodes in order */ | |
7838 | if (!node->done) | |
7839 | break; | |
7840 | list_del(&node->node); | |
7841 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); | |
7842 | } | |
7843 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); | |
7844 | ||
7845 | if (first_add) | |
b36a2050 | 7846 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
b9bd2bea PB |
7847 | } |
7848 | ||
f6133fbd | 7849 | static struct io_rsrc_node *io_rsrc_node_alloc(void) |
b9bd2bea PB |
7850 | { |
7851 | struct io_rsrc_node *ref_node; | |
7852 | ||
7853 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); | |
7854 | if (!ref_node) | |
7855 | return NULL; | |
7856 | ||
7857 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, | |
7858 | 0, GFP_KERNEL)) { | |
7859 | kfree(ref_node); | |
7860 | return NULL; | |
7861 | } | |
7862 | INIT_LIST_HEAD(&ref_node->node); | |
7863 | INIT_LIST_HEAD(&ref_node->rsrc_list); | |
7864 | ref_node->done = false; | |
7865 | return ref_node; | |
7866 | } | |
7867 | ||
a7f0ed5a PB |
7868 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
7869 | struct io_rsrc_data *data_to_kill) | |
ab409402 | 7870 | __must_hold(&ctx->uring_lock) |
6b06314c | 7871 | { |
a7f0ed5a PB |
7872 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
7873 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); | |
6b06314c | 7874 | |
ab409402 PB |
7875 | io_rsrc_refs_drop(ctx); |
7876 | ||
a7f0ed5a PB |
7877 | if (data_to_kill) { |
7878 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; | |
82fbcfa9 | 7879 | |
a7f0ed5a | 7880 | rsrc_node->rsrc_data = data_to_kill; |
4956b9ea | 7881 | spin_lock_irq(&ctx->rsrc_ref_lock); |
a7f0ed5a | 7882 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
4956b9ea | 7883 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
82fbcfa9 | 7884 | |
3e942498 | 7885 | atomic_inc(&data_to_kill->refs); |
a7f0ed5a PB |
7886 | percpu_ref_kill(&rsrc_node->refs); |
7887 | ctx->rsrc_node = NULL; | |
7888 | } | |
6b06314c | 7889 | |
a7f0ed5a PB |
7890 | if (!ctx->rsrc_node) { |
7891 | ctx->rsrc_node = ctx->rsrc_backup_node; | |
7892 | ctx->rsrc_backup_node = NULL; | |
7893 | } | |
8bad28d8 HX |
7894 | } |
7895 | ||
a7f0ed5a | 7896 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
8dd03afe PB |
7897 | { |
7898 | if (ctx->rsrc_backup_node) | |
7899 | return 0; | |
f6133fbd | 7900 | ctx->rsrc_backup_node = io_rsrc_node_alloc(); |
8dd03afe | 7901 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
8bad28d8 HX |
7902 | } |
7903 | ||
c072481d PB |
7904 | static __cold int io_rsrc_ref_quiesce(struct io_rsrc_data *data, |
7905 | struct io_ring_ctx *ctx) | |
8bad28d8 HX |
7906 | { |
7907 | int ret; | |
05589553 | 7908 | |
215c3902 | 7909 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
8bad28d8 HX |
7910 | if (data->quiesce) |
7911 | return -ENXIO; | |
05589553 | 7912 | |
8bad28d8 | 7913 | data->quiesce = true; |
1ffc5422 | 7914 | do { |
a7f0ed5a | 7915 | ret = io_rsrc_node_switch_start(ctx); |
8dd03afe | 7916 | if (ret) |
f2303b1f | 7917 | break; |
a7f0ed5a | 7918 | io_rsrc_node_switch(ctx, data); |
f2303b1f | 7919 | |
3e942498 PB |
7920 | /* kill initial ref, already quiesced if zero */ |
7921 | if (atomic_dec_and_test(&data->refs)) | |
7922 | break; | |
c018db4a | 7923 | mutex_unlock(&ctx->uring_lock); |
8bad28d8 | 7924 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 | 7925 | ret = wait_for_completion_interruptible(&data->done); |
c018db4a JA |
7926 | if (!ret) { |
7927 | mutex_lock(&ctx->uring_lock); | |
1ffc5422 | 7928 | break; |
c018db4a | 7929 | } |
8bad28d8 | 7930 | |
3e942498 PB |
7931 | atomic_inc(&data->refs); |
7932 | /* wait for all works potentially completing data->done */ | |
7933 | flush_delayed_work(&ctx->rsrc_put_work); | |
cb5e1b81 | 7934 | reinit_completion(&data->done); |
8dd03afe | 7935 | |
1ffc5422 | 7936 | ret = io_run_task_work_sig(); |
8bad28d8 | 7937 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 7938 | } while (ret >= 0); |
8bad28d8 | 7939 | data->quiesce = false; |
05f3fb3c | 7940 | |
8bad28d8 | 7941 | return ret; |
d7954b2b BM |
7942 | } |
7943 | ||
2d091d62 PB |
7944 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
7945 | { | |
7946 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; | |
7947 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; | |
7948 | ||
7949 | return &data->tags[table_idx][off]; | |
7950 | } | |
7951 | ||
44b31f2f | 7952 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
1ad555c6 | 7953 | { |
2d091d62 PB |
7954 | size_t size = data->nr * sizeof(data->tags[0][0]); |
7955 | ||
7956 | if (data->tags) | |
7957 | io_free_page_table((void **)data->tags, size); | |
44b31f2f PB |
7958 | kfree(data); |
7959 | } | |
7960 | ||
c072481d PB |
7961 | static __cold int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
7962 | u64 __user *utags, unsigned nr, | |
7963 | struct io_rsrc_data **pdata) | |
1ad555c6 | 7964 | { |
b895c9a6 | 7965 | struct io_rsrc_data *data; |
2d091d62 | 7966 | int ret = -ENOMEM; |
d878c816 | 7967 | unsigned i; |
1ad555c6 BM |
7968 | |
7969 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7970 | if (!data) | |
d878c816 | 7971 | return -ENOMEM; |
2d091d62 | 7972 | data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0])); |
b60c8dce | 7973 | if (!data->tags) { |
1ad555c6 | 7974 | kfree(data); |
d878c816 PB |
7975 | return -ENOMEM; |
7976 | } | |
2d091d62 PB |
7977 | |
7978 | data->nr = nr; | |
7979 | data->ctx = ctx; | |
7980 | data->do_put = do_put; | |
d878c816 | 7981 | if (utags) { |
2d091d62 | 7982 | ret = -EFAULT; |
d878c816 | 7983 | for (i = 0; i < nr; i++) { |
fdd1dc31 CIK |
7984 | u64 *tag_slot = io_get_tag_slot(data, i); |
7985 | ||
7986 | if (copy_from_user(tag_slot, &utags[i], | |
7987 | sizeof(*tag_slot))) | |
2d091d62 | 7988 | goto fail; |
d878c816 | 7989 | } |
1ad555c6 | 7990 | } |
b60c8dce | 7991 | |
3e942498 | 7992 | atomic_set(&data->refs, 1); |
1ad555c6 | 7993 | init_completion(&data->done); |
d878c816 PB |
7994 | *pdata = data; |
7995 | return 0; | |
2d091d62 PB |
7996 | fail: |
7997 | io_rsrc_data_free(data); | |
7998 | return ret; | |
1ad555c6 BM |
7999 | } |
8000 | ||
9123c8ff PB |
8001 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
8002 | { | |
0bea96f5 PB |
8003 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
8004 | GFP_KERNEL_ACCOUNT); | |
9123c8ff PB |
8005 | return !!table->files; |
8006 | } | |
8007 | ||
042b0d85 | 8008 | static void io_free_file_tables(struct io_file_table *table) |
9123c8ff | 8009 | { |
042b0d85 | 8010 | kvfree(table->files); |
9123c8ff PB |
8011 | table->files = NULL; |
8012 | } | |
8013 | ||
fff4db76 | 8014 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
1ad555c6 | 8015 | { |
fff4db76 PB |
8016 | #if defined(CONFIG_UNIX) |
8017 | if (ctx->ring_sock) { | |
8018 | struct sock *sock = ctx->ring_sock->sk; | |
8019 | struct sk_buff *skb; | |
8020 | ||
8021 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
8022 | kfree_skb(skb); | |
8023 | } | |
8024 | #else | |
8025 | int i; | |
8026 | ||
8027 | for (i = 0; i < ctx->nr_user_files; i++) { | |
8028 | struct file *file; | |
8029 | ||
8030 | file = io_file_from_index(ctx, i); | |
8031 | if (file) | |
8032 | fput(file); | |
8033 | } | |
8034 | #endif | |
042b0d85 | 8035 | io_free_file_tables(&ctx->file_table); |
44b31f2f | 8036 | io_rsrc_data_free(ctx->file_data); |
fff4db76 PB |
8037 | ctx->file_data = NULL; |
8038 | ctx->nr_user_files = 0; | |
1ad555c6 BM |
8039 | } |
8040 | ||
d7954b2b BM |
8041 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
8042 | { | |
d7954b2b BM |
8043 | int ret; |
8044 | ||
08480400 | 8045 | if (!ctx->file_data) |
d7954b2b | 8046 | return -ENXIO; |
08480400 PB |
8047 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
8048 | if (!ret) | |
8049 | __io_sqe_files_unregister(ctx); | |
8050 | return ret; | |
6b06314c JA |
8051 | } |
8052 | ||
37d1e2e3 | 8053 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
09a6f4ef | 8054 | __releases(&sqd->lock) |
37d1e2e3 | 8055 | { |
521d6a73 PB |
8056 | WARN_ON_ONCE(sqd->thread == current); |
8057 | ||
9e138a48 PB |
8058 | /* |
8059 | * Do the dance but not conditional clear_bit() because it'd race with | |
8060 | * other threads incrementing park_pending and setting the bit. | |
8061 | */ | |
37d1e2e3 | 8062 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
9e138a48 PB |
8063 | if (atomic_dec_return(&sqd->park_pending)) |
8064 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
09a6f4ef | 8065 | mutex_unlock(&sqd->lock); |
37d1e2e3 JA |
8066 | } |
8067 | ||
86e0d676 | 8068 | static void io_sq_thread_park(struct io_sq_data *sqd) |
09a6f4ef | 8069 | __acquires(&sqd->lock) |
37d1e2e3 | 8070 | { |
521d6a73 PB |
8071 | WARN_ON_ONCE(sqd->thread == current); |
8072 | ||
9e138a48 | 8073 | atomic_inc(&sqd->park_pending); |
86e0d676 | 8074 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
09a6f4ef | 8075 | mutex_lock(&sqd->lock); |
05962f95 | 8076 | if (sqd->thread) |
86e0d676 | 8077 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
8078 | } |
8079 | ||
8080 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
8081 | { | |
521d6a73 | 8082 | WARN_ON_ONCE(sqd->thread == current); |
88885f66 | 8083 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
521d6a73 | 8084 | |
05962f95 | 8085 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
88885f66 | 8086 | mutex_lock(&sqd->lock); |
e8f98f24 JA |
8087 | if (sqd->thread) |
8088 | wake_up_process(sqd->thread); | |
09a6f4ef | 8089 | mutex_unlock(&sqd->lock); |
05962f95 | 8090 | wait_for_completion(&sqd->exited); |
37d1e2e3 JA |
8091 | } |
8092 | ||
534ca6d6 | 8093 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 8094 | { |
534ca6d6 | 8095 | if (refcount_dec_and_test(&sqd->refs)) { |
9e138a48 PB |
8096 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
8097 | ||
37d1e2e3 JA |
8098 | io_sq_thread_stop(sqd); |
8099 | kfree(sqd); | |
8100 | } | |
8101 | } | |
8102 | ||
8103 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
8104 | { | |
8105 | struct io_sq_data *sqd = ctx->sq_data; | |
8106 | ||
8107 | if (sqd) { | |
05962f95 | 8108 | io_sq_thread_park(sqd); |
521d6a73 | 8109 | list_del_init(&ctx->sqd_list); |
37d1e2e3 | 8110 | io_sqd_update_thread_idle(sqd); |
05962f95 | 8111 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
8112 | |
8113 | io_put_sq_data(sqd); | |
8114 | ctx->sq_data = NULL; | |
534ca6d6 JA |
8115 | } |
8116 | } | |
8117 | ||
aa06165d JA |
8118 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
8119 | { | |
8120 | struct io_ring_ctx *ctx_attach; | |
8121 | struct io_sq_data *sqd; | |
8122 | struct fd f; | |
8123 | ||
8124 | f = fdget(p->wq_fd); | |
8125 | if (!f.file) | |
8126 | return ERR_PTR(-ENXIO); | |
8127 | if (f.file->f_op != &io_uring_fops) { | |
8128 | fdput(f); | |
8129 | return ERR_PTR(-EINVAL); | |
8130 | } | |
8131 | ||
8132 | ctx_attach = f.file->private_data; | |
8133 | sqd = ctx_attach->sq_data; | |
8134 | if (!sqd) { | |
8135 | fdput(f); | |
8136 | return ERR_PTR(-EINVAL); | |
8137 | } | |
5c2469e0 JA |
8138 | if (sqd->task_tgid != current->tgid) { |
8139 | fdput(f); | |
8140 | return ERR_PTR(-EPERM); | |
8141 | } | |
aa06165d JA |
8142 | |
8143 | refcount_inc(&sqd->refs); | |
8144 | fdput(f); | |
8145 | return sqd; | |
8146 | } | |
8147 | ||
26984fbf PB |
8148 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
8149 | bool *attached) | |
534ca6d6 JA |
8150 | { |
8151 | struct io_sq_data *sqd; | |
8152 | ||
26984fbf | 8153 | *attached = false; |
5c2469e0 JA |
8154 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
8155 | sqd = io_attach_sq_data(p); | |
26984fbf PB |
8156 | if (!IS_ERR(sqd)) { |
8157 | *attached = true; | |
5c2469e0 | 8158 | return sqd; |
26984fbf | 8159 | } |
5c2469e0 JA |
8160 | /* fall through for EPERM case, setup new sqd/task */ |
8161 | if (PTR_ERR(sqd) != -EPERM) | |
8162 | return sqd; | |
8163 | } | |
aa06165d | 8164 | |
534ca6d6 JA |
8165 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
8166 | if (!sqd) | |
8167 | return ERR_PTR(-ENOMEM); | |
8168 | ||
9e138a48 | 8169 | atomic_set(&sqd->park_pending, 0); |
534ca6d6 | 8170 | refcount_set(&sqd->refs, 1); |
69fb2131 | 8171 | INIT_LIST_HEAD(&sqd->ctx_list); |
09a6f4ef | 8172 | mutex_init(&sqd->lock); |
534ca6d6 | 8173 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 8174 | init_completion(&sqd->exited); |
534ca6d6 JA |
8175 | return sqd; |
8176 | } | |
8177 | ||
6b06314c | 8178 | #if defined(CONFIG_UNIX) |
6b06314c JA |
8179 | /* |
8180 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
8181 | * the io_uring can be safely unregistered on process exit, even if we have | |
8182 | * loops in the file referencing. | |
8183 | */ | |
8184 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
8185 | { | |
8186 | struct sock *sk = ctx->ring_sock->sk; | |
8187 | struct scm_fp_list *fpl; | |
8188 | struct sk_buff *skb; | |
08a45173 | 8189 | int i, nr_files; |
6b06314c | 8190 | |
6b06314c JA |
8191 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
8192 | if (!fpl) | |
8193 | return -ENOMEM; | |
8194 | ||
8195 | skb = alloc_skb(0, GFP_KERNEL); | |
8196 | if (!skb) { | |
8197 | kfree(fpl); | |
8198 | return -ENOMEM; | |
8199 | } | |
8200 | ||
8201 | skb->sk = sk; | |
6b06314c | 8202 | |
08a45173 | 8203 | nr_files = 0; |
62e398be | 8204 | fpl->user = get_uid(current_user()); |
6b06314c | 8205 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
8206 | struct file *file = io_file_from_index(ctx, i + offset); |
8207 | ||
8208 | if (!file) | |
08a45173 | 8209 | continue; |
65e19f54 | 8210 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
8211 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
8212 | nr_files++; | |
6b06314c JA |
8213 | } |
8214 | ||
08a45173 JA |
8215 | if (nr_files) { |
8216 | fpl->max = SCM_MAX_FD; | |
8217 | fpl->count = nr_files; | |
8218 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 8219 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
8220 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
8221 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 8222 | |
08a45173 JA |
8223 | for (i = 0; i < nr_files; i++) |
8224 | fput(fpl->fp[i]); | |
8225 | } else { | |
8226 | kfree_skb(skb); | |
8227 | kfree(fpl); | |
8228 | } | |
6b06314c JA |
8229 | |
8230 | return 0; | |
8231 | } | |
8232 | ||
8233 | /* | |
8234 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
8235 | * causes regular reference counting to break down. We rely on the UNIX | |
8236 | * garbage collection to take care of this problem for us. | |
8237 | */ | |
8238 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8239 | { | |
8240 | unsigned left, total; | |
8241 | int ret = 0; | |
8242 | ||
8243 | total = 0; | |
8244 | left = ctx->nr_user_files; | |
8245 | while (left) { | |
8246 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
8247 | |
8248 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
8249 | if (ret) | |
8250 | break; | |
8251 | left -= this_files; | |
8252 | total += this_files; | |
8253 | } | |
8254 | ||
8255 | if (!ret) | |
8256 | return 0; | |
8257 | ||
8258 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
8259 | struct file *file = io_file_from_index(ctx, total); |
8260 | ||
8261 | if (file) | |
8262 | fput(file); | |
6b06314c JA |
8263 | total++; |
8264 | } | |
8265 | ||
8266 | return ret; | |
8267 | } | |
8268 | #else | |
8269 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8270 | { | |
8271 | return 0; | |
8272 | } | |
8273 | #endif | |
8274 | ||
47e90392 | 8275 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 8276 | { |
50238531 | 8277 | struct file *file = prsrc->file; |
05f3fb3c JA |
8278 | #if defined(CONFIG_UNIX) |
8279 | struct sock *sock = ctx->ring_sock->sk; | |
8280 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
8281 | struct sk_buff *skb; | |
8282 | int i; | |
8283 | ||
8284 | __skb_queue_head_init(&list); | |
8285 | ||
8286 | /* | |
8287 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
8288 | * remove this entry and rearrange the file array. | |
8289 | */ | |
8290 | skb = skb_dequeue(head); | |
8291 | while (skb) { | |
8292 | struct scm_fp_list *fp; | |
8293 | ||
8294 | fp = UNIXCB(skb).fp; | |
8295 | for (i = 0; i < fp->count; i++) { | |
8296 | int left; | |
8297 | ||
8298 | if (fp->fp[i] != file) | |
8299 | continue; | |
8300 | ||
8301 | unix_notinflight(fp->user, fp->fp[i]); | |
8302 | left = fp->count - 1 - i; | |
8303 | if (left) { | |
8304 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
8305 | left * sizeof(struct file *)); | |
8306 | } | |
8307 | fp->count--; | |
8308 | if (!fp->count) { | |
8309 | kfree_skb(skb); | |
8310 | skb = NULL; | |
8311 | } else { | |
8312 | __skb_queue_tail(&list, skb); | |
8313 | } | |
8314 | fput(file); | |
8315 | file = NULL; | |
8316 | break; | |
8317 | } | |
8318 | ||
8319 | if (!file) | |
8320 | break; | |
8321 | ||
8322 | __skb_queue_tail(&list, skb); | |
8323 | ||
8324 | skb = skb_dequeue(head); | |
8325 | } | |
8326 | ||
8327 | if (skb_peek(&list)) { | |
8328 | spin_lock_irq(&head->lock); | |
8329 | while ((skb = __skb_dequeue(&list)) != NULL) | |
8330 | __skb_queue_tail(head, skb); | |
8331 | spin_unlock_irq(&head->lock); | |
8332 | } | |
8333 | #else | |
8334 | fput(file); | |
8335 | #endif | |
8336 | } | |
8337 | ||
b895c9a6 | 8338 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
65e19f54 | 8339 | { |
b895c9a6 | 8340 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
269bbe5f BM |
8341 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
8342 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 8343 | |
269bbe5f BM |
8344 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
8345 | list_del(&prsrc->list); | |
b60c8dce PB |
8346 | |
8347 | if (prsrc->tag) { | |
8348 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; | |
b60c8dce PB |
8349 | |
8350 | io_ring_submit_lock(ctx, lock_ring); | |
79ebeaee | 8351 | spin_lock(&ctx->completion_lock); |
913a571a | 8352 | io_fill_cqe_aux(ctx, prsrc->tag, 0, 0); |
b60c8dce | 8353 | io_commit_cqring(ctx); |
79ebeaee | 8354 | spin_unlock(&ctx->completion_lock); |
b60c8dce PB |
8355 | io_cqring_ev_posted(ctx); |
8356 | io_ring_submit_unlock(ctx, lock_ring); | |
8357 | } | |
8358 | ||
40ae0ff7 | 8359 | rsrc_data->do_put(ctx, prsrc); |
269bbe5f | 8360 | kfree(prsrc); |
65e19f54 | 8361 | } |
05589553 | 8362 | |
28a9fe25 | 8363 | io_rsrc_node_destroy(ref_node); |
3e942498 PB |
8364 | if (atomic_dec_and_test(&rsrc_data->refs)) |
8365 | complete(&rsrc_data->done); | |
2faf852d | 8366 | } |
65e19f54 | 8367 | |
269bbe5f | 8368 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
8369 | { |
8370 | struct io_ring_ctx *ctx; | |
8371 | struct llist_node *node; | |
8372 | ||
269bbe5f BM |
8373 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
8374 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
8375 | |
8376 | while (node) { | |
b895c9a6 | 8377 | struct io_rsrc_node *ref_node; |
4a38aed2 JA |
8378 | struct llist_node *next = node->next; |
8379 | ||
b895c9a6 | 8380 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
269bbe5f | 8381 | __io_rsrc_put_work(ref_node); |
4a38aed2 JA |
8382 | node = next; |
8383 | } | |
8384 | } | |
8385 | ||
6b06314c | 8386 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
792e3582 | 8387 | unsigned nr_args, u64 __user *tags) |
6b06314c JA |
8388 | { |
8389 | __s32 __user *fds = (__s32 __user *) arg; | |
05f3fb3c | 8390 | struct file *file; |
f3baed39 | 8391 | int fd, ret; |
846a4ef2 | 8392 | unsigned i; |
6b06314c | 8393 | |
05f3fb3c | 8394 | if (ctx->file_data) |
6b06314c JA |
8395 | return -EBUSY; |
8396 | if (!nr_args) | |
8397 | return -EINVAL; | |
8398 | if (nr_args > IORING_MAX_FIXED_FILES) | |
8399 | return -EMFILE; | |
3a1b8a4e PB |
8400 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
8401 | return -EMFILE; | |
a7f0ed5a | 8402 | ret = io_rsrc_node_switch_start(ctx); |
f3baed39 PB |
8403 | if (ret) |
8404 | return ret; | |
d878c816 PB |
8405 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
8406 | &ctx->file_data); | |
8407 | if (ret) | |
8408 | return ret; | |
6b06314c | 8409 | |
f3baed39 | 8410 | ret = -ENOMEM; |
aeca241b | 8411 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
1ad555c6 | 8412 | goto out_free; |
65e19f54 | 8413 | |
08a45173 | 8414 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
d878c816 | 8415 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
600cf3f8 PB |
8416 | ret = -EFAULT; |
8417 | goto out_fput; | |
8418 | } | |
08a45173 | 8419 | /* allow sparse sets */ |
792e3582 PB |
8420 | if (fd == -1) { |
8421 | ret = -EINVAL; | |
2d091d62 | 8422 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
792e3582 | 8423 | goto out_fput; |
08a45173 | 8424 | continue; |
792e3582 | 8425 | } |
6b06314c | 8426 | |
05f3fb3c | 8427 | file = fget(fd); |
6b06314c | 8428 | ret = -EBADF; |
792e3582 | 8429 | if (unlikely(!file)) |
600cf3f8 | 8430 | goto out_fput; |
05f3fb3c | 8431 | |
6b06314c JA |
8432 | /* |
8433 | * Don't allow io_uring instances to be registered. If UNIX | |
8434 | * isn't enabled, then this causes a reference cycle and this | |
8435 | * instance can never get freed. If UNIX is enabled we'll | |
8436 | * handle it just fine, but there's still no point in allowing | |
8437 | * a ring fd as it doesn't support regular read/write anyway. | |
8438 | */ | |
05f3fb3c JA |
8439 | if (file->f_op == &io_uring_fops) { |
8440 | fput(file); | |
600cf3f8 | 8441 | goto out_fput; |
6b06314c | 8442 | } |
aeca241b | 8443 | io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file); |
6b06314c JA |
8444 | } |
8445 | ||
6b06314c | 8446 | ret = io_sqe_files_scm(ctx); |
05589553 | 8447 | if (ret) { |
08480400 | 8448 | __io_sqe_files_unregister(ctx); |
05589553 XW |
8449 | return ret; |
8450 | } | |
6b06314c | 8451 | |
a7f0ed5a | 8452 | io_rsrc_node_switch(ctx, NULL); |
6b06314c | 8453 | return ret; |
600cf3f8 PB |
8454 | out_fput: |
8455 | for (i = 0; i < ctx->nr_user_files; i++) { | |
8456 | file = io_file_from_index(ctx, i); | |
8457 | if (file) | |
8458 | fput(file); | |
8459 | } | |
042b0d85 | 8460 | io_free_file_tables(&ctx->file_table); |
600cf3f8 | 8461 | ctx->nr_user_files = 0; |
600cf3f8 | 8462 | out_free: |
44b31f2f | 8463 | io_rsrc_data_free(ctx->file_data); |
55cbc256 | 8464 | ctx->file_data = NULL; |
6b06314c JA |
8465 | return ret; |
8466 | } | |
8467 | ||
c3a31e60 JA |
8468 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
8469 | int index) | |
8470 | { | |
8471 | #if defined(CONFIG_UNIX) | |
8472 | struct sock *sock = ctx->ring_sock->sk; | |
8473 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
8474 | struct sk_buff *skb; | |
8475 | ||
8476 | /* | |
8477 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
8478 | * file set. If there's no room, fall back to allocating a new skb | |
8479 | * and filling it in. | |
8480 | */ | |
8481 | spin_lock_irq(&head->lock); | |
8482 | skb = skb_peek(head); | |
8483 | if (skb) { | |
8484 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
8485 | ||
8486 | if (fpl->count < SCM_MAX_FD) { | |
8487 | __skb_unlink(skb, head); | |
8488 | spin_unlock_irq(&head->lock); | |
8489 | fpl->fp[fpl->count] = get_file(file); | |
8490 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
8491 | fpl->count++; | |
8492 | spin_lock_irq(&head->lock); | |
8493 | __skb_queue_head(head, skb); | |
8494 | } else { | |
8495 | skb = NULL; | |
8496 | } | |
8497 | } | |
8498 | spin_unlock_irq(&head->lock); | |
8499 | ||
8500 | if (skb) { | |
8501 | fput(file); | |
8502 | return 0; | |
8503 | } | |
8504 | ||
8505 | return __io_sqe_files_scm(ctx, 1, index); | |
8506 | #else | |
8507 | return 0; | |
8508 | #endif | |
8509 | } | |
8510 | ||
9c7b0ba8 PB |
8511 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
8512 | struct io_rsrc_node *node, void *rsrc) | |
8513 | { | |
8514 | struct io_rsrc_put *prsrc; | |
8515 | ||
8516 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); | |
8517 | if (!prsrc) | |
8518 | return -ENOMEM; | |
8519 | ||
8520 | prsrc->tag = *io_get_tag_slot(data, idx); | |
8521 | prsrc->rsrc = rsrc; | |
8522 | list_add(&prsrc->list, &node->rsrc_list); | |
8523 | return 0; | |
8524 | } | |
8525 | ||
b9445598 PB |
8526 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
8527 | unsigned int issue_flags, u32 slot_index) | |
8528 | { | |
8529 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 8530 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
9c7b0ba8 | 8531 | bool needs_switch = false; |
b9445598 PB |
8532 | struct io_fixed_file *file_slot; |
8533 | int ret = -EBADF; | |
8534 | ||
3b44b371 | 8535 | io_ring_submit_lock(ctx, needs_lock); |
b9445598 PB |
8536 | if (file->f_op == &io_uring_fops) |
8537 | goto err; | |
8538 | ret = -ENXIO; | |
8539 | if (!ctx->file_data) | |
8540 | goto err; | |
8541 | ret = -EINVAL; | |
8542 | if (slot_index >= ctx->nr_user_files) | |
8543 | goto err; | |
8544 | ||
8545 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); | |
8546 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); | |
9c7b0ba8 PB |
8547 | |
8548 | if (file_slot->file_ptr) { | |
8549 | struct file *old_file; | |
8550 | ||
8551 | ret = io_rsrc_node_switch_start(ctx); | |
8552 | if (ret) | |
8553 | goto err; | |
8554 | ||
8555 | old_file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8556 | ret = io_queue_rsrc_removal(ctx->file_data, slot_index, | |
8557 | ctx->rsrc_node, old_file); | |
8558 | if (ret) | |
8559 | goto err; | |
8560 | file_slot->file_ptr = 0; | |
8561 | needs_switch = true; | |
8562 | } | |
b9445598 PB |
8563 | |
8564 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; | |
8565 | io_fixed_file_set(file_slot, file); | |
8566 | ret = io_sqe_file_register(ctx, file, slot_index); | |
8567 | if (ret) { | |
8568 | file_slot->file_ptr = 0; | |
8569 | goto err; | |
8570 | } | |
8571 | ||
8572 | ret = 0; | |
8573 | err: | |
9c7b0ba8 PB |
8574 | if (needs_switch) |
8575 | io_rsrc_node_switch(ctx, ctx->file_data); | |
3b44b371 | 8576 | io_ring_submit_unlock(ctx, needs_lock); |
b9445598 PB |
8577 | if (ret) |
8578 | fput(file); | |
8579 | return ret; | |
8580 | } | |
8581 | ||
7df778be PB |
8582 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) |
8583 | { | |
8584 | unsigned int offset = req->close.file_slot - 1; | |
8585 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 8586 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
7df778be PB |
8587 | struct io_fixed_file *file_slot; |
8588 | struct file *file; | |
8589 | int ret, i; | |
8590 | ||
3b44b371 | 8591 | io_ring_submit_lock(ctx, needs_lock); |
7df778be PB |
8592 | ret = -ENXIO; |
8593 | if (unlikely(!ctx->file_data)) | |
8594 | goto out; | |
8595 | ret = -EINVAL; | |
8596 | if (offset >= ctx->nr_user_files) | |
8597 | goto out; | |
8598 | ret = io_rsrc_node_switch_start(ctx); | |
8599 | if (ret) | |
8600 | goto out; | |
8601 | ||
8602 | i = array_index_nospec(offset, ctx->nr_user_files); | |
8603 | file_slot = io_fixed_file_slot(&ctx->file_table, i); | |
8604 | ret = -EBADF; | |
8605 | if (!file_slot->file_ptr) | |
8606 | goto out; | |
8607 | ||
8608 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8609 | ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file); | |
8610 | if (ret) | |
8611 | goto out; | |
8612 | ||
8613 | file_slot->file_ptr = 0; | |
8614 | io_rsrc_node_switch(ctx, ctx->file_data); | |
8615 | ret = 0; | |
8616 | out: | |
3b44b371 | 8617 | io_ring_submit_unlock(ctx, needs_lock); |
7df778be PB |
8618 | return ret; |
8619 | } | |
8620 | ||
05f3fb3c | 8621 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
c3bdad02 | 8622 | struct io_uring_rsrc_update2 *up, |
05f3fb3c JA |
8623 | unsigned nr_args) |
8624 | { | |
c3bdad02 | 8625 | u64 __user *tags = u64_to_user_ptr(up->tags); |
98f0b3b4 | 8626 | __s32 __user *fds = u64_to_user_ptr(up->data); |
b895c9a6 | 8627 | struct io_rsrc_data *data = ctx->file_data; |
a04b0ac0 PB |
8628 | struct io_fixed_file *file_slot; |
8629 | struct file *file; | |
98f0b3b4 PB |
8630 | int fd, i, err = 0; |
8631 | unsigned int done; | |
05589553 | 8632 | bool needs_switch = false; |
c3a31e60 | 8633 | |
98f0b3b4 PB |
8634 | if (!ctx->file_data) |
8635 | return -ENXIO; | |
8636 | if (up->offset + nr_args > ctx->nr_user_files) | |
c3a31e60 JA |
8637 | return -EINVAL; |
8638 | ||
67973b93 | 8639 | for (done = 0; done < nr_args; done++) { |
c3bdad02 PB |
8640 | u64 tag = 0; |
8641 | ||
8642 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || | |
8643 | copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
c3a31e60 JA |
8644 | err = -EFAULT; |
8645 | break; | |
8646 | } | |
c3bdad02 PB |
8647 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
8648 | err = -EINVAL; | |
8649 | break; | |
8650 | } | |
4e0377a1 | 8651 | if (fd == IORING_REGISTER_FILES_SKIP) |
8652 | continue; | |
8653 | ||
67973b93 | 8654 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
aeca241b | 8655 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
ea64ec02 | 8656 | |
a04b0ac0 PB |
8657 | if (file_slot->file_ptr) { |
8658 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
b60c8dce PB |
8659 | err = io_queue_rsrc_removal(data, up->offset + done, |
8660 | ctx->rsrc_node, file); | |
a5318d3c HD |
8661 | if (err) |
8662 | break; | |
a04b0ac0 | 8663 | file_slot->file_ptr = 0; |
05589553 | 8664 | needs_switch = true; |
c3a31e60 JA |
8665 | } |
8666 | if (fd != -1) { | |
c3a31e60 JA |
8667 | file = fget(fd); |
8668 | if (!file) { | |
8669 | err = -EBADF; | |
8670 | break; | |
8671 | } | |
8672 | /* | |
8673 | * Don't allow io_uring instances to be registered. If | |
8674 | * UNIX isn't enabled, then this causes a reference | |
8675 | * cycle and this instance can never get freed. If UNIX | |
8676 | * is enabled we'll handle it just fine, but there's | |
8677 | * still no point in allowing a ring fd as it doesn't | |
8678 | * support regular read/write anyway. | |
8679 | */ | |
8680 | if (file->f_op == &io_uring_fops) { | |
8681 | fput(file); | |
8682 | err = -EBADF; | |
8683 | break; | |
8684 | } | |
2d091d62 | 8685 | *io_get_tag_slot(data, up->offset + done) = tag; |
9a321c98 | 8686 | io_fixed_file_set(file_slot, file); |
c3a31e60 | 8687 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 8688 | if (err) { |
a04b0ac0 | 8689 | file_slot->file_ptr = 0; |
f3bd9dae | 8690 | fput(file); |
c3a31e60 | 8691 | break; |
f3bd9dae | 8692 | } |
c3a31e60 | 8693 | } |
05f3fb3c JA |
8694 | } |
8695 | ||
a7f0ed5a PB |
8696 | if (needs_switch) |
8697 | io_rsrc_node_switch(ctx, data); | |
c3a31e60 JA |
8698 | return done ? done : err; |
8699 | } | |
05589553 | 8700 | |
685fe7fe JA |
8701 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
8702 | struct task_struct *task) | |
24369c2e | 8703 | { |
e941894e | 8704 | struct io_wq_hash *hash; |
24369c2e | 8705 | struct io_wq_data data; |
24369c2e | 8706 | unsigned int concurrency; |
24369c2e | 8707 | |
362a9e65 | 8708 | mutex_lock(&ctx->uring_lock); |
e941894e JA |
8709 | hash = ctx->hash_map; |
8710 | if (!hash) { | |
8711 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
362a9e65 YY |
8712 | if (!hash) { |
8713 | mutex_unlock(&ctx->uring_lock); | |
e941894e | 8714 | return ERR_PTR(-ENOMEM); |
362a9e65 | 8715 | } |
e941894e JA |
8716 | refcount_set(&hash->refs, 1); |
8717 | init_waitqueue_head(&hash->wait); | |
8718 | ctx->hash_map = hash; | |
24369c2e | 8719 | } |
362a9e65 | 8720 | mutex_unlock(&ctx->uring_lock); |
24369c2e | 8721 | |
e941894e | 8722 | data.hash = hash; |
685fe7fe | 8723 | data.task = task; |
ebc11b6c | 8724 | data.free_work = io_wq_free_work; |
f5fa38c5 | 8725 | data.do_work = io_wq_submit_work; |
24369c2e | 8726 | |
d25e3a3d JA |
8727 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
8728 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 8729 | |
5aa75ed5 | 8730 | return io_wq_create(concurrency, &data); |
24369c2e PB |
8731 | } |
8732 | ||
c072481d PB |
8733 | static __cold int io_uring_alloc_task_context(struct task_struct *task, |
8734 | struct io_ring_ctx *ctx) | |
0f212204 JA |
8735 | { |
8736 | struct io_uring_task *tctx; | |
d8a6df10 | 8737 | int ret; |
0f212204 | 8738 | |
09899b19 | 8739 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
0f212204 JA |
8740 | if (unlikely(!tctx)) |
8741 | return -ENOMEM; | |
8742 | ||
d8a6df10 JA |
8743 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
8744 | if (unlikely(ret)) { | |
8745 | kfree(tctx); | |
8746 | return ret; | |
8747 | } | |
8748 | ||
685fe7fe | 8749 | tctx->io_wq = io_init_wq_offload(ctx, task); |
5aa75ed5 JA |
8750 | if (IS_ERR(tctx->io_wq)) { |
8751 | ret = PTR_ERR(tctx->io_wq); | |
8752 | percpu_counter_destroy(&tctx->inflight); | |
8753 | kfree(tctx); | |
8754 | return ret; | |
8755 | } | |
8756 | ||
0f212204 JA |
8757 | xa_init(&tctx->xa); |
8758 | init_waitqueue_head(&tctx->wait); | |
fdaf083c | 8759 | atomic_set(&tctx->in_idle, 0); |
b303fe2e | 8760 | atomic_set(&tctx->inflight_tracked, 0); |
0f212204 | 8761 | task->io_uring = tctx; |
7cbf1722 JA |
8762 | spin_lock_init(&tctx->task_lock); |
8763 | INIT_WQ_LIST(&tctx->task_list); | |
4813c377 | 8764 | INIT_WQ_LIST(&tctx->prior_task_list); |
7cbf1722 | 8765 | init_task_work(&tctx->task_work, tctx_task_work); |
0f212204 JA |
8766 | return 0; |
8767 | } | |
8768 | ||
8769 | void __io_uring_free(struct task_struct *tsk) | |
8770 | { | |
8771 | struct io_uring_task *tctx = tsk->io_uring; | |
8772 | ||
8773 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e | 8774 | WARN_ON_ONCE(tctx->io_wq); |
09899b19 | 8775 | WARN_ON_ONCE(tctx->cached_refs); |
ef8eaa4e | 8776 | |
d8a6df10 | 8777 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
8778 | kfree(tctx); |
8779 | tsk->io_uring = NULL; | |
8780 | } | |
8781 | ||
c072481d PB |
8782 | static __cold int io_sq_offload_create(struct io_ring_ctx *ctx, |
8783 | struct io_uring_params *p) | |
2b188cc1 JA |
8784 | { |
8785 | int ret; | |
8786 | ||
d25e3a3d JA |
8787 | /* Retain compatibility with failing for an invalid attach attempt */ |
8788 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
8789 | IORING_SETUP_ATTACH_WQ) { | |
8790 | struct fd f; | |
8791 | ||
8792 | f = fdget(p->wq_fd); | |
8793 | if (!f.file) | |
8794 | return -ENXIO; | |
0cc936f7 JA |
8795 | if (f.file->f_op != &io_uring_fops) { |
8796 | fdput(f); | |
f2a48dd0 | 8797 | return -EINVAL; |
0cc936f7 JA |
8798 | } |
8799 | fdput(f); | |
d25e3a3d | 8800 | } |
6c271ce2 | 8801 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 8802 | struct task_struct *tsk; |
534ca6d6 | 8803 | struct io_sq_data *sqd; |
26984fbf | 8804 | bool attached; |
534ca6d6 | 8805 | |
cdc1404a PM |
8806 | ret = security_uring_sqpoll(); |
8807 | if (ret) | |
8808 | return ret; | |
8809 | ||
26984fbf | 8810 | sqd = io_get_sq_data(p, &attached); |
534ca6d6 JA |
8811 | if (IS_ERR(sqd)) { |
8812 | ret = PTR_ERR(sqd); | |
8813 | goto err; | |
8814 | } | |
69fb2131 | 8815 | |
7c30f36a | 8816 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 8817 | ctx->sq_data = sqd; |
917257da JA |
8818 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
8819 | if (!ctx->sq_thread_idle) | |
8820 | ctx->sq_thread_idle = HZ; | |
8821 | ||
78d7f6ba | 8822 | io_sq_thread_park(sqd); |
de75a3d3 PB |
8823 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
8824 | io_sqd_update_thread_idle(sqd); | |
26984fbf | 8825 | /* don't attach to a dying SQPOLL thread, would be racy */ |
f2a48dd0 | 8826 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
78d7f6ba PB |
8827 | io_sq_thread_unpark(sqd); |
8828 | ||
de75a3d3 PB |
8829 | if (ret < 0) |
8830 | goto err; | |
8831 | if (attached) | |
5aa75ed5 | 8832 | return 0; |
aa06165d | 8833 | |
6c271ce2 | 8834 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 8835 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 8836 | |
917257da | 8837 | ret = -EINVAL; |
f2a48dd0 | 8838 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
e8f98f24 | 8839 | goto err_sqpoll; |
37d1e2e3 | 8840 | sqd->sq_cpu = cpu; |
6c271ce2 | 8841 | } else { |
37d1e2e3 | 8842 | sqd->sq_cpu = -1; |
6c271ce2 | 8843 | } |
37d1e2e3 JA |
8844 | |
8845 | sqd->task_pid = current->pid; | |
5c2469e0 | 8846 | sqd->task_tgid = current->tgid; |
46fe18b1 JA |
8847 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
8848 | if (IS_ERR(tsk)) { | |
8849 | ret = PTR_ERR(tsk); | |
e8f98f24 | 8850 | goto err_sqpoll; |
6c271ce2 | 8851 | } |
97a73a0f | 8852 | |
46fe18b1 | 8853 | sqd->thread = tsk; |
97a73a0f | 8854 | ret = io_uring_alloc_task_context(tsk, ctx); |
46fe18b1 | 8855 | wake_up_new_task(tsk); |
0f212204 JA |
8856 | if (ret) |
8857 | goto err; | |
6c271ce2 JA |
8858 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
8859 | /* Can't have SQ_AFF without SQPOLL */ | |
8860 | ret = -EINVAL; | |
8861 | goto err; | |
8862 | } | |
8863 | ||
2b188cc1 | 8864 | return 0; |
f2a48dd0 PB |
8865 | err_sqpoll: |
8866 | complete(&ctx->sq_data->exited); | |
2b188cc1 | 8867 | err: |
37d1e2e3 | 8868 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
8869 | return ret; |
8870 | } | |
8871 | ||
a087e2b5 BM |
8872 | static inline void __io_unaccount_mem(struct user_struct *user, |
8873 | unsigned long nr_pages) | |
2b188cc1 JA |
8874 | { |
8875 | atomic_long_sub(nr_pages, &user->locked_vm); | |
8876 | } | |
8877 | ||
a087e2b5 BM |
8878 | static inline int __io_account_mem(struct user_struct *user, |
8879 | unsigned long nr_pages) | |
2b188cc1 JA |
8880 | { |
8881 | unsigned long page_limit, cur_pages, new_pages; | |
8882 | ||
8883 | /* Don't allow more pages than we can safely lock */ | |
8884 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
8885 | ||
8886 | do { | |
8887 | cur_pages = atomic_long_read(&user->locked_vm); | |
8888 | new_pages = cur_pages + nr_pages; | |
8889 | if (new_pages > page_limit) | |
8890 | return -ENOMEM; | |
8891 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
8892 | new_pages) != cur_pages); | |
8893 | ||
8894 | return 0; | |
8895 | } | |
8896 | ||
26bfa89e | 8897 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8898 | { |
62e398be | 8899 | if (ctx->user) |
a087e2b5 | 8900 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 8901 | |
26bfa89e JA |
8902 | if (ctx->mm_account) |
8903 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8904 | } |
8905 | ||
26bfa89e | 8906 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8907 | { |
30975825 BM |
8908 | int ret; |
8909 | ||
62e398be | 8910 | if (ctx->user) { |
30975825 BM |
8911 | ret = __io_account_mem(ctx->user, nr_pages); |
8912 | if (ret) | |
8913 | return ret; | |
8914 | } | |
8915 | ||
26bfa89e JA |
8916 | if (ctx->mm_account) |
8917 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8918 | |
8919 | return 0; | |
8920 | } | |
8921 | ||
2b188cc1 JA |
8922 | static void io_mem_free(void *ptr) |
8923 | { | |
52e04ef4 MR |
8924 | struct page *page; |
8925 | ||
8926 | if (!ptr) | |
8927 | return; | |
2b188cc1 | 8928 | |
52e04ef4 | 8929 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8930 | if (put_page_testzero(page)) |
8931 | free_compound_page(page); | |
8932 | } | |
8933 | ||
8934 | static void *io_mem_alloc(size_t size) | |
8935 | { | |
8936 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
26bfa89e | 8937 | __GFP_NORETRY | __GFP_ACCOUNT; |
2b188cc1 JA |
8938 | |
8939 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8940 | } | |
8941 | ||
75b28aff HV |
8942 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8943 | size_t *sq_offset) | |
8944 | { | |
8945 | struct io_rings *rings; | |
8946 | size_t off, sq_array_size; | |
8947 | ||
8948 | off = struct_size(rings, cqes, cq_entries); | |
8949 | if (off == SIZE_MAX) | |
8950 | return SIZE_MAX; | |
8951 | ||
8952 | #ifdef CONFIG_SMP | |
8953 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8954 | if (off == 0) | |
8955 | return SIZE_MAX; | |
8956 | #endif | |
8957 | ||
b36200f5 DV |
8958 | if (sq_offset) |
8959 | *sq_offset = off; | |
8960 | ||
75b28aff HV |
8961 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8962 | if (sq_array_size == SIZE_MAX) | |
8963 | return SIZE_MAX; | |
8964 | ||
8965 | if (check_add_overflow(off, sq_array_size, &off)) | |
8966 | return SIZE_MAX; | |
8967 | ||
75b28aff HV |
8968 | return off; |
8969 | } | |
8970 | ||
41edf1a5 | 8971 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot) |
7f61a1e9 | 8972 | { |
41edf1a5 | 8973 | struct io_mapped_ubuf *imu = *slot; |
7f61a1e9 PB |
8974 | unsigned int i; |
8975 | ||
6224843d PB |
8976 | if (imu != ctx->dummy_ubuf) { |
8977 | for (i = 0; i < imu->nr_bvecs; i++) | |
8978 | unpin_user_page(imu->bvec[i].bv_page); | |
8979 | if (imu->acct_pages) | |
8980 | io_unaccount_mem(ctx, imu->acct_pages); | |
8981 | kvfree(imu); | |
8982 | } | |
41edf1a5 | 8983 | *slot = NULL; |
7f61a1e9 PB |
8984 | } |
8985 | ||
bd54b6fe | 8986 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
edafccee | 8987 | { |
634d00df PB |
8988 | io_buffer_unmap(ctx, &prsrc->buf); |
8989 | prsrc->buf = NULL; | |
bd54b6fe | 8990 | } |
edafccee | 8991 | |
bd54b6fe BM |
8992 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
8993 | { | |
8994 | unsigned int i; | |
edafccee | 8995 | |
7f61a1e9 PB |
8996 | for (i = 0; i < ctx->nr_user_bufs; i++) |
8997 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); | |
edafccee | 8998 | kfree(ctx->user_bufs); |
bb6659cc | 8999 | io_rsrc_data_free(ctx->buf_data); |
edafccee | 9000 | ctx->user_bufs = NULL; |
bd54b6fe | 9001 | ctx->buf_data = NULL; |
edafccee | 9002 | ctx->nr_user_bufs = 0; |
bd54b6fe BM |
9003 | } |
9004 | ||
0a96bbe4 | 9005 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee | 9006 | { |
bd54b6fe | 9007 | int ret; |
edafccee | 9008 | |
bd54b6fe | 9009 | if (!ctx->buf_data) |
edafccee JA |
9010 | return -ENXIO; |
9011 | ||
bd54b6fe BM |
9012 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
9013 | if (!ret) | |
9014 | __io_sqe_buffers_unregister(ctx); | |
9015 | return ret; | |
edafccee JA |
9016 | } |
9017 | ||
9018 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
9019 | void __user *arg, unsigned index) | |
9020 | { | |
9021 | struct iovec __user *src; | |
9022 | ||
9023 | #ifdef CONFIG_COMPAT | |
9024 | if (ctx->compat) { | |
9025 | struct compat_iovec __user *ciovs; | |
9026 | struct compat_iovec ciov; | |
9027 | ||
9028 | ciovs = (struct compat_iovec __user *) arg; | |
9029 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
9030 | return -EFAULT; | |
9031 | ||
d55e5f5b | 9032 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
9033 | dst->iov_len = ciov.iov_len; |
9034 | return 0; | |
9035 | } | |
9036 | #endif | |
9037 | src = (struct iovec __user *) arg; | |
9038 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
9039 | return -EFAULT; | |
9040 | return 0; | |
9041 | } | |
9042 | ||
de293938 JA |
9043 | /* |
9044 | * Not super efficient, but this is just a registration time. And we do cache | |
9045 | * the last compound head, so generally we'll only do a full search if we don't | |
9046 | * match that one. | |
9047 | * | |
9048 | * We check if the given compound head page has already been accounted, to | |
9049 | * avoid double accounting it. This allows us to account the full size of the | |
9050 | * page, not just the constituent pages of a huge page. | |
9051 | */ | |
9052 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
9053 | int nr_pages, struct page *hpage) | |
9054 | { | |
9055 | int i, j; | |
9056 | ||
9057 | /* check current page array */ | |
9058 | for (i = 0; i < nr_pages; i++) { | |
9059 | if (!PageCompound(pages[i])) | |
9060 | continue; | |
9061 | if (compound_head(pages[i]) == hpage) | |
9062 | return true; | |
9063 | } | |
9064 | ||
9065 | /* check previously registered pages */ | |
9066 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
41edf1a5 | 9067 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
de293938 JA |
9068 | |
9069 | for (j = 0; j < imu->nr_bvecs; j++) { | |
9070 | if (!PageCompound(imu->bvec[j].bv_page)) | |
9071 | continue; | |
9072 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
9073 | return true; | |
9074 | } | |
9075 | } | |
9076 | ||
9077 | return false; | |
9078 | } | |
9079 | ||
9080 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
9081 | int nr_pages, struct io_mapped_ubuf *imu, | |
9082 | struct page **last_hpage) | |
9083 | { | |
9084 | int i, ret; | |
9085 | ||
216e5835 | 9086 | imu->acct_pages = 0; |
de293938 JA |
9087 | for (i = 0; i < nr_pages; i++) { |
9088 | if (!PageCompound(pages[i])) { | |
9089 | imu->acct_pages++; | |
9090 | } else { | |
9091 | struct page *hpage; | |
9092 | ||
9093 | hpage = compound_head(pages[i]); | |
9094 | if (hpage == *last_hpage) | |
9095 | continue; | |
9096 | *last_hpage = hpage; | |
9097 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
9098 | continue; | |
9099 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
9100 | } | |
9101 | } | |
9102 | ||
9103 | if (!imu->acct_pages) | |
9104 | return 0; | |
9105 | ||
26bfa89e | 9106 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
9107 | if (ret) |
9108 | imu->acct_pages = 0; | |
9109 | return ret; | |
9110 | } | |
9111 | ||
0a96bbe4 | 9112 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
41edf1a5 | 9113 | struct io_mapped_ubuf **pimu, |
0a96bbe4 | 9114 | struct page **last_hpage) |
edafccee | 9115 | { |
41edf1a5 | 9116 | struct io_mapped_ubuf *imu = NULL; |
edafccee JA |
9117 | struct vm_area_struct **vmas = NULL; |
9118 | struct page **pages = NULL; | |
0a96bbe4 BM |
9119 | unsigned long off, start, end, ubuf; |
9120 | size_t size; | |
9121 | int ret, pret, nr_pages, i; | |
9122 | ||
6224843d PB |
9123 | if (!iov->iov_base) { |
9124 | *pimu = ctx->dummy_ubuf; | |
9125 | return 0; | |
9126 | } | |
9127 | ||
0a96bbe4 BM |
9128 | ubuf = (unsigned long) iov->iov_base; |
9129 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
9130 | start = ubuf >> PAGE_SHIFT; | |
9131 | nr_pages = end - start; | |
9132 | ||
41edf1a5 | 9133 | *pimu = NULL; |
0a96bbe4 BM |
9134 | ret = -ENOMEM; |
9135 | ||
9136 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
9137 | if (!pages) | |
9138 | goto done; | |
9139 | ||
9140 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
9141 | GFP_KERNEL); | |
9142 | if (!vmas) | |
9143 | goto done; | |
edafccee | 9144 | |
41edf1a5 | 9145 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
a2b4198c | 9146 | if (!imu) |
0a96bbe4 BM |
9147 | goto done; |
9148 | ||
9149 | ret = 0; | |
9150 | mmap_read_lock(current->mm); | |
9151 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
9152 | pages, vmas); | |
9153 | if (pret == nr_pages) { | |
9154 | /* don't support file backed memory */ | |
9155 | for (i = 0; i < nr_pages; i++) { | |
9156 | struct vm_area_struct *vma = vmas[i]; | |
9157 | ||
40dad765 PB |
9158 | if (vma_is_shmem(vma)) |
9159 | continue; | |
0a96bbe4 BM |
9160 | if (vma->vm_file && |
9161 | !is_file_hugepages(vma->vm_file)) { | |
9162 | ret = -EOPNOTSUPP; | |
9163 | break; | |
9164 | } | |
9165 | } | |
9166 | } else { | |
9167 | ret = pret < 0 ? pret : -EFAULT; | |
9168 | } | |
9169 | mmap_read_unlock(current->mm); | |
9170 | if (ret) { | |
9171 | /* | |
9172 | * if we did partial map, or found file backed vmas, | |
9173 | * release any pages we did get | |
9174 | */ | |
9175 | if (pret > 0) | |
9176 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9177 | goto done; |
9178 | } | |
9179 | ||
9180 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
9181 | if (ret) { | |
9182 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9183 | goto done; |
9184 | } | |
9185 | ||
9186 | off = ubuf & ~PAGE_MASK; | |
9187 | size = iov->iov_len; | |
9188 | for (i = 0; i < nr_pages; i++) { | |
9189 | size_t vec_len; | |
9190 | ||
9191 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
9192 | imu->bvec[i].bv_page = pages[i]; | |
9193 | imu->bvec[i].bv_len = vec_len; | |
9194 | imu->bvec[i].bv_offset = off; | |
9195 | off = 0; | |
9196 | size -= vec_len; | |
9197 | } | |
9198 | /* store original address for later verification */ | |
9199 | imu->ubuf = ubuf; | |
4751f53d | 9200 | imu->ubuf_end = ubuf + iov->iov_len; |
0a96bbe4 | 9201 | imu->nr_bvecs = nr_pages; |
41edf1a5 | 9202 | *pimu = imu; |
0a96bbe4 BM |
9203 | ret = 0; |
9204 | done: | |
41edf1a5 PB |
9205 | if (ret) |
9206 | kvfree(imu); | |
0a96bbe4 BM |
9207 | kvfree(pages); |
9208 | kvfree(vmas); | |
9209 | return ret; | |
9210 | } | |
9211 | ||
2b358604 | 9212 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 9213 | { |
87094465 PB |
9214 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
9215 | return ctx->user_bufs ? 0 : -ENOMEM; | |
2b358604 | 9216 | } |
edafccee | 9217 | |
2b358604 BM |
9218 | static int io_buffer_validate(struct iovec *iov) |
9219 | { | |
50e96989 PB |
9220 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
9221 | ||
2b358604 BM |
9222 | /* |
9223 | * Don't impose further limits on the size and buffer | |
9224 | * constraints here, we'll -EINVAL later when IO is | |
9225 | * submitted if they are wrong. | |
9226 | */ | |
6224843d PB |
9227 | if (!iov->iov_base) |
9228 | return iov->iov_len ? -EFAULT : 0; | |
9229 | if (!iov->iov_len) | |
2b358604 | 9230 | return -EFAULT; |
edafccee | 9231 | |
2b358604 BM |
9232 | /* arbitrary limit, but we need something */ |
9233 | if (iov->iov_len > SZ_1G) | |
9234 | return -EFAULT; | |
edafccee | 9235 | |
50e96989 PB |
9236 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
9237 | return -EOVERFLOW; | |
9238 | ||
2b358604 BM |
9239 | return 0; |
9240 | } | |
edafccee | 9241 | |
2b358604 | 9242 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
634d00df | 9243 | unsigned int nr_args, u64 __user *tags) |
2b358604 | 9244 | { |
bd54b6fe BM |
9245 | struct page *last_hpage = NULL; |
9246 | struct io_rsrc_data *data; | |
2b358604 BM |
9247 | int i, ret; |
9248 | struct iovec iov; | |
edafccee | 9249 | |
87094465 PB |
9250 | if (ctx->user_bufs) |
9251 | return -EBUSY; | |
489809e2 | 9252 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
87094465 | 9253 | return -EINVAL; |
bd54b6fe | 9254 | ret = io_rsrc_node_switch_start(ctx); |
2b358604 BM |
9255 | if (ret) |
9256 | return ret; | |
d878c816 PB |
9257 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
9258 | if (ret) | |
9259 | return ret; | |
bd54b6fe BM |
9260 | ret = io_buffers_map_alloc(ctx, nr_args); |
9261 | if (ret) { | |
bb6659cc | 9262 | io_rsrc_data_free(data); |
bd54b6fe BM |
9263 | return ret; |
9264 | } | |
edafccee | 9265 | |
87094465 | 9266 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
edafccee JA |
9267 | ret = io_copy_iov(ctx, &iov, arg, i); |
9268 | if (ret) | |
0a96bbe4 | 9269 | break; |
2b358604 BM |
9270 | ret = io_buffer_validate(&iov); |
9271 | if (ret) | |
0a96bbe4 | 9272 | break; |
2d091d62 | 9273 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
cf3770e7 CIK |
9274 | ret = -EINVAL; |
9275 | break; | |
9276 | } | |
edafccee | 9277 | |
41edf1a5 PB |
9278 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
9279 | &last_hpage); | |
0a96bbe4 BM |
9280 | if (ret) |
9281 | break; | |
edafccee | 9282 | } |
0a96bbe4 | 9283 | |
bd54b6fe | 9284 | WARN_ON_ONCE(ctx->buf_data); |
0a96bbe4 | 9285 | |
bd54b6fe BM |
9286 | ctx->buf_data = data; |
9287 | if (ret) | |
9288 | __io_sqe_buffers_unregister(ctx); | |
9289 | else | |
9290 | io_rsrc_node_switch(ctx, NULL); | |
edafccee JA |
9291 | return ret; |
9292 | } | |
9293 | ||
634d00df PB |
9294 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
9295 | struct io_uring_rsrc_update2 *up, | |
9296 | unsigned int nr_args) | |
9297 | { | |
9298 | u64 __user *tags = u64_to_user_ptr(up->tags); | |
9299 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); | |
634d00df PB |
9300 | struct page *last_hpage = NULL; |
9301 | bool needs_switch = false; | |
9302 | __u32 done; | |
9303 | int i, err; | |
9304 | ||
9305 | if (!ctx->buf_data) | |
9306 | return -ENXIO; | |
9307 | if (up->offset + nr_args > ctx->nr_user_bufs) | |
9308 | return -EINVAL; | |
9309 | ||
9310 | for (done = 0; done < nr_args; done++) { | |
0b8c0e7c PB |
9311 | struct io_mapped_ubuf *imu; |
9312 | int offset = up->offset + done; | |
634d00df PB |
9313 | u64 tag = 0; |
9314 | ||
9315 | err = io_copy_iov(ctx, &iov, iovs, done); | |
9316 | if (err) | |
9317 | break; | |
9318 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { | |
9319 | err = -EFAULT; | |
9320 | break; | |
9321 | } | |
0b8c0e7c PB |
9322 | err = io_buffer_validate(&iov); |
9323 | if (err) | |
9324 | break; | |
cf3770e7 CIK |
9325 | if (!iov.iov_base && tag) { |
9326 | err = -EINVAL; | |
9327 | break; | |
9328 | } | |
0b8c0e7c PB |
9329 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
9330 | if (err) | |
9331 | break; | |
634d00df | 9332 | |
0b8c0e7c | 9333 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
6224843d | 9334 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
0b8c0e7c PB |
9335 | err = io_queue_rsrc_removal(ctx->buf_data, offset, |
9336 | ctx->rsrc_node, ctx->user_bufs[i]); | |
9337 | if (unlikely(err)) { | |
9338 | io_buffer_unmap(ctx, &imu); | |
634d00df | 9339 | break; |
0b8c0e7c | 9340 | } |
634d00df PB |
9341 | ctx->user_bufs[i] = NULL; |
9342 | needs_switch = true; | |
9343 | } | |
9344 | ||
0b8c0e7c | 9345 | ctx->user_bufs[i] = imu; |
2d091d62 | 9346 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
634d00df PB |
9347 | } |
9348 | ||
9349 | if (needs_switch) | |
9350 | io_rsrc_node_switch(ctx, ctx->buf_data); | |
9351 | return done ? done : err; | |
9352 | } | |
9353 | ||
9b402849 JA |
9354 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
9355 | { | |
9356 | __s32 __user *fds = arg; | |
9357 | int fd; | |
9358 | ||
9359 | if (ctx->cq_ev_fd) | |
9360 | return -EBUSY; | |
9361 | ||
9362 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
9363 | return -EFAULT; | |
9364 | ||
9365 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
9366 | if (IS_ERR(ctx->cq_ev_fd)) { | |
9367 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
fe7e3257 | 9368 | |
9b402849 JA |
9369 | ctx->cq_ev_fd = NULL; |
9370 | return ret; | |
9371 | } | |
9372 | ||
9373 | return 0; | |
9374 | } | |
9375 | ||
9376 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
9377 | { | |
9378 | if (ctx->cq_ev_fd) { | |
9379 | eventfd_ctx_put(ctx->cq_ev_fd); | |
9380 | ctx->cq_ev_fd = NULL; | |
9381 | return 0; | |
9382 | } | |
9383 | ||
9384 | return -ENXIO; | |
9385 | } | |
9386 | ||
5a2e745d JA |
9387 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
9388 | { | |
9e15c3a0 JA |
9389 | struct io_buffer *buf; |
9390 | unsigned long index; | |
9391 | ||
1d0254e6 | 9392 | xa_for_each(&ctx->io_buffers, index, buf) |
9e15c3a0 | 9393 | __io_remove_buffers(ctx, buf, index, -1U); |
5a2e745d JA |
9394 | } |
9395 | ||
4010fec4 | 9396 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9397 | { |
cd0ca2e0 | 9398 | struct io_submit_state *state = &ctx->submit_state; |
37f0e767 | 9399 | int nr = 0; |
bf019da7 | 9400 | |
9a4fdbd8 | 9401 | mutex_lock(&ctx->uring_lock); |
cd0ca2e0 | 9402 | io_flush_cached_locked_reqs(ctx, state); |
9a4fdbd8 | 9403 | |
c2b6c6bc PB |
9404 | while (state->free_list.next) { |
9405 | struct io_wq_work_node *node; | |
9406 | struct io_kiocb *req; | |
9a4fdbd8 | 9407 | |
c2b6c6bc PB |
9408 | node = wq_stack_extract(&state->free_list); |
9409 | req = container_of(node, struct io_kiocb, comp_list); | |
9410 | kmem_cache_free(req_cachep, req); | |
37f0e767 | 9411 | nr++; |
c2b6c6bc | 9412 | } |
37f0e767 PB |
9413 | if (nr) |
9414 | percpu_ref_put_many(&ctx->refs, nr); | |
9a4fdbd8 JA |
9415 | mutex_unlock(&ctx->uring_lock); |
9416 | } | |
9417 | ||
43597aac | 9418 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
2b188cc1 | 9419 | { |
43597aac | 9420 | if (data && !atomic_dec_and_test(&data->refs)) |
bd54b6fe | 9421 | wait_for_completion(&data->done); |
bd54b6fe | 9422 | } |
04fc6c80 | 9423 | |
c072481d | 9424 | static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9425 | { |
37d1e2e3 | 9426 | io_sq_thread_finish(ctx); |
2aede0e4 | 9427 | |
37d1e2e3 | 9428 | if (ctx->mm_account) { |
2aede0e4 JA |
9429 | mmdrop(ctx->mm_account); |
9430 | ctx->mm_account = NULL; | |
30975825 | 9431 | } |
def596e9 | 9432 | |
ab409402 | 9433 | io_rsrc_refs_drop(ctx); |
43597aac PB |
9434 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
9435 | io_wait_rsrc_data(ctx->buf_data); | |
9436 | io_wait_rsrc_data(ctx->file_data); | |
9437 | ||
8bad28d8 | 9438 | mutex_lock(&ctx->uring_lock); |
43597aac | 9439 | if (ctx->buf_data) |
bd54b6fe | 9440 | __io_sqe_buffers_unregister(ctx); |
43597aac | 9441 | if (ctx->file_data) |
08480400 | 9442 | __io_sqe_files_unregister(ctx); |
c4ea060e PB |
9443 | if (ctx->rings) |
9444 | __io_cqring_overflow_flush(ctx, true); | |
8bad28d8 | 9445 | mutex_unlock(&ctx->uring_lock); |
9b402849 | 9446 | io_eventfd_unregister(ctx); |
5a2e745d | 9447 | io_destroy_buffers(ctx); |
07db298a PB |
9448 | if (ctx->sq_creds) |
9449 | put_cred(ctx->sq_creds); | |
def596e9 | 9450 | |
a7f0ed5a PB |
9451 | /* there are no registered resources left, nobody uses it */ |
9452 | if (ctx->rsrc_node) | |
9453 | io_rsrc_node_destroy(ctx->rsrc_node); | |
8dd03afe | 9454 | if (ctx->rsrc_backup_node) |
b895c9a6 | 9455 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
a7f0ed5a | 9456 | flush_delayed_work(&ctx->rsrc_put_work); |
756ab7c0 | 9457 | flush_delayed_work(&ctx->fallback_work); |
a7f0ed5a PB |
9458 | |
9459 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); | |
9460 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); | |
def596e9 | 9461 | |
2b188cc1 | 9462 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
9463 | if (ctx->ring_sock) { |
9464 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 9465 | sock_release(ctx->ring_sock); |
355e8d26 | 9466 | } |
2b188cc1 | 9467 | #endif |
ef9dd637 | 9468 | WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); |
2b188cc1 | 9469 | |
75b28aff | 9470 | io_mem_free(ctx->rings); |
2b188cc1 | 9471 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
9472 | |
9473 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 9474 | free_uid(ctx->user); |
4010fec4 | 9475 | io_req_caches_free(ctx); |
e941894e JA |
9476 | if (ctx->hash_map) |
9477 | io_wq_put_hash(ctx->hash_map); | |
78076bb6 | 9478 | kfree(ctx->cancel_hash); |
6224843d | 9479 | kfree(ctx->dummy_ubuf); |
2b188cc1 JA |
9480 | kfree(ctx); |
9481 | } | |
9482 | ||
9483 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
9484 | { | |
9485 | struct io_ring_ctx *ctx = file->private_data; | |
9486 | __poll_t mask = 0; | |
9487 | ||
d60aa65b | 9488 | poll_wait(file, &ctx->cq_wait, wait); |
4f7067c3 SB |
9489 | /* |
9490 | * synchronizes with barrier from wq_has_sleeper call in | |
9491 | * io_commit_cqring | |
9492 | */ | |
2b188cc1 | 9493 | smp_rmb(); |
90554200 | 9494 | if (!io_sqring_full(ctx)) |
2b188cc1 | 9495 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
9496 | |
9497 | /* | |
9498 | * Don't flush cqring overflow list here, just do a simple check. | |
9499 | * Otherwise there could possible be ABBA deadlock: | |
9500 | * CPU0 CPU1 | |
9501 | * ---- ---- | |
9502 | * lock(&ctx->uring_lock); | |
9503 | * lock(&ep->mtx); | |
9504 | * lock(&ctx->uring_lock); | |
9505 | * lock(&ep->mtx); | |
9506 | * | |
9507 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
9508 | * pushs them to do the flush. | |
9509 | */ | |
5ed7a37d | 9510 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
2b188cc1 JA |
9511 | mask |= EPOLLIN | EPOLLRDNORM; |
9512 | ||
9513 | return mask; | |
9514 | } | |
9515 | ||
0bead8cd | 9516 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 9517 | { |
4379bf8b | 9518 | const struct cred *creds; |
071698e1 | 9519 | |
61cf9370 | 9520 | creds = xa_erase(&ctx->personalities, id); |
4379bf8b JA |
9521 | if (creds) { |
9522 | put_cred(creds); | |
0bead8cd | 9523 | return 0; |
1e6fa521 | 9524 | } |
0bead8cd YD |
9525 | |
9526 | return -EINVAL; | |
9527 | } | |
9528 | ||
d56d938b PB |
9529 | struct io_tctx_exit { |
9530 | struct callback_head task_work; | |
9531 | struct completion completion; | |
baf186c4 | 9532 | struct io_ring_ctx *ctx; |
d56d938b PB |
9533 | }; |
9534 | ||
c072481d | 9535 | static __cold void io_tctx_exit_cb(struct callback_head *cb) |
d56d938b PB |
9536 | { |
9537 | struct io_uring_task *tctx = current->io_uring; | |
9538 | struct io_tctx_exit *work; | |
9539 | ||
9540 | work = container_of(cb, struct io_tctx_exit, task_work); | |
9541 | /* | |
9542 | * When @in_idle, we're in cancellation and it's racy to remove the | |
9543 | * node. It'll be removed by the end of cancellation, just ignore it. | |
9544 | */ | |
9545 | if (!atomic_read(&tctx->in_idle)) | |
eef51daa | 9546 | io_uring_del_tctx_node((unsigned long)work->ctx); |
d56d938b PB |
9547 | complete(&work->completion); |
9548 | } | |
9549 | ||
c072481d | 9550 | static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
28090c13 PB |
9551 | { |
9552 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
9553 | ||
9554 | return req->ctx == data; | |
9555 | } | |
9556 | ||
c072481d | 9557 | static __cold void io_ring_exit_work(struct work_struct *work) |
85faa7b8 | 9558 | { |
d56d938b | 9559 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 9560 | unsigned long timeout = jiffies + HZ * 60 * 5; |
58d3be2c | 9561 | unsigned long interval = HZ / 20; |
d56d938b PB |
9562 | struct io_tctx_exit exit; |
9563 | struct io_tctx_node *node; | |
9564 | int ret; | |
85faa7b8 | 9565 | |
56952e91 JA |
9566 | /* |
9567 | * If we're doing polled IO and end up having requests being | |
9568 | * submitted async (out-of-line), then completions can come in while | |
9569 | * we're waiting for refs to drop. We need to reap these manually, | |
9570 | * as nobody else will be looking for them. | |
9571 | */ | |
b2edc0a7 | 9572 | do { |
3dd0c97a | 9573 | io_uring_try_cancel_requests(ctx, NULL, true); |
28090c13 PB |
9574 | if (ctx->sq_data) { |
9575 | struct io_sq_data *sqd = ctx->sq_data; | |
9576 | struct task_struct *tsk; | |
9577 | ||
9578 | io_sq_thread_park(sqd); | |
9579 | tsk = sqd->thread; | |
9580 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) | |
9581 | io_wq_cancel_cb(tsk->io_uring->io_wq, | |
9582 | io_cancel_ctx_cb, ctx, true); | |
9583 | io_sq_thread_unpark(sqd); | |
9584 | } | |
b5bb3a24 | 9585 | |
37f0e767 PB |
9586 | io_req_caches_free(ctx); |
9587 | ||
58d3be2c PB |
9588 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
9589 | /* there is little hope left, don't run it too often */ | |
9590 | interval = HZ * 60; | |
9591 | } | |
9592 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); | |
d56d938b | 9593 | |
7f00651a PB |
9594 | init_completion(&exit.completion); |
9595 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
9596 | exit.ctx = ctx; | |
89b5066e PB |
9597 | /* |
9598 | * Some may use context even when all refs and requests have been put, | |
9599 | * and they are free to do so while still holding uring_lock or | |
5b0a6acc | 9600 | * completion_lock, see io_req_task_submit(). Apart from other work, |
89b5066e PB |
9601 | * this lock/unlock section also waits them to finish. |
9602 | */ | |
d56d938b PB |
9603 | mutex_lock(&ctx->uring_lock); |
9604 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
9605 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
9606 | ||
d56d938b PB |
9607 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
9608 | ctx_node); | |
7f00651a PB |
9609 | /* don't spin on a single task if cancellation failed */ |
9610 | list_rotate_left(&ctx->tctx_list); | |
d56d938b PB |
9611 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
9612 | if (WARN_ON_ONCE(ret)) | |
9613 | continue; | |
d56d938b PB |
9614 | |
9615 | mutex_unlock(&ctx->uring_lock); | |
9616 | wait_for_completion(&exit.completion); | |
d56d938b PB |
9617 | mutex_lock(&ctx->uring_lock); |
9618 | } | |
9619 | mutex_unlock(&ctx->uring_lock); | |
79ebeaee JA |
9620 | spin_lock(&ctx->completion_lock); |
9621 | spin_unlock(&ctx->completion_lock); | |
d56d938b | 9622 | |
85faa7b8 JA |
9623 | io_ring_ctx_free(ctx); |
9624 | } | |
9625 | ||
80c4cbdb | 9626 | /* Returns true if we found and killed one or more timeouts */ |
c072481d PB |
9627 | static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx, |
9628 | struct task_struct *tsk, bool cancel_all) | |
80c4cbdb PB |
9629 | { |
9630 | struct io_kiocb *req, *tmp; | |
9631 | int canceled = 0; | |
9632 | ||
79ebeaee JA |
9633 | spin_lock(&ctx->completion_lock); |
9634 | spin_lock_irq(&ctx->timeout_lock); | |
80c4cbdb | 9635 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
3dd0c97a | 9636 | if (io_match_task(req, tsk, cancel_all)) { |
80c4cbdb PB |
9637 | io_kill_timeout(req, -ECANCELED); |
9638 | canceled++; | |
9639 | } | |
9640 | } | |
79ebeaee | 9641 | spin_unlock_irq(&ctx->timeout_lock); |
51520426 PB |
9642 | if (canceled != 0) |
9643 | io_commit_cqring(ctx); | |
79ebeaee | 9644 | spin_unlock(&ctx->completion_lock); |
80c4cbdb PB |
9645 | if (canceled != 0) |
9646 | io_cqring_ev_posted(ctx); | |
9647 | return canceled != 0; | |
9648 | } | |
9649 | ||
c072481d | 9650 | static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
2b188cc1 | 9651 | { |
61cf9370 MWO |
9652 | unsigned long index; |
9653 | struct creds *creds; | |
9654 | ||
2b188cc1 JA |
9655 | mutex_lock(&ctx->uring_lock); |
9656 | percpu_ref_kill(&ctx->refs); | |
634578f8 | 9657 | if (ctx->rings) |
6c2450ae | 9658 | __io_cqring_overflow_flush(ctx, true); |
61cf9370 MWO |
9659 | xa_for_each(&ctx->personalities, index, creds) |
9660 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
9661 | mutex_unlock(&ctx->uring_lock); |
9662 | ||
3dd0c97a PB |
9663 | io_kill_timeouts(ctx, NULL, true); |
9664 | io_poll_remove_all(ctx, NULL, true); | |
561fb04a | 9665 | |
15dff286 | 9666 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 9667 | io_iopoll_try_reap_events(ctx); |
309fc03a | 9668 | |
85faa7b8 | 9669 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
9670 | /* |
9671 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
9672 | * if we're exiting a ton of rings at the same time. It just adds | |
9673 | * noise and overhead, there's no discernable change in runtime | |
9674 | * over using system_wq. | |
9675 | */ | |
9676 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
9677 | } |
9678 | ||
9679 | static int io_uring_release(struct inode *inode, struct file *file) | |
9680 | { | |
9681 | struct io_ring_ctx *ctx = file->private_data; | |
9682 | ||
9683 | file->private_data = NULL; | |
9684 | io_ring_ctx_wait_and_kill(ctx); | |
9685 | return 0; | |
9686 | } | |
9687 | ||
f6edbabb PB |
9688 | struct io_task_cancel { |
9689 | struct task_struct *task; | |
3dd0c97a | 9690 | bool all; |
f6edbabb | 9691 | }; |
f254ac04 | 9692 | |
f6edbabb | 9693 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 9694 | { |
9a472ef7 | 9695 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 9696 | struct io_task_cancel *cancel = data; |
9a472ef7 | 9697 | |
6af3f48b | 9698 | return io_match_task_safe(req, cancel->task, cancel->all); |
b711d4ea JA |
9699 | } |
9700 | ||
c072481d PB |
9701 | static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
9702 | struct task_struct *task, | |
9703 | bool cancel_all) | |
b7ddce3c | 9704 | { |
e1915f76 | 9705 | struct io_defer_entry *de; |
b7ddce3c PB |
9706 | LIST_HEAD(list); |
9707 | ||
79ebeaee | 9708 | spin_lock(&ctx->completion_lock); |
b7ddce3c | 9709 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
6af3f48b | 9710 | if (io_match_task_safe(de->req, task, cancel_all)) { |
b7ddce3c PB |
9711 | list_cut_position(&list, &ctx->defer_list, &de->list); |
9712 | break; | |
9713 | } | |
9714 | } | |
79ebeaee | 9715 | spin_unlock(&ctx->completion_lock); |
e1915f76 PB |
9716 | if (list_empty(&list)) |
9717 | return false; | |
b7ddce3c PB |
9718 | |
9719 | while (!list_empty(&list)) { | |
9720 | de = list_first_entry(&list, struct io_defer_entry, list); | |
9721 | list_del_init(&de->list); | |
f41db273 | 9722 | io_req_complete_failed(de->req, -ECANCELED); |
b7ddce3c PB |
9723 | kfree(de); |
9724 | } | |
e1915f76 | 9725 | return true; |
b7ddce3c PB |
9726 | } |
9727 | ||
c072481d | 9728 | static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
1b00764f PB |
9729 | { |
9730 | struct io_tctx_node *node; | |
9731 | enum io_wq_cancel cret; | |
9732 | bool ret = false; | |
9733 | ||
9734 | mutex_lock(&ctx->uring_lock); | |
9735 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
9736 | struct io_uring_task *tctx = node->task->io_uring; | |
9737 | ||
9738 | /* | |
9739 | * io_wq will stay alive while we hold uring_lock, because it's | |
9740 | * killed after ctx nodes, which requires to take the lock. | |
9741 | */ | |
9742 | if (!tctx || !tctx->io_wq) | |
9743 | continue; | |
9744 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
9745 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9746 | } | |
9747 | mutex_unlock(&ctx->uring_lock); | |
9748 | ||
9749 | return ret; | |
9750 | } | |
9751 | ||
c072481d PB |
9752 | static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
9753 | struct task_struct *task, | |
9754 | bool cancel_all) | |
9936c7c2 | 9755 | { |
3dd0c97a | 9756 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
1b00764f | 9757 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 PB |
9758 | |
9759 | while (1) { | |
9760 | enum io_wq_cancel cret; | |
9761 | bool ret = false; | |
9762 | ||
1b00764f PB |
9763 | if (!task) { |
9764 | ret |= io_uring_try_cancel_iowq(ctx); | |
9765 | } else if (tctx && tctx->io_wq) { | |
9766 | /* | |
9767 | * Cancels requests of all rings, not only @ctx, but | |
9768 | * it's fine as the task is in exit/exec. | |
9769 | */ | |
5aa75ed5 | 9770 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
9771 | &cancel, true); |
9772 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9773 | } | |
9774 | ||
9775 | /* SQPOLL thread does its own polling */ | |
3dd0c97a | 9776 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
d052d1d6 | 9777 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
5eef4e87 | 9778 | while (!wq_list_empty(&ctx->iopoll_list)) { |
9936c7c2 PB |
9779 | io_iopoll_try_reap_events(ctx); |
9780 | ret = true; | |
9781 | } | |
9782 | } | |
9783 | ||
3dd0c97a PB |
9784 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
9785 | ret |= io_poll_remove_all(ctx, task, cancel_all); | |
9786 | ret |= io_kill_timeouts(ctx, task, cancel_all); | |
e5dc480d PB |
9787 | if (task) |
9788 | ret |= io_run_task_work(); | |
9936c7c2 PB |
9789 | if (!ret) |
9790 | break; | |
9791 | cond_resched(); | |
9792 | } | |
9793 | } | |
9794 | ||
eef51daa | 9795 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
0f212204 | 9796 | { |
236434c3 | 9797 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 9798 | struct io_tctx_node *node; |
a528b04e | 9799 | int ret; |
236434c3 MWO |
9800 | |
9801 | if (unlikely(!tctx)) { | |
5aa75ed5 | 9802 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
9803 | if (unlikely(ret)) |
9804 | return ret; | |
e139a1ec | 9805 | |
236434c3 | 9806 | tctx = current->io_uring; |
e139a1ec PB |
9807 | if (ctx->iowq_limits_set) { |
9808 | unsigned int limits[2] = { ctx->iowq_limits[0], | |
9809 | ctx->iowq_limits[1], }; | |
9810 | ||
9811 | ret = io_wq_max_workers(tctx->io_wq, limits); | |
9812 | if (ret) | |
9813 | return ret; | |
9814 | } | |
0f212204 | 9815 | } |
cf27f3b1 PB |
9816 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
9817 | node = kmalloc(sizeof(*node), GFP_KERNEL); | |
9818 | if (!node) | |
9819 | return -ENOMEM; | |
9820 | node->ctx = ctx; | |
9821 | node->task = current; | |
13bf43f5 | 9822 | |
cf27f3b1 PB |
9823 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
9824 | node, GFP_KERNEL)); | |
9825 | if (ret) { | |
9826 | kfree(node); | |
9827 | return ret; | |
0f212204 | 9828 | } |
cf27f3b1 PB |
9829 | |
9830 | mutex_lock(&ctx->uring_lock); | |
9831 | list_add(&node->ctx_node, &ctx->tctx_list); | |
9832 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 9833 | } |
cf27f3b1 | 9834 | tctx->last = ctx; |
0f212204 JA |
9835 | return 0; |
9836 | } | |
9837 | ||
cf27f3b1 PB |
9838 | /* |
9839 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
9840 | */ | |
eef51daa | 9841 | static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
cf27f3b1 PB |
9842 | { |
9843 | struct io_uring_task *tctx = current->io_uring; | |
9844 | ||
9845 | if (likely(tctx && tctx->last == ctx)) | |
9846 | return 0; | |
eef51daa | 9847 | return __io_uring_add_tctx_node(ctx); |
cf27f3b1 PB |
9848 | } |
9849 | ||
0f212204 JA |
9850 | /* |
9851 | * Remove this io_uring_file -> task mapping. | |
9852 | */ | |
c072481d | 9853 | static __cold void io_uring_del_tctx_node(unsigned long index) |
0f212204 JA |
9854 | { |
9855 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 9856 | struct io_tctx_node *node; |
2941267b | 9857 | |
eebd2e37 PB |
9858 | if (!tctx) |
9859 | return; | |
13bf43f5 PB |
9860 | node = xa_erase(&tctx->xa, index); |
9861 | if (!node) | |
2941267b | 9862 | return; |
0f212204 | 9863 | |
13bf43f5 PB |
9864 | WARN_ON_ONCE(current != node->task); |
9865 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
9866 | ||
9867 | mutex_lock(&node->ctx->uring_lock); | |
9868 | list_del(&node->ctx_node); | |
9869 | mutex_unlock(&node->ctx->uring_lock); | |
9870 | ||
baf186c4 | 9871 | if (tctx->last == node->ctx) |
0f212204 | 9872 | tctx->last = NULL; |
13bf43f5 | 9873 | kfree(node); |
0f212204 JA |
9874 | } |
9875 | ||
c072481d | 9876 | static __cold void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 9877 | { |
ba5ef6dc | 9878 | struct io_wq *wq = tctx->io_wq; |
13bf43f5 | 9879 | struct io_tctx_node *node; |
de7f1d9e PB |
9880 | unsigned long index; |
9881 | ||
8bab4c09 | 9882 | xa_for_each(&tctx->xa, index, node) { |
eef51daa | 9883 | io_uring_del_tctx_node(index); |
8bab4c09 JA |
9884 | cond_resched(); |
9885 | } | |
b16ef427 ME |
9886 | if (wq) { |
9887 | /* | |
f6f9b278 | 9888 | * Must be after io_uring_del_tctx_node() (removes nodes under |
b16ef427 ME |
9889 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). |
9890 | */ | |
ba5ef6dc | 9891 | io_wq_put_and_exit(wq); |
dadebc35 | 9892 | tctx->io_wq = NULL; |
b16ef427 | 9893 | } |
de7f1d9e PB |
9894 | } |
9895 | ||
3f48cf18 | 9896 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
521d6a73 | 9897 | { |
3f48cf18 PB |
9898 | if (tracked) |
9899 | return atomic_read(&tctx->inflight_tracked); | |
521d6a73 PB |
9900 | return percpu_counter_sum(&tctx->inflight); |
9901 | } | |
9902 | ||
78cc687b PB |
9903 | /* |
9904 | * Find any io_uring ctx that this task has registered or done IO on, and cancel | |
78a78060 | 9905 | * requests. @sqd should be not-null IFF it's an SQPOLL thread cancellation. |
78cc687b | 9906 | */ |
c072481d PB |
9907 | static __cold void io_uring_cancel_generic(bool cancel_all, |
9908 | struct io_sq_data *sqd) | |
0e9ddb39 | 9909 | { |
521d6a73 | 9910 | struct io_uring_task *tctx = current->io_uring; |
734551df | 9911 | struct io_ring_ctx *ctx; |
0e9ddb39 PB |
9912 | s64 inflight; |
9913 | DEFINE_WAIT(wait); | |
fdaf083c | 9914 | |
78cc687b PB |
9915 | WARN_ON_ONCE(sqd && sqd->thread != current); |
9916 | ||
6d042ffb PO |
9917 | if (!current->io_uring) |
9918 | return; | |
17a91051 PB |
9919 | if (tctx->io_wq) |
9920 | io_wq_exit_start(tctx->io_wq); | |
9921 | ||
0e9ddb39 PB |
9922 | atomic_inc(&tctx->in_idle); |
9923 | do { | |
e9dbe221 | 9924 | io_uring_drop_tctx_refs(current); |
0e9ddb39 | 9925 | /* read completions before cancelations */ |
78cc687b | 9926 | inflight = tctx_inflight(tctx, !cancel_all); |
0e9ddb39 PB |
9927 | if (!inflight) |
9928 | break; | |
fdaf083c | 9929 | |
78cc687b PB |
9930 | if (!sqd) { |
9931 | struct io_tctx_node *node; | |
9932 | unsigned long index; | |
0f212204 | 9933 | |
78cc687b PB |
9934 | xa_for_each(&tctx->xa, index, node) { |
9935 | /* sqpoll task will cancel all its requests */ | |
9936 | if (node->ctx->sq_data) | |
9937 | continue; | |
9938 | io_uring_try_cancel_requests(node->ctx, current, | |
9939 | cancel_all); | |
9940 | } | |
9941 | } else { | |
9942 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
9943 | io_uring_try_cancel_requests(ctx, current, | |
9944 | cancel_all); | |
9945 | } | |
17a91051 | 9946 | |
78a78060 JA |
9947 | prepare_to_wait(&tctx->wait, &wait, TASK_INTERRUPTIBLE); |
9948 | io_run_task_work(); | |
e9dbe221 | 9949 | io_uring_drop_tctx_refs(current); |
78a78060 | 9950 | |
0f212204 | 9951 | /* |
a1bb3cd5 PB |
9952 | * If we've seen completions, retry without waiting. This |
9953 | * avoids a race where a completion comes in before we did | |
9954 | * prepare_to_wait(). | |
0f212204 | 9955 | */ |
3dd0c97a | 9956 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
a1bb3cd5 | 9957 | schedule(); |
f57555ed | 9958 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 9959 | } while (1); |
de7f1d9e | 9960 | |
8452d4a6 | 9961 | io_uring_clean_tctx(tctx); |
3dd0c97a | 9962 | if (cancel_all) { |
3cc7fdb9 PB |
9963 | /* |
9964 | * We shouldn't run task_works after cancel, so just leave | |
9965 | * ->in_idle set for normal exit. | |
9966 | */ | |
9967 | atomic_dec(&tctx->in_idle); | |
3f48cf18 PB |
9968 | /* for exec all current's requests should be gone, kill tctx */ |
9969 | __io_uring_free(current); | |
9970 | } | |
44e728b8 PB |
9971 | } |
9972 | ||
f552a27a | 9973 | void __io_uring_cancel(bool cancel_all) |
78cc687b | 9974 | { |
f552a27a | 9975 | io_uring_cancel_generic(cancel_all, NULL); |
78cc687b PB |
9976 | } |
9977 | ||
6c5c240e RP |
9978 | static void *io_uring_validate_mmap_request(struct file *file, |
9979 | loff_t pgoff, size_t sz) | |
2b188cc1 | 9980 | { |
2b188cc1 | 9981 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 9982 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
9983 | struct page *page; |
9984 | void *ptr; | |
9985 | ||
9986 | switch (offset) { | |
9987 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
9988 | case IORING_OFF_CQ_RING: |
9989 | ptr = ctx->rings; | |
2b188cc1 JA |
9990 | break; |
9991 | case IORING_OFF_SQES: | |
9992 | ptr = ctx->sq_sqes; | |
9993 | break; | |
2b188cc1 | 9994 | default: |
6c5c240e | 9995 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
9996 | } |
9997 | ||
9998 | page = virt_to_head_page(ptr); | |
a50b854e | 9999 | if (sz > page_size(page)) |
6c5c240e RP |
10000 | return ERR_PTR(-EINVAL); |
10001 | ||
10002 | return ptr; | |
10003 | } | |
10004 | ||
10005 | #ifdef CONFIG_MMU | |
10006 | ||
c072481d | 10007 | static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
6c5c240e RP |
10008 | { |
10009 | size_t sz = vma->vm_end - vma->vm_start; | |
10010 | unsigned long pfn; | |
10011 | void *ptr; | |
10012 | ||
10013 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
10014 | if (IS_ERR(ptr)) | |
10015 | return PTR_ERR(ptr); | |
2b188cc1 JA |
10016 | |
10017 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
10018 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
10019 | } | |
10020 | ||
6c5c240e RP |
10021 | #else /* !CONFIG_MMU */ |
10022 | ||
10023 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
10024 | { | |
10025 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
10026 | } | |
10027 | ||
10028 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
10029 | { | |
10030 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
10031 | } | |
10032 | ||
10033 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
10034 | unsigned long addr, unsigned long len, | |
10035 | unsigned long pgoff, unsigned long flags) | |
10036 | { | |
10037 | void *ptr; | |
10038 | ||
10039 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
10040 | if (IS_ERR(ptr)) | |
10041 | return PTR_ERR(ptr); | |
10042 | ||
10043 | return (unsigned long) ptr; | |
10044 | } | |
10045 | ||
10046 | #endif /* !CONFIG_MMU */ | |
10047 | ||
d9d05217 | 10048 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 JA |
10049 | { |
10050 | DEFINE_WAIT(wait); | |
10051 | ||
10052 | do { | |
10053 | if (!io_sqring_full(ctx)) | |
10054 | break; | |
90554200 JA |
10055 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
10056 | ||
10057 | if (!io_sqring_full(ctx)) | |
10058 | break; | |
90554200 JA |
10059 | schedule(); |
10060 | } while (!signal_pending(current)); | |
10061 | ||
10062 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
5199328a | 10063 | return 0; |
90554200 JA |
10064 | } |
10065 | ||
c73ebb68 HX |
10066 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
10067 | struct __kernel_timespec __user **ts, | |
10068 | const sigset_t __user **sig) | |
10069 | { | |
10070 | struct io_uring_getevents_arg arg; | |
10071 | ||
10072 | /* | |
10073 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
10074 | * is just a pointer to the sigset_t. | |
10075 | */ | |
10076 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
10077 | *sig = (const sigset_t __user *) argp; | |
10078 | *ts = NULL; | |
10079 | return 0; | |
10080 | } | |
10081 | ||
10082 | /* | |
10083 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
10084 | * timespec and sigset_t pointers if good. | |
10085 | */ | |
10086 | if (*argsz != sizeof(arg)) | |
10087 | return -EINVAL; | |
10088 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
10089 | return -EFAULT; | |
10090 | *sig = u64_to_user_ptr(arg.sigmask); | |
10091 | *argsz = arg.sigmask_sz; | |
10092 | *ts = u64_to_user_ptr(arg.ts); | |
10093 | return 0; | |
10094 | } | |
10095 | ||
2b188cc1 | 10096 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
10097 | u32, min_complete, u32, flags, const void __user *, argp, |
10098 | size_t, argsz) | |
2b188cc1 JA |
10099 | { |
10100 | struct io_ring_ctx *ctx; | |
2b188cc1 JA |
10101 | int submitted = 0; |
10102 | struct fd f; | |
33f993da | 10103 | long ret; |
2b188cc1 | 10104 | |
4c6e277c | 10105 | io_run_task_work(); |
b41e9852 | 10106 | |
33f993da PB |
10107 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
10108 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) | |
2b188cc1 JA |
10109 | return -EINVAL; |
10110 | ||
10111 | f = fdget(fd); | |
33f993da | 10112 | if (unlikely(!f.file)) |
2b188cc1 JA |
10113 | return -EBADF; |
10114 | ||
10115 | ret = -EOPNOTSUPP; | |
33f993da | 10116 | if (unlikely(f.file->f_op != &io_uring_fops)) |
2b188cc1 JA |
10117 | goto out_fput; |
10118 | ||
10119 | ret = -ENXIO; | |
10120 | ctx = f.file->private_data; | |
33f993da | 10121 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
2b188cc1 JA |
10122 | goto out_fput; |
10123 | ||
7e84e1c7 | 10124 | ret = -EBADFD; |
33f993da | 10125 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
10126 | goto out; |
10127 | ||
6c271ce2 JA |
10128 | /* |
10129 | * For SQ polling, the thread will do all submissions and completions. | |
10130 | * Just return the requested submit count, and wake the thread if | |
10131 | * we were asked to. | |
10132 | */ | |
b2a9eada | 10133 | ret = 0; |
6c271ce2 | 10134 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
90f67366 | 10135 | io_cqring_overflow_flush(ctx); |
89448c47 | 10136 | |
21f96522 JA |
10137 | if (unlikely(ctx->sq_data->thread == NULL)) { |
10138 | ret = -EOWNERDEAD; | |
04147488 | 10139 | goto out; |
21f96522 | 10140 | } |
6c271ce2 | 10141 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 10142 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
10143 | if (flags & IORING_ENTER_SQ_WAIT) { |
10144 | ret = io_sqpoll_wait_sq(ctx); | |
10145 | if (ret) | |
10146 | goto out; | |
10147 | } | |
6c271ce2 | 10148 | submitted = to_submit; |
b2a9eada | 10149 | } else if (to_submit) { |
eef51daa | 10150 | ret = io_uring_add_tctx_node(ctx); |
0f212204 JA |
10151 | if (unlikely(ret)) |
10152 | goto out; | |
2b188cc1 | 10153 | mutex_lock(&ctx->uring_lock); |
0f212204 | 10154 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 10155 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
10156 | |
10157 | if (submitted != to_submit) | |
10158 | goto out; | |
2b188cc1 JA |
10159 | } |
10160 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
10161 | const sigset_t __user *sig; |
10162 | struct __kernel_timespec __user *ts; | |
10163 | ||
10164 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
10165 | if (unlikely(ret)) | |
10166 | goto out; | |
10167 | ||
2b188cc1 JA |
10168 | min_complete = min(min_complete, ctx->cq_entries); |
10169 | ||
32b2244a XW |
10170 | /* |
10171 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
10172 | * space applications don't need to do io completion events | |
10173 | * polling again, they can rely on io_sq_thread to do polling | |
10174 | * work, which can reduce cpu usage and uring_lock contention. | |
10175 | */ | |
10176 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
10177 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 10178 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 10179 | } else { |
c73ebb68 | 10180 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 10181 | } |
2b188cc1 JA |
10182 | } |
10183 | ||
7c504e65 | 10184 | out: |
6805b32e | 10185 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
10186 | out_fput: |
10187 | fdput(f); | |
10188 | return submitted ? submitted : ret; | |
10189 | } | |
10190 | ||
bebdb65e | 10191 | #ifdef CONFIG_PROC_FS |
c072481d | 10192 | static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id, |
61cf9370 | 10193 | const struct cred *cred) |
87ce955b | 10194 | { |
87ce955b JA |
10195 | struct user_namespace *uns = seq_user_ns(m); |
10196 | struct group_info *gi; | |
10197 | kernel_cap_t cap; | |
10198 | unsigned __capi; | |
10199 | int g; | |
10200 | ||
10201 | seq_printf(m, "%5d\n", id); | |
10202 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
10203 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
10204 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
10205 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
10206 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
10207 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
10208 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
10209 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
10210 | seq_puts(m, "\n\tGroups:\t"); | |
10211 | gi = cred->group_info; | |
10212 | for (g = 0; g < gi->ngroups; g++) { | |
10213 | seq_put_decimal_ull(m, g ? " " : "", | |
10214 | from_kgid_munged(uns, gi->gid[g])); | |
10215 | } | |
10216 | seq_puts(m, "\n\tCapEff:\t"); | |
10217 | cap = cred->cap_effective; | |
10218 | CAP_FOR_EACH_U32(__capi) | |
10219 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
10220 | seq_putc(m, '\n'); | |
10221 | return 0; | |
10222 | } | |
10223 | ||
c072481d PB |
10224 | static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, |
10225 | struct seq_file *m) | |
87ce955b | 10226 | { |
dbbe9c64 | 10227 | struct io_sq_data *sq = NULL; |
83f84356 HX |
10228 | struct io_overflow_cqe *ocqe; |
10229 | struct io_rings *r = ctx->rings; | |
10230 | unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1; | |
83f84356 HX |
10231 | unsigned int sq_head = READ_ONCE(r->sq.head); |
10232 | unsigned int sq_tail = READ_ONCE(r->sq.tail); | |
10233 | unsigned int cq_head = READ_ONCE(r->cq.head); | |
10234 | unsigned int cq_tail = READ_ONCE(r->cq.tail); | |
f75d1183 | 10235 | unsigned int sq_entries, cq_entries; |
fad8e0de | 10236 | bool has_lock; |
83f84356 HX |
10237 | unsigned int i; |
10238 | ||
10239 | /* | |
10240 | * we may get imprecise sqe and cqe info if uring is actively running | |
10241 | * since we get cached_sq_head and cached_cq_tail without uring_lock | |
10242 | * and sq_tail and cq_head are changed by userspace. But it's ok since | |
10243 | * we usually use these info when it is stuck. | |
10244 | */ | |
c0235652 | 10245 | seq_printf(m, "SqMask:\t0x%x\n", sq_mask); |
f75d1183 JA |
10246 | seq_printf(m, "SqHead:\t%u\n", sq_head); |
10247 | seq_printf(m, "SqTail:\t%u\n", sq_tail); | |
10248 | seq_printf(m, "CachedSqHead:\t%u\n", ctx->cached_sq_head); | |
10249 | seq_printf(m, "CqMask:\t0x%x\n", cq_mask); | |
10250 | seq_printf(m, "CqHead:\t%u\n", cq_head); | |
10251 | seq_printf(m, "CqTail:\t%u\n", cq_tail); | |
10252 | seq_printf(m, "CachedCqTail:\t%u\n", ctx->cached_cq_tail); | |
10253 | seq_printf(m, "SQEs:\t%u\n", sq_tail - ctx->cached_sq_head); | |
10254 | sq_entries = min(sq_tail - sq_head, ctx->sq_entries); | |
10255 | for (i = 0; i < sq_entries; i++) { | |
10256 | unsigned int entry = i + sq_head; | |
10257 | unsigned int sq_idx = READ_ONCE(ctx->sq_array[entry & sq_mask]); | |
a1957780 | 10258 | struct io_uring_sqe *sqe; |
f75d1183 JA |
10259 | |
10260 | if (sq_idx > sq_mask) | |
10261 | continue; | |
10262 | sqe = &ctx->sq_sqes[sq_idx]; | |
10263 | seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n", | |
10264 | sq_idx, sqe->opcode, sqe->fd, sqe->flags, | |
10265 | sqe->user_data); | |
83f84356 | 10266 | } |
f75d1183 JA |
10267 | seq_printf(m, "CQEs:\t%u\n", cq_tail - cq_head); |
10268 | cq_entries = min(cq_tail - cq_head, ctx->cq_entries); | |
10269 | for (i = 0; i < cq_entries; i++) { | |
10270 | unsigned int entry = i + cq_head; | |
10271 | struct io_uring_cqe *cqe = &r->cqes[entry & cq_mask]; | |
83f84356 HX |
10272 | |
10273 | seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n", | |
f75d1183 JA |
10274 | entry & cq_mask, cqe->user_data, cqe->res, |
10275 | cqe->flags); | |
83f84356 | 10276 | } |
87ce955b | 10277 | |
fad8e0de JA |
10278 | /* |
10279 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
10280 | * since fdinfo case grabs it in the opposite direction of normal use | |
10281 | * cases. If we fail to get the lock, we just don't iterate any | |
10282 | * structures that could be going away outside the io_uring mutex. | |
10283 | */ | |
10284 | has_lock = mutex_trylock(&ctx->uring_lock); | |
10285 | ||
5f3f26f9 | 10286 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 10287 | sq = ctx->sq_data; |
5f3f26f9 JA |
10288 | if (!sq->thread) |
10289 | sq = NULL; | |
10290 | } | |
dbbe9c64 JQ |
10291 | |
10292 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
10293 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 10294 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 10295 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
7b29f92d | 10296 | struct file *f = io_file_from_index(ctx, i); |
87ce955b | 10297 | |
87ce955b JA |
10298 | if (f) |
10299 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
10300 | else | |
10301 | seq_printf(m, "%5u: <none>\n", i); | |
10302 | } | |
10303 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 10304 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
41edf1a5 | 10305 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
4751f53d | 10306 | unsigned int len = buf->ubuf_end - buf->ubuf; |
87ce955b | 10307 | |
4751f53d | 10308 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
87ce955b | 10309 | } |
61cf9370 MWO |
10310 | if (has_lock && !xa_empty(&ctx->personalities)) { |
10311 | unsigned long index; | |
10312 | const struct cred *cred; | |
10313 | ||
87ce955b | 10314 | seq_printf(m, "Personalities:\n"); |
61cf9370 MWO |
10315 | xa_for_each(&ctx->personalities, index, cred) |
10316 | io_uring_show_cred(m, index, cred); | |
87ce955b | 10317 | } |
83f84356 HX |
10318 | if (has_lock) |
10319 | mutex_unlock(&ctx->uring_lock); | |
10320 | ||
10321 | seq_puts(m, "PollList:\n"); | |
79ebeaee | 10322 | spin_lock(&ctx->completion_lock); |
d7718a9d JA |
10323 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
10324 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
10325 | struct io_kiocb *req; | |
10326 | ||
10327 | hlist_for_each_entry(req, list, hash_node) | |
10328 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
10329 | req->task->task_works != NULL); | |
10330 | } | |
83f84356 HX |
10331 | |
10332 | seq_puts(m, "CqOverflowList:\n"); | |
10333 | list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) { | |
10334 | struct io_uring_cqe *cqe = &ocqe->cqe; | |
10335 | ||
10336 | seq_printf(m, " user_data=%llu, res=%d, flags=%x\n", | |
10337 | cqe->user_data, cqe->res, cqe->flags); | |
10338 | ||
10339 | } | |
10340 | ||
79ebeaee | 10341 | spin_unlock(&ctx->completion_lock); |
87ce955b JA |
10342 | } |
10343 | ||
c072481d | 10344 | static __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
87ce955b JA |
10345 | { |
10346 | struct io_ring_ctx *ctx = f->private_data; | |
10347 | ||
10348 | if (percpu_ref_tryget(&ctx->refs)) { | |
10349 | __io_uring_show_fdinfo(ctx, m); | |
10350 | percpu_ref_put(&ctx->refs); | |
10351 | } | |
10352 | } | |
bebdb65e | 10353 | #endif |
87ce955b | 10354 | |
2b188cc1 JA |
10355 | static const struct file_operations io_uring_fops = { |
10356 | .release = io_uring_release, | |
10357 | .mmap = io_uring_mmap, | |
6c5c240e RP |
10358 | #ifndef CONFIG_MMU |
10359 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
10360 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
10361 | #endif | |
2b188cc1 | 10362 | .poll = io_uring_poll, |
bebdb65e | 10363 | #ifdef CONFIG_PROC_FS |
87ce955b | 10364 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 10365 | #endif |
2b188cc1 JA |
10366 | }; |
10367 | ||
c072481d PB |
10368 | static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
10369 | struct io_uring_params *p) | |
2b188cc1 | 10370 | { |
75b28aff HV |
10371 | struct io_rings *rings; |
10372 | size_t size, sq_array_offset; | |
2b188cc1 | 10373 | |
bd740481 JA |
10374 | /* make sure these are sane, as we already accounted them */ |
10375 | ctx->sq_entries = p->sq_entries; | |
10376 | ctx->cq_entries = p->cq_entries; | |
10377 | ||
75b28aff HV |
10378 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
10379 | if (size == SIZE_MAX) | |
10380 | return -EOVERFLOW; | |
10381 | ||
10382 | rings = io_mem_alloc(size); | |
10383 | if (!rings) | |
2b188cc1 JA |
10384 | return -ENOMEM; |
10385 | ||
75b28aff HV |
10386 | ctx->rings = rings; |
10387 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
10388 | rings->sq_ring_mask = p->sq_entries - 1; | |
10389 | rings->cq_ring_mask = p->cq_entries - 1; | |
10390 | rings->sq_ring_entries = p->sq_entries; | |
10391 | rings->cq_ring_entries = p->cq_entries; | |
2b188cc1 JA |
10392 | |
10393 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
10394 | if (size == SIZE_MAX) { |
10395 | io_mem_free(ctx->rings); | |
10396 | ctx->rings = NULL; | |
2b188cc1 | 10397 | return -EOVERFLOW; |
eb065d30 | 10398 | } |
2b188cc1 JA |
10399 | |
10400 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
10401 | if (!ctx->sq_sqes) { |
10402 | io_mem_free(ctx->rings); | |
10403 | ctx->rings = NULL; | |
2b188cc1 | 10404 | return -ENOMEM; |
eb065d30 | 10405 | } |
2b188cc1 | 10406 | |
2b188cc1 JA |
10407 | return 0; |
10408 | } | |
10409 | ||
9faadcc8 PB |
10410 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
10411 | { | |
10412 | int ret, fd; | |
10413 | ||
10414 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
10415 | if (fd < 0) | |
10416 | return fd; | |
10417 | ||
eef51daa | 10418 | ret = io_uring_add_tctx_node(ctx); |
9faadcc8 PB |
10419 | if (ret) { |
10420 | put_unused_fd(fd); | |
10421 | return ret; | |
10422 | } | |
10423 | fd_install(fd, file); | |
10424 | return fd; | |
10425 | } | |
10426 | ||
2b188cc1 JA |
10427 | /* |
10428 | * Allocate an anonymous fd, this is what constitutes the application | |
10429 | * visible backing of an io_uring instance. The application mmaps this | |
10430 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
10431 | * we have to tie this fd to a socket for file garbage collection purposes. | |
10432 | */ | |
9faadcc8 | 10433 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
10434 | { |
10435 | struct file *file; | |
9faadcc8 | 10436 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
10437 | int ret; |
10438 | ||
2b188cc1 JA |
10439 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
10440 | &ctx->ring_sock); | |
10441 | if (ret) | |
9faadcc8 | 10442 | return ERR_PTR(ret); |
2b188cc1 JA |
10443 | #endif |
10444 | ||
91a9ab7c PM |
10445 | file = anon_inode_getfile_secure("[io_uring]", &io_uring_fops, ctx, |
10446 | O_RDWR | O_CLOEXEC, NULL); | |
2b188cc1 | 10447 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
10448 | if (IS_ERR(file)) { |
10449 | sock_release(ctx->ring_sock); | |
10450 | ctx->ring_sock = NULL; | |
10451 | } else { | |
10452 | ctx->ring_sock->file = file; | |
0f212204 | 10453 | } |
2b188cc1 | 10454 | #endif |
9faadcc8 | 10455 | return file; |
2b188cc1 JA |
10456 | } |
10457 | ||
c072481d PB |
10458 | static __cold int io_uring_create(unsigned entries, struct io_uring_params *p, |
10459 | struct io_uring_params __user *params) | |
2b188cc1 | 10460 | { |
2b188cc1 | 10461 | struct io_ring_ctx *ctx; |
9faadcc8 | 10462 | struct file *file; |
2b188cc1 JA |
10463 | int ret; |
10464 | ||
8110c1a6 | 10465 | if (!entries) |
2b188cc1 | 10466 | return -EINVAL; |
8110c1a6 JA |
10467 | if (entries > IORING_MAX_ENTRIES) { |
10468 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
10469 | return -EINVAL; | |
10470 | entries = IORING_MAX_ENTRIES; | |
10471 | } | |
2b188cc1 JA |
10472 | |
10473 | /* | |
10474 | * Use twice as many entries for the CQ ring. It's possible for the | |
10475 | * application to drive a higher depth than the size of the SQ ring, | |
10476 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
10477 | * some flexibility in overcommitting a bit. If the application has |
10478 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
10479 | * of CQ ring entries manually. | |
2b188cc1 JA |
10480 | */ |
10481 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
10482 | if (p->flags & IORING_SETUP_CQSIZE) { |
10483 | /* | |
10484 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
10485 | * to a power-of-two, if it isn't already. We do NOT impose | |
10486 | * any cq vs sq ring sizing. | |
10487 | */ | |
eb2667b3 | 10488 | if (!p->cq_entries) |
33a107f0 | 10489 | return -EINVAL; |
8110c1a6 JA |
10490 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
10491 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
10492 | return -EINVAL; | |
10493 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
10494 | } | |
eb2667b3 JQ |
10495 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
10496 | if (p->cq_entries < p->sq_entries) | |
10497 | return -EINVAL; | |
33a107f0 JA |
10498 | } else { |
10499 | p->cq_entries = 2 * p->sq_entries; | |
10500 | } | |
2b188cc1 | 10501 | |
2b188cc1 | 10502 | ctx = io_ring_ctx_alloc(p); |
62e398be | 10503 | if (!ctx) |
2b188cc1 | 10504 | return -ENOMEM; |
2b188cc1 | 10505 | ctx->compat = in_compat_syscall(); |
62e398be JA |
10506 | if (!capable(CAP_IPC_LOCK)) |
10507 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
10508 | |
10509 | /* | |
10510 | * This is just grabbed for accounting purposes. When a process exits, | |
10511 | * the mm is exited and dropped before the files, hence we need to hang | |
10512 | * on to this mm purely for the purposes of being able to unaccount | |
10513 | * memory (locked/pinned vm). It's not used for anything else. | |
10514 | */ | |
6b7898eb | 10515 | mmgrab(current->mm); |
2aede0e4 | 10516 | ctx->mm_account = current->mm; |
6b7898eb | 10517 | |
2b188cc1 JA |
10518 | ret = io_allocate_scq_urings(ctx, p); |
10519 | if (ret) | |
10520 | goto err; | |
10521 | ||
7e84e1c7 | 10522 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
10523 | if (ret) |
10524 | goto err; | |
eae071c9 | 10525 | /* always set a rsrc node */ |
47b228ce PB |
10526 | ret = io_rsrc_node_switch_start(ctx); |
10527 | if (ret) | |
10528 | goto err; | |
eae071c9 | 10529 | io_rsrc_node_switch(ctx, NULL); |
2b188cc1 | 10530 | |
2b188cc1 | 10531 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
10532 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
10533 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
10534 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
10535 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
10536 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
10537 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
10538 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
10539 | |
10540 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
10541 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
10542 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
10543 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
10544 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
10545 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
10546 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 10547 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 10548 | |
7f13657d XW |
10549 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
10550 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 10551 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 10552 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9690557e | 10553 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
04c76b41 | 10554 | IORING_FEAT_RSRC_TAGS | IORING_FEAT_CQE_SKIP; |
7f13657d XW |
10555 | |
10556 | if (copy_to_user(params, p, sizeof(*p))) { | |
10557 | ret = -EFAULT; | |
10558 | goto err; | |
10559 | } | |
d1719f70 | 10560 | |
9faadcc8 PB |
10561 | file = io_uring_get_file(ctx); |
10562 | if (IS_ERR(file)) { | |
10563 | ret = PTR_ERR(file); | |
10564 | goto err; | |
10565 | } | |
10566 | ||
044c1ab3 JA |
10567 | /* |
10568 | * Install ring fd as the very last thing, so we don't risk someone | |
10569 | * having closed it before we finish setup | |
10570 | */ | |
9faadcc8 PB |
10571 | ret = io_uring_install_fd(ctx, file); |
10572 | if (ret < 0) { | |
10573 | /* fput will clean it up */ | |
10574 | fput(file); | |
10575 | return ret; | |
10576 | } | |
044c1ab3 | 10577 | |
c826bd7a | 10578 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
10579 | return ret; |
10580 | err: | |
10581 | io_ring_ctx_wait_and_kill(ctx); | |
10582 | return ret; | |
10583 | } | |
10584 | ||
10585 | /* | |
10586 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
10587 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
10588 | * params structure passed in. | |
10589 | */ | |
10590 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
10591 | { | |
10592 | struct io_uring_params p; | |
2b188cc1 JA |
10593 | int i; |
10594 | ||
10595 | if (copy_from_user(&p, params, sizeof(p))) | |
10596 | return -EFAULT; | |
10597 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
10598 | if (p.resv[i]) | |
10599 | return -EINVAL; | |
10600 | } | |
10601 | ||
6c271ce2 | 10602 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 10603 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
10604 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
10605 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
10606 | return -EINVAL; |
10607 | ||
7f13657d | 10608 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
10609 | } |
10610 | ||
10611 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
10612 | struct io_uring_params __user *, params) | |
10613 | { | |
10614 | return io_uring_setup(entries, params); | |
10615 | } | |
10616 | ||
c072481d PB |
10617 | static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg, |
10618 | unsigned nr_args) | |
66f4af93 JA |
10619 | { |
10620 | struct io_uring_probe *p; | |
10621 | size_t size; | |
10622 | int i, ret; | |
10623 | ||
10624 | size = struct_size(p, ops, nr_args); | |
10625 | if (size == SIZE_MAX) | |
10626 | return -EOVERFLOW; | |
10627 | p = kzalloc(size, GFP_KERNEL); | |
10628 | if (!p) | |
10629 | return -ENOMEM; | |
10630 | ||
10631 | ret = -EFAULT; | |
10632 | if (copy_from_user(p, arg, size)) | |
10633 | goto out; | |
10634 | ret = -EINVAL; | |
10635 | if (memchr_inv(p, 0, size)) | |
10636 | goto out; | |
10637 | ||
10638 | p->last_op = IORING_OP_LAST - 1; | |
10639 | if (nr_args > IORING_OP_LAST) | |
10640 | nr_args = IORING_OP_LAST; | |
10641 | ||
10642 | for (i = 0; i < nr_args; i++) { | |
10643 | p->ops[i].op = i; | |
10644 | if (!io_op_defs[i].not_supported) | |
10645 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
10646 | } | |
10647 | p->ops_len = i; | |
10648 | ||
10649 | ret = 0; | |
10650 | if (copy_to_user(arg, p, size)) | |
10651 | ret = -EFAULT; | |
10652 | out: | |
10653 | kfree(p); | |
10654 | return ret; | |
10655 | } | |
10656 | ||
071698e1 JA |
10657 | static int io_register_personality(struct io_ring_ctx *ctx) |
10658 | { | |
4379bf8b | 10659 | const struct cred *creds; |
61cf9370 | 10660 | u32 id; |
1e6fa521 | 10661 | int ret; |
071698e1 | 10662 | |
4379bf8b | 10663 | creds = get_current_cred(); |
1e6fa521 | 10664 | |
61cf9370 MWO |
10665 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
10666 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); | |
a30f895a JA |
10667 | if (ret < 0) { |
10668 | put_cred(creds); | |
10669 | return ret; | |
10670 | } | |
10671 | return id; | |
071698e1 JA |
10672 | } |
10673 | ||
c072481d PB |
10674 | static __cold int io_register_restrictions(struct io_ring_ctx *ctx, |
10675 | void __user *arg, unsigned int nr_args) | |
21b55dbc SG |
10676 | { |
10677 | struct io_uring_restriction *res; | |
10678 | size_t size; | |
10679 | int i, ret; | |
10680 | ||
7e84e1c7 SG |
10681 | /* Restrictions allowed only if rings started disabled */ |
10682 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10683 | return -EBADFD; | |
10684 | ||
21b55dbc | 10685 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 10686 | if (ctx->restrictions.registered) |
21b55dbc SG |
10687 | return -EBUSY; |
10688 | ||
10689 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
10690 | return -EINVAL; | |
10691 | ||
10692 | size = array_size(nr_args, sizeof(*res)); | |
10693 | if (size == SIZE_MAX) | |
10694 | return -EOVERFLOW; | |
10695 | ||
10696 | res = memdup_user(arg, size); | |
10697 | if (IS_ERR(res)) | |
10698 | return PTR_ERR(res); | |
10699 | ||
10700 | ret = 0; | |
10701 | ||
10702 | for (i = 0; i < nr_args; i++) { | |
10703 | switch (res[i].opcode) { | |
10704 | case IORING_RESTRICTION_REGISTER_OP: | |
10705 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
10706 | ret = -EINVAL; | |
10707 | goto out; | |
10708 | } | |
10709 | ||
10710 | __set_bit(res[i].register_op, | |
10711 | ctx->restrictions.register_op); | |
10712 | break; | |
10713 | case IORING_RESTRICTION_SQE_OP: | |
10714 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
10715 | ret = -EINVAL; | |
10716 | goto out; | |
10717 | } | |
10718 | ||
10719 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
10720 | break; | |
10721 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
10722 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
10723 | break; | |
10724 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
10725 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
10726 | break; | |
10727 | default: | |
10728 | ret = -EINVAL; | |
10729 | goto out; | |
10730 | } | |
10731 | } | |
10732 | ||
10733 | out: | |
10734 | /* Reset all restrictions if an error happened */ | |
10735 | if (ret != 0) | |
10736 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
10737 | else | |
7e84e1c7 | 10738 | ctx->restrictions.registered = true; |
21b55dbc SG |
10739 | |
10740 | kfree(res); | |
10741 | return ret; | |
10742 | } | |
10743 | ||
7e84e1c7 SG |
10744 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
10745 | { | |
10746 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10747 | return -EBADFD; | |
10748 | ||
10749 | if (ctx->restrictions.registered) | |
10750 | ctx->restricted = 1; | |
10751 | ||
0298ef96 PB |
10752 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
10753 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) | |
10754 | wake_up(&ctx->sq_data->wait); | |
7e84e1c7 SG |
10755 | return 0; |
10756 | } | |
10757 | ||
fdecb662 | 10758 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 10759 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 PB |
10760 | unsigned nr_args) |
10761 | { | |
10762 | __u32 tmp; | |
10763 | int err; | |
10764 | ||
c3bdad02 PB |
10765 | if (up->resv) |
10766 | return -EINVAL; | |
98f0b3b4 PB |
10767 | if (check_add_overflow(up->offset, nr_args, &tmp)) |
10768 | return -EOVERFLOW; | |
10769 | err = io_rsrc_node_switch_start(ctx); | |
10770 | if (err) | |
10771 | return err; | |
10772 | ||
fdecb662 PB |
10773 | switch (type) { |
10774 | case IORING_RSRC_FILE: | |
98f0b3b4 | 10775 | return __io_sqe_files_update(ctx, up, nr_args); |
634d00df PB |
10776 | case IORING_RSRC_BUFFER: |
10777 | return __io_sqe_buffers_update(ctx, up, nr_args); | |
98f0b3b4 PB |
10778 | } |
10779 | return -EINVAL; | |
10780 | } | |
10781 | ||
c3bdad02 PB |
10782 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
10783 | unsigned nr_args) | |
98f0b3b4 | 10784 | { |
c3bdad02 | 10785 | struct io_uring_rsrc_update2 up; |
98f0b3b4 PB |
10786 | |
10787 | if (!nr_args) | |
10788 | return -EINVAL; | |
c3bdad02 PB |
10789 | memset(&up, 0, sizeof(up)); |
10790 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) | |
10791 | return -EFAULT; | |
10792 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); | |
10793 | } | |
10794 | ||
10795 | static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
992da01a | 10796 | unsigned size, unsigned type) |
c3bdad02 PB |
10797 | { |
10798 | struct io_uring_rsrc_update2 up; | |
10799 | ||
10800 | if (size != sizeof(up)) | |
10801 | return -EINVAL; | |
98f0b3b4 PB |
10802 | if (copy_from_user(&up, arg, sizeof(up))) |
10803 | return -EFAULT; | |
992da01a | 10804 | if (!up.nr || up.resv) |
98f0b3b4 | 10805 | return -EINVAL; |
992da01a | 10806 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
98f0b3b4 PB |
10807 | } |
10808 | ||
c072481d | 10809 | static __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
992da01a | 10810 | unsigned int size, unsigned int type) |
792e3582 PB |
10811 | { |
10812 | struct io_uring_rsrc_register rr; | |
10813 | ||
10814 | /* keep it extendible */ | |
10815 | if (size != sizeof(rr)) | |
10816 | return -EINVAL; | |
10817 | ||
10818 | memset(&rr, 0, sizeof(rr)); | |
10819 | if (copy_from_user(&rr, arg, size)) | |
10820 | return -EFAULT; | |
992da01a | 10821 | if (!rr.nr || rr.resv || rr.resv2) |
792e3582 PB |
10822 | return -EINVAL; |
10823 | ||
992da01a | 10824 | switch (type) { |
792e3582 PB |
10825 | case IORING_RSRC_FILE: |
10826 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), | |
10827 | rr.nr, u64_to_user_ptr(rr.tags)); | |
634d00df PB |
10828 | case IORING_RSRC_BUFFER: |
10829 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), | |
10830 | rr.nr, u64_to_user_ptr(rr.tags)); | |
792e3582 PB |
10831 | } |
10832 | return -EINVAL; | |
10833 | } | |
10834 | ||
c072481d PB |
10835 | static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx, |
10836 | void __user *arg, unsigned len) | |
fe76421d JA |
10837 | { |
10838 | struct io_uring_task *tctx = current->io_uring; | |
10839 | cpumask_var_t new_mask; | |
10840 | int ret; | |
10841 | ||
10842 | if (!tctx || !tctx->io_wq) | |
10843 | return -EINVAL; | |
10844 | ||
10845 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | |
10846 | return -ENOMEM; | |
10847 | ||
10848 | cpumask_clear(new_mask); | |
10849 | if (len > cpumask_size()) | |
10850 | len = cpumask_size(); | |
10851 | ||
10852 | if (copy_from_user(new_mask, arg, len)) { | |
10853 | free_cpumask_var(new_mask); | |
10854 | return -EFAULT; | |
10855 | } | |
10856 | ||
10857 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); | |
10858 | free_cpumask_var(new_mask); | |
10859 | return ret; | |
10860 | } | |
10861 | ||
c072481d | 10862 | static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx) |
fe76421d JA |
10863 | { |
10864 | struct io_uring_task *tctx = current->io_uring; | |
10865 | ||
10866 | if (!tctx || !tctx->io_wq) | |
10867 | return -EINVAL; | |
10868 | ||
10869 | return io_wq_cpu_affinity(tctx->io_wq, NULL); | |
10870 | } | |
10871 | ||
c072481d PB |
10872 | static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, |
10873 | void __user *arg) | |
b22fa62a | 10874 | __must_hold(&ctx->uring_lock) |
2e480058 | 10875 | { |
b22fa62a | 10876 | struct io_tctx_node *node; |
fa84693b JA |
10877 | struct io_uring_task *tctx = NULL; |
10878 | struct io_sq_data *sqd = NULL; | |
2e480058 JA |
10879 | __u32 new_count[2]; |
10880 | int i, ret; | |
10881 | ||
2e480058 JA |
10882 | if (copy_from_user(new_count, arg, sizeof(new_count))) |
10883 | return -EFAULT; | |
10884 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
10885 | if (new_count[i] > INT_MAX) | |
10886 | return -EINVAL; | |
10887 | ||
fa84693b JA |
10888 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
10889 | sqd = ctx->sq_data; | |
10890 | if (sqd) { | |
009ad9f0 JA |
10891 | /* |
10892 | * Observe the correct sqd->lock -> ctx->uring_lock | |
10893 | * ordering. Fine to drop uring_lock here, we hold | |
10894 | * a ref to the ctx. | |
10895 | */ | |
41d3a6bd | 10896 | refcount_inc(&sqd->refs); |
009ad9f0 | 10897 | mutex_unlock(&ctx->uring_lock); |
fa84693b | 10898 | mutex_lock(&sqd->lock); |
009ad9f0 | 10899 | mutex_lock(&ctx->uring_lock); |
41d3a6bd JA |
10900 | if (sqd->thread) |
10901 | tctx = sqd->thread->io_uring; | |
fa84693b JA |
10902 | } |
10903 | } else { | |
10904 | tctx = current->io_uring; | |
10905 | } | |
10906 | ||
e139a1ec | 10907 | BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits)); |
fa84693b | 10908 | |
bad119b9 PB |
10909 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
10910 | if (new_count[i]) | |
10911 | ctx->iowq_limits[i] = new_count[i]; | |
e139a1ec PB |
10912 | ctx->iowq_limits_set = true; |
10913 | ||
e139a1ec PB |
10914 | if (tctx && tctx->io_wq) { |
10915 | ret = io_wq_max_workers(tctx->io_wq, new_count); | |
10916 | if (ret) | |
10917 | goto err; | |
10918 | } else { | |
10919 | memset(new_count, 0, sizeof(new_count)); | |
10920 | } | |
fa84693b | 10921 | |
41d3a6bd | 10922 | if (sqd) { |
fa84693b | 10923 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
10924 | io_put_sq_data(sqd); |
10925 | } | |
2e480058 JA |
10926 | |
10927 | if (copy_to_user(arg, new_count, sizeof(new_count))) | |
10928 | return -EFAULT; | |
10929 | ||
b22fa62a PB |
10930 | /* that's it for SQPOLL, only the SQPOLL task creates requests */ |
10931 | if (sqd) | |
10932 | return 0; | |
10933 | ||
10934 | /* now propagate the restriction to all registered users */ | |
10935 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
10936 | struct io_uring_task *tctx = node->task->io_uring; | |
10937 | ||
10938 | if (WARN_ON_ONCE(!tctx->io_wq)) | |
10939 | continue; | |
10940 | ||
10941 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
10942 | new_count[i] = ctx->iowq_limits[i]; | |
10943 | /* ignore errors, it always returns zero anyway */ | |
10944 | (void)io_wq_max_workers(tctx->io_wq, new_count); | |
10945 | } | |
2e480058 | 10946 | return 0; |
fa84693b | 10947 | err: |
41d3a6bd | 10948 | if (sqd) { |
fa84693b | 10949 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
10950 | io_put_sq_data(sqd); |
10951 | } | |
fa84693b | 10952 | return ret; |
2e480058 JA |
10953 | } |
10954 | ||
071698e1 JA |
10955 | static bool io_register_op_must_quiesce(int op) |
10956 | { | |
10957 | switch (op) { | |
bd54b6fe BM |
10958 | case IORING_REGISTER_BUFFERS: |
10959 | case IORING_UNREGISTER_BUFFERS: | |
f4f7d21c | 10960 | case IORING_REGISTER_FILES: |
071698e1 JA |
10961 | case IORING_UNREGISTER_FILES: |
10962 | case IORING_REGISTER_FILES_UPDATE: | |
10963 | case IORING_REGISTER_PROBE: | |
10964 | case IORING_REGISTER_PERSONALITY: | |
10965 | case IORING_UNREGISTER_PERSONALITY: | |
992da01a PB |
10966 | case IORING_REGISTER_FILES2: |
10967 | case IORING_REGISTER_FILES_UPDATE2: | |
10968 | case IORING_REGISTER_BUFFERS2: | |
10969 | case IORING_REGISTER_BUFFERS_UPDATE: | |
fe76421d JA |
10970 | case IORING_REGISTER_IOWQ_AFF: |
10971 | case IORING_UNREGISTER_IOWQ_AFF: | |
2e480058 | 10972 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
071698e1 JA |
10973 | return false; |
10974 | default: | |
10975 | return true; | |
10976 | } | |
10977 | } | |
10978 | ||
c072481d | 10979 | static __cold int io_ctx_quiesce(struct io_ring_ctx *ctx) |
e73c5c7c PB |
10980 | { |
10981 | long ret; | |
10982 | ||
10983 | percpu_ref_kill(&ctx->refs); | |
10984 | ||
10985 | /* | |
10986 | * Drop uring mutex before waiting for references to exit. If another | |
10987 | * thread is currently inside io_uring_enter() it might need to grab the | |
10988 | * uring_lock to make progress. If we hold it here across the drain | |
10989 | * wait, then we can deadlock. It's safe to drop the mutex here, since | |
10990 | * no new references will come in after we've killed the percpu ref. | |
10991 | */ | |
10992 | mutex_unlock(&ctx->uring_lock); | |
10993 | do { | |
37f0e767 PB |
10994 | ret = wait_for_completion_interruptible_timeout(&ctx->ref_comp, HZ); |
10995 | if (ret) { | |
10996 | ret = min(0L, ret); | |
e73c5c7c | 10997 | break; |
37f0e767 PB |
10998 | } |
10999 | ||
e73c5c7c | 11000 | ret = io_run_task_work_sig(); |
37f0e767 | 11001 | io_req_caches_free(ctx); |
e73c5c7c PB |
11002 | } while (ret >= 0); |
11003 | mutex_lock(&ctx->uring_lock); | |
11004 | ||
11005 | if (ret) | |
11006 | io_refs_resurrect(&ctx->refs, &ctx->ref_comp); | |
11007 | return ret; | |
11008 | } | |
11009 | ||
edafccee JA |
11010 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
11011 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
11012 | __releases(ctx->uring_lock) |
11013 | __acquires(ctx->uring_lock) | |
edafccee JA |
11014 | { |
11015 | int ret; | |
11016 | ||
35fa71a0 JA |
11017 | /* |
11018 | * We're inside the ring mutex, if the ref is already dying, then | |
11019 | * someone else killed the ctx or is already going through | |
11020 | * io_uring_register(). | |
11021 | */ | |
11022 | if (percpu_ref_is_dying(&ctx->refs)) | |
11023 | return -ENXIO; | |
11024 | ||
75c4021a PB |
11025 | if (ctx->restricted) { |
11026 | if (opcode >= IORING_REGISTER_LAST) | |
11027 | return -EINVAL; | |
11028 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); | |
11029 | if (!test_bit(opcode, ctx->restrictions.register_op)) | |
11030 | return -EACCES; | |
11031 | } | |
11032 | ||
071698e1 | 11033 | if (io_register_op_must_quiesce(opcode)) { |
e73c5c7c PB |
11034 | ret = io_ctx_quiesce(ctx); |
11035 | if (ret) | |
f70865db | 11036 | return ret; |
05f3fb3c | 11037 | } |
edafccee JA |
11038 | |
11039 | switch (opcode) { | |
11040 | case IORING_REGISTER_BUFFERS: | |
634d00df | 11041 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
edafccee JA |
11042 | break; |
11043 | case IORING_UNREGISTER_BUFFERS: | |
11044 | ret = -EINVAL; | |
11045 | if (arg || nr_args) | |
11046 | break; | |
0a96bbe4 | 11047 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 11048 | break; |
6b06314c | 11049 | case IORING_REGISTER_FILES: |
792e3582 | 11050 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
6b06314c JA |
11051 | break; |
11052 | case IORING_UNREGISTER_FILES: | |
11053 | ret = -EINVAL; | |
11054 | if (arg || nr_args) | |
11055 | break; | |
11056 | ret = io_sqe_files_unregister(ctx); | |
11057 | break; | |
c3a31e60 | 11058 | case IORING_REGISTER_FILES_UPDATE: |
c3bdad02 | 11059 | ret = io_register_files_update(ctx, arg, nr_args); |
c3a31e60 | 11060 | break; |
9b402849 | 11061 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 11062 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
11063 | ret = -EINVAL; |
11064 | if (nr_args != 1) | |
11065 | break; | |
11066 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
11067 | if (ret) |
11068 | break; | |
11069 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
11070 | ctx->eventfd_async = 1; | |
11071 | else | |
11072 | ctx->eventfd_async = 0; | |
9b402849 JA |
11073 | break; |
11074 | case IORING_UNREGISTER_EVENTFD: | |
11075 | ret = -EINVAL; | |
11076 | if (arg || nr_args) | |
11077 | break; | |
11078 | ret = io_eventfd_unregister(ctx); | |
11079 | break; | |
66f4af93 JA |
11080 | case IORING_REGISTER_PROBE: |
11081 | ret = -EINVAL; | |
11082 | if (!arg || nr_args > 256) | |
11083 | break; | |
11084 | ret = io_probe(ctx, arg, nr_args); | |
11085 | break; | |
071698e1 JA |
11086 | case IORING_REGISTER_PERSONALITY: |
11087 | ret = -EINVAL; | |
11088 | if (arg || nr_args) | |
11089 | break; | |
11090 | ret = io_register_personality(ctx); | |
11091 | break; | |
11092 | case IORING_UNREGISTER_PERSONALITY: | |
11093 | ret = -EINVAL; | |
11094 | if (arg) | |
11095 | break; | |
11096 | ret = io_unregister_personality(ctx, nr_args); | |
11097 | break; | |
7e84e1c7 SG |
11098 | case IORING_REGISTER_ENABLE_RINGS: |
11099 | ret = -EINVAL; | |
11100 | if (arg || nr_args) | |
11101 | break; | |
11102 | ret = io_register_enable_rings(ctx); | |
11103 | break; | |
21b55dbc SG |
11104 | case IORING_REGISTER_RESTRICTIONS: |
11105 | ret = io_register_restrictions(ctx, arg, nr_args); | |
11106 | break; | |
992da01a PB |
11107 | case IORING_REGISTER_FILES2: |
11108 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); | |
11109 | break; | |
11110 | case IORING_REGISTER_FILES_UPDATE2: | |
11111 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
11112 | IORING_RSRC_FILE); | |
11113 | break; | |
11114 | case IORING_REGISTER_BUFFERS2: | |
11115 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); | |
792e3582 | 11116 | break; |
992da01a PB |
11117 | case IORING_REGISTER_BUFFERS_UPDATE: |
11118 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
11119 | IORING_RSRC_BUFFER); | |
c3bdad02 | 11120 | break; |
fe76421d JA |
11121 | case IORING_REGISTER_IOWQ_AFF: |
11122 | ret = -EINVAL; | |
11123 | if (!arg || !nr_args) | |
11124 | break; | |
11125 | ret = io_register_iowq_aff(ctx, arg, nr_args); | |
11126 | break; | |
11127 | case IORING_UNREGISTER_IOWQ_AFF: | |
11128 | ret = -EINVAL; | |
11129 | if (arg || nr_args) | |
11130 | break; | |
11131 | ret = io_unregister_iowq_aff(ctx); | |
11132 | break; | |
2e480058 JA |
11133 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
11134 | ret = -EINVAL; | |
11135 | if (!arg || nr_args != 2) | |
11136 | break; | |
11137 | ret = io_register_iowq_max_workers(ctx, arg); | |
11138 | break; | |
edafccee JA |
11139 | default: |
11140 | ret = -EINVAL; | |
11141 | break; | |
11142 | } | |
11143 | ||
071698e1 | 11144 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 11145 | /* bring the ctx back to life */ |
05f3fb3c | 11146 | percpu_ref_reinit(&ctx->refs); |
0f158b4c | 11147 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 11148 | } |
edafccee JA |
11149 | return ret; |
11150 | } | |
11151 | ||
11152 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
11153 | void __user *, arg, unsigned int, nr_args) | |
11154 | { | |
11155 | struct io_ring_ctx *ctx; | |
11156 | long ret = -EBADF; | |
11157 | struct fd f; | |
11158 | ||
11159 | f = fdget(fd); | |
11160 | if (!f.file) | |
11161 | return -EBADF; | |
11162 | ||
11163 | ret = -EOPNOTSUPP; | |
11164 | if (f.file->f_op != &io_uring_fops) | |
11165 | goto out_fput; | |
11166 | ||
11167 | ctx = f.file->private_data; | |
11168 | ||
b6c23dd5 PB |
11169 | io_run_task_work(); |
11170 | ||
edafccee JA |
11171 | mutex_lock(&ctx->uring_lock); |
11172 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
11173 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
11174 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
11175 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
11176 | out_fput: |
11177 | fdput(f); | |
11178 | return ret; | |
11179 | } | |
11180 | ||
2b188cc1 JA |
11181 | static int __init io_uring_init(void) |
11182 | { | |
d7f62e82 SM |
11183 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
11184 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
11185 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
11186 | } while (0) | |
11187 | ||
11188 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
11189 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
11190 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
11191 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
11192 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
11193 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
11194 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
11195 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
11196 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
11197 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 11198 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
11199 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
11200 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
11201 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
11202 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
11203 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
11204 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
11205 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
11206 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
11207 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
11208 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
11209 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
11210 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
11211 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
11212 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
11213 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 11214 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
11215 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
11216 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
16340eab | 11217 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
d7f62e82 | 11218 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
7d67af2c | 11219 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
b9445598 | 11220 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
d7f62e82 | 11221 | |
b0d658ec PB |
11222 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
11223 | sizeof(struct io_uring_rsrc_update)); | |
11224 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > | |
11225 | sizeof(struct io_uring_rsrc_update2)); | |
90499ad0 PB |
11226 | |
11227 | /* ->buf_index is u16 */ | |
11228 | BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); | |
11229 | ||
b0d658ec PB |
11230 | /* should fit into one byte */ |
11231 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); | |
68fe256a PB |
11232 | BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8)); |
11233 | BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS); | |
b0d658ec | 11234 | |
d3656344 | 11235 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
32c2d33e | 11236 | BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int)); |
16340eab | 11237 | |
91f245d5 JA |
11238 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
11239 | SLAB_ACCOUNT); | |
2b188cc1 JA |
11240 | return 0; |
11241 | }; | |
11242 | __initcall(io_uring_init); |