Commit | Line | Data |
---|---|---|
2b188cc1 JA |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Shared application/kernel submission and completion ring pairs, for | |
4 | * supporting fast/efficient IO. | |
5 | * | |
6 | * A note on the read/write ordering memory barriers that are matched between | |
1e84b97b SB |
7 | * the application and kernel side. |
8 | * | |
9 | * After the application reads the CQ ring tail, it must use an | |
10 | * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses | |
11 | * before writing the tail (using smp_load_acquire to read the tail will | |
12 | * do). It also needs a smp_mb() before updating CQ head (ordering the | |
13 | * entry load(s) with the head store), pairing with an implicit barrier | |
d068b506 | 14 | * through a control-dependency in io_get_cqe (smp_store_release to |
1e84b97b SB |
15 | * store head will do). Failure to do so could lead to reading invalid |
16 | * CQ entries. | |
17 | * | |
18 | * Likewise, the application must use an appropriate smp_wmb() before | |
19 | * writing the SQ tail (ordering SQ entry stores with the tail store), | |
20 | * which pairs with smp_load_acquire in io_get_sqring (smp_store_release | |
21 | * to store the tail will do). And it needs a barrier ordering the SQ | |
22 | * head load before writing new SQ entries (smp_load_acquire to read | |
23 | * head will do). | |
24 | * | |
25 | * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application | |
26 | * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after* | |
27 | * updating the SQ tail; a full memory barrier smp_mb() is needed | |
28 | * between. | |
2b188cc1 JA |
29 | * |
30 | * Also see the examples in the liburing library: | |
31 | * | |
32 | * git://git.kernel.dk/liburing | |
33 | * | |
34 | * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens | |
35 | * from data shared between the kernel and application. This is done both | |
36 | * for ordering purposes, but also to ensure that once a value is loaded from | |
37 | * data that the application could potentially modify, it remains stable. | |
38 | * | |
39 | * Copyright (C) 2018-2019 Jens Axboe | |
c992fe29 | 40 | * Copyright (c) 2018-2019 Christoph Hellwig |
2b188cc1 JA |
41 | */ |
42 | #include <linux/kernel.h> | |
43 | #include <linux/init.h> | |
44 | #include <linux/errno.h> | |
45 | #include <linux/syscalls.h> | |
46 | #include <linux/compat.h> | |
52de1fe1 | 47 | #include <net/compat.h> |
2b188cc1 JA |
48 | #include <linux/refcount.h> |
49 | #include <linux/uio.h> | |
6b47ee6e | 50 | #include <linux/bits.h> |
2b188cc1 JA |
51 | |
52 | #include <linux/sched/signal.h> | |
53 | #include <linux/fs.h> | |
54 | #include <linux/file.h> | |
55 | #include <linux/fdtable.h> | |
56 | #include <linux/mm.h> | |
57 | #include <linux/mman.h> | |
2b188cc1 JA |
58 | #include <linux/percpu.h> |
59 | #include <linux/slab.h> | |
2b188cc1 | 60 | #include <linux/blkdev.h> |
edafccee | 61 | #include <linux/bvec.h> |
2b188cc1 JA |
62 | #include <linux/net.h> |
63 | #include <net/sock.h> | |
64 | #include <net/af_unix.h> | |
6b06314c | 65 | #include <net/scm.h> |
2b188cc1 JA |
66 | #include <linux/anon_inodes.h> |
67 | #include <linux/sched/mm.h> | |
68 | #include <linux/uaccess.h> | |
69 | #include <linux/nospec.h> | |
edafccee JA |
70 | #include <linux/sizes.h> |
71 | #include <linux/hugetlb.h> | |
aa4c3967 | 72 | #include <linux/highmem.h> |
15b71abe JA |
73 | #include <linux/namei.h> |
74 | #include <linux/fsnotify.h> | |
4840e418 | 75 | #include <linux/fadvise.h> |
3e4827b0 | 76 | #include <linux/eventpoll.h> |
7d67af2c | 77 | #include <linux/splice.h> |
b41e9852 | 78 | #include <linux/task_work.h> |
bcf5a063 | 79 | #include <linux/pagemap.h> |
0f212204 | 80 | #include <linux/io_uring.h> |
ef98eb04 | 81 | #include <linux/tracehook.h> |
2b188cc1 | 82 | |
c826bd7a DD |
83 | #define CREATE_TRACE_POINTS |
84 | #include <trace/events/io_uring.h> | |
85 | ||
2b188cc1 JA |
86 | #include <uapi/linux/io_uring.h> |
87 | ||
88 | #include "internal.h" | |
561fb04a | 89 | #include "io-wq.h" |
2b188cc1 | 90 | |
5277deaa | 91 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 92 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
4ce8ad95 | 93 | #define IORING_SQPOLL_CAP_ENTRIES_VALUE 8 |
65e19f54 | 94 | |
187f08c1 | 95 | /* only define max */ |
042b0d85 | 96 | #define IORING_MAX_FIXED_FILES (1U << 15) |
21b55dbc SG |
97 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
98 | IORING_REGISTER_LAST + IORING_OP_LAST) | |
2b188cc1 | 99 | |
187f08c1 | 100 | #define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3) |
2d091d62 PB |
101 | #define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT) |
102 | #define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1) | |
103 | ||
489809e2 PB |
104 | #define IORING_MAX_REG_BUFFERS (1U << 14) |
105 | ||
b16fed66 PB |
106 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
107 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ | |
108 | IOSQE_BUFFER_SELECT) | |
c854357b PB |
109 | #define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \ |
110 | REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS) | |
b16fed66 | 111 | |
09899b19 PB |
112 | #define IO_TCTX_REFS_CACHE_NR (1U << 10) |
113 | ||
2b188cc1 JA |
114 | struct io_uring { |
115 | u32 head ____cacheline_aligned_in_smp; | |
116 | u32 tail ____cacheline_aligned_in_smp; | |
117 | }; | |
118 | ||
1e84b97b | 119 | /* |
75b28aff HV |
120 | * This data is shared with the application through the mmap at offsets |
121 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
122 | * |
123 | * The offsets to the member fields are published through struct | |
124 | * io_sqring_offsets when calling io_uring_setup. | |
125 | */ | |
75b28aff | 126 | struct io_rings { |
1e84b97b SB |
127 | /* |
128 | * Head and tail offsets into the ring; the offsets need to be | |
129 | * masked to get valid indices. | |
130 | * | |
75b28aff HV |
131 | * The kernel controls head of the sq ring and the tail of the cq ring, |
132 | * and the application controls tail of the sq ring and the head of the | |
133 | * cq ring. | |
1e84b97b | 134 | */ |
75b28aff | 135 | struct io_uring sq, cq; |
1e84b97b | 136 | /* |
75b28aff | 137 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
138 | * ring_entries - 1) |
139 | */ | |
75b28aff HV |
140 | u32 sq_ring_mask, cq_ring_mask; |
141 | /* Ring sizes (constant, power of 2) */ | |
142 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
143 | /* |
144 | * Number of invalid entries dropped by the kernel due to | |
145 | * invalid index stored in array | |
146 | * | |
147 | * Written by the kernel, shouldn't be modified by the | |
148 | * application (i.e. get number of "new events" by comparing to | |
149 | * cached value). | |
150 | * | |
151 | * After a new SQ head value was read by the application this | |
152 | * counter includes all submissions that were dropped reaching | |
153 | * the new SQ head (and possibly more). | |
154 | */ | |
75b28aff | 155 | u32 sq_dropped; |
1e84b97b | 156 | /* |
0d9b5b3a | 157 | * Runtime SQ flags |
1e84b97b SB |
158 | * |
159 | * Written by the kernel, shouldn't be modified by the | |
160 | * application. | |
161 | * | |
162 | * The application needs a full memory barrier before checking | |
163 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
164 | */ | |
75b28aff | 165 | u32 sq_flags; |
0d9b5b3a SG |
166 | /* |
167 | * Runtime CQ flags | |
168 | * | |
169 | * Written by the application, shouldn't be modified by the | |
170 | * kernel. | |
171 | */ | |
fe7e3257 | 172 | u32 cq_flags; |
1e84b97b SB |
173 | /* |
174 | * Number of completion events lost because the queue was full; | |
175 | * this should be avoided by the application by making sure | |
0b4295b5 | 176 | * there are not more requests pending than there is space in |
1e84b97b SB |
177 | * the completion queue. |
178 | * | |
179 | * Written by the kernel, shouldn't be modified by the | |
180 | * application (i.e. get number of "new events" by comparing to | |
181 | * cached value). | |
182 | * | |
183 | * As completion events come in out of order this counter is not | |
184 | * ordered with any other data. | |
185 | */ | |
75b28aff | 186 | u32 cq_overflow; |
1e84b97b SB |
187 | /* |
188 | * Ring buffer of completion events. | |
189 | * | |
190 | * The kernel writes completion events fresh every time they are | |
191 | * produced, so the application is allowed to modify pending | |
192 | * entries. | |
193 | */ | |
75b28aff | 194 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
195 | }; |
196 | ||
45d189c6 PB |
197 | enum io_uring_cmd_flags { |
198 | IO_URING_F_NONBLOCK = 1, | |
889fca73 | 199 | IO_URING_F_COMPLETE_DEFER = 2, |
45d189c6 PB |
200 | }; |
201 | ||
edafccee JA |
202 | struct io_mapped_ubuf { |
203 | u64 ubuf; | |
4751f53d | 204 | u64 ubuf_end; |
edafccee | 205 | unsigned int nr_bvecs; |
de293938 | 206 | unsigned long acct_pages; |
41edf1a5 | 207 | struct bio_vec bvec[]; |
edafccee JA |
208 | }; |
209 | ||
50238531 BM |
210 | struct io_ring_ctx; |
211 | ||
6c2450ae PB |
212 | struct io_overflow_cqe { |
213 | struct io_uring_cqe cqe; | |
214 | struct list_head list; | |
215 | }; | |
216 | ||
a04b0ac0 PB |
217 | struct io_fixed_file { |
218 | /* file * with additional FFS_* flags */ | |
219 | unsigned long file_ptr; | |
220 | }; | |
221 | ||
269bbe5f BM |
222 | struct io_rsrc_put { |
223 | struct list_head list; | |
b60c8dce | 224 | u64 tag; |
50238531 BM |
225 | union { |
226 | void *rsrc; | |
227 | struct file *file; | |
bd54b6fe | 228 | struct io_mapped_ubuf *buf; |
50238531 | 229 | }; |
269bbe5f BM |
230 | }; |
231 | ||
aeca241b | 232 | struct io_file_table { |
042b0d85 | 233 | struct io_fixed_file *files; |
31b51510 JA |
234 | }; |
235 | ||
b895c9a6 | 236 | struct io_rsrc_node { |
05589553 XW |
237 | struct percpu_ref refs; |
238 | struct list_head node; | |
269bbe5f | 239 | struct list_head rsrc_list; |
b895c9a6 | 240 | struct io_rsrc_data *rsrc_data; |
4a38aed2 | 241 | struct llist_node llist; |
e297822b | 242 | bool done; |
05589553 XW |
243 | }; |
244 | ||
40ae0ff7 PB |
245 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
246 | ||
b895c9a6 | 247 | struct io_rsrc_data { |
05f3fb3c JA |
248 | struct io_ring_ctx *ctx; |
249 | ||
2d091d62 PB |
250 | u64 **tags; |
251 | unsigned int nr; | |
40ae0ff7 | 252 | rsrc_put_fn *do_put; |
3e942498 | 253 | atomic_t refs; |
05f3fb3c | 254 | struct completion done; |
8bad28d8 | 255 | bool quiesce; |
05f3fb3c JA |
256 | }; |
257 | ||
5a2e745d JA |
258 | struct io_buffer { |
259 | struct list_head list; | |
260 | __u64 addr; | |
d1f82808 | 261 | __u32 len; |
5a2e745d JA |
262 | __u16 bid; |
263 | }; | |
264 | ||
21b55dbc SG |
265 | struct io_restriction { |
266 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
267 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
268 | u8 sqe_flags_allowed; | |
269 | u8 sqe_flags_required; | |
7e84e1c7 | 270 | bool registered; |
21b55dbc SG |
271 | }; |
272 | ||
37d1e2e3 JA |
273 | enum { |
274 | IO_SQ_THREAD_SHOULD_STOP = 0, | |
275 | IO_SQ_THREAD_SHOULD_PARK, | |
276 | }; | |
277 | ||
534ca6d6 JA |
278 | struct io_sq_data { |
279 | refcount_t refs; | |
9e138a48 | 280 | atomic_t park_pending; |
09a6f4ef | 281 | struct mutex lock; |
69fb2131 JA |
282 | |
283 | /* ctx's that are using this sqd */ | |
284 | struct list_head ctx_list; | |
69fb2131 | 285 | |
534ca6d6 JA |
286 | struct task_struct *thread; |
287 | struct wait_queue_head wait; | |
08369246 XW |
288 | |
289 | unsigned sq_thread_idle; | |
37d1e2e3 JA |
290 | int sq_cpu; |
291 | pid_t task_pid; | |
5c2469e0 | 292 | pid_t task_tgid; |
37d1e2e3 JA |
293 | |
294 | unsigned long state; | |
37d1e2e3 | 295 | struct completion exited; |
534ca6d6 JA |
296 | }; |
297 | ||
6dd0be1e | 298 | #define IO_COMPL_BATCH 32 |
6ff119a6 | 299 | #define IO_REQ_CACHE_SIZE 32 |
bf019da7 | 300 | #define IO_REQ_ALLOC_BATCH 8 |
258b29a9 | 301 | |
a1ab7b35 PB |
302 | struct io_submit_link { |
303 | struct io_kiocb *head; | |
304 | struct io_kiocb *last; | |
305 | }; | |
306 | ||
258b29a9 PB |
307 | struct io_submit_state { |
308 | struct blk_plug plug; | |
a1ab7b35 | 309 | struct io_submit_link link; |
258b29a9 PB |
310 | |
311 | /* | |
312 | * io_kiocb alloc cache | |
313 | */ | |
bf019da7 | 314 | void *reqs[IO_REQ_CACHE_SIZE]; |
258b29a9 PB |
315 | unsigned int free_reqs; |
316 | ||
317 | bool plug_started; | |
318 | ||
319 | /* | |
320 | * Batch completion logic | |
321 | */ | |
cd0ca2e0 PB |
322 | struct io_kiocb *compl_reqs[IO_COMPL_BATCH]; |
323 | unsigned int compl_nr; | |
324 | /* inline/task_work completion list, under ->uring_lock */ | |
325 | struct list_head free_list; | |
258b29a9 | 326 | |
258b29a9 PB |
327 | unsigned int ios_left; |
328 | }; | |
329 | ||
2b188cc1 | 330 | struct io_ring_ctx { |
b52ecf8c | 331 | /* const or read-mostly hot data */ |
2b188cc1 JA |
332 | struct { |
333 | struct percpu_ref refs; | |
2b188cc1 | 334 | |
b52ecf8c | 335 | struct io_rings *rings; |
2b188cc1 | 336 | unsigned int flags; |
e1d85334 | 337 | unsigned int compat: 1; |
e1d85334 RD |
338 | unsigned int drain_next: 1; |
339 | unsigned int eventfd_async: 1; | |
21b55dbc | 340 | unsigned int restricted: 1; |
f18ee4cf | 341 | unsigned int off_timeout_used: 1; |
10c66904 | 342 | unsigned int drain_active: 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; | |
371 | struct io_file_table file_table; | |
372 | unsigned nr_user_files; | |
373 | unsigned nr_user_bufs; | |
374 | struct io_mapped_ubuf **user_bufs; | |
375 | ||
376 | struct io_submit_state submit_state; | |
5262f567 | 377 | struct list_head timeout_list; |
ef9dd637 | 378 | struct list_head ltimeout_list; |
1d7bb1d5 | 379 | struct list_head cq_overflow_list; |
7f1129d2 PB |
380 | struct xarray io_buffers; |
381 | struct xarray personalities; | |
382 | u32 pers_next; | |
383 | unsigned sq_thread_idle; | |
2b188cc1 JA |
384 | } ____cacheline_aligned_in_smp; |
385 | ||
d0acdee2 PB |
386 | /* IRQ completion list, under ->completion_lock */ |
387 | struct list_head locked_free_list; | |
388 | unsigned int locked_free_nr; | |
3c1a2ead | 389 | |
7c30f36a | 390 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
534ca6d6 JA |
391 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
392 | ||
90554200 | 393 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 394 | struct list_head sqd_list; |
75b28aff | 395 | |
5ed7a37d PB |
396 | unsigned long check_cq_overflow; |
397 | ||
206aefde JA |
398 | struct { |
399 | unsigned cached_cq_tail; | |
400 | unsigned cq_entries; | |
0499e582 | 401 | struct eventfd_ctx *cq_ev_fd; |
311997b3 | 402 | struct wait_queue_head poll_wait; |
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 | */ | |
540e32a0 | 420 | struct list_head 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; | |
459 | }; | |
2b188cc1 JA |
460 | }; |
461 | ||
53e043b2 SM |
462 | struct io_uring_task { |
463 | /* submission side */ | |
09899b19 | 464 | int cached_refs; |
53e043b2 SM |
465 | struct xarray xa; |
466 | struct wait_queue_head wait; | |
ee53fb2b SM |
467 | const struct io_ring_ctx *last; |
468 | struct io_wq *io_wq; | |
53e043b2 | 469 | struct percpu_counter inflight; |
b303fe2e | 470 | atomic_t inflight_tracked; |
53e043b2 | 471 | atomic_t in_idle; |
53e043b2 SM |
472 | |
473 | spinlock_t task_lock; | |
474 | struct io_wq_work_list task_list; | |
53e043b2 | 475 | struct callback_head task_work; |
6294f368 | 476 | bool task_running; |
53e043b2 SM |
477 | }; |
478 | ||
09bb8394 JA |
479 | /* |
480 | * First field must be the file pointer in all the | |
481 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
482 | */ | |
221c5eb2 JA |
483 | struct io_poll_iocb { |
484 | struct file *file; | |
018043be | 485 | struct wait_queue_head *head; |
221c5eb2 | 486 | __poll_t events; |
8c838788 | 487 | bool done; |
221c5eb2 | 488 | bool canceled; |
392edb45 | 489 | struct wait_queue_entry wait; |
221c5eb2 JA |
490 | }; |
491 | ||
9d805892 | 492 | struct io_poll_update { |
018043be | 493 | struct file *file; |
9d805892 PB |
494 | u64 old_user_data; |
495 | u64 new_user_data; | |
496 | __poll_t events; | |
b69de288 JA |
497 | bool update_events; |
498 | bool update_user_data; | |
018043be PB |
499 | }; |
500 | ||
b5dba59e JA |
501 | struct io_close { |
502 | struct file *file; | |
b5dba59e | 503 | int fd; |
7df778be | 504 | u32 file_slot; |
b5dba59e JA |
505 | }; |
506 | ||
ad8a48ac JA |
507 | struct io_timeout_data { |
508 | struct io_kiocb *req; | |
509 | struct hrtimer timer; | |
510 | struct timespec64 ts; | |
511 | enum hrtimer_mode mode; | |
50c1df2b | 512 | u32 flags; |
ad8a48ac JA |
513 | }; |
514 | ||
8ed8d3c3 JA |
515 | struct io_accept { |
516 | struct file *file; | |
517 | struct sockaddr __user *addr; | |
518 | int __user *addr_len; | |
519 | int flags; | |
aaa4db12 | 520 | u32 file_slot; |
09952e3e | 521 | unsigned long nofile; |
8ed8d3c3 JA |
522 | }; |
523 | ||
524 | struct io_sync { | |
525 | struct file *file; | |
526 | loff_t len; | |
527 | loff_t off; | |
528 | int flags; | |
d63d1b5e | 529 | int mode; |
8ed8d3c3 JA |
530 | }; |
531 | ||
fbf23849 JA |
532 | struct io_cancel { |
533 | struct file *file; | |
534 | u64 addr; | |
535 | }; | |
536 | ||
b29472ee JA |
537 | struct io_timeout { |
538 | struct file *file; | |
bfe68a22 PB |
539 | u32 off; |
540 | u32 target_seq; | |
135fcde8 | 541 | struct list_head list; |
90cd7e42 PB |
542 | /* head of the link, used by linked timeouts only */ |
543 | struct io_kiocb *head; | |
89b263f6 JA |
544 | /* for linked completions */ |
545 | struct io_kiocb *prev; | |
b29472ee JA |
546 | }; |
547 | ||
0bdf7a2d PB |
548 | struct io_timeout_rem { |
549 | struct file *file; | |
550 | u64 addr; | |
9c8e11b3 PB |
551 | |
552 | /* timeout update */ | |
553 | struct timespec64 ts; | |
554 | u32 flags; | |
f1042b6c | 555 | bool ltimeout; |
0bdf7a2d PB |
556 | }; |
557 | ||
9adbd45d JA |
558 | struct io_rw { |
559 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
560 | struct kiocb kiocb; | |
561 | u64 addr; | |
562 | u64 len; | |
563 | }; | |
564 | ||
3fbb51c1 JA |
565 | struct io_connect { |
566 | struct file *file; | |
567 | struct sockaddr __user *addr; | |
568 | int addr_len; | |
569 | }; | |
570 | ||
e47293fd JA |
571 | struct io_sr_msg { |
572 | struct file *file; | |
fddaface | 573 | union { |
4af3417a PB |
574 | struct compat_msghdr __user *umsg_compat; |
575 | struct user_msghdr __user *umsg; | |
576 | void __user *buf; | |
fddaface | 577 | }; |
e47293fd | 578 | int msg_flags; |
bcda7baa | 579 | int bgid; |
fddaface | 580 | size_t len; |
bcda7baa | 581 | struct io_buffer *kbuf; |
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 | ||
3ca405eb PB |
693 | struct io_completion { |
694 | struct file *file; | |
8c3f9cd1 | 695 | u32 cflags; |
3ca405eb PB |
696 | }; |
697 | ||
f499a021 JA |
698 | struct io_async_connect { |
699 | struct sockaddr_storage address; | |
700 | }; | |
701 | ||
03b1230c JA |
702 | struct io_async_msghdr { |
703 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
704 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
705 | struct iovec *free_iov; | |
03b1230c JA |
706 | struct sockaddr __user *uaddr; |
707 | struct msghdr msg; | |
b537916c | 708 | struct sockaddr_storage addr; |
03b1230c JA |
709 | }; |
710 | ||
f67676d1 JA |
711 | struct io_async_rw { |
712 | struct iovec fast_iov[UIO_FASTIOV]; | |
ff6165b2 JA |
713 | const struct iovec *free_iovec; |
714 | struct iov_iter iter; | |
cd658695 | 715 | struct iov_iter_state iter_state; |
227c0c96 | 716 | size_t bytes_done; |
bcf5a063 | 717 | struct wait_page_queue wpq; |
f67676d1 JA |
718 | }; |
719 | ||
6b47ee6e PB |
720 | enum { |
721 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
722 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
723 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
724 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
725 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 726 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
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, |
7b29f92d | 742 | /* keep async read/write and isreg together and in order */ |
b191e2df PB |
743 | REQ_F_NOWAIT_READ_BIT, |
744 | REQ_F_NOWAIT_WRITE_BIT, | |
7b29f92d | 745 | REQ_F_ISREG_BIT, |
84557871 JA |
746 | |
747 | /* not a real bit, just to check we're not overflowing the space */ | |
748 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
749 | }; |
750 | ||
751 | enum { | |
752 | /* ctx owns file */ | |
753 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
754 | /* drain existing IO first */ | |
755 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
756 | /* linked sqes */ | |
757 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
758 | /* doesn't sever on completion < 0 */ | |
759 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
760 | /* IOSQE_ASYNC */ | |
761 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
762 | /* IOSQE_BUFFER_SELECT */ |
763 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 764 | |
6b47ee6e | 765 | /* fail rest of links */ |
93d2bcd2 | 766 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
b05a1bcd | 767 | /* on inflight list, should be cancelled and waited on exit reliably */ |
6b47ee6e PB |
768 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
769 | /* read/write uses file position */ | |
770 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
771 | /* must not punt to workers */ | |
772 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 773 | /* has or had linked timeout */ |
6b47ee6e | 774 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
99bc4c38 PB |
775 | /* needs cleanup */ |
776 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
777 | /* already went through poll handler */ |
778 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
779 | /* buffer already selected */ |
780 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
e342c807 PB |
781 | /* completion is deferred through io_comp_state */ |
782 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
230d50d4 JA |
783 | /* caller should reissue async */ |
784 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), | |
7b29f92d | 785 | /* supports async reads */ |
b191e2df | 786 | REQ_F_NOWAIT_READ = BIT(REQ_F_NOWAIT_READ_BIT), |
7b29f92d | 787 | /* supports async writes */ |
b191e2df | 788 | REQ_F_NOWAIT_WRITE = BIT(REQ_F_NOWAIT_WRITE_BIT), |
7b29f92d JA |
789 | /* regular file */ |
790 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
b8e64b53 PB |
791 | /* has creds assigned */ |
792 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), | |
20e60a38 PB |
793 | /* skip refcounting if not set */ |
794 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), | |
4d13d1a4 PB |
795 | /* there is a linked timeout that has to be armed */ |
796 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT), | |
d7718a9d JA |
797 | }; |
798 | ||
799 | struct async_poll { | |
800 | struct io_poll_iocb poll; | |
807abcb0 | 801 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
802 | }; |
803 | ||
f237c30a | 804 | typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked); |
5b0a6acc | 805 | |
7cbf1722 | 806 | struct io_task_work { |
5b0a6acc PB |
807 | union { |
808 | struct io_wq_work_node node; | |
809 | struct llist_node fallback_node; | |
810 | }; | |
811 | io_req_tw_func_t func; | |
7cbf1722 JA |
812 | }; |
813 | ||
992da01a PB |
814 | enum { |
815 | IORING_RSRC_FILE = 0, | |
816 | IORING_RSRC_BUFFER = 1, | |
817 | }; | |
818 | ||
09bb8394 JA |
819 | /* |
820 | * NOTE! Each of the iocb union members has the file pointer | |
821 | * as the first entry in their struct definition. So you can | |
822 | * access the file pointer through any of the sub-structs, | |
823 | * or directly as just 'ki_filp' in this struct. | |
824 | */ | |
2b188cc1 | 825 | struct io_kiocb { |
221c5eb2 | 826 | union { |
09bb8394 | 827 | struct file *file; |
9adbd45d | 828 | struct io_rw rw; |
221c5eb2 | 829 | struct io_poll_iocb poll; |
9d805892 | 830 | struct io_poll_update poll_update; |
8ed8d3c3 JA |
831 | struct io_accept accept; |
832 | struct io_sync sync; | |
fbf23849 | 833 | struct io_cancel cancel; |
b29472ee | 834 | struct io_timeout timeout; |
0bdf7a2d | 835 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 836 | struct io_connect connect; |
e47293fd | 837 | struct io_sr_msg sr_msg; |
15b71abe | 838 | struct io_open open; |
b5dba59e | 839 | struct io_close close; |
269bbe5f | 840 | struct io_rsrc_update rsrc_update; |
4840e418 | 841 | struct io_fadvise fadvise; |
c1ca757b | 842 | struct io_madvise madvise; |
3e4827b0 | 843 | struct io_epoll epoll; |
7d67af2c | 844 | struct io_splice splice; |
ddf0322d | 845 | struct io_provide_buf pbuf; |
1d9e1288 | 846 | struct io_statx statx; |
36f4fa68 | 847 | struct io_shutdown shutdown; |
80a261fd | 848 | struct io_rename rename; |
14a1143b | 849 | struct io_unlink unlink; |
e34a02dc | 850 | struct io_mkdir mkdir; |
7a8721f8 | 851 | struct io_symlink symlink; |
cf30da90 | 852 | struct io_hardlink hardlink; |
3ca405eb PB |
853 | /* use only after cleaning per-op data, see io_clean_op() */ |
854 | struct io_completion compl; | |
221c5eb2 | 855 | }; |
2b188cc1 | 856 | |
e8c2bc1f JA |
857 | /* opcode allocated if it needs to store data for async defer */ |
858 | void *async_data; | |
d625c6ee | 859 | u8 opcode; |
65a6543d XW |
860 | /* polled IO has completed */ |
861 | u8 iopoll_completed; | |
2b188cc1 | 862 | |
4f4eeba8 | 863 | u16 buf_index; |
9cf7c104 | 864 | u32 result; |
4f4eeba8 | 865 | |
010e8e6b PB |
866 | struct io_ring_ctx *ctx; |
867 | unsigned int flags; | |
abc54d63 | 868 | atomic_t refs; |
010e8e6b PB |
869 | struct task_struct *task; |
870 | u64 user_data; | |
d7718a9d | 871 | |
f2f87370 | 872 | struct io_kiocb *link; |
269bbe5f | 873 | struct percpu_ref *fixed_rsrc_refs; |
fcb323cc | 874 | |
b303fe2e | 875 | /* used with ctx->iopoll_list with reads/writes */ |
010e8e6b | 876 | struct list_head inflight_entry; |
5b0a6acc | 877 | struct io_task_work io_task_work; |
010e8e6b PB |
878 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
879 | struct hlist_node hash_node; | |
880 | struct async_poll *apoll; | |
881 | struct io_wq_work work; | |
fe7e3257 | 882 | const struct cred *creds; |
c10d1f98 | 883 | |
eae071c9 PB |
884 | /* store used ubuf, so we can prevent reloading */ |
885 | struct io_mapped_ubuf *imu; | |
2b188cc1 | 886 | }; |
05589553 | 887 | |
13bf43f5 PB |
888 | struct io_tctx_node { |
889 | struct list_head ctx_node; | |
890 | struct task_struct *task; | |
13bf43f5 PB |
891 | struct io_ring_ctx *ctx; |
892 | }; | |
893 | ||
27dc8338 PB |
894 | struct io_defer_entry { |
895 | struct list_head list; | |
896 | struct io_kiocb *req; | |
9cf7c104 | 897 | u32 seq; |
2b188cc1 JA |
898 | }; |
899 | ||
d3656344 | 900 | struct io_op_def { |
d3656344 JA |
901 | /* needs req->file assigned */ |
902 | unsigned needs_file : 1; | |
d3656344 JA |
903 | /* hash wq insertion if file is a regular file */ |
904 | unsigned hash_reg_file : 1; | |
905 | /* unbound wq insertion if file is a non-regular file */ | |
906 | unsigned unbound_nonreg_file : 1; | |
66f4af93 JA |
907 | /* opcode is not supported by this kernel */ |
908 | unsigned not_supported : 1; | |
8a72758c JA |
909 | /* set if opcode supports polled "wait" */ |
910 | unsigned pollin : 1; | |
911 | unsigned pollout : 1; | |
bcda7baa JA |
912 | /* op supports buffer selection */ |
913 | unsigned buffer_select : 1; | |
26f0505a PB |
914 | /* do prep async if is going to be punted */ |
915 | unsigned needs_async_setup : 1; | |
27926b68 JA |
916 | /* should block plug */ |
917 | unsigned plug : 1; | |
e8c2bc1f JA |
918 | /* size of async data needed, if any */ |
919 | unsigned short async_size; | |
d3656344 JA |
920 | }; |
921 | ||
0918682b | 922 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
923 | [IORING_OP_NOP] = {}, |
924 | [IORING_OP_READV] = { | |
d3656344 JA |
925 | .needs_file = 1, |
926 | .unbound_nonreg_file = 1, | |
8a72758c | 927 | .pollin = 1, |
4d954c25 | 928 | .buffer_select = 1, |
26f0505a | 929 | .needs_async_setup = 1, |
27926b68 | 930 | .plug = 1, |
e8c2bc1f | 931 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 932 | }, |
0463b6c5 | 933 | [IORING_OP_WRITEV] = { |
d3656344 JA |
934 | .needs_file = 1, |
935 | .hash_reg_file = 1, | |
936 | .unbound_nonreg_file = 1, | |
8a72758c | 937 | .pollout = 1, |
26f0505a | 938 | .needs_async_setup = 1, |
27926b68 | 939 | .plug = 1, |
e8c2bc1f | 940 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 941 | }, |
0463b6c5 | 942 | [IORING_OP_FSYNC] = { |
d3656344 JA |
943 | .needs_file = 1, |
944 | }, | |
0463b6c5 | 945 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
946 | .needs_file = 1, |
947 | .unbound_nonreg_file = 1, | |
8a72758c | 948 | .pollin = 1, |
27926b68 | 949 | .plug = 1, |
e8c2bc1f | 950 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 951 | }, |
0463b6c5 | 952 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
953 | .needs_file = 1, |
954 | .hash_reg_file = 1, | |
955 | .unbound_nonreg_file = 1, | |
8a72758c | 956 | .pollout = 1, |
27926b68 | 957 | .plug = 1, |
e8c2bc1f | 958 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 959 | }, |
0463b6c5 | 960 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
961 | .needs_file = 1, |
962 | .unbound_nonreg_file = 1, | |
963 | }, | |
0463b6c5 PB |
964 | [IORING_OP_POLL_REMOVE] = {}, |
965 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 JA |
966 | .needs_file = 1, |
967 | }, | |
0463b6c5 | 968 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
969 | .needs_file = 1, |
970 | .unbound_nonreg_file = 1, | |
8a72758c | 971 | .pollout = 1, |
26f0505a | 972 | .needs_async_setup = 1, |
e8c2bc1f | 973 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 974 | }, |
0463b6c5 | 975 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
976 | .needs_file = 1, |
977 | .unbound_nonreg_file = 1, | |
8a72758c | 978 | .pollin = 1, |
52de1fe1 | 979 | .buffer_select = 1, |
26f0505a | 980 | .needs_async_setup = 1, |
e8c2bc1f | 981 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 982 | }, |
0463b6c5 | 983 | [IORING_OP_TIMEOUT] = { |
e8c2bc1f | 984 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 985 | }, |
9c8e11b3 PB |
986 | [IORING_OP_TIMEOUT_REMOVE] = { |
987 | /* used by timeout updates' prep() */ | |
9c8e11b3 | 988 | }, |
0463b6c5 | 989 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
990 | .needs_file = 1, |
991 | .unbound_nonreg_file = 1, | |
8a72758c | 992 | .pollin = 1, |
d3656344 | 993 | }, |
0463b6c5 PB |
994 | [IORING_OP_ASYNC_CANCEL] = {}, |
995 | [IORING_OP_LINK_TIMEOUT] = { | |
e8c2bc1f | 996 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 997 | }, |
0463b6c5 | 998 | [IORING_OP_CONNECT] = { |
d3656344 JA |
999 | .needs_file = 1, |
1000 | .unbound_nonreg_file = 1, | |
8a72758c | 1001 | .pollout = 1, |
26f0505a | 1002 | .needs_async_setup = 1, |
e8c2bc1f | 1003 | .async_size = sizeof(struct io_async_connect), |
d3656344 | 1004 | }, |
0463b6c5 | 1005 | [IORING_OP_FALLOCATE] = { |
d3656344 | 1006 | .needs_file = 1, |
d3656344 | 1007 | }, |
44526bed JA |
1008 | [IORING_OP_OPENAT] = {}, |
1009 | [IORING_OP_CLOSE] = {}, | |
1010 | [IORING_OP_FILES_UPDATE] = {}, | |
1011 | [IORING_OP_STATX] = {}, | |
0463b6c5 | 1012 | [IORING_OP_READ] = { |
3a6820f2 JA |
1013 | .needs_file = 1, |
1014 | .unbound_nonreg_file = 1, | |
8a72758c | 1015 | .pollin = 1, |
bcda7baa | 1016 | .buffer_select = 1, |
27926b68 | 1017 | .plug = 1, |
e8c2bc1f | 1018 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1019 | }, |
0463b6c5 | 1020 | [IORING_OP_WRITE] = { |
3a6820f2 | 1021 | .needs_file = 1, |
7b3188e7 | 1022 | .hash_reg_file = 1, |
3a6820f2 | 1023 | .unbound_nonreg_file = 1, |
8a72758c | 1024 | .pollout = 1, |
27926b68 | 1025 | .plug = 1, |
e8c2bc1f | 1026 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1027 | }, |
0463b6c5 | 1028 | [IORING_OP_FADVISE] = { |
4840e418 | 1029 | .needs_file = 1, |
c1ca757b | 1030 | }, |
44526bed | 1031 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 1032 | [IORING_OP_SEND] = { |
fddaface JA |
1033 | .needs_file = 1, |
1034 | .unbound_nonreg_file = 1, | |
8a72758c | 1035 | .pollout = 1, |
fddaface | 1036 | }, |
0463b6c5 | 1037 | [IORING_OP_RECV] = { |
fddaface JA |
1038 | .needs_file = 1, |
1039 | .unbound_nonreg_file = 1, | |
8a72758c | 1040 | .pollin = 1, |
bcda7baa | 1041 | .buffer_select = 1, |
fddaface | 1042 | }, |
0463b6c5 | 1043 | [IORING_OP_OPENAT2] = { |
cebdb986 | 1044 | }, |
3e4827b0 JA |
1045 | [IORING_OP_EPOLL_CTL] = { |
1046 | .unbound_nonreg_file = 1, | |
3e4827b0 | 1047 | }, |
7d67af2c PB |
1048 | [IORING_OP_SPLICE] = { |
1049 | .needs_file = 1, | |
1050 | .hash_reg_file = 1, | |
1051 | .unbound_nonreg_file = 1, | |
ddf0322d JA |
1052 | }, |
1053 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 1054 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
1055 | [IORING_OP_TEE] = { |
1056 | .needs_file = 1, | |
1057 | .hash_reg_file = 1, | |
1058 | .unbound_nonreg_file = 1, | |
1059 | }, | |
36f4fa68 JA |
1060 | [IORING_OP_SHUTDOWN] = { |
1061 | .needs_file = 1, | |
1062 | }, | |
44526bed JA |
1063 | [IORING_OP_RENAMEAT] = {}, |
1064 | [IORING_OP_UNLINKAT] = {}, | |
e34a02dc | 1065 | [IORING_OP_MKDIRAT] = {}, |
7a8721f8 | 1066 | [IORING_OP_SYMLINKAT] = {}, |
cf30da90 | 1067 | [IORING_OP_LINKAT] = {}, |
d3656344 JA |
1068 | }; |
1069 | ||
0756a869 PB |
1070 | /* requests with any of those set should undergo io_disarm_next() */ |
1071 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) | |
1072 | ||
7a612350 | 1073 | static bool io_disarm_next(struct io_kiocb *req); |
eef51daa | 1074 | static void io_uring_del_tctx_node(unsigned long index); |
9936c7c2 PB |
1075 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1076 | struct task_struct *task, | |
3dd0c97a | 1077 | bool cancel_all); |
78cc687b | 1078 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); |
1ffc5422 | 1079 | |
d4d19c19 PB |
1080 | static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
1081 | long res, unsigned int cflags); | |
ec9c02ad | 1082 | static void io_put_req(struct io_kiocb *req); |
91c2f697 | 1083 | static void io_put_req_deferred(struct io_kiocb *req); |
c7dae4ba | 1084 | static void io_dismantle_req(struct io_kiocb *req); |
94ae5e77 | 1085 | static void io_queue_linked_timeout(struct io_kiocb *req); |
fdecb662 | 1086 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 1087 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 | 1088 | unsigned nr_args); |
68fb8979 | 1089 | static void io_clean_op(struct io_kiocb *req); |
ac177053 | 1090 | static struct file *io_file_get(struct io_ring_ctx *ctx, |
8371adf5 | 1091 | struct io_kiocb *req, int fd, bool fixed); |
c5eef2b9 | 1092 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1093 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1094 | |
907d1df3 | 1095 | static void io_req_task_queue(struct io_kiocb *req); |
2a2758f2 | 1096 | static void io_submit_flush_completions(struct io_ring_ctx *ctx); |
179ae0d1 | 1097 | static int io_req_prep_async(struct io_kiocb *req); |
de0617e4 | 1098 | |
b9445598 PB |
1099 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
1100 | unsigned int issue_flags, u32 slot_index); | |
7df778be PB |
1101 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags); |
1102 | ||
f1042b6c | 1103 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer); |
b9445598 | 1104 | |
2b188cc1 JA |
1105 | static struct kmem_cache *req_cachep; |
1106 | ||
0918682b | 1107 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1108 | |
1109 | struct sock *io_uring_get_socket(struct file *file) | |
1110 | { | |
1111 | #if defined(CONFIG_UNIX) | |
1112 | if (file->f_op == &io_uring_fops) { | |
1113 | struct io_ring_ctx *ctx = file->private_data; | |
1114 | ||
1115 | return ctx->ring_sock->sk; | |
1116 | } | |
1117 | #endif | |
1118 | return NULL; | |
1119 | } | |
1120 | EXPORT_SYMBOL(io_uring_get_socket); | |
1121 | ||
f237c30a PB |
1122 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
1123 | { | |
1124 | if (!*locked) { | |
1125 | mutex_lock(&ctx->uring_lock); | |
1126 | *locked = true; | |
1127 | } | |
1128 | } | |
1129 | ||
f2f87370 PB |
1130 | #define io_for_each_link(pos, head) \ |
1131 | for (pos = (head); pos; pos = pos->link) | |
1132 | ||
21c843d5 PB |
1133 | /* |
1134 | * Shamelessly stolen from the mm implementation of page reference checking, | |
1135 | * see commit f958d7b528b1 for details. | |
1136 | */ | |
1137 | #define req_ref_zero_or_close_to_overflow(req) \ | |
1138 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) | |
1139 | ||
1140 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) | |
1141 | { | |
20e60a38 | 1142 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1143 | return atomic_inc_not_zero(&req->refs); |
1144 | } | |
1145 | ||
21c843d5 PB |
1146 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
1147 | { | |
20e60a38 PB |
1148 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
1149 | return true; | |
1150 | ||
21c843d5 PB |
1151 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1152 | return atomic_dec_and_test(&req->refs); | |
1153 | } | |
1154 | ||
1155 | static inline void req_ref_put(struct io_kiocb *req) | |
1156 | { | |
20e60a38 | 1157 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1158 | WARN_ON_ONCE(req_ref_put_and_test(req)); |
1159 | } | |
1160 | ||
1161 | static inline void req_ref_get(struct io_kiocb *req) | |
1162 | { | |
20e60a38 | 1163 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1164 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1165 | atomic_inc(&req->refs); | |
1166 | } | |
1167 | ||
48dcd38d | 1168 | static inline void __io_req_set_refcount(struct io_kiocb *req, int nr) |
20e60a38 PB |
1169 | { |
1170 | if (!(req->flags & REQ_F_REFCOUNT)) { | |
1171 | req->flags |= REQ_F_REFCOUNT; | |
48dcd38d | 1172 | atomic_set(&req->refs, nr); |
20e60a38 PB |
1173 | } |
1174 | } | |
1175 | ||
48dcd38d PB |
1176 | static inline void io_req_set_refcount(struct io_kiocb *req) |
1177 | { | |
1178 | __io_req_set_refcount(req, 1); | |
1179 | } | |
1180 | ||
b895c9a6 | 1181 | static inline void io_req_set_rsrc_node(struct io_kiocb *req) |
36f72fe2 PB |
1182 | { |
1183 | struct io_ring_ctx *ctx = req->ctx; | |
1184 | ||
269bbe5f | 1185 | if (!req->fixed_rsrc_refs) { |
a7f0ed5a | 1186 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
269bbe5f | 1187 | percpu_ref_get(req->fixed_rsrc_refs); |
36f72fe2 PB |
1188 | } |
1189 | } | |
1190 | ||
f70865db PB |
1191 | static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
1192 | { | |
1193 | bool got = percpu_ref_tryget(ref); | |
1194 | ||
1195 | /* already at zero, wait for ->release() */ | |
1196 | if (!got) | |
1197 | wait_for_completion(compl); | |
1198 | percpu_ref_resurrect(ref); | |
1199 | if (got) | |
1200 | percpu_ref_put(ref); | |
1201 | } | |
1202 | ||
3dd0c97a PB |
1203 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
1204 | bool cancel_all) | |
08d23634 PB |
1205 | { |
1206 | struct io_kiocb *req; | |
1207 | ||
68207680 | 1208 | if (task && head->task != task) |
08d23634 | 1209 | return false; |
3dd0c97a | 1210 | if (cancel_all) |
08d23634 PB |
1211 | return true; |
1212 | ||
1213 | io_for_each_link(req, head) { | |
b05a1bcd | 1214 | if (req->flags & REQ_F_INFLIGHT) |
02a13674 | 1215 | return true; |
08d23634 PB |
1216 | } |
1217 | return false; | |
1218 | } | |
1219 | ||
93d2bcd2 | 1220 | static inline void req_set_fail(struct io_kiocb *req) |
c40f6379 | 1221 | { |
93d2bcd2 | 1222 | req->flags |= REQ_F_FAIL; |
c40f6379 | 1223 | } |
4a38aed2 | 1224 | |
a8295b98 HX |
1225 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
1226 | { | |
1227 | req_set_fail(req); | |
1228 | req->result = res; | |
1229 | } | |
1230 | ||
2b188cc1 JA |
1231 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
1232 | { | |
1233 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1234 | ||
0f158b4c | 1235 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1236 | } |
1237 | ||
8eb7e2d0 PB |
1238 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1239 | { | |
1240 | return !req->timeout.off; | |
1241 | } | |
1242 | ||
f56165e6 PB |
1243 | static void io_fallback_req_func(struct work_struct *work) |
1244 | { | |
1245 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, | |
1246 | fallback_work.work); | |
1247 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); | |
1248 | struct io_kiocb *req, *tmp; | |
f237c30a | 1249 | bool locked = false; |
f56165e6 PB |
1250 | |
1251 | percpu_ref_get(&ctx->refs); | |
1252 | llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node) | |
f237c30a | 1253 | req->io_task_work.func(req, &locked); |
5636c00d | 1254 | |
f237c30a PB |
1255 | if (locked) { |
1256 | if (ctx->submit_state.compl_nr) | |
1257 | io_submit_flush_completions(ctx); | |
1258 | mutex_unlock(&ctx->uring_lock); | |
1259 | } | |
f56165e6 | 1260 | percpu_ref_put(&ctx->refs); |
f237c30a | 1261 | |
f56165e6 PB |
1262 | } |
1263 | ||
2b188cc1 JA |
1264 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
1265 | { | |
1266 | struct io_ring_ctx *ctx; | |
78076bb6 | 1267 | int hash_bits; |
2b188cc1 JA |
1268 | |
1269 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1270 | if (!ctx) | |
1271 | return NULL; | |
1272 | ||
78076bb6 JA |
1273 | /* |
1274 | * Use 5 bits less than the max cq entries, that should give us around | |
1275 | * 32 entries per hash list if totally full and uniformly spread. | |
1276 | */ | |
1277 | hash_bits = ilog2(p->cq_entries); | |
1278 | hash_bits -= 5; | |
1279 | if (hash_bits <= 0) | |
1280 | hash_bits = 1; | |
1281 | ctx->cancel_hash_bits = hash_bits; | |
1282 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1283 | GFP_KERNEL); | |
1284 | if (!ctx->cancel_hash) | |
1285 | goto err; | |
1286 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1287 | ||
6224843d PB |
1288 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
1289 | if (!ctx->dummy_ubuf) | |
1290 | goto err; | |
1291 | /* set invalid range, so io_import_fixed() fails meeting it */ | |
1292 | ctx->dummy_ubuf->ubuf = -1UL; | |
1293 | ||
21482896 | 1294 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1295 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1296 | goto err; | |
2b188cc1 JA |
1297 | |
1298 | ctx->flags = p->flags; | |
90554200 | 1299 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1300 | INIT_LIST_HEAD(&ctx->sqd_list); |
311997b3 | 1301 | init_waitqueue_head(&ctx->poll_wait); |
1d7bb1d5 | 1302 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c | 1303 | init_completion(&ctx->ref_comp); |
9e15c3a0 | 1304 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
61cf9370 | 1305 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
2b188cc1 | 1306 | mutex_init(&ctx->uring_lock); |
311997b3 | 1307 | init_waitqueue_head(&ctx->cq_wait); |
2b188cc1 | 1308 | spin_lock_init(&ctx->completion_lock); |
89850fce | 1309 | spin_lock_init(&ctx->timeout_lock); |
540e32a0 | 1310 | INIT_LIST_HEAD(&ctx->iopoll_list); |
de0617e4 | 1311 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1312 | INIT_LIST_HEAD(&ctx->timeout_list); |
ef9dd637 | 1313 | INIT_LIST_HEAD(&ctx->ltimeout_list); |
d67d2263 BM |
1314 | spin_lock_init(&ctx->rsrc_ref_lock); |
1315 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1316 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1317 | init_llist_head(&ctx->rsrc_put_llist); | |
13bf43f5 | 1318 | INIT_LIST_HEAD(&ctx->tctx_list); |
cd0ca2e0 | 1319 | INIT_LIST_HEAD(&ctx->submit_state.free_list); |
d0acdee2 | 1320 | INIT_LIST_HEAD(&ctx->locked_free_list); |
9011bf9a | 1321 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
2b188cc1 | 1322 | return ctx; |
206aefde | 1323 | err: |
6224843d | 1324 | kfree(ctx->dummy_ubuf); |
78076bb6 | 1325 | kfree(ctx->cancel_hash); |
206aefde JA |
1326 | kfree(ctx); |
1327 | return NULL; | |
2b188cc1 JA |
1328 | } |
1329 | ||
8f6ed49a PB |
1330 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
1331 | { | |
1332 | struct io_rings *r = ctx->rings; | |
1333 | ||
1334 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); | |
1335 | ctx->cq_extra--; | |
1336 | } | |
1337 | ||
9cf7c104 | 1338 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1339 | { |
2bc9930e JA |
1340 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1341 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1342 | |
8f6ed49a | 1343 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
2bc9930e | 1344 | } |
de0617e4 | 1345 | |
9d858b21 | 1346 | return false; |
de0617e4 JA |
1347 | } |
1348 | ||
c97d8a0f PB |
1349 | #define FFS_ASYNC_READ 0x1UL |
1350 | #define FFS_ASYNC_WRITE 0x2UL | |
1351 | #ifdef CONFIG_64BIT | |
1352 | #define FFS_ISREG 0x4UL | |
1353 | #else | |
1354 | #define FFS_ISREG 0x0UL | |
1355 | #endif | |
1356 | #define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG) | |
1357 | ||
1358 | static inline bool io_req_ffs_set(struct io_kiocb *req) | |
1359 | { | |
1360 | return IS_ENABLED(CONFIG_64BIT) && (req->flags & REQ_F_FIXED_FILE); | |
1361 | } | |
1362 | ||
ce3d5aae PB |
1363 | static void io_req_track_inflight(struct io_kiocb *req) |
1364 | { | |
ce3d5aae | 1365 | if (!(req->flags & REQ_F_INFLIGHT)) { |
ce3d5aae | 1366 | req->flags |= REQ_F_INFLIGHT; |
b303fe2e | 1367 | atomic_inc(¤t->io_uring->inflight_tracked); |
ce3d5aae PB |
1368 | } |
1369 | } | |
1370 | ||
906c6caa PB |
1371 | static inline void io_unprep_linked_timeout(struct io_kiocb *req) |
1372 | { | |
1373 | req->flags &= ~REQ_F_LINK_TIMEOUT; | |
1374 | } | |
1375 | ||
fd08e530 PB |
1376 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
1377 | { | |
906c6caa PB |
1378 | if (WARN_ON_ONCE(!req->link)) |
1379 | return NULL; | |
1380 | ||
4d13d1a4 PB |
1381 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
1382 | req->flags |= REQ_F_LINK_TIMEOUT; | |
fd08e530 PB |
1383 | |
1384 | /* linked timeouts should have two refs once prep'ed */ | |
48dcd38d | 1385 | io_req_set_refcount(req); |
4d13d1a4 PB |
1386 | __io_req_set_refcount(req->link, 2); |
1387 | return req->link; | |
fd08e530 PB |
1388 | } |
1389 | ||
1390 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) | |
1391 | { | |
4d13d1a4 | 1392 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
fd08e530 PB |
1393 | return NULL; |
1394 | return __io_prep_linked_timeout(req); | |
1395 | } | |
1396 | ||
1e6fa521 JA |
1397 | static void io_prep_async_work(struct io_kiocb *req) |
1398 | { | |
1399 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1400 | struct io_ring_ctx *ctx = req->ctx; |
1401 | ||
b8e64b53 PB |
1402 | if (!(req->flags & REQ_F_CREDS)) { |
1403 | req->flags |= REQ_F_CREDS; | |
c10d1f98 | 1404 | req->creds = get_current_cred(); |
b8e64b53 | 1405 | } |
003e8dcc | 1406 | |
e1d675df PB |
1407 | req->work.list.next = NULL; |
1408 | req->work.flags = 0; | |
feaadc4f PB |
1409 | if (req->flags & REQ_F_FORCE_ASYNC) |
1410 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1411 | ||
1e6fa521 JA |
1412 | if (req->flags & REQ_F_ISREG) { |
1413 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1414 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
4b982bd0 | 1415 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
1e6fa521 JA |
1416 | if (def->unbound_nonreg_file) |
1417 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1418 | } | |
e1d675df PB |
1419 | |
1420 | switch (req->opcode) { | |
1421 | case IORING_OP_SPLICE: | |
1422 | case IORING_OP_TEE: | |
e1d675df PB |
1423 | if (!S_ISREG(file_inode(req->splice.file_in)->i_mode)) |
1424 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1425 | break; | |
1426 | } | |
561fb04a | 1427 | } |
cccf0ee8 | 1428 | |
cbdcb435 | 1429 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1430 | { |
cbdcb435 | 1431 | struct io_kiocb *cur; |
54a91f3b | 1432 | |
44eff40a PB |
1433 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
1434 | struct io_ring_ctx *ctx = req->ctx; | |
1435 | ||
79ebeaee | 1436 | spin_lock(&ctx->completion_lock); |
44eff40a PB |
1437 | io_for_each_link(cur, req) |
1438 | io_prep_async_work(cur); | |
79ebeaee | 1439 | spin_unlock(&ctx->completion_lock); |
44eff40a PB |
1440 | } else { |
1441 | io_for_each_link(cur, req) | |
1442 | io_prep_async_work(cur); | |
1443 | } | |
561fb04a JA |
1444 | } |
1445 | ||
f237c30a | 1446 | static void io_queue_async_work(struct io_kiocb *req, bool *locked) |
561fb04a | 1447 | { |
a197f664 | 1448 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1449 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1450 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1451 | |
f237c30a PB |
1452 | /* must not take the lock, NULL it as a precaution */ |
1453 | locked = NULL; | |
1454 | ||
3bfe6106 JA |
1455 | BUG_ON(!tctx); |
1456 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1457 | |
cbdcb435 PB |
1458 | /* init ->work of the whole link before punting */ |
1459 | io_prep_async_link(req); | |
991468dc JA |
1460 | |
1461 | /* | |
1462 | * Not expected to happen, but if we do have a bug where this _can_ | |
1463 | * happen, catch it here and ensure the request is marked as | |
1464 | * canceled. That will make io-wq go through the usual work cancel | |
1465 | * procedure rather than attempt to run this request (or create a new | |
1466 | * worker for it). | |
1467 | */ | |
1468 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) | |
1469 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1470 | ||
d07f1e8a PB |
1471 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1472 | &req->work, req->flags); | |
ebf93667 | 1473 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a JA |
1474 | if (link) |
1475 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1476 | } |
1477 | ||
1ee4160c | 1478 | static void io_kill_timeout(struct io_kiocb *req, int status) |
8c855885 | 1479 | __must_hold(&req->ctx->completion_lock) |
89850fce | 1480 | __must_hold(&req->ctx->timeout_lock) |
5262f567 | 1481 | { |
e8c2bc1f | 1482 | struct io_timeout_data *io = req->async_data; |
5262f567 | 1483 | |
fd9c7bc5 | 1484 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
2ae2eb9d PB |
1485 | if (status) |
1486 | req_set_fail(req); | |
01cec8c1 PB |
1487 | atomic_set(&req->ctx->cq_timeouts, |
1488 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1489 | list_del_init(&req->timeout.list); |
d4d19c19 | 1490 | io_cqring_fill_event(req->ctx, req->user_data, status, 0); |
91c2f697 | 1491 | io_put_req_deferred(req); |
5262f567 JA |
1492 | } |
1493 | } | |
1494 | ||
441b8a78 | 1495 | static void io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1496 | { |
441b8a78 | 1497 | while (!list_empty(&ctx->defer_list)) { |
27dc8338 PB |
1498 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1499 | struct io_defer_entry, list); | |
de0617e4 | 1500 | |
9cf7c104 | 1501 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1502 | break; |
27dc8338 | 1503 | list_del_init(&de->list); |
907d1df3 | 1504 | io_req_task_queue(de->req); |
27dc8338 | 1505 | kfree(de); |
441b8a78 | 1506 | } |
04518945 PB |
1507 | } |
1508 | ||
360428f8 | 1509 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
89850fce | 1510 | __must_hold(&ctx->completion_lock) |
de0617e4 | 1511 | { |
441b8a78 | 1512 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
f010505b | 1513 | |
79ebeaee | 1514 | spin_lock_irq(&ctx->timeout_lock); |
f18ee4cf | 1515 | while (!list_empty(&ctx->timeout_list)) { |
f010505b | 1516 | u32 events_needed, events_got; |
360428f8 | 1517 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1518 | struct io_kiocb, timeout.list); |
de0617e4 | 1519 | |
8eb7e2d0 | 1520 | if (io_is_timeout_noseq(req)) |
360428f8 | 1521 | break; |
f010505b MDG |
1522 | |
1523 | /* | |
1524 | * Since seq can easily wrap around over time, subtract | |
1525 | * the last seq at which timeouts were flushed before comparing. | |
1526 | * Assuming not more than 2^31-1 events have happened since, | |
1527 | * these subtractions won't have wrapped, so we can check if | |
1528 | * target is in [last_seq, current_seq] by comparing the two. | |
1529 | */ | |
1530 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1531 | events_got = seq - ctx->cq_last_tm_flush; | |
1532 | if (events_got < events_needed) | |
360428f8 | 1533 | break; |
bfe68a22 | 1534 | |
135fcde8 | 1535 | list_del_init(&req->timeout.list); |
1ee4160c | 1536 | io_kill_timeout(req, 0); |
f18ee4cf | 1537 | } |
f010505b | 1538 | ctx->cq_last_tm_flush = seq; |
79ebeaee | 1539 | spin_unlock_irq(&ctx->timeout_lock); |
360428f8 | 1540 | } |
5262f567 | 1541 | |
2335f6f5 | 1542 | static void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
360428f8 | 1543 | { |
2335f6f5 PB |
1544 | if (ctx->off_timeout_used) |
1545 | io_flush_timeouts(ctx); | |
1546 | if (ctx->drain_active) | |
1547 | io_queue_deferred(ctx); | |
1548 | } | |
1549 | ||
1550 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) | |
1551 | { | |
1552 | if (unlikely(ctx->off_timeout_used || ctx->drain_active)) | |
1553 | __io_commit_cqring_flush(ctx); | |
ec30e04b PB |
1554 | /* order cqe stores with ring update */ |
1555 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 JA |
1556 | } |
1557 | ||
90554200 JA |
1558 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1559 | { | |
1560 | struct io_rings *r = ctx->rings; | |
1561 | ||
a566c556 | 1562 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries; |
90554200 JA |
1563 | } |
1564 | ||
888aae2e PB |
1565 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1566 | { | |
1567 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1568 | } | |
1569 | ||
d068b506 | 1570 | static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) |
2b188cc1 | 1571 | { |
75b28aff | 1572 | struct io_rings *rings = ctx->rings; |
ea5ab3b5 | 1573 | unsigned tail, mask = ctx->cq_entries - 1; |
2b188cc1 | 1574 | |
115e12e5 SB |
1575 | /* |
1576 | * writes to the cq entry need to come after reading head; the | |
1577 | * control dependency is enough as we're using WRITE_ONCE to | |
1578 | * fill the cq entry | |
1579 | */ | |
a566c556 | 1580 | if (__io_cqring_events(ctx) == ctx->cq_entries) |
2b188cc1 JA |
1581 | return NULL; |
1582 | ||
888aae2e | 1583 | tail = ctx->cached_cq_tail++; |
ea5ab3b5 | 1584 | return &rings->cqes[tail & mask]; |
2b188cc1 JA |
1585 | } |
1586 | ||
f2842ab5 JA |
1587 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1588 | { | |
44c769de | 1589 | if (likely(!ctx->cq_ev_fd)) |
f0b493e6 | 1590 | return false; |
7e55a19c SG |
1591 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1592 | return false; | |
44c769de | 1593 | return !ctx->eventfd_async || io_wq_current_is_worker(); |
f2842ab5 JA |
1594 | } |
1595 | ||
2c5d763c JA |
1596 | /* |
1597 | * This should only get called when at least one event has been posted. | |
1598 | * Some applications rely on the eventfd notification count only changing | |
1599 | * IFF a new CQE has been added to the CQ ring. There's no depedency on | |
1600 | * 1:1 relationship between how many times this function is called (and | |
1601 | * hence the eventfd count) and number of CQEs posted to the CQ ring. | |
1602 | */ | |
b41e9852 | 1603 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1604 | { |
5fd46178 JA |
1605 | /* |
1606 | * wake_up_all() may seem excessive, but io_wake_function() and | |
1607 | * io_should_wake() handle the termination of the loop and only | |
1608 | * wake as many waiters as we need to. | |
1609 | */ | |
1610 | if (wq_has_sleeper(&ctx->cq_wait)) | |
1611 | wake_up_all(&ctx->cq_wait); | |
534ca6d6 JA |
1612 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
1613 | wake_up(&ctx->sq_data->wait); | |
b41e9852 | 1614 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 | 1615 | eventfd_signal(ctx->cq_ev_fd, 1); |
3f008385 | 1616 | if (waitqueue_active(&ctx->poll_wait)) |
311997b3 | 1617 | wake_up_interruptible(&ctx->poll_wait); |
1d7bb1d5 JA |
1618 | } |
1619 | ||
80c18e4a PB |
1620 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1621 | { | |
c57a91fb PB |
1622 | /* see waitqueue_active() comment */ |
1623 | smp_mb(); | |
1624 | ||
80c18e4a | 1625 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
c57a91fb | 1626 | if (waitqueue_active(&ctx->cq_wait)) |
5fd46178 | 1627 | wake_up_all(&ctx->cq_wait); |
80c18e4a PB |
1628 | } |
1629 | if (io_should_trigger_evfd(ctx)) | |
1630 | eventfd_signal(ctx->cq_ev_fd, 1); | |
3f008385 | 1631 | if (waitqueue_active(&ctx->poll_wait)) |
311997b3 | 1632 | wake_up_interruptible(&ctx->poll_wait); |
80c18e4a PB |
1633 | } |
1634 | ||
c4a2ed72 | 1635 | /* Returns true if there are no backlogged entries after the flush */ |
6c2450ae | 1636 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
1d7bb1d5 | 1637 | { |
b18032bb | 1638 | bool all_flushed, posted; |
1d7bb1d5 | 1639 | |
a566c556 | 1640 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
e23de15f | 1641 | return false; |
1d7bb1d5 | 1642 | |
b18032bb | 1643 | posted = false; |
79ebeaee | 1644 | spin_lock(&ctx->completion_lock); |
6c2450ae | 1645 | while (!list_empty(&ctx->cq_overflow_list)) { |
d068b506 | 1646 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
6c2450ae | 1647 | struct io_overflow_cqe *ocqe; |
e6c8aa9a | 1648 | |
1d7bb1d5 JA |
1649 | if (!cqe && !force) |
1650 | break; | |
6c2450ae PB |
1651 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
1652 | struct io_overflow_cqe, list); | |
1653 | if (cqe) | |
1654 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); | |
1655 | else | |
8f6ed49a PB |
1656 | io_account_cq_overflow(ctx); |
1657 | ||
b18032bb | 1658 | posted = true; |
6c2450ae PB |
1659 | list_del(&ocqe->list); |
1660 | kfree(ocqe); | |
1d7bb1d5 JA |
1661 | } |
1662 | ||
09e88404 PB |
1663 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1664 | if (all_flushed) { | |
5ed7a37d | 1665 | clear_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1666 | WRITE_ONCE(ctx->rings->sq_flags, |
1667 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); | |
09e88404 | 1668 | } |
46930143 | 1669 | |
b18032bb JA |
1670 | if (posted) |
1671 | io_commit_cqring(ctx); | |
79ebeaee | 1672 | spin_unlock(&ctx->completion_lock); |
b18032bb JA |
1673 | if (posted) |
1674 | io_cqring_ev_posted(ctx); | |
09e88404 | 1675 | return all_flushed; |
1d7bb1d5 JA |
1676 | } |
1677 | ||
90f67366 | 1678 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
6c503150 | 1679 | { |
ca0a2651 JA |
1680 | bool ret = true; |
1681 | ||
5ed7a37d | 1682 | if (test_bit(0, &ctx->check_cq_overflow)) { |
6c503150 PB |
1683 | /* iopoll syncs against uring_lock, not completion_lock */ |
1684 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1685 | mutex_lock(&ctx->uring_lock); | |
90f67366 | 1686 | ret = __io_cqring_overflow_flush(ctx, false); |
6c503150 PB |
1687 | if (ctx->flags & IORING_SETUP_IOPOLL) |
1688 | mutex_unlock(&ctx->uring_lock); | |
1689 | } | |
ca0a2651 JA |
1690 | |
1691 | return ret; | |
6c503150 PB |
1692 | } |
1693 | ||
6a290a14 PB |
1694 | /* must to be called somewhat shortly after putting a request */ |
1695 | static inline void io_put_task(struct task_struct *task, int nr) | |
1696 | { | |
1697 | struct io_uring_task *tctx = task->io_uring; | |
1698 | ||
e98e49b2 PB |
1699 | if (likely(task == current)) { |
1700 | tctx->cached_refs += nr; | |
1701 | } else { | |
1702 | percpu_counter_sub(&tctx->inflight, nr); | |
1703 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1704 | wake_up(&tctx->wait); | |
1705 | put_task_struct_many(task, nr); | |
1706 | } | |
6a290a14 PB |
1707 | } |
1708 | ||
9a10867a PB |
1709 | static void io_task_refs_refill(struct io_uring_task *tctx) |
1710 | { | |
1711 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; | |
1712 | ||
1713 | percpu_counter_add(&tctx->inflight, refill); | |
1714 | refcount_add(refill, ¤t->usage); | |
1715 | tctx->cached_refs += refill; | |
1716 | } | |
1717 | ||
1718 | static inline void io_get_task_refs(int nr) | |
1719 | { | |
1720 | struct io_uring_task *tctx = current->io_uring; | |
1721 | ||
1722 | tctx->cached_refs -= nr; | |
1723 | if (unlikely(tctx->cached_refs < 0)) | |
1724 | io_task_refs_refill(tctx); | |
1725 | } | |
1726 | ||
d4d19c19 PB |
1727 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
1728 | long res, unsigned int cflags) | |
2b188cc1 | 1729 | { |
cce4b8b0 | 1730 | struct io_overflow_cqe *ocqe; |
2b188cc1 | 1731 | |
cce4b8b0 PB |
1732 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
1733 | if (!ocqe) { | |
1734 | /* | |
1735 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1736 | * or cannot allocate an overflow entry, then we need to drop it | |
1737 | * on the floor. | |
1738 | */ | |
8f6ed49a | 1739 | io_account_cq_overflow(ctx); |
cce4b8b0 | 1740 | return false; |
2b188cc1 | 1741 | } |
cce4b8b0 | 1742 | if (list_empty(&ctx->cq_overflow_list)) { |
5ed7a37d | 1743 | set_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1744 | WRITE_ONCE(ctx->rings->sq_flags, |
1745 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); | |
1746 | ||
cce4b8b0 | 1747 | } |
d4d19c19 | 1748 | ocqe->cqe.user_data = user_data; |
cce4b8b0 PB |
1749 | ocqe->cqe.res = res; |
1750 | ocqe->cqe.flags = cflags; | |
1751 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); | |
1752 | return true; | |
2b188cc1 JA |
1753 | } |
1754 | ||
d4d19c19 PB |
1755 | static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
1756 | long res, unsigned int cflags) | |
2b188cc1 JA |
1757 | { |
1758 | struct io_uring_cqe *cqe; | |
1759 | ||
d4d19c19 | 1760 | trace_io_uring_complete(ctx, user_data, res, cflags); |
51c3ff62 | 1761 | |
2b188cc1 JA |
1762 | /* |
1763 | * If we can't get a cq entry, userspace overflowed the | |
1764 | * submission (by quite a lot). Increment the overflow count in | |
1765 | * the ring. | |
1766 | */ | |
d068b506 | 1767 | cqe = io_get_cqe(ctx); |
1d7bb1d5 | 1768 | if (likely(cqe)) { |
d4d19c19 | 1769 | WRITE_ONCE(cqe->user_data, user_data); |
2b188cc1 | 1770 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1771 | WRITE_ONCE(cqe->flags, cflags); |
8d13326e | 1772 | return true; |
2b188cc1 | 1773 | } |
d4d19c19 | 1774 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
2b188cc1 JA |
1775 | } |
1776 | ||
8d13326e | 1777 | /* not as hot to bloat with inlining */ |
d4d19c19 PB |
1778 | static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
1779 | long res, unsigned int cflags) | |
bcda7baa | 1780 | { |
d4d19c19 | 1781 | return __io_cqring_fill_event(ctx, user_data, res, cflags); |
bcda7baa JA |
1782 | } |
1783 | ||
7a612350 PB |
1784 | static void io_req_complete_post(struct io_kiocb *req, long res, |
1785 | unsigned int cflags) | |
2b188cc1 | 1786 | { |
78e19bbe | 1787 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 | 1788 | |
79ebeaee | 1789 | spin_lock(&ctx->completion_lock); |
d4d19c19 | 1790 | __io_cqring_fill_event(ctx, req->user_data, res, cflags); |
c7dae4ba JA |
1791 | /* |
1792 | * If we're the last reference to this request, add to our locked | |
1793 | * free_list cache. | |
1794 | */ | |
de9b4cca | 1795 | if (req_ref_put_and_test(req)) { |
7a612350 | 1796 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
0756a869 | 1797 | if (req->flags & IO_DISARM_MASK) |
7a612350 PB |
1798 | io_disarm_next(req); |
1799 | if (req->link) { | |
1800 | io_req_task_queue(req->link); | |
1801 | req->link = NULL; | |
1802 | } | |
1803 | } | |
c7dae4ba JA |
1804 | io_dismantle_req(req); |
1805 | io_put_task(req->task, 1); | |
bb943b82 | 1806 | list_add(&req->inflight_entry, &ctx->locked_free_list); |
d0acdee2 | 1807 | ctx->locked_free_nr++; |
180f829f PB |
1808 | } else { |
1809 | if (!percpu_ref_tryget(&ctx->refs)) | |
1810 | req = NULL; | |
1811 | } | |
7a612350 | 1812 | io_commit_cqring(ctx); |
79ebeaee | 1813 | spin_unlock(&ctx->completion_lock); |
7a612350 | 1814 | |
180f829f PB |
1815 | if (req) { |
1816 | io_cqring_ev_posted(ctx); | |
c7dae4ba | 1817 | percpu_ref_put(&ctx->refs); |
180f829f | 1818 | } |
229a7b63 JA |
1819 | } |
1820 | ||
4e3d9ff9 JA |
1821 | static inline bool io_req_needs_clean(struct io_kiocb *req) |
1822 | { | |
c854357b | 1823 | return req->flags & IO_REQ_CLEAN_FLAGS; |
4e3d9ff9 JA |
1824 | } |
1825 | ||
a38d68db | 1826 | static void io_req_complete_state(struct io_kiocb *req, long res, |
889fca73 | 1827 | unsigned int cflags) |
229a7b63 | 1828 | { |
4e3d9ff9 | 1829 | if (io_req_needs_clean(req)) |
68fb8979 | 1830 | io_clean_op(req); |
a38d68db PB |
1831 | req->result = res; |
1832 | req->compl.cflags = cflags; | |
e342c807 | 1833 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
1834 | } |
1835 | ||
889fca73 PB |
1836 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
1837 | long res, unsigned cflags) | |
bcda7baa | 1838 | { |
889fca73 PB |
1839 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
1840 | io_req_complete_state(req, res, cflags); | |
a38d68db | 1841 | else |
c7dae4ba | 1842 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
1843 | } |
1844 | ||
a38d68db | 1845 | static inline void io_req_complete(struct io_kiocb *req, long res) |
0ddf92e8 | 1846 | { |
889fca73 | 1847 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
1848 | } |
1849 | ||
f41db273 PB |
1850 | static void io_req_complete_failed(struct io_kiocb *req, long res) |
1851 | { | |
93d2bcd2 | 1852 | req_set_fail(req); |
f41db273 PB |
1853 | io_req_complete_post(req, res, 0); |
1854 | } | |
1855 | ||
c6d3d9cb PB |
1856 | static void io_req_complete_fail_submit(struct io_kiocb *req) |
1857 | { | |
1858 | /* | |
1859 | * We don't submit, fail them all, for that replace hardlinks with | |
1860 | * normal links. Extra REQ_F_LINK is tolerated. | |
1861 | */ | |
1862 | req->flags &= ~REQ_F_HARDLINK; | |
1863 | req->flags |= REQ_F_LINK; | |
1864 | io_req_complete_failed(req, req->result); | |
1865 | } | |
1866 | ||
864ea921 PB |
1867 | /* |
1868 | * Don't initialise the fields below on every allocation, but do that in | |
1869 | * advance and keep them valid across allocations. | |
1870 | */ | |
1871 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
1872 | { | |
1873 | req->ctx = ctx; | |
1874 | req->link = NULL; | |
1875 | req->async_data = NULL; | |
1876 | /* not necessary, but safer to zero */ | |
1877 | req->result = 0; | |
1878 | } | |
1879 | ||
dac7a098 | 1880 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
cd0ca2e0 | 1881 | struct io_submit_state *state) |
dac7a098 | 1882 | { |
79ebeaee | 1883 | spin_lock(&ctx->completion_lock); |
cd0ca2e0 | 1884 | list_splice_init(&ctx->locked_free_list, &state->free_list); |
d0acdee2 | 1885 | ctx->locked_free_nr = 0; |
79ebeaee | 1886 | spin_unlock(&ctx->completion_lock); |
dac7a098 PB |
1887 | } |
1888 | ||
dd78f492 | 1889 | /* Returns true IFF there are requests in the cache */ |
c7dae4ba | 1890 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 1891 | { |
c7dae4ba | 1892 | struct io_submit_state *state = &ctx->submit_state; |
dd78f492 | 1893 | int nr; |
0ddf92e8 | 1894 | |
c7dae4ba JA |
1895 | /* |
1896 | * If we have more than a batch's worth of requests in our IRQ side | |
1897 | * locked cache, grab the lock and move them over to our submission | |
1898 | * side cache. | |
1899 | */ | |
d0acdee2 | 1900 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
cd0ca2e0 | 1901 | io_flush_cached_locked_reqs(ctx, state); |
0ddf92e8 | 1902 | |
dd78f492 | 1903 | nr = state->free_reqs; |
cd0ca2e0 PB |
1904 | while (!list_empty(&state->free_list)) { |
1905 | struct io_kiocb *req = list_first_entry(&state->free_list, | |
bb943b82 | 1906 | struct io_kiocb, inflight_entry); |
dd78f492 | 1907 | |
bb943b82 | 1908 | list_del(&req->inflight_entry); |
dd78f492 PB |
1909 | state->reqs[nr++] = req; |
1910 | if (nr == ARRAY_SIZE(state->reqs)) | |
e5d1bc0a | 1911 | break; |
1b4c351f JA |
1912 | } |
1913 | ||
dd78f492 PB |
1914 | state->free_reqs = nr; |
1915 | return nr != 0; | |
0ddf92e8 JA |
1916 | } |
1917 | ||
5d5901a3 PB |
1918 | /* |
1919 | * A request might get retired back into the request caches even before opcode | |
1920 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. | |
1921 | * Because of that, io_alloc_req() should be called only under ->uring_lock | |
1922 | * and with extra caution to not get a request that is still worked on. | |
1923 | */ | |
e5d1bc0a | 1924 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
5d5901a3 | 1925 | __must_hold(&ctx->uring_lock) |
2b188cc1 | 1926 | { |
e5d1bc0a | 1927 | struct io_submit_state *state = &ctx->submit_state; |
864ea921 PB |
1928 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
1929 | int ret, i; | |
e5d1bc0a | 1930 | |
fe7e3257 | 1931 | BUILD_BUG_ON(ARRAY_SIZE(state->reqs) < IO_REQ_ALLOC_BATCH); |
e5d1bc0a | 1932 | |
864ea921 PB |
1933 | if (likely(state->free_reqs || io_flush_cached_reqs(ctx))) |
1934 | goto got_req; | |
e5d1bc0a | 1935 | |
864ea921 PB |
1936 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
1937 | state->reqs); | |
fd6fab2c | 1938 | |
864ea921 PB |
1939 | /* |
1940 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1941 | * retry single alloc to be on the safe side. | |
1942 | */ | |
1943 | if (unlikely(ret <= 0)) { | |
1944 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
1945 | if (!state->reqs[0]) | |
1946 | return NULL; | |
1947 | ret = 1; | |
2b188cc1 | 1948 | } |
864ea921 PB |
1949 | |
1950 | for (i = 0; i < ret; i++) | |
1951 | io_preinit_req(state->reqs[i], ctx); | |
1952 | state->free_reqs = ret; | |
e5d1bc0a | 1953 | got_req: |
291b2821 PB |
1954 | state->free_reqs--; |
1955 | return state->reqs[state->free_reqs]; | |
2b188cc1 JA |
1956 | } |
1957 | ||
e1d767f0 | 1958 | static inline void io_put_file(struct file *file) |
8da11c19 | 1959 | { |
e1d767f0 | 1960 | if (file) |
8da11c19 PB |
1961 | fput(file); |
1962 | } | |
1963 | ||
4edf20f9 | 1964 | static void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 1965 | { |
094bae49 | 1966 | unsigned int flags = req->flags; |
929a3af9 | 1967 | |
3a0a6902 PB |
1968 | if (io_req_needs_clean(req)) |
1969 | io_clean_op(req); | |
e1d767f0 PB |
1970 | if (!(flags & REQ_F_FIXED_FILE)) |
1971 | io_put_file(req->file); | |
269bbe5f BM |
1972 | if (req->fixed_rsrc_refs) |
1973 | percpu_ref_put(req->fixed_rsrc_refs); | |
99ebe4ef | 1974 | if (req->async_data) { |
094bae49 | 1975 | kfree(req->async_data); |
99ebe4ef PB |
1976 | req->async_data = NULL; |
1977 | } | |
e65ef56d JA |
1978 | } |
1979 | ||
216578e5 | 1980 | static void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 1981 | { |
51a4cc11 | 1982 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 1983 | |
216578e5 | 1984 | io_dismantle_req(req); |
7c660731 | 1985 | io_put_task(req->task, 1); |
c6ca97b3 | 1986 | |
79ebeaee | 1987 | spin_lock(&ctx->completion_lock); |
bb943b82 | 1988 | list_add(&req->inflight_entry, &ctx->locked_free_list); |
c34b025f | 1989 | ctx->locked_free_nr++; |
79ebeaee | 1990 | spin_unlock(&ctx->completion_lock); |
c34b025f | 1991 | |
ecfc5177 | 1992 | percpu_ref_put(&ctx->refs); |
e65ef56d JA |
1993 | } |
1994 | ||
f2f87370 PB |
1995 | static inline void io_remove_next_linked(struct io_kiocb *req) |
1996 | { | |
1997 | struct io_kiocb *nxt = req->link; | |
1998 | ||
1999 | req->link = nxt->link; | |
2000 | nxt->link = NULL; | |
2001 | } | |
2002 | ||
33cc89a9 PB |
2003 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
2004 | __must_hold(&req->ctx->completion_lock) | |
89b263f6 | 2005 | __must_hold(&req->ctx->timeout_lock) |
2665abfd | 2006 | { |
33cc89a9 | 2007 | struct io_kiocb *link = req->link; |
f2f87370 | 2008 | |
b97e736a | 2009 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
c9abd7ad | 2010 | struct io_timeout_data *io = link->async_data; |
7c86ffee | 2011 | |
f2f87370 | 2012 | io_remove_next_linked(req); |
90cd7e42 | 2013 | link->timeout.head = NULL; |
fd9c7bc5 | 2014 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
ef9dd637 | 2015 | list_del(&link->timeout.list); |
d4d19c19 PB |
2016 | io_cqring_fill_event(link->ctx, link->user_data, |
2017 | -ECANCELED, 0); | |
91c2f697 | 2018 | io_put_req_deferred(link); |
d4729fbd | 2019 | return true; |
c9abd7ad PB |
2020 | } |
2021 | } | |
d4729fbd | 2022 | return false; |
7c86ffee PB |
2023 | } |
2024 | ||
d148ca4b | 2025 | static void io_fail_links(struct io_kiocb *req) |
33cc89a9 | 2026 | __must_hold(&req->ctx->completion_lock) |
9e645e11 | 2027 | { |
33cc89a9 | 2028 | struct io_kiocb *nxt, *link = req->link; |
9e645e11 | 2029 | |
f2f87370 | 2030 | req->link = NULL; |
f2f87370 | 2031 | while (link) { |
a8295b98 HX |
2032 | long res = -ECANCELED; |
2033 | ||
2034 | if (link->flags & REQ_F_FAIL) | |
2035 | res = link->result; | |
2036 | ||
f2f87370 PB |
2037 | nxt = link->link; |
2038 | link->link = NULL; | |
2665abfd | 2039 | |
f2f87370 | 2040 | trace_io_uring_fail_link(req, link); |
a8295b98 | 2041 | io_cqring_fill_event(link->ctx, link->user_data, res, 0); |
91c2f697 | 2042 | io_put_req_deferred(link); |
f2f87370 | 2043 | link = nxt; |
9e645e11 | 2044 | } |
33cc89a9 | 2045 | } |
9e645e11 | 2046 | |
33cc89a9 PB |
2047 | static bool io_disarm_next(struct io_kiocb *req) |
2048 | __must_hold(&req->ctx->completion_lock) | |
2049 | { | |
2050 | bool posted = false; | |
2051 | ||
0756a869 PB |
2052 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
2053 | struct io_kiocb *link = req->link; | |
2054 | ||
906c6caa | 2055 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
0756a869 PB |
2056 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
2057 | io_remove_next_linked(req); | |
2058 | io_cqring_fill_event(link->ctx, link->user_data, | |
2059 | -ECANCELED, 0); | |
2060 | io_put_req_deferred(link); | |
2061 | posted = true; | |
2062 | } | |
2063 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { | |
89b263f6 JA |
2064 | struct io_ring_ctx *ctx = req->ctx; |
2065 | ||
2066 | spin_lock_irq(&ctx->timeout_lock); | |
33cc89a9 | 2067 | posted = io_kill_linked_timeout(req); |
89b263f6 JA |
2068 | spin_unlock_irq(&ctx->timeout_lock); |
2069 | } | |
93d2bcd2 | 2070 | if (unlikely((req->flags & REQ_F_FAIL) && |
e4335ed3 | 2071 | !(req->flags & REQ_F_HARDLINK))) { |
33cc89a9 PB |
2072 | posted |= (req->link != NULL); |
2073 | io_fail_links(req); | |
2074 | } | |
2075 | return posted; | |
9e645e11 JA |
2076 | } |
2077 | ||
3fa5e0f3 | 2078 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
c69f8dbe | 2079 | { |
33cc89a9 | 2080 | struct io_kiocb *nxt; |
944e58bf | 2081 | |
9e645e11 JA |
2082 | /* |
2083 | * If LINK is set, we have dependent requests in this chain. If we | |
2084 | * didn't fail this request, queue the first one up, moving any other | |
2085 | * dependencies to the next request. In case of failure, fail the rest | |
2086 | * of the chain. | |
2087 | */ | |
0756a869 | 2088 | if (req->flags & IO_DISARM_MASK) { |
33cc89a9 | 2089 | struct io_ring_ctx *ctx = req->ctx; |
33cc89a9 PB |
2090 | bool posted; |
2091 | ||
79ebeaee | 2092 | spin_lock(&ctx->completion_lock); |
33cc89a9 PB |
2093 | posted = io_disarm_next(req); |
2094 | if (posted) | |
2095 | io_commit_cqring(req->ctx); | |
79ebeaee | 2096 | spin_unlock(&ctx->completion_lock); |
33cc89a9 PB |
2097 | if (posted) |
2098 | io_cqring_ev_posted(ctx); | |
f2f87370 | 2099 | } |
33cc89a9 PB |
2100 | nxt = req->link; |
2101 | req->link = NULL; | |
2102 | return nxt; | |
4d7dd462 | 2103 | } |
9e645e11 | 2104 | |
f2f87370 | 2105 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
3fa5e0f3 | 2106 | { |
cdbff982 | 2107 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
3fa5e0f3 PB |
2108 | return NULL; |
2109 | return __io_req_find_next(req); | |
2110 | } | |
2111 | ||
f237c30a | 2112 | static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked) |
2c32395d PB |
2113 | { |
2114 | if (!ctx) | |
2115 | return; | |
f237c30a | 2116 | if (*locked) { |
99c8bc52 HX |
2117 | if (ctx->submit_state.compl_nr) |
2118 | io_submit_flush_completions(ctx); | |
2c32395d | 2119 | mutex_unlock(&ctx->uring_lock); |
f237c30a | 2120 | *locked = false; |
2c32395d PB |
2121 | } |
2122 | percpu_ref_put(&ctx->refs); | |
2123 | } | |
2124 | ||
7cbf1722 | 2125 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 2126 | { |
f237c30a | 2127 | bool locked = false; |
ebd0df2e | 2128 | struct io_ring_ctx *ctx = NULL; |
3f18407d PB |
2129 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
2130 | task_work); | |
c40f6379 | 2131 | |
16f72070 | 2132 | while (1) { |
3f18407d PB |
2133 | struct io_wq_work_node *node; |
2134 | ||
8d4ad41e PB |
2135 | if (!tctx->task_list.first && locked && ctx->submit_state.compl_nr) |
2136 | io_submit_flush_completions(ctx); | |
2137 | ||
3f18407d | 2138 | spin_lock_irq(&tctx->task_lock); |
c6538be9 | 2139 | node = tctx->task_list.first; |
3f18407d | 2140 | INIT_WQ_LIST(&tctx->task_list); |
6294f368 PB |
2141 | if (!node) |
2142 | tctx->task_running = false; | |
3f18407d | 2143 | spin_unlock_irq(&tctx->task_lock); |
6294f368 PB |
2144 | if (!node) |
2145 | break; | |
3f18407d | 2146 | |
6294f368 | 2147 | do { |
3f18407d PB |
2148 | struct io_wq_work_node *next = node->next; |
2149 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2150 | io_task_work.node); | |
2151 | ||
2152 | if (req->ctx != ctx) { | |
f237c30a | 2153 | ctx_flush_and_put(ctx, &locked); |
3f18407d | 2154 | ctx = req->ctx; |
126180b9 PB |
2155 | /* if not contended, grab and improve batching */ |
2156 | locked = mutex_trylock(&ctx->uring_lock); | |
3f18407d PB |
2157 | percpu_ref_get(&ctx->refs); |
2158 | } | |
f237c30a | 2159 | req->io_task_work.func(req, &locked); |
3f18407d | 2160 | node = next; |
6294f368 PB |
2161 | } while (node); |
2162 | ||
7cbf1722 | 2163 | cond_resched(); |
3f18407d | 2164 | } |
ebd0df2e | 2165 | |
f237c30a | 2166 | ctx_flush_and_put(ctx, &locked); |
7cbf1722 JA |
2167 | } |
2168 | ||
e09ee510 | 2169 | static void io_req_task_work_add(struct io_kiocb *req) |
7cbf1722 | 2170 | { |
c15b79de | 2171 | struct task_struct *tsk = req->task; |
7cbf1722 | 2172 | struct io_uring_task *tctx = tsk->io_uring; |
c15b79de | 2173 | enum task_work_notify_mode notify; |
e09ee510 | 2174 | struct io_wq_work_node *node; |
0b81e80c | 2175 | unsigned long flags; |
6294f368 | 2176 | bool running; |
7cbf1722 JA |
2177 | |
2178 | WARN_ON_ONCE(!tctx); | |
2179 | ||
0b81e80c | 2180 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 | 2181 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
6294f368 PB |
2182 | running = tctx->task_running; |
2183 | if (!running) | |
2184 | tctx->task_running = true; | |
0b81e80c | 2185 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2186 | |
2187 | /* task_work already pending, we're done */ | |
6294f368 | 2188 | if (running) |
e09ee510 | 2189 | return; |
7cbf1722 | 2190 | |
c15b79de PB |
2191 | /* |
2192 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For | |
2193 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2194 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2195 | * will do the job. | |
2196 | */ | |
2197 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; | |
c15b79de PB |
2198 | if (!task_work_add(tsk, &tctx->task_work, notify)) { |
2199 | wake_up_process(tsk); | |
e09ee510 | 2200 | return; |
c15b79de | 2201 | } |
2215bed9 | 2202 | |
0b81e80c | 2203 | spin_lock_irqsave(&tctx->task_lock, flags); |
6294f368 | 2204 | tctx->task_running = false; |
e09ee510 PB |
2205 | node = tctx->task_list.first; |
2206 | INIT_WQ_LIST(&tctx->task_list); | |
0b81e80c | 2207 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 | 2208 | |
e09ee510 PB |
2209 | while (node) { |
2210 | req = container_of(node, struct io_kiocb, io_task_work.node); | |
2211 | node = node->next; | |
2212 | if (llist_add(&req->io_task_work.fallback_node, | |
2213 | &req->ctx->fallback_llist)) | |
2214 | schedule_delayed_work(&req->ctx->fallback_work, 1); | |
2215 | } | |
eab30c4d PB |
2216 | } |
2217 | ||
f237c30a | 2218 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
c40f6379 | 2219 | { |
87ceb6a6 | 2220 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2221 | |
b18a1a45 | 2222 | /* not needed for normal modes, but SQPOLL depends on it */ |
f237c30a | 2223 | io_tw_lock(ctx, locked); |
2593553a | 2224 | io_req_complete_failed(req, req->result); |
c40f6379 JA |
2225 | } |
2226 | ||
f237c30a | 2227 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
c40f6379 JA |
2228 | { |
2229 | struct io_ring_ctx *ctx = req->ctx; | |
2230 | ||
f237c30a | 2231 | io_tw_lock(ctx, locked); |
316319e8 | 2232 | /* req->task == current here, checking PF_EXITING is safe */ |
af066f31 | 2233 | if (likely(!(req->task->flags & PF_EXITING))) |
c5eef2b9 | 2234 | __io_queue_sqe(req); |
81b6d05c | 2235 | else |
2593553a | 2236 | io_req_complete_failed(req, -EFAULT); |
c40f6379 JA |
2237 | } |
2238 | ||
2c4b8eb6 | 2239 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
c40f6379 | 2240 | { |
2c4b8eb6 | 2241 | req->result = ret; |
5b0a6acc | 2242 | req->io_task_work.func = io_req_task_cancel; |
e09ee510 | 2243 | io_req_task_work_add(req); |
c40f6379 JA |
2244 | } |
2245 | ||
2c4b8eb6 | 2246 | static void io_req_task_queue(struct io_kiocb *req) |
a3df7698 | 2247 | { |
5b0a6acc | 2248 | req->io_task_work.func = io_req_task_submit; |
e09ee510 | 2249 | io_req_task_work_add(req); |
a3df7698 PB |
2250 | } |
2251 | ||
773af691 JA |
2252 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
2253 | { | |
2254 | req->io_task_work.func = io_queue_async_work; | |
2255 | io_req_task_work_add(req); | |
2256 | } | |
2257 | ||
f2f87370 | 2258 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2259 | { |
9b5f7bd9 | 2260 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2261 | |
2262 | if (nxt) | |
906a8c3f | 2263 | io_req_task_queue(nxt); |
c69f8dbe JL |
2264 | } |
2265 | ||
c3524383 | 2266 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2267 | { |
c3524383 PB |
2268 | io_queue_next(req); |
2269 | __io_free_req(req); | |
2270 | } | |
8766dd51 | 2271 | |
f237c30a PB |
2272 | static void io_free_req_work(struct io_kiocb *req, bool *locked) |
2273 | { | |
2274 | io_free_req(req); | |
2275 | } | |
2276 | ||
2d6500d4 | 2277 | struct req_batch { |
5af1d13e PB |
2278 | struct task_struct *task; |
2279 | int task_refs; | |
1b4c351f | 2280 | int ctx_refs; |
2d6500d4 PB |
2281 | }; |
2282 | ||
5af1d13e PB |
2283 | static inline void io_init_req_batch(struct req_batch *rb) |
2284 | { | |
5af1d13e | 2285 | rb->task_refs = 0; |
9ae72463 | 2286 | rb->ctx_refs = 0; |
5af1d13e PB |
2287 | rb->task = NULL; |
2288 | } | |
2289 | ||
2d6500d4 PB |
2290 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
2291 | struct req_batch *rb) | |
2292 | { | |
9ae72463 PB |
2293 | if (rb->ctx_refs) |
2294 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); | |
e98e49b2 | 2295 | if (rb->task) |
e9dbe221 | 2296 | io_put_task(rb->task, rb->task_refs); |
2d6500d4 PB |
2297 | } |
2298 | ||
6ff119a6 PB |
2299 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
2300 | struct io_submit_state *state) | |
2d6500d4 | 2301 | { |
f2f87370 | 2302 | io_queue_next(req); |
96670657 | 2303 | io_dismantle_req(req); |
2d6500d4 | 2304 | |
e3bc8e9d | 2305 | if (req->task != rb->task) { |
7c660731 PB |
2306 | if (rb->task) |
2307 | io_put_task(rb->task, rb->task_refs); | |
e3bc8e9d JA |
2308 | rb->task = req->task; |
2309 | rb->task_refs = 0; | |
5af1d13e | 2310 | } |
e3bc8e9d | 2311 | rb->task_refs++; |
9ae72463 | 2312 | rb->ctx_refs++; |
5af1d13e | 2313 | |
bd759045 | 2314 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
6ff119a6 | 2315 | state->reqs[state->free_reqs++] = req; |
bd759045 | 2316 | else |
cd0ca2e0 | 2317 | list_add(&req->inflight_entry, &state->free_list); |
7a743e22 PB |
2318 | } |
2319 | ||
2a2758f2 | 2320 | static void io_submit_flush_completions(struct io_ring_ctx *ctx) |
a141dd89 | 2321 | __must_hold(&ctx->uring_lock) |
905c172f | 2322 | { |
cd0ca2e0 PB |
2323 | struct io_submit_state *state = &ctx->submit_state; |
2324 | int i, nr = state->compl_nr; | |
905c172f PB |
2325 | struct req_batch rb; |
2326 | ||
79ebeaee | 2327 | spin_lock(&ctx->completion_lock); |
905c172f | 2328 | for (i = 0; i < nr; i++) { |
cd0ca2e0 | 2329 | struct io_kiocb *req = state->compl_reqs[i]; |
5182ed2e | 2330 | |
d4d19c19 PB |
2331 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
2332 | req->compl.cflags); | |
905c172f PB |
2333 | } |
2334 | io_commit_cqring(ctx); | |
79ebeaee | 2335 | spin_unlock(&ctx->completion_lock); |
905c172f | 2336 | io_cqring_ev_posted(ctx); |
5182ed2e PB |
2337 | |
2338 | io_init_req_batch(&rb); | |
905c172f | 2339 | for (i = 0; i < nr; i++) { |
cd0ca2e0 | 2340 | struct io_kiocb *req = state->compl_reqs[i]; |
905c172f | 2341 | |
91c2f697 | 2342 | if (req_ref_put_and_test(req)) |
6ff119a6 | 2343 | io_req_free_batch(&rb, req, &ctx->submit_state); |
905c172f PB |
2344 | } |
2345 | ||
2346 | io_req_free_batch_finish(ctx, &rb); | |
cd0ca2e0 | 2347 | state->compl_nr = 0; |
7a743e22 PB |
2348 | } |
2349 | ||
ba816ad6 JA |
2350 | /* |
2351 | * Drop reference to request, return next in chain (if there is one) if this | |
2352 | * was the last reference to this request. | |
2353 | */ | |
0d85035a | 2354 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2355 | { |
9b5f7bd9 PB |
2356 | struct io_kiocb *nxt = NULL; |
2357 | ||
de9b4cca | 2358 | if (req_ref_put_and_test(req)) { |
9b5f7bd9 | 2359 | nxt = io_req_find_next(req); |
4d7dd462 | 2360 | __io_free_req(req); |
2a44f467 | 2361 | } |
9b5f7bd9 | 2362 | return nxt; |
2b188cc1 JA |
2363 | } |
2364 | ||
0d85035a | 2365 | static inline void io_put_req(struct io_kiocb *req) |
e65ef56d | 2366 | { |
de9b4cca | 2367 | if (req_ref_put_and_test(req)) |
e65ef56d | 2368 | io_free_req(req); |
2b188cc1 JA |
2369 | } |
2370 | ||
91c2f697 | 2371 | static inline void io_put_req_deferred(struct io_kiocb *req) |
216578e5 | 2372 | { |
91c2f697 | 2373 | if (req_ref_put_and_test(req)) { |
f237c30a | 2374 | req->io_task_work.func = io_free_req_work; |
543af3a1 PB |
2375 | io_req_task_work_add(req); |
2376 | } | |
216578e5 PB |
2377 | } |
2378 | ||
6c503150 | 2379 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2380 | { |
2381 | /* See comment at the top of this file */ | |
2382 | smp_rmb(); | |
e23de15f | 2383 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2384 | } |
2385 | ||
fb5ccc98 PB |
2386 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2387 | { | |
2388 | struct io_rings *rings = ctx->rings; | |
2389 | ||
2390 | /* make sure SQ entry isn't read before tail */ | |
2391 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2392 | } | |
2393 | ||
8ff069bf | 2394 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2395 | { |
8ff069bf | 2396 | unsigned int cflags; |
e94f141b | 2397 | |
bcda7baa JA |
2398 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2399 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2400 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2401 | kfree(kbuf); |
2402 | return cflags; | |
e94f141b JA |
2403 | } |
2404 | ||
8ff069bf | 2405 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2406 | { |
4d954c25 | 2407 | struct io_buffer *kbuf; |
bcda7baa | 2408 | |
ae421d93 PB |
2409 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
2410 | return 0; | |
4d954c25 | 2411 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
8ff069bf PB |
2412 | return io_put_kbuf(req, kbuf); |
2413 | } | |
2414 | ||
4c6e277c JA |
2415 | static inline bool io_run_task_work(void) |
2416 | { | |
ef98eb04 | 2417 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) { |
4c6e277c | 2418 | __set_current_state(TASK_RUNNING); |
ef98eb04 | 2419 | tracehook_notify_signal(); |
4c6e277c JA |
2420 | return true; |
2421 | } | |
2422 | ||
2423 | return false; | |
bcda7baa JA |
2424 | } |
2425 | ||
def596e9 JA |
2426 | /* |
2427 | * Find and free completed poll iocbs | |
2428 | */ | |
2429 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
a8576af9 | 2430 | struct list_head *done) |
def596e9 | 2431 | { |
8237e045 | 2432 | struct req_batch rb; |
def596e9 | 2433 | struct io_kiocb *req; |
bbde017a XW |
2434 | |
2435 | /* order with ->result store in io_complete_rw_iopoll() */ | |
2436 | smp_rmb(); | |
def596e9 | 2437 | |
5af1d13e | 2438 | io_init_req_batch(&rb); |
def596e9 | 2439 | while (!list_empty(done)) { |
d21ffe7e | 2440 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
f161340d PB |
2441 | list_del(&req->inflight_entry); |
2442 | ||
ae421d93 PB |
2443 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
2444 | io_put_rw_kbuf(req)); | |
def596e9 JA |
2445 | (*nr_events)++; |
2446 | ||
de9b4cca | 2447 | if (req_ref_put_and_test(req)) |
6ff119a6 | 2448 | io_req_free_batch(&rb, req, &ctx->submit_state); |
def596e9 | 2449 | } |
def596e9 | 2450 | |
09bb8394 | 2451 | io_commit_cqring(ctx); |
80c18e4a | 2452 | io_cqring_ev_posted_iopoll(ctx); |
2d6500d4 | 2453 | io_req_free_batch_finish(ctx, &rb); |
581f9810 BM |
2454 | } |
2455 | ||
def596e9 | 2456 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
a8576af9 | 2457 | long min) |
def596e9 JA |
2458 | { |
2459 | struct io_kiocb *req, *tmp; | |
d729cf9a | 2460 | unsigned int poll_flags = BLK_POLL_NOSLEEP; |
def596e9 | 2461 | LIST_HEAD(done); |
def596e9 JA |
2462 | |
2463 | /* | |
2464 | * Only spin for completions if we don't have multiple devices hanging | |
2465 | * off our complete list, and we're under the requested amount. | |
2466 | */ | |
ef99b2d3 CH |
2467 | if (ctx->poll_multi_queue || *nr_events >= min) |
2468 | poll_flags |= BLK_POLL_ONESHOT; | |
def596e9 | 2469 | |
d21ffe7e | 2470 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
9adbd45d | 2471 | struct kiocb *kiocb = &req->rw.kiocb; |
a2416e1e | 2472 | int ret; |
def596e9 JA |
2473 | |
2474 | /* | |
581f9810 BM |
2475 | * Move completed and retryable entries to our local lists. |
2476 | * If we find a request that requires polling, break out | |
2477 | * and complete those lists first, if we have entries there. | |
def596e9 | 2478 | */ |
65a6543d | 2479 | if (READ_ONCE(req->iopoll_completed)) { |
d21ffe7e | 2480 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2481 | continue; |
2482 | } | |
2483 | if (!list_empty(&done)) | |
2484 | break; | |
2485 | ||
ef99b2d3 | 2486 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, poll_flags); |
a2416e1e PB |
2487 | if (unlikely(ret < 0)) |
2488 | return ret; | |
2489 | else if (ret) | |
ef99b2d3 | 2490 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2491 | |
3aadc23e PB |
2492 | /* iopoll may have completed current req */ |
2493 | if (READ_ONCE(req->iopoll_completed)) | |
d21ffe7e | 2494 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2495 | } |
2496 | ||
2497 | if (!list_empty(&done)) | |
a8576af9 | 2498 | io_iopoll_complete(ctx, nr_events, &done); |
def596e9 | 2499 | |
a2416e1e | 2500 | return 0; |
def596e9 JA |
2501 | } |
2502 | ||
def596e9 JA |
2503 | /* |
2504 | * We can't just wait for polled events to come to us, we have to actively | |
2505 | * find and complete them. | |
2506 | */ | |
b2edc0a7 | 2507 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2508 | { |
2509 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2510 | return; | |
2511 | ||
2512 | mutex_lock(&ctx->uring_lock); | |
540e32a0 | 2513 | while (!list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2514 | unsigned int nr_events = 0; |
2515 | ||
a8576af9 | 2516 | io_do_iopoll(ctx, &nr_events, 0); |
08f5439f | 2517 | |
b2edc0a7 PB |
2518 | /* let it sleep and repeat later if can't complete a request */ |
2519 | if (nr_events == 0) | |
2520 | break; | |
08f5439f JA |
2521 | /* |
2522 | * Ensure we allow local-to-the-cpu processing to take place, | |
2523 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2524 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2525 | */ |
3fcee5a6 PB |
2526 | if (need_resched()) { |
2527 | mutex_unlock(&ctx->uring_lock); | |
2528 | cond_resched(); | |
2529 | mutex_lock(&ctx->uring_lock); | |
2530 | } | |
def596e9 JA |
2531 | } |
2532 | mutex_unlock(&ctx->uring_lock); | |
2533 | } | |
2534 | ||
7668b92a | 2535 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2536 | { |
7668b92a | 2537 | unsigned int nr_events = 0; |
e9979b36 | 2538 | int ret = 0; |
500f9fba | 2539 | |
c7849be9 XW |
2540 | /* |
2541 | * We disallow the app entering submit/complete with polling, but we | |
2542 | * still need to lock the ring to prevent racing with polled issue | |
2543 | * that got punted to a workqueue. | |
2544 | */ | |
2545 | mutex_lock(&ctx->uring_lock); | |
f39c8a5b PB |
2546 | /* |
2547 | * Don't enter poll loop if we already have events pending. | |
2548 | * If we do, we can potentially be spinning for commands that | |
2549 | * already triggered a CQE (eg in error). | |
2550 | */ | |
5ed7a37d | 2551 | if (test_bit(0, &ctx->check_cq_overflow)) |
f39c8a5b PB |
2552 | __io_cqring_overflow_flush(ctx, false); |
2553 | if (io_cqring_events(ctx)) | |
2554 | goto out; | |
def596e9 | 2555 | do { |
500f9fba JA |
2556 | /* |
2557 | * If a submit got punted to a workqueue, we can have the | |
2558 | * application entering polling for a command before it gets | |
2559 | * issued. That app will hold the uring_lock for the duration | |
2560 | * of the poll right here, so we need to take a breather every | |
2561 | * now and then to ensure that the issue has a chance to add | |
2562 | * the poll to the issued list. Otherwise we can spin here | |
2563 | * forever, while the workqueue is stuck trying to acquire the | |
2564 | * very same mutex. | |
2565 | */ | |
e9979b36 | 2566 | if (list_empty(&ctx->iopoll_list)) { |
8f487ef2 PB |
2567 | u32 tail = ctx->cached_cq_tail; |
2568 | ||
500f9fba | 2569 | mutex_unlock(&ctx->uring_lock); |
4c6e277c | 2570 | io_run_task_work(); |
500f9fba | 2571 | mutex_lock(&ctx->uring_lock); |
def596e9 | 2572 | |
8f487ef2 PB |
2573 | /* some requests don't go through iopoll_list */ |
2574 | if (tail != ctx->cached_cq_tail || | |
2575 | list_empty(&ctx->iopoll_list)) | |
e9979b36 | 2576 | break; |
500f9fba | 2577 | } |
a8576af9 | 2578 | ret = io_do_iopoll(ctx, &nr_events, min); |
f39c8a5b PB |
2579 | } while (!ret && nr_events < min && !need_resched()); |
2580 | out: | |
500f9fba | 2581 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2582 | return ret; |
2583 | } | |
2584 | ||
491381ce | 2585 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2586 | { |
491381ce JA |
2587 | /* |
2588 | * Tell lockdep we inherited freeze protection from submission | |
2589 | * thread. | |
2590 | */ | |
2591 | if (req->flags & REQ_F_ISREG) { | |
1c98679d | 2592 | struct super_block *sb = file_inode(req->file)->i_sb; |
2b188cc1 | 2593 | |
1c98679d PB |
2594 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
2595 | sb_end_write(sb); | |
2b188cc1 JA |
2596 | } |
2597 | } | |
2598 | ||
b63534c4 | 2599 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2600 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 | 2601 | { |
ab454438 | 2602 | struct io_async_rw *rw = req->async_data; |
b63534c4 | 2603 | |
ab454438 PB |
2604 | if (!rw) |
2605 | return !io_req_prep_async(req); | |
cd658695 | 2606 | iov_iter_restore(&rw->iter, &rw->iter_state); |
ab454438 | 2607 | return true; |
b63534c4 | 2608 | } |
b63534c4 | 2609 | |
3e6a0d3c | 2610 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 2611 | { |
355afaeb | 2612 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 2613 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 2614 | |
355afaeb JA |
2615 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2616 | return false; | |
3e6a0d3c JA |
2617 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
2618 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 2619 | return false; |
7c977a58 JA |
2620 | /* |
2621 | * If ref is dying, we might be running poll reap from the exit work. | |
2622 | * Don't attempt to reissue from that path, just let it fail with | |
2623 | * -EAGAIN. | |
2624 | */ | |
3e6a0d3c JA |
2625 | if (percpu_ref_is_dying(&ctx->refs)) |
2626 | return false; | |
ef046888 JA |
2627 | /* |
2628 | * Play it safe and assume not safe to re-import and reissue if we're | |
2629 | * not in the original thread group (or in task context). | |
2630 | */ | |
2631 | if (!same_thread_group(req->task, current) || !in_task()) | |
2632 | return false; | |
3e6a0d3c JA |
2633 | return true; |
2634 | } | |
e82ad485 | 2635 | #else |
a1ff1e3f | 2636 | static bool io_resubmit_prep(struct io_kiocb *req) |
e82ad485 JA |
2637 | { |
2638 | return false; | |
2639 | } | |
e82ad485 | 2640 | static bool io_rw_should_reissue(struct io_kiocb *req) |
3e6a0d3c | 2641 | { |
b63534c4 JA |
2642 | return false; |
2643 | } | |
3e6a0d3c | 2644 | #endif |
b63534c4 | 2645 | |
8ef12efe | 2646 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
a1d7c393 | 2647 | { |
b65c128f PB |
2648 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
2649 | kiocb_end_write(req); | |
9532b99b PB |
2650 | if (res != req->result) { |
2651 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && | |
2652 | io_rw_should_reissue(req)) { | |
2653 | req->flags |= REQ_F_REISSUE; | |
8ef12efe | 2654 | return true; |
9532b99b | 2655 | } |
93d2bcd2 | 2656 | req_set_fail(req); |
8ef12efe | 2657 | req->result = res; |
9532b99b | 2658 | } |
8ef12efe JA |
2659 | return false; |
2660 | } | |
2661 | ||
f237c30a | 2662 | static void io_req_task_complete(struct io_kiocb *req, bool *locked) |
8ef12efe | 2663 | { |
126180b9 PB |
2664 | unsigned int cflags = io_put_rw_kbuf(req); |
2665 | long res = req->result; | |
2666 | ||
2667 | if (*locked) { | |
2668 | struct io_ring_ctx *ctx = req->ctx; | |
2669 | struct io_submit_state *state = &ctx->submit_state; | |
2670 | ||
2671 | io_req_complete_state(req, res, cflags); | |
2672 | state->compl_reqs[state->compl_nr++] = req; | |
2673 | if (state->compl_nr == ARRAY_SIZE(state->compl_reqs)) | |
2674 | io_submit_flush_completions(ctx); | |
2675 | } else { | |
2676 | io_req_complete_post(req, res, cflags); | |
2677 | } | |
8ef12efe JA |
2678 | } |
2679 | ||
2680 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, | |
2681 | unsigned int issue_flags) | |
2682 | { | |
2683 | if (__io_complete_rw_common(req, res)) | |
2684 | return; | |
63637853 | 2685 | __io_req_complete(req, issue_flags, req->result, io_put_rw_kbuf(req)); |
ba816ad6 JA |
2686 | } |
2687 | ||
2688 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2689 | { | |
9adbd45d | 2690 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2691 | |
8ef12efe JA |
2692 | if (__io_complete_rw_common(req, res)) |
2693 | return; | |
2694 | req->result = res; | |
2695 | req->io_task_work.func = io_req_task_complete; | |
2696 | io_req_task_work_add(req); | |
2b188cc1 JA |
2697 | } |
2698 | ||
def596e9 JA |
2699 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2700 | { | |
9adbd45d | 2701 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2702 | |
491381ce JA |
2703 | if (kiocb->ki_flags & IOCB_WRITE) |
2704 | kiocb_end_write(req); | |
9532b99b | 2705 | if (unlikely(res != req->result)) { |
b66ceaf3 PB |
2706 | if (res == -EAGAIN && io_rw_should_reissue(req)) { |
2707 | req->flags |= REQ_F_REISSUE; | |
2708 | return; | |
9532b99b | 2709 | } |
8c130827 | 2710 | } |
bbde017a XW |
2711 | |
2712 | WRITE_ONCE(req->result, res); | |
b9b0e0d3 | 2713 | /* order with io_iopoll_complete() checking ->result */ |
cd664b0e PB |
2714 | smp_wmb(); |
2715 | WRITE_ONCE(req->iopoll_completed, 1); | |
def596e9 JA |
2716 | } |
2717 | ||
2718 | /* | |
2719 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2720 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
f39c8a5b | 2721 | * find it from a io_do_iopoll() thread before the issuer is done |
def596e9 JA |
2722 | * accessing the kiocb cookie. |
2723 | */ | |
cb3d8972 | 2724 | static void io_iopoll_req_issued(struct io_kiocb *req) |
def596e9 JA |
2725 | { |
2726 | struct io_ring_ctx *ctx = req->ctx; | |
cb3d8972 PB |
2727 | const bool in_async = io_wq_current_is_worker(); |
2728 | ||
2729 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
2730 | if (unlikely(in_async)) | |
2731 | mutex_lock(&ctx->uring_lock); | |
def596e9 JA |
2732 | |
2733 | /* | |
2734 | * Track whether we have multiple files in our lists. This will impact | |
2735 | * how we do polling eventually, not spinning if we're on potentially | |
2736 | * different devices. | |
2737 | */ | |
540e32a0 | 2738 | if (list_empty(&ctx->iopoll_list)) { |
915b3dde HX |
2739 | ctx->poll_multi_queue = false; |
2740 | } else if (!ctx->poll_multi_queue) { | |
def596e9 JA |
2741 | struct io_kiocb *list_req; |
2742 | ||
540e32a0 | 2743 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
d21ffe7e | 2744 | inflight_entry); |
915b3dde | 2745 | |
30da1b45 | 2746 | if (list_req->file != req->file) |
915b3dde | 2747 | ctx->poll_multi_queue = true; |
def596e9 JA |
2748 | } |
2749 | ||
2750 | /* | |
2751 | * For fast devices, IO may have already completed. If it has, add | |
2752 | * it to the front so we find it first. | |
2753 | */ | |
65a6543d | 2754 | if (READ_ONCE(req->iopoll_completed)) |
d21ffe7e | 2755 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
def596e9 | 2756 | else |
d21ffe7e | 2757 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
bdcd3eab | 2758 | |
cb3d8972 PB |
2759 | if (unlikely(in_async)) { |
2760 | /* | |
2761 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle | |
2762 | * in sq thread task context or in io worker task context. If | |
2763 | * current task context is sq thread, we don't need to check | |
2764 | * whether should wake up sq thread. | |
2765 | */ | |
2766 | if ((ctx->flags & IORING_SETUP_SQPOLL) && | |
2767 | wq_has_sleeper(&ctx->sq_data->wait)) | |
2768 | wake_up(&ctx->sq_data->wait); | |
2769 | ||
2770 | mutex_unlock(&ctx->uring_lock); | |
2771 | } | |
def596e9 JA |
2772 | } |
2773 | ||
4503b767 JA |
2774 | static bool io_bdev_nowait(struct block_device *bdev) |
2775 | { | |
9ba0d0c8 | 2776 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2777 | } |
2778 | ||
2b188cc1 JA |
2779 | /* |
2780 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2781 | * any file. For now, just ensure that anything potentially problematic is done | |
2782 | * inline. | |
2783 | */ | |
b191e2df | 2784 | static bool __io_file_supports_nowait(struct file *file, int rw) |
2b188cc1 JA |
2785 | { |
2786 | umode_t mode = file_inode(file)->i_mode; | |
2787 | ||
4503b767 | 2788 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2789 | if (IS_ENABLED(CONFIG_BLOCK) && |
2790 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2791 | return true; |
2792 | return false; | |
2793 | } | |
976517f1 | 2794 | if (S_ISSOCK(mode)) |
2b188cc1 | 2795 | return true; |
4503b767 | 2796 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2797 | if (IS_ENABLED(CONFIG_BLOCK) && |
2798 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2799 | file->f_op != &io_uring_fops) |
2800 | return true; | |
2801 | return false; | |
2802 | } | |
2b188cc1 | 2803 | |
c5b85625 JA |
2804 | /* any ->read/write should understand O_NONBLOCK */ |
2805 | if (file->f_flags & O_NONBLOCK) | |
2806 | return true; | |
2807 | ||
af197f50 JA |
2808 | if (!(file->f_mode & FMODE_NOWAIT)) |
2809 | return false; | |
2810 | ||
2811 | if (rw == READ) | |
2812 | return file->f_op->read_iter != NULL; | |
2813 | ||
2814 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
2815 | } |
2816 | ||
b191e2df | 2817 | static bool io_file_supports_nowait(struct io_kiocb *req, int rw) |
7b29f92d | 2818 | { |
b191e2df | 2819 | if (rw == READ && (req->flags & REQ_F_NOWAIT_READ)) |
7b29f92d | 2820 | return true; |
b191e2df | 2821 | else if (rw == WRITE && (req->flags & REQ_F_NOWAIT_WRITE)) |
7b29f92d JA |
2822 | return true; |
2823 | ||
b191e2df | 2824 | return __io_file_supports_nowait(req->file, rw); |
7b29f92d JA |
2825 | } |
2826 | ||
5d329e12 JA |
2827 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2828 | int rw) | |
2b188cc1 | 2829 | { |
def596e9 | 2830 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2831 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 2832 | struct file *file = req->file; |
09bb8394 JA |
2833 | unsigned ioprio; |
2834 | int ret; | |
2b188cc1 | 2835 | |
c97d8a0f | 2836 | if (!io_req_ffs_set(req) && S_ISREG(file_inode(file)->i_mode)) |
491381ce JA |
2837 | req->flags |= REQ_F_ISREG; |
2838 | ||
2b188cc1 | 2839 | kiocb->ki_pos = READ_ONCE(sqe->off); |
75c668cd | 2840 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
ba04291e | 2841 | req->flags |= REQ_F_CUR_POS; |
75c668cd | 2842 | kiocb->ki_pos = file->f_pos; |
ba04291e | 2843 | } |
2b188cc1 | 2844 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
2845 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
2846 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
2847 | if (unlikely(ret)) | |
2848 | return ret; | |
2b188cc1 | 2849 | |
5d329e12 JA |
2850 | /* |
2851 | * If the file is marked O_NONBLOCK, still allow retry for it if it | |
2852 | * supports async. Otherwise it's impossible to use O_NONBLOCK files | |
2853 | * reliably. If not, or it IOCB_NOWAIT is set, don't retry. | |
2854 | */ | |
2855 | if ((kiocb->ki_flags & IOCB_NOWAIT) || | |
2856 | ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req, rw))) | |
75c668cd PB |
2857 | req->flags |= REQ_F_NOWAIT; |
2858 | ||
2b188cc1 JA |
2859 | ioprio = READ_ONCE(sqe->ioprio); |
2860 | if (ioprio) { | |
2861 | ret = ioprio_check_cap(ioprio); | |
2862 | if (ret) | |
09bb8394 | 2863 | return ret; |
2b188cc1 JA |
2864 | |
2865 | kiocb->ki_ioprio = ioprio; | |
2866 | } else | |
2867 | kiocb->ki_ioprio = get_current_ioprio(); | |
2868 | ||
def596e9 | 2869 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
2870 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
2871 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 2872 | return -EOPNOTSUPP; |
2b188cc1 | 2873 | |
394918eb | 2874 | kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE; |
def596e9 | 2875 | kiocb->ki_complete = io_complete_rw_iopoll; |
65a6543d | 2876 | req->iopoll_completed = 0; |
def596e9 | 2877 | } else { |
09bb8394 JA |
2878 | if (kiocb->ki_flags & IOCB_HIPRI) |
2879 | return -EINVAL; | |
def596e9 JA |
2880 | kiocb->ki_complete = io_complete_rw; |
2881 | } | |
9adbd45d | 2882 | |
eae071c9 PB |
2883 | if (req->opcode == IORING_OP_READ_FIXED || |
2884 | req->opcode == IORING_OP_WRITE_FIXED) { | |
2885 | req->imu = NULL; | |
2886 | io_req_set_rsrc_node(req); | |
2887 | } | |
2888 | ||
3529d8c2 JA |
2889 | req->rw.addr = READ_ONCE(sqe->addr); |
2890 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2891 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2892 | return 0; |
2b188cc1 JA |
2893 | } |
2894 | ||
2895 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2896 | { | |
2897 | switch (ret) { | |
2898 | case -EIOCBQUEUED: | |
2899 | break; | |
2900 | case -ERESTARTSYS: | |
2901 | case -ERESTARTNOINTR: | |
2902 | case -ERESTARTNOHAND: | |
2903 | case -ERESTART_RESTARTBLOCK: | |
2904 | /* | |
2905 | * We can't just restart the syscall, since previously | |
2906 | * submitted sqes may already be in progress. Just fail this | |
2907 | * IO with EINTR. | |
2908 | */ | |
2909 | ret = -EINTR; | |
df561f66 | 2910 | fallthrough; |
2b188cc1 JA |
2911 | default: |
2912 | kiocb->ki_complete(kiocb, ret, 0); | |
2913 | } | |
2914 | } | |
2915 | ||
a1d7c393 | 2916 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
889fca73 | 2917 | unsigned int issue_flags) |
ba816ad6 | 2918 | { |
ba04291e | 2919 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2920 | struct io_async_rw *io = req->async_data; |
ba04291e | 2921 | |
227c0c96 | 2922 | /* add previously done IO, if any */ |
e8c2bc1f | 2923 | if (io && io->bytes_done > 0) { |
227c0c96 | 2924 | if (ret < 0) |
e8c2bc1f | 2925 | ret = io->bytes_done; |
227c0c96 | 2926 | else |
e8c2bc1f | 2927 | ret += io->bytes_done; |
227c0c96 JA |
2928 | } |
2929 | ||
ba04291e JA |
2930 | if (req->flags & REQ_F_CUR_POS) |
2931 | req->file->f_pos = kiocb->ki_pos; | |
b66ceaf3 | 2932 | if (ret >= 0 && (kiocb->ki_complete == io_complete_rw)) |
889fca73 | 2933 | __io_complete_rw(req, ret, 0, issue_flags); |
ba816ad6 JA |
2934 | else |
2935 | io_rw_done(kiocb, ret); | |
97284637 | 2936 | |
b66ceaf3 | 2937 | if (req->flags & REQ_F_REISSUE) { |
97284637 | 2938 | req->flags &= ~REQ_F_REISSUE; |
a7be7c23 | 2939 | if (io_resubmit_prep(req)) { |
773af691 | 2940 | io_req_task_queue_reissue(req); |
8c130827 | 2941 | } else { |
b66ceaf3 PB |
2942 | unsigned int cflags = io_put_rw_kbuf(req); |
2943 | struct io_ring_ctx *ctx = req->ctx; | |
2944 | ||
93d2bcd2 | 2945 | req_set_fail(req); |
14cfbb7a | 2946 | if (!(issue_flags & IO_URING_F_NONBLOCK)) { |
b66ceaf3 PB |
2947 | mutex_lock(&ctx->uring_lock); |
2948 | __io_req_complete(req, issue_flags, ret, cflags); | |
2949 | mutex_unlock(&ctx->uring_lock); | |
2950 | } else { | |
2951 | __io_req_complete(req, issue_flags, ret, cflags); | |
2952 | } | |
97284637 PB |
2953 | } |
2954 | } | |
ba816ad6 JA |
2955 | } |
2956 | ||
eae071c9 PB |
2957 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
2958 | struct io_mapped_ubuf *imu) | |
edafccee | 2959 | { |
9adbd45d | 2960 | size_t len = req->rw.len; |
75769e3f | 2961 | u64 buf_end, buf_addr = req->rw.addr; |
edafccee | 2962 | size_t offset; |
edafccee | 2963 | |
75769e3f | 2964 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
edafccee JA |
2965 | return -EFAULT; |
2966 | /* not inside the mapped region */ | |
4751f53d | 2967 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
edafccee JA |
2968 | return -EFAULT; |
2969 | ||
2970 | /* | |
2971 | * May not be a start of buffer, set size appropriately | |
2972 | * and advance us to the beginning. | |
2973 | */ | |
2974 | offset = buf_addr - imu->ubuf; | |
2975 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
2976 | |
2977 | if (offset) { | |
2978 | /* | |
2979 | * Don't use iov_iter_advance() here, as it's really slow for | |
2980 | * using the latter parts of a big fixed buffer - it iterates | |
2981 | * over each segment manually. We can cheat a bit here, because | |
2982 | * we know that: | |
2983 | * | |
2984 | * 1) it's a BVEC iter, we set it up | |
2985 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
2986 | * first and last bvec | |
2987 | * | |
2988 | * So just find our index, and adjust the iterator afterwards. | |
2989 | * If the offset is within the first bvec (or the whole first | |
2990 | * bvec, just use iov_iter_advance(). This makes it easier | |
2991 | * since we can just skip the first segment, which may not | |
2992 | * be PAGE_SIZE aligned. | |
2993 | */ | |
2994 | const struct bio_vec *bvec = imu->bvec; | |
2995 | ||
2996 | if (offset <= bvec->bv_len) { | |
2997 | iov_iter_advance(iter, offset); | |
2998 | } else { | |
2999 | unsigned long seg_skip; | |
3000 | ||
3001 | /* skip first vec */ | |
3002 | offset -= bvec->bv_len; | |
3003 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
3004 | ||
3005 | iter->bvec = bvec + seg_skip; | |
3006 | iter->nr_segs -= seg_skip; | |
99c79f66 | 3007 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 3008 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
3009 | } |
3010 | } | |
3011 | ||
847595de | 3012 | return 0; |
edafccee JA |
3013 | } |
3014 | ||
eae071c9 PB |
3015 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
3016 | { | |
3017 | struct io_ring_ctx *ctx = req->ctx; | |
3018 | struct io_mapped_ubuf *imu = req->imu; | |
3019 | u16 index, buf_index = req->buf_index; | |
3020 | ||
3021 | if (likely(!imu)) { | |
3022 | if (unlikely(buf_index >= ctx->nr_user_bufs)) | |
3023 | return -EFAULT; | |
3024 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); | |
3025 | imu = READ_ONCE(ctx->user_bufs[index]); | |
3026 | req->imu = imu; | |
3027 | } | |
3028 | return __io_import_fixed(req, rw, iter, imu); | |
3029 | } | |
3030 | ||
bcda7baa JA |
3031 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
3032 | { | |
3033 | if (needs_lock) | |
3034 | mutex_unlock(&ctx->uring_lock); | |
3035 | } | |
3036 | ||
3037 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
3038 | { | |
3039 | /* | |
3040 | * "Normal" inline submissions always hold the uring_lock, since we | |
3041 | * grab it from the system call. Same is true for the SQPOLL offload. | |
3042 | * The only exception is when we've detached the request and issue it | |
3043 | * from an async worker thread, grab the lock for that case. | |
3044 | */ | |
3045 | if (needs_lock) | |
3046 | mutex_lock(&ctx->uring_lock); | |
3047 | } | |
3048 | ||
3049 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
3050 | int bgid, struct io_buffer *kbuf, | |
3051 | bool needs_lock) | |
3052 | { | |
3053 | struct io_buffer *head; | |
3054 | ||
3055 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3056 | return kbuf; | |
3057 | ||
3058 | io_ring_submit_lock(req->ctx, needs_lock); | |
3059 | ||
3060 | lockdep_assert_held(&req->ctx->uring_lock); | |
3061 | ||
9e15c3a0 | 3062 | head = xa_load(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
3063 | if (head) { |
3064 | if (!list_empty(&head->list)) { | |
3065 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
3066 | list); | |
3067 | list_del(&kbuf->list); | |
3068 | } else { | |
3069 | kbuf = head; | |
9e15c3a0 | 3070 | xa_erase(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
3071 | } |
3072 | if (*len > kbuf->len) | |
3073 | *len = kbuf->len; | |
3074 | } else { | |
3075 | kbuf = ERR_PTR(-ENOBUFS); | |
3076 | } | |
3077 | ||
3078 | io_ring_submit_unlock(req->ctx, needs_lock); | |
3079 | ||
3080 | return kbuf; | |
3081 | } | |
3082 | ||
4d954c25 JA |
3083 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
3084 | bool needs_lock) | |
3085 | { | |
3086 | struct io_buffer *kbuf; | |
4f4eeba8 | 3087 | u16 bgid; |
4d954c25 JA |
3088 | |
3089 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
4f4eeba8 | 3090 | bgid = req->buf_index; |
4d954c25 JA |
3091 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
3092 | if (IS_ERR(kbuf)) | |
3093 | return kbuf; | |
3094 | req->rw.addr = (u64) (unsigned long) kbuf; | |
3095 | req->flags |= REQ_F_BUFFER_SELECTED; | |
3096 | return u64_to_user_ptr(kbuf->addr); | |
3097 | } | |
3098 | ||
3099 | #ifdef CONFIG_COMPAT | |
3100 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
3101 | bool needs_lock) | |
3102 | { | |
3103 | struct compat_iovec __user *uiov; | |
3104 | compat_ssize_t clen; | |
3105 | void __user *buf; | |
3106 | ssize_t len; | |
3107 | ||
3108 | uiov = u64_to_user_ptr(req->rw.addr); | |
3109 | if (!access_ok(uiov, sizeof(*uiov))) | |
3110 | return -EFAULT; | |
3111 | if (__get_user(clen, &uiov->iov_len)) | |
3112 | return -EFAULT; | |
3113 | if (clen < 0) | |
3114 | return -EINVAL; | |
3115 | ||
3116 | len = clen; | |
3117 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3118 | if (IS_ERR(buf)) | |
3119 | return PTR_ERR(buf); | |
3120 | iov[0].iov_base = buf; | |
3121 | iov[0].iov_len = (compat_size_t) len; | |
3122 | return 0; | |
3123 | } | |
3124 | #endif | |
3125 | ||
3126 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3127 | bool needs_lock) | |
3128 | { | |
3129 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3130 | void __user *buf; | |
3131 | ssize_t len; | |
3132 | ||
3133 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3134 | return -EFAULT; | |
3135 | ||
3136 | len = iov[0].iov_len; | |
3137 | if (len < 0) | |
3138 | return -EINVAL; | |
3139 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3140 | if (IS_ERR(buf)) | |
3141 | return PTR_ERR(buf); | |
3142 | iov[0].iov_base = buf; | |
3143 | iov[0].iov_len = len; | |
3144 | return 0; | |
3145 | } | |
3146 | ||
3147 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3148 | bool needs_lock) | |
3149 | { | |
dddb3e26 JA |
3150 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
3151 | struct io_buffer *kbuf; | |
3152 | ||
3153 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
3154 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); | |
3155 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3156 | return 0; |
dddb3e26 | 3157 | } |
dd201662 | 3158 | if (req->rw.len != 1) |
4d954c25 JA |
3159 | return -EINVAL; |
3160 | ||
3161 | #ifdef CONFIG_COMPAT | |
3162 | if (req->ctx->compat) | |
3163 | return io_compat_import(req, iov, needs_lock); | |
3164 | #endif | |
3165 | ||
3166 | return __io_iov_buffer_select(req, iov, needs_lock); | |
3167 | } | |
3168 | ||
847595de PB |
3169 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
3170 | struct iov_iter *iter, bool needs_lock) | |
2b188cc1 | 3171 | { |
9adbd45d JA |
3172 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
3173 | size_t sqe_len = req->rw.len; | |
847595de | 3174 | u8 opcode = req->opcode; |
4d954c25 | 3175 | ssize_t ret; |
edafccee | 3176 | |
7d009165 | 3177 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 3178 | *iovec = NULL; |
9adbd45d | 3179 | return io_import_fixed(req, rw, iter); |
edafccee | 3180 | } |
2b188cc1 | 3181 | |
bcda7baa | 3182 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 3183 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
3184 | return -EINVAL; |
3185 | ||
3a6820f2 | 3186 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3187 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 3188 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 3189 | if (IS_ERR(buf)) |
4d954c25 | 3190 | return PTR_ERR(buf); |
3f9d6441 | 3191 | req->rw.len = sqe_len; |
bcda7baa JA |
3192 | } |
3193 | ||
3a6820f2 JA |
3194 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
3195 | *iovec = NULL; | |
10fc72e4 | 3196 | return ret; |
3a6820f2 JA |
3197 | } |
3198 | ||
4d954c25 JA |
3199 | if (req->flags & REQ_F_BUFFER_SELECT) { |
3200 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
847595de PB |
3201 | if (!ret) |
3202 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); | |
4d954c25 JA |
3203 | *iovec = NULL; |
3204 | return ret; | |
3205 | } | |
3206 | ||
89cd35c5 CH |
3207 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
3208 | req->ctx->compat); | |
2b188cc1 JA |
3209 | } |
3210 | ||
0fef9483 JA |
3211 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3212 | { | |
5b09e37e | 3213 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3214 | } |
3215 | ||
31b51510 | 3216 | /* |
32960613 JA |
3217 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3218 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3219 | */ |
4017eb91 | 3220 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3221 | { |
4017eb91 JA |
3222 | struct kiocb *kiocb = &req->rw.kiocb; |
3223 | struct file *file = req->file; | |
32960613 JA |
3224 | ssize_t ret = 0; |
3225 | ||
3226 | /* | |
3227 | * Don't support polled IO through this interface, and we can't | |
3228 | * support non-blocking either. For the latter, this just causes | |
3229 | * the kiocb to be handled from an async context. | |
3230 | */ | |
3231 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3232 | return -EOPNOTSUPP; | |
3233 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3234 | return -EAGAIN; | |
3235 | ||
3236 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3237 | struct iovec iovec; |
32960613 JA |
3238 | ssize_t nr; |
3239 | ||
311ae9e1 PB |
3240 | if (!iov_iter_is_bvec(iter)) { |
3241 | iovec = iov_iter_iovec(iter); | |
3242 | } else { | |
4017eb91 JA |
3243 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3244 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3245 | } |
3246 | ||
32960613 JA |
3247 | if (rw == READ) { |
3248 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3249 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3250 | } else { |
3251 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3252 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3253 | } |
3254 | ||
3255 | if (nr < 0) { | |
3256 | if (!ret) | |
3257 | ret = nr; | |
3258 | break; | |
3259 | } | |
16c8d2df JA |
3260 | if (!iov_iter_is_bvec(iter)) { |
3261 | iov_iter_advance(iter, nr); | |
3262 | } else { | |
3263 | req->rw.len -= nr; | |
3264 | req->rw.addr += nr; | |
3265 | } | |
32960613 JA |
3266 | ret += nr; |
3267 | if (nr != iovec.iov_len) | |
3268 | break; | |
32960613 JA |
3269 | } |
3270 | ||
3271 | return ret; | |
3272 | } | |
3273 | ||
ff6165b2 JA |
3274 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3275 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3276 | { |
e8c2bc1f | 3277 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3278 | |
ff6165b2 | 3279 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3280 | rw->free_iovec = iovec; |
227c0c96 | 3281 | rw->bytes_done = 0; |
ff6165b2 | 3282 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3283 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3284 | return; |
b64e3444 | 3285 | if (!iovec) { |
ff6165b2 JA |
3286 | unsigned iov_off = 0; |
3287 | ||
3288 | rw->iter.iov = rw->fast_iov; | |
3289 | if (iter->iov != fast_iov) { | |
3290 | iov_off = iter->iov - fast_iov; | |
3291 | rw->iter.iov += iov_off; | |
3292 | } | |
3293 | if (rw->fast_iov != fast_iov) | |
3294 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3295 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3296 | } else { |
3297 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3298 | } |
3299 | } | |
3300 | ||
6cb78689 | 3301 | static inline int io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3302 | { |
e8c2bc1f JA |
3303 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3304 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3305 | return req->async_data == NULL; | |
3d9932a8 XW |
3306 | } |
3307 | ||
ff6165b2 JA |
3308 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3309 | const struct iovec *fast_iov, | |
227c0c96 | 3310 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3311 | { |
26f0505a | 3312 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
74566df3 | 3313 | return 0; |
e8c2bc1f | 3314 | if (!req->async_data) { |
cd658695 JA |
3315 | struct io_async_rw *iorw; |
3316 | ||
6cb78689 | 3317 | if (io_alloc_async_data(req)) { |
6bf985dc | 3318 | kfree(iovec); |
5d204bcf | 3319 | return -ENOMEM; |
6bf985dc | 3320 | } |
b7bb4f7d | 3321 | |
ff6165b2 | 3322 | io_req_map_rw(req, iovec, fast_iov, iter); |
cd658695 JA |
3323 | iorw = req->async_data; |
3324 | /* we've copied and mapped the iter, ensure state is saved */ | |
3325 | iov_iter_save_state(&iorw->iter, &iorw->iter_state); | |
5d204bcf | 3326 | } |
b7bb4f7d | 3327 | return 0; |
f67676d1 JA |
3328 | } |
3329 | ||
73debe68 | 3330 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3331 | { |
e8c2bc1f | 3332 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3333 | struct iovec *iov = iorw->fast_iov; |
847595de | 3334 | int ret; |
c3e330a4 | 3335 | |
2846c481 | 3336 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
c3e330a4 PB |
3337 | if (unlikely(ret < 0)) |
3338 | return ret; | |
3339 | ||
ab0b196c PB |
3340 | iorw->bytes_done = 0; |
3341 | iorw->free_iovec = iov; | |
3342 | if (iov) | |
3343 | req->flags |= REQ_F_NEED_CLEANUP; | |
cd658695 | 3344 | iov_iter_save_state(&iorw->iter, &iorw->iter_state); |
c3e330a4 PB |
3345 | return 0; |
3346 | } | |
3347 | ||
73debe68 | 3348 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3349 | { |
3529d8c2 JA |
3350 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3351 | return -EBADF; | |
5d329e12 | 3352 | return io_prep_rw(req, sqe, READ); |
f67676d1 JA |
3353 | } |
3354 | ||
c1dd91d1 JA |
3355 | /* |
3356 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3357 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3358 | * This gets called when the page is unlocked, and we generally expect that to | |
3359 | * happen when the page IO is completed and the page is now uptodate. This will | |
3360 | * queue a task_work based retry of the operation, attempting to copy the data | |
3361 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3362 | * do a thread based blocking retry of the operation. That's the unexpected | |
3363 | * slow path. | |
3364 | */ | |
bcf5a063 JA |
3365 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3366 | int sync, void *arg) | |
3367 | { | |
3368 | struct wait_page_queue *wpq; | |
3369 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3370 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3371 | |
3372 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3373 | ||
cdc8fcb4 LT |
3374 | if (!wake_page_match(wpq, key)) |
3375 | return 0; | |
3376 | ||
c8d317aa | 3377 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 | 3378 | list_del_init(&wait->entry); |
921b9054 | 3379 | io_req_task_queue(req); |
bcf5a063 JA |
3380 | return 1; |
3381 | } | |
3382 | ||
c1dd91d1 JA |
3383 | /* |
3384 | * This controls whether a given IO request should be armed for async page | |
3385 | * based retry. If we return false here, the request is handed to the async | |
3386 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3387 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3388 | * will either succeed because the page is now uptodate and unlocked, or it | |
3389 | * will register a callback when the page is unlocked at IO completion. Through | |
3390 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3391 | * That retry will attempt the buffered read again. The retry will generally | |
3392 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3393 | * async worker threads for a blocking retry. | |
3394 | */ | |
227c0c96 | 3395 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3396 | { |
e8c2bc1f JA |
3397 | struct io_async_rw *rw = req->async_data; |
3398 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3399 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3400 | |
bcf5a063 JA |
3401 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3402 | if (req->flags & REQ_F_NOWAIT) | |
3403 | return false; | |
f67676d1 | 3404 | |
227c0c96 | 3405 | /* Only for buffered IO */ |
3b2a4439 | 3406 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3407 | return false; |
3b2a4439 | 3408 | |
bcf5a063 JA |
3409 | /* |
3410 | * just use poll if we can, and don't attempt if the fs doesn't | |
3411 | * support callback based unlocks | |
3412 | */ | |
3413 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3414 | return false; | |
f67676d1 | 3415 | |
3b2a4439 JA |
3416 | wait->wait.func = io_async_buf_func; |
3417 | wait->wait.private = req; | |
3418 | wait->wait.flags = 0; | |
3419 | INIT_LIST_HEAD(&wait->wait.entry); | |
3420 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3421 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3422 | kiocb->ki_waitq = wait; |
3b2a4439 | 3423 | return true; |
bcf5a063 JA |
3424 | } |
3425 | ||
aeab9506 | 3426 | static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
bcf5a063 JA |
3427 | { |
3428 | if (req->file->f_op->read_iter) | |
3429 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d | 3430 | else if (req->file->f_op->read) |
4017eb91 | 3431 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3432 | else |
3433 | return -EINVAL; | |
f67676d1 JA |
3434 | } |
3435 | ||
7db30437 ML |
3436 | static bool need_read_all(struct io_kiocb *req) |
3437 | { | |
3438 | return req->flags & REQ_F_ISREG || | |
3439 | S_ISBLK(file_inode(req->file)->i_mode); | |
3440 | } | |
3441 | ||
889fca73 | 3442 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3443 | { |
3444 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3445 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3446 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3447 | struct io_async_rw *rw = req->async_data; |
45d189c6 | 3448 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
cd658695 JA |
3449 | struct iov_iter_state __state, *state; |
3450 | ssize_t ret, ret2; | |
ff6165b2 | 3451 | |
2846c481 | 3452 | if (rw) { |
e8c2bc1f | 3453 | iter = &rw->iter; |
cd658695 JA |
3454 | state = &rw->iter_state; |
3455 | /* | |
3456 | * We come here from an earlier attempt, restore our state to | |
3457 | * match in case it doesn't. It's cheap enough that we don't | |
3458 | * need to make this conditional. | |
3459 | */ | |
3460 | iov_iter_restore(iter, state); | |
2846c481 PB |
3461 | iovec = NULL; |
3462 | } else { | |
3463 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); | |
3464 | if (ret < 0) | |
3465 | return ret; | |
cd658695 JA |
3466 | state = &__state; |
3467 | iov_iter_save_state(iter, state); | |
2846c481 | 3468 | } |
cd658695 | 3469 | req->result = iov_iter_count(iter); |
2b188cc1 | 3470 | |
fd6c2e4c JA |
3471 | /* Ensure we clear previously set non-block flag */ |
3472 | if (!force_nonblock) | |
29de5f6a | 3473 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
a88fc400 PB |
3474 | else |
3475 | kiocb->ki_flags |= IOCB_NOWAIT; | |
3476 | ||
24c74678 | 3477 | /* If the file doesn't support async, just async punt */ |
b191e2df | 3478 | if (force_nonblock && !io_file_supports_nowait(req, READ)) { |
6713e7a6 | 3479 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc | 3480 | return ret ?: -EAGAIN; |
6713e7a6 | 3481 | } |
9e645e11 | 3482 | |
cd658695 | 3483 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), req->result); |
5ea5dd45 PB |
3484 | if (unlikely(ret)) { |
3485 | kfree(iovec); | |
3486 | return ret; | |
3487 | } | |
2b188cc1 | 3488 | |
227c0c96 | 3489 | ret = io_iter_do_read(req, iter); |
32960613 | 3490 | |
230d50d4 | 3491 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
6ad7f233 | 3492 | req->flags &= ~REQ_F_REISSUE; |
eefdf30f JA |
3493 | /* IOPOLL retry should happen for io-wq threads */ |
3494 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3495 | goto done; |
75c668cd PB |
3496 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3497 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3498 | goto done; |
f38c7e3a | 3499 | ret = 0; |
230d50d4 JA |
3500 | } else if (ret == -EIOCBQUEUED) { |
3501 | goto out_free; | |
cd658695 | 3502 | } else if (ret <= 0 || ret == req->result || !force_nonblock || |
7db30437 | 3503 | (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) { |
7335e3bf | 3504 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3505 | goto done; |
227c0c96 JA |
3506 | } |
3507 | ||
cd658695 JA |
3508 | /* |
3509 | * Don't depend on the iter state matching what was consumed, or being | |
3510 | * untouched in case of error. Restore it and we'll advance it | |
3511 | * manually if we need to. | |
3512 | */ | |
3513 | iov_iter_restore(iter, state); | |
3514 | ||
227c0c96 | 3515 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc PB |
3516 | if (ret2) |
3517 | return ret2; | |
3518 | ||
fe1cdd55 | 3519 | iovec = NULL; |
e8c2bc1f | 3520 | rw = req->async_data; |
cd658695 JA |
3521 | /* |
3522 | * Now use our persistent iterator and state, if we aren't already. | |
3523 | * We've restored and mapped the iter to match. | |
3524 | */ | |
3525 | if (iter != &rw->iter) { | |
3526 | iter = &rw->iter; | |
3527 | state = &rw->iter_state; | |
3528 | } | |
227c0c96 | 3529 | |
b23df91b | 3530 | do { |
cd658695 JA |
3531 | /* |
3532 | * We end up here because of a partial read, either from | |
3533 | * above or inside this loop. Advance the iter by the bytes | |
3534 | * that were consumed. | |
3535 | */ | |
3536 | iov_iter_advance(iter, ret); | |
3537 | if (!iov_iter_count(iter)) | |
3538 | break; | |
b23df91b | 3539 | rw->bytes_done += ret; |
cd658695 JA |
3540 | iov_iter_save_state(iter, state); |
3541 | ||
b23df91b PB |
3542 | /* if we can retry, do so with the callbacks armed */ |
3543 | if (!io_rw_should_retry(req)) { | |
3544 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3545 | return -EAGAIN; | |
3546 | } | |
3547 | ||
3548 | /* | |
3549 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3550 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3551 | * desired page gets unlocked. We can also get a partial read | |
3552 | * here, and if we do, then just retry at the new offset. | |
3553 | */ | |
3554 | ret = io_iter_do_read(req, iter); | |
3555 | if (ret == -EIOCBQUEUED) | |
3556 | return 0; | |
227c0c96 | 3557 | /* we got some bytes, but not all. retry. */ |
b5b0ecb7 | 3558 | kiocb->ki_flags &= ~IOCB_WAITQ; |
cd658695 JA |
3559 | iov_iter_restore(iter, state); |
3560 | } while (ret > 0); | |
227c0c96 | 3561 | done: |
889fca73 | 3562 | kiocb_done(kiocb, ret, issue_flags); |
fe1cdd55 PB |
3563 | out_free: |
3564 | /* it's faster to check here then delegate to kfree */ | |
3565 | if (iovec) | |
3566 | kfree(iovec); | |
5ea5dd45 | 3567 | return 0; |
2b188cc1 JA |
3568 | } |
3569 | ||
73debe68 | 3570 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3571 | { |
3529d8c2 JA |
3572 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3573 | return -EBADF; | |
5d329e12 | 3574 | return io_prep_rw(req, sqe, WRITE); |
f67676d1 JA |
3575 | } |
3576 | ||
889fca73 | 3577 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3578 | { |
3579 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3580 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3581 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3582 | struct io_async_rw *rw = req->async_data; |
45d189c6 | 3583 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
cd658695 JA |
3584 | struct iov_iter_state __state, *state; |
3585 | ssize_t ret, ret2; | |
2b188cc1 | 3586 | |
2846c481 | 3587 | if (rw) { |
e8c2bc1f | 3588 | iter = &rw->iter; |
cd658695 JA |
3589 | state = &rw->iter_state; |
3590 | iov_iter_restore(iter, state); | |
2846c481 PB |
3591 | iovec = NULL; |
3592 | } else { | |
3593 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
3594 | if (ret < 0) | |
3595 | return ret; | |
cd658695 JA |
3596 | state = &__state; |
3597 | iov_iter_save_state(iter, state); | |
2846c481 | 3598 | } |
cd658695 | 3599 | req->result = iov_iter_count(iter); |
2b188cc1 | 3600 | |
fd6c2e4c JA |
3601 | /* Ensure we clear previously set non-block flag */ |
3602 | if (!force_nonblock) | |
a88fc400 PB |
3603 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3604 | else | |
3605 | kiocb->ki_flags |= IOCB_NOWAIT; | |
fd6c2e4c | 3606 | |
24c74678 | 3607 | /* If the file doesn't support async, just async punt */ |
b191e2df | 3608 | if (force_nonblock && !io_file_supports_nowait(req, WRITE)) |
f67676d1 | 3609 | goto copy_iov; |
31b51510 | 3610 | |
10d59345 JA |
3611 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3612 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3613 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3614 | goto copy_iov; |
31b51510 | 3615 | |
cd658695 | 3616 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), req->result); |
fa15bafb PB |
3617 | if (unlikely(ret)) |
3618 | goto out_free; | |
4ed734b0 | 3619 | |
fa15bafb PB |
3620 | /* |
3621 | * Open-code file_start_write here to grab freeze protection, | |
3622 | * which will be released by another thread in | |
3623 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3624 | * released so that it doesn't complain about the held lock when | |
3625 | * we return to userspace. | |
3626 | */ | |
3627 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3628 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3629 | __sb_writers_release(file_inode(req->file)->i_sb, |
3630 | SB_FREEZE_WRITE); | |
3631 | } | |
3632 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3633 | |
fa15bafb | 3634 | if (req->file->f_op->write_iter) |
ff6165b2 | 3635 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3636 | else if (req->file->f_op->write) |
4017eb91 | 3637 | ret2 = loop_rw_iter(WRITE, req, iter); |
2dd2111d GH |
3638 | else |
3639 | ret2 = -EINVAL; | |
4ed734b0 | 3640 | |
6ad7f233 PB |
3641 | if (req->flags & REQ_F_REISSUE) { |
3642 | req->flags &= ~REQ_F_REISSUE; | |
230d50d4 | 3643 | ret2 = -EAGAIN; |
6ad7f233 | 3644 | } |
230d50d4 | 3645 | |
fa15bafb PB |
3646 | /* |
3647 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3648 | * retry them without IOCB_NOWAIT. | |
3649 | */ | |
3650 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3651 | ret2 = -EAGAIN; | |
75c668cd PB |
3652 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3653 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3654 | goto done; |
fa15bafb | 3655 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3656 | /* IOPOLL retry should happen for io-wq threads */ |
3657 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3658 | goto copy_iov; | |
355afaeb | 3659 | done: |
889fca73 | 3660 | kiocb_done(kiocb, ret2, issue_flags); |
fa15bafb | 3661 | } else { |
f67676d1 | 3662 | copy_iov: |
cd658695 | 3663 | iov_iter_restore(iter, state); |
227c0c96 | 3664 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
6bf985dc | 3665 | return ret ?: -EAGAIN; |
2b188cc1 | 3666 | } |
31b51510 | 3667 | out_free: |
f261c168 | 3668 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3669 | if (iovec) |
6f2cc166 | 3670 | kfree(iovec); |
2b188cc1 JA |
3671 | return ret; |
3672 | } | |
3673 | ||
80a261fd JA |
3674 | static int io_renameat_prep(struct io_kiocb *req, |
3675 | const struct io_uring_sqe *sqe) | |
3676 | { | |
3677 | struct io_rename *ren = &req->rename; | |
3678 | const char __user *oldf, *newf; | |
3679 | ||
ed7eb259 JA |
3680 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3681 | return -EINVAL; | |
26578cda | 3682 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
ed7eb259 | 3683 | return -EINVAL; |
80a261fd JA |
3684 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3685 | return -EBADF; | |
3686 | ||
3687 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3688 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3689 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3690 | ren->new_dfd = READ_ONCE(sqe->len); | |
3691 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3692 | ||
3693 | ren->oldpath = getname(oldf); | |
3694 | if (IS_ERR(ren->oldpath)) | |
3695 | return PTR_ERR(ren->oldpath); | |
3696 | ||
3697 | ren->newpath = getname(newf); | |
3698 | if (IS_ERR(ren->newpath)) { | |
3699 | putname(ren->oldpath); | |
3700 | return PTR_ERR(ren->newpath); | |
3701 | } | |
3702 | ||
3703 | req->flags |= REQ_F_NEED_CLEANUP; | |
3704 | return 0; | |
3705 | } | |
3706 | ||
45d189c6 | 3707 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
3708 | { |
3709 | struct io_rename *ren = &req->rename; | |
3710 | int ret; | |
3711 | ||
45d189c6 | 3712 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
3713 | return -EAGAIN; |
3714 | ||
3715 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3716 | ren->newpath, ren->flags); | |
3717 | ||
3718 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3719 | if (ret < 0) | |
93d2bcd2 | 3720 | req_set_fail(req); |
80a261fd JA |
3721 | io_req_complete(req, ret); |
3722 | return 0; | |
3723 | } | |
3724 | ||
14a1143b JA |
3725 | static int io_unlinkat_prep(struct io_kiocb *req, |
3726 | const struct io_uring_sqe *sqe) | |
3727 | { | |
3728 | struct io_unlink *un = &req->unlink; | |
3729 | const char __user *fname; | |
3730 | ||
22634bc5 JA |
3731 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3732 | return -EINVAL; | |
26578cda PB |
3733 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
3734 | sqe->splice_fd_in) | |
22634bc5 | 3735 | return -EINVAL; |
14a1143b JA |
3736 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3737 | return -EBADF; | |
3738 | ||
3739 | un->dfd = READ_ONCE(sqe->fd); | |
3740 | ||
3741 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3742 | if (un->flags & ~AT_REMOVEDIR) | |
3743 | return -EINVAL; | |
3744 | ||
3745 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3746 | un->filename = getname(fname); | |
3747 | if (IS_ERR(un->filename)) | |
3748 | return PTR_ERR(un->filename); | |
3749 | ||
3750 | req->flags |= REQ_F_NEED_CLEANUP; | |
3751 | return 0; | |
3752 | } | |
3753 | ||
45d189c6 | 3754 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
3755 | { |
3756 | struct io_unlink *un = &req->unlink; | |
3757 | int ret; | |
3758 | ||
45d189c6 | 3759 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
3760 | return -EAGAIN; |
3761 | ||
3762 | if (un->flags & AT_REMOVEDIR) | |
3763 | ret = do_rmdir(un->dfd, un->filename); | |
3764 | else | |
3765 | ret = do_unlinkat(un->dfd, un->filename); | |
3766 | ||
3767 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3768 | if (ret < 0) | |
93d2bcd2 | 3769 | req_set_fail(req); |
14a1143b JA |
3770 | io_req_complete(req, ret); |
3771 | return 0; | |
3772 | } | |
3773 | ||
e34a02dc DK |
3774 | static int io_mkdirat_prep(struct io_kiocb *req, |
3775 | const struct io_uring_sqe *sqe) | |
3776 | { | |
3777 | struct io_mkdir *mkd = &req->mkdir; | |
3778 | const char __user *fname; | |
3779 | ||
3780 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3781 | return -EINVAL; | |
3782 | if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index || | |
3783 | sqe->splice_fd_in) | |
3784 | return -EINVAL; | |
3785 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3786 | return -EBADF; | |
3787 | ||
3788 | mkd->dfd = READ_ONCE(sqe->fd); | |
3789 | mkd->mode = READ_ONCE(sqe->len); | |
3790 | ||
3791 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3792 | mkd->filename = getname(fname); | |
3793 | if (IS_ERR(mkd->filename)) | |
3794 | return PTR_ERR(mkd->filename); | |
3795 | ||
3796 | req->flags |= REQ_F_NEED_CLEANUP; | |
3797 | return 0; | |
3798 | } | |
3799 | ||
3800 | static int io_mkdirat(struct io_kiocb *req, int issue_flags) | |
3801 | { | |
3802 | struct io_mkdir *mkd = &req->mkdir; | |
3803 | int ret; | |
3804 | ||
3805 | if (issue_flags & IO_URING_F_NONBLOCK) | |
3806 | return -EAGAIN; | |
3807 | ||
3808 | ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode); | |
3809 | ||
3810 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3811 | if (ret < 0) | |
3812 | req_set_fail(req); | |
3813 | io_req_complete(req, ret); | |
3814 | return 0; | |
3815 | } | |
3816 | ||
7a8721f8 DK |
3817 | static int io_symlinkat_prep(struct io_kiocb *req, |
3818 | const struct io_uring_sqe *sqe) | |
3819 | { | |
3820 | struct io_symlink *sl = &req->symlink; | |
3821 | const char __user *oldpath, *newpath; | |
3822 | ||
3823 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3824 | return -EINVAL; | |
3825 | if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index || | |
3826 | sqe->splice_fd_in) | |
3827 | return -EINVAL; | |
3828 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3829 | return -EBADF; | |
3830 | ||
3831 | sl->new_dfd = READ_ONCE(sqe->fd); | |
3832 | oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3833 | newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3834 | ||
3835 | sl->oldpath = getname(oldpath); | |
3836 | if (IS_ERR(sl->oldpath)) | |
3837 | return PTR_ERR(sl->oldpath); | |
3838 | ||
3839 | sl->newpath = getname(newpath); | |
3840 | if (IS_ERR(sl->newpath)) { | |
3841 | putname(sl->oldpath); | |
3842 | return PTR_ERR(sl->newpath); | |
3843 | } | |
3844 | ||
3845 | req->flags |= REQ_F_NEED_CLEANUP; | |
3846 | return 0; | |
3847 | } | |
3848 | ||
3849 | static int io_symlinkat(struct io_kiocb *req, int issue_flags) | |
3850 | { | |
3851 | struct io_symlink *sl = &req->symlink; | |
3852 | int ret; | |
3853 | ||
3854 | if (issue_flags & IO_URING_F_NONBLOCK) | |
3855 | return -EAGAIN; | |
3856 | ||
3857 | ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath); | |
3858 | ||
3859 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3860 | if (ret < 0) | |
3861 | req_set_fail(req); | |
3862 | io_req_complete(req, ret); | |
3863 | return 0; | |
3864 | } | |
3865 | ||
cf30da90 DK |
3866 | static int io_linkat_prep(struct io_kiocb *req, |
3867 | const struct io_uring_sqe *sqe) | |
3868 | { | |
3869 | struct io_hardlink *lnk = &req->hardlink; | |
3870 | const char __user *oldf, *newf; | |
3871 | ||
3872 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3873 | return -EINVAL; | |
3874 | if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) | |
3875 | return -EINVAL; | |
3876 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3877 | return -EBADF; | |
3878 | ||
3879 | lnk->old_dfd = READ_ONCE(sqe->fd); | |
3880 | lnk->new_dfd = READ_ONCE(sqe->len); | |
3881 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3882 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3883 | lnk->flags = READ_ONCE(sqe->hardlink_flags); | |
3884 | ||
3885 | lnk->oldpath = getname(oldf); | |
3886 | if (IS_ERR(lnk->oldpath)) | |
3887 | return PTR_ERR(lnk->oldpath); | |
3888 | ||
3889 | lnk->newpath = getname(newf); | |
3890 | if (IS_ERR(lnk->newpath)) { | |
3891 | putname(lnk->oldpath); | |
3892 | return PTR_ERR(lnk->newpath); | |
3893 | } | |
3894 | ||
3895 | req->flags |= REQ_F_NEED_CLEANUP; | |
3896 | return 0; | |
3897 | } | |
3898 | ||
3899 | static int io_linkat(struct io_kiocb *req, int issue_flags) | |
3900 | { | |
3901 | struct io_hardlink *lnk = &req->hardlink; | |
3902 | int ret; | |
3903 | ||
3904 | if (issue_flags & IO_URING_F_NONBLOCK) | |
3905 | return -EAGAIN; | |
3906 | ||
3907 | ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd, | |
3908 | lnk->newpath, lnk->flags); | |
3909 | ||
3910 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3911 | if (ret < 0) | |
3912 | req_set_fail(req); | |
3913 | io_req_complete(req, ret); | |
3914 | return 0; | |
3915 | } | |
3916 | ||
36f4fa68 JA |
3917 | static int io_shutdown_prep(struct io_kiocb *req, |
3918 | const struct io_uring_sqe *sqe) | |
3919 | { | |
3920 | #if defined(CONFIG_NET) | |
3921 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3922 | return -EINVAL; | |
26578cda PB |
3923 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
3924 | sqe->buf_index || sqe->splice_fd_in)) | |
36f4fa68 JA |
3925 | return -EINVAL; |
3926 | ||
3927 | req->shutdown.how = READ_ONCE(sqe->len); | |
3928 | return 0; | |
3929 | #else | |
3930 | return -EOPNOTSUPP; | |
3931 | #endif | |
3932 | } | |
3933 | ||
45d189c6 | 3934 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
3935 | { |
3936 | #if defined(CONFIG_NET) | |
3937 | struct socket *sock; | |
3938 | int ret; | |
3939 | ||
45d189c6 | 3940 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
3941 | return -EAGAIN; |
3942 | ||
48aba79b | 3943 | sock = sock_from_file(req->file); |
36f4fa68 | 3944 | if (unlikely(!sock)) |
48aba79b | 3945 | return -ENOTSOCK; |
36f4fa68 JA |
3946 | |
3947 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d | 3948 | if (ret < 0) |
93d2bcd2 | 3949 | req_set_fail(req); |
36f4fa68 JA |
3950 | io_req_complete(req, ret); |
3951 | return 0; | |
3952 | #else | |
3953 | return -EOPNOTSUPP; | |
3954 | #endif | |
3955 | } | |
3956 | ||
f2a8d5c7 PB |
3957 | static int __io_splice_prep(struct io_kiocb *req, |
3958 | const struct io_uring_sqe *sqe) | |
7d67af2c | 3959 | { |
fe7e3257 | 3960 | struct io_splice *sp = &req->splice; |
7d67af2c | 3961 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
7d67af2c | 3962 | |
3232dd02 PB |
3963 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3964 | return -EINVAL; | |
7d67af2c PB |
3965 | |
3966 | sp->file_in = NULL; | |
7d67af2c PB |
3967 | sp->len = READ_ONCE(sqe->len); |
3968 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3969 | ||
3970 | if (unlikely(sp->flags & ~valid_flags)) | |
3971 | return -EINVAL; | |
3972 | ||
62906e89 | 3973 | sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in), |
8371adf5 PB |
3974 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
3975 | if (!sp->file_in) | |
3976 | return -EBADF; | |
7d67af2c | 3977 | req->flags |= REQ_F_NEED_CLEANUP; |
7d67af2c PB |
3978 | return 0; |
3979 | } | |
3980 | ||
f2a8d5c7 PB |
3981 | static int io_tee_prep(struct io_kiocb *req, |
3982 | const struct io_uring_sqe *sqe) | |
3983 | { | |
3984 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3985 | return -EINVAL; | |
3986 | return __io_splice_prep(req, sqe); | |
3987 | } | |
3988 | ||
45d189c6 | 3989 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
3990 | { |
3991 | struct io_splice *sp = &req->splice; | |
3992 | struct file *in = sp->file_in; | |
3993 | struct file *out = sp->file_out; | |
3994 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3995 | long ret = 0; | |
3996 | ||
45d189c6 | 3997 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
3998 | return -EAGAIN; |
3999 | if (sp->len) | |
4000 | ret = do_tee(in, out, sp->len, flags); | |
4001 | ||
e1d767f0 PB |
4002 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4003 | io_put_file(in); | |
f2a8d5c7 PB |
4004 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4005 | ||
f2a8d5c7 | 4006 | if (ret != sp->len) |
93d2bcd2 | 4007 | req_set_fail(req); |
e1e16097 | 4008 | io_req_complete(req, ret); |
f2a8d5c7 PB |
4009 | return 0; |
4010 | } | |
4011 | ||
4012 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4013 | { | |
fe7e3257 | 4014 | struct io_splice *sp = &req->splice; |
f2a8d5c7 PB |
4015 | |
4016 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
4017 | sp->off_out = READ_ONCE(sqe->off); | |
4018 | return __io_splice_prep(req, sqe); | |
4019 | } | |
4020 | ||
45d189c6 | 4021 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
4022 | { |
4023 | struct io_splice *sp = &req->splice; | |
4024 | struct file *in = sp->file_in; | |
4025 | struct file *out = sp->file_out; | |
4026 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
4027 | loff_t *poff_in, *poff_out; | |
c9687426 | 4028 | long ret = 0; |
7d67af2c | 4029 | |
45d189c6 | 4030 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 4031 | return -EAGAIN; |
7d67af2c PB |
4032 | |
4033 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
4034 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 4035 | |
948a7749 | 4036 | if (sp->len) |
c9687426 | 4037 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c | 4038 | |
e1d767f0 PB |
4039 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4040 | io_put_file(in); | |
7d67af2c PB |
4041 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4042 | ||
7d67af2c | 4043 | if (ret != sp->len) |
93d2bcd2 | 4044 | req_set_fail(req); |
e1e16097 | 4045 | io_req_complete(req, ret); |
7d67af2c PB |
4046 | return 0; |
4047 | } | |
4048 | ||
2b188cc1 JA |
4049 | /* |
4050 | * IORING_OP_NOP just posts a completion event, nothing else. | |
4051 | */ | |
889fca73 | 4052 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
4053 | { |
4054 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 4055 | |
def596e9 JA |
4056 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4057 | return -EINVAL; | |
4058 | ||
889fca73 | 4059 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
4060 | return 0; |
4061 | } | |
4062 | ||
1155c76a | 4063 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 4064 | { |
6b06314c | 4065 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 4066 | |
09bb8394 JA |
4067 | if (!req->file) |
4068 | return -EBADF; | |
c992fe29 | 4069 | |
6b06314c | 4070 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 4071 | return -EINVAL; |
26578cda PB |
4072 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4073 | sqe->splice_fd_in)) | |
c992fe29 CH |
4074 | return -EINVAL; |
4075 | ||
8ed8d3c3 JA |
4076 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
4077 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
4078 | return -EINVAL; | |
4079 | ||
4080 | req->sync.off = READ_ONCE(sqe->off); | |
4081 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
4082 | return 0; |
4083 | } | |
4084 | ||
45d189c6 | 4085 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4086 | { |
8ed8d3c3 | 4087 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
4088 | int ret; |
4089 | ||
ac45abc0 | 4090 | /* fsync always requires a blocking context */ |
45d189c6 | 4091 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4092 | return -EAGAIN; |
4093 | ||
9adbd45d | 4094 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
4095 | end > 0 ? end : LLONG_MAX, |
4096 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
4097 | if (ret < 0) | |
93d2bcd2 | 4098 | req_set_fail(req); |
e1e16097 | 4099 | io_req_complete(req, ret); |
c992fe29 CH |
4100 | return 0; |
4101 | } | |
4102 | ||
d63d1b5e JA |
4103 | static int io_fallocate_prep(struct io_kiocb *req, |
4104 | const struct io_uring_sqe *sqe) | |
4105 | { | |
26578cda PB |
4106 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
4107 | sqe->splice_fd_in) | |
d63d1b5e | 4108 | return -EINVAL; |
3232dd02 PB |
4109 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4110 | return -EINVAL; | |
d63d1b5e JA |
4111 | |
4112 | req->sync.off = READ_ONCE(sqe->off); | |
4113 | req->sync.len = READ_ONCE(sqe->addr); | |
4114 | req->sync.mode = READ_ONCE(sqe->len); | |
4115 | return 0; | |
4116 | } | |
4117 | ||
45d189c6 | 4118 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 4119 | { |
ac45abc0 PB |
4120 | int ret; |
4121 | ||
d63d1b5e | 4122 | /* fallocate always requiring blocking context */ |
45d189c6 | 4123 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 4124 | return -EAGAIN; |
ac45abc0 PB |
4125 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
4126 | req->sync.len); | |
ac45abc0 | 4127 | if (ret < 0) |
93d2bcd2 | 4128 | req_set_fail(req); |
e1e16097 | 4129 | io_req_complete(req, ret); |
5d17b4a4 JA |
4130 | return 0; |
4131 | } | |
4132 | ||
ec65fea5 | 4133 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 4134 | { |
f8748881 | 4135 | const char __user *fname; |
15b71abe | 4136 | int ret; |
b7bb4f7d | 4137 | |
d3fddf6d PB |
4138 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4139 | return -EINVAL; | |
b9445598 | 4140 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 4141 | return -EINVAL; |
ec65fea5 | 4142 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 4143 | return -EBADF; |
03b1230c | 4144 | |
ec65fea5 PB |
4145 | /* open.how should be already initialised */ |
4146 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 4147 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 4148 | |
25e72d10 PB |
4149 | req->open.dfd = READ_ONCE(sqe->fd); |
4150 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 4151 | req->open.filename = getname(fname); |
15b71abe JA |
4152 | if (IS_ERR(req->open.filename)) { |
4153 | ret = PTR_ERR(req->open.filename); | |
4154 | req->open.filename = NULL; | |
4155 | return ret; | |
4156 | } | |
b9445598 PB |
4157 | |
4158 | req->open.file_slot = READ_ONCE(sqe->file_index); | |
4159 | if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) | |
4160 | return -EINVAL; | |
4161 | ||
4022e7af | 4162 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 4163 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 4164 | return 0; |
03b1230c JA |
4165 | } |
4166 | ||
ec65fea5 PB |
4167 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4168 | { | |
d3fddf6d PB |
4169 | u64 mode = READ_ONCE(sqe->len); |
4170 | u64 flags = READ_ONCE(sqe->open_flags); | |
ec65fea5 | 4171 | |
ec65fea5 PB |
4172 | req->open.how = build_open_how(flags, mode); |
4173 | return __io_openat_prep(req, sqe); | |
4174 | } | |
4175 | ||
cebdb986 | 4176 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 4177 | { |
cebdb986 | 4178 | struct open_how __user *how; |
cebdb986 | 4179 | size_t len; |
0fa03c62 JA |
4180 | int ret; |
4181 | ||
cebdb986 JA |
4182 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4183 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
4184 | if (len < OPEN_HOW_SIZE_VER0) |
4185 | return -EINVAL; | |
3529d8c2 | 4186 | |
cebdb986 JA |
4187 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
4188 | len); | |
4189 | if (ret) | |
4190 | return ret; | |
3529d8c2 | 4191 | |
ec65fea5 | 4192 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
4193 | } |
4194 | ||
45d189c6 | 4195 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
4196 | { |
4197 | struct open_flags op; | |
15b71abe | 4198 | struct file *file; |
b9445598 PB |
4199 | bool resolve_nonblock, nonblock_set; |
4200 | bool fixed = !!req->open.file_slot; | |
15b71abe JA |
4201 | int ret; |
4202 | ||
cebdb986 | 4203 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
4204 | if (ret) |
4205 | goto err; | |
3a81fd02 JA |
4206 | nonblock_set = op.open_flag & O_NONBLOCK; |
4207 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 4208 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
4209 | /* |
4210 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
4211 | * it'll always -EAGAIN | |
4212 | */ | |
4213 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
4214 | return -EAGAIN; | |
4215 | op.lookup_flags |= LOOKUP_CACHED; | |
4216 | op.open_flag |= O_NONBLOCK; | |
4217 | } | |
15b71abe | 4218 | |
b9445598 PB |
4219 | if (!fixed) { |
4220 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); | |
4221 | if (ret < 0) | |
4222 | goto err; | |
4223 | } | |
15b71abe JA |
4224 | |
4225 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
12dcb58a | 4226 | if (IS_ERR(file)) { |
944d1444 | 4227 | /* |
12dcb58a PB |
4228 | * We could hang on to this 'fd' on retrying, but seems like |
4229 | * marginal gain for something that is now known to be a slower | |
4230 | * path. So just put it, and we'll get a new one when we retry. | |
944d1444 | 4231 | */ |
b9445598 PB |
4232 | if (!fixed) |
4233 | put_unused_fd(ret); | |
3a81fd02 | 4234 | |
15b71abe | 4235 | ret = PTR_ERR(file); |
12dcb58a PB |
4236 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
4237 | if (ret == -EAGAIN && | |
4238 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) | |
4239 | return -EAGAIN; | |
4240 | goto err; | |
15b71abe | 4241 | } |
12dcb58a PB |
4242 | |
4243 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) | |
4244 | file->f_flags &= ~O_NONBLOCK; | |
4245 | fsnotify_open(file); | |
b9445598 PB |
4246 | |
4247 | if (!fixed) | |
4248 | fd_install(ret, file); | |
4249 | else | |
4250 | ret = io_install_fixed_file(req, file, issue_flags, | |
4251 | req->open.file_slot - 1); | |
15b71abe JA |
4252 | err: |
4253 | putname(req->open.filename); | |
8fef80bf | 4254 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe | 4255 | if (ret < 0) |
93d2bcd2 | 4256 | req_set_fail(req); |
0bdf3398 | 4257 | __io_req_complete(req, issue_flags, ret, 0); |
15b71abe JA |
4258 | return 0; |
4259 | } | |
4260 | ||
45d189c6 | 4261 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 4262 | { |
e45cff58 | 4263 | return io_openat2(req, issue_flags); |
cebdb986 JA |
4264 | } |
4265 | ||
067524e9 JA |
4266 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4267 | const struct io_uring_sqe *sqe) | |
4268 | { | |
4269 | struct io_provide_buf *p = &req->pbuf; | |
4270 | u64 tmp; | |
4271 | ||
26578cda PB |
4272 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
4273 | sqe->splice_fd_in) | |
067524e9 JA |
4274 | return -EINVAL; |
4275 | ||
4276 | tmp = READ_ONCE(sqe->fd); | |
4277 | if (!tmp || tmp > USHRT_MAX) | |
4278 | return -EINVAL; | |
4279 | ||
4280 | memset(p, 0, sizeof(*p)); | |
4281 | p->nbufs = tmp; | |
4282 | p->bgid = READ_ONCE(sqe->buf_group); | |
4283 | return 0; | |
4284 | } | |
4285 | ||
4286 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
4287 | int bgid, unsigned nbufs) | |
4288 | { | |
4289 | unsigned i = 0; | |
4290 | ||
4291 | /* shouldn't happen */ | |
4292 | if (!nbufs) | |
4293 | return 0; | |
4294 | ||
4295 | /* the head kbuf is the list itself */ | |
4296 | while (!list_empty(&buf->list)) { | |
4297 | struct io_buffer *nxt; | |
4298 | ||
4299 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
4300 | list_del(&nxt->list); | |
4301 | kfree(nxt); | |
4302 | if (++i == nbufs) | |
4303 | return i; | |
4304 | } | |
4305 | i++; | |
4306 | kfree(buf); | |
9e15c3a0 | 4307 | xa_erase(&ctx->io_buffers, bgid); |
067524e9 JA |
4308 | |
4309 | return i; | |
4310 | } | |
4311 | ||
889fca73 | 4312 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
4313 | { |
4314 | struct io_provide_buf *p = &req->pbuf; | |
4315 | struct io_ring_ctx *ctx = req->ctx; | |
4316 | struct io_buffer *head; | |
4317 | int ret = 0; | |
45d189c6 | 4318 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
067524e9 JA |
4319 | |
4320 | io_ring_submit_lock(ctx, !force_nonblock); | |
4321 | ||
4322 | lockdep_assert_held(&ctx->uring_lock); | |
4323 | ||
4324 | ret = -ENOENT; | |
9e15c3a0 | 4325 | head = xa_load(&ctx->io_buffers, p->bgid); |
067524e9 JA |
4326 | if (head) |
4327 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 | 4328 | if (ret < 0) |
93d2bcd2 | 4329 | req_set_fail(req); |
067524e9 | 4330 | |
9fb8cb49 PB |
4331 | /* complete before unlock, IOPOLL may need the lock */ |
4332 | __io_req_complete(req, issue_flags, ret, 0); | |
4333 | io_ring_submit_unlock(ctx, !force_nonblock); | |
067524e9 JA |
4334 | return 0; |
4335 | } | |
4336 | ||
ddf0322d JA |
4337 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4338 | const struct io_uring_sqe *sqe) | |
4339 | { | |
38134ada | 4340 | unsigned long size, tmp_check; |
ddf0322d JA |
4341 | struct io_provide_buf *p = &req->pbuf; |
4342 | u64 tmp; | |
4343 | ||
26578cda | 4344 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
ddf0322d JA |
4345 | return -EINVAL; |
4346 | ||
4347 | tmp = READ_ONCE(sqe->fd); | |
4348 | if (!tmp || tmp > USHRT_MAX) | |
4349 | return -E2BIG; | |
4350 | p->nbufs = tmp; | |
4351 | p->addr = READ_ONCE(sqe->addr); | |
4352 | p->len = READ_ONCE(sqe->len); | |
4353 | ||
38134ada PB |
4354 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
4355 | &size)) | |
4356 | return -EOVERFLOW; | |
4357 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) | |
4358 | return -EOVERFLOW; | |
4359 | ||
d81269fe PB |
4360 | size = (unsigned long)p->len * p->nbufs; |
4361 | if (!access_ok(u64_to_user_ptr(p->addr), size)) | |
ddf0322d JA |
4362 | return -EFAULT; |
4363 | ||
4364 | p->bgid = READ_ONCE(sqe->buf_group); | |
4365 | tmp = READ_ONCE(sqe->off); | |
4366 | if (tmp > USHRT_MAX) | |
4367 | return -E2BIG; | |
4368 | p->bid = tmp; | |
4369 | return 0; | |
4370 | } | |
4371 | ||
4372 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
4373 | { | |
4374 | struct io_buffer *buf; | |
4375 | u64 addr = pbuf->addr; | |
4376 | int i, bid = pbuf->bid; | |
4377 | ||
4378 | for (i = 0; i < pbuf->nbufs; i++) { | |
9990da93 | 4379 | buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT); |
ddf0322d JA |
4380 | if (!buf) |
4381 | break; | |
4382 | ||
4383 | buf->addr = addr; | |
d1f82808 | 4384 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
ddf0322d JA |
4385 | buf->bid = bid; |
4386 | addr += pbuf->len; | |
4387 | bid++; | |
4388 | if (!*head) { | |
4389 | INIT_LIST_HEAD(&buf->list); | |
4390 | *head = buf; | |
4391 | } else { | |
4392 | list_add_tail(&buf->list, &(*head)->list); | |
4393 | } | |
4394 | } | |
4395 | ||
4396 | return i ? i : -ENOMEM; | |
4397 | } | |
4398 | ||
889fca73 | 4399 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4400 | { |
4401 | struct io_provide_buf *p = &req->pbuf; | |
4402 | struct io_ring_ctx *ctx = req->ctx; | |
4403 | struct io_buffer *head, *list; | |
4404 | int ret = 0; | |
45d189c6 | 4405 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ddf0322d JA |
4406 | |
4407 | io_ring_submit_lock(ctx, !force_nonblock); | |
4408 | ||
4409 | lockdep_assert_held(&ctx->uring_lock); | |
4410 | ||
9e15c3a0 | 4411 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
ddf0322d JA |
4412 | |
4413 | ret = io_add_buffers(p, &head); | |
9e15c3a0 JA |
4414 | if (ret >= 0 && !list) { |
4415 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); | |
4416 | if (ret < 0) | |
067524e9 | 4417 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d | 4418 | } |
ddf0322d | 4419 | if (ret < 0) |
93d2bcd2 | 4420 | req_set_fail(req); |
9fb8cb49 PB |
4421 | /* complete before unlock, IOPOLL may need the lock */ |
4422 | __io_req_complete(req, issue_flags, ret, 0); | |
4423 | io_ring_submit_unlock(ctx, !force_nonblock); | |
ddf0322d | 4424 | return 0; |
cebdb986 JA |
4425 | } |
4426 | ||
3e4827b0 JA |
4427 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4428 | const struct io_uring_sqe *sqe) | |
4429 | { | |
4430 | #if defined(CONFIG_EPOLL) | |
26578cda | 4431 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
3e4827b0 | 4432 | return -EINVAL; |
2d74d042 | 4433 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4434 | return -EINVAL; |
3e4827b0 JA |
4435 | |
4436 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4437 | req->epoll.op = READ_ONCE(sqe->len); | |
4438 | req->epoll.fd = READ_ONCE(sqe->off); | |
4439 | ||
4440 | if (ep_op_has_event(req->epoll.op)) { | |
4441 | struct epoll_event __user *ev; | |
4442 | ||
4443 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4444 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4445 | return -EFAULT; | |
4446 | } | |
4447 | ||
4448 | return 0; | |
4449 | #else | |
4450 | return -EOPNOTSUPP; | |
4451 | #endif | |
4452 | } | |
4453 | ||
889fca73 | 4454 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4455 | { |
4456 | #if defined(CONFIG_EPOLL) | |
4457 | struct io_epoll *ie = &req->epoll; | |
4458 | int ret; | |
45d189c6 | 4459 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4460 | |
4461 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4462 | if (force_nonblock && ret == -EAGAIN) | |
4463 | return -EAGAIN; | |
4464 | ||
4465 | if (ret < 0) | |
93d2bcd2 | 4466 | req_set_fail(req); |
889fca73 | 4467 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4468 | return 0; |
4469 | #else | |
4470 | return -EOPNOTSUPP; | |
4471 | #endif | |
4472 | } | |
4473 | ||
c1ca757b JA |
4474 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4475 | { | |
4476 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
26578cda | 4477 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
c1ca757b | 4478 | return -EINVAL; |
3232dd02 PB |
4479 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4480 | return -EINVAL; | |
c1ca757b JA |
4481 | |
4482 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4483 | req->madvise.len = READ_ONCE(sqe->len); | |
4484 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4485 | return 0; | |
4486 | #else | |
4487 | return -EOPNOTSUPP; | |
4488 | #endif | |
4489 | } | |
4490 | ||
45d189c6 | 4491 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4492 | { |
4493 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4494 | struct io_madvise *ma = &req->madvise; | |
4495 | int ret; | |
4496 | ||
45d189c6 | 4497 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4498 | return -EAGAIN; |
4499 | ||
0726b01e | 4500 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b | 4501 | if (ret < 0) |
93d2bcd2 | 4502 | req_set_fail(req); |
e1e16097 | 4503 | io_req_complete(req, ret); |
c1ca757b JA |
4504 | return 0; |
4505 | #else | |
4506 | return -EOPNOTSUPP; | |
4507 | #endif | |
4508 | } | |
4509 | ||
4840e418 JA |
4510 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4511 | { | |
26578cda | 4512 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
4840e418 | 4513 | return -EINVAL; |
3232dd02 PB |
4514 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4515 | return -EINVAL; | |
4840e418 JA |
4516 | |
4517 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4518 | req->fadvise.len = READ_ONCE(sqe->len); | |
4519 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4520 | return 0; | |
4521 | } | |
4522 | ||
45d189c6 | 4523 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4524 | { |
4525 | struct io_fadvise *fa = &req->fadvise; | |
4526 | int ret; | |
4527 | ||
45d189c6 | 4528 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4529 | switch (fa->advice) { |
4530 | case POSIX_FADV_NORMAL: | |
4531 | case POSIX_FADV_RANDOM: | |
4532 | case POSIX_FADV_SEQUENTIAL: | |
4533 | break; | |
4534 | default: | |
4535 | return -EAGAIN; | |
4536 | } | |
4537 | } | |
4840e418 JA |
4538 | |
4539 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4540 | if (ret < 0) | |
93d2bcd2 | 4541 | req_set_fail(req); |
0bdf3398 | 4542 | __io_req_complete(req, issue_flags, ret, 0); |
4840e418 JA |
4543 | return 0; |
4544 | } | |
4545 | ||
eddc7ef5 JA |
4546 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4547 | { | |
2d74d042 | 4548 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4549 | return -EINVAL; |
26578cda | 4550 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
eddc7ef5 | 4551 | return -EINVAL; |
9c280f90 | 4552 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4553 | return -EBADF; |
eddc7ef5 | 4554 | |
1d9e1288 BM |
4555 | req->statx.dfd = READ_ONCE(sqe->fd); |
4556 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4557 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4558 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4559 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4560 | |
4561 | return 0; | |
4562 | } | |
4563 | ||
45d189c6 | 4564 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4565 | { |
1d9e1288 | 4566 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4567 | int ret; |
4568 | ||
59d70013 | 4569 | if (issue_flags & IO_URING_F_NONBLOCK) |
eddc7ef5 JA |
4570 | return -EAGAIN; |
4571 | ||
e62753e4 BM |
4572 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4573 | ctx->buffer); | |
eddc7ef5 | 4574 | |
eddc7ef5 | 4575 | if (ret < 0) |
93d2bcd2 | 4576 | req_set_fail(req); |
e1e16097 | 4577 | io_req_complete(req, ret); |
eddc7ef5 JA |
4578 | return 0; |
4579 | } | |
4580 | ||
b5dba59e JA |
4581 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4582 | { | |
14587a46 | 4583 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4584 | return -EINVAL; |
b5dba59e | 4585 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
7df778be | 4586 | sqe->rw_flags || sqe->buf_index) |
b5dba59e | 4587 | return -EINVAL; |
9c280f90 | 4588 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4589 | return -EBADF; |
b5dba59e JA |
4590 | |
4591 | req->close.fd = READ_ONCE(sqe->fd); | |
7df778be PB |
4592 | req->close.file_slot = READ_ONCE(sqe->file_index); |
4593 | if (req->close.file_slot && req->close.fd) | |
4594 | return -EINVAL; | |
4595 | ||
b5dba59e | 4596 | return 0; |
b5dba59e JA |
4597 | } |
4598 | ||
889fca73 | 4599 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4600 | { |
9eac1904 | 4601 | struct files_struct *files = current->files; |
3af73b28 | 4602 | struct io_close *close = &req->close; |
9eac1904 | 4603 | struct fdtable *fdt; |
a1fde923 PB |
4604 | struct file *file = NULL; |
4605 | int ret = -EBADF; | |
b5dba59e | 4606 | |
7df778be PB |
4607 | if (req->close.file_slot) { |
4608 | ret = io_close_fixed(req, issue_flags); | |
4609 | goto err; | |
4610 | } | |
4611 | ||
9eac1904 JA |
4612 | spin_lock(&files->file_lock); |
4613 | fdt = files_fdtable(files); | |
4614 | if (close->fd >= fdt->max_fds) { | |
4615 | spin_unlock(&files->file_lock); | |
4616 | goto err; | |
4617 | } | |
4618 | file = fdt->fd[close->fd]; | |
a1fde923 | 4619 | if (!file || file->f_op == &io_uring_fops) { |
9eac1904 JA |
4620 | spin_unlock(&files->file_lock); |
4621 | file = NULL; | |
4622 | goto err; | |
3af73b28 | 4623 | } |
b5dba59e JA |
4624 | |
4625 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 4626 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 4627 | spin_unlock(&files->file_lock); |
0bf0eefd | 4628 | return -EAGAIN; |
a2100672 | 4629 | } |
b5dba59e | 4630 | |
9eac1904 JA |
4631 | ret = __close_fd_get_file(close->fd, &file); |
4632 | spin_unlock(&files->file_lock); | |
4633 | if (ret < 0) { | |
4634 | if (ret == -ENOENT) | |
4635 | ret = -EBADF; | |
4636 | goto err; | |
4637 | } | |
4638 | ||
3af73b28 | 4639 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4640 | ret = filp_close(file, current->files); |
4641 | err: | |
3af73b28 | 4642 | if (ret < 0) |
93d2bcd2 | 4643 | req_set_fail(req); |
9eac1904 JA |
4644 | if (file) |
4645 | fput(file); | |
889fca73 | 4646 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 4647 | return 0; |
b5dba59e JA |
4648 | } |
4649 | ||
1155c76a | 4650 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4651 | { |
4652 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 4653 | |
5d17b4a4 JA |
4654 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4655 | return -EINVAL; | |
26578cda PB |
4656 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4657 | sqe->splice_fd_in)) | |
5d17b4a4 JA |
4658 | return -EINVAL; |
4659 | ||
8ed8d3c3 JA |
4660 | req->sync.off = READ_ONCE(sqe->off); |
4661 | req->sync.len = READ_ONCE(sqe->len); | |
4662 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4663 | return 0; |
4664 | } | |
4665 | ||
45d189c6 | 4666 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4667 | { |
8ed8d3c3 JA |
4668 | int ret; |
4669 | ||
ac45abc0 | 4670 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 4671 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4672 | return -EAGAIN; |
4673 | ||
9adbd45d | 4674 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4675 | req->sync.flags); |
4676 | if (ret < 0) | |
93d2bcd2 | 4677 | req_set_fail(req); |
e1e16097 | 4678 | io_req_complete(req, ret); |
5d17b4a4 JA |
4679 | return 0; |
4680 | } | |
4681 | ||
469956e8 | 4682 | #if defined(CONFIG_NET) |
02d27d89 PB |
4683 | static int io_setup_async_msg(struct io_kiocb *req, |
4684 | struct io_async_msghdr *kmsg) | |
4685 | { | |
e8c2bc1f JA |
4686 | struct io_async_msghdr *async_msg = req->async_data; |
4687 | ||
4688 | if (async_msg) | |
02d27d89 | 4689 | return -EAGAIN; |
e8c2bc1f | 4690 | if (io_alloc_async_data(req)) { |
257e84a5 | 4691 | kfree(kmsg->free_iov); |
02d27d89 PB |
4692 | return -ENOMEM; |
4693 | } | |
e8c2bc1f | 4694 | async_msg = req->async_data; |
02d27d89 | 4695 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4696 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 4697 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
4698 | /* if were using fast_iov, set it to the new one */ |
4699 | if (!async_msg->free_iov) | |
4700 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
4701 | ||
02d27d89 PB |
4702 | return -EAGAIN; |
4703 | } | |
4704 | ||
2ae523ed PB |
4705 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4706 | struct io_async_msghdr *iomsg) | |
4707 | { | |
2ae523ed | 4708 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 4709 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 4710 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 4711 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
4712 | } |
4713 | ||
93642ef8 PB |
4714 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
4715 | { | |
4716 | int ret; | |
4717 | ||
93642ef8 PB |
4718 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
4719 | if (!ret) | |
4720 | req->flags |= REQ_F_NEED_CLEANUP; | |
4721 | return ret; | |
4722 | } | |
4723 | ||
3529d8c2 | 4724 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4725 | { |
e47293fd | 4726 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 4727 | |
d2b6f48b PB |
4728 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4729 | return -EINVAL; | |
4730 | ||
270a5940 | 4731 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4732 | sr->len = READ_ONCE(sqe->len); |
04411806 PB |
4733 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4734 | if (sr->msg_flags & MSG_DONTWAIT) | |
4735 | req->flags |= REQ_F_NOWAIT; | |
3529d8c2 | 4736 | |
d8768362 JA |
4737 | #ifdef CONFIG_COMPAT |
4738 | if (req->ctx->compat) | |
4739 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4740 | #endif | |
93642ef8 | 4741 | return 0; |
03b1230c JA |
4742 | } |
4743 | ||
889fca73 | 4744 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4745 | { |
6b754c8b | 4746 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4747 | struct socket *sock; |
7a7cacba | 4748 | unsigned flags; |
0031275d | 4749 | int min_ret = 0; |
0fa03c62 JA |
4750 | int ret; |
4751 | ||
dba4a925 | 4752 | sock = sock_from_file(req->file); |
7a7cacba | 4753 | if (unlikely(!sock)) |
dba4a925 | 4754 | return -ENOTSOCK; |
3529d8c2 | 4755 | |
257e84a5 PB |
4756 | kmsg = req->async_data; |
4757 | if (!kmsg) { | |
7a7cacba PB |
4758 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
4759 | if (ret) | |
4760 | return ret; | |
4761 | kmsg = &iomsg; | |
0fa03c62 | 4762 | } |
0fa03c62 | 4763 | |
04411806 PB |
4764 | flags = req->sr_msg.msg_flags; |
4765 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4766 | flags |= MSG_DONTWAIT; |
0031275d SM |
4767 | if (flags & MSG_WAITALL) |
4768 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4769 | ||
7a7cacba | 4770 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
45d189c6 | 4771 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4772 | return io_setup_async_msg(req, kmsg); |
4773 | if (ret == -ERESTARTSYS) | |
4774 | ret = -EINTR; | |
0fa03c62 | 4775 | |
257e84a5 PB |
4776 | /* fast path, check for non-NULL to avoid function call */ |
4777 | if (kmsg->free_iov) | |
4778 | kfree(kmsg->free_iov); | |
99bc4c38 | 4779 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 4780 | if (ret < min_ret) |
93d2bcd2 | 4781 | req_set_fail(req); |
889fca73 | 4782 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 4783 | return 0; |
03b1230c | 4784 | } |
aa1fa28f | 4785 | |
889fca73 | 4786 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4787 | { |
7a7cacba PB |
4788 | struct io_sr_msg *sr = &req->sr_msg; |
4789 | struct msghdr msg; | |
4790 | struct iovec iov; | |
fddaface | 4791 | struct socket *sock; |
7a7cacba | 4792 | unsigned flags; |
0031275d | 4793 | int min_ret = 0; |
fddaface JA |
4794 | int ret; |
4795 | ||
dba4a925 | 4796 | sock = sock_from_file(req->file); |
7a7cacba | 4797 | if (unlikely(!sock)) |
dba4a925 | 4798 | return -ENOTSOCK; |
fddaface | 4799 | |
7a7cacba PB |
4800 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4801 | if (unlikely(ret)) | |
14db8411 | 4802 | return ret; |
fddaface | 4803 | |
7a7cacba PB |
4804 | msg.msg_name = NULL; |
4805 | msg.msg_control = NULL; | |
4806 | msg.msg_controllen = 0; | |
4807 | msg.msg_namelen = 0; | |
fddaface | 4808 | |
04411806 PB |
4809 | flags = req->sr_msg.msg_flags; |
4810 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4811 | flags |= MSG_DONTWAIT; |
0031275d SM |
4812 | if (flags & MSG_WAITALL) |
4813 | min_ret = iov_iter_count(&msg.msg_iter); | |
4814 | ||
7a7cacba PB |
4815 | msg.msg_flags = flags; |
4816 | ret = sock_sendmsg(sock, &msg); | |
45d189c6 | 4817 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4818 | return -EAGAIN; |
4819 | if (ret == -ERESTARTSYS) | |
4820 | ret = -EINTR; | |
fddaface | 4821 | |
0031275d | 4822 | if (ret < min_ret) |
93d2bcd2 | 4823 | req_set_fail(req); |
889fca73 | 4824 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 4825 | return 0; |
fddaface JA |
4826 | } |
4827 | ||
1400e697 PB |
4828 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4829 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4830 | { |
4831 | struct io_sr_msg *sr = &req->sr_msg; | |
4832 | struct iovec __user *uiov; | |
4833 | size_t iov_len; | |
4834 | int ret; | |
4835 | ||
1400e697 PB |
4836 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4837 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4838 | if (ret) |
4839 | return ret; | |
4840 | ||
4841 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4842 | if (iov_len > 1) | |
4843 | return -EINVAL; | |
5476dfed | 4844 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4845 | return -EFAULT; |
5476dfed | 4846 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 4847 | iomsg->free_iov = NULL; |
52de1fe1 | 4848 | } else { |
257e84a5 | 4849 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4850 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 4851 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 4852 | false); |
52de1fe1 JA |
4853 | if (ret > 0) |
4854 | ret = 0; | |
4855 | } | |
4856 | ||
4857 | return ret; | |
4858 | } | |
4859 | ||
4860 | #ifdef CONFIG_COMPAT | |
4861 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4862 | struct io_async_msghdr *iomsg) |
52de1fe1 | 4863 | { |
52de1fe1 JA |
4864 | struct io_sr_msg *sr = &req->sr_msg; |
4865 | struct compat_iovec __user *uiov; | |
4866 | compat_uptr_t ptr; | |
4867 | compat_size_t len; | |
4868 | int ret; | |
4869 | ||
4af3417a PB |
4870 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
4871 | &ptr, &len); | |
52de1fe1 JA |
4872 | if (ret) |
4873 | return ret; | |
4874 | ||
4875 | uiov = compat_ptr(ptr); | |
4876 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4877 | compat_ssize_t clen; | |
4878 | ||
4879 | if (len > 1) | |
4880 | return -EINVAL; | |
4881 | if (!access_ok(uiov, sizeof(*uiov))) | |
4882 | return -EFAULT; | |
4883 | if (__get_user(clen, &uiov->iov_len)) | |
4884 | return -EFAULT; | |
4885 | if (clen < 0) | |
4886 | return -EINVAL; | |
2d280bc8 | 4887 | sr->len = clen; |
257e84a5 | 4888 | iomsg->free_iov = NULL; |
52de1fe1 | 4889 | } else { |
257e84a5 | 4890 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4891 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 4892 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 4893 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
4894 | if (ret < 0) |
4895 | return ret; | |
4896 | } | |
4897 | ||
4898 | return 0; | |
4899 | } | |
4900 | #endif | |
4901 | ||
1400e697 PB |
4902 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4903 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4904 | { |
1400e697 | 4905 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
4906 | |
4907 | #ifdef CONFIG_COMPAT | |
4908 | if (req->ctx->compat) | |
1400e697 | 4909 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4910 | #endif |
52de1fe1 | 4911 | |
1400e697 | 4912 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4913 | } |
4914 | ||
bcda7baa | 4915 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4916 | bool needs_lock) |
bcda7baa JA |
4917 | { |
4918 | struct io_sr_msg *sr = &req->sr_msg; | |
4919 | struct io_buffer *kbuf; | |
4920 | ||
bcda7baa JA |
4921 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
4922 | if (IS_ERR(kbuf)) | |
4923 | return kbuf; | |
4924 | ||
4925 | sr->kbuf = kbuf; | |
4926 | req->flags |= REQ_F_BUFFER_SELECTED; | |
bcda7baa | 4927 | return kbuf; |
fddaface JA |
4928 | } |
4929 | ||
7fbb1b54 PB |
4930 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4931 | { | |
4932 | return io_put_kbuf(req, req->sr_msg.kbuf); | |
4933 | } | |
4934 | ||
93642ef8 | 4935 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 4936 | { |
99bc4c38 | 4937 | int ret; |
3529d8c2 | 4938 | |
93642ef8 PB |
4939 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
4940 | if (!ret) | |
4941 | req->flags |= REQ_F_NEED_CLEANUP; | |
4942 | return ret; | |
4943 | } | |
4944 | ||
4945 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4946 | { | |
4947 | struct io_sr_msg *sr = &req->sr_msg; | |
4948 | ||
d2b6f48b PB |
4949 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4950 | return -EINVAL; | |
4951 | ||
270a5940 | 4952 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4953 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4954 | sr->bgid = READ_ONCE(sqe->buf_group); |
04411806 PB |
4955 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4956 | if (sr->msg_flags & MSG_DONTWAIT) | |
4957 | req->flags |= REQ_F_NOWAIT; | |
06b76d44 | 4958 | |
d8768362 JA |
4959 | #ifdef CONFIG_COMPAT |
4960 | if (req->ctx->compat) | |
4961 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4962 | #endif | |
93642ef8 | 4963 | return 0; |
aa1fa28f JA |
4964 | } |
4965 | ||
889fca73 | 4966 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4967 | { |
6b754c8b | 4968 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4969 | struct socket *sock; |
7fbb1b54 | 4970 | struct io_buffer *kbuf; |
7a7cacba | 4971 | unsigned flags; |
0031275d | 4972 | int min_ret = 0; |
52de1fe1 | 4973 | int ret, cflags = 0; |
45d189c6 | 4974 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 4975 | |
dba4a925 | 4976 | sock = sock_from_file(req->file); |
7a7cacba | 4977 | if (unlikely(!sock)) |
dba4a925 | 4978 | return -ENOTSOCK; |
3529d8c2 | 4979 | |
257e84a5 PB |
4980 | kmsg = req->async_data; |
4981 | if (!kmsg) { | |
7a7cacba PB |
4982 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
4983 | if (ret) | |
681fda8d | 4984 | return ret; |
7a7cacba PB |
4985 | kmsg = &iomsg; |
4986 | } | |
03b1230c | 4987 | |
bc02ef33 | 4988 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4989 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4990 | if (IS_ERR(kbuf)) |
52de1fe1 | 4991 | return PTR_ERR(kbuf); |
7a7cacba | 4992 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
4993 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
4994 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
4995 | 1, req->sr_msg.len); |
4996 | } | |
52de1fe1 | 4997 | |
04411806 PB |
4998 | flags = req->sr_msg.msg_flags; |
4999 | if (force_nonblock) | |
7a7cacba | 5000 | flags |= MSG_DONTWAIT; |
0031275d SM |
5001 | if (flags & MSG_WAITALL) |
5002 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
5003 | ||
7a7cacba PB |
5004 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
5005 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
5006 | if (force_nonblock && ret == -EAGAIN) |
5007 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
5008 | if (ret == -ERESTARTSYS) |
5009 | ret = -EINTR; | |
03b1230c | 5010 | |
7fbb1b54 PB |
5011 | if (req->flags & REQ_F_BUFFER_SELECTED) |
5012 | cflags = io_put_recv_kbuf(req); | |
257e84a5 PB |
5013 | /* fast path, check for non-NULL to avoid function call */ |
5014 | if (kmsg->free_iov) | |
5015 | kfree(kmsg->free_iov); | |
99bc4c38 | 5016 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 5017 | if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
93d2bcd2 | 5018 | req_set_fail(req); |
889fca73 | 5019 | __io_req_complete(req, issue_flags, ret, cflags); |
03b1230c | 5020 | return 0; |
0fa03c62 | 5021 | } |
5d17b4a4 | 5022 | |
889fca73 | 5023 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 5024 | { |
6b754c8b | 5025 | struct io_buffer *kbuf; |
7a7cacba PB |
5026 | struct io_sr_msg *sr = &req->sr_msg; |
5027 | struct msghdr msg; | |
5028 | void __user *buf = sr->buf; | |
fddaface | 5029 | struct socket *sock; |
7a7cacba PB |
5030 | struct iovec iov; |
5031 | unsigned flags; | |
0031275d | 5032 | int min_ret = 0; |
bcda7baa | 5033 | int ret, cflags = 0; |
45d189c6 | 5034 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 5035 | |
dba4a925 | 5036 | sock = sock_from_file(req->file); |
7a7cacba | 5037 | if (unlikely(!sock)) |
dba4a925 | 5038 | return -ENOTSOCK; |
fddaface | 5039 | |
bc02ef33 | 5040 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 5041 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
5042 | if (IS_ERR(kbuf)) |
5043 | return PTR_ERR(kbuf); | |
7a7cacba | 5044 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 5045 | } |
bcda7baa | 5046 | |
7a7cacba | 5047 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
5048 | if (unlikely(ret)) |
5049 | goto out_free; | |
fddaface | 5050 | |
7a7cacba PB |
5051 | msg.msg_name = NULL; |
5052 | msg.msg_control = NULL; | |
5053 | msg.msg_controllen = 0; | |
5054 | msg.msg_namelen = 0; | |
5055 | msg.msg_iocb = NULL; | |
5056 | msg.msg_flags = 0; | |
fddaface | 5057 | |
04411806 PB |
5058 | flags = req->sr_msg.msg_flags; |
5059 | if (force_nonblock) | |
7a7cacba | 5060 | flags |= MSG_DONTWAIT; |
0031275d SM |
5061 | if (flags & MSG_WAITALL) |
5062 | min_ret = iov_iter_count(&msg.msg_iter); | |
5063 | ||
7a7cacba PB |
5064 | ret = sock_recvmsg(sock, &msg, flags); |
5065 | if (force_nonblock && ret == -EAGAIN) | |
5066 | return -EAGAIN; | |
5067 | if (ret == -ERESTARTSYS) | |
5068 | ret = -EINTR; | |
14c32eee | 5069 | out_free: |
7fbb1b54 PB |
5070 | if (req->flags & REQ_F_BUFFER_SELECTED) |
5071 | cflags = io_put_recv_kbuf(req); | |
0031275d | 5072 | if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
93d2bcd2 | 5073 | req_set_fail(req); |
889fca73 | 5074 | __io_req_complete(req, issue_flags, ret, cflags); |
fddaface | 5075 | return 0; |
fddaface JA |
5076 | } |
5077 | ||
3529d8c2 | 5078 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 5079 | { |
8ed8d3c3 JA |
5080 | struct io_accept *accept = &req->accept; |
5081 | ||
14587a46 | 5082 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 5083 | return -EINVAL; |
aaa4db12 | 5084 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
5085 | return -EINVAL; |
5086 | ||
d55e5f5b JA |
5087 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5088 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 5089 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 5090 | accept->nofile = rlimit(RLIMIT_NOFILE); |
a7083ad5 | 5091 | |
aaa4db12 PB |
5092 | accept->file_slot = READ_ONCE(sqe->file_index); |
5093 | if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) || | |
5094 | (accept->flags & SOCK_CLOEXEC))) | |
5095 | return -EINVAL; | |
a7083ad5 PB |
5096 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
5097 | return -EINVAL; | |
5098 | if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) | |
5099 | accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; | |
8ed8d3c3 | 5100 | return 0; |
8ed8d3c3 | 5101 | } |
17f2fe35 | 5102 | |
889fca73 | 5103 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
5104 | { |
5105 | struct io_accept *accept = &req->accept; | |
45d189c6 | 5106 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 5107 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
aaa4db12 | 5108 | bool fixed = !!accept->file_slot; |
a7083ad5 PB |
5109 | struct file *file; |
5110 | int ret, fd; | |
8ed8d3c3 | 5111 | |
e697deed JX |
5112 | if (req->file->f_flags & O_NONBLOCK) |
5113 | req->flags |= REQ_F_NOWAIT; | |
5114 | ||
aaa4db12 PB |
5115 | if (!fixed) { |
5116 | fd = __get_unused_fd_flags(accept->flags, accept->nofile); | |
5117 | if (unlikely(fd < 0)) | |
5118 | return fd; | |
5119 | } | |
a7083ad5 PB |
5120 | file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, |
5121 | accept->flags); | |
5122 | if (IS_ERR(file)) { | |
aaa4db12 PB |
5123 | if (!fixed) |
5124 | put_unused_fd(fd); | |
a7083ad5 PB |
5125 | ret = PTR_ERR(file); |
5126 | if (ret == -EAGAIN && force_nonblock) | |
5127 | return -EAGAIN; | |
ac45abc0 PB |
5128 | if (ret == -ERESTARTSYS) |
5129 | ret = -EINTR; | |
93d2bcd2 | 5130 | req_set_fail(req); |
aaa4db12 | 5131 | } else if (!fixed) { |
a7083ad5 PB |
5132 | fd_install(fd, file); |
5133 | ret = fd; | |
aaa4db12 PB |
5134 | } else { |
5135 | ret = io_install_fixed_file(req, file, issue_flags, | |
5136 | accept->file_slot - 1); | |
ac45abc0 | 5137 | } |
889fca73 | 5138 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 5139 | return 0; |
8ed8d3c3 JA |
5140 | } |
5141 | ||
93642ef8 PB |
5142 | static int io_connect_prep_async(struct io_kiocb *req) |
5143 | { | |
5144 | struct io_async_connect *io = req->async_data; | |
5145 | struct io_connect *conn = &req->connect; | |
5146 | ||
5147 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
5148 | } | |
5149 | ||
3529d8c2 | 5150 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 5151 | { |
3529d8c2 | 5152 | struct io_connect *conn = &req->connect; |
f499a021 | 5153 | |
14587a46 | 5154 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 | 5155 | return -EINVAL; |
26578cda PB |
5156 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
5157 | sqe->splice_fd_in) | |
3fbb51c1 JA |
5158 | return -EINVAL; |
5159 | ||
3529d8c2 JA |
5160 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5161 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 5162 | return 0; |
f499a021 JA |
5163 | } |
5164 | ||
889fca73 | 5165 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 5166 | { |
e8c2bc1f | 5167 | struct io_async_connect __io, *io; |
f8e85cf2 | 5168 | unsigned file_flags; |
3fbb51c1 | 5169 | int ret; |
45d189c6 | 5170 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 5171 | |
e8c2bc1f JA |
5172 | if (req->async_data) { |
5173 | io = req->async_data; | |
f499a021 | 5174 | } else { |
3529d8c2 JA |
5175 | ret = move_addr_to_kernel(req->connect.addr, |
5176 | req->connect.addr_len, | |
e8c2bc1f | 5177 | &__io.address); |
f499a021 JA |
5178 | if (ret) |
5179 | goto out; | |
5180 | io = &__io; | |
5181 | } | |
5182 | ||
3fbb51c1 JA |
5183 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
5184 | ||
e8c2bc1f | 5185 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 5186 | req->connect.addr_len, file_flags); |
87f80d62 | 5187 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 5188 | if (req->async_data) |
b7bb4f7d | 5189 | return -EAGAIN; |
e8c2bc1f | 5190 | if (io_alloc_async_data(req)) { |
f499a021 JA |
5191 | ret = -ENOMEM; |
5192 | goto out; | |
5193 | } | |
e8c2bc1f | 5194 | memcpy(req->async_data, &__io, sizeof(__io)); |
f8e85cf2 | 5195 | return -EAGAIN; |
f499a021 | 5196 | } |
f8e85cf2 JA |
5197 | if (ret == -ERESTARTSYS) |
5198 | ret = -EINTR; | |
f499a021 | 5199 | out: |
4e88d6e7 | 5200 | if (ret < 0) |
93d2bcd2 | 5201 | req_set_fail(req); |
889fca73 | 5202 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 5203 | return 0; |
469956e8 Y |
5204 | } |
5205 | #else /* !CONFIG_NET */ | |
99a10081 JA |
5206 | #define IO_NETOP_FN(op) \ |
5207 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
5208 | { \ | |
5209 | return -EOPNOTSUPP; \ | |
5210 | } | |
5211 | ||
5212 | #define IO_NETOP_PREP(op) \ | |
5213 | IO_NETOP_FN(op) \ | |
5214 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
5215 | { \ | |
5216 | return -EOPNOTSUPP; \ | |
5217 | } \ | |
5218 | ||
5219 | #define IO_NETOP_PREP_ASYNC(op) \ | |
5220 | IO_NETOP_PREP(op) \ | |
5221 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
5222 | { \ | |
5223 | return -EOPNOTSUPP; \ | |
5224 | } | |
5225 | ||
5226 | IO_NETOP_PREP_ASYNC(sendmsg); | |
5227 | IO_NETOP_PREP_ASYNC(recvmsg); | |
5228 | IO_NETOP_PREP_ASYNC(connect); | |
5229 | IO_NETOP_PREP(accept); | |
5230 | IO_NETOP_FN(send); | |
5231 | IO_NETOP_FN(recv); | |
469956e8 | 5232 | #endif /* CONFIG_NET */ |
f8e85cf2 | 5233 | |
d7718a9d JA |
5234 | struct io_poll_table { |
5235 | struct poll_table_struct pt; | |
5236 | struct io_kiocb *req; | |
68b11e8b | 5237 | int nr_entries; |
d7718a9d JA |
5238 | int error; |
5239 | }; | |
ce593a6c | 5240 | |
d7718a9d | 5241 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
5b0a6acc | 5242 | __poll_t mask, io_req_tw_func_t func) |
d7718a9d | 5243 | { |
d7718a9d JA |
5244 | /* for instances that support it check for an event match first: */ |
5245 | if (mask && !(mask & poll->events)) | |
5246 | return 0; | |
5247 | ||
5248 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
5249 | ||
5250 | list_del_init(&poll->wait.entry); | |
5251 | ||
d7718a9d | 5252 | req->result = mask; |
5b0a6acc | 5253 | req->io_task_work.func = func; |
6d816e08 | 5254 | |
d7718a9d | 5255 | /* |
e3aabf95 JA |
5256 | * If this fails, then the task is exiting. When a task exits, the |
5257 | * work gets canceled, so just cancel this request as well instead | |
5258 | * of executing it. We can't safely execute it anyway, as we may not | |
5259 | * have the needed state needed for it anyway. | |
d7718a9d | 5260 | */ |
e09ee510 | 5261 | io_req_task_work_add(req); |
d7718a9d JA |
5262 | return 1; |
5263 | } | |
5264 | ||
74ce6ce4 JA |
5265 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
5266 | __acquires(&req->ctx->completion_lock) | |
5267 | { | |
5268 | struct io_ring_ctx *ctx = req->ctx; | |
5269 | ||
316319e8 | 5270 | /* req->task == current here, checking PF_EXITING is safe */ |
e09ee510 PB |
5271 | if (unlikely(req->task->flags & PF_EXITING)) |
5272 | WRITE_ONCE(poll->canceled, true); | |
5273 | ||
74ce6ce4 JA |
5274 | if (!req->result && !READ_ONCE(poll->canceled)) { |
5275 | struct poll_table_struct pt = { ._key = poll->events }; | |
5276 | ||
5277 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
5278 | } | |
5279 | ||
79ebeaee | 5280 | spin_lock(&ctx->completion_lock); |
74ce6ce4 JA |
5281 | if (!req->result && !READ_ONCE(poll->canceled)) { |
5282 | add_wait_queue(poll->head, &poll->wait); | |
5283 | return true; | |
5284 | } | |
5285 | ||
5286 | return false; | |
5287 | } | |
5288 | ||
d4e7cd36 | 5289 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5290 | { |
e8c2bc1f | 5291 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5292 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5293 | return req->async_data; |
d4e7cd36 JA |
5294 | return req->apoll->double_poll; |
5295 | } | |
5296 | ||
5297 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5298 | { | |
5299 | if (req->opcode == IORING_OP_POLL_ADD) | |
5300 | return &req->poll; | |
5301 | return &req->apoll->poll; | |
5302 | } | |
5303 | ||
5304 | static void io_poll_remove_double(struct io_kiocb *req) | |
e07785b0 | 5305 | __must_hold(&req->ctx->completion_lock) |
d4e7cd36 JA |
5306 | { |
5307 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
5308 | |
5309 | lockdep_assert_held(&req->ctx->completion_lock); | |
5310 | ||
5311 | if (poll && poll->head) { | |
5312 | struct wait_queue_head *head = poll->head; | |
5313 | ||
79ebeaee | 5314 | spin_lock_irq(&head->lock); |
18bceab1 JA |
5315 | list_del_init(&poll->wait.entry); |
5316 | if (poll->wait.private) | |
de9b4cca | 5317 | req_ref_put(req); |
18bceab1 | 5318 | poll->head = NULL; |
79ebeaee | 5319 | spin_unlock_irq(&head->lock); |
18bceab1 JA |
5320 | } |
5321 | } | |
5322 | ||
31efe48e | 5323 | static bool __io_poll_complete(struct io_kiocb *req, __poll_t mask) |
e07785b0 | 5324 | __must_hold(&req->ctx->completion_lock) |
18bceab1 JA |
5325 | { |
5326 | struct io_ring_ctx *ctx = req->ctx; | |
88e41cf9 | 5327 | unsigned flags = IORING_CQE_F_MORE; |
e27414be | 5328 | int error; |
18bceab1 | 5329 | |
e27414be | 5330 | if (READ_ONCE(req->poll.canceled)) { |
45ab03b1 | 5331 | error = -ECANCELED; |
88e41cf9 | 5332 | req->poll.events |= EPOLLONESHOT; |
e27414be | 5333 | } else { |
5082620f | 5334 | error = mangle_poll(mask); |
e27414be | 5335 | } |
b69de288 JA |
5336 | if (req->poll.events & EPOLLONESHOT) |
5337 | flags = 0; | |
a62682f9 HX |
5338 | if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) { |
5339 | req->poll.events |= EPOLLONESHOT; | |
88e41cf9 | 5340 | flags = 0; |
a62682f9 | 5341 | } |
7b289c38 HX |
5342 | if (flags & IORING_CQE_F_MORE) |
5343 | ctx->cq_extra++; | |
18bceab1 | 5344 | |
88e41cf9 | 5345 | return !(flags & IORING_CQE_F_MORE); |
18bceab1 JA |
5346 | } |
5347 | ||
31efe48e XW |
5348 | static inline bool io_poll_complete(struct io_kiocb *req, __poll_t mask) |
5349 | __must_hold(&req->ctx->completion_lock) | |
5350 | { | |
5351 | bool done; | |
5352 | ||
5353 | done = __io_poll_complete(req, mask); | |
5354 | io_commit_cqring(req->ctx); | |
5355 | return done; | |
5356 | } | |
5357 | ||
f237c30a | 5358 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 JA |
5359 | { |
5360 | struct io_ring_ctx *ctx = req->ctx; | |
dd221f46 | 5361 | struct io_kiocb *nxt; |
18bceab1 JA |
5362 | |
5363 | if (io_poll_rewait(req, &req->poll)) { | |
79ebeaee | 5364 | spin_unlock(&ctx->completion_lock); |
dd221f46 | 5365 | } else { |
f40b964a | 5366 | bool done; |
18bceab1 | 5367 | |
5b7aa38d HX |
5368 | if (req->poll.done) { |
5369 | spin_unlock(&ctx->completion_lock); | |
5370 | return; | |
5371 | } | |
31efe48e | 5372 | done = __io_poll_complete(req, req->result); |
88e41cf9 | 5373 | if (done) { |
a890d01e | 5374 | io_poll_remove_double(req); |
88e41cf9 | 5375 | hash_del(&req->hash_node); |
bd99c71b | 5376 | req->poll.done = true; |
f40b964a | 5377 | } else { |
88e41cf9 JA |
5378 | req->result = 0; |
5379 | add_wait_queue(req->poll.head, &req->poll.wait); | |
5380 | } | |
31efe48e | 5381 | io_commit_cqring(ctx); |
79ebeaee | 5382 | spin_unlock(&ctx->completion_lock); |
dd221f46 | 5383 | io_cqring_ev_posted(ctx); |
18bceab1 | 5384 | |
88e41cf9 JA |
5385 | if (done) { |
5386 | nxt = io_put_req_find_next(req); | |
5387 | if (nxt) | |
f237c30a | 5388 | io_req_task_submit(nxt, locked); |
88e41cf9 | 5389 | } |
dd221f46 | 5390 | } |
18bceab1 JA |
5391 | } |
5392 | ||
5393 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
5394 | int sync, void *key) | |
5395 | { | |
5396 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 5397 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 | 5398 | __poll_t mask = key_to_poll(key); |
79ebeaee | 5399 | unsigned long flags; |
18bceab1 JA |
5400 | |
5401 | /* for instances that support it check for an event match first: */ | |
5402 | if (mask && !(mask & poll->events)) | |
5403 | return 0; | |
88e41cf9 JA |
5404 | if (!(poll->events & EPOLLONESHOT)) |
5405 | return poll->wait.func(&poll->wait, mode, sync, key); | |
18bceab1 | 5406 | |
8706e04e JA |
5407 | list_del_init(&wait->entry); |
5408 | ||
9ce85ef2 | 5409 | if (poll->head) { |
18bceab1 JA |
5410 | bool done; |
5411 | ||
79ebeaee | 5412 | spin_lock_irqsave(&poll->head->lock, flags); |
807abcb0 | 5413 | done = list_empty(&poll->wait.entry); |
18bceab1 | 5414 | if (!done) |
807abcb0 | 5415 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
5416 | /* make sure double remove sees this as being gone */ |
5417 | wait->private = NULL; | |
79ebeaee | 5418 | spin_unlock_irqrestore(&poll->head->lock, flags); |
c8b5e260 JA |
5419 | if (!done) { |
5420 | /* use wait func handler, so it matches the rq type */ | |
5421 | poll->wait.func(&poll->wait, mode, sync, key); | |
5422 | } | |
18bceab1 | 5423 | } |
de9b4cca | 5424 | req_ref_put(req); |
18bceab1 JA |
5425 | return 1; |
5426 | } | |
5427 | ||
5428 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
5429 | wait_queue_func_t wake_func) | |
5430 | { | |
5431 | poll->head = NULL; | |
5432 | poll->done = false; | |
5433 | poll->canceled = false; | |
464dca61 JA |
5434 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
5435 | /* mask in events that we always want/need */ | |
5436 | poll->events = events | IO_POLL_UNMASK; | |
18bceab1 JA |
5437 | INIT_LIST_HEAD(&poll->wait.entry); |
5438 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
5439 | } | |
5440 | ||
5441 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
5442 | struct wait_queue_head *head, |
5443 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
5444 | { |
5445 | struct io_kiocb *req = pt->req; | |
5446 | ||
5447 | /* | |
68b11e8b PB |
5448 | * The file being polled uses multiple waitqueues for poll handling |
5449 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb | |
5450 | * if this happens. | |
18bceab1 | 5451 | */ |
68b11e8b | 5452 | if (unlikely(pt->nr_entries)) { |
58852d4d PB |
5453 | struct io_poll_iocb *poll_one = poll; |
5454 | ||
23a65db8 PB |
5455 | /* double add on the same waitqueue head, ignore */ |
5456 | if (poll_one->head == head) | |
5457 | return; | |
18bceab1 | 5458 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5459 | if (*poll_ptr) { |
23a65db8 PB |
5460 | if ((*poll_ptr)->head == head) |
5461 | return; | |
18bceab1 JA |
5462 | pt->error = -EINVAL; |
5463 | return; | |
5464 | } | |
ea6a693d JA |
5465 | /* |
5466 | * Can't handle multishot for double wait for now, turn it | |
5467 | * into one-shot mode. | |
5468 | */ | |
7a274727 PB |
5469 | if (!(poll_one->events & EPOLLONESHOT)) |
5470 | poll_one->events |= EPOLLONESHOT; | |
18bceab1 JA |
5471 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
5472 | if (!poll) { | |
5473 | pt->error = -ENOMEM; | |
5474 | return; | |
5475 | } | |
58852d4d | 5476 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
de9b4cca | 5477 | req_ref_get(req); |
18bceab1 | 5478 | poll->wait.private = req; |
807abcb0 | 5479 | *poll_ptr = poll; |
18bceab1 JA |
5480 | } |
5481 | ||
68b11e8b | 5482 | pt->nr_entries++; |
18bceab1 | 5483 | poll->head = head; |
a31eb4a2 JX |
5484 | |
5485 | if (poll->events & EPOLLEXCLUSIVE) | |
5486 | add_wait_queue_exclusive(head, &poll->wait); | |
5487 | else | |
5488 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5489 | } |
5490 | ||
5491 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5492 | struct poll_table_struct *p) | |
5493 | { | |
5494 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5495 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5496 | |
807abcb0 | 5497 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5498 | } |
5499 | ||
f237c30a | 5500 | static void io_async_task_func(struct io_kiocb *req, bool *locked) |
d7718a9d | 5501 | { |
d7718a9d JA |
5502 | struct async_poll *apoll = req->apoll; |
5503 | struct io_ring_ctx *ctx = req->ctx; | |
5504 | ||
236daeae | 5505 | trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data); |
d7718a9d | 5506 | |
74ce6ce4 | 5507 | if (io_poll_rewait(req, &apoll->poll)) { |
79ebeaee | 5508 | spin_unlock(&ctx->completion_lock); |
74ce6ce4 | 5509 | return; |
d7718a9d JA |
5510 | } |
5511 | ||
0ea13b44 | 5512 | hash_del(&req->hash_node); |
d4e7cd36 | 5513 | io_poll_remove_double(req); |
bd99c71b | 5514 | apoll->poll.done = true; |
79ebeaee | 5515 | spin_unlock(&ctx->completion_lock); |
74ce6ce4 | 5516 | |
0be0b0e3 | 5517 | if (!READ_ONCE(apoll->poll.canceled)) |
f237c30a | 5518 | io_req_task_submit(req, locked); |
0be0b0e3 | 5519 | else |
2593553a | 5520 | io_req_complete_failed(req, -ECANCELED); |
d7718a9d JA |
5521 | } |
5522 | ||
5523 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5524 | void *key) | |
5525 | { | |
5526 | struct io_kiocb *req = wait->private; | |
5527 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5528 | ||
5529 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5530 | key_to_poll(key)); | |
5531 | ||
5532 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5533 | } | |
5534 | ||
5535 | static void io_poll_req_insert(struct io_kiocb *req) | |
5536 | { | |
5537 | struct io_ring_ctx *ctx = req->ctx; | |
5538 | struct hlist_head *list; | |
5539 | ||
5540 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5541 | hlist_add_head(&req->hash_node, list); | |
5542 | } | |
5543 | ||
5544 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5545 | struct io_poll_iocb *poll, | |
5546 | struct io_poll_table *ipt, __poll_t mask, | |
5547 | wait_queue_func_t wake_func) | |
5548 | __acquires(&ctx->completion_lock) | |
5549 | { | |
5550 | struct io_ring_ctx *ctx = req->ctx; | |
5551 | bool cancel = false; | |
5552 | ||
4d52f338 | 5553 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5554 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5555 | poll->file = req->file; |
18bceab1 | 5556 | poll->wait.private = req; |
d7718a9d JA |
5557 | |
5558 | ipt->pt._key = mask; | |
5559 | ipt->req = req; | |
68b11e8b PB |
5560 | ipt->error = 0; |
5561 | ipt->nr_entries = 0; | |
d7718a9d | 5562 | |
d7718a9d | 5563 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
68b11e8b PB |
5564 | if (unlikely(!ipt->nr_entries) && !ipt->error) |
5565 | ipt->error = -EINVAL; | |
d7718a9d | 5566 | |
79ebeaee | 5567 | spin_lock(&ctx->completion_lock); |
a890d01e | 5568 | if (ipt->error || (mask && (poll->events & EPOLLONESHOT))) |
46fee9ab | 5569 | io_poll_remove_double(req); |
d7718a9d | 5570 | if (likely(poll->head)) { |
79ebeaee | 5571 | spin_lock_irq(&poll->head->lock); |
d7718a9d JA |
5572 | if (unlikely(list_empty(&poll->wait.entry))) { |
5573 | if (ipt->error) | |
5574 | cancel = true; | |
5575 | ipt->error = 0; | |
5576 | mask = 0; | |
5577 | } | |
88e41cf9 | 5578 | if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error) |
d7718a9d JA |
5579 | list_del_init(&poll->wait.entry); |
5580 | else if (cancel) | |
5581 | WRITE_ONCE(poll->canceled, true); | |
5582 | else if (!poll->done) /* actually waiting for an event */ | |
5583 | io_poll_req_insert(req); | |
79ebeaee | 5584 | spin_unlock_irq(&poll->head->lock); |
d7718a9d JA |
5585 | } |
5586 | ||
5587 | return mask; | |
5588 | } | |
5589 | ||
59b735ae OL |
5590 | enum { |
5591 | IO_APOLL_OK, | |
5592 | IO_APOLL_ABORTED, | |
5593 | IO_APOLL_READY | |
5594 | }; | |
5595 | ||
5596 | static int io_arm_poll_handler(struct io_kiocb *req) | |
d7718a9d JA |
5597 | { |
5598 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5599 | struct io_ring_ctx *ctx = req->ctx; | |
5600 | struct async_poll *apoll; | |
5601 | struct io_poll_table ipt; | |
b2d9c3da | 5602 | __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI; |
9dab14b8 | 5603 | int rw; |
d7718a9d JA |
5604 | |
5605 | if (!req->file || !file_can_poll(req->file)) | |
59b735ae | 5606 | return IO_APOLL_ABORTED; |
24c74678 | 5607 | if (req->flags & REQ_F_POLLED) |
59b735ae | 5608 | return IO_APOLL_ABORTED; |
b2d9c3da PB |
5609 | if (!def->pollin && !def->pollout) |
5610 | return IO_APOLL_ABORTED; | |
5611 | ||
5612 | if (def->pollin) { | |
9dab14b8 | 5613 | rw = READ; |
b2d9c3da PB |
5614 | mask |= POLLIN | POLLRDNORM; |
5615 | ||
5616 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5617 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5618 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5619 | mask &= ~POLLIN; | |
5620 | } else { | |
9dab14b8 | 5621 | rw = WRITE; |
b2d9c3da PB |
5622 | mask |= POLLOUT | POLLWRNORM; |
5623 | } | |
5624 | ||
9dab14b8 | 5625 | /* if we can't nonblock try, then no point in arming a poll handler */ |
b191e2df | 5626 | if (!io_file_supports_nowait(req, rw)) |
59b735ae | 5627 | return IO_APOLL_ABORTED; |
d7718a9d JA |
5628 | |
5629 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
5630 | if (unlikely(!apoll)) | |
59b735ae | 5631 | return IO_APOLL_ABORTED; |
807abcb0 | 5632 | apoll->double_poll = NULL; |
d7718a9d | 5633 | req->apoll = apoll; |
b2d9c3da | 5634 | req->flags |= REQ_F_POLLED; |
d7718a9d | 5635 | ipt.pt._qproc = io_async_queue_proc; |
48dcd38d | 5636 | io_req_set_refcount(req); |
d7718a9d JA |
5637 | |
5638 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5639 | io_async_wake); | |
79ebeaee | 5640 | spin_unlock(&ctx->completion_lock); |
41a5169c HX |
5641 | if (ret || ipt.error) |
5642 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; | |
5643 | ||
236daeae OL |
5644 | trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data, |
5645 | mask, apoll->poll.events); | |
59b735ae | 5646 | return IO_APOLL_OK; |
d7718a9d JA |
5647 | } |
5648 | ||
5649 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
b2e720ac | 5650 | struct io_poll_iocb *poll, bool do_cancel) |
e07785b0 | 5651 | __must_hold(&req->ctx->completion_lock) |
221c5eb2 | 5652 | { |
b41e9852 | 5653 | bool do_complete = false; |
221c5eb2 | 5654 | |
5082620f JA |
5655 | if (!poll->head) |
5656 | return false; | |
79ebeaee | 5657 | spin_lock_irq(&poll->head->lock); |
b2e720ac JA |
5658 | if (do_cancel) |
5659 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5660 | if (!list_empty(&poll->wait.entry)) { |
5661 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5662 | do_complete = true; |
221c5eb2 | 5663 | } |
79ebeaee | 5664 | spin_unlock_irq(&poll->head->lock); |
3bfa5bcb | 5665 | hash_del(&req->hash_node); |
d7718a9d JA |
5666 | return do_complete; |
5667 | } | |
5668 | ||
5d709043 | 5669 | static bool io_poll_remove_one(struct io_kiocb *req) |
e07785b0 | 5670 | __must_hold(&req->ctx->completion_lock) |
d7718a9d JA |
5671 | { |
5672 | bool do_complete; | |
5673 | ||
d4e7cd36 | 5674 | io_poll_remove_double(req); |
e31001a3 | 5675 | do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true); |
d4e7cd36 | 5676 | |
b41e9852 | 5677 | if (do_complete) { |
d4d19c19 | 5678 | io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0); |
b41e9852 | 5679 | io_commit_cqring(req->ctx); |
93d2bcd2 | 5680 | req_set_fail(req); |
91c2f697 | 5681 | io_put_req_deferred(req); |
5d709043 | 5682 | } |
b41e9852 | 5683 | return do_complete; |
221c5eb2 JA |
5684 | } |
5685 | ||
76e1b642 JA |
5686 | /* |
5687 | * Returns true if we found and killed one or more poll requests | |
5688 | */ | |
6b81928d | 5689 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
3dd0c97a | 5690 | bool cancel_all) |
221c5eb2 | 5691 | { |
78076bb6 | 5692 | struct hlist_node *tmp; |
221c5eb2 | 5693 | struct io_kiocb *req; |
8e2e1faf | 5694 | int posted = 0, i; |
221c5eb2 | 5695 | |
79ebeaee | 5696 | spin_lock(&ctx->completion_lock); |
78076bb6 JA |
5697 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5698 | struct hlist_head *list; | |
5699 | ||
5700 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5701 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
3dd0c97a | 5702 | if (io_match_task(req, tsk, cancel_all)) |
f3606e3a JA |
5703 | posted += io_poll_remove_one(req); |
5704 | } | |
221c5eb2 | 5705 | } |
79ebeaee | 5706 | spin_unlock(&ctx->completion_lock); |
b41e9852 | 5707 | |
8e2e1faf JA |
5708 | if (posted) |
5709 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5710 | |
5711 | return posted != 0; | |
221c5eb2 JA |
5712 | } |
5713 | ||
9ba5fac8 PB |
5714 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5715 | bool poll_only) | |
e07785b0 | 5716 | __must_hold(&ctx->completion_lock) |
47f46768 | 5717 | { |
78076bb6 | 5718 | struct hlist_head *list; |
47f46768 JA |
5719 | struct io_kiocb *req; |
5720 | ||
78076bb6 JA |
5721 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5722 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5723 | if (sqe_addr != req->user_data) |
5724 | continue; | |
9ba5fac8 PB |
5725 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
5726 | continue; | |
b2cb805f | 5727 | return req; |
47f46768 | 5728 | } |
b2cb805f JA |
5729 | return NULL; |
5730 | } | |
5731 | ||
9ba5fac8 PB |
5732 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5733 | bool poll_only) | |
e07785b0 | 5734 | __must_hold(&ctx->completion_lock) |
b2cb805f JA |
5735 | { |
5736 | struct io_kiocb *req; | |
5737 | ||
9ba5fac8 | 5738 | req = io_poll_find(ctx, sqe_addr, poll_only); |
b2cb805f JA |
5739 | if (!req) |
5740 | return -ENOENT; | |
5741 | if (io_poll_remove_one(req)) | |
5742 | return 0; | |
5743 | ||
5744 | return -EALREADY; | |
47f46768 JA |
5745 | } |
5746 | ||
9096af3e PB |
5747 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
5748 | unsigned int flags) | |
5749 | { | |
5750 | u32 events; | |
47f46768 | 5751 | |
9096af3e PB |
5752 | events = READ_ONCE(sqe->poll32_events); |
5753 | #ifdef __BIG_ENDIAN | |
5754 | events = swahw32(events); | |
5755 | #endif | |
5756 | if (!(flags & IORING_POLL_ADD_MULTI)) | |
5757 | events |= EPOLLONESHOT; | |
5758 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); | |
47f46768 JA |
5759 | } |
5760 | ||
c5de0036 | 5761 | static int io_poll_update_prep(struct io_kiocb *req, |
3529d8c2 | 5762 | const struct io_uring_sqe *sqe) |
0969e783 | 5763 | { |
c5de0036 PB |
5764 | struct io_poll_update *upd = &req->poll_update; |
5765 | u32 flags; | |
5766 | ||
0969e783 JA |
5767 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5768 | return -EINVAL; | |
26578cda | 5769 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
c5de0036 PB |
5770 | return -EINVAL; |
5771 | flags = READ_ONCE(sqe->len); | |
5772 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | | |
5773 | IORING_POLL_ADD_MULTI)) | |
5774 | return -EINVAL; | |
5775 | /* meaningless without update */ | |
5776 | if (flags == IORING_POLL_ADD_MULTI) | |
0969e783 JA |
5777 | return -EINVAL; |
5778 | ||
c5de0036 PB |
5779 | upd->old_user_data = READ_ONCE(sqe->addr); |
5780 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; | |
5781 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; | |
221c5eb2 | 5782 | |
c5de0036 PB |
5783 | upd->new_user_data = READ_ONCE(sqe->off); |
5784 | if (!upd->update_user_data && upd->new_user_data) | |
5785 | return -EINVAL; | |
5786 | if (upd->update_events) | |
5787 | upd->events = io_poll_parse_events(sqe, flags); | |
5788 | else if (sqe->poll32_events) | |
5789 | return -EINVAL; | |
221c5eb2 | 5790 | |
221c5eb2 JA |
5791 | return 0; |
5792 | } | |
5793 | ||
221c5eb2 JA |
5794 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5795 | void *key) | |
5796 | { | |
c2f2eb7d JA |
5797 | struct io_kiocb *req = wait->private; |
5798 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5799 | |
d7718a9d | 5800 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5801 | } |
5802 | ||
221c5eb2 JA |
5803 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5804 | struct poll_table_struct *p) | |
5805 | { | |
5806 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5807 | ||
e8c2bc1f | 5808 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5809 | } |
5810 | ||
3529d8c2 | 5811 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5812 | { |
5813 | struct io_poll_iocb *poll = &req->poll; | |
c5de0036 | 5814 | u32 flags; |
221c5eb2 JA |
5815 | |
5816 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5817 | return -EINVAL; | |
c5de0036 | 5818 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
88e41cf9 JA |
5819 | return -EINVAL; |
5820 | flags = READ_ONCE(sqe->len); | |
c5de0036 | 5821 | if (flags & ~IORING_POLL_ADD_MULTI) |
221c5eb2 JA |
5822 | return -EINVAL; |
5823 | ||
48dcd38d | 5824 | io_req_set_refcount(req); |
c5de0036 | 5825 | poll->events = io_poll_parse_events(sqe, flags); |
0969e783 JA |
5826 | return 0; |
5827 | } | |
5828 | ||
61e98203 | 5829 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
5830 | { |
5831 | struct io_poll_iocb *poll = &req->poll; | |
5832 | struct io_ring_ctx *ctx = req->ctx; | |
5833 | struct io_poll_table ipt; | |
0969e783 | 5834 | __poll_t mask; |
5b7aa38d | 5835 | bool done; |
0969e783 | 5836 | |
d7718a9d | 5837 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5838 | |
d7718a9d JA |
5839 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5840 | io_poll_wake); | |
221c5eb2 | 5841 | |
8c838788 | 5842 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5843 | ipt.error = 0; |
5b7aa38d | 5844 | done = io_poll_complete(req, mask); |
221c5eb2 | 5845 | } |
79ebeaee | 5846 | spin_unlock(&ctx->completion_lock); |
221c5eb2 | 5847 | |
8c838788 JA |
5848 | if (mask) { |
5849 | io_cqring_ev_posted(ctx); | |
5b7aa38d | 5850 | if (done) |
88e41cf9 | 5851 | io_put_req(req); |
221c5eb2 | 5852 | } |
8c838788 | 5853 | return ipt.error; |
221c5eb2 JA |
5854 | } |
5855 | ||
c5de0036 | 5856 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
b69de288 JA |
5857 | { |
5858 | struct io_ring_ctx *ctx = req->ctx; | |
5859 | struct io_kiocb *preq; | |
cb3b200e | 5860 | bool completing; |
b69de288 JA |
5861 | int ret; |
5862 | ||
79ebeaee | 5863 | spin_lock(&ctx->completion_lock); |
9ba5fac8 | 5864 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
b69de288 JA |
5865 | if (!preq) { |
5866 | ret = -ENOENT; | |
5867 | goto err; | |
b69de288 | 5868 | } |
cb3b200e | 5869 | |
c5de0036 PB |
5870 | if (!req->poll_update.update_events && !req->poll_update.update_user_data) { |
5871 | completing = true; | |
5872 | ret = io_poll_remove_one(preq) ? 0 : -EALREADY; | |
5873 | goto err; | |
5874 | } | |
5875 | ||
cb3b200e JA |
5876 | /* |
5877 | * Don't allow racy completion with singleshot, as we cannot safely | |
5878 | * update those. For multishot, if we're racing with completion, just | |
5879 | * let completion re-add it. | |
5880 | */ | |
5881 | completing = !__io_poll_remove_one(preq, &preq->poll, false); | |
5882 | if (completing && (preq->poll.events & EPOLLONESHOT)) { | |
5883 | ret = -EALREADY; | |
5884 | goto err; | |
b69de288 JA |
5885 | } |
5886 | /* we now have a detached poll request. reissue. */ | |
5887 | ret = 0; | |
5888 | err: | |
b69de288 | 5889 | if (ret < 0) { |
79ebeaee | 5890 | spin_unlock(&ctx->completion_lock); |
93d2bcd2 | 5891 | req_set_fail(req); |
b69de288 JA |
5892 | io_req_complete(req, ret); |
5893 | return 0; | |
5894 | } | |
5895 | /* only mask one event flags, keep behavior flags */ | |
9d805892 | 5896 | if (req->poll_update.update_events) { |
b69de288 | 5897 | preq->poll.events &= ~0xffff; |
9d805892 | 5898 | preq->poll.events |= req->poll_update.events & 0xffff; |
b69de288 JA |
5899 | preq->poll.events |= IO_POLL_UNMASK; |
5900 | } | |
9d805892 PB |
5901 | if (req->poll_update.update_user_data) |
5902 | preq->user_data = req->poll_update.new_user_data; | |
79ebeaee | 5903 | spin_unlock(&ctx->completion_lock); |
cb3b200e | 5904 | |
b69de288 JA |
5905 | /* complete update request, we're done with it */ |
5906 | io_req_complete(req, ret); | |
5907 | ||
cb3b200e | 5908 | if (!completing) { |
c5de0036 | 5909 | ret = io_poll_add(preq, issue_flags); |
cb3b200e | 5910 | if (ret < 0) { |
93d2bcd2 | 5911 | req_set_fail(preq); |
cb3b200e JA |
5912 | io_req_complete(preq, ret); |
5913 | } | |
b69de288 JA |
5914 | } |
5915 | return 0; | |
5916 | } | |
5917 | ||
f237c30a | 5918 | static void io_req_task_timeout(struct io_kiocb *req, bool *locked) |
89850fce | 5919 | { |
89850fce | 5920 | req_set_fail(req); |
505657bc | 5921 | io_req_complete_post(req, -ETIME, 0); |
89850fce JA |
5922 | } |
5923 | ||
5262f567 JA |
5924 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5925 | { | |
ad8a48ac JA |
5926 | struct io_timeout_data *data = container_of(timer, |
5927 | struct io_timeout_data, timer); | |
5928 | struct io_kiocb *req = data->req; | |
5929 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5930 | unsigned long flags; |
5931 | ||
89850fce | 5932 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
a71976f3 | 5933 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5934 | atomic_set(&req->ctx->cq_timeouts, |
5935 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
89850fce | 5936 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
01cec8c1 | 5937 | |
89850fce JA |
5938 | req->io_task_work.func = io_req_task_timeout; |
5939 | io_req_task_work_add(req); | |
5262f567 JA |
5940 | return HRTIMER_NORESTART; |
5941 | } | |
5942 | ||
fbd15848 PB |
5943 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5944 | __u64 user_data) | |
89850fce | 5945 | __must_hold(&ctx->timeout_lock) |
f254ac04 | 5946 | { |
fbd15848 | 5947 | struct io_timeout_data *io; |
47f46768 | 5948 | struct io_kiocb *req; |
fd9c7bc5 | 5949 | bool found = false; |
f254ac04 | 5950 | |
135fcde8 | 5951 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
fd9c7bc5 PB |
5952 | found = user_data == req->user_data; |
5953 | if (found) | |
47f46768 | 5954 | break; |
47f46768 | 5955 | } |
fd9c7bc5 PB |
5956 | if (!found) |
5957 | return ERR_PTR(-ENOENT); | |
fbd15848 PB |
5958 | |
5959 | io = req->async_data; | |
fd9c7bc5 | 5960 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
fbd15848 | 5961 | return ERR_PTR(-EALREADY); |
a71976f3 | 5962 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5963 | return req; |
5964 | } | |
47f46768 | 5965 | |
fbd15848 | 5966 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
ec3c3d0f | 5967 | __must_hold(&ctx->completion_lock) |
89850fce | 5968 | __must_hold(&ctx->timeout_lock) |
fbd15848 PB |
5969 | { |
5970 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5971 | ||
5972 | if (IS_ERR(req)) | |
5973 | return PTR_ERR(req); | |
f254ac04 | 5974 | |
93d2bcd2 | 5975 | req_set_fail(req); |
d4d19c19 | 5976 | io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0); |
91c2f697 | 5977 | io_put_req_deferred(req); |
f254ac04 JA |
5978 | return 0; |
5979 | } | |
5980 | ||
50c1df2b JA |
5981 | static clockid_t io_timeout_get_clock(struct io_timeout_data *data) |
5982 | { | |
5983 | switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) { | |
5984 | case IORING_TIMEOUT_BOOTTIME: | |
5985 | return CLOCK_BOOTTIME; | |
5986 | case IORING_TIMEOUT_REALTIME: | |
5987 | return CLOCK_REALTIME; | |
5988 | default: | |
5989 | /* can't happen, vetted at prep time */ | |
5990 | WARN_ON_ONCE(1); | |
5991 | fallthrough; | |
5992 | case 0: | |
5993 | return CLOCK_MONOTONIC; | |
5994 | } | |
5995 | } | |
5996 | ||
f1042b6c PB |
5997 | static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5998 | struct timespec64 *ts, enum hrtimer_mode mode) | |
5999 | __must_hold(&ctx->timeout_lock) | |
6000 | { | |
6001 | struct io_timeout_data *io; | |
6002 | struct io_kiocb *req; | |
6003 | bool found = false; | |
6004 | ||
6005 | list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) { | |
6006 | found = user_data == req->user_data; | |
6007 | if (found) | |
6008 | break; | |
6009 | } | |
6010 | if (!found) | |
6011 | return -ENOENT; | |
6012 | ||
6013 | io = req->async_data; | |
6014 | if (hrtimer_try_to_cancel(&io->timer) == -1) | |
6015 | return -EALREADY; | |
6016 | hrtimer_init(&io->timer, io_timeout_get_clock(io), mode); | |
6017 | io->timer.function = io_link_timeout_fn; | |
6018 | hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode); | |
6019 | return 0; | |
6020 | } | |
6021 | ||
9c8e11b3 PB |
6022 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
6023 | struct timespec64 *ts, enum hrtimer_mode mode) | |
89850fce | 6024 | __must_hold(&ctx->timeout_lock) |
47f46768 | 6025 | { |
9c8e11b3 PB |
6026 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
6027 | struct io_timeout_data *data; | |
47f46768 | 6028 | |
9c8e11b3 PB |
6029 | if (IS_ERR(req)) |
6030 | return PTR_ERR(req); | |
47f46768 | 6031 | |
9c8e11b3 PB |
6032 | req->timeout.off = 0; /* noseq */ |
6033 | data = req->async_data; | |
6034 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
50c1df2b | 6035 | hrtimer_init(&data->timer, io_timeout_get_clock(data), mode); |
9c8e11b3 PB |
6036 | data->timer.function = io_timeout_fn; |
6037 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
6038 | return 0; | |
47f46768 JA |
6039 | } |
6040 | ||
3529d8c2 JA |
6041 | static int io_timeout_remove_prep(struct io_kiocb *req, |
6042 | const struct io_uring_sqe *sqe) | |
b29472ee | 6043 | { |
9c8e11b3 PB |
6044 | struct io_timeout_rem *tr = &req->timeout_rem; |
6045 | ||
b29472ee JA |
6046 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
6047 | return -EINVAL; | |
61710e43 DA |
6048 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6049 | return -EINVAL; | |
26578cda | 6050 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
b29472ee JA |
6051 | return -EINVAL; |
6052 | ||
f1042b6c | 6053 | tr->ltimeout = false; |
9c8e11b3 PB |
6054 | tr->addr = READ_ONCE(sqe->addr); |
6055 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
f1042b6c PB |
6056 | if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) { |
6057 | if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
6058 | return -EINVAL; | |
6059 | if (tr->flags & IORING_LINK_TIMEOUT_UPDATE) | |
6060 | tr->ltimeout = true; | |
6061 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS)) | |
9c8e11b3 PB |
6062 | return -EINVAL; |
6063 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
6064 | return -EFAULT; | |
6065 | } else if (tr->flags) { | |
6066 | /* timeout removal doesn't support flags */ | |
b29472ee | 6067 | return -EINVAL; |
9c8e11b3 | 6068 | } |
b29472ee | 6069 | |
b29472ee JA |
6070 | return 0; |
6071 | } | |
6072 | ||
8662daec PB |
6073 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
6074 | { | |
6075 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
6076 | : HRTIMER_MODE_REL; | |
6077 | } | |
6078 | ||
11365043 JA |
6079 | /* |
6080 | * Remove or update an existing timeout command | |
6081 | */ | |
61e98203 | 6082 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 6083 | { |
9c8e11b3 | 6084 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 6085 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 6086 | int ret; |
11365043 | 6087 | |
ec3c3d0f PB |
6088 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
6089 | spin_lock(&ctx->completion_lock); | |
6090 | spin_lock_irq(&ctx->timeout_lock); | |
9c8e11b3 | 6091 | ret = io_timeout_cancel(ctx, tr->addr); |
ec3c3d0f PB |
6092 | spin_unlock_irq(&ctx->timeout_lock); |
6093 | spin_unlock(&ctx->completion_lock); | |
6094 | } else { | |
f1042b6c PB |
6095 | enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags); |
6096 | ||
ec3c3d0f | 6097 | spin_lock_irq(&ctx->timeout_lock); |
f1042b6c PB |
6098 | if (tr->ltimeout) |
6099 | ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
6100 | else | |
6101 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
ec3c3d0f PB |
6102 | spin_unlock_irq(&ctx->timeout_lock); |
6103 | } | |
11365043 | 6104 | |
4e88d6e7 | 6105 | if (ret < 0) |
93d2bcd2 | 6106 | req_set_fail(req); |
505657bc | 6107 | io_req_complete_post(req, ret, 0); |
11365043 | 6108 | return 0; |
5262f567 JA |
6109 | } |
6110 | ||
3529d8c2 | 6111 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 6112 | bool is_timeout_link) |
5262f567 | 6113 | { |
ad8a48ac | 6114 | struct io_timeout_data *data; |
a41525ab | 6115 | unsigned flags; |
56080b02 | 6116 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 6117 | |
ad8a48ac | 6118 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 6119 | return -EINVAL; |
26578cda PB |
6120 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
6121 | sqe->splice_fd_in) | |
a41525ab | 6122 | return -EINVAL; |
56080b02 | 6123 | if (off && is_timeout_link) |
2d28390a | 6124 | return -EINVAL; |
a41525ab | 6125 | flags = READ_ONCE(sqe->timeout_flags); |
50c1df2b JA |
6126 | if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK)) |
6127 | return -EINVAL; | |
6128 | /* more than one clock specified is invalid, obviously */ | |
6129 | if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
5262f567 | 6130 | return -EINVAL; |
bdf20073 | 6131 | |
ef9dd637 | 6132 | INIT_LIST_HEAD(&req->timeout.list); |
bfe68a22 | 6133 | req->timeout.off = off; |
f18ee4cf PB |
6134 | if (unlikely(off && !req->ctx->off_timeout_used)) |
6135 | req->ctx->off_timeout_used = true; | |
26a61679 | 6136 | |
e8c2bc1f | 6137 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
6138 | return -ENOMEM; |
6139 | ||
e8c2bc1f | 6140 | data = req->async_data; |
ad8a48ac | 6141 | data->req = req; |
50c1df2b | 6142 | data->flags = flags; |
ad8a48ac JA |
6143 | |
6144 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
6145 | return -EFAULT; |
6146 | ||
8662daec | 6147 | data->mode = io_translate_timeout_mode(flags); |
50c1df2b | 6148 | hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode); |
b97e736a PB |
6149 | |
6150 | if (is_timeout_link) { | |
6151 | struct io_submit_link *link = &req->ctx->submit_state.link; | |
6152 | ||
6153 | if (!link->head) | |
6154 | return -EINVAL; | |
6155 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) | |
6156 | return -EINVAL; | |
4d13d1a4 PB |
6157 | req->timeout.head = link->last; |
6158 | link->last->flags |= REQ_F_ARM_LTIMEOUT; | |
b97e736a | 6159 | } |
ad8a48ac JA |
6160 | return 0; |
6161 | } | |
6162 | ||
61e98203 | 6163 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 6164 | { |
ad8a48ac | 6165 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 6166 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 6167 | struct list_head *entry; |
bfe68a22 | 6168 | u32 tail, off = req->timeout.off; |
ad8a48ac | 6169 | |
89850fce | 6170 | spin_lock_irq(&ctx->timeout_lock); |
93bd25bb | 6171 | |
5262f567 JA |
6172 | /* |
6173 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
6174 | * timeout event to be satisfied. If it isn't set, then this is |
6175 | * a pure timeout request, sequence isn't used. | |
5262f567 | 6176 | */ |
8eb7e2d0 | 6177 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
6178 | entry = ctx->timeout_list.prev; |
6179 | goto add; | |
6180 | } | |
5262f567 | 6181 | |
bfe68a22 PB |
6182 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
6183 | req->timeout.target_seq = tail + off; | |
5262f567 | 6184 | |
f010505b MDG |
6185 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
6186 | * This is safe because ->completion_lock is held, and submissions | |
6187 | * and completions are never mixed in the same ->completion_lock section. | |
6188 | */ | |
6189 | ctx->cq_last_tm_flush = tail; | |
6190 | ||
5262f567 JA |
6191 | /* |
6192 | * Insertion sort, ensuring the first entry in the list is always | |
6193 | * the one we need first. | |
6194 | */ | |
5262f567 | 6195 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
6196 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
6197 | timeout.list); | |
5262f567 | 6198 | |
8eb7e2d0 | 6199 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 6200 | continue; |
bfe68a22 PB |
6201 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
6202 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
6203 | break; |
6204 | } | |
93bd25bb | 6205 | add: |
135fcde8 | 6206 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
6207 | data->timer.function = io_timeout_fn; |
6208 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
89850fce | 6209 | spin_unlock_irq(&ctx->timeout_lock); |
5262f567 JA |
6210 | return 0; |
6211 | } | |
5262f567 | 6212 | |
f458dd84 PB |
6213 | struct io_cancel_data { |
6214 | struct io_ring_ctx *ctx; | |
6215 | u64 user_data; | |
6216 | }; | |
6217 | ||
62755e35 JA |
6218 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
6219 | { | |
6220 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 6221 | struct io_cancel_data *cd = data; |
62755e35 | 6222 | |
f458dd84 | 6223 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
62755e35 JA |
6224 | } |
6225 | ||
f458dd84 PB |
6226 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
6227 | struct io_ring_ctx *ctx) | |
62755e35 | 6228 | { |
f458dd84 | 6229 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
62755e35 | 6230 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
6231 | int ret = 0; |
6232 | ||
f458dd84 | 6233 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
6234 | return -ENOENT; |
6235 | ||
f458dd84 | 6236 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
62755e35 JA |
6237 | switch (cancel_ret) { |
6238 | case IO_WQ_CANCEL_OK: | |
6239 | ret = 0; | |
6240 | break; | |
6241 | case IO_WQ_CANCEL_RUNNING: | |
6242 | ret = -EALREADY; | |
6243 | break; | |
6244 | case IO_WQ_CANCEL_NOTFOUND: | |
6245 | ret = -ENOENT; | |
6246 | break; | |
6247 | } | |
6248 | ||
e977d6d3 JA |
6249 | return ret; |
6250 | } | |
6251 | ||
8cb01fac | 6252 | static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr) |
47f46768 | 6253 | { |
8cb01fac | 6254 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 JA |
6255 | int ret; |
6256 | ||
dadebc35 | 6257 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
8cb01fac | 6258 | |
f458dd84 | 6259 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
df9727af | 6260 | if (ret != -ENOENT) |
8cb01fac | 6261 | return ret; |
505657bc PB |
6262 | |
6263 | spin_lock(&ctx->completion_lock); | |
79ebeaee | 6264 | spin_lock_irq(&ctx->timeout_lock); |
47f46768 | 6265 | ret = io_timeout_cancel(ctx, sqe_addr); |
79ebeaee | 6266 | spin_unlock_irq(&ctx->timeout_lock); |
47f46768 | 6267 | if (ret != -ENOENT) |
505657bc PB |
6268 | goto out; |
6269 | ret = io_poll_cancel(ctx, sqe_addr, false); | |
6270 | out: | |
6271 | spin_unlock(&ctx->completion_lock); | |
6272 | return ret; | |
47f46768 JA |
6273 | } |
6274 | ||
3529d8c2 JA |
6275 | static int io_async_cancel_prep(struct io_kiocb *req, |
6276 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 6277 | { |
fbf23849 | 6278 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 6279 | return -EINVAL; |
61710e43 DA |
6280 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6281 | return -EINVAL; | |
26578cda PB |
6282 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
6283 | sqe->splice_fd_in) | |
e977d6d3 JA |
6284 | return -EINVAL; |
6285 | ||
fbf23849 JA |
6286 | req->cancel.addr = READ_ONCE(sqe->addr); |
6287 | return 0; | |
6288 | } | |
6289 | ||
61e98203 | 6290 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6291 | { |
6292 | struct io_ring_ctx *ctx = req->ctx; | |
58f99373 PB |
6293 | u64 sqe_addr = req->cancel.addr; |
6294 | struct io_tctx_node *node; | |
6295 | int ret; | |
6296 | ||
8cb01fac | 6297 | ret = io_try_cancel_userdata(req, sqe_addr); |
58f99373 PB |
6298 | if (ret != -ENOENT) |
6299 | goto done; | |
58f99373 PB |
6300 | |
6301 | /* slow path, try all io-wq's */ | |
6302 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
6303 | ret = -ENOENT; | |
6304 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
6305 | struct io_uring_task *tctx = node->task->io_uring; | |
fbf23849 | 6306 | |
58f99373 PB |
6307 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
6308 | if (ret != -ENOENT) | |
6309 | break; | |
6310 | } | |
6311 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
58f99373 | 6312 | done: |
58f99373 | 6313 | if (ret < 0) |
93d2bcd2 | 6314 | req_set_fail(req); |
505657bc | 6315 | io_req_complete_post(req, ret, 0); |
5262f567 JA |
6316 | return 0; |
6317 | } | |
6318 | ||
269bbe5f | 6319 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
6320 | const struct io_uring_sqe *sqe) |
6321 | { | |
61710e43 DA |
6322 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6323 | return -EINVAL; | |
26578cda | 6324 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
05f3fb3c JA |
6325 | return -EINVAL; |
6326 | ||
269bbe5f BM |
6327 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
6328 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
6329 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 6330 | return -EINVAL; |
269bbe5f | 6331 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
6332 | return 0; |
6333 | } | |
6334 | ||
889fca73 | 6335 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6336 | { |
6337 | struct io_ring_ctx *ctx = req->ctx; | |
c3bdad02 | 6338 | struct io_uring_rsrc_update2 up; |
05f3fb3c | 6339 | int ret; |
fbf23849 | 6340 | |
269bbe5f BM |
6341 | up.offset = req->rsrc_update.offset; |
6342 | up.data = req->rsrc_update.arg; | |
c3bdad02 PB |
6343 | up.nr = 0; |
6344 | up.tags = 0; | |
615cee49 | 6345 | up.resv = 0; |
05f3fb3c | 6346 | |
cdb31c29 | 6347 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
fdecb662 | 6348 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
98f0b3b4 | 6349 | &up, req->rsrc_update.nr_args); |
cdb31c29 | 6350 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
05f3fb3c JA |
6351 | |
6352 | if (ret < 0) | |
93d2bcd2 | 6353 | req_set_fail(req); |
889fca73 | 6354 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
6355 | return 0; |
6356 | } | |
6357 | ||
bfe76559 | 6358 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 6359 | { |
d625c6ee | 6360 | switch (req->opcode) { |
e781573e | 6361 | case IORING_OP_NOP: |
bfe76559 | 6362 | return 0; |
f67676d1 JA |
6363 | case IORING_OP_READV: |
6364 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 6365 | case IORING_OP_READ: |
bfe76559 | 6366 | return io_read_prep(req, sqe); |
f67676d1 JA |
6367 | case IORING_OP_WRITEV: |
6368 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 6369 | case IORING_OP_WRITE: |
bfe76559 | 6370 | return io_write_prep(req, sqe); |
0969e783 | 6371 | case IORING_OP_POLL_ADD: |
bfe76559 | 6372 | return io_poll_add_prep(req, sqe); |
0969e783 | 6373 | case IORING_OP_POLL_REMOVE: |
c5de0036 | 6374 | return io_poll_update_prep(req, sqe); |
8ed8d3c3 | 6375 | case IORING_OP_FSYNC: |
1155c76a | 6376 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 6377 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 6378 | return io_sfr_prep(req, sqe); |
03b1230c | 6379 | case IORING_OP_SENDMSG: |
fddaface | 6380 | case IORING_OP_SEND: |
bfe76559 | 6381 | return io_sendmsg_prep(req, sqe); |
03b1230c | 6382 | case IORING_OP_RECVMSG: |
fddaface | 6383 | case IORING_OP_RECV: |
bfe76559 | 6384 | return io_recvmsg_prep(req, sqe); |
f499a021 | 6385 | case IORING_OP_CONNECT: |
bfe76559 | 6386 | return io_connect_prep(req, sqe); |
2d28390a | 6387 | case IORING_OP_TIMEOUT: |
bfe76559 | 6388 | return io_timeout_prep(req, sqe, false); |
b29472ee | 6389 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 6390 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 6391 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 6392 | return io_async_cancel_prep(req, sqe); |
2d28390a | 6393 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 6394 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 6395 | case IORING_OP_ACCEPT: |
bfe76559 | 6396 | return io_accept_prep(req, sqe); |
d63d1b5e | 6397 | case IORING_OP_FALLOCATE: |
bfe76559 | 6398 | return io_fallocate_prep(req, sqe); |
15b71abe | 6399 | case IORING_OP_OPENAT: |
bfe76559 | 6400 | return io_openat_prep(req, sqe); |
b5dba59e | 6401 | case IORING_OP_CLOSE: |
bfe76559 | 6402 | return io_close_prep(req, sqe); |
05f3fb3c | 6403 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 6404 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6405 | case IORING_OP_STATX: |
bfe76559 | 6406 | return io_statx_prep(req, sqe); |
4840e418 | 6407 | case IORING_OP_FADVISE: |
bfe76559 | 6408 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6409 | case IORING_OP_MADVISE: |
bfe76559 | 6410 | return io_madvise_prep(req, sqe); |
cebdb986 | 6411 | case IORING_OP_OPENAT2: |
bfe76559 | 6412 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6413 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6414 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6415 | case IORING_OP_SPLICE: |
bfe76559 | 6416 | return io_splice_prep(req, sqe); |
ddf0322d | 6417 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6418 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6419 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6420 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6421 | case IORING_OP_TEE: |
bfe76559 | 6422 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6423 | case IORING_OP_SHUTDOWN: |
6424 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6425 | case IORING_OP_RENAMEAT: |
6426 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6427 | case IORING_OP_UNLINKAT: |
6428 | return io_unlinkat_prep(req, sqe); | |
e34a02dc DK |
6429 | case IORING_OP_MKDIRAT: |
6430 | return io_mkdirat_prep(req, sqe); | |
7a8721f8 DK |
6431 | case IORING_OP_SYMLINKAT: |
6432 | return io_symlinkat_prep(req, sqe); | |
cf30da90 DK |
6433 | case IORING_OP_LINKAT: |
6434 | return io_linkat_prep(req, sqe); | |
f67676d1 JA |
6435 | } |
6436 | ||
bfe76559 PB |
6437 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6438 | req->opcode); | |
bd54b6fe | 6439 | return -EINVAL; |
bfe76559 PB |
6440 | } |
6441 | ||
93642ef8 | 6442 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 6443 | { |
b7e298d2 PB |
6444 | if (!io_op_defs[req->opcode].needs_async_setup) |
6445 | return 0; | |
6446 | if (WARN_ON_ONCE(req->async_data)) | |
6447 | return -EFAULT; | |
6448 | if (io_alloc_async_data(req)) | |
6449 | return -EAGAIN; | |
6450 | ||
93642ef8 PB |
6451 | switch (req->opcode) { |
6452 | case IORING_OP_READV: | |
93642ef8 PB |
6453 | return io_rw_prep_async(req, READ); |
6454 | case IORING_OP_WRITEV: | |
93642ef8 PB |
6455 | return io_rw_prep_async(req, WRITE); |
6456 | case IORING_OP_SENDMSG: | |
93642ef8 PB |
6457 | return io_sendmsg_prep_async(req); |
6458 | case IORING_OP_RECVMSG: | |
93642ef8 PB |
6459 | return io_recvmsg_prep_async(req); |
6460 | case IORING_OP_CONNECT: | |
6461 | return io_connect_prep_async(req); | |
6462 | } | |
b7e298d2 PB |
6463 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
6464 | req->opcode); | |
6465 | return -EFAULT; | |
f67676d1 JA |
6466 | } |
6467 | ||
9cf7c104 PB |
6468 | static u32 io_get_sequence(struct io_kiocb *req) |
6469 | { | |
a3dbdf54 | 6470 | u32 seq = req->ctx->cached_sq_head; |
9cf7c104 | 6471 | |
a3dbdf54 PB |
6472 | /* need original cached_sq_head, but it was increased for each req */ |
6473 | io_for_each_link(req, req) | |
6474 | seq--; | |
6475 | return seq; | |
9cf7c104 PB |
6476 | } |
6477 | ||
76cc33d7 | 6478 | static bool io_drain_req(struct io_kiocb *req) |
de0617e4 | 6479 | { |
3c19966d | 6480 | struct io_kiocb *pos; |
a197f664 | 6481 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6482 | struct io_defer_entry *de; |
f67676d1 | 6483 | int ret; |
9cf7c104 | 6484 | u32 seq; |
de0617e4 | 6485 | |
b8ce1b9d PB |
6486 | if (req->flags & REQ_F_FAIL) { |
6487 | io_req_complete_fail_submit(req); | |
6488 | return true; | |
6489 | } | |
6490 | ||
3c19966d PB |
6491 | /* |
6492 | * If we need to drain a request in the middle of a link, drain the | |
6493 | * head request and the next request/link after the current link. | |
6494 | * Considering sequential execution of links, IOSQE_IO_DRAIN will be | |
6495 | * maintained for every request of our link. | |
6496 | */ | |
6497 | if (ctx->drain_next) { | |
6498 | req->flags |= REQ_F_IO_DRAIN; | |
6499 | ctx->drain_next = false; | |
6500 | } | |
6501 | /* not interested in head, start from the first linked */ | |
6502 | io_for_each_link(pos, req->link) { | |
6503 | if (pos->flags & REQ_F_IO_DRAIN) { | |
6504 | ctx->drain_next = true; | |
6505 | req->flags |= REQ_F_IO_DRAIN; | |
6506 | break; | |
6507 | } | |
6508 | } | |
6509 | ||
9d858b21 | 6510 | /* Still need defer if there is pending req in defer list. */ |
9cf7c104 | 6511 | if (likely(list_empty_careful(&ctx->defer_list) && |
10c66904 PB |
6512 | !(req->flags & REQ_F_IO_DRAIN))) { |
6513 | ctx->drain_active = false; | |
76cc33d7 | 6514 | return false; |
10c66904 | 6515 | } |
9cf7c104 PB |
6516 | |
6517 | seq = io_get_sequence(req); | |
6518 | /* Still a chance to pass the sequence check */ | |
6519 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
76cc33d7 | 6520 | return false; |
de0617e4 | 6521 | |
b7e298d2 | 6522 | ret = io_req_prep_async(req); |
be7053b7 | 6523 | if (ret) |
1b48773f | 6524 | goto fail; |
cbdcb435 | 6525 | io_prep_async_link(req); |
27dc8338 | 6526 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
76cc33d7 | 6527 | if (!de) { |
1b48773f PB |
6528 | ret = -ENOMEM; |
6529 | fail: | |
6530 | io_req_complete_failed(req, ret); | |
76cc33d7 PB |
6531 | return true; |
6532 | } | |
2d28390a | 6533 | |
79ebeaee | 6534 | spin_lock(&ctx->completion_lock); |
9cf7c104 | 6535 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
79ebeaee | 6536 | spin_unlock(&ctx->completion_lock); |
27dc8338 | 6537 | kfree(de); |
f237c30a | 6538 | io_queue_async_work(req, NULL); |
76cc33d7 | 6539 | return true; |
de0617e4 JA |
6540 | } |
6541 | ||
915967f6 | 6542 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 6543 | de->req = req; |
9cf7c104 | 6544 | de->seq = seq; |
27dc8338 | 6545 | list_add_tail(&de->list, &ctx->defer_list); |
79ebeaee | 6546 | spin_unlock(&ctx->completion_lock); |
76cc33d7 | 6547 | return true; |
de0617e4 JA |
6548 | } |
6549 | ||
68fb8979 | 6550 | static void io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6551 | { |
0e1b6fe3 PB |
6552 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
6553 | switch (req->opcode) { | |
6554 | case IORING_OP_READV: | |
6555 | case IORING_OP_READ_FIXED: | |
6556 | case IORING_OP_READ: | |
bcda7baa | 6557 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
6558 | break; |
6559 | case IORING_OP_RECVMSG: | |
6560 | case IORING_OP_RECV: | |
bcda7baa | 6561 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
6562 | break; |
6563 | } | |
99bc4c38 PB |
6564 | } |
6565 | ||
0e1b6fe3 PB |
6566 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6567 | switch (req->opcode) { | |
6568 | case IORING_OP_READV: | |
6569 | case IORING_OP_READ_FIXED: | |
6570 | case IORING_OP_READ: | |
6571 | case IORING_OP_WRITEV: | |
6572 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6573 | case IORING_OP_WRITE: { |
6574 | struct io_async_rw *io = req->async_data; | |
1dacb4df PB |
6575 | |
6576 | kfree(io->free_iovec); | |
0e1b6fe3 | 6577 | break; |
e8c2bc1f | 6578 | } |
0e1b6fe3 | 6579 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6580 | case IORING_OP_SENDMSG: { |
6581 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
6582 | |
6583 | kfree(io->free_iov); | |
0e1b6fe3 | 6584 | break; |
e8c2bc1f | 6585 | } |
0e1b6fe3 PB |
6586 | case IORING_OP_SPLICE: |
6587 | case IORING_OP_TEE: | |
e1d767f0 PB |
6588 | if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED)) |
6589 | io_put_file(req->splice.file_in); | |
0e1b6fe3 | 6590 | break; |
f3cd4850 JA |
6591 | case IORING_OP_OPENAT: |
6592 | case IORING_OP_OPENAT2: | |
6593 | if (req->open.filename) | |
6594 | putname(req->open.filename); | |
6595 | break; | |
80a261fd JA |
6596 | case IORING_OP_RENAMEAT: |
6597 | putname(req->rename.oldpath); | |
6598 | putname(req->rename.newpath); | |
6599 | break; | |
14a1143b JA |
6600 | case IORING_OP_UNLINKAT: |
6601 | putname(req->unlink.filename); | |
6602 | break; | |
e34a02dc DK |
6603 | case IORING_OP_MKDIRAT: |
6604 | putname(req->mkdir.filename); | |
6605 | break; | |
7a8721f8 DK |
6606 | case IORING_OP_SYMLINKAT: |
6607 | putname(req->symlink.oldpath); | |
6608 | putname(req->symlink.newpath); | |
6609 | break; | |
cf30da90 DK |
6610 | case IORING_OP_LINKAT: |
6611 | putname(req->hardlink.oldpath); | |
6612 | putname(req->hardlink.newpath); | |
6613 | break; | |
0e1b6fe3 | 6614 | } |
99bc4c38 | 6615 | } |
75652a30 JA |
6616 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
6617 | kfree(req->apoll->double_poll); | |
6618 | kfree(req->apoll); | |
6619 | req->apoll = NULL; | |
6620 | } | |
3a0a6902 PB |
6621 | if (req->flags & REQ_F_INFLIGHT) { |
6622 | struct io_uring_task *tctx = req->task->io_uring; | |
6623 | ||
6624 | atomic_dec(&tctx->inflight_tracked); | |
3a0a6902 | 6625 | } |
c854357b | 6626 | if (req->flags & REQ_F_CREDS) |
b8e64b53 | 6627 | put_cred(req->creds); |
c854357b PB |
6628 | |
6629 | req->flags &= ~IO_REQ_CLEAN_FLAGS; | |
99bc4c38 PB |
6630 | } |
6631 | ||
889fca73 | 6632 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 6633 | { |
a197f664 | 6634 | struct io_ring_ctx *ctx = req->ctx; |
5730b27e | 6635 | const struct cred *creds = NULL; |
d625c6ee | 6636 | int ret; |
2b188cc1 | 6637 | |
b8e64b53 | 6638 | if ((req->flags & REQ_F_CREDS) && req->creds != current_cred()) |
c10d1f98 | 6639 | creds = override_creds(req->creds); |
5730b27e | 6640 | |
d625c6ee | 6641 | switch (req->opcode) { |
2b188cc1 | 6642 | case IORING_OP_NOP: |
889fca73 | 6643 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
6644 | break; |
6645 | case IORING_OP_READV: | |
edafccee | 6646 | case IORING_OP_READ_FIXED: |
3a6820f2 | 6647 | case IORING_OP_READ: |
889fca73 | 6648 | ret = io_read(req, issue_flags); |
edafccee | 6649 | break; |
3529d8c2 | 6650 | case IORING_OP_WRITEV: |
edafccee | 6651 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 6652 | case IORING_OP_WRITE: |
889fca73 | 6653 | ret = io_write(req, issue_flags); |
2b188cc1 | 6654 | break; |
c992fe29 | 6655 | case IORING_OP_FSYNC: |
45d189c6 | 6656 | ret = io_fsync(req, issue_flags); |
c992fe29 | 6657 | break; |
221c5eb2 | 6658 | case IORING_OP_POLL_ADD: |
61e98203 | 6659 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
6660 | break; |
6661 | case IORING_OP_POLL_REMOVE: | |
c5de0036 | 6662 | ret = io_poll_update(req, issue_flags); |
221c5eb2 | 6663 | break; |
5d17b4a4 | 6664 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 6665 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 6666 | break; |
0fa03c62 | 6667 | case IORING_OP_SENDMSG: |
889fca73 | 6668 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 6669 | break; |
fddaface | 6670 | case IORING_OP_SEND: |
889fca73 | 6671 | ret = io_send(req, issue_flags); |
0fa03c62 | 6672 | break; |
aa1fa28f | 6673 | case IORING_OP_RECVMSG: |
889fca73 | 6674 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 6675 | break; |
fddaface | 6676 | case IORING_OP_RECV: |
889fca73 | 6677 | ret = io_recv(req, issue_flags); |
aa1fa28f | 6678 | break; |
5262f567 | 6679 | case IORING_OP_TIMEOUT: |
61e98203 | 6680 | ret = io_timeout(req, issue_flags); |
5262f567 | 6681 | break; |
11365043 | 6682 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 6683 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 6684 | break; |
17f2fe35 | 6685 | case IORING_OP_ACCEPT: |
889fca73 | 6686 | ret = io_accept(req, issue_flags); |
17f2fe35 | 6687 | break; |
f8e85cf2 | 6688 | case IORING_OP_CONNECT: |
889fca73 | 6689 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 6690 | break; |
62755e35 | 6691 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 6692 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 6693 | break; |
d63d1b5e | 6694 | case IORING_OP_FALLOCATE: |
45d189c6 | 6695 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 6696 | break; |
15b71abe | 6697 | case IORING_OP_OPENAT: |
45d189c6 | 6698 | ret = io_openat(req, issue_flags); |
15b71abe | 6699 | break; |
b5dba59e | 6700 | case IORING_OP_CLOSE: |
889fca73 | 6701 | ret = io_close(req, issue_flags); |
b5dba59e | 6702 | break; |
05f3fb3c | 6703 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6704 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6705 | break; |
eddc7ef5 | 6706 | case IORING_OP_STATX: |
45d189c6 | 6707 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6708 | break; |
4840e418 | 6709 | case IORING_OP_FADVISE: |
45d189c6 | 6710 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6711 | break; |
c1ca757b | 6712 | case IORING_OP_MADVISE: |
45d189c6 | 6713 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6714 | break; |
cebdb986 | 6715 | case IORING_OP_OPENAT2: |
45d189c6 | 6716 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6717 | break; |
3e4827b0 | 6718 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6719 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6720 | break; |
7d67af2c | 6721 | case IORING_OP_SPLICE: |
45d189c6 | 6722 | ret = io_splice(req, issue_flags); |
7d67af2c | 6723 | break; |
ddf0322d | 6724 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6725 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6726 | break; |
067524e9 | 6727 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6728 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6729 | break; |
f2a8d5c7 | 6730 | case IORING_OP_TEE: |
45d189c6 | 6731 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6732 | break; |
36f4fa68 | 6733 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6734 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6735 | break; |
80a261fd | 6736 | case IORING_OP_RENAMEAT: |
45d189c6 | 6737 | ret = io_renameat(req, issue_flags); |
80a261fd | 6738 | break; |
14a1143b | 6739 | case IORING_OP_UNLINKAT: |
45d189c6 | 6740 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6741 | break; |
e34a02dc DK |
6742 | case IORING_OP_MKDIRAT: |
6743 | ret = io_mkdirat(req, issue_flags); | |
6744 | break; | |
7a8721f8 DK |
6745 | case IORING_OP_SYMLINKAT: |
6746 | ret = io_symlinkat(req, issue_flags); | |
6747 | break; | |
cf30da90 DK |
6748 | case IORING_OP_LINKAT: |
6749 | ret = io_linkat(req, issue_flags); | |
6750 | break; | |
2b188cc1 JA |
6751 | default: |
6752 | ret = -EINVAL; | |
6753 | break; | |
6754 | } | |
6755 | ||
5730b27e JA |
6756 | if (creds) |
6757 | revert_creds(creds); | |
def596e9 JA |
6758 | if (ret) |
6759 | return ret; | |
b532576e | 6760 | /* If the op doesn't have a file, we're not polling for it */ |
cb3d8972 PB |
6761 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
6762 | io_iopoll_req_issued(req); | |
def596e9 JA |
6763 | |
6764 | return 0; | |
2b188cc1 JA |
6765 | } |
6766 | ||
ebc11b6c PB |
6767 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
6768 | { | |
6769 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6770 | ||
6771 | req = io_put_req_find_next(req); | |
6772 | return req ? &req->work : NULL; | |
6773 | } | |
6774 | ||
5280f7e5 | 6775 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6776 | { |
6777 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6778 | struct io_kiocb *timeout; |
561fb04a | 6779 | int ret = 0; |
2b188cc1 | 6780 | |
48dcd38d PB |
6781 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
6782 | if (!(req->flags & REQ_F_REFCOUNT)) | |
6783 | __io_req_set_refcount(req, 2); | |
6784 | else | |
6785 | req_ref_get(req); | |
5d5901a3 | 6786 | |
6df1db6b PB |
6787 | timeout = io_prep_linked_timeout(req); |
6788 | if (timeout) | |
6789 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6790 | |
dadebc35 | 6791 | /* either cancelled or io-wq is dying, so don't touch tctx->iowq */ |
4014d943 | 6792 | if (work->flags & IO_WQ_WORK_CANCEL) |
561fb04a | 6793 | ret = -ECANCELED; |
31b51510 | 6794 | |
561fb04a | 6795 | if (!ret) { |
561fb04a | 6796 | do { |
889fca73 | 6797 | ret = io_issue_sqe(req, 0); |
561fb04a JA |
6798 | /* |
6799 | * We can get EAGAIN for polled IO even though we're | |
6800 | * forcing a sync submission from here, since we can't | |
6801 | * wait for request slots on the block side. | |
6802 | */ | |
6803 | if (ret != -EAGAIN) | |
6804 | break; | |
6805 | cond_resched(); | |
6806 | } while (1); | |
6807 | } | |
31b51510 | 6808 | |
a3df7698 | 6809 | /* avoid locking problems by failing it from a clean context */ |
5d5901a3 | 6810 | if (ret) |
a3df7698 | 6811 | io_req_task_queue_fail(req, ret); |
2b188cc1 JA |
6812 | } |
6813 | ||
aeca241b | 6814 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
042b0d85 | 6815 | unsigned i) |
65e19f54 | 6816 | { |
042b0d85 | 6817 | return &table->files[i]; |
dafecf19 PB |
6818 | } |
6819 | ||
65e19f54 JA |
6820 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6821 | int index) | |
6822 | { | |
aeca241b | 6823 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
65e19f54 | 6824 | |
a04b0ac0 | 6825 | return (struct file *) (slot->file_ptr & FFS_MASK); |
65e19f54 JA |
6826 | } |
6827 | ||
a04b0ac0 | 6828 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
9a321c98 PB |
6829 | { |
6830 | unsigned long file_ptr = (unsigned long) file; | |
6831 | ||
b191e2df | 6832 | if (__io_file_supports_nowait(file, READ)) |
9a321c98 | 6833 | file_ptr |= FFS_ASYNC_READ; |
b191e2df | 6834 | if (__io_file_supports_nowait(file, WRITE)) |
9a321c98 PB |
6835 | file_ptr |= FFS_ASYNC_WRITE; |
6836 | if (S_ISREG(file_inode(file)->i_mode)) | |
6837 | file_ptr |= FFS_ISREG; | |
a04b0ac0 | 6838 | file_slot->file_ptr = file_ptr; |
65e19f54 JA |
6839 | } |
6840 | ||
ac177053 PB |
6841 | static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx, |
6842 | struct io_kiocb *req, int fd) | |
09bb8394 | 6843 | { |
8da11c19 | 6844 | struct file *file; |
ac177053 | 6845 | unsigned long file_ptr; |
09bb8394 | 6846 | |
ac177053 PB |
6847 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
6848 | return NULL; | |
6849 | fd = array_index_nospec(fd, ctx->nr_user_files); | |
6850 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; | |
6851 | file = (struct file *) (file_ptr & FFS_MASK); | |
6852 | file_ptr &= ~FFS_MASK; | |
6853 | /* mask in overlapping REQ_F and FFS bits */ | |
b191e2df | 6854 | req->flags |= (file_ptr << REQ_F_NOWAIT_READ_BIT); |
ac177053 PB |
6855 | io_req_set_rsrc_node(req); |
6856 | return file; | |
6857 | } | |
d44f554e | 6858 | |
ac177053 | 6859 | static struct file *io_file_get_normal(struct io_ring_ctx *ctx, |
ac177053 PB |
6860 | struct io_kiocb *req, int fd) |
6861 | { | |
62906e89 | 6862 | struct file *file = fget(fd); |
ac177053 PB |
6863 | |
6864 | trace_io_uring_file_get(ctx, fd); | |
09bb8394 | 6865 | |
ac177053 PB |
6866 | /* we don't allow fixed io_uring files */ |
6867 | if (file && unlikely(file->f_op == &io_uring_fops)) | |
6868 | io_req_track_inflight(req); | |
8371adf5 | 6869 | return file; |
09bb8394 JA |
6870 | } |
6871 | ||
ac177053 | 6872 | static inline struct file *io_file_get(struct io_ring_ctx *ctx, |
ac177053 PB |
6873 | struct io_kiocb *req, int fd, bool fixed) |
6874 | { | |
6875 | if (fixed) | |
6876 | return io_file_get_fixed(ctx, req, fd); | |
6877 | else | |
62906e89 | 6878 | return io_file_get_normal(ctx, req, fd); |
ac177053 PB |
6879 | } |
6880 | ||
f237c30a | 6881 | static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked) |
89b263f6 JA |
6882 | { |
6883 | struct io_kiocb *prev = req->timeout.prev; | |
8cb01fac | 6884 | int ret; |
89b263f6 JA |
6885 | |
6886 | if (prev) { | |
8cb01fac | 6887 | ret = io_try_cancel_userdata(req, prev->user_data); |
505657bc | 6888 | io_req_complete_post(req, ret ?: -ETIME, 0); |
89b263f6 | 6889 | io_put_req(prev); |
89b263f6 JA |
6890 | } else { |
6891 | io_req_complete_post(req, -ETIME, 0); | |
6892 | } | |
6893 | } | |
6894 | ||
2665abfd | 6895 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6896 | { |
ad8a48ac JA |
6897 | struct io_timeout_data *data = container_of(timer, |
6898 | struct io_timeout_data, timer); | |
90cd7e42 | 6899 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6900 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6901 | unsigned long flags; |
2665abfd | 6902 | |
89b263f6 | 6903 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
90cd7e42 PB |
6904 | prev = req->timeout.head; |
6905 | req->timeout.head = NULL; | |
2665abfd JA |
6906 | |
6907 | /* | |
6908 | * We don't expect the list to be empty, that will only happen if we | |
6909 | * race with the completion of the linked work. | |
6910 | */ | |
447c19f3 | 6911 | if (prev) { |
f2f87370 | 6912 | io_remove_next_linked(prev); |
447c19f3 PB |
6913 | if (!req_ref_inc_not_zero(prev)) |
6914 | prev = NULL; | |
6915 | } | |
ef9dd637 | 6916 | list_del(&req->timeout.list); |
89b263f6 JA |
6917 | req->timeout.prev = prev; |
6918 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); | |
2665abfd | 6919 | |
89b263f6 JA |
6920 | req->io_task_work.func = io_req_task_link_timeout; |
6921 | io_req_task_work_add(req); | |
2665abfd JA |
6922 | return HRTIMER_NORESTART; |
6923 | } | |
6924 | ||
de968c18 | 6925 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6926 | { |
de968c18 PB |
6927 | struct io_ring_ctx *ctx = req->ctx; |
6928 | ||
89b263f6 | 6929 | spin_lock_irq(&ctx->timeout_lock); |
76a46e06 | 6930 | /* |
f2f87370 PB |
6931 | * If the back reference is NULL, then our linked request finished |
6932 | * before we got a chance to setup the timer | |
76a46e06 | 6933 | */ |
90cd7e42 | 6934 | if (req->timeout.head) { |
e8c2bc1f | 6935 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6936 | |
ad8a48ac JA |
6937 | data->timer.function = io_link_timeout_fn; |
6938 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6939 | data->mode); | |
ef9dd637 | 6940 | list_add_tail(&req->timeout.list, &ctx->ltimeout_list); |
2665abfd | 6941 | } |
89b263f6 | 6942 | spin_unlock_irq(&ctx->timeout_lock); |
2665abfd | 6943 | /* drop submission reference */ |
76a46e06 JA |
6944 | io_put_req(req); |
6945 | } | |
2665abfd | 6946 | |
c5eef2b9 | 6947 | static void __io_queue_sqe(struct io_kiocb *req) |
282cdc86 | 6948 | __must_hold(&req->ctx->uring_lock) |
2b188cc1 | 6949 | { |
906c6caa | 6950 | struct io_kiocb *linked_timeout; |
e0c5c576 | 6951 | int ret; |
2b188cc1 | 6952 | |
59b735ae | 6953 | issue_sqe: |
c5eef2b9 | 6954 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 6955 | |
491381ce JA |
6956 | /* |
6957 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6958 | * doesn't support non-blocking read/write attempts | |
6959 | */ | |
1840038e | 6960 | if (likely(!ret)) { |
e342c807 | 6961 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
c5eef2b9 | 6962 | struct io_ring_ctx *ctx = req->ctx; |
cd0ca2e0 | 6963 | struct io_submit_state *state = &ctx->submit_state; |
e65ef56d | 6964 | |
cd0ca2e0 PB |
6965 | state->compl_reqs[state->compl_nr++] = req; |
6966 | if (state->compl_nr == ARRAY_SIZE(state->compl_reqs)) | |
2a2758f2 | 6967 | io_submit_flush_completions(ctx); |
906c6caa | 6968 | return; |
0d63c148 | 6969 | } |
906c6caa PB |
6970 | |
6971 | linked_timeout = io_prep_linked_timeout(req); | |
6972 | if (linked_timeout) | |
6973 | io_queue_linked_timeout(linked_timeout); | |
1840038e | 6974 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
906c6caa PB |
6975 | linked_timeout = io_prep_linked_timeout(req); |
6976 | ||
59b735ae OL |
6977 | switch (io_arm_poll_handler(req)) { |
6978 | case IO_APOLL_READY: | |
906c6caa PB |
6979 | if (linked_timeout) |
6980 | io_unprep_linked_timeout(req); | |
59b735ae OL |
6981 | goto issue_sqe; |
6982 | case IO_APOLL_ABORTED: | |
1840038e PB |
6983 | /* |
6984 | * Queued up for async execution, worker will release | |
6985 | * submit reference when the iocb is actually submitted. | |
6986 | */ | |
f237c30a | 6987 | io_queue_async_work(req, NULL); |
59b735ae | 6988 | break; |
1840038e | 6989 | } |
906c6caa PB |
6990 | |
6991 | if (linked_timeout) | |
6992 | io_queue_linked_timeout(linked_timeout); | |
0d63c148 | 6993 | } else { |
f41db273 | 6994 | io_req_complete_failed(req, ret); |
9e645e11 | 6995 | } |
2b188cc1 JA |
6996 | } |
6997 | ||
441b8a78 | 6998 | static inline void io_queue_sqe(struct io_kiocb *req) |
282cdc86 | 6999 | __must_hold(&req->ctx->uring_lock) |
4fe2c963 | 7000 | { |
10c66904 | 7001 | if (unlikely(req->ctx->drain_active) && io_drain_req(req)) |
76cc33d7 | 7002 | return; |
4fe2c963 | 7003 | |
a8295b98 | 7004 | if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) { |
c5eef2b9 | 7005 | __io_queue_sqe(req); |
a8295b98 | 7006 | } else if (req->flags & REQ_F_FAIL) { |
c6d3d9cb | 7007 | io_req_complete_fail_submit(req); |
76cc33d7 PB |
7008 | } else { |
7009 | int ret = io_req_prep_async(req); | |
7010 | ||
7011 | if (unlikely(ret)) | |
7012 | io_req_complete_failed(req, ret); | |
7013 | else | |
f237c30a | 7014 | io_queue_async_work(req, NULL); |
ce35a47a | 7015 | } |
4fe2c963 JL |
7016 | } |
7017 | ||
b16fed66 PB |
7018 | /* |
7019 | * Check SQE restrictions (opcode and flags). | |
7020 | * | |
7021 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
7022 | */ | |
7023 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
7024 | struct io_kiocb *req, | |
7025 | unsigned int sqe_flags) | |
4fe2c963 | 7026 | { |
4cfb25bf | 7027 | if (likely(!ctx->restricted)) |
b16fed66 PB |
7028 | return true; |
7029 | ||
7030 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) | |
7031 | return false; | |
7032 | ||
7033 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
7034 | ctx->restrictions.sqe_flags_required) | |
7035 | return false; | |
7036 | ||
7037 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
7038 | ctx->restrictions.sqe_flags_required)) | |
7039 | return false; | |
7040 | ||
7041 | return true; | |
4fe2c963 JL |
7042 | } |
7043 | ||
b16fed66 PB |
7044 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
7045 | const struct io_uring_sqe *sqe) | |
282cdc86 | 7046 | __must_hold(&ctx->uring_lock) |
b16fed66 PB |
7047 | { |
7048 | struct io_submit_state *state; | |
7049 | unsigned int sqe_flags; | |
003e8dcc | 7050 | int personality, ret = 0; |
b16fed66 | 7051 | |
864ea921 | 7052 | /* req is partially pre-initialised, see io_preinit_req() */ |
b16fed66 PB |
7053 | req->opcode = READ_ONCE(sqe->opcode); |
7054 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
7055 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
7056 | req->user_data = READ_ONCE(sqe->user_data); | |
b16fed66 | 7057 | req->file = NULL; |
b16fed66 | 7058 | req->fixed_rsrc_refs = NULL; |
b16fed66 | 7059 | req->task = current; |
b16fed66 PB |
7060 | |
7061 | /* enforce forwards compatibility on users */ | |
dddca226 | 7062 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) |
b16fed66 | 7063 | return -EINVAL; |
b16fed66 PB |
7064 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
7065 | return -EINVAL; | |
4cfb25bf | 7066 | if (!io_check_restriction(ctx, req, sqe_flags)) |
b16fed66 PB |
7067 | return -EACCES; |
7068 | ||
7069 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
7070 | !io_op_defs[req->opcode].buffer_select) | |
7071 | return -EOPNOTSUPP; | |
3c19966d PB |
7072 | if (unlikely(sqe_flags & IOSQE_IO_DRAIN)) |
7073 | ctx->drain_active = true; | |
863e0560 | 7074 | |
003e8dcc JA |
7075 | personality = READ_ONCE(sqe->personality); |
7076 | if (personality) { | |
c10d1f98 PB |
7077 | req->creds = xa_load(&ctx->personalities, personality); |
7078 | if (!req->creds) | |
003e8dcc | 7079 | return -EINVAL; |
c10d1f98 | 7080 | get_cred(req->creds); |
b8e64b53 | 7081 | req->flags |= REQ_F_CREDS; |
003e8dcc | 7082 | } |
b16fed66 PB |
7083 | state = &ctx->submit_state; |
7084 | ||
7085 | /* | |
7086 | * Plug now if we have more than 1 IO left after this, and the target | |
7087 | * is potentially a read/write to block based storage. | |
7088 | */ | |
7089 | if (!state->plug_started && state->ios_left > 1 && | |
7090 | io_op_defs[req->opcode].plug) { | |
7091 | blk_start_plug(&state->plug); | |
7092 | state->plug_started = true; | |
7093 | } | |
7094 | ||
7095 | if (io_op_defs[req->opcode].needs_file) { | |
62906e89 | 7096 | req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd), |
ac177053 | 7097 | (sqe_flags & IOSQE_FIXED_FILE)); |
b16fed66 PB |
7098 | if (unlikely(!req->file)) |
7099 | ret = -EBADF; | |
7100 | } | |
7101 | ||
7102 | state->ios_left--; | |
7103 | return ret; | |
7104 | } | |
7105 | ||
a6b8cadc | 7106 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 7107 | const struct io_uring_sqe *sqe) |
282cdc86 | 7108 | __must_hold(&ctx->uring_lock) |
9e645e11 | 7109 | { |
a1ab7b35 | 7110 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 7111 | int ret; |
9e645e11 | 7112 | |
a6b8cadc PB |
7113 | ret = io_init_req(ctx, req, sqe); |
7114 | if (unlikely(ret)) { | |
7115 | fail_req: | |
a8295b98 | 7116 | /* fail even hard links since we don't submit */ |
de59bc10 | 7117 | if (link->head) { |
a8295b98 HX |
7118 | /* |
7119 | * we can judge a link req is failed or cancelled by if | |
7120 | * REQ_F_FAIL is set, but the head is an exception since | |
7121 | * it may be set REQ_F_FAIL because of other req's failure | |
7122 | * so let's leverage req->result to distinguish if a head | |
7123 | * is set REQ_F_FAIL because of its failure or other req's | |
7124 | * failure so that we can set the correct ret code for it. | |
7125 | * init result here to avoid affecting the normal path. | |
7126 | */ | |
7127 | if (!(link->head->flags & REQ_F_FAIL)) | |
7128 | req_fail_link_node(link->head, -ECANCELED); | |
7129 | } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { | |
7130 | /* | |
7131 | * the current req is a normal req, we should return | |
7132 | * error and thus break the submittion loop. | |
7133 | */ | |
7134 | io_req_complete_failed(req, ret); | |
7135 | return ret; | |
de59bc10 | 7136 | } |
a8295b98 HX |
7137 | req_fail_link_node(req, ret); |
7138 | } else { | |
7139 | ret = io_req_prep(req, sqe); | |
7140 | if (unlikely(ret)) | |
7141 | goto fail_req; | |
a6b8cadc | 7142 | } |
441b8a78 | 7143 | |
be7053b7 | 7144 | /* don't need @sqe from now on */ |
236daeae OL |
7145 | trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data, |
7146 | req->flags, true, | |
7147 | ctx->flags & IORING_SETUP_SQPOLL); | |
a6b8cadc | 7148 | |
9e645e11 JA |
7149 | /* |
7150 | * If we already have a head request, queue this one for async | |
7151 | * submittal once the head completes. If we don't have a head but | |
7152 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
7153 | * submitted sync once the chain is complete. If none of those | |
7154 | * conditions are true (normal request), then just queue it. | |
7155 | */ | |
863e0560 PB |
7156 | if (link->head) { |
7157 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 7158 | |
a8295b98 HX |
7159 | if (!(req->flags & REQ_F_FAIL)) { |
7160 | ret = io_req_prep_async(req); | |
7161 | if (unlikely(ret)) { | |
7162 | req_fail_link_node(req, ret); | |
7163 | if (!(head->flags & REQ_F_FAIL)) | |
7164 | req_fail_link_node(head, -ECANCELED); | |
7165 | } | |
7166 | } | |
9d76377f | 7167 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 7168 | link->last->link = req; |
863e0560 | 7169 | link->last = req; |
32fe525b PB |
7170 | |
7171 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 7172 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
863e0560 | 7173 | link->head = NULL; |
5e159204 | 7174 | io_queue_sqe(head); |
32fe525b | 7175 | } |
9e645e11 | 7176 | } else { |
ef4ff581 | 7177 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
863e0560 PB |
7178 | link->head = req; |
7179 | link->last = req; | |
711be031 | 7180 | } else { |
be7053b7 | 7181 | io_queue_sqe(req); |
711be031 | 7182 | } |
9e645e11 | 7183 | } |
2e6e1fde | 7184 | |
1d4240cc | 7185 | return 0; |
9e645e11 JA |
7186 | } |
7187 | ||
9a56a232 JA |
7188 | /* |
7189 | * Batched submission is done, ensure local IO is flushed out. | |
7190 | */ | |
ba88ff11 PB |
7191 | static void io_submit_state_end(struct io_submit_state *state, |
7192 | struct io_ring_ctx *ctx) | |
9a56a232 | 7193 | { |
a1ab7b35 | 7194 | if (state->link.head) |
de59bc10 | 7195 | io_queue_sqe(state->link.head); |
cd0ca2e0 | 7196 | if (state->compl_nr) |
2a2758f2 | 7197 | io_submit_flush_completions(ctx); |
27926b68 JA |
7198 | if (state->plug_started) |
7199 | blk_finish_plug(&state->plug); | |
9a56a232 JA |
7200 | } |
7201 | ||
7202 | /* | |
7203 | * Start submission side cache. | |
7204 | */ | |
7205 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 7206 | unsigned int max_ios) |
9a56a232 | 7207 | { |
27926b68 | 7208 | state->plug_started = false; |
9a56a232 | 7209 | state->ios_left = max_ios; |
a1ab7b35 PB |
7210 | /* set only head, no need to init link_last in advance */ |
7211 | state->link.head = NULL; | |
9a56a232 JA |
7212 | } |
7213 | ||
2b188cc1 JA |
7214 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
7215 | { | |
75b28aff | 7216 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 7217 | |
caf582c6 PB |
7218 | /* |
7219 | * Ensure any loads from the SQEs are done at this point, | |
7220 | * since once we write the new head, the application could | |
7221 | * write new data to them. | |
7222 | */ | |
7223 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
7224 | } |
7225 | ||
2b188cc1 | 7226 | /* |
dd9ae8a0 | 7227 | * Fetch an sqe, if one is available. Note this returns a pointer to memory |
2b188cc1 JA |
7228 | * that is mapped by userspace. This means that care needs to be taken to |
7229 | * ensure that reads are stable, as we cannot rely on userspace always | |
7230 | * being a good citizen. If members of the sqe are validated and then later | |
7231 | * used, it's important that those reads are done through READ_ONCE() to | |
7232 | * prevent a re-load down the line. | |
7233 | */ | |
709b302f | 7234 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 7235 | { |
ea5ab3b5 | 7236 | unsigned head, mask = ctx->sq_entries - 1; |
17d3aeb3 | 7237 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
2b188cc1 JA |
7238 | |
7239 | /* | |
7240 | * The cached sq head (or cq tail) serves two purposes: | |
7241 | * | |
7242 | * 1) allows us to batch the cost of updating the user visible | |
7243 | * head updates. | |
7244 | * 2) allows the kernel side to track the head on its own, even | |
7245 | * though the application is the one updating it. | |
7246 | */ | |
17d3aeb3 | 7247 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
709b302f PB |
7248 | if (likely(head < ctx->sq_entries)) |
7249 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
7250 | |
7251 | /* drop invalid entries */ | |
15641e42 PB |
7252 | ctx->cq_extra--; |
7253 | WRITE_ONCE(ctx->rings->sq_dropped, | |
7254 | READ_ONCE(ctx->rings->sq_dropped) + 1); | |
709b302f PB |
7255 | return NULL; |
7256 | } | |
7257 | ||
0f212204 | 7258 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
282cdc86 | 7259 | __must_hold(&ctx->uring_lock) |
6c271ce2 | 7260 | { |
46c4e16a | 7261 | int submitted = 0; |
6c271ce2 | 7262 | |
ee7d46d9 PB |
7263 | /* make sure SQ entry isn't read before tail */ |
7264 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
2b85edfc PB |
7265 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
7266 | return -EAGAIN; | |
9a10867a | 7267 | io_get_task_refs(nr); |
6c271ce2 | 7268 | |
ba88ff11 | 7269 | io_submit_state_start(&ctx->submit_state, nr); |
46c4e16a | 7270 | while (submitted < nr) { |
3529d8c2 | 7271 | const struct io_uring_sqe *sqe; |
196be95c | 7272 | struct io_kiocb *req; |
fb5ccc98 | 7273 | |
258b29a9 | 7274 | req = io_alloc_req(ctx); |
196be95c PB |
7275 | if (unlikely(!req)) { |
7276 | if (!submitted) | |
7277 | submitted = -EAGAIN; | |
fb5ccc98 | 7278 | break; |
196be95c | 7279 | } |
4fccfcbb PB |
7280 | sqe = io_get_sqe(ctx); |
7281 | if (unlikely(!sqe)) { | |
0c6e1d7f | 7282 | list_add(&req->inflight_entry, &ctx->submit_state.free_list); |
4fccfcbb PB |
7283 | break; |
7284 | } | |
d3656344 JA |
7285 | /* will complete beyond this point, count as submitted */ |
7286 | submitted++; | |
a1ab7b35 | 7287 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 7288 | break; |
6c271ce2 JA |
7289 | } |
7290 | ||
9466f437 PB |
7291 | if (unlikely(submitted != nr)) { |
7292 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 | 7293 | int unused = nr - ref_used; |
9466f437 | 7294 | |
09899b19 | 7295 | current->io_uring->cached_refs += unused; |
d8a6df10 | 7296 | percpu_ref_put_many(&ctx->refs, unused); |
9466f437 | 7297 | } |
6c271ce2 | 7298 | |
a1ab7b35 | 7299 | io_submit_state_end(&ctx->submit_state, ctx); |
ae9428ca PB |
7300 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
7301 | io_commit_sqring(ctx); | |
7302 | ||
6c271ce2 JA |
7303 | return submitted; |
7304 | } | |
7305 | ||
e4b6d902 PB |
7306 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
7307 | { | |
7308 | return READ_ONCE(sqd->state); | |
7309 | } | |
7310 | ||
23b3628e XW |
7311 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
7312 | { | |
7313 | /* Tell userspace we may need a wakeup call */ | |
79ebeaee | 7314 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7315 | WRITE_ONCE(ctx->rings->sq_flags, |
7316 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7317 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7318 | } |
7319 | ||
7320 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
7321 | { | |
79ebeaee | 7322 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7323 | WRITE_ONCE(ctx->rings->sq_flags, |
7324 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7325 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7326 | } |
7327 | ||
08369246 | 7328 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 7329 | { |
c8d1ba58 | 7330 | unsigned int to_submit; |
bdcd3eab | 7331 | int ret = 0; |
6c271ce2 | 7332 | |
c8d1ba58 | 7333 | to_submit = io_sqring_entries(ctx); |
e95eee2d | 7334 | /* if we're handling multiple rings, cap submit size for fairness */ |
4ce8ad95 OL |
7335 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
7336 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; | |
e95eee2d | 7337 | |
906a3c6f | 7338 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
c8d1ba58 | 7339 | unsigned nr_events = 0; |
948e1947 PB |
7340 | const struct cred *creds = NULL; |
7341 | ||
7342 | if (ctx->sq_creds != current_cred()) | |
7343 | creds = override_creds(ctx->sq_creds); | |
a4c0b3de | 7344 | |
c8d1ba58 | 7345 | mutex_lock(&ctx->uring_lock); |
906a3c6f | 7346 | if (!list_empty(&ctx->iopoll_list)) |
a8576af9 | 7347 | io_do_iopoll(ctx, &nr_events, 0); |
906a3c6f | 7348 | |
3b763ba1 PB |
7349 | /* |
7350 | * Don't submit if refs are dying, good for io_uring_register(), | |
7351 | * but also it is relied upon by io_ring_exit_work() | |
7352 | */ | |
0298ef96 PB |
7353 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
7354 | !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
08369246 | 7355 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 | 7356 | mutex_unlock(&ctx->uring_lock); |
6c271ce2 | 7357 | |
acfb381d PB |
7358 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
7359 | wake_up(&ctx->sqo_sq_wait); | |
948e1947 PB |
7360 | if (creds) |
7361 | revert_creds(creds); | |
acfb381d | 7362 | } |
6c271ce2 | 7363 | |
08369246 XW |
7364 | return ret; |
7365 | } | |
6c271ce2 | 7366 | |
08369246 XW |
7367 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
7368 | { | |
7369 | struct io_ring_ctx *ctx; | |
7370 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 7371 | |
c9dca27d PB |
7372 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7373 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); | |
08369246 | 7374 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 7375 | } |
6c271ce2 | 7376 | |
e4b6d902 PB |
7377 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
7378 | { | |
7379 | bool did_sig = false; | |
7380 | struct ksignal ksig; | |
7381 | ||
7382 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || | |
7383 | signal_pending(current)) { | |
7384 | mutex_unlock(&sqd->lock); | |
7385 | if (signal_pending(current)) | |
7386 | did_sig = get_signal(&ksig); | |
7387 | cond_resched(); | |
7388 | mutex_lock(&sqd->lock); | |
7389 | } | |
e4b6d902 PB |
7390 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
7391 | } | |
7392 | ||
c8d1ba58 JA |
7393 | static int io_sq_thread(void *data) |
7394 | { | |
69fb2131 JA |
7395 | struct io_sq_data *sqd = data; |
7396 | struct io_ring_ctx *ctx; | |
a0d9205f | 7397 | unsigned long timeout = 0; |
37d1e2e3 | 7398 | char buf[TASK_COMM_LEN]; |
08369246 | 7399 | DEFINE_WAIT(wait); |
6c271ce2 | 7400 | |
696ee88a | 7401 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
37d1e2e3 | 7402 | set_task_comm(current, buf); |
37d1e2e3 JA |
7403 | |
7404 | if (sqd->sq_cpu != -1) | |
7405 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
7406 | else | |
7407 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
7408 | current->flags |= PF_NO_SETAFFINITY; | |
7409 | ||
09a6f4ef | 7410 | mutex_lock(&sqd->lock); |
e4b6d902 | 7411 | while (1) { |
1a924a80 | 7412 | bool cap_entries, sqt_spin = false; |
c1edbf5f | 7413 | |
e4b6d902 PB |
7414 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
7415 | if (io_sqd_handle_event(sqd)) | |
c7d95613 | 7416 | break; |
08369246 XW |
7417 | timeout = jiffies + sqd->sq_thread_idle; |
7418 | } | |
e4b6d902 | 7419 | |
e95eee2d | 7420 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 7421 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
948e1947 | 7422 | int ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a | 7423 | |
08369246 XW |
7424 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) |
7425 | sqt_spin = true; | |
69fb2131 | 7426 | } |
dd432ea5 PB |
7427 | if (io_run_task_work()) |
7428 | sqt_spin = true; | |
6c271ce2 | 7429 | |
08369246 | 7430 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 7431 | cond_resched(); |
08369246 XW |
7432 | if (sqt_spin) |
7433 | timeout = jiffies + sqd->sq_thread_idle; | |
7434 | continue; | |
7435 | } | |
7436 | ||
08369246 | 7437 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
dd432ea5 | 7438 | if (!io_sqd_events_pending(sqd) && !current->task_works) { |
1a924a80 PB |
7439 | bool needs_sched = true; |
7440 | ||
724cb4f9 | 7441 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
aaa9f0f4 PB |
7442 | io_ring_set_wakeup_flag(ctx); |
7443 | ||
724cb4f9 HX |
7444 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
7445 | !list_empty_careful(&ctx->iopoll_list)) { | |
7446 | needs_sched = false; | |
7447 | break; | |
7448 | } | |
7449 | if (io_sqring_entries(ctx)) { | |
7450 | needs_sched = false; | |
7451 | break; | |
7452 | } | |
7453 | } | |
7454 | ||
7455 | if (needs_sched) { | |
7456 | mutex_unlock(&sqd->lock); | |
7457 | schedule(); | |
7458 | mutex_lock(&sqd->lock); | |
7459 | } | |
69fb2131 JA |
7460 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7461 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 7462 | } |
08369246 XW |
7463 | |
7464 | finish_wait(&sqd->wait, &wait); | |
7465 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 | 7466 | } |
28cea78a | 7467 | |
78cc687b | 7468 | io_uring_cancel_generic(true, sqd); |
37d1e2e3 | 7469 | sqd->thread = NULL; |
05962f95 | 7470 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
5f3f26f9 | 7471 | io_ring_set_wakeup_flag(ctx); |
521d6a73 | 7472 | io_run_task_work(); |
734551df PB |
7473 | mutex_unlock(&sqd->lock); |
7474 | ||
37d1e2e3 JA |
7475 | complete(&sqd->exited); |
7476 | do_exit(0); | |
6c271ce2 JA |
7477 | } |
7478 | ||
bda52162 JA |
7479 | struct io_wait_queue { |
7480 | struct wait_queue_entry wq; | |
7481 | struct io_ring_ctx *ctx; | |
5fd46178 | 7482 | unsigned cq_tail; |
bda52162 JA |
7483 | unsigned nr_timeouts; |
7484 | }; | |
7485 | ||
6c503150 | 7486 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
7487 | { |
7488 | struct io_ring_ctx *ctx = iowq->ctx; | |
5fd46178 | 7489 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
bda52162 JA |
7490 | |
7491 | /* | |
d195a66e | 7492 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
7493 | * started waiting. For timeouts, we always want to return to userspace, |
7494 | * regardless of event count. | |
7495 | */ | |
5fd46178 | 7496 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
bda52162 JA |
7497 | } |
7498 | ||
7499 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
7500 | int wake_flags, void *key) | |
7501 | { | |
7502 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
7503 | wq); | |
7504 | ||
6c503150 PB |
7505 | /* |
7506 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
7507 | * the task, and the next invocation will do it. | |
7508 | */ | |
5ed7a37d | 7509 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow)) |
6c503150 PB |
7510 | return autoremove_wake_function(curr, mode, wake_flags, key); |
7511 | return -1; | |
bda52162 JA |
7512 | } |
7513 | ||
af9c1a44 JA |
7514 | static int io_run_task_work_sig(void) |
7515 | { | |
7516 | if (io_run_task_work()) | |
7517 | return 1; | |
7518 | if (!signal_pending(current)) | |
7519 | return 0; | |
0b8cfa97 | 7520 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
792ee0f6 | 7521 | return -ERESTARTSYS; |
af9c1a44 JA |
7522 | return -EINTR; |
7523 | } | |
7524 | ||
eeb60b9a PB |
7525 | /* when returns >0, the caller should retry */ |
7526 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
7527 | struct io_wait_queue *iowq, | |
7528 | signed long *timeout) | |
7529 | { | |
7530 | int ret; | |
7531 | ||
7532 | /* make sure we run task_work before checking for signals */ | |
7533 | ret = io_run_task_work_sig(); | |
7534 | if (ret || io_should_wake(iowq)) | |
7535 | return ret; | |
7536 | /* let the caller flush overflows, retry */ | |
5ed7a37d | 7537 | if (test_bit(0, &ctx->check_cq_overflow)) |
eeb60b9a PB |
7538 | return 1; |
7539 | ||
7540 | *timeout = schedule_timeout(*timeout); | |
7541 | return !*timeout ? -ETIME : 1; | |
7542 | } | |
7543 | ||
2b188cc1 JA |
7544 | /* |
7545 | * Wait until events become available, if we don't already have some. The | |
7546 | * application must reap them itself, as they reside on the shared cq ring. | |
7547 | */ | |
7548 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
7549 | const sigset_t __user *sig, size_t sigsz, |
7550 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 7551 | { |
90291099 | 7552 | struct io_wait_queue iowq; |
75b28aff | 7553 | struct io_rings *rings = ctx->rings; |
c1d5a224 PB |
7554 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
7555 | int ret; | |
2b188cc1 | 7556 | |
b41e9852 | 7557 | do { |
90f67366 | 7558 | io_cqring_overflow_flush(ctx); |
6c503150 | 7559 | if (io_cqring_events(ctx) >= min_events) |
b41e9852 | 7560 | return 0; |
4c6e277c | 7561 | if (!io_run_task_work()) |
b41e9852 | 7562 | break; |
b41e9852 | 7563 | } while (1); |
2b188cc1 | 7564 | |
44df58d4 XW |
7565 | if (uts) { |
7566 | struct timespec64 ts; | |
7567 | ||
7568 | if (get_timespec64(&ts, uts)) | |
7569 | return -EFAULT; | |
7570 | timeout = timespec64_to_jiffies(&ts); | |
7571 | } | |
7572 | ||
2b188cc1 | 7573 | if (sig) { |
9e75ad5d AB |
7574 | #ifdef CONFIG_COMPAT |
7575 | if (in_compat_syscall()) | |
7576 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 7577 | sigsz); |
9e75ad5d AB |
7578 | else |
7579 | #endif | |
b772434b | 7580 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 7581 | |
2b188cc1 JA |
7582 | if (ret) |
7583 | return ret; | |
7584 | } | |
7585 | ||
90291099 PB |
7586 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
7587 | iowq.wq.private = current; | |
7588 | INIT_LIST_HEAD(&iowq.wq.entry); | |
7589 | iowq.ctx = ctx; | |
bda52162 | 7590 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
5fd46178 | 7591 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
90291099 | 7592 | |
c826bd7a | 7593 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 7594 | do { |
ca0a2651 | 7595 | /* if we can't even flush overflow, don't wait for more */ |
90f67366 | 7596 | if (!io_cqring_overflow_flush(ctx)) { |
ca0a2651 JA |
7597 | ret = -EBUSY; |
7598 | break; | |
7599 | } | |
311997b3 | 7600 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
bda52162 | 7601 | TASK_INTERRUPTIBLE); |
eeb60b9a | 7602 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
311997b3 | 7603 | finish_wait(&ctx->cq_wait, &iowq.wq); |
ca0a2651 | 7604 | cond_resched(); |
eeb60b9a | 7605 | } while (ret > 0); |
bda52162 | 7606 | |
b7db41c9 | 7607 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7608 | |
75b28aff | 7609 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7610 | } |
7611 | ||
9123c8ff | 7612 | static void io_free_page_table(void **table, size_t size) |
05f3fb3c | 7613 | { |
9123c8ff | 7614 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
05f3fb3c | 7615 | |
846a4ef2 | 7616 | for (i = 0; i < nr_tables; i++) |
9123c8ff PB |
7617 | kfree(table[i]); |
7618 | kfree(table); | |
7619 | } | |
7620 | ||
7621 | static void **io_alloc_page_table(size_t size) | |
7622 | { | |
7623 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); | |
7624 | size_t init_size = size; | |
7625 | void **table; | |
7626 | ||
0bea96f5 | 7627 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7628 | if (!table) |
7629 | return NULL; | |
7630 | ||
7631 | for (i = 0; i < nr_tables; i++) { | |
27f6b318 | 7632 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
9123c8ff | 7633 | |
0bea96f5 | 7634 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7635 | if (!table[i]) { |
7636 | io_free_page_table(table, init_size); | |
7637 | return NULL; | |
7638 | } | |
7639 | size -= this_size; | |
7640 | } | |
7641 | return table; | |
05f3fb3c JA |
7642 | } |
7643 | ||
28a9fe25 | 7644 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
1642b445 | 7645 | { |
28a9fe25 PB |
7646 | percpu_ref_exit(&ref_node->refs); |
7647 | kfree(ref_node); | |
1642b445 PB |
7648 | } |
7649 | ||
b9bd2bea PB |
7650 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
7651 | { | |
7652 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); | |
7653 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; | |
7654 | unsigned long flags; | |
7655 | bool first_add = false; | |
7656 | ||
7657 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); | |
7658 | node->done = true; | |
7659 | ||
7660 | while (!list_empty(&ctx->rsrc_ref_list)) { | |
7661 | node = list_first_entry(&ctx->rsrc_ref_list, | |
7662 | struct io_rsrc_node, node); | |
7663 | /* recycle ref nodes in order */ | |
7664 | if (!node->done) | |
7665 | break; | |
7666 | list_del(&node->node); | |
7667 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); | |
7668 | } | |
7669 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); | |
7670 | ||
7671 | if (first_add) | |
7672 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ); | |
7673 | } | |
7674 | ||
7675 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx) | |
7676 | { | |
7677 | struct io_rsrc_node *ref_node; | |
7678 | ||
7679 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); | |
7680 | if (!ref_node) | |
7681 | return NULL; | |
7682 | ||
7683 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, | |
7684 | 0, GFP_KERNEL)) { | |
7685 | kfree(ref_node); | |
7686 | return NULL; | |
7687 | } | |
7688 | INIT_LIST_HEAD(&ref_node->node); | |
7689 | INIT_LIST_HEAD(&ref_node->rsrc_list); | |
7690 | ref_node->done = false; | |
7691 | return ref_node; | |
7692 | } | |
7693 | ||
a7f0ed5a PB |
7694 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
7695 | struct io_rsrc_data *data_to_kill) | |
6b06314c | 7696 | { |
a7f0ed5a PB |
7697 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
7698 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); | |
6b06314c | 7699 | |
a7f0ed5a PB |
7700 | if (data_to_kill) { |
7701 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; | |
82fbcfa9 | 7702 | |
a7f0ed5a | 7703 | rsrc_node->rsrc_data = data_to_kill; |
4956b9ea | 7704 | spin_lock_irq(&ctx->rsrc_ref_lock); |
a7f0ed5a | 7705 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
4956b9ea | 7706 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
82fbcfa9 | 7707 | |
3e942498 | 7708 | atomic_inc(&data_to_kill->refs); |
a7f0ed5a PB |
7709 | percpu_ref_kill(&rsrc_node->refs); |
7710 | ctx->rsrc_node = NULL; | |
7711 | } | |
6b06314c | 7712 | |
a7f0ed5a PB |
7713 | if (!ctx->rsrc_node) { |
7714 | ctx->rsrc_node = ctx->rsrc_backup_node; | |
7715 | ctx->rsrc_backup_node = NULL; | |
7716 | } | |
8bad28d8 HX |
7717 | } |
7718 | ||
a7f0ed5a | 7719 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
8dd03afe PB |
7720 | { |
7721 | if (ctx->rsrc_backup_node) | |
7722 | return 0; | |
b895c9a6 | 7723 | ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx); |
8dd03afe | 7724 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
8bad28d8 HX |
7725 | } |
7726 | ||
40ae0ff7 | 7727 | static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, struct io_ring_ctx *ctx) |
8bad28d8 HX |
7728 | { |
7729 | int ret; | |
05589553 | 7730 | |
215c3902 | 7731 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
8bad28d8 HX |
7732 | if (data->quiesce) |
7733 | return -ENXIO; | |
05589553 | 7734 | |
8bad28d8 | 7735 | data->quiesce = true; |
1ffc5422 | 7736 | do { |
a7f0ed5a | 7737 | ret = io_rsrc_node_switch_start(ctx); |
8dd03afe | 7738 | if (ret) |
f2303b1f | 7739 | break; |
a7f0ed5a | 7740 | io_rsrc_node_switch(ctx, data); |
f2303b1f | 7741 | |
3e942498 PB |
7742 | /* kill initial ref, already quiesced if zero */ |
7743 | if (atomic_dec_and_test(&data->refs)) | |
7744 | break; | |
c018db4a | 7745 | mutex_unlock(&ctx->uring_lock); |
8bad28d8 | 7746 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 | 7747 | ret = wait_for_completion_interruptible(&data->done); |
c018db4a JA |
7748 | if (!ret) { |
7749 | mutex_lock(&ctx->uring_lock); | |
1ffc5422 | 7750 | break; |
c018db4a | 7751 | } |
8bad28d8 | 7752 | |
3e942498 PB |
7753 | atomic_inc(&data->refs); |
7754 | /* wait for all works potentially completing data->done */ | |
7755 | flush_delayed_work(&ctx->rsrc_put_work); | |
cb5e1b81 | 7756 | reinit_completion(&data->done); |
8dd03afe | 7757 | |
1ffc5422 | 7758 | ret = io_run_task_work_sig(); |
8bad28d8 | 7759 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 7760 | } while (ret >= 0); |
8bad28d8 | 7761 | data->quiesce = false; |
05f3fb3c | 7762 | |
8bad28d8 | 7763 | return ret; |
d7954b2b BM |
7764 | } |
7765 | ||
2d091d62 PB |
7766 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
7767 | { | |
7768 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; | |
7769 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; | |
7770 | ||
7771 | return &data->tags[table_idx][off]; | |
7772 | } | |
7773 | ||
44b31f2f | 7774 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
1ad555c6 | 7775 | { |
2d091d62 PB |
7776 | size_t size = data->nr * sizeof(data->tags[0][0]); |
7777 | ||
7778 | if (data->tags) | |
7779 | io_free_page_table((void **)data->tags, size); | |
44b31f2f PB |
7780 | kfree(data); |
7781 | } | |
7782 | ||
d878c816 PB |
7783 | static int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
7784 | u64 __user *utags, unsigned nr, | |
7785 | struct io_rsrc_data **pdata) | |
1ad555c6 | 7786 | { |
b895c9a6 | 7787 | struct io_rsrc_data *data; |
2d091d62 | 7788 | int ret = -ENOMEM; |
d878c816 | 7789 | unsigned i; |
1ad555c6 BM |
7790 | |
7791 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7792 | if (!data) | |
d878c816 | 7793 | return -ENOMEM; |
2d091d62 | 7794 | data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0])); |
b60c8dce | 7795 | if (!data->tags) { |
1ad555c6 | 7796 | kfree(data); |
d878c816 PB |
7797 | return -ENOMEM; |
7798 | } | |
2d091d62 PB |
7799 | |
7800 | data->nr = nr; | |
7801 | data->ctx = ctx; | |
7802 | data->do_put = do_put; | |
d878c816 | 7803 | if (utags) { |
2d091d62 | 7804 | ret = -EFAULT; |
d878c816 | 7805 | for (i = 0; i < nr; i++) { |
fdd1dc31 CIK |
7806 | u64 *tag_slot = io_get_tag_slot(data, i); |
7807 | ||
7808 | if (copy_from_user(tag_slot, &utags[i], | |
7809 | sizeof(*tag_slot))) | |
2d091d62 | 7810 | goto fail; |
d878c816 | 7811 | } |
1ad555c6 | 7812 | } |
b60c8dce | 7813 | |
3e942498 | 7814 | atomic_set(&data->refs, 1); |
1ad555c6 | 7815 | init_completion(&data->done); |
d878c816 PB |
7816 | *pdata = data; |
7817 | return 0; | |
2d091d62 PB |
7818 | fail: |
7819 | io_rsrc_data_free(data); | |
7820 | return ret; | |
1ad555c6 BM |
7821 | } |
7822 | ||
9123c8ff PB |
7823 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
7824 | { | |
0bea96f5 PB |
7825 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
7826 | GFP_KERNEL_ACCOUNT); | |
9123c8ff PB |
7827 | return !!table->files; |
7828 | } | |
7829 | ||
042b0d85 | 7830 | static void io_free_file_tables(struct io_file_table *table) |
9123c8ff | 7831 | { |
042b0d85 | 7832 | kvfree(table->files); |
9123c8ff PB |
7833 | table->files = NULL; |
7834 | } | |
7835 | ||
fff4db76 | 7836 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
1ad555c6 | 7837 | { |
fff4db76 PB |
7838 | #if defined(CONFIG_UNIX) |
7839 | if (ctx->ring_sock) { | |
7840 | struct sock *sock = ctx->ring_sock->sk; | |
7841 | struct sk_buff *skb; | |
7842 | ||
7843 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
7844 | kfree_skb(skb); | |
7845 | } | |
7846 | #else | |
7847 | int i; | |
7848 | ||
7849 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7850 | struct file *file; | |
7851 | ||
7852 | file = io_file_from_index(ctx, i); | |
7853 | if (file) | |
7854 | fput(file); | |
7855 | } | |
7856 | #endif | |
042b0d85 | 7857 | io_free_file_tables(&ctx->file_table); |
44b31f2f | 7858 | io_rsrc_data_free(ctx->file_data); |
fff4db76 PB |
7859 | ctx->file_data = NULL; |
7860 | ctx->nr_user_files = 0; | |
1ad555c6 BM |
7861 | } |
7862 | ||
d7954b2b BM |
7863 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7864 | { | |
d7954b2b BM |
7865 | int ret; |
7866 | ||
08480400 | 7867 | if (!ctx->file_data) |
d7954b2b | 7868 | return -ENXIO; |
08480400 PB |
7869 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
7870 | if (!ret) | |
7871 | __io_sqe_files_unregister(ctx); | |
7872 | return ret; | |
6b06314c JA |
7873 | } |
7874 | ||
37d1e2e3 | 7875 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
09a6f4ef | 7876 | __releases(&sqd->lock) |
37d1e2e3 | 7877 | { |
521d6a73 PB |
7878 | WARN_ON_ONCE(sqd->thread == current); |
7879 | ||
9e138a48 PB |
7880 | /* |
7881 | * Do the dance but not conditional clear_bit() because it'd race with | |
7882 | * other threads incrementing park_pending and setting the bit. | |
7883 | */ | |
37d1e2e3 | 7884 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
9e138a48 PB |
7885 | if (atomic_dec_return(&sqd->park_pending)) |
7886 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
09a6f4ef | 7887 | mutex_unlock(&sqd->lock); |
37d1e2e3 JA |
7888 | } |
7889 | ||
86e0d676 | 7890 | static void io_sq_thread_park(struct io_sq_data *sqd) |
09a6f4ef | 7891 | __acquires(&sqd->lock) |
37d1e2e3 | 7892 | { |
521d6a73 PB |
7893 | WARN_ON_ONCE(sqd->thread == current); |
7894 | ||
9e138a48 | 7895 | atomic_inc(&sqd->park_pending); |
86e0d676 | 7896 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
09a6f4ef | 7897 | mutex_lock(&sqd->lock); |
05962f95 | 7898 | if (sqd->thread) |
86e0d676 | 7899 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
7900 | } |
7901 | ||
7902 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
7903 | { | |
521d6a73 | 7904 | WARN_ON_ONCE(sqd->thread == current); |
88885f66 | 7905 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
521d6a73 | 7906 | |
05962f95 | 7907 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
88885f66 | 7908 | mutex_lock(&sqd->lock); |
e8f98f24 JA |
7909 | if (sqd->thread) |
7910 | wake_up_process(sqd->thread); | |
09a6f4ef | 7911 | mutex_unlock(&sqd->lock); |
05962f95 | 7912 | wait_for_completion(&sqd->exited); |
37d1e2e3 JA |
7913 | } |
7914 | ||
534ca6d6 | 7915 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7916 | { |
534ca6d6 | 7917 | if (refcount_dec_and_test(&sqd->refs)) { |
9e138a48 PB |
7918 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
7919 | ||
37d1e2e3 JA |
7920 | io_sq_thread_stop(sqd); |
7921 | kfree(sqd); | |
7922 | } | |
7923 | } | |
7924 | ||
7925 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
7926 | { | |
7927 | struct io_sq_data *sqd = ctx->sq_data; | |
7928 | ||
7929 | if (sqd) { | |
05962f95 | 7930 | io_sq_thread_park(sqd); |
521d6a73 | 7931 | list_del_init(&ctx->sqd_list); |
37d1e2e3 | 7932 | io_sqd_update_thread_idle(sqd); |
05962f95 | 7933 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
7934 | |
7935 | io_put_sq_data(sqd); | |
7936 | ctx->sq_data = NULL; | |
534ca6d6 JA |
7937 | } |
7938 | } | |
7939 | ||
aa06165d JA |
7940 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7941 | { | |
7942 | struct io_ring_ctx *ctx_attach; | |
7943 | struct io_sq_data *sqd; | |
7944 | struct fd f; | |
7945 | ||
7946 | f = fdget(p->wq_fd); | |
7947 | if (!f.file) | |
7948 | return ERR_PTR(-ENXIO); | |
7949 | if (f.file->f_op != &io_uring_fops) { | |
7950 | fdput(f); | |
7951 | return ERR_PTR(-EINVAL); | |
7952 | } | |
7953 | ||
7954 | ctx_attach = f.file->private_data; | |
7955 | sqd = ctx_attach->sq_data; | |
7956 | if (!sqd) { | |
7957 | fdput(f); | |
7958 | return ERR_PTR(-EINVAL); | |
7959 | } | |
5c2469e0 JA |
7960 | if (sqd->task_tgid != current->tgid) { |
7961 | fdput(f); | |
7962 | return ERR_PTR(-EPERM); | |
7963 | } | |
aa06165d JA |
7964 | |
7965 | refcount_inc(&sqd->refs); | |
7966 | fdput(f); | |
7967 | return sqd; | |
7968 | } | |
7969 | ||
26984fbf PB |
7970 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
7971 | bool *attached) | |
534ca6d6 JA |
7972 | { |
7973 | struct io_sq_data *sqd; | |
7974 | ||
26984fbf | 7975 | *attached = false; |
5c2469e0 JA |
7976 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
7977 | sqd = io_attach_sq_data(p); | |
26984fbf PB |
7978 | if (!IS_ERR(sqd)) { |
7979 | *attached = true; | |
5c2469e0 | 7980 | return sqd; |
26984fbf | 7981 | } |
5c2469e0 JA |
7982 | /* fall through for EPERM case, setup new sqd/task */ |
7983 | if (PTR_ERR(sqd) != -EPERM) | |
7984 | return sqd; | |
7985 | } | |
aa06165d | 7986 | |
534ca6d6 JA |
7987 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7988 | if (!sqd) | |
7989 | return ERR_PTR(-ENOMEM); | |
7990 | ||
9e138a48 | 7991 | atomic_set(&sqd->park_pending, 0); |
534ca6d6 | 7992 | refcount_set(&sqd->refs, 1); |
69fb2131 | 7993 | INIT_LIST_HEAD(&sqd->ctx_list); |
09a6f4ef | 7994 | mutex_init(&sqd->lock); |
534ca6d6 | 7995 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 7996 | init_completion(&sqd->exited); |
534ca6d6 JA |
7997 | return sqd; |
7998 | } | |
7999 | ||
6b06314c | 8000 | #if defined(CONFIG_UNIX) |
6b06314c JA |
8001 | /* |
8002 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
8003 | * the io_uring can be safely unregistered on process exit, even if we have | |
8004 | * loops in the file referencing. | |
8005 | */ | |
8006 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
8007 | { | |
8008 | struct sock *sk = ctx->ring_sock->sk; | |
8009 | struct scm_fp_list *fpl; | |
8010 | struct sk_buff *skb; | |
08a45173 | 8011 | int i, nr_files; |
6b06314c | 8012 | |
6b06314c JA |
8013 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
8014 | if (!fpl) | |
8015 | return -ENOMEM; | |
8016 | ||
8017 | skb = alloc_skb(0, GFP_KERNEL); | |
8018 | if (!skb) { | |
8019 | kfree(fpl); | |
8020 | return -ENOMEM; | |
8021 | } | |
8022 | ||
8023 | skb->sk = sk; | |
6b06314c | 8024 | |
08a45173 | 8025 | nr_files = 0; |
62e398be | 8026 | fpl->user = get_uid(current_user()); |
6b06314c | 8027 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
8028 | struct file *file = io_file_from_index(ctx, i + offset); |
8029 | ||
8030 | if (!file) | |
08a45173 | 8031 | continue; |
65e19f54 | 8032 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
8033 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
8034 | nr_files++; | |
6b06314c JA |
8035 | } |
8036 | ||
08a45173 JA |
8037 | if (nr_files) { |
8038 | fpl->max = SCM_MAX_FD; | |
8039 | fpl->count = nr_files; | |
8040 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 8041 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
8042 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
8043 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 8044 | |
08a45173 JA |
8045 | for (i = 0; i < nr_files; i++) |
8046 | fput(fpl->fp[i]); | |
8047 | } else { | |
8048 | kfree_skb(skb); | |
8049 | kfree(fpl); | |
8050 | } | |
6b06314c JA |
8051 | |
8052 | return 0; | |
8053 | } | |
8054 | ||
8055 | /* | |
8056 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
8057 | * causes regular reference counting to break down. We rely on the UNIX | |
8058 | * garbage collection to take care of this problem for us. | |
8059 | */ | |
8060 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8061 | { | |
8062 | unsigned left, total; | |
8063 | int ret = 0; | |
8064 | ||
8065 | total = 0; | |
8066 | left = ctx->nr_user_files; | |
8067 | while (left) { | |
8068 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
8069 | |
8070 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
8071 | if (ret) | |
8072 | break; | |
8073 | left -= this_files; | |
8074 | total += this_files; | |
8075 | } | |
8076 | ||
8077 | if (!ret) | |
8078 | return 0; | |
8079 | ||
8080 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
8081 | struct file *file = io_file_from_index(ctx, total); |
8082 | ||
8083 | if (file) | |
8084 | fput(file); | |
6b06314c JA |
8085 | total++; |
8086 | } | |
8087 | ||
8088 | return ret; | |
8089 | } | |
8090 | #else | |
8091 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8092 | { | |
8093 | return 0; | |
8094 | } | |
8095 | #endif | |
8096 | ||
47e90392 | 8097 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 8098 | { |
50238531 | 8099 | struct file *file = prsrc->file; |
05f3fb3c JA |
8100 | #if defined(CONFIG_UNIX) |
8101 | struct sock *sock = ctx->ring_sock->sk; | |
8102 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
8103 | struct sk_buff *skb; | |
8104 | int i; | |
8105 | ||
8106 | __skb_queue_head_init(&list); | |
8107 | ||
8108 | /* | |
8109 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
8110 | * remove this entry and rearrange the file array. | |
8111 | */ | |
8112 | skb = skb_dequeue(head); | |
8113 | while (skb) { | |
8114 | struct scm_fp_list *fp; | |
8115 | ||
8116 | fp = UNIXCB(skb).fp; | |
8117 | for (i = 0; i < fp->count; i++) { | |
8118 | int left; | |
8119 | ||
8120 | if (fp->fp[i] != file) | |
8121 | continue; | |
8122 | ||
8123 | unix_notinflight(fp->user, fp->fp[i]); | |
8124 | left = fp->count - 1 - i; | |
8125 | if (left) { | |
8126 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
8127 | left * sizeof(struct file *)); | |
8128 | } | |
8129 | fp->count--; | |
8130 | if (!fp->count) { | |
8131 | kfree_skb(skb); | |
8132 | skb = NULL; | |
8133 | } else { | |
8134 | __skb_queue_tail(&list, skb); | |
8135 | } | |
8136 | fput(file); | |
8137 | file = NULL; | |
8138 | break; | |
8139 | } | |
8140 | ||
8141 | if (!file) | |
8142 | break; | |
8143 | ||
8144 | __skb_queue_tail(&list, skb); | |
8145 | ||
8146 | skb = skb_dequeue(head); | |
8147 | } | |
8148 | ||
8149 | if (skb_peek(&list)) { | |
8150 | spin_lock_irq(&head->lock); | |
8151 | while ((skb = __skb_dequeue(&list)) != NULL) | |
8152 | __skb_queue_tail(head, skb); | |
8153 | spin_unlock_irq(&head->lock); | |
8154 | } | |
8155 | #else | |
8156 | fput(file); | |
8157 | #endif | |
8158 | } | |
8159 | ||
b895c9a6 | 8160 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
65e19f54 | 8161 | { |
b895c9a6 | 8162 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
269bbe5f BM |
8163 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
8164 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 8165 | |
269bbe5f BM |
8166 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
8167 | list_del(&prsrc->list); | |
b60c8dce PB |
8168 | |
8169 | if (prsrc->tag) { | |
8170 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; | |
b60c8dce PB |
8171 | |
8172 | io_ring_submit_lock(ctx, lock_ring); | |
79ebeaee | 8173 | spin_lock(&ctx->completion_lock); |
b60c8dce | 8174 | io_cqring_fill_event(ctx, prsrc->tag, 0, 0); |
2840f710 | 8175 | ctx->cq_extra++; |
b60c8dce | 8176 | io_commit_cqring(ctx); |
79ebeaee | 8177 | spin_unlock(&ctx->completion_lock); |
b60c8dce PB |
8178 | io_cqring_ev_posted(ctx); |
8179 | io_ring_submit_unlock(ctx, lock_ring); | |
8180 | } | |
8181 | ||
40ae0ff7 | 8182 | rsrc_data->do_put(ctx, prsrc); |
269bbe5f | 8183 | kfree(prsrc); |
65e19f54 | 8184 | } |
05589553 | 8185 | |
28a9fe25 | 8186 | io_rsrc_node_destroy(ref_node); |
3e942498 PB |
8187 | if (atomic_dec_and_test(&rsrc_data->refs)) |
8188 | complete(&rsrc_data->done); | |
2faf852d | 8189 | } |
65e19f54 | 8190 | |
269bbe5f | 8191 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
8192 | { |
8193 | struct io_ring_ctx *ctx; | |
8194 | struct llist_node *node; | |
8195 | ||
269bbe5f BM |
8196 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
8197 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
8198 | |
8199 | while (node) { | |
b895c9a6 | 8200 | struct io_rsrc_node *ref_node; |
4a38aed2 JA |
8201 | struct llist_node *next = node->next; |
8202 | ||
b895c9a6 | 8203 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
269bbe5f | 8204 | __io_rsrc_put_work(ref_node); |
4a38aed2 JA |
8205 | node = next; |
8206 | } | |
8207 | } | |
8208 | ||
6b06314c | 8209 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
792e3582 | 8210 | unsigned nr_args, u64 __user *tags) |
6b06314c JA |
8211 | { |
8212 | __s32 __user *fds = (__s32 __user *) arg; | |
05f3fb3c | 8213 | struct file *file; |
f3baed39 | 8214 | int fd, ret; |
846a4ef2 | 8215 | unsigned i; |
6b06314c | 8216 | |
05f3fb3c | 8217 | if (ctx->file_data) |
6b06314c JA |
8218 | return -EBUSY; |
8219 | if (!nr_args) | |
8220 | return -EINVAL; | |
8221 | if (nr_args > IORING_MAX_FIXED_FILES) | |
8222 | return -EMFILE; | |
3a1b8a4e PB |
8223 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
8224 | return -EMFILE; | |
a7f0ed5a | 8225 | ret = io_rsrc_node_switch_start(ctx); |
f3baed39 PB |
8226 | if (ret) |
8227 | return ret; | |
d878c816 PB |
8228 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
8229 | &ctx->file_data); | |
8230 | if (ret) | |
8231 | return ret; | |
6b06314c | 8232 | |
f3baed39 | 8233 | ret = -ENOMEM; |
aeca241b | 8234 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
1ad555c6 | 8235 | goto out_free; |
65e19f54 | 8236 | |
08a45173 | 8237 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
d878c816 | 8238 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
600cf3f8 PB |
8239 | ret = -EFAULT; |
8240 | goto out_fput; | |
8241 | } | |
08a45173 | 8242 | /* allow sparse sets */ |
792e3582 PB |
8243 | if (fd == -1) { |
8244 | ret = -EINVAL; | |
2d091d62 | 8245 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
792e3582 | 8246 | goto out_fput; |
08a45173 | 8247 | continue; |
792e3582 | 8248 | } |
6b06314c | 8249 | |
05f3fb3c | 8250 | file = fget(fd); |
6b06314c | 8251 | ret = -EBADF; |
792e3582 | 8252 | if (unlikely(!file)) |
600cf3f8 | 8253 | goto out_fput; |
05f3fb3c | 8254 | |
6b06314c JA |
8255 | /* |
8256 | * Don't allow io_uring instances to be registered. If UNIX | |
8257 | * isn't enabled, then this causes a reference cycle and this | |
8258 | * instance can never get freed. If UNIX is enabled we'll | |
8259 | * handle it just fine, but there's still no point in allowing | |
8260 | * a ring fd as it doesn't support regular read/write anyway. | |
8261 | */ | |
05f3fb3c JA |
8262 | if (file->f_op == &io_uring_fops) { |
8263 | fput(file); | |
600cf3f8 | 8264 | goto out_fput; |
6b06314c | 8265 | } |
aeca241b | 8266 | io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file); |
6b06314c JA |
8267 | } |
8268 | ||
6b06314c | 8269 | ret = io_sqe_files_scm(ctx); |
05589553 | 8270 | if (ret) { |
08480400 | 8271 | __io_sqe_files_unregister(ctx); |
05589553 XW |
8272 | return ret; |
8273 | } | |
6b06314c | 8274 | |
a7f0ed5a | 8275 | io_rsrc_node_switch(ctx, NULL); |
6b06314c | 8276 | return ret; |
600cf3f8 PB |
8277 | out_fput: |
8278 | for (i = 0; i < ctx->nr_user_files; i++) { | |
8279 | file = io_file_from_index(ctx, i); | |
8280 | if (file) | |
8281 | fput(file); | |
8282 | } | |
042b0d85 | 8283 | io_free_file_tables(&ctx->file_table); |
600cf3f8 | 8284 | ctx->nr_user_files = 0; |
600cf3f8 | 8285 | out_free: |
44b31f2f | 8286 | io_rsrc_data_free(ctx->file_data); |
55cbc256 | 8287 | ctx->file_data = NULL; |
6b06314c JA |
8288 | return ret; |
8289 | } | |
8290 | ||
c3a31e60 JA |
8291 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
8292 | int index) | |
8293 | { | |
8294 | #if defined(CONFIG_UNIX) | |
8295 | struct sock *sock = ctx->ring_sock->sk; | |
8296 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
8297 | struct sk_buff *skb; | |
8298 | ||
8299 | /* | |
8300 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
8301 | * file set. If there's no room, fall back to allocating a new skb | |
8302 | * and filling it in. | |
8303 | */ | |
8304 | spin_lock_irq(&head->lock); | |
8305 | skb = skb_peek(head); | |
8306 | if (skb) { | |
8307 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
8308 | ||
8309 | if (fpl->count < SCM_MAX_FD) { | |
8310 | __skb_unlink(skb, head); | |
8311 | spin_unlock_irq(&head->lock); | |
8312 | fpl->fp[fpl->count] = get_file(file); | |
8313 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
8314 | fpl->count++; | |
8315 | spin_lock_irq(&head->lock); | |
8316 | __skb_queue_head(head, skb); | |
8317 | } else { | |
8318 | skb = NULL; | |
8319 | } | |
8320 | } | |
8321 | spin_unlock_irq(&head->lock); | |
8322 | ||
8323 | if (skb) { | |
8324 | fput(file); | |
8325 | return 0; | |
8326 | } | |
8327 | ||
8328 | return __io_sqe_files_scm(ctx, 1, index); | |
8329 | #else | |
8330 | return 0; | |
8331 | #endif | |
8332 | } | |
8333 | ||
9c7b0ba8 PB |
8334 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
8335 | struct io_rsrc_node *node, void *rsrc) | |
8336 | { | |
8337 | struct io_rsrc_put *prsrc; | |
8338 | ||
8339 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); | |
8340 | if (!prsrc) | |
8341 | return -ENOMEM; | |
8342 | ||
8343 | prsrc->tag = *io_get_tag_slot(data, idx); | |
8344 | prsrc->rsrc = rsrc; | |
8345 | list_add(&prsrc->list, &node->rsrc_list); | |
8346 | return 0; | |
8347 | } | |
8348 | ||
b9445598 PB |
8349 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
8350 | unsigned int issue_flags, u32 slot_index) | |
8351 | { | |
8352 | struct io_ring_ctx *ctx = req->ctx; | |
8353 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; | |
9c7b0ba8 | 8354 | bool needs_switch = false; |
b9445598 PB |
8355 | struct io_fixed_file *file_slot; |
8356 | int ret = -EBADF; | |
8357 | ||
8358 | io_ring_submit_lock(ctx, !force_nonblock); | |
8359 | if (file->f_op == &io_uring_fops) | |
8360 | goto err; | |
8361 | ret = -ENXIO; | |
8362 | if (!ctx->file_data) | |
8363 | goto err; | |
8364 | ret = -EINVAL; | |
8365 | if (slot_index >= ctx->nr_user_files) | |
8366 | goto err; | |
8367 | ||
8368 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); | |
8369 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); | |
9c7b0ba8 PB |
8370 | |
8371 | if (file_slot->file_ptr) { | |
8372 | struct file *old_file; | |
8373 | ||
8374 | ret = io_rsrc_node_switch_start(ctx); | |
8375 | if (ret) | |
8376 | goto err; | |
8377 | ||
8378 | old_file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8379 | ret = io_queue_rsrc_removal(ctx->file_data, slot_index, | |
8380 | ctx->rsrc_node, old_file); | |
8381 | if (ret) | |
8382 | goto err; | |
8383 | file_slot->file_ptr = 0; | |
8384 | needs_switch = true; | |
8385 | } | |
b9445598 PB |
8386 | |
8387 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; | |
8388 | io_fixed_file_set(file_slot, file); | |
8389 | ret = io_sqe_file_register(ctx, file, slot_index); | |
8390 | if (ret) { | |
8391 | file_slot->file_ptr = 0; | |
8392 | goto err; | |
8393 | } | |
8394 | ||
8395 | ret = 0; | |
8396 | err: | |
9c7b0ba8 PB |
8397 | if (needs_switch) |
8398 | io_rsrc_node_switch(ctx, ctx->file_data); | |
b9445598 PB |
8399 | io_ring_submit_unlock(ctx, !force_nonblock); |
8400 | if (ret) | |
8401 | fput(file); | |
8402 | return ret; | |
8403 | } | |
8404 | ||
7df778be PB |
8405 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) |
8406 | { | |
8407 | unsigned int offset = req->close.file_slot - 1; | |
8408 | struct io_ring_ctx *ctx = req->ctx; | |
8409 | struct io_fixed_file *file_slot; | |
8410 | struct file *file; | |
8411 | int ret, i; | |
8412 | ||
8413 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
8414 | ret = -ENXIO; | |
8415 | if (unlikely(!ctx->file_data)) | |
8416 | goto out; | |
8417 | ret = -EINVAL; | |
8418 | if (offset >= ctx->nr_user_files) | |
8419 | goto out; | |
8420 | ret = io_rsrc_node_switch_start(ctx); | |
8421 | if (ret) | |
8422 | goto out; | |
8423 | ||
8424 | i = array_index_nospec(offset, ctx->nr_user_files); | |
8425 | file_slot = io_fixed_file_slot(&ctx->file_table, i); | |
8426 | ret = -EBADF; | |
8427 | if (!file_slot->file_ptr) | |
8428 | goto out; | |
8429 | ||
8430 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8431 | ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file); | |
8432 | if (ret) | |
8433 | goto out; | |
8434 | ||
8435 | file_slot->file_ptr = 0; | |
8436 | io_rsrc_node_switch(ctx, ctx->file_data); | |
8437 | ret = 0; | |
8438 | out: | |
8439 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
8440 | return ret; | |
8441 | } | |
8442 | ||
05f3fb3c | 8443 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
c3bdad02 | 8444 | struct io_uring_rsrc_update2 *up, |
05f3fb3c JA |
8445 | unsigned nr_args) |
8446 | { | |
c3bdad02 | 8447 | u64 __user *tags = u64_to_user_ptr(up->tags); |
98f0b3b4 | 8448 | __s32 __user *fds = u64_to_user_ptr(up->data); |
b895c9a6 | 8449 | struct io_rsrc_data *data = ctx->file_data; |
a04b0ac0 PB |
8450 | struct io_fixed_file *file_slot; |
8451 | struct file *file; | |
98f0b3b4 PB |
8452 | int fd, i, err = 0; |
8453 | unsigned int done; | |
05589553 | 8454 | bool needs_switch = false; |
c3a31e60 | 8455 | |
98f0b3b4 PB |
8456 | if (!ctx->file_data) |
8457 | return -ENXIO; | |
8458 | if (up->offset + nr_args > ctx->nr_user_files) | |
c3a31e60 JA |
8459 | return -EINVAL; |
8460 | ||
67973b93 | 8461 | for (done = 0; done < nr_args; done++) { |
c3bdad02 PB |
8462 | u64 tag = 0; |
8463 | ||
8464 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || | |
8465 | copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
c3a31e60 JA |
8466 | err = -EFAULT; |
8467 | break; | |
8468 | } | |
c3bdad02 PB |
8469 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
8470 | err = -EINVAL; | |
8471 | break; | |
8472 | } | |
4e0377a1 | 8473 | if (fd == IORING_REGISTER_FILES_SKIP) |
8474 | continue; | |
8475 | ||
67973b93 | 8476 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
aeca241b | 8477 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
ea64ec02 | 8478 | |
a04b0ac0 PB |
8479 | if (file_slot->file_ptr) { |
8480 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
b60c8dce PB |
8481 | err = io_queue_rsrc_removal(data, up->offset + done, |
8482 | ctx->rsrc_node, file); | |
a5318d3c HD |
8483 | if (err) |
8484 | break; | |
a04b0ac0 | 8485 | file_slot->file_ptr = 0; |
05589553 | 8486 | needs_switch = true; |
c3a31e60 JA |
8487 | } |
8488 | if (fd != -1) { | |
c3a31e60 JA |
8489 | file = fget(fd); |
8490 | if (!file) { | |
8491 | err = -EBADF; | |
8492 | break; | |
8493 | } | |
8494 | /* | |
8495 | * Don't allow io_uring instances to be registered. If | |
8496 | * UNIX isn't enabled, then this causes a reference | |
8497 | * cycle and this instance can never get freed. If UNIX | |
8498 | * is enabled we'll handle it just fine, but there's | |
8499 | * still no point in allowing a ring fd as it doesn't | |
8500 | * support regular read/write anyway. | |
8501 | */ | |
8502 | if (file->f_op == &io_uring_fops) { | |
8503 | fput(file); | |
8504 | err = -EBADF; | |
8505 | break; | |
8506 | } | |
2d091d62 | 8507 | *io_get_tag_slot(data, up->offset + done) = tag; |
9a321c98 | 8508 | io_fixed_file_set(file_slot, file); |
c3a31e60 | 8509 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 8510 | if (err) { |
a04b0ac0 | 8511 | file_slot->file_ptr = 0; |
f3bd9dae | 8512 | fput(file); |
c3a31e60 | 8513 | break; |
f3bd9dae | 8514 | } |
c3a31e60 | 8515 | } |
05f3fb3c JA |
8516 | } |
8517 | ||
a7f0ed5a PB |
8518 | if (needs_switch) |
8519 | io_rsrc_node_switch(ctx, data); | |
c3a31e60 JA |
8520 | return done ? done : err; |
8521 | } | |
05589553 | 8522 | |
685fe7fe JA |
8523 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
8524 | struct task_struct *task) | |
24369c2e | 8525 | { |
e941894e | 8526 | struct io_wq_hash *hash; |
24369c2e | 8527 | struct io_wq_data data; |
24369c2e | 8528 | unsigned int concurrency; |
24369c2e | 8529 | |
362a9e65 | 8530 | mutex_lock(&ctx->uring_lock); |
e941894e JA |
8531 | hash = ctx->hash_map; |
8532 | if (!hash) { | |
8533 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
362a9e65 YY |
8534 | if (!hash) { |
8535 | mutex_unlock(&ctx->uring_lock); | |
e941894e | 8536 | return ERR_PTR(-ENOMEM); |
362a9e65 | 8537 | } |
e941894e JA |
8538 | refcount_set(&hash->refs, 1); |
8539 | init_waitqueue_head(&hash->wait); | |
8540 | ctx->hash_map = hash; | |
24369c2e | 8541 | } |
362a9e65 | 8542 | mutex_unlock(&ctx->uring_lock); |
24369c2e | 8543 | |
e941894e | 8544 | data.hash = hash; |
685fe7fe | 8545 | data.task = task; |
ebc11b6c | 8546 | data.free_work = io_wq_free_work; |
f5fa38c5 | 8547 | data.do_work = io_wq_submit_work; |
24369c2e | 8548 | |
d25e3a3d JA |
8549 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
8550 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 8551 | |
5aa75ed5 | 8552 | return io_wq_create(concurrency, &data); |
24369c2e PB |
8553 | } |
8554 | ||
5aa75ed5 JA |
8555 | static int io_uring_alloc_task_context(struct task_struct *task, |
8556 | struct io_ring_ctx *ctx) | |
0f212204 JA |
8557 | { |
8558 | struct io_uring_task *tctx; | |
d8a6df10 | 8559 | int ret; |
0f212204 | 8560 | |
09899b19 | 8561 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
0f212204 JA |
8562 | if (unlikely(!tctx)) |
8563 | return -ENOMEM; | |
8564 | ||
d8a6df10 JA |
8565 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
8566 | if (unlikely(ret)) { | |
8567 | kfree(tctx); | |
8568 | return ret; | |
8569 | } | |
8570 | ||
685fe7fe | 8571 | tctx->io_wq = io_init_wq_offload(ctx, task); |
5aa75ed5 JA |
8572 | if (IS_ERR(tctx->io_wq)) { |
8573 | ret = PTR_ERR(tctx->io_wq); | |
8574 | percpu_counter_destroy(&tctx->inflight); | |
8575 | kfree(tctx); | |
8576 | return ret; | |
8577 | } | |
8578 | ||
0f212204 JA |
8579 | xa_init(&tctx->xa); |
8580 | init_waitqueue_head(&tctx->wait); | |
fdaf083c | 8581 | atomic_set(&tctx->in_idle, 0); |
b303fe2e | 8582 | atomic_set(&tctx->inflight_tracked, 0); |
0f212204 | 8583 | task->io_uring = tctx; |
7cbf1722 JA |
8584 | spin_lock_init(&tctx->task_lock); |
8585 | INIT_WQ_LIST(&tctx->task_list); | |
7cbf1722 | 8586 | init_task_work(&tctx->task_work, tctx_task_work); |
0f212204 JA |
8587 | return 0; |
8588 | } | |
8589 | ||
8590 | void __io_uring_free(struct task_struct *tsk) | |
8591 | { | |
8592 | struct io_uring_task *tctx = tsk->io_uring; | |
8593 | ||
8594 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e | 8595 | WARN_ON_ONCE(tctx->io_wq); |
09899b19 | 8596 | WARN_ON_ONCE(tctx->cached_refs); |
ef8eaa4e | 8597 | |
d8a6df10 | 8598 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
8599 | kfree(tctx); |
8600 | tsk->io_uring = NULL; | |
8601 | } | |
8602 | ||
7e84e1c7 SG |
8603 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
8604 | struct io_uring_params *p) | |
2b188cc1 JA |
8605 | { |
8606 | int ret; | |
8607 | ||
d25e3a3d JA |
8608 | /* Retain compatibility with failing for an invalid attach attempt */ |
8609 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
8610 | IORING_SETUP_ATTACH_WQ) { | |
8611 | struct fd f; | |
8612 | ||
8613 | f = fdget(p->wq_fd); | |
8614 | if (!f.file) | |
8615 | return -ENXIO; | |
0cc936f7 JA |
8616 | if (f.file->f_op != &io_uring_fops) { |
8617 | fdput(f); | |
f2a48dd0 | 8618 | return -EINVAL; |
0cc936f7 JA |
8619 | } |
8620 | fdput(f); | |
d25e3a3d | 8621 | } |
6c271ce2 | 8622 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 8623 | struct task_struct *tsk; |
534ca6d6 | 8624 | struct io_sq_data *sqd; |
26984fbf | 8625 | bool attached; |
534ca6d6 | 8626 | |
26984fbf | 8627 | sqd = io_get_sq_data(p, &attached); |
534ca6d6 JA |
8628 | if (IS_ERR(sqd)) { |
8629 | ret = PTR_ERR(sqd); | |
8630 | goto err; | |
8631 | } | |
69fb2131 | 8632 | |
7c30f36a | 8633 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 8634 | ctx->sq_data = sqd; |
917257da JA |
8635 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
8636 | if (!ctx->sq_thread_idle) | |
8637 | ctx->sq_thread_idle = HZ; | |
8638 | ||
78d7f6ba | 8639 | io_sq_thread_park(sqd); |
de75a3d3 PB |
8640 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
8641 | io_sqd_update_thread_idle(sqd); | |
26984fbf | 8642 | /* don't attach to a dying SQPOLL thread, would be racy */ |
f2a48dd0 | 8643 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
78d7f6ba PB |
8644 | io_sq_thread_unpark(sqd); |
8645 | ||
de75a3d3 PB |
8646 | if (ret < 0) |
8647 | goto err; | |
8648 | if (attached) | |
5aa75ed5 | 8649 | return 0; |
aa06165d | 8650 | |
6c271ce2 | 8651 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 8652 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 8653 | |
917257da | 8654 | ret = -EINVAL; |
f2a48dd0 | 8655 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
e8f98f24 | 8656 | goto err_sqpoll; |
37d1e2e3 | 8657 | sqd->sq_cpu = cpu; |
6c271ce2 | 8658 | } else { |
37d1e2e3 | 8659 | sqd->sq_cpu = -1; |
6c271ce2 | 8660 | } |
37d1e2e3 JA |
8661 | |
8662 | sqd->task_pid = current->pid; | |
5c2469e0 | 8663 | sqd->task_tgid = current->tgid; |
46fe18b1 JA |
8664 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
8665 | if (IS_ERR(tsk)) { | |
8666 | ret = PTR_ERR(tsk); | |
e8f98f24 | 8667 | goto err_sqpoll; |
6c271ce2 | 8668 | } |
97a73a0f | 8669 | |
46fe18b1 | 8670 | sqd->thread = tsk; |
97a73a0f | 8671 | ret = io_uring_alloc_task_context(tsk, ctx); |
46fe18b1 | 8672 | wake_up_new_task(tsk); |
0f212204 JA |
8673 | if (ret) |
8674 | goto err; | |
6c271ce2 JA |
8675 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
8676 | /* Can't have SQ_AFF without SQPOLL */ | |
8677 | ret = -EINVAL; | |
8678 | goto err; | |
8679 | } | |
8680 | ||
2b188cc1 | 8681 | return 0; |
f2a48dd0 PB |
8682 | err_sqpoll: |
8683 | complete(&ctx->sq_data->exited); | |
2b188cc1 | 8684 | err: |
37d1e2e3 | 8685 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
8686 | return ret; |
8687 | } | |
8688 | ||
a087e2b5 BM |
8689 | static inline void __io_unaccount_mem(struct user_struct *user, |
8690 | unsigned long nr_pages) | |
2b188cc1 JA |
8691 | { |
8692 | atomic_long_sub(nr_pages, &user->locked_vm); | |
8693 | } | |
8694 | ||
a087e2b5 BM |
8695 | static inline int __io_account_mem(struct user_struct *user, |
8696 | unsigned long nr_pages) | |
2b188cc1 JA |
8697 | { |
8698 | unsigned long page_limit, cur_pages, new_pages; | |
8699 | ||
8700 | /* Don't allow more pages than we can safely lock */ | |
8701 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
8702 | ||
8703 | do { | |
8704 | cur_pages = atomic_long_read(&user->locked_vm); | |
8705 | new_pages = cur_pages + nr_pages; | |
8706 | if (new_pages > page_limit) | |
8707 | return -ENOMEM; | |
8708 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
8709 | new_pages) != cur_pages); | |
8710 | ||
8711 | return 0; | |
8712 | } | |
8713 | ||
26bfa89e | 8714 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8715 | { |
62e398be | 8716 | if (ctx->user) |
a087e2b5 | 8717 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 8718 | |
26bfa89e JA |
8719 | if (ctx->mm_account) |
8720 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8721 | } |
8722 | ||
26bfa89e | 8723 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8724 | { |
30975825 BM |
8725 | int ret; |
8726 | ||
62e398be | 8727 | if (ctx->user) { |
30975825 BM |
8728 | ret = __io_account_mem(ctx->user, nr_pages); |
8729 | if (ret) | |
8730 | return ret; | |
8731 | } | |
8732 | ||
26bfa89e JA |
8733 | if (ctx->mm_account) |
8734 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8735 | |
8736 | return 0; | |
8737 | } | |
8738 | ||
2b188cc1 JA |
8739 | static void io_mem_free(void *ptr) |
8740 | { | |
52e04ef4 MR |
8741 | struct page *page; |
8742 | ||
8743 | if (!ptr) | |
8744 | return; | |
2b188cc1 | 8745 | |
52e04ef4 | 8746 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8747 | if (put_page_testzero(page)) |
8748 | free_compound_page(page); | |
8749 | } | |
8750 | ||
8751 | static void *io_mem_alloc(size_t size) | |
8752 | { | |
8753 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
26bfa89e | 8754 | __GFP_NORETRY | __GFP_ACCOUNT; |
2b188cc1 JA |
8755 | |
8756 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8757 | } | |
8758 | ||
75b28aff HV |
8759 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8760 | size_t *sq_offset) | |
8761 | { | |
8762 | struct io_rings *rings; | |
8763 | size_t off, sq_array_size; | |
8764 | ||
8765 | off = struct_size(rings, cqes, cq_entries); | |
8766 | if (off == SIZE_MAX) | |
8767 | return SIZE_MAX; | |
8768 | ||
8769 | #ifdef CONFIG_SMP | |
8770 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8771 | if (off == 0) | |
8772 | return SIZE_MAX; | |
8773 | #endif | |
8774 | ||
b36200f5 DV |
8775 | if (sq_offset) |
8776 | *sq_offset = off; | |
8777 | ||
75b28aff HV |
8778 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8779 | if (sq_array_size == SIZE_MAX) | |
8780 | return SIZE_MAX; | |
8781 | ||
8782 | if (check_add_overflow(off, sq_array_size, &off)) | |
8783 | return SIZE_MAX; | |
8784 | ||
75b28aff HV |
8785 | return off; |
8786 | } | |
8787 | ||
41edf1a5 | 8788 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot) |
7f61a1e9 | 8789 | { |
41edf1a5 | 8790 | struct io_mapped_ubuf *imu = *slot; |
7f61a1e9 PB |
8791 | unsigned int i; |
8792 | ||
6224843d PB |
8793 | if (imu != ctx->dummy_ubuf) { |
8794 | for (i = 0; i < imu->nr_bvecs; i++) | |
8795 | unpin_user_page(imu->bvec[i].bv_page); | |
8796 | if (imu->acct_pages) | |
8797 | io_unaccount_mem(ctx, imu->acct_pages); | |
8798 | kvfree(imu); | |
8799 | } | |
41edf1a5 | 8800 | *slot = NULL; |
7f61a1e9 PB |
8801 | } |
8802 | ||
bd54b6fe | 8803 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
edafccee | 8804 | { |
634d00df PB |
8805 | io_buffer_unmap(ctx, &prsrc->buf); |
8806 | prsrc->buf = NULL; | |
bd54b6fe | 8807 | } |
edafccee | 8808 | |
bd54b6fe BM |
8809 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
8810 | { | |
8811 | unsigned int i; | |
edafccee | 8812 | |
7f61a1e9 PB |
8813 | for (i = 0; i < ctx->nr_user_bufs; i++) |
8814 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); | |
edafccee | 8815 | kfree(ctx->user_bufs); |
bb6659cc | 8816 | io_rsrc_data_free(ctx->buf_data); |
edafccee | 8817 | ctx->user_bufs = NULL; |
bd54b6fe | 8818 | ctx->buf_data = NULL; |
edafccee | 8819 | ctx->nr_user_bufs = 0; |
bd54b6fe BM |
8820 | } |
8821 | ||
0a96bbe4 | 8822 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee | 8823 | { |
bd54b6fe | 8824 | int ret; |
edafccee | 8825 | |
bd54b6fe | 8826 | if (!ctx->buf_data) |
edafccee JA |
8827 | return -ENXIO; |
8828 | ||
bd54b6fe BM |
8829 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
8830 | if (!ret) | |
8831 | __io_sqe_buffers_unregister(ctx); | |
8832 | return ret; | |
edafccee JA |
8833 | } |
8834 | ||
8835 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8836 | void __user *arg, unsigned index) | |
8837 | { | |
8838 | struct iovec __user *src; | |
8839 | ||
8840 | #ifdef CONFIG_COMPAT | |
8841 | if (ctx->compat) { | |
8842 | struct compat_iovec __user *ciovs; | |
8843 | struct compat_iovec ciov; | |
8844 | ||
8845 | ciovs = (struct compat_iovec __user *) arg; | |
8846 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8847 | return -EFAULT; | |
8848 | ||
d55e5f5b | 8849 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8850 | dst->iov_len = ciov.iov_len; |
8851 | return 0; | |
8852 | } | |
8853 | #endif | |
8854 | src = (struct iovec __user *) arg; | |
8855 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8856 | return -EFAULT; | |
8857 | return 0; | |
8858 | } | |
8859 | ||
de293938 JA |
8860 | /* |
8861 | * Not super efficient, but this is just a registration time. And we do cache | |
8862 | * the last compound head, so generally we'll only do a full search if we don't | |
8863 | * match that one. | |
8864 | * | |
8865 | * We check if the given compound head page has already been accounted, to | |
8866 | * avoid double accounting it. This allows us to account the full size of the | |
8867 | * page, not just the constituent pages of a huge page. | |
8868 | */ | |
8869 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8870 | int nr_pages, struct page *hpage) | |
8871 | { | |
8872 | int i, j; | |
8873 | ||
8874 | /* check current page array */ | |
8875 | for (i = 0; i < nr_pages; i++) { | |
8876 | if (!PageCompound(pages[i])) | |
8877 | continue; | |
8878 | if (compound_head(pages[i]) == hpage) | |
8879 | return true; | |
8880 | } | |
8881 | ||
8882 | /* check previously registered pages */ | |
8883 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
41edf1a5 | 8884 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
de293938 JA |
8885 | |
8886 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8887 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8888 | continue; | |
8889 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8890 | return true; | |
8891 | } | |
8892 | } | |
8893 | ||
8894 | return false; | |
8895 | } | |
8896 | ||
8897 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8898 | int nr_pages, struct io_mapped_ubuf *imu, | |
8899 | struct page **last_hpage) | |
8900 | { | |
8901 | int i, ret; | |
8902 | ||
216e5835 | 8903 | imu->acct_pages = 0; |
de293938 JA |
8904 | for (i = 0; i < nr_pages; i++) { |
8905 | if (!PageCompound(pages[i])) { | |
8906 | imu->acct_pages++; | |
8907 | } else { | |
8908 | struct page *hpage; | |
8909 | ||
8910 | hpage = compound_head(pages[i]); | |
8911 | if (hpage == *last_hpage) | |
8912 | continue; | |
8913 | *last_hpage = hpage; | |
8914 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8915 | continue; | |
8916 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8917 | } | |
8918 | } | |
8919 | ||
8920 | if (!imu->acct_pages) | |
8921 | return 0; | |
8922 | ||
26bfa89e | 8923 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
8924 | if (ret) |
8925 | imu->acct_pages = 0; | |
8926 | return ret; | |
8927 | } | |
8928 | ||
0a96bbe4 | 8929 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
41edf1a5 | 8930 | struct io_mapped_ubuf **pimu, |
0a96bbe4 | 8931 | struct page **last_hpage) |
edafccee | 8932 | { |
41edf1a5 | 8933 | struct io_mapped_ubuf *imu = NULL; |
edafccee JA |
8934 | struct vm_area_struct **vmas = NULL; |
8935 | struct page **pages = NULL; | |
0a96bbe4 BM |
8936 | unsigned long off, start, end, ubuf; |
8937 | size_t size; | |
8938 | int ret, pret, nr_pages, i; | |
8939 | ||
6224843d PB |
8940 | if (!iov->iov_base) { |
8941 | *pimu = ctx->dummy_ubuf; | |
8942 | return 0; | |
8943 | } | |
8944 | ||
0a96bbe4 BM |
8945 | ubuf = (unsigned long) iov->iov_base; |
8946 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8947 | start = ubuf >> PAGE_SHIFT; | |
8948 | nr_pages = end - start; | |
8949 | ||
41edf1a5 | 8950 | *pimu = NULL; |
0a96bbe4 BM |
8951 | ret = -ENOMEM; |
8952 | ||
8953 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8954 | if (!pages) | |
8955 | goto done; | |
8956 | ||
8957 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8958 | GFP_KERNEL); | |
8959 | if (!vmas) | |
8960 | goto done; | |
edafccee | 8961 | |
41edf1a5 | 8962 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
a2b4198c | 8963 | if (!imu) |
0a96bbe4 BM |
8964 | goto done; |
8965 | ||
8966 | ret = 0; | |
8967 | mmap_read_lock(current->mm); | |
8968 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8969 | pages, vmas); | |
8970 | if (pret == nr_pages) { | |
8971 | /* don't support file backed memory */ | |
8972 | for (i = 0; i < nr_pages; i++) { | |
8973 | struct vm_area_struct *vma = vmas[i]; | |
8974 | ||
40dad765 PB |
8975 | if (vma_is_shmem(vma)) |
8976 | continue; | |
0a96bbe4 BM |
8977 | if (vma->vm_file && |
8978 | !is_file_hugepages(vma->vm_file)) { | |
8979 | ret = -EOPNOTSUPP; | |
8980 | break; | |
8981 | } | |
8982 | } | |
8983 | } else { | |
8984 | ret = pret < 0 ? pret : -EFAULT; | |
8985 | } | |
8986 | mmap_read_unlock(current->mm); | |
8987 | if (ret) { | |
8988 | /* | |
8989 | * if we did partial map, or found file backed vmas, | |
8990 | * release any pages we did get | |
8991 | */ | |
8992 | if (pret > 0) | |
8993 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
8994 | goto done; |
8995 | } | |
8996 | ||
8997 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
8998 | if (ret) { | |
8999 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9000 | goto done; |
9001 | } | |
9002 | ||
9003 | off = ubuf & ~PAGE_MASK; | |
9004 | size = iov->iov_len; | |
9005 | for (i = 0; i < nr_pages; i++) { | |
9006 | size_t vec_len; | |
9007 | ||
9008 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
9009 | imu->bvec[i].bv_page = pages[i]; | |
9010 | imu->bvec[i].bv_len = vec_len; | |
9011 | imu->bvec[i].bv_offset = off; | |
9012 | off = 0; | |
9013 | size -= vec_len; | |
9014 | } | |
9015 | /* store original address for later verification */ | |
9016 | imu->ubuf = ubuf; | |
4751f53d | 9017 | imu->ubuf_end = ubuf + iov->iov_len; |
0a96bbe4 | 9018 | imu->nr_bvecs = nr_pages; |
41edf1a5 | 9019 | *pimu = imu; |
0a96bbe4 BM |
9020 | ret = 0; |
9021 | done: | |
41edf1a5 PB |
9022 | if (ret) |
9023 | kvfree(imu); | |
0a96bbe4 BM |
9024 | kvfree(pages); |
9025 | kvfree(vmas); | |
9026 | return ret; | |
9027 | } | |
9028 | ||
2b358604 | 9029 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 9030 | { |
87094465 PB |
9031 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
9032 | return ctx->user_bufs ? 0 : -ENOMEM; | |
2b358604 | 9033 | } |
edafccee | 9034 | |
2b358604 BM |
9035 | static int io_buffer_validate(struct iovec *iov) |
9036 | { | |
50e96989 PB |
9037 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
9038 | ||
2b358604 BM |
9039 | /* |
9040 | * Don't impose further limits on the size and buffer | |
9041 | * constraints here, we'll -EINVAL later when IO is | |
9042 | * submitted if they are wrong. | |
9043 | */ | |
6224843d PB |
9044 | if (!iov->iov_base) |
9045 | return iov->iov_len ? -EFAULT : 0; | |
9046 | if (!iov->iov_len) | |
2b358604 | 9047 | return -EFAULT; |
edafccee | 9048 | |
2b358604 BM |
9049 | /* arbitrary limit, but we need something */ |
9050 | if (iov->iov_len > SZ_1G) | |
9051 | return -EFAULT; | |
edafccee | 9052 | |
50e96989 PB |
9053 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
9054 | return -EOVERFLOW; | |
9055 | ||
2b358604 BM |
9056 | return 0; |
9057 | } | |
edafccee | 9058 | |
2b358604 | 9059 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
634d00df | 9060 | unsigned int nr_args, u64 __user *tags) |
2b358604 | 9061 | { |
bd54b6fe BM |
9062 | struct page *last_hpage = NULL; |
9063 | struct io_rsrc_data *data; | |
2b358604 BM |
9064 | int i, ret; |
9065 | struct iovec iov; | |
edafccee | 9066 | |
87094465 PB |
9067 | if (ctx->user_bufs) |
9068 | return -EBUSY; | |
489809e2 | 9069 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
87094465 | 9070 | return -EINVAL; |
bd54b6fe | 9071 | ret = io_rsrc_node_switch_start(ctx); |
2b358604 BM |
9072 | if (ret) |
9073 | return ret; | |
d878c816 PB |
9074 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
9075 | if (ret) | |
9076 | return ret; | |
bd54b6fe BM |
9077 | ret = io_buffers_map_alloc(ctx, nr_args); |
9078 | if (ret) { | |
bb6659cc | 9079 | io_rsrc_data_free(data); |
bd54b6fe BM |
9080 | return ret; |
9081 | } | |
edafccee | 9082 | |
87094465 | 9083 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
edafccee JA |
9084 | ret = io_copy_iov(ctx, &iov, arg, i); |
9085 | if (ret) | |
0a96bbe4 | 9086 | break; |
2b358604 BM |
9087 | ret = io_buffer_validate(&iov); |
9088 | if (ret) | |
0a96bbe4 | 9089 | break; |
2d091d62 | 9090 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
cf3770e7 CIK |
9091 | ret = -EINVAL; |
9092 | break; | |
9093 | } | |
edafccee | 9094 | |
41edf1a5 PB |
9095 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
9096 | &last_hpage); | |
0a96bbe4 BM |
9097 | if (ret) |
9098 | break; | |
edafccee | 9099 | } |
0a96bbe4 | 9100 | |
bd54b6fe | 9101 | WARN_ON_ONCE(ctx->buf_data); |
0a96bbe4 | 9102 | |
bd54b6fe BM |
9103 | ctx->buf_data = data; |
9104 | if (ret) | |
9105 | __io_sqe_buffers_unregister(ctx); | |
9106 | else | |
9107 | io_rsrc_node_switch(ctx, NULL); | |
edafccee JA |
9108 | return ret; |
9109 | } | |
9110 | ||
634d00df PB |
9111 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
9112 | struct io_uring_rsrc_update2 *up, | |
9113 | unsigned int nr_args) | |
9114 | { | |
9115 | u64 __user *tags = u64_to_user_ptr(up->tags); | |
9116 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); | |
634d00df PB |
9117 | struct page *last_hpage = NULL; |
9118 | bool needs_switch = false; | |
9119 | __u32 done; | |
9120 | int i, err; | |
9121 | ||
9122 | if (!ctx->buf_data) | |
9123 | return -ENXIO; | |
9124 | if (up->offset + nr_args > ctx->nr_user_bufs) | |
9125 | return -EINVAL; | |
9126 | ||
9127 | for (done = 0; done < nr_args; done++) { | |
0b8c0e7c PB |
9128 | struct io_mapped_ubuf *imu; |
9129 | int offset = up->offset + done; | |
634d00df PB |
9130 | u64 tag = 0; |
9131 | ||
9132 | err = io_copy_iov(ctx, &iov, iovs, done); | |
9133 | if (err) | |
9134 | break; | |
9135 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { | |
9136 | err = -EFAULT; | |
9137 | break; | |
9138 | } | |
0b8c0e7c PB |
9139 | err = io_buffer_validate(&iov); |
9140 | if (err) | |
9141 | break; | |
cf3770e7 CIK |
9142 | if (!iov.iov_base && tag) { |
9143 | err = -EINVAL; | |
9144 | break; | |
9145 | } | |
0b8c0e7c PB |
9146 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
9147 | if (err) | |
9148 | break; | |
634d00df | 9149 | |
0b8c0e7c | 9150 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
6224843d | 9151 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
0b8c0e7c PB |
9152 | err = io_queue_rsrc_removal(ctx->buf_data, offset, |
9153 | ctx->rsrc_node, ctx->user_bufs[i]); | |
9154 | if (unlikely(err)) { | |
9155 | io_buffer_unmap(ctx, &imu); | |
634d00df | 9156 | break; |
0b8c0e7c | 9157 | } |
634d00df PB |
9158 | ctx->user_bufs[i] = NULL; |
9159 | needs_switch = true; | |
9160 | } | |
9161 | ||
0b8c0e7c | 9162 | ctx->user_bufs[i] = imu; |
2d091d62 | 9163 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
634d00df PB |
9164 | } |
9165 | ||
9166 | if (needs_switch) | |
9167 | io_rsrc_node_switch(ctx, ctx->buf_data); | |
9168 | return done ? done : err; | |
9169 | } | |
9170 | ||
9b402849 JA |
9171 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
9172 | { | |
9173 | __s32 __user *fds = arg; | |
9174 | int fd; | |
9175 | ||
9176 | if (ctx->cq_ev_fd) | |
9177 | return -EBUSY; | |
9178 | ||
9179 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
9180 | return -EFAULT; | |
9181 | ||
9182 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
9183 | if (IS_ERR(ctx->cq_ev_fd)) { | |
9184 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
fe7e3257 | 9185 | |
9b402849 JA |
9186 | ctx->cq_ev_fd = NULL; |
9187 | return ret; | |
9188 | } | |
9189 | ||
9190 | return 0; | |
9191 | } | |
9192 | ||
9193 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
9194 | { | |
9195 | if (ctx->cq_ev_fd) { | |
9196 | eventfd_ctx_put(ctx->cq_ev_fd); | |
9197 | ctx->cq_ev_fd = NULL; | |
9198 | return 0; | |
9199 | } | |
9200 | ||
9201 | return -ENXIO; | |
9202 | } | |
9203 | ||
5a2e745d JA |
9204 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
9205 | { | |
9e15c3a0 JA |
9206 | struct io_buffer *buf; |
9207 | unsigned long index; | |
9208 | ||
8bab4c09 | 9209 | xa_for_each(&ctx->io_buffers, index, buf) { |
9e15c3a0 | 9210 | __io_remove_buffers(ctx, buf, index, -1U); |
8bab4c09 JA |
9211 | cond_resched(); |
9212 | } | |
5a2e745d JA |
9213 | } |
9214 | ||
7255834e | 9215 | static void io_req_cache_free(struct list_head *list) |
1b4c351f | 9216 | { |
68e68ee6 | 9217 | struct io_kiocb *req, *nxt; |
1b4c351f | 9218 | |
bb943b82 PB |
9219 | list_for_each_entry_safe(req, nxt, list, inflight_entry) { |
9220 | list_del(&req->inflight_entry); | |
1b4c351f JA |
9221 | kmem_cache_free(req_cachep, req); |
9222 | } | |
9223 | } | |
9224 | ||
4010fec4 | 9225 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9226 | { |
cd0ca2e0 | 9227 | struct io_submit_state *state = &ctx->submit_state; |
bf019da7 | 9228 | |
9a4fdbd8 JA |
9229 | mutex_lock(&ctx->uring_lock); |
9230 | ||
cd0ca2e0 PB |
9231 | if (state->free_reqs) { |
9232 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); | |
9233 | state->free_reqs = 0; | |
8e5c66c4 | 9234 | } |
9a4fdbd8 | 9235 | |
cd0ca2e0 PB |
9236 | io_flush_cached_locked_reqs(ctx, state); |
9237 | io_req_cache_free(&state->free_list); | |
9a4fdbd8 JA |
9238 | mutex_unlock(&ctx->uring_lock); |
9239 | } | |
9240 | ||
43597aac | 9241 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
2b188cc1 | 9242 | { |
43597aac | 9243 | if (data && !atomic_dec_and_test(&data->refs)) |
bd54b6fe | 9244 | wait_for_completion(&data->done); |
bd54b6fe | 9245 | } |
04fc6c80 | 9246 | |
2b188cc1 JA |
9247 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
9248 | { | |
37d1e2e3 | 9249 | io_sq_thread_finish(ctx); |
2aede0e4 | 9250 | |
37d1e2e3 | 9251 | if (ctx->mm_account) { |
2aede0e4 JA |
9252 | mmdrop(ctx->mm_account); |
9253 | ctx->mm_account = NULL; | |
30975825 | 9254 | } |
def596e9 | 9255 | |
43597aac PB |
9256 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
9257 | io_wait_rsrc_data(ctx->buf_data); | |
9258 | io_wait_rsrc_data(ctx->file_data); | |
9259 | ||
8bad28d8 | 9260 | mutex_lock(&ctx->uring_lock); |
43597aac | 9261 | if (ctx->buf_data) |
bd54b6fe | 9262 | __io_sqe_buffers_unregister(ctx); |
43597aac | 9263 | if (ctx->file_data) |
08480400 | 9264 | __io_sqe_files_unregister(ctx); |
c4ea060e PB |
9265 | if (ctx->rings) |
9266 | __io_cqring_overflow_flush(ctx, true); | |
8bad28d8 | 9267 | mutex_unlock(&ctx->uring_lock); |
9b402849 | 9268 | io_eventfd_unregister(ctx); |
5a2e745d | 9269 | io_destroy_buffers(ctx); |
07db298a PB |
9270 | if (ctx->sq_creds) |
9271 | put_cred(ctx->sq_creds); | |
def596e9 | 9272 | |
a7f0ed5a PB |
9273 | /* there are no registered resources left, nobody uses it */ |
9274 | if (ctx->rsrc_node) | |
9275 | io_rsrc_node_destroy(ctx->rsrc_node); | |
8dd03afe | 9276 | if (ctx->rsrc_backup_node) |
b895c9a6 | 9277 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
a7f0ed5a PB |
9278 | flush_delayed_work(&ctx->rsrc_put_work); |
9279 | ||
9280 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); | |
9281 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); | |
def596e9 | 9282 | |
2b188cc1 | 9283 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
9284 | if (ctx->ring_sock) { |
9285 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 9286 | sock_release(ctx->ring_sock); |
355e8d26 | 9287 | } |
2b188cc1 | 9288 | #endif |
ef9dd637 | 9289 | WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); |
2b188cc1 | 9290 | |
75b28aff | 9291 | io_mem_free(ctx->rings); |
2b188cc1 | 9292 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
9293 | |
9294 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 9295 | free_uid(ctx->user); |
4010fec4 | 9296 | io_req_caches_free(ctx); |
e941894e JA |
9297 | if (ctx->hash_map) |
9298 | io_wq_put_hash(ctx->hash_map); | |
78076bb6 | 9299 | kfree(ctx->cancel_hash); |
6224843d | 9300 | kfree(ctx->dummy_ubuf); |
2b188cc1 JA |
9301 | kfree(ctx); |
9302 | } | |
9303 | ||
9304 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
9305 | { | |
9306 | struct io_ring_ctx *ctx = file->private_data; | |
9307 | __poll_t mask = 0; | |
9308 | ||
311997b3 | 9309 | poll_wait(file, &ctx->poll_wait, wait); |
4f7067c3 SB |
9310 | /* |
9311 | * synchronizes with barrier from wq_has_sleeper call in | |
9312 | * io_commit_cqring | |
9313 | */ | |
2b188cc1 | 9314 | smp_rmb(); |
90554200 | 9315 | if (!io_sqring_full(ctx)) |
2b188cc1 | 9316 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
9317 | |
9318 | /* | |
9319 | * Don't flush cqring overflow list here, just do a simple check. | |
9320 | * Otherwise there could possible be ABBA deadlock: | |
9321 | * CPU0 CPU1 | |
9322 | * ---- ---- | |
9323 | * lock(&ctx->uring_lock); | |
9324 | * lock(&ep->mtx); | |
9325 | * lock(&ctx->uring_lock); | |
9326 | * lock(&ep->mtx); | |
9327 | * | |
9328 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
9329 | * pushs them to do the flush. | |
9330 | */ | |
5ed7a37d | 9331 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
2b188cc1 JA |
9332 | mask |= EPOLLIN | EPOLLRDNORM; |
9333 | ||
9334 | return mask; | |
9335 | } | |
9336 | ||
0bead8cd | 9337 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 9338 | { |
4379bf8b | 9339 | const struct cred *creds; |
071698e1 | 9340 | |
61cf9370 | 9341 | creds = xa_erase(&ctx->personalities, id); |
4379bf8b JA |
9342 | if (creds) { |
9343 | put_cred(creds); | |
0bead8cd | 9344 | return 0; |
1e6fa521 | 9345 | } |
0bead8cd YD |
9346 | |
9347 | return -EINVAL; | |
9348 | } | |
9349 | ||
d56d938b PB |
9350 | struct io_tctx_exit { |
9351 | struct callback_head task_work; | |
9352 | struct completion completion; | |
baf186c4 | 9353 | struct io_ring_ctx *ctx; |
d56d938b PB |
9354 | }; |
9355 | ||
9356 | static void io_tctx_exit_cb(struct callback_head *cb) | |
9357 | { | |
9358 | struct io_uring_task *tctx = current->io_uring; | |
9359 | struct io_tctx_exit *work; | |
9360 | ||
9361 | work = container_of(cb, struct io_tctx_exit, task_work); | |
9362 | /* | |
9363 | * When @in_idle, we're in cancellation and it's racy to remove the | |
9364 | * node. It'll be removed by the end of cancellation, just ignore it. | |
9365 | */ | |
9366 | if (!atomic_read(&tctx->in_idle)) | |
eef51daa | 9367 | io_uring_del_tctx_node((unsigned long)work->ctx); |
d56d938b PB |
9368 | complete(&work->completion); |
9369 | } | |
9370 | ||
28090c13 PB |
9371 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
9372 | { | |
9373 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
9374 | ||
9375 | return req->ctx == data; | |
9376 | } | |
9377 | ||
85faa7b8 JA |
9378 | static void io_ring_exit_work(struct work_struct *work) |
9379 | { | |
d56d938b | 9380 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 9381 | unsigned long timeout = jiffies + HZ * 60 * 5; |
58d3be2c | 9382 | unsigned long interval = HZ / 20; |
d56d938b PB |
9383 | struct io_tctx_exit exit; |
9384 | struct io_tctx_node *node; | |
9385 | int ret; | |
85faa7b8 | 9386 | |
56952e91 JA |
9387 | /* |
9388 | * If we're doing polled IO and end up having requests being | |
9389 | * submitted async (out-of-line), then completions can come in while | |
9390 | * we're waiting for refs to drop. We need to reap these manually, | |
9391 | * as nobody else will be looking for them. | |
9392 | */ | |
b2edc0a7 | 9393 | do { |
3dd0c97a | 9394 | io_uring_try_cancel_requests(ctx, NULL, true); |
28090c13 PB |
9395 | if (ctx->sq_data) { |
9396 | struct io_sq_data *sqd = ctx->sq_data; | |
9397 | struct task_struct *tsk; | |
9398 | ||
9399 | io_sq_thread_park(sqd); | |
9400 | tsk = sqd->thread; | |
9401 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) | |
9402 | io_wq_cancel_cb(tsk->io_uring->io_wq, | |
9403 | io_cancel_ctx_cb, ctx, true); | |
9404 | io_sq_thread_unpark(sqd); | |
9405 | } | |
b5bb3a24 | 9406 | |
58d3be2c PB |
9407 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
9408 | /* there is little hope left, don't run it too often */ | |
9409 | interval = HZ * 60; | |
9410 | } | |
9411 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); | |
d56d938b | 9412 | |
7f00651a PB |
9413 | init_completion(&exit.completion); |
9414 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
9415 | exit.ctx = ctx; | |
89b5066e PB |
9416 | /* |
9417 | * Some may use context even when all refs and requests have been put, | |
9418 | * and they are free to do so while still holding uring_lock or | |
5b0a6acc | 9419 | * completion_lock, see io_req_task_submit(). Apart from other work, |
89b5066e PB |
9420 | * this lock/unlock section also waits them to finish. |
9421 | */ | |
d56d938b PB |
9422 | mutex_lock(&ctx->uring_lock); |
9423 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
9424 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
9425 | ||
d56d938b PB |
9426 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
9427 | ctx_node); | |
7f00651a PB |
9428 | /* don't spin on a single task if cancellation failed */ |
9429 | list_rotate_left(&ctx->tctx_list); | |
d56d938b PB |
9430 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
9431 | if (WARN_ON_ONCE(ret)) | |
9432 | continue; | |
9433 | wake_up_process(node->task); | |
9434 | ||
9435 | mutex_unlock(&ctx->uring_lock); | |
9436 | wait_for_completion(&exit.completion); | |
d56d938b PB |
9437 | mutex_lock(&ctx->uring_lock); |
9438 | } | |
9439 | mutex_unlock(&ctx->uring_lock); | |
79ebeaee JA |
9440 | spin_lock(&ctx->completion_lock); |
9441 | spin_unlock(&ctx->completion_lock); | |
d56d938b | 9442 | |
85faa7b8 JA |
9443 | io_ring_ctx_free(ctx); |
9444 | } | |
9445 | ||
80c4cbdb PB |
9446 | /* Returns true if we found and killed one or more timeouts */ |
9447 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, | |
3dd0c97a | 9448 | bool cancel_all) |
80c4cbdb PB |
9449 | { |
9450 | struct io_kiocb *req, *tmp; | |
9451 | int canceled = 0; | |
9452 | ||
79ebeaee JA |
9453 | spin_lock(&ctx->completion_lock); |
9454 | spin_lock_irq(&ctx->timeout_lock); | |
80c4cbdb | 9455 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
3dd0c97a | 9456 | if (io_match_task(req, tsk, cancel_all)) { |
80c4cbdb PB |
9457 | io_kill_timeout(req, -ECANCELED); |
9458 | canceled++; | |
9459 | } | |
9460 | } | |
79ebeaee | 9461 | spin_unlock_irq(&ctx->timeout_lock); |
51520426 PB |
9462 | if (canceled != 0) |
9463 | io_commit_cqring(ctx); | |
79ebeaee | 9464 | spin_unlock(&ctx->completion_lock); |
80c4cbdb PB |
9465 | if (canceled != 0) |
9466 | io_cqring_ev_posted(ctx); | |
9467 | return canceled != 0; | |
9468 | } | |
9469 | ||
2b188cc1 JA |
9470 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
9471 | { | |
61cf9370 MWO |
9472 | unsigned long index; |
9473 | struct creds *creds; | |
9474 | ||
2b188cc1 JA |
9475 | mutex_lock(&ctx->uring_lock); |
9476 | percpu_ref_kill(&ctx->refs); | |
634578f8 | 9477 | if (ctx->rings) |
6c2450ae | 9478 | __io_cqring_overflow_flush(ctx, true); |
61cf9370 MWO |
9479 | xa_for_each(&ctx->personalities, index, creds) |
9480 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
9481 | mutex_unlock(&ctx->uring_lock); |
9482 | ||
3dd0c97a PB |
9483 | io_kill_timeouts(ctx, NULL, true); |
9484 | io_poll_remove_all(ctx, NULL, true); | |
561fb04a | 9485 | |
15dff286 | 9486 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 9487 | io_iopoll_try_reap_events(ctx); |
309fc03a | 9488 | |
85faa7b8 | 9489 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
9490 | /* |
9491 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
9492 | * if we're exiting a ton of rings at the same time. It just adds | |
9493 | * noise and overhead, there's no discernable change in runtime | |
9494 | * over using system_wq. | |
9495 | */ | |
9496 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
9497 | } |
9498 | ||
9499 | static int io_uring_release(struct inode *inode, struct file *file) | |
9500 | { | |
9501 | struct io_ring_ctx *ctx = file->private_data; | |
9502 | ||
9503 | file->private_data = NULL; | |
9504 | io_ring_ctx_wait_and_kill(ctx); | |
9505 | return 0; | |
9506 | } | |
9507 | ||
f6edbabb PB |
9508 | struct io_task_cancel { |
9509 | struct task_struct *task; | |
3dd0c97a | 9510 | bool all; |
f6edbabb | 9511 | }; |
f254ac04 | 9512 | |
f6edbabb | 9513 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 9514 | { |
9a472ef7 | 9515 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 9516 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
9517 | bool ret; |
9518 | ||
3dd0c97a | 9519 | if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
9520 | struct io_ring_ctx *ctx = req->ctx; |
9521 | ||
9522 | /* protect against races with linked timeouts */ | |
79ebeaee | 9523 | spin_lock(&ctx->completion_lock); |
3dd0c97a | 9524 | ret = io_match_task(req, cancel->task, cancel->all); |
79ebeaee | 9525 | spin_unlock(&ctx->completion_lock); |
9a472ef7 | 9526 | } else { |
3dd0c97a | 9527 | ret = io_match_task(req, cancel->task, cancel->all); |
9a472ef7 PB |
9528 | } |
9529 | return ret; | |
b711d4ea JA |
9530 | } |
9531 | ||
e1915f76 | 9532 | static bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
3dd0c97a | 9533 | struct task_struct *task, bool cancel_all) |
b7ddce3c | 9534 | { |
e1915f76 | 9535 | struct io_defer_entry *de; |
b7ddce3c PB |
9536 | LIST_HEAD(list); |
9537 | ||
79ebeaee | 9538 | spin_lock(&ctx->completion_lock); |
b7ddce3c | 9539 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
3dd0c97a | 9540 | if (io_match_task(de->req, task, cancel_all)) { |
b7ddce3c PB |
9541 | list_cut_position(&list, &ctx->defer_list, &de->list); |
9542 | break; | |
9543 | } | |
9544 | } | |
79ebeaee | 9545 | spin_unlock(&ctx->completion_lock); |
e1915f76 PB |
9546 | if (list_empty(&list)) |
9547 | return false; | |
b7ddce3c PB |
9548 | |
9549 | while (!list_empty(&list)) { | |
9550 | de = list_first_entry(&list, struct io_defer_entry, list); | |
9551 | list_del_init(&de->list); | |
f41db273 | 9552 | io_req_complete_failed(de->req, -ECANCELED); |
b7ddce3c PB |
9553 | kfree(de); |
9554 | } | |
e1915f76 | 9555 | return true; |
b7ddce3c PB |
9556 | } |
9557 | ||
1b00764f PB |
9558 | static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
9559 | { | |
9560 | struct io_tctx_node *node; | |
9561 | enum io_wq_cancel cret; | |
9562 | bool ret = false; | |
9563 | ||
9564 | mutex_lock(&ctx->uring_lock); | |
9565 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
9566 | struct io_uring_task *tctx = node->task->io_uring; | |
9567 | ||
9568 | /* | |
9569 | * io_wq will stay alive while we hold uring_lock, because it's | |
9570 | * killed after ctx nodes, which requires to take the lock. | |
9571 | */ | |
9572 | if (!tctx || !tctx->io_wq) | |
9573 | continue; | |
9574 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
9575 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9576 | } | |
9577 | mutex_unlock(&ctx->uring_lock); | |
9578 | ||
9579 | return ret; | |
9580 | } | |
9581 | ||
9936c7c2 PB |
9582 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
9583 | struct task_struct *task, | |
3dd0c97a | 9584 | bool cancel_all) |
9936c7c2 | 9585 | { |
3dd0c97a | 9586 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
1b00764f | 9587 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 PB |
9588 | |
9589 | while (1) { | |
9590 | enum io_wq_cancel cret; | |
9591 | bool ret = false; | |
9592 | ||
1b00764f PB |
9593 | if (!task) { |
9594 | ret |= io_uring_try_cancel_iowq(ctx); | |
9595 | } else if (tctx && tctx->io_wq) { | |
9596 | /* | |
9597 | * Cancels requests of all rings, not only @ctx, but | |
9598 | * it's fine as the task is in exit/exec. | |
9599 | */ | |
5aa75ed5 | 9600 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
9601 | &cancel, true); |
9602 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9603 | } | |
9604 | ||
9605 | /* SQPOLL thread does its own polling */ | |
3dd0c97a | 9606 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
d052d1d6 | 9607 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
9936c7c2 PB |
9608 | while (!list_empty_careful(&ctx->iopoll_list)) { |
9609 | io_iopoll_try_reap_events(ctx); | |
9610 | ret = true; | |
9611 | } | |
9612 | } | |
9613 | ||
3dd0c97a PB |
9614 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
9615 | ret |= io_poll_remove_all(ctx, task, cancel_all); | |
9616 | ret |= io_kill_timeouts(ctx, task, cancel_all); | |
e5dc480d PB |
9617 | if (task) |
9618 | ret |= io_run_task_work(); | |
9936c7c2 PB |
9619 | if (!ret) |
9620 | break; | |
9621 | cond_resched(); | |
9622 | } | |
9623 | } | |
9624 | ||
eef51daa | 9625 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
0f212204 | 9626 | { |
236434c3 | 9627 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 9628 | struct io_tctx_node *node; |
a528b04e | 9629 | int ret; |
236434c3 MWO |
9630 | |
9631 | if (unlikely(!tctx)) { | |
5aa75ed5 | 9632 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
9633 | if (unlikely(ret)) |
9634 | return ret; | |
236434c3 | 9635 | tctx = current->io_uring; |
0f212204 | 9636 | } |
cf27f3b1 PB |
9637 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
9638 | node = kmalloc(sizeof(*node), GFP_KERNEL); | |
9639 | if (!node) | |
9640 | return -ENOMEM; | |
9641 | node->ctx = ctx; | |
9642 | node->task = current; | |
13bf43f5 | 9643 | |
cf27f3b1 PB |
9644 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
9645 | node, GFP_KERNEL)); | |
9646 | if (ret) { | |
9647 | kfree(node); | |
9648 | return ret; | |
0f212204 | 9649 | } |
cf27f3b1 PB |
9650 | |
9651 | mutex_lock(&ctx->uring_lock); | |
9652 | list_add(&node->ctx_node, &ctx->tctx_list); | |
9653 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 9654 | } |
cf27f3b1 | 9655 | tctx->last = ctx; |
0f212204 JA |
9656 | return 0; |
9657 | } | |
9658 | ||
cf27f3b1 PB |
9659 | /* |
9660 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
9661 | */ | |
eef51daa | 9662 | static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
cf27f3b1 PB |
9663 | { |
9664 | struct io_uring_task *tctx = current->io_uring; | |
9665 | ||
9666 | if (likely(tctx && tctx->last == ctx)) | |
9667 | return 0; | |
eef51daa | 9668 | return __io_uring_add_tctx_node(ctx); |
cf27f3b1 PB |
9669 | } |
9670 | ||
0f212204 JA |
9671 | /* |
9672 | * Remove this io_uring_file -> task mapping. | |
9673 | */ | |
eef51daa | 9674 | static void io_uring_del_tctx_node(unsigned long index) |
0f212204 JA |
9675 | { |
9676 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 9677 | struct io_tctx_node *node; |
2941267b | 9678 | |
eebd2e37 PB |
9679 | if (!tctx) |
9680 | return; | |
13bf43f5 PB |
9681 | node = xa_erase(&tctx->xa, index); |
9682 | if (!node) | |
2941267b | 9683 | return; |
0f212204 | 9684 | |
13bf43f5 PB |
9685 | WARN_ON_ONCE(current != node->task); |
9686 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
9687 | ||
9688 | mutex_lock(&node->ctx->uring_lock); | |
9689 | list_del(&node->ctx_node); | |
9690 | mutex_unlock(&node->ctx->uring_lock); | |
9691 | ||
baf186c4 | 9692 | if (tctx->last == node->ctx) |
0f212204 | 9693 | tctx->last = NULL; |
13bf43f5 | 9694 | kfree(node); |
0f212204 JA |
9695 | } |
9696 | ||
8452d4a6 | 9697 | static void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 9698 | { |
ba5ef6dc | 9699 | struct io_wq *wq = tctx->io_wq; |
13bf43f5 | 9700 | struct io_tctx_node *node; |
de7f1d9e PB |
9701 | unsigned long index; |
9702 | ||
8bab4c09 | 9703 | xa_for_each(&tctx->xa, index, node) { |
eef51daa | 9704 | io_uring_del_tctx_node(index); |
8bab4c09 JA |
9705 | cond_resched(); |
9706 | } | |
b16ef427 ME |
9707 | if (wq) { |
9708 | /* | |
9709 | * Must be after io_uring_del_task_file() (removes nodes under | |
9710 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). | |
9711 | */ | |
ba5ef6dc | 9712 | io_wq_put_and_exit(wq); |
dadebc35 | 9713 | tctx->io_wq = NULL; |
b16ef427 | 9714 | } |
de7f1d9e PB |
9715 | } |
9716 | ||
3f48cf18 | 9717 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
521d6a73 | 9718 | { |
3f48cf18 PB |
9719 | if (tracked) |
9720 | return atomic_read(&tctx->inflight_tracked); | |
521d6a73 PB |
9721 | return percpu_counter_sum(&tctx->inflight); |
9722 | } | |
9723 | ||
09899b19 PB |
9724 | static void io_uring_drop_tctx_refs(struct task_struct *task) |
9725 | { | |
9726 | struct io_uring_task *tctx = task->io_uring; | |
9727 | unsigned int refs = tctx->cached_refs; | |
9728 | ||
e9dbe221 PB |
9729 | if (refs) { |
9730 | tctx->cached_refs = 0; | |
9731 | percpu_counter_sub(&tctx->inflight, refs); | |
9732 | put_task_struct_many(task, refs); | |
9733 | } | |
09899b19 PB |
9734 | } |
9735 | ||
78cc687b PB |
9736 | /* |
9737 | * Find any io_uring ctx that this task has registered or done IO on, and cancel | |
9738 | * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation. | |
9739 | */ | |
9740 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd) | |
0e9ddb39 | 9741 | { |
521d6a73 | 9742 | struct io_uring_task *tctx = current->io_uring; |
734551df | 9743 | struct io_ring_ctx *ctx; |
0e9ddb39 PB |
9744 | s64 inflight; |
9745 | DEFINE_WAIT(wait); | |
fdaf083c | 9746 | |
78cc687b PB |
9747 | WARN_ON_ONCE(sqd && sqd->thread != current); |
9748 | ||
6d042ffb PO |
9749 | if (!current->io_uring) |
9750 | return; | |
17a91051 PB |
9751 | if (tctx->io_wq) |
9752 | io_wq_exit_start(tctx->io_wq); | |
9753 | ||
0e9ddb39 PB |
9754 | atomic_inc(&tctx->in_idle); |
9755 | do { | |
e9dbe221 | 9756 | io_uring_drop_tctx_refs(current); |
0e9ddb39 | 9757 | /* read completions before cancelations */ |
78cc687b | 9758 | inflight = tctx_inflight(tctx, !cancel_all); |
0e9ddb39 PB |
9759 | if (!inflight) |
9760 | break; | |
fdaf083c | 9761 | |
78cc687b PB |
9762 | if (!sqd) { |
9763 | struct io_tctx_node *node; | |
9764 | unsigned long index; | |
0f212204 | 9765 | |
78cc687b PB |
9766 | xa_for_each(&tctx->xa, index, node) { |
9767 | /* sqpoll task will cancel all its requests */ | |
9768 | if (node->ctx->sq_data) | |
9769 | continue; | |
9770 | io_uring_try_cancel_requests(node->ctx, current, | |
9771 | cancel_all); | |
9772 | } | |
9773 | } else { | |
9774 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
9775 | io_uring_try_cancel_requests(ctx, current, | |
9776 | cancel_all); | |
9777 | } | |
17a91051 | 9778 | |
0f212204 | 9779 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
e9dbe221 | 9780 | io_uring_drop_tctx_refs(current); |
0f212204 | 9781 | /* |
a1bb3cd5 PB |
9782 | * If we've seen completions, retry without waiting. This |
9783 | * avoids a race where a completion comes in before we did | |
9784 | * prepare_to_wait(). | |
0f212204 | 9785 | */ |
3dd0c97a | 9786 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
a1bb3cd5 | 9787 | schedule(); |
f57555ed | 9788 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 9789 | } while (1); |
fdaf083c | 9790 | atomic_dec(&tctx->in_idle); |
de7f1d9e | 9791 | |
8452d4a6 | 9792 | io_uring_clean_tctx(tctx); |
3dd0c97a | 9793 | if (cancel_all) { |
3f48cf18 PB |
9794 | /* for exec all current's requests should be gone, kill tctx */ |
9795 | __io_uring_free(current); | |
9796 | } | |
44e728b8 PB |
9797 | } |
9798 | ||
f552a27a | 9799 | void __io_uring_cancel(bool cancel_all) |
78cc687b | 9800 | { |
f552a27a | 9801 | io_uring_cancel_generic(cancel_all, NULL); |
78cc687b PB |
9802 | } |
9803 | ||
6c5c240e RP |
9804 | static void *io_uring_validate_mmap_request(struct file *file, |
9805 | loff_t pgoff, size_t sz) | |
2b188cc1 | 9806 | { |
2b188cc1 | 9807 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 9808 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
9809 | struct page *page; |
9810 | void *ptr; | |
9811 | ||
9812 | switch (offset) { | |
9813 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
9814 | case IORING_OFF_CQ_RING: |
9815 | ptr = ctx->rings; | |
2b188cc1 JA |
9816 | break; |
9817 | case IORING_OFF_SQES: | |
9818 | ptr = ctx->sq_sqes; | |
9819 | break; | |
2b188cc1 | 9820 | default: |
6c5c240e | 9821 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
9822 | } |
9823 | ||
9824 | page = virt_to_head_page(ptr); | |
a50b854e | 9825 | if (sz > page_size(page)) |
6c5c240e RP |
9826 | return ERR_PTR(-EINVAL); |
9827 | ||
9828 | return ptr; | |
9829 | } | |
9830 | ||
9831 | #ifdef CONFIG_MMU | |
9832 | ||
9833 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9834 | { | |
9835 | size_t sz = vma->vm_end - vma->vm_start; | |
9836 | unsigned long pfn; | |
9837 | void *ptr; | |
9838 | ||
9839 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
9840 | if (IS_ERR(ptr)) | |
9841 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9842 | |
9843 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9844 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9845 | } | |
9846 | ||
6c5c240e RP |
9847 | #else /* !CONFIG_MMU */ |
9848 | ||
9849 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9850 | { | |
9851 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9852 | } | |
9853 | ||
9854 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9855 | { | |
9856 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9857 | } | |
9858 | ||
9859 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9860 | unsigned long addr, unsigned long len, | |
9861 | unsigned long pgoff, unsigned long flags) | |
9862 | { | |
9863 | void *ptr; | |
9864 | ||
9865 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9866 | if (IS_ERR(ptr)) | |
9867 | return PTR_ERR(ptr); | |
9868 | ||
9869 | return (unsigned long) ptr; | |
9870 | } | |
9871 | ||
9872 | #endif /* !CONFIG_MMU */ | |
9873 | ||
d9d05217 | 9874 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 JA |
9875 | { |
9876 | DEFINE_WAIT(wait); | |
9877 | ||
9878 | do { | |
9879 | if (!io_sqring_full(ctx)) | |
9880 | break; | |
90554200 JA |
9881 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
9882 | ||
9883 | if (!io_sqring_full(ctx)) | |
9884 | break; | |
90554200 JA |
9885 | schedule(); |
9886 | } while (!signal_pending(current)); | |
9887 | ||
9888 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
5199328a | 9889 | return 0; |
90554200 JA |
9890 | } |
9891 | ||
c73ebb68 HX |
9892 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9893 | struct __kernel_timespec __user **ts, | |
9894 | const sigset_t __user **sig) | |
9895 | { | |
9896 | struct io_uring_getevents_arg arg; | |
9897 | ||
9898 | /* | |
9899 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9900 | * is just a pointer to the sigset_t. | |
9901 | */ | |
9902 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9903 | *sig = (const sigset_t __user *) argp; | |
9904 | *ts = NULL; | |
9905 | return 0; | |
9906 | } | |
9907 | ||
9908 | /* | |
9909 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9910 | * timespec and sigset_t pointers if good. | |
9911 | */ | |
9912 | if (*argsz != sizeof(arg)) | |
9913 | return -EINVAL; | |
9914 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9915 | return -EFAULT; | |
9916 | *sig = u64_to_user_ptr(arg.sigmask); | |
9917 | *argsz = arg.sigmask_sz; | |
9918 | *ts = u64_to_user_ptr(arg.ts); | |
9919 | return 0; | |
9920 | } | |
9921 | ||
2b188cc1 | 9922 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9923 | u32, min_complete, u32, flags, const void __user *, argp, |
9924 | size_t, argsz) | |
2b188cc1 JA |
9925 | { |
9926 | struct io_ring_ctx *ctx; | |
2b188cc1 JA |
9927 | int submitted = 0; |
9928 | struct fd f; | |
33f993da | 9929 | long ret; |
2b188cc1 | 9930 | |
4c6e277c | 9931 | io_run_task_work(); |
b41e9852 | 9932 | |
33f993da PB |
9933 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
9934 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) | |
2b188cc1 JA |
9935 | return -EINVAL; |
9936 | ||
9937 | f = fdget(fd); | |
33f993da | 9938 | if (unlikely(!f.file)) |
2b188cc1 JA |
9939 | return -EBADF; |
9940 | ||
9941 | ret = -EOPNOTSUPP; | |
33f993da | 9942 | if (unlikely(f.file->f_op != &io_uring_fops)) |
2b188cc1 JA |
9943 | goto out_fput; |
9944 | ||
9945 | ret = -ENXIO; | |
9946 | ctx = f.file->private_data; | |
33f993da | 9947 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
2b188cc1 JA |
9948 | goto out_fput; |
9949 | ||
7e84e1c7 | 9950 | ret = -EBADFD; |
33f993da | 9951 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
9952 | goto out; |
9953 | ||
6c271ce2 JA |
9954 | /* |
9955 | * For SQ polling, the thread will do all submissions and completions. | |
9956 | * Just return the requested submit count, and wake the thread if | |
9957 | * we were asked to. | |
9958 | */ | |
b2a9eada | 9959 | ret = 0; |
6c271ce2 | 9960 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
90f67366 | 9961 | io_cqring_overflow_flush(ctx); |
89448c47 | 9962 | |
21f96522 JA |
9963 | if (unlikely(ctx->sq_data->thread == NULL)) { |
9964 | ret = -EOWNERDEAD; | |
04147488 | 9965 | goto out; |
21f96522 | 9966 | } |
6c271ce2 | 9967 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9968 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9969 | if (flags & IORING_ENTER_SQ_WAIT) { |
9970 | ret = io_sqpoll_wait_sq(ctx); | |
9971 | if (ret) | |
9972 | goto out; | |
9973 | } | |
6c271ce2 | 9974 | submitted = to_submit; |
b2a9eada | 9975 | } else if (to_submit) { |
eef51daa | 9976 | ret = io_uring_add_tctx_node(ctx); |
0f212204 JA |
9977 | if (unlikely(ret)) |
9978 | goto out; | |
2b188cc1 | 9979 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9980 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9981 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9982 | |
9983 | if (submitted != to_submit) | |
9984 | goto out; | |
2b188cc1 JA |
9985 | } |
9986 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9987 | const sigset_t __user *sig; |
9988 | struct __kernel_timespec __user *ts; | |
9989 | ||
9990 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9991 | if (unlikely(ret)) | |
9992 | goto out; | |
9993 | ||
2b188cc1 JA |
9994 | min_complete = min(min_complete, ctx->cq_entries); |
9995 | ||
32b2244a XW |
9996 | /* |
9997 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9998 | * space applications don't need to do io completion events | |
9999 | * polling again, they can rely on io_sq_thread to do polling | |
10000 | * work, which can reduce cpu usage and uring_lock contention. | |
10001 | */ | |
10002 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
10003 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 10004 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 10005 | } else { |
c73ebb68 | 10006 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 10007 | } |
2b188cc1 JA |
10008 | } |
10009 | ||
7c504e65 | 10010 | out: |
6805b32e | 10011 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
10012 | out_fput: |
10013 | fdput(f); | |
10014 | return submitted ? submitted : ret; | |
10015 | } | |
10016 | ||
bebdb65e | 10017 | #ifdef CONFIG_PROC_FS |
61cf9370 MWO |
10018 | static int io_uring_show_cred(struct seq_file *m, unsigned int id, |
10019 | const struct cred *cred) | |
87ce955b | 10020 | { |
87ce955b JA |
10021 | struct user_namespace *uns = seq_user_ns(m); |
10022 | struct group_info *gi; | |
10023 | kernel_cap_t cap; | |
10024 | unsigned __capi; | |
10025 | int g; | |
10026 | ||
10027 | seq_printf(m, "%5d\n", id); | |
10028 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
10029 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
10030 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
10031 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
10032 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
10033 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
10034 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
10035 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
10036 | seq_puts(m, "\n\tGroups:\t"); | |
10037 | gi = cred->group_info; | |
10038 | for (g = 0; g < gi->ngroups; g++) { | |
10039 | seq_put_decimal_ull(m, g ? " " : "", | |
10040 | from_kgid_munged(uns, gi->gid[g])); | |
10041 | } | |
10042 | seq_puts(m, "\n\tCapEff:\t"); | |
10043 | cap = cred->cap_effective; | |
10044 | CAP_FOR_EACH_U32(__capi) | |
10045 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
10046 | seq_putc(m, '\n'); | |
10047 | return 0; | |
10048 | } | |
10049 | ||
10050 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
10051 | { | |
dbbe9c64 | 10052 | struct io_sq_data *sq = NULL; |
fad8e0de | 10053 | bool has_lock; |
87ce955b JA |
10054 | int i; |
10055 | ||
fad8e0de JA |
10056 | /* |
10057 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
10058 | * since fdinfo case grabs it in the opposite direction of normal use | |
10059 | * cases. If we fail to get the lock, we just don't iterate any | |
10060 | * structures that could be going away outside the io_uring mutex. | |
10061 | */ | |
10062 | has_lock = mutex_trylock(&ctx->uring_lock); | |
10063 | ||
5f3f26f9 | 10064 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 10065 | sq = ctx->sq_data; |
5f3f26f9 JA |
10066 | if (!sq->thread) |
10067 | sq = NULL; | |
10068 | } | |
dbbe9c64 JQ |
10069 | |
10070 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
10071 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 10072 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 10073 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
7b29f92d | 10074 | struct file *f = io_file_from_index(ctx, i); |
87ce955b | 10075 | |
87ce955b JA |
10076 | if (f) |
10077 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
10078 | else | |
10079 | seq_printf(m, "%5u: <none>\n", i); | |
10080 | } | |
10081 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 10082 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
41edf1a5 | 10083 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
4751f53d | 10084 | unsigned int len = buf->ubuf_end - buf->ubuf; |
87ce955b | 10085 | |
4751f53d | 10086 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
87ce955b | 10087 | } |
61cf9370 MWO |
10088 | if (has_lock && !xa_empty(&ctx->personalities)) { |
10089 | unsigned long index; | |
10090 | const struct cred *cred; | |
10091 | ||
87ce955b | 10092 | seq_printf(m, "Personalities:\n"); |
61cf9370 MWO |
10093 | xa_for_each(&ctx->personalities, index, cred) |
10094 | io_uring_show_cred(m, index, cred); | |
87ce955b | 10095 | } |
d7718a9d | 10096 | seq_printf(m, "PollList:\n"); |
79ebeaee | 10097 | spin_lock(&ctx->completion_lock); |
d7718a9d JA |
10098 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
10099 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
10100 | struct io_kiocb *req; | |
10101 | ||
10102 | hlist_for_each_entry(req, list, hash_node) | |
10103 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
10104 | req->task->task_works != NULL); | |
10105 | } | |
79ebeaee | 10106 | spin_unlock(&ctx->completion_lock); |
fad8e0de JA |
10107 | if (has_lock) |
10108 | mutex_unlock(&ctx->uring_lock); | |
87ce955b JA |
10109 | } |
10110 | ||
10111 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
10112 | { | |
10113 | struct io_ring_ctx *ctx = f->private_data; | |
10114 | ||
10115 | if (percpu_ref_tryget(&ctx->refs)) { | |
10116 | __io_uring_show_fdinfo(ctx, m); | |
10117 | percpu_ref_put(&ctx->refs); | |
10118 | } | |
10119 | } | |
bebdb65e | 10120 | #endif |
87ce955b | 10121 | |
2b188cc1 JA |
10122 | static const struct file_operations io_uring_fops = { |
10123 | .release = io_uring_release, | |
10124 | .mmap = io_uring_mmap, | |
6c5c240e RP |
10125 | #ifndef CONFIG_MMU |
10126 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
10127 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
10128 | #endif | |
2b188cc1 | 10129 | .poll = io_uring_poll, |
bebdb65e | 10130 | #ifdef CONFIG_PROC_FS |
87ce955b | 10131 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 10132 | #endif |
2b188cc1 JA |
10133 | }; |
10134 | ||
10135 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
10136 | struct io_uring_params *p) | |
10137 | { | |
75b28aff HV |
10138 | struct io_rings *rings; |
10139 | size_t size, sq_array_offset; | |
2b188cc1 | 10140 | |
bd740481 JA |
10141 | /* make sure these are sane, as we already accounted them */ |
10142 | ctx->sq_entries = p->sq_entries; | |
10143 | ctx->cq_entries = p->cq_entries; | |
10144 | ||
75b28aff HV |
10145 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
10146 | if (size == SIZE_MAX) | |
10147 | return -EOVERFLOW; | |
10148 | ||
10149 | rings = io_mem_alloc(size); | |
10150 | if (!rings) | |
2b188cc1 JA |
10151 | return -ENOMEM; |
10152 | ||
75b28aff HV |
10153 | ctx->rings = rings; |
10154 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
10155 | rings->sq_ring_mask = p->sq_entries - 1; | |
10156 | rings->cq_ring_mask = p->cq_entries - 1; | |
10157 | rings->sq_ring_entries = p->sq_entries; | |
10158 | rings->cq_ring_entries = p->cq_entries; | |
2b188cc1 JA |
10159 | |
10160 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
10161 | if (size == SIZE_MAX) { |
10162 | io_mem_free(ctx->rings); | |
10163 | ctx->rings = NULL; | |
2b188cc1 | 10164 | return -EOVERFLOW; |
eb065d30 | 10165 | } |
2b188cc1 JA |
10166 | |
10167 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
10168 | if (!ctx->sq_sqes) { |
10169 | io_mem_free(ctx->rings); | |
10170 | ctx->rings = NULL; | |
2b188cc1 | 10171 | return -ENOMEM; |
eb065d30 | 10172 | } |
2b188cc1 | 10173 | |
2b188cc1 JA |
10174 | return 0; |
10175 | } | |
10176 | ||
9faadcc8 PB |
10177 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
10178 | { | |
10179 | int ret, fd; | |
10180 | ||
10181 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
10182 | if (fd < 0) | |
10183 | return fd; | |
10184 | ||
eef51daa | 10185 | ret = io_uring_add_tctx_node(ctx); |
9faadcc8 PB |
10186 | if (ret) { |
10187 | put_unused_fd(fd); | |
10188 | return ret; | |
10189 | } | |
10190 | fd_install(fd, file); | |
10191 | return fd; | |
10192 | } | |
10193 | ||
2b188cc1 JA |
10194 | /* |
10195 | * Allocate an anonymous fd, this is what constitutes the application | |
10196 | * visible backing of an io_uring instance. The application mmaps this | |
10197 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
10198 | * we have to tie this fd to a socket for file garbage collection purposes. | |
10199 | */ | |
9faadcc8 | 10200 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
10201 | { |
10202 | struct file *file; | |
9faadcc8 | 10203 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
10204 | int ret; |
10205 | ||
2b188cc1 JA |
10206 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
10207 | &ctx->ring_sock); | |
10208 | if (ret) | |
9faadcc8 | 10209 | return ERR_PTR(ret); |
2b188cc1 JA |
10210 | #endif |
10211 | ||
2b188cc1 JA |
10212 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
10213 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 10214 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
10215 | if (IS_ERR(file)) { |
10216 | sock_release(ctx->ring_sock); | |
10217 | ctx->ring_sock = NULL; | |
10218 | } else { | |
10219 | ctx->ring_sock->file = file; | |
0f212204 | 10220 | } |
2b188cc1 | 10221 | #endif |
9faadcc8 | 10222 | return file; |
2b188cc1 JA |
10223 | } |
10224 | ||
7f13657d XW |
10225 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
10226 | struct io_uring_params __user *params) | |
2b188cc1 | 10227 | { |
2b188cc1 | 10228 | struct io_ring_ctx *ctx; |
9faadcc8 | 10229 | struct file *file; |
2b188cc1 JA |
10230 | int ret; |
10231 | ||
8110c1a6 | 10232 | if (!entries) |
2b188cc1 | 10233 | return -EINVAL; |
8110c1a6 JA |
10234 | if (entries > IORING_MAX_ENTRIES) { |
10235 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
10236 | return -EINVAL; | |
10237 | entries = IORING_MAX_ENTRIES; | |
10238 | } | |
2b188cc1 JA |
10239 | |
10240 | /* | |
10241 | * Use twice as many entries for the CQ ring. It's possible for the | |
10242 | * application to drive a higher depth than the size of the SQ ring, | |
10243 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
10244 | * some flexibility in overcommitting a bit. If the application has |
10245 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
10246 | * of CQ ring entries manually. | |
2b188cc1 JA |
10247 | */ |
10248 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
10249 | if (p->flags & IORING_SETUP_CQSIZE) { |
10250 | /* | |
10251 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
10252 | * to a power-of-two, if it isn't already. We do NOT impose | |
10253 | * any cq vs sq ring sizing. | |
10254 | */ | |
eb2667b3 | 10255 | if (!p->cq_entries) |
33a107f0 | 10256 | return -EINVAL; |
8110c1a6 JA |
10257 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
10258 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
10259 | return -EINVAL; | |
10260 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
10261 | } | |
eb2667b3 JQ |
10262 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
10263 | if (p->cq_entries < p->sq_entries) | |
10264 | return -EINVAL; | |
33a107f0 JA |
10265 | } else { |
10266 | p->cq_entries = 2 * p->sq_entries; | |
10267 | } | |
2b188cc1 | 10268 | |
2b188cc1 | 10269 | ctx = io_ring_ctx_alloc(p); |
62e398be | 10270 | if (!ctx) |
2b188cc1 | 10271 | return -ENOMEM; |
2b188cc1 | 10272 | ctx->compat = in_compat_syscall(); |
62e398be JA |
10273 | if (!capable(CAP_IPC_LOCK)) |
10274 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
10275 | |
10276 | /* | |
10277 | * This is just grabbed for accounting purposes. When a process exits, | |
10278 | * the mm is exited and dropped before the files, hence we need to hang | |
10279 | * on to this mm purely for the purposes of being able to unaccount | |
10280 | * memory (locked/pinned vm). It's not used for anything else. | |
10281 | */ | |
6b7898eb | 10282 | mmgrab(current->mm); |
2aede0e4 | 10283 | ctx->mm_account = current->mm; |
6b7898eb | 10284 | |
2b188cc1 JA |
10285 | ret = io_allocate_scq_urings(ctx, p); |
10286 | if (ret) | |
10287 | goto err; | |
10288 | ||
7e84e1c7 | 10289 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
10290 | if (ret) |
10291 | goto err; | |
eae071c9 | 10292 | /* always set a rsrc node */ |
47b228ce PB |
10293 | ret = io_rsrc_node_switch_start(ctx); |
10294 | if (ret) | |
10295 | goto err; | |
eae071c9 | 10296 | io_rsrc_node_switch(ctx, NULL); |
2b188cc1 | 10297 | |
2b188cc1 | 10298 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
10299 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
10300 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
10301 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
10302 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
10303 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
10304 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
10305 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
10306 | |
10307 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
10308 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
10309 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
10310 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
10311 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
10312 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
10313 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 10314 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 10315 | |
7f13657d XW |
10316 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
10317 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 10318 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 10319 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9690557e PB |
10320 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
10321 | IORING_FEAT_RSRC_TAGS; | |
7f13657d XW |
10322 | |
10323 | if (copy_to_user(params, p, sizeof(*p))) { | |
10324 | ret = -EFAULT; | |
10325 | goto err; | |
10326 | } | |
d1719f70 | 10327 | |
9faadcc8 PB |
10328 | file = io_uring_get_file(ctx); |
10329 | if (IS_ERR(file)) { | |
10330 | ret = PTR_ERR(file); | |
10331 | goto err; | |
10332 | } | |
10333 | ||
044c1ab3 JA |
10334 | /* |
10335 | * Install ring fd as the very last thing, so we don't risk someone | |
10336 | * having closed it before we finish setup | |
10337 | */ | |
9faadcc8 PB |
10338 | ret = io_uring_install_fd(ctx, file); |
10339 | if (ret < 0) { | |
10340 | /* fput will clean it up */ | |
10341 | fput(file); | |
10342 | return ret; | |
10343 | } | |
044c1ab3 | 10344 | |
c826bd7a | 10345 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
10346 | return ret; |
10347 | err: | |
10348 | io_ring_ctx_wait_and_kill(ctx); | |
10349 | return ret; | |
10350 | } | |
10351 | ||
10352 | /* | |
10353 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
10354 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
10355 | * params structure passed in. | |
10356 | */ | |
10357 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
10358 | { | |
10359 | struct io_uring_params p; | |
2b188cc1 JA |
10360 | int i; |
10361 | ||
10362 | if (copy_from_user(&p, params, sizeof(p))) | |
10363 | return -EFAULT; | |
10364 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
10365 | if (p.resv[i]) | |
10366 | return -EINVAL; | |
10367 | } | |
10368 | ||
6c271ce2 | 10369 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 10370 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
10371 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
10372 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
10373 | return -EINVAL; |
10374 | ||
7f13657d | 10375 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
10376 | } |
10377 | ||
10378 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
10379 | struct io_uring_params __user *, params) | |
10380 | { | |
10381 | return io_uring_setup(entries, params); | |
10382 | } | |
10383 | ||
66f4af93 JA |
10384 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
10385 | { | |
10386 | struct io_uring_probe *p; | |
10387 | size_t size; | |
10388 | int i, ret; | |
10389 | ||
10390 | size = struct_size(p, ops, nr_args); | |
10391 | if (size == SIZE_MAX) | |
10392 | return -EOVERFLOW; | |
10393 | p = kzalloc(size, GFP_KERNEL); | |
10394 | if (!p) | |
10395 | return -ENOMEM; | |
10396 | ||
10397 | ret = -EFAULT; | |
10398 | if (copy_from_user(p, arg, size)) | |
10399 | goto out; | |
10400 | ret = -EINVAL; | |
10401 | if (memchr_inv(p, 0, size)) | |
10402 | goto out; | |
10403 | ||
10404 | p->last_op = IORING_OP_LAST - 1; | |
10405 | if (nr_args > IORING_OP_LAST) | |
10406 | nr_args = IORING_OP_LAST; | |
10407 | ||
10408 | for (i = 0; i < nr_args; i++) { | |
10409 | p->ops[i].op = i; | |
10410 | if (!io_op_defs[i].not_supported) | |
10411 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
10412 | } | |
10413 | p->ops_len = i; | |
10414 | ||
10415 | ret = 0; | |
10416 | if (copy_to_user(arg, p, size)) | |
10417 | ret = -EFAULT; | |
10418 | out: | |
10419 | kfree(p); | |
10420 | return ret; | |
10421 | } | |
10422 | ||
071698e1 JA |
10423 | static int io_register_personality(struct io_ring_ctx *ctx) |
10424 | { | |
4379bf8b | 10425 | const struct cred *creds; |
61cf9370 | 10426 | u32 id; |
1e6fa521 | 10427 | int ret; |
071698e1 | 10428 | |
4379bf8b | 10429 | creds = get_current_cred(); |
1e6fa521 | 10430 | |
61cf9370 MWO |
10431 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
10432 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); | |
a30f895a JA |
10433 | if (ret < 0) { |
10434 | put_cred(creds); | |
10435 | return ret; | |
10436 | } | |
10437 | return id; | |
071698e1 JA |
10438 | } |
10439 | ||
21b55dbc SG |
10440 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
10441 | unsigned int nr_args) | |
10442 | { | |
10443 | struct io_uring_restriction *res; | |
10444 | size_t size; | |
10445 | int i, ret; | |
10446 | ||
7e84e1c7 SG |
10447 | /* Restrictions allowed only if rings started disabled */ |
10448 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10449 | return -EBADFD; | |
10450 | ||
21b55dbc | 10451 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 10452 | if (ctx->restrictions.registered) |
21b55dbc SG |
10453 | return -EBUSY; |
10454 | ||
10455 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
10456 | return -EINVAL; | |
10457 | ||
10458 | size = array_size(nr_args, sizeof(*res)); | |
10459 | if (size == SIZE_MAX) | |
10460 | return -EOVERFLOW; | |
10461 | ||
10462 | res = memdup_user(arg, size); | |
10463 | if (IS_ERR(res)) | |
10464 | return PTR_ERR(res); | |
10465 | ||
10466 | ret = 0; | |
10467 | ||
10468 | for (i = 0; i < nr_args; i++) { | |
10469 | switch (res[i].opcode) { | |
10470 | case IORING_RESTRICTION_REGISTER_OP: | |
10471 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
10472 | ret = -EINVAL; | |
10473 | goto out; | |
10474 | } | |
10475 | ||
10476 | __set_bit(res[i].register_op, | |
10477 | ctx->restrictions.register_op); | |
10478 | break; | |
10479 | case IORING_RESTRICTION_SQE_OP: | |
10480 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
10481 | ret = -EINVAL; | |
10482 | goto out; | |
10483 | } | |
10484 | ||
10485 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
10486 | break; | |
10487 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
10488 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
10489 | break; | |
10490 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
10491 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
10492 | break; | |
10493 | default: | |
10494 | ret = -EINVAL; | |
10495 | goto out; | |
10496 | } | |
10497 | } | |
10498 | ||
10499 | out: | |
10500 | /* Reset all restrictions if an error happened */ | |
10501 | if (ret != 0) | |
10502 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
10503 | else | |
7e84e1c7 | 10504 | ctx->restrictions.registered = true; |
21b55dbc SG |
10505 | |
10506 | kfree(res); | |
10507 | return ret; | |
10508 | } | |
10509 | ||
7e84e1c7 SG |
10510 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
10511 | { | |
10512 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10513 | return -EBADFD; | |
10514 | ||
10515 | if (ctx->restrictions.registered) | |
10516 | ctx->restricted = 1; | |
10517 | ||
0298ef96 PB |
10518 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
10519 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) | |
10520 | wake_up(&ctx->sq_data->wait); | |
7e84e1c7 SG |
10521 | return 0; |
10522 | } | |
10523 | ||
fdecb662 | 10524 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 10525 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 PB |
10526 | unsigned nr_args) |
10527 | { | |
10528 | __u32 tmp; | |
10529 | int err; | |
10530 | ||
c3bdad02 PB |
10531 | if (up->resv) |
10532 | return -EINVAL; | |
98f0b3b4 PB |
10533 | if (check_add_overflow(up->offset, nr_args, &tmp)) |
10534 | return -EOVERFLOW; | |
10535 | err = io_rsrc_node_switch_start(ctx); | |
10536 | if (err) | |
10537 | return err; | |
10538 | ||
fdecb662 PB |
10539 | switch (type) { |
10540 | case IORING_RSRC_FILE: | |
98f0b3b4 | 10541 | return __io_sqe_files_update(ctx, up, nr_args); |
634d00df PB |
10542 | case IORING_RSRC_BUFFER: |
10543 | return __io_sqe_buffers_update(ctx, up, nr_args); | |
98f0b3b4 PB |
10544 | } |
10545 | return -EINVAL; | |
10546 | } | |
10547 | ||
c3bdad02 PB |
10548 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
10549 | unsigned nr_args) | |
98f0b3b4 | 10550 | { |
c3bdad02 | 10551 | struct io_uring_rsrc_update2 up; |
98f0b3b4 PB |
10552 | |
10553 | if (!nr_args) | |
10554 | return -EINVAL; | |
c3bdad02 PB |
10555 | memset(&up, 0, sizeof(up)); |
10556 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) | |
10557 | return -EFAULT; | |
10558 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); | |
10559 | } | |
10560 | ||
10561 | static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
992da01a | 10562 | unsigned size, unsigned type) |
c3bdad02 PB |
10563 | { |
10564 | struct io_uring_rsrc_update2 up; | |
10565 | ||
10566 | if (size != sizeof(up)) | |
10567 | return -EINVAL; | |
98f0b3b4 PB |
10568 | if (copy_from_user(&up, arg, sizeof(up))) |
10569 | return -EFAULT; | |
992da01a | 10570 | if (!up.nr || up.resv) |
98f0b3b4 | 10571 | return -EINVAL; |
992da01a | 10572 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
98f0b3b4 PB |
10573 | } |
10574 | ||
792e3582 | 10575 | static int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
992da01a | 10576 | unsigned int size, unsigned int type) |
792e3582 PB |
10577 | { |
10578 | struct io_uring_rsrc_register rr; | |
10579 | ||
10580 | /* keep it extendible */ | |
10581 | if (size != sizeof(rr)) | |
10582 | return -EINVAL; | |
10583 | ||
10584 | memset(&rr, 0, sizeof(rr)); | |
10585 | if (copy_from_user(&rr, arg, size)) | |
10586 | return -EFAULT; | |
992da01a | 10587 | if (!rr.nr || rr.resv || rr.resv2) |
792e3582 PB |
10588 | return -EINVAL; |
10589 | ||
992da01a | 10590 | switch (type) { |
792e3582 PB |
10591 | case IORING_RSRC_FILE: |
10592 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), | |
10593 | rr.nr, u64_to_user_ptr(rr.tags)); | |
634d00df PB |
10594 | case IORING_RSRC_BUFFER: |
10595 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), | |
10596 | rr.nr, u64_to_user_ptr(rr.tags)); | |
792e3582 PB |
10597 | } |
10598 | return -EINVAL; | |
10599 | } | |
10600 | ||
fe76421d JA |
10601 | static int io_register_iowq_aff(struct io_ring_ctx *ctx, void __user *arg, |
10602 | unsigned len) | |
10603 | { | |
10604 | struct io_uring_task *tctx = current->io_uring; | |
10605 | cpumask_var_t new_mask; | |
10606 | int ret; | |
10607 | ||
10608 | if (!tctx || !tctx->io_wq) | |
10609 | return -EINVAL; | |
10610 | ||
10611 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | |
10612 | return -ENOMEM; | |
10613 | ||
10614 | cpumask_clear(new_mask); | |
10615 | if (len > cpumask_size()) | |
10616 | len = cpumask_size(); | |
10617 | ||
10618 | if (copy_from_user(new_mask, arg, len)) { | |
10619 | free_cpumask_var(new_mask); | |
10620 | return -EFAULT; | |
10621 | } | |
10622 | ||
10623 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); | |
10624 | free_cpumask_var(new_mask); | |
10625 | return ret; | |
10626 | } | |
10627 | ||
10628 | static int io_unregister_iowq_aff(struct io_ring_ctx *ctx) | |
10629 | { | |
10630 | struct io_uring_task *tctx = current->io_uring; | |
10631 | ||
10632 | if (!tctx || !tctx->io_wq) | |
10633 | return -EINVAL; | |
10634 | ||
10635 | return io_wq_cpu_affinity(tctx->io_wq, NULL); | |
10636 | } | |
10637 | ||
2e480058 JA |
10638 | static int io_register_iowq_max_workers(struct io_ring_ctx *ctx, |
10639 | void __user *arg) | |
10640 | { | |
fa84693b JA |
10641 | struct io_uring_task *tctx = NULL; |
10642 | struct io_sq_data *sqd = NULL; | |
2e480058 JA |
10643 | __u32 new_count[2]; |
10644 | int i, ret; | |
10645 | ||
2e480058 JA |
10646 | if (copy_from_user(new_count, arg, sizeof(new_count))) |
10647 | return -EFAULT; | |
10648 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
10649 | if (new_count[i] > INT_MAX) | |
10650 | return -EINVAL; | |
10651 | ||
fa84693b JA |
10652 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
10653 | sqd = ctx->sq_data; | |
10654 | if (sqd) { | |
009ad9f0 JA |
10655 | /* |
10656 | * Observe the correct sqd->lock -> ctx->uring_lock | |
10657 | * ordering. Fine to drop uring_lock here, we hold | |
10658 | * a ref to the ctx. | |
10659 | */ | |
41d3a6bd | 10660 | refcount_inc(&sqd->refs); |
009ad9f0 | 10661 | mutex_unlock(&ctx->uring_lock); |
fa84693b | 10662 | mutex_lock(&sqd->lock); |
009ad9f0 | 10663 | mutex_lock(&ctx->uring_lock); |
41d3a6bd JA |
10664 | if (sqd->thread) |
10665 | tctx = sqd->thread->io_uring; | |
fa84693b JA |
10666 | } |
10667 | } else { | |
10668 | tctx = current->io_uring; | |
10669 | } | |
10670 | ||
10671 | ret = -EINVAL; | |
10672 | if (!tctx || !tctx->io_wq) | |
10673 | goto err; | |
10674 | ||
2e480058 JA |
10675 | ret = io_wq_max_workers(tctx->io_wq, new_count); |
10676 | if (ret) | |
fa84693b JA |
10677 | goto err; |
10678 | ||
41d3a6bd | 10679 | if (sqd) { |
fa84693b | 10680 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
10681 | io_put_sq_data(sqd); |
10682 | } | |
2e480058 JA |
10683 | |
10684 | if (copy_to_user(arg, new_count, sizeof(new_count))) | |
10685 | return -EFAULT; | |
10686 | ||
10687 | return 0; | |
fa84693b | 10688 | err: |
41d3a6bd | 10689 | if (sqd) { |
fa84693b | 10690 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
10691 | io_put_sq_data(sqd); |
10692 | } | |
fa84693b | 10693 | return ret; |
2e480058 JA |
10694 | } |
10695 | ||
071698e1 JA |
10696 | static bool io_register_op_must_quiesce(int op) |
10697 | { | |
10698 | switch (op) { | |
bd54b6fe BM |
10699 | case IORING_REGISTER_BUFFERS: |
10700 | case IORING_UNREGISTER_BUFFERS: | |
f4f7d21c | 10701 | case IORING_REGISTER_FILES: |
071698e1 JA |
10702 | case IORING_UNREGISTER_FILES: |
10703 | case IORING_REGISTER_FILES_UPDATE: | |
10704 | case IORING_REGISTER_PROBE: | |
10705 | case IORING_REGISTER_PERSONALITY: | |
10706 | case IORING_UNREGISTER_PERSONALITY: | |
992da01a PB |
10707 | case IORING_REGISTER_FILES2: |
10708 | case IORING_REGISTER_FILES_UPDATE2: | |
10709 | case IORING_REGISTER_BUFFERS2: | |
10710 | case IORING_REGISTER_BUFFERS_UPDATE: | |
fe76421d JA |
10711 | case IORING_REGISTER_IOWQ_AFF: |
10712 | case IORING_UNREGISTER_IOWQ_AFF: | |
2e480058 | 10713 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
071698e1 JA |
10714 | return false; |
10715 | default: | |
10716 | return true; | |
10717 | } | |
10718 | } | |
10719 | ||
e73c5c7c PB |
10720 | static int io_ctx_quiesce(struct io_ring_ctx *ctx) |
10721 | { | |
10722 | long ret; | |
10723 | ||
10724 | percpu_ref_kill(&ctx->refs); | |
10725 | ||
10726 | /* | |
10727 | * Drop uring mutex before waiting for references to exit. If another | |
10728 | * thread is currently inside io_uring_enter() it might need to grab the | |
10729 | * uring_lock to make progress. If we hold it here across the drain | |
10730 | * wait, then we can deadlock. It's safe to drop the mutex here, since | |
10731 | * no new references will come in after we've killed the percpu ref. | |
10732 | */ | |
10733 | mutex_unlock(&ctx->uring_lock); | |
10734 | do { | |
10735 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
10736 | if (!ret) | |
10737 | break; | |
10738 | ret = io_run_task_work_sig(); | |
10739 | } while (ret >= 0); | |
10740 | mutex_lock(&ctx->uring_lock); | |
10741 | ||
10742 | if (ret) | |
10743 | io_refs_resurrect(&ctx->refs, &ctx->ref_comp); | |
10744 | return ret; | |
10745 | } | |
10746 | ||
edafccee JA |
10747 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
10748 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
10749 | __releases(ctx->uring_lock) |
10750 | __acquires(ctx->uring_lock) | |
edafccee JA |
10751 | { |
10752 | int ret; | |
10753 | ||
35fa71a0 JA |
10754 | /* |
10755 | * We're inside the ring mutex, if the ref is already dying, then | |
10756 | * someone else killed the ctx or is already going through | |
10757 | * io_uring_register(). | |
10758 | */ | |
10759 | if (percpu_ref_is_dying(&ctx->refs)) | |
10760 | return -ENXIO; | |
10761 | ||
75c4021a PB |
10762 | if (ctx->restricted) { |
10763 | if (opcode >= IORING_REGISTER_LAST) | |
10764 | return -EINVAL; | |
10765 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); | |
10766 | if (!test_bit(opcode, ctx->restrictions.register_op)) | |
10767 | return -EACCES; | |
10768 | } | |
10769 | ||
071698e1 | 10770 | if (io_register_op_must_quiesce(opcode)) { |
e73c5c7c PB |
10771 | ret = io_ctx_quiesce(ctx); |
10772 | if (ret) | |
f70865db | 10773 | return ret; |
05f3fb3c | 10774 | } |
edafccee JA |
10775 | |
10776 | switch (opcode) { | |
10777 | case IORING_REGISTER_BUFFERS: | |
634d00df | 10778 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
edafccee JA |
10779 | break; |
10780 | case IORING_UNREGISTER_BUFFERS: | |
10781 | ret = -EINVAL; | |
10782 | if (arg || nr_args) | |
10783 | break; | |
0a96bbe4 | 10784 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 10785 | break; |
6b06314c | 10786 | case IORING_REGISTER_FILES: |
792e3582 | 10787 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
6b06314c JA |
10788 | break; |
10789 | case IORING_UNREGISTER_FILES: | |
10790 | ret = -EINVAL; | |
10791 | if (arg || nr_args) | |
10792 | break; | |
10793 | ret = io_sqe_files_unregister(ctx); | |
10794 | break; | |
c3a31e60 | 10795 | case IORING_REGISTER_FILES_UPDATE: |
c3bdad02 | 10796 | ret = io_register_files_update(ctx, arg, nr_args); |
c3a31e60 | 10797 | break; |
9b402849 | 10798 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 10799 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
10800 | ret = -EINVAL; |
10801 | if (nr_args != 1) | |
10802 | break; | |
10803 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
10804 | if (ret) |
10805 | break; | |
10806 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
10807 | ctx->eventfd_async = 1; | |
10808 | else | |
10809 | ctx->eventfd_async = 0; | |
9b402849 JA |
10810 | break; |
10811 | case IORING_UNREGISTER_EVENTFD: | |
10812 | ret = -EINVAL; | |
10813 | if (arg || nr_args) | |
10814 | break; | |
10815 | ret = io_eventfd_unregister(ctx); | |
10816 | break; | |
66f4af93 JA |
10817 | case IORING_REGISTER_PROBE: |
10818 | ret = -EINVAL; | |
10819 | if (!arg || nr_args > 256) | |
10820 | break; | |
10821 | ret = io_probe(ctx, arg, nr_args); | |
10822 | break; | |
071698e1 JA |
10823 | case IORING_REGISTER_PERSONALITY: |
10824 | ret = -EINVAL; | |
10825 | if (arg || nr_args) | |
10826 | break; | |
10827 | ret = io_register_personality(ctx); | |
10828 | break; | |
10829 | case IORING_UNREGISTER_PERSONALITY: | |
10830 | ret = -EINVAL; | |
10831 | if (arg) | |
10832 | break; | |
10833 | ret = io_unregister_personality(ctx, nr_args); | |
10834 | break; | |
7e84e1c7 SG |
10835 | case IORING_REGISTER_ENABLE_RINGS: |
10836 | ret = -EINVAL; | |
10837 | if (arg || nr_args) | |
10838 | break; | |
10839 | ret = io_register_enable_rings(ctx); | |
10840 | break; | |
21b55dbc SG |
10841 | case IORING_REGISTER_RESTRICTIONS: |
10842 | ret = io_register_restrictions(ctx, arg, nr_args); | |
10843 | break; | |
992da01a PB |
10844 | case IORING_REGISTER_FILES2: |
10845 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); | |
10846 | break; | |
10847 | case IORING_REGISTER_FILES_UPDATE2: | |
10848 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
10849 | IORING_RSRC_FILE); | |
10850 | break; | |
10851 | case IORING_REGISTER_BUFFERS2: | |
10852 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); | |
792e3582 | 10853 | break; |
992da01a PB |
10854 | case IORING_REGISTER_BUFFERS_UPDATE: |
10855 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
10856 | IORING_RSRC_BUFFER); | |
c3bdad02 | 10857 | break; |
fe76421d JA |
10858 | case IORING_REGISTER_IOWQ_AFF: |
10859 | ret = -EINVAL; | |
10860 | if (!arg || !nr_args) | |
10861 | break; | |
10862 | ret = io_register_iowq_aff(ctx, arg, nr_args); | |
10863 | break; | |
10864 | case IORING_UNREGISTER_IOWQ_AFF: | |
10865 | ret = -EINVAL; | |
10866 | if (arg || nr_args) | |
10867 | break; | |
10868 | ret = io_unregister_iowq_aff(ctx); | |
10869 | break; | |
2e480058 JA |
10870 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
10871 | ret = -EINVAL; | |
10872 | if (!arg || nr_args != 2) | |
10873 | break; | |
10874 | ret = io_register_iowq_max_workers(ctx, arg); | |
10875 | break; | |
edafccee JA |
10876 | default: |
10877 | ret = -EINVAL; | |
10878 | break; | |
10879 | } | |
10880 | ||
071698e1 | 10881 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 10882 | /* bring the ctx back to life */ |
05f3fb3c | 10883 | percpu_ref_reinit(&ctx->refs); |
0f158b4c | 10884 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 10885 | } |
edafccee JA |
10886 | return ret; |
10887 | } | |
10888 | ||
10889 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
10890 | void __user *, arg, unsigned int, nr_args) | |
10891 | { | |
10892 | struct io_ring_ctx *ctx; | |
10893 | long ret = -EBADF; | |
10894 | struct fd f; | |
10895 | ||
10896 | f = fdget(fd); | |
10897 | if (!f.file) | |
10898 | return -EBADF; | |
10899 | ||
10900 | ret = -EOPNOTSUPP; | |
10901 | if (f.file->f_op != &io_uring_fops) | |
10902 | goto out_fput; | |
10903 | ||
10904 | ctx = f.file->private_data; | |
10905 | ||
b6c23dd5 PB |
10906 | io_run_task_work(); |
10907 | ||
edafccee JA |
10908 | mutex_lock(&ctx->uring_lock); |
10909 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
10910 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
10911 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
10912 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
10913 | out_fput: |
10914 | fdput(f); | |
10915 | return ret; | |
10916 | } | |
10917 | ||
2b188cc1 JA |
10918 | static int __init io_uring_init(void) |
10919 | { | |
d7f62e82 SM |
10920 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
10921 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
10922 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
10923 | } while (0) | |
10924 | ||
10925 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
10926 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
10927 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
10928 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
10929 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
10930 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
10931 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
10932 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
10933 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
10934 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 10935 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
10936 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
10937 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
10938 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
10939 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
10940 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
10941 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
10942 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
10943 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
10944 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
10945 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
10946 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
10947 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
10948 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
10949 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
10950 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 10951 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
10952 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
10953 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
16340eab | 10954 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
d7f62e82 | 10955 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
7d67af2c | 10956 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
b9445598 | 10957 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
d7f62e82 | 10958 | |
b0d658ec PB |
10959 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
10960 | sizeof(struct io_uring_rsrc_update)); | |
10961 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > | |
10962 | sizeof(struct io_uring_rsrc_update2)); | |
90499ad0 PB |
10963 | |
10964 | /* ->buf_index is u16 */ | |
10965 | BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); | |
10966 | ||
b0d658ec PB |
10967 | /* should fit into one byte */ |
10968 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); | |
10969 | ||
d3656344 | 10970 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
32c2d33e | 10971 | BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int)); |
16340eab | 10972 | |
91f245d5 JA |
10973 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
10974 | SLAB_ACCOUNT); | |
2b188cc1 JA |
10975 | return 0; |
10976 | }; | |
10977 | __initcall(io_uring_init); |