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; |
1d7bb1d5 | 378 | struct list_head cq_overflow_list; |
7f1129d2 PB |
379 | struct xarray io_buffers; |
380 | struct xarray personalities; | |
381 | u32 pers_next; | |
382 | unsigned sq_thread_idle; | |
2b188cc1 JA |
383 | } ____cacheline_aligned_in_smp; |
384 | ||
d0acdee2 PB |
385 | /* IRQ completion list, under ->completion_lock */ |
386 | struct list_head locked_free_list; | |
387 | unsigned int locked_free_nr; | |
3c1a2ead | 388 | |
7c30f36a | 389 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
534ca6d6 JA |
390 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
391 | ||
90554200 | 392 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 393 | struct list_head sqd_list; |
75b28aff | 394 | |
5ed7a37d PB |
395 | unsigned long check_cq_overflow; |
396 | ||
206aefde JA |
397 | struct { |
398 | unsigned cached_cq_tail; | |
399 | unsigned cq_entries; | |
0499e582 | 400 | struct eventfd_ctx *cq_ev_fd; |
311997b3 | 401 | struct wait_queue_head poll_wait; |
0499e582 PB |
402 | struct wait_queue_head cq_wait; |
403 | unsigned cq_extra; | |
404 | atomic_t cq_timeouts; | |
206aefde | 405 | struct fasync_struct *cq_fasync; |
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 JA |
503 | int fd; |
504 | }; | |
505 | ||
ad8a48ac JA |
506 | struct io_timeout_data { |
507 | struct io_kiocb *req; | |
508 | struct hrtimer timer; | |
509 | struct timespec64 ts; | |
510 | enum hrtimer_mode mode; | |
511 | }; | |
512 | ||
8ed8d3c3 JA |
513 | struct io_accept { |
514 | struct file *file; | |
515 | struct sockaddr __user *addr; | |
516 | int __user *addr_len; | |
517 | int flags; | |
aaa4db12 | 518 | u32 file_slot; |
09952e3e | 519 | unsigned long nofile; |
8ed8d3c3 JA |
520 | }; |
521 | ||
522 | struct io_sync { | |
523 | struct file *file; | |
524 | loff_t len; | |
525 | loff_t off; | |
526 | int flags; | |
d63d1b5e | 527 | int mode; |
8ed8d3c3 JA |
528 | }; |
529 | ||
fbf23849 JA |
530 | struct io_cancel { |
531 | struct file *file; | |
532 | u64 addr; | |
533 | }; | |
534 | ||
b29472ee JA |
535 | struct io_timeout { |
536 | struct file *file; | |
bfe68a22 PB |
537 | u32 off; |
538 | u32 target_seq; | |
135fcde8 | 539 | struct list_head list; |
90cd7e42 PB |
540 | /* head of the link, used by linked timeouts only */ |
541 | struct io_kiocb *head; | |
89b263f6 JA |
542 | /* for linked completions */ |
543 | struct io_kiocb *prev; | |
b29472ee JA |
544 | }; |
545 | ||
0bdf7a2d PB |
546 | struct io_timeout_rem { |
547 | struct file *file; | |
548 | u64 addr; | |
9c8e11b3 PB |
549 | |
550 | /* timeout update */ | |
551 | struct timespec64 ts; | |
552 | u32 flags; | |
0bdf7a2d PB |
553 | }; |
554 | ||
9adbd45d JA |
555 | struct io_rw { |
556 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
557 | struct kiocb kiocb; | |
558 | u64 addr; | |
559 | u64 len; | |
560 | }; | |
561 | ||
3fbb51c1 JA |
562 | struct io_connect { |
563 | struct file *file; | |
564 | struct sockaddr __user *addr; | |
565 | int addr_len; | |
566 | }; | |
567 | ||
e47293fd JA |
568 | struct io_sr_msg { |
569 | struct file *file; | |
fddaface | 570 | union { |
4af3417a PB |
571 | struct compat_msghdr __user *umsg_compat; |
572 | struct user_msghdr __user *umsg; | |
573 | void __user *buf; | |
fddaface | 574 | }; |
e47293fd | 575 | int msg_flags; |
bcda7baa | 576 | int bgid; |
fddaface | 577 | size_t len; |
bcda7baa | 578 | struct io_buffer *kbuf; |
e47293fd JA |
579 | }; |
580 | ||
15b71abe JA |
581 | struct io_open { |
582 | struct file *file; | |
583 | int dfd; | |
b9445598 | 584 | u32 file_slot; |
15b71abe | 585 | struct filename *filename; |
c12cedf2 | 586 | struct open_how how; |
4022e7af | 587 | unsigned long nofile; |
15b71abe JA |
588 | }; |
589 | ||
269bbe5f | 590 | struct io_rsrc_update { |
05f3fb3c JA |
591 | struct file *file; |
592 | u64 arg; | |
593 | u32 nr_args; | |
594 | u32 offset; | |
595 | }; | |
596 | ||
4840e418 JA |
597 | struct io_fadvise { |
598 | struct file *file; | |
599 | u64 offset; | |
600 | u32 len; | |
601 | u32 advice; | |
602 | }; | |
603 | ||
c1ca757b JA |
604 | struct io_madvise { |
605 | struct file *file; | |
606 | u64 addr; | |
607 | u32 len; | |
608 | u32 advice; | |
609 | }; | |
610 | ||
3e4827b0 JA |
611 | struct io_epoll { |
612 | struct file *file; | |
613 | int epfd; | |
614 | int op; | |
615 | int fd; | |
616 | struct epoll_event event; | |
e47293fd JA |
617 | }; |
618 | ||
7d67af2c PB |
619 | struct io_splice { |
620 | struct file *file_out; | |
621 | struct file *file_in; | |
622 | loff_t off_out; | |
623 | loff_t off_in; | |
624 | u64 len; | |
625 | unsigned int flags; | |
626 | }; | |
627 | ||
ddf0322d JA |
628 | struct io_provide_buf { |
629 | struct file *file; | |
630 | __u64 addr; | |
38134ada | 631 | __u32 len; |
ddf0322d JA |
632 | __u32 bgid; |
633 | __u16 nbufs; | |
634 | __u16 bid; | |
635 | }; | |
636 | ||
1d9e1288 BM |
637 | struct io_statx { |
638 | struct file *file; | |
639 | int dfd; | |
640 | unsigned int mask; | |
641 | unsigned int flags; | |
e62753e4 | 642 | const char __user *filename; |
1d9e1288 BM |
643 | struct statx __user *buffer; |
644 | }; | |
645 | ||
36f4fa68 JA |
646 | struct io_shutdown { |
647 | struct file *file; | |
648 | int how; | |
649 | }; | |
650 | ||
80a261fd JA |
651 | struct io_rename { |
652 | struct file *file; | |
653 | int old_dfd; | |
654 | int new_dfd; | |
655 | struct filename *oldpath; | |
656 | struct filename *newpath; | |
657 | int flags; | |
658 | }; | |
659 | ||
14a1143b JA |
660 | struct io_unlink { |
661 | struct file *file; | |
662 | int dfd; | |
663 | int flags; | |
664 | struct filename *filename; | |
665 | }; | |
666 | ||
3ca405eb PB |
667 | struct io_completion { |
668 | struct file *file; | |
8c3f9cd1 | 669 | u32 cflags; |
3ca405eb PB |
670 | }; |
671 | ||
f499a021 JA |
672 | struct io_async_connect { |
673 | struct sockaddr_storage address; | |
674 | }; | |
675 | ||
03b1230c JA |
676 | struct io_async_msghdr { |
677 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
678 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
679 | struct iovec *free_iov; | |
03b1230c JA |
680 | struct sockaddr __user *uaddr; |
681 | struct msghdr msg; | |
b537916c | 682 | struct sockaddr_storage addr; |
03b1230c JA |
683 | }; |
684 | ||
f67676d1 JA |
685 | struct io_async_rw { |
686 | struct iovec fast_iov[UIO_FASTIOV]; | |
ff6165b2 JA |
687 | const struct iovec *free_iovec; |
688 | struct iov_iter iter; | |
227c0c96 | 689 | size_t bytes_done; |
bcf5a063 | 690 | struct wait_page_queue wpq; |
f67676d1 JA |
691 | }; |
692 | ||
6b47ee6e PB |
693 | enum { |
694 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
695 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
696 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
697 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
698 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 699 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 700 | |
dddca226 | 701 | /* first byte is taken by user flags, shift it to not overlap */ |
93d2bcd2 | 702 | REQ_F_FAIL_BIT = 8, |
6b47ee6e PB |
703 | REQ_F_INFLIGHT_BIT, |
704 | REQ_F_CUR_POS_BIT, | |
705 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 706 | REQ_F_LINK_TIMEOUT_BIT, |
99bc4c38 | 707 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 708 | REQ_F_POLLED_BIT, |
bcda7baa | 709 | REQ_F_BUFFER_SELECTED_BIT, |
e342c807 | 710 | REQ_F_COMPLETE_INLINE_BIT, |
230d50d4 | 711 | REQ_F_REISSUE_BIT, |
8c130827 | 712 | REQ_F_DONT_REISSUE_BIT, |
b8e64b53 | 713 | REQ_F_CREDS_BIT, |
20e60a38 | 714 | REQ_F_REFCOUNT_BIT, |
4d13d1a4 | 715 | REQ_F_ARM_LTIMEOUT_BIT, |
7b29f92d | 716 | /* keep async read/write and isreg together and in order */ |
b191e2df PB |
717 | REQ_F_NOWAIT_READ_BIT, |
718 | REQ_F_NOWAIT_WRITE_BIT, | |
7b29f92d | 719 | REQ_F_ISREG_BIT, |
84557871 JA |
720 | |
721 | /* not a real bit, just to check we're not overflowing the space */ | |
722 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
723 | }; |
724 | ||
725 | enum { | |
726 | /* ctx owns file */ | |
727 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
728 | /* drain existing IO first */ | |
729 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
730 | /* linked sqes */ | |
731 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
732 | /* doesn't sever on completion < 0 */ | |
733 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
734 | /* IOSQE_ASYNC */ | |
735 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
736 | /* IOSQE_BUFFER_SELECT */ |
737 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 738 | |
6b47ee6e | 739 | /* fail rest of links */ |
93d2bcd2 | 740 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
b05a1bcd | 741 | /* on inflight list, should be cancelled and waited on exit reliably */ |
6b47ee6e PB |
742 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
743 | /* read/write uses file position */ | |
744 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
745 | /* must not punt to workers */ | |
746 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 747 | /* has or had linked timeout */ |
6b47ee6e | 748 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
99bc4c38 PB |
749 | /* needs cleanup */ |
750 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
751 | /* already went through poll handler */ |
752 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
753 | /* buffer already selected */ |
754 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
e342c807 PB |
755 | /* completion is deferred through io_comp_state */ |
756 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
230d50d4 JA |
757 | /* caller should reissue async */ |
758 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), | |
8c130827 PB |
759 | /* don't attempt request reissue, see io_rw_reissue() */ |
760 | REQ_F_DONT_REISSUE = BIT(REQ_F_DONT_REISSUE_BIT), | |
7b29f92d | 761 | /* supports async reads */ |
b191e2df | 762 | REQ_F_NOWAIT_READ = BIT(REQ_F_NOWAIT_READ_BIT), |
7b29f92d | 763 | /* supports async writes */ |
b191e2df | 764 | REQ_F_NOWAIT_WRITE = BIT(REQ_F_NOWAIT_WRITE_BIT), |
7b29f92d JA |
765 | /* regular file */ |
766 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
b8e64b53 PB |
767 | /* has creds assigned */ |
768 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), | |
20e60a38 PB |
769 | /* skip refcounting if not set */ |
770 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), | |
4d13d1a4 PB |
771 | /* there is a linked timeout that has to be armed */ |
772 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT), | |
d7718a9d JA |
773 | }; |
774 | ||
775 | struct async_poll { | |
776 | struct io_poll_iocb poll; | |
807abcb0 | 777 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
778 | }; |
779 | ||
f237c30a | 780 | typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked); |
5b0a6acc | 781 | |
7cbf1722 | 782 | struct io_task_work { |
5b0a6acc PB |
783 | union { |
784 | struct io_wq_work_node node; | |
785 | struct llist_node fallback_node; | |
786 | }; | |
787 | io_req_tw_func_t func; | |
7cbf1722 JA |
788 | }; |
789 | ||
992da01a PB |
790 | enum { |
791 | IORING_RSRC_FILE = 0, | |
792 | IORING_RSRC_BUFFER = 1, | |
793 | }; | |
794 | ||
09bb8394 JA |
795 | /* |
796 | * NOTE! Each of the iocb union members has the file pointer | |
797 | * as the first entry in their struct definition. So you can | |
798 | * access the file pointer through any of the sub-structs, | |
799 | * or directly as just 'ki_filp' in this struct. | |
800 | */ | |
2b188cc1 | 801 | struct io_kiocb { |
221c5eb2 | 802 | union { |
09bb8394 | 803 | struct file *file; |
9adbd45d | 804 | struct io_rw rw; |
221c5eb2 | 805 | struct io_poll_iocb poll; |
9d805892 | 806 | struct io_poll_update poll_update; |
8ed8d3c3 JA |
807 | struct io_accept accept; |
808 | struct io_sync sync; | |
fbf23849 | 809 | struct io_cancel cancel; |
b29472ee | 810 | struct io_timeout timeout; |
0bdf7a2d | 811 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 812 | struct io_connect connect; |
e47293fd | 813 | struct io_sr_msg sr_msg; |
15b71abe | 814 | struct io_open open; |
b5dba59e | 815 | struct io_close close; |
269bbe5f | 816 | struct io_rsrc_update rsrc_update; |
4840e418 | 817 | struct io_fadvise fadvise; |
c1ca757b | 818 | struct io_madvise madvise; |
3e4827b0 | 819 | struct io_epoll epoll; |
7d67af2c | 820 | struct io_splice splice; |
ddf0322d | 821 | struct io_provide_buf pbuf; |
1d9e1288 | 822 | struct io_statx statx; |
36f4fa68 | 823 | struct io_shutdown shutdown; |
80a261fd | 824 | struct io_rename rename; |
14a1143b | 825 | struct io_unlink unlink; |
3ca405eb PB |
826 | /* use only after cleaning per-op data, see io_clean_op() */ |
827 | struct io_completion compl; | |
221c5eb2 | 828 | }; |
2b188cc1 | 829 | |
e8c2bc1f JA |
830 | /* opcode allocated if it needs to store data for async defer */ |
831 | void *async_data; | |
d625c6ee | 832 | u8 opcode; |
65a6543d XW |
833 | /* polled IO has completed */ |
834 | u8 iopoll_completed; | |
2b188cc1 | 835 | |
4f4eeba8 | 836 | u16 buf_index; |
9cf7c104 | 837 | u32 result; |
4f4eeba8 | 838 | |
010e8e6b PB |
839 | struct io_ring_ctx *ctx; |
840 | unsigned int flags; | |
abc54d63 | 841 | atomic_t refs; |
010e8e6b PB |
842 | struct task_struct *task; |
843 | u64 user_data; | |
d7718a9d | 844 | |
f2f87370 | 845 | struct io_kiocb *link; |
269bbe5f | 846 | struct percpu_ref *fixed_rsrc_refs; |
fcb323cc | 847 | |
b303fe2e | 848 | /* used with ctx->iopoll_list with reads/writes */ |
010e8e6b | 849 | struct list_head inflight_entry; |
5b0a6acc | 850 | struct io_task_work io_task_work; |
010e8e6b PB |
851 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
852 | struct hlist_node hash_node; | |
853 | struct async_poll *apoll; | |
854 | struct io_wq_work work; | |
fe7e3257 | 855 | const struct cred *creds; |
c10d1f98 | 856 | |
eae071c9 PB |
857 | /* store used ubuf, so we can prevent reloading */ |
858 | struct io_mapped_ubuf *imu; | |
2b188cc1 | 859 | }; |
05589553 | 860 | |
13bf43f5 PB |
861 | struct io_tctx_node { |
862 | struct list_head ctx_node; | |
863 | struct task_struct *task; | |
13bf43f5 PB |
864 | struct io_ring_ctx *ctx; |
865 | }; | |
866 | ||
27dc8338 PB |
867 | struct io_defer_entry { |
868 | struct list_head list; | |
869 | struct io_kiocb *req; | |
9cf7c104 | 870 | u32 seq; |
2b188cc1 JA |
871 | }; |
872 | ||
d3656344 | 873 | struct io_op_def { |
d3656344 JA |
874 | /* needs req->file assigned */ |
875 | unsigned needs_file : 1; | |
d3656344 JA |
876 | /* hash wq insertion if file is a regular file */ |
877 | unsigned hash_reg_file : 1; | |
878 | /* unbound wq insertion if file is a non-regular file */ | |
879 | unsigned unbound_nonreg_file : 1; | |
66f4af93 JA |
880 | /* opcode is not supported by this kernel */ |
881 | unsigned not_supported : 1; | |
8a72758c JA |
882 | /* set if opcode supports polled "wait" */ |
883 | unsigned pollin : 1; | |
884 | unsigned pollout : 1; | |
bcda7baa JA |
885 | /* op supports buffer selection */ |
886 | unsigned buffer_select : 1; | |
26f0505a PB |
887 | /* do prep async if is going to be punted */ |
888 | unsigned needs_async_setup : 1; | |
27926b68 JA |
889 | /* should block plug */ |
890 | unsigned plug : 1; | |
e8c2bc1f JA |
891 | /* size of async data needed, if any */ |
892 | unsigned short async_size; | |
d3656344 JA |
893 | }; |
894 | ||
0918682b | 895 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
896 | [IORING_OP_NOP] = {}, |
897 | [IORING_OP_READV] = { | |
d3656344 JA |
898 | .needs_file = 1, |
899 | .unbound_nonreg_file = 1, | |
8a72758c | 900 | .pollin = 1, |
4d954c25 | 901 | .buffer_select = 1, |
26f0505a | 902 | .needs_async_setup = 1, |
27926b68 | 903 | .plug = 1, |
e8c2bc1f | 904 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 905 | }, |
0463b6c5 | 906 | [IORING_OP_WRITEV] = { |
d3656344 JA |
907 | .needs_file = 1, |
908 | .hash_reg_file = 1, | |
909 | .unbound_nonreg_file = 1, | |
8a72758c | 910 | .pollout = 1, |
26f0505a | 911 | .needs_async_setup = 1, |
27926b68 | 912 | .plug = 1, |
e8c2bc1f | 913 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 914 | }, |
0463b6c5 | 915 | [IORING_OP_FSYNC] = { |
d3656344 JA |
916 | .needs_file = 1, |
917 | }, | |
0463b6c5 | 918 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
919 | .needs_file = 1, |
920 | .unbound_nonreg_file = 1, | |
8a72758c | 921 | .pollin = 1, |
27926b68 | 922 | .plug = 1, |
e8c2bc1f | 923 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 924 | }, |
0463b6c5 | 925 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
926 | .needs_file = 1, |
927 | .hash_reg_file = 1, | |
928 | .unbound_nonreg_file = 1, | |
8a72758c | 929 | .pollout = 1, |
27926b68 | 930 | .plug = 1, |
e8c2bc1f | 931 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 932 | }, |
0463b6c5 | 933 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
934 | .needs_file = 1, |
935 | .unbound_nonreg_file = 1, | |
936 | }, | |
0463b6c5 PB |
937 | [IORING_OP_POLL_REMOVE] = {}, |
938 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 JA |
939 | .needs_file = 1, |
940 | }, | |
0463b6c5 | 941 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
942 | .needs_file = 1, |
943 | .unbound_nonreg_file = 1, | |
8a72758c | 944 | .pollout = 1, |
26f0505a | 945 | .needs_async_setup = 1, |
e8c2bc1f | 946 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 947 | }, |
0463b6c5 | 948 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
949 | .needs_file = 1, |
950 | .unbound_nonreg_file = 1, | |
8a72758c | 951 | .pollin = 1, |
52de1fe1 | 952 | .buffer_select = 1, |
26f0505a | 953 | .needs_async_setup = 1, |
e8c2bc1f | 954 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 955 | }, |
0463b6c5 | 956 | [IORING_OP_TIMEOUT] = { |
e8c2bc1f | 957 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 958 | }, |
9c8e11b3 PB |
959 | [IORING_OP_TIMEOUT_REMOVE] = { |
960 | /* used by timeout updates' prep() */ | |
9c8e11b3 | 961 | }, |
0463b6c5 | 962 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
963 | .needs_file = 1, |
964 | .unbound_nonreg_file = 1, | |
8a72758c | 965 | .pollin = 1, |
d3656344 | 966 | }, |
0463b6c5 PB |
967 | [IORING_OP_ASYNC_CANCEL] = {}, |
968 | [IORING_OP_LINK_TIMEOUT] = { | |
e8c2bc1f | 969 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 970 | }, |
0463b6c5 | 971 | [IORING_OP_CONNECT] = { |
d3656344 JA |
972 | .needs_file = 1, |
973 | .unbound_nonreg_file = 1, | |
8a72758c | 974 | .pollout = 1, |
26f0505a | 975 | .needs_async_setup = 1, |
e8c2bc1f | 976 | .async_size = sizeof(struct io_async_connect), |
d3656344 | 977 | }, |
0463b6c5 | 978 | [IORING_OP_FALLOCATE] = { |
d3656344 | 979 | .needs_file = 1, |
d3656344 | 980 | }, |
44526bed JA |
981 | [IORING_OP_OPENAT] = {}, |
982 | [IORING_OP_CLOSE] = {}, | |
983 | [IORING_OP_FILES_UPDATE] = {}, | |
984 | [IORING_OP_STATX] = {}, | |
0463b6c5 | 985 | [IORING_OP_READ] = { |
3a6820f2 JA |
986 | .needs_file = 1, |
987 | .unbound_nonreg_file = 1, | |
8a72758c | 988 | .pollin = 1, |
bcda7baa | 989 | .buffer_select = 1, |
27926b68 | 990 | .plug = 1, |
e8c2bc1f | 991 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 992 | }, |
0463b6c5 | 993 | [IORING_OP_WRITE] = { |
3a6820f2 JA |
994 | .needs_file = 1, |
995 | .unbound_nonreg_file = 1, | |
8a72758c | 996 | .pollout = 1, |
27926b68 | 997 | .plug = 1, |
e8c2bc1f | 998 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 999 | }, |
0463b6c5 | 1000 | [IORING_OP_FADVISE] = { |
4840e418 | 1001 | .needs_file = 1, |
c1ca757b | 1002 | }, |
44526bed | 1003 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 1004 | [IORING_OP_SEND] = { |
fddaface JA |
1005 | .needs_file = 1, |
1006 | .unbound_nonreg_file = 1, | |
8a72758c | 1007 | .pollout = 1, |
fddaface | 1008 | }, |
0463b6c5 | 1009 | [IORING_OP_RECV] = { |
fddaface JA |
1010 | .needs_file = 1, |
1011 | .unbound_nonreg_file = 1, | |
8a72758c | 1012 | .pollin = 1, |
bcda7baa | 1013 | .buffer_select = 1, |
fddaface | 1014 | }, |
0463b6c5 | 1015 | [IORING_OP_OPENAT2] = { |
cebdb986 | 1016 | }, |
3e4827b0 JA |
1017 | [IORING_OP_EPOLL_CTL] = { |
1018 | .unbound_nonreg_file = 1, | |
3e4827b0 | 1019 | }, |
7d67af2c PB |
1020 | [IORING_OP_SPLICE] = { |
1021 | .needs_file = 1, | |
1022 | .hash_reg_file = 1, | |
1023 | .unbound_nonreg_file = 1, | |
ddf0322d JA |
1024 | }, |
1025 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 1026 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
1027 | [IORING_OP_TEE] = { |
1028 | .needs_file = 1, | |
1029 | .hash_reg_file = 1, | |
1030 | .unbound_nonreg_file = 1, | |
1031 | }, | |
36f4fa68 JA |
1032 | [IORING_OP_SHUTDOWN] = { |
1033 | .needs_file = 1, | |
1034 | }, | |
44526bed JA |
1035 | [IORING_OP_RENAMEAT] = {}, |
1036 | [IORING_OP_UNLINKAT] = {}, | |
d3656344 JA |
1037 | }; |
1038 | ||
0756a869 PB |
1039 | /* requests with any of those set should undergo io_disarm_next() */ |
1040 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) | |
1041 | ||
7a612350 | 1042 | static bool io_disarm_next(struct io_kiocb *req); |
eef51daa | 1043 | static void io_uring_del_tctx_node(unsigned long index); |
9936c7c2 PB |
1044 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1045 | struct task_struct *task, | |
3dd0c97a | 1046 | bool cancel_all); |
78cc687b | 1047 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); |
1ffc5422 | 1048 | |
d4d19c19 PB |
1049 | static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
1050 | long res, unsigned int cflags); | |
ec9c02ad | 1051 | static void io_put_req(struct io_kiocb *req); |
91c2f697 | 1052 | static void io_put_req_deferred(struct io_kiocb *req); |
c7dae4ba | 1053 | static void io_dismantle_req(struct io_kiocb *req); |
94ae5e77 | 1054 | static void io_queue_linked_timeout(struct io_kiocb *req); |
fdecb662 | 1055 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 1056 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 | 1057 | unsigned nr_args); |
68fb8979 | 1058 | static void io_clean_op(struct io_kiocb *req); |
ac177053 | 1059 | static struct file *io_file_get(struct io_ring_ctx *ctx, |
8371adf5 | 1060 | struct io_kiocb *req, int fd, bool fixed); |
c5eef2b9 | 1061 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1062 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1063 | |
907d1df3 | 1064 | static void io_req_task_queue(struct io_kiocb *req); |
2a2758f2 | 1065 | static void io_submit_flush_completions(struct io_ring_ctx *ctx); |
179ae0d1 | 1066 | static int io_req_prep_async(struct io_kiocb *req); |
de0617e4 | 1067 | |
b9445598 PB |
1068 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
1069 | unsigned int issue_flags, u32 slot_index); | |
1070 | ||
2b188cc1 JA |
1071 | static struct kmem_cache *req_cachep; |
1072 | ||
0918682b | 1073 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1074 | |
1075 | struct sock *io_uring_get_socket(struct file *file) | |
1076 | { | |
1077 | #if defined(CONFIG_UNIX) | |
1078 | if (file->f_op == &io_uring_fops) { | |
1079 | struct io_ring_ctx *ctx = file->private_data; | |
1080 | ||
1081 | return ctx->ring_sock->sk; | |
1082 | } | |
1083 | #endif | |
1084 | return NULL; | |
1085 | } | |
1086 | EXPORT_SYMBOL(io_uring_get_socket); | |
1087 | ||
f237c30a PB |
1088 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
1089 | { | |
1090 | if (!*locked) { | |
1091 | mutex_lock(&ctx->uring_lock); | |
1092 | *locked = true; | |
1093 | } | |
1094 | } | |
1095 | ||
f2f87370 PB |
1096 | #define io_for_each_link(pos, head) \ |
1097 | for (pos = (head); pos; pos = pos->link) | |
1098 | ||
21c843d5 PB |
1099 | /* |
1100 | * Shamelessly stolen from the mm implementation of page reference checking, | |
1101 | * see commit f958d7b528b1 for details. | |
1102 | */ | |
1103 | #define req_ref_zero_or_close_to_overflow(req) \ | |
1104 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) | |
1105 | ||
1106 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) | |
1107 | { | |
20e60a38 | 1108 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1109 | return atomic_inc_not_zero(&req->refs); |
1110 | } | |
1111 | ||
21c843d5 PB |
1112 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
1113 | { | |
20e60a38 PB |
1114 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
1115 | return true; | |
1116 | ||
21c843d5 PB |
1117 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1118 | return atomic_dec_and_test(&req->refs); | |
1119 | } | |
1120 | ||
1121 | static inline void req_ref_put(struct io_kiocb *req) | |
1122 | { | |
20e60a38 | 1123 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1124 | WARN_ON_ONCE(req_ref_put_and_test(req)); |
1125 | } | |
1126 | ||
1127 | static inline void req_ref_get(struct io_kiocb *req) | |
1128 | { | |
20e60a38 | 1129 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1130 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1131 | atomic_inc(&req->refs); | |
1132 | } | |
1133 | ||
48dcd38d | 1134 | static inline void __io_req_set_refcount(struct io_kiocb *req, int nr) |
20e60a38 PB |
1135 | { |
1136 | if (!(req->flags & REQ_F_REFCOUNT)) { | |
1137 | req->flags |= REQ_F_REFCOUNT; | |
48dcd38d | 1138 | atomic_set(&req->refs, nr); |
20e60a38 PB |
1139 | } |
1140 | } | |
1141 | ||
48dcd38d PB |
1142 | static inline void io_req_set_refcount(struct io_kiocb *req) |
1143 | { | |
1144 | __io_req_set_refcount(req, 1); | |
1145 | } | |
1146 | ||
b895c9a6 | 1147 | static inline void io_req_set_rsrc_node(struct io_kiocb *req) |
36f72fe2 PB |
1148 | { |
1149 | struct io_ring_ctx *ctx = req->ctx; | |
1150 | ||
269bbe5f | 1151 | if (!req->fixed_rsrc_refs) { |
a7f0ed5a | 1152 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
269bbe5f | 1153 | percpu_ref_get(req->fixed_rsrc_refs); |
36f72fe2 PB |
1154 | } |
1155 | } | |
1156 | ||
f70865db PB |
1157 | static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
1158 | { | |
1159 | bool got = percpu_ref_tryget(ref); | |
1160 | ||
1161 | /* already at zero, wait for ->release() */ | |
1162 | if (!got) | |
1163 | wait_for_completion(compl); | |
1164 | percpu_ref_resurrect(ref); | |
1165 | if (got) | |
1166 | percpu_ref_put(ref); | |
1167 | } | |
1168 | ||
3dd0c97a PB |
1169 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
1170 | bool cancel_all) | |
08d23634 PB |
1171 | { |
1172 | struct io_kiocb *req; | |
1173 | ||
68207680 | 1174 | if (task && head->task != task) |
08d23634 | 1175 | return false; |
3dd0c97a | 1176 | if (cancel_all) |
08d23634 PB |
1177 | return true; |
1178 | ||
1179 | io_for_each_link(req, head) { | |
b05a1bcd | 1180 | if (req->flags & REQ_F_INFLIGHT) |
02a13674 | 1181 | return true; |
08d23634 PB |
1182 | } |
1183 | return false; | |
1184 | } | |
1185 | ||
93d2bcd2 | 1186 | static inline void req_set_fail(struct io_kiocb *req) |
c40f6379 | 1187 | { |
93d2bcd2 | 1188 | req->flags |= REQ_F_FAIL; |
c40f6379 | 1189 | } |
4a38aed2 | 1190 | |
a8295b98 HX |
1191 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
1192 | { | |
1193 | req_set_fail(req); | |
1194 | req->result = res; | |
1195 | } | |
1196 | ||
2b188cc1 JA |
1197 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
1198 | { | |
1199 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1200 | ||
0f158b4c | 1201 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1202 | } |
1203 | ||
8eb7e2d0 PB |
1204 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1205 | { | |
1206 | return !req->timeout.off; | |
1207 | } | |
1208 | ||
f56165e6 PB |
1209 | static void io_fallback_req_func(struct work_struct *work) |
1210 | { | |
1211 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, | |
1212 | fallback_work.work); | |
1213 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); | |
1214 | struct io_kiocb *req, *tmp; | |
f237c30a | 1215 | bool locked = false; |
f56165e6 PB |
1216 | |
1217 | percpu_ref_get(&ctx->refs); | |
1218 | llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node) | |
f237c30a | 1219 | req->io_task_work.func(req, &locked); |
5636c00d | 1220 | |
f237c30a PB |
1221 | if (locked) { |
1222 | if (ctx->submit_state.compl_nr) | |
1223 | io_submit_flush_completions(ctx); | |
1224 | mutex_unlock(&ctx->uring_lock); | |
1225 | } | |
f56165e6 | 1226 | percpu_ref_put(&ctx->refs); |
f237c30a | 1227 | |
f56165e6 PB |
1228 | } |
1229 | ||
2b188cc1 JA |
1230 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
1231 | { | |
1232 | struct io_ring_ctx *ctx; | |
78076bb6 | 1233 | int hash_bits; |
2b188cc1 JA |
1234 | |
1235 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1236 | if (!ctx) | |
1237 | return NULL; | |
1238 | ||
78076bb6 JA |
1239 | /* |
1240 | * Use 5 bits less than the max cq entries, that should give us around | |
1241 | * 32 entries per hash list if totally full and uniformly spread. | |
1242 | */ | |
1243 | hash_bits = ilog2(p->cq_entries); | |
1244 | hash_bits -= 5; | |
1245 | if (hash_bits <= 0) | |
1246 | hash_bits = 1; | |
1247 | ctx->cancel_hash_bits = hash_bits; | |
1248 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1249 | GFP_KERNEL); | |
1250 | if (!ctx->cancel_hash) | |
1251 | goto err; | |
1252 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1253 | ||
6224843d PB |
1254 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
1255 | if (!ctx->dummy_ubuf) | |
1256 | goto err; | |
1257 | /* set invalid range, so io_import_fixed() fails meeting it */ | |
1258 | ctx->dummy_ubuf->ubuf = -1UL; | |
1259 | ||
21482896 | 1260 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1261 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1262 | goto err; | |
2b188cc1 JA |
1263 | |
1264 | ctx->flags = p->flags; | |
90554200 | 1265 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1266 | INIT_LIST_HEAD(&ctx->sqd_list); |
311997b3 | 1267 | init_waitqueue_head(&ctx->poll_wait); |
1d7bb1d5 | 1268 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c | 1269 | init_completion(&ctx->ref_comp); |
9e15c3a0 | 1270 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
61cf9370 | 1271 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
2b188cc1 | 1272 | mutex_init(&ctx->uring_lock); |
311997b3 | 1273 | init_waitqueue_head(&ctx->cq_wait); |
2b188cc1 | 1274 | spin_lock_init(&ctx->completion_lock); |
89850fce | 1275 | spin_lock_init(&ctx->timeout_lock); |
540e32a0 | 1276 | INIT_LIST_HEAD(&ctx->iopoll_list); |
de0617e4 | 1277 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1278 | INIT_LIST_HEAD(&ctx->timeout_list); |
d67d2263 BM |
1279 | spin_lock_init(&ctx->rsrc_ref_lock); |
1280 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1281 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1282 | init_llist_head(&ctx->rsrc_put_llist); | |
13bf43f5 | 1283 | INIT_LIST_HEAD(&ctx->tctx_list); |
cd0ca2e0 | 1284 | INIT_LIST_HEAD(&ctx->submit_state.free_list); |
d0acdee2 | 1285 | INIT_LIST_HEAD(&ctx->locked_free_list); |
9011bf9a | 1286 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
2b188cc1 | 1287 | return ctx; |
206aefde | 1288 | err: |
6224843d | 1289 | kfree(ctx->dummy_ubuf); |
78076bb6 | 1290 | kfree(ctx->cancel_hash); |
206aefde JA |
1291 | kfree(ctx); |
1292 | return NULL; | |
2b188cc1 JA |
1293 | } |
1294 | ||
8f6ed49a PB |
1295 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
1296 | { | |
1297 | struct io_rings *r = ctx->rings; | |
1298 | ||
1299 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); | |
1300 | ctx->cq_extra--; | |
1301 | } | |
1302 | ||
9cf7c104 | 1303 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1304 | { |
2bc9930e JA |
1305 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1306 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1307 | |
8f6ed49a | 1308 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
2bc9930e | 1309 | } |
de0617e4 | 1310 | |
9d858b21 | 1311 | return false; |
de0617e4 JA |
1312 | } |
1313 | ||
c97d8a0f PB |
1314 | #define FFS_ASYNC_READ 0x1UL |
1315 | #define FFS_ASYNC_WRITE 0x2UL | |
1316 | #ifdef CONFIG_64BIT | |
1317 | #define FFS_ISREG 0x4UL | |
1318 | #else | |
1319 | #define FFS_ISREG 0x0UL | |
1320 | #endif | |
1321 | #define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG) | |
1322 | ||
1323 | static inline bool io_req_ffs_set(struct io_kiocb *req) | |
1324 | { | |
1325 | return IS_ENABLED(CONFIG_64BIT) && (req->flags & REQ_F_FIXED_FILE); | |
1326 | } | |
1327 | ||
ce3d5aae PB |
1328 | static void io_req_track_inflight(struct io_kiocb *req) |
1329 | { | |
ce3d5aae | 1330 | if (!(req->flags & REQ_F_INFLIGHT)) { |
ce3d5aae | 1331 | req->flags |= REQ_F_INFLIGHT; |
b303fe2e | 1332 | atomic_inc(¤t->io_uring->inflight_tracked); |
ce3d5aae PB |
1333 | } |
1334 | } | |
1335 | ||
906c6caa PB |
1336 | static inline void io_unprep_linked_timeout(struct io_kiocb *req) |
1337 | { | |
1338 | req->flags &= ~REQ_F_LINK_TIMEOUT; | |
1339 | } | |
1340 | ||
fd08e530 PB |
1341 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
1342 | { | |
906c6caa PB |
1343 | if (WARN_ON_ONCE(!req->link)) |
1344 | return NULL; | |
1345 | ||
4d13d1a4 PB |
1346 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
1347 | req->flags |= REQ_F_LINK_TIMEOUT; | |
fd08e530 PB |
1348 | |
1349 | /* linked timeouts should have two refs once prep'ed */ | |
48dcd38d | 1350 | io_req_set_refcount(req); |
4d13d1a4 PB |
1351 | __io_req_set_refcount(req->link, 2); |
1352 | return req->link; | |
fd08e530 PB |
1353 | } |
1354 | ||
1355 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) | |
1356 | { | |
4d13d1a4 | 1357 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
fd08e530 PB |
1358 | return NULL; |
1359 | return __io_prep_linked_timeout(req); | |
1360 | } | |
1361 | ||
1e6fa521 JA |
1362 | static void io_prep_async_work(struct io_kiocb *req) |
1363 | { | |
1364 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1365 | struct io_ring_ctx *ctx = req->ctx; |
1366 | ||
b8e64b53 PB |
1367 | if (!(req->flags & REQ_F_CREDS)) { |
1368 | req->flags |= REQ_F_CREDS; | |
c10d1f98 | 1369 | req->creds = get_current_cred(); |
b8e64b53 | 1370 | } |
003e8dcc | 1371 | |
e1d675df PB |
1372 | req->work.list.next = NULL; |
1373 | req->work.flags = 0; | |
feaadc4f PB |
1374 | if (req->flags & REQ_F_FORCE_ASYNC) |
1375 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1376 | ||
1e6fa521 JA |
1377 | if (req->flags & REQ_F_ISREG) { |
1378 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1379 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
4b982bd0 | 1380 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
1e6fa521 JA |
1381 | if (def->unbound_nonreg_file) |
1382 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1383 | } | |
e1d675df PB |
1384 | |
1385 | switch (req->opcode) { | |
1386 | case IORING_OP_SPLICE: | |
1387 | case IORING_OP_TEE: | |
e1d675df PB |
1388 | if (!S_ISREG(file_inode(req->splice.file_in)->i_mode)) |
1389 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1390 | break; | |
1391 | } | |
561fb04a | 1392 | } |
cccf0ee8 | 1393 | |
cbdcb435 | 1394 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1395 | { |
cbdcb435 | 1396 | struct io_kiocb *cur; |
54a91f3b | 1397 | |
44eff40a PB |
1398 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
1399 | struct io_ring_ctx *ctx = req->ctx; | |
1400 | ||
79ebeaee | 1401 | spin_lock(&ctx->completion_lock); |
44eff40a PB |
1402 | io_for_each_link(cur, req) |
1403 | io_prep_async_work(cur); | |
79ebeaee | 1404 | spin_unlock(&ctx->completion_lock); |
44eff40a PB |
1405 | } else { |
1406 | io_for_each_link(cur, req) | |
1407 | io_prep_async_work(cur); | |
1408 | } | |
561fb04a JA |
1409 | } |
1410 | ||
f237c30a | 1411 | static void io_queue_async_work(struct io_kiocb *req, bool *locked) |
561fb04a | 1412 | { |
a197f664 | 1413 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1414 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1415 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1416 | |
f237c30a PB |
1417 | /* must not take the lock, NULL it as a precaution */ |
1418 | locked = NULL; | |
1419 | ||
3bfe6106 JA |
1420 | BUG_ON(!tctx); |
1421 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1422 | |
cbdcb435 PB |
1423 | /* init ->work of the whole link before punting */ |
1424 | io_prep_async_link(req); | |
991468dc JA |
1425 | |
1426 | /* | |
1427 | * Not expected to happen, but if we do have a bug where this _can_ | |
1428 | * happen, catch it here and ensure the request is marked as | |
1429 | * canceled. That will make io-wq go through the usual work cancel | |
1430 | * procedure rather than attempt to run this request (or create a new | |
1431 | * worker for it). | |
1432 | */ | |
1433 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) | |
1434 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1435 | ||
d07f1e8a PB |
1436 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1437 | &req->work, req->flags); | |
ebf93667 | 1438 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a JA |
1439 | if (link) |
1440 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1441 | } |
1442 | ||
1ee4160c | 1443 | static void io_kill_timeout(struct io_kiocb *req, int status) |
8c855885 | 1444 | __must_hold(&req->ctx->completion_lock) |
89850fce | 1445 | __must_hold(&req->ctx->timeout_lock) |
5262f567 | 1446 | { |
e8c2bc1f | 1447 | struct io_timeout_data *io = req->async_data; |
5262f567 | 1448 | |
fd9c7bc5 | 1449 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
01cec8c1 PB |
1450 | atomic_set(&req->ctx->cq_timeouts, |
1451 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1452 | list_del_init(&req->timeout.list); |
d4d19c19 | 1453 | io_cqring_fill_event(req->ctx, req->user_data, status, 0); |
91c2f697 | 1454 | io_put_req_deferred(req); |
5262f567 JA |
1455 | } |
1456 | } | |
1457 | ||
441b8a78 | 1458 | static void io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1459 | { |
441b8a78 | 1460 | while (!list_empty(&ctx->defer_list)) { |
27dc8338 PB |
1461 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1462 | struct io_defer_entry, list); | |
de0617e4 | 1463 | |
9cf7c104 | 1464 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1465 | break; |
27dc8338 | 1466 | list_del_init(&de->list); |
907d1df3 | 1467 | io_req_task_queue(de->req); |
27dc8338 | 1468 | kfree(de); |
441b8a78 | 1469 | } |
04518945 PB |
1470 | } |
1471 | ||
360428f8 | 1472 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
89850fce | 1473 | __must_hold(&ctx->completion_lock) |
de0617e4 | 1474 | { |
441b8a78 | 1475 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
f010505b | 1476 | |
79ebeaee | 1477 | spin_lock_irq(&ctx->timeout_lock); |
f18ee4cf | 1478 | while (!list_empty(&ctx->timeout_list)) { |
f010505b | 1479 | u32 events_needed, events_got; |
360428f8 | 1480 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1481 | struct io_kiocb, timeout.list); |
de0617e4 | 1482 | |
8eb7e2d0 | 1483 | if (io_is_timeout_noseq(req)) |
360428f8 | 1484 | break; |
f010505b MDG |
1485 | |
1486 | /* | |
1487 | * Since seq can easily wrap around over time, subtract | |
1488 | * the last seq at which timeouts were flushed before comparing. | |
1489 | * Assuming not more than 2^31-1 events have happened since, | |
1490 | * these subtractions won't have wrapped, so we can check if | |
1491 | * target is in [last_seq, current_seq] by comparing the two. | |
1492 | */ | |
1493 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1494 | events_got = seq - ctx->cq_last_tm_flush; | |
1495 | if (events_got < events_needed) | |
360428f8 | 1496 | break; |
bfe68a22 | 1497 | |
135fcde8 | 1498 | list_del_init(&req->timeout.list); |
1ee4160c | 1499 | io_kill_timeout(req, 0); |
f18ee4cf | 1500 | } |
f010505b | 1501 | ctx->cq_last_tm_flush = seq; |
79ebeaee | 1502 | spin_unlock_irq(&ctx->timeout_lock); |
360428f8 | 1503 | } |
5262f567 | 1504 | |
2335f6f5 | 1505 | static void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
360428f8 | 1506 | { |
2335f6f5 PB |
1507 | if (ctx->off_timeout_used) |
1508 | io_flush_timeouts(ctx); | |
1509 | if (ctx->drain_active) | |
1510 | io_queue_deferred(ctx); | |
1511 | } | |
1512 | ||
1513 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) | |
1514 | { | |
1515 | if (unlikely(ctx->off_timeout_used || ctx->drain_active)) | |
1516 | __io_commit_cqring_flush(ctx); | |
ec30e04b PB |
1517 | /* order cqe stores with ring update */ |
1518 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 JA |
1519 | } |
1520 | ||
90554200 JA |
1521 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1522 | { | |
1523 | struct io_rings *r = ctx->rings; | |
1524 | ||
a566c556 | 1525 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries; |
90554200 JA |
1526 | } |
1527 | ||
888aae2e PB |
1528 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1529 | { | |
1530 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1531 | } | |
1532 | ||
d068b506 | 1533 | static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) |
2b188cc1 | 1534 | { |
75b28aff | 1535 | struct io_rings *rings = ctx->rings; |
ea5ab3b5 | 1536 | unsigned tail, mask = ctx->cq_entries - 1; |
2b188cc1 | 1537 | |
115e12e5 SB |
1538 | /* |
1539 | * writes to the cq entry need to come after reading head; the | |
1540 | * control dependency is enough as we're using WRITE_ONCE to | |
1541 | * fill the cq entry | |
1542 | */ | |
a566c556 | 1543 | if (__io_cqring_events(ctx) == ctx->cq_entries) |
2b188cc1 JA |
1544 | return NULL; |
1545 | ||
888aae2e | 1546 | tail = ctx->cached_cq_tail++; |
ea5ab3b5 | 1547 | return &rings->cqes[tail & mask]; |
2b188cc1 JA |
1548 | } |
1549 | ||
f2842ab5 JA |
1550 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1551 | { | |
44c769de | 1552 | if (likely(!ctx->cq_ev_fd)) |
f0b493e6 | 1553 | return false; |
7e55a19c SG |
1554 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1555 | return false; | |
44c769de | 1556 | return !ctx->eventfd_async || io_wq_current_is_worker(); |
f2842ab5 JA |
1557 | } |
1558 | ||
2c5d763c JA |
1559 | /* |
1560 | * This should only get called when at least one event has been posted. | |
1561 | * Some applications rely on the eventfd notification count only changing | |
1562 | * IFF a new CQE has been added to the CQ ring. There's no depedency on | |
1563 | * 1:1 relationship between how many times this function is called (and | |
1564 | * hence the eventfd count) and number of CQEs posted to the CQ ring. | |
1565 | */ | |
b41e9852 | 1566 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1567 | { |
5fd46178 JA |
1568 | /* |
1569 | * wake_up_all() may seem excessive, but io_wake_function() and | |
1570 | * io_should_wake() handle the termination of the loop and only | |
1571 | * wake as many waiters as we need to. | |
1572 | */ | |
1573 | if (wq_has_sleeper(&ctx->cq_wait)) | |
1574 | wake_up_all(&ctx->cq_wait); | |
534ca6d6 JA |
1575 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
1576 | wake_up(&ctx->sq_data->wait); | |
b41e9852 | 1577 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 | 1578 | eventfd_signal(ctx->cq_ev_fd, 1); |
311997b3 PB |
1579 | if (waitqueue_active(&ctx->poll_wait)) { |
1580 | wake_up_interruptible(&ctx->poll_wait); | |
4aa84f2f PB |
1581 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
1582 | } | |
1d7bb1d5 JA |
1583 | } |
1584 | ||
80c18e4a PB |
1585 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1586 | { | |
1587 | if (ctx->flags & IORING_SETUP_SQPOLL) { | |
5fd46178 JA |
1588 | if (wq_has_sleeper(&ctx->cq_wait)) |
1589 | wake_up_all(&ctx->cq_wait); | |
80c18e4a PB |
1590 | } |
1591 | if (io_should_trigger_evfd(ctx)) | |
1592 | eventfd_signal(ctx->cq_ev_fd, 1); | |
311997b3 PB |
1593 | if (waitqueue_active(&ctx->poll_wait)) { |
1594 | wake_up_interruptible(&ctx->poll_wait); | |
4aa84f2f PB |
1595 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
1596 | } | |
80c18e4a PB |
1597 | } |
1598 | ||
c4a2ed72 | 1599 | /* Returns true if there are no backlogged entries after the flush */ |
6c2450ae | 1600 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
1d7bb1d5 | 1601 | { |
b18032bb | 1602 | bool all_flushed, posted; |
1d7bb1d5 | 1603 | |
a566c556 | 1604 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
e23de15f | 1605 | return false; |
1d7bb1d5 | 1606 | |
b18032bb | 1607 | posted = false; |
79ebeaee | 1608 | spin_lock(&ctx->completion_lock); |
6c2450ae | 1609 | while (!list_empty(&ctx->cq_overflow_list)) { |
d068b506 | 1610 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
6c2450ae | 1611 | struct io_overflow_cqe *ocqe; |
e6c8aa9a | 1612 | |
1d7bb1d5 JA |
1613 | if (!cqe && !force) |
1614 | break; | |
6c2450ae PB |
1615 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
1616 | struct io_overflow_cqe, list); | |
1617 | if (cqe) | |
1618 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); | |
1619 | else | |
8f6ed49a PB |
1620 | io_account_cq_overflow(ctx); |
1621 | ||
b18032bb | 1622 | posted = true; |
6c2450ae PB |
1623 | list_del(&ocqe->list); |
1624 | kfree(ocqe); | |
1d7bb1d5 JA |
1625 | } |
1626 | ||
09e88404 PB |
1627 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1628 | if (all_flushed) { | |
5ed7a37d | 1629 | clear_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1630 | WRITE_ONCE(ctx->rings->sq_flags, |
1631 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); | |
09e88404 | 1632 | } |
46930143 | 1633 | |
b18032bb JA |
1634 | if (posted) |
1635 | io_commit_cqring(ctx); | |
79ebeaee | 1636 | spin_unlock(&ctx->completion_lock); |
b18032bb JA |
1637 | if (posted) |
1638 | io_cqring_ev_posted(ctx); | |
09e88404 | 1639 | return all_flushed; |
1d7bb1d5 JA |
1640 | } |
1641 | ||
90f67366 | 1642 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
6c503150 | 1643 | { |
ca0a2651 JA |
1644 | bool ret = true; |
1645 | ||
5ed7a37d | 1646 | if (test_bit(0, &ctx->check_cq_overflow)) { |
6c503150 PB |
1647 | /* iopoll syncs against uring_lock, not completion_lock */ |
1648 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1649 | mutex_lock(&ctx->uring_lock); | |
90f67366 | 1650 | ret = __io_cqring_overflow_flush(ctx, false); |
6c503150 PB |
1651 | if (ctx->flags & IORING_SETUP_IOPOLL) |
1652 | mutex_unlock(&ctx->uring_lock); | |
1653 | } | |
ca0a2651 JA |
1654 | |
1655 | return ret; | |
6c503150 PB |
1656 | } |
1657 | ||
6a290a14 PB |
1658 | /* must to be called somewhat shortly after putting a request */ |
1659 | static inline void io_put_task(struct task_struct *task, int nr) | |
1660 | { | |
1661 | struct io_uring_task *tctx = task->io_uring; | |
1662 | ||
e98e49b2 PB |
1663 | if (likely(task == current)) { |
1664 | tctx->cached_refs += nr; | |
1665 | } else { | |
1666 | percpu_counter_sub(&tctx->inflight, nr); | |
1667 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1668 | wake_up(&tctx->wait); | |
1669 | put_task_struct_many(task, nr); | |
1670 | } | |
6a290a14 PB |
1671 | } |
1672 | ||
d4d19c19 PB |
1673 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
1674 | long res, unsigned int cflags) | |
2b188cc1 | 1675 | { |
cce4b8b0 | 1676 | struct io_overflow_cqe *ocqe; |
2b188cc1 | 1677 | |
cce4b8b0 PB |
1678 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
1679 | if (!ocqe) { | |
1680 | /* | |
1681 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1682 | * or cannot allocate an overflow entry, then we need to drop it | |
1683 | * on the floor. | |
1684 | */ | |
8f6ed49a | 1685 | io_account_cq_overflow(ctx); |
cce4b8b0 | 1686 | return false; |
2b188cc1 | 1687 | } |
cce4b8b0 | 1688 | if (list_empty(&ctx->cq_overflow_list)) { |
5ed7a37d | 1689 | set_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1690 | WRITE_ONCE(ctx->rings->sq_flags, |
1691 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); | |
1692 | ||
cce4b8b0 | 1693 | } |
d4d19c19 | 1694 | ocqe->cqe.user_data = user_data; |
cce4b8b0 PB |
1695 | ocqe->cqe.res = res; |
1696 | ocqe->cqe.flags = cflags; | |
1697 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); | |
1698 | return true; | |
2b188cc1 JA |
1699 | } |
1700 | ||
d4d19c19 PB |
1701 | static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
1702 | long res, unsigned int cflags) | |
2b188cc1 JA |
1703 | { |
1704 | struct io_uring_cqe *cqe; | |
1705 | ||
d4d19c19 | 1706 | trace_io_uring_complete(ctx, user_data, res, cflags); |
51c3ff62 | 1707 | |
2b188cc1 JA |
1708 | /* |
1709 | * If we can't get a cq entry, userspace overflowed the | |
1710 | * submission (by quite a lot). Increment the overflow count in | |
1711 | * the ring. | |
1712 | */ | |
d068b506 | 1713 | cqe = io_get_cqe(ctx); |
1d7bb1d5 | 1714 | if (likely(cqe)) { |
d4d19c19 | 1715 | WRITE_ONCE(cqe->user_data, user_data); |
2b188cc1 | 1716 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1717 | WRITE_ONCE(cqe->flags, cflags); |
8d13326e | 1718 | return true; |
2b188cc1 | 1719 | } |
d4d19c19 | 1720 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
2b188cc1 JA |
1721 | } |
1722 | ||
8d13326e | 1723 | /* not as hot to bloat with inlining */ |
d4d19c19 PB |
1724 | static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
1725 | long res, unsigned int cflags) | |
bcda7baa | 1726 | { |
d4d19c19 | 1727 | return __io_cqring_fill_event(ctx, user_data, res, cflags); |
bcda7baa JA |
1728 | } |
1729 | ||
7a612350 PB |
1730 | static void io_req_complete_post(struct io_kiocb *req, long res, |
1731 | unsigned int cflags) | |
2b188cc1 | 1732 | { |
78e19bbe | 1733 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 | 1734 | |
79ebeaee | 1735 | spin_lock(&ctx->completion_lock); |
d4d19c19 | 1736 | __io_cqring_fill_event(ctx, req->user_data, res, cflags); |
c7dae4ba JA |
1737 | /* |
1738 | * If we're the last reference to this request, add to our locked | |
1739 | * free_list cache. | |
1740 | */ | |
de9b4cca | 1741 | if (req_ref_put_and_test(req)) { |
7a612350 | 1742 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
0756a869 | 1743 | if (req->flags & IO_DISARM_MASK) |
7a612350 PB |
1744 | io_disarm_next(req); |
1745 | if (req->link) { | |
1746 | io_req_task_queue(req->link); | |
1747 | req->link = NULL; | |
1748 | } | |
1749 | } | |
c7dae4ba JA |
1750 | io_dismantle_req(req); |
1751 | io_put_task(req->task, 1); | |
bb943b82 | 1752 | list_add(&req->inflight_entry, &ctx->locked_free_list); |
d0acdee2 | 1753 | ctx->locked_free_nr++; |
180f829f PB |
1754 | } else { |
1755 | if (!percpu_ref_tryget(&ctx->refs)) | |
1756 | req = NULL; | |
1757 | } | |
7a612350 | 1758 | io_commit_cqring(ctx); |
79ebeaee | 1759 | spin_unlock(&ctx->completion_lock); |
7a612350 | 1760 | |
180f829f PB |
1761 | if (req) { |
1762 | io_cqring_ev_posted(ctx); | |
c7dae4ba | 1763 | percpu_ref_put(&ctx->refs); |
180f829f | 1764 | } |
229a7b63 JA |
1765 | } |
1766 | ||
4e3d9ff9 JA |
1767 | static inline bool io_req_needs_clean(struct io_kiocb *req) |
1768 | { | |
c854357b | 1769 | return req->flags & IO_REQ_CLEAN_FLAGS; |
4e3d9ff9 JA |
1770 | } |
1771 | ||
a38d68db | 1772 | static void io_req_complete_state(struct io_kiocb *req, long res, |
889fca73 | 1773 | unsigned int cflags) |
229a7b63 | 1774 | { |
4e3d9ff9 | 1775 | if (io_req_needs_clean(req)) |
68fb8979 | 1776 | io_clean_op(req); |
a38d68db PB |
1777 | req->result = res; |
1778 | req->compl.cflags = cflags; | |
e342c807 | 1779 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
1780 | } |
1781 | ||
889fca73 PB |
1782 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
1783 | long res, unsigned cflags) | |
bcda7baa | 1784 | { |
889fca73 PB |
1785 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
1786 | io_req_complete_state(req, res, cflags); | |
a38d68db | 1787 | else |
c7dae4ba | 1788 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
1789 | } |
1790 | ||
a38d68db | 1791 | static inline void io_req_complete(struct io_kiocb *req, long res) |
0ddf92e8 | 1792 | { |
889fca73 | 1793 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
1794 | } |
1795 | ||
f41db273 PB |
1796 | static void io_req_complete_failed(struct io_kiocb *req, long res) |
1797 | { | |
93d2bcd2 | 1798 | req_set_fail(req); |
f41db273 PB |
1799 | io_req_complete_post(req, res, 0); |
1800 | } | |
1801 | ||
864ea921 PB |
1802 | /* |
1803 | * Don't initialise the fields below on every allocation, but do that in | |
1804 | * advance and keep them valid across allocations. | |
1805 | */ | |
1806 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
1807 | { | |
1808 | req->ctx = ctx; | |
1809 | req->link = NULL; | |
1810 | req->async_data = NULL; | |
1811 | /* not necessary, but safer to zero */ | |
1812 | req->result = 0; | |
1813 | } | |
1814 | ||
dac7a098 | 1815 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
cd0ca2e0 | 1816 | struct io_submit_state *state) |
dac7a098 | 1817 | { |
79ebeaee | 1818 | spin_lock(&ctx->completion_lock); |
cd0ca2e0 | 1819 | list_splice_init(&ctx->locked_free_list, &state->free_list); |
d0acdee2 | 1820 | ctx->locked_free_nr = 0; |
79ebeaee | 1821 | spin_unlock(&ctx->completion_lock); |
dac7a098 PB |
1822 | } |
1823 | ||
dd78f492 | 1824 | /* Returns true IFF there are requests in the cache */ |
c7dae4ba | 1825 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 1826 | { |
c7dae4ba | 1827 | struct io_submit_state *state = &ctx->submit_state; |
dd78f492 | 1828 | int nr; |
0ddf92e8 | 1829 | |
c7dae4ba JA |
1830 | /* |
1831 | * If we have more than a batch's worth of requests in our IRQ side | |
1832 | * locked cache, grab the lock and move them over to our submission | |
1833 | * side cache. | |
1834 | */ | |
d0acdee2 | 1835 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
cd0ca2e0 | 1836 | io_flush_cached_locked_reqs(ctx, state); |
0ddf92e8 | 1837 | |
dd78f492 | 1838 | nr = state->free_reqs; |
cd0ca2e0 PB |
1839 | while (!list_empty(&state->free_list)) { |
1840 | struct io_kiocb *req = list_first_entry(&state->free_list, | |
bb943b82 | 1841 | struct io_kiocb, inflight_entry); |
dd78f492 | 1842 | |
bb943b82 | 1843 | list_del(&req->inflight_entry); |
dd78f492 PB |
1844 | state->reqs[nr++] = req; |
1845 | if (nr == ARRAY_SIZE(state->reqs)) | |
e5d1bc0a | 1846 | break; |
1b4c351f JA |
1847 | } |
1848 | ||
dd78f492 PB |
1849 | state->free_reqs = nr; |
1850 | return nr != 0; | |
0ddf92e8 JA |
1851 | } |
1852 | ||
5d5901a3 PB |
1853 | /* |
1854 | * A request might get retired back into the request caches even before opcode | |
1855 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. | |
1856 | * Because of that, io_alloc_req() should be called only under ->uring_lock | |
1857 | * and with extra caution to not get a request that is still worked on. | |
1858 | */ | |
e5d1bc0a | 1859 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
5d5901a3 | 1860 | __must_hold(&ctx->uring_lock) |
2b188cc1 | 1861 | { |
e5d1bc0a | 1862 | struct io_submit_state *state = &ctx->submit_state; |
864ea921 PB |
1863 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
1864 | int ret, i; | |
e5d1bc0a | 1865 | |
fe7e3257 | 1866 | BUILD_BUG_ON(ARRAY_SIZE(state->reqs) < IO_REQ_ALLOC_BATCH); |
e5d1bc0a | 1867 | |
864ea921 PB |
1868 | if (likely(state->free_reqs || io_flush_cached_reqs(ctx))) |
1869 | goto got_req; | |
e5d1bc0a | 1870 | |
864ea921 PB |
1871 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
1872 | state->reqs); | |
fd6fab2c | 1873 | |
864ea921 PB |
1874 | /* |
1875 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1876 | * retry single alloc to be on the safe side. | |
1877 | */ | |
1878 | if (unlikely(ret <= 0)) { | |
1879 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
1880 | if (!state->reqs[0]) | |
1881 | return NULL; | |
1882 | ret = 1; | |
2b188cc1 | 1883 | } |
864ea921 PB |
1884 | |
1885 | for (i = 0; i < ret; i++) | |
1886 | io_preinit_req(state->reqs[i], ctx); | |
1887 | state->free_reqs = ret; | |
e5d1bc0a | 1888 | got_req: |
291b2821 PB |
1889 | state->free_reqs--; |
1890 | return state->reqs[state->free_reqs]; | |
2b188cc1 JA |
1891 | } |
1892 | ||
e1d767f0 | 1893 | static inline void io_put_file(struct file *file) |
8da11c19 | 1894 | { |
e1d767f0 | 1895 | if (file) |
8da11c19 PB |
1896 | fput(file); |
1897 | } | |
1898 | ||
4edf20f9 | 1899 | static void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 1900 | { |
094bae49 | 1901 | unsigned int flags = req->flags; |
929a3af9 | 1902 | |
3a0a6902 PB |
1903 | if (io_req_needs_clean(req)) |
1904 | io_clean_op(req); | |
e1d767f0 PB |
1905 | if (!(flags & REQ_F_FIXED_FILE)) |
1906 | io_put_file(req->file); | |
269bbe5f BM |
1907 | if (req->fixed_rsrc_refs) |
1908 | percpu_ref_put(req->fixed_rsrc_refs); | |
99ebe4ef | 1909 | if (req->async_data) { |
094bae49 | 1910 | kfree(req->async_data); |
99ebe4ef PB |
1911 | req->async_data = NULL; |
1912 | } | |
e65ef56d JA |
1913 | } |
1914 | ||
216578e5 | 1915 | static void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 1916 | { |
51a4cc11 | 1917 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 1918 | |
216578e5 | 1919 | io_dismantle_req(req); |
7c660731 | 1920 | io_put_task(req->task, 1); |
c6ca97b3 | 1921 | |
79ebeaee | 1922 | spin_lock(&ctx->completion_lock); |
bb943b82 | 1923 | list_add(&req->inflight_entry, &ctx->locked_free_list); |
c34b025f | 1924 | ctx->locked_free_nr++; |
79ebeaee | 1925 | spin_unlock(&ctx->completion_lock); |
c34b025f | 1926 | |
ecfc5177 | 1927 | percpu_ref_put(&ctx->refs); |
e65ef56d JA |
1928 | } |
1929 | ||
f2f87370 PB |
1930 | static inline void io_remove_next_linked(struct io_kiocb *req) |
1931 | { | |
1932 | struct io_kiocb *nxt = req->link; | |
1933 | ||
1934 | req->link = nxt->link; | |
1935 | nxt->link = NULL; | |
1936 | } | |
1937 | ||
33cc89a9 PB |
1938 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
1939 | __must_hold(&req->ctx->completion_lock) | |
89b263f6 | 1940 | __must_hold(&req->ctx->timeout_lock) |
2665abfd | 1941 | { |
33cc89a9 | 1942 | struct io_kiocb *link = req->link; |
f2f87370 | 1943 | |
b97e736a | 1944 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
c9abd7ad | 1945 | struct io_timeout_data *io = link->async_data; |
7c86ffee | 1946 | |
f2f87370 | 1947 | io_remove_next_linked(req); |
90cd7e42 | 1948 | link->timeout.head = NULL; |
fd9c7bc5 | 1949 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
d4d19c19 PB |
1950 | io_cqring_fill_event(link->ctx, link->user_data, |
1951 | -ECANCELED, 0); | |
91c2f697 | 1952 | io_put_req_deferred(link); |
d4729fbd | 1953 | return true; |
c9abd7ad PB |
1954 | } |
1955 | } | |
d4729fbd | 1956 | return false; |
7c86ffee PB |
1957 | } |
1958 | ||
d148ca4b | 1959 | static void io_fail_links(struct io_kiocb *req) |
33cc89a9 | 1960 | __must_hold(&req->ctx->completion_lock) |
9e645e11 | 1961 | { |
33cc89a9 | 1962 | struct io_kiocb *nxt, *link = req->link; |
9e645e11 | 1963 | |
f2f87370 | 1964 | req->link = NULL; |
f2f87370 | 1965 | while (link) { |
a8295b98 HX |
1966 | long res = -ECANCELED; |
1967 | ||
1968 | if (link->flags & REQ_F_FAIL) | |
1969 | res = link->result; | |
1970 | ||
f2f87370 PB |
1971 | nxt = link->link; |
1972 | link->link = NULL; | |
2665abfd | 1973 | |
f2f87370 | 1974 | trace_io_uring_fail_link(req, link); |
a8295b98 | 1975 | io_cqring_fill_event(link->ctx, link->user_data, res, 0); |
91c2f697 | 1976 | io_put_req_deferred(link); |
f2f87370 | 1977 | link = nxt; |
9e645e11 | 1978 | } |
33cc89a9 | 1979 | } |
9e645e11 | 1980 | |
33cc89a9 PB |
1981 | static bool io_disarm_next(struct io_kiocb *req) |
1982 | __must_hold(&req->ctx->completion_lock) | |
1983 | { | |
1984 | bool posted = false; | |
1985 | ||
0756a869 PB |
1986 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
1987 | struct io_kiocb *link = req->link; | |
1988 | ||
906c6caa | 1989 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
0756a869 PB |
1990 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
1991 | io_remove_next_linked(req); | |
1992 | io_cqring_fill_event(link->ctx, link->user_data, | |
1993 | -ECANCELED, 0); | |
1994 | io_put_req_deferred(link); | |
1995 | posted = true; | |
1996 | } | |
1997 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { | |
89b263f6 JA |
1998 | struct io_ring_ctx *ctx = req->ctx; |
1999 | ||
2000 | spin_lock_irq(&ctx->timeout_lock); | |
33cc89a9 | 2001 | posted = io_kill_linked_timeout(req); |
89b263f6 JA |
2002 | spin_unlock_irq(&ctx->timeout_lock); |
2003 | } | |
93d2bcd2 | 2004 | if (unlikely((req->flags & REQ_F_FAIL) && |
e4335ed3 | 2005 | !(req->flags & REQ_F_HARDLINK))) { |
33cc89a9 PB |
2006 | posted |= (req->link != NULL); |
2007 | io_fail_links(req); | |
2008 | } | |
2009 | return posted; | |
9e645e11 JA |
2010 | } |
2011 | ||
3fa5e0f3 | 2012 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
c69f8dbe | 2013 | { |
33cc89a9 | 2014 | struct io_kiocb *nxt; |
944e58bf | 2015 | |
9e645e11 JA |
2016 | /* |
2017 | * If LINK is set, we have dependent requests in this chain. If we | |
2018 | * didn't fail this request, queue the first one up, moving any other | |
2019 | * dependencies to the next request. In case of failure, fail the rest | |
2020 | * of the chain. | |
2021 | */ | |
0756a869 | 2022 | if (req->flags & IO_DISARM_MASK) { |
33cc89a9 | 2023 | struct io_ring_ctx *ctx = req->ctx; |
33cc89a9 PB |
2024 | bool posted; |
2025 | ||
79ebeaee | 2026 | spin_lock(&ctx->completion_lock); |
33cc89a9 PB |
2027 | posted = io_disarm_next(req); |
2028 | if (posted) | |
2029 | io_commit_cqring(req->ctx); | |
79ebeaee | 2030 | spin_unlock(&ctx->completion_lock); |
33cc89a9 PB |
2031 | if (posted) |
2032 | io_cqring_ev_posted(ctx); | |
f2f87370 | 2033 | } |
33cc89a9 PB |
2034 | nxt = req->link; |
2035 | req->link = NULL; | |
2036 | return nxt; | |
4d7dd462 | 2037 | } |
9e645e11 | 2038 | |
f2f87370 | 2039 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
3fa5e0f3 | 2040 | { |
cdbff982 | 2041 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
3fa5e0f3 PB |
2042 | return NULL; |
2043 | return __io_req_find_next(req); | |
2044 | } | |
2045 | ||
f237c30a | 2046 | static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked) |
2c32395d PB |
2047 | { |
2048 | if (!ctx) | |
2049 | return; | |
f237c30a | 2050 | if (*locked) { |
99c8bc52 HX |
2051 | if (ctx->submit_state.compl_nr) |
2052 | io_submit_flush_completions(ctx); | |
2c32395d | 2053 | mutex_unlock(&ctx->uring_lock); |
f237c30a | 2054 | *locked = false; |
2c32395d PB |
2055 | } |
2056 | percpu_ref_put(&ctx->refs); | |
2057 | } | |
2058 | ||
7cbf1722 | 2059 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 2060 | { |
f237c30a | 2061 | bool locked = false; |
ebd0df2e | 2062 | struct io_ring_ctx *ctx = NULL; |
3f18407d PB |
2063 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
2064 | task_work); | |
c40f6379 | 2065 | |
16f72070 | 2066 | while (1) { |
3f18407d PB |
2067 | struct io_wq_work_node *node; |
2068 | ||
2069 | spin_lock_irq(&tctx->task_lock); | |
c6538be9 | 2070 | node = tctx->task_list.first; |
3f18407d | 2071 | INIT_WQ_LIST(&tctx->task_list); |
6294f368 PB |
2072 | if (!node) |
2073 | tctx->task_running = false; | |
3f18407d | 2074 | spin_unlock_irq(&tctx->task_lock); |
6294f368 PB |
2075 | if (!node) |
2076 | break; | |
3f18407d | 2077 | |
6294f368 | 2078 | do { |
3f18407d PB |
2079 | struct io_wq_work_node *next = node->next; |
2080 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2081 | io_task_work.node); | |
2082 | ||
2083 | if (req->ctx != ctx) { | |
f237c30a | 2084 | ctx_flush_and_put(ctx, &locked); |
3f18407d | 2085 | ctx = req->ctx; |
126180b9 PB |
2086 | /* if not contended, grab and improve batching */ |
2087 | locked = mutex_trylock(&ctx->uring_lock); | |
3f18407d PB |
2088 | percpu_ref_get(&ctx->refs); |
2089 | } | |
f237c30a | 2090 | req->io_task_work.func(req, &locked); |
3f18407d | 2091 | node = next; |
6294f368 PB |
2092 | } while (node); |
2093 | ||
7cbf1722 | 2094 | cond_resched(); |
3f18407d | 2095 | } |
ebd0df2e | 2096 | |
f237c30a | 2097 | ctx_flush_and_put(ctx, &locked); |
7cbf1722 JA |
2098 | } |
2099 | ||
e09ee510 | 2100 | static void io_req_task_work_add(struct io_kiocb *req) |
7cbf1722 | 2101 | { |
c15b79de | 2102 | struct task_struct *tsk = req->task; |
7cbf1722 | 2103 | struct io_uring_task *tctx = tsk->io_uring; |
c15b79de | 2104 | enum task_work_notify_mode notify; |
e09ee510 | 2105 | struct io_wq_work_node *node; |
0b81e80c | 2106 | unsigned long flags; |
6294f368 | 2107 | bool running; |
7cbf1722 JA |
2108 | |
2109 | WARN_ON_ONCE(!tctx); | |
2110 | ||
0b81e80c | 2111 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 | 2112 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
6294f368 PB |
2113 | running = tctx->task_running; |
2114 | if (!running) | |
2115 | tctx->task_running = true; | |
0b81e80c | 2116 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2117 | |
2118 | /* task_work already pending, we're done */ | |
6294f368 | 2119 | if (running) |
e09ee510 | 2120 | return; |
7cbf1722 | 2121 | |
c15b79de PB |
2122 | /* |
2123 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For | |
2124 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2125 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2126 | * will do the job. | |
2127 | */ | |
2128 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; | |
c15b79de PB |
2129 | if (!task_work_add(tsk, &tctx->task_work, notify)) { |
2130 | wake_up_process(tsk); | |
e09ee510 | 2131 | return; |
c15b79de | 2132 | } |
2215bed9 | 2133 | |
0b81e80c | 2134 | spin_lock_irqsave(&tctx->task_lock, flags); |
6294f368 | 2135 | tctx->task_running = false; |
e09ee510 PB |
2136 | node = tctx->task_list.first; |
2137 | INIT_WQ_LIST(&tctx->task_list); | |
0b81e80c | 2138 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 | 2139 | |
e09ee510 PB |
2140 | while (node) { |
2141 | req = container_of(node, struct io_kiocb, io_task_work.node); | |
2142 | node = node->next; | |
2143 | if (llist_add(&req->io_task_work.fallback_node, | |
2144 | &req->ctx->fallback_llist)) | |
2145 | schedule_delayed_work(&req->ctx->fallback_work, 1); | |
2146 | } | |
eab30c4d PB |
2147 | } |
2148 | ||
f237c30a | 2149 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
c40f6379 | 2150 | { |
87ceb6a6 | 2151 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2152 | |
e83acd7d | 2153 | /* ctx is guaranteed to stay alive while we hold uring_lock */ |
f237c30a | 2154 | io_tw_lock(ctx, locked); |
2593553a | 2155 | io_req_complete_failed(req, req->result); |
c40f6379 JA |
2156 | } |
2157 | ||
f237c30a | 2158 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
c40f6379 JA |
2159 | { |
2160 | struct io_ring_ctx *ctx = req->ctx; | |
2161 | ||
04fc6c80 | 2162 | /* ctx stays valid until unlock, even if we drop all ours ctx->refs */ |
f237c30a | 2163 | io_tw_lock(ctx, locked); |
316319e8 | 2164 | /* req->task == current here, checking PF_EXITING is safe */ |
af066f31 | 2165 | if (likely(!(req->task->flags & PF_EXITING))) |
c5eef2b9 | 2166 | __io_queue_sqe(req); |
81b6d05c | 2167 | else |
2593553a | 2168 | io_req_complete_failed(req, -EFAULT); |
c40f6379 JA |
2169 | } |
2170 | ||
2c4b8eb6 | 2171 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
c40f6379 | 2172 | { |
2c4b8eb6 | 2173 | req->result = ret; |
5b0a6acc | 2174 | req->io_task_work.func = io_req_task_cancel; |
e09ee510 | 2175 | io_req_task_work_add(req); |
c40f6379 JA |
2176 | } |
2177 | ||
2c4b8eb6 | 2178 | static void io_req_task_queue(struct io_kiocb *req) |
a3df7698 | 2179 | { |
5b0a6acc | 2180 | req->io_task_work.func = io_req_task_submit; |
e09ee510 | 2181 | io_req_task_work_add(req); |
a3df7698 PB |
2182 | } |
2183 | ||
773af691 JA |
2184 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
2185 | { | |
2186 | req->io_task_work.func = io_queue_async_work; | |
2187 | io_req_task_work_add(req); | |
2188 | } | |
2189 | ||
f2f87370 | 2190 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2191 | { |
9b5f7bd9 | 2192 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2193 | |
2194 | if (nxt) | |
906a8c3f | 2195 | io_req_task_queue(nxt); |
c69f8dbe JL |
2196 | } |
2197 | ||
c3524383 | 2198 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2199 | { |
c3524383 PB |
2200 | io_queue_next(req); |
2201 | __io_free_req(req); | |
2202 | } | |
8766dd51 | 2203 | |
f237c30a PB |
2204 | static void io_free_req_work(struct io_kiocb *req, bool *locked) |
2205 | { | |
2206 | io_free_req(req); | |
2207 | } | |
2208 | ||
2d6500d4 | 2209 | struct req_batch { |
5af1d13e PB |
2210 | struct task_struct *task; |
2211 | int task_refs; | |
1b4c351f | 2212 | int ctx_refs; |
2d6500d4 PB |
2213 | }; |
2214 | ||
5af1d13e PB |
2215 | static inline void io_init_req_batch(struct req_batch *rb) |
2216 | { | |
5af1d13e | 2217 | rb->task_refs = 0; |
9ae72463 | 2218 | rb->ctx_refs = 0; |
5af1d13e PB |
2219 | rb->task = NULL; |
2220 | } | |
2221 | ||
2d6500d4 PB |
2222 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
2223 | struct req_batch *rb) | |
2224 | { | |
9ae72463 PB |
2225 | if (rb->ctx_refs) |
2226 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); | |
e98e49b2 | 2227 | if (rb->task) |
e9dbe221 | 2228 | io_put_task(rb->task, rb->task_refs); |
2d6500d4 PB |
2229 | } |
2230 | ||
6ff119a6 PB |
2231 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
2232 | struct io_submit_state *state) | |
2d6500d4 | 2233 | { |
f2f87370 | 2234 | io_queue_next(req); |
96670657 | 2235 | io_dismantle_req(req); |
2d6500d4 | 2236 | |
e3bc8e9d | 2237 | if (req->task != rb->task) { |
7c660731 PB |
2238 | if (rb->task) |
2239 | io_put_task(rb->task, rb->task_refs); | |
e3bc8e9d JA |
2240 | rb->task = req->task; |
2241 | rb->task_refs = 0; | |
5af1d13e | 2242 | } |
e3bc8e9d | 2243 | rb->task_refs++; |
9ae72463 | 2244 | rb->ctx_refs++; |
5af1d13e | 2245 | |
bd759045 | 2246 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
6ff119a6 | 2247 | state->reqs[state->free_reqs++] = req; |
bd759045 | 2248 | else |
cd0ca2e0 | 2249 | list_add(&req->inflight_entry, &state->free_list); |
7a743e22 PB |
2250 | } |
2251 | ||
2a2758f2 | 2252 | static void io_submit_flush_completions(struct io_ring_ctx *ctx) |
a141dd89 | 2253 | __must_hold(&ctx->uring_lock) |
905c172f | 2254 | { |
cd0ca2e0 PB |
2255 | struct io_submit_state *state = &ctx->submit_state; |
2256 | int i, nr = state->compl_nr; | |
905c172f PB |
2257 | struct req_batch rb; |
2258 | ||
79ebeaee | 2259 | spin_lock(&ctx->completion_lock); |
905c172f | 2260 | for (i = 0; i < nr; i++) { |
cd0ca2e0 | 2261 | struct io_kiocb *req = state->compl_reqs[i]; |
5182ed2e | 2262 | |
d4d19c19 PB |
2263 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
2264 | req->compl.cflags); | |
905c172f PB |
2265 | } |
2266 | io_commit_cqring(ctx); | |
79ebeaee | 2267 | spin_unlock(&ctx->completion_lock); |
905c172f | 2268 | io_cqring_ev_posted(ctx); |
5182ed2e PB |
2269 | |
2270 | io_init_req_batch(&rb); | |
905c172f | 2271 | for (i = 0; i < nr; i++) { |
cd0ca2e0 | 2272 | struct io_kiocb *req = state->compl_reqs[i]; |
905c172f | 2273 | |
91c2f697 | 2274 | if (req_ref_put_and_test(req)) |
6ff119a6 | 2275 | io_req_free_batch(&rb, req, &ctx->submit_state); |
905c172f PB |
2276 | } |
2277 | ||
2278 | io_req_free_batch_finish(ctx, &rb); | |
cd0ca2e0 | 2279 | state->compl_nr = 0; |
7a743e22 PB |
2280 | } |
2281 | ||
ba816ad6 JA |
2282 | /* |
2283 | * Drop reference to request, return next in chain (if there is one) if this | |
2284 | * was the last reference to this request. | |
2285 | */ | |
0d85035a | 2286 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2287 | { |
9b5f7bd9 PB |
2288 | struct io_kiocb *nxt = NULL; |
2289 | ||
de9b4cca | 2290 | if (req_ref_put_and_test(req)) { |
9b5f7bd9 | 2291 | nxt = io_req_find_next(req); |
4d7dd462 | 2292 | __io_free_req(req); |
2a44f467 | 2293 | } |
9b5f7bd9 | 2294 | return nxt; |
2b188cc1 JA |
2295 | } |
2296 | ||
0d85035a | 2297 | static inline void io_put_req(struct io_kiocb *req) |
e65ef56d | 2298 | { |
de9b4cca | 2299 | if (req_ref_put_and_test(req)) |
e65ef56d | 2300 | io_free_req(req); |
2b188cc1 JA |
2301 | } |
2302 | ||
91c2f697 | 2303 | static inline void io_put_req_deferred(struct io_kiocb *req) |
216578e5 | 2304 | { |
91c2f697 | 2305 | if (req_ref_put_and_test(req)) { |
f237c30a | 2306 | req->io_task_work.func = io_free_req_work; |
543af3a1 PB |
2307 | io_req_task_work_add(req); |
2308 | } | |
216578e5 PB |
2309 | } |
2310 | ||
6c503150 | 2311 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2312 | { |
2313 | /* See comment at the top of this file */ | |
2314 | smp_rmb(); | |
e23de15f | 2315 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2316 | } |
2317 | ||
fb5ccc98 PB |
2318 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2319 | { | |
2320 | struct io_rings *rings = ctx->rings; | |
2321 | ||
2322 | /* make sure SQ entry isn't read before tail */ | |
2323 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2324 | } | |
2325 | ||
8ff069bf | 2326 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2327 | { |
8ff069bf | 2328 | unsigned int cflags; |
e94f141b | 2329 | |
bcda7baa JA |
2330 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2331 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2332 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2333 | kfree(kbuf); |
2334 | return cflags; | |
e94f141b JA |
2335 | } |
2336 | ||
8ff069bf | 2337 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2338 | { |
4d954c25 | 2339 | struct io_buffer *kbuf; |
bcda7baa | 2340 | |
ae421d93 PB |
2341 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
2342 | return 0; | |
4d954c25 | 2343 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
8ff069bf PB |
2344 | return io_put_kbuf(req, kbuf); |
2345 | } | |
2346 | ||
4c6e277c JA |
2347 | static inline bool io_run_task_work(void) |
2348 | { | |
ef98eb04 | 2349 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) { |
4c6e277c | 2350 | __set_current_state(TASK_RUNNING); |
ef98eb04 | 2351 | tracehook_notify_signal(); |
4c6e277c JA |
2352 | return true; |
2353 | } | |
2354 | ||
2355 | return false; | |
bcda7baa JA |
2356 | } |
2357 | ||
def596e9 JA |
2358 | /* |
2359 | * Find and free completed poll iocbs | |
2360 | */ | |
2361 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
a8576af9 | 2362 | struct list_head *done) |
def596e9 | 2363 | { |
8237e045 | 2364 | struct req_batch rb; |
def596e9 | 2365 | struct io_kiocb *req; |
bbde017a XW |
2366 | |
2367 | /* order with ->result store in io_complete_rw_iopoll() */ | |
2368 | smp_rmb(); | |
def596e9 | 2369 | |
5af1d13e | 2370 | io_init_req_batch(&rb); |
def596e9 | 2371 | while (!list_empty(done)) { |
d21ffe7e | 2372 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
f161340d PB |
2373 | list_del(&req->inflight_entry); |
2374 | ||
a8576af9 | 2375 | if (READ_ONCE(req->result) == -EAGAIN && |
8c130827 | 2376 | !(req->flags & REQ_F_DONT_REISSUE)) { |
bbde017a | 2377 | req->iopoll_completed = 0; |
773af691 | 2378 | io_req_task_queue_reissue(req); |
8c130827 | 2379 | continue; |
bbde017a | 2380 | } |
def596e9 | 2381 | |
ae421d93 PB |
2382 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
2383 | io_put_rw_kbuf(req)); | |
def596e9 JA |
2384 | (*nr_events)++; |
2385 | ||
de9b4cca | 2386 | if (req_ref_put_and_test(req)) |
6ff119a6 | 2387 | io_req_free_batch(&rb, req, &ctx->submit_state); |
def596e9 | 2388 | } |
def596e9 | 2389 | |
09bb8394 | 2390 | io_commit_cqring(ctx); |
80c18e4a | 2391 | io_cqring_ev_posted_iopoll(ctx); |
2d6500d4 | 2392 | io_req_free_batch_finish(ctx, &rb); |
581f9810 BM |
2393 | } |
2394 | ||
def596e9 | 2395 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
a8576af9 | 2396 | long min) |
def596e9 JA |
2397 | { |
2398 | struct io_kiocb *req, *tmp; | |
2399 | LIST_HEAD(done); | |
2400 | bool spin; | |
def596e9 JA |
2401 | |
2402 | /* | |
2403 | * Only spin for completions if we don't have multiple devices hanging | |
2404 | * off our complete list, and we're under the requested amount. | |
2405 | */ | |
915b3dde | 2406 | spin = !ctx->poll_multi_queue && *nr_events < min; |
def596e9 | 2407 | |
d21ffe7e | 2408 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
9adbd45d | 2409 | struct kiocb *kiocb = &req->rw.kiocb; |
a2416e1e | 2410 | int ret; |
def596e9 JA |
2411 | |
2412 | /* | |
581f9810 BM |
2413 | * Move completed and retryable entries to our local lists. |
2414 | * If we find a request that requires polling, break out | |
2415 | * and complete those lists first, if we have entries there. | |
def596e9 | 2416 | */ |
65a6543d | 2417 | if (READ_ONCE(req->iopoll_completed)) { |
d21ffe7e | 2418 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2419 | continue; |
2420 | } | |
2421 | if (!list_empty(&done)) | |
2422 | break; | |
2423 | ||
2424 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
a2416e1e PB |
2425 | if (unlikely(ret < 0)) |
2426 | return ret; | |
2427 | else if (ret) | |
2428 | spin = false; | |
def596e9 | 2429 | |
3aadc23e PB |
2430 | /* iopoll may have completed current req */ |
2431 | if (READ_ONCE(req->iopoll_completed)) | |
d21ffe7e | 2432 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2433 | } |
2434 | ||
2435 | if (!list_empty(&done)) | |
a8576af9 | 2436 | io_iopoll_complete(ctx, nr_events, &done); |
def596e9 | 2437 | |
a2416e1e | 2438 | return 0; |
def596e9 JA |
2439 | } |
2440 | ||
def596e9 JA |
2441 | /* |
2442 | * We can't just wait for polled events to come to us, we have to actively | |
2443 | * find and complete them. | |
2444 | */ | |
b2edc0a7 | 2445 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2446 | { |
2447 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2448 | return; | |
2449 | ||
2450 | mutex_lock(&ctx->uring_lock); | |
540e32a0 | 2451 | while (!list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2452 | unsigned int nr_events = 0; |
2453 | ||
a8576af9 | 2454 | io_do_iopoll(ctx, &nr_events, 0); |
08f5439f | 2455 | |
b2edc0a7 PB |
2456 | /* let it sleep and repeat later if can't complete a request */ |
2457 | if (nr_events == 0) | |
2458 | break; | |
08f5439f JA |
2459 | /* |
2460 | * Ensure we allow local-to-the-cpu processing to take place, | |
2461 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2462 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2463 | */ |
3fcee5a6 PB |
2464 | if (need_resched()) { |
2465 | mutex_unlock(&ctx->uring_lock); | |
2466 | cond_resched(); | |
2467 | mutex_lock(&ctx->uring_lock); | |
2468 | } | |
def596e9 JA |
2469 | } |
2470 | mutex_unlock(&ctx->uring_lock); | |
2471 | } | |
2472 | ||
7668b92a | 2473 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2474 | { |
7668b92a | 2475 | unsigned int nr_events = 0; |
e9979b36 | 2476 | int ret = 0; |
500f9fba | 2477 | |
c7849be9 XW |
2478 | /* |
2479 | * We disallow the app entering submit/complete with polling, but we | |
2480 | * still need to lock the ring to prevent racing with polled issue | |
2481 | * that got punted to a workqueue. | |
2482 | */ | |
2483 | mutex_lock(&ctx->uring_lock); | |
f39c8a5b PB |
2484 | /* |
2485 | * Don't enter poll loop if we already have events pending. | |
2486 | * If we do, we can potentially be spinning for commands that | |
2487 | * already triggered a CQE (eg in error). | |
2488 | */ | |
5ed7a37d | 2489 | if (test_bit(0, &ctx->check_cq_overflow)) |
f39c8a5b PB |
2490 | __io_cqring_overflow_flush(ctx, false); |
2491 | if (io_cqring_events(ctx)) | |
2492 | goto out; | |
def596e9 | 2493 | do { |
500f9fba JA |
2494 | /* |
2495 | * If a submit got punted to a workqueue, we can have the | |
2496 | * application entering polling for a command before it gets | |
2497 | * issued. That app will hold the uring_lock for the duration | |
2498 | * of the poll right here, so we need to take a breather every | |
2499 | * now and then to ensure that the issue has a chance to add | |
2500 | * the poll to the issued list. Otherwise we can spin here | |
2501 | * forever, while the workqueue is stuck trying to acquire the | |
2502 | * very same mutex. | |
2503 | */ | |
e9979b36 | 2504 | if (list_empty(&ctx->iopoll_list)) { |
8f487ef2 PB |
2505 | u32 tail = ctx->cached_cq_tail; |
2506 | ||
500f9fba | 2507 | mutex_unlock(&ctx->uring_lock); |
4c6e277c | 2508 | io_run_task_work(); |
500f9fba | 2509 | mutex_lock(&ctx->uring_lock); |
def596e9 | 2510 | |
8f487ef2 PB |
2511 | /* some requests don't go through iopoll_list */ |
2512 | if (tail != ctx->cached_cq_tail || | |
2513 | list_empty(&ctx->iopoll_list)) | |
e9979b36 | 2514 | break; |
500f9fba | 2515 | } |
a8576af9 | 2516 | ret = io_do_iopoll(ctx, &nr_events, min); |
f39c8a5b PB |
2517 | } while (!ret && nr_events < min && !need_resched()); |
2518 | out: | |
500f9fba | 2519 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2520 | return ret; |
2521 | } | |
2522 | ||
491381ce | 2523 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2524 | { |
491381ce JA |
2525 | /* |
2526 | * Tell lockdep we inherited freeze protection from submission | |
2527 | * thread. | |
2528 | */ | |
2529 | if (req->flags & REQ_F_ISREG) { | |
1c98679d | 2530 | struct super_block *sb = file_inode(req->file)->i_sb; |
2b188cc1 | 2531 | |
1c98679d PB |
2532 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
2533 | sb_end_write(sb); | |
2b188cc1 JA |
2534 | } |
2535 | } | |
2536 | ||
b63534c4 | 2537 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2538 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 | 2539 | { |
ab454438 | 2540 | struct io_async_rw *rw = req->async_data; |
b63534c4 | 2541 | |
ab454438 PB |
2542 | if (!rw) |
2543 | return !io_req_prep_async(req); | |
2544 | /* may have left rw->iter inconsistent on -EIOCBQUEUED */ | |
2545 | iov_iter_revert(&rw->iter, req->result - iov_iter_count(&rw->iter)); | |
2546 | return true; | |
b63534c4 | 2547 | } |
b63534c4 | 2548 | |
3e6a0d3c | 2549 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 2550 | { |
355afaeb | 2551 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 2552 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 2553 | |
355afaeb JA |
2554 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2555 | return false; | |
3e6a0d3c JA |
2556 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
2557 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 2558 | return false; |
7c977a58 JA |
2559 | /* |
2560 | * If ref is dying, we might be running poll reap from the exit work. | |
2561 | * Don't attempt to reissue from that path, just let it fail with | |
2562 | * -EAGAIN. | |
2563 | */ | |
3e6a0d3c JA |
2564 | if (percpu_ref_is_dying(&ctx->refs)) |
2565 | return false; | |
ef046888 JA |
2566 | /* |
2567 | * Play it safe and assume not safe to re-import and reissue if we're | |
2568 | * not in the original thread group (or in task context). | |
2569 | */ | |
2570 | if (!same_thread_group(req->task, current) || !in_task()) | |
2571 | return false; | |
3e6a0d3c JA |
2572 | return true; |
2573 | } | |
e82ad485 | 2574 | #else |
a1ff1e3f | 2575 | static bool io_resubmit_prep(struct io_kiocb *req) |
e82ad485 JA |
2576 | { |
2577 | return false; | |
2578 | } | |
e82ad485 | 2579 | static bool io_rw_should_reissue(struct io_kiocb *req) |
3e6a0d3c | 2580 | { |
b63534c4 JA |
2581 | return false; |
2582 | } | |
3e6a0d3c | 2583 | #endif |
b63534c4 | 2584 | |
8ef12efe | 2585 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
a1d7c393 | 2586 | { |
b65c128f PB |
2587 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
2588 | kiocb_end_write(req); | |
9532b99b PB |
2589 | if (res != req->result) { |
2590 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && | |
2591 | io_rw_should_reissue(req)) { | |
2592 | req->flags |= REQ_F_REISSUE; | |
8ef12efe | 2593 | return true; |
9532b99b | 2594 | } |
93d2bcd2 | 2595 | req_set_fail(req); |
8ef12efe | 2596 | req->result = res; |
9532b99b | 2597 | } |
8ef12efe JA |
2598 | return false; |
2599 | } | |
2600 | ||
f237c30a | 2601 | static void io_req_task_complete(struct io_kiocb *req, bool *locked) |
8ef12efe | 2602 | { |
126180b9 PB |
2603 | unsigned int cflags = io_put_rw_kbuf(req); |
2604 | long res = req->result; | |
2605 | ||
2606 | if (*locked) { | |
2607 | struct io_ring_ctx *ctx = req->ctx; | |
2608 | struct io_submit_state *state = &ctx->submit_state; | |
2609 | ||
2610 | io_req_complete_state(req, res, cflags); | |
2611 | state->compl_reqs[state->compl_nr++] = req; | |
2612 | if (state->compl_nr == ARRAY_SIZE(state->compl_reqs)) | |
2613 | io_submit_flush_completions(ctx); | |
2614 | } else { | |
2615 | io_req_complete_post(req, res, cflags); | |
2616 | } | |
8ef12efe JA |
2617 | } |
2618 | ||
2619 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, | |
2620 | unsigned int issue_flags) | |
2621 | { | |
2622 | if (__io_complete_rw_common(req, res)) | |
2623 | return; | |
f237c30a | 2624 | __io_req_complete(req, 0, req->result, io_put_rw_kbuf(req)); |
ba816ad6 JA |
2625 | } |
2626 | ||
2627 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2628 | { | |
9adbd45d | 2629 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2630 | |
8ef12efe JA |
2631 | if (__io_complete_rw_common(req, res)) |
2632 | return; | |
2633 | req->result = res; | |
2634 | req->io_task_work.func = io_req_task_complete; | |
2635 | io_req_task_work_add(req); | |
2b188cc1 JA |
2636 | } |
2637 | ||
def596e9 JA |
2638 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2639 | { | |
9adbd45d | 2640 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2641 | |
491381ce JA |
2642 | if (kiocb->ki_flags & IOCB_WRITE) |
2643 | kiocb_end_write(req); | |
9532b99b | 2644 | if (unlikely(res != req->result)) { |
a1ff1e3f JA |
2645 | if (!(res == -EAGAIN && io_rw_should_reissue(req) && |
2646 | io_resubmit_prep(req))) { | |
93d2bcd2 | 2647 | req_set_fail(req); |
9532b99b PB |
2648 | req->flags |= REQ_F_DONT_REISSUE; |
2649 | } | |
8c130827 | 2650 | } |
bbde017a XW |
2651 | |
2652 | WRITE_ONCE(req->result, res); | |
b9b0e0d3 | 2653 | /* order with io_iopoll_complete() checking ->result */ |
cd664b0e PB |
2654 | smp_wmb(); |
2655 | WRITE_ONCE(req->iopoll_completed, 1); | |
def596e9 JA |
2656 | } |
2657 | ||
2658 | /* | |
2659 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2660 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
f39c8a5b | 2661 | * find it from a io_do_iopoll() thread before the issuer is done |
def596e9 JA |
2662 | * accessing the kiocb cookie. |
2663 | */ | |
cb3d8972 | 2664 | static void io_iopoll_req_issued(struct io_kiocb *req) |
def596e9 JA |
2665 | { |
2666 | struct io_ring_ctx *ctx = req->ctx; | |
cb3d8972 PB |
2667 | const bool in_async = io_wq_current_is_worker(); |
2668 | ||
2669 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
2670 | if (unlikely(in_async)) | |
2671 | mutex_lock(&ctx->uring_lock); | |
def596e9 JA |
2672 | |
2673 | /* | |
2674 | * Track whether we have multiple files in our lists. This will impact | |
2675 | * how we do polling eventually, not spinning if we're on potentially | |
2676 | * different devices. | |
2677 | */ | |
540e32a0 | 2678 | if (list_empty(&ctx->iopoll_list)) { |
915b3dde HX |
2679 | ctx->poll_multi_queue = false; |
2680 | } else if (!ctx->poll_multi_queue) { | |
def596e9 | 2681 | struct io_kiocb *list_req; |
915b3dde | 2682 | unsigned int queue_num0, queue_num1; |
def596e9 | 2683 | |
540e32a0 | 2684 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
d21ffe7e | 2685 | inflight_entry); |
915b3dde HX |
2686 | |
2687 | if (list_req->file != req->file) { | |
2688 | ctx->poll_multi_queue = true; | |
2689 | } else { | |
2690 | queue_num0 = blk_qc_t_to_queue_num(list_req->rw.kiocb.ki_cookie); | |
2691 | queue_num1 = blk_qc_t_to_queue_num(req->rw.kiocb.ki_cookie); | |
2692 | if (queue_num0 != queue_num1) | |
2693 | ctx->poll_multi_queue = true; | |
2694 | } | |
def596e9 JA |
2695 | } |
2696 | ||
2697 | /* | |
2698 | * For fast devices, IO may have already completed. If it has, add | |
2699 | * it to the front so we find it first. | |
2700 | */ | |
65a6543d | 2701 | if (READ_ONCE(req->iopoll_completed)) |
d21ffe7e | 2702 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
def596e9 | 2703 | else |
d21ffe7e | 2704 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
bdcd3eab | 2705 | |
cb3d8972 PB |
2706 | if (unlikely(in_async)) { |
2707 | /* | |
2708 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle | |
2709 | * in sq thread task context or in io worker task context. If | |
2710 | * current task context is sq thread, we don't need to check | |
2711 | * whether should wake up sq thread. | |
2712 | */ | |
2713 | if ((ctx->flags & IORING_SETUP_SQPOLL) && | |
2714 | wq_has_sleeper(&ctx->sq_data->wait)) | |
2715 | wake_up(&ctx->sq_data->wait); | |
2716 | ||
2717 | mutex_unlock(&ctx->uring_lock); | |
2718 | } | |
def596e9 JA |
2719 | } |
2720 | ||
4503b767 JA |
2721 | static bool io_bdev_nowait(struct block_device *bdev) |
2722 | { | |
9ba0d0c8 | 2723 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2724 | } |
2725 | ||
2b188cc1 JA |
2726 | /* |
2727 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2728 | * any file. For now, just ensure that anything potentially problematic is done | |
2729 | * inline. | |
2730 | */ | |
b191e2df | 2731 | static bool __io_file_supports_nowait(struct file *file, int rw) |
2b188cc1 JA |
2732 | { |
2733 | umode_t mode = file_inode(file)->i_mode; | |
2734 | ||
4503b767 | 2735 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2736 | if (IS_ENABLED(CONFIG_BLOCK) && |
2737 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2738 | return true; |
2739 | return false; | |
2740 | } | |
976517f1 | 2741 | if (S_ISSOCK(mode)) |
2b188cc1 | 2742 | return true; |
4503b767 | 2743 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2744 | if (IS_ENABLED(CONFIG_BLOCK) && |
2745 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2746 | file->f_op != &io_uring_fops) |
2747 | return true; | |
2748 | return false; | |
2749 | } | |
2b188cc1 | 2750 | |
c5b85625 JA |
2751 | /* any ->read/write should understand O_NONBLOCK */ |
2752 | if (file->f_flags & O_NONBLOCK) | |
2753 | return true; | |
2754 | ||
af197f50 JA |
2755 | if (!(file->f_mode & FMODE_NOWAIT)) |
2756 | return false; | |
2757 | ||
2758 | if (rw == READ) | |
2759 | return file->f_op->read_iter != NULL; | |
2760 | ||
2761 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
2762 | } |
2763 | ||
b191e2df | 2764 | static bool io_file_supports_nowait(struct io_kiocb *req, int rw) |
7b29f92d | 2765 | { |
b191e2df | 2766 | if (rw == READ && (req->flags & REQ_F_NOWAIT_READ)) |
7b29f92d | 2767 | return true; |
b191e2df | 2768 | else if (rw == WRITE && (req->flags & REQ_F_NOWAIT_WRITE)) |
7b29f92d JA |
2769 | return true; |
2770 | ||
b191e2df | 2771 | return __io_file_supports_nowait(req->file, rw); |
7b29f92d JA |
2772 | } |
2773 | ||
a88fc400 | 2774 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 2775 | { |
def596e9 | 2776 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2777 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 2778 | struct file *file = req->file; |
09bb8394 JA |
2779 | unsigned ioprio; |
2780 | int ret; | |
2b188cc1 | 2781 | |
c97d8a0f | 2782 | if (!io_req_ffs_set(req) && S_ISREG(file_inode(file)->i_mode)) |
491381ce JA |
2783 | req->flags |= REQ_F_ISREG; |
2784 | ||
2b188cc1 | 2785 | kiocb->ki_pos = READ_ONCE(sqe->off); |
75c668cd | 2786 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
ba04291e | 2787 | req->flags |= REQ_F_CUR_POS; |
75c668cd | 2788 | kiocb->ki_pos = file->f_pos; |
ba04291e | 2789 | } |
2b188cc1 | 2790 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
2791 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
2792 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
2793 | if (unlikely(ret)) | |
2794 | return ret; | |
2b188cc1 | 2795 | |
75c668cd PB |
2796 | /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */ |
2797 | if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK)) | |
2798 | req->flags |= REQ_F_NOWAIT; | |
2799 | ||
2b188cc1 JA |
2800 | ioprio = READ_ONCE(sqe->ioprio); |
2801 | if (ioprio) { | |
2802 | ret = ioprio_check_cap(ioprio); | |
2803 | if (ret) | |
09bb8394 | 2804 | return ret; |
2b188cc1 JA |
2805 | |
2806 | kiocb->ki_ioprio = ioprio; | |
2807 | } else | |
2808 | kiocb->ki_ioprio = get_current_ioprio(); | |
2809 | ||
def596e9 | 2810 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
2811 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
2812 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 2813 | return -EOPNOTSUPP; |
2b188cc1 | 2814 | |
def596e9 JA |
2815 | kiocb->ki_flags |= IOCB_HIPRI; |
2816 | kiocb->ki_complete = io_complete_rw_iopoll; | |
65a6543d | 2817 | req->iopoll_completed = 0; |
def596e9 | 2818 | } else { |
09bb8394 JA |
2819 | if (kiocb->ki_flags & IOCB_HIPRI) |
2820 | return -EINVAL; | |
def596e9 JA |
2821 | kiocb->ki_complete = io_complete_rw; |
2822 | } | |
9adbd45d | 2823 | |
eae071c9 PB |
2824 | if (req->opcode == IORING_OP_READ_FIXED || |
2825 | req->opcode == IORING_OP_WRITE_FIXED) { | |
2826 | req->imu = NULL; | |
2827 | io_req_set_rsrc_node(req); | |
2828 | } | |
2829 | ||
3529d8c2 JA |
2830 | req->rw.addr = READ_ONCE(sqe->addr); |
2831 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2832 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2833 | return 0; |
2b188cc1 JA |
2834 | } |
2835 | ||
2836 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2837 | { | |
2838 | switch (ret) { | |
2839 | case -EIOCBQUEUED: | |
2840 | break; | |
2841 | case -ERESTARTSYS: | |
2842 | case -ERESTARTNOINTR: | |
2843 | case -ERESTARTNOHAND: | |
2844 | case -ERESTART_RESTARTBLOCK: | |
2845 | /* | |
2846 | * We can't just restart the syscall, since previously | |
2847 | * submitted sqes may already be in progress. Just fail this | |
2848 | * IO with EINTR. | |
2849 | */ | |
2850 | ret = -EINTR; | |
df561f66 | 2851 | fallthrough; |
2b188cc1 JA |
2852 | default: |
2853 | kiocb->ki_complete(kiocb, ret, 0); | |
2854 | } | |
2855 | } | |
2856 | ||
a1d7c393 | 2857 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
889fca73 | 2858 | unsigned int issue_flags) |
ba816ad6 | 2859 | { |
ba04291e | 2860 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2861 | struct io_async_rw *io = req->async_data; |
97284637 | 2862 | bool check_reissue = kiocb->ki_complete == io_complete_rw; |
ba04291e | 2863 | |
227c0c96 | 2864 | /* add previously done IO, if any */ |
e8c2bc1f | 2865 | if (io && io->bytes_done > 0) { |
227c0c96 | 2866 | if (ret < 0) |
e8c2bc1f | 2867 | ret = io->bytes_done; |
227c0c96 | 2868 | else |
e8c2bc1f | 2869 | ret += io->bytes_done; |
227c0c96 JA |
2870 | } |
2871 | ||
ba04291e JA |
2872 | if (req->flags & REQ_F_CUR_POS) |
2873 | req->file->f_pos = kiocb->ki_pos; | |
e149bd74 | 2874 | if (ret >= 0 && check_reissue) |
889fca73 | 2875 | __io_complete_rw(req, ret, 0, issue_flags); |
ba816ad6 JA |
2876 | else |
2877 | io_rw_done(kiocb, ret); | |
97284637 | 2878 | |
fe7e3257 | 2879 | if (check_reissue && (req->flags & REQ_F_REISSUE)) { |
97284637 | 2880 | req->flags &= ~REQ_F_REISSUE; |
a7be7c23 | 2881 | if (io_resubmit_prep(req)) { |
773af691 | 2882 | io_req_task_queue_reissue(req); |
8c130827 | 2883 | } else { |
93d2bcd2 | 2884 | req_set_fail(req); |
ae421d93 PB |
2885 | __io_req_complete(req, issue_flags, ret, |
2886 | io_put_rw_kbuf(req)); | |
97284637 PB |
2887 | } |
2888 | } | |
ba816ad6 JA |
2889 | } |
2890 | ||
eae071c9 PB |
2891 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
2892 | struct io_mapped_ubuf *imu) | |
edafccee | 2893 | { |
9adbd45d | 2894 | size_t len = req->rw.len; |
75769e3f | 2895 | u64 buf_end, buf_addr = req->rw.addr; |
edafccee | 2896 | size_t offset; |
edafccee | 2897 | |
75769e3f | 2898 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
edafccee JA |
2899 | return -EFAULT; |
2900 | /* not inside the mapped region */ | |
4751f53d | 2901 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
edafccee JA |
2902 | return -EFAULT; |
2903 | ||
2904 | /* | |
2905 | * May not be a start of buffer, set size appropriately | |
2906 | * and advance us to the beginning. | |
2907 | */ | |
2908 | offset = buf_addr - imu->ubuf; | |
2909 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
2910 | |
2911 | if (offset) { | |
2912 | /* | |
2913 | * Don't use iov_iter_advance() here, as it's really slow for | |
2914 | * using the latter parts of a big fixed buffer - it iterates | |
2915 | * over each segment manually. We can cheat a bit here, because | |
2916 | * we know that: | |
2917 | * | |
2918 | * 1) it's a BVEC iter, we set it up | |
2919 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
2920 | * first and last bvec | |
2921 | * | |
2922 | * So just find our index, and adjust the iterator afterwards. | |
2923 | * If the offset is within the first bvec (or the whole first | |
2924 | * bvec, just use iov_iter_advance(). This makes it easier | |
2925 | * since we can just skip the first segment, which may not | |
2926 | * be PAGE_SIZE aligned. | |
2927 | */ | |
2928 | const struct bio_vec *bvec = imu->bvec; | |
2929 | ||
2930 | if (offset <= bvec->bv_len) { | |
2931 | iov_iter_advance(iter, offset); | |
2932 | } else { | |
2933 | unsigned long seg_skip; | |
2934 | ||
2935 | /* skip first vec */ | |
2936 | offset -= bvec->bv_len; | |
2937 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
2938 | ||
2939 | iter->bvec = bvec + seg_skip; | |
2940 | iter->nr_segs -= seg_skip; | |
99c79f66 | 2941 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 2942 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
2943 | } |
2944 | } | |
2945 | ||
847595de | 2946 | return 0; |
edafccee JA |
2947 | } |
2948 | ||
eae071c9 PB |
2949 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
2950 | { | |
2951 | struct io_ring_ctx *ctx = req->ctx; | |
2952 | struct io_mapped_ubuf *imu = req->imu; | |
2953 | u16 index, buf_index = req->buf_index; | |
2954 | ||
2955 | if (likely(!imu)) { | |
2956 | if (unlikely(buf_index >= ctx->nr_user_bufs)) | |
2957 | return -EFAULT; | |
2958 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); | |
2959 | imu = READ_ONCE(ctx->user_bufs[index]); | |
2960 | req->imu = imu; | |
2961 | } | |
2962 | return __io_import_fixed(req, rw, iter, imu); | |
2963 | } | |
2964 | ||
bcda7baa JA |
2965 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
2966 | { | |
2967 | if (needs_lock) | |
2968 | mutex_unlock(&ctx->uring_lock); | |
2969 | } | |
2970 | ||
2971 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
2972 | { | |
2973 | /* | |
2974 | * "Normal" inline submissions always hold the uring_lock, since we | |
2975 | * grab it from the system call. Same is true for the SQPOLL offload. | |
2976 | * The only exception is when we've detached the request and issue it | |
2977 | * from an async worker thread, grab the lock for that case. | |
2978 | */ | |
2979 | if (needs_lock) | |
2980 | mutex_lock(&ctx->uring_lock); | |
2981 | } | |
2982 | ||
2983 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
2984 | int bgid, struct io_buffer *kbuf, | |
2985 | bool needs_lock) | |
2986 | { | |
2987 | struct io_buffer *head; | |
2988 | ||
2989 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
2990 | return kbuf; | |
2991 | ||
2992 | io_ring_submit_lock(req->ctx, needs_lock); | |
2993 | ||
2994 | lockdep_assert_held(&req->ctx->uring_lock); | |
2995 | ||
9e15c3a0 | 2996 | head = xa_load(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
2997 | if (head) { |
2998 | if (!list_empty(&head->list)) { | |
2999 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
3000 | list); | |
3001 | list_del(&kbuf->list); | |
3002 | } else { | |
3003 | kbuf = head; | |
9e15c3a0 | 3004 | xa_erase(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
3005 | } |
3006 | if (*len > kbuf->len) | |
3007 | *len = kbuf->len; | |
3008 | } else { | |
3009 | kbuf = ERR_PTR(-ENOBUFS); | |
3010 | } | |
3011 | ||
3012 | io_ring_submit_unlock(req->ctx, needs_lock); | |
3013 | ||
3014 | return kbuf; | |
3015 | } | |
3016 | ||
4d954c25 JA |
3017 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
3018 | bool needs_lock) | |
3019 | { | |
3020 | struct io_buffer *kbuf; | |
4f4eeba8 | 3021 | u16 bgid; |
4d954c25 JA |
3022 | |
3023 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
4f4eeba8 | 3024 | bgid = req->buf_index; |
4d954c25 JA |
3025 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
3026 | if (IS_ERR(kbuf)) | |
3027 | return kbuf; | |
3028 | req->rw.addr = (u64) (unsigned long) kbuf; | |
3029 | req->flags |= REQ_F_BUFFER_SELECTED; | |
3030 | return u64_to_user_ptr(kbuf->addr); | |
3031 | } | |
3032 | ||
3033 | #ifdef CONFIG_COMPAT | |
3034 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
3035 | bool needs_lock) | |
3036 | { | |
3037 | struct compat_iovec __user *uiov; | |
3038 | compat_ssize_t clen; | |
3039 | void __user *buf; | |
3040 | ssize_t len; | |
3041 | ||
3042 | uiov = u64_to_user_ptr(req->rw.addr); | |
3043 | if (!access_ok(uiov, sizeof(*uiov))) | |
3044 | return -EFAULT; | |
3045 | if (__get_user(clen, &uiov->iov_len)) | |
3046 | return -EFAULT; | |
3047 | if (clen < 0) | |
3048 | return -EINVAL; | |
3049 | ||
3050 | len = clen; | |
3051 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3052 | if (IS_ERR(buf)) | |
3053 | return PTR_ERR(buf); | |
3054 | iov[0].iov_base = buf; | |
3055 | iov[0].iov_len = (compat_size_t) len; | |
3056 | return 0; | |
3057 | } | |
3058 | #endif | |
3059 | ||
3060 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3061 | bool needs_lock) | |
3062 | { | |
3063 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3064 | void __user *buf; | |
3065 | ssize_t len; | |
3066 | ||
3067 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3068 | return -EFAULT; | |
3069 | ||
3070 | len = iov[0].iov_len; | |
3071 | if (len < 0) | |
3072 | return -EINVAL; | |
3073 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3074 | if (IS_ERR(buf)) | |
3075 | return PTR_ERR(buf); | |
3076 | iov[0].iov_base = buf; | |
3077 | iov[0].iov_len = len; | |
3078 | return 0; | |
3079 | } | |
3080 | ||
3081 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3082 | bool needs_lock) | |
3083 | { | |
dddb3e26 JA |
3084 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
3085 | struct io_buffer *kbuf; | |
3086 | ||
3087 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
3088 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); | |
3089 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3090 | return 0; |
dddb3e26 | 3091 | } |
dd201662 | 3092 | if (req->rw.len != 1) |
4d954c25 JA |
3093 | return -EINVAL; |
3094 | ||
3095 | #ifdef CONFIG_COMPAT | |
3096 | if (req->ctx->compat) | |
3097 | return io_compat_import(req, iov, needs_lock); | |
3098 | #endif | |
3099 | ||
3100 | return __io_iov_buffer_select(req, iov, needs_lock); | |
3101 | } | |
3102 | ||
847595de PB |
3103 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
3104 | struct iov_iter *iter, bool needs_lock) | |
2b188cc1 | 3105 | { |
9adbd45d JA |
3106 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
3107 | size_t sqe_len = req->rw.len; | |
847595de | 3108 | u8 opcode = req->opcode; |
4d954c25 | 3109 | ssize_t ret; |
edafccee | 3110 | |
7d009165 | 3111 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 3112 | *iovec = NULL; |
9adbd45d | 3113 | return io_import_fixed(req, rw, iter); |
edafccee | 3114 | } |
2b188cc1 | 3115 | |
bcda7baa | 3116 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 3117 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
3118 | return -EINVAL; |
3119 | ||
3a6820f2 | 3120 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3121 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 3122 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 3123 | if (IS_ERR(buf)) |
4d954c25 | 3124 | return PTR_ERR(buf); |
3f9d6441 | 3125 | req->rw.len = sqe_len; |
bcda7baa JA |
3126 | } |
3127 | ||
3a6820f2 JA |
3128 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
3129 | *iovec = NULL; | |
10fc72e4 | 3130 | return ret; |
3a6820f2 JA |
3131 | } |
3132 | ||
4d954c25 JA |
3133 | if (req->flags & REQ_F_BUFFER_SELECT) { |
3134 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
847595de PB |
3135 | if (!ret) |
3136 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); | |
4d954c25 JA |
3137 | *iovec = NULL; |
3138 | return ret; | |
3139 | } | |
3140 | ||
89cd35c5 CH |
3141 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
3142 | req->ctx->compat); | |
2b188cc1 JA |
3143 | } |
3144 | ||
0fef9483 JA |
3145 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3146 | { | |
5b09e37e | 3147 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3148 | } |
3149 | ||
31b51510 | 3150 | /* |
32960613 JA |
3151 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3152 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3153 | */ |
4017eb91 | 3154 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3155 | { |
4017eb91 JA |
3156 | struct kiocb *kiocb = &req->rw.kiocb; |
3157 | struct file *file = req->file; | |
32960613 JA |
3158 | ssize_t ret = 0; |
3159 | ||
3160 | /* | |
3161 | * Don't support polled IO through this interface, and we can't | |
3162 | * support non-blocking either. For the latter, this just causes | |
3163 | * the kiocb to be handled from an async context. | |
3164 | */ | |
3165 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3166 | return -EOPNOTSUPP; | |
3167 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3168 | return -EAGAIN; | |
3169 | ||
3170 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3171 | struct iovec iovec; |
32960613 JA |
3172 | ssize_t nr; |
3173 | ||
311ae9e1 PB |
3174 | if (!iov_iter_is_bvec(iter)) { |
3175 | iovec = iov_iter_iovec(iter); | |
3176 | } else { | |
4017eb91 JA |
3177 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3178 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3179 | } |
3180 | ||
32960613 JA |
3181 | if (rw == READ) { |
3182 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3183 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3184 | } else { |
3185 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3186 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3187 | } |
3188 | ||
3189 | if (nr < 0) { | |
3190 | if (!ret) | |
3191 | ret = nr; | |
3192 | break; | |
3193 | } | |
3194 | ret += nr; | |
3195 | if (nr != iovec.iov_len) | |
3196 | break; | |
4017eb91 JA |
3197 | req->rw.len -= nr; |
3198 | req->rw.addr += nr; | |
32960613 JA |
3199 | iov_iter_advance(iter, nr); |
3200 | } | |
3201 | ||
3202 | return ret; | |
3203 | } | |
3204 | ||
ff6165b2 JA |
3205 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3206 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3207 | { |
e8c2bc1f | 3208 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3209 | |
ff6165b2 | 3210 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3211 | rw->free_iovec = iovec; |
227c0c96 | 3212 | rw->bytes_done = 0; |
ff6165b2 | 3213 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3214 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3215 | return; |
b64e3444 | 3216 | if (!iovec) { |
ff6165b2 JA |
3217 | unsigned iov_off = 0; |
3218 | ||
3219 | rw->iter.iov = rw->fast_iov; | |
3220 | if (iter->iov != fast_iov) { | |
3221 | iov_off = iter->iov - fast_iov; | |
3222 | rw->iter.iov += iov_off; | |
3223 | } | |
3224 | if (rw->fast_iov != fast_iov) | |
3225 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3226 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3227 | } else { |
3228 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3229 | } |
3230 | } | |
3231 | ||
6cb78689 | 3232 | static inline int io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3233 | { |
e8c2bc1f JA |
3234 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3235 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3236 | return req->async_data == NULL; | |
3d9932a8 XW |
3237 | } |
3238 | ||
ff6165b2 JA |
3239 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3240 | const struct iovec *fast_iov, | |
227c0c96 | 3241 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3242 | { |
26f0505a | 3243 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
74566df3 | 3244 | return 0; |
e8c2bc1f | 3245 | if (!req->async_data) { |
6cb78689 | 3246 | if (io_alloc_async_data(req)) { |
6bf985dc | 3247 | kfree(iovec); |
5d204bcf | 3248 | return -ENOMEM; |
6bf985dc | 3249 | } |
b7bb4f7d | 3250 | |
ff6165b2 | 3251 | io_req_map_rw(req, iovec, fast_iov, iter); |
5d204bcf | 3252 | } |
b7bb4f7d | 3253 | return 0; |
f67676d1 JA |
3254 | } |
3255 | ||
73debe68 | 3256 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3257 | { |
e8c2bc1f | 3258 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3259 | struct iovec *iov = iorw->fast_iov; |
847595de | 3260 | int ret; |
c3e330a4 | 3261 | |
2846c481 | 3262 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
c3e330a4 PB |
3263 | if (unlikely(ret < 0)) |
3264 | return ret; | |
3265 | ||
ab0b196c PB |
3266 | iorw->bytes_done = 0; |
3267 | iorw->free_iovec = iov; | |
3268 | if (iov) | |
3269 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3270 | return 0; |
3271 | } | |
3272 | ||
73debe68 | 3273 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3274 | { |
3529d8c2 JA |
3275 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3276 | return -EBADF; | |
93642ef8 | 3277 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3278 | } |
3279 | ||
c1dd91d1 JA |
3280 | /* |
3281 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3282 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3283 | * This gets called when the page is unlocked, and we generally expect that to | |
3284 | * happen when the page IO is completed and the page is now uptodate. This will | |
3285 | * queue a task_work based retry of the operation, attempting to copy the data | |
3286 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3287 | * do a thread based blocking retry of the operation. That's the unexpected | |
3288 | * slow path. | |
3289 | */ | |
bcf5a063 JA |
3290 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3291 | int sync, void *arg) | |
3292 | { | |
3293 | struct wait_page_queue *wpq; | |
3294 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3295 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3296 | |
3297 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3298 | ||
cdc8fcb4 LT |
3299 | if (!wake_page_match(wpq, key)) |
3300 | return 0; | |
3301 | ||
c8d317aa | 3302 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 | 3303 | list_del_init(&wait->entry); |
921b9054 | 3304 | io_req_task_queue(req); |
bcf5a063 JA |
3305 | return 1; |
3306 | } | |
3307 | ||
c1dd91d1 JA |
3308 | /* |
3309 | * This controls whether a given IO request should be armed for async page | |
3310 | * based retry. If we return false here, the request is handed to the async | |
3311 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3312 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3313 | * will either succeed because the page is now uptodate and unlocked, or it | |
3314 | * will register a callback when the page is unlocked at IO completion. Through | |
3315 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3316 | * That retry will attempt the buffered read again. The retry will generally | |
3317 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3318 | * async worker threads for a blocking retry. | |
3319 | */ | |
227c0c96 | 3320 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3321 | { |
e8c2bc1f JA |
3322 | struct io_async_rw *rw = req->async_data; |
3323 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3324 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3325 | |
bcf5a063 JA |
3326 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3327 | if (req->flags & REQ_F_NOWAIT) | |
3328 | return false; | |
f67676d1 | 3329 | |
227c0c96 | 3330 | /* Only for buffered IO */ |
3b2a4439 | 3331 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3332 | return false; |
3b2a4439 | 3333 | |
bcf5a063 JA |
3334 | /* |
3335 | * just use poll if we can, and don't attempt if the fs doesn't | |
3336 | * support callback based unlocks | |
3337 | */ | |
3338 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3339 | return false; | |
f67676d1 | 3340 | |
3b2a4439 JA |
3341 | wait->wait.func = io_async_buf_func; |
3342 | wait->wait.private = req; | |
3343 | wait->wait.flags = 0; | |
3344 | INIT_LIST_HEAD(&wait->wait.entry); | |
3345 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3346 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3347 | kiocb->ki_waitq = wait; |
3b2a4439 | 3348 | return true; |
bcf5a063 JA |
3349 | } |
3350 | ||
aeab9506 | 3351 | static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
bcf5a063 JA |
3352 | { |
3353 | if (req->file->f_op->read_iter) | |
3354 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d | 3355 | else if (req->file->f_op->read) |
4017eb91 | 3356 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3357 | else |
3358 | return -EINVAL; | |
f67676d1 JA |
3359 | } |
3360 | ||
889fca73 | 3361 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3362 | { |
3363 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3364 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3365 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3366 | struct io_async_rw *rw = req->async_data; |
227c0c96 | 3367 | ssize_t io_size, ret, ret2; |
45d189c6 | 3368 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ff6165b2 | 3369 | |
2846c481 | 3370 | if (rw) { |
e8c2bc1f | 3371 | iter = &rw->iter; |
2846c481 PB |
3372 | iovec = NULL; |
3373 | } else { | |
3374 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); | |
3375 | if (ret < 0) | |
3376 | return ret; | |
3377 | } | |
632546c4 | 3378 | io_size = iov_iter_count(iter); |
fa15bafb | 3379 | req->result = io_size; |
2b188cc1 | 3380 | |
fd6c2e4c JA |
3381 | /* Ensure we clear previously set non-block flag */ |
3382 | if (!force_nonblock) | |
29de5f6a | 3383 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
a88fc400 PB |
3384 | else |
3385 | kiocb->ki_flags |= IOCB_NOWAIT; | |
3386 | ||
24c74678 | 3387 | /* If the file doesn't support async, just async punt */ |
b191e2df | 3388 | if (force_nonblock && !io_file_supports_nowait(req, READ)) { |
6713e7a6 | 3389 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc | 3390 | return ret ?: -EAGAIN; |
6713e7a6 | 3391 | } |
9e645e11 | 3392 | |
632546c4 | 3393 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size); |
5ea5dd45 PB |
3394 | if (unlikely(ret)) { |
3395 | kfree(iovec); | |
3396 | return ret; | |
3397 | } | |
2b188cc1 | 3398 | |
227c0c96 | 3399 | ret = io_iter_do_read(req, iter); |
32960613 | 3400 | |
230d50d4 | 3401 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
6ad7f233 | 3402 | req->flags &= ~REQ_F_REISSUE; |
eefdf30f JA |
3403 | /* IOPOLL retry should happen for io-wq threads */ |
3404 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3405 | goto done; |
75c668cd PB |
3406 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3407 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3408 | goto done; |
84216315 | 3409 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3410 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
f38c7e3a | 3411 | ret = 0; |
230d50d4 JA |
3412 | } else if (ret == -EIOCBQUEUED) { |
3413 | goto out_free; | |
7335e3bf | 3414 | } else if (ret <= 0 || ret == io_size || !force_nonblock || |
75c668cd | 3415 | (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) { |
7335e3bf | 3416 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3417 | goto done; |
227c0c96 JA |
3418 | } |
3419 | ||
227c0c96 | 3420 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc PB |
3421 | if (ret2) |
3422 | return ret2; | |
3423 | ||
fe1cdd55 | 3424 | iovec = NULL; |
e8c2bc1f | 3425 | rw = req->async_data; |
227c0c96 | 3426 | /* now use our persistent iterator, if we aren't already */ |
e8c2bc1f | 3427 | iter = &rw->iter; |
227c0c96 | 3428 | |
b23df91b PB |
3429 | do { |
3430 | io_size -= ret; | |
3431 | rw->bytes_done += ret; | |
3432 | /* if we can retry, do so with the callbacks armed */ | |
3433 | if (!io_rw_should_retry(req)) { | |
3434 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3435 | return -EAGAIN; | |
3436 | } | |
3437 | ||
3438 | /* | |
3439 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3440 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3441 | * desired page gets unlocked. We can also get a partial read | |
3442 | * here, and if we do, then just retry at the new offset. | |
3443 | */ | |
3444 | ret = io_iter_do_read(req, iter); | |
3445 | if (ret == -EIOCBQUEUED) | |
3446 | return 0; | |
227c0c96 | 3447 | /* we got some bytes, but not all. retry. */ |
b5b0ecb7 | 3448 | kiocb->ki_flags &= ~IOCB_WAITQ; |
b23df91b | 3449 | } while (ret > 0 && ret < io_size); |
227c0c96 | 3450 | done: |
889fca73 | 3451 | kiocb_done(kiocb, ret, issue_flags); |
fe1cdd55 PB |
3452 | out_free: |
3453 | /* it's faster to check here then delegate to kfree */ | |
3454 | if (iovec) | |
3455 | kfree(iovec); | |
5ea5dd45 | 3456 | return 0; |
2b188cc1 JA |
3457 | } |
3458 | ||
73debe68 | 3459 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3460 | { |
3529d8c2 JA |
3461 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3462 | return -EBADF; | |
93642ef8 | 3463 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3464 | } |
3465 | ||
889fca73 | 3466 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3467 | { |
3468 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3469 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3470 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3471 | struct io_async_rw *rw = req->async_data; |
fa15bafb | 3472 | ssize_t ret, ret2, io_size; |
45d189c6 | 3473 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
2b188cc1 | 3474 | |
2846c481 | 3475 | if (rw) { |
e8c2bc1f | 3476 | iter = &rw->iter; |
2846c481 PB |
3477 | iovec = NULL; |
3478 | } else { | |
3479 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
3480 | if (ret < 0) | |
3481 | return ret; | |
3482 | } | |
632546c4 | 3483 | io_size = iov_iter_count(iter); |
fa15bafb | 3484 | req->result = io_size; |
2b188cc1 | 3485 | |
fd6c2e4c JA |
3486 | /* Ensure we clear previously set non-block flag */ |
3487 | if (!force_nonblock) | |
a88fc400 PB |
3488 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3489 | else | |
3490 | kiocb->ki_flags |= IOCB_NOWAIT; | |
fd6c2e4c | 3491 | |
24c74678 | 3492 | /* If the file doesn't support async, just async punt */ |
b191e2df | 3493 | if (force_nonblock && !io_file_supports_nowait(req, WRITE)) |
f67676d1 | 3494 | goto copy_iov; |
31b51510 | 3495 | |
10d59345 JA |
3496 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3497 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3498 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3499 | goto copy_iov; |
31b51510 | 3500 | |
632546c4 | 3501 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size); |
fa15bafb PB |
3502 | if (unlikely(ret)) |
3503 | goto out_free; | |
4ed734b0 | 3504 | |
fa15bafb PB |
3505 | /* |
3506 | * Open-code file_start_write here to grab freeze protection, | |
3507 | * which will be released by another thread in | |
3508 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3509 | * released so that it doesn't complain about the held lock when | |
3510 | * we return to userspace. | |
3511 | */ | |
3512 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3513 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3514 | __sb_writers_release(file_inode(req->file)->i_sb, |
3515 | SB_FREEZE_WRITE); | |
3516 | } | |
3517 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3518 | |
fa15bafb | 3519 | if (req->file->f_op->write_iter) |
ff6165b2 | 3520 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3521 | else if (req->file->f_op->write) |
4017eb91 | 3522 | ret2 = loop_rw_iter(WRITE, req, iter); |
2dd2111d GH |
3523 | else |
3524 | ret2 = -EINVAL; | |
4ed734b0 | 3525 | |
6ad7f233 PB |
3526 | if (req->flags & REQ_F_REISSUE) { |
3527 | req->flags &= ~REQ_F_REISSUE; | |
230d50d4 | 3528 | ret2 = -EAGAIN; |
6ad7f233 | 3529 | } |
230d50d4 | 3530 | |
fa15bafb PB |
3531 | /* |
3532 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3533 | * retry them without IOCB_NOWAIT. | |
3534 | */ | |
3535 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3536 | ret2 = -EAGAIN; | |
75c668cd PB |
3537 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3538 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3539 | goto done; |
fa15bafb | 3540 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3541 | /* IOPOLL retry should happen for io-wq threads */ |
3542 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3543 | goto copy_iov; | |
355afaeb | 3544 | done: |
889fca73 | 3545 | kiocb_done(kiocb, ret2, issue_flags); |
fa15bafb | 3546 | } else { |
f67676d1 | 3547 | copy_iov: |
84216315 | 3548 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3549 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
227c0c96 | 3550 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
6bf985dc | 3551 | return ret ?: -EAGAIN; |
2b188cc1 | 3552 | } |
31b51510 | 3553 | out_free: |
f261c168 | 3554 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3555 | if (iovec) |
6f2cc166 | 3556 | kfree(iovec); |
2b188cc1 JA |
3557 | return ret; |
3558 | } | |
3559 | ||
80a261fd JA |
3560 | static int io_renameat_prep(struct io_kiocb *req, |
3561 | const struct io_uring_sqe *sqe) | |
3562 | { | |
3563 | struct io_rename *ren = &req->rename; | |
3564 | const char __user *oldf, *newf; | |
3565 | ||
ed7eb259 JA |
3566 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3567 | return -EINVAL; | |
26578cda | 3568 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
ed7eb259 | 3569 | return -EINVAL; |
80a261fd JA |
3570 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3571 | return -EBADF; | |
3572 | ||
3573 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3574 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3575 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3576 | ren->new_dfd = READ_ONCE(sqe->len); | |
3577 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3578 | ||
3579 | ren->oldpath = getname(oldf); | |
3580 | if (IS_ERR(ren->oldpath)) | |
3581 | return PTR_ERR(ren->oldpath); | |
3582 | ||
3583 | ren->newpath = getname(newf); | |
3584 | if (IS_ERR(ren->newpath)) { | |
3585 | putname(ren->oldpath); | |
3586 | return PTR_ERR(ren->newpath); | |
3587 | } | |
3588 | ||
3589 | req->flags |= REQ_F_NEED_CLEANUP; | |
3590 | return 0; | |
3591 | } | |
3592 | ||
45d189c6 | 3593 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
3594 | { |
3595 | struct io_rename *ren = &req->rename; | |
3596 | int ret; | |
3597 | ||
45d189c6 | 3598 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
3599 | return -EAGAIN; |
3600 | ||
3601 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3602 | ren->newpath, ren->flags); | |
3603 | ||
3604 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3605 | if (ret < 0) | |
93d2bcd2 | 3606 | req_set_fail(req); |
80a261fd JA |
3607 | io_req_complete(req, ret); |
3608 | return 0; | |
3609 | } | |
3610 | ||
14a1143b JA |
3611 | static int io_unlinkat_prep(struct io_kiocb *req, |
3612 | const struct io_uring_sqe *sqe) | |
3613 | { | |
3614 | struct io_unlink *un = &req->unlink; | |
3615 | const char __user *fname; | |
3616 | ||
22634bc5 JA |
3617 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3618 | return -EINVAL; | |
26578cda PB |
3619 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
3620 | sqe->splice_fd_in) | |
22634bc5 | 3621 | return -EINVAL; |
14a1143b JA |
3622 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3623 | return -EBADF; | |
3624 | ||
3625 | un->dfd = READ_ONCE(sqe->fd); | |
3626 | ||
3627 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3628 | if (un->flags & ~AT_REMOVEDIR) | |
3629 | return -EINVAL; | |
3630 | ||
3631 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3632 | un->filename = getname(fname); | |
3633 | if (IS_ERR(un->filename)) | |
3634 | return PTR_ERR(un->filename); | |
3635 | ||
3636 | req->flags |= REQ_F_NEED_CLEANUP; | |
3637 | return 0; | |
3638 | } | |
3639 | ||
45d189c6 | 3640 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
3641 | { |
3642 | struct io_unlink *un = &req->unlink; | |
3643 | int ret; | |
3644 | ||
45d189c6 | 3645 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
3646 | return -EAGAIN; |
3647 | ||
3648 | if (un->flags & AT_REMOVEDIR) | |
3649 | ret = do_rmdir(un->dfd, un->filename); | |
3650 | else | |
3651 | ret = do_unlinkat(un->dfd, un->filename); | |
3652 | ||
3653 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3654 | if (ret < 0) | |
93d2bcd2 | 3655 | req_set_fail(req); |
14a1143b JA |
3656 | io_req_complete(req, ret); |
3657 | return 0; | |
3658 | } | |
3659 | ||
36f4fa68 JA |
3660 | static int io_shutdown_prep(struct io_kiocb *req, |
3661 | const struct io_uring_sqe *sqe) | |
3662 | { | |
3663 | #if defined(CONFIG_NET) | |
3664 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3665 | return -EINVAL; | |
26578cda PB |
3666 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
3667 | sqe->buf_index || sqe->splice_fd_in)) | |
36f4fa68 JA |
3668 | return -EINVAL; |
3669 | ||
3670 | req->shutdown.how = READ_ONCE(sqe->len); | |
3671 | return 0; | |
3672 | #else | |
3673 | return -EOPNOTSUPP; | |
3674 | #endif | |
3675 | } | |
3676 | ||
45d189c6 | 3677 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
3678 | { |
3679 | #if defined(CONFIG_NET) | |
3680 | struct socket *sock; | |
3681 | int ret; | |
3682 | ||
45d189c6 | 3683 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
3684 | return -EAGAIN; |
3685 | ||
48aba79b | 3686 | sock = sock_from_file(req->file); |
36f4fa68 | 3687 | if (unlikely(!sock)) |
48aba79b | 3688 | return -ENOTSOCK; |
36f4fa68 JA |
3689 | |
3690 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d | 3691 | if (ret < 0) |
93d2bcd2 | 3692 | req_set_fail(req); |
36f4fa68 JA |
3693 | io_req_complete(req, ret); |
3694 | return 0; | |
3695 | #else | |
3696 | return -EOPNOTSUPP; | |
3697 | #endif | |
3698 | } | |
3699 | ||
f2a8d5c7 PB |
3700 | static int __io_splice_prep(struct io_kiocb *req, |
3701 | const struct io_uring_sqe *sqe) | |
7d67af2c | 3702 | { |
fe7e3257 | 3703 | struct io_splice *sp = &req->splice; |
7d67af2c | 3704 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
7d67af2c | 3705 | |
3232dd02 PB |
3706 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3707 | return -EINVAL; | |
7d67af2c PB |
3708 | |
3709 | sp->file_in = NULL; | |
7d67af2c PB |
3710 | sp->len = READ_ONCE(sqe->len); |
3711 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3712 | ||
3713 | if (unlikely(sp->flags & ~valid_flags)) | |
3714 | return -EINVAL; | |
3715 | ||
62906e89 | 3716 | sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in), |
8371adf5 PB |
3717 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
3718 | if (!sp->file_in) | |
3719 | return -EBADF; | |
7d67af2c | 3720 | req->flags |= REQ_F_NEED_CLEANUP; |
7d67af2c PB |
3721 | return 0; |
3722 | } | |
3723 | ||
f2a8d5c7 PB |
3724 | static int io_tee_prep(struct io_kiocb *req, |
3725 | const struct io_uring_sqe *sqe) | |
3726 | { | |
3727 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3728 | return -EINVAL; | |
3729 | return __io_splice_prep(req, sqe); | |
3730 | } | |
3731 | ||
45d189c6 | 3732 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
3733 | { |
3734 | struct io_splice *sp = &req->splice; | |
3735 | struct file *in = sp->file_in; | |
3736 | struct file *out = sp->file_out; | |
3737 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3738 | long ret = 0; | |
3739 | ||
45d189c6 | 3740 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
3741 | return -EAGAIN; |
3742 | if (sp->len) | |
3743 | ret = do_tee(in, out, sp->len, flags); | |
3744 | ||
e1d767f0 PB |
3745 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
3746 | io_put_file(in); | |
f2a8d5c7 PB |
3747 | req->flags &= ~REQ_F_NEED_CLEANUP; |
3748 | ||
f2a8d5c7 | 3749 | if (ret != sp->len) |
93d2bcd2 | 3750 | req_set_fail(req); |
e1e16097 | 3751 | io_req_complete(req, ret); |
f2a8d5c7 PB |
3752 | return 0; |
3753 | } | |
3754 | ||
3755 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
3756 | { | |
fe7e3257 | 3757 | struct io_splice *sp = &req->splice; |
f2a8d5c7 PB |
3758 | |
3759 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
3760 | sp->off_out = READ_ONCE(sqe->off); | |
3761 | return __io_splice_prep(req, sqe); | |
3762 | } | |
3763 | ||
45d189c6 | 3764 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
3765 | { |
3766 | struct io_splice *sp = &req->splice; | |
3767 | struct file *in = sp->file_in; | |
3768 | struct file *out = sp->file_out; | |
3769 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3770 | loff_t *poff_in, *poff_out; | |
c9687426 | 3771 | long ret = 0; |
7d67af2c | 3772 | |
45d189c6 | 3773 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 3774 | return -EAGAIN; |
7d67af2c PB |
3775 | |
3776 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
3777 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 3778 | |
948a7749 | 3779 | if (sp->len) |
c9687426 | 3780 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c | 3781 | |
e1d767f0 PB |
3782 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
3783 | io_put_file(in); | |
7d67af2c PB |
3784 | req->flags &= ~REQ_F_NEED_CLEANUP; |
3785 | ||
7d67af2c | 3786 | if (ret != sp->len) |
93d2bcd2 | 3787 | req_set_fail(req); |
e1e16097 | 3788 | io_req_complete(req, ret); |
7d67af2c PB |
3789 | return 0; |
3790 | } | |
3791 | ||
2b188cc1 JA |
3792 | /* |
3793 | * IORING_OP_NOP just posts a completion event, nothing else. | |
3794 | */ | |
889fca73 | 3795 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3796 | { |
3797 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 3798 | |
def596e9 JA |
3799 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
3800 | return -EINVAL; | |
3801 | ||
889fca73 | 3802 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
3803 | return 0; |
3804 | } | |
3805 | ||
1155c76a | 3806 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 3807 | { |
6b06314c | 3808 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 3809 | |
09bb8394 JA |
3810 | if (!req->file) |
3811 | return -EBADF; | |
c992fe29 | 3812 | |
6b06314c | 3813 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 3814 | return -EINVAL; |
26578cda PB |
3815 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
3816 | sqe->splice_fd_in)) | |
c992fe29 CH |
3817 | return -EINVAL; |
3818 | ||
8ed8d3c3 JA |
3819 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
3820 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
3821 | return -EINVAL; | |
3822 | ||
3823 | req->sync.off = READ_ONCE(sqe->off); | |
3824 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
3825 | return 0; |
3826 | } | |
3827 | ||
45d189c6 | 3828 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 3829 | { |
8ed8d3c3 | 3830 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
3831 | int ret; |
3832 | ||
ac45abc0 | 3833 | /* fsync always requires a blocking context */ |
45d189c6 | 3834 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
3835 | return -EAGAIN; |
3836 | ||
9adbd45d | 3837 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
3838 | end > 0 ? end : LLONG_MAX, |
3839 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
3840 | if (ret < 0) | |
93d2bcd2 | 3841 | req_set_fail(req); |
e1e16097 | 3842 | io_req_complete(req, ret); |
c992fe29 CH |
3843 | return 0; |
3844 | } | |
3845 | ||
d63d1b5e JA |
3846 | static int io_fallocate_prep(struct io_kiocb *req, |
3847 | const struct io_uring_sqe *sqe) | |
3848 | { | |
26578cda PB |
3849 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
3850 | sqe->splice_fd_in) | |
d63d1b5e | 3851 | return -EINVAL; |
3232dd02 PB |
3852 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3853 | return -EINVAL; | |
d63d1b5e JA |
3854 | |
3855 | req->sync.off = READ_ONCE(sqe->off); | |
3856 | req->sync.len = READ_ONCE(sqe->addr); | |
3857 | req->sync.mode = READ_ONCE(sqe->len); | |
3858 | return 0; | |
3859 | } | |
3860 | ||
45d189c6 | 3861 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 3862 | { |
ac45abc0 PB |
3863 | int ret; |
3864 | ||
d63d1b5e | 3865 | /* fallocate always requiring blocking context */ |
45d189c6 | 3866 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 3867 | return -EAGAIN; |
ac45abc0 PB |
3868 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
3869 | req->sync.len); | |
ac45abc0 | 3870 | if (ret < 0) |
93d2bcd2 | 3871 | req_set_fail(req); |
e1e16097 | 3872 | io_req_complete(req, ret); |
5d17b4a4 JA |
3873 | return 0; |
3874 | } | |
3875 | ||
ec65fea5 | 3876 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 3877 | { |
f8748881 | 3878 | const char __user *fname; |
15b71abe | 3879 | int ret; |
b7bb4f7d | 3880 | |
d3fddf6d PB |
3881 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3882 | return -EINVAL; | |
b9445598 | 3883 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 3884 | return -EINVAL; |
ec65fea5 | 3885 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 3886 | return -EBADF; |
03b1230c | 3887 | |
ec65fea5 PB |
3888 | /* open.how should be already initialised */ |
3889 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 3890 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 3891 | |
25e72d10 PB |
3892 | req->open.dfd = READ_ONCE(sqe->fd); |
3893 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 3894 | req->open.filename = getname(fname); |
15b71abe JA |
3895 | if (IS_ERR(req->open.filename)) { |
3896 | ret = PTR_ERR(req->open.filename); | |
3897 | req->open.filename = NULL; | |
3898 | return ret; | |
3899 | } | |
b9445598 PB |
3900 | |
3901 | req->open.file_slot = READ_ONCE(sqe->file_index); | |
3902 | if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) | |
3903 | return -EINVAL; | |
3904 | ||
4022e7af | 3905 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 3906 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 3907 | return 0; |
03b1230c JA |
3908 | } |
3909 | ||
ec65fea5 PB |
3910 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
3911 | { | |
d3fddf6d PB |
3912 | u64 mode = READ_ONCE(sqe->len); |
3913 | u64 flags = READ_ONCE(sqe->open_flags); | |
ec65fea5 | 3914 | |
ec65fea5 PB |
3915 | req->open.how = build_open_how(flags, mode); |
3916 | return __io_openat_prep(req, sqe); | |
3917 | } | |
3918 | ||
cebdb986 | 3919 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 3920 | { |
cebdb986 | 3921 | struct open_how __user *how; |
cebdb986 | 3922 | size_t len; |
0fa03c62 JA |
3923 | int ret; |
3924 | ||
cebdb986 JA |
3925 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
3926 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
3927 | if (len < OPEN_HOW_SIZE_VER0) |
3928 | return -EINVAL; | |
3529d8c2 | 3929 | |
cebdb986 JA |
3930 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
3931 | len); | |
3932 | if (ret) | |
3933 | return ret; | |
3529d8c2 | 3934 | |
ec65fea5 | 3935 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
3936 | } |
3937 | ||
45d189c6 | 3938 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
3939 | { |
3940 | struct open_flags op; | |
15b71abe | 3941 | struct file *file; |
b9445598 PB |
3942 | bool resolve_nonblock, nonblock_set; |
3943 | bool fixed = !!req->open.file_slot; | |
15b71abe JA |
3944 | int ret; |
3945 | ||
cebdb986 | 3946 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
3947 | if (ret) |
3948 | goto err; | |
3a81fd02 JA |
3949 | nonblock_set = op.open_flag & O_NONBLOCK; |
3950 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 3951 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
3952 | /* |
3953 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
3954 | * it'll always -EAGAIN | |
3955 | */ | |
3956 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
3957 | return -EAGAIN; | |
3958 | op.lookup_flags |= LOOKUP_CACHED; | |
3959 | op.open_flag |= O_NONBLOCK; | |
3960 | } | |
15b71abe | 3961 | |
b9445598 PB |
3962 | if (!fixed) { |
3963 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); | |
3964 | if (ret < 0) | |
3965 | goto err; | |
3966 | } | |
15b71abe JA |
3967 | |
3968 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
12dcb58a | 3969 | if (IS_ERR(file)) { |
944d1444 | 3970 | /* |
12dcb58a PB |
3971 | * We could hang on to this 'fd' on retrying, but seems like |
3972 | * marginal gain for something that is now known to be a slower | |
3973 | * path. So just put it, and we'll get a new one when we retry. | |
944d1444 | 3974 | */ |
b9445598 PB |
3975 | if (!fixed) |
3976 | put_unused_fd(ret); | |
3a81fd02 | 3977 | |
15b71abe | 3978 | ret = PTR_ERR(file); |
12dcb58a PB |
3979 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
3980 | if (ret == -EAGAIN && | |
3981 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) | |
3982 | return -EAGAIN; | |
3983 | goto err; | |
15b71abe | 3984 | } |
12dcb58a PB |
3985 | |
3986 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) | |
3987 | file->f_flags &= ~O_NONBLOCK; | |
3988 | fsnotify_open(file); | |
b9445598 PB |
3989 | |
3990 | if (!fixed) | |
3991 | fd_install(ret, file); | |
3992 | else | |
3993 | ret = io_install_fixed_file(req, file, issue_flags, | |
3994 | req->open.file_slot - 1); | |
15b71abe JA |
3995 | err: |
3996 | putname(req->open.filename); | |
8fef80bf | 3997 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe | 3998 | if (ret < 0) |
93d2bcd2 | 3999 | req_set_fail(req); |
0bdf3398 | 4000 | __io_req_complete(req, issue_flags, ret, 0); |
15b71abe JA |
4001 | return 0; |
4002 | } | |
4003 | ||
45d189c6 | 4004 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 4005 | { |
e45cff58 | 4006 | return io_openat2(req, issue_flags); |
cebdb986 JA |
4007 | } |
4008 | ||
067524e9 JA |
4009 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4010 | const struct io_uring_sqe *sqe) | |
4011 | { | |
4012 | struct io_provide_buf *p = &req->pbuf; | |
4013 | u64 tmp; | |
4014 | ||
26578cda PB |
4015 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
4016 | sqe->splice_fd_in) | |
067524e9 JA |
4017 | return -EINVAL; |
4018 | ||
4019 | tmp = READ_ONCE(sqe->fd); | |
4020 | if (!tmp || tmp > USHRT_MAX) | |
4021 | return -EINVAL; | |
4022 | ||
4023 | memset(p, 0, sizeof(*p)); | |
4024 | p->nbufs = tmp; | |
4025 | p->bgid = READ_ONCE(sqe->buf_group); | |
4026 | return 0; | |
4027 | } | |
4028 | ||
4029 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
4030 | int bgid, unsigned nbufs) | |
4031 | { | |
4032 | unsigned i = 0; | |
4033 | ||
4034 | /* shouldn't happen */ | |
4035 | if (!nbufs) | |
4036 | return 0; | |
4037 | ||
4038 | /* the head kbuf is the list itself */ | |
4039 | while (!list_empty(&buf->list)) { | |
4040 | struct io_buffer *nxt; | |
4041 | ||
4042 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
4043 | list_del(&nxt->list); | |
4044 | kfree(nxt); | |
4045 | if (++i == nbufs) | |
4046 | return i; | |
4047 | } | |
4048 | i++; | |
4049 | kfree(buf); | |
9e15c3a0 | 4050 | xa_erase(&ctx->io_buffers, bgid); |
067524e9 JA |
4051 | |
4052 | return i; | |
4053 | } | |
4054 | ||
889fca73 | 4055 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
4056 | { |
4057 | struct io_provide_buf *p = &req->pbuf; | |
4058 | struct io_ring_ctx *ctx = req->ctx; | |
4059 | struct io_buffer *head; | |
4060 | int ret = 0; | |
45d189c6 | 4061 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
067524e9 JA |
4062 | |
4063 | io_ring_submit_lock(ctx, !force_nonblock); | |
4064 | ||
4065 | lockdep_assert_held(&ctx->uring_lock); | |
4066 | ||
4067 | ret = -ENOENT; | |
9e15c3a0 | 4068 | head = xa_load(&ctx->io_buffers, p->bgid); |
067524e9 JA |
4069 | if (head) |
4070 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 | 4071 | if (ret < 0) |
93d2bcd2 | 4072 | req_set_fail(req); |
067524e9 | 4073 | |
9fb8cb49 PB |
4074 | /* complete before unlock, IOPOLL may need the lock */ |
4075 | __io_req_complete(req, issue_flags, ret, 0); | |
4076 | io_ring_submit_unlock(ctx, !force_nonblock); | |
067524e9 JA |
4077 | return 0; |
4078 | } | |
4079 | ||
ddf0322d JA |
4080 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4081 | const struct io_uring_sqe *sqe) | |
4082 | { | |
38134ada | 4083 | unsigned long size, tmp_check; |
ddf0322d JA |
4084 | struct io_provide_buf *p = &req->pbuf; |
4085 | u64 tmp; | |
4086 | ||
26578cda | 4087 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
ddf0322d JA |
4088 | return -EINVAL; |
4089 | ||
4090 | tmp = READ_ONCE(sqe->fd); | |
4091 | if (!tmp || tmp > USHRT_MAX) | |
4092 | return -E2BIG; | |
4093 | p->nbufs = tmp; | |
4094 | p->addr = READ_ONCE(sqe->addr); | |
4095 | p->len = READ_ONCE(sqe->len); | |
4096 | ||
38134ada PB |
4097 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
4098 | &size)) | |
4099 | return -EOVERFLOW; | |
4100 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) | |
4101 | return -EOVERFLOW; | |
4102 | ||
d81269fe PB |
4103 | size = (unsigned long)p->len * p->nbufs; |
4104 | if (!access_ok(u64_to_user_ptr(p->addr), size)) | |
ddf0322d JA |
4105 | return -EFAULT; |
4106 | ||
4107 | p->bgid = READ_ONCE(sqe->buf_group); | |
4108 | tmp = READ_ONCE(sqe->off); | |
4109 | if (tmp > USHRT_MAX) | |
4110 | return -E2BIG; | |
4111 | p->bid = tmp; | |
4112 | return 0; | |
4113 | } | |
4114 | ||
4115 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
4116 | { | |
4117 | struct io_buffer *buf; | |
4118 | u64 addr = pbuf->addr; | |
4119 | int i, bid = pbuf->bid; | |
4120 | ||
4121 | for (i = 0; i < pbuf->nbufs; i++) { | |
4122 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); | |
4123 | if (!buf) | |
4124 | break; | |
4125 | ||
4126 | buf->addr = addr; | |
d1f82808 | 4127 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
ddf0322d JA |
4128 | buf->bid = bid; |
4129 | addr += pbuf->len; | |
4130 | bid++; | |
4131 | if (!*head) { | |
4132 | INIT_LIST_HEAD(&buf->list); | |
4133 | *head = buf; | |
4134 | } else { | |
4135 | list_add_tail(&buf->list, &(*head)->list); | |
4136 | } | |
4137 | } | |
4138 | ||
4139 | return i ? i : -ENOMEM; | |
4140 | } | |
4141 | ||
889fca73 | 4142 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4143 | { |
4144 | struct io_provide_buf *p = &req->pbuf; | |
4145 | struct io_ring_ctx *ctx = req->ctx; | |
4146 | struct io_buffer *head, *list; | |
4147 | int ret = 0; | |
45d189c6 | 4148 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ddf0322d JA |
4149 | |
4150 | io_ring_submit_lock(ctx, !force_nonblock); | |
4151 | ||
4152 | lockdep_assert_held(&ctx->uring_lock); | |
4153 | ||
9e15c3a0 | 4154 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
ddf0322d JA |
4155 | |
4156 | ret = io_add_buffers(p, &head); | |
9e15c3a0 JA |
4157 | if (ret >= 0 && !list) { |
4158 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); | |
4159 | if (ret < 0) | |
067524e9 | 4160 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d | 4161 | } |
ddf0322d | 4162 | if (ret < 0) |
93d2bcd2 | 4163 | req_set_fail(req); |
9fb8cb49 PB |
4164 | /* complete before unlock, IOPOLL may need the lock */ |
4165 | __io_req_complete(req, issue_flags, ret, 0); | |
4166 | io_ring_submit_unlock(ctx, !force_nonblock); | |
ddf0322d | 4167 | return 0; |
cebdb986 JA |
4168 | } |
4169 | ||
3e4827b0 JA |
4170 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4171 | const struct io_uring_sqe *sqe) | |
4172 | { | |
4173 | #if defined(CONFIG_EPOLL) | |
26578cda | 4174 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
3e4827b0 | 4175 | return -EINVAL; |
2d74d042 | 4176 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4177 | return -EINVAL; |
3e4827b0 JA |
4178 | |
4179 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4180 | req->epoll.op = READ_ONCE(sqe->len); | |
4181 | req->epoll.fd = READ_ONCE(sqe->off); | |
4182 | ||
4183 | if (ep_op_has_event(req->epoll.op)) { | |
4184 | struct epoll_event __user *ev; | |
4185 | ||
4186 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4187 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4188 | return -EFAULT; | |
4189 | } | |
4190 | ||
4191 | return 0; | |
4192 | #else | |
4193 | return -EOPNOTSUPP; | |
4194 | #endif | |
4195 | } | |
4196 | ||
889fca73 | 4197 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4198 | { |
4199 | #if defined(CONFIG_EPOLL) | |
4200 | struct io_epoll *ie = &req->epoll; | |
4201 | int ret; | |
45d189c6 | 4202 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4203 | |
4204 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4205 | if (force_nonblock && ret == -EAGAIN) | |
4206 | return -EAGAIN; | |
4207 | ||
4208 | if (ret < 0) | |
93d2bcd2 | 4209 | req_set_fail(req); |
889fca73 | 4210 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4211 | return 0; |
4212 | #else | |
4213 | return -EOPNOTSUPP; | |
4214 | #endif | |
4215 | } | |
4216 | ||
c1ca757b JA |
4217 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4218 | { | |
4219 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
26578cda | 4220 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
c1ca757b | 4221 | return -EINVAL; |
3232dd02 PB |
4222 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4223 | return -EINVAL; | |
c1ca757b JA |
4224 | |
4225 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4226 | req->madvise.len = READ_ONCE(sqe->len); | |
4227 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4228 | return 0; | |
4229 | #else | |
4230 | return -EOPNOTSUPP; | |
4231 | #endif | |
4232 | } | |
4233 | ||
45d189c6 | 4234 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4235 | { |
4236 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4237 | struct io_madvise *ma = &req->madvise; | |
4238 | int ret; | |
4239 | ||
45d189c6 | 4240 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4241 | return -EAGAIN; |
4242 | ||
0726b01e | 4243 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b | 4244 | if (ret < 0) |
93d2bcd2 | 4245 | req_set_fail(req); |
e1e16097 | 4246 | io_req_complete(req, ret); |
c1ca757b JA |
4247 | return 0; |
4248 | #else | |
4249 | return -EOPNOTSUPP; | |
4250 | #endif | |
4251 | } | |
4252 | ||
4840e418 JA |
4253 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4254 | { | |
26578cda | 4255 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
4840e418 | 4256 | return -EINVAL; |
3232dd02 PB |
4257 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4258 | return -EINVAL; | |
4840e418 JA |
4259 | |
4260 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4261 | req->fadvise.len = READ_ONCE(sqe->len); | |
4262 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4263 | return 0; | |
4264 | } | |
4265 | ||
45d189c6 | 4266 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4267 | { |
4268 | struct io_fadvise *fa = &req->fadvise; | |
4269 | int ret; | |
4270 | ||
45d189c6 | 4271 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4272 | switch (fa->advice) { |
4273 | case POSIX_FADV_NORMAL: | |
4274 | case POSIX_FADV_RANDOM: | |
4275 | case POSIX_FADV_SEQUENTIAL: | |
4276 | break; | |
4277 | default: | |
4278 | return -EAGAIN; | |
4279 | } | |
4280 | } | |
4840e418 JA |
4281 | |
4282 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4283 | if (ret < 0) | |
93d2bcd2 | 4284 | req_set_fail(req); |
0bdf3398 | 4285 | __io_req_complete(req, issue_flags, ret, 0); |
4840e418 JA |
4286 | return 0; |
4287 | } | |
4288 | ||
eddc7ef5 JA |
4289 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4290 | { | |
2d74d042 | 4291 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4292 | return -EINVAL; |
26578cda | 4293 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
eddc7ef5 | 4294 | return -EINVAL; |
9c280f90 | 4295 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4296 | return -EBADF; |
eddc7ef5 | 4297 | |
1d9e1288 BM |
4298 | req->statx.dfd = READ_ONCE(sqe->fd); |
4299 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4300 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4301 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4302 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4303 | |
4304 | return 0; | |
4305 | } | |
4306 | ||
45d189c6 | 4307 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4308 | { |
1d9e1288 | 4309 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4310 | int ret; |
4311 | ||
59d70013 | 4312 | if (issue_flags & IO_URING_F_NONBLOCK) |
eddc7ef5 JA |
4313 | return -EAGAIN; |
4314 | ||
e62753e4 BM |
4315 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4316 | ctx->buffer); | |
eddc7ef5 | 4317 | |
eddc7ef5 | 4318 | if (ret < 0) |
93d2bcd2 | 4319 | req_set_fail(req); |
e1e16097 | 4320 | io_req_complete(req, ret); |
eddc7ef5 JA |
4321 | return 0; |
4322 | } | |
4323 | ||
b5dba59e JA |
4324 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4325 | { | |
14587a46 | 4326 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4327 | return -EINVAL; |
b5dba59e | 4328 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
26578cda | 4329 | sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) |
b5dba59e | 4330 | return -EINVAL; |
9c280f90 | 4331 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4332 | return -EBADF; |
b5dba59e JA |
4333 | |
4334 | req->close.fd = READ_ONCE(sqe->fd); | |
b5dba59e | 4335 | return 0; |
b5dba59e JA |
4336 | } |
4337 | ||
889fca73 | 4338 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4339 | { |
9eac1904 | 4340 | struct files_struct *files = current->files; |
3af73b28 | 4341 | struct io_close *close = &req->close; |
9eac1904 | 4342 | struct fdtable *fdt; |
a1fde923 PB |
4343 | struct file *file = NULL; |
4344 | int ret = -EBADF; | |
b5dba59e | 4345 | |
9eac1904 JA |
4346 | spin_lock(&files->file_lock); |
4347 | fdt = files_fdtable(files); | |
4348 | if (close->fd >= fdt->max_fds) { | |
4349 | spin_unlock(&files->file_lock); | |
4350 | goto err; | |
4351 | } | |
4352 | file = fdt->fd[close->fd]; | |
a1fde923 | 4353 | if (!file || file->f_op == &io_uring_fops) { |
9eac1904 JA |
4354 | spin_unlock(&files->file_lock); |
4355 | file = NULL; | |
4356 | goto err; | |
3af73b28 | 4357 | } |
b5dba59e JA |
4358 | |
4359 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 4360 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 4361 | spin_unlock(&files->file_lock); |
0bf0eefd | 4362 | return -EAGAIN; |
a2100672 | 4363 | } |
b5dba59e | 4364 | |
9eac1904 JA |
4365 | ret = __close_fd_get_file(close->fd, &file); |
4366 | spin_unlock(&files->file_lock); | |
4367 | if (ret < 0) { | |
4368 | if (ret == -ENOENT) | |
4369 | ret = -EBADF; | |
4370 | goto err; | |
4371 | } | |
4372 | ||
3af73b28 | 4373 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4374 | ret = filp_close(file, current->files); |
4375 | err: | |
3af73b28 | 4376 | if (ret < 0) |
93d2bcd2 | 4377 | req_set_fail(req); |
9eac1904 JA |
4378 | if (file) |
4379 | fput(file); | |
889fca73 | 4380 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 4381 | return 0; |
b5dba59e JA |
4382 | } |
4383 | ||
1155c76a | 4384 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4385 | { |
4386 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 4387 | |
5d17b4a4 JA |
4388 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4389 | return -EINVAL; | |
26578cda PB |
4390 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4391 | sqe->splice_fd_in)) | |
5d17b4a4 JA |
4392 | return -EINVAL; |
4393 | ||
8ed8d3c3 JA |
4394 | req->sync.off = READ_ONCE(sqe->off); |
4395 | req->sync.len = READ_ONCE(sqe->len); | |
4396 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4397 | return 0; |
4398 | } | |
4399 | ||
45d189c6 | 4400 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4401 | { |
8ed8d3c3 JA |
4402 | int ret; |
4403 | ||
ac45abc0 | 4404 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 4405 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4406 | return -EAGAIN; |
4407 | ||
9adbd45d | 4408 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4409 | req->sync.flags); |
4410 | if (ret < 0) | |
93d2bcd2 | 4411 | req_set_fail(req); |
e1e16097 | 4412 | io_req_complete(req, ret); |
5d17b4a4 JA |
4413 | return 0; |
4414 | } | |
4415 | ||
469956e8 | 4416 | #if defined(CONFIG_NET) |
02d27d89 PB |
4417 | static int io_setup_async_msg(struct io_kiocb *req, |
4418 | struct io_async_msghdr *kmsg) | |
4419 | { | |
e8c2bc1f JA |
4420 | struct io_async_msghdr *async_msg = req->async_data; |
4421 | ||
4422 | if (async_msg) | |
02d27d89 | 4423 | return -EAGAIN; |
e8c2bc1f | 4424 | if (io_alloc_async_data(req)) { |
257e84a5 | 4425 | kfree(kmsg->free_iov); |
02d27d89 PB |
4426 | return -ENOMEM; |
4427 | } | |
e8c2bc1f | 4428 | async_msg = req->async_data; |
02d27d89 | 4429 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4430 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 4431 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
4432 | /* if were using fast_iov, set it to the new one */ |
4433 | if (!async_msg->free_iov) | |
4434 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
4435 | ||
02d27d89 PB |
4436 | return -EAGAIN; |
4437 | } | |
4438 | ||
2ae523ed PB |
4439 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4440 | struct io_async_msghdr *iomsg) | |
4441 | { | |
2ae523ed | 4442 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 4443 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 4444 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 4445 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
4446 | } |
4447 | ||
93642ef8 PB |
4448 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
4449 | { | |
4450 | int ret; | |
4451 | ||
93642ef8 PB |
4452 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
4453 | if (!ret) | |
4454 | req->flags |= REQ_F_NEED_CLEANUP; | |
4455 | return ret; | |
4456 | } | |
4457 | ||
3529d8c2 | 4458 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4459 | { |
e47293fd | 4460 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 4461 | |
d2b6f48b PB |
4462 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4463 | return -EINVAL; | |
4464 | ||
270a5940 | 4465 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4466 | sr->len = READ_ONCE(sqe->len); |
04411806 PB |
4467 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4468 | if (sr->msg_flags & MSG_DONTWAIT) | |
4469 | req->flags |= REQ_F_NOWAIT; | |
3529d8c2 | 4470 | |
d8768362 JA |
4471 | #ifdef CONFIG_COMPAT |
4472 | if (req->ctx->compat) | |
4473 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4474 | #endif | |
93642ef8 | 4475 | return 0; |
03b1230c JA |
4476 | } |
4477 | ||
889fca73 | 4478 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4479 | { |
6b754c8b | 4480 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4481 | struct socket *sock; |
7a7cacba | 4482 | unsigned flags; |
0031275d | 4483 | int min_ret = 0; |
0fa03c62 JA |
4484 | int ret; |
4485 | ||
dba4a925 | 4486 | sock = sock_from_file(req->file); |
7a7cacba | 4487 | if (unlikely(!sock)) |
dba4a925 | 4488 | return -ENOTSOCK; |
3529d8c2 | 4489 | |
257e84a5 PB |
4490 | kmsg = req->async_data; |
4491 | if (!kmsg) { | |
7a7cacba PB |
4492 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
4493 | if (ret) | |
4494 | return ret; | |
4495 | kmsg = &iomsg; | |
0fa03c62 | 4496 | } |
0fa03c62 | 4497 | |
04411806 PB |
4498 | flags = req->sr_msg.msg_flags; |
4499 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4500 | flags |= MSG_DONTWAIT; |
0031275d SM |
4501 | if (flags & MSG_WAITALL) |
4502 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4503 | ||
7a7cacba | 4504 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
45d189c6 | 4505 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4506 | return io_setup_async_msg(req, kmsg); |
4507 | if (ret == -ERESTARTSYS) | |
4508 | ret = -EINTR; | |
0fa03c62 | 4509 | |
257e84a5 PB |
4510 | /* fast path, check for non-NULL to avoid function call */ |
4511 | if (kmsg->free_iov) | |
4512 | kfree(kmsg->free_iov); | |
99bc4c38 | 4513 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 4514 | if (ret < min_ret) |
93d2bcd2 | 4515 | req_set_fail(req); |
889fca73 | 4516 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 4517 | return 0; |
03b1230c | 4518 | } |
aa1fa28f | 4519 | |
889fca73 | 4520 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4521 | { |
7a7cacba PB |
4522 | struct io_sr_msg *sr = &req->sr_msg; |
4523 | struct msghdr msg; | |
4524 | struct iovec iov; | |
fddaface | 4525 | struct socket *sock; |
7a7cacba | 4526 | unsigned flags; |
0031275d | 4527 | int min_ret = 0; |
fddaface JA |
4528 | int ret; |
4529 | ||
dba4a925 | 4530 | sock = sock_from_file(req->file); |
7a7cacba | 4531 | if (unlikely(!sock)) |
dba4a925 | 4532 | return -ENOTSOCK; |
fddaface | 4533 | |
7a7cacba PB |
4534 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4535 | if (unlikely(ret)) | |
14db8411 | 4536 | return ret; |
fddaface | 4537 | |
7a7cacba PB |
4538 | msg.msg_name = NULL; |
4539 | msg.msg_control = NULL; | |
4540 | msg.msg_controllen = 0; | |
4541 | msg.msg_namelen = 0; | |
fddaface | 4542 | |
04411806 PB |
4543 | flags = req->sr_msg.msg_flags; |
4544 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4545 | flags |= MSG_DONTWAIT; |
0031275d SM |
4546 | if (flags & MSG_WAITALL) |
4547 | min_ret = iov_iter_count(&msg.msg_iter); | |
4548 | ||
7a7cacba PB |
4549 | msg.msg_flags = flags; |
4550 | ret = sock_sendmsg(sock, &msg); | |
45d189c6 | 4551 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4552 | return -EAGAIN; |
4553 | if (ret == -ERESTARTSYS) | |
4554 | ret = -EINTR; | |
fddaface | 4555 | |
0031275d | 4556 | if (ret < min_ret) |
93d2bcd2 | 4557 | req_set_fail(req); |
889fca73 | 4558 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 4559 | return 0; |
fddaface JA |
4560 | } |
4561 | ||
1400e697 PB |
4562 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4563 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4564 | { |
4565 | struct io_sr_msg *sr = &req->sr_msg; | |
4566 | struct iovec __user *uiov; | |
4567 | size_t iov_len; | |
4568 | int ret; | |
4569 | ||
1400e697 PB |
4570 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4571 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4572 | if (ret) |
4573 | return ret; | |
4574 | ||
4575 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4576 | if (iov_len > 1) | |
4577 | return -EINVAL; | |
5476dfed | 4578 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4579 | return -EFAULT; |
5476dfed | 4580 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 4581 | iomsg->free_iov = NULL; |
52de1fe1 | 4582 | } else { |
257e84a5 | 4583 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4584 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 4585 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 4586 | false); |
52de1fe1 JA |
4587 | if (ret > 0) |
4588 | ret = 0; | |
4589 | } | |
4590 | ||
4591 | return ret; | |
4592 | } | |
4593 | ||
4594 | #ifdef CONFIG_COMPAT | |
4595 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4596 | struct io_async_msghdr *iomsg) |
52de1fe1 | 4597 | { |
52de1fe1 JA |
4598 | struct io_sr_msg *sr = &req->sr_msg; |
4599 | struct compat_iovec __user *uiov; | |
4600 | compat_uptr_t ptr; | |
4601 | compat_size_t len; | |
4602 | int ret; | |
4603 | ||
4af3417a PB |
4604 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
4605 | &ptr, &len); | |
52de1fe1 JA |
4606 | if (ret) |
4607 | return ret; | |
4608 | ||
4609 | uiov = compat_ptr(ptr); | |
4610 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4611 | compat_ssize_t clen; | |
4612 | ||
4613 | if (len > 1) | |
4614 | return -EINVAL; | |
4615 | if (!access_ok(uiov, sizeof(*uiov))) | |
4616 | return -EFAULT; | |
4617 | if (__get_user(clen, &uiov->iov_len)) | |
4618 | return -EFAULT; | |
4619 | if (clen < 0) | |
4620 | return -EINVAL; | |
2d280bc8 | 4621 | sr->len = clen; |
257e84a5 | 4622 | iomsg->free_iov = NULL; |
52de1fe1 | 4623 | } else { |
257e84a5 | 4624 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4625 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 4626 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 4627 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
4628 | if (ret < 0) |
4629 | return ret; | |
4630 | } | |
4631 | ||
4632 | return 0; | |
4633 | } | |
4634 | #endif | |
4635 | ||
1400e697 PB |
4636 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4637 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4638 | { |
1400e697 | 4639 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
4640 | |
4641 | #ifdef CONFIG_COMPAT | |
4642 | if (req->ctx->compat) | |
1400e697 | 4643 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4644 | #endif |
52de1fe1 | 4645 | |
1400e697 | 4646 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4647 | } |
4648 | ||
bcda7baa | 4649 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4650 | bool needs_lock) |
bcda7baa JA |
4651 | { |
4652 | struct io_sr_msg *sr = &req->sr_msg; | |
4653 | struct io_buffer *kbuf; | |
4654 | ||
bcda7baa JA |
4655 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
4656 | if (IS_ERR(kbuf)) | |
4657 | return kbuf; | |
4658 | ||
4659 | sr->kbuf = kbuf; | |
4660 | req->flags |= REQ_F_BUFFER_SELECTED; | |
bcda7baa | 4661 | return kbuf; |
fddaface JA |
4662 | } |
4663 | ||
7fbb1b54 PB |
4664 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4665 | { | |
4666 | return io_put_kbuf(req, req->sr_msg.kbuf); | |
4667 | } | |
4668 | ||
93642ef8 | 4669 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 4670 | { |
99bc4c38 | 4671 | int ret; |
3529d8c2 | 4672 | |
93642ef8 PB |
4673 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
4674 | if (!ret) | |
4675 | req->flags |= REQ_F_NEED_CLEANUP; | |
4676 | return ret; | |
4677 | } | |
4678 | ||
4679 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4680 | { | |
4681 | struct io_sr_msg *sr = &req->sr_msg; | |
4682 | ||
d2b6f48b PB |
4683 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4684 | return -EINVAL; | |
4685 | ||
270a5940 | 4686 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4687 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4688 | sr->bgid = READ_ONCE(sqe->buf_group); |
04411806 PB |
4689 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4690 | if (sr->msg_flags & MSG_DONTWAIT) | |
4691 | req->flags |= REQ_F_NOWAIT; | |
06b76d44 | 4692 | |
d8768362 JA |
4693 | #ifdef CONFIG_COMPAT |
4694 | if (req->ctx->compat) | |
4695 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4696 | #endif | |
93642ef8 | 4697 | return 0; |
aa1fa28f JA |
4698 | } |
4699 | ||
889fca73 | 4700 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4701 | { |
6b754c8b | 4702 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4703 | struct socket *sock; |
7fbb1b54 | 4704 | struct io_buffer *kbuf; |
7a7cacba | 4705 | unsigned flags; |
0031275d | 4706 | int min_ret = 0; |
52de1fe1 | 4707 | int ret, cflags = 0; |
45d189c6 | 4708 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 4709 | |
dba4a925 | 4710 | sock = sock_from_file(req->file); |
7a7cacba | 4711 | if (unlikely(!sock)) |
dba4a925 | 4712 | return -ENOTSOCK; |
3529d8c2 | 4713 | |
257e84a5 PB |
4714 | kmsg = req->async_data; |
4715 | if (!kmsg) { | |
7a7cacba PB |
4716 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
4717 | if (ret) | |
681fda8d | 4718 | return ret; |
7a7cacba PB |
4719 | kmsg = &iomsg; |
4720 | } | |
03b1230c | 4721 | |
bc02ef33 | 4722 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4723 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4724 | if (IS_ERR(kbuf)) |
52de1fe1 | 4725 | return PTR_ERR(kbuf); |
7a7cacba | 4726 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
4727 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
4728 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
4729 | 1, req->sr_msg.len); |
4730 | } | |
52de1fe1 | 4731 | |
04411806 PB |
4732 | flags = req->sr_msg.msg_flags; |
4733 | if (force_nonblock) | |
7a7cacba | 4734 | flags |= MSG_DONTWAIT; |
0031275d SM |
4735 | if (flags & MSG_WAITALL) |
4736 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4737 | ||
7a7cacba PB |
4738 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4739 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4740 | if (force_nonblock && ret == -EAGAIN) |
4741 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4742 | if (ret == -ERESTARTSYS) |
4743 | ret = -EINTR; | |
03b1230c | 4744 | |
7fbb1b54 PB |
4745 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4746 | cflags = io_put_recv_kbuf(req); | |
257e84a5 PB |
4747 | /* fast path, check for non-NULL to avoid function call */ |
4748 | if (kmsg->free_iov) | |
4749 | kfree(kmsg->free_iov); | |
99bc4c38 | 4750 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 4751 | if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
93d2bcd2 | 4752 | req_set_fail(req); |
889fca73 | 4753 | __io_req_complete(req, issue_flags, ret, cflags); |
03b1230c | 4754 | return 0; |
0fa03c62 | 4755 | } |
5d17b4a4 | 4756 | |
889fca73 | 4757 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4758 | { |
6b754c8b | 4759 | struct io_buffer *kbuf; |
7a7cacba PB |
4760 | struct io_sr_msg *sr = &req->sr_msg; |
4761 | struct msghdr msg; | |
4762 | void __user *buf = sr->buf; | |
fddaface | 4763 | struct socket *sock; |
7a7cacba PB |
4764 | struct iovec iov; |
4765 | unsigned flags; | |
0031275d | 4766 | int min_ret = 0; |
bcda7baa | 4767 | int ret, cflags = 0; |
45d189c6 | 4768 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 4769 | |
dba4a925 | 4770 | sock = sock_from_file(req->file); |
7a7cacba | 4771 | if (unlikely(!sock)) |
dba4a925 | 4772 | return -ENOTSOCK; |
fddaface | 4773 | |
bc02ef33 | 4774 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4775 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
4776 | if (IS_ERR(kbuf)) |
4777 | return PTR_ERR(kbuf); | |
7a7cacba | 4778 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 4779 | } |
bcda7baa | 4780 | |
7a7cacba | 4781 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
4782 | if (unlikely(ret)) |
4783 | goto out_free; | |
fddaface | 4784 | |
7a7cacba PB |
4785 | msg.msg_name = NULL; |
4786 | msg.msg_control = NULL; | |
4787 | msg.msg_controllen = 0; | |
4788 | msg.msg_namelen = 0; | |
4789 | msg.msg_iocb = NULL; | |
4790 | msg.msg_flags = 0; | |
fddaface | 4791 | |
04411806 PB |
4792 | flags = req->sr_msg.msg_flags; |
4793 | if (force_nonblock) | |
7a7cacba | 4794 | flags |= MSG_DONTWAIT; |
0031275d SM |
4795 | if (flags & MSG_WAITALL) |
4796 | min_ret = iov_iter_count(&msg.msg_iter); | |
4797 | ||
7a7cacba PB |
4798 | ret = sock_recvmsg(sock, &msg, flags); |
4799 | if (force_nonblock && ret == -EAGAIN) | |
4800 | return -EAGAIN; | |
4801 | if (ret == -ERESTARTSYS) | |
4802 | ret = -EINTR; | |
14c32eee | 4803 | out_free: |
7fbb1b54 PB |
4804 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4805 | cflags = io_put_recv_kbuf(req); | |
0031275d | 4806 | if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
93d2bcd2 | 4807 | req_set_fail(req); |
889fca73 | 4808 | __io_req_complete(req, issue_flags, ret, cflags); |
fddaface | 4809 | return 0; |
fddaface JA |
4810 | } |
4811 | ||
3529d8c2 | 4812 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 4813 | { |
8ed8d3c3 JA |
4814 | struct io_accept *accept = &req->accept; |
4815 | ||
14587a46 | 4816 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 4817 | return -EINVAL; |
aaa4db12 | 4818 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
4819 | return -EINVAL; |
4820 | ||
d55e5f5b JA |
4821 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4822 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 4823 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 4824 | accept->nofile = rlimit(RLIMIT_NOFILE); |
a7083ad5 | 4825 | |
aaa4db12 PB |
4826 | accept->file_slot = READ_ONCE(sqe->file_index); |
4827 | if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) || | |
4828 | (accept->flags & SOCK_CLOEXEC))) | |
4829 | return -EINVAL; | |
a7083ad5 PB |
4830 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
4831 | return -EINVAL; | |
4832 | if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) | |
4833 | accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; | |
8ed8d3c3 | 4834 | return 0; |
8ed8d3c3 | 4835 | } |
17f2fe35 | 4836 | |
889fca73 | 4837 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
4838 | { |
4839 | struct io_accept *accept = &req->accept; | |
45d189c6 | 4840 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 4841 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
aaa4db12 | 4842 | bool fixed = !!accept->file_slot; |
a7083ad5 PB |
4843 | struct file *file; |
4844 | int ret, fd; | |
8ed8d3c3 | 4845 | |
e697deed JX |
4846 | if (req->file->f_flags & O_NONBLOCK) |
4847 | req->flags |= REQ_F_NOWAIT; | |
4848 | ||
aaa4db12 PB |
4849 | if (!fixed) { |
4850 | fd = __get_unused_fd_flags(accept->flags, accept->nofile); | |
4851 | if (unlikely(fd < 0)) | |
4852 | return fd; | |
4853 | } | |
a7083ad5 PB |
4854 | file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, |
4855 | accept->flags); | |
4856 | if (IS_ERR(file)) { | |
aaa4db12 PB |
4857 | if (!fixed) |
4858 | put_unused_fd(fd); | |
a7083ad5 PB |
4859 | ret = PTR_ERR(file); |
4860 | if (ret == -EAGAIN && force_nonblock) | |
4861 | return -EAGAIN; | |
ac45abc0 PB |
4862 | if (ret == -ERESTARTSYS) |
4863 | ret = -EINTR; | |
93d2bcd2 | 4864 | req_set_fail(req); |
aaa4db12 | 4865 | } else if (!fixed) { |
a7083ad5 PB |
4866 | fd_install(fd, file); |
4867 | ret = fd; | |
aaa4db12 PB |
4868 | } else { |
4869 | ret = io_install_fixed_file(req, file, issue_flags, | |
4870 | accept->file_slot - 1); | |
ac45abc0 | 4871 | } |
889fca73 | 4872 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 4873 | return 0; |
8ed8d3c3 JA |
4874 | } |
4875 | ||
93642ef8 PB |
4876 | static int io_connect_prep_async(struct io_kiocb *req) |
4877 | { | |
4878 | struct io_async_connect *io = req->async_data; | |
4879 | struct io_connect *conn = &req->connect; | |
4880 | ||
4881 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
4882 | } | |
4883 | ||
3529d8c2 | 4884 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 4885 | { |
3529d8c2 | 4886 | struct io_connect *conn = &req->connect; |
f499a021 | 4887 | |
14587a46 | 4888 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 | 4889 | return -EINVAL; |
26578cda PB |
4890 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
4891 | sqe->splice_fd_in) | |
3fbb51c1 JA |
4892 | return -EINVAL; |
4893 | ||
3529d8c2 JA |
4894 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4895 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 4896 | return 0; |
f499a021 JA |
4897 | } |
4898 | ||
889fca73 | 4899 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 4900 | { |
e8c2bc1f | 4901 | struct io_async_connect __io, *io; |
f8e85cf2 | 4902 | unsigned file_flags; |
3fbb51c1 | 4903 | int ret; |
45d189c6 | 4904 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 4905 | |
e8c2bc1f JA |
4906 | if (req->async_data) { |
4907 | io = req->async_data; | |
f499a021 | 4908 | } else { |
3529d8c2 JA |
4909 | ret = move_addr_to_kernel(req->connect.addr, |
4910 | req->connect.addr_len, | |
e8c2bc1f | 4911 | &__io.address); |
f499a021 JA |
4912 | if (ret) |
4913 | goto out; | |
4914 | io = &__io; | |
4915 | } | |
4916 | ||
3fbb51c1 JA |
4917 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
4918 | ||
e8c2bc1f | 4919 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 4920 | req->connect.addr_len, file_flags); |
87f80d62 | 4921 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 4922 | if (req->async_data) |
b7bb4f7d | 4923 | return -EAGAIN; |
e8c2bc1f | 4924 | if (io_alloc_async_data(req)) { |
f499a021 JA |
4925 | ret = -ENOMEM; |
4926 | goto out; | |
4927 | } | |
e8c2bc1f | 4928 | memcpy(req->async_data, &__io, sizeof(__io)); |
f8e85cf2 | 4929 | return -EAGAIN; |
f499a021 | 4930 | } |
f8e85cf2 JA |
4931 | if (ret == -ERESTARTSYS) |
4932 | ret = -EINTR; | |
f499a021 | 4933 | out: |
4e88d6e7 | 4934 | if (ret < 0) |
93d2bcd2 | 4935 | req_set_fail(req); |
889fca73 | 4936 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 4937 | return 0; |
469956e8 Y |
4938 | } |
4939 | #else /* !CONFIG_NET */ | |
99a10081 JA |
4940 | #define IO_NETOP_FN(op) \ |
4941 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
4942 | { \ | |
4943 | return -EOPNOTSUPP; \ | |
4944 | } | |
4945 | ||
4946 | #define IO_NETOP_PREP(op) \ | |
4947 | IO_NETOP_FN(op) \ | |
4948 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
4949 | { \ | |
4950 | return -EOPNOTSUPP; \ | |
4951 | } \ | |
4952 | ||
4953 | #define IO_NETOP_PREP_ASYNC(op) \ | |
4954 | IO_NETOP_PREP(op) \ | |
4955 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
4956 | { \ | |
4957 | return -EOPNOTSUPP; \ | |
4958 | } | |
4959 | ||
4960 | IO_NETOP_PREP_ASYNC(sendmsg); | |
4961 | IO_NETOP_PREP_ASYNC(recvmsg); | |
4962 | IO_NETOP_PREP_ASYNC(connect); | |
4963 | IO_NETOP_PREP(accept); | |
4964 | IO_NETOP_FN(send); | |
4965 | IO_NETOP_FN(recv); | |
469956e8 | 4966 | #endif /* CONFIG_NET */ |
f8e85cf2 | 4967 | |
d7718a9d JA |
4968 | struct io_poll_table { |
4969 | struct poll_table_struct pt; | |
4970 | struct io_kiocb *req; | |
68b11e8b | 4971 | int nr_entries; |
d7718a9d JA |
4972 | int error; |
4973 | }; | |
ce593a6c | 4974 | |
d7718a9d | 4975 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
5b0a6acc | 4976 | __poll_t mask, io_req_tw_func_t func) |
d7718a9d | 4977 | { |
d7718a9d JA |
4978 | /* for instances that support it check for an event match first: */ |
4979 | if (mask && !(mask & poll->events)) | |
4980 | return 0; | |
4981 | ||
4982 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
4983 | ||
4984 | list_del_init(&poll->wait.entry); | |
4985 | ||
d7718a9d | 4986 | req->result = mask; |
5b0a6acc | 4987 | req->io_task_work.func = func; |
6d816e08 | 4988 | |
d7718a9d | 4989 | /* |
e3aabf95 JA |
4990 | * If this fails, then the task is exiting. When a task exits, the |
4991 | * work gets canceled, so just cancel this request as well instead | |
4992 | * of executing it. We can't safely execute it anyway, as we may not | |
4993 | * have the needed state needed for it anyway. | |
d7718a9d | 4994 | */ |
e09ee510 | 4995 | io_req_task_work_add(req); |
d7718a9d JA |
4996 | return 1; |
4997 | } | |
4998 | ||
74ce6ce4 JA |
4999 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
5000 | __acquires(&req->ctx->completion_lock) | |
5001 | { | |
5002 | struct io_ring_ctx *ctx = req->ctx; | |
5003 | ||
316319e8 | 5004 | /* req->task == current here, checking PF_EXITING is safe */ |
e09ee510 PB |
5005 | if (unlikely(req->task->flags & PF_EXITING)) |
5006 | WRITE_ONCE(poll->canceled, true); | |
5007 | ||
74ce6ce4 JA |
5008 | if (!req->result && !READ_ONCE(poll->canceled)) { |
5009 | struct poll_table_struct pt = { ._key = poll->events }; | |
5010 | ||
5011 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
5012 | } | |
5013 | ||
79ebeaee | 5014 | spin_lock(&ctx->completion_lock); |
74ce6ce4 JA |
5015 | if (!req->result && !READ_ONCE(poll->canceled)) { |
5016 | add_wait_queue(poll->head, &poll->wait); | |
5017 | return true; | |
5018 | } | |
5019 | ||
5020 | return false; | |
5021 | } | |
5022 | ||
d4e7cd36 | 5023 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5024 | { |
e8c2bc1f | 5025 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5026 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5027 | return req->async_data; |
d4e7cd36 JA |
5028 | return req->apoll->double_poll; |
5029 | } | |
5030 | ||
5031 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5032 | { | |
5033 | if (req->opcode == IORING_OP_POLL_ADD) | |
5034 | return &req->poll; | |
5035 | return &req->apoll->poll; | |
5036 | } | |
5037 | ||
5038 | static void io_poll_remove_double(struct io_kiocb *req) | |
e07785b0 | 5039 | __must_hold(&req->ctx->completion_lock) |
d4e7cd36 JA |
5040 | { |
5041 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
5042 | |
5043 | lockdep_assert_held(&req->ctx->completion_lock); | |
5044 | ||
5045 | if (poll && poll->head) { | |
5046 | struct wait_queue_head *head = poll->head; | |
5047 | ||
79ebeaee | 5048 | spin_lock_irq(&head->lock); |
18bceab1 JA |
5049 | list_del_init(&poll->wait.entry); |
5050 | if (poll->wait.private) | |
de9b4cca | 5051 | req_ref_put(req); |
18bceab1 | 5052 | poll->head = NULL; |
79ebeaee | 5053 | spin_unlock_irq(&head->lock); |
18bceab1 JA |
5054 | } |
5055 | } | |
5056 | ||
e27414be | 5057 | static bool io_poll_complete(struct io_kiocb *req, __poll_t mask) |
e07785b0 | 5058 | __must_hold(&req->ctx->completion_lock) |
18bceab1 JA |
5059 | { |
5060 | struct io_ring_ctx *ctx = req->ctx; | |
88e41cf9 | 5061 | unsigned flags = IORING_CQE_F_MORE; |
e27414be | 5062 | int error; |
18bceab1 | 5063 | |
e27414be | 5064 | if (READ_ONCE(req->poll.canceled)) { |
45ab03b1 | 5065 | error = -ECANCELED; |
88e41cf9 | 5066 | req->poll.events |= EPOLLONESHOT; |
e27414be | 5067 | } else { |
5082620f | 5068 | error = mangle_poll(mask); |
e27414be | 5069 | } |
b69de288 JA |
5070 | if (req->poll.events & EPOLLONESHOT) |
5071 | flags = 0; | |
d4d19c19 | 5072 | if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) { |
88e41cf9 JA |
5073 | req->poll.done = true; |
5074 | flags = 0; | |
5075 | } | |
7b289c38 HX |
5076 | if (flags & IORING_CQE_F_MORE) |
5077 | ctx->cq_extra++; | |
18bceab1 | 5078 | |
18bceab1 | 5079 | io_commit_cqring(ctx); |
88e41cf9 | 5080 | return !(flags & IORING_CQE_F_MORE); |
18bceab1 JA |
5081 | } |
5082 | ||
f237c30a | 5083 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 JA |
5084 | { |
5085 | struct io_ring_ctx *ctx = req->ctx; | |
dd221f46 | 5086 | struct io_kiocb *nxt; |
18bceab1 JA |
5087 | |
5088 | if (io_poll_rewait(req, &req->poll)) { | |
79ebeaee | 5089 | spin_unlock(&ctx->completion_lock); |
dd221f46 | 5090 | } else { |
f40b964a | 5091 | bool done; |
18bceab1 | 5092 | |
e27414be | 5093 | done = io_poll_complete(req, req->result); |
88e41cf9 | 5094 | if (done) { |
a890d01e | 5095 | io_poll_remove_double(req); |
88e41cf9 | 5096 | hash_del(&req->hash_node); |
f40b964a | 5097 | } else { |
88e41cf9 JA |
5098 | req->result = 0; |
5099 | add_wait_queue(req->poll.head, &req->poll.wait); | |
5100 | } | |
79ebeaee | 5101 | spin_unlock(&ctx->completion_lock); |
dd221f46 | 5102 | io_cqring_ev_posted(ctx); |
18bceab1 | 5103 | |
88e41cf9 JA |
5104 | if (done) { |
5105 | nxt = io_put_req_find_next(req); | |
5106 | if (nxt) | |
f237c30a | 5107 | io_req_task_submit(nxt, locked); |
88e41cf9 | 5108 | } |
dd221f46 | 5109 | } |
18bceab1 JA |
5110 | } |
5111 | ||
5112 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
5113 | int sync, void *key) | |
5114 | { | |
5115 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 5116 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 | 5117 | __poll_t mask = key_to_poll(key); |
79ebeaee | 5118 | unsigned long flags; |
18bceab1 JA |
5119 | |
5120 | /* for instances that support it check for an event match first: */ | |
5121 | if (mask && !(mask & poll->events)) | |
5122 | return 0; | |
88e41cf9 JA |
5123 | if (!(poll->events & EPOLLONESHOT)) |
5124 | return poll->wait.func(&poll->wait, mode, sync, key); | |
18bceab1 | 5125 | |
8706e04e JA |
5126 | list_del_init(&wait->entry); |
5127 | ||
9ce85ef2 | 5128 | if (poll->head) { |
18bceab1 JA |
5129 | bool done; |
5130 | ||
79ebeaee | 5131 | spin_lock_irqsave(&poll->head->lock, flags); |
807abcb0 | 5132 | done = list_empty(&poll->wait.entry); |
18bceab1 | 5133 | if (!done) |
807abcb0 | 5134 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
5135 | /* make sure double remove sees this as being gone */ |
5136 | wait->private = NULL; | |
79ebeaee | 5137 | spin_unlock_irqrestore(&poll->head->lock, flags); |
c8b5e260 JA |
5138 | if (!done) { |
5139 | /* use wait func handler, so it matches the rq type */ | |
5140 | poll->wait.func(&poll->wait, mode, sync, key); | |
5141 | } | |
18bceab1 | 5142 | } |
de9b4cca | 5143 | req_ref_put(req); |
18bceab1 JA |
5144 | return 1; |
5145 | } | |
5146 | ||
5147 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
5148 | wait_queue_func_t wake_func) | |
5149 | { | |
5150 | poll->head = NULL; | |
5151 | poll->done = false; | |
5152 | poll->canceled = false; | |
464dca61 JA |
5153 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
5154 | /* mask in events that we always want/need */ | |
5155 | poll->events = events | IO_POLL_UNMASK; | |
18bceab1 JA |
5156 | INIT_LIST_HEAD(&poll->wait.entry); |
5157 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
5158 | } | |
5159 | ||
5160 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
5161 | struct wait_queue_head *head, |
5162 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
5163 | { |
5164 | struct io_kiocb *req = pt->req; | |
5165 | ||
5166 | /* | |
68b11e8b PB |
5167 | * The file being polled uses multiple waitqueues for poll handling |
5168 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb | |
5169 | * if this happens. | |
18bceab1 | 5170 | */ |
68b11e8b | 5171 | if (unlikely(pt->nr_entries)) { |
58852d4d PB |
5172 | struct io_poll_iocb *poll_one = poll; |
5173 | ||
23a65db8 PB |
5174 | /* double add on the same waitqueue head, ignore */ |
5175 | if (poll_one->head == head) | |
5176 | return; | |
18bceab1 | 5177 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5178 | if (*poll_ptr) { |
23a65db8 PB |
5179 | if ((*poll_ptr)->head == head) |
5180 | return; | |
18bceab1 JA |
5181 | pt->error = -EINVAL; |
5182 | return; | |
5183 | } | |
ea6a693d JA |
5184 | /* |
5185 | * Can't handle multishot for double wait for now, turn it | |
5186 | * into one-shot mode. | |
5187 | */ | |
7a274727 PB |
5188 | if (!(poll_one->events & EPOLLONESHOT)) |
5189 | poll_one->events |= EPOLLONESHOT; | |
18bceab1 JA |
5190 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
5191 | if (!poll) { | |
5192 | pt->error = -ENOMEM; | |
5193 | return; | |
5194 | } | |
58852d4d | 5195 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
de9b4cca | 5196 | req_ref_get(req); |
18bceab1 | 5197 | poll->wait.private = req; |
807abcb0 | 5198 | *poll_ptr = poll; |
18bceab1 JA |
5199 | } |
5200 | ||
68b11e8b | 5201 | pt->nr_entries++; |
18bceab1 | 5202 | poll->head = head; |
a31eb4a2 JX |
5203 | |
5204 | if (poll->events & EPOLLEXCLUSIVE) | |
5205 | add_wait_queue_exclusive(head, &poll->wait); | |
5206 | else | |
5207 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5208 | } |
5209 | ||
5210 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5211 | struct poll_table_struct *p) | |
5212 | { | |
5213 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5214 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5215 | |
807abcb0 | 5216 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5217 | } |
5218 | ||
f237c30a | 5219 | static void io_async_task_func(struct io_kiocb *req, bool *locked) |
d7718a9d | 5220 | { |
d7718a9d JA |
5221 | struct async_poll *apoll = req->apoll; |
5222 | struct io_ring_ctx *ctx = req->ctx; | |
5223 | ||
236daeae | 5224 | trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data); |
d7718a9d | 5225 | |
74ce6ce4 | 5226 | if (io_poll_rewait(req, &apoll->poll)) { |
79ebeaee | 5227 | spin_unlock(&ctx->completion_lock); |
74ce6ce4 | 5228 | return; |
d7718a9d JA |
5229 | } |
5230 | ||
0ea13b44 | 5231 | hash_del(&req->hash_node); |
d4e7cd36 | 5232 | io_poll_remove_double(req); |
79ebeaee | 5233 | spin_unlock(&ctx->completion_lock); |
74ce6ce4 | 5234 | |
0be0b0e3 | 5235 | if (!READ_ONCE(apoll->poll.canceled)) |
f237c30a | 5236 | io_req_task_submit(req, locked); |
0be0b0e3 | 5237 | else |
2593553a | 5238 | io_req_complete_failed(req, -ECANCELED); |
d7718a9d JA |
5239 | } |
5240 | ||
5241 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5242 | void *key) | |
5243 | { | |
5244 | struct io_kiocb *req = wait->private; | |
5245 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5246 | ||
5247 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5248 | key_to_poll(key)); | |
5249 | ||
5250 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5251 | } | |
5252 | ||
5253 | static void io_poll_req_insert(struct io_kiocb *req) | |
5254 | { | |
5255 | struct io_ring_ctx *ctx = req->ctx; | |
5256 | struct hlist_head *list; | |
5257 | ||
5258 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5259 | hlist_add_head(&req->hash_node, list); | |
5260 | } | |
5261 | ||
5262 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5263 | struct io_poll_iocb *poll, | |
5264 | struct io_poll_table *ipt, __poll_t mask, | |
5265 | wait_queue_func_t wake_func) | |
5266 | __acquires(&ctx->completion_lock) | |
5267 | { | |
5268 | struct io_ring_ctx *ctx = req->ctx; | |
5269 | bool cancel = false; | |
5270 | ||
4d52f338 | 5271 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5272 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5273 | poll->file = req->file; |
18bceab1 | 5274 | poll->wait.private = req; |
d7718a9d JA |
5275 | |
5276 | ipt->pt._key = mask; | |
5277 | ipt->req = req; | |
68b11e8b PB |
5278 | ipt->error = 0; |
5279 | ipt->nr_entries = 0; | |
d7718a9d | 5280 | |
d7718a9d | 5281 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
68b11e8b PB |
5282 | if (unlikely(!ipt->nr_entries) && !ipt->error) |
5283 | ipt->error = -EINVAL; | |
d7718a9d | 5284 | |
79ebeaee | 5285 | spin_lock(&ctx->completion_lock); |
a890d01e | 5286 | if (ipt->error || (mask && (poll->events & EPOLLONESHOT))) |
46fee9ab | 5287 | io_poll_remove_double(req); |
d7718a9d | 5288 | if (likely(poll->head)) { |
79ebeaee | 5289 | spin_lock_irq(&poll->head->lock); |
d7718a9d JA |
5290 | if (unlikely(list_empty(&poll->wait.entry))) { |
5291 | if (ipt->error) | |
5292 | cancel = true; | |
5293 | ipt->error = 0; | |
5294 | mask = 0; | |
5295 | } | |
88e41cf9 | 5296 | if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error) |
d7718a9d JA |
5297 | list_del_init(&poll->wait.entry); |
5298 | else if (cancel) | |
5299 | WRITE_ONCE(poll->canceled, true); | |
5300 | else if (!poll->done) /* actually waiting for an event */ | |
5301 | io_poll_req_insert(req); | |
79ebeaee | 5302 | spin_unlock_irq(&poll->head->lock); |
d7718a9d JA |
5303 | } |
5304 | ||
5305 | return mask; | |
5306 | } | |
5307 | ||
59b735ae OL |
5308 | enum { |
5309 | IO_APOLL_OK, | |
5310 | IO_APOLL_ABORTED, | |
5311 | IO_APOLL_READY | |
5312 | }; | |
5313 | ||
5314 | static int io_arm_poll_handler(struct io_kiocb *req) | |
d7718a9d JA |
5315 | { |
5316 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5317 | struct io_ring_ctx *ctx = req->ctx; | |
5318 | struct async_poll *apoll; | |
5319 | struct io_poll_table ipt; | |
b2d9c3da | 5320 | __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI; |
9dab14b8 | 5321 | int rw; |
d7718a9d JA |
5322 | |
5323 | if (!req->file || !file_can_poll(req->file)) | |
59b735ae | 5324 | return IO_APOLL_ABORTED; |
24c74678 | 5325 | if (req->flags & REQ_F_POLLED) |
59b735ae | 5326 | return IO_APOLL_ABORTED; |
b2d9c3da PB |
5327 | if (!def->pollin && !def->pollout) |
5328 | return IO_APOLL_ABORTED; | |
5329 | ||
5330 | if (def->pollin) { | |
9dab14b8 | 5331 | rw = READ; |
b2d9c3da PB |
5332 | mask |= POLLIN | POLLRDNORM; |
5333 | ||
5334 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5335 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5336 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5337 | mask &= ~POLLIN; | |
5338 | } else { | |
9dab14b8 | 5339 | rw = WRITE; |
b2d9c3da PB |
5340 | mask |= POLLOUT | POLLWRNORM; |
5341 | } | |
5342 | ||
9dab14b8 | 5343 | /* if we can't nonblock try, then no point in arming a poll handler */ |
b191e2df | 5344 | if (!io_file_supports_nowait(req, rw)) |
59b735ae | 5345 | return IO_APOLL_ABORTED; |
d7718a9d JA |
5346 | |
5347 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
5348 | if (unlikely(!apoll)) | |
59b735ae | 5349 | return IO_APOLL_ABORTED; |
807abcb0 | 5350 | apoll->double_poll = NULL; |
d7718a9d | 5351 | req->apoll = apoll; |
b2d9c3da | 5352 | req->flags |= REQ_F_POLLED; |
d7718a9d | 5353 | ipt.pt._qproc = io_async_queue_proc; |
48dcd38d | 5354 | io_req_set_refcount(req); |
d7718a9d JA |
5355 | |
5356 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5357 | io_async_wake); | |
79ebeaee | 5358 | spin_unlock(&ctx->completion_lock); |
41a5169c HX |
5359 | if (ret || ipt.error) |
5360 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; | |
5361 | ||
236daeae OL |
5362 | trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data, |
5363 | mask, apoll->poll.events); | |
59b735ae | 5364 | return IO_APOLL_OK; |
d7718a9d JA |
5365 | } |
5366 | ||
5367 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
b2e720ac | 5368 | struct io_poll_iocb *poll, bool do_cancel) |
e07785b0 | 5369 | __must_hold(&req->ctx->completion_lock) |
221c5eb2 | 5370 | { |
b41e9852 | 5371 | bool do_complete = false; |
221c5eb2 | 5372 | |
5082620f JA |
5373 | if (!poll->head) |
5374 | return false; | |
79ebeaee | 5375 | spin_lock_irq(&poll->head->lock); |
b2e720ac JA |
5376 | if (do_cancel) |
5377 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5378 | if (!list_empty(&poll->wait.entry)) { |
5379 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5380 | do_complete = true; |
221c5eb2 | 5381 | } |
79ebeaee | 5382 | spin_unlock_irq(&poll->head->lock); |
3bfa5bcb | 5383 | hash_del(&req->hash_node); |
d7718a9d JA |
5384 | return do_complete; |
5385 | } | |
5386 | ||
5d709043 | 5387 | static bool io_poll_remove_one(struct io_kiocb *req) |
e07785b0 | 5388 | __must_hold(&req->ctx->completion_lock) |
d7718a9d JA |
5389 | { |
5390 | bool do_complete; | |
5391 | ||
d4e7cd36 | 5392 | io_poll_remove_double(req); |
e31001a3 | 5393 | do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true); |
d4e7cd36 | 5394 | |
b41e9852 | 5395 | if (do_complete) { |
d4d19c19 | 5396 | io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0); |
b41e9852 | 5397 | io_commit_cqring(req->ctx); |
93d2bcd2 | 5398 | req_set_fail(req); |
91c2f697 | 5399 | io_put_req_deferred(req); |
5d709043 | 5400 | } |
b41e9852 | 5401 | return do_complete; |
221c5eb2 JA |
5402 | } |
5403 | ||
76e1b642 JA |
5404 | /* |
5405 | * Returns true if we found and killed one or more poll requests | |
5406 | */ | |
6b81928d | 5407 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
3dd0c97a | 5408 | bool cancel_all) |
221c5eb2 | 5409 | { |
78076bb6 | 5410 | struct hlist_node *tmp; |
221c5eb2 | 5411 | struct io_kiocb *req; |
8e2e1faf | 5412 | int posted = 0, i; |
221c5eb2 | 5413 | |
79ebeaee | 5414 | spin_lock(&ctx->completion_lock); |
78076bb6 JA |
5415 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5416 | struct hlist_head *list; | |
5417 | ||
5418 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5419 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
3dd0c97a | 5420 | if (io_match_task(req, tsk, cancel_all)) |
f3606e3a JA |
5421 | posted += io_poll_remove_one(req); |
5422 | } | |
221c5eb2 | 5423 | } |
79ebeaee | 5424 | spin_unlock(&ctx->completion_lock); |
b41e9852 | 5425 | |
8e2e1faf JA |
5426 | if (posted) |
5427 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5428 | |
5429 | return posted != 0; | |
221c5eb2 JA |
5430 | } |
5431 | ||
9ba5fac8 PB |
5432 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5433 | bool poll_only) | |
e07785b0 | 5434 | __must_hold(&ctx->completion_lock) |
47f46768 | 5435 | { |
78076bb6 | 5436 | struct hlist_head *list; |
47f46768 JA |
5437 | struct io_kiocb *req; |
5438 | ||
78076bb6 JA |
5439 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5440 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5441 | if (sqe_addr != req->user_data) |
5442 | continue; | |
9ba5fac8 PB |
5443 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
5444 | continue; | |
b2cb805f | 5445 | return req; |
47f46768 | 5446 | } |
b2cb805f JA |
5447 | return NULL; |
5448 | } | |
5449 | ||
9ba5fac8 PB |
5450 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5451 | bool poll_only) | |
e07785b0 | 5452 | __must_hold(&ctx->completion_lock) |
b2cb805f JA |
5453 | { |
5454 | struct io_kiocb *req; | |
5455 | ||
9ba5fac8 | 5456 | req = io_poll_find(ctx, sqe_addr, poll_only); |
b2cb805f JA |
5457 | if (!req) |
5458 | return -ENOENT; | |
5459 | if (io_poll_remove_one(req)) | |
5460 | return 0; | |
5461 | ||
5462 | return -EALREADY; | |
47f46768 JA |
5463 | } |
5464 | ||
9096af3e PB |
5465 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
5466 | unsigned int flags) | |
5467 | { | |
5468 | u32 events; | |
47f46768 | 5469 | |
9096af3e PB |
5470 | events = READ_ONCE(sqe->poll32_events); |
5471 | #ifdef __BIG_ENDIAN | |
5472 | events = swahw32(events); | |
5473 | #endif | |
5474 | if (!(flags & IORING_POLL_ADD_MULTI)) | |
5475 | events |= EPOLLONESHOT; | |
5476 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); | |
47f46768 JA |
5477 | } |
5478 | ||
c5de0036 | 5479 | static int io_poll_update_prep(struct io_kiocb *req, |
3529d8c2 | 5480 | const struct io_uring_sqe *sqe) |
0969e783 | 5481 | { |
c5de0036 PB |
5482 | struct io_poll_update *upd = &req->poll_update; |
5483 | u32 flags; | |
5484 | ||
0969e783 JA |
5485 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5486 | return -EINVAL; | |
26578cda | 5487 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
c5de0036 PB |
5488 | return -EINVAL; |
5489 | flags = READ_ONCE(sqe->len); | |
5490 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | | |
5491 | IORING_POLL_ADD_MULTI)) | |
5492 | return -EINVAL; | |
5493 | /* meaningless without update */ | |
5494 | if (flags == IORING_POLL_ADD_MULTI) | |
0969e783 JA |
5495 | return -EINVAL; |
5496 | ||
c5de0036 PB |
5497 | upd->old_user_data = READ_ONCE(sqe->addr); |
5498 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; | |
5499 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; | |
221c5eb2 | 5500 | |
c5de0036 PB |
5501 | upd->new_user_data = READ_ONCE(sqe->off); |
5502 | if (!upd->update_user_data && upd->new_user_data) | |
5503 | return -EINVAL; | |
5504 | if (upd->update_events) | |
5505 | upd->events = io_poll_parse_events(sqe, flags); | |
5506 | else if (sqe->poll32_events) | |
5507 | return -EINVAL; | |
221c5eb2 | 5508 | |
221c5eb2 JA |
5509 | return 0; |
5510 | } | |
5511 | ||
221c5eb2 JA |
5512 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5513 | void *key) | |
5514 | { | |
c2f2eb7d JA |
5515 | struct io_kiocb *req = wait->private; |
5516 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5517 | |
d7718a9d | 5518 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5519 | } |
5520 | ||
221c5eb2 JA |
5521 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5522 | struct poll_table_struct *p) | |
5523 | { | |
5524 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5525 | ||
e8c2bc1f | 5526 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5527 | } |
5528 | ||
3529d8c2 | 5529 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5530 | { |
5531 | struct io_poll_iocb *poll = &req->poll; | |
c5de0036 | 5532 | u32 flags; |
221c5eb2 JA |
5533 | |
5534 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5535 | return -EINVAL; | |
c5de0036 | 5536 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
88e41cf9 JA |
5537 | return -EINVAL; |
5538 | flags = READ_ONCE(sqe->len); | |
c5de0036 | 5539 | if (flags & ~IORING_POLL_ADD_MULTI) |
221c5eb2 JA |
5540 | return -EINVAL; |
5541 | ||
48dcd38d | 5542 | io_req_set_refcount(req); |
c5de0036 | 5543 | poll->events = io_poll_parse_events(sqe, flags); |
0969e783 JA |
5544 | return 0; |
5545 | } | |
5546 | ||
61e98203 | 5547 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
5548 | { |
5549 | struct io_poll_iocb *poll = &req->poll; | |
5550 | struct io_ring_ctx *ctx = req->ctx; | |
5551 | struct io_poll_table ipt; | |
0969e783 | 5552 | __poll_t mask; |
0969e783 | 5553 | |
d7718a9d | 5554 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5555 | |
d7718a9d JA |
5556 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5557 | io_poll_wake); | |
221c5eb2 | 5558 | |
8c838788 | 5559 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5560 | ipt.error = 0; |
e27414be | 5561 | io_poll_complete(req, mask); |
221c5eb2 | 5562 | } |
79ebeaee | 5563 | spin_unlock(&ctx->completion_lock); |
221c5eb2 | 5564 | |
8c838788 JA |
5565 | if (mask) { |
5566 | io_cqring_ev_posted(ctx); | |
88e41cf9 JA |
5567 | if (poll->events & EPOLLONESHOT) |
5568 | io_put_req(req); | |
221c5eb2 | 5569 | } |
8c838788 | 5570 | return ipt.error; |
221c5eb2 JA |
5571 | } |
5572 | ||
c5de0036 | 5573 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
b69de288 JA |
5574 | { |
5575 | struct io_ring_ctx *ctx = req->ctx; | |
5576 | struct io_kiocb *preq; | |
cb3b200e | 5577 | bool completing; |
b69de288 JA |
5578 | int ret; |
5579 | ||
79ebeaee | 5580 | spin_lock(&ctx->completion_lock); |
9ba5fac8 | 5581 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
b69de288 JA |
5582 | if (!preq) { |
5583 | ret = -ENOENT; | |
5584 | goto err; | |
b69de288 | 5585 | } |
cb3b200e | 5586 | |
c5de0036 PB |
5587 | if (!req->poll_update.update_events && !req->poll_update.update_user_data) { |
5588 | completing = true; | |
5589 | ret = io_poll_remove_one(preq) ? 0 : -EALREADY; | |
5590 | goto err; | |
5591 | } | |
5592 | ||
cb3b200e JA |
5593 | /* |
5594 | * Don't allow racy completion with singleshot, as we cannot safely | |
5595 | * update those. For multishot, if we're racing with completion, just | |
5596 | * let completion re-add it. | |
5597 | */ | |
5598 | completing = !__io_poll_remove_one(preq, &preq->poll, false); | |
5599 | if (completing && (preq->poll.events & EPOLLONESHOT)) { | |
5600 | ret = -EALREADY; | |
5601 | goto err; | |
b69de288 JA |
5602 | } |
5603 | /* we now have a detached poll request. reissue. */ | |
5604 | ret = 0; | |
5605 | err: | |
b69de288 | 5606 | if (ret < 0) { |
79ebeaee | 5607 | spin_unlock(&ctx->completion_lock); |
93d2bcd2 | 5608 | req_set_fail(req); |
b69de288 JA |
5609 | io_req_complete(req, ret); |
5610 | return 0; | |
5611 | } | |
5612 | /* only mask one event flags, keep behavior flags */ | |
9d805892 | 5613 | if (req->poll_update.update_events) { |
b69de288 | 5614 | preq->poll.events &= ~0xffff; |
9d805892 | 5615 | preq->poll.events |= req->poll_update.events & 0xffff; |
b69de288 JA |
5616 | preq->poll.events |= IO_POLL_UNMASK; |
5617 | } | |
9d805892 PB |
5618 | if (req->poll_update.update_user_data) |
5619 | preq->user_data = req->poll_update.new_user_data; | |
79ebeaee | 5620 | spin_unlock(&ctx->completion_lock); |
cb3b200e | 5621 | |
b69de288 JA |
5622 | /* complete update request, we're done with it */ |
5623 | io_req_complete(req, ret); | |
5624 | ||
cb3b200e | 5625 | if (!completing) { |
c5de0036 | 5626 | ret = io_poll_add(preq, issue_flags); |
cb3b200e | 5627 | if (ret < 0) { |
93d2bcd2 | 5628 | req_set_fail(preq); |
cb3b200e JA |
5629 | io_req_complete(preq, ret); |
5630 | } | |
b69de288 JA |
5631 | } |
5632 | return 0; | |
5633 | } | |
5634 | ||
f237c30a | 5635 | static void io_req_task_timeout(struct io_kiocb *req, bool *locked) |
89850fce | 5636 | { |
89850fce | 5637 | req_set_fail(req); |
505657bc | 5638 | io_req_complete_post(req, -ETIME, 0); |
89850fce JA |
5639 | } |
5640 | ||
5262f567 JA |
5641 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5642 | { | |
ad8a48ac JA |
5643 | struct io_timeout_data *data = container_of(timer, |
5644 | struct io_timeout_data, timer); | |
5645 | struct io_kiocb *req = data->req; | |
5646 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5647 | unsigned long flags; |
5648 | ||
89850fce | 5649 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
a71976f3 | 5650 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5651 | atomic_set(&req->ctx->cq_timeouts, |
5652 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
89850fce | 5653 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
01cec8c1 | 5654 | |
89850fce JA |
5655 | req->io_task_work.func = io_req_task_timeout; |
5656 | io_req_task_work_add(req); | |
5262f567 JA |
5657 | return HRTIMER_NORESTART; |
5658 | } | |
5659 | ||
fbd15848 PB |
5660 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5661 | __u64 user_data) | |
89850fce | 5662 | __must_hold(&ctx->timeout_lock) |
f254ac04 | 5663 | { |
fbd15848 | 5664 | struct io_timeout_data *io; |
47f46768 | 5665 | struct io_kiocb *req; |
fd9c7bc5 | 5666 | bool found = false; |
f254ac04 | 5667 | |
135fcde8 | 5668 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
fd9c7bc5 PB |
5669 | found = user_data == req->user_data; |
5670 | if (found) | |
47f46768 | 5671 | break; |
47f46768 | 5672 | } |
fd9c7bc5 PB |
5673 | if (!found) |
5674 | return ERR_PTR(-ENOENT); | |
fbd15848 PB |
5675 | |
5676 | io = req->async_data; | |
fd9c7bc5 | 5677 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
fbd15848 | 5678 | return ERR_PTR(-EALREADY); |
a71976f3 | 5679 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5680 | return req; |
5681 | } | |
47f46768 | 5682 | |
fbd15848 | 5683 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
ec3c3d0f | 5684 | __must_hold(&ctx->completion_lock) |
89850fce | 5685 | __must_hold(&ctx->timeout_lock) |
fbd15848 PB |
5686 | { |
5687 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5688 | ||
5689 | if (IS_ERR(req)) | |
5690 | return PTR_ERR(req); | |
f254ac04 | 5691 | |
93d2bcd2 | 5692 | req_set_fail(req); |
d4d19c19 | 5693 | io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0); |
91c2f697 | 5694 | io_put_req_deferred(req); |
f254ac04 JA |
5695 | return 0; |
5696 | } | |
5697 | ||
9c8e11b3 PB |
5698 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5699 | struct timespec64 *ts, enum hrtimer_mode mode) | |
89850fce | 5700 | __must_hold(&ctx->timeout_lock) |
47f46768 | 5701 | { |
9c8e11b3 PB |
5702 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
5703 | struct io_timeout_data *data; | |
47f46768 | 5704 | |
9c8e11b3 PB |
5705 | if (IS_ERR(req)) |
5706 | return PTR_ERR(req); | |
47f46768 | 5707 | |
9c8e11b3 PB |
5708 | req->timeout.off = 0; /* noseq */ |
5709 | data = req->async_data; | |
5710 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
5711 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); | |
5712 | data->timer.function = io_timeout_fn; | |
5713 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
5714 | return 0; | |
47f46768 JA |
5715 | } |
5716 | ||
3529d8c2 JA |
5717 | static int io_timeout_remove_prep(struct io_kiocb *req, |
5718 | const struct io_uring_sqe *sqe) | |
b29472ee | 5719 | { |
9c8e11b3 PB |
5720 | struct io_timeout_rem *tr = &req->timeout_rem; |
5721 | ||
b29472ee JA |
5722 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5723 | return -EINVAL; | |
61710e43 DA |
5724 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5725 | return -EINVAL; | |
26578cda | 5726 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
b29472ee JA |
5727 | return -EINVAL; |
5728 | ||
9c8e11b3 PB |
5729 | tr->addr = READ_ONCE(sqe->addr); |
5730 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
5731 | if (tr->flags & IORING_TIMEOUT_UPDATE) { | |
5732 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) | |
5733 | return -EINVAL; | |
5734 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
5735 | return -EFAULT; | |
5736 | } else if (tr->flags) { | |
5737 | /* timeout removal doesn't support flags */ | |
b29472ee | 5738 | return -EINVAL; |
9c8e11b3 | 5739 | } |
b29472ee | 5740 | |
b29472ee JA |
5741 | return 0; |
5742 | } | |
5743 | ||
8662daec PB |
5744 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
5745 | { | |
5746 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
5747 | : HRTIMER_MODE_REL; | |
5748 | } | |
5749 | ||
11365043 JA |
5750 | /* |
5751 | * Remove or update an existing timeout command | |
5752 | */ | |
61e98203 | 5753 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 5754 | { |
9c8e11b3 | 5755 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 5756 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 5757 | int ret; |
11365043 | 5758 | |
ec3c3d0f PB |
5759 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
5760 | spin_lock(&ctx->completion_lock); | |
5761 | spin_lock_irq(&ctx->timeout_lock); | |
9c8e11b3 | 5762 | ret = io_timeout_cancel(ctx, tr->addr); |
ec3c3d0f PB |
5763 | spin_unlock_irq(&ctx->timeout_lock); |
5764 | spin_unlock(&ctx->completion_lock); | |
5765 | } else { | |
5766 | spin_lock_irq(&ctx->timeout_lock); | |
8662daec PB |
5767 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, |
5768 | io_translate_timeout_mode(tr->flags)); | |
ec3c3d0f PB |
5769 | spin_unlock_irq(&ctx->timeout_lock); |
5770 | } | |
11365043 | 5771 | |
4e88d6e7 | 5772 | if (ret < 0) |
93d2bcd2 | 5773 | req_set_fail(req); |
505657bc | 5774 | io_req_complete_post(req, ret, 0); |
11365043 | 5775 | return 0; |
5262f567 JA |
5776 | } |
5777 | ||
3529d8c2 | 5778 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 5779 | bool is_timeout_link) |
5262f567 | 5780 | { |
ad8a48ac | 5781 | struct io_timeout_data *data; |
a41525ab | 5782 | unsigned flags; |
56080b02 | 5783 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 5784 | |
ad8a48ac | 5785 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 5786 | return -EINVAL; |
26578cda PB |
5787 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
5788 | sqe->splice_fd_in) | |
a41525ab | 5789 | return -EINVAL; |
56080b02 | 5790 | if (off && is_timeout_link) |
2d28390a | 5791 | return -EINVAL; |
a41525ab JA |
5792 | flags = READ_ONCE(sqe->timeout_flags); |
5793 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 5794 | return -EINVAL; |
bdf20073 | 5795 | |
bfe68a22 | 5796 | req->timeout.off = off; |
f18ee4cf PB |
5797 | if (unlikely(off && !req->ctx->off_timeout_used)) |
5798 | req->ctx->off_timeout_used = true; | |
26a61679 | 5799 | |
e8c2bc1f | 5800 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
5801 | return -ENOMEM; |
5802 | ||
e8c2bc1f | 5803 | data = req->async_data; |
ad8a48ac | 5804 | data->req = req; |
ad8a48ac JA |
5805 | |
5806 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
5807 | return -EFAULT; |
5808 | ||
8662daec | 5809 | data->mode = io_translate_timeout_mode(flags); |
ad8a48ac | 5810 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
b97e736a PB |
5811 | |
5812 | if (is_timeout_link) { | |
5813 | struct io_submit_link *link = &req->ctx->submit_state.link; | |
5814 | ||
5815 | if (!link->head) | |
5816 | return -EINVAL; | |
5817 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) | |
5818 | return -EINVAL; | |
4d13d1a4 PB |
5819 | req->timeout.head = link->last; |
5820 | link->last->flags |= REQ_F_ARM_LTIMEOUT; | |
b97e736a | 5821 | } |
ad8a48ac JA |
5822 | return 0; |
5823 | } | |
5824 | ||
61e98203 | 5825 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 5826 | { |
ad8a48ac | 5827 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 5828 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 5829 | struct list_head *entry; |
bfe68a22 | 5830 | u32 tail, off = req->timeout.off; |
ad8a48ac | 5831 | |
89850fce | 5832 | spin_lock_irq(&ctx->timeout_lock); |
93bd25bb | 5833 | |
5262f567 JA |
5834 | /* |
5835 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
5836 | * timeout event to be satisfied. If it isn't set, then this is |
5837 | * a pure timeout request, sequence isn't used. | |
5262f567 | 5838 | */ |
8eb7e2d0 | 5839 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
5840 | entry = ctx->timeout_list.prev; |
5841 | goto add; | |
5842 | } | |
5262f567 | 5843 | |
bfe68a22 PB |
5844 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
5845 | req->timeout.target_seq = tail + off; | |
5262f567 | 5846 | |
f010505b MDG |
5847 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
5848 | * This is safe because ->completion_lock is held, and submissions | |
5849 | * and completions are never mixed in the same ->completion_lock section. | |
5850 | */ | |
5851 | ctx->cq_last_tm_flush = tail; | |
5852 | ||
5262f567 JA |
5853 | /* |
5854 | * Insertion sort, ensuring the first entry in the list is always | |
5855 | * the one we need first. | |
5856 | */ | |
5262f567 | 5857 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
5858 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
5859 | timeout.list); | |
5262f567 | 5860 | |
8eb7e2d0 | 5861 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 5862 | continue; |
bfe68a22 PB |
5863 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
5864 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
5865 | break; |
5866 | } | |
93bd25bb | 5867 | add: |
135fcde8 | 5868 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
5869 | data->timer.function = io_timeout_fn; |
5870 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
89850fce | 5871 | spin_unlock_irq(&ctx->timeout_lock); |
5262f567 JA |
5872 | return 0; |
5873 | } | |
5262f567 | 5874 | |
f458dd84 PB |
5875 | struct io_cancel_data { |
5876 | struct io_ring_ctx *ctx; | |
5877 | u64 user_data; | |
5878 | }; | |
5879 | ||
62755e35 JA |
5880 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
5881 | { | |
5882 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 5883 | struct io_cancel_data *cd = data; |
62755e35 | 5884 | |
f458dd84 | 5885 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
62755e35 JA |
5886 | } |
5887 | ||
f458dd84 PB |
5888 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
5889 | struct io_ring_ctx *ctx) | |
62755e35 | 5890 | { |
f458dd84 | 5891 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
62755e35 | 5892 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
5893 | int ret = 0; |
5894 | ||
f458dd84 | 5895 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
5896 | return -ENOENT; |
5897 | ||
f458dd84 | 5898 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
62755e35 JA |
5899 | switch (cancel_ret) { |
5900 | case IO_WQ_CANCEL_OK: | |
5901 | ret = 0; | |
5902 | break; | |
5903 | case IO_WQ_CANCEL_RUNNING: | |
5904 | ret = -EALREADY; | |
5905 | break; | |
5906 | case IO_WQ_CANCEL_NOTFOUND: | |
5907 | ret = -ENOENT; | |
5908 | break; | |
5909 | } | |
5910 | ||
e977d6d3 JA |
5911 | return ret; |
5912 | } | |
5913 | ||
8cb01fac | 5914 | static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr) |
47f46768 | 5915 | { |
8cb01fac | 5916 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 JA |
5917 | int ret; |
5918 | ||
dadebc35 | 5919 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
8cb01fac | 5920 | |
f458dd84 | 5921 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
df9727af | 5922 | if (ret != -ENOENT) |
8cb01fac | 5923 | return ret; |
505657bc PB |
5924 | |
5925 | spin_lock(&ctx->completion_lock); | |
79ebeaee | 5926 | spin_lock_irq(&ctx->timeout_lock); |
47f46768 | 5927 | ret = io_timeout_cancel(ctx, sqe_addr); |
79ebeaee | 5928 | spin_unlock_irq(&ctx->timeout_lock); |
47f46768 | 5929 | if (ret != -ENOENT) |
505657bc PB |
5930 | goto out; |
5931 | ret = io_poll_cancel(ctx, sqe_addr, false); | |
5932 | out: | |
5933 | spin_unlock(&ctx->completion_lock); | |
5934 | return ret; | |
47f46768 JA |
5935 | } |
5936 | ||
3529d8c2 JA |
5937 | static int io_async_cancel_prep(struct io_kiocb *req, |
5938 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 5939 | { |
fbf23849 | 5940 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 5941 | return -EINVAL; |
61710e43 DA |
5942 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5943 | return -EINVAL; | |
26578cda PB |
5944 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
5945 | sqe->splice_fd_in) | |
e977d6d3 JA |
5946 | return -EINVAL; |
5947 | ||
fbf23849 JA |
5948 | req->cancel.addr = READ_ONCE(sqe->addr); |
5949 | return 0; | |
5950 | } | |
5951 | ||
61e98203 | 5952 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
5953 | { |
5954 | struct io_ring_ctx *ctx = req->ctx; | |
58f99373 PB |
5955 | u64 sqe_addr = req->cancel.addr; |
5956 | struct io_tctx_node *node; | |
5957 | int ret; | |
5958 | ||
8cb01fac | 5959 | ret = io_try_cancel_userdata(req, sqe_addr); |
58f99373 PB |
5960 | if (ret != -ENOENT) |
5961 | goto done; | |
58f99373 PB |
5962 | |
5963 | /* slow path, try all io-wq's */ | |
5964 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
5965 | ret = -ENOENT; | |
5966 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
5967 | struct io_uring_task *tctx = node->task->io_uring; | |
fbf23849 | 5968 | |
58f99373 PB |
5969 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
5970 | if (ret != -ENOENT) | |
5971 | break; | |
5972 | } | |
5973 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
58f99373 | 5974 | done: |
58f99373 | 5975 | if (ret < 0) |
93d2bcd2 | 5976 | req_set_fail(req); |
505657bc | 5977 | io_req_complete_post(req, ret, 0); |
5262f567 JA |
5978 | return 0; |
5979 | } | |
5980 | ||
269bbe5f | 5981 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
5982 | const struct io_uring_sqe *sqe) |
5983 | { | |
61710e43 DA |
5984 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5985 | return -EINVAL; | |
26578cda | 5986 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
05f3fb3c JA |
5987 | return -EINVAL; |
5988 | ||
269bbe5f BM |
5989 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
5990 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
5991 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 5992 | return -EINVAL; |
269bbe5f | 5993 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
5994 | return 0; |
5995 | } | |
5996 | ||
889fca73 | 5997 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
5998 | { |
5999 | struct io_ring_ctx *ctx = req->ctx; | |
c3bdad02 | 6000 | struct io_uring_rsrc_update2 up; |
05f3fb3c | 6001 | int ret; |
fbf23849 | 6002 | |
45d189c6 | 6003 | if (issue_flags & IO_URING_F_NONBLOCK) |
05f3fb3c | 6004 | return -EAGAIN; |
05f3fb3c | 6005 | |
269bbe5f BM |
6006 | up.offset = req->rsrc_update.offset; |
6007 | up.data = req->rsrc_update.arg; | |
c3bdad02 PB |
6008 | up.nr = 0; |
6009 | up.tags = 0; | |
615cee49 | 6010 | up.resv = 0; |
05f3fb3c JA |
6011 | |
6012 | mutex_lock(&ctx->uring_lock); | |
fdecb662 | 6013 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
98f0b3b4 | 6014 | &up, req->rsrc_update.nr_args); |
05f3fb3c JA |
6015 | mutex_unlock(&ctx->uring_lock); |
6016 | ||
6017 | if (ret < 0) | |
93d2bcd2 | 6018 | req_set_fail(req); |
889fca73 | 6019 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
6020 | return 0; |
6021 | } | |
6022 | ||
bfe76559 | 6023 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 6024 | { |
d625c6ee | 6025 | switch (req->opcode) { |
e781573e | 6026 | case IORING_OP_NOP: |
bfe76559 | 6027 | return 0; |
f67676d1 JA |
6028 | case IORING_OP_READV: |
6029 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 6030 | case IORING_OP_READ: |
bfe76559 | 6031 | return io_read_prep(req, sqe); |
f67676d1 JA |
6032 | case IORING_OP_WRITEV: |
6033 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 6034 | case IORING_OP_WRITE: |
bfe76559 | 6035 | return io_write_prep(req, sqe); |
0969e783 | 6036 | case IORING_OP_POLL_ADD: |
bfe76559 | 6037 | return io_poll_add_prep(req, sqe); |
0969e783 | 6038 | case IORING_OP_POLL_REMOVE: |
c5de0036 | 6039 | return io_poll_update_prep(req, sqe); |
8ed8d3c3 | 6040 | case IORING_OP_FSYNC: |
1155c76a | 6041 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 6042 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 6043 | return io_sfr_prep(req, sqe); |
03b1230c | 6044 | case IORING_OP_SENDMSG: |
fddaface | 6045 | case IORING_OP_SEND: |
bfe76559 | 6046 | return io_sendmsg_prep(req, sqe); |
03b1230c | 6047 | case IORING_OP_RECVMSG: |
fddaface | 6048 | case IORING_OP_RECV: |
bfe76559 | 6049 | return io_recvmsg_prep(req, sqe); |
f499a021 | 6050 | case IORING_OP_CONNECT: |
bfe76559 | 6051 | return io_connect_prep(req, sqe); |
2d28390a | 6052 | case IORING_OP_TIMEOUT: |
bfe76559 | 6053 | return io_timeout_prep(req, sqe, false); |
b29472ee | 6054 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 6055 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 6056 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 6057 | return io_async_cancel_prep(req, sqe); |
2d28390a | 6058 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 6059 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 6060 | case IORING_OP_ACCEPT: |
bfe76559 | 6061 | return io_accept_prep(req, sqe); |
d63d1b5e | 6062 | case IORING_OP_FALLOCATE: |
bfe76559 | 6063 | return io_fallocate_prep(req, sqe); |
15b71abe | 6064 | case IORING_OP_OPENAT: |
bfe76559 | 6065 | return io_openat_prep(req, sqe); |
b5dba59e | 6066 | case IORING_OP_CLOSE: |
bfe76559 | 6067 | return io_close_prep(req, sqe); |
05f3fb3c | 6068 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 6069 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6070 | case IORING_OP_STATX: |
bfe76559 | 6071 | return io_statx_prep(req, sqe); |
4840e418 | 6072 | case IORING_OP_FADVISE: |
bfe76559 | 6073 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6074 | case IORING_OP_MADVISE: |
bfe76559 | 6075 | return io_madvise_prep(req, sqe); |
cebdb986 | 6076 | case IORING_OP_OPENAT2: |
bfe76559 | 6077 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6078 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6079 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6080 | case IORING_OP_SPLICE: |
bfe76559 | 6081 | return io_splice_prep(req, sqe); |
ddf0322d | 6082 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6083 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6084 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6085 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6086 | case IORING_OP_TEE: |
bfe76559 | 6087 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6088 | case IORING_OP_SHUTDOWN: |
6089 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6090 | case IORING_OP_RENAMEAT: |
6091 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6092 | case IORING_OP_UNLINKAT: |
6093 | return io_unlinkat_prep(req, sqe); | |
f67676d1 JA |
6094 | } |
6095 | ||
bfe76559 PB |
6096 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6097 | req->opcode); | |
bd54b6fe | 6098 | return -EINVAL; |
bfe76559 PB |
6099 | } |
6100 | ||
93642ef8 | 6101 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 6102 | { |
b7e298d2 PB |
6103 | if (!io_op_defs[req->opcode].needs_async_setup) |
6104 | return 0; | |
6105 | if (WARN_ON_ONCE(req->async_data)) | |
6106 | return -EFAULT; | |
6107 | if (io_alloc_async_data(req)) | |
6108 | return -EAGAIN; | |
6109 | ||
93642ef8 PB |
6110 | switch (req->opcode) { |
6111 | case IORING_OP_READV: | |
93642ef8 PB |
6112 | return io_rw_prep_async(req, READ); |
6113 | case IORING_OP_WRITEV: | |
93642ef8 PB |
6114 | return io_rw_prep_async(req, WRITE); |
6115 | case IORING_OP_SENDMSG: | |
93642ef8 PB |
6116 | return io_sendmsg_prep_async(req); |
6117 | case IORING_OP_RECVMSG: | |
93642ef8 PB |
6118 | return io_recvmsg_prep_async(req); |
6119 | case IORING_OP_CONNECT: | |
6120 | return io_connect_prep_async(req); | |
6121 | } | |
b7e298d2 PB |
6122 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
6123 | req->opcode); | |
6124 | return -EFAULT; | |
f67676d1 JA |
6125 | } |
6126 | ||
9cf7c104 PB |
6127 | static u32 io_get_sequence(struct io_kiocb *req) |
6128 | { | |
a3dbdf54 | 6129 | u32 seq = req->ctx->cached_sq_head; |
9cf7c104 | 6130 | |
a3dbdf54 PB |
6131 | /* need original cached_sq_head, but it was increased for each req */ |
6132 | io_for_each_link(req, req) | |
6133 | seq--; | |
6134 | return seq; | |
9cf7c104 PB |
6135 | } |
6136 | ||
76cc33d7 | 6137 | static bool io_drain_req(struct io_kiocb *req) |
de0617e4 | 6138 | { |
3c19966d | 6139 | struct io_kiocb *pos; |
a197f664 | 6140 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6141 | struct io_defer_entry *de; |
f67676d1 | 6142 | int ret; |
9cf7c104 | 6143 | u32 seq; |
de0617e4 | 6144 | |
3c19966d PB |
6145 | /* |
6146 | * If we need to drain a request in the middle of a link, drain the | |
6147 | * head request and the next request/link after the current link. | |
6148 | * Considering sequential execution of links, IOSQE_IO_DRAIN will be | |
6149 | * maintained for every request of our link. | |
6150 | */ | |
6151 | if (ctx->drain_next) { | |
6152 | req->flags |= REQ_F_IO_DRAIN; | |
6153 | ctx->drain_next = false; | |
6154 | } | |
6155 | /* not interested in head, start from the first linked */ | |
6156 | io_for_each_link(pos, req->link) { | |
6157 | if (pos->flags & REQ_F_IO_DRAIN) { | |
6158 | ctx->drain_next = true; | |
6159 | req->flags |= REQ_F_IO_DRAIN; | |
6160 | break; | |
6161 | } | |
6162 | } | |
6163 | ||
9d858b21 | 6164 | /* Still need defer if there is pending req in defer list. */ |
9cf7c104 | 6165 | if (likely(list_empty_careful(&ctx->defer_list) && |
10c66904 PB |
6166 | !(req->flags & REQ_F_IO_DRAIN))) { |
6167 | ctx->drain_active = false; | |
76cc33d7 | 6168 | return false; |
10c66904 | 6169 | } |
9cf7c104 PB |
6170 | |
6171 | seq = io_get_sequence(req); | |
6172 | /* Still a chance to pass the sequence check */ | |
6173 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
76cc33d7 | 6174 | return false; |
de0617e4 | 6175 | |
b7e298d2 | 6176 | ret = io_req_prep_async(req); |
be7053b7 | 6177 | if (ret) |
1b48773f | 6178 | goto fail; |
cbdcb435 | 6179 | io_prep_async_link(req); |
27dc8338 | 6180 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
76cc33d7 | 6181 | if (!de) { |
1b48773f PB |
6182 | ret = -ENOMEM; |
6183 | fail: | |
6184 | io_req_complete_failed(req, ret); | |
76cc33d7 PB |
6185 | return true; |
6186 | } | |
2d28390a | 6187 | |
79ebeaee | 6188 | spin_lock(&ctx->completion_lock); |
9cf7c104 | 6189 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
79ebeaee | 6190 | spin_unlock(&ctx->completion_lock); |
27dc8338 | 6191 | kfree(de); |
f237c30a | 6192 | io_queue_async_work(req, NULL); |
76cc33d7 | 6193 | return true; |
de0617e4 JA |
6194 | } |
6195 | ||
915967f6 | 6196 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 6197 | de->req = req; |
9cf7c104 | 6198 | de->seq = seq; |
27dc8338 | 6199 | list_add_tail(&de->list, &ctx->defer_list); |
79ebeaee | 6200 | spin_unlock(&ctx->completion_lock); |
76cc33d7 | 6201 | return true; |
de0617e4 JA |
6202 | } |
6203 | ||
68fb8979 | 6204 | static void io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6205 | { |
0e1b6fe3 PB |
6206 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
6207 | switch (req->opcode) { | |
6208 | case IORING_OP_READV: | |
6209 | case IORING_OP_READ_FIXED: | |
6210 | case IORING_OP_READ: | |
bcda7baa | 6211 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
6212 | break; |
6213 | case IORING_OP_RECVMSG: | |
6214 | case IORING_OP_RECV: | |
bcda7baa | 6215 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
6216 | break; |
6217 | } | |
99bc4c38 PB |
6218 | } |
6219 | ||
0e1b6fe3 PB |
6220 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6221 | switch (req->opcode) { | |
6222 | case IORING_OP_READV: | |
6223 | case IORING_OP_READ_FIXED: | |
6224 | case IORING_OP_READ: | |
6225 | case IORING_OP_WRITEV: | |
6226 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6227 | case IORING_OP_WRITE: { |
6228 | struct io_async_rw *io = req->async_data; | |
1dacb4df PB |
6229 | |
6230 | kfree(io->free_iovec); | |
0e1b6fe3 | 6231 | break; |
e8c2bc1f | 6232 | } |
0e1b6fe3 | 6233 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6234 | case IORING_OP_SENDMSG: { |
6235 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
6236 | |
6237 | kfree(io->free_iov); | |
0e1b6fe3 | 6238 | break; |
e8c2bc1f | 6239 | } |
0e1b6fe3 PB |
6240 | case IORING_OP_SPLICE: |
6241 | case IORING_OP_TEE: | |
e1d767f0 PB |
6242 | if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED)) |
6243 | io_put_file(req->splice.file_in); | |
0e1b6fe3 | 6244 | break; |
f3cd4850 JA |
6245 | case IORING_OP_OPENAT: |
6246 | case IORING_OP_OPENAT2: | |
6247 | if (req->open.filename) | |
6248 | putname(req->open.filename); | |
6249 | break; | |
80a261fd JA |
6250 | case IORING_OP_RENAMEAT: |
6251 | putname(req->rename.oldpath); | |
6252 | putname(req->rename.newpath); | |
6253 | break; | |
14a1143b JA |
6254 | case IORING_OP_UNLINKAT: |
6255 | putname(req->unlink.filename); | |
6256 | break; | |
0e1b6fe3 | 6257 | } |
99bc4c38 | 6258 | } |
75652a30 JA |
6259 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
6260 | kfree(req->apoll->double_poll); | |
6261 | kfree(req->apoll); | |
6262 | req->apoll = NULL; | |
6263 | } | |
3a0a6902 PB |
6264 | if (req->flags & REQ_F_INFLIGHT) { |
6265 | struct io_uring_task *tctx = req->task->io_uring; | |
6266 | ||
6267 | atomic_dec(&tctx->inflight_tracked); | |
3a0a6902 | 6268 | } |
c854357b | 6269 | if (req->flags & REQ_F_CREDS) |
b8e64b53 | 6270 | put_cred(req->creds); |
c854357b PB |
6271 | |
6272 | req->flags &= ~IO_REQ_CLEAN_FLAGS; | |
99bc4c38 PB |
6273 | } |
6274 | ||
889fca73 | 6275 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 6276 | { |
a197f664 | 6277 | struct io_ring_ctx *ctx = req->ctx; |
5730b27e | 6278 | const struct cred *creds = NULL; |
d625c6ee | 6279 | int ret; |
2b188cc1 | 6280 | |
b8e64b53 | 6281 | if ((req->flags & REQ_F_CREDS) && req->creds != current_cred()) |
c10d1f98 | 6282 | creds = override_creds(req->creds); |
5730b27e | 6283 | |
d625c6ee | 6284 | switch (req->opcode) { |
2b188cc1 | 6285 | case IORING_OP_NOP: |
889fca73 | 6286 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
6287 | break; |
6288 | case IORING_OP_READV: | |
edafccee | 6289 | case IORING_OP_READ_FIXED: |
3a6820f2 | 6290 | case IORING_OP_READ: |
889fca73 | 6291 | ret = io_read(req, issue_flags); |
edafccee | 6292 | break; |
3529d8c2 | 6293 | case IORING_OP_WRITEV: |
edafccee | 6294 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 6295 | case IORING_OP_WRITE: |
889fca73 | 6296 | ret = io_write(req, issue_flags); |
2b188cc1 | 6297 | break; |
c992fe29 | 6298 | case IORING_OP_FSYNC: |
45d189c6 | 6299 | ret = io_fsync(req, issue_flags); |
c992fe29 | 6300 | break; |
221c5eb2 | 6301 | case IORING_OP_POLL_ADD: |
61e98203 | 6302 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
6303 | break; |
6304 | case IORING_OP_POLL_REMOVE: | |
c5de0036 | 6305 | ret = io_poll_update(req, issue_flags); |
221c5eb2 | 6306 | break; |
5d17b4a4 | 6307 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 6308 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 6309 | break; |
0fa03c62 | 6310 | case IORING_OP_SENDMSG: |
889fca73 | 6311 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 6312 | break; |
fddaface | 6313 | case IORING_OP_SEND: |
889fca73 | 6314 | ret = io_send(req, issue_flags); |
0fa03c62 | 6315 | break; |
aa1fa28f | 6316 | case IORING_OP_RECVMSG: |
889fca73 | 6317 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 6318 | break; |
fddaface | 6319 | case IORING_OP_RECV: |
889fca73 | 6320 | ret = io_recv(req, issue_flags); |
aa1fa28f | 6321 | break; |
5262f567 | 6322 | case IORING_OP_TIMEOUT: |
61e98203 | 6323 | ret = io_timeout(req, issue_flags); |
5262f567 | 6324 | break; |
11365043 | 6325 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 6326 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 6327 | break; |
17f2fe35 | 6328 | case IORING_OP_ACCEPT: |
889fca73 | 6329 | ret = io_accept(req, issue_flags); |
17f2fe35 | 6330 | break; |
f8e85cf2 | 6331 | case IORING_OP_CONNECT: |
889fca73 | 6332 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 6333 | break; |
62755e35 | 6334 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 6335 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 6336 | break; |
d63d1b5e | 6337 | case IORING_OP_FALLOCATE: |
45d189c6 | 6338 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 6339 | break; |
15b71abe | 6340 | case IORING_OP_OPENAT: |
45d189c6 | 6341 | ret = io_openat(req, issue_flags); |
15b71abe | 6342 | break; |
b5dba59e | 6343 | case IORING_OP_CLOSE: |
889fca73 | 6344 | ret = io_close(req, issue_flags); |
b5dba59e | 6345 | break; |
05f3fb3c | 6346 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6347 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6348 | break; |
eddc7ef5 | 6349 | case IORING_OP_STATX: |
45d189c6 | 6350 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6351 | break; |
4840e418 | 6352 | case IORING_OP_FADVISE: |
45d189c6 | 6353 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6354 | break; |
c1ca757b | 6355 | case IORING_OP_MADVISE: |
45d189c6 | 6356 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6357 | break; |
cebdb986 | 6358 | case IORING_OP_OPENAT2: |
45d189c6 | 6359 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6360 | break; |
3e4827b0 | 6361 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6362 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6363 | break; |
7d67af2c | 6364 | case IORING_OP_SPLICE: |
45d189c6 | 6365 | ret = io_splice(req, issue_flags); |
7d67af2c | 6366 | break; |
ddf0322d | 6367 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6368 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6369 | break; |
067524e9 | 6370 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6371 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6372 | break; |
f2a8d5c7 | 6373 | case IORING_OP_TEE: |
45d189c6 | 6374 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6375 | break; |
36f4fa68 | 6376 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6377 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6378 | break; |
80a261fd | 6379 | case IORING_OP_RENAMEAT: |
45d189c6 | 6380 | ret = io_renameat(req, issue_flags); |
80a261fd | 6381 | break; |
14a1143b | 6382 | case IORING_OP_UNLINKAT: |
45d189c6 | 6383 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6384 | break; |
2b188cc1 JA |
6385 | default: |
6386 | ret = -EINVAL; | |
6387 | break; | |
6388 | } | |
6389 | ||
5730b27e JA |
6390 | if (creds) |
6391 | revert_creds(creds); | |
def596e9 JA |
6392 | if (ret) |
6393 | return ret; | |
b532576e | 6394 | /* If the op doesn't have a file, we're not polling for it */ |
cb3d8972 PB |
6395 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
6396 | io_iopoll_req_issued(req); | |
def596e9 JA |
6397 | |
6398 | return 0; | |
2b188cc1 JA |
6399 | } |
6400 | ||
ebc11b6c PB |
6401 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
6402 | { | |
6403 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6404 | ||
6405 | req = io_put_req_find_next(req); | |
6406 | return req ? &req->work : NULL; | |
6407 | } | |
6408 | ||
5280f7e5 | 6409 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6410 | { |
6411 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6412 | struct io_kiocb *timeout; |
561fb04a | 6413 | int ret = 0; |
2b188cc1 | 6414 | |
48dcd38d PB |
6415 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
6416 | if (!(req->flags & REQ_F_REFCOUNT)) | |
6417 | __io_req_set_refcount(req, 2); | |
6418 | else | |
6419 | req_ref_get(req); | |
5d5901a3 | 6420 | |
6df1db6b PB |
6421 | timeout = io_prep_linked_timeout(req); |
6422 | if (timeout) | |
6423 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6424 | |
dadebc35 | 6425 | /* either cancelled or io-wq is dying, so don't touch tctx->iowq */ |
4014d943 | 6426 | if (work->flags & IO_WQ_WORK_CANCEL) |
561fb04a | 6427 | ret = -ECANCELED; |
31b51510 | 6428 | |
561fb04a | 6429 | if (!ret) { |
561fb04a | 6430 | do { |
889fca73 | 6431 | ret = io_issue_sqe(req, 0); |
561fb04a JA |
6432 | /* |
6433 | * We can get EAGAIN for polled IO even though we're | |
6434 | * forcing a sync submission from here, since we can't | |
6435 | * wait for request slots on the block side. | |
6436 | */ | |
6437 | if (ret != -EAGAIN) | |
6438 | break; | |
6439 | cond_resched(); | |
6440 | } while (1); | |
6441 | } | |
31b51510 | 6442 | |
a3df7698 | 6443 | /* avoid locking problems by failing it from a clean context */ |
5d5901a3 | 6444 | if (ret) |
a3df7698 | 6445 | io_req_task_queue_fail(req, ret); |
2b188cc1 JA |
6446 | } |
6447 | ||
aeca241b | 6448 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
042b0d85 | 6449 | unsigned i) |
65e19f54 | 6450 | { |
042b0d85 | 6451 | return &table->files[i]; |
dafecf19 PB |
6452 | } |
6453 | ||
65e19f54 JA |
6454 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6455 | int index) | |
6456 | { | |
aeca241b | 6457 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
65e19f54 | 6458 | |
a04b0ac0 | 6459 | return (struct file *) (slot->file_ptr & FFS_MASK); |
65e19f54 JA |
6460 | } |
6461 | ||
a04b0ac0 | 6462 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
9a321c98 PB |
6463 | { |
6464 | unsigned long file_ptr = (unsigned long) file; | |
6465 | ||
b191e2df | 6466 | if (__io_file_supports_nowait(file, READ)) |
9a321c98 | 6467 | file_ptr |= FFS_ASYNC_READ; |
b191e2df | 6468 | if (__io_file_supports_nowait(file, WRITE)) |
9a321c98 PB |
6469 | file_ptr |= FFS_ASYNC_WRITE; |
6470 | if (S_ISREG(file_inode(file)->i_mode)) | |
6471 | file_ptr |= FFS_ISREG; | |
a04b0ac0 | 6472 | file_slot->file_ptr = file_ptr; |
65e19f54 JA |
6473 | } |
6474 | ||
ac177053 PB |
6475 | static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx, |
6476 | struct io_kiocb *req, int fd) | |
09bb8394 | 6477 | { |
8da11c19 | 6478 | struct file *file; |
ac177053 | 6479 | unsigned long file_ptr; |
09bb8394 | 6480 | |
ac177053 PB |
6481 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
6482 | return NULL; | |
6483 | fd = array_index_nospec(fd, ctx->nr_user_files); | |
6484 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; | |
6485 | file = (struct file *) (file_ptr & FFS_MASK); | |
6486 | file_ptr &= ~FFS_MASK; | |
6487 | /* mask in overlapping REQ_F and FFS bits */ | |
b191e2df | 6488 | req->flags |= (file_ptr << REQ_F_NOWAIT_READ_BIT); |
ac177053 PB |
6489 | io_req_set_rsrc_node(req); |
6490 | return file; | |
6491 | } | |
d44f554e | 6492 | |
ac177053 | 6493 | static struct file *io_file_get_normal(struct io_ring_ctx *ctx, |
ac177053 PB |
6494 | struct io_kiocb *req, int fd) |
6495 | { | |
62906e89 | 6496 | struct file *file = fget(fd); |
ac177053 PB |
6497 | |
6498 | trace_io_uring_file_get(ctx, fd); | |
09bb8394 | 6499 | |
ac177053 PB |
6500 | /* we don't allow fixed io_uring files */ |
6501 | if (file && unlikely(file->f_op == &io_uring_fops)) | |
6502 | io_req_track_inflight(req); | |
8371adf5 | 6503 | return file; |
09bb8394 JA |
6504 | } |
6505 | ||
ac177053 | 6506 | static inline struct file *io_file_get(struct io_ring_ctx *ctx, |
ac177053 PB |
6507 | struct io_kiocb *req, int fd, bool fixed) |
6508 | { | |
6509 | if (fixed) | |
6510 | return io_file_get_fixed(ctx, req, fd); | |
6511 | else | |
62906e89 | 6512 | return io_file_get_normal(ctx, req, fd); |
ac177053 PB |
6513 | } |
6514 | ||
f237c30a | 6515 | static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked) |
89b263f6 JA |
6516 | { |
6517 | struct io_kiocb *prev = req->timeout.prev; | |
8cb01fac | 6518 | int ret; |
89b263f6 JA |
6519 | |
6520 | if (prev) { | |
8cb01fac | 6521 | ret = io_try_cancel_userdata(req, prev->user_data); |
505657bc | 6522 | io_req_complete_post(req, ret ?: -ETIME, 0); |
89b263f6 | 6523 | io_put_req(prev); |
89b263f6 JA |
6524 | } else { |
6525 | io_req_complete_post(req, -ETIME, 0); | |
6526 | } | |
6527 | } | |
6528 | ||
2665abfd | 6529 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6530 | { |
ad8a48ac JA |
6531 | struct io_timeout_data *data = container_of(timer, |
6532 | struct io_timeout_data, timer); | |
90cd7e42 | 6533 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6534 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6535 | unsigned long flags; |
2665abfd | 6536 | |
89b263f6 | 6537 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
90cd7e42 PB |
6538 | prev = req->timeout.head; |
6539 | req->timeout.head = NULL; | |
2665abfd JA |
6540 | |
6541 | /* | |
6542 | * We don't expect the list to be empty, that will only happen if we | |
6543 | * race with the completion of the linked work. | |
6544 | */ | |
447c19f3 | 6545 | if (prev) { |
f2f87370 | 6546 | io_remove_next_linked(prev); |
447c19f3 PB |
6547 | if (!req_ref_inc_not_zero(prev)) |
6548 | prev = NULL; | |
6549 | } | |
89b263f6 JA |
6550 | req->timeout.prev = prev; |
6551 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); | |
2665abfd | 6552 | |
89b263f6 JA |
6553 | req->io_task_work.func = io_req_task_link_timeout; |
6554 | io_req_task_work_add(req); | |
2665abfd JA |
6555 | return HRTIMER_NORESTART; |
6556 | } | |
6557 | ||
de968c18 | 6558 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6559 | { |
de968c18 PB |
6560 | struct io_ring_ctx *ctx = req->ctx; |
6561 | ||
89b263f6 | 6562 | spin_lock_irq(&ctx->timeout_lock); |
76a46e06 | 6563 | /* |
f2f87370 PB |
6564 | * If the back reference is NULL, then our linked request finished |
6565 | * before we got a chance to setup the timer | |
76a46e06 | 6566 | */ |
90cd7e42 | 6567 | if (req->timeout.head) { |
e8c2bc1f | 6568 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6569 | |
ad8a48ac JA |
6570 | data->timer.function = io_link_timeout_fn; |
6571 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6572 | data->mode); | |
2665abfd | 6573 | } |
89b263f6 | 6574 | spin_unlock_irq(&ctx->timeout_lock); |
2665abfd | 6575 | /* drop submission reference */ |
76a46e06 JA |
6576 | io_put_req(req); |
6577 | } | |
2665abfd | 6578 | |
c5eef2b9 | 6579 | static void __io_queue_sqe(struct io_kiocb *req) |
282cdc86 | 6580 | __must_hold(&req->ctx->uring_lock) |
2b188cc1 | 6581 | { |
906c6caa | 6582 | struct io_kiocb *linked_timeout; |
e0c5c576 | 6583 | int ret; |
2b188cc1 | 6584 | |
59b735ae | 6585 | issue_sqe: |
c5eef2b9 | 6586 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 6587 | |
491381ce JA |
6588 | /* |
6589 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6590 | * doesn't support non-blocking read/write attempts | |
6591 | */ | |
1840038e | 6592 | if (likely(!ret)) { |
e342c807 | 6593 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
c5eef2b9 | 6594 | struct io_ring_ctx *ctx = req->ctx; |
cd0ca2e0 | 6595 | struct io_submit_state *state = &ctx->submit_state; |
e65ef56d | 6596 | |
cd0ca2e0 PB |
6597 | state->compl_reqs[state->compl_nr++] = req; |
6598 | if (state->compl_nr == ARRAY_SIZE(state->compl_reqs)) | |
2a2758f2 | 6599 | io_submit_flush_completions(ctx); |
906c6caa | 6600 | return; |
0d63c148 | 6601 | } |
906c6caa PB |
6602 | |
6603 | linked_timeout = io_prep_linked_timeout(req); | |
6604 | if (linked_timeout) | |
6605 | io_queue_linked_timeout(linked_timeout); | |
1840038e | 6606 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
906c6caa PB |
6607 | linked_timeout = io_prep_linked_timeout(req); |
6608 | ||
59b735ae OL |
6609 | switch (io_arm_poll_handler(req)) { |
6610 | case IO_APOLL_READY: | |
906c6caa PB |
6611 | if (linked_timeout) |
6612 | io_unprep_linked_timeout(req); | |
59b735ae OL |
6613 | goto issue_sqe; |
6614 | case IO_APOLL_ABORTED: | |
1840038e PB |
6615 | /* |
6616 | * Queued up for async execution, worker will release | |
6617 | * submit reference when the iocb is actually submitted. | |
6618 | */ | |
f237c30a | 6619 | io_queue_async_work(req, NULL); |
59b735ae | 6620 | break; |
1840038e | 6621 | } |
906c6caa PB |
6622 | |
6623 | if (linked_timeout) | |
6624 | io_queue_linked_timeout(linked_timeout); | |
0d63c148 | 6625 | } else { |
f41db273 | 6626 | io_req_complete_failed(req, ret); |
9e645e11 | 6627 | } |
2b188cc1 JA |
6628 | } |
6629 | ||
441b8a78 | 6630 | static inline void io_queue_sqe(struct io_kiocb *req) |
282cdc86 | 6631 | __must_hold(&req->ctx->uring_lock) |
4fe2c963 | 6632 | { |
10c66904 | 6633 | if (unlikely(req->ctx->drain_active) && io_drain_req(req)) |
76cc33d7 | 6634 | return; |
4fe2c963 | 6635 | |
a8295b98 | 6636 | if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) { |
c5eef2b9 | 6637 | __io_queue_sqe(req); |
a8295b98 HX |
6638 | } else if (req->flags & REQ_F_FAIL) { |
6639 | io_req_complete_failed(req, req->result); | |
76cc33d7 PB |
6640 | } else { |
6641 | int ret = io_req_prep_async(req); | |
6642 | ||
6643 | if (unlikely(ret)) | |
6644 | io_req_complete_failed(req, ret); | |
6645 | else | |
f237c30a | 6646 | io_queue_async_work(req, NULL); |
ce35a47a | 6647 | } |
4fe2c963 JL |
6648 | } |
6649 | ||
b16fed66 PB |
6650 | /* |
6651 | * Check SQE restrictions (opcode and flags). | |
6652 | * | |
6653 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6654 | */ | |
6655 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6656 | struct io_kiocb *req, | |
6657 | unsigned int sqe_flags) | |
4fe2c963 | 6658 | { |
4cfb25bf | 6659 | if (likely(!ctx->restricted)) |
b16fed66 PB |
6660 | return true; |
6661 | ||
6662 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) | |
6663 | return false; | |
6664 | ||
6665 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6666 | ctx->restrictions.sqe_flags_required) | |
6667 | return false; | |
6668 | ||
6669 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
6670 | ctx->restrictions.sqe_flags_required)) | |
6671 | return false; | |
6672 | ||
6673 | return true; | |
4fe2c963 JL |
6674 | } |
6675 | ||
b16fed66 PB |
6676 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
6677 | const struct io_uring_sqe *sqe) | |
282cdc86 | 6678 | __must_hold(&ctx->uring_lock) |
b16fed66 PB |
6679 | { |
6680 | struct io_submit_state *state; | |
6681 | unsigned int sqe_flags; | |
003e8dcc | 6682 | int personality, ret = 0; |
b16fed66 | 6683 | |
864ea921 | 6684 | /* req is partially pre-initialised, see io_preinit_req() */ |
b16fed66 PB |
6685 | req->opcode = READ_ONCE(sqe->opcode); |
6686 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
6687 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
6688 | req->user_data = READ_ONCE(sqe->user_data); | |
b16fed66 | 6689 | req->file = NULL; |
b16fed66 | 6690 | req->fixed_rsrc_refs = NULL; |
b16fed66 | 6691 | req->task = current; |
b16fed66 PB |
6692 | |
6693 | /* enforce forwards compatibility on users */ | |
dddca226 | 6694 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) |
b16fed66 | 6695 | return -EINVAL; |
b16fed66 PB |
6696 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
6697 | return -EINVAL; | |
4cfb25bf | 6698 | if (!io_check_restriction(ctx, req, sqe_flags)) |
b16fed66 PB |
6699 | return -EACCES; |
6700 | ||
6701 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
6702 | !io_op_defs[req->opcode].buffer_select) | |
6703 | return -EOPNOTSUPP; | |
3c19966d PB |
6704 | if (unlikely(sqe_flags & IOSQE_IO_DRAIN)) |
6705 | ctx->drain_active = true; | |
863e0560 | 6706 | |
003e8dcc JA |
6707 | personality = READ_ONCE(sqe->personality); |
6708 | if (personality) { | |
c10d1f98 PB |
6709 | req->creds = xa_load(&ctx->personalities, personality); |
6710 | if (!req->creds) | |
003e8dcc | 6711 | return -EINVAL; |
c10d1f98 | 6712 | get_cred(req->creds); |
b8e64b53 | 6713 | req->flags |= REQ_F_CREDS; |
003e8dcc | 6714 | } |
b16fed66 PB |
6715 | state = &ctx->submit_state; |
6716 | ||
6717 | /* | |
6718 | * Plug now if we have more than 1 IO left after this, and the target | |
6719 | * is potentially a read/write to block based storage. | |
6720 | */ | |
6721 | if (!state->plug_started && state->ios_left > 1 && | |
6722 | io_op_defs[req->opcode].plug) { | |
6723 | blk_start_plug(&state->plug); | |
6724 | state->plug_started = true; | |
6725 | } | |
6726 | ||
6727 | if (io_op_defs[req->opcode].needs_file) { | |
62906e89 | 6728 | req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd), |
ac177053 | 6729 | (sqe_flags & IOSQE_FIXED_FILE)); |
b16fed66 PB |
6730 | if (unlikely(!req->file)) |
6731 | ret = -EBADF; | |
6732 | } | |
6733 | ||
6734 | state->ios_left--; | |
6735 | return ret; | |
6736 | } | |
6737 | ||
a6b8cadc | 6738 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 6739 | const struct io_uring_sqe *sqe) |
282cdc86 | 6740 | __must_hold(&ctx->uring_lock) |
9e645e11 | 6741 | { |
a1ab7b35 | 6742 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 6743 | int ret; |
9e645e11 | 6744 | |
a6b8cadc PB |
6745 | ret = io_init_req(ctx, req, sqe); |
6746 | if (unlikely(ret)) { | |
6747 | fail_req: | |
a8295b98 | 6748 | /* fail even hard links since we don't submit */ |
de59bc10 | 6749 | if (link->head) { |
a8295b98 HX |
6750 | /* |
6751 | * we can judge a link req is failed or cancelled by if | |
6752 | * REQ_F_FAIL is set, but the head is an exception since | |
6753 | * it may be set REQ_F_FAIL because of other req's failure | |
6754 | * so let's leverage req->result to distinguish if a head | |
6755 | * is set REQ_F_FAIL because of its failure or other req's | |
6756 | * failure so that we can set the correct ret code for it. | |
6757 | * init result here to avoid affecting the normal path. | |
6758 | */ | |
6759 | if (!(link->head->flags & REQ_F_FAIL)) | |
6760 | req_fail_link_node(link->head, -ECANCELED); | |
6761 | } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { | |
6762 | /* | |
6763 | * the current req is a normal req, we should return | |
6764 | * error and thus break the submittion loop. | |
6765 | */ | |
6766 | io_req_complete_failed(req, ret); | |
6767 | return ret; | |
de59bc10 | 6768 | } |
a8295b98 HX |
6769 | req_fail_link_node(req, ret); |
6770 | } else { | |
6771 | ret = io_req_prep(req, sqe); | |
6772 | if (unlikely(ret)) | |
6773 | goto fail_req; | |
a6b8cadc | 6774 | } |
441b8a78 | 6775 | |
be7053b7 | 6776 | /* don't need @sqe from now on */ |
236daeae OL |
6777 | trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data, |
6778 | req->flags, true, | |
6779 | ctx->flags & IORING_SETUP_SQPOLL); | |
a6b8cadc | 6780 | |
9e645e11 JA |
6781 | /* |
6782 | * If we already have a head request, queue this one for async | |
6783 | * submittal once the head completes. If we don't have a head but | |
6784 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
6785 | * submitted sync once the chain is complete. If none of those | |
6786 | * conditions are true (normal request), then just queue it. | |
6787 | */ | |
863e0560 PB |
6788 | if (link->head) { |
6789 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 6790 | |
a8295b98 HX |
6791 | if (!(req->flags & REQ_F_FAIL)) { |
6792 | ret = io_req_prep_async(req); | |
6793 | if (unlikely(ret)) { | |
6794 | req_fail_link_node(req, ret); | |
6795 | if (!(head->flags & REQ_F_FAIL)) | |
6796 | req_fail_link_node(head, -ECANCELED); | |
6797 | } | |
6798 | } | |
9d76377f | 6799 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 6800 | link->last->link = req; |
863e0560 | 6801 | link->last = req; |
32fe525b PB |
6802 | |
6803 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 6804 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
863e0560 | 6805 | link->head = NULL; |
5e159204 | 6806 | io_queue_sqe(head); |
32fe525b | 6807 | } |
9e645e11 | 6808 | } else { |
ef4ff581 | 6809 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
863e0560 PB |
6810 | link->head = req; |
6811 | link->last = req; | |
711be031 | 6812 | } else { |
be7053b7 | 6813 | io_queue_sqe(req); |
711be031 | 6814 | } |
9e645e11 | 6815 | } |
2e6e1fde | 6816 | |
1d4240cc | 6817 | return 0; |
9e645e11 JA |
6818 | } |
6819 | ||
9a56a232 JA |
6820 | /* |
6821 | * Batched submission is done, ensure local IO is flushed out. | |
6822 | */ | |
ba88ff11 PB |
6823 | static void io_submit_state_end(struct io_submit_state *state, |
6824 | struct io_ring_ctx *ctx) | |
9a56a232 | 6825 | { |
a1ab7b35 | 6826 | if (state->link.head) |
de59bc10 | 6827 | io_queue_sqe(state->link.head); |
cd0ca2e0 | 6828 | if (state->compl_nr) |
2a2758f2 | 6829 | io_submit_flush_completions(ctx); |
27926b68 JA |
6830 | if (state->plug_started) |
6831 | blk_finish_plug(&state->plug); | |
9a56a232 JA |
6832 | } |
6833 | ||
6834 | /* | |
6835 | * Start submission side cache. | |
6836 | */ | |
6837 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 6838 | unsigned int max_ios) |
9a56a232 | 6839 | { |
27926b68 | 6840 | state->plug_started = false; |
9a56a232 | 6841 | state->ios_left = max_ios; |
a1ab7b35 PB |
6842 | /* set only head, no need to init link_last in advance */ |
6843 | state->link.head = NULL; | |
9a56a232 JA |
6844 | } |
6845 | ||
2b188cc1 JA |
6846 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
6847 | { | |
75b28aff | 6848 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 6849 | |
caf582c6 PB |
6850 | /* |
6851 | * Ensure any loads from the SQEs are done at this point, | |
6852 | * since once we write the new head, the application could | |
6853 | * write new data to them. | |
6854 | */ | |
6855 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
6856 | } |
6857 | ||
2b188cc1 | 6858 | /* |
dd9ae8a0 | 6859 | * Fetch an sqe, if one is available. Note this returns a pointer to memory |
2b188cc1 JA |
6860 | * that is mapped by userspace. This means that care needs to be taken to |
6861 | * ensure that reads are stable, as we cannot rely on userspace always | |
6862 | * being a good citizen. If members of the sqe are validated and then later | |
6863 | * used, it's important that those reads are done through READ_ONCE() to | |
6864 | * prevent a re-load down the line. | |
6865 | */ | |
709b302f | 6866 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 6867 | { |
ea5ab3b5 | 6868 | unsigned head, mask = ctx->sq_entries - 1; |
17d3aeb3 | 6869 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
2b188cc1 JA |
6870 | |
6871 | /* | |
6872 | * The cached sq head (or cq tail) serves two purposes: | |
6873 | * | |
6874 | * 1) allows us to batch the cost of updating the user visible | |
6875 | * head updates. | |
6876 | * 2) allows the kernel side to track the head on its own, even | |
6877 | * though the application is the one updating it. | |
6878 | */ | |
17d3aeb3 | 6879 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
709b302f PB |
6880 | if (likely(head < ctx->sq_entries)) |
6881 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
6882 | |
6883 | /* drop invalid entries */ | |
15641e42 PB |
6884 | ctx->cq_extra--; |
6885 | WRITE_ONCE(ctx->rings->sq_dropped, | |
6886 | READ_ONCE(ctx->rings->sq_dropped) + 1); | |
709b302f PB |
6887 | return NULL; |
6888 | } | |
6889 | ||
0f212204 | 6890 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
282cdc86 | 6891 | __must_hold(&ctx->uring_lock) |
6c271ce2 | 6892 | { |
09899b19 | 6893 | struct io_uring_task *tctx; |
46c4e16a | 6894 | int submitted = 0; |
6c271ce2 | 6895 | |
ee7d46d9 PB |
6896 | /* make sure SQ entry isn't read before tail */ |
6897 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
2b85edfc PB |
6898 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
6899 | return -EAGAIN; | |
6c271ce2 | 6900 | |
09899b19 PB |
6901 | tctx = current->io_uring; |
6902 | tctx->cached_refs -= nr; | |
6903 | if (unlikely(tctx->cached_refs < 0)) { | |
6904 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; | |
6905 | ||
6906 | percpu_counter_add(&tctx->inflight, refill); | |
6907 | refcount_add(refill, ¤t->usage); | |
6908 | tctx->cached_refs += refill; | |
6909 | } | |
ba88ff11 | 6910 | io_submit_state_start(&ctx->submit_state, nr); |
b14cca0c | 6911 | |
46c4e16a | 6912 | while (submitted < nr) { |
3529d8c2 | 6913 | const struct io_uring_sqe *sqe; |
196be95c | 6914 | struct io_kiocb *req; |
fb5ccc98 | 6915 | |
258b29a9 | 6916 | req = io_alloc_req(ctx); |
196be95c PB |
6917 | if (unlikely(!req)) { |
6918 | if (!submitted) | |
6919 | submitted = -EAGAIN; | |
fb5ccc98 | 6920 | break; |
196be95c | 6921 | } |
4fccfcbb PB |
6922 | sqe = io_get_sqe(ctx); |
6923 | if (unlikely(!sqe)) { | |
0c6e1d7f | 6924 | list_add(&req->inflight_entry, &ctx->submit_state.free_list); |
4fccfcbb PB |
6925 | break; |
6926 | } | |
d3656344 JA |
6927 | /* will complete beyond this point, count as submitted */ |
6928 | submitted++; | |
a1ab7b35 | 6929 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 6930 | break; |
6c271ce2 JA |
6931 | } |
6932 | ||
9466f437 PB |
6933 | if (unlikely(submitted != nr)) { |
6934 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 | 6935 | int unused = nr - ref_used; |
9466f437 | 6936 | |
09899b19 | 6937 | current->io_uring->cached_refs += unused; |
d8a6df10 | 6938 | percpu_ref_put_many(&ctx->refs, unused); |
9466f437 | 6939 | } |
6c271ce2 | 6940 | |
a1ab7b35 | 6941 | io_submit_state_end(&ctx->submit_state, ctx); |
ae9428ca PB |
6942 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
6943 | io_commit_sqring(ctx); | |
6944 | ||
6c271ce2 JA |
6945 | return submitted; |
6946 | } | |
6947 | ||
e4b6d902 PB |
6948 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
6949 | { | |
6950 | return READ_ONCE(sqd->state); | |
6951 | } | |
6952 | ||
23b3628e XW |
6953 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
6954 | { | |
6955 | /* Tell userspace we may need a wakeup call */ | |
79ebeaee | 6956 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
6957 | WRITE_ONCE(ctx->rings->sq_flags, |
6958 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 6959 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
6960 | } |
6961 | ||
6962 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
6963 | { | |
79ebeaee | 6964 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
6965 | WRITE_ONCE(ctx->rings->sq_flags, |
6966 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 6967 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
6968 | } |
6969 | ||
08369246 | 6970 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 6971 | { |
c8d1ba58 | 6972 | unsigned int to_submit; |
bdcd3eab | 6973 | int ret = 0; |
6c271ce2 | 6974 | |
c8d1ba58 | 6975 | to_submit = io_sqring_entries(ctx); |
e95eee2d | 6976 | /* if we're handling multiple rings, cap submit size for fairness */ |
4ce8ad95 OL |
6977 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
6978 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; | |
e95eee2d | 6979 | |
906a3c6f | 6980 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
c8d1ba58 | 6981 | unsigned nr_events = 0; |
948e1947 PB |
6982 | const struct cred *creds = NULL; |
6983 | ||
6984 | if (ctx->sq_creds != current_cred()) | |
6985 | creds = override_creds(ctx->sq_creds); | |
a4c0b3de | 6986 | |
c8d1ba58 | 6987 | mutex_lock(&ctx->uring_lock); |
906a3c6f | 6988 | if (!list_empty(&ctx->iopoll_list)) |
a8576af9 | 6989 | io_do_iopoll(ctx, &nr_events, 0); |
906a3c6f | 6990 | |
3b763ba1 PB |
6991 | /* |
6992 | * Don't submit if refs are dying, good for io_uring_register(), | |
6993 | * but also it is relied upon by io_ring_exit_work() | |
6994 | */ | |
0298ef96 PB |
6995 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
6996 | !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
08369246 | 6997 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 | 6998 | mutex_unlock(&ctx->uring_lock); |
6c271ce2 | 6999 | |
acfb381d PB |
7000 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
7001 | wake_up(&ctx->sqo_sq_wait); | |
948e1947 PB |
7002 | if (creds) |
7003 | revert_creds(creds); | |
acfb381d | 7004 | } |
6c271ce2 | 7005 | |
08369246 XW |
7006 | return ret; |
7007 | } | |
6c271ce2 | 7008 | |
08369246 XW |
7009 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
7010 | { | |
7011 | struct io_ring_ctx *ctx; | |
7012 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 7013 | |
c9dca27d PB |
7014 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7015 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); | |
08369246 | 7016 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 7017 | } |
6c271ce2 | 7018 | |
e4b6d902 PB |
7019 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
7020 | { | |
7021 | bool did_sig = false; | |
7022 | struct ksignal ksig; | |
7023 | ||
7024 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || | |
7025 | signal_pending(current)) { | |
7026 | mutex_unlock(&sqd->lock); | |
7027 | if (signal_pending(current)) | |
7028 | did_sig = get_signal(&ksig); | |
7029 | cond_resched(); | |
7030 | mutex_lock(&sqd->lock); | |
7031 | } | |
e4b6d902 PB |
7032 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
7033 | } | |
7034 | ||
c8d1ba58 JA |
7035 | static int io_sq_thread(void *data) |
7036 | { | |
69fb2131 JA |
7037 | struct io_sq_data *sqd = data; |
7038 | struct io_ring_ctx *ctx; | |
a0d9205f | 7039 | unsigned long timeout = 0; |
37d1e2e3 | 7040 | char buf[TASK_COMM_LEN]; |
08369246 | 7041 | DEFINE_WAIT(wait); |
6c271ce2 | 7042 | |
696ee88a | 7043 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
37d1e2e3 | 7044 | set_task_comm(current, buf); |
37d1e2e3 JA |
7045 | |
7046 | if (sqd->sq_cpu != -1) | |
7047 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
7048 | else | |
7049 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
7050 | current->flags |= PF_NO_SETAFFINITY; | |
7051 | ||
09a6f4ef | 7052 | mutex_lock(&sqd->lock); |
e4b6d902 | 7053 | while (1) { |
1a924a80 | 7054 | bool cap_entries, sqt_spin = false; |
c1edbf5f | 7055 | |
e4b6d902 PB |
7056 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
7057 | if (io_sqd_handle_event(sqd)) | |
c7d95613 | 7058 | break; |
08369246 XW |
7059 | timeout = jiffies + sqd->sq_thread_idle; |
7060 | } | |
e4b6d902 | 7061 | |
e95eee2d | 7062 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 7063 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
948e1947 | 7064 | int ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a | 7065 | |
08369246 XW |
7066 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) |
7067 | sqt_spin = true; | |
69fb2131 | 7068 | } |
dd432ea5 PB |
7069 | if (io_run_task_work()) |
7070 | sqt_spin = true; | |
6c271ce2 | 7071 | |
08369246 | 7072 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 7073 | cond_resched(); |
08369246 XW |
7074 | if (sqt_spin) |
7075 | timeout = jiffies + sqd->sq_thread_idle; | |
7076 | continue; | |
7077 | } | |
7078 | ||
08369246 | 7079 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
dd432ea5 | 7080 | if (!io_sqd_events_pending(sqd) && !current->task_works) { |
1a924a80 PB |
7081 | bool needs_sched = true; |
7082 | ||
724cb4f9 | 7083 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
aaa9f0f4 PB |
7084 | io_ring_set_wakeup_flag(ctx); |
7085 | ||
724cb4f9 HX |
7086 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
7087 | !list_empty_careful(&ctx->iopoll_list)) { | |
7088 | needs_sched = false; | |
7089 | break; | |
7090 | } | |
7091 | if (io_sqring_entries(ctx)) { | |
7092 | needs_sched = false; | |
7093 | break; | |
7094 | } | |
7095 | } | |
7096 | ||
7097 | if (needs_sched) { | |
7098 | mutex_unlock(&sqd->lock); | |
7099 | schedule(); | |
7100 | mutex_lock(&sqd->lock); | |
7101 | } | |
69fb2131 JA |
7102 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7103 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 7104 | } |
08369246 XW |
7105 | |
7106 | finish_wait(&sqd->wait, &wait); | |
7107 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 | 7108 | } |
28cea78a | 7109 | |
78cc687b | 7110 | io_uring_cancel_generic(true, sqd); |
37d1e2e3 | 7111 | sqd->thread = NULL; |
05962f95 | 7112 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
5f3f26f9 | 7113 | io_ring_set_wakeup_flag(ctx); |
521d6a73 | 7114 | io_run_task_work(); |
734551df PB |
7115 | mutex_unlock(&sqd->lock); |
7116 | ||
37d1e2e3 JA |
7117 | complete(&sqd->exited); |
7118 | do_exit(0); | |
6c271ce2 JA |
7119 | } |
7120 | ||
bda52162 JA |
7121 | struct io_wait_queue { |
7122 | struct wait_queue_entry wq; | |
7123 | struct io_ring_ctx *ctx; | |
5fd46178 | 7124 | unsigned cq_tail; |
bda52162 JA |
7125 | unsigned nr_timeouts; |
7126 | }; | |
7127 | ||
6c503150 | 7128 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
7129 | { |
7130 | struct io_ring_ctx *ctx = iowq->ctx; | |
5fd46178 | 7131 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
bda52162 JA |
7132 | |
7133 | /* | |
d195a66e | 7134 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
7135 | * started waiting. For timeouts, we always want to return to userspace, |
7136 | * regardless of event count. | |
7137 | */ | |
5fd46178 | 7138 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
bda52162 JA |
7139 | } |
7140 | ||
7141 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
7142 | int wake_flags, void *key) | |
7143 | { | |
7144 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
7145 | wq); | |
7146 | ||
6c503150 PB |
7147 | /* |
7148 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
7149 | * the task, and the next invocation will do it. | |
7150 | */ | |
5ed7a37d | 7151 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow)) |
6c503150 PB |
7152 | return autoremove_wake_function(curr, mode, wake_flags, key); |
7153 | return -1; | |
bda52162 JA |
7154 | } |
7155 | ||
af9c1a44 JA |
7156 | static int io_run_task_work_sig(void) |
7157 | { | |
7158 | if (io_run_task_work()) | |
7159 | return 1; | |
7160 | if (!signal_pending(current)) | |
7161 | return 0; | |
0b8cfa97 | 7162 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
792ee0f6 | 7163 | return -ERESTARTSYS; |
af9c1a44 JA |
7164 | return -EINTR; |
7165 | } | |
7166 | ||
eeb60b9a PB |
7167 | /* when returns >0, the caller should retry */ |
7168 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
7169 | struct io_wait_queue *iowq, | |
7170 | signed long *timeout) | |
7171 | { | |
7172 | int ret; | |
7173 | ||
7174 | /* make sure we run task_work before checking for signals */ | |
7175 | ret = io_run_task_work_sig(); | |
7176 | if (ret || io_should_wake(iowq)) | |
7177 | return ret; | |
7178 | /* let the caller flush overflows, retry */ | |
5ed7a37d | 7179 | if (test_bit(0, &ctx->check_cq_overflow)) |
eeb60b9a PB |
7180 | return 1; |
7181 | ||
7182 | *timeout = schedule_timeout(*timeout); | |
7183 | return !*timeout ? -ETIME : 1; | |
7184 | } | |
7185 | ||
2b188cc1 JA |
7186 | /* |
7187 | * Wait until events become available, if we don't already have some. The | |
7188 | * application must reap them itself, as they reside on the shared cq ring. | |
7189 | */ | |
7190 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
7191 | const sigset_t __user *sig, size_t sigsz, |
7192 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 7193 | { |
90291099 | 7194 | struct io_wait_queue iowq; |
75b28aff | 7195 | struct io_rings *rings = ctx->rings; |
c1d5a224 PB |
7196 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
7197 | int ret; | |
2b188cc1 | 7198 | |
b41e9852 | 7199 | do { |
90f67366 | 7200 | io_cqring_overflow_flush(ctx); |
6c503150 | 7201 | if (io_cqring_events(ctx) >= min_events) |
b41e9852 | 7202 | return 0; |
4c6e277c | 7203 | if (!io_run_task_work()) |
b41e9852 | 7204 | break; |
b41e9852 | 7205 | } while (1); |
2b188cc1 JA |
7206 | |
7207 | if (sig) { | |
9e75ad5d AB |
7208 | #ifdef CONFIG_COMPAT |
7209 | if (in_compat_syscall()) | |
7210 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 7211 | sigsz); |
9e75ad5d AB |
7212 | else |
7213 | #endif | |
b772434b | 7214 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 7215 | |
2b188cc1 JA |
7216 | if (ret) |
7217 | return ret; | |
7218 | } | |
7219 | ||
c73ebb68 | 7220 | if (uts) { |
c1d5a224 PB |
7221 | struct timespec64 ts; |
7222 | ||
c73ebb68 HX |
7223 | if (get_timespec64(&ts, uts)) |
7224 | return -EFAULT; | |
7225 | timeout = timespec64_to_jiffies(&ts); | |
7226 | } | |
7227 | ||
90291099 PB |
7228 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
7229 | iowq.wq.private = current; | |
7230 | INIT_LIST_HEAD(&iowq.wq.entry); | |
7231 | iowq.ctx = ctx; | |
bda52162 | 7232 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
5fd46178 | 7233 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
90291099 | 7234 | |
c826bd7a | 7235 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 7236 | do { |
ca0a2651 | 7237 | /* if we can't even flush overflow, don't wait for more */ |
90f67366 | 7238 | if (!io_cqring_overflow_flush(ctx)) { |
ca0a2651 JA |
7239 | ret = -EBUSY; |
7240 | break; | |
7241 | } | |
311997b3 | 7242 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
bda52162 | 7243 | TASK_INTERRUPTIBLE); |
eeb60b9a | 7244 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
311997b3 | 7245 | finish_wait(&ctx->cq_wait, &iowq.wq); |
ca0a2651 | 7246 | cond_resched(); |
eeb60b9a | 7247 | } while (ret > 0); |
bda52162 | 7248 | |
b7db41c9 | 7249 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7250 | |
75b28aff | 7251 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7252 | } |
7253 | ||
9123c8ff | 7254 | static void io_free_page_table(void **table, size_t size) |
05f3fb3c | 7255 | { |
9123c8ff | 7256 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
05f3fb3c | 7257 | |
846a4ef2 | 7258 | for (i = 0; i < nr_tables; i++) |
9123c8ff PB |
7259 | kfree(table[i]); |
7260 | kfree(table); | |
7261 | } | |
7262 | ||
7263 | static void **io_alloc_page_table(size_t size) | |
7264 | { | |
7265 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); | |
7266 | size_t init_size = size; | |
7267 | void **table; | |
7268 | ||
0bea96f5 | 7269 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7270 | if (!table) |
7271 | return NULL; | |
7272 | ||
7273 | for (i = 0; i < nr_tables; i++) { | |
27f6b318 | 7274 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
9123c8ff | 7275 | |
0bea96f5 | 7276 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7277 | if (!table[i]) { |
7278 | io_free_page_table(table, init_size); | |
7279 | return NULL; | |
7280 | } | |
7281 | size -= this_size; | |
7282 | } | |
7283 | return table; | |
05f3fb3c JA |
7284 | } |
7285 | ||
28a9fe25 | 7286 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
1642b445 | 7287 | { |
28a9fe25 PB |
7288 | percpu_ref_exit(&ref_node->refs); |
7289 | kfree(ref_node); | |
1642b445 PB |
7290 | } |
7291 | ||
b9bd2bea PB |
7292 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
7293 | { | |
7294 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); | |
7295 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; | |
7296 | unsigned long flags; | |
7297 | bool first_add = false; | |
7298 | ||
7299 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); | |
7300 | node->done = true; | |
7301 | ||
7302 | while (!list_empty(&ctx->rsrc_ref_list)) { | |
7303 | node = list_first_entry(&ctx->rsrc_ref_list, | |
7304 | struct io_rsrc_node, node); | |
7305 | /* recycle ref nodes in order */ | |
7306 | if (!node->done) | |
7307 | break; | |
7308 | list_del(&node->node); | |
7309 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); | |
7310 | } | |
7311 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); | |
7312 | ||
7313 | if (first_add) | |
7314 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ); | |
7315 | } | |
7316 | ||
7317 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx) | |
7318 | { | |
7319 | struct io_rsrc_node *ref_node; | |
7320 | ||
7321 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); | |
7322 | if (!ref_node) | |
7323 | return NULL; | |
7324 | ||
7325 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, | |
7326 | 0, GFP_KERNEL)) { | |
7327 | kfree(ref_node); | |
7328 | return NULL; | |
7329 | } | |
7330 | INIT_LIST_HEAD(&ref_node->node); | |
7331 | INIT_LIST_HEAD(&ref_node->rsrc_list); | |
7332 | ref_node->done = false; | |
7333 | return ref_node; | |
7334 | } | |
7335 | ||
a7f0ed5a PB |
7336 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
7337 | struct io_rsrc_data *data_to_kill) | |
6b06314c | 7338 | { |
a7f0ed5a PB |
7339 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
7340 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); | |
6b06314c | 7341 | |
a7f0ed5a PB |
7342 | if (data_to_kill) { |
7343 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; | |
82fbcfa9 | 7344 | |
a7f0ed5a | 7345 | rsrc_node->rsrc_data = data_to_kill; |
4956b9ea | 7346 | spin_lock_irq(&ctx->rsrc_ref_lock); |
a7f0ed5a | 7347 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
4956b9ea | 7348 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
82fbcfa9 | 7349 | |
3e942498 | 7350 | atomic_inc(&data_to_kill->refs); |
a7f0ed5a PB |
7351 | percpu_ref_kill(&rsrc_node->refs); |
7352 | ctx->rsrc_node = NULL; | |
7353 | } | |
6b06314c | 7354 | |
a7f0ed5a PB |
7355 | if (!ctx->rsrc_node) { |
7356 | ctx->rsrc_node = ctx->rsrc_backup_node; | |
7357 | ctx->rsrc_backup_node = NULL; | |
7358 | } | |
8bad28d8 HX |
7359 | } |
7360 | ||
a7f0ed5a | 7361 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
8dd03afe PB |
7362 | { |
7363 | if (ctx->rsrc_backup_node) | |
7364 | return 0; | |
b895c9a6 | 7365 | ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx); |
8dd03afe | 7366 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
8bad28d8 HX |
7367 | } |
7368 | ||
40ae0ff7 | 7369 | static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, struct io_ring_ctx *ctx) |
8bad28d8 HX |
7370 | { |
7371 | int ret; | |
05589553 | 7372 | |
215c3902 | 7373 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
8bad28d8 HX |
7374 | if (data->quiesce) |
7375 | return -ENXIO; | |
05589553 | 7376 | |
8bad28d8 | 7377 | data->quiesce = true; |
1ffc5422 | 7378 | do { |
a7f0ed5a | 7379 | ret = io_rsrc_node_switch_start(ctx); |
8dd03afe | 7380 | if (ret) |
f2303b1f | 7381 | break; |
a7f0ed5a | 7382 | io_rsrc_node_switch(ctx, data); |
f2303b1f | 7383 | |
3e942498 PB |
7384 | /* kill initial ref, already quiesced if zero */ |
7385 | if (atomic_dec_and_test(&data->refs)) | |
7386 | break; | |
c018db4a | 7387 | mutex_unlock(&ctx->uring_lock); |
8bad28d8 | 7388 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 | 7389 | ret = wait_for_completion_interruptible(&data->done); |
c018db4a JA |
7390 | if (!ret) { |
7391 | mutex_lock(&ctx->uring_lock); | |
1ffc5422 | 7392 | break; |
c018db4a | 7393 | } |
8bad28d8 | 7394 | |
3e942498 PB |
7395 | atomic_inc(&data->refs); |
7396 | /* wait for all works potentially completing data->done */ | |
7397 | flush_delayed_work(&ctx->rsrc_put_work); | |
cb5e1b81 | 7398 | reinit_completion(&data->done); |
8dd03afe | 7399 | |
1ffc5422 | 7400 | ret = io_run_task_work_sig(); |
8bad28d8 | 7401 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 7402 | } while (ret >= 0); |
8bad28d8 | 7403 | data->quiesce = false; |
05f3fb3c | 7404 | |
8bad28d8 | 7405 | return ret; |
d7954b2b BM |
7406 | } |
7407 | ||
2d091d62 PB |
7408 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
7409 | { | |
7410 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; | |
7411 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; | |
7412 | ||
7413 | return &data->tags[table_idx][off]; | |
7414 | } | |
7415 | ||
44b31f2f | 7416 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
1ad555c6 | 7417 | { |
2d091d62 PB |
7418 | size_t size = data->nr * sizeof(data->tags[0][0]); |
7419 | ||
7420 | if (data->tags) | |
7421 | io_free_page_table((void **)data->tags, size); | |
44b31f2f PB |
7422 | kfree(data); |
7423 | } | |
7424 | ||
d878c816 PB |
7425 | static int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
7426 | u64 __user *utags, unsigned nr, | |
7427 | struct io_rsrc_data **pdata) | |
1ad555c6 | 7428 | { |
b895c9a6 | 7429 | struct io_rsrc_data *data; |
2d091d62 | 7430 | int ret = -ENOMEM; |
d878c816 | 7431 | unsigned i; |
1ad555c6 BM |
7432 | |
7433 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7434 | if (!data) | |
d878c816 | 7435 | return -ENOMEM; |
2d091d62 | 7436 | data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0])); |
b60c8dce | 7437 | if (!data->tags) { |
1ad555c6 | 7438 | kfree(data); |
d878c816 PB |
7439 | return -ENOMEM; |
7440 | } | |
2d091d62 PB |
7441 | |
7442 | data->nr = nr; | |
7443 | data->ctx = ctx; | |
7444 | data->do_put = do_put; | |
d878c816 | 7445 | if (utags) { |
2d091d62 | 7446 | ret = -EFAULT; |
d878c816 | 7447 | for (i = 0; i < nr; i++) { |
fdd1dc31 CIK |
7448 | u64 *tag_slot = io_get_tag_slot(data, i); |
7449 | ||
7450 | if (copy_from_user(tag_slot, &utags[i], | |
7451 | sizeof(*tag_slot))) | |
2d091d62 | 7452 | goto fail; |
d878c816 | 7453 | } |
1ad555c6 | 7454 | } |
b60c8dce | 7455 | |
3e942498 | 7456 | atomic_set(&data->refs, 1); |
1ad555c6 | 7457 | init_completion(&data->done); |
d878c816 PB |
7458 | *pdata = data; |
7459 | return 0; | |
2d091d62 PB |
7460 | fail: |
7461 | io_rsrc_data_free(data); | |
7462 | return ret; | |
1ad555c6 BM |
7463 | } |
7464 | ||
9123c8ff PB |
7465 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
7466 | { | |
0bea96f5 PB |
7467 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
7468 | GFP_KERNEL_ACCOUNT); | |
9123c8ff PB |
7469 | return !!table->files; |
7470 | } | |
7471 | ||
042b0d85 | 7472 | static void io_free_file_tables(struct io_file_table *table) |
9123c8ff | 7473 | { |
042b0d85 | 7474 | kvfree(table->files); |
9123c8ff PB |
7475 | table->files = NULL; |
7476 | } | |
7477 | ||
fff4db76 | 7478 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
1ad555c6 | 7479 | { |
fff4db76 PB |
7480 | #if defined(CONFIG_UNIX) |
7481 | if (ctx->ring_sock) { | |
7482 | struct sock *sock = ctx->ring_sock->sk; | |
7483 | struct sk_buff *skb; | |
7484 | ||
7485 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
7486 | kfree_skb(skb); | |
7487 | } | |
7488 | #else | |
7489 | int i; | |
7490 | ||
7491 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7492 | struct file *file; | |
7493 | ||
7494 | file = io_file_from_index(ctx, i); | |
7495 | if (file) | |
7496 | fput(file); | |
7497 | } | |
7498 | #endif | |
042b0d85 | 7499 | io_free_file_tables(&ctx->file_table); |
44b31f2f | 7500 | io_rsrc_data_free(ctx->file_data); |
fff4db76 PB |
7501 | ctx->file_data = NULL; |
7502 | ctx->nr_user_files = 0; | |
1ad555c6 BM |
7503 | } |
7504 | ||
d7954b2b BM |
7505 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7506 | { | |
d7954b2b BM |
7507 | int ret; |
7508 | ||
08480400 | 7509 | if (!ctx->file_data) |
d7954b2b | 7510 | return -ENXIO; |
08480400 PB |
7511 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
7512 | if (!ret) | |
7513 | __io_sqe_files_unregister(ctx); | |
7514 | return ret; | |
6b06314c JA |
7515 | } |
7516 | ||
37d1e2e3 | 7517 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
09a6f4ef | 7518 | __releases(&sqd->lock) |
37d1e2e3 | 7519 | { |
521d6a73 PB |
7520 | WARN_ON_ONCE(sqd->thread == current); |
7521 | ||
9e138a48 PB |
7522 | /* |
7523 | * Do the dance but not conditional clear_bit() because it'd race with | |
7524 | * other threads incrementing park_pending and setting the bit. | |
7525 | */ | |
37d1e2e3 | 7526 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
9e138a48 PB |
7527 | if (atomic_dec_return(&sqd->park_pending)) |
7528 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
09a6f4ef | 7529 | mutex_unlock(&sqd->lock); |
37d1e2e3 JA |
7530 | } |
7531 | ||
86e0d676 | 7532 | static void io_sq_thread_park(struct io_sq_data *sqd) |
09a6f4ef | 7533 | __acquires(&sqd->lock) |
37d1e2e3 | 7534 | { |
521d6a73 PB |
7535 | WARN_ON_ONCE(sqd->thread == current); |
7536 | ||
9e138a48 | 7537 | atomic_inc(&sqd->park_pending); |
86e0d676 | 7538 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
09a6f4ef | 7539 | mutex_lock(&sqd->lock); |
05962f95 | 7540 | if (sqd->thread) |
86e0d676 | 7541 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
7542 | } |
7543 | ||
7544 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
7545 | { | |
521d6a73 | 7546 | WARN_ON_ONCE(sqd->thread == current); |
88885f66 | 7547 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
521d6a73 | 7548 | |
05962f95 | 7549 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
88885f66 | 7550 | mutex_lock(&sqd->lock); |
e8f98f24 JA |
7551 | if (sqd->thread) |
7552 | wake_up_process(sqd->thread); | |
09a6f4ef | 7553 | mutex_unlock(&sqd->lock); |
05962f95 | 7554 | wait_for_completion(&sqd->exited); |
37d1e2e3 JA |
7555 | } |
7556 | ||
534ca6d6 | 7557 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7558 | { |
534ca6d6 | 7559 | if (refcount_dec_and_test(&sqd->refs)) { |
9e138a48 PB |
7560 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
7561 | ||
37d1e2e3 JA |
7562 | io_sq_thread_stop(sqd); |
7563 | kfree(sqd); | |
7564 | } | |
7565 | } | |
7566 | ||
7567 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
7568 | { | |
7569 | struct io_sq_data *sqd = ctx->sq_data; | |
7570 | ||
7571 | if (sqd) { | |
05962f95 | 7572 | io_sq_thread_park(sqd); |
521d6a73 | 7573 | list_del_init(&ctx->sqd_list); |
37d1e2e3 | 7574 | io_sqd_update_thread_idle(sqd); |
05962f95 | 7575 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
7576 | |
7577 | io_put_sq_data(sqd); | |
7578 | ctx->sq_data = NULL; | |
534ca6d6 JA |
7579 | } |
7580 | } | |
7581 | ||
aa06165d JA |
7582 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7583 | { | |
7584 | struct io_ring_ctx *ctx_attach; | |
7585 | struct io_sq_data *sqd; | |
7586 | struct fd f; | |
7587 | ||
7588 | f = fdget(p->wq_fd); | |
7589 | if (!f.file) | |
7590 | return ERR_PTR(-ENXIO); | |
7591 | if (f.file->f_op != &io_uring_fops) { | |
7592 | fdput(f); | |
7593 | return ERR_PTR(-EINVAL); | |
7594 | } | |
7595 | ||
7596 | ctx_attach = f.file->private_data; | |
7597 | sqd = ctx_attach->sq_data; | |
7598 | if (!sqd) { | |
7599 | fdput(f); | |
7600 | return ERR_PTR(-EINVAL); | |
7601 | } | |
5c2469e0 JA |
7602 | if (sqd->task_tgid != current->tgid) { |
7603 | fdput(f); | |
7604 | return ERR_PTR(-EPERM); | |
7605 | } | |
aa06165d JA |
7606 | |
7607 | refcount_inc(&sqd->refs); | |
7608 | fdput(f); | |
7609 | return sqd; | |
7610 | } | |
7611 | ||
26984fbf PB |
7612 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
7613 | bool *attached) | |
534ca6d6 JA |
7614 | { |
7615 | struct io_sq_data *sqd; | |
7616 | ||
26984fbf | 7617 | *attached = false; |
5c2469e0 JA |
7618 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
7619 | sqd = io_attach_sq_data(p); | |
26984fbf PB |
7620 | if (!IS_ERR(sqd)) { |
7621 | *attached = true; | |
5c2469e0 | 7622 | return sqd; |
26984fbf | 7623 | } |
5c2469e0 JA |
7624 | /* fall through for EPERM case, setup new sqd/task */ |
7625 | if (PTR_ERR(sqd) != -EPERM) | |
7626 | return sqd; | |
7627 | } | |
aa06165d | 7628 | |
534ca6d6 JA |
7629 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7630 | if (!sqd) | |
7631 | return ERR_PTR(-ENOMEM); | |
7632 | ||
9e138a48 | 7633 | atomic_set(&sqd->park_pending, 0); |
534ca6d6 | 7634 | refcount_set(&sqd->refs, 1); |
69fb2131 | 7635 | INIT_LIST_HEAD(&sqd->ctx_list); |
09a6f4ef | 7636 | mutex_init(&sqd->lock); |
534ca6d6 | 7637 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 7638 | init_completion(&sqd->exited); |
534ca6d6 JA |
7639 | return sqd; |
7640 | } | |
7641 | ||
6b06314c | 7642 | #if defined(CONFIG_UNIX) |
6b06314c JA |
7643 | /* |
7644 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
7645 | * the io_uring can be safely unregistered on process exit, even if we have | |
7646 | * loops in the file referencing. | |
7647 | */ | |
7648 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
7649 | { | |
7650 | struct sock *sk = ctx->ring_sock->sk; | |
7651 | struct scm_fp_list *fpl; | |
7652 | struct sk_buff *skb; | |
08a45173 | 7653 | int i, nr_files; |
6b06314c | 7654 | |
6b06314c JA |
7655 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
7656 | if (!fpl) | |
7657 | return -ENOMEM; | |
7658 | ||
7659 | skb = alloc_skb(0, GFP_KERNEL); | |
7660 | if (!skb) { | |
7661 | kfree(fpl); | |
7662 | return -ENOMEM; | |
7663 | } | |
7664 | ||
7665 | skb->sk = sk; | |
6b06314c | 7666 | |
08a45173 | 7667 | nr_files = 0; |
62e398be | 7668 | fpl->user = get_uid(current_user()); |
6b06314c | 7669 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
7670 | struct file *file = io_file_from_index(ctx, i + offset); |
7671 | ||
7672 | if (!file) | |
08a45173 | 7673 | continue; |
65e19f54 | 7674 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
7675 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
7676 | nr_files++; | |
6b06314c JA |
7677 | } |
7678 | ||
08a45173 JA |
7679 | if (nr_files) { |
7680 | fpl->max = SCM_MAX_FD; | |
7681 | fpl->count = nr_files; | |
7682 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 7683 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
7684 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
7685 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 7686 | |
08a45173 JA |
7687 | for (i = 0; i < nr_files; i++) |
7688 | fput(fpl->fp[i]); | |
7689 | } else { | |
7690 | kfree_skb(skb); | |
7691 | kfree(fpl); | |
7692 | } | |
6b06314c JA |
7693 | |
7694 | return 0; | |
7695 | } | |
7696 | ||
7697 | /* | |
7698 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
7699 | * causes regular reference counting to break down. We rely on the UNIX | |
7700 | * garbage collection to take care of this problem for us. | |
7701 | */ | |
7702 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7703 | { | |
7704 | unsigned left, total; | |
7705 | int ret = 0; | |
7706 | ||
7707 | total = 0; | |
7708 | left = ctx->nr_user_files; | |
7709 | while (left) { | |
7710 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
7711 | |
7712 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
7713 | if (ret) | |
7714 | break; | |
7715 | left -= this_files; | |
7716 | total += this_files; | |
7717 | } | |
7718 | ||
7719 | if (!ret) | |
7720 | return 0; | |
7721 | ||
7722 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
7723 | struct file *file = io_file_from_index(ctx, total); |
7724 | ||
7725 | if (file) | |
7726 | fput(file); | |
6b06314c JA |
7727 | total++; |
7728 | } | |
7729 | ||
7730 | return ret; | |
7731 | } | |
7732 | #else | |
7733 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7734 | { | |
7735 | return 0; | |
7736 | } | |
7737 | #endif | |
7738 | ||
47e90392 | 7739 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 7740 | { |
50238531 | 7741 | struct file *file = prsrc->file; |
05f3fb3c JA |
7742 | #if defined(CONFIG_UNIX) |
7743 | struct sock *sock = ctx->ring_sock->sk; | |
7744 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
7745 | struct sk_buff *skb; | |
7746 | int i; | |
7747 | ||
7748 | __skb_queue_head_init(&list); | |
7749 | ||
7750 | /* | |
7751 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
7752 | * remove this entry and rearrange the file array. | |
7753 | */ | |
7754 | skb = skb_dequeue(head); | |
7755 | while (skb) { | |
7756 | struct scm_fp_list *fp; | |
7757 | ||
7758 | fp = UNIXCB(skb).fp; | |
7759 | for (i = 0; i < fp->count; i++) { | |
7760 | int left; | |
7761 | ||
7762 | if (fp->fp[i] != file) | |
7763 | continue; | |
7764 | ||
7765 | unix_notinflight(fp->user, fp->fp[i]); | |
7766 | left = fp->count - 1 - i; | |
7767 | if (left) { | |
7768 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
7769 | left * sizeof(struct file *)); | |
7770 | } | |
7771 | fp->count--; | |
7772 | if (!fp->count) { | |
7773 | kfree_skb(skb); | |
7774 | skb = NULL; | |
7775 | } else { | |
7776 | __skb_queue_tail(&list, skb); | |
7777 | } | |
7778 | fput(file); | |
7779 | file = NULL; | |
7780 | break; | |
7781 | } | |
7782 | ||
7783 | if (!file) | |
7784 | break; | |
7785 | ||
7786 | __skb_queue_tail(&list, skb); | |
7787 | ||
7788 | skb = skb_dequeue(head); | |
7789 | } | |
7790 | ||
7791 | if (skb_peek(&list)) { | |
7792 | spin_lock_irq(&head->lock); | |
7793 | while ((skb = __skb_dequeue(&list)) != NULL) | |
7794 | __skb_queue_tail(head, skb); | |
7795 | spin_unlock_irq(&head->lock); | |
7796 | } | |
7797 | #else | |
7798 | fput(file); | |
7799 | #endif | |
7800 | } | |
7801 | ||
b895c9a6 | 7802 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
65e19f54 | 7803 | { |
b895c9a6 | 7804 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
269bbe5f BM |
7805 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
7806 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 7807 | |
269bbe5f BM |
7808 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
7809 | list_del(&prsrc->list); | |
b60c8dce PB |
7810 | |
7811 | if (prsrc->tag) { | |
7812 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; | |
b60c8dce PB |
7813 | |
7814 | io_ring_submit_lock(ctx, lock_ring); | |
79ebeaee | 7815 | spin_lock(&ctx->completion_lock); |
b60c8dce | 7816 | io_cqring_fill_event(ctx, prsrc->tag, 0, 0); |
2840f710 | 7817 | ctx->cq_extra++; |
b60c8dce | 7818 | io_commit_cqring(ctx); |
79ebeaee | 7819 | spin_unlock(&ctx->completion_lock); |
b60c8dce PB |
7820 | io_cqring_ev_posted(ctx); |
7821 | io_ring_submit_unlock(ctx, lock_ring); | |
7822 | } | |
7823 | ||
40ae0ff7 | 7824 | rsrc_data->do_put(ctx, prsrc); |
269bbe5f | 7825 | kfree(prsrc); |
65e19f54 | 7826 | } |
05589553 | 7827 | |
28a9fe25 | 7828 | io_rsrc_node_destroy(ref_node); |
3e942498 PB |
7829 | if (atomic_dec_and_test(&rsrc_data->refs)) |
7830 | complete(&rsrc_data->done); | |
2faf852d | 7831 | } |
65e19f54 | 7832 | |
269bbe5f | 7833 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
7834 | { |
7835 | struct io_ring_ctx *ctx; | |
7836 | struct llist_node *node; | |
7837 | ||
269bbe5f BM |
7838 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
7839 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
7840 | |
7841 | while (node) { | |
b895c9a6 | 7842 | struct io_rsrc_node *ref_node; |
4a38aed2 JA |
7843 | struct llist_node *next = node->next; |
7844 | ||
b895c9a6 | 7845 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
269bbe5f | 7846 | __io_rsrc_put_work(ref_node); |
4a38aed2 JA |
7847 | node = next; |
7848 | } | |
7849 | } | |
7850 | ||
6b06314c | 7851 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
792e3582 | 7852 | unsigned nr_args, u64 __user *tags) |
6b06314c JA |
7853 | { |
7854 | __s32 __user *fds = (__s32 __user *) arg; | |
05f3fb3c | 7855 | struct file *file; |
f3baed39 | 7856 | int fd, ret; |
846a4ef2 | 7857 | unsigned i; |
6b06314c | 7858 | |
05f3fb3c | 7859 | if (ctx->file_data) |
6b06314c JA |
7860 | return -EBUSY; |
7861 | if (!nr_args) | |
7862 | return -EINVAL; | |
7863 | if (nr_args > IORING_MAX_FIXED_FILES) | |
7864 | return -EMFILE; | |
3a1b8a4e PB |
7865 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
7866 | return -EMFILE; | |
a7f0ed5a | 7867 | ret = io_rsrc_node_switch_start(ctx); |
f3baed39 PB |
7868 | if (ret) |
7869 | return ret; | |
d878c816 PB |
7870 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
7871 | &ctx->file_data); | |
7872 | if (ret) | |
7873 | return ret; | |
6b06314c | 7874 | |
f3baed39 | 7875 | ret = -ENOMEM; |
aeca241b | 7876 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
1ad555c6 | 7877 | goto out_free; |
65e19f54 | 7878 | |
08a45173 | 7879 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
d878c816 | 7880 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
600cf3f8 PB |
7881 | ret = -EFAULT; |
7882 | goto out_fput; | |
7883 | } | |
08a45173 | 7884 | /* allow sparse sets */ |
792e3582 PB |
7885 | if (fd == -1) { |
7886 | ret = -EINVAL; | |
2d091d62 | 7887 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
792e3582 | 7888 | goto out_fput; |
08a45173 | 7889 | continue; |
792e3582 | 7890 | } |
6b06314c | 7891 | |
05f3fb3c | 7892 | file = fget(fd); |
6b06314c | 7893 | ret = -EBADF; |
792e3582 | 7894 | if (unlikely(!file)) |
600cf3f8 | 7895 | goto out_fput; |
05f3fb3c | 7896 | |
6b06314c JA |
7897 | /* |
7898 | * Don't allow io_uring instances to be registered. If UNIX | |
7899 | * isn't enabled, then this causes a reference cycle and this | |
7900 | * instance can never get freed. If UNIX is enabled we'll | |
7901 | * handle it just fine, but there's still no point in allowing | |
7902 | * a ring fd as it doesn't support regular read/write anyway. | |
7903 | */ | |
05f3fb3c JA |
7904 | if (file->f_op == &io_uring_fops) { |
7905 | fput(file); | |
600cf3f8 | 7906 | goto out_fput; |
6b06314c | 7907 | } |
aeca241b | 7908 | io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file); |
6b06314c JA |
7909 | } |
7910 | ||
6b06314c | 7911 | ret = io_sqe_files_scm(ctx); |
05589553 | 7912 | if (ret) { |
08480400 | 7913 | __io_sqe_files_unregister(ctx); |
05589553 XW |
7914 | return ret; |
7915 | } | |
6b06314c | 7916 | |
a7f0ed5a | 7917 | io_rsrc_node_switch(ctx, NULL); |
6b06314c | 7918 | return ret; |
600cf3f8 PB |
7919 | out_fput: |
7920 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7921 | file = io_file_from_index(ctx, i); | |
7922 | if (file) | |
7923 | fput(file); | |
7924 | } | |
042b0d85 | 7925 | io_free_file_tables(&ctx->file_table); |
600cf3f8 | 7926 | ctx->nr_user_files = 0; |
600cf3f8 | 7927 | out_free: |
44b31f2f | 7928 | io_rsrc_data_free(ctx->file_data); |
55cbc256 | 7929 | ctx->file_data = NULL; |
6b06314c JA |
7930 | return ret; |
7931 | } | |
7932 | ||
c3a31e60 JA |
7933 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
7934 | int index) | |
7935 | { | |
7936 | #if defined(CONFIG_UNIX) | |
7937 | struct sock *sock = ctx->ring_sock->sk; | |
7938 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
7939 | struct sk_buff *skb; | |
7940 | ||
7941 | /* | |
7942 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
7943 | * file set. If there's no room, fall back to allocating a new skb | |
7944 | * and filling it in. | |
7945 | */ | |
7946 | spin_lock_irq(&head->lock); | |
7947 | skb = skb_peek(head); | |
7948 | if (skb) { | |
7949 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
7950 | ||
7951 | if (fpl->count < SCM_MAX_FD) { | |
7952 | __skb_unlink(skb, head); | |
7953 | spin_unlock_irq(&head->lock); | |
7954 | fpl->fp[fpl->count] = get_file(file); | |
7955 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
7956 | fpl->count++; | |
7957 | spin_lock_irq(&head->lock); | |
7958 | __skb_queue_head(head, skb); | |
7959 | } else { | |
7960 | skb = NULL; | |
7961 | } | |
7962 | } | |
7963 | spin_unlock_irq(&head->lock); | |
7964 | ||
7965 | if (skb) { | |
7966 | fput(file); | |
7967 | return 0; | |
7968 | } | |
7969 | ||
7970 | return __io_sqe_files_scm(ctx, 1, index); | |
7971 | #else | |
7972 | return 0; | |
7973 | #endif | |
7974 | } | |
7975 | ||
b9445598 PB |
7976 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
7977 | unsigned int issue_flags, u32 slot_index) | |
7978 | { | |
7979 | struct io_ring_ctx *ctx = req->ctx; | |
7980 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; | |
7981 | struct io_fixed_file *file_slot; | |
7982 | int ret = -EBADF; | |
7983 | ||
7984 | io_ring_submit_lock(ctx, !force_nonblock); | |
7985 | if (file->f_op == &io_uring_fops) | |
7986 | goto err; | |
7987 | ret = -ENXIO; | |
7988 | if (!ctx->file_data) | |
7989 | goto err; | |
7990 | ret = -EINVAL; | |
7991 | if (slot_index >= ctx->nr_user_files) | |
7992 | goto err; | |
7993 | ||
7994 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); | |
7995 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); | |
7996 | ret = -EBADF; | |
7997 | if (file_slot->file_ptr) | |
7998 | goto err; | |
7999 | ||
8000 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; | |
8001 | io_fixed_file_set(file_slot, file); | |
8002 | ret = io_sqe_file_register(ctx, file, slot_index); | |
8003 | if (ret) { | |
8004 | file_slot->file_ptr = 0; | |
8005 | goto err; | |
8006 | } | |
8007 | ||
8008 | ret = 0; | |
8009 | err: | |
8010 | io_ring_submit_unlock(ctx, !force_nonblock); | |
8011 | if (ret) | |
8012 | fput(file); | |
8013 | return ret; | |
8014 | } | |
8015 | ||
b60c8dce | 8016 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
e7c78371 | 8017 | struct io_rsrc_node *node, void *rsrc) |
05f3fb3c | 8018 | { |
269bbe5f | 8019 | struct io_rsrc_put *prsrc; |
05f3fb3c | 8020 | |
269bbe5f BM |
8021 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
8022 | if (!prsrc) | |
a5318d3c | 8023 | return -ENOMEM; |
05f3fb3c | 8024 | |
2d091d62 | 8025 | prsrc->tag = *io_get_tag_slot(data, idx); |
50238531 | 8026 | prsrc->rsrc = rsrc; |
e7c78371 | 8027 | list_add(&prsrc->list, &node->rsrc_list); |
a5318d3c | 8028 | return 0; |
05f3fb3c JA |
8029 | } |
8030 | ||
8031 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, | |
c3bdad02 | 8032 | struct io_uring_rsrc_update2 *up, |
05f3fb3c JA |
8033 | unsigned nr_args) |
8034 | { | |
c3bdad02 | 8035 | u64 __user *tags = u64_to_user_ptr(up->tags); |
98f0b3b4 | 8036 | __s32 __user *fds = u64_to_user_ptr(up->data); |
b895c9a6 | 8037 | struct io_rsrc_data *data = ctx->file_data; |
a04b0ac0 PB |
8038 | struct io_fixed_file *file_slot; |
8039 | struct file *file; | |
98f0b3b4 PB |
8040 | int fd, i, err = 0; |
8041 | unsigned int done; | |
05589553 | 8042 | bool needs_switch = false; |
c3a31e60 | 8043 | |
98f0b3b4 PB |
8044 | if (!ctx->file_data) |
8045 | return -ENXIO; | |
8046 | if (up->offset + nr_args > ctx->nr_user_files) | |
c3a31e60 JA |
8047 | return -EINVAL; |
8048 | ||
67973b93 | 8049 | for (done = 0; done < nr_args; done++) { |
c3bdad02 PB |
8050 | u64 tag = 0; |
8051 | ||
8052 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || | |
8053 | copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
c3a31e60 JA |
8054 | err = -EFAULT; |
8055 | break; | |
8056 | } | |
c3bdad02 PB |
8057 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
8058 | err = -EINVAL; | |
8059 | break; | |
8060 | } | |
4e0377a1 | 8061 | if (fd == IORING_REGISTER_FILES_SKIP) |
8062 | continue; | |
8063 | ||
67973b93 | 8064 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
aeca241b | 8065 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
ea64ec02 | 8066 | |
a04b0ac0 PB |
8067 | if (file_slot->file_ptr) { |
8068 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
b60c8dce PB |
8069 | err = io_queue_rsrc_removal(data, up->offset + done, |
8070 | ctx->rsrc_node, file); | |
a5318d3c HD |
8071 | if (err) |
8072 | break; | |
a04b0ac0 | 8073 | file_slot->file_ptr = 0; |
05589553 | 8074 | needs_switch = true; |
c3a31e60 JA |
8075 | } |
8076 | if (fd != -1) { | |
c3a31e60 JA |
8077 | file = fget(fd); |
8078 | if (!file) { | |
8079 | err = -EBADF; | |
8080 | break; | |
8081 | } | |
8082 | /* | |
8083 | * Don't allow io_uring instances to be registered. If | |
8084 | * UNIX isn't enabled, then this causes a reference | |
8085 | * cycle and this instance can never get freed. If UNIX | |
8086 | * is enabled we'll handle it just fine, but there's | |
8087 | * still no point in allowing a ring fd as it doesn't | |
8088 | * support regular read/write anyway. | |
8089 | */ | |
8090 | if (file->f_op == &io_uring_fops) { | |
8091 | fput(file); | |
8092 | err = -EBADF; | |
8093 | break; | |
8094 | } | |
2d091d62 | 8095 | *io_get_tag_slot(data, up->offset + done) = tag; |
9a321c98 | 8096 | io_fixed_file_set(file_slot, file); |
c3a31e60 | 8097 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 8098 | if (err) { |
a04b0ac0 | 8099 | file_slot->file_ptr = 0; |
f3bd9dae | 8100 | fput(file); |
c3a31e60 | 8101 | break; |
f3bd9dae | 8102 | } |
c3a31e60 | 8103 | } |
05f3fb3c JA |
8104 | } |
8105 | ||
a7f0ed5a PB |
8106 | if (needs_switch) |
8107 | io_rsrc_node_switch(ctx, data); | |
c3a31e60 JA |
8108 | return done ? done : err; |
8109 | } | |
05589553 | 8110 | |
685fe7fe JA |
8111 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
8112 | struct task_struct *task) | |
24369c2e | 8113 | { |
e941894e | 8114 | struct io_wq_hash *hash; |
24369c2e | 8115 | struct io_wq_data data; |
24369c2e | 8116 | unsigned int concurrency; |
24369c2e | 8117 | |
362a9e65 | 8118 | mutex_lock(&ctx->uring_lock); |
e941894e JA |
8119 | hash = ctx->hash_map; |
8120 | if (!hash) { | |
8121 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
362a9e65 YY |
8122 | if (!hash) { |
8123 | mutex_unlock(&ctx->uring_lock); | |
e941894e | 8124 | return ERR_PTR(-ENOMEM); |
362a9e65 | 8125 | } |
e941894e JA |
8126 | refcount_set(&hash->refs, 1); |
8127 | init_waitqueue_head(&hash->wait); | |
8128 | ctx->hash_map = hash; | |
24369c2e | 8129 | } |
362a9e65 | 8130 | mutex_unlock(&ctx->uring_lock); |
24369c2e | 8131 | |
e941894e | 8132 | data.hash = hash; |
685fe7fe | 8133 | data.task = task; |
ebc11b6c | 8134 | data.free_work = io_wq_free_work; |
f5fa38c5 | 8135 | data.do_work = io_wq_submit_work; |
24369c2e | 8136 | |
d25e3a3d JA |
8137 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
8138 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 8139 | |
5aa75ed5 | 8140 | return io_wq_create(concurrency, &data); |
24369c2e PB |
8141 | } |
8142 | ||
5aa75ed5 JA |
8143 | static int io_uring_alloc_task_context(struct task_struct *task, |
8144 | struct io_ring_ctx *ctx) | |
0f212204 JA |
8145 | { |
8146 | struct io_uring_task *tctx; | |
d8a6df10 | 8147 | int ret; |
0f212204 | 8148 | |
09899b19 | 8149 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
0f212204 JA |
8150 | if (unlikely(!tctx)) |
8151 | return -ENOMEM; | |
8152 | ||
d8a6df10 JA |
8153 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
8154 | if (unlikely(ret)) { | |
8155 | kfree(tctx); | |
8156 | return ret; | |
8157 | } | |
8158 | ||
685fe7fe | 8159 | tctx->io_wq = io_init_wq_offload(ctx, task); |
5aa75ed5 JA |
8160 | if (IS_ERR(tctx->io_wq)) { |
8161 | ret = PTR_ERR(tctx->io_wq); | |
8162 | percpu_counter_destroy(&tctx->inflight); | |
8163 | kfree(tctx); | |
8164 | return ret; | |
8165 | } | |
8166 | ||
0f212204 JA |
8167 | xa_init(&tctx->xa); |
8168 | init_waitqueue_head(&tctx->wait); | |
fdaf083c | 8169 | atomic_set(&tctx->in_idle, 0); |
b303fe2e | 8170 | atomic_set(&tctx->inflight_tracked, 0); |
0f212204 | 8171 | task->io_uring = tctx; |
7cbf1722 JA |
8172 | spin_lock_init(&tctx->task_lock); |
8173 | INIT_WQ_LIST(&tctx->task_list); | |
7cbf1722 | 8174 | init_task_work(&tctx->task_work, tctx_task_work); |
0f212204 JA |
8175 | return 0; |
8176 | } | |
8177 | ||
8178 | void __io_uring_free(struct task_struct *tsk) | |
8179 | { | |
8180 | struct io_uring_task *tctx = tsk->io_uring; | |
8181 | ||
8182 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e | 8183 | WARN_ON_ONCE(tctx->io_wq); |
09899b19 | 8184 | WARN_ON_ONCE(tctx->cached_refs); |
ef8eaa4e | 8185 | |
d8a6df10 | 8186 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
8187 | kfree(tctx); |
8188 | tsk->io_uring = NULL; | |
8189 | } | |
8190 | ||
7e84e1c7 SG |
8191 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
8192 | struct io_uring_params *p) | |
2b188cc1 JA |
8193 | { |
8194 | int ret; | |
8195 | ||
d25e3a3d JA |
8196 | /* Retain compatibility with failing for an invalid attach attempt */ |
8197 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
8198 | IORING_SETUP_ATTACH_WQ) { | |
8199 | struct fd f; | |
8200 | ||
8201 | f = fdget(p->wq_fd); | |
8202 | if (!f.file) | |
8203 | return -ENXIO; | |
0cc936f7 JA |
8204 | if (f.file->f_op != &io_uring_fops) { |
8205 | fdput(f); | |
f2a48dd0 | 8206 | return -EINVAL; |
0cc936f7 JA |
8207 | } |
8208 | fdput(f); | |
d25e3a3d | 8209 | } |
6c271ce2 | 8210 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 8211 | struct task_struct *tsk; |
534ca6d6 | 8212 | struct io_sq_data *sqd; |
26984fbf | 8213 | bool attached; |
534ca6d6 | 8214 | |
26984fbf | 8215 | sqd = io_get_sq_data(p, &attached); |
534ca6d6 JA |
8216 | if (IS_ERR(sqd)) { |
8217 | ret = PTR_ERR(sqd); | |
8218 | goto err; | |
8219 | } | |
69fb2131 | 8220 | |
7c30f36a | 8221 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 8222 | ctx->sq_data = sqd; |
917257da JA |
8223 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
8224 | if (!ctx->sq_thread_idle) | |
8225 | ctx->sq_thread_idle = HZ; | |
8226 | ||
78d7f6ba | 8227 | io_sq_thread_park(sqd); |
de75a3d3 PB |
8228 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
8229 | io_sqd_update_thread_idle(sqd); | |
26984fbf | 8230 | /* don't attach to a dying SQPOLL thread, would be racy */ |
f2a48dd0 | 8231 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
78d7f6ba PB |
8232 | io_sq_thread_unpark(sqd); |
8233 | ||
de75a3d3 PB |
8234 | if (ret < 0) |
8235 | goto err; | |
8236 | if (attached) | |
5aa75ed5 | 8237 | return 0; |
aa06165d | 8238 | |
6c271ce2 | 8239 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 8240 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 8241 | |
917257da | 8242 | ret = -EINVAL; |
f2a48dd0 | 8243 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
e8f98f24 | 8244 | goto err_sqpoll; |
37d1e2e3 | 8245 | sqd->sq_cpu = cpu; |
6c271ce2 | 8246 | } else { |
37d1e2e3 | 8247 | sqd->sq_cpu = -1; |
6c271ce2 | 8248 | } |
37d1e2e3 JA |
8249 | |
8250 | sqd->task_pid = current->pid; | |
5c2469e0 | 8251 | sqd->task_tgid = current->tgid; |
46fe18b1 JA |
8252 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
8253 | if (IS_ERR(tsk)) { | |
8254 | ret = PTR_ERR(tsk); | |
e8f98f24 | 8255 | goto err_sqpoll; |
6c271ce2 | 8256 | } |
97a73a0f | 8257 | |
46fe18b1 | 8258 | sqd->thread = tsk; |
97a73a0f | 8259 | ret = io_uring_alloc_task_context(tsk, ctx); |
46fe18b1 | 8260 | wake_up_new_task(tsk); |
0f212204 JA |
8261 | if (ret) |
8262 | goto err; | |
6c271ce2 JA |
8263 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
8264 | /* Can't have SQ_AFF without SQPOLL */ | |
8265 | ret = -EINVAL; | |
8266 | goto err; | |
8267 | } | |
8268 | ||
2b188cc1 | 8269 | return 0; |
f2a48dd0 PB |
8270 | err_sqpoll: |
8271 | complete(&ctx->sq_data->exited); | |
2b188cc1 | 8272 | err: |
37d1e2e3 | 8273 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
8274 | return ret; |
8275 | } | |
8276 | ||
a087e2b5 BM |
8277 | static inline void __io_unaccount_mem(struct user_struct *user, |
8278 | unsigned long nr_pages) | |
2b188cc1 JA |
8279 | { |
8280 | atomic_long_sub(nr_pages, &user->locked_vm); | |
8281 | } | |
8282 | ||
a087e2b5 BM |
8283 | static inline int __io_account_mem(struct user_struct *user, |
8284 | unsigned long nr_pages) | |
2b188cc1 JA |
8285 | { |
8286 | unsigned long page_limit, cur_pages, new_pages; | |
8287 | ||
8288 | /* Don't allow more pages than we can safely lock */ | |
8289 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
8290 | ||
8291 | do { | |
8292 | cur_pages = atomic_long_read(&user->locked_vm); | |
8293 | new_pages = cur_pages + nr_pages; | |
8294 | if (new_pages > page_limit) | |
8295 | return -ENOMEM; | |
8296 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
8297 | new_pages) != cur_pages); | |
8298 | ||
8299 | return 0; | |
8300 | } | |
8301 | ||
26bfa89e | 8302 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8303 | { |
62e398be | 8304 | if (ctx->user) |
a087e2b5 | 8305 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 8306 | |
26bfa89e JA |
8307 | if (ctx->mm_account) |
8308 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8309 | } |
8310 | ||
26bfa89e | 8311 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8312 | { |
30975825 BM |
8313 | int ret; |
8314 | ||
62e398be | 8315 | if (ctx->user) { |
30975825 BM |
8316 | ret = __io_account_mem(ctx->user, nr_pages); |
8317 | if (ret) | |
8318 | return ret; | |
8319 | } | |
8320 | ||
26bfa89e JA |
8321 | if (ctx->mm_account) |
8322 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8323 | |
8324 | return 0; | |
8325 | } | |
8326 | ||
2b188cc1 JA |
8327 | static void io_mem_free(void *ptr) |
8328 | { | |
52e04ef4 MR |
8329 | struct page *page; |
8330 | ||
8331 | if (!ptr) | |
8332 | return; | |
2b188cc1 | 8333 | |
52e04ef4 | 8334 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8335 | if (put_page_testzero(page)) |
8336 | free_compound_page(page); | |
8337 | } | |
8338 | ||
8339 | static void *io_mem_alloc(size_t size) | |
8340 | { | |
8341 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
26bfa89e | 8342 | __GFP_NORETRY | __GFP_ACCOUNT; |
2b188cc1 JA |
8343 | |
8344 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8345 | } | |
8346 | ||
75b28aff HV |
8347 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8348 | size_t *sq_offset) | |
8349 | { | |
8350 | struct io_rings *rings; | |
8351 | size_t off, sq_array_size; | |
8352 | ||
8353 | off = struct_size(rings, cqes, cq_entries); | |
8354 | if (off == SIZE_MAX) | |
8355 | return SIZE_MAX; | |
8356 | ||
8357 | #ifdef CONFIG_SMP | |
8358 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8359 | if (off == 0) | |
8360 | return SIZE_MAX; | |
8361 | #endif | |
8362 | ||
b36200f5 DV |
8363 | if (sq_offset) |
8364 | *sq_offset = off; | |
8365 | ||
75b28aff HV |
8366 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8367 | if (sq_array_size == SIZE_MAX) | |
8368 | return SIZE_MAX; | |
8369 | ||
8370 | if (check_add_overflow(off, sq_array_size, &off)) | |
8371 | return SIZE_MAX; | |
8372 | ||
75b28aff HV |
8373 | return off; |
8374 | } | |
8375 | ||
41edf1a5 | 8376 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot) |
7f61a1e9 | 8377 | { |
41edf1a5 | 8378 | struct io_mapped_ubuf *imu = *slot; |
7f61a1e9 PB |
8379 | unsigned int i; |
8380 | ||
6224843d PB |
8381 | if (imu != ctx->dummy_ubuf) { |
8382 | for (i = 0; i < imu->nr_bvecs; i++) | |
8383 | unpin_user_page(imu->bvec[i].bv_page); | |
8384 | if (imu->acct_pages) | |
8385 | io_unaccount_mem(ctx, imu->acct_pages); | |
8386 | kvfree(imu); | |
8387 | } | |
41edf1a5 | 8388 | *slot = NULL; |
7f61a1e9 PB |
8389 | } |
8390 | ||
bd54b6fe | 8391 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
edafccee | 8392 | { |
634d00df PB |
8393 | io_buffer_unmap(ctx, &prsrc->buf); |
8394 | prsrc->buf = NULL; | |
bd54b6fe | 8395 | } |
edafccee | 8396 | |
bd54b6fe BM |
8397 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
8398 | { | |
8399 | unsigned int i; | |
edafccee | 8400 | |
7f61a1e9 PB |
8401 | for (i = 0; i < ctx->nr_user_bufs; i++) |
8402 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); | |
edafccee | 8403 | kfree(ctx->user_bufs); |
bb6659cc | 8404 | io_rsrc_data_free(ctx->buf_data); |
edafccee | 8405 | ctx->user_bufs = NULL; |
bd54b6fe | 8406 | ctx->buf_data = NULL; |
edafccee | 8407 | ctx->nr_user_bufs = 0; |
bd54b6fe BM |
8408 | } |
8409 | ||
0a96bbe4 | 8410 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee | 8411 | { |
bd54b6fe | 8412 | int ret; |
edafccee | 8413 | |
bd54b6fe | 8414 | if (!ctx->buf_data) |
edafccee JA |
8415 | return -ENXIO; |
8416 | ||
bd54b6fe BM |
8417 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
8418 | if (!ret) | |
8419 | __io_sqe_buffers_unregister(ctx); | |
8420 | return ret; | |
edafccee JA |
8421 | } |
8422 | ||
8423 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8424 | void __user *arg, unsigned index) | |
8425 | { | |
8426 | struct iovec __user *src; | |
8427 | ||
8428 | #ifdef CONFIG_COMPAT | |
8429 | if (ctx->compat) { | |
8430 | struct compat_iovec __user *ciovs; | |
8431 | struct compat_iovec ciov; | |
8432 | ||
8433 | ciovs = (struct compat_iovec __user *) arg; | |
8434 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8435 | return -EFAULT; | |
8436 | ||
d55e5f5b | 8437 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8438 | dst->iov_len = ciov.iov_len; |
8439 | return 0; | |
8440 | } | |
8441 | #endif | |
8442 | src = (struct iovec __user *) arg; | |
8443 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8444 | return -EFAULT; | |
8445 | return 0; | |
8446 | } | |
8447 | ||
de293938 JA |
8448 | /* |
8449 | * Not super efficient, but this is just a registration time. And we do cache | |
8450 | * the last compound head, so generally we'll only do a full search if we don't | |
8451 | * match that one. | |
8452 | * | |
8453 | * We check if the given compound head page has already been accounted, to | |
8454 | * avoid double accounting it. This allows us to account the full size of the | |
8455 | * page, not just the constituent pages of a huge page. | |
8456 | */ | |
8457 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8458 | int nr_pages, struct page *hpage) | |
8459 | { | |
8460 | int i, j; | |
8461 | ||
8462 | /* check current page array */ | |
8463 | for (i = 0; i < nr_pages; i++) { | |
8464 | if (!PageCompound(pages[i])) | |
8465 | continue; | |
8466 | if (compound_head(pages[i]) == hpage) | |
8467 | return true; | |
8468 | } | |
8469 | ||
8470 | /* check previously registered pages */ | |
8471 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
41edf1a5 | 8472 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
de293938 JA |
8473 | |
8474 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8475 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8476 | continue; | |
8477 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8478 | return true; | |
8479 | } | |
8480 | } | |
8481 | ||
8482 | return false; | |
8483 | } | |
8484 | ||
8485 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8486 | int nr_pages, struct io_mapped_ubuf *imu, | |
8487 | struct page **last_hpage) | |
8488 | { | |
8489 | int i, ret; | |
8490 | ||
216e5835 | 8491 | imu->acct_pages = 0; |
de293938 JA |
8492 | for (i = 0; i < nr_pages; i++) { |
8493 | if (!PageCompound(pages[i])) { | |
8494 | imu->acct_pages++; | |
8495 | } else { | |
8496 | struct page *hpage; | |
8497 | ||
8498 | hpage = compound_head(pages[i]); | |
8499 | if (hpage == *last_hpage) | |
8500 | continue; | |
8501 | *last_hpage = hpage; | |
8502 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8503 | continue; | |
8504 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8505 | } | |
8506 | } | |
8507 | ||
8508 | if (!imu->acct_pages) | |
8509 | return 0; | |
8510 | ||
26bfa89e | 8511 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
8512 | if (ret) |
8513 | imu->acct_pages = 0; | |
8514 | return ret; | |
8515 | } | |
8516 | ||
0a96bbe4 | 8517 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
41edf1a5 | 8518 | struct io_mapped_ubuf **pimu, |
0a96bbe4 | 8519 | struct page **last_hpage) |
edafccee | 8520 | { |
41edf1a5 | 8521 | struct io_mapped_ubuf *imu = NULL; |
edafccee JA |
8522 | struct vm_area_struct **vmas = NULL; |
8523 | struct page **pages = NULL; | |
0a96bbe4 BM |
8524 | unsigned long off, start, end, ubuf; |
8525 | size_t size; | |
8526 | int ret, pret, nr_pages, i; | |
8527 | ||
6224843d PB |
8528 | if (!iov->iov_base) { |
8529 | *pimu = ctx->dummy_ubuf; | |
8530 | return 0; | |
8531 | } | |
8532 | ||
0a96bbe4 BM |
8533 | ubuf = (unsigned long) iov->iov_base; |
8534 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8535 | start = ubuf >> PAGE_SHIFT; | |
8536 | nr_pages = end - start; | |
8537 | ||
41edf1a5 | 8538 | *pimu = NULL; |
0a96bbe4 BM |
8539 | ret = -ENOMEM; |
8540 | ||
8541 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8542 | if (!pages) | |
8543 | goto done; | |
8544 | ||
8545 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8546 | GFP_KERNEL); | |
8547 | if (!vmas) | |
8548 | goto done; | |
edafccee | 8549 | |
41edf1a5 | 8550 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
a2b4198c | 8551 | if (!imu) |
0a96bbe4 BM |
8552 | goto done; |
8553 | ||
8554 | ret = 0; | |
8555 | mmap_read_lock(current->mm); | |
8556 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8557 | pages, vmas); | |
8558 | if (pret == nr_pages) { | |
8559 | /* don't support file backed memory */ | |
8560 | for (i = 0; i < nr_pages; i++) { | |
8561 | struct vm_area_struct *vma = vmas[i]; | |
8562 | ||
40dad765 PB |
8563 | if (vma_is_shmem(vma)) |
8564 | continue; | |
0a96bbe4 BM |
8565 | if (vma->vm_file && |
8566 | !is_file_hugepages(vma->vm_file)) { | |
8567 | ret = -EOPNOTSUPP; | |
8568 | break; | |
8569 | } | |
8570 | } | |
8571 | } else { | |
8572 | ret = pret < 0 ? pret : -EFAULT; | |
8573 | } | |
8574 | mmap_read_unlock(current->mm); | |
8575 | if (ret) { | |
8576 | /* | |
8577 | * if we did partial map, or found file backed vmas, | |
8578 | * release any pages we did get | |
8579 | */ | |
8580 | if (pret > 0) | |
8581 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
8582 | goto done; |
8583 | } | |
8584 | ||
8585 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
8586 | if (ret) { | |
8587 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
8588 | goto done; |
8589 | } | |
8590 | ||
8591 | off = ubuf & ~PAGE_MASK; | |
8592 | size = iov->iov_len; | |
8593 | for (i = 0; i < nr_pages; i++) { | |
8594 | size_t vec_len; | |
8595 | ||
8596 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
8597 | imu->bvec[i].bv_page = pages[i]; | |
8598 | imu->bvec[i].bv_len = vec_len; | |
8599 | imu->bvec[i].bv_offset = off; | |
8600 | off = 0; | |
8601 | size -= vec_len; | |
8602 | } | |
8603 | /* store original address for later verification */ | |
8604 | imu->ubuf = ubuf; | |
4751f53d | 8605 | imu->ubuf_end = ubuf + iov->iov_len; |
0a96bbe4 | 8606 | imu->nr_bvecs = nr_pages; |
41edf1a5 | 8607 | *pimu = imu; |
0a96bbe4 BM |
8608 | ret = 0; |
8609 | done: | |
41edf1a5 PB |
8610 | if (ret) |
8611 | kvfree(imu); | |
0a96bbe4 BM |
8612 | kvfree(pages); |
8613 | kvfree(vmas); | |
8614 | return ret; | |
8615 | } | |
8616 | ||
2b358604 | 8617 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 8618 | { |
87094465 PB |
8619 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
8620 | return ctx->user_bufs ? 0 : -ENOMEM; | |
2b358604 | 8621 | } |
edafccee | 8622 | |
2b358604 BM |
8623 | static int io_buffer_validate(struct iovec *iov) |
8624 | { | |
50e96989 PB |
8625 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
8626 | ||
2b358604 BM |
8627 | /* |
8628 | * Don't impose further limits on the size and buffer | |
8629 | * constraints here, we'll -EINVAL later when IO is | |
8630 | * submitted if they are wrong. | |
8631 | */ | |
6224843d PB |
8632 | if (!iov->iov_base) |
8633 | return iov->iov_len ? -EFAULT : 0; | |
8634 | if (!iov->iov_len) | |
2b358604 | 8635 | return -EFAULT; |
edafccee | 8636 | |
2b358604 BM |
8637 | /* arbitrary limit, but we need something */ |
8638 | if (iov->iov_len > SZ_1G) | |
8639 | return -EFAULT; | |
edafccee | 8640 | |
50e96989 PB |
8641 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
8642 | return -EOVERFLOW; | |
8643 | ||
2b358604 BM |
8644 | return 0; |
8645 | } | |
edafccee | 8646 | |
2b358604 | 8647 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
634d00df | 8648 | unsigned int nr_args, u64 __user *tags) |
2b358604 | 8649 | { |
bd54b6fe BM |
8650 | struct page *last_hpage = NULL; |
8651 | struct io_rsrc_data *data; | |
2b358604 BM |
8652 | int i, ret; |
8653 | struct iovec iov; | |
edafccee | 8654 | |
87094465 PB |
8655 | if (ctx->user_bufs) |
8656 | return -EBUSY; | |
489809e2 | 8657 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
87094465 | 8658 | return -EINVAL; |
bd54b6fe | 8659 | ret = io_rsrc_node_switch_start(ctx); |
2b358604 BM |
8660 | if (ret) |
8661 | return ret; | |
d878c816 PB |
8662 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
8663 | if (ret) | |
8664 | return ret; | |
bd54b6fe BM |
8665 | ret = io_buffers_map_alloc(ctx, nr_args); |
8666 | if (ret) { | |
bb6659cc | 8667 | io_rsrc_data_free(data); |
bd54b6fe BM |
8668 | return ret; |
8669 | } | |
edafccee | 8670 | |
87094465 | 8671 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
edafccee JA |
8672 | ret = io_copy_iov(ctx, &iov, arg, i); |
8673 | if (ret) | |
0a96bbe4 | 8674 | break; |
2b358604 BM |
8675 | ret = io_buffer_validate(&iov); |
8676 | if (ret) | |
0a96bbe4 | 8677 | break; |
2d091d62 | 8678 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
cf3770e7 CIK |
8679 | ret = -EINVAL; |
8680 | break; | |
8681 | } | |
edafccee | 8682 | |
41edf1a5 PB |
8683 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
8684 | &last_hpage); | |
0a96bbe4 BM |
8685 | if (ret) |
8686 | break; | |
edafccee | 8687 | } |
0a96bbe4 | 8688 | |
bd54b6fe | 8689 | WARN_ON_ONCE(ctx->buf_data); |
0a96bbe4 | 8690 | |
bd54b6fe BM |
8691 | ctx->buf_data = data; |
8692 | if (ret) | |
8693 | __io_sqe_buffers_unregister(ctx); | |
8694 | else | |
8695 | io_rsrc_node_switch(ctx, NULL); | |
edafccee JA |
8696 | return ret; |
8697 | } | |
8698 | ||
634d00df PB |
8699 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
8700 | struct io_uring_rsrc_update2 *up, | |
8701 | unsigned int nr_args) | |
8702 | { | |
8703 | u64 __user *tags = u64_to_user_ptr(up->tags); | |
8704 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); | |
634d00df PB |
8705 | struct page *last_hpage = NULL; |
8706 | bool needs_switch = false; | |
8707 | __u32 done; | |
8708 | int i, err; | |
8709 | ||
8710 | if (!ctx->buf_data) | |
8711 | return -ENXIO; | |
8712 | if (up->offset + nr_args > ctx->nr_user_bufs) | |
8713 | return -EINVAL; | |
8714 | ||
8715 | for (done = 0; done < nr_args; done++) { | |
0b8c0e7c PB |
8716 | struct io_mapped_ubuf *imu; |
8717 | int offset = up->offset + done; | |
634d00df PB |
8718 | u64 tag = 0; |
8719 | ||
8720 | err = io_copy_iov(ctx, &iov, iovs, done); | |
8721 | if (err) | |
8722 | break; | |
8723 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { | |
8724 | err = -EFAULT; | |
8725 | break; | |
8726 | } | |
0b8c0e7c PB |
8727 | err = io_buffer_validate(&iov); |
8728 | if (err) | |
8729 | break; | |
cf3770e7 CIK |
8730 | if (!iov.iov_base && tag) { |
8731 | err = -EINVAL; | |
8732 | break; | |
8733 | } | |
0b8c0e7c PB |
8734 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
8735 | if (err) | |
8736 | break; | |
634d00df | 8737 | |
0b8c0e7c | 8738 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
6224843d | 8739 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
0b8c0e7c PB |
8740 | err = io_queue_rsrc_removal(ctx->buf_data, offset, |
8741 | ctx->rsrc_node, ctx->user_bufs[i]); | |
8742 | if (unlikely(err)) { | |
8743 | io_buffer_unmap(ctx, &imu); | |
634d00df | 8744 | break; |
0b8c0e7c | 8745 | } |
634d00df PB |
8746 | ctx->user_bufs[i] = NULL; |
8747 | needs_switch = true; | |
8748 | } | |
8749 | ||
0b8c0e7c | 8750 | ctx->user_bufs[i] = imu; |
2d091d62 | 8751 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
634d00df PB |
8752 | } |
8753 | ||
8754 | if (needs_switch) | |
8755 | io_rsrc_node_switch(ctx, ctx->buf_data); | |
8756 | return done ? done : err; | |
8757 | } | |
8758 | ||
9b402849 JA |
8759 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
8760 | { | |
8761 | __s32 __user *fds = arg; | |
8762 | int fd; | |
8763 | ||
8764 | if (ctx->cq_ev_fd) | |
8765 | return -EBUSY; | |
8766 | ||
8767 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
8768 | return -EFAULT; | |
8769 | ||
8770 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
8771 | if (IS_ERR(ctx->cq_ev_fd)) { | |
8772 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
fe7e3257 | 8773 | |
9b402849 JA |
8774 | ctx->cq_ev_fd = NULL; |
8775 | return ret; | |
8776 | } | |
8777 | ||
8778 | return 0; | |
8779 | } | |
8780 | ||
8781 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
8782 | { | |
8783 | if (ctx->cq_ev_fd) { | |
8784 | eventfd_ctx_put(ctx->cq_ev_fd); | |
8785 | ctx->cq_ev_fd = NULL; | |
8786 | return 0; | |
8787 | } | |
8788 | ||
8789 | return -ENXIO; | |
8790 | } | |
8791 | ||
5a2e745d JA |
8792 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
8793 | { | |
9e15c3a0 JA |
8794 | struct io_buffer *buf; |
8795 | unsigned long index; | |
8796 | ||
8797 | xa_for_each(&ctx->io_buffers, index, buf) | |
8798 | __io_remove_buffers(ctx, buf, index, -1U); | |
5a2e745d JA |
8799 | } |
8800 | ||
7255834e | 8801 | static void io_req_cache_free(struct list_head *list) |
1b4c351f | 8802 | { |
68e68ee6 | 8803 | struct io_kiocb *req, *nxt; |
1b4c351f | 8804 | |
bb943b82 PB |
8805 | list_for_each_entry_safe(req, nxt, list, inflight_entry) { |
8806 | list_del(&req->inflight_entry); | |
1b4c351f JA |
8807 | kmem_cache_free(req_cachep, req); |
8808 | } | |
8809 | } | |
8810 | ||
4010fec4 | 8811 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 8812 | { |
cd0ca2e0 | 8813 | struct io_submit_state *state = &ctx->submit_state; |
bf019da7 | 8814 | |
9a4fdbd8 JA |
8815 | mutex_lock(&ctx->uring_lock); |
8816 | ||
cd0ca2e0 PB |
8817 | if (state->free_reqs) { |
8818 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); | |
8819 | state->free_reqs = 0; | |
8e5c66c4 | 8820 | } |
9a4fdbd8 | 8821 | |
cd0ca2e0 PB |
8822 | io_flush_cached_locked_reqs(ctx, state); |
8823 | io_req_cache_free(&state->free_list); | |
9a4fdbd8 JA |
8824 | mutex_unlock(&ctx->uring_lock); |
8825 | } | |
8826 | ||
43597aac | 8827 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
2b188cc1 | 8828 | { |
43597aac | 8829 | if (data && !atomic_dec_and_test(&data->refs)) |
bd54b6fe | 8830 | wait_for_completion(&data->done); |
bd54b6fe | 8831 | } |
04fc6c80 | 8832 | |
2b188cc1 JA |
8833 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
8834 | { | |
37d1e2e3 | 8835 | io_sq_thread_finish(ctx); |
2aede0e4 | 8836 | |
37d1e2e3 | 8837 | if (ctx->mm_account) { |
2aede0e4 JA |
8838 | mmdrop(ctx->mm_account); |
8839 | ctx->mm_account = NULL; | |
30975825 | 8840 | } |
def596e9 | 8841 | |
43597aac PB |
8842 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
8843 | io_wait_rsrc_data(ctx->buf_data); | |
8844 | io_wait_rsrc_data(ctx->file_data); | |
8845 | ||
8bad28d8 | 8846 | mutex_lock(&ctx->uring_lock); |
43597aac | 8847 | if (ctx->buf_data) |
bd54b6fe | 8848 | __io_sqe_buffers_unregister(ctx); |
43597aac | 8849 | if (ctx->file_data) |
08480400 | 8850 | __io_sqe_files_unregister(ctx); |
c4ea060e PB |
8851 | if (ctx->rings) |
8852 | __io_cqring_overflow_flush(ctx, true); | |
8bad28d8 | 8853 | mutex_unlock(&ctx->uring_lock); |
9b402849 | 8854 | io_eventfd_unregister(ctx); |
5a2e745d | 8855 | io_destroy_buffers(ctx); |
07db298a PB |
8856 | if (ctx->sq_creds) |
8857 | put_cred(ctx->sq_creds); | |
def596e9 | 8858 | |
a7f0ed5a PB |
8859 | /* there are no registered resources left, nobody uses it */ |
8860 | if (ctx->rsrc_node) | |
8861 | io_rsrc_node_destroy(ctx->rsrc_node); | |
8dd03afe | 8862 | if (ctx->rsrc_backup_node) |
b895c9a6 | 8863 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
a7f0ed5a PB |
8864 | flush_delayed_work(&ctx->rsrc_put_work); |
8865 | ||
8866 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); | |
8867 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); | |
def596e9 | 8868 | |
2b188cc1 | 8869 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
8870 | if (ctx->ring_sock) { |
8871 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 8872 | sock_release(ctx->ring_sock); |
355e8d26 | 8873 | } |
2b188cc1 JA |
8874 | #endif |
8875 | ||
75b28aff | 8876 | io_mem_free(ctx->rings); |
2b188cc1 | 8877 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
8878 | |
8879 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 8880 | free_uid(ctx->user); |
4010fec4 | 8881 | io_req_caches_free(ctx); |
e941894e JA |
8882 | if (ctx->hash_map) |
8883 | io_wq_put_hash(ctx->hash_map); | |
78076bb6 | 8884 | kfree(ctx->cancel_hash); |
6224843d | 8885 | kfree(ctx->dummy_ubuf); |
2b188cc1 JA |
8886 | kfree(ctx); |
8887 | } | |
8888 | ||
8889 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
8890 | { | |
8891 | struct io_ring_ctx *ctx = file->private_data; | |
8892 | __poll_t mask = 0; | |
8893 | ||
311997b3 | 8894 | poll_wait(file, &ctx->poll_wait, wait); |
4f7067c3 SB |
8895 | /* |
8896 | * synchronizes with barrier from wq_has_sleeper call in | |
8897 | * io_commit_cqring | |
8898 | */ | |
2b188cc1 | 8899 | smp_rmb(); |
90554200 | 8900 | if (!io_sqring_full(ctx)) |
2b188cc1 | 8901 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
8902 | |
8903 | /* | |
8904 | * Don't flush cqring overflow list here, just do a simple check. | |
8905 | * Otherwise there could possible be ABBA deadlock: | |
8906 | * CPU0 CPU1 | |
8907 | * ---- ---- | |
8908 | * lock(&ctx->uring_lock); | |
8909 | * lock(&ep->mtx); | |
8910 | * lock(&ctx->uring_lock); | |
8911 | * lock(&ep->mtx); | |
8912 | * | |
8913 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
8914 | * pushs them to do the flush. | |
8915 | */ | |
5ed7a37d | 8916 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
2b188cc1 JA |
8917 | mask |= EPOLLIN | EPOLLRDNORM; |
8918 | ||
8919 | return mask; | |
8920 | } | |
8921 | ||
8922 | static int io_uring_fasync(int fd, struct file *file, int on) | |
8923 | { | |
8924 | struct io_ring_ctx *ctx = file->private_data; | |
8925 | ||
8926 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
8927 | } | |
8928 | ||
0bead8cd | 8929 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 8930 | { |
4379bf8b | 8931 | const struct cred *creds; |
071698e1 | 8932 | |
61cf9370 | 8933 | creds = xa_erase(&ctx->personalities, id); |
4379bf8b JA |
8934 | if (creds) { |
8935 | put_cred(creds); | |
0bead8cd | 8936 | return 0; |
1e6fa521 | 8937 | } |
0bead8cd YD |
8938 | |
8939 | return -EINVAL; | |
8940 | } | |
8941 | ||
d56d938b PB |
8942 | struct io_tctx_exit { |
8943 | struct callback_head task_work; | |
8944 | struct completion completion; | |
baf186c4 | 8945 | struct io_ring_ctx *ctx; |
d56d938b PB |
8946 | }; |
8947 | ||
8948 | static void io_tctx_exit_cb(struct callback_head *cb) | |
8949 | { | |
8950 | struct io_uring_task *tctx = current->io_uring; | |
8951 | struct io_tctx_exit *work; | |
8952 | ||
8953 | work = container_of(cb, struct io_tctx_exit, task_work); | |
8954 | /* | |
8955 | * When @in_idle, we're in cancellation and it's racy to remove the | |
8956 | * node. It'll be removed by the end of cancellation, just ignore it. | |
8957 | */ | |
8958 | if (!atomic_read(&tctx->in_idle)) | |
eef51daa | 8959 | io_uring_del_tctx_node((unsigned long)work->ctx); |
d56d938b PB |
8960 | complete(&work->completion); |
8961 | } | |
8962 | ||
28090c13 PB |
8963 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
8964 | { | |
8965 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
8966 | ||
8967 | return req->ctx == data; | |
8968 | } | |
8969 | ||
85faa7b8 JA |
8970 | static void io_ring_exit_work(struct work_struct *work) |
8971 | { | |
d56d938b | 8972 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 8973 | unsigned long timeout = jiffies + HZ * 60 * 5; |
58d3be2c | 8974 | unsigned long interval = HZ / 20; |
d56d938b PB |
8975 | struct io_tctx_exit exit; |
8976 | struct io_tctx_node *node; | |
8977 | int ret; | |
85faa7b8 | 8978 | |
56952e91 JA |
8979 | /* |
8980 | * If we're doing polled IO and end up having requests being | |
8981 | * submitted async (out-of-line), then completions can come in while | |
8982 | * we're waiting for refs to drop. We need to reap these manually, | |
8983 | * as nobody else will be looking for them. | |
8984 | */ | |
b2edc0a7 | 8985 | do { |
3dd0c97a | 8986 | io_uring_try_cancel_requests(ctx, NULL, true); |
28090c13 PB |
8987 | if (ctx->sq_data) { |
8988 | struct io_sq_data *sqd = ctx->sq_data; | |
8989 | struct task_struct *tsk; | |
8990 | ||
8991 | io_sq_thread_park(sqd); | |
8992 | tsk = sqd->thread; | |
8993 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) | |
8994 | io_wq_cancel_cb(tsk->io_uring->io_wq, | |
8995 | io_cancel_ctx_cb, ctx, true); | |
8996 | io_sq_thread_unpark(sqd); | |
8997 | } | |
b5bb3a24 | 8998 | |
58d3be2c PB |
8999 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
9000 | /* there is little hope left, don't run it too often */ | |
9001 | interval = HZ * 60; | |
9002 | } | |
9003 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); | |
d56d938b | 9004 | |
7f00651a PB |
9005 | init_completion(&exit.completion); |
9006 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
9007 | exit.ctx = ctx; | |
89b5066e PB |
9008 | /* |
9009 | * Some may use context even when all refs and requests have been put, | |
9010 | * and they are free to do so while still holding uring_lock or | |
5b0a6acc | 9011 | * completion_lock, see io_req_task_submit(). Apart from other work, |
89b5066e PB |
9012 | * this lock/unlock section also waits them to finish. |
9013 | */ | |
d56d938b PB |
9014 | mutex_lock(&ctx->uring_lock); |
9015 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
9016 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
9017 | ||
d56d938b PB |
9018 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
9019 | ctx_node); | |
7f00651a PB |
9020 | /* don't spin on a single task if cancellation failed */ |
9021 | list_rotate_left(&ctx->tctx_list); | |
d56d938b PB |
9022 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
9023 | if (WARN_ON_ONCE(ret)) | |
9024 | continue; | |
9025 | wake_up_process(node->task); | |
9026 | ||
9027 | mutex_unlock(&ctx->uring_lock); | |
9028 | wait_for_completion(&exit.completion); | |
d56d938b PB |
9029 | mutex_lock(&ctx->uring_lock); |
9030 | } | |
9031 | mutex_unlock(&ctx->uring_lock); | |
79ebeaee JA |
9032 | spin_lock(&ctx->completion_lock); |
9033 | spin_unlock(&ctx->completion_lock); | |
d56d938b | 9034 | |
85faa7b8 JA |
9035 | io_ring_ctx_free(ctx); |
9036 | } | |
9037 | ||
80c4cbdb PB |
9038 | /* Returns true if we found and killed one or more timeouts */ |
9039 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, | |
3dd0c97a | 9040 | bool cancel_all) |
80c4cbdb PB |
9041 | { |
9042 | struct io_kiocb *req, *tmp; | |
9043 | int canceled = 0; | |
9044 | ||
79ebeaee JA |
9045 | spin_lock(&ctx->completion_lock); |
9046 | spin_lock_irq(&ctx->timeout_lock); | |
80c4cbdb | 9047 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
3dd0c97a | 9048 | if (io_match_task(req, tsk, cancel_all)) { |
80c4cbdb PB |
9049 | io_kill_timeout(req, -ECANCELED); |
9050 | canceled++; | |
9051 | } | |
9052 | } | |
79ebeaee | 9053 | spin_unlock_irq(&ctx->timeout_lock); |
51520426 PB |
9054 | if (canceled != 0) |
9055 | io_commit_cqring(ctx); | |
79ebeaee | 9056 | spin_unlock(&ctx->completion_lock); |
80c4cbdb PB |
9057 | if (canceled != 0) |
9058 | io_cqring_ev_posted(ctx); | |
9059 | return canceled != 0; | |
9060 | } | |
9061 | ||
2b188cc1 JA |
9062 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
9063 | { | |
61cf9370 MWO |
9064 | unsigned long index; |
9065 | struct creds *creds; | |
9066 | ||
2b188cc1 JA |
9067 | mutex_lock(&ctx->uring_lock); |
9068 | percpu_ref_kill(&ctx->refs); | |
634578f8 | 9069 | if (ctx->rings) |
6c2450ae | 9070 | __io_cqring_overflow_flush(ctx, true); |
61cf9370 MWO |
9071 | xa_for_each(&ctx->personalities, index, creds) |
9072 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
9073 | mutex_unlock(&ctx->uring_lock); |
9074 | ||
3dd0c97a PB |
9075 | io_kill_timeouts(ctx, NULL, true); |
9076 | io_poll_remove_all(ctx, NULL, true); | |
561fb04a | 9077 | |
15dff286 | 9078 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 9079 | io_iopoll_try_reap_events(ctx); |
309fc03a | 9080 | |
85faa7b8 | 9081 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
9082 | /* |
9083 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
9084 | * if we're exiting a ton of rings at the same time. It just adds | |
9085 | * noise and overhead, there's no discernable change in runtime | |
9086 | * over using system_wq. | |
9087 | */ | |
9088 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
9089 | } |
9090 | ||
9091 | static int io_uring_release(struct inode *inode, struct file *file) | |
9092 | { | |
9093 | struct io_ring_ctx *ctx = file->private_data; | |
9094 | ||
9095 | file->private_data = NULL; | |
9096 | io_ring_ctx_wait_and_kill(ctx); | |
9097 | return 0; | |
9098 | } | |
9099 | ||
f6edbabb PB |
9100 | struct io_task_cancel { |
9101 | struct task_struct *task; | |
3dd0c97a | 9102 | bool all; |
f6edbabb | 9103 | }; |
f254ac04 | 9104 | |
f6edbabb | 9105 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 9106 | { |
9a472ef7 | 9107 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 9108 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
9109 | bool ret; |
9110 | ||
3dd0c97a | 9111 | if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
9112 | struct io_ring_ctx *ctx = req->ctx; |
9113 | ||
9114 | /* protect against races with linked timeouts */ | |
79ebeaee | 9115 | spin_lock(&ctx->completion_lock); |
3dd0c97a | 9116 | ret = io_match_task(req, cancel->task, cancel->all); |
79ebeaee | 9117 | spin_unlock(&ctx->completion_lock); |
9a472ef7 | 9118 | } else { |
3dd0c97a | 9119 | ret = io_match_task(req, cancel->task, cancel->all); |
9a472ef7 PB |
9120 | } |
9121 | return ret; | |
b711d4ea JA |
9122 | } |
9123 | ||
e1915f76 | 9124 | static bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
3dd0c97a | 9125 | struct task_struct *task, bool cancel_all) |
b7ddce3c | 9126 | { |
e1915f76 | 9127 | struct io_defer_entry *de; |
b7ddce3c PB |
9128 | LIST_HEAD(list); |
9129 | ||
79ebeaee | 9130 | spin_lock(&ctx->completion_lock); |
b7ddce3c | 9131 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
3dd0c97a | 9132 | if (io_match_task(de->req, task, cancel_all)) { |
b7ddce3c PB |
9133 | list_cut_position(&list, &ctx->defer_list, &de->list); |
9134 | break; | |
9135 | } | |
9136 | } | |
79ebeaee | 9137 | spin_unlock(&ctx->completion_lock); |
e1915f76 PB |
9138 | if (list_empty(&list)) |
9139 | return false; | |
b7ddce3c PB |
9140 | |
9141 | while (!list_empty(&list)) { | |
9142 | de = list_first_entry(&list, struct io_defer_entry, list); | |
9143 | list_del_init(&de->list); | |
f41db273 | 9144 | io_req_complete_failed(de->req, -ECANCELED); |
b7ddce3c PB |
9145 | kfree(de); |
9146 | } | |
e1915f76 | 9147 | return true; |
b7ddce3c PB |
9148 | } |
9149 | ||
1b00764f PB |
9150 | static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
9151 | { | |
9152 | struct io_tctx_node *node; | |
9153 | enum io_wq_cancel cret; | |
9154 | bool ret = false; | |
9155 | ||
9156 | mutex_lock(&ctx->uring_lock); | |
9157 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
9158 | struct io_uring_task *tctx = node->task->io_uring; | |
9159 | ||
9160 | /* | |
9161 | * io_wq will stay alive while we hold uring_lock, because it's | |
9162 | * killed after ctx nodes, which requires to take the lock. | |
9163 | */ | |
9164 | if (!tctx || !tctx->io_wq) | |
9165 | continue; | |
9166 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
9167 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9168 | } | |
9169 | mutex_unlock(&ctx->uring_lock); | |
9170 | ||
9171 | return ret; | |
9172 | } | |
9173 | ||
9936c7c2 PB |
9174 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
9175 | struct task_struct *task, | |
3dd0c97a | 9176 | bool cancel_all) |
9936c7c2 | 9177 | { |
3dd0c97a | 9178 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
1b00764f | 9179 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 PB |
9180 | |
9181 | while (1) { | |
9182 | enum io_wq_cancel cret; | |
9183 | bool ret = false; | |
9184 | ||
1b00764f PB |
9185 | if (!task) { |
9186 | ret |= io_uring_try_cancel_iowq(ctx); | |
9187 | } else if (tctx && tctx->io_wq) { | |
9188 | /* | |
9189 | * Cancels requests of all rings, not only @ctx, but | |
9190 | * it's fine as the task is in exit/exec. | |
9191 | */ | |
5aa75ed5 | 9192 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
9193 | &cancel, true); |
9194 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9195 | } | |
9196 | ||
9197 | /* SQPOLL thread does its own polling */ | |
3dd0c97a | 9198 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
d052d1d6 | 9199 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
9936c7c2 PB |
9200 | while (!list_empty_careful(&ctx->iopoll_list)) { |
9201 | io_iopoll_try_reap_events(ctx); | |
9202 | ret = true; | |
9203 | } | |
9204 | } | |
9205 | ||
3dd0c97a PB |
9206 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
9207 | ret |= io_poll_remove_all(ctx, task, cancel_all); | |
9208 | ret |= io_kill_timeouts(ctx, task, cancel_all); | |
e5dc480d PB |
9209 | if (task) |
9210 | ret |= io_run_task_work(); | |
9936c7c2 PB |
9211 | if (!ret) |
9212 | break; | |
9213 | cond_resched(); | |
9214 | } | |
9215 | } | |
9216 | ||
eef51daa | 9217 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
0f212204 | 9218 | { |
236434c3 | 9219 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 9220 | struct io_tctx_node *node; |
a528b04e | 9221 | int ret; |
236434c3 MWO |
9222 | |
9223 | if (unlikely(!tctx)) { | |
5aa75ed5 | 9224 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
9225 | if (unlikely(ret)) |
9226 | return ret; | |
236434c3 | 9227 | tctx = current->io_uring; |
0f212204 | 9228 | } |
cf27f3b1 PB |
9229 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
9230 | node = kmalloc(sizeof(*node), GFP_KERNEL); | |
9231 | if (!node) | |
9232 | return -ENOMEM; | |
9233 | node->ctx = ctx; | |
9234 | node->task = current; | |
13bf43f5 | 9235 | |
cf27f3b1 PB |
9236 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
9237 | node, GFP_KERNEL)); | |
9238 | if (ret) { | |
9239 | kfree(node); | |
9240 | return ret; | |
0f212204 | 9241 | } |
cf27f3b1 PB |
9242 | |
9243 | mutex_lock(&ctx->uring_lock); | |
9244 | list_add(&node->ctx_node, &ctx->tctx_list); | |
9245 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 9246 | } |
cf27f3b1 | 9247 | tctx->last = ctx; |
0f212204 JA |
9248 | return 0; |
9249 | } | |
9250 | ||
cf27f3b1 PB |
9251 | /* |
9252 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
9253 | */ | |
eef51daa | 9254 | static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
cf27f3b1 PB |
9255 | { |
9256 | struct io_uring_task *tctx = current->io_uring; | |
9257 | ||
9258 | if (likely(tctx && tctx->last == ctx)) | |
9259 | return 0; | |
eef51daa | 9260 | return __io_uring_add_tctx_node(ctx); |
cf27f3b1 PB |
9261 | } |
9262 | ||
0f212204 JA |
9263 | /* |
9264 | * Remove this io_uring_file -> task mapping. | |
9265 | */ | |
eef51daa | 9266 | static void io_uring_del_tctx_node(unsigned long index) |
0f212204 JA |
9267 | { |
9268 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 9269 | struct io_tctx_node *node; |
2941267b | 9270 | |
eebd2e37 PB |
9271 | if (!tctx) |
9272 | return; | |
13bf43f5 PB |
9273 | node = xa_erase(&tctx->xa, index); |
9274 | if (!node) | |
2941267b | 9275 | return; |
0f212204 | 9276 | |
13bf43f5 PB |
9277 | WARN_ON_ONCE(current != node->task); |
9278 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
9279 | ||
9280 | mutex_lock(&node->ctx->uring_lock); | |
9281 | list_del(&node->ctx_node); | |
9282 | mutex_unlock(&node->ctx->uring_lock); | |
9283 | ||
baf186c4 | 9284 | if (tctx->last == node->ctx) |
0f212204 | 9285 | tctx->last = NULL; |
13bf43f5 | 9286 | kfree(node); |
0f212204 JA |
9287 | } |
9288 | ||
8452d4a6 | 9289 | static void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 9290 | { |
ba5ef6dc | 9291 | struct io_wq *wq = tctx->io_wq; |
13bf43f5 | 9292 | struct io_tctx_node *node; |
de7f1d9e PB |
9293 | unsigned long index; |
9294 | ||
13bf43f5 | 9295 | xa_for_each(&tctx->xa, index, node) |
eef51daa | 9296 | io_uring_del_tctx_node(index); |
b16ef427 ME |
9297 | if (wq) { |
9298 | /* | |
9299 | * Must be after io_uring_del_task_file() (removes nodes under | |
9300 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). | |
9301 | */ | |
ba5ef6dc | 9302 | io_wq_put_and_exit(wq); |
dadebc35 | 9303 | tctx->io_wq = NULL; |
b16ef427 | 9304 | } |
de7f1d9e PB |
9305 | } |
9306 | ||
3f48cf18 | 9307 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
521d6a73 | 9308 | { |
3f48cf18 PB |
9309 | if (tracked) |
9310 | return atomic_read(&tctx->inflight_tracked); | |
521d6a73 PB |
9311 | return percpu_counter_sum(&tctx->inflight); |
9312 | } | |
9313 | ||
09899b19 PB |
9314 | static void io_uring_drop_tctx_refs(struct task_struct *task) |
9315 | { | |
9316 | struct io_uring_task *tctx = task->io_uring; | |
9317 | unsigned int refs = tctx->cached_refs; | |
9318 | ||
e9dbe221 PB |
9319 | if (refs) { |
9320 | tctx->cached_refs = 0; | |
9321 | percpu_counter_sub(&tctx->inflight, refs); | |
9322 | put_task_struct_many(task, refs); | |
9323 | } | |
09899b19 PB |
9324 | } |
9325 | ||
78cc687b PB |
9326 | /* |
9327 | * Find any io_uring ctx that this task has registered or done IO on, and cancel | |
9328 | * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation. | |
9329 | */ | |
9330 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd) | |
0e9ddb39 | 9331 | { |
521d6a73 | 9332 | struct io_uring_task *tctx = current->io_uring; |
734551df | 9333 | struct io_ring_ctx *ctx; |
0e9ddb39 PB |
9334 | s64 inflight; |
9335 | DEFINE_WAIT(wait); | |
fdaf083c | 9336 | |
78cc687b PB |
9337 | WARN_ON_ONCE(sqd && sqd->thread != current); |
9338 | ||
6d042ffb PO |
9339 | if (!current->io_uring) |
9340 | return; | |
17a91051 PB |
9341 | if (tctx->io_wq) |
9342 | io_wq_exit_start(tctx->io_wq); | |
9343 | ||
0e9ddb39 PB |
9344 | atomic_inc(&tctx->in_idle); |
9345 | do { | |
e9dbe221 | 9346 | io_uring_drop_tctx_refs(current); |
0e9ddb39 | 9347 | /* read completions before cancelations */ |
78cc687b | 9348 | inflight = tctx_inflight(tctx, !cancel_all); |
0e9ddb39 PB |
9349 | if (!inflight) |
9350 | break; | |
fdaf083c | 9351 | |
78cc687b PB |
9352 | if (!sqd) { |
9353 | struct io_tctx_node *node; | |
9354 | unsigned long index; | |
0f212204 | 9355 | |
78cc687b PB |
9356 | xa_for_each(&tctx->xa, index, node) { |
9357 | /* sqpoll task will cancel all its requests */ | |
9358 | if (node->ctx->sq_data) | |
9359 | continue; | |
9360 | io_uring_try_cancel_requests(node->ctx, current, | |
9361 | cancel_all); | |
9362 | } | |
9363 | } else { | |
9364 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
9365 | io_uring_try_cancel_requests(ctx, current, | |
9366 | cancel_all); | |
9367 | } | |
17a91051 | 9368 | |
0f212204 | 9369 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
e9dbe221 | 9370 | io_uring_drop_tctx_refs(current); |
0f212204 | 9371 | /* |
a1bb3cd5 PB |
9372 | * If we've seen completions, retry without waiting. This |
9373 | * avoids a race where a completion comes in before we did | |
9374 | * prepare_to_wait(). | |
0f212204 | 9375 | */ |
3dd0c97a | 9376 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
a1bb3cd5 | 9377 | schedule(); |
f57555ed | 9378 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 9379 | } while (1); |
fdaf083c | 9380 | atomic_dec(&tctx->in_idle); |
de7f1d9e | 9381 | |
8452d4a6 | 9382 | io_uring_clean_tctx(tctx); |
3dd0c97a | 9383 | if (cancel_all) { |
3f48cf18 PB |
9384 | /* for exec all current's requests should be gone, kill tctx */ |
9385 | __io_uring_free(current); | |
9386 | } | |
44e728b8 PB |
9387 | } |
9388 | ||
f552a27a | 9389 | void __io_uring_cancel(bool cancel_all) |
78cc687b | 9390 | { |
f552a27a | 9391 | io_uring_cancel_generic(cancel_all, NULL); |
78cc687b PB |
9392 | } |
9393 | ||
6c5c240e RP |
9394 | static void *io_uring_validate_mmap_request(struct file *file, |
9395 | loff_t pgoff, size_t sz) | |
2b188cc1 | 9396 | { |
2b188cc1 | 9397 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 9398 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
9399 | struct page *page; |
9400 | void *ptr; | |
9401 | ||
9402 | switch (offset) { | |
9403 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
9404 | case IORING_OFF_CQ_RING: |
9405 | ptr = ctx->rings; | |
2b188cc1 JA |
9406 | break; |
9407 | case IORING_OFF_SQES: | |
9408 | ptr = ctx->sq_sqes; | |
9409 | break; | |
2b188cc1 | 9410 | default: |
6c5c240e | 9411 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
9412 | } |
9413 | ||
9414 | page = virt_to_head_page(ptr); | |
a50b854e | 9415 | if (sz > page_size(page)) |
6c5c240e RP |
9416 | return ERR_PTR(-EINVAL); |
9417 | ||
9418 | return ptr; | |
9419 | } | |
9420 | ||
9421 | #ifdef CONFIG_MMU | |
9422 | ||
9423 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9424 | { | |
9425 | size_t sz = vma->vm_end - vma->vm_start; | |
9426 | unsigned long pfn; | |
9427 | void *ptr; | |
9428 | ||
9429 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
9430 | if (IS_ERR(ptr)) | |
9431 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9432 | |
9433 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9434 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9435 | } | |
9436 | ||
6c5c240e RP |
9437 | #else /* !CONFIG_MMU */ |
9438 | ||
9439 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9440 | { | |
9441 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9442 | } | |
9443 | ||
9444 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9445 | { | |
9446 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9447 | } | |
9448 | ||
9449 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9450 | unsigned long addr, unsigned long len, | |
9451 | unsigned long pgoff, unsigned long flags) | |
9452 | { | |
9453 | void *ptr; | |
9454 | ||
9455 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9456 | if (IS_ERR(ptr)) | |
9457 | return PTR_ERR(ptr); | |
9458 | ||
9459 | return (unsigned long) ptr; | |
9460 | } | |
9461 | ||
9462 | #endif /* !CONFIG_MMU */ | |
9463 | ||
d9d05217 | 9464 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 JA |
9465 | { |
9466 | DEFINE_WAIT(wait); | |
9467 | ||
9468 | do { | |
9469 | if (!io_sqring_full(ctx)) | |
9470 | break; | |
90554200 JA |
9471 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
9472 | ||
9473 | if (!io_sqring_full(ctx)) | |
9474 | break; | |
90554200 JA |
9475 | schedule(); |
9476 | } while (!signal_pending(current)); | |
9477 | ||
9478 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
5199328a | 9479 | return 0; |
90554200 JA |
9480 | } |
9481 | ||
c73ebb68 HX |
9482 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9483 | struct __kernel_timespec __user **ts, | |
9484 | const sigset_t __user **sig) | |
9485 | { | |
9486 | struct io_uring_getevents_arg arg; | |
9487 | ||
9488 | /* | |
9489 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9490 | * is just a pointer to the sigset_t. | |
9491 | */ | |
9492 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9493 | *sig = (const sigset_t __user *) argp; | |
9494 | *ts = NULL; | |
9495 | return 0; | |
9496 | } | |
9497 | ||
9498 | /* | |
9499 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9500 | * timespec and sigset_t pointers if good. | |
9501 | */ | |
9502 | if (*argsz != sizeof(arg)) | |
9503 | return -EINVAL; | |
9504 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9505 | return -EFAULT; | |
9506 | *sig = u64_to_user_ptr(arg.sigmask); | |
9507 | *argsz = arg.sigmask_sz; | |
9508 | *ts = u64_to_user_ptr(arg.ts); | |
9509 | return 0; | |
9510 | } | |
9511 | ||
2b188cc1 | 9512 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9513 | u32, min_complete, u32, flags, const void __user *, argp, |
9514 | size_t, argsz) | |
2b188cc1 JA |
9515 | { |
9516 | struct io_ring_ctx *ctx; | |
2b188cc1 JA |
9517 | int submitted = 0; |
9518 | struct fd f; | |
33f993da | 9519 | long ret; |
2b188cc1 | 9520 | |
4c6e277c | 9521 | io_run_task_work(); |
b41e9852 | 9522 | |
33f993da PB |
9523 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
9524 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) | |
2b188cc1 JA |
9525 | return -EINVAL; |
9526 | ||
9527 | f = fdget(fd); | |
33f993da | 9528 | if (unlikely(!f.file)) |
2b188cc1 JA |
9529 | return -EBADF; |
9530 | ||
9531 | ret = -EOPNOTSUPP; | |
33f993da | 9532 | if (unlikely(f.file->f_op != &io_uring_fops)) |
2b188cc1 JA |
9533 | goto out_fput; |
9534 | ||
9535 | ret = -ENXIO; | |
9536 | ctx = f.file->private_data; | |
33f993da | 9537 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
2b188cc1 JA |
9538 | goto out_fput; |
9539 | ||
7e84e1c7 | 9540 | ret = -EBADFD; |
33f993da | 9541 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
9542 | goto out; |
9543 | ||
6c271ce2 JA |
9544 | /* |
9545 | * For SQ polling, the thread will do all submissions and completions. | |
9546 | * Just return the requested submit count, and wake the thread if | |
9547 | * we were asked to. | |
9548 | */ | |
b2a9eada | 9549 | ret = 0; |
6c271ce2 | 9550 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
90f67366 | 9551 | io_cqring_overflow_flush(ctx); |
89448c47 | 9552 | |
21f96522 JA |
9553 | if (unlikely(ctx->sq_data->thread == NULL)) { |
9554 | ret = -EOWNERDEAD; | |
04147488 | 9555 | goto out; |
21f96522 | 9556 | } |
6c271ce2 | 9557 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9558 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9559 | if (flags & IORING_ENTER_SQ_WAIT) { |
9560 | ret = io_sqpoll_wait_sq(ctx); | |
9561 | if (ret) | |
9562 | goto out; | |
9563 | } | |
6c271ce2 | 9564 | submitted = to_submit; |
b2a9eada | 9565 | } else if (to_submit) { |
eef51daa | 9566 | ret = io_uring_add_tctx_node(ctx); |
0f212204 JA |
9567 | if (unlikely(ret)) |
9568 | goto out; | |
2b188cc1 | 9569 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9570 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9571 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9572 | |
9573 | if (submitted != to_submit) | |
9574 | goto out; | |
2b188cc1 JA |
9575 | } |
9576 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9577 | const sigset_t __user *sig; |
9578 | struct __kernel_timespec __user *ts; | |
9579 | ||
9580 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9581 | if (unlikely(ret)) | |
9582 | goto out; | |
9583 | ||
2b188cc1 JA |
9584 | min_complete = min(min_complete, ctx->cq_entries); |
9585 | ||
32b2244a XW |
9586 | /* |
9587 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9588 | * space applications don't need to do io completion events | |
9589 | * polling again, they can rely on io_sq_thread to do polling | |
9590 | * work, which can reduce cpu usage and uring_lock contention. | |
9591 | */ | |
9592 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
9593 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 9594 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 9595 | } else { |
c73ebb68 | 9596 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 9597 | } |
2b188cc1 JA |
9598 | } |
9599 | ||
7c504e65 | 9600 | out: |
6805b32e | 9601 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
9602 | out_fput: |
9603 | fdput(f); | |
9604 | return submitted ? submitted : ret; | |
9605 | } | |
9606 | ||
bebdb65e | 9607 | #ifdef CONFIG_PROC_FS |
61cf9370 MWO |
9608 | static int io_uring_show_cred(struct seq_file *m, unsigned int id, |
9609 | const struct cred *cred) | |
87ce955b | 9610 | { |
87ce955b JA |
9611 | struct user_namespace *uns = seq_user_ns(m); |
9612 | struct group_info *gi; | |
9613 | kernel_cap_t cap; | |
9614 | unsigned __capi; | |
9615 | int g; | |
9616 | ||
9617 | seq_printf(m, "%5d\n", id); | |
9618 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
9619 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
9620 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
9621 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
9622 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
9623 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
9624 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
9625 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
9626 | seq_puts(m, "\n\tGroups:\t"); | |
9627 | gi = cred->group_info; | |
9628 | for (g = 0; g < gi->ngroups; g++) { | |
9629 | seq_put_decimal_ull(m, g ? " " : "", | |
9630 | from_kgid_munged(uns, gi->gid[g])); | |
9631 | } | |
9632 | seq_puts(m, "\n\tCapEff:\t"); | |
9633 | cap = cred->cap_effective; | |
9634 | CAP_FOR_EACH_U32(__capi) | |
9635 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
9636 | seq_putc(m, '\n'); | |
9637 | return 0; | |
9638 | } | |
9639 | ||
9640 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
9641 | { | |
dbbe9c64 | 9642 | struct io_sq_data *sq = NULL; |
fad8e0de | 9643 | bool has_lock; |
87ce955b JA |
9644 | int i; |
9645 | ||
fad8e0de JA |
9646 | /* |
9647 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
9648 | * since fdinfo case grabs it in the opposite direction of normal use | |
9649 | * cases. If we fail to get the lock, we just don't iterate any | |
9650 | * structures that could be going away outside the io_uring mutex. | |
9651 | */ | |
9652 | has_lock = mutex_trylock(&ctx->uring_lock); | |
9653 | ||
5f3f26f9 | 9654 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 9655 | sq = ctx->sq_data; |
5f3f26f9 JA |
9656 | if (!sq->thread) |
9657 | sq = NULL; | |
9658 | } | |
dbbe9c64 JQ |
9659 | |
9660 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
9661 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 9662 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 9663 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
7b29f92d | 9664 | struct file *f = io_file_from_index(ctx, i); |
87ce955b | 9665 | |
87ce955b JA |
9666 | if (f) |
9667 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
9668 | else | |
9669 | seq_printf(m, "%5u: <none>\n", i); | |
9670 | } | |
9671 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 9672 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
41edf1a5 | 9673 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
4751f53d | 9674 | unsigned int len = buf->ubuf_end - buf->ubuf; |
87ce955b | 9675 | |
4751f53d | 9676 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
87ce955b | 9677 | } |
61cf9370 MWO |
9678 | if (has_lock && !xa_empty(&ctx->personalities)) { |
9679 | unsigned long index; | |
9680 | const struct cred *cred; | |
9681 | ||
87ce955b | 9682 | seq_printf(m, "Personalities:\n"); |
61cf9370 MWO |
9683 | xa_for_each(&ctx->personalities, index, cred) |
9684 | io_uring_show_cred(m, index, cred); | |
87ce955b | 9685 | } |
d7718a9d | 9686 | seq_printf(m, "PollList:\n"); |
79ebeaee | 9687 | spin_lock(&ctx->completion_lock); |
d7718a9d JA |
9688 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
9689 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
9690 | struct io_kiocb *req; | |
9691 | ||
9692 | hlist_for_each_entry(req, list, hash_node) | |
9693 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
9694 | req->task->task_works != NULL); | |
9695 | } | |
79ebeaee | 9696 | spin_unlock(&ctx->completion_lock); |
fad8e0de JA |
9697 | if (has_lock) |
9698 | mutex_unlock(&ctx->uring_lock); | |
87ce955b JA |
9699 | } |
9700 | ||
9701 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
9702 | { | |
9703 | struct io_ring_ctx *ctx = f->private_data; | |
9704 | ||
9705 | if (percpu_ref_tryget(&ctx->refs)) { | |
9706 | __io_uring_show_fdinfo(ctx, m); | |
9707 | percpu_ref_put(&ctx->refs); | |
9708 | } | |
9709 | } | |
bebdb65e | 9710 | #endif |
87ce955b | 9711 | |
2b188cc1 JA |
9712 | static const struct file_operations io_uring_fops = { |
9713 | .release = io_uring_release, | |
9714 | .mmap = io_uring_mmap, | |
6c5c240e RP |
9715 | #ifndef CONFIG_MMU |
9716 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
9717 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
9718 | #endif | |
2b188cc1 JA |
9719 | .poll = io_uring_poll, |
9720 | .fasync = io_uring_fasync, | |
bebdb65e | 9721 | #ifdef CONFIG_PROC_FS |
87ce955b | 9722 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 9723 | #endif |
2b188cc1 JA |
9724 | }; |
9725 | ||
9726 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
9727 | struct io_uring_params *p) | |
9728 | { | |
75b28aff HV |
9729 | struct io_rings *rings; |
9730 | size_t size, sq_array_offset; | |
2b188cc1 | 9731 | |
bd740481 JA |
9732 | /* make sure these are sane, as we already accounted them */ |
9733 | ctx->sq_entries = p->sq_entries; | |
9734 | ctx->cq_entries = p->cq_entries; | |
9735 | ||
75b28aff HV |
9736 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
9737 | if (size == SIZE_MAX) | |
9738 | return -EOVERFLOW; | |
9739 | ||
9740 | rings = io_mem_alloc(size); | |
9741 | if (!rings) | |
2b188cc1 JA |
9742 | return -ENOMEM; |
9743 | ||
75b28aff HV |
9744 | ctx->rings = rings; |
9745 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
9746 | rings->sq_ring_mask = p->sq_entries - 1; | |
9747 | rings->cq_ring_mask = p->cq_entries - 1; | |
9748 | rings->sq_ring_entries = p->sq_entries; | |
9749 | rings->cq_ring_entries = p->cq_entries; | |
2b188cc1 JA |
9750 | |
9751 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
9752 | if (size == SIZE_MAX) { |
9753 | io_mem_free(ctx->rings); | |
9754 | ctx->rings = NULL; | |
2b188cc1 | 9755 | return -EOVERFLOW; |
eb065d30 | 9756 | } |
2b188cc1 JA |
9757 | |
9758 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
9759 | if (!ctx->sq_sqes) { |
9760 | io_mem_free(ctx->rings); | |
9761 | ctx->rings = NULL; | |
2b188cc1 | 9762 | return -ENOMEM; |
eb065d30 | 9763 | } |
2b188cc1 | 9764 | |
2b188cc1 JA |
9765 | return 0; |
9766 | } | |
9767 | ||
9faadcc8 PB |
9768 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
9769 | { | |
9770 | int ret, fd; | |
9771 | ||
9772 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
9773 | if (fd < 0) | |
9774 | return fd; | |
9775 | ||
eef51daa | 9776 | ret = io_uring_add_tctx_node(ctx); |
9faadcc8 PB |
9777 | if (ret) { |
9778 | put_unused_fd(fd); | |
9779 | return ret; | |
9780 | } | |
9781 | fd_install(fd, file); | |
9782 | return fd; | |
9783 | } | |
9784 | ||
2b188cc1 JA |
9785 | /* |
9786 | * Allocate an anonymous fd, this is what constitutes the application | |
9787 | * visible backing of an io_uring instance. The application mmaps this | |
9788 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
9789 | * we have to tie this fd to a socket for file garbage collection purposes. | |
9790 | */ | |
9faadcc8 | 9791 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
9792 | { |
9793 | struct file *file; | |
9faadcc8 | 9794 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
9795 | int ret; |
9796 | ||
2b188cc1 JA |
9797 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
9798 | &ctx->ring_sock); | |
9799 | if (ret) | |
9faadcc8 | 9800 | return ERR_PTR(ret); |
2b188cc1 JA |
9801 | #endif |
9802 | ||
2b188cc1 JA |
9803 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
9804 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 9805 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
9806 | if (IS_ERR(file)) { |
9807 | sock_release(ctx->ring_sock); | |
9808 | ctx->ring_sock = NULL; | |
9809 | } else { | |
9810 | ctx->ring_sock->file = file; | |
0f212204 | 9811 | } |
2b188cc1 | 9812 | #endif |
9faadcc8 | 9813 | return file; |
2b188cc1 JA |
9814 | } |
9815 | ||
7f13657d XW |
9816 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
9817 | struct io_uring_params __user *params) | |
2b188cc1 | 9818 | { |
2b188cc1 | 9819 | struct io_ring_ctx *ctx; |
9faadcc8 | 9820 | struct file *file; |
2b188cc1 JA |
9821 | int ret; |
9822 | ||
8110c1a6 | 9823 | if (!entries) |
2b188cc1 | 9824 | return -EINVAL; |
8110c1a6 JA |
9825 | if (entries > IORING_MAX_ENTRIES) { |
9826 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9827 | return -EINVAL; | |
9828 | entries = IORING_MAX_ENTRIES; | |
9829 | } | |
2b188cc1 JA |
9830 | |
9831 | /* | |
9832 | * Use twice as many entries for the CQ ring. It's possible for the | |
9833 | * application to drive a higher depth than the size of the SQ ring, | |
9834 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
9835 | * some flexibility in overcommitting a bit. If the application has |
9836 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
9837 | * of CQ ring entries manually. | |
2b188cc1 JA |
9838 | */ |
9839 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
9840 | if (p->flags & IORING_SETUP_CQSIZE) { |
9841 | /* | |
9842 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
9843 | * to a power-of-two, if it isn't already. We do NOT impose | |
9844 | * any cq vs sq ring sizing. | |
9845 | */ | |
eb2667b3 | 9846 | if (!p->cq_entries) |
33a107f0 | 9847 | return -EINVAL; |
8110c1a6 JA |
9848 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
9849 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9850 | return -EINVAL; | |
9851 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
9852 | } | |
eb2667b3 JQ |
9853 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
9854 | if (p->cq_entries < p->sq_entries) | |
9855 | return -EINVAL; | |
33a107f0 JA |
9856 | } else { |
9857 | p->cq_entries = 2 * p->sq_entries; | |
9858 | } | |
2b188cc1 | 9859 | |
2b188cc1 | 9860 | ctx = io_ring_ctx_alloc(p); |
62e398be | 9861 | if (!ctx) |
2b188cc1 | 9862 | return -ENOMEM; |
2b188cc1 | 9863 | ctx->compat = in_compat_syscall(); |
62e398be JA |
9864 | if (!capable(CAP_IPC_LOCK)) |
9865 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
9866 | |
9867 | /* | |
9868 | * This is just grabbed for accounting purposes. When a process exits, | |
9869 | * the mm is exited and dropped before the files, hence we need to hang | |
9870 | * on to this mm purely for the purposes of being able to unaccount | |
9871 | * memory (locked/pinned vm). It's not used for anything else. | |
9872 | */ | |
6b7898eb | 9873 | mmgrab(current->mm); |
2aede0e4 | 9874 | ctx->mm_account = current->mm; |
6b7898eb | 9875 | |
2b188cc1 JA |
9876 | ret = io_allocate_scq_urings(ctx, p); |
9877 | if (ret) | |
9878 | goto err; | |
9879 | ||
7e84e1c7 | 9880 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
9881 | if (ret) |
9882 | goto err; | |
eae071c9 | 9883 | /* always set a rsrc node */ |
47b228ce PB |
9884 | ret = io_rsrc_node_switch_start(ctx); |
9885 | if (ret) | |
9886 | goto err; | |
eae071c9 | 9887 | io_rsrc_node_switch(ctx, NULL); |
2b188cc1 | 9888 | |
2b188cc1 | 9889 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
9890 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
9891 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
9892 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
9893 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
9894 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
9895 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
9896 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
9897 | |
9898 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
9899 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
9900 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
9901 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
9902 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
9903 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
9904 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 9905 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 9906 | |
7f13657d XW |
9907 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
9908 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 9909 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 9910 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9690557e PB |
9911 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
9912 | IORING_FEAT_RSRC_TAGS; | |
7f13657d XW |
9913 | |
9914 | if (copy_to_user(params, p, sizeof(*p))) { | |
9915 | ret = -EFAULT; | |
9916 | goto err; | |
9917 | } | |
d1719f70 | 9918 | |
9faadcc8 PB |
9919 | file = io_uring_get_file(ctx); |
9920 | if (IS_ERR(file)) { | |
9921 | ret = PTR_ERR(file); | |
9922 | goto err; | |
9923 | } | |
9924 | ||
044c1ab3 JA |
9925 | /* |
9926 | * Install ring fd as the very last thing, so we don't risk someone | |
9927 | * having closed it before we finish setup | |
9928 | */ | |
9faadcc8 PB |
9929 | ret = io_uring_install_fd(ctx, file); |
9930 | if (ret < 0) { | |
9931 | /* fput will clean it up */ | |
9932 | fput(file); | |
9933 | return ret; | |
9934 | } | |
044c1ab3 | 9935 | |
c826bd7a | 9936 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
9937 | return ret; |
9938 | err: | |
9939 | io_ring_ctx_wait_and_kill(ctx); | |
9940 | return ret; | |
9941 | } | |
9942 | ||
9943 | /* | |
9944 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
9945 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
9946 | * params structure passed in. | |
9947 | */ | |
9948 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
9949 | { | |
9950 | struct io_uring_params p; | |
2b188cc1 JA |
9951 | int i; |
9952 | ||
9953 | if (copy_from_user(&p, params, sizeof(p))) | |
9954 | return -EFAULT; | |
9955 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
9956 | if (p.resv[i]) | |
9957 | return -EINVAL; | |
9958 | } | |
9959 | ||
6c271ce2 | 9960 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 9961 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
9962 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
9963 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
9964 | return -EINVAL; |
9965 | ||
7f13657d | 9966 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
9967 | } |
9968 | ||
9969 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
9970 | struct io_uring_params __user *, params) | |
9971 | { | |
9972 | return io_uring_setup(entries, params); | |
9973 | } | |
9974 | ||
66f4af93 JA |
9975 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
9976 | { | |
9977 | struct io_uring_probe *p; | |
9978 | size_t size; | |
9979 | int i, ret; | |
9980 | ||
9981 | size = struct_size(p, ops, nr_args); | |
9982 | if (size == SIZE_MAX) | |
9983 | return -EOVERFLOW; | |
9984 | p = kzalloc(size, GFP_KERNEL); | |
9985 | if (!p) | |
9986 | return -ENOMEM; | |
9987 | ||
9988 | ret = -EFAULT; | |
9989 | if (copy_from_user(p, arg, size)) | |
9990 | goto out; | |
9991 | ret = -EINVAL; | |
9992 | if (memchr_inv(p, 0, size)) | |
9993 | goto out; | |
9994 | ||
9995 | p->last_op = IORING_OP_LAST - 1; | |
9996 | if (nr_args > IORING_OP_LAST) | |
9997 | nr_args = IORING_OP_LAST; | |
9998 | ||
9999 | for (i = 0; i < nr_args; i++) { | |
10000 | p->ops[i].op = i; | |
10001 | if (!io_op_defs[i].not_supported) | |
10002 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
10003 | } | |
10004 | p->ops_len = i; | |
10005 | ||
10006 | ret = 0; | |
10007 | if (copy_to_user(arg, p, size)) | |
10008 | ret = -EFAULT; | |
10009 | out: | |
10010 | kfree(p); | |
10011 | return ret; | |
10012 | } | |
10013 | ||
071698e1 JA |
10014 | static int io_register_personality(struct io_ring_ctx *ctx) |
10015 | { | |
4379bf8b | 10016 | const struct cred *creds; |
61cf9370 | 10017 | u32 id; |
1e6fa521 | 10018 | int ret; |
071698e1 | 10019 | |
4379bf8b | 10020 | creds = get_current_cred(); |
1e6fa521 | 10021 | |
61cf9370 MWO |
10022 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
10023 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); | |
a30f895a JA |
10024 | if (ret < 0) { |
10025 | put_cred(creds); | |
10026 | return ret; | |
10027 | } | |
10028 | return id; | |
071698e1 JA |
10029 | } |
10030 | ||
21b55dbc SG |
10031 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
10032 | unsigned int nr_args) | |
10033 | { | |
10034 | struct io_uring_restriction *res; | |
10035 | size_t size; | |
10036 | int i, ret; | |
10037 | ||
7e84e1c7 SG |
10038 | /* Restrictions allowed only if rings started disabled */ |
10039 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10040 | return -EBADFD; | |
10041 | ||
21b55dbc | 10042 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 10043 | if (ctx->restrictions.registered) |
21b55dbc SG |
10044 | return -EBUSY; |
10045 | ||
10046 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
10047 | return -EINVAL; | |
10048 | ||
10049 | size = array_size(nr_args, sizeof(*res)); | |
10050 | if (size == SIZE_MAX) | |
10051 | return -EOVERFLOW; | |
10052 | ||
10053 | res = memdup_user(arg, size); | |
10054 | if (IS_ERR(res)) | |
10055 | return PTR_ERR(res); | |
10056 | ||
10057 | ret = 0; | |
10058 | ||
10059 | for (i = 0; i < nr_args; i++) { | |
10060 | switch (res[i].opcode) { | |
10061 | case IORING_RESTRICTION_REGISTER_OP: | |
10062 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
10063 | ret = -EINVAL; | |
10064 | goto out; | |
10065 | } | |
10066 | ||
10067 | __set_bit(res[i].register_op, | |
10068 | ctx->restrictions.register_op); | |
10069 | break; | |
10070 | case IORING_RESTRICTION_SQE_OP: | |
10071 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
10072 | ret = -EINVAL; | |
10073 | goto out; | |
10074 | } | |
10075 | ||
10076 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
10077 | break; | |
10078 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
10079 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
10080 | break; | |
10081 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
10082 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
10083 | break; | |
10084 | default: | |
10085 | ret = -EINVAL; | |
10086 | goto out; | |
10087 | } | |
10088 | } | |
10089 | ||
10090 | out: | |
10091 | /* Reset all restrictions if an error happened */ | |
10092 | if (ret != 0) | |
10093 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
10094 | else | |
7e84e1c7 | 10095 | ctx->restrictions.registered = true; |
21b55dbc SG |
10096 | |
10097 | kfree(res); | |
10098 | return ret; | |
10099 | } | |
10100 | ||
7e84e1c7 SG |
10101 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
10102 | { | |
10103 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10104 | return -EBADFD; | |
10105 | ||
10106 | if (ctx->restrictions.registered) | |
10107 | ctx->restricted = 1; | |
10108 | ||
0298ef96 PB |
10109 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
10110 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) | |
10111 | wake_up(&ctx->sq_data->wait); | |
7e84e1c7 SG |
10112 | return 0; |
10113 | } | |
10114 | ||
fdecb662 | 10115 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 10116 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 PB |
10117 | unsigned nr_args) |
10118 | { | |
10119 | __u32 tmp; | |
10120 | int err; | |
10121 | ||
c3bdad02 PB |
10122 | if (up->resv) |
10123 | return -EINVAL; | |
98f0b3b4 PB |
10124 | if (check_add_overflow(up->offset, nr_args, &tmp)) |
10125 | return -EOVERFLOW; | |
10126 | err = io_rsrc_node_switch_start(ctx); | |
10127 | if (err) | |
10128 | return err; | |
10129 | ||
fdecb662 PB |
10130 | switch (type) { |
10131 | case IORING_RSRC_FILE: | |
98f0b3b4 | 10132 | return __io_sqe_files_update(ctx, up, nr_args); |
634d00df PB |
10133 | case IORING_RSRC_BUFFER: |
10134 | return __io_sqe_buffers_update(ctx, up, nr_args); | |
98f0b3b4 PB |
10135 | } |
10136 | return -EINVAL; | |
10137 | } | |
10138 | ||
c3bdad02 PB |
10139 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
10140 | unsigned nr_args) | |
98f0b3b4 | 10141 | { |
c3bdad02 | 10142 | struct io_uring_rsrc_update2 up; |
98f0b3b4 PB |
10143 | |
10144 | if (!nr_args) | |
10145 | return -EINVAL; | |
c3bdad02 PB |
10146 | memset(&up, 0, sizeof(up)); |
10147 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) | |
10148 | return -EFAULT; | |
10149 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); | |
10150 | } | |
10151 | ||
10152 | static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
992da01a | 10153 | unsigned size, unsigned type) |
c3bdad02 PB |
10154 | { |
10155 | struct io_uring_rsrc_update2 up; | |
10156 | ||
10157 | if (size != sizeof(up)) | |
10158 | return -EINVAL; | |
98f0b3b4 PB |
10159 | if (copy_from_user(&up, arg, sizeof(up))) |
10160 | return -EFAULT; | |
992da01a | 10161 | if (!up.nr || up.resv) |
98f0b3b4 | 10162 | return -EINVAL; |
992da01a | 10163 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
98f0b3b4 PB |
10164 | } |
10165 | ||
792e3582 | 10166 | static int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
992da01a | 10167 | unsigned int size, unsigned int type) |
792e3582 PB |
10168 | { |
10169 | struct io_uring_rsrc_register rr; | |
10170 | ||
10171 | /* keep it extendible */ | |
10172 | if (size != sizeof(rr)) | |
10173 | return -EINVAL; | |
10174 | ||
10175 | memset(&rr, 0, sizeof(rr)); | |
10176 | if (copy_from_user(&rr, arg, size)) | |
10177 | return -EFAULT; | |
992da01a | 10178 | if (!rr.nr || rr.resv || rr.resv2) |
792e3582 PB |
10179 | return -EINVAL; |
10180 | ||
992da01a | 10181 | switch (type) { |
792e3582 PB |
10182 | case IORING_RSRC_FILE: |
10183 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), | |
10184 | rr.nr, u64_to_user_ptr(rr.tags)); | |
634d00df PB |
10185 | case IORING_RSRC_BUFFER: |
10186 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), | |
10187 | rr.nr, u64_to_user_ptr(rr.tags)); | |
792e3582 PB |
10188 | } |
10189 | return -EINVAL; | |
10190 | } | |
10191 | ||
fe76421d JA |
10192 | static int io_register_iowq_aff(struct io_ring_ctx *ctx, void __user *arg, |
10193 | unsigned len) | |
10194 | { | |
10195 | struct io_uring_task *tctx = current->io_uring; | |
10196 | cpumask_var_t new_mask; | |
10197 | int ret; | |
10198 | ||
10199 | if (!tctx || !tctx->io_wq) | |
10200 | return -EINVAL; | |
10201 | ||
10202 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | |
10203 | return -ENOMEM; | |
10204 | ||
10205 | cpumask_clear(new_mask); | |
10206 | if (len > cpumask_size()) | |
10207 | len = cpumask_size(); | |
10208 | ||
10209 | if (copy_from_user(new_mask, arg, len)) { | |
10210 | free_cpumask_var(new_mask); | |
10211 | return -EFAULT; | |
10212 | } | |
10213 | ||
10214 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); | |
10215 | free_cpumask_var(new_mask); | |
10216 | return ret; | |
10217 | } | |
10218 | ||
10219 | static int io_unregister_iowq_aff(struct io_ring_ctx *ctx) | |
10220 | { | |
10221 | struct io_uring_task *tctx = current->io_uring; | |
10222 | ||
10223 | if (!tctx || !tctx->io_wq) | |
10224 | return -EINVAL; | |
10225 | ||
10226 | return io_wq_cpu_affinity(tctx->io_wq, NULL); | |
10227 | } | |
10228 | ||
071698e1 JA |
10229 | static bool io_register_op_must_quiesce(int op) |
10230 | { | |
10231 | switch (op) { | |
bd54b6fe BM |
10232 | case IORING_REGISTER_BUFFERS: |
10233 | case IORING_UNREGISTER_BUFFERS: | |
f4f7d21c | 10234 | case IORING_REGISTER_FILES: |
071698e1 JA |
10235 | case IORING_UNREGISTER_FILES: |
10236 | case IORING_REGISTER_FILES_UPDATE: | |
10237 | case IORING_REGISTER_PROBE: | |
10238 | case IORING_REGISTER_PERSONALITY: | |
10239 | case IORING_UNREGISTER_PERSONALITY: | |
992da01a PB |
10240 | case IORING_REGISTER_FILES2: |
10241 | case IORING_REGISTER_FILES_UPDATE2: | |
10242 | case IORING_REGISTER_BUFFERS2: | |
10243 | case IORING_REGISTER_BUFFERS_UPDATE: | |
fe76421d JA |
10244 | case IORING_REGISTER_IOWQ_AFF: |
10245 | case IORING_UNREGISTER_IOWQ_AFF: | |
071698e1 JA |
10246 | return false; |
10247 | default: | |
10248 | return true; | |
10249 | } | |
10250 | } | |
10251 | ||
e73c5c7c PB |
10252 | static int io_ctx_quiesce(struct io_ring_ctx *ctx) |
10253 | { | |
10254 | long ret; | |
10255 | ||
10256 | percpu_ref_kill(&ctx->refs); | |
10257 | ||
10258 | /* | |
10259 | * Drop uring mutex before waiting for references to exit. If another | |
10260 | * thread is currently inside io_uring_enter() it might need to grab the | |
10261 | * uring_lock to make progress. If we hold it here across the drain | |
10262 | * wait, then we can deadlock. It's safe to drop the mutex here, since | |
10263 | * no new references will come in after we've killed the percpu ref. | |
10264 | */ | |
10265 | mutex_unlock(&ctx->uring_lock); | |
10266 | do { | |
10267 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
10268 | if (!ret) | |
10269 | break; | |
10270 | ret = io_run_task_work_sig(); | |
10271 | } while (ret >= 0); | |
10272 | mutex_lock(&ctx->uring_lock); | |
10273 | ||
10274 | if (ret) | |
10275 | io_refs_resurrect(&ctx->refs, &ctx->ref_comp); | |
10276 | return ret; | |
10277 | } | |
10278 | ||
edafccee JA |
10279 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
10280 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
10281 | __releases(ctx->uring_lock) |
10282 | __acquires(ctx->uring_lock) | |
edafccee JA |
10283 | { |
10284 | int ret; | |
10285 | ||
35fa71a0 JA |
10286 | /* |
10287 | * We're inside the ring mutex, if the ref is already dying, then | |
10288 | * someone else killed the ctx or is already going through | |
10289 | * io_uring_register(). | |
10290 | */ | |
10291 | if (percpu_ref_is_dying(&ctx->refs)) | |
10292 | return -ENXIO; | |
10293 | ||
75c4021a PB |
10294 | if (ctx->restricted) { |
10295 | if (opcode >= IORING_REGISTER_LAST) | |
10296 | return -EINVAL; | |
10297 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); | |
10298 | if (!test_bit(opcode, ctx->restrictions.register_op)) | |
10299 | return -EACCES; | |
10300 | } | |
10301 | ||
071698e1 | 10302 | if (io_register_op_must_quiesce(opcode)) { |
e73c5c7c PB |
10303 | ret = io_ctx_quiesce(ctx); |
10304 | if (ret) | |
f70865db | 10305 | return ret; |
05f3fb3c | 10306 | } |
edafccee JA |
10307 | |
10308 | switch (opcode) { | |
10309 | case IORING_REGISTER_BUFFERS: | |
634d00df | 10310 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
edafccee JA |
10311 | break; |
10312 | case IORING_UNREGISTER_BUFFERS: | |
10313 | ret = -EINVAL; | |
10314 | if (arg || nr_args) | |
10315 | break; | |
0a96bbe4 | 10316 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 10317 | break; |
6b06314c | 10318 | case IORING_REGISTER_FILES: |
792e3582 | 10319 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
6b06314c JA |
10320 | break; |
10321 | case IORING_UNREGISTER_FILES: | |
10322 | ret = -EINVAL; | |
10323 | if (arg || nr_args) | |
10324 | break; | |
10325 | ret = io_sqe_files_unregister(ctx); | |
10326 | break; | |
c3a31e60 | 10327 | case IORING_REGISTER_FILES_UPDATE: |
c3bdad02 | 10328 | ret = io_register_files_update(ctx, arg, nr_args); |
c3a31e60 | 10329 | break; |
9b402849 | 10330 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 10331 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
10332 | ret = -EINVAL; |
10333 | if (nr_args != 1) | |
10334 | break; | |
10335 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
10336 | if (ret) |
10337 | break; | |
10338 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
10339 | ctx->eventfd_async = 1; | |
10340 | else | |
10341 | ctx->eventfd_async = 0; | |
9b402849 JA |
10342 | break; |
10343 | case IORING_UNREGISTER_EVENTFD: | |
10344 | ret = -EINVAL; | |
10345 | if (arg || nr_args) | |
10346 | break; | |
10347 | ret = io_eventfd_unregister(ctx); | |
10348 | break; | |
66f4af93 JA |
10349 | case IORING_REGISTER_PROBE: |
10350 | ret = -EINVAL; | |
10351 | if (!arg || nr_args > 256) | |
10352 | break; | |
10353 | ret = io_probe(ctx, arg, nr_args); | |
10354 | break; | |
071698e1 JA |
10355 | case IORING_REGISTER_PERSONALITY: |
10356 | ret = -EINVAL; | |
10357 | if (arg || nr_args) | |
10358 | break; | |
10359 | ret = io_register_personality(ctx); | |
10360 | break; | |
10361 | case IORING_UNREGISTER_PERSONALITY: | |
10362 | ret = -EINVAL; | |
10363 | if (arg) | |
10364 | break; | |
10365 | ret = io_unregister_personality(ctx, nr_args); | |
10366 | break; | |
7e84e1c7 SG |
10367 | case IORING_REGISTER_ENABLE_RINGS: |
10368 | ret = -EINVAL; | |
10369 | if (arg || nr_args) | |
10370 | break; | |
10371 | ret = io_register_enable_rings(ctx); | |
10372 | break; | |
21b55dbc SG |
10373 | case IORING_REGISTER_RESTRICTIONS: |
10374 | ret = io_register_restrictions(ctx, arg, nr_args); | |
10375 | break; | |
992da01a PB |
10376 | case IORING_REGISTER_FILES2: |
10377 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); | |
10378 | break; | |
10379 | case IORING_REGISTER_FILES_UPDATE2: | |
10380 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
10381 | IORING_RSRC_FILE); | |
10382 | break; | |
10383 | case IORING_REGISTER_BUFFERS2: | |
10384 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); | |
792e3582 | 10385 | break; |
992da01a PB |
10386 | case IORING_REGISTER_BUFFERS_UPDATE: |
10387 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
10388 | IORING_RSRC_BUFFER); | |
c3bdad02 | 10389 | break; |
fe76421d JA |
10390 | case IORING_REGISTER_IOWQ_AFF: |
10391 | ret = -EINVAL; | |
10392 | if (!arg || !nr_args) | |
10393 | break; | |
10394 | ret = io_register_iowq_aff(ctx, arg, nr_args); | |
10395 | break; | |
10396 | case IORING_UNREGISTER_IOWQ_AFF: | |
10397 | ret = -EINVAL; | |
10398 | if (arg || nr_args) | |
10399 | break; | |
10400 | ret = io_unregister_iowq_aff(ctx); | |
10401 | break; | |
edafccee JA |
10402 | default: |
10403 | ret = -EINVAL; | |
10404 | break; | |
10405 | } | |
10406 | ||
071698e1 | 10407 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 10408 | /* bring the ctx back to life */ |
05f3fb3c | 10409 | percpu_ref_reinit(&ctx->refs); |
0f158b4c | 10410 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 10411 | } |
edafccee JA |
10412 | return ret; |
10413 | } | |
10414 | ||
10415 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
10416 | void __user *, arg, unsigned int, nr_args) | |
10417 | { | |
10418 | struct io_ring_ctx *ctx; | |
10419 | long ret = -EBADF; | |
10420 | struct fd f; | |
10421 | ||
10422 | f = fdget(fd); | |
10423 | if (!f.file) | |
10424 | return -EBADF; | |
10425 | ||
10426 | ret = -EOPNOTSUPP; | |
10427 | if (f.file->f_op != &io_uring_fops) | |
10428 | goto out_fput; | |
10429 | ||
10430 | ctx = f.file->private_data; | |
10431 | ||
b6c23dd5 PB |
10432 | io_run_task_work(); |
10433 | ||
edafccee JA |
10434 | mutex_lock(&ctx->uring_lock); |
10435 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
10436 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
10437 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
10438 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
10439 | out_fput: |
10440 | fdput(f); | |
10441 | return ret; | |
10442 | } | |
10443 | ||
2b188cc1 JA |
10444 | static int __init io_uring_init(void) |
10445 | { | |
d7f62e82 SM |
10446 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
10447 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
10448 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
10449 | } while (0) | |
10450 | ||
10451 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
10452 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
10453 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
10454 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
10455 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
10456 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
10457 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
10458 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
10459 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
10460 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 10461 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
10462 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
10463 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
10464 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
10465 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
10466 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
10467 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
10468 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
10469 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
10470 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
10471 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
10472 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
10473 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
10474 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
10475 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
10476 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 10477 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
10478 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
10479 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
16340eab | 10480 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
d7f62e82 | 10481 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
7d67af2c | 10482 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
b9445598 | 10483 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
d7f62e82 | 10484 | |
b0d658ec PB |
10485 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
10486 | sizeof(struct io_uring_rsrc_update)); | |
10487 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > | |
10488 | sizeof(struct io_uring_rsrc_update2)); | |
10489 | /* should fit into one byte */ | |
10490 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); | |
10491 | ||
d3656344 | 10492 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
84557871 | 10493 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
16340eab | 10494 | |
91f245d5 JA |
10495 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
10496 | SLAB_ACCOUNT); | |
2b188cc1 JA |
10497 | return 0; |
10498 | }; | |
10499 | __initcall(io_uring_init); |