]>
Commit | Line | Data |
---|---|---|
2b188cc1 JA |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Shared application/kernel submission and completion ring pairs, for | |
4 | * supporting fast/efficient IO. | |
5 | * | |
6 | * A note on the read/write ordering memory barriers that are matched between | |
1e84b97b SB |
7 | * the application and kernel side. |
8 | * | |
9 | * After the application reads the CQ ring tail, it must use an | |
10 | * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses | |
11 | * before writing the tail (using smp_load_acquire to read the tail will | |
12 | * do). It also needs a smp_mb() before updating CQ head (ordering the | |
13 | * entry load(s) with the head store), pairing with an implicit barrier | |
d068b506 | 14 | * through a control-dependency in io_get_cqe (smp_store_release to |
1e84b97b SB |
15 | * store head will do). Failure to do so could lead to reading invalid |
16 | * CQ entries. | |
17 | * | |
18 | * Likewise, the application must use an appropriate smp_wmb() before | |
19 | * writing the SQ tail (ordering SQ entry stores with the tail store), | |
20 | * which pairs with smp_load_acquire in io_get_sqring (smp_store_release | |
21 | * to store the tail will do). And it needs a barrier ordering the SQ | |
22 | * head load before writing new SQ entries (smp_load_acquire to read | |
23 | * head will do). | |
24 | * | |
25 | * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application | |
26 | * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after* | |
27 | * updating the SQ tail; a full memory barrier smp_mb() is needed | |
28 | * between. | |
2b188cc1 JA |
29 | * |
30 | * Also see the examples in the liburing library: | |
31 | * | |
32 | * git://git.kernel.dk/liburing | |
33 | * | |
34 | * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens | |
35 | * from data shared between the kernel and application. This is done both | |
36 | * for ordering purposes, but also to ensure that once a value is loaded from | |
37 | * data that the application could potentially modify, it remains stable. | |
38 | * | |
39 | * Copyright (C) 2018-2019 Jens Axboe | |
c992fe29 | 40 | * Copyright (c) 2018-2019 Christoph Hellwig |
2b188cc1 JA |
41 | */ |
42 | #include <linux/kernel.h> | |
43 | #include <linux/init.h> | |
44 | #include <linux/errno.h> | |
45 | #include <linux/syscalls.h> | |
46 | #include <linux/compat.h> | |
52de1fe1 | 47 | #include <net/compat.h> |
2b188cc1 JA |
48 | #include <linux/refcount.h> |
49 | #include <linux/uio.h> | |
6b47ee6e | 50 | #include <linux/bits.h> |
2b188cc1 JA |
51 | |
52 | #include <linux/sched/signal.h> | |
53 | #include <linux/fs.h> | |
54 | #include <linux/file.h> | |
55 | #include <linux/fdtable.h> | |
56 | #include <linux/mm.h> | |
57 | #include <linux/mman.h> | |
2b188cc1 JA |
58 | #include <linux/percpu.h> |
59 | #include <linux/slab.h> | |
2b188cc1 | 60 | #include <linux/blkdev.h> |
edafccee | 61 | #include <linux/bvec.h> |
2b188cc1 JA |
62 | #include <linux/net.h> |
63 | #include <net/sock.h> | |
64 | #include <net/af_unix.h> | |
6b06314c | 65 | #include <net/scm.h> |
2b188cc1 JA |
66 | #include <linux/anon_inodes.h> |
67 | #include <linux/sched/mm.h> | |
68 | #include <linux/uaccess.h> | |
69 | #include <linux/nospec.h> | |
edafccee JA |
70 | #include <linux/sizes.h> |
71 | #include <linux/hugetlb.h> | |
aa4c3967 | 72 | #include <linux/highmem.h> |
15b71abe JA |
73 | #include <linux/namei.h> |
74 | #include <linux/fsnotify.h> | |
4840e418 | 75 | #include <linux/fadvise.h> |
3e4827b0 | 76 | #include <linux/eventpoll.h> |
7d67af2c | 77 | #include <linux/splice.h> |
b41e9852 | 78 | #include <linux/task_work.h> |
bcf5a063 | 79 | #include <linux/pagemap.h> |
0f212204 | 80 | #include <linux/io_uring.h> |
ef98eb04 | 81 | #include <linux/tracehook.h> |
2b188cc1 | 82 | |
c826bd7a DD |
83 | #define CREATE_TRACE_POINTS |
84 | #include <trace/events/io_uring.h> | |
85 | ||
2b188cc1 JA |
86 | #include <uapi/linux/io_uring.h> |
87 | ||
88 | #include "internal.h" | |
561fb04a | 89 | #include "io-wq.h" |
2b188cc1 | 90 | |
5277deaa | 91 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 92 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
4ce8ad95 | 93 | #define IORING_SQPOLL_CAP_ENTRIES_VALUE 8 |
65e19f54 | 94 | |
187f08c1 | 95 | /* only define max */ |
042b0d85 | 96 | #define IORING_MAX_FIXED_FILES (1U << 15) |
21b55dbc SG |
97 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
98 | IORING_REGISTER_LAST + IORING_OP_LAST) | |
2b188cc1 | 99 | |
187f08c1 | 100 | #define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3) |
2d091d62 PB |
101 | #define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT) |
102 | #define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1) | |
103 | ||
489809e2 PB |
104 | #define IORING_MAX_REG_BUFFERS (1U << 14) |
105 | ||
b16fed66 PB |
106 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
107 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ | |
108 | IOSQE_BUFFER_SELECT) | |
c854357b PB |
109 | #define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \ |
110 | REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS) | |
b16fed66 | 111 | |
09899b19 PB |
112 | #define IO_TCTX_REFS_CACHE_NR (1U << 10) |
113 | ||
2b188cc1 JA |
114 | struct io_uring { |
115 | u32 head ____cacheline_aligned_in_smp; | |
116 | u32 tail ____cacheline_aligned_in_smp; | |
117 | }; | |
118 | ||
1e84b97b | 119 | /* |
75b28aff HV |
120 | * This data is shared with the application through the mmap at offsets |
121 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
122 | * |
123 | * The offsets to the member fields are published through struct | |
124 | * io_sqring_offsets when calling io_uring_setup. | |
125 | */ | |
75b28aff | 126 | struct io_rings { |
1e84b97b SB |
127 | /* |
128 | * Head and tail offsets into the ring; the offsets need to be | |
129 | * masked to get valid indices. | |
130 | * | |
75b28aff HV |
131 | * The kernel controls head of the sq ring and the tail of the cq ring, |
132 | * and the application controls tail of the sq ring and the head of the | |
133 | * cq ring. | |
1e84b97b | 134 | */ |
75b28aff | 135 | struct io_uring sq, cq; |
1e84b97b | 136 | /* |
75b28aff | 137 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
138 | * ring_entries - 1) |
139 | */ | |
75b28aff HV |
140 | u32 sq_ring_mask, cq_ring_mask; |
141 | /* Ring sizes (constant, power of 2) */ | |
142 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
143 | /* |
144 | * Number of invalid entries dropped by the kernel due to | |
145 | * invalid index stored in array | |
146 | * | |
147 | * Written by the kernel, shouldn't be modified by the | |
148 | * application (i.e. get number of "new events" by comparing to | |
149 | * cached value). | |
150 | * | |
151 | * After a new SQ head value was read by the application this | |
152 | * counter includes all submissions that were dropped reaching | |
153 | * the new SQ head (and possibly more). | |
154 | */ | |
75b28aff | 155 | u32 sq_dropped; |
1e84b97b | 156 | /* |
0d9b5b3a | 157 | * Runtime SQ flags |
1e84b97b SB |
158 | * |
159 | * Written by the kernel, shouldn't be modified by the | |
160 | * application. | |
161 | * | |
162 | * The application needs a full memory barrier before checking | |
163 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
164 | */ | |
75b28aff | 165 | u32 sq_flags; |
0d9b5b3a SG |
166 | /* |
167 | * Runtime CQ flags | |
168 | * | |
169 | * Written by the application, shouldn't be modified by the | |
170 | * kernel. | |
171 | */ | |
fe7e3257 | 172 | u32 cq_flags; |
1e84b97b SB |
173 | /* |
174 | * Number of completion events lost because the queue was full; | |
175 | * this should be avoided by the application by making sure | |
0b4295b5 | 176 | * there are not more requests pending than there is space in |
1e84b97b SB |
177 | * the completion queue. |
178 | * | |
179 | * Written by the kernel, shouldn't be modified by the | |
180 | * application (i.e. get number of "new events" by comparing to | |
181 | * cached value). | |
182 | * | |
183 | * As completion events come in out of order this counter is not | |
184 | * ordered with any other data. | |
185 | */ | |
75b28aff | 186 | u32 cq_overflow; |
1e84b97b SB |
187 | /* |
188 | * Ring buffer of completion events. | |
189 | * | |
190 | * The kernel writes completion events fresh every time they are | |
191 | * produced, so the application is allowed to modify pending | |
192 | * entries. | |
193 | */ | |
75b28aff | 194 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
195 | }; |
196 | ||
45d189c6 PB |
197 | enum io_uring_cmd_flags { |
198 | IO_URING_F_NONBLOCK = 1, | |
889fca73 | 199 | IO_URING_F_COMPLETE_DEFER = 2, |
45d189c6 PB |
200 | }; |
201 | ||
edafccee JA |
202 | struct io_mapped_ubuf { |
203 | u64 ubuf; | |
4751f53d | 204 | u64 ubuf_end; |
edafccee | 205 | unsigned int nr_bvecs; |
de293938 | 206 | unsigned long acct_pages; |
41edf1a5 | 207 | struct bio_vec bvec[]; |
edafccee JA |
208 | }; |
209 | ||
50238531 BM |
210 | struct io_ring_ctx; |
211 | ||
6c2450ae PB |
212 | struct io_overflow_cqe { |
213 | struct io_uring_cqe cqe; | |
214 | struct list_head list; | |
215 | }; | |
216 | ||
a04b0ac0 PB |
217 | struct io_fixed_file { |
218 | /* file * with additional FFS_* flags */ | |
219 | unsigned long file_ptr; | |
220 | }; | |
221 | ||
269bbe5f BM |
222 | struct io_rsrc_put { |
223 | struct list_head list; | |
b60c8dce | 224 | u64 tag; |
50238531 BM |
225 | union { |
226 | void *rsrc; | |
227 | struct file *file; | |
bd54b6fe | 228 | struct io_mapped_ubuf *buf; |
50238531 | 229 | }; |
269bbe5f BM |
230 | }; |
231 | ||
aeca241b | 232 | struct io_file_table { |
042b0d85 | 233 | struct io_fixed_file *files; |
31b51510 JA |
234 | }; |
235 | ||
b895c9a6 | 236 | struct io_rsrc_node { |
05589553 XW |
237 | struct percpu_ref refs; |
238 | struct list_head node; | |
269bbe5f | 239 | struct list_head rsrc_list; |
b895c9a6 | 240 | struct io_rsrc_data *rsrc_data; |
4a38aed2 | 241 | struct llist_node llist; |
e297822b | 242 | bool done; |
05589553 XW |
243 | }; |
244 | ||
40ae0ff7 PB |
245 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
246 | ||
b895c9a6 | 247 | struct io_rsrc_data { |
05f3fb3c JA |
248 | struct io_ring_ctx *ctx; |
249 | ||
2d091d62 PB |
250 | u64 **tags; |
251 | unsigned int nr; | |
40ae0ff7 | 252 | rsrc_put_fn *do_put; |
3e942498 | 253 | atomic_t refs; |
05f3fb3c | 254 | struct completion done; |
8bad28d8 | 255 | bool quiesce; |
05f3fb3c JA |
256 | }; |
257 | ||
5a2e745d JA |
258 | struct io_buffer { |
259 | struct list_head list; | |
260 | __u64 addr; | |
d1f82808 | 261 | __u32 len; |
5a2e745d JA |
262 | __u16 bid; |
263 | }; | |
264 | ||
21b55dbc SG |
265 | struct io_restriction { |
266 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
267 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
268 | u8 sqe_flags_allowed; | |
269 | u8 sqe_flags_required; | |
7e84e1c7 | 270 | bool registered; |
21b55dbc SG |
271 | }; |
272 | ||
37d1e2e3 JA |
273 | enum { |
274 | IO_SQ_THREAD_SHOULD_STOP = 0, | |
275 | IO_SQ_THREAD_SHOULD_PARK, | |
276 | }; | |
277 | ||
534ca6d6 JA |
278 | struct io_sq_data { |
279 | refcount_t refs; | |
9e138a48 | 280 | atomic_t park_pending; |
09a6f4ef | 281 | struct mutex lock; |
69fb2131 JA |
282 | |
283 | /* ctx's that are using this sqd */ | |
284 | struct list_head ctx_list; | |
69fb2131 | 285 | |
534ca6d6 JA |
286 | struct task_struct *thread; |
287 | struct wait_queue_head wait; | |
08369246 XW |
288 | |
289 | unsigned sq_thread_idle; | |
37d1e2e3 JA |
290 | int sq_cpu; |
291 | pid_t task_pid; | |
5c2469e0 | 292 | pid_t task_tgid; |
37d1e2e3 JA |
293 | |
294 | unsigned long state; | |
37d1e2e3 | 295 | struct completion exited; |
534ca6d6 JA |
296 | }; |
297 | ||
6dd0be1e | 298 | #define IO_COMPL_BATCH 32 |
6ff119a6 | 299 | #define IO_REQ_CACHE_SIZE 32 |
bf019da7 | 300 | #define IO_REQ_ALLOC_BATCH 8 |
258b29a9 | 301 | |
a1ab7b35 PB |
302 | struct io_submit_link { |
303 | struct io_kiocb *head; | |
304 | struct io_kiocb *last; | |
305 | }; | |
306 | ||
258b29a9 PB |
307 | struct io_submit_state { |
308 | struct blk_plug plug; | |
a1ab7b35 | 309 | struct io_submit_link link; |
258b29a9 PB |
310 | |
311 | /* | |
312 | * io_kiocb alloc cache | |
313 | */ | |
bf019da7 | 314 | void *reqs[IO_REQ_CACHE_SIZE]; |
258b29a9 PB |
315 | unsigned int free_reqs; |
316 | ||
317 | bool plug_started; | |
318 | ||
319 | /* | |
320 | * Batch completion logic | |
321 | */ | |
cd0ca2e0 PB |
322 | struct io_kiocb *compl_reqs[IO_COMPL_BATCH]; |
323 | unsigned int compl_nr; | |
324 | /* inline/task_work completion list, under ->uring_lock */ | |
325 | struct list_head free_list; | |
258b29a9 | 326 | |
258b29a9 PB |
327 | unsigned int ios_left; |
328 | }; | |
329 | ||
2b188cc1 | 330 | struct io_ring_ctx { |
b52ecf8c | 331 | /* const or read-mostly hot data */ |
2b188cc1 JA |
332 | struct { |
333 | struct percpu_ref refs; | |
2b188cc1 | 334 | |
b52ecf8c | 335 | struct io_rings *rings; |
2b188cc1 | 336 | unsigned int flags; |
e1d85334 | 337 | unsigned int compat: 1; |
e1d85334 RD |
338 | unsigned int drain_next: 1; |
339 | unsigned int eventfd_async: 1; | |
21b55dbc | 340 | unsigned int restricted: 1; |
f18ee4cf | 341 | unsigned int off_timeout_used: 1; |
10c66904 | 342 | unsigned int drain_active: 1; |
b52ecf8c | 343 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 344 | |
7f1129d2 | 345 | /* submission data */ |
b52ecf8c | 346 | struct { |
0499e582 PB |
347 | struct mutex uring_lock; |
348 | ||
75b28aff HV |
349 | /* |
350 | * Ring buffer of indices into array of io_uring_sqe, which is | |
351 | * mmapped by the application using the IORING_OFF_SQES offset. | |
352 | * | |
353 | * This indirection could e.g. be used to assign fixed | |
354 | * io_uring_sqe entries to operations and only submit them to | |
355 | * the queue when needed. | |
356 | * | |
357 | * The kernel modifies neither the indices array nor the entries | |
358 | * array. | |
359 | */ | |
360 | u32 *sq_array; | |
c7af47cf | 361 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
362 | unsigned cached_sq_head; |
363 | unsigned sq_entries; | |
de0617e4 | 364 | struct list_head defer_list; |
7f1129d2 PB |
365 | |
366 | /* | |
367 | * Fixed resources fast path, should be accessed only under | |
368 | * uring_lock, and updated through io_uring_register(2) | |
369 | */ | |
370 | struct io_rsrc_node *rsrc_node; | |
371 | struct io_file_table file_table; | |
372 | unsigned nr_user_files; | |
373 | unsigned nr_user_bufs; | |
374 | struct io_mapped_ubuf **user_bufs; | |
375 | ||
376 | struct io_submit_state submit_state; | |
5262f567 | 377 | struct list_head timeout_list; |
ef9dd637 | 378 | struct list_head ltimeout_list; |
1d7bb1d5 | 379 | struct list_head cq_overflow_list; |
7f1129d2 PB |
380 | struct xarray io_buffers; |
381 | struct xarray personalities; | |
382 | u32 pers_next; | |
383 | unsigned sq_thread_idle; | |
2b188cc1 JA |
384 | } ____cacheline_aligned_in_smp; |
385 | ||
d0acdee2 PB |
386 | /* IRQ completion list, under ->completion_lock */ |
387 | struct list_head locked_free_list; | |
388 | unsigned int locked_free_nr; | |
3c1a2ead | 389 | |
7c30f36a | 390 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
534ca6d6 JA |
391 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
392 | ||
90554200 | 393 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 394 | struct list_head sqd_list; |
75b28aff | 395 | |
5ed7a37d PB |
396 | unsigned long check_cq_overflow; |
397 | ||
206aefde JA |
398 | struct { |
399 | unsigned cached_cq_tail; | |
400 | unsigned cq_entries; | |
0499e582 | 401 | struct eventfd_ctx *cq_ev_fd; |
311997b3 | 402 | struct wait_queue_head poll_wait; |
0499e582 PB |
403 | struct wait_queue_head cq_wait; |
404 | unsigned cq_extra; | |
405 | atomic_t cq_timeouts; | |
0499e582 | 406 | unsigned cq_last_tm_flush; |
206aefde | 407 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 408 | |
2b188cc1 JA |
409 | struct { |
410 | spinlock_t completion_lock; | |
e94f141b | 411 | |
89850fce JA |
412 | spinlock_t timeout_lock; |
413 | ||
def596e9 | 414 | /* |
540e32a0 | 415 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
416 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
417 | * For SQPOLL, only the single threaded io_sq_thread() will | |
418 | * manipulate the list, hence no extra locking is needed there. | |
419 | */ | |
540e32a0 | 420 | struct list_head iopoll_list; |
78076bb6 JA |
421 | struct hlist_head *cancel_hash; |
422 | unsigned cancel_hash_bits; | |
915b3dde | 423 | bool poll_multi_queue; |
2b188cc1 | 424 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 425 | |
21b55dbc | 426 | struct io_restriction restrictions; |
3c1a2ead | 427 | |
b13a8918 PB |
428 | /* slow path rsrc auxilary data, used by update/register */ |
429 | struct { | |
430 | struct io_rsrc_node *rsrc_backup_node; | |
431 | struct io_mapped_ubuf *dummy_ubuf; | |
432 | struct io_rsrc_data *file_data; | |
433 | struct io_rsrc_data *buf_data; | |
434 | ||
435 | struct delayed_work rsrc_put_work; | |
436 | struct llist_head rsrc_put_llist; | |
437 | struct list_head rsrc_ref_list; | |
438 | spinlock_t rsrc_ref_lock; | |
439 | }; | |
440 | ||
3c1a2ead | 441 | /* Keep this last, we don't need it for the fast path */ |
b986af7e PB |
442 | struct { |
443 | #if defined(CONFIG_UNIX) | |
444 | struct socket *ring_sock; | |
445 | #endif | |
446 | /* hashed buffered write serialization */ | |
447 | struct io_wq_hash *hash_map; | |
448 | ||
449 | /* Only used for accounting purposes */ | |
450 | struct user_struct *user; | |
451 | struct mm_struct *mm_account; | |
452 | ||
453 | /* ctx exit and cancelation */ | |
9011bf9a PB |
454 | struct llist_head fallback_llist; |
455 | struct delayed_work fallback_work; | |
b986af7e PB |
456 | struct work_struct exit_work; |
457 | struct list_head tctx_list; | |
458 | struct completion ref_comp; | |
e139a1ec PB |
459 | u32 iowq_limits[2]; |
460 | bool iowq_limits_set; | |
b986af7e | 461 | }; |
2b188cc1 JA |
462 | }; |
463 | ||
53e043b2 SM |
464 | struct io_uring_task { |
465 | /* submission side */ | |
09899b19 | 466 | int cached_refs; |
53e043b2 SM |
467 | struct xarray xa; |
468 | struct wait_queue_head wait; | |
ee53fb2b SM |
469 | const struct io_ring_ctx *last; |
470 | struct io_wq *io_wq; | |
53e043b2 | 471 | struct percpu_counter inflight; |
b303fe2e | 472 | atomic_t inflight_tracked; |
53e043b2 | 473 | atomic_t in_idle; |
53e043b2 SM |
474 | |
475 | spinlock_t task_lock; | |
476 | struct io_wq_work_list task_list; | |
53e043b2 | 477 | struct callback_head task_work; |
6294f368 | 478 | bool task_running; |
53e043b2 SM |
479 | }; |
480 | ||
09bb8394 JA |
481 | /* |
482 | * First field must be the file pointer in all the | |
483 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
484 | */ | |
221c5eb2 JA |
485 | struct io_poll_iocb { |
486 | struct file *file; | |
018043be | 487 | struct wait_queue_head *head; |
221c5eb2 | 488 | __poll_t events; |
392edb45 | 489 | struct wait_queue_entry wait; |
221c5eb2 JA |
490 | }; |
491 | ||
9d805892 | 492 | struct io_poll_update { |
018043be | 493 | struct file *file; |
9d805892 PB |
494 | u64 old_user_data; |
495 | u64 new_user_data; | |
496 | __poll_t events; | |
b69de288 JA |
497 | bool update_events; |
498 | bool update_user_data; | |
018043be PB |
499 | }; |
500 | ||
b5dba59e JA |
501 | struct io_close { |
502 | struct file *file; | |
b5dba59e | 503 | int fd; |
7df778be | 504 | u32 file_slot; |
b5dba59e JA |
505 | }; |
506 | ||
ad8a48ac JA |
507 | struct io_timeout_data { |
508 | struct io_kiocb *req; | |
509 | struct hrtimer timer; | |
510 | struct timespec64 ts; | |
511 | enum hrtimer_mode mode; | |
50c1df2b | 512 | u32 flags; |
ad8a48ac JA |
513 | }; |
514 | ||
8ed8d3c3 JA |
515 | struct io_accept { |
516 | struct file *file; | |
517 | struct sockaddr __user *addr; | |
518 | int __user *addr_len; | |
519 | int flags; | |
aaa4db12 | 520 | u32 file_slot; |
09952e3e | 521 | unsigned long nofile; |
8ed8d3c3 JA |
522 | }; |
523 | ||
524 | struct io_sync { | |
525 | struct file *file; | |
526 | loff_t len; | |
527 | loff_t off; | |
528 | int flags; | |
d63d1b5e | 529 | int mode; |
8ed8d3c3 JA |
530 | }; |
531 | ||
fbf23849 JA |
532 | struct io_cancel { |
533 | struct file *file; | |
534 | u64 addr; | |
535 | }; | |
536 | ||
b29472ee JA |
537 | struct io_timeout { |
538 | struct file *file; | |
bfe68a22 PB |
539 | u32 off; |
540 | u32 target_seq; | |
135fcde8 | 541 | struct list_head list; |
90cd7e42 PB |
542 | /* head of the link, used by linked timeouts only */ |
543 | struct io_kiocb *head; | |
89b263f6 JA |
544 | /* for linked completions */ |
545 | struct io_kiocb *prev; | |
b29472ee JA |
546 | }; |
547 | ||
0bdf7a2d PB |
548 | struct io_timeout_rem { |
549 | struct file *file; | |
550 | u64 addr; | |
9c8e11b3 PB |
551 | |
552 | /* timeout update */ | |
553 | struct timespec64 ts; | |
554 | u32 flags; | |
f1042b6c | 555 | bool ltimeout; |
0bdf7a2d PB |
556 | }; |
557 | ||
9adbd45d JA |
558 | struct io_rw { |
559 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
560 | struct kiocb kiocb; | |
561 | u64 addr; | |
562 | u64 len; | |
563 | }; | |
564 | ||
3fbb51c1 JA |
565 | struct io_connect { |
566 | struct file *file; | |
567 | struct sockaddr __user *addr; | |
568 | int addr_len; | |
569 | }; | |
570 | ||
e47293fd JA |
571 | struct io_sr_msg { |
572 | struct file *file; | |
fddaface | 573 | union { |
4af3417a PB |
574 | struct compat_msghdr __user *umsg_compat; |
575 | struct user_msghdr __user *umsg; | |
576 | void __user *buf; | |
fddaface | 577 | }; |
e47293fd | 578 | int msg_flags; |
bcda7baa | 579 | int bgid; |
fddaface | 580 | size_t len; |
bcda7baa | 581 | struct io_buffer *kbuf; |
e47293fd JA |
582 | }; |
583 | ||
15b71abe JA |
584 | struct io_open { |
585 | struct file *file; | |
586 | int dfd; | |
b9445598 | 587 | u32 file_slot; |
15b71abe | 588 | struct filename *filename; |
c12cedf2 | 589 | struct open_how how; |
4022e7af | 590 | unsigned long nofile; |
15b71abe JA |
591 | }; |
592 | ||
269bbe5f | 593 | struct io_rsrc_update { |
05f3fb3c JA |
594 | struct file *file; |
595 | u64 arg; | |
596 | u32 nr_args; | |
597 | u32 offset; | |
598 | }; | |
599 | ||
4840e418 JA |
600 | struct io_fadvise { |
601 | struct file *file; | |
602 | u64 offset; | |
603 | u32 len; | |
604 | u32 advice; | |
605 | }; | |
606 | ||
c1ca757b JA |
607 | struct io_madvise { |
608 | struct file *file; | |
609 | u64 addr; | |
610 | u32 len; | |
611 | u32 advice; | |
612 | }; | |
613 | ||
3e4827b0 JA |
614 | struct io_epoll { |
615 | struct file *file; | |
616 | int epfd; | |
617 | int op; | |
618 | int fd; | |
619 | struct epoll_event event; | |
e47293fd JA |
620 | }; |
621 | ||
7d67af2c PB |
622 | struct io_splice { |
623 | struct file *file_out; | |
7d67af2c PB |
624 | loff_t off_out; |
625 | loff_t off_in; | |
626 | u64 len; | |
dfadddfc | 627 | int splice_fd_in; |
7d67af2c PB |
628 | unsigned int flags; |
629 | }; | |
630 | ||
ddf0322d JA |
631 | struct io_provide_buf { |
632 | struct file *file; | |
633 | __u64 addr; | |
38134ada | 634 | __u32 len; |
ddf0322d JA |
635 | __u32 bgid; |
636 | __u16 nbufs; | |
637 | __u16 bid; | |
638 | }; | |
639 | ||
1d9e1288 BM |
640 | struct io_statx { |
641 | struct file *file; | |
642 | int dfd; | |
643 | unsigned int mask; | |
644 | unsigned int flags; | |
e62753e4 | 645 | const char __user *filename; |
1d9e1288 BM |
646 | struct statx __user *buffer; |
647 | }; | |
648 | ||
36f4fa68 JA |
649 | struct io_shutdown { |
650 | struct file *file; | |
651 | int how; | |
652 | }; | |
653 | ||
80a261fd JA |
654 | struct io_rename { |
655 | struct file *file; | |
656 | int old_dfd; | |
657 | int new_dfd; | |
658 | struct filename *oldpath; | |
659 | struct filename *newpath; | |
660 | int flags; | |
661 | }; | |
662 | ||
14a1143b JA |
663 | struct io_unlink { |
664 | struct file *file; | |
665 | int dfd; | |
666 | int flags; | |
667 | struct filename *filename; | |
668 | }; | |
669 | ||
e34a02dc DK |
670 | struct io_mkdir { |
671 | struct file *file; | |
672 | int dfd; | |
673 | umode_t mode; | |
674 | struct filename *filename; | |
675 | }; | |
676 | ||
7a8721f8 DK |
677 | struct io_symlink { |
678 | struct file *file; | |
679 | int new_dfd; | |
680 | struct filename *oldpath; | |
681 | struct filename *newpath; | |
682 | }; | |
683 | ||
cf30da90 DK |
684 | struct io_hardlink { |
685 | struct file *file; | |
686 | int old_dfd; | |
687 | int new_dfd; | |
688 | struct filename *oldpath; | |
689 | struct filename *newpath; | |
690 | int flags; | |
691 | }; | |
692 | ||
3ca405eb PB |
693 | struct io_completion { |
694 | struct file *file; | |
8c3f9cd1 | 695 | u32 cflags; |
3ca405eb PB |
696 | }; |
697 | ||
f499a021 JA |
698 | struct io_async_connect { |
699 | struct sockaddr_storage address; | |
700 | }; | |
701 | ||
03b1230c JA |
702 | struct io_async_msghdr { |
703 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
704 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
705 | struct iovec *free_iov; | |
03b1230c JA |
706 | struct sockaddr __user *uaddr; |
707 | struct msghdr msg; | |
b537916c | 708 | struct sockaddr_storage addr; |
03b1230c JA |
709 | }; |
710 | ||
f67676d1 JA |
711 | struct io_async_rw { |
712 | struct iovec fast_iov[UIO_FASTIOV]; | |
ff6165b2 JA |
713 | const struct iovec *free_iovec; |
714 | struct iov_iter iter; | |
cd658695 | 715 | struct iov_iter_state iter_state; |
227c0c96 | 716 | size_t bytes_done; |
bcf5a063 | 717 | struct wait_page_queue wpq; |
f67676d1 JA |
718 | }; |
719 | ||
6b47ee6e PB |
720 | enum { |
721 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
722 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
723 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
724 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
725 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 726 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 727 | |
dddca226 | 728 | /* first byte is taken by user flags, shift it to not overlap */ |
93d2bcd2 | 729 | REQ_F_FAIL_BIT = 8, |
6b47ee6e PB |
730 | REQ_F_INFLIGHT_BIT, |
731 | REQ_F_CUR_POS_BIT, | |
732 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 733 | REQ_F_LINK_TIMEOUT_BIT, |
99bc4c38 | 734 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 735 | REQ_F_POLLED_BIT, |
bcda7baa | 736 | REQ_F_BUFFER_SELECTED_BIT, |
e342c807 | 737 | REQ_F_COMPLETE_INLINE_BIT, |
230d50d4 | 738 | REQ_F_REISSUE_BIT, |
b8e64b53 | 739 | REQ_F_CREDS_BIT, |
20e60a38 | 740 | REQ_F_REFCOUNT_BIT, |
4d13d1a4 | 741 | REQ_F_ARM_LTIMEOUT_BIT, |
7b29f92d | 742 | /* keep async read/write and isreg together and in order */ |
b191e2df PB |
743 | REQ_F_NOWAIT_READ_BIT, |
744 | REQ_F_NOWAIT_WRITE_BIT, | |
7b29f92d | 745 | REQ_F_ISREG_BIT, |
84557871 JA |
746 | |
747 | /* not a real bit, just to check we're not overflowing the space */ | |
748 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
749 | }; |
750 | ||
751 | enum { | |
752 | /* ctx owns file */ | |
753 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
754 | /* drain existing IO first */ | |
755 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
756 | /* linked sqes */ | |
757 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
758 | /* doesn't sever on completion < 0 */ | |
759 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
760 | /* IOSQE_ASYNC */ | |
761 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
762 | /* IOSQE_BUFFER_SELECT */ |
763 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 764 | |
6b47ee6e | 765 | /* fail rest of links */ |
93d2bcd2 | 766 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
b05a1bcd | 767 | /* on inflight list, should be cancelled and waited on exit reliably */ |
6b47ee6e PB |
768 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
769 | /* read/write uses file position */ | |
770 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
771 | /* must not punt to workers */ | |
772 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 773 | /* has or had linked timeout */ |
6b47ee6e | 774 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
99bc4c38 PB |
775 | /* needs cleanup */ |
776 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
777 | /* already went through poll handler */ |
778 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
779 | /* buffer already selected */ |
780 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
e342c807 PB |
781 | /* completion is deferred through io_comp_state */ |
782 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
230d50d4 JA |
783 | /* caller should reissue async */ |
784 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), | |
7b29f92d | 785 | /* supports async reads */ |
b191e2df | 786 | REQ_F_NOWAIT_READ = BIT(REQ_F_NOWAIT_READ_BIT), |
7b29f92d | 787 | /* supports async writes */ |
b191e2df | 788 | REQ_F_NOWAIT_WRITE = BIT(REQ_F_NOWAIT_WRITE_BIT), |
7b29f92d JA |
789 | /* regular file */ |
790 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
b8e64b53 PB |
791 | /* has creds assigned */ |
792 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), | |
20e60a38 PB |
793 | /* skip refcounting if not set */ |
794 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), | |
4d13d1a4 PB |
795 | /* there is a linked timeout that has to be armed */ |
796 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT), | |
d7718a9d JA |
797 | }; |
798 | ||
799 | struct async_poll { | |
800 | struct io_poll_iocb poll; | |
807abcb0 | 801 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
802 | }; |
803 | ||
f237c30a | 804 | typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked); |
5b0a6acc | 805 | |
7cbf1722 | 806 | struct io_task_work { |
5b0a6acc PB |
807 | union { |
808 | struct io_wq_work_node node; | |
809 | struct llist_node fallback_node; | |
810 | }; | |
811 | io_req_tw_func_t func; | |
7cbf1722 JA |
812 | }; |
813 | ||
992da01a PB |
814 | enum { |
815 | IORING_RSRC_FILE = 0, | |
816 | IORING_RSRC_BUFFER = 1, | |
817 | }; | |
818 | ||
09bb8394 JA |
819 | /* |
820 | * NOTE! Each of the iocb union members has the file pointer | |
821 | * as the first entry in their struct definition. So you can | |
822 | * access the file pointer through any of the sub-structs, | |
823 | * or directly as just 'ki_filp' in this struct. | |
824 | */ | |
2b188cc1 | 825 | struct io_kiocb { |
221c5eb2 | 826 | union { |
09bb8394 | 827 | struct file *file; |
9adbd45d | 828 | struct io_rw rw; |
221c5eb2 | 829 | struct io_poll_iocb poll; |
9d805892 | 830 | struct io_poll_update poll_update; |
8ed8d3c3 JA |
831 | struct io_accept accept; |
832 | struct io_sync sync; | |
fbf23849 | 833 | struct io_cancel cancel; |
b29472ee | 834 | struct io_timeout timeout; |
0bdf7a2d | 835 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 836 | struct io_connect connect; |
e47293fd | 837 | struct io_sr_msg sr_msg; |
15b71abe | 838 | struct io_open open; |
b5dba59e | 839 | struct io_close close; |
269bbe5f | 840 | struct io_rsrc_update rsrc_update; |
4840e418 | 841 | struct io_fadvise fadvise; |
c1ca757b | 842 | struct io_madvise madvise; |
3e4827b0 | 843 | struct io_epoll epoll; |
7d67af2c | 844 | struct io_splice splice; |
ddf0322d | 845 | struct io_provide_buf pbuf; |
1d9e1288 | 846 | struct io_statx statx; |
36f4fa68 | 847 | struct io_shutdown shutdown; |
80a261fd | 848 | struct io_rename rename; |
14a1143b | 849 | struct io_unlink unlink; |
e34a02dc | 850 | struct io_mkdir mkdir; |
7a8721f8 | 851 | struct io_symlink symlink; |
cf30da90 | 852 | struct io_hardlink hardlink; |
3ca405eb PB |
853 | /* use only after cleaning per-op data, see io_clean_op() */ |
854 | struct io_completion compl; | |
221c5eb2 | 855 | }; |
2b188cc1 | 856 | |
e8c2bc1f JA |
857 | /* opcode allocated if it needs to store data for async defer */ |
858 | void *async_data; | |
d625c6ee | 859 | u8 opcode; |
65a6543d XW |
860 | /* polled IO has completed */ |
861 | u8 iopoll_completed; | |
2b188cc1 | 862 | |
4f4eeba8 | 863 | u16 buf_index; |
9cf7c104 | 864 | u32 result; |
4f4eeba8 | 865 | |
010e8e6b PB |
866 | struct io_ring_ctx *ctx; |
867 | unsigned int flags; | |
abc54d63 | 868 | atomic_t refs; |
010e8e6b PB |
869 | struct task_struct *task; |
870 | u64 user_data; | |
d7718a9d | 871 | |
f2f87370 | 872 | struct io_kiocb *link; |
269bbe5f | 873 | struct percpu_ref *fixed_rsrc_refs; |
fcb323cc | 874 | |
b303fe2e | 875 | /* used with ctx->iopoll_list with reads/writes */ |
010e8e6b | 876 | struct list_head inflight_entry; |
5b0a6acc | 877 | struct io_task_work io_task_work; |
010e8e6b PB |
878 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
879 | struct hlist_node hash_node; | |
880 | struct async_poll *apoll; | |
881 | struct io_wq_work work; | |
fe7e3257 | 882 | const struct cred *creds; |
c10d1f98 | 883 | |
eae071c9 PB |
884 | /* store used ubuf, so we can prevent reloading */ |
885 | struct io_mapped_ubuf *imu; | |
350804e3 PB |
886 | /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */ |
887 | struct io_buffer *kbuf; | |
888 | atomic_t poll_refs; | |
2b188cc1 | 889 | }; |
05589553 | 890 | |
13bf43f5 PB |
891 | struct io_tctx_node { |
892 | struct list_head ctx_node; | |
893 | struct task_struct *task; | |
13bf43f5 PB |
894 | struct io_ring_ctx *ctx; |
895 | }; | |
896 | ||
27dc8338 PB |
897 | struct io_defer_entry { |
898 | struct list_head list; | |
899 | struct io_kiocb *req; | |
9cf7c104 | 900 | u32 seq; |
2b188cc1 JA |
901 | }; |
902 | ||
d3656344 | 903 | struct io_op_def { |
d3656344 JA |
904 | /* needs req->file assigned */ |
905 | unsigned needs_file : 1; | |
d3656344 JA |
906 | /* hash wq insertion if file is a regular file */ |
907 | unsigned hash_reg_file : 1; | |
908 | /* unbound wq insertion if file is a non-regular file */ | |
909 | unsigned unbound_nonreg_file : 1; | |
66f4af93 JA |
910 | /* opcode is not supported by this kernel */ |
911 | unsigned not_supported : 1; | |
8a72758c JA |
912 | /* set if opcode supports polled "wait" */ |
913 | unsigned pollin : 1; | |
914 | unsigned pollout : 1; | |
bcda7baa JA |
915 | /* op supports buffer selection */ |
916 | unsigned buffer_select : 1; | |
26f0505a PB |
917 | /* do prep async if is going to be punted */ |
918 | unsigned needs_async_setup : 1; | |
27926b68 JA |
919 | /* should block plug */ |
920 | unsigned plug : 1; | |
e8c2bc1f JA |
921 | /* size of async data needed, if any */ |
922 | unsigned short async_size; | |
d3656344 JA |
923 | }; |
924 | ||
0918682b | 925 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
926 | [IORING_OP_NOP] = {}, |
927 | [IORING_OP_READV] = { | |
d3656344 JA |
928 | .needs_file = 1, |
929 | .unbound_nonreg_file = 1, | |
8a72758c | 930 | .pollin = 1, |
4d954c25 | 931 | .buffer_select = 1, |
26f0505a | 932 | .needs_async_setup = 1, |
27926b68 | 933 | .plug = 1, |
e8c2bc1f | 934 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 935 | }, |
0463b6c5 | 936 | [IORING_OP_WRITEV] = { |
d3656344 JA |
937 | .needs_file = 1, |
938 | .hash_reg_file = 1, | |
939 | .unbound_nonreg_file = 1, | |
8a72758c | 940 | .pollout = 1, |
26f0505a | 941 | .needs_async_setup = 1, |
27926b68 | 942 | .plug = 1, |
e8c2bc1f | 943 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 944 | }, |
0463b6c5 | 945 | [IORING_OP_FSYNC] = { |
d3656344 JA |
946 | .needs_file = 1, |
947 | }, | |
0463b6c5 | 948 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
949 | .needs_file = 1, |
950 | .unbound_nonreg_file = 1, | |
8a72758c | 951 | .pollin = 1, |
27926b68 | 952 | .plug = 1, |
e8c2bc1f | 953 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 954 | }, |
0463b6c5 | 955 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
956 | .needs_file = 1, |
957 | .hash_reg_file = 1, | |
958 | .unbound_nonreg_file = 1, | |
8a72758c | 959 | .pollout = 1, |
27926b68 | 960 | .plug = 1, |
e8c2bc1f | 961 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 962 | }, |
0463b6c5 | 963 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
964 | .needs_file = 1, |
965 | .unbound_nonreg_file = 1, | |
966 | }, | |
0463b6c5 PB |
967 | [IORING_OP_POLL_REMOVE] = {}, |
968 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 JA |
969 | .needs_file = 1, |
970 | }, | |
0463b6c5 | 971 | [IORING_OP_SENDMSG] = { |
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_msghdr), |
d3656344 | 977 | }, |
0463b6c5 | 978 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
979 | .needs_file = 1, |
980 | .unbound_nonreg_file = 1, | |
8a72758c | 981 | .pollin = 1, |
52de1fe1 | 982 | .buffer_select = 1, |
26f0505a | 983 | .needs_async_setup = 1, |
e8c2bc1f | 984 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 985 | }, |
0463b6c5 | 986 | [IORING_OP_TIMEOUT] = { |
e8c2bc1f | 987 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 988 | }, |
9c8e11b3 PB |
989 | [IORING_OP_TIMEOUT_REMOVE] = { |
990 | /* used by timeout updates' prep() */ | |
9c8e11b3 | 991 | }, |
0463b6c5 | 992 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
993 | .needs_file = 1, |
994 | .unbound_nonreg_file = 1, | |
8a72758c | 995 | .pollin = 1, |
d3656344 | 996 | }, |
0463b6c5 PB |
997 | [IORING_OP_ASYNC_CANCEL] = {}, |
998 | [IORING_OP_LINK_TIMEOUT] = { | |
e8c2bc1f | 999 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 1000 | }, |
0463b6c5 | 1001 | [IORING_OP_CONNECT] = { |
d3656344 JA |
1002 | .needs_file = 1, |
1003 | .unbound_nonreg_file = 1, | |
8a72758c | 1004 | .pollout = 1, |
26f0505a | 1005 | .needs_async_setup = 1, |
e8c2bc1f | 1006 | .async_size = sizeof(struct io_async_connect), |
d3656344 | 1007 | }, |
0463b6c5 | 1008 | [IORING_OP_FALLOCATE] = { |
d3656344 | 1009 | .needs_file = 1, |
d3656344 | 1010 | }, |
44526bed JA |
1011 | [IORING_OP_OPENAT] = {}, |
1012 | [IORING_OP_CLOSE] = {}, | |
1013 | [IORING_OP_FILES_UPDATE] = {}, | |
1014 | [IORING_OP_STATX] = {}, | |
0463b6c5 | 1015 | [IORING_OP_READ] = { |
3a6820f2 JA |
1016 | .needs_file = 1, |
1017 | .unbound_nonreg_file = 1, | |
8a72758c | 1018 | .pollin = 1, |
bcda7baa | 1019 | .buffer_select = 1, |
27926b68 | 1020 | .plug = 1, |
e8c2bc1f | 1021 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1022 | }, |
0463b6c5 | 1023 | [IORING_OP_WRITE] = { |
3a6820f2 | 1024 | .needs_file = 1, |
7b3188e7 | 1025 | .hash_reg_file = 1, |
3a6820f2 | 1026 | .unbound_nonreg_file = 1, |
8a72758c | 1027 | .pollout = 1, |
27926b68 | 1028 | .plug = 1, |
e8c2bc1f | 1029 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1030 | }, |
0463b6c5 | 1031 | [IORING_OP_FADVISE] = { |
4840e418 | 1032 | .needs_file = 1, |
c1ca757b | 1033 | }, |
44526bed | 1034 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 1035 | [IORING_OP_SEND] = { |
fddaface JA |
1036 | .needs_file = 1, |
1037 | .unbound_nonreg_file = 1, | |
8a72758c | 1038 | .pollout = 1, |
fddaface | 1039 | }, |
0463b6c5 | 1040 | [IORING_OP_RECV] = { |
fddaface JA |
1041 | .needs_file = 1, |
1042 | .unbound_nonreg_file = 1, | |
8a72758c | 1043 | .pollin = 1, |
bcda7baa | 1044 | .buffer_select = 1, |
fddaface | 1045 | }, |
0463b6c5 | 1046 | [IORING_OP_OPENAT2] = { |
cebdb986 | 1047 | }, |
3e4827b0 JA |
1048 | [IORING_OP_EPOLL_CTL] = { |
1049 | .unbound_nonreg_file = 1, | |
3e4827b0 | 1050 | }, |
7d67af2c PB |
1051 | [IORING_OP_SPLICE] = { |
1052 | .needs_file = 1, | |
1053 | .hash_reg_file = 1, | |
1054 | .unbound_nonreg_file = 1, | |
ddf0322d JA |
1055 | }, |
1056 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 1057 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
1058 | [IORING_OP_TEE] = { |
1059 | .needs_file = 1, | |
1060 | .hash_reg_file = 1, | |
1061 | .unbound_nonreg_file = 1, | |
1062 | }, | |
36f4fa68 JA |
1063 | [IORING_OP_SHUTDOWN] = { |
1064 | .needs_file = 1, | |
1065 | }, | |
44526bed JA |
1066 | [IORING_OP_RENAMEAT] = {}, |
1067 | [IORING_OP_UNLINKAT] = {}, | |
e34a02dc | 1068 | [IORING_OP_MKDIRAT] = {}, |
7a8721f8 | 1069 | [IORING_OP_SYMLINKAT] = {}, |
cf30da90 | 1070 | [IORING_OP_LINKAT] = {}, |
d3656344 JA |
1071 | }; |
1072 | ||
0756a869 PB |
1073 | /* requests with any of those set should undergo io_disarm_next() */ |
1074 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) | |
1075 | ||
7a612350 | 1076 | static bool io_disarm_next(struct io_kiocb *req); |
eef51daa | 1077 | static void io_uring_del_tctx_node(unsigned long index); |
9936c7c2 PB |
1078 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1079 | struct task_struct *task, | |
3dd0c97a | 1080 | bool cancel_all); |
78cc687b | 1081 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); |
1ffc5422 | 1082 | |
142f794e PB |
1083 | static void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags); |
1084 | ||
ec9c02ad | 1085 | static void io_put_req(struct io_kiocb *req); |
91c2f697 | 1086 | static void io_put_req_deferred(struct io_kiocb *req); |
c7dae4ba | 1087 | static void io_dismantle_req(struct io_kiocb *req); |
94ae5e77 | 1088 | static void io_queue_linked_timeout(struct io_kiocb *req); |
fdecb662 | 1089 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 1090 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 | 1091 | unsigned nr_args); |
68fb8979 | 1092 | static void io_clean_op(struct io_kiocb *req); |
ac177053 | 1093 | static struct file *io_file_get(struct io_ring_ctx *ctx, |
8371adf5 | 1094 | struct io_kiocb *req, int fd, bool fixed); |
c5eef2b9 | 1095 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1096 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1097 | |
907d1df3 | 1098 | static void io_req_task_queue(struct io_kiocb *req); |
2a2758f2 | 1099 | static void io_submit_flush_completions(struct io_ring_ctx *ctx); |
179ae0d1 | 1100 | static int io_req_prep_async(struct io_kiocb *req); |
de0617e4 | 1101 | |
b9445598 PB |
1102 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
1103 | unsigned int issue_flags, u32 slot_index); | |
7df778be PB |
1104 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags); |
1105 | ||
f1042b6c | 1106 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer); |
b9445598 | 1107 | |
2b188cc1 JA |
1108 | static struct kmem_cache *req_cachep; |
1109 | ||
0918682b | 1110 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1111 | |
1112 | struct sock *io_uring_get_socket(struct file *file) | |
1113 | { | |
1114 | #if defined(CONFIG_UNIX) | |
1115 | if (file->f_op == &io_uring_fops) { | |
1116 | struct io_ring_ctx *ctx = file->private_data; | |
1117 | ||
1118 | return ctx->ring_sock->sk; | |
1119 | } | |
1120 | #endif | |
1121 | return NULL; | |
1122 | } | |
1123 | EXPORT_SYMBOL(io_uring_get_socket); | |
1124 | ||
f237c30a PB |
1125 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
1126 | { | |
1127 | if (!*locked) { | |
1128 | mutex_lock(&ctx->uring_lock); | |
1129 | *locked = true; | |
1130 | } | |
1131 | } | |
1132 | ||
f2f87370 PB |
1133 | #define io_for_each_link(pos, head) \ |
1134 | for (pos = (head); pos; pos = pos->link) | |
1135 | ||
21c843d5 PB |
1136 | /* |
1137 | * Shamelessly stolen from the mm implementation of page reference checking, | |
1138 | * see commit f958d7b528b1 for details. | |
1139 | */ | |
1140 | #define req_ref_zero_or_close_to_overflow(req) \ | |
1141 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) | |
1142 | ||
1143 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) | |
1144 | { | |
20e60a38 | 1145 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1146 | return atomic_inc_not_zero(&req->refs); |
1147 | } | |
1148 | ||
21c843d5 PB |
1149 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
1150 | { | |
20e60a38 PB |
1151 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
1152 | return true; | |
1153 | ||
21c843d5 PB |
1154 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1155 | return atomic_dec_and_test(&req->refs); | |
1156 | } | |
1157 | ||
1158 | static inline void req_ref_put(struct io_kiocb *req) | |
1159 | { | |
20e60a38 | 1160 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1161 | WARN_ON_ONCE(req_ref_put_and_test(req)); |
1162 | } | |
1163 | ||
1164 | static inline void req_ref_get(struct io_kiocb *req) | |
1165 | { | |
20e60a38 | 1166 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1167 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1168 | atomic_inc(&req->refs); | |
1169 | } | |
1170 | ||
48dcd38d | 1171 | static inline void __io_req_set_refcount(struct io_kiocb *req, int nr) |
20e60a38 PB |
1172 | { |
1173 | if (!(req->flags & REQ_F_REFCOUNT)) { | |
1174 | req->flags |= REQ_F_REFCOUNT; | |
48dcd38d | 1175 | atomic_set(&req->refs, nr); |
20e60a38 PB |
1176 | } |
1177 | } | |
1178 | ||
48dcd38d PB |
1179 | static inline void io_req_set_refcount(struct io_kiocb *req) |
1180 | { | |
1181 | __io_req_set_refcount(req, 1); | |
1182 | } | |
1183 | ||
b895c9a6 | 1184 | static inline void io_req_set_rsrc_node(struct io_kiocb *req) |
36f72fe2 PB |
1185 | { |
1186 | struct io_ring_ctx *ctx = req->ctx; | |
1187 | ||
269bbe5f | 1188 | if (!req->fixed_rsrc_refs) { |
a7f0ed5a | 1189 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
269bbe5f | 1190 | percpu_ref_get(req->fixed_rsrc_refs); |
36f72fe2 PB |
1191 | } |
1192 | } | |
1193 | ||
f70865db PB |
1194 | static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
1195 | { | |
1196 | bool got = percpu_ref_tryget(ref); | |
1197 | ||
1198 | /* already at zero, wait for ->release() */ | |
1199 | if (!got) | |
1200 | wait_for_completion(compl); | |
1201 | percpu_ref_resurrect(ref); | |
1202 | if (got) | |
1203 | percpu_ref_put(ref); | |
1204 | } | |
1205 | ||
3dd0c97a PB |
1206 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
1207 | bool cancel_all) | |
f0baed8e | 1208 | __must_hold(&req->ctx->timeout_lock) |
08d23634 PB |
1209 | { |
1210 | struct io_kiocb *req; | |
1211 | ||
68207680 | 1212 | if (task && head->task != task) |
08d23634 | 1213 | return false; |
3dd0c97a | 1214 | if (cancel_all) |
08d23634 PB |
1215 | return true; |
1216 | ||
1217 | io_for_each_link(req, head) { | |
b05a1bcd | 1218 | if (req->flags & REQ_F_INFLIGHT) |
02a13674 | 1219 | return true; |
08d23634 PB |
1220 | } |
1221 | return false; | |
1222 | } | |
1223 | ||
f0baed8e PB |
1224 | static bool io_match_linked(struct io_kiocb *head) |
1225 | { | |
1226 | struct io_kiocb *req; | |
1227 | ||
1228 | io_for_each_link(req, head) { | |
1229 | if (req->flags & REQ_F_INFLIGHT) | |
1230 | return true; | |
1231 | } | |
1232 | return false; | |
1233 | } | |
1234 | ||
1235 | /* | |
1236 | * As io_match_task() but protected against racing with linked timeouts. | |
1237 | * User must not hold timeout_lock. | |
1238 | */ | |
1239 | static bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task, | |
1240 | bool cancel_all) | |
1241 | { | |
1242 | bool matched; | |
1243 | ||
1244 | if (task && head->task != task) | |
1245 | return false; | |
1246 | if (cancel_all) | |
1247 | return true; | |
1248 | ||
1249 | if (head->flags & REQ_F_LINK_TIMEOUT) { | |
1250 | struct io_ring_ctx *ctx = head->ctx; | |
1251 | ||
1252 | /* protect against races with linked timeouts */ | |
1253 | spin_lock_irq(&ctx->timeout_lock); | |
1254 | matched = io_match_linked(head); | |
1255 | spin_unlock_irq(&ctx->timeout_lock); | |
1256 | } else { | |
1257 | matched = io_match_linked(head); | |
1258 | } | |
1259 | return matched; | |
1260 | } | |
1261 | ||
93d2bcd2 | 1262 | static inline void req_set_fail(struct io_kiocb *req) |
c40f6379 | 1263 | { |
93d2bcd2 | 1264 | req->flags |= REQ_F_FAIL; |
c40f6379 | 1265 | } |
4a38aed2 | 1266 | |
a8295b98 HX |
1267 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
1268 | { | |
1269 | req_set_fail(req); | |
1270 | req->result = res; | |
1271 | } | |
1272 | ||
2b188cc1 JA |
1273 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
1274 | { | |
1275 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1276 | ||
0f158b4c | 1277 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1278 | } |
1279 | ||
8eb7e2d0 PB |
1280 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1281 | { | |
1282 | return !req->timeout.off; | |
1283 | } | |
1284 | ||
f56165e6 PB |
1285 | static void io_fallback_req_func(struct work_struct *work) |
1286 | { | |
1287 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, | |
1288 | fallback_work.work); | |
1289 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); | |
1290 | struct io_kiocb *req, *tmp; | |
f237c30a | 1291 | bool locked = false; |
f56165e6 PB |
1292 | |
1293 | percpu_ref_get(&ctx->refs); | |
1294 | llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node) | |
f237c30a | 1295 | req->io_task_work.func(req, &locked); |
5636c00d | 1296 | |
f237c30a PB |
1297 | if (locked) { |
1298 | if (ctx->submit_state.compl_nr) | |
1299 | io_submit_flush_completions(ctx); | |
1300 | mutex_unlock(&ctx->uring_lock); | |
1301 | } | |
f56165e6 | 1302 | percpu_ref_put(&ctx->refs); |
f237c30a | 1303 | |
f56165e6 PB |
1304 | } |
1305 | ||
2b188cc1 JA |
1306 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
1307 | { | |
1308 | struct io_ring_ctx *ctx; | |
78076bb6 | 1309 | int hash_bits; |
2b188cc1 JA |
1310 | |
1311 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1312 | if (!ctx) | |
1313 | return NULL; | |
1314 | ||
78076bb6 JA |
1315 | /* |
1316 | * Use 5 bits less than the max cq entries, that should give us around | |
1317 | * 32 entries per hash list if totally full and uniformly spread. | |
1318 | */ | |
1319 | hash_bits = ilog2(p->cq_entries); | |
1320 | hash_bits -= 5; | |
1321 | if (hash_bits <= 0) | |
1322 | hash_bits = 1; | |
1323 | ctx->cancel_hash_bits = hash_bits; | |
1324 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1325 | GFP_KERNEL); | |
1326 | if (!ctx->cancel_hash) | |
1327 | goto err; | |
1328 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1329 | ||
6224843d PB |
1330 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
1331 | if (!ctx->dummy_ubuf) | |
1332 | goto err; | |
1333 | /* set invalid range, so io_import_fixed() fails meeting it */ | |
1334 | ctx->dummy_ubuf->ubuf = -1UL; | |
1335 | ||
21482896 | 1336 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1337 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1338 | goto err; | |
2b188cc1 JA |
1339 | |
1340 | ctx->flags = p->flags; | |
90554200 | 1341 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1342 | INIT_LIST_HEAD(&ctx->sqd_list); |
311997b3 | 1343 | init_waitqueue_head(&ctx->poll_wait); |
1d7bb1d5 | 1344 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c | 1345 | init_completion(&ctx->ref_comp); |
9e15c3a0 | 1346 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
61cf9370 | 1347 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
2b188cc1 | 1348 | mutex_init(&ctx->uring_lock); |
311997b3 | 1349 | init_waitqueue_head(&ctx->cq_wait); |
2b188cc1 | 1350 | spin_lock_init(&ctx->completion_lock); |
89850fce | 1351 | spin_lock_init(&ctx->timeout_lock); |
540e32a0 | 1352 | INIT_LIST_HEAD(&ctx->iopoll_list); |
de0617e4 | 1353 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1354 | INIT_LIST_HEAD(&ctx->timeout_list); |
ef9dd637 | 1355 | INIT_LIST_HEAD(&ctx->ltimeout_list); |
d67d2263 BM |
1356 | spin_lock_init(&ctx->rsrc_ref_lock); |
1357 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1358 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1359 | init_llist_head(&ctx->rsrc_put_llist); | |
13bf43f5 | 1360 | INIT_LIST_HEAD(&ctx->tctx_list); |
cd0ca2e0 | 1361 | INIT_LIST_HEAD(&ctx->submit_state.free_list); |
d0acdee2 | 1362 | INIT_LIST_HEAD(&ctx->locked_free_list); |
9011bf9a | 1363 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
2b188cc1 | 1364 | return ctx; |
206aefde | 1365 | err: |
6224843d | 1366 | kfree(ctx->dummy_ubuf); |
78076bb6 | 1367 | kfree(ctx->cancel_hash); |
206aefde JA |
1368 | kfree(ctx); |
1369 | return NULL; | |
2b188cc1 JA |
1370 | } |
1371 | ||
8f6ed49a PB |
1372 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
1373 | { | |
1374 | struct io_rings *r = ctx->rings; | |
1375 | ||
1376 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); | |
1377 | ctx->cq_extra--; | |
1378 | } | |
1379 | ||
9cf7c104 | 1380 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1381 | { |
2bc9930e JA |
1382 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1383 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1384 | |
8f6ed49a | 1385 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
2bc9930e | 1386 | } |
de0617e4 | 1387 | |
9d858b21 | 1388 | return false; |
de0617e4 JA |
1389 | } |
1390 | ||
c97d8a0f PB |
1391 | #define FFS_ASYNC_READ 0x1UL |
1392 | #define FFS_ASYNC_WRITE 0x2UL | |
1393 | #ifdef CONFIG_64BIT | |
1394 | #define FFS_ISREG 0x4UL | |
1395 | #else | |
1396 | #define FFS_ISREG 0x0UL | |
1397 | #endif | |
1398 | #define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG) | |
1399 | ||
1400 | static inline bool io_req_ffs_set(struct io_kiocb *req) | |
1401 | { | |
1402 | return IS_ENABLED(CONFIG_64BIT) && (req->flags & REQ_F_FIXED_FILE); | |
1403 | } | |
1404 | ||
ce3d5aae PB |
1405 | static void io_req_track_inflight(struct io_kiocb *req) |
1406 | { | |
ce3d5aae | 1407 | if (!(req->flags & REQ_F_INFLIGHT)) { |
ce3d5aae | 1408 | req->flags |= REQ_F_INFLIGHT; |
b303fe2e | 1409 | atomic_inc(¤t->io_uring->inflight_tracked); |
ce3d5aae PB |
1410 | } |
1411 | } | |
1412 | ||
fd08e530 PB |
1413 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
1414 | { | |
906c6caa PB |
1415 | if (WARN_ON_ONCE(!req->link)) |
1416 | return NULL; | |
1417 | ||
4d13d1a4 PB |
1418 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
1419 | req->flags |= REQ_F_LINK_TIMEOUT; | |
fd08e530 PB |
1420 | |
1421 | /* linked timeouts should have two refs once prep'ed */ | |
48dcd38d | 1422 | io_req_set_refcount(req); |
4d13d1a4 PB |
1423 | __io_req_set_refcount(req->link, 2); |
1424 | return req->link; | |
fd08e530 PB |
1425 | } |
1426 | ||
1427 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) | |
1428 | { | |
4d13d1a4 | 1429 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
fd08e530 PB |
1430 | return NULL; |
1431 | return __io_prep_linked_timeout(req); | |
1432 | } | |
1433 | ||
1e6fa521 JA |
1434 | static void io_prep_async_work(struct io_kiocb *req) |
1435 | { | |
1436 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1437 | struct io_ring_ctx *ctx = req->ctx; |
1438 | ||
b8e64b53 PB |
1439 | if (!(req->flags & REQ_F_CREDS)) { |
1440 | req->flags |= REQ_F_CREDS; | |
c10d1f98 | 1441 | req->creds = get_current_cred(); |
b8e64b53 | 1442 | } |
003e8dcc | 1443 | |
e1d675df PB |
1444 | req->work.list.next = NULL; |
1445 | req->work.flags = 0; | |
feaadc4f PB |
1446 | if (req->flags & REQ_F_FORCE_ASYNC) |
1447 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1448 | ||
1e6fa521 JA |
1449 | if (req->flags & REQ_F_ISREG) { |
1450 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1451 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
4b982bd0 | 1452 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
1e6fa521 JA |
1453 | if (def->unbound_nonreg_file) |
1454 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1455 | } | |
561fb04a | 1456 | } |
cccf0ee8 | 1457 | |
cbdcb435 | 1458 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1459 | { |
cbdcb435 | 1460 | struct io_kiocb *cur; |
54a91f3b | 1461 | |
44eff40a PB |
1462 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
1463 | struct io_ring_ctx *ctx = req->ctx; | |
1464 | ||
51b348e1 | 1465 | spin_lock_irq(&ctx->timeout_lock); |
44eff40a PB |
1466 | io_for_each_link(cur, req) |
1467 | io_prep_async_work(cur); | |
51b348e1 | 1468 | spin_unlock_irq(&ctx->timeout_lock); |
44eff40a PB |
1469 | } else { |
1470 | io_for_each_link(cur, req) | |
1471 | io_prep_async_work(cur); | |
1472 | } | |
561fb04a JA |
1473 | } |
1474 | ||
f237c30a | 1475 | static void io_queue_async_work(struct io_kiocb *req, bool *locked) |
561fb04a | 1476 | { |
a197f664 | 1477 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1478 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1479 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1480 | |
f237c30a PB |
1481 | /* must not take the lock, NULL it as a precaution */ |
1482 | locked = NULL; | |
1483 | ||
3bfe6106 JA |
1484 | BUG_ON(!tctx); |
1485 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1486 | |
cbdcb435 PB |
1487 | /* init ->work of the whole link before punting */ |
1488 | io_prep_async_link(req); | |
991468dc JA |
1489 | |
1490 | /* | |
1491 | * Not expected to happen, but if we do have a bug where this _can_ | |
1492 | * happen, catch it here and ensure the request is marked as | |
1493 | * canceled. That will make io-wq go through the usual work cancel | |
1494 | * procedure rather than attempt to run this request (or create a new | |
1495 | * worker for it). | |
1496 | */ | |
1497 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) | |
1498 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1499 | ||
d07f1e8a PB |
1500 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1501 | &req->work, req->flags); | |
ebf93667 | 1502 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a JA |
1503 | if (link) |
1504 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1505 | } |
1506 | ||
1ee4160c | 1507 | static void io_kill_timeout(struct io_kiocb *req, int status) |
8c855885 | 1508 | __must_hold(&req->ctx->completion_lock) |
89850fce | 1509 | __must_hold(&req->ctx->timeout_lock) |
5262f567 | 1510 | { |
e8c2bc1f | 1511 | struct io_timeout_data *io = req->async_data; |
5262f567 | 1512 | |
fd9c7bc5 | 1513 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
2ae2eb9d PB |
1514 | if (status) |
1515 | req_set_fail(req); | |
01cec8c1 PB |
1516 | atomic_set(&req->ctx->cq_timeouts, |
1517 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1518 | list_del_init(&req->timeout.list); |
142f794e | 1519 | io_fill_cqe_req(req, status, 0); |
91c2f697 | 1520 | io_put_req_deferred(req); |
5262f567 JA |
1521 | } |
1522 | } | |
1523 | ||
441b8a78 | 1524 | static void io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1525 | { |
441b8a78 | 1526 | while (!list_empty(&ctx->defer_list)) { |
27dc8338 PB |
1527 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1528 | struct io_defer_entry, list); | |
de0617e4 | 1529 | |
9cf7c104 | 1530 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1531 | break; |
27dc8338 | 1532 | list_del_init(&de->list); |
907d1df3 | 1533 | io_req_task_queue(de->req); |
27dc8338 | 1534 | kfree(de); |
441b8a78 | 1535 | } |
04518945 PB |
1536 | } |
1537 | ||
360428f8 | 1538 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
89850fce | 1539 | __must_hold(&ctx->completion_lock) |
de0617e4 | 1540 | { |
441b8a78 | 1541 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
7abdd4c7 | 1542 | struct io_kiocb *req, *tmp; |
f010505b | 1543 | |
79ebeaee | 1544 | spin_lock_irq(&ctx->timeout_lock); |
7abdd4c7 | 1545 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
f010505b | 1546 | u32 events_needed, events_got; |
de0617e4 | 1547 | |
8eb7e2d0 | 1548 | if (io_is_timeout_noseq(req)) |
360428f8 | 1549 | break; |
f010505b MDG |
1550 | |
1551 | /* | |
1552 | * Since seq can easily wrap around over time, subtract | |
1553 | * the last seq at which timeouts were flushed before comparing. | |
1554 | * Assuming not more than 2^31-1 events have happened since, | |
1555 | * these subtractions won't have wrapped, so we can check if | |
1556 | * target is in [last_seq, current_seq] by comparing the two. | |
1557 | */ | |
1558 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1559 | events_got = seq - ctx->cq_last_tm_flush; | |
1560 | if (events_got < events_needed) | |
360428f8 | 1561 | break; |
bfe68a22 | 1562 | |
1ee4160c | 1563 | io_kill_timeout(req, 0); |
f18ee4cf | 1564 | } |
f010505b | 1565 | ctx->cq_last_tm_flush = seq; |
79ebeaee | 1566 | spin_unlock_irq(&ctx->timeout_lock); |
360428f8 | 1567 | } |
5262f567 | 1568 | |
2335f6f5 | 1569 | static void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
360428f8 | 1570 | { |
2335f6f5 PB |
1571 | if (ctx->off_timeout_used) |
1572 | io_flush_timeouts(ctx); | |
1573 | if (ctx->drain_active) | |
1574 | io_queue_deferred(ctx); | |
1575 | } | |
1576 | ||
1577 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) | |
1578 | { | |
1579 | if (unlikely(ctx->off_timeout_used || ctx->drain_active)) | |
1580 | __io_commit_cqring_flush(ctx); | |
ec30e04b PB |
1581 | /* order cqe stores with ring update */ |
1582 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 JA |
1583 | } |
1584 | ||
90554200 JA |
1585 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1586 | { | |
1587 | struct io_rings *r = ctx->rings; | |
1588 | ||
a566c556 | 1589 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries; |
90554200 JA |
1590 | } |
1591 | ||
888aae2e PB |
1592 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1593 | { | |
1594 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1595 | } | |
1596 | ||
d068b506 | 1597 | static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) |
2b188cc1 | 1598 | { |
75b28aff | 1599 | struct io_rings *rings = ctx->rings; |
ea5ab3b5 | 1600 | unsigned tail, mask = ctx->cq_entries - 1; |
2b188cc1 | 1601 | |
115e12e5 SB |
1602 | /* |
1603 | * writes to the cq entry need to come after reading head; the | |
1604 | * control dependency is enough as we're using WRITE_ONCE to | |
1605 | * fill the cq entry | |
1606 | */ | |
a566c556 | 1607 | if (__io_cqring_events(ctx) == ctx->cq_entries) |
2b188cc1 JA |
1608 | return NULL; |
1609 | ||
888aae2e | 1610 | tail = ctx->cached_cq_tail++; |
ea5ab3b5 | 1611 | return &rings->cqes[tail & mask]; |
2b188cc1 JA |
1612 | } |
1613 | ||
f2842ab5 JA |
1614 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1615 | { | |
44c769de | 1616 | if (likely(!ctx->cq_ev_fd)) |
f0b493e6 | 1617 | return false; |
7e55a19c SG |
1618 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1619 | return false; | |
44c769de | 1620 | return !ctx->eventfd_async || io_wq_current_is_worker(); |
f2842ab5 JA |
1621 | } |
1622 | ||
2c5d763c JA |
1623 | /* |
1624 | * This should only get called when at least one event has been posted. | |
1625 | * Some applications rely on the eventfd notification count only changing | |
1626 | * IFF a new CQE has been added to the CQ ring. There's no depedency on | |
1627 | * 1:1 relationship between how many times this function is called (and | |
1628 | * hence the eventfd count) and number of CQEs posted to the CQ ring. | |
1629 | */ | |
b41e9852 | 1630 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1631 | { |
5fd46178 JA |
1632 | /* |
1633 | * wake_up_all() may seem excessive, but io_wake_function() and | |
1634 | * io_should_wake() handle the termination of the loop and only | |
1635 | * wake as many waiters as we need to. | |
1636 | */ | |
1637 | if (wq_has_sleeper(&ctx->cq_wait)) | |
1638 | wake_up_all(&ctx->cq_wait); | |
534ca6d6 JA |
1639 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
1640 | wake_up(&ctx->sq_data->wait); | |
b41e9852 | 1641 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 | 1642 | eventfd_signal(ctx->cq_ev_fd, 1); |
3f008385 | 1643 | if (waitqueue_active(&ctx->poll_wait)) |
311997b3 | 1644 | wake_up_interruptible(&ctx->poll_wait); |
1d7bb1d5 JA |
1645 | } |
1646 | ||
80c18e4a PB |
1647 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1648 | { | |
c57a91fb PB |
1649 | /* see waitqueue_active() comment */ |
1650 | smp_mb(); | |
1651 | ||
80c18e4a | 1652 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
c57a91fb | 1653 | if (waitqueue_active(&ctx->cq_wait)) |
5fd46178 | 1654 | wake_up_all(&ctx->cq_wait); |
80c18e4a PB |
1655 | } |
1656 | if (io_should_trigger_evfd(ctx)) | |
1657 | eventfd_signal(ctx->cq_ev_fd, 1); | |
3f008385 | 1658 | if (waitqueue_active(&ctx->poll_wait)) |
311997b3 | 1659 | wake_up_interruptible(&ctx->poll_wait); |
80c18e4a PB |
1660 | } |
1661 | ||
c4a2ed72 | 1662 | /* Returns true if there are no backlogged entries after the flush */ |
6c2450ae | 1663 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
1d7bb1d5 | 1664 | { |
b18032bb | 1665 | bool all_flushed, posted; |
1d7bb1d5 | 1666 | |
a566c556 | 1667 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
e23de15f | 1668 | return false; |
1d7bb1d5 | 1669 | |
b18032bb | 1670 | posted = false; |
79ebeaee | 1671 | spin_lock(&ctx->completion_lock); |
6c2450ae | 1672 | while (!list_empty(&ctx->cq_overflow_list)) { |
d068b506 | 1673 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
6c2450ae | 1674 | struct io_overflow_cqe *ocqe; |
e6c8aa9a | 1675 | |
1d7bb1d5 JA |
1676 | if (!cqe && !force) |
1677 | break; | |
6c2450ae PB |
1678 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
1679 | struct io_overflow_cqe, list); | |
1680 | if (cqe) | |
1681 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); | |
1682 | else | |
8f6ed49a PB |
1683 | io_account_cq_overflow(ctx); |
1684 | ||
b18032bb | 1685 | posted = true; |
6c2450ae PB |
1686 | list_del(&ocqe->list); |
1687 | kfree(ocqe); | |
1d7bb1d5 JA |
1688 | } |
1689 | ||
09e88404 PB |
1690 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1691 | if (all_flushed) { | |
5ed7a37d | 1692 | clear_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1693 | WRITE_ONCE(ctx->rings->sq_flags, |
1694 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); | |
09e88404 | 1695 | } |
46930143 | 1696 | |
b18032bb JA |
1697 | if (posted) |
1698 | io_commit_cqring(ctx); | |
79ebeaee | 1699 | spin_unlock(&ctx->completion_lock); |
b18032bb JA |
1700 | if (posted) |
1701 | io_cqring_ev_posted(ctx); | |
09e88404 | 1702 | return all_flushed; |
1d7bb1d5 JA |
1703 | } |
1704 | ||
90f67366 | 1705 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
6c503150 | 1706 | { |
ca0a2651 JA |
1707 | bool ret = true; |
1708 | ||
5ed7a37d | 1709 | if (test_bit(0, &ctx->check_cq_overflow)) { |
6c503150 PB |
1710 | /* iopoll syncs against uring_lock, not completion_lock */ |
1711 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1712 | mutex_lock(&ctx->uring_lock); | |
90f67366 | 1713 | ret = __io_cqring_overflow_flush(ctx, false); |
6c503150 PB |
1714 | if (ctx->flags & IORING_SETUP_IOPOLL) |
1715 | mutex_unlock(&ctx->uring_lock); | |
1716 | } | |
ca0a2651 JA |
1717 | |
1718 | return ret; | |
6c503150 PB |
1719 | } |
1720 | ||
6a290a14 PB |
1721 | /* must to be called somewhat shortly after putting a request */ |
1722 | static inline void io_put_task(struct task_struct *task, int nr) | |
1723 | { | |
1724 | struct io_uring_task *tctx = task->io_uring; | |
1725 | ||
e98e49b2 PB |
1726 | if (likely(task == current)) { |
1727 | tctx->cached_refs += nr; | |
1728 | } else { | |
1729 | percpu_counter_sub(&tctx->inflight, nr); | |
1730 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1731 | wake_up(&tctx->wait); | |
1732 | put_task_struct_many(task, nr); | |
1733 | } | |
6a290a14 PB |
1734 | } |
1735 | ||
9a10867a PB |
1736 | static void io_task_refs_refill(struct io_uring_task *tctx) |
1737 | { | |
1738 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; | |
1739 | ||
1740 | percpu_counter_add(&tctx->inflight, refill); | |
1741 | refcount_add(refill, ¤t->usage); | |
1742 | tctx->cached_refs += refill; | |
1743 | } | |
1744 | ||
1745 | static inline void io_get_task_refs(int nr) | |
1746 | { | |
1747 | struct io_uring_task *tctx = current->io_uring; | |
1748 | ||
1749 | tctx->cached_refs -= nr; | |
1750 | if (unlikely(tctx->cached_refs < 0)) | |
1751 | io_task_refs_refill(tctx); | |
1752 | } | |
1753 | ||
abdebba9 PB |
1754 | static __cold void io_uring_drop_tctx_refs(struct task_struct *task) |
1755 | { | |
1756 | struct io_uring_task *tctx = task->io_uring; | |
1757 | unsigned int refs = tctx->cached_refs; | |
1758 | ||
1759 | if (refs) { | |
1760 | tctx->cached_refs = 0; | |
1761 | percpu_counter_sub(&tctx->inflight, refs); | |
1762 | put_task_struct_many(task, refs); | |
1763 | } | |
1764 | } | |
1765 | ||
d4d19c19 | 1766 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
e1cd96d1 | 1767 | s32 res, u32 cflags) |
2b188cc1 | 1768 | { |
cce4b8b0 | 1769 | struct io_overflow_cqe *ocqe; |
2b188cc1 | 1770 | |
cce4b8b0 PB |
1771 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
1772 | if (!ocqe) { | |
1773 | /* | |
1774 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1775 | * or cannot allocate an overflow entry, then we need to drop it | |
1776 | * on the floor. | |
1777 | */ | |
8f6ed49a | 1778 | io_account_cq_overflow(ctx); |
cce4b8b0 | 1779 | return false; |
2b188cc1 | 1780 | } |
cce4b8b0 | 1781 | if (list_empty(&ctx->cq_overflow_list)) { |
5ed7a37d | 1782 | set_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1783 | WRITE_ONCE(ctx->rings->sq_flags, |
1784 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); | |
1785 | ||
cce4b8b0 | 1786 | } |
d4d19c19 | 1787 | ocqe->cqe.user_data = user_data; |
cce4b8b0 PB |
1788 | ocqe->cqe.res = res; |
1789 | ocqe->cqe.flags = cflags; | |
1790 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); | |
1791 | return true; | |
2b188cc1 JA |
1792 | } |
1793 | ||
142f794e PB |
1794 | static inline bool __io_fill_cqe(struct io_ring_ctx *ctx, u64 user_data, |
1795 | s32 res, u32 cflags) | |
2b188cc1 JA |
1796 | { |
1797 | struct io_uring_cqe *cqe; | |
1798 | ||
d4d19c19 | 1799 | trace_io_uring_complete(ctx, user_data, res, cflags); |
51c3ff62 | 1800 | |
2b188cc1 JA |
1801 | /* |
1802 | * If we can't get a cq entry, userspace overflowed the | |
1803 | * submission (by quite a lot). Increment the overflow count in | |
1804 | * the ring. | |
1805 | */ | |
d068b506 | 1806 | cqe = io_get_cqe(ctx); |
1d7bb1d5 | 1807 | if (likely(cqe)) { |
d4d19c19 | 1808 | WRITE_ONCE(cqe->user_data, user_data); |
2b188cc1 | 1809 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1810 | WRITE_ONCE(cqe->flags, cflags); |
8d13326e | 1811 | return true; |
2b188cc1 | 1812 | } |
d4d19c19 | 1813 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
2b188cc1 JA |
1814 | } |
1815 | ||
142f794e PB |
1816 | static noinline void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags) |
1817 | { | |
1818 | __io_fill_cqe(req->ctx, req->user_data, res, cflags); | |
1819 | } | |
1820 | ||
1821 | static noinline bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data, | |
1822 | s32 res, u32 cflags) | |
bcda7baa | 1823 | { |
142f794e PB |
1824 | ctx->cq_extra++; |
1825 | return __io_fill_cqe(ctx, user_data, res, cflags); | |
bcda7baa JA |
1826 | } |
1827 | ||
e1cd96d1 PB |
1828 | static void io_req_complete_post(struct io_kiocb *req, s32 res, |
1829 | u32 cflags) | |
2b188cc1 | 1830 | { |
78e19bbe | 1831 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 | 1832 | |
79ebeaee | 1833 | spin_lock(&ctx->completion_lock); |
142f794e | 1834 | __io_fill_cqe(ctx, req->user_data, res, cflags); |
c7dae4ba JA |
1835 | /* |
1836 | * If we're the last reference to this request, add to our locked | |
1837 | * free_list cache. | |
1838 | */ | |
de9b4cca | 1839 | if (req_ref_put_and_test(req)) { |
7a612350 | 1840 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
0756a869 | 1841 | if (req->flags & IO_DISARM_MASK) |
7a612350 PB |
1842 | io_disarm_next(req); |
1843 | if (req->link) { | |
1844 | io_req_task_queue(req->link); | |
1845 | req->link = NULL; | |
1846 | } | |
1847 | } | |
c7dae4ba JA |
1848 | io_dismantle_req(req); |
1849 | io_put_task(req->task, 1); | |
bb943b82 | 1850 | list_add(&req->inflight_entry, &ctx->locked_free_list); |
d0acdee2 | 1851 | ctx->locked_free_nr++; |
180f829f PB |
1852 | } else { |
1853 | if (!percpu_ref_tryget(&ctx->refs)) | |
1854 | req = NULL; | |
1855 | } | |
7a612350 | 1856 | io_commit_cqring(ctx); |
79ebeaee | 1857 | spin_unlock(&ctx->completion_lock); |
7a612350 | 1858 | |
180f829f PB |
1859 | if (req) { |
1860 | io_cqring_ev_posted(ctx); | |
c7dae4ba | 1861 | percpu_ref_put(&ctx->refs); |
180f829f | 1862 | } |
229a7b63 JA |
1863 | } |
1864 | ||
4e3d9ff9 JA |
1865 | static inline bool io_req_needs_clean(struct io_kiocb *req) |
1866 | { | |
c854357b | 1867 | return req->flags & IO_REQ_CLEAN_FLAGS; |
4e3d9ff9 JA |
1868 | } |
1869 | ||
e1cd96d1 PB |
1870 | static inline void io_req_complete_state(struct io_kiocb *req, s32 res, |
1871 | u32 cflags) | |
229a7b63 | 1872 | { |
4e3d9ff9 | 1873 | if (io_req_needs_clean(req)) |
68fb8979 | 1874 | io_clean_op(req); |
a38d68db PB |
1875 | req->result = res; |
1876 | req->compl.cflags = cflags; | |
e342c807 | 1877 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
1878 | } |
1879 | ||
889fca73 | 1880 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
e1cd96d1 | 1881 | s32 res, u32 cflags) |
bcda7baa | 1882 | { |
889fca73 PB |
1883 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
1884 | io_req_complete_state(req, res, cflags); | |
a38d68db | 1885 | else |
c7dae4ba | 1886 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
1887 | } |
1888 | ||
e1cd96d1 | 1889 | static inline void io_req_complete(struct io_kiocb *req, s32 res) |
0ddf92e8 | 1890 | { |
889fca73 | 1891 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
1892 | } |
1893 | ||
e1cd96d1 | 1894 | static void io_req_complete_failed(struct io_kiocb *req, s32 res) |
f41db273 | 1895 | { |
93d2bcd2 | 1896 | req_set_fail(req); |
f41db273 PB |
1897 | io_req_complete_post(req, res, 0); |
1898 | } | |
1899 | ||
c6d3d9cb PB |
1900 | static void io_req_complete_fail_submit(struct io_kiocb *req) |
1901 | { | |
1902 | /* | |
1903 | * We don't submit, fail them all, for that replace hardlinks with | |
1904 | * normal links. Extra REQ_F_LINK is tolerated. | |
1905 | */ | |
1906 | req->flags &= ~REQ_F_HARDLINK; | |
1907 | req->flags |= REQ_F_LINK; | |
1908 | io_req_complete_failed(req, req->result); | |
1909 | } | |
1910 | ||
864ea921 PB |
1911 | /* |
1912 | * Don't initialise the fields below on every allocation, but do that in | |
1913 | * advance and keep them valid across allocations. | |
1914 | */ | |
1915 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
1916 | { | |
1917 | req->ctx = ctx; | |
1918 | req->link = NULL; | |
1919 | req->async_data = NULL; | |
1920 | /* not necessary, but safer to zero */ | |
1921 | req->result = 0; | |
1922 | } | |
1923 | ||
dac7a098 | 1924 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
cd0ca2e0 | 1925 | struct io_submit_state *state) |
dac7a098 | 1926 | { |
79ebeaee | 1927 | spin_lock(&ctx->completion_lock); |
cd0ca2e0 | 1928 | list_splice_init(&ctx->locked_free_list, &state->free_list); |
d0acdee2 | 1929 | ctx->locked_free_nr = 0; |
79ebeaee | 1930 | spin_unlock(&ctx->completion_lock); |
dac7a098 PB |
1931 | } |
1932 | ||
dd78f492 | 1933 | /* Returns true IFF there are requests in the cache */ |
c7dae4ba | 1934 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 1935 | { |
c7dae4ba | 1936 | struct io_submit_state *state = &ctx->submit_state; |
dd78f492 | 1937 | int nr; |
0ddf92e8 | 1938 | |
c7dae4ba JA |
1939 | /* |
1940 | * If we have more than a batch's worth of requests in our IRQ side | |
1941 | * locked cache, grab the lock and move them over to our submission | |
1942 | * side cache. | |
1943 | */ | |
d0acdee2 | 1944 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
cd0ca2e0 | 1945 | io_flush_cached_locked_reqs(ctx, state); |
0ddf92e8 | 1946 | |
dd78f492 | 1947 | nr = state->free_reqs; |
cd0ca2e0 PB |
1948 | while (!list_empty(&state->free_list)) { |
1949 | struct io_kiocb *req = list_first_entry(&state->free_list, | |
bb943b82 | 1950 | struct io_kiocb, inflight_entry); |
dd78f492 | 1951 | |
bb943b82 | 1952 | list_del(&req->inflight_entry); |
dd78f492 PB |
1953 | state->reqs[nr++] = req; |
1954 | if (nr == ARRAY_SIZE(state->reqs)) | |
e5d1bc0a | 1955 | break; |
1b4c351f JA |
1956 | } |
1957 | ||
dd78f492 PB |
1958 | state->free_reqs = nr; |
1959 | return nr != 0; | |
0ddf92e8 JA |
1960 | } |
1961 | ||
5d5901a3 PB |
1962 | /* |
1963 | * A request might get retired back into the request caches even before opcode | |
1964 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. | |
1965 | * Because of that, io_alloc_req() should be called only under ->uring_lock | |
1966 | * and with extra caution to not get a request that is still worked on. | |
1967 | */ | |
e5d1bc0a | 1968 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
5d5901a3 | 1969 | __must_hold(&ctx->uring_lock) |
2b188cc1 | 1970 | { |
e5d1bc0a | 1971 | struct io_submit_state *state = &ctx->submit_state; |
864ea921 PB |
1972 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
1973 | int ret, i; | |
e5d1bc0a | 1974 | |
fe7e3257 | 1975 | BUILD_BUG_ON(ARRAY_SIZE(state->reqs) < IO_REQ_ALLOC_BATCH); |
e5d1bc0a | 1976 | |
864ea921 PB |
1977 | if (likely(state->free_reqs || io_flush_cached_reqs(ctx))) |
1978 | goto got_req; | |
e5d1bc0a | 1979 | |
864ea921 PB |
1980 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
1981 | state->reqs); | |
fd6fab2c | 1982 | |
864ea921 PB |
1983 | /* |
1984 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1985 | * retry single alloc to be on the safe side. | |
1986 | */ | |
1987 | if (unlikely(ret <= 0)) { | |
1988 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
1989 | if (!state->reqs[0]) | |
1990 | return NULL; | |
1991 | ret = 1; | |
2b188cc1 | 1992 | } |
864ea921 PB |
1993 | |
1994 | for (i = 0; i < ret; i++) | |
1995 | io_preinit_req(state->reqs[i], ctx); | |
1996 | state->free_reqs = ret; | |
e5d1bc0a | 1997 | got_req: |
291b2821 PB |
1998 | state->free_reqs--; |
1999 | return state->reqs[state->free_reqs]; | |
2b188cc1 JA |
2000 | } |
2001 | ||
e1d767f0 | 2002 | static inline void io_put_file(struct file *file) |
8da11c19 | 2003 | { |
e1d767f0 | 2004 | if (file) |
8da11c19 PB |
2005 | fput(file); |
2006 | } | |
2007 | ||
4edf20f9 | 2008 | static void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 2009 | { |
094bae49 | 2010 | unsigned int flags = req->flags; |
929a3af9 | 2011 | |
3a0a6902 PB |
2012 | if (io_req_needs_clean(req)) |
2013 | io_clean_op(req); | |
e1d767f0 PB |
2014 | if (!(flags & REQ_F_FIXED_FILE)) |
2015 | io_put_file(req->file); | |
269bbe5f BM |
2016 | if (req->fixed_rsrc_refs) |
2017 | percpu_ref_put(req->fixed_rsrc_refs); | |
99ebe4ef | 2018 | if (req->async_data) { |
094bae49 | 2019 | kfree(req->async_data); |
99ebe4ef PB |
2020 | req->async_data = NULL; |
2021 | } | |
e65ef56d JA |
2022 | } |
2023 | ||
216578e5 | 2024 | static void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 2025 | { |
51a4cc11 | 2026 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 2027 | |
216578e5 | 2028 | io_dismantle_req(req); |
7c660731 | 2029 | io_put_task(req->task, 1); |
c6ca97b3 | 2030 | |
79ebeaee | 2031 | spin_lock(&ctx->completion_lock); |
bb943b82 | 2032 | list_add(&req->inflight_entry, &ctx->locked_free_list); |
c34b025f | 2033 | ctx->locked_free_nr++; |
79ebeaee | 2034 | spin_unlock(&ctx->completion_lock); |
c34b025f | 2035 | |
ecfc5177 | 2036 | percpu_ref_put(&ctx->refs); |
e65ef56d JA |
2037 | } |
2038 | ||
f2f87370 PB |
2039 | static inline void io_remove_next_linked(struct io_kiocb *req) |
2040 | { | |
2041 | struct io_kiocb *nxt = req->link; | |
2042 | ||
2043 | req->link = nxt->link; | |
2044 | nxt->link = NULL; | |
2045 | } | |
2046 | ||
33cc89a9 PB |
2047 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
2048 | __must_hold(&req->ctx->completion_lock) | |
89b263f6 | 2049 | __must_hold(&req->ctx->timeout_lock) |
2665abfd | 2050 | { |
33cc89a9 | 2051 | struct io_kiocb *link = req->link; |
f2f87370 | 2052 | |
b97e736a | 2053 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
c9abd7ad | 2054 | struct io_timeout_data *io = link->async_data; |
7c86ffee | 2055 | |
f2f87370 | 2056 | io_remove_next_linked(req); |
90cd7e42 | 2057 | link->timeout.head = NULL; |
fd9c7bc5 | 2058 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
ef9dd637 | 2059 | list_del(&link->timeout.list); |
142f794e | 2060 | io_fill_cqe_req(link, -ECANCELED, 0); |
91c2f697 | 2061 | io_put_req_deferred(link); |
d4729fbd | 2062 | return true; |
c9abd7ad PB |
2063 | } |
2064 | } | |
d4729fbd | 2065 | return false; |
7c86ffee PB |
2066 | } |
2067 | ||
d148ca4b | 2068 | static void io_fail_links(struct io_kiocb *req) |
33cc89a9 | 2069 | __must_hold(&req->ctx->completion_lock) |
9e645e11 | 2070 | { |
33cc89a9 | 2071 | struct io_kiocb *nxt, *link = req->link; |
9e645e11 | 2072 | |
f2f87370 | 2073 | req->link = NULL; |
f2f87370 | 2074 | while (link) { |
a8295b98 HX |
2075 | long res = -ECANCELED; |
2076 | ||
2077 | if (link->flags & REQ_F_FAIL) | |
2078 | res = link->result; | |
2079 | ||
f2f87370 PB |
2080 | nxt = link->link; |
2081 | link->link = NULL; | |
2665abfd | 2082 | |
f2f87370 | 2083 | trace_io_uring_fail_link(req, link); |
142f794e | 2084 | io_fill_cqe_req(link, res, 0); |
91c2f697 | 2085 | io_put_req_deferred(link); |
f2f87370 | 2086 | link = nxt; |
9e645e11 | 2087 | } |
33cc89a9 | 2088 | } |
9e645e11 | 2089 | |
33cc89a9 PB |
2090 | static bool io_disarm_next(struct io_kiocb *req) |
2091 | __must_hold(&req->ctx->completion_lock) | |
2092 | { | |
2093 | bool posted = false; | |
2094 | ||
0756a869 PB |
2095 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
2096 | struct io_kiocb *link = req->link; | |
2097 | ||
906c6caa | 2098 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
0756a869 PB |
2099 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
2100 | io_remove_next_linked(req); | |
142f794e | 2101 | io_fill_cqe_req(link, -ECANCELED, 0); |
0756a869 PB |
2102 | io_put_req_deferred(link); |
2103 | posted = true; | |
2104 | } | |
2105 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { | |
89b263f6 JA |
2106 | struct io_ring_ctx *ctx = req->ctx; |
2107 | ||
2108 | spin_lock_irq(&ctx->timeout_lock); | |
33cc89a9 | 2109 | posted = io_kill_linked_timeout(req); |
89b263f6 JA |
2110 | spin_unlock_irq(&ctx->timeout_lock); |
2111 | } | |
93d2bcd2 | 2112 | if (unlikely((req->flags & REQ_F_FAIL) && |
e4335ed3 | 2113 | !(req->flags & REQ_F_HARDLINK))) { |
33cc89a9 PB |
2114 | posted |= (req->link != NULL); |
2115 | io_fail_links(req); | |
2116 | } | |
2117 | return posted; | |
9e645e11 JA |
2118 | } |
2119 | ||
3fa5e0f3 | 2120 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
c69f8dbe | 2121 | { |
33cc89a9 | 2122 | struct io_kiocb *nxt; |
944e58bf | 2123 | |
9e645e11 JA |
2124 | /* |
2125 | * If LINK is set, we have dependent requests in this chain. If we | |
2126 | * didn't fail this request, queue the first one up, moving any other | |
2127 | * dependencies to the next request. In case of failure, fail the rest | |
2128 | * of the chain. | |
2129 | */ | |
0756a869 | 2130 | if (req->flags & IO_DISARM_MASK) { |
33cc89a9 | 2131 | struct io_ring_ctx *ctx = req->ctx; |
33cc89a9 PB |
2132 | bool posted; |
2133 | ||
79ebeaee | 2134 | spin_lock(&ctx->completion_lock); |
33cc89a9 PB |
2135 | posted = io_disarm_next(req); |
2136 | if (posted) | |
2137 | io_commit_cqring(req->ctx); | |
79ebeaee | 2138 | spin_unlock(&ctx->completion_lock); |
33cc89a9 PB |
2139 | if (posted) |
2140 | io_cqring_ev_posted(ctx); | |
f2f87370 | 2141 | } |
33cc89a9 PB |
2142 | nxt = req->link; |
2143 | req->link = NULL; | |
2144 | return nxt; | |
4d7dd462 | 2145 | } |
9e645e11 | 2146 | |
f2f87370 | 2147 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
3fa5e0f3 | 2148 | { |
cdbff982 | 2149 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
3fa5e0f3 PB |
2150 | return NULL; |
2151 | return __io_req_find_next(req); | |
2152 | } | |
2153 | ||
f237c30a | 2154 | static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked) |
2c32395d PB |
2155 | { |
2156 | if (!ctx) | |
2157 | return; | |
f237c30a | 2158 | if (*locked) { |
99c8bc52 HX |
2159 | if (ctx->submit_state.compl_nr) |
2160 | io_submit_flush_completions(ctx); | |
2c32395d | 2161 | mutex_unlock(&ctx->uring_lock); |
f237c30a | 2162 | *locked = false; |
2c32395d PB |
2163 | } |
2164 | percpu_ref_put(&ctx->refs); | |
2165 | } | |
2166 | ||
7cbf1722 | 2167 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 2168 | { |
f237c30a | 2169 | bool locked = false; |
ebd0df2e | 2170 | struct io_ring_ctx *ctx = NULL; |
3f18407d PB |
2171 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
2172 | task_work); | |
c40f6379 | 2173 | |
16f72070 | 2174 | while (1) { |
3f18407d PB |
2175 | struct io_wq_work_node *node; |
2176 | ||
8d4ad41e PB |
2177 | if (!tctx->task_list.first && locked && ctx->submit_state.compl_nr) |
2178 | io_submit_flush_completions(ctx); | |
2179 | ||
3f18407d | 2180 | spin_lock_irq(&tctx->task_lock); |
c6538be9 | 2181 | node = tctx->task_list.first; |
3f18407d | 2182 | INIT_WQ_LIST(&tctx->task_list); |
6294f368 PB |
2183 | if (!node) |
2184 | tctx->task_running = false; | |
3f18407d | 2185 | spin_unlock_irq(&tctx->task_lock); |
6294f368 PB |
2186 | if (!node) |
2187 | break; | |
3f18407d | 2188 | |
6294f368 | 2189 | do { |
3f18407d PB |
2190 | struct io_wq_work_node *next = node->next; |
2191 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2192 | io_task_work.node); | |
2193 | ||
2194 | if (req->ctx != ctx) { | |
f237c30a | 2195 | ctx_flush_and_put(ctx, &locked); |
3f18407d | 2196 | ctx = req->ctx; |
126180b9 PB |
2197 | /* if not contended, grab and improve batching */ |
2198 | locked = mutex_trylock(&ctx->uring_lock); | |
3f18407d PB |
2199 | percpu_ref_get(&ctx->refs); |
2200 | } | |
f237c30a | 2201 | req->io_task_work.func(req, &locked); |
3f18407d | 2202 | node = next; |
6294f368 PB |
2203 | } while (node); |
2204 | ||
7cbf1722 | 2205 | cond_resched(); |
3f18407d | 2206 | } |
ebd0df2e | 2207 | |
f237c30a | 2208 | ctx_flush_and_put(ctx, &locked); |
abdebba9 PB |
2209 | |
2210 | /* relaxed read is enough as only the task itself sets ->in_idle */ | |
2211 | if (unlikely(atomic_read(&tctx->in_idle))) | |
2212 | io_uring_drop_tctx_refs(current); | |
7cbf1722 JA |
2213 | } |
2214 | ||
e09ee510 | 2215 | static void io_req_task_work_add(struct io_kiocb *req) |
7cbf1722 | 2216 | { |
c15b79de | 2217 | struct task_struct *tsk = req->task; |
7cbf1722 | 2218 | struct io_uring_task *tctx = tsk->io_uring; |
c15b79de | 2219 | enum task_work_notify_mode notify; |
e09ee510 | 2220 | struct io_wq_work_node *node; |
0b81e80c | 2221 | unsigned long flags; |
6294f368 | 2222 | bool running; |
7cbf1722 JA |
2223 | |
2224 | WARN_ON_ONCE(!tctx); | |
2225 | ||
0b81e80c | 2226 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 | 2227 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
6294f368 PB |
2228 | running = tctx->task_running; |
2229 | if (!running) | |
2230 | tctx->task_running = true; | |
0b81e80c | 2231 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2232 | |
2233 | /* task_work already pending, we're done */ | |
6294f368 | 2234 | if (running) |
e09ee510 | 2235 | return; |
7cbf1722 | 2236 | |
c15b79de PB |
2237 | /* |
2238 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For | |
2239 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2240 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2241 | * will do the job. | |
2242 | */ | |
2243 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; | |
c15b79de PB |
2244 | if (!task_work_add(tsk, &tctx->task_work, notify)) { |
2245 | wake_up_process(tsk); | |
e09ee510 | 2246 | return; |
c15b79de | 2247 | } |
2215bed9 | 2248 | |
0b81e80c | 2249 | spin_lock_irqsave(&tctx->task_lock, flags); |
6294f368 | 2250 | tctx->task_running = false; |
e09ee510 PB |
2251 | node = tctx->task_list.first; |
2252 | INIT_WQ_LIST(&tctx->task_list); | |
0b81e80c | 2253 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 | 2254 | |
e09ee510 PB |
2255 | while (node) { |
2256 | req = container_of(node, struct io_kiocb, io_task_work.node); | |
2257 | node = node->next; | |
2258 | if (llist_add(&req->io_task_work.fallback_node, | |
2259 | &req->ctx->fallback_llist)) | |
2260 | schedule_delayed_work(&req->ctx->fallback_work, 1); | |
2261 | } | |
eab30c4d PB |
2262 | } |
2263 | ||
f237c30a | 2264 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
c40f6379 | 2265 | { |
87ceb6a6 | 2266 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2267 | |
b18a1a45 | 2268 | /* not needed for normal modes, but SQPOLL depends on it */ |
f237c30a | 2269 | io_tw_lock(ctx, locked); |
2593553a | 2270 | io_req_complete_failed(req, req->result); |
c40f6379 JA |
2271 | } |
2272 | ||
f237c30a | 2273 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
c40f6379 JA |
2274 | { |
2275 | struct io_ring_ctx *ctx = req->ctx; | |
2276 | ||
f237c30a | 2277 | io_tw_lock(ctx, locked); |
316319e8 | 2278 | /* req->task == current here, checking PF_EXITING is safe */ |
af066f31 | 2279 | if (likely(!(req->task->flags & PF_EXITING))) |
c5eef2b9 | 2280 | __io_queue_sqe(req); |
81b6d05c | 2281 | else |
2593553a | 2282 | io_req_complete_failed(req, -EFAULT); |
c40f6379 JA |
2283 | } |
2284 | ||
2c4b8eb6 | 2285 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
c40f6379 | 2286 | { |
2c4b8eb6 | 2287 | req->result = ret; |
5b0a6acc | 2288 | req->io_task_work.func = io_req_task_cancel; |
e09ee510 | 2289 | io_req_task_work_add(req); |
c40f6379 JA |
2290 | } |
2291 | ||
2c4b8eb6 | 2292 | static void io_req_task_queue(struct io_kiocb *req) |
a3df7698 | 2293 | { |
5b0a6acc | 2294 | req->io_task_work.func = io_req_task_submit; |
e09ee510 | 2295 | io_req_task_work_add(req); |
a3df7698 PB |
2296 | } |
2297 | ||
773af691 JA |
2298 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
2299 | { | |
2300 | req->io_task_work.func = io_queue_async_work; | |
2301 | io_req_task_work_add(req); | |
2302 | } | |
2303 | ||
f2f87370 | 2304 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2305 | { |
9b5f7bd9 | 2306 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2307 | |
2308 | if (nxt) | |
906a8c3f | 2309 | io_req_task_queue(nxt); |
c69f8dbe JL |
2310 | } |
2311 | ||
c3524383 | 2312 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2313 | { |
c3524383 PB |
2314 | io_queue_next(req); |
2315 | __io_free_req(req); | |
2316 | } | |
8766dd51 | 2317 | |
f237c30a PB |
2318 | static void io_free_req_work(struct io_kiocb *req, bool *locked) |
2319 | { | |
2320 | io_free_req(req); | |
2321 | } | |
2322 | ||
2d6500d4 | 2323 | struct req_batch { |
5af1d13e PB |
2324 | struct task_struct *task; |
2325 | int task_refs; | |
1b4c351f | 2326 | int ctx_refs; |
2d6500d4 PB |
2327 | }; |
2328 | ||
5af1d13e PB |
2329 | static inline void io_init_req_batch(struct req_batch *rb) |
2330 | { | |
5af1d13e | 2331 | rb->task_refs = 0; |
9ae72463 | 2332 | rb->ctx_refs = 0; |
5af1d13e PB |
2333 | rb->task = NULL; |
2334 | } | |
2335 | ||
2d6500d4 PB |
2336 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
2337 | struct req_batch *rb) | |
2338 | { | |
9ae72463 PB |
2339 | if (rb->ctx_refs) |
2340 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); | |
e98e49b2 | 2341 | if (rb->task) |
e9dbe221 | 2342 | io_put_task(rb->task, rb->task_refs); |
2d6500d4 PB |
2343 | } |
2344 | ||
6ff119a6 PB |
2345 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
2346 | struct io_submit_state *state) | |
2d6500d4 | 2347 | { |
f2f87370 | 2348 | io_queue_next(req); |
96670657 | 2349 | io_dismantle_req(req); |
2d6500d4 | 2350 | |
e3bc8e9d | 2351 | if (req->task != rb->task) { |
7c660731 PB |
2352 | if (rb->task) |
2353 | io_put_task(rb->task, rb->task_refs); | |
e3bc8e9d JA |
2354 | rb->task = req->task; |
2355 | rb->task_refs = 0; | |
5af1d13e | 2356 | } |
e3bc8e9d | 2357 | rb->task_refs++; |
9ae72463 | 2358 | rb->ctx_refs++; |
5af1d13e | 2359 | |
bd759045 | 2360 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
6ff119a6 | 2361 | state->reqs[state->free_reqs++] = req; |
bd759045 | 2362 | else |
cd0ca2e0 | 2363 | list_add(&req->inflight_entry, &state->free_list); |
7a743e22 PB |
2364 | } |
2365 | ||
2a2758f2 | 2366 | static void io_submit_flush_completions(struct io_ring_ctx *ctx) |
a141dd89 | 2367 | __must_hold(&ctx->uring_lock) |
905c172f | 2368 | { |
cd0ca2e0 PB |
2369 | struct io_submit_state *state = &ctx->submit_state; |
2370 | int i, nr = state->compl_nr; | |
905c172f PB |
2371 | struct req_batch rb; |
2372 | ||
79ebeaee | 2373 | spin_lock(&ctx->completion_lock); |
905c172f | 2374 | for (i = 0; i < nr; i++) { |
cd0ca2e0 | 2375 | struct io_kiocb *req = state->compl_reqs[i]; |
5182ed2e | 2376 | |
142f794e PB |
2377 | __io_fill_cqe(ctx, req->user_data, req->result, |
2378 | req->compl.cflags); | |
905c172f PB |
2379 | } |
2380 | io_commit_cqring(ctx); | |
79ebeaee | 2381 | spin_unlock(&ctx->completion_lock); |
905c172f | 2382 | io_cqring_ev_posted(ctx); |
5182ed2e PB |
2383 | |
2384 | io_init_req_batch(&rb); | |
905c172f | 2385 | for (i = 0; i < nr; i++) { |
cd0ca2e0 | 2386 | struct io_kiocb *req = state->compl_reqs[i]; |
905c172f | 2387 | |
91c2f697 | 2388 | if (req_ref_put_and_test(req)) |
6ff119a6 | 2389 | io_req_free_batch(&rb, req, &ctx->submit_state); |
905c172f PB |
2390 | } |
2391 | ||
2392 | io_req_free_batch_finish(ctx, &rb); | |
cd0ca2e0 | 2393 | state->compl_nr = 0; |
7a743e22 PB |
2394 | } |
2395 | ||
ba816ad6 JA |
2396 | /* |
2397 | * Drop reference to request, return next in chain (if there is one) if this | |
2398 | * was the last reference to this request. | |
2399 | */ | |
0d85035a | 2400 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2401 | { |
9b5f7bd9 PB |
2402 | struct io_kiocb *nxt = NULL; |
2403 | ||
de9b4cca | 2404 | if (req_ref_put_and_test(req)) { |
9b5f7bd9 | 2405 | nxt = io_req_find_next(req); |
4d7dd462 | 2406 | __io_free_req(req); |
2a44f467 | 2407 | } |
9b5f7bd9 | 2408 | return nxt; |
2b188cc1 JA |
2409 | } |
2410 | ||
0d85035a | 2411 | static inline void io_put_req(struct io_kiocb *req) |
e65ef56d | 2412 | { |
de9b4cca | 2413 | if (req_ref_put_and_test(req)) |
e65ef56d | 2414 | io_free_req(req); |
2b188cc1 JA |
2415 | } |
2416 | ||
91c2f697 | 2417 | static inline void io_put_req_deferred(struct io_kiocb *req) |
216578e5 | 2418 | { |
91c2f697 | 2419 | if (req_ref_put_and_test(req)) { |
f237c30a | 2420 | req->io_task_work.func = io_free_req_work; |
543af3a1 PB |
2421 | io_req_task_work_add(req); |
2422 | } | |
216578e5 PB |
2423 | } |
2424 | ||
6c503150 | 2425 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2426 | { |
2427 | /* See comment at the top of this file */ | |
2428 | smp_rmb(); | |
e23de15f | 2429 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2430 | } |
2431 | ||
fb5ccc98 PB |
2432 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2433 | { | |
2434 | struct io_rings *rings = ctx->rings; | |
2435 | ||
2436 | /* make sure SQ entry isn't read before tail */ | |
2437 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2438 | } | |
2439 | ||
8ff069bf | 2440 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2441 | { |
8ff069bf | 2442 | unsigned int cflags; |
e94f141b | 2443 | |
bcda7baa JA |
2444 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2445 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2446 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2447 | kfree(kbuf); |
2448 | return cflags; | |
e94f141b JA |
2449 | } |
2450 | ||
8ff069bf | 2451 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2452 | { |
4d954c25 | 2453 | struct io_buffer *kbuf; |
bcda7baa | 2454 | |
ae421d93 PB |
2455 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
2456 | return 0; | |
4d954c25 | 2457 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
8ff069bf PB |
2458 | return io_put_kbuf(req, kbuf); |
2459 | } | |
2460 | ||
4c6e277c JA |
2461 | static inline bool io_run_task_work(void) |
2462 | { | |
ef98eb04 | 2463 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) { |
4c6e277c | 2464 | __set_current_state(TASK_RUNNING); |
ef98eb04 | 2465 | tracehook_notify_signal(); |
4c6e277c JA |
2466 | return true; |
2467 | } | |
2468 | ||
2469 | return false; | |
bcda7baa JA |
2470 | } |
2471 | ||
def596e9 JA |
2472 | /* |
2473 | * Find and free completed poll iocbs | |
2474 | */ | |
2475 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
a8576af9 | 2476 | struct list_head *done) |
def596e9 | 2477 | { |
8237e045 | 2478 | struct req_batch rb; |
def596e9 | 2479 | struct io_kiocb *req; |
bbde017a XW |
2480 | |
2481 | /* order with ->result store in io_complete_rw_iopoll() */ | |
2482 | smp_rmb(); | |
def596e9 | 2483 | |
5af1d13e | 2484 | io_init_req_batch(&rb); |
def596e9 | 2485 | while (!list_empty(done)) { |
d21ffe7e | 2486 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
f161340d PB |
2487 | list_del(&req->inflight_entry); |
2488 | ||
142f794e | 2489 | io_fill_cqe_req(req, req->result, io_put_rw_kbuf(req)); |
def596e9 JA |
2490 | (*nr_events)++; |
2491 | ||
de9b4cca | 2492 | if (req_ref_put_and_test(req)) |
6ff119a6 | 2493 | io_req_free_batch(&rb, req, &ctx->submit_state); |
def596e9 | 2494 | } |
def596e9 | 2495 | |
09bb8394 | 2496 | io_commit_cqring(ctx); |
80c18e4a | 2497 | io_cqring_ev_posted_iopoll(ctx); |
2d6500d4 | 2498 | io_req_free_batch_finish(ctx, &rb); |
581f9810 BM |
2499 | } |
2500 | ||
def596e9 | 2501 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
a8576af9 | 2502 | long min) |
def596e9 JA |
2503 | { |
2504 | struct io_kiocb *req, *tmp; | |
2505 | LIST_HEAD(done); | |
2506 | bool spin; | |
def596e9 JA |
2507 | |
2508 | /* | |
2509 | * Only spin for completions if we don't have multiple devices hanging | |
2510 | * off our complete list, and we're under the requested amount. | |
2511 | */ | |
915b3dde | 2512 | spin = !ctx->poll_multi_queue && *nr_events < min; |
def596e9 | 2513 | |
d21ffe7e | 2514 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
9adbd45d | 2515 | struct kiocb *kiocb = &req->rw.kiocb; |
a2416e1e | 2516 | int ret; |
def596e9 JA |
2517 | |
2518 | /* | |
581f9810 BM |
2519 | * Move completed and retryable entries to our local lists. |
2520 | * If we find a request that requires polling, break out | |
2521 | * and complete those lists first, if we have entries there. | |
def596e9 | 2522 | */ |
65a6543d | 2523 | if (READ_ONCE(req->iopoll_completed)) { |
d21ffe7e | 2524 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2525 | continue; |
2526 | } | |
2527 | if (!list_empty(&done)) | |
2528 | break; | |
2529 | ||
2530 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
a2416e1e PB |
2531 | if (unlikely(ret < 0)) |
2532 | return ret; | |
2533 | else if (ret) | |
2534 | spin = false; | |
def596e9 | 2535 | |
3aadc23e PB |
2536 | /* iopoll may have completed current req */ |
2537 | if (READ_ONCE(req->iopoll_completed)) | |
d21ffe7e | 2538 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2539 | } |
2540 | ||
2541 | if (!list_empty(&done)) | |
a8576af9 | 2542 | io_iopoll_complete(ctx, nr_events, &done); |
def596e9 | 2543 | |
a2416e1e | 2544 | return 0; |
def596e9 JA |
2545 | } |
2546 | ||
def596e9 JA |
2547 | /* |
2548 | * We can't just wait for polled events to come to us, we have to actively | |
2549 | * find and complete them. | |
2550 | */ | |
b2edc0a7 | 2551 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2552 | { |
2553 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2554 | return; | |
2555 | ||
2556 | mutex_lock(&ctx->uring_lock); | |
540e32a0 | 2557 | while (!list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2558 | unsigned int nr_events = 0; |
2559 | ||
a8576af9 | 2560 | io_do_iopoll(ctx, &nr_events, 0); |
08f5439f | 2561 | |
b2edc0a7 PB |
2562 | /* let it sleep and repeat later if can't complete a request */ |
2563 | if (nr_events == 0) | |
2564 | break; | |
08f5439f JA |
2565 | /* |
2566 | * Ensure we allow local-to-the-cpu processing to take place, | |
2567 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2568 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2569 | */ |
3fcee5a6 PB |
2570 | if (need_resched()) { |
2571 | mutex_unlock(&ctx->uring_lock); | |
2572 | cond_resched(); | |
2573 | mutex_lock(&ctx->uring_lock); | |
2574 | } | |
def596e9 JA |
2575 | } |
2576 | mutex_unlock(&ctx->uring_lock); | |
2577 | } | |
2578 | ||
7668b92a | 2579 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2580 | { |
7668b92a | 2581 | unsigned int nr_events = 0; |
e9979b36 | 2582 | int ret = 0; |
500f9fba | 2583 | |
c7849be9 XW |
2584 | /* |
2585 | * We disallow the app entering submit/complete with polling, but we | |
2586 | * still need to lock the ring to prevent racing with polled issue | |
2587 | * that got punted to a workqueue. | |
2588 | */ | |
2589 | mutex_lock(&ctx->uring_lock); | |
f39c8a5b PB |
2590 | /* |
2591 | * Don't enter poll loop if we already have events pending. | |
2592 | * If we do, we can potentially be spinning for commands that | |
2593 | * already triggered a CQE (eg in error). | |
2594 | */ | |
5ed7a37d | 2595 | if (test_bit(0, &ctx->check_cq_overflow)) |
f39c8a5b PB |
2596 | __io_cqring_overflow_flush(ctx, false); |
2597 | if (io_cqring_events(ctx)) | |
2598 | goto out; | |
def596e9 | 2599 | do { |
500f9fba JA |
2600 | /* |
2601 | * If a submit got punted to a workqueue, we can have the | |
2602 | * application entering polling for a command before it gets | |
2603 | * issued. That app will hold the uring_lock for the duration | |
2604 | * of the poll right here, so we need to take a breather every | |
2605 | * now and then to ensure that the issue has a chance to add | |
2606 | * the poll to the issued list. Otherwise we can spin here | |
2607 | * forever, while the workqueue is stuck trying to acquire the | |
2608 | * very same mutex. | |
2609 | */ | |
e9979b36 | 2610 | if (list_empty(&ctx->iopoll_list)) { |
8f487ef2 PB |
2611 | u32 tail = ctx->cached_cq_tail; |
2612 | ||
500f9fba | 2613 | mutex_unlock(&ctx->uring_lock); |
4c6e277c | 2614 | io_run_task_work(); |
500f9fba | 2615 | mutex_lock(&ctx->uring_lock); |
def596e9 | 2616 | |
8f487ef2 PB |
2617 | /* some requests don't go through iopoll_list */ |
2618 | if (tail != ctx->cached_cq_tail || | |
2619 | list_empty(&ctx->iopoll_list)) | |
e9979b36 | 2620 | break; |
500f9fba | 2621 | } |
a8576af9 | 2622 | ret = io_do_iopoll(ctx, &nr_events, min); |
f39c8a5b PB |
2623 | } while (!ret && nr_events < min && !need_resched()); |
2624 | out: | |
500f9fba | 2625 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2626 | return ret; |
2627 | } | |
2628 | ||
491381ce | 2629 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2630 | { |
491381ce JA |
2631 | /* |
2632 | * Tell lockdep we inherited freeze protection from submission | |
2633 | * thread. | |
2634 | */ | |
2635 | if (req->flags & REQ_F_ISREG) { | |
1c98679d | 2636 | struct super_block *sb = file_inode(req->file)->i_sb; |
2b188cc1 | 2637 | |
1c98679d PB |
2638 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
2639 | sb_end_write(sb); | |
2b188cc1 JA |
2640 | } |
2641 | } | |
2642 | ||
b63534c4 | 2643 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2644 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 | 2645 | { |
ab454438 | 2646 | struct io_async_rw *rw = req->async_data; |
b63534c4 | 2647 | |
ab454438 PB |
2648 | if (!rw) |
2649 | return !io_req_prep_async(req); | |
cd658695 | 2650 | iov_iter_restore(&rw->iter, &rw->iter_state); |
ab454438 | 2651 | return true; |
b63534c4 | 2652 | } |
b63534c4 | 2653 | |
3e6a0d3c | 2654 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 2655 | { |
355afaeb | 2656 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 2657 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 2658 | |
355afaeb JA |
2659 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2660 | return false; | |
3e6a0d3c JA |
2661 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
2662 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 2663 | return false; |
7c977a58 JA |
2664 | /* |
2665 | * If ref is dying, we might be running poll reap from the exit work. | |
2666 | * Don't attempt to reissue from that path, just let it fail with | |
2667 | * -EAGAIN. | |
2668 | */ | |
3e6a0d3c JA |
2669 | if (percpu_ref_is_dying(&ctx->refs)) |
2670 | return false; | |
ef046888 JA |
2671 | /* |
2672 | * Play it safe and assume not safe to re-import and reissue if we're | |
2673 | * not in the original thread group (or in task context). | |
2674 | */ | |
2675 | if (!same_thread_group(req->task, current) || !in_task()) | |
2676 | return false; | |
3e6a0d3c JA |
2677 | return true; |
2678 | } | |
e82ad485 | 2679 | #else |
a1ff1e3f | 2680 | static bool io_resubmit_prep(struct io_kiocb *req) |
e82ad485 JA |
2681 | { |
2682 | return false; | |
2683 | } | |
e82ad485 | 2684 | static bool io_rw_should_reissue(struct io_kiocb *req) |
3e6a0d3c | 2685 | { |
b63534c4 JA |
2686 | return false; |
2687 | } | |
3e6a0d3c | 2688 | #endif |
b63534c4 | 2689 | |
8ef12efe | 2690 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
a1d7c393 | 2691 | { |
48a904e2 | 2692 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) { |
b65c128f | 2693 | kiocb_end_write(req); |
48a904e2 JA |
2694 | fsnotify_modify(req->file); |
2695 | } else { | |
2696 | fsnotify_access(req->file); | |
2697 | } | |
9532b99b PB |
2698 | if (res != req->result) { |
2699 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && | |
2700 | io_rw_should_reissue(req)) { | |
2701 | req->flags |= REQ_F_REISSUE; | |
8ef12efe | 2702 | return true; |
9532b99b | 2703 | } |
93d2bcd2 | 2704 | req_set_fail(req); |
8ef12efe | 2705 | req->result = res; |
9532b99b | 2706 | } |
8ef12efe JA |
2707 | return false; |
2708 | } | |
2709 | ||
f237c30a | 2710 | static void io_req_task_complete(struct io_kiocb *req, bool *locked) |
8ef12efe | 2711 | { |
126180b9 | 2712 | unsigned int cflags = io_put_rw_kbuf(req); |
e1cd96d1 | 2713 | int res = req->result; |
126180b9 PB |
2714 | |
2715 | if (*locked) { | |
2716 | struct io_ring_ctx *ctx = req->ctx; | |
2717 | struct io_submit_state *state = &ctx->submit_state; | |
2718 | ||
2719 | io_req_complete_state(req, res, cflags); | |
2720 | state->compl_reqs[state->compl_nr++] = req; | |
2721 | if (state->compl_nr == ARRAY_SIZE(state->compl_reqs)) | |
2722 | io_submit_flush_completions(ctx); | |
2723 | } else { | |
2724 | io_req_complete_post(req, res, cflags); | |
2725 | } | |
8ef12efe JA |
2726 | } |
2727 | ||
2728 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, | |
2729 | unsigned int issue_flags) | |
2730 | { | |
2731 | if (__io_complete_rw_common(req, res)) | |
2732 | return; | |
63637853 | 2733 | __io_req_complete(req, issue_flags, req->result, io_put_rw_kbuf(req)); |
ba816ad6 JA |
2734 | } |
2735 | ||
2736 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2737 | { | |
9adbd45d | 2738 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2739 | |
8ef12efe JA |
2740 | if (__io_complete_rw_common(req, res)) |
2741 | return; | |
2742 | req->result = res; | |
2743 | req->io_task_work.func = io_req_task_complete; | |
2744 | io_req_task_work_add(req); | |
2b188cc1 JA |
2745 | } |
2746 | ||
def596e9 JA |
2747 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2748 | { | |
9adbd45d | 2749 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2750 | |
491381ce JA |
2751 | if (kiocb->ki_flags & IOCB_WRITE) |
2752 | kiocb_end_write(req); | |
9532b99b | 2753 | if (unlikely(res != req->result)) { |
b66ceaf3 PB |
2754 | if (res == -EAGAIN && io_rw_should_reissue(req)) { |
2755 | req->flags |= REQ_F_REISSUE; | |
2756 | return; | |
9532b99b | 2757 | } |
8c130827 | 2758 | } |
bbde017a XW |
2759 | |
2760 | WRITE_ONCE(req->result, res); | |
b9b0e0d3 | 2761 | /* order with io_iopoll_complete() checking ->result */ |
cd664b0e PB |
2762 | smp_wmb(); |
2763 | WRITE_ONCE(req->iopoll_completed, 1); | |
def596e9 JA |
2764 | } |
2765 | ||
2766 | /* | |
2767 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2768 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
f39c8a5b | 2769 | * find it from a io_do_iopoll() thread before the issuer is done |
def596e9 JA |
2770 | * accessing the kiocb cookie. |
2771 | */ | |
cb3d8972 | 2772 | static void io_iopoll_req_issued(struct io_kiocb *req) |
def596e9 JA |
2773 | { |
2774 | struct io_ring_ctx *ctx = req->ctx; | |
cb3d8972 PB |
2775 | const bool in_async = io_wq_current_is_worker(); |
2776 | ||
2777 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
2778 | if (unlikely(in_async)) | |
2779 | mutex_lock(&ctx->uring_lock); | |
def596e9 JA |
2780 | |
2781 | /* | |
2782 | * Track whether we have multiple files in our lists. This will impact | |
2783 | * how we do polling eventually, not spinning if we're on potentially | |
2784 | * different devices. | |
2785 | */ | |
540e32a0 | 2786 | if (list_empty(&ctx->iopoll_list)) { |
915b3dde HX |
2787 | ctx->poll_multi_queue = false; |
2788 | } else if (!ctx->poll_multi_queue) { | |
def596e9 | 2789 | struct io_kiocb *list_req; |
915b3dde | 2790 | unsigned int queue_num0, queue_num1; |
def596e9 | 2791 | |
540e32a0 | 2792 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
d21ffe7e | 2793 | inflight_entry); |
915b3dde HX |
2794 | |
2795 | if (list_req->file != req->file) { | |
2796 | ctx->poll_multi_queue = true; | |
2797 | } else { | |
2798 | queue_num0 = blk_qc_t_to_queue_num(list_req->rw.kiocb.ki_cookie); | |
2799 | queue_num1 = blk_qc_t_to_queue_num(req->rw.kiocb.ki_cookie); | |
2800 | if (queue_num0 != queue_num1) | |
2801 | ctx->poll_multi_queue = true; | |
2802 | } | |
def596e9 JA |
2803 | } |
2804 | ||
2805 | /* | |
2806 | * For fast devices, IO may have already completed. If it has, add | |
2807 | * it to the front so we find it first. | |
2808 | */ | |
65a6543d | 2809 | if (READ_ONCE(req->iopoll_completed)) |
d21ffe7e | 2810 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
def596e9 | 2811 | else |
d21ffe7e | 2812 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
bdcd3eab | 2813 | |
cb3d8972 PB |
2814 | if (unlikely(in_async)) { |
2815 | /* | |
2816 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle | |
2817 | * in sq thread task context or in io worker task context. If | |
2818 | * current task context is sq thread, we don't need to check | |
2819 | * whether should wake up sq thread. | |
2820 | */ | |
2821 | if ((ctx->flags & IORING_SETUP_SQPOLL) && | |
2822 | wq_has_sleeper(&ctx->sq_data->wait)) | |
2823 | wake_up(&ctx->sq_data->wait); | |
2824 | ||
2825 | mutex_unlock(&ctx->uring_lock); | |
2826 | } | |
def596e9 JA |
2827 | } |
2828 | ||
4503b767 JA |
2829 | static bool io_bdev_nowait(struct block_device *bdev) |
2830 | { | |
9ba0d0c8 | 2831 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2832 | } |
2833 | ||
2b188cc1 JA |
2834 | /* |
2835 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2836 | * any file. For now, just ensure that anything potentially problematic is done | |
2837 | * inline. | |
2838 | */ | |
b191e2df | 2839 | static bool __io_file_supports_nowait(struct file *file, int rw) |
2b188cc1 JA |
2840 | { |
2841 | umode_t mode = file_inode(file)->i_mode; | |
2842 | ||
4503b767 | 2843 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2844 | if (IS_ENABLED(CONFIG_BLOCK) && |
2845 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2846 | return true; |
2847 | return false; | |
2848 | } | |
976517f1 | 2849 | if (S_ISSOCK(mode)) |
2b188cc1 | 2850 | return true; |
4503b767 | 2851 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2852 | if (IS_ENABLED(CONFIG_BLOCK) && |
2853 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2854 | file->f_op != &io_uring_fops) |
2855 | return true; | |
2856 | return false; | |
2857 | } | |
2b188cc1 | 2858 | |
c5b85625 JA |
2859 | /* any ->read/write should understand O_NONBLOCK */ |
2860 | if (file->f_flags & O_NONBLOCK) | |
2861 | return true; | |
2862 | ||
af197f50 JA |
2863 | if (!(file->f_mode & FMODE_NOWAIT)) |
2864 | return false; | |
2865 | ||
2866 | if (rw == READ) | |
2867 | return file->f_op->read_iter != NULL; | |
2868 | ||
2869 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
2870 | } |
2871 | ||
b191e2df | 2872 | static bool io_file_supports_nowait(struct io_kiocb *req, int rw) |
7b29f92d | 2873 | { |
b191e2df | 2874 | if (rw == READ && (req->flags & REQ_F_NOWAIT_READ)) |
7b29f92d | 2875 | return true; |
b191e2df | 2876 | else if (rw == WRITE && (req->flags & REQ_F_NOWAIT_WRITE)) |
7b29f92d JA |
2877 | return true; |
2878 | ||
b191e2df | 2879 | return __io_file_supports_nowait(req->file, rw); |
7b29f92d JA |
2880 | } |
2881 | ||
5d329e12 JA |
2882 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2883 | int rw) | |
2b188cc1 | 2884 | { |
def596e9 | 2885 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2886 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 2887 | struct file *file = req->file; |
09bb8394 JA |
2888 | unsigned ioprio; |
2889 | int ret; | |
2b188cc1 | 2890 | |
c97d8a0f | 2891 | if (!io_req_ffs_set(req) && S_ISREG(file_inode(file)->i_mode)) |
491381ce JA |
2892 | req->flags |= REQ_F_ISREG; |
2893 | ||
2b188cc1 | 2894 | kiocb->ki_pos = READ_ONCE(sqe->off); |
0c840c47 JA |
2895 | if (kiocb->ki_pos == -1) { |
2896 | if (!(file->f_mode & FMODE_STREAM)) { | |
2897 | req->flags |= REQ_F_CUR_POS; | |
2898 | kiocb->ki_pos = file->f_pos; | |
2899 | } else { | |
2900 | kiocb->ki_pos = 0; | |
2901 | } | |
ba04291e | 2902 | } |
2b188cc1 | 2903 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
2904 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
2905 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
2906 | if (unlikely(ret)) | |
2907 | return ret; | |
2b188cc1 | 2908 | |
5d329e12 JA |
2909 | /* |
2910 | * If the file is marked O_NONBLOCK, still allow retry for it if it | |
2911 | * supports async. Otherwise it's impossible to use O_NONBLOCK files | |
2912 | * reliably. If not, or it IOCB_NOWAIT is set, don't retry. | |
2913 | */ | |
2914 | if ((kiocb->ki_flags & IOCB_NOWAIT) || | |
2915 | ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req, rw))) | |
75c668cd PB |
2916 | req->flags |= REQ_F_NOWAIT; |
2917 | ||
2b188cc1 JA |
2918 | ioprio = READ_ONCE(sqe->ioprio); |
2919 | if (ioprio) { | |
2920 | ret = ioprio_check_cap(ioprio); | |
2921 | if (ret) | |
09bb8394 | 2922 | return ret; |
2b188cc1 JA |
2923 | |
2924 | kiocb->ki_ioprio = ioprio; | |
2925 | } else | |
2926 | kiocb->ki_ioprio = get_current_ioprio(); | |
2927 | ||
def596e9 | 2928 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
2929 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
2930 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 2931 | return -EOPNOTSUPP; |
2b188cc1 | 2932 | |
394918eb | 2933 | kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE; |
def596e9 | 2934 | kiocb->ki_complete = io_complete_rw_iopoll; |
65a6543d | 2935 | req->iopoll_completed = 0; |
def596e9 | 2936 | } else { |
09bb8394 JA |
2937 | if (kiocb->ki_flags & IOCB_HIPRI) |
2938 | return -EINVAL; | |
def596e9 JA |
2939 | kiocb->ki_complete = io_complete_rw; |
2940 | } | |
9adbd45d | 2941 | |
bf1aa2d2 PB |
2942 | /* used for fixed read/write too - just read unconditionally */ |
2943 | req->buf_index = READ_ONCE(sqe->buf_index); | |
2944 | req->imu = NULL; | |
2945 | ||
eae071c9 PB |
2946 | if (req->opcode == IORING_OP_READ_FIXED || |
2947 | req->opcode == IORING_OP_WRITE_FIXED) { | |
bf1aa2d2 PB |
2948 | struct io_ring_ctx *ctx = req->ctx; |
2949 | u16 index; | |
2950 | ||
2951 | if (unlikely(req->buf_index >= ctx->nr_user_bufs)) | |
2952 | return -EFAULT; | |
2953 | index = array_index_nospec(req->buf_index, ctx->nr_user_bufs); | |
2954 | req->imu = ctx->user_bufs[index]; | |
eae071c9 PB |
2955 | io_req_set_rsrc_node(req); |
2956 | } | |
2957 | ||
3529d8c2 JA |
2958 | req->rw.addr = READ_ONCE(sqe->addr); |
2959 | req->rw.len = READ_ONCE(sqe->len); | |
2b188cc1 | 2960 | return 0; |
2b188cc1 JA |
2961 | } |
2962 | ||
2963 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2964 | { | |
2965 | switch (ret) { | |
2966 | case -EIOCBQUEUED: | |
2967 | break; | |
2968 | case -ERESTARTSYS: | |
2969 | case -ERESTARTNOINTR: | |
2970 | case -ERESTARTNOHAND: | |
2971 | case -ERESTART_RESTARTBLOCK: | |
2972 | /* | |
2973 | * We can't just restart the syscall, since previously | |
2974 | * submitted sqes may already be in progress. Just fail this | |
2975 | * IO with EINTR. | |
2976 | */ | |
2977 | ret = -EINTR; | |
df561f66 | 2978 | fallthrough; |
2b188cc1 JA |
2979 | default: |
2980 | kiocb->ki_complete(kiocb, ret, 0); | |
2981 | } | |
2982 | } | |
2983 | ||
a1d7c393 | 2984 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
889fca73 | 2985 | unsigned int issue_flags) |
ba816ad6 | 2986 | { |
ba04291e | 2987 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2988 | struct io_async_rw *io = req->async_data; |
ba04291e | 2989 | |
227c0c96 | 2990 | /* add previously done IO, if any */ |
e8c2bc1f | 2991 | if (io && io->bytes_done > 0) { |
227c0c96 | 2992 | if (ret < 0) |
e8c2bc1f | 2993 | ret = io->bytes_done; |
227c0c96 | 2994 | else |
e8c2bc1f | 2995 | ret += io->bytes_done; |
227c0c96 JA |
2996 | } |
2997 | ||
ba04291e JA |
2998 | if (req->flags & REQ_F_CUR_POS) |
2999 | req->file->f_pos = kiocb->ki_pos; | |
b66ceaf3 | 3000 | if (ret >= 0 && (kiocb->ki_complete == io_complete_rw)) |
889fca73 | 3001 | __io_complete_rw(req, ret, 0, issue_flags); |
ba816ad6 JA |
3002 | else |
3003 | io_rw_done(kiocb, ret); | |
97284637 | 3004 | |
b66ceaf3 | 3005 | if (req->flags & REQ_F_REISSUE) { |
97284637 | 3006 | req->flags &= ~REQ_F_REISSUE; |
a7be7c23 | 3007 | if (io_resubmit_prep(req)) { |
773af691 | 3008 | io_req_task_queue_reissue(req); |
8c130827 | 3009 | } else { |
b66ceaf3 PB |
3010 | unsigned int cflags = io_put_rw_kbuf(req); |
3011 | struct io_ring_ctx *ctx = req->ctx; | |
3012 | ||
93d2bcd2 | 3013 | req_set_fail(req); |
14cfbb7a | 3014 | if (!(issue_flags & IO_URING_F_NONBLOCK)) { |
b66ceaf3 PB |
3015 | mutex_lock(&ctx->uring_lock); |
3016 | __io_req_complete(req, issue_flags, ret, cflags); | |
3017 | mutex_unlock(&ctx->uring_lock); | |
3018 | } else { | |
3019 | __io_req_complete(req, issue_flags, ret, cflags); | |
3020 | } | |
97284637 PB |
3021 | } |
3022 | } | |
ba816ad6 JA |
3023 | } |
3024 | ||
eae071c9 PB |
3025 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
3026 | struct io_mapped_ubuf *imu) | |
edafccee | 3027 | { |
9adbd45d | 3028 | size_t len = req->rw.len; |
75769e3f | 3029 | u64 buf_end, buf_addr = req->rw.addr; |
edafccee | 3030 | size_t offset; |
edafccee | 3031 | |
75769e3f | 3032 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
edafccee JA |
3033 | return -EFAULT; |
3034 | /* not inside the mapped region */ | |
4751f53d | 3035 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
edafccee JA |
3036 | return -EFAULT; |
3037 | ||
3038 | /* | |
3039 | * May not be a start of buffer, set size appropriately | |
3040 | * and advance us to the beginning. | |
3041 | */ | |
3042 | offset = buf_addr - imu->ubuf; | |
3043 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
3044 | |
3045 | if (offset) { | |
3046 | /* | |
3047 | * Don't use iov_iter_advance() here, as it's really slow for | |
3048 | * using the latter parts of a big fixed buffer - it iterates | |
3049 | * over each segment manually. We can cheat a bit here, because | |
3050 | * we know that: | |
3051 | * | |
3052 | * 1) it's a BVEC iter, we set it up | |
3053 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
3054 | * first and last bvec | |
3055 | * | |
3056 | * So just find our index, and adjust the iterator afterwards. | |
3057 | * If the offset is within the first bvec (or the whole first | |
3058 | * bvec, just use iov_iter_advance(). This makes it easier | |
3059 | * since we can just skip the first segment, which may not | |
3060 | * be PAGE_SIZE aligned. | |
3061 | */ | |
3062 | const struct bio_vec *bvec = imu->bvec; | |
3063 | ||
3064 | if (offset <= bvec->bv_len) { | |
3065 | iov_iter_advance(iter, offset); | |
3066 | } else { | |
3067 | unsigned long seg_skip; | |
3068 | ||
3069 | /* skip first vec */ | |
3070 | offset -= bvec->bv_len; | |
3071 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
3072 | ||
3073 | iter->bvec = bvec + seg_skip; | |
3074 | iter->nr_segs -= seg_skip; | |
99c79f66 | 3075 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 3076 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
3077 | } |
3078 | } | |
3079 | ||
847595de | 3080 | return 0; |
edafccee JA |
3081 | } |
3082 | ||
eae071c9 PB |
3083 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
3084 | { | |
bf1aa2d2 PB |
3085 | if (WARN_ON_ONCE(!req->imu)) |
3086 | return -EFAULT; | |
3087 | return __io_import_fixed(req, rw, iter, req->imu); | |
eae071c9 PB |
3088 | } |
3089 | ||
bcda7baa JA |
3090 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
3091 | { | |
3092 | if (needs_lock) | |
3093 | mutex_unlock(&ctx->uring_lock); | |
3094 | } | |
3095 | ||
3096 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
3097 | { | |
3098 | /* | |
3099 | * "Normal" inline submissions always hold the uring_lock, since we | |
3100 | * grab it from the system call. Same is true for the SQPOLL offload. | |
3101 | * The only exception is when we've detached the request and issue it | |
3102 | * from an async worker thread, grab the lock for that case. | |
3103 | */ | |
3104 | if (needs_lock) | |
3105 | mutex_lock(&ctx->uring_lock); | |
3106 | } | |
3107 | ||
3108 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
3109 | int bgid, struct io_buffer *kbuf, | |
3110 | bool needs_lock) | |
3111 | { | |
3112 | struct io_buffer *head; | |
3113 | ||
3114 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3115 | return kbuf; | |
3116 | ||
3117 | io_ring_submit_lock(req->ctx, needs_lock); | |
3118 | ||
3119 | lockdep_assert_held(&req->ctx->uring_lock); | |
3120 | ||
9e15c3a0 | 3121 | head = xa_load(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
3122 | if (head) { |
3123 | if (!list_empty(&head->list)) { | |
3124 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
3125 | list); | |
3126 | list_del(&kbuf->list); | |
3127 | } else { | |
3128 | kbuf = head; | |
9e15c3a0 | 3129 | xa_erase(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
3130 | } |
3131 | if (*len > kbuf->len) | |
3132 | *len = kbuf->len; | |
3133 | } else { | |
3134 | kbuf = ERR_PTR(-ENOBUFS); | |
3135 | } | |
3136 | ||
3137 | io_ring_submit_unlock(req->ctx, needs_lock); | |
3138 | ||
3139 | return kbuf; | |
3140 | } | |
3141 | ||
4d954c25 JA |
3142 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
3143 | bool needs_lock) | |
3144 | { | |
3145 | struct io_buffer *kbuf; | |
4f4eeba8 | 3146 | u16 bgid; |
4d954c25 JA |
3147 | |
3148 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
4f4eeba8 | 3149 | bgid = req->buf_index; |
4d954c25 JA |
3150 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
3151 | if (IS_ERR(kbuf)) | |
3152 | return kbuf; | |
3153 | req->rw.addr = (u64) (unsigned long) kbuf; | |
3154 | req->flags |= REQ_F_BUFFER_SELECTED; | |
3155 | return u64_to_user_ptr(kbuf->addr); | |
3156 | } | |
3157 | ||
3158 | #ifdef CONFIG_COMPAT | |
3159 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
3160 | bool needs_lock) | |
3161 | { | |
3162 | struct compat_iovec __user *uiov; | |
3163 | compat_ssize_t clen; | |
3164 | void __user *buf; | |
3165 | ssize_t len; | |
3166 | ||
3167 | uiov = u64_to_user_ptr(req->rw.addr); | |
3168 | if (!access_ok(uiov, sizeof(*uiov))) | |
3169 | return -EFAULT; | |
3170 | if (__get_user(clen, &uiov->iov_len)) | |
3171 | return -EFAULT; | |
3172 | if (clen < 0) | |
3173 | return -EINVAL; | |
3174 | ||
3175 | len = clen; | |
3176 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3177 | if (IS_ERR(buf)) | |
3178 | return PTR_ERR(buf); | |
3179 | iov[0].iov_base = buf; | |
3180 | iov[0].iov_len = (compat_size_t) len; | |
3181 | return 0; | |
3182 | } | |
3183 | #endif | |
3184 | ||
3185 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3186 | bool needs_lock) | |
3187 | { | |
3188 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3189 | void __user *buf; | |
3190 | ssize_t len; | |
3191 | ||
3192 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3193 | return -EFAULT; | |
3194 | ||
3195 | len = iov[0].iov_len; | |
3196 | if (len < 0) | |
3197 | return -EINVAL; | |
3198 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3199 | if (IS_ERR(buf)) | |
3200 | return PTR_ERR(buf); | |
3201 | iov[0].iov_base = buf; | |
3202 | iov[0].iov_len = len; | |
3203 | return 0; | |
3204 | } | |
3205 | ||
3206 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3207 | bool needs_lock) | |
3208 | { | |
dddb3e26 JA |
3209 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
3210 | struct io_buffer *kbuf; | |
3211 | ||
3212 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
3213 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); | |
3214 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3215 | return 0; |
dddb3e26 | 3216 | } |
dd201662 | 3217 | if (req->rw.len != 1) |
4d954c25 JA |
3218 | return -EINVAL; |
3219 | ||
3220 | #ifdef CONFIG_COMPAT | |
3221 | if (req->ctx->compat) | |
3222 | return io_compat_import(req, iov, needs_lock); | |
3223 | #endif | |
3224 | ||
3225 | return __io_iov_buffer_select(req, iov, needs_lock); | |
3226 | } | |
3227 | ||
847595de PB |
3228 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
3229 | struct iov_iter *iter, bool needs_lock) | |
2b188cc1 | 3230 | { |
9adbd45d JA |
3231 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
3232 | size_t sqe_len = req->rw.len; | |
847595de | 3233 | u8 opcode = req->opcode; |
4d954c25 | 3234 | ssize_t ret; |
edafccee | 3235 | |
7d009165 | 3236 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 3237 | *iovec = NULL; |
9adbd45d | 3238 | return io_import_fixed(req, rw, iter); |
edafccee | 3239 | } |
2b188cc1 | 3240 | |
bcda7baa | 3241 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 3242 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
3243 | return -EINVAL; |
3244 | ||
3a6820f2 | 3245 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3246 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 3247 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 3248 | if (IS_ERR(buf)) |
4d954c25 | 3249 | return PTR_ERR(buf); |
3f9d6441 | 3250 | req->rw.len = sqe_len; |
bcda7baa JA |
3251 | } |
3252 | ||
3a6820f2 JA |
3253 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
3254 | *iovec = NULL; | |
10fc72e4 | 3255 | return ret; |
3a6820f2 JA |
3256 | } |
3257 | ||
4d954c25 JA |
3258 | if (req->flags & REQ_F_BUFFER_SELECT) { |
3259 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
847595de PB |
3260 | if (!ret) |
3261 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); | |
4d954c25 JA |
3262 | *iovec = NULL; |
3263 | return ret; | |
3264 | } | |
3265 | ||
89cd35c5 CH |
3266 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
3267 | req->ctx->compat); | |
2b188cc1 JA |
3268 | } |
3269 | ||
0fef9483 JA |
3270 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3271 | { | |
5b09e37e | 3272 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3273 | } |
3274 | ||
31b51510 | 3275 | /* |
32960613 JA |
3276 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3277 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3278 | */ |
4017eb91 | 3279 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3280 | { |
4017eb91 JA |
3281 | struct kiocb *kiocb = &req->rw.kiocb; |
3282 | struct file *file = req->file; | |
32960613 JA |
3283 | ssize_t ret = 0; |
3284 | ||
3285 | /* | |
3286 | * Don't support polled IO through this interface, and we can't | |
3287 | * support non-blocking either. For the latter, this just causes | |
3288 | * the kiocb to be handled from an async context. | |
3289 | */ | |
3290 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3291 | return -EOPNOTSUPP; | |
3292 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3293 | return -EAGAIN; | |
3294 | ||
3295 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3296 | struct iovec iovec; |
32960613 JA |
3297 | ssize_t nr; |
3298 | ||
311ae9e1 PB |
3299 | if (!iov_iter_is_bvec(iter)) { |
3300 | iovec = iov_iter_iovec(iter); | |
3301 | } else { | |
4017eb91 JA |
3302 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3303 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3304 | } |
3305 | ||
32960613 JA |
3306 | if (rw == READ) { |
3307 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3308 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3309 | } else { |
3310 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3311 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3312 | } |
3313 | ||
3314 | if (nr < 0) { | |
3315 | if (!ret) | |
3316 | ret = nr; | |
3317 | break; | |
3318 | } | |
cf15ae0a | 3319 | ret += nr; |
16c8d2df JA |
3320 | if (!iov_iter_is_bvec(iter)) { |
3321 | iov_iter_advance(iter, nr); | |
3322 | } else { | |
16c8d2df | 3323 | req->rw.addr += nr; |
cf15ae0a JA |
3324 | req->rw.len -= nr; |
3325 | if (!req->rw.len) | |
3326 | break; | |
16c8d2df | 3327 | } |
32960613 JA |
3328 | if (nr != iovec.iov_len) |
3329 | break; | |
32960613 JA |
3330 | } |
3331 | ||
3332 | return ret; | |
3333 | } | |
3334 | ||
ff6165b2 JA |
3335 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3336 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3337 | { |
e8c2bc1f | 3338 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3339 | |
ff6165b2 | 3340 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3341 | rw->free_iovec = iovec; |
227c0c96 | 3342 | rw->bytes_done = 0; |
ff6165b2 | 3343 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3344 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3345 | return; |
b64e3444 | 3346 | if (!iovec) { |
ff6165b2 JA |
3347 | unsigned iov_off = 0; |
3348 | ||
3349 | rw->iter.iov = rw->fast_iov; | |
3350 | if (iter->iov != fast_iov) { | |
3351 | iov_off = iter->iov - fast_iov; | |
3352 | rw->iter.iov += iov_off; | |
3353 | } | |
3354 | if (rw->fast_iov != fast_iov) | |
3355 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3356 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3357 | } else { |
3358 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3359 | } |
3360 | } | |
3361 | ||
6cb78689 | 3362 | static inline int io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3363 | { |
e8c2bc1f JA |
3364 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3365 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3366 | return req->async_data == NULL; | |
3d9932a8 XW |
3367 | } |
3368 | ||
ff6165b2 JA |
3369 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3370 | const struct iovec *fast_iov, | |
227c0c96 | 3371 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3372 | { |
26f0505a | 3373 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
74566df3 | 3374 | return 0; |
e8c2bc1f | 3375 | if (!req->async_data) { |
cd658695 JA |
3376 | struct io_async_rw *iorw; |
3377 | ||
6cb78689 | 3378 | if (io_alloc_async_data(req)) { |
6bf985dc | 3379 | kfree(iovec); |
5d204bcf | 3380 | return -ENOMEM; |
6bf985dc | 3381 | } |
b7bb4f7d | 3382 | |
ff6165b2 | 3383 | io_req_map_rw(req, iovec, fast_iov, iter); |
cd658695 JA |
3384 | iorw = req->async_data; |
3385 | /* we've copied and mapped the iter, ensure state is saved */ | |
3386 | iov_iter_save_state(&iorw->iter, &iorw->iter_state); | |
5d204bcf | 3387 | } |
b7bb4f7d | 3388 | return 0; |
f67676d1 JA |
3389 | } |
3390 | ||
73debe68 | 3391 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3392 | { |
e8c2bc1f | 3393 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3394 | struct iovec *iov = iorw->fast_iov; |
847595de | 3395 | int ret; |
c3e330a4 | 3396 | |
2846c481 | 3397 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
c3e330a4 PB |
3398 | if (unlikely(ret < 0)) |
3399 | return ret; | |
3400 | ||
ab0b196c PB |
3401 | iorw->bytes_done = 0; |
3402 | iorw->free_iovec = iov; | |
3403 | if (iov) | |
3404 | req->flags |= REQ_F_NEED_CLEANUP; | |
cd658695 | 3405 | iov_iter_save_state(&iorw->iter, &iorw->iter_state); |
c3e330a4 PB |
3406 | return 0; |
3407 | } | |
3408 | ||
73debe68 | 3409 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3410 | { |
3529d8c2 JA |
3411 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3412 | return -EBADF; | |
5d329e12 | 3413 | return io_prep_rw(req, sqe, READ); |
f67676d1 JA |
3414 | } |
3415 | ||
c1dd91d1 JA |
3416 | /* |
3417 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3418 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3419 | * This gets called when the page is unlocked, and we generally expect that to | |
3420 | * happen when the page IO is completed and the page is now uptodate. This will | |
3421 | * queue a task_work based retry of the operation, attempting to copy the data | |
3422 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3423 | * do a thread based blocking retry of the operation. That's the unexpected | |
3424 | * slow path. | |
3425 | */ | |
bcf5a063 JA |
3426 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3427 | int sync, void *arg) | |
3428 | { | |
3429 | struct wait_page_queue *wpq; | |
3430 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3431 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3432 | |
3433 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3434 | ||
cdc8fcb4 LT |
3435 | if (!wake_page_match(wpq, key)) |
3436 | return 0; | |
3437 | ||
c8d317aa | 3438 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 | 3439 | list_del_init(&wait->entry); |
921b9054 | 3440 | io_req_task_queue(req); |
bcf5a063 JA |
3441 | return 1; |
3442 | } | |
3443 | ||
c1dd91d1 JA |
3444 | /* |
3445 | * This controls whether a given IO request should be armed for async page | |
3446 | * based retry. If we return false here, the request is handed to the async | |
3447 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3448 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3449 | * will either succeed because the page is now uptodate and unlocked, or it | |
3450 | * will register a callback when the page is unlocked at IO completion. Through | |
3451 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3452 | * That retry will attempt the buffered read again. The retry will generally | |
3453 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3454 | * async worker threads for a blocking retry. | |
3455 | */ | |
227c0c96 | 3456 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3457 | { |
e8c2bc1f JA |
3458 | struct io_async_rw *rw = req->async_data; |
3459 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3460 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3461 | |
bcf5a063 JA |
3462 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3463 | if (req->flags & REQ_F_NOWAIT) | |
3464 | return false; | |
f67676d1 | 3465 | |
227c0c96 | 3466 | /* Only for buffered IO */ |
3b2a4439 | 3467 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3468 | return false; |
3b2a4439 | 3469 | |
bcf5a063 JA |
3470 | /* |
3471 | * just use poll if we can, and don't attempt if the fs doesn't | |
3472 | * support callback based unlocks | |
3473 | */ | |
3474 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3475 | return false; | |
f67676d1 | 3476 | |
3b2a4439 JA |
3477 | wait->wait.func = io_async_buf_func; |
3478 | wait->wait.private = req; | |
3479 | wait->wait.flags = 0; | |
3480 | INIT_LIST_HEAD(&wait->wait.entry); | |
3481 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3482 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3483 | kiocb->ki_waitq = wait; |
3b2a4439 | 3484 | return true; |
bcf5a063 JA |
3485 | } |
3486 | ||
aeab9506 | 3487 | static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
bcf5a063 JA |
3488 | { |
3489 | if (req->file->f_op->read_iter) | |
3490 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d | 3491 | else if (req->file->f_op->read) |
4017eb91 | 3492 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3493 | else |
3494 | return -EINVAL; | |
f67676d1 JA |
3495 | } |
3496 | ||
7db30437 ML |
3497 | static bool need_read_all(struct io_kiocb *req) |
3498 | { | |
3499 | return req->flags & REQ_F_ISREG || | |
3500 | S_ISBLK(file_inode(req->file)->i_mode); | |
3501 | } | |
3502 | ||
889fca73 | 3503 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3504 | { |
3505 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3506 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3507 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3508 | struct io_async_rw *rw = req->async_data; |
45d189c6 | 3509 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
cd658695 JA |
3510 | struct iov_iter_state __state, *state; |
3511 | ssize_t ret, ret2; | |
ff6165b2 | 3512 | |
2846c481 | 3513 | if (rw) { |
e8c2bc1f | 3514 | iter = &rw->iter; |
cd658695 JA |
3515 | state = &rw->iter_state; |
3516 | /* | |
3517 | * We come here from an earlier attempt, restore our state to | |
3518 | * match in case it doesn't. It's cheap enough that we don't | |
3519 | * need to make this conditional. | |
3520 | */ | |
3521 | iov_iter_restore(iter, state); | |
2846c481 PB |
3522 | iovec = NULL; |
3523 | } else { | |
3524 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); | |
3525 | if (ret < 0) | |
3526 | return ret; | |
cd658695 JA |
3527 | state = &__state; |
3528 | iov_iter_save_state(iter, state); | |
2846c481 | 3529 | } |
cd658695 | 3530 | req->result = iov_iter_count(iter); |
2b188cc1 | 3531 | |
fd6c2e4c JA |
3532 | /* Ensure we clear previously set non-block flag */ |
3533 | if (!force_nonblock) | |
29de5f6a | 3534 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
a88fc400 PB |
3535 | else |
3536 | kiocb->ki_flags |= IOCB_NOWAIT; | |
3537 | ||
24c74678 | 3538 | /* If the file doesn't support async, just async punt */ |
b191e2df | 3539 | if (force_nonblock && !io_file_supports_nowait(req, READ)) { |
6713e7a6 | 3540 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc | 3541 | return ret ?: -EAGAIN; |
6713e7a6 | 3542 | } |
9e645e11 | 3543 | |
cd658695 | 3544 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), req->result); |
5ea5dd45 PB |
3545 | if (unlikely(ret)) { |
3546 | kfree(iovec); | |
3547 | return ret; | |
3548 | } | |
2b188cc1 | 3549 | |
227c0c96 | 3550 | ret = io_iter_do_read(req, iter); |
32960613 | 3551 | |
230d50d4 | 3552 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
6ad7f233 | 3553 | req->flags &= ~REQ_F_REISSUE; |
eefdf30f JA |
3554 | /* IOPOLL retry should happen for io-wq threads */ |
3555 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3556 | goto done; |
75c668cd PB |
3557 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3558 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3559 | goto done; |
f38c7e3a | 3560 | ret = 0; |
230d50d4 JA |
3561 | } else if (ret == -EIOCBQUEUED) { |
3562 | goto out_free; | |
cd658695 | 3563 | } else if (ret <= 0 || ret == req->result || !force_nonblock || |
7db30437 | 3564 | (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) { |
7335e3bf | 3565 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3566 | goto done; |
227c0c96 JA |
3567 | } |
3568 | ||
cd658695 JA |
3569 | /* |
3570 | * Don't depend on the iter state matching what was consumed, or being | |
3571 | * untouched in case of error. Restore it and we'll advance it | |
3572 | * manually if we need to. | |
3573 | */ | |
3574 | iov_iter_restore(iter, state); | |
3575 | ||
227c0c96 | 3576 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc PB |
3577 | if (ret2) |
3578 | return ret2; | |
3579 | ||
fe1cdd55 | 3580 | iovec = NULL; |
e8c2bc1f | 3581 | rw = req->async_data; |
cd658695 JA |
3582 | /* |
3583 | * Now use our persistent iterator and state, if we aren't already. | |
3584 | * We've restored and mapped the iter to match. | |
3585 | */ | |
3586 | if (iter != &rw->iter) { | |
3587 | iter = &rw->iter; | |
3588 | state = &rw->iter_state; | |
3589 | } | |
227c0c96 | 3590 | |
b23df91b | 3591 | do { |
cd658695 JA |
3592 | /* |
3593 | * We end up here because of a partial read, either from | |
3594 | * above or inside this loop. Advance the iter by the bytes | |
3595 | * that were consumed. | |
3596 | */ | |
3597 | iov_iter_advance(iter, ret); | |
3598 | if (!iov_iter_count(iter)) | |
3599 | break; | |
b23df91b | 3600 | rw->bytes_done += ret; |
cd658695 JA |
3601 | iov_iter_save_state(iter, state); |
3602 | ||
b23df91b PB |
3603 | /* if we can retry, do so with the callbacks armed */ |
3604 | if (!io_rw_should_retry(req)) { | |
3605 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3606 | return -EAGAIN; | |
3607 | } | |
3608 | ||
3609 | /* | |
3610 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3611 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3612 | * desired page gets unlocked. We can also get a partial read | |
3613 | * here, and if we do, then just retry at the new offset. | |
3614 | */ | |
3615 | ret = io_iter_do_read(req, iter); | |
3616 | if (ret == -EIOCBQUEUED) | |
3617 | return 0; | |
227c0c96 | 3618 | /* we got some bytes, but not all. retry. */ |
b5b0ecb7 | 3619 | kiocb->ki_flags &= ~IOCB_WAITQ; |
cd658695 JA |
3620 | iov_iter_restore(iter, state); |
3621 | } while (ret > 0); | |
227c0c96 | 3622 | done: |
889fca73 | 3623 | kiocb_done(kiocb, ret, issue_flags); |
fe1cdd55 PB |
3624 | out_free: |
3625 | /* it's faster to check here then delegate to kfree */ | |
3626 | if (iovec) | |
3627 | kfree(iovec); | |
5ea5dd45 | 3628 | return 0; |
2b188cc1 JA |
3629 | } |
3630 | ||
73debe68 | 3631 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3632 | { |
3529d8c2 JA |
3633 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3634 | return -EBADF; | |
5d329e12 | 3635 | return io_prep_rw(req, sqe, WRITE); |
f67676d1 JA |
3636 | } |
3637 | ||
889fca73 | 3638 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3639 | { |
3640 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3641 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3642 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3643 | struct io_async_rw *rw = req->async_data; |
45d189c6 | 3644 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
cd658695 JA |
3645 | struct iov_iter_state __state, *state; |
3646 | ssize_t ret, ret2; | |
2b188cc1 | 3647 | |
2846c481 | 3648 | if (rw) { |
e8c2bc1f | 3649 | iter = &rw->iter; |
cd658695 JA |
3650 | state = &rw->iter_state; |
3651 | iov_iter_restore(iter, state); | |
2846c481 PB |
3652 | iovec = NULL; |
3653 | } else { | |
3654 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
3655 | if (ret < 0) | |
3656 | return ret; | |
cd658695 JA |
3657 | state = &__state; |
3658 | iov_iter_save_state(iter, state); | |
2846c481 | 3659 | } |
cd658695 | 3660 | req->result = iov_iter_count(iter); |
2b188cc1 | 3661 | |
fd6c2e4c JA |
3662 | /* Ensure we clear previously set non-block flag */ |
3663 | if (!force_nonblock) | |
a88fc400 PB |
3664 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3665 | else | |
3666 | kiocb->ki_flags |= IOCB_NOWAIT; | |
fd6c2e4c | 3667 | |
24c74678 | 3668 | /* If the file doesn't support async, just async punt */ |
b191e2df | 3669 | if (force_nonblock && !io_file_supports_nowait(req, WRITE)) |
f67676d1 | 3670 | goto copy_iov; |
31b51510 | 3671 | |
10d59345 JA |
3672 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3673 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3674 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3675 | goto copy_iov; |
31b51510 | 3676 | |
cd658695 | 3677 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), req->result); |
fa15bafb PB |
3678 | if (unlikely(ret)) |
3679 | goto out_free; | |
4ed734b0 | 3680 | |
fa15bafb PB |
3681 | /* |
3682 | * Open-code file_start_write here to grab freeze protection, | |
3683 | * which will be released by another thread in | |
3684 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3685 | * released so that it doesn't complain about the held lock when | |
3686 | * we return to userspace. | |
3687 | */ | |
3688 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3689 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3690 | __sb_writers_release(file_inode(req->file)->i_sb, |
3691 | SB_FREEZE_WRITE); | |
3692 | } | |
3693 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3694 | |
fa15bafb | 3695 | if (req->file->f_op->write_iter) |
ff6165b2 | 3696 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3697 | else if (req->file->f_op->write) |
4017eb91 | 3698 | ret2 = loop_rw_iter(WRITE, req, iter); |
2dd2111d GH |
3699 | else |
3700 | ret2 = -EINVAL; | |
4ed734b0 | 3701 | |
6ad7f233 PB |
3702 | if (req->flags & REQ_F_REISSUE) { |
3703 | req->flags &= ~REQ_F_REISSUE; | |
230d50d4 | 3704 | ret2 = -EAGAIN; |
6ad7f233 | 3705 | } |
230d50d4 | 3706 | |
fa15bafb PB |
3707 | /* |
3708 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3709 | * retry them without IOCB_NOWAIT. | |
3710 | */ | |
3711 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3712 | ret2 = -EAGAIN; | |
75c668cd PB |
3713 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3714 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3715 | goto done; |
fa15bafb | 3716 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3717 | /* IOPOLL retry should happen for io-wq threads */ |
3718 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3719 | goto copy_iov; | |
355afaeb | 3720 | done: |
889fca73 | 3721 | kiocb_done(kiocb, ret2, issue_flags); |
fa15bafb | 3722 | } else { |
f67676d1 | 3723 | copy_iov: |
cd658695 | 3724 | iov_iter_restore(iter, state); |
227c0c96 | 3725 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
6bf985dc | 3726 | return ret ?: -EAGAIN; |
2b188cc1 | 3727 | } |
31b51510 | 3728 | out_free: |
f261c168 | 3729 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3730 | if (iovec) |
6f2cc166 | 3731 | kfree(iovec); |
2b188cc1 JA |
3732 | return ret; |
3733 | } | |
3734 | ||
80a261fd JA |
3735 | static int io_renameat_prep(struct io_kiocb *req, |
3736 | const struct io_uring_sqe *sqe) | |
3737 | { | |
3738 | struct io_rename *ren = &req->rename; | |
3739 | const char __user *oldf, *newf; | |
3740 | ||
ed7eb259 JA |
3741 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3742 | return -EINVAL; | |
26578cda | 3743 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
ed7eb259 | 3744 | return -EINVAL; |
80a261fd JA |
3745 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3746 | return -EBADF; | |
3747 | ||
3748 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3749 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3750 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3751 | ren->new_dfd = READ_ONCE(sqe->len); | |
3752 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3753 | ||
3754 | ren->oldpath = getname(oldf); | |
3755 | if (IS_ERR(ren->oldpath)) | |
3756 | return PTR_ERR(ren->oldpath); | |
3757 | ||
3758 | ren->newpath = getname(newf); | |
3759 | if (IS_ERR(ren->newpath)) { | |
3760 | putname(ren->oldpath); | |
3761 | return PTR_ERR(ren->newpath); | |
3762 | } | |
3763 | ||
3764 | req->flags |= REQ_F_NEED_CLEANUP; | |
3765 | return 0; | |
3766 | } | |
3767 | ||
45d189c6 | 3768 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
3769 | { |
3770 | struct io_rename *ren = &req->rename; | |
3771 | int ret; | |
3772 | ||
45d189c6 | 3773 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
3774 | return -EAGAIN; |
3775 | ||
3776 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3777 | ren->newpath, ren->flags); | |
3778 | ||
3779 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3780 | if (ret < 0) | |
93d2bcd2 | 3781 | req_set_fail(req); |
80a261fd JA |
3782 | io_req_complete(req, ret); |
3783 | return 0; | |
3784 | } | |
3785 | ||
14a1143b JA |
3786 | static int io_unlinkat_prep(struct io_kiocb *req, |
3787 | const struct io_uring_sqe *sqe) | |
3788 | { | |
3789 | struct io_unlink *un = &req->unlink; | |
3790 | const char __user *fname; | |
3791 | ||
22634bc5 JA |
3792 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3793 | return -EINVAL; | |
26578cda PB |
3794 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
3795 | sqe->splice_fd_in) | |
22634bc5 | 3796 | return -EINVAL; |
14a1143b JA |
3797 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3798 | return -EBADF; | |
3799 | ||
3800 | un->dfd = READ_ONCE(sqe->fd); | |
3801 | ||
3802 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3803 | if (un->flags & ~AT_REMOVEDIR) | |
3804 | return -EINVAL; | |
3805 | ||
3806 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3807 | un->filename = getname(fname); | |
3808 | if (IS_ERR(un->filename)) | |
3809 | return PTR_ERR(un->filename); | |
3810 | ||
3811 | req->flags |= REQ_F_NEED_CLEANUP; | |
3812 | return 0; | |
3813 | } | |
3814 | ||
45d189c6 | 3815 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
3816 | { |
3817 | struct io_unlink *un = &req->unlink; | |
3818 | int ret; | |
3819 | ||
45d189c6 | 3820 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
3821 | return -EAGAIN; |
3822 | ||
3823 | if (un->flags & AT_REMOVEDIR) | |
3824 | ret = do_rmdir(un->dfd, un->filename); | |
3825 | else | |
3826 | ret = do_unlinkat(un->dfd, un->filename); | |
3827 | ||
3828 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3829 | if (ret < 0) | |
93d2bcd2 | 3830 | req_set_fail(req); |
14a1143b JA |
3831 | io_req_complete(req, ret); |
3832 | return 0; | |
3833 | } | |
3834 | ||
e34a02dc DK |
3835 | static int io_mkdirat_prep(struct io_kiocb *req, |
3836 | const struct io_uring_sqe *sqe) | |
3837 | { | |
3838 | struct io_mkdir *mkd = &req->mkdir; | |
3839 | const char __user *fname; | |
3840 | ||
3841 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3842 | return -EINVAL; | |
3843 | if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index || | |
3844 | sqe->splice_fd_in) | |
3845 | return -EINVAL; | |
3846 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3847 | return -EBADF; | |
3848 | ||
3849 | mkd->dfd = READ_ONCE(sqe->fd); | |
3850 | mkd->mode = READ_ONCE(sqe->len); | |
3851 | ||
3852 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3853 | mkd->filename = getname(fname); | |
3854 | if (IS_ERR(mkd->filename)) | |
3855 | return PTR_ERR(mkd->filename); | |
3856 | ||
3857 | req->flags |= REQ_F_NEED_CLEANUP; | |
3858 | return 0; | |
3859 | } | |
3860 | ||
3861 | static int io_mkdirat(struct io_kiocb *req, int issue_flags) | |
3862 | { | |
3863 | struct io_mkdir *mkd = &req->mkdir; | |
3864 | int ret; | |
3865 | ||
3866 | if (issue_flags & IO_URING_F_NONBLOCK) | |
3867 | return -EAGAIN; | |
3868 | ||
3869 | ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode); | |
3870 | ||
3871 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3872 | if (ret < 0) | |
3873 | req_set_fail(req); | |
3874 | io_req_complete(req, ret); | |
3875 | return 0; | |
3876 | } | |
3877 | ||
7a8721f8 DK |
3878 | static int io_symlinkat_prep(struct io_kiocb *req, |
3879 | const struct io_uring_sqe *sqe) | |
3880 | { | |
3881 | struct io_symlink *sl = &req->symlink; | |
3882 | const char __user *oldpath, *newpath; | |
3883 | ||
3884 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3885 | return -EINVAL; | |
3886 | if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index || | |
3887 | sqe->splice_fd_in) | |
3888 | return -EINVAL; | |
3889 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3890 | return -EBADF; | |
3891 | ||
3892 | sl->new_dfd = READ_ONCE(sqe->fd); | |
3893 | oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3894 | newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3895 | ||
3896 | sl->oldpath = getname(oldpath); | |
3897 | if (IS_ERR(sl->oldpath)) | |
3898 | return PTR_ERR(sl->oldpath); | |
3899 | ||
3900 | sl->newpath = getname(newpath); | |
3901 | if (IS_ERR(sl->newpath)) { | |
3902 | putname(sl->oldpath); | |
3903 | return PTR_ERR(sl->newpath); | |
3904 | } | |
3905 | ||
3906 | req->flags |= REQ_F_NEED_CLEANUP; | |
3907 | return 0; | |
3908 | } | |
3909 | ||
3910 | static int io_symlinkat(struct io_kiocb *req, int issue_flags) | |
3911 | { | |
3912 | struct io_symlink *sl = &req->symlink; | |
3913 | int ret; | |
3914 | ||
3915 | if (issue_flags & IO_URING_F_NONBLOCK) | |
3916 | return -EAGAIN; | |
3917 | ||
3918 | ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath); | |
3919 | ||
3920 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3921 | if (ret < 0) | |
3922 | req_set_fail(req); | |
3923 | io_req_complete(req, ret); | |
3924 | return 0; | |
3925 | } | |
3926 | ||
cf30da90 DK |
3927 | static int io_linkat_prep(struct io_kiocb *req, |
3928 | const struct io_uring_sqe *sqe) | |
3929 | { | |
3930 | struct io_hardlink *lnk = &req->hardlink; | |
3931 | const char __user *oldf, *newf; | |
3932 | ||
3933 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3934 | return -EINVAL; | |
3935 | if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) | |
3936 | return -EINVAL; | |
3937 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3938 | return -EBADF; | |
3939 | ||
3940 | lnk->old_dfd = READ_ONCE(sqe->fd); | |
3941 | lnk->new_dfd = READ_ONCE(sqe->len); | |
3942 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3943 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3944 | lnk->flags = READ_ONCE(sqe->hardlink_flags); | |
3945 | ||
3946 | lnk->oldpath = getname(oldf); | |
3947 | if (IS_ERR(lnk->oldpath)) | |
3948 | return PTR_ERR(lnk->oldpath); | |
3949 | ||
3950 | lnk->newpath = getname(newf); | |
3951 | if (IS_ERR(lnk->newpath)) { | |
3952 | putname(lnk->oldpath); | |
3953 | return PTR_ERR(lnk->newpath); | |
3954 | } | |
3955 | ||
3956 | req->flags |= REQ_F_NEED_CLEANUP; | |
3957 | return 0; | |
3958 | } | |
3959 | ||
3960 | static int io_linkat(struct io_kiocb *req, int issue_flags) | |
3961 | { | |
3962 | struct io_hardlink *lnk = &req->hardlink; | |
3963 | int ret; | |
3964 | ||
3965 | if (issue_flags & IO_URING_F_NONBLOCK) | |
3966 | return -EAGAIN; | |
3967 | ||
3968 | ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd, | |
3969 | lnk->newpath, lnk->flags); | |
3970 | ||
3971 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3972 | if (ret < 0) | |
3973 | req_set_fail(req); | |
3974 | io_req_complete(req, ret); | |
3975 | return 0; | |
3976 | } | |
3977 | ||
36f4fa68 JA |
3978 | static int io_shutdown_prep(struct io_kiocb *req, |
3979 | const struct io_uring_sqe *sqe) | |
3980 | { | |
3981 | #if defined(CONFIG_NET) | |
3982 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3983 | return -EINVAL; | |
26578cda PB |
3984 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
3985 | sqe->buf_index || sqe->splice_fd_in)) | |
36f4fa68 JA |
3986 | return -EINVAL; |
3987 | ||
3988 | req->shutdown.how = READ_ONCE(sqe->len); | |
3989 | return 0; | |
3990 | #else | |
3991 | return -EOPNOTSUPP; | |
3992 | #endif | |
3993 | } | |
3994 | ||
45d189c6 | 3995 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
3996 | { |
3997 | #if defined(CONFIG_NET) | |
3998 | struct socket *sock; | |
3999 | int ret; | |
4000 | ||
45d189c6 | 4001 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
4002 | return -EAGAIN; |
4003 | ||
48aba79b | 4004 | sock = sock_from_file(req->file); |
36f4fa68 | 4005 | if (unlikely(!sock)) |
48aba79b | 4006 | return -ENOTSOCK; |
36f4fa68 JA |
4007 | |
4008 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d | 4009 | if (ret < 0) |
93d2bcd2 | 4010 | req_set_fail(req); |
36f4fa68 JA |
4011 | io_req_complete(req, ret); |
4012 | return 0; | |
4013 | #else | |
4014 | return -EOPNOTSUPP; | |
4015 | #endif | |
4016 | } | |
4017 | ||
f2a8d5c7 PB |
4018 | static int __io_splice_prep(struct io_kiocb *req, |
4019 | const struct io_uring_sqe *sqe) | |
7d67af2c | 4020 | { |
fe7e3257 | 4021 | struct io_splice *sp = &req->splice; |
7d67af2c | 4022 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
7d67af2c | 4023 | |
3232dd02 PB |
4024 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4025 | return -EINVAL; | |
7d67af2c | 4026 | |
7d67af2c PB |
4027 | sp->len = READ_ONCE(sqe->len); |
4028 | sp->flags = READ_ONCE(sqe->splice_flags); | |
7d67af2c PB |
4029 | if (unlikely(sp->flags & ~valid_flags)) |
4030 | return -EINVAL; | |
dfadddfc | 4031 | sp->splice_fd_in = READ_ONCE(sqe->splice_fd_in); |
7d67af2c PB |
4032 | return 0; |
4033 | } | |
4034 | ||
f2a8d5c7 PB |
4035 | static int io_tee_prep(struct io_kiocb *req, |
4036 | const struct io_uring_sqe *sqe) | |
4037 | { | |
4038 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
4039 | return -EINVAL; | |
4040 | return __io_splice_prep(req, sqe); | |
4041 | } | |
4042 | ||
45d189c6 | 4043 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
4044 | { |
4045 | struct io_splice *sp = &req->splice; | |
f2a8d5c7 PB |
4046 | struct file *out = sp->file_out; |
4047 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
dfadddfc | 4048 | struct file *in; |
f2a8d5c7 PB |
4049 | long ret = 0; |
4050 | ||
45d189c6 | 4051 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 | 4052 | return -EAGAIN; |
dfadddfc JA |
4053 | |
4054 | in = io_file_get(req->ctx, req, sp->splice_fd_in, | |
4055 | (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
4056 | if (!in) { | |
4057 | ret = -EBADF; | |
4058 | goto done; | |
4059 | } | |
4060 | ||
f2a8d5c7 PB |
4061 | if (sp->len) |
4062 | ret = do_tee(in, out, sp->len, flags); | |
4063 | ||
e1d767f0 PB |
4064 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4065 | io_put_file(in); | |
dfadddfc | 4066 | done: |
f2a8d5c7 | 4067 | if (ret != sp->len) |
93d2bcd2 | 4068 | req_set_fail(req); |
e1e16097 | 4069 | io_req_complete(req, ret); |
f2a8d5c7 PB |
4070 | return 0; |
4071 | } | |
4072 | ||
4073 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4074 | { | |
fe7e3257 | 4075 | struct io_splice *sp = &req->splice; |
f2a8d5c7 PB |
4076 | |
4077 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
4078 | sp->off_out = READ_ONCE(sqe->off); | |
4079 | return __io_splice_prep(req, sqe); | |
4080 | } | |
4081 | ||
45d189c6 | 4082 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
4083 | { |
4084 | struct io_splice *sp = &req->splice; | |
7d67af2c PB |
4085 | struct file *out = sp->file_out; |
4086 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
4087 | loff_t *poff_in, *poff_out; | |
dfadddfc | 4088 | struct file *in; |
c9687426 | 4089 | long ret = 0; |
7d67af2c | 4090 | |
45d189c6 | 4091 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 4092 | return -EAGAIN; |
7d67af2c | 4093 | |
dfadddfc JA |
4094 | in = io_file_get(req->ctx, req, sp->splice_fd_in, |
4095 | (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
4096 | if (!in) { | |
4097 | ret = -EBADF; | |
4098 | goto done; | |
4099 | } | |
4100 | ||
7d67af2c PB |
4101 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
4102 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 4103 | |
948a7749 | 4104 | if (sp->len) |
c9687426 | 4105 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c | 4106 | |
e1d767f0 PB |
4107 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4108 | io_put_file(in); | |
dfadddfc | 4109 | done: |
7d67af2c | 4110 | if (ret != sp->len) |
93d2bcd2 | 4111 | req_set_fail(req); |
e1e16097 | 4112 | io_req_complete(req, ret); |
7d67af2c PB |
4113 | return 0; |
4114 | } | |
4115 | ||
2b188cc1 JA |
4116 | /* |
4117 | * IORING_OP_NOP just posts a completion event, nothing else. | |
4118 | */ | |
889fca73 | 4119 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
4120 | { |
4121 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 4122 | |
def596e9 JA |
4123 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4124 | return -EINVAL; | |
4125 | ||
889fca73 | 4126 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
4127 | return 0; |
4128 | } | |
4129 | ||
1155c76a | 4130 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 4131 | { |
6b06314c | 4132 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 4133 | |
6b06314c | 4134 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 4135 | return -EINVAL; |
26578cda PB |
4136 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4137 | sqe->splice_fd_in)) | |
c992fe29 CH |
4138 | return -EINVAL; |
4139 | ||
8ed8d3c3 JA |
4140 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
4141 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
4142 | return -EINVAL; | |
4143 | ||
4144 | req->sync.off = READ_ONCE(sqe->off); | |
4145 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
4146 | return 0; |
4147 | } | |
4148 | ||
45d189c6 | 4149 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4150 | { |
8ed8d3c3 | 4151 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
4152 | int ret; |
4153 | ||
ac45abc0 | 4154 | /* fsync always requires a blocking context */ |
45d189c6 | 4155 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4156 | return -EAGAIN; |
4157 | ||
9adbd45d | 4158 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
4159 | end > 0 ? end : LLONG_MAX, |
4160 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
4161 | if (ret < 0) | |
93d2bcd2 | 4162 | req_set_fail(req); |
e1e16097 | 4163 | io_req_complete(req, ret); |
c992fe29 CH |
4164 | return 0; |
4165 | } | |
4166 | ||
d63d1b5e JA |
4167 | static int io_fallocate_prep(struct io_kiocb *req, |
4168 | const struct io_uring_sqe *sqe) | |
4169 | { | |
26578cda PB |
4170 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
4171 | sqe->splice_fd_in) | |
d63d1b5e | 4172 | return -EINVAL; |
3232dd02 PB |
4173 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4174 | return -EINVAL; | |
d63d1b5e JA |
4175 | |
4176 | req->sync.off = READ_ONCE(sqe->off); | |
4177 | req->sync.len = READ_ONCE(sqe->addr); | |
4178 | req->sync.mode = READ_ONCE(sqe->len); | |
4179 | return 0; | |
4180 | } | |
4181 | ||
45d189c6 | 4182 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 4183 | { |
ac45abc0 PB |
4184 | int ret; |
4185 | ||
d63d1b5e | 4186 | /* fallocate always requiring blocking context */ |
45d189c6 | 4187 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 4188 | return -EAGAIN; |
ac45abc0 PB |
4189 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
4190 | req->sync.len); | |
ac45abc0 | 4191 | if (ret < 0) |
93d2bcd2 | 4192 | req_set_fail(req); |
48a904e2 JA |
4193 | else |
4194 | fsnotify_modify(req->file); | |
e1e16097 | 4195 | io_req_complete(req, ret); |
5d17b4a4 JA |
4196 | return 0; |
4197 | } | |
4198 | ||
ec65fea5 | 4199 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 4200 | { |
f8748881 | 4201 | const char __user *fname; |
15b71abe | 4202 | int ret; |
b7bb4f7d | 4203 | |
d3fddf6d PB |
4204 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4205 | return -EINVAL; | |
b9445598 | 4206 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 4207 | return -EINVAL; |
ec65fea5 | 4208 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 4209 | return -EBADF; |
03b1230c | 4210 | |
ec65fea5 PB |
4211 | /* open.how should be already initialised */ |
4212 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 4213 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 4214 | |
25e72d10 PB |
4215 | req->open.dfd = READ_ONCE(sqe->fd); |
4216 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 4217 | req->open.filename = getname(fname); |
15b71abe JA |
4218 | if (IS_ERR(req->open.filename)) { |
4219 | ret = PTR_ERR(req->open.filename); | |
4220 | req->open.filename = NULL; | |
4221 | return ret; | |
4222 | } | |
b9445598 PB |
4223 | |
4224 | req->open.file_slot = READ_ONCE(sqe->file_index); | |
4225 | if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) | |
4226 | return -EINVAL; | |
4227 | ||
4022e7af | 4228 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 4229 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 4230 | return 0; |
03b1230c JA |
4231 | } |
4232 | ||
ec65fea5 PB |
4233 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4234 | { | |
d3fddf6d PB |
4235 | u64 mode = READ_ONCE(sqe->len); |
4236 | u64 flags = READ_ONCE(sqe->open_flags); | |
ec65fea5 | 4237 | |
ec65fea5 PB |
4238 | req->open.how = build_open_how(flags, mode); |
4239 | return __io_openat_prep(req, sqe); | |
4240 | } | |
4241 | ||
cebdb986 | 4242 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 4243 | { |
cebdb986 | 4244 | struct open_how __user *how; |
cebdb986 | 4245 | size_t len; |
0fa03c62 JA |
4246 | int ret; |
4247 | ||
cebdb986 JA |
4248 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4249 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
4250 | if (len < OPEN_HOW_SIZE_VER0) |
4251 | return -EINVAL; | |
3529d8c2 | 4252 | |
cebdb986 JA |
4253 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
4254 | len); | |
4255 | if (ret) | |
4256 | return ret; | |
3529d8c2 | 4257 | |
ec65fea5 | 4258 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
4259 | } |
4260 | ||
45d189c6 | 4261 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
4262 | { |
4263 | struct open_flags op; | |
15b71abe | 4264 | struct file *file; |
b9445598 PB |
4265 | bool resolve_nonblock, nonblock_set; |
4266 | bool fixed = !!req->open.file_slot; | |
15b71abe JA |
4267 | int ret; |
4268 | ||
cebdb986 | 4269 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
4270 | if (ret) |
4271 | goto err; | |
3a81fd02 JA |
4272 | nonblock_set = op.open_flag & O_NONBLOCK; |
4273 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 4274 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
4275 | /* |
4276 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
4277 | * it'll always -EAGAIN | |
4278 | */ | |
4279 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
4280 | return -EAGAIN; | |
4281 | op.lookup_flags |= LOOKUP_CACHED; | |
4282 | op.open_flag |= O_NONBLOCK; | |
4283 | } | |
15b71abe | 4284 | |
b9445598 PB |
4285 | if (!fixed) { |
4286 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); | |
4287 | if (ret < 0) | |
4288 | goto err; | |
4289 | } | |
15b71abe JA |
4290 | |
4291 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
12dcb58a | 4292 | if (IS_ERR(file)) { |
944d1444 | 4293 | /* |
12dcb58a PB |
4294 | * We could hang on to this 'fd' on retrying, but seems like |
4295 | * marginal gain for something that is now known to be a slower | |
4296 | * path. So just put it, and we'll get a new one when we retry. | |
944d1444 | 4297 | */ |
b9445598 PB |
4298 | if (!fixed) |
4299 | put_unused_fd(ret); | |
3a81fd02 | 4300 | |
15b71abe | 4301 | ret = PTR_ERR(file); |
12dcb58a PB |
4302 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
4303 | if (ret == -EAGAIN && | |
4304 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) | |
4305 | return -EAGAIN; | |
4306 | goto err; | |
15b71abe | 4307 | } |
12dcb58a PB |
4308 | |
4309 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) | |
4310 | file->f_flags &= ~O_NONBLOCK; | |
4311 | fsnotify_open(file); | |
b9445598 PB |
4312 | |
4313 | if (!fixed) | |
4314 | fd_install(ret, file); | |
4315 | else | |
4316 | ret = io_install_fixed_file(req, file, issue_flags, | |
4317 | req->open.file_slot - 1); | |
15b71abe JA |
4318 | err: |
4319 | putname(req->open.filename); | |
8fef80bf | 4320 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe | 4321 | if (ret < 0) |
93d2bcd2 | 4322 | req_set_fail(req); |
0bdf3398 | 4323 | __io_req_complete(req, issue_flags, ret, 0); |
15b71abe JA |
4324 | return 0; |
4325 | } | |
4326 | ||
45d189c6 | 4327 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 4328 | { |
e45cff58 | 4329 | return io_openat2(req, issue_flags); |
cebdb986 JA |
4330 | } |
4331 | ||
067524e9 JA |
4332 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4333 | const struct io_uring_sqe *sqe) | |
4334 | { | |
4335 | struct io_provide_buf *p = &req->pbuf; | |
4336 | u64 tmp; | |
4337 | ||
26578cda PB |
4338 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
4339 | sqe->splice_fd_in) | |
067524e9 JA |
4340 | return -EINVAL; |
4341 | ||
4342 | tmp = READ_ONCE(sqe->fd); | |
4343 | if (!tmp || tmp > USHRT_MAX) | |
4344 | return -EINVAL; | |
4345 | ||
4346 | memset(p, 0, sizeof(*p)); | |
4347 | p->nbufs = tmp; | |
4348 | p->bgid = READ_ONCE(sqe->buf_group); | |
4349 | return 0; | |
4350 | } | |
4351 | ||
4352 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
4353 | int bgid, unsigned nbufs) | |
4354 | { | |
4355 | unsigned i = 0; | |
4356 | ||
4357 | /* shouldn't happen */ | |
4358 | if (!nbufs) | |
4359 | return 0; | |
4360 | ||
4361 | /* the head kbuf is the list itself */ | |
4362 | while (!list_empty(&buf->list)) { | |
4363 | struct io_buffer *nxt; | |
4364 | ||
4365 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
4366 | list_del(&nxt->list); | |
4367 | kfree(nxt); | |
4368 | if (++i == nbufs) | |
4369 | return i; | |
07edfd19 | 4370 | cond_resched(); |
067524e9 JA |
4371 | } |
4372 | i++; | |
4373 | kfree(buf); | |
9e15c3a0 | 4374 | xa_erase(&ctx->io_buffers, bgid); |
067524e9 JA |
4375 | |
4376 | return i; | |
4377 | } | |
4378 | ||
889fca73 | 4379 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
4380 | { |
4381 | struct io_provide_buf *p = &req->pbuf; | |
4382 | struct io_ring_ctx *ctx = req->ctx; | |
4383 | struct io_buffer *head; | |
4384 | int ret = 0; | |
45d189c6 | 4385 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
067524e9 JA |
4386 | |
4387 | io_ring_submit_lock(ctx, !force_nonblock); | |
4388 | ||
4389 | lockdep_assert_held(&ctx->uring_lock); | |
4390 | ||
4391 | ret = -ENOENT; | |
9e15c3a0 | 4392 | head = xa_load(&ctx->io_buffers, p->bgid); |
067524e9 JA |
4393 | if (head) |
4394 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 | 4395 | if (ret < 0) |
93d2bcd2 | 4396 | req_set_fail(req); |
067524e9 | 4397 | |
9fb8cb49 PB |
4398 | /* complete before unlock, IOPOLL may need the lock */ |
4399 | __io_req_complete(req, issue_flags, ret, 0); | |
4400 | io_ring_submit_unlock(ctx, !force_nonblock); | |
067524e9 JA |
4401 | return 0; |
4402 | } | |
4403 | ||
ddf0322d JA |
4404 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4405 | const struct io_uring_sqe *sqe) | |
4406 | { | |
38134ada | 4407 | unsigned long size, tmp_check; |
ddf0322d JA |
4408 | struct io_provide_buf *p = &req->pbuf; |
4409 | u64 tmp; | |
4410 | ||
26578cda | 4411 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
ddf0322d JA |
4412 | return -EINVAL; |
4413 | ||
4414 | tmp = READ_ONCE(sqe->fd); | |
4415 | if (!tmp || tmp > USHRT_MAX) | |
4416 | return -E2BIG; | |
4417 | p->nbufs = tmp; | |
4418 | p->addr = READ_ONCE(sqe->addr); | |
4419 | p->len = READ_ONCE(sqe->len); | |
4420 | ||
38134ada PB |
4421 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
4422 | &size)) | |
4423 | return -EOVERFLOW; | |
4424 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) | |
4425 | return -EOVERFLOW; | |
4426 | ||
d81269fe PB |
4427 | size = (unsigned long)p->len * p->nbufs; |
4428 | if (!access_ok(u64_to_user_ptr(p->addr), size)) | |
ddf0322d JA |
4429 | return -EFAULT; |
4430 | ||
4431 | p->bgid = READ_ONCE(sqe->buf_group); | |
4432 | tmp = READ_ONCE(sqe->off); | |
4433 | if (tmp > USHRT_MAX) | |
4434 | return -E2BIG; | |
4435 | p->bid = tmp; | |
4436 | return 0; | |
4437 | } | |
4438 | ||
4439 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
4440 | { | |
4441 | struct io_buffer *buf; | |
4442 | u64 addr = pbuf->addr; | |
4443 | int i, bid = pbuf->bid; | |
4444 | ||
4445 | for (i = 0; i < pbuf->nbufs; i++) { | |
9990da93 | 4446 | buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT); |
ddf0322d JA |
4447 | if (!buf) |
4448 | break; | |
4449 | ||
4450 | buf->addr = addr; | |
d1f82808 | 4451 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
ddf0322d JA |
4452 | buf->bid = bid; |
4453 | addr += pbuf->len; | |
4454 | bid++; | |
4455 | if (!*head) { | |
4456 | INIT_LIST_HEAD(&buf->list); | |
4457 | *head = buf; | |
4458 | } else { | |
4459 | list_add_tail(&buf->list, &(*head)->list); | |
4460 | } | |
2d1829ba | 4461 | cond_resched(); |
ddf0322d JA |
4462 | } |
4463 | ||
4464 | return i ? i : -ENOMEM; | |
4465 | } | |
4466 | ||
889fca73 | 4467 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4468 | { |
4469 | struct io_provide_buf *p = &req->pbuf; | |
4470 | struct io_ring_ctx *ctx = req->ctx; | |
4471 | struct io_buffer *head, *list; | |
4472 | int ret = 0; | |
45d189c6 | 4473 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ddf0322d JA |
4474 | |
4475 | io_ring_submit_lock(ctx, !force_nonblock); | |
4476 | ||
4477 | lockdep_assert_held(&ctx->uring_lock); | |
4478 | ||
9e15c3a0 | 4479 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
ddf0322d JA |
4480 | |
4481 | ret = io_add_buffers(p, &head); | |
9e15c3a0 JA |
4482 | if (ret >= 0 && !list) { |
4483 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); | |
4484 | if (ret < 0) | |
067524e9 | 4485 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d | 4486 | } |
ddf0322d | 4487 | if (ret < 0) |
93d2bcd2 | 4488 | req_set_fail(req); |
9fb8cb49 PB |
4489 | /* complete before unlock, IOPOLL may need the lock */ |
4490 | __io_req_complete(req, issue_flags, ret, 0); | |
4491 | io_ring_submit_unlock(ctx, !force_nonblock); | |
ddf0322d | 4492 | return 0; |
cebdb986 JA |
4493 | } |
4494 | ||
3e4827b0 JA |
4495 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4496 | const struct io_uring_sqe *sqe) | |
4497 | { | |
4498 | #if defined(CONFIG_EPOLL) | |
26578cda | 4499 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
3e4827b0 | 4500 | return -EINVAL; |
2d74d042 | 4501 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4502 | return -EINVAL; |
3e4827b0 JA |
4503 | |
4504 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4505 | req->epoll.op = READ_ONCE(sqe->len); | |
4506 | req->epoll.fd = READ_ONCE(sqe->off); | |
4507 | ||
4508 | if (ep_op_has_event(req->epoll.op)) { | |
4509 | struct epoll_event __user *ev; | |
4510 | ||
4511 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4512 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4513 | return -EFAULT; | |
4514 | } | |
4515 | ||
4516 | return 0; | |
4517 | #else | |
4518 | return -EOPNOTSUPP; | |
4519 | #endif | |
4520 | } | |
4521 | ||
889fca73 | 4522 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4523 | { |
4524 | #if defined(CONFIG_EPOLL) | |
4525 | struct io_epoll *ie = &req->epoll; | |
4526 | int ret; | |
45d189c6 | 4527 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4528 | |
4529 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4530 | if (force_nonblock && ret == -EAGAIN) | |
4531 | return -EAGAIN; | |
4532 | ||
4533 | if (ret < 0) | |
93d2bcd2 | 4534 | req_set_fail(req); |
889fca73 | 4535 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4536 | return 0; |
4537 | #else | |
4538 | return -EOPNOTSUPP; | |
4539 | #endif | |
4540 | } | |
4541 | ||
c1ca757b JA |
4542 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4543 | { | |
4544 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
26578cda | 4545 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
c1ca757b | 4546 | return -EINVAL; |
3232dd02 PB |
4547 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4548 | return -EINVAL; | |
c1ca757b JA |
4549 | |
4550 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4551 | req->madvise.len = READ_ONCE(sqe->len); | |
4552 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4553 | return 0; | |
4554 | #else | |
4555 | return -EOPNOTSUPP; | |
4556 | #endif | |
4557 | } | |
4558 | ||
45d189c6 | 4559 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4560 | { |
4561 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4562 | struct io_madvise *ma = &req->madvise; | |
4563 | int ret; | |
4564 | ||
45d189c6 | 4565 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4566 | return -EAGAIN; |
4567 | ||
0726b01e | 4568 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b | 4569 | if (ret < 0) |
93d2bcd2 | 4570 | req_set_fail(req); |
e1e16097 | 4571 | io_req_complete(req, ret); |
c1ca757b JA |
4572 | return 0; |
4573 | #else | |
4574 | return -EOPNOTSUPP; | |
4575 | #endif | |
4576 | } | |
4577 | ||
4840e418 JA |
4578 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4579 | { | |
26578cda | 4580 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
4840e418 | 4581 | return -EINVAL; |
3232dd02 PB |
4582 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4583 | return -EINVAL; | |
4840e418 JA |
4584 | |
4585 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4586 | req->fadvise.len = READ_ONCE(sqe->len); | |
4587 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4588 | return 0; | |
4589 | } | |
4590 | ||
45d189c6 | 4591 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4592 | { |
4593 | struct io_fadvise *fa = &req->fadvise; | |
4594 | int ret; | |
4595 | ||
45d189c6 | 4596 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4597 | switch (fa->advice) { |
4598 | case POSIX_FADV_NORMAL: | |
4599 | case POSIX_FADV_RANDOM: | |
4600 | case POSIX_FADV_SEQUENTIAL: | |
4601 | break; | |
4602 | default: | |
4603 | return -EAGAIN; | |
4604 | } | |
4605 | } | |
4840e418 JA |
4606 | |
4607 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4608 | if (ret < 0) | |
93d2bcd2 | 4609 | req_set_fail(req); |
0bdf3398 | 4610 | __io_req_complete(req, issue_flags, ret, 0); |
4840e418 JA |
4611 | return 0; |
4612 | } | |
4613 | ||
eddc7ef5 JA |
4614 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4615 | { | |
2d74d042 | 4616 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4617 | return -EINVAL; |
26578cda | 4618 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
eddc7ef5 | 4619 | return -EINVAL; |
9c280f90 | 4620 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4621 | return -EBADF; |
eddc7ef5 | 4622 | |
1d9e1288 BM |
4623 | req->statx.dfd = READ_ONCE(sqe->fd); |
4624 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4625 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4626 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4627 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4628 | |
4629 | return 0; | |
4630 | } | |
4631 | ||
45d189c6 | 4632 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4633 | { |
1d9e1288 | 4634 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4635 | int ret; |
4636 | ||
59d70013 | 4637 | if (issue_flags & IO_URING_F_NONBLOCK) |
eddc7ef5 JA |
4638 | return -EAGAIN; |
4639 | ||
e62753e4 BM |
4640 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4641 | ctx->buffer); | |
eddc7ef5 | 4642 | |
eddc7ef5 | 4643 | if (ret < 0) |
93d2bcd2 | 4644 | req_set_fail(req); |
e1e16097 | 4645 | io_req_complete(req, ret); |
eddc7ef5 JA |
4646 | return 0; |
4647 | } | |
4648 | ||
b5dba59e JA |
4649 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4650 | { | |
14587a46 | 4651 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4652 | return -EINVAL; |
b5dba59e | 4653 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
7df778be | 4654 | sqe->rw_flags || sqe->buf_index) |
b5dba59e | 4655 | return -EINVAL; |
9c280f90 | 4656 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4657 | return -EBADF; |
b5dba59e JA |
4658 | |
4659 | req->close.fd = READ_ONCE(sqe->fd); | |
7df778be PB |
4660 | req->close.file_slot = READ_ONCE(sqe->file_index); |
4661 | if (req->close.file_slot && req->close.fd) | |
4662 | return -EINVAL; | |
4663 | ||
b5dba59e | 4664 | return 0; |
b5dba59e JA |
4665 | } |
4666 | ||
889fca73 | 4667 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4668 | { |
9eac1904 | 4669 | struct files_struct *files = current->files; |
3af73b28 | 4670 | struct io_close *close = &req->close; |
9eac1904 | 4671 | struct fdtable *fdt; |
a1fde923 PB |
4672 | struct file *file = NULL; |
4673 | int ret = -EBADF; | |
b5dba59e | 4674 | |
7df778be PB |
4675 | if (req->close.file_slot) { |
4676 | ret = io_close_fixed(req, issue_flags); | |
4677 | goto err; | |
4678 | } | |
4679 | ||
9eac1904 JA |
4680 | spin_lock(&files->file_lock); |
4681 | fdt = files_fdtable(files); | |
4682 | if (close->fd >= fdt->max_fds) { | |
4683 | spin_unlock(&files->file_lock); | |
4684 | goto err; | |
4685 | } | |
4686 | file = fdt->fd[close->fd]; | |
a1fde923 | 4687 | if (!file || file->f_op == &io_uring_fops) { |
9eac1904 JA |
4688 | spin_unlock(&files->file_lock); |
4689 | file = NULL; | |
4690 | goto err; | |
3af73b28 | 4691 | } |
b5dba59e JA |
4692 | |
4693 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 4694 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 4695 | spin_unlock(&files->file_lock); |
0bf0eefd | 4696 | return -EAGAIN; |
a2100672 | 4697 | } |
b5dba59e | 4698 | |
9eac1904 JA |
4699 | ret = __close_fd_get_file(close->fd, &file); |
4700 | spin_unlock(&files->file_lock); | |
4701 | if (ret < 0) { | |
4702 | if (ret == -ENOENT) | |
4703 | ret = -EBADF; | |
4704 | goto err; | |
4705 | } | |
4706 | ||
3af73b28 | 4707 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4708 | ret = filp_close(file, current->files); |
4709 | err: | |
3af73b28 | 4710 | if (ret < 0) |
93d2bcd2 | 4711 | req_set_fail(req); |
9eac1904 JA |
4712 | if (file) |
4713 | fput(file); | |
889fca73 | 4714 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 4715 | return 0; |
b5dba59e JA |
4716 | } |
4717 | ||
1155c76a | 4718 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4719 | { |
4720 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 4721 | |
5d17b4a4 JA |
4722 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4723 | return -EINVAL; | |
26578cda PB |
4724 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4725 | sqe->splice_fd_in)) | |
5d17b4a4 JA |
4726 | return -EINVAL; |
4727 | ||
8ed8d3c3 JA |
4728 | req->sync.off = READ_ONCE(sqe->off); |
4729 | req->sync.len = READ_ONCE(sqe->len); | |
4730 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4731 | return 0; |
4732 | } | |
4733 | ||
45d189c6 | 4734 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4735 | { |
8ed8d3c3 JA |
4736 | int ret; |
4737 | ||
ac45abc0 | 4738 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 4739 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4740 | return -EAGAIN; |
4741 | ||
9adbd45d | 4742 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4743 | req->sync.flags); |
4744 | if (ret < 0) | |
93d2bcd2 | 4745 | req_set_fail(req); |
e1e16097 | 4746 | io_req_complete(req, ret); |
5d17b4a4 JA |
4747 | return 0; |
4748 | } | |
4749 | ||
469956e8 | 4750 | #if defined(CONFIG_NET) |
02d27d89 PB |
4751 | static int io_setup_async_msg(struct io_kiocb *req, |
4752 | struct io_async_msghdr *kmsg) | |
4753 | { | |
e8c2bc1f JA |
4754 | struct io_async_msghdr *async_msg = req->async_data; |
4755 | ||
4756 | if (async_msg) | |
02d27d89 | 4757 | return -EAGAIN; |
e8c2bc1f | 4758 | if (io_alloc_async_data(req)) { |
257e84a5 | 4759 | kfree(kmsg->free_iov); |
02d27d89 PB |
4760 | return -ENOMEM; |
4761 | } | |
e8c2bc1f | 4762 | async_msg = req->async_data; |
02d27d89 | 4763 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4764 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 4765 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
4766 | /* if were using fast_iov, set it to the new one */ |
4767 | if (!async_msg->free_iov) | |
4768 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
4769 | ||
02d27d89 PB |
4770 | return -EAGAIN; |
4771 | } | |
4772 | ||
2ae523ed PB |
4773 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4774 | struct io_async_msghdr *iomsg) | |
4775 | { | |
2ae523ed | 4776 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 4777 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 4778 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 4779 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
4780 | } |
4781 | ||
93642ef8 PB |
4782 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
4783 | { | |
4784 | int ret; | |
4785 | ||
93642ef8 PB |
4786 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
4787 | if (!ret) | |
4788 | req->flags |= REQ_F_NEED_CLEANUP; | |
4789 | return ret; | |
4790 | } | |
4791 | ||
3529d8c2 | 4792 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4793 | { |
e47293fd | 4794 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 4795 | |
d2b6f48b PB |
4796 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4797 | return -EINVAL; | |
7cb4bf4c JA |
4798 | if (unlikely(sqe->addr2 || sqe->file_index)) |
4799 | return -EINVAL; | |
f55b8a50 JA |
4800 | if (unlikely(sqe->addr2 || sqe->file_index || sqe->ioprio)) |
4801 | return -EINVAL; | |
d2b6f48b | 4802 | |
270a5940 | 4803 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4804 | sr->len = READ_ONCE(sqe->len); |
04411806 PB |
4805 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4806 | if (sr->msg_flags & MSG_DONTWAIT) | |
4807 | req->flags |= REQ_F_NOWAIT; | |
3529d8c2 | 4808 | |
d8768362 JA |
4809 | #ifdef CONFIG_COMPAT |
4810 | if (req->ctx->compat) | |
4811 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4812 | #endif | |
93642ef8 | 4813 | return 0; |
03b1230c JA |
4814 | } |
4815 | ||
889fca73 | 4816 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4817 | { |
6b754c8b | 4818 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4819 | struct socket *sock; |
7a7cacba | 4820 | unsigned flags; |
0031275d | 4821 | int min_ret = 0; |
0fa03c62 JA |
4822 | int ret; |
4823 | ||
dba4a925 | 4824 | sock = sock_from_file(req->file); |
7a7cacba | 4825 | if (unlikely(!sock)) |
dba4a925 | 4826 | return -ENOTSOCK; |
3529d8c2 | 4827 | |
257e84a5 PB |
4828 | kmsg = req->async_data; |
4829 | if (!kmsg) { | |
7a7cacba PB |
4830 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
4831 | if (ret) | |
4832 | return ret; | |
4833 | kmsg = &iomsg; | |
0fa03c62 | 4834 | } |
0fa03c62 | 4835 | |
04411806 PB |
4836 | flags = req->sr_msg.msg_flags; |
4837 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4838 | flags |= MSG_DONTWAIT; |
0031275d SM |
4839 | if (flags & MSG_WAITALL) |
4840 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4841 | ||
7a7cacba | 4842 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
45d189c6 | 4843 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4844 | return io_setup_async_msg(req, kmsg); |
4845 | if (ret == -ERESTARTSYS) | |
4846 | ret = -EINTR; | |
0fa03c62 | 4847 | |
257e84a5 PB |
4848 | /* fast path, check for non-NULL to avoid function call */ |
4849 | if (kmsg->free_iov) | |
4850 | kfree(kmsg->free_iov); | |
99bc4c38 | 4851 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 4852 | if (ret < min_ret) |
93d2bcd2 | 4853 | req_set_fail(req); |
889fca73 | 4854 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 4855 | return 0; |
03b1230c | 4856 | } |
aa1fa28f | 4857 | |
889fca73 | 4858 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4859 | { |
7a7cacba PB |
4860 | struct io_sr_msg *sr = &req->sr_msg; |
4861 | struct msghdr msg; | |
4862 | struct iovec iov; | |
fddaface | 4863 | struct socket *sock; |
7a7cacba | 4864 | unsigned flags; |
0031275d | 4865 | int min_ret = 0; |
fddaface JA |
4866 | int ret; |
4867 | ||
dba4a925 | 4868 | sock = sock_from_file(req->file); |
7a7cacba | 4869 | if (unlikely(!sock)) |
dba4a925 | 4870 | return -ENOTSOCK; |
fddaface | 4871 | |
7a7cacba PB |
4872 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4873 | if (unlikely(ret)) | |
14db8411 | 4874 | return ret; |
fddaface | 4875 | |
7a7cacba PB |
4876 | msg.msg_name = NULL; |
4877 | msg.msg_control = NULL; | |
4878 | msg.msg_controllen = 0; | |
4879 | msg.msg_namelen = 0; | |
fddaface | 4880 | |
04411806 PB |
4881 | flags = req->sr_msg.msg_flags; |
4882 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4883 | flags |= MSG_DONTWAIT; |
0031275d SM |
4884 | if (flags & MSG_WAITALL) |
4885 | min_ret = iov_iter_count(&msg.msg_iter); | |
4886 | ||
7a7cacba PB |
4887 | msg.msg_flags = flags; |
4888 | ret = sock_sendmsg(sock, &msg); | |
45d189c6 | 4889 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4890 | return -EAGAIN; |
4891 | if (ret == -ERESTARTSYS) | |
4892 | ret = -EINTR; | |
fddaface | 4893 | |
0031275d | 4894 | if (ret < min_ret) |
93d2bcd2 | 4895 | req_set_fail(req); |
889fca73 | 4896 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 4897 | return 0; |
fddaface JA |
4898 | } |
4899 | ||
1400e697 PB |
4900 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4901 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4902 | { |
4903 | struct io_sr_msg *sr = &req->sr_msg; | |
4904 | struct iovec __user *uiov; | |
4905 | size_t iov_len; | |
4906 | int ret; | |
4907 | ||
1400e697 PB |
4908 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4909 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4910 | if (ret) |
4911 | return ret; | |
4912 | ||
4913 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4914 | if (iov_len > 1) | |
4915 | return -EINVAL; | |
5476dfed | 4916 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4917 | return -EFAULT; |
5476dfed | 4918 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 4919 | iomsg->free_iov = NULL; |
52de1fe1 | 4920 | } else { |
257e84a5 | 4921 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4922 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 4923 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 4924 | false); |
52de1fe1 JA |
4925 | if (ret > 0) |
4926 | ret = 0; | |
4927 | } | |
4928 | ||
4929 | return ret; | |
4930 | } | |
4931 | ||
4932 | #ifdef CONFIG_COMPAT | |
4933 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4934 | struct io_async_msghdr *iomsg) |
52de1fe1 | 4935 | { |
52de1fe1 JA |
4936 | struct io_sr_msg *sr = &req->sr_msg; |
4937 | struct compat_iovec __user *uiov; | |
4938 | compat_uptr_t ptr; | |
4939 | compat_size_t len; | |
4940 | int ret; | |
4941 | ||
4af3417a PB |
4942 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
4943 | &ptr, &len); | |
52de1fe1 JA |
4944 | if (ret) |
4945 | return ret; | |
4946 | ||
4947 | uiov = compat_ptr(ptr); | |
4948 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4949 | compat_ssize_t clen; | |
4950 | ||
4951 | if (len > 1) | |
4952 | return -EINVAL; | |
4953 | if (!access_ok(uiov, sizeof(*uiov))) | |
4954 | return -EFAULT; | |
4955 | if (__get_user(clen, &uiov->iov_len)) | |
4956 | return -EFAULT; | |
4957 | if (clen < 0) | |
4958 | return -EINVAL; | |
2d280bc8 | 4959 | sr->len = clen; |
257e84a5 | 4960 | iomsg->free_iov = NULL; |
52de1fe1 | 4961 | } else { |
257e84a5 | 4962 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4963 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 4964 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 4965 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
4966 | if (ret < 0) |
4967 | return ret; | |
4968 | } | |
4969 | ||
4970 | return 0; | |
4971 | } | |
4972 | #endif | |
4973 | ||
1400e697 PB |
4974 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4975 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4976 | { |
1400e697 | 4977 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
4978 | |
4979 | #ifdef CONFIG_COMPAT | |
4980 | if (req->ctx->compat) | |
1400e697 | 4981 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4982 | #endif |
52de1fe1 | 4983 | |
1400e697 | 4984 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4985 | } |
4986 | ||
bcda7baa | 4987 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4988 | bool needs_lock) |
bcda7baa JA |
4989 | { |
4990 | struct io_sr_msg *sr = &req->sr_msg; | |
4991 | struct io_buffer *kbuf; | |
4992 | ||
bcda7baa JA |
4993 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
4994 | if (IS_ERR(kbuf)) | |
4995 | return kbuf; | |
4996 | ||
4997 | sr->kbuf = kbuf; | |
4998 | req->flags |= REQ_F_BUFFER_SELECTED; | |
bcda7baa | 4999 | return kbuf; |
fddaface JA |
5000 | } |
5001 | ||
7fbb1b54 PB |
5002 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
5003 | { | |
5004 | return io_put_kbuf(req, req->sr_msg.kbuf); | |
5005 | } | |
5006 | ||
93642ef8 | 5007 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 5008 | { |
99bc4c38 | 5009 | int ret; |
3529d8c2 | 5010 | |
93642ef8 PB |
5011 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
5012 | if (!ret) | |
5013 | req->flags |= REQ_F_NEED_CLEANUP; | |
5014 | return ret; | |
5015 | } | |
5016 | ||
5017 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
5018 | { | |
5019 | struct io_sr_msg *sr = &req->sr_msg; | |
5020 | ||
d2b6f48b PB |
5021 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5022 | return -EINVAL; | |
aa9362ee JA |
5023 | if (unlikely(sqe->addr2 || sqe->file_index)) |
5024 | return -EINVAL; | |
f55b8a50 JA |
5025 | if (unlikely(sqe->addr2 || sqe->file_index || sqe->ioprio)) |
5026 | return -EINVAL; | |
d2b6f48b | 5027 | |
270a5940 | 5028 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 5029 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 5030 | sr->bgid = READ_ONCE(sqe->buf_group); |
04411806 PB |
5031 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
5032 | if (sr->msg_flags & MSG_DONTWAIT) | |
5033 | req->flags |= REQ_F_NOWAIT; | |
06b76d44 | 5034 | |
d8768362 JA |
5035 | #ifdef CONFIG_COMPAT |
5036 | if (req->ctx->compat) | |
5037 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
5038 | #endif | |
93642ef8 | 5039 | return 0; |
aa1fa28f JA |
5040 | } |
5041 | ||
889fca73 | 5042 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 5043 | { |
6b754c8b | 5044 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 5045 | struct socket *sock; |
7fbb1b54 | 5046 | struct io_buffer *kbuf; |
7a7cacba | 5047 | unsigned flags; |
0031275d | 5048 | int min_ret = 0; |
52de1fe1 | 5049 | int ret, cflags = 0; |
45d189c6 | 5050 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 5051 | |
dba4a925 | 5052 | sock = sock_from_file(req->file); |
7a7cacba | 5053 | if (unlikely(!sock)) |
dba4a925 | 5054 | return -ENOTSOCK; |
3529d8c2 | 5055 | |
257e84a5 PB |
5056 | kmsg = req->async_data; |
5057 | if (!kmsg) { | |
7a7cacba PB |
5058 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
5059 | if (ret) | |
681fda8d | 5060 | return ret; |
7a7cacba PB |
5061 | kmsg = &iomsg; |
5062 | } | |
03b1230c | 5063 | |
bc02ef33 | 5064 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 5065 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 5066 | if (IS_ERR(kbuf)) |
52de1fe1 | 5067 | return PTR_ERR(kbuf); |
7a7cacba | 5068 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
5069 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
5070 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
5071 | 1, req->sr_msg.len); |
5072 | } | |
52de1fe1 | 5073 | |
04411806 PB |
5074 | flags = req->sr_msg.msg_flags; |
5075 | if (force_nonblock) | |
7a7cacba | 5076 | flags |= MSG_DONTWAIT; |
0031275d SM |
5077 | if (flags & MSG_WAITALL) |
5078 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
5079 | ||
7a7cacba PB |
5080 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
5081 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
5082 | if (force_nonblock && ret == -EAGAIN) |
5083 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
5084 | if (ret == -ERESTARTSYS) |
5085 | ret = -EINTR; | |
03b1230c | 5086 | |
7fbb1b54 PB |
5087 | if (req->flags & REQ_F_BUFFER_SELECTED) |
5088 | cflags = io_put_recv_kbuf(req); | |
257e84a5 PB |
5089 | /* fast path, check for non-NULL to avoid function call */ |
5090 | if (kmsg->free_iov) | |
5091 | kfree(kmsg->free_iov); | |
99bc4c38 | 5092 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 5093 | if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
93d2bcd2 | 5094 | req_set_fail(req); |
889fca73 | 5095 | __io_req_complete(req, issue_flags, ret, cflags); |
03b1230c | 5096 | return 0; |
0fa03c62 | 5097 | } |
5d17b4a4 | 5098 | |
889fca73 | 5099 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 5100 | { |
6b754c8b | 5101 | struct io_buffer *kbuf; |
7a7cacba PB |
5102 | struct io_sr_msg *sr = &req->sr_msg; |
5103 | struct msghdr msg; | |
5104 | void __user *buf = sr->buf; | |
fddaface | 5105 | struct socket *sock; |
7a7cacba PB |
5106 | struct iovec iov; |
5107 | unsigned flags; | |
0031275d | 5108 | int min_ret = 0; |
bcda7baa | 5109 | int ret, cflags = 0; |
45d189c6 | 5110 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 5111 | |
dba4a925 | 5112 | sock = sock_from_file(req->file); |
7a7cacba | 5113 | if (unlikely(!sock)) |
dba4a925 | 5114 | return -ENOTSOCK; |
fddaface | 5115 | |
bc02ef33 | 5116 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 5117 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
5118 | if (IS_ERR(kbuf)) |
5119 | return PTR_ERR(kbuf); | |
7a7cacba | 5120 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 5121 | } |
bcda7baa | 5122 | |
7a7cacba | 5123 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
5124 | if (unlikely(ret)) |
5125 | goto out_free; | |
fddaface | 5126 | |
7a7cacba PB |
5127 | msg.msg_name = NULL; |
5128 | msg.msg_control = NULL; | |
5129 | msg.msg_controllen = 0; | |
5130 | msg.msg_namelen = 0; | |
5131 | msg.msg_iocb = NULL; | |
5132 | msg.msg_flags = 0; | |
fddaface | 5133 | |
04411806 PB |
5134 | flags = req->sr_msg.msg_flags; |
5135 | if (force_nonblock) | |
7a7cacba | 5136 | flags |= MSG_DONTWAIT; |
0031275d SM |
5137 | if (flags & MSG_WAITALL) |
5138 | min_ret = iov_iter_count(&msg.msg_iter); | |
5139 | ||
7a7cacba PB |
5140 | ret = sock_recvmsg(sock, &msg, flags); |
5141 | if (force_nonblock && ret == -EAGAIN) | |
5142 | return -EAGAIN; | |
5143 | if (ret == -ERESTARTSYS) | |
5144 | ret = -EINTR; | |
14c32eee | 5145 | out_free: |
7fbb1b54 PB |
5146 | if (req->flags & REQ_F_BUFFER_SELECTED) |
5147 | cflags = io_put_recv_kbuf(req); | |
0031275d | 5148 | if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
93d2bcd2 | 5149 | req_set_fail(req); |
889fca73 | 5150 | __io_req_complete(req, issue_flags, ret, cflags); |
fddaface | 5151 | return 0; |
fddaface JA |
5152 | } |
5153 | ||
3529d8c2 | 5154 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 5155 | { |
8ed8d3c3 JA |
5156 | struct io_accept *accept = &req->accept; |
5157 | ||
14587a46 | 5158 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 5159 | return -EINVAL; |
aaa4db12 | 5160 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
5161 | return -EINVAL; |
5162 | ||
d55e5f5b JA |
5163 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5164 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 5165 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 5166 | accept->nofile = rlimit(RLIMIT_NOFILE); |
a7083ad5 | 5167 | |
aaa4db12 | 5168 | accept->file_slot = READ_ONCE(sqe->file_index); |
56a8e15e | 5169 | if (accept->file_slot && (accept->flags & SOCK_CLOEXEC)) |
aaa4db12 | 5170 | return -EINVAL; |
a7083ad5 PB |
5171 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
5172 | return -EINVAL; | |
5173 | if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) | |
5174 | accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; | |
8ed8d3c3 | 5175 | return 0; |
8ed8d3c3 | 5176 | } |
17f2fe35 | 5177 | |
889fca73 | 5178 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
5179 | { |
5180 | struct io_accept *accept = &req->accept; | |
45d189c6 | 5181 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 5182 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
aaa4db12 | 5183 | bool fixed = !!accept->file_slot; |
a7083ad5 PB |
5184 | struct file *file; |
5185 | int ret, fd; | |
8ed8d3c3 | 5186 | |
e697deed JX |
5187 | if (req->file->f_flags & O_NONBLOCK) |
5188 | req->flags |= REQ_F_NOWAIT; | |
5189 | ||
aaa4db12 PB |
5190 | if (!fixed) { |
5191 | fd = __get_unused_fd_flags(accept->flags, accept->nofile); | |
5192 | if (unlikely(fd < 0)) | |
5193 | return fd; | |
5194 | } | |
a7083ad5 PB |
5195 | file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, |
5196 | accept->flags); | |
5197 | if (IS_ERR(file)) { | |
aaa4db12 PB |
5198 | if (!fixed) |
5199 | put_unused_fd(fd); | |
a7083ad5 PB |
5200 | ret = PTR_ERR(file); |
5201 | if (ret == -EAGAIN && force_nonblock) | |
5202 | return -EAGAIN; | |
ac45abc0 PB |
5203 | if (ret == -ERESTARTSYS) |
5204 | ret = -EINTR; | |
93d2bcd2 | 5205 | req_set_fail(req); |
aaa4db12 | 5206 | } else if (!fixed) { |
a7083ad5 PB |
5207 | fd_install(fd, file); |
5208 | ret = fd; | |
aaa4db12 PB |
5209 | } else { |
5210 | ret = io_install_fixed_file(req, file, issue_flags, | |
5211 | accept->file_slot - 1); | |
ac45abc0 | 5212 | } |
889fca73 | 5213 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 5214 | return 0; |
8ed8d3c3 JA |
5215 | } |
5216 | ||
93642ef8 PB |
5217 | static int io_connect_prep_async(struct io_kiocb *req) |
5218 | { | |
5219 | struct io_async_connect *io = req->async_data; | |
5220 | struct io_connect *conn = &req->connect; | |
5221 | ||
5222 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
5223 | } | |
5224 | ||
3529d8c2 | 5225 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 5226 | { |
3529d8c2 | 5227 | struct io_connect *conn = &req->connect; |
f499a021 | 5228 | |
14587a46 | 5229 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 | 5230 | return -EINVAL; |
26578cda PB |
5231 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
5232 | sqe->splice_fd_in) | |
3fbb51c1 JA |
5233 | return -EINVAL; |
5234 | ||
3529d8c2 JA |
5235 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5236 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 5237 | return 0; |
f499a021 JA |
5238 | } |
5239 | ||
889fca73 | 5240 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 5241 | { |
e8c2bc1f | 5242 | struct io_async_connect __io, *io; |
f8e85cf2 | 5243 | unsigned file_flags; |
3fbb51c1 | 5244 | int ret; |
45d189c6 | 5245 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 5246 | |
e8c2bc1f JA |
5247 | if (req->async_data) { |
5248 | io = req->async_data; | |
f499a021 | 5249 | } else { |
3529d8c2 JA |
5250 | ret = move_addr_to_kernel(req->connect.addr, |
5251 | req->connect.addr_len, | |
e8c2bc1f | 5252 | &__io.address); |
f499a021 JA |
5253 | if (ret) |
5254 | goto out; | |
5255 | io = &__io; | |
5256 | } | |
5257 | ||
3fbb51c1 JA |
5258 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
5259 | ||
e8c2bc1f | 5260 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 5261 | req->connect.addr_len, file_flags); |
87f80d62 | 5262 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 5263 | if (req->async_data) |
b7bb4f7d | 5264 | return -EAGAIN; |
e8c2bc1f | 5265 | if (io_alloc_async_data(req)) { |
f499a021 JA |
5266 | ret = -ENOMEM; |
5267 | goto out; | |
5268 | } | |
e8c2bc1f | 5269 | memcpy(req->async_data, &__io, sizeof(__io)); |
f8e85cf2 | 5270 | return -EAGAIN; |
f499a021 | 5271 | } |
f8e85cf2 JA |
5272 | if (ret == -ERESTARTSYS) |
5273 | ret = -EINTR; | |
f499a021 | 5274 | out: |
4e88d6e7 | 5275 | if (ret < 0) |
93d2bcd2 | 5276 | req_set_fail(req); |
889fca73 | 5277 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 5278 | return 0; |
469956e8 Y |
5279 | } |
5280 | #else /* !CONFIG_NET */ | |
99a10081 JA |
5281 | #define IO_NETOP_FN(op) \ |
5282 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
5283 | { \ | |
5284 | return -EOPNOTSUPP; \ | |
5285 | } | |
5286 | ||
5287 | #define IO_NETOP_PREP(op) \ | |
5288 | IO_NETOP_FN(op) \ | |
5289 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
5290 | { \ | |
5291 | return -EOPNOTSUPP; \ | |
5292 | } \ | |
5293 | ||
5294 | #define IO_NETOP_PREP_ASYNC(op) \ | |
5295 | IO_NETOP_PREP(op) \ | |
5296 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
5297 | { \ | |
5298 | return -EOPNOTSUPP; \ | |
5299 | } | |
5300 | ||
5301 | IO_NETOP_PREP_ASYNC(sendmsg); | |
5302 | IO_NETOP_PREP_ASYNC(recvmsg); | |
5303 | IO_NETOP_PREP_ASYNC(connect); | |
5304 | IO_NETOP_PREP(accept); | |
5305 | IO_NETOP_FN(send); | |
5306 | IO_NETOP_FN(recv); | |
469956e8 | 5307 | #endif /* CONFIG_NET */ |
f8e85cf2 | 5308 | |
d7718a9d JA |
5309 | struct io_poll_table { |
5310 | struct poll_table_struct pt; | |
5311 | struct io_kiocb *req; | |
68b11e8b | 5312 | int nr_entries; |
d7718a9d JA |
5313 | int error; |
5314 | }; | |
ce593a6c | 5315 | |
350804e3 | 5316 | #define IO_POLL_CANCEL_FLAG BIT(31) |
17098de4 | 5317 | #define IO_POLL_REF_MASK GENMASK(30, 0) |
350804e3 PB |
5318 | |
5319 | /* | |
5320 | * If refs part of ->poll_refs (see IO_POLL_REF_MASK) is 0, it's free. We can | |
5321 | * bump it and acquire ownership. It's disallowed to modify requests while not | |
5322 | * owning it, that prevents from races for enqueueing task_work's and b/w | |
5323 | * arming poll and wakeups. | |
5324 | */ | |
5325 | static inline bool io_poll_get_ownership(struct io_kiocb *req) | |
5326 | { | |
5327 | return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK); | |
5328 | } | |
5329 | ||
5330 | static void io_poll_mark_cancelled(struct io_kiocb *req) | |
5331 | { | |
5332 | atomic_or(IO_POLL_CANCEL_FLAG, &req->poll_refs); | |
5333 | } | |
5334 | ||
f5547370 PB |
5335 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
5336 | { | |
5337 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ | |
5338 | if (req->opcode == IORING_OP_POLL_ADD) | |
5339 | return req->async_data; | |
5340 | return req->apoll->double_poll; | |
5341 | } | |
5342 | ||
5343 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5344 | { | |
5345 | if (req->opcode == IORING_OP_POLL_ADD) | |
5346 | return &req->poll; | |
5347 | return &req->apoll->poll; | |
5348 | } | |
5349 | ||
5350 | static void io_poll_req_insert(struct io_kiocb *req) | |
5351 | { | |
5352 | struct io_ring_ctx *ctx = req->ctx; | |
5353 | struct hlist_head *list; | |
5354 | ||
5355 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5356 | hlist_add_head(&req->hash_node, list); | |
5357 | } | |
5358 | ||
5359 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
5360 | wait_queue_func_t wake_func) | |
5361 | { | |
5362 | poll->head = NULL; | |
f5547370 PB |
5363 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
5364 | /* mask in events that we always want/need */ | |
5365 | poll->events = events | IO_POLL_UNMASK; | |
5366 | INIT_LIST_HEAD(&poll->wait.entry); | |
5367 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
5368 | } | |
5369 | ||
350804e3 | 5370 | static inline void io_poll_remove_entry(struct io_poll_iocb *poll) |
d7718a9d | 5371 | { |
350804e3 | 5372 | struct wait_queue_head *head = poll->head; |
d7718a9d | 5373 | |
350804e3 | 5374 | spin_lock_irq(&head->lock); |
d7718a9d | 5375 | list_del_init(&poll->wait.entry); |
350804e3 PB |
5376 | poll->head = NULL; |
5377 | spin_unlock_irq(&head->lock); | |
5378 | } | |
d7718a9d | 5379 | |
350804e3 PB |
5380 | static void io_poll_remove_entries(struct io_kiocb *req) |
5381 | { | |
5382 | struct io_poll_iocb *poll = io_poll_get_single(req); | |
5383 | struct io_poll_iocb *poll_double = io_poll_get_double(req); | |
6d816e08 | 5384 | |
350804e3 PB |
5385 | if (poll->head) |
5386 | io_poll_remove_entry(poll); | |
5387 | if (poll_double && poll_double->head) | |
5388 | io_poll_remove_entry(poll_double); | |
d7718a9d JA |
5389 | } |
5390 | ||
350804e3 PB |
5391 | /* |
5392 | * All poll tw should go through this. Checks for poll events, manages | |
5393 | * references, does rewait, etc. | |
5394 | * | |
5395 | * Returns a negative error on failure. >0 when no action require, which is | |
5396 | * either spurious wakeup or multishot CQE is served. 0 when it's done with | |
5397 | * the request, then the mask is stored in req->result. | |
5398 | */ | |
5399 | static int io_poll_check_events(struct io_kiocb *req) | |
74ce6ce4 JA |
5400 | { |
5401 | struct io_ring_ctx *ctx = req->ctx; | |
350804e3 PB |
5402 | struct io_poll_iocb *poll = io_poll_get_single(req); |
5403 | int v; | |
74ce6ce4 | 5404 | |
316319e8 | 5405 | /* req->task == current here, checking PF_EXITING is safe */ |
e09ee510 | 5406 | if (unlikely(req->task->flags & PF_EXITING)) |
350804e3 | 5407 | io_poll_mark_cancelled(req); |
e09ee510 | 5408 | |
350804e3 PB |
5409 | do { |
5410 | v = atomic_read(&req->poll_refs); | |
74ce6ce4 | 5411 | |
350804e3 PB |
5412 | /* tw handler should be the owner, and so have some references */ |
5413 | if (WARN_ON_ONCE(!(v & IO_POLL_REF_MASK))) | |
5414 | return 0; | |
5415 | if (v & IO_POLL_CANCEL_FLAG) | |
5416 | return -ECANCELED; | |
74ce6ce4 | 5417 | |
350804e3 PB |
5418 | if (!req->result) { |
5419 | struct poll_table_struct pt = { ._key = poll->events }; | |
74ce6ce4 | 5420 | |
350804e3 PB |
5421 | req->result = vfs_poll(req->file, &pt) & poll->events; |
5422 | } | |
74ce6ce4 | 5423 | |
350804e3 PB |
5424 | /* multishot, just fill an CQE and proceed */ |
5425 | if (req->result && !(poll->events & EPOLLONESHOT)) { | |
5426 | __poll_t mask = mangle_poll(req->result & poll->events); | |
5427 | bool filled; | |
18bceab1 | 5428 | |
350804e3 PB |
5429 | spin_lock(&ctx->completion_lock); |
5430 | filled = io_fill_cqe_aux(ctx, req->user_data, mask, | |
5431 | IORING_CQE_F_MORE); | |
5432 | io_commit_cqring(ctx); | |
5433 | spin_unlock(&ctx->completion_lock); | |
5434 | if (unlikely(!filled)) | |
5435 | return -ECANCELED; | |
5436 | io_cqring_ev_posted(ctx); | |
5437 | } else if (req->result) { | |
5438 | return 0; | |
5439 | } | |
18bceab1 | 5440 | |
350804e3 PB |
5441 | /* |
5442 | * Release all references, retry if someone tried to restart | |
5443 | * task_work while we were executing it. | |
5444 | */ | |
5445 | } while (atomic_sub_return(v & IO_POLL_REF_MASK, &req->poll_refs)); | |
18bceab1 | 5446 | |
350804e3 | 5447 | return 1; |
18bceab1 JA |
5448 | } |
5449 | ||
350804e3 | 5450 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 JA |
5451 | { |
5452 | struct io_ring_ctx *ctx = req->ctx; | |
350804e3 PB |
5453 | int ret; |
5454 | ||
5455 | ret = io_poll_check_events(req); | |
5456 | if (ret > 0) | |
5457 | return; | |
18bceab1 | 5458 | |
350804e3 PB |
5459 | if (!ret) { |
5460 | req->result = mangle_poll(req->result & req->poll.events); | |
e27414be | 5461 | } else { |
350804e3 PB |
5462 | req->result = ret; |
5463 | req_set_fail(req); | |
a62682f9 | 5464 | } |
350804e3 PB |
5465 | |
5466 | io_poll_remove_entries(req); | |
5467 | spin_lock(&ctx->completion_lock); | |
5468 | hash_del(&req->hash_node); | |
5469 | spin_unlock(&ctx->completion_lock); | |
5470 | io_req_complete_post(req, req->result, 0); | |
18bceab1 JA |
5471 | } |
5472 | ||
350804e3 | 5473 | static void io_apoll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 JA |
5474 | { |
5475 | struct io_ring_ctx *ctx = req->ctx; | |
350804e3 | 5476 | int ret; |
18bceab1 | 5477 | |
350804e3 PB |
5478 | ret = io_poll_check_events(req); |
5479 | if (ret > 0) | |
5480 | return; | |
18bceab1 | 5481 | |
350804e3 PB |
5482 | io_poll_remove_entries(req); |
5483 | spin_lock(&ctx->completion_lock); | |
5484 | hash_del(&req->hash_node); | |
5485 | spin_unlock(&ctx->completion_lock); | |
18bceab1 | 5486 | |
350804e3 PB |
5487 | if (!ret) |
5488 | io_req_task_submit(req, locked); | |
5489 | else | |
5490 | io_req_complete_failed(req, ret); | |
18bceab1 JA |
5491 | } |
5492 | ||
350804e3 PB |
5493 | static void __io_poll_execute(struct io_kiocb *req, int mask) |
5494 | { | |
5495 | req->result = mask; | |
5496 | if (req->opcode == IORING_OP_POLL_ADD) | |
5497 | req->io_task_work.func = io_poll_task_func; | |
5498 | else | |
5499 | req->io_task_work.func = io_apoll_task_func; | |
5500 | ||
5501 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
5502 | io_req_task_work_add(req); | |
5503 | } | |
5504 | ||
5505 | static inline void io_poll_execute(struct io_kiocb *req, int res) | |
5506 | { | |
5507 | if (io_poll_get_ownership(req)) | |
5508 | __io_poll_execute(req, res); | |
5509 | } | |
5510 | ||
5511 | static void io_poll_cancel_req(struct io_kiocb *req) | |
5512 | { | |
5513 | io_poll_mark_cancelled(req); | |
5514 | /* kick tw, which should complete the request */ | |
5515 | io_poll_execute(req, 0); | |
5516 | } | |
5517 | ||
5518 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5519 | void *key) | |
18bceab1 JA |
5520 | { |
5521 | struct io_kiocb *req = wait->private; | |
350804e3 PB |
5522 | struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb, |
5523 | wait); | |
18bceab1 JA |
5524 | __poll_t mask = key_to_poll(key); |
5525 | ||
350804e3 | 5526 | /* for instances that support it check for an event match first */ |
18bceab1 JA |
5527 | if (mask && !(mask & poll->events)) |
5528 | return 0; | |
8706e04e | 5529 | |
350804e3 PB |
5530 | if (io_poll_get_ownership(req)) |
5531 | __io_poll_execute(req, mask); | |
18bceab1 JA |
5532 | return 1; |
5533 | } | |
5534 | ||
18bceab1 | 5535 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, |
807abcb0 JA |
5536 | struct wait_queue_head *head, |
5537 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
5538 | { |
5539 | struct io_kiocb *req = pt->req; | |
5540 | ||
5541 | /* | |
68b11e8b PB |
5542 | * The file being polled uses multiple waitqueues for poll handling |
5543 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb | |
5544 | * if this happens. | |
18bceab1 | 5545 | */ |
68b11e8b | 5546 | if (unlikely(pt->nr_entries)) { |
350804e3 | 5547 | struct io_poll_iocb *first = poll; |
58852d4d | 5548 | |
23a65db8 | 5549 | /* double add on the same waitqueue head, ignore */ |
350804e3 | 5550 | if (first->head == head) |
23a65db8 | 5551 | return; |
18bceab1 | 5552 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5553 | if (*poll_ptr) { |
23a65db8 PB |
5554 | if ((*poll_ptr)->head == head) |
5555 | return; | |
18bceab1 JA |
5556 | pt->error = -EINVAL; |
5557 | return; | |
5558 | } | |
350804e3 | 5559 | |
18bceab1 JA |
5560 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
5561 | if (!poll) { | |
5562 | pt->error = -ENOMEM; | |
5563 | return; | |
5564 | } | |
350804e3 | 5565 | io_init_poll_iocb(poll, first->events, first->wait.func); |
807abcb0 | 5566 | *poll_ptr = poll; |
18bceab1 JA |
5567 | } |
5568 | ||
68b11e8b | 5569 | pt->nr_entries++; |
18bceab1 | 5570 | poll->head = head; |
350804e3 | 5571 | poll->wait.private = req; |
a31eb4a2 JX |
5572 | |
5573 | if (poll->events & EPOLLEXCLUSIVE) | |
5574 | add_wait_queue_exclusive(head, &poll->wait); | |
5575 | else | |
5576 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5577 | } |
5578 | ||
350804e3 | 5579 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
18bceab1 JA |
5580 | struct poll_table_struct *p) |
5581 | { | |
5582 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
d7718a9d | 5583 | |
350804e3 PB |
5584 | __io_queue_proc(&pt->req->poll, pt, head, |
5585 | (struct io_poll_iocb **) &pt->req->async_data); | |
d7718a9d JA |
5586 | } |
5587 | ||
350804e3 PB |
5588 | static int __io_arm_poll_handler(struct io_kiocb *req, |
5589 | struct io_poll_iocb *poll, | |
5590 | struct io_poll_table *ipt, __poll_t mask) | |
d7718a9d JA |
5591 | { |
5592 | struct io_ring_ctx *ctx = req->ctx; | |
350804e3 | 5593 | int v; |
d7718a9d | 5594 | |
4d52f338 | 5595 | INIT_HLIST_NODE(&req->hash_node); |
350804e3 | 5596 | io_init_poll_iocb(poll, mask, io_poll_wake); |
b90cd197 | 5597 | poll->file = req->file; |
18bceab1 | 5598 | poll->wait.private = req; |
d7718a9d JA |
5599 | |
5600 | ipt->pt._key = mask; | |
5601 | ipt->req = req; | |
68b11e8b PB |
5602 | ipt->error = 0; |
5603 | ipt->nr_entries = 0; | |
d7718a9d | 5604 | |
350804e3 PB |
5605 | /* |
5606 | * Take the ownership to delay any tw execution up until we're done | |
5607 | * with poll arming. see io_poll_get_ownership(). | |
5608 | */ | |
5609 | atomic_set(&req->poll_refs, 1); | |
d7718a9d | 5610 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
350804e3 PB |
5611 | |
5612 | if (mask && (poll->events & EPOLLONESHOT)) { | |
5613 | io_poll_remove_entries(req); | |
5614 | /* no one else has access to the req, forget about the ref */ | |
5615 | return mask; | |
5616 | } | |
5617 | if (!mask && unlikely(ipt->error || !ipt->nr_entries)) { | |
5618 | io_poll_remove_entries(req); | |
5619 | if (!ipt->error) | |
5620 | ipt->error = -EINVAL; | |
5621 | return 0; | |
5622 | } | |
d7718a9d | 5623 | |
79ebeaee | 5624 | spin_lock(&ctx->completion_lock); |
350804e3 PB |
5625 | io_poll_req_insert(req); |
5626 | spin_unlock(&ctx->completion_lock); | |
5627 | ||
5628 | if (mask) { | |
5629 | /* can't multishot if failed, just queue the event we've got */ | |
1383e357 | 5630 | if (unlikely(ipt->error || !ipt->nr_entries)) { |
350804e3 | 5631 | poll->events |= EPOLLONESHOT; |
1383e357 PB |
5632 | ipt->error = 0; |
5633 | } | |
350804e3 PB |
5634 | __io_poll_execute(req, mask); |
5635 | return 0; | |
d7718a9d JA |
5636 | } |
5637 | ||
350804e3 PB |
5638 | /* |
5639 | * Release ownership. If someone tried to queue a tw while it was | |
5640 | * locked, kick it off for them. | |
5641 | */ | |
5642 | v = atomic_dec_return(&req->poll_refs); | |
5643 | if (unlikely(v & IO_POLL_REF_MASK)) | |
5644 | __io_poll_execute(req, 0); | |
5645 | return 0; | |
5646 | } | |
5647 | ||
5648 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5649 | struct poll_table_struct *p) | |
5650 | { | |
5651 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5652 | struct async_poll *apoll = pt->req->apoll; | |
5653 | ||
5654 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); | |
d7718a9d JA |
5655 | } |
5656 | ||
59b735ae OL |
5657 | enum { |
5658 | IO_APOLL_OK, | |
5659 | IO_APOLL_ABORTED, | |
5660 | IO_APOLL_READY | |
5661 | }; | |
5662 | ||
5663 | static int io_arm_poll_handler(struct io_kiocb *req) | |
d7718a9d JA |
5664 | { |
5665 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5666 | struct io_ring_ctx *ctx = req->ctx; | |
5667 | struct async_poll *apoll; | |
5668 | struct io_poll_table ipt; | |
350804e3 PB |
5669 | __poll_t mask = EPOLLONESHOT | POLLERR | POLLPRI; |
5670 | int ret; | |
d7718a9d JA |
5671 | |
5672 | if (!req->file || !file_can_poll(req->file)) | |
59b735ae | 5673 | return IO_APOLL_ABORTED; |
24c74678 | 5674 | if (req->flags & REQ_F_POLLED) |
59b735ae | 5675 | return IO_APOLL_ABORTED; |
b2d9c3da PB |
5676 | if (!def->pollin && !def->pollout) |
5677 | return IO_APOLL_ABORTED; | |
5678 | ||
5679 | if (def->pollin) { | |
b2d9c3da PB |
5680 | mask |= POLLIN | POLLRDNORM; |
5681 | ||
5682 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5683 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5684 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5685 | mask &= ~POLLIN; | |
5686 | } else { | |
b2d9c3da PB |
5687 | mask |= POLLOUT | POLLWRNORM; |
5688 | } | |
5689 | ||
d7718a9d JA |
5690 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
5691 | if (unlikely(!apoll)) | |
59b735ae | 5692 | return IO_APOLL_ABORTED; |
807abcb0 | 5693 | apoll->double_poll = NULL; |
d7718a9d | 5694 | req->apoll = apoll; |
b2d9c3da | 5695 | req->flags |= REQ_F_POLLED; |
d7718a9d JA |
5696 | ipt.pt._qproc = io_async_queue_proc; |
5697 | ||
350804e3 | 5698 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask); |
41a5169c HX |
5699 | if (ret || ipt.error) |
5700 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; | |
5701 | ||
236daeae OL |
5702 | trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data, |
5703 | mask, apoll->poll.events); | |
59b735ae | 5704 | return IO_APOLL_OK; |
d7718a9d JA |
5705 | } |
5706 | ||
76e1b642 JA |
5707 | /* |
5708 | * Returns true if we found and killed one or more poll requests | |
5709 | */ | |
6b81928d | 5710 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
3dd0c97a | 5711 | bool cancel_all) |
221c5eb2 | 5712 | { |
78076bb6 | 5713 | struct hlist_node *tmp; |
221c5eb2 | 5714 | struct io_kiocb *req; |
350804e3 PB |
5715 | bool found = false; |
5716 | int i; | |
221c5eb2 | 5717 | |
79ebeaee | 5718 | spin_lock(&ctx->completion_lock); |
78076bb6 JA |
5719 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5720 | struct hlist_head *list; | |
5721 | ||
5722 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5723 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
350804e3 | 5724 | if (io_match_task_safe(req, tsk, cancel_all)) { |
41a4774d | 5725 | hlist_del_init(&req->hash_node); |
350804e3 PB |
5726 | io_poll_cancel_req(req); |
5727 | found = true; | |
5728 | } | |
f3606e3a | 5729 | } |
221c5eb2 | 5730 | } |
79ebeaee | 5731 | spin_unlock(&ctx->completion_lock); |
350804e3 | 5732 | return found; |
221c5eb2 JA |
5733 | } |
5734 | ||
9ba5fac8 PB |
5735 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5736 | bool poll_only) | |
e07785b0 | 5737 | __must_hold(&ctx->completion_lock) |
47f46768 | 5738 | { |
78076bb6 | 5739 | struct hlist_head *list; |
47f46768 JA |
5740 | struct io_kiocb *req; |
5741 | ||
78076bb6 JA |
5742 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5743 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5744 | if (sqe_addr != req->user_data) |
5745 | continue; | |
9ba5fac8 PB |
5746 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
5747 | continue; | |
b2cb805f | 5748 | return req; |
47f46768 | 5749 | } |
b2cb805f JA |
5750 | return NULL; |
5751 | } | |
5752 | ||
350804e3 PB |
5753 | static bool io_poll_disarm(struct io_kiocb *req) |
5754 | __must_hold(&ctx->completion_lock) | |
5755 | { | |
5756 | if (!io_poll_get_ownership(req)) | |
5757 | return false; | |
5758 | io_poll_remove_entries(req); | |
5759 | hash_del(&req->hash_node); | |
5760 | return true; | |
5761 | } | |
5762 | ||
9ba5fac8 PB |
5763 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5764 | bool poll_only) | |
e07785b0 | 5765 | __must_hold(&ctx->completion_lock) |
b2cb805f | 5766 | { |
350804e3 | 5767 | struct io_kiocb *req = io_poll_find(ctx, sqe_addr, poll_only); |
b2cb805f | 5768 | |
b2cb805f JA |
5769 | if (!req) |
5770 | return -ENOENT; | |
350804e3 PB |
5771 | io_poll_cancel_req(req); |
5772 | return 0; | |
47f46768 JA |
5773 | } |
5774 | ||
9096af3e PB |
5775 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
5776 | unsigned int flags) | |
5777 | { | |
5778 | u32 events; | |
47f46768 | 5779 | |
9096af3e PB |
5780 | events = READ_ONCE(sqe->poll32_events); |
5781 | #ifdef __BIG_ENDIAN | |
5782 | events = swahw32(events); | |
5783 | #endif | |
5784 | if (!(flags & IORING_POLL_ADD_MULTI)) | |
5785 | events |= EPOLLONESHOT; | |
5786 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); | |
47f46768 JA |
5787 | } |
5788 | ||
c5de0036 | 5789 | static int io_poll_update_prep(struct io_kiocb *req, |
3529d8c2 | 5790 | const struct io_uring_sqe *sqe) |
0969e783 | 5791 | { |
c5de0036 PB |
5792 | struct io_poll_update *upd = &req->poll_update; |
5793 | u32 flags; | |
5794 | ||
0969e783 JA |
5795 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5796 | return -EINVAL; | |
26578cda | 5797 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
c5de0036 PB |
5798 | return -EINVAL; |
5799 | flags = READ_ONCE(sqe->len); | |
5800 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | | |
5801 | IORING_POLL_ADD_MULTI)) | |
5802 | return -EINVAL; | |
5803 | /* meaningless without update */ | |
5804 | if (flags == IORING_POLL_ADD_MULTI) | |
0969e783 JA |
5805 | return -EINVAL; |
5806 | ||
c5de0036 PB |
5807 | upd->old_user_data = READ_ONCE(sqe->addr); |
5808 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; | |
5809 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; | |
221c5eb2 | 5810 | |
c5de0036 PB |
5811 | upd->new_user_data = READ_ONCE(sqe->off); |
5812 | if (!upd->update_user_data && upd->new_user_data) | |
5813 | return -EINVAL; | |
5814 | if (upd->update_events) | |
5815 | upd->events = io_poll_parse_events(sqe, flags); | |
5816 | else if (sqe->poll32_events) | |
5817 | return -EINVAL; | |
221c5eb2 | 5818 | |
221c5eb2 JA |
5819 | return 0; |
5820 | } | |
5821 | ||
3529d8c2 | 5822 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5823 | { |
5824 | struct io_poll_iocb *poll = &req->poll; | |
c5de0036 | 5825 | u32 flags; |
221c5eb2 JA |
5826 | |
5827 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5828 | return -EINVAL; | |
c5de0036 | 5829 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
88e41cf9 JA |
5830 | return -EINVAL; |
5831 | flags = READ_ONCE(sqe->len); | |
c5de0036 | 5832 | if (flags & ~IORING_POLL_ADD_MULTI) |
221c5eb2 JA |
5833 | return -EINVAL; |
5834 | ||
48dcd38d | 5835 | io_req_set_refcount(req); |
c5de0036 | 5836 | poll->events = io_poll_parse_events(sqe, flags); |
0969e783 JA |
5837 | return 0; |
5838 | } | |
5839 | ||
61e98203 | 5840 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
5841 | { |
5842 | struct io_poll_iocb *poll = &req->poll; | |
0969e783 | 5843 | struct io_poll_table ipt; |
350804e3 | 5844 | int ret; |
0969e783 | 5845 | |
d7718a9d | 5846 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5847 | |
350804e3 | 5848 | ret = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events); |
cfb1f1b5 PB |
5849 | if (!ret && ipt.error) |
5850 | req_set_fail(req); | |
350804e3 PB |
5851 | ret = ret ?: ipt.error; |
5852 | if (ret) | |
5853 | __io_req_complete(req, issue_flags, ret, 0); | |
5854 | return 0; | |
221c5eb2 JA |
5855 | } |
5856 | ||
c5de0036 | 5857 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
b69de288 JA |
5858 | { |
5859 | struct io_ring_ctx *ctx = req->ctx; | |
5860 | struct io_kiocb *preq; | |
448e6c46 | 5861 | int ret2, ret = 0; |
b69de288 | 5862 | |
79ebeaee | 5863 | spin_lock(&ctx->completion_lock); |
9ba5fac8 | 5864 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
350804e3 | 5865 | if (!preq || !io_poll_disarm(preq)) { |
448e6c46 | 5866 | spin_unlock(&ctx->completion_lock); |
350804e3 | 5867 | ret = preq ? -EALREADY : -ENOENT; |
448e6c46 | 5868 | goto out; |
c5de0036 | 5869 | } |
79ebeaee | 5870 | spin_unlock(&ctx->completion_lock); |
cb3b200e | 5871 | |
448e6c46 PB |
5872 | if (req->poll_update.update_events || req->poll_update.update_user_data) { |
5873 | /* only mask one event flags, keep behavior flags */ | |
5874 | if (req->poll_update.update_events) { | |
5875 | preq->poll.events &= ~0xffff; | |
5876 | preq->poll.events |= req->poll_update.events & 0xffff; | |
5877 | preq->poll.events |= IO_POLL_UNMASK; | |
cb3b200e | 5878 | } |
448e6c46 PB |
5879 | if (req->poll_update.update_user_data) |
5880 | preq->user_data = req->poll_update.new_user_data; | |
5881 | ||
5882 | ret2 = io_poll_add(preq, issue_flags); | |
5883 | /* successfully updated, don't complete poll request */ | |
5884 | if (!ret2) | |
5885 | goto out; | |
b69de288 | 5886 | } |
448e6c46 PB |
5887 | req_set_fail(preq); |
5888 | io_req_complete(preq, -ECANCELED); | |
5889 | out: | |
5890 | if (ret < 0) | |
5891 | req_set_fail(req); | |
5892 | /* complete update request, we're done with it */ | |
5893 | io_req_complete(req, ret); | |
b69de288 JA |
5894 | return 0; |
5895 | } | |
5896 | ||
f237c30a | 5897 | static void io_req_task_timeout(struct io_kiocb *req, bool *locked) |
89850fce | 5898 | { |
89850fce | 5899 | req_set_fail(req); |
505657bc | 5900 | io_req_complete_post(req, -ETIME, 0); |
89850fce JA |
5901 | } |
5902 | ||
5262f567 JA |
5903 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5904 | { | |
ad8a48ac JA |
5905 | struct io_timeout_data *data = container_of(timer, |
5906 | struct io_timeout_data, timer); | |
5907 | struct io_kiocb *req = data->req; | |
5908 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5909 | unsigned long flags; |
5910 | ||
89850fce | 5911 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
a71976f3 | 5912 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5913 | atomic_set(&req->ctx->cq_timeouts, |
5914 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
89850fce | 5915 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
01cec8c1 | 5916 | |
89850fce JA |
5917 | req->io_task_work.func = io_req_task_timeout; |
5918 | io_req_task_work_add(req); | |
5262f567 JA |
5919 | return HRTIMER_NORESTART; |
5920 | } | |
5921 | ||
fbd15848 PB |
5922 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5923 | __u64 user_data) | |
89850fce | 5924 | __must_hold(&ctx->timeout_lock) |
f254ac04 | 5925 | { |
fbd15848 | 5926 | struct io_timeout_data *io; |
47f46768 | 5927 | struct io_kiocb *req; |
fd9c7bc5 | 5928 | bool found = false; |
f254ac04 | 5929 | |
135fcde8 | 5930 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
fd9c7bc5 PB |
5931 | found = user_data == req->user_data; |
5932 | if (found) | |
47f46768 | 5933 | break; |
47f46768 | 5934 | } |
fd9c7bc5 PB |
5935 | if (!found) |
5936 | return ERR_PTR(-ENOENT); | |
fbd15848 PB |
5937 | |
5938 | io = req->async_data; | |
fd9c7bc5 | 5939 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
fbd15848 | 5940 | return ERR_PTR(-EALREADY); |
a71976f3 | 5941 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5942 | return req; |
5943 | } | |
47f46768 | 5944 | |
fbd15848 | 5945 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
ec3c3d0f | 5946 | __must_hold(&ctx->completion_lock) |
89850fce | 5947 | __must_hold(&ctx->timeout_lock) |
fbd15848 PB |
5948 | { |
5949 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5950 | ||
5951 | if (IS_ERR(req)) | |
5952 | return PTR_ERR(req); | |
f254ac04 | 5953 | |
93d2bcd2 | 5954 | req_set_fail(req); |
142f794e | 5955 | io_fill_cqe_req(req, -ECANCELED, 0); |
91c2f697 | 5956 | io_put_req_deferred(req); |
f254ac04 JA |
5957 | return 0; |
5958 | } | |
5959 | ||
50c1df2b JA |
5960 | static clockid_t io_timeout_get_clock(struct io_timeout_data *data) |
5961 | { | |
5962 | switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) { | |
5963 | case IORING_TIMEOUT_BOOTTIME: | |
5964 | return CLOCK_BOOTTIME; | |
5965 | case IORING_TIMEOUT_REALTIME: | |
5966 | return CLOCK_REALTIME; | |
5967 | default: | |
5968 | /* can't happen, vetted at prep time */ | |
5969 | WARN_ON_ONCE(1); | |
5970 | fallthrough; | |
5971 | case 0: | |
5972 | return CLOCK_MONOTONIC; | |
5973 | } | |
5974 | } | |
5975 | ||
f1042b6c PB |
5976 | static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5977 | struct timespec64 *ts, enum hrtimer_mode mode) | |
5978 | __must_hold(&ctx->timeout_lock) | |
5979 | { | |
5980 | struct io_timeout_data *io; | |
5981 | struct io_kiocb *req; | |
5982 | bool found = false; | |
5983 | ||
5984 | list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) { | |
5985 | found = user_data == req->user_data; | |
5986 | if (found) | |
5987 | break; | |
5988 | } | |
5989 | if (!found) | |
5990 | return -ENOENT; | |
5991 | ||
5992 | io = req->async_data; | |
5993 | if (hrtimer_try_to_cancel(&io->timer) == -1) | |
5994 | return -EALREADY; | |
5995 | hrtimer_init(&io->timer, io_timeout_get_clock(io), mode); | |
5996 | io->timer.function = io_link_timeout_fn; | |
5997 | hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode); | |
5998 | return 0; | |
5999 | } | |
6000 | ||
9c8e11b3 PB |
6001 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
6002 | struct timespec64 *ts, enum hrtimer_mode mode) | |
89850fce | 6003 | __must_hold(&ctx->timeout_lock) |
47f46768 | 6004 | { |
9c8e11b3 PB |
6005 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
6006 | struct io_timeout_data *data; | |
47f46768 | 6007 | |
9c8e11b3 PB |
6008 | if (IS_ERR(req)) |
6009 | return PTR_ERR(req); | |
47f46768 | 6010 | |
9c8e11b3 PB |
6011 | req->timeout.off = 0; /* noseq */ |
6012 | data = req->async_data; | |
6013 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
50c1df2b | 6014 | hrtimer_init(&data->timer, io_timeout_get_clock(data), mode); |
9c8e11b3 PB |
6015 | data->timer.function = io_timeout_fn; |
6016 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
6017 | return 0; | |
47f46768 JA |
6018 | } |
6019 | ||
3529d8c2 JA |
6020 | static int io_timeout_remove_prep(struct io_kiocb *req, |
6021 | const struct io_uring_sqe *sqe) | |
b29472ee | 6022 | { |
9c8e11b3 PB |
6023 | struct io_timeout_rem *tr = &req->timeout_rem; |
6024 | ||
b29472ee JA |
6025 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
6026 | return -EINVAL; | |
61710e43 DA |
6027 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6028 | return -EINVAL; | |
26578cda | 6029 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
b29472ee JA |
6030 | return -EINVAL; |
6031 | ||
f1042b6c | 6032 | tr->ltimeout = false; |
9c8e11b3 PB |
6033 | tr->addr = READ_ONCE(sqe->addr); |
6034 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
f1042b6c PB |
6035 | if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) { |
6036 | if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
6037 | return -EINVAL; | |
6038 | if (tr->flags & IORING_LINK_TIMEOUT_UPDATE) | |
6039 | tr->ltimeout = true; | |
6040 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS)) | |
9c8e11b3 PB |
6041 | return -EINVAL; |
6042 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
6043 | return -EFAULT; | |
6044 | } else if (tr->flags) { | |
6045 | /* timeout removal doesn't support flags */ | |
b29472ee | 6046 | return -EINVAL; |
9c8e11b3 | 6047 | } |
b29472ee | 6048 | |
b29472ee JA |
6049 | return 0; |
6050 | } | |
6051 | ||
8662daec PB |
6052 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
6053 | { | |
6054 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
6055 | : HRTIMER_MODE_REL; | |
6056 | } | |
6057 | ||
11365043 JA |
6058 | /* |
6059 | * Remove or update an existing timeout command | |
6060 | */ | |
61e98203 | 6061 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 6062 | { |
9c8e11b3 | 6063 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 6064 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 6065 | int ret; |
11365043 | 6066 | |
ec3c3d0f PB |
6067 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
6068 | spin_lock(&ctx->completion_lock); | |
6069 | spin_lock_irq(&ctx->timeout_lock); | |
9c8e11b3 | 6070 | ret = io_timeout_cancel(ctx, tr->addr); |
ec3c3d0f PB |
6071 | spin_unlock_irq(&ctx->timeout_lock); |
6072 | spin_unlock(&ctx->completion_lock); | |
6073 | } else { | |
f1042b6c PB |
6074 | enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags); |
6075 | ||
ec3c3d0f | 6076 | spin_lock_irq(&ctx->timeout_lock); |
f1042b6c PB |
6077 | if (tr->ltimeout) |
6078 | ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
6079 | else | |
6080 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
ec3c3d0f PB |
6081 | spin_unlock_irq(&ctx->timeout_lock); |
6082 | } | |
11365043 | 6083 | |
4e88d6e7 | 6084 | if (ret < 0) |
93d2bcd2 | 6085 | req_set_fail(req); |
505657bc | 6086 | io_req_complete_post(req, ret, 0); |
11365043 | 6087 | return 0; |
5262f567 JA |
6088 | } |
6089 | ||
3529d8c2 | 6090 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 6091 | bool is_timeout_link) |
5262f567 | 6092 | { |
ad8a48ac | 6093 | struct io_timeout_data *data; |
a41525ab | 6094 | unsigned flags; |
56080b02 | 6095 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 6096 | |
ad8a48ac | 6097 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 6098 | return -EINVAL; |
26578cda PB |
6099 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
6100 | sqe->splice_fd_in) | |
a41525ab | 6101 | return -EINVAL; |
56080b02 | 6102 | if (off && is_timeout_link) |
2d28390a | 6103 | return -EINVAL; |
a41525ab | 6104 | flags = READ_ONCE(sqe->timeout_flags); |
50c1df2b JA |
6105 | if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK)) |
6106 | return -EINVAL; | |
6107 | /* more than one clock specified is invalid, obviously */ | |
6108 | if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
5262f567 | 6109 | return -EINVAL; |
bdf20073 | 6110 | |
ef9dd637 | 6111 | INIT_LIST_HEAD(&req->timeout.list); |
bfe68a22 | 6112 | req->timeout.off = off; |
f18ee4cf PB |
6113 | if (unlikely(off && !req->ctx->off_timeout_used)) |
6114 | req->ctx->off_timeout_used = true; | |
26a61679 | 6115 | |
e8c2bc1f | 6116 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
6117 | return -ENOMEM; |
6118 | ||
e8c2bc1f | 6119 | data = req->async_data; |
ad8a48ac | 6120 | data->req = req; |
50c1df2b | 6121 | data->flags = flags; |
ad8a48ac JA |
6122 | |
6123 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
6124 | return -EFAULT; |
6125 | ||
7abdd4c7 | 6126 | INIT_LIST_HEAD(&req->timeout.list); |
8662daec | 6127 | data->mode = io_translate_timeout_mode(flags); |
50c1df2b | 6128 | hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode); |
b97e736a PB |
6129 | |
6130 | if (is_timeout_link) { | |
6131 | struct io_submit_link *link = &req->ctx->submit_state.link; | |
6132 | ||
6133 | if (!link->head) | |
6134 | return -EINVAL; | |
6135 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) | |
6136 | return -EINVAL; | |
4d13d1a4 PB |
6137 | req->timeout.head = link->last; |
6138 | link->last->flags |= REQ_F_ARM_LTIMEOUT; | |
b97e736a | 6139 | } |
ad8a48ac JA |
6140 | return 0; |
6141 | } | |
6142 | ||
61e98203 | 6143 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 6144 | { |
ad8a48ac | 6145 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 6146 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 6147 | struct list_head *entry; |
bfe68a22 | 6148 | u32 tail, off = req->timeout.off; |
ad8a48ac | 6149 | |
89850fce | 6150 | spin_lock_irq(&ctx->timeout_lock); |
93bd25bb | 6151 | |
5262f567 JA |
6152 | /* |
6153 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
6154 | * timeout event to be satisfied. If it isn't set, then this is |
6155 | * a pure timeout request, sequence isn't used. | |
5262f567 | 6156 | */ |
8eb7e2d0 | 6157 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
6158 | entry = ctx->timeout_list.prev; |
6159 | goto add; | |
6160 | } | |
5262f567 | 6161 | |
bfe68a22 PB |
6162 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
6163 | req->timeout.target_seq = tail + off; | |
5262f567 | 6164 | |
f010505b MDG |
6165 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
6166 | * This is safe because ->completion_lock is held, and submissions | |
6167 | * and completions are never mixed in the same ->completion_lock section. | |
6168 | */ | |
6169 | ctx->cq_last_tm_flush = tail; | |
6170 | ||
5262f567 JA |
6171 | /* |
6172 | * Insertion sort, ensuring the first entry in the list is always | |
6173 | * the one we need first. | |
6174 | */ | |
5262f567 | 6175 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
6176 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
6177 | timeout.list); | |
5262f567 | 6178 | |
8eb7e2d0 | 6179 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 6180 | continue; |
bfe68a22 PB |
6181 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
6182 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
6183 | break; |
6184 | } | |
93bd25bb | 6185 | add: |
135fcde8 | 6186 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
6187 | data->timer.function = io_timeout_fn; |
6188 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
89850fce | 6189 | spin_unlock_irq(&ctx->timeout_lock); |
5262f567 JA |
6190 | return 0; |
6191 | } | |
5262f567 | 6192 | |
f458dd84 PB |
6193 | struct io_cancel_data { |
6194 | struct io_ring_ctx *ctx; | |
6195 | u64 user_data; | |
6196 | }; | |
6197 | ||
62755e35 JA |
6198 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
6199 | { | |
6200 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 6201 | struct io_cancel_data *cd = data; |
62755e35 | 6202 | |
f458dd84 | 6203 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
62755e35 JA |
6204 | } |
6205 | ||
f458dd84 PB |
6206 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
6207 | struct io_ring_ctx *ctx) | |
62755e35 | 6208 | { |
f458dd84 | 6209 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
62755e35 | 6210 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
6211 | int ret = 0; |
6212 | ||
f458dd84 | 6213 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
6214 | return -ENOENT; |
6215 | ||
f458dd84 | 6216 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
62755e35 JA |
6217 | switch (cancel_ret) { |
6218 | case IO_WQ_CANCEL_OK: | |
6219 | ret = 0; | |
6220 | break; | |
6221 | case IO_WQ_CANCEL_RUNNING: | |
6222 | ret = -EALREADY; | |
6223 | break; | |
6224 | case IO_WQ_CANCEL_NOTFOUND: | |
6225 | ret = -ENOENT; | |
6226 | break; | |
6227 | } | |
6228 | ||
e977d6d3 JA |
6229 | return ret; |
6230 | } | |
6231 | ||
8cb01fac | 6232 | static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr) |
47f46768 | 6233 | { |
8cb01fac | 6234 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 JA |
6235 | int ret; |
6236 | ||
dadebc35 | 6237 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
8cb01fac | 6238 | |
f458dd84 | 6239 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
df9727af | 6240 | if (ret != -ENOENT) |
8cb01fac | 6241 | return ret; |
505657bc PB |
6242 | |
6243 | spin_lock(&ctx->completion_lock); | |
79ebeaee | 6244 | spin_lock_irq(&ctx->timeout_lock); |
47f46768 | 6245 | ret = io_timeout_cancel(ctx, sqe_addr); |
79ebeaee | 6246 | spin_unlock_irq(&ctx->timeout_lock); |
47f46768 | 6247 | if (ret != -ENOENT) |
505657bc PB |
6248 | goto out; |
6249 | ret = io_poll_cancel(ctx, sqe_addr, false); | |
6250 | out: | |
6251 | spin_unlock(&ctx->completion_lock); | |
6252 | return ret; | |
47f46768 JA |
6253 | } |
6254 | ||
3529d8c2 JA |
6255 | static int io_async_cancel_prep(struct io_kiocb *req, |
6256 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 6257 | { |
fbf23849 | 6258 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 6259 | return -EINVAL; |
61710e43 DA |
6260 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6261 | return -EINVAL; | |
26578cda PB |
6262 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
6263 | sqe->splice_fd_in) | |
e977d6d3 JA |
6264 | return -EINVAL; |
6265 | ||
fbf23849 JA |
6266 | req->cancel.addr = READ_ONCE(sqe->addr); |
6267 | return 0; | |
6268 | } | |
6269 | ||
61e98203 | 6270 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6271 | { |
6272 | struct io_ring_ctx *ctx = req->ctx; | |
58f99373 PB |
6273 | u64 sqe_addr = req->cancel.addr; |
6274 | struct io_tctx_node *node; | |
6275 | int ret; | |
6276 | ||
8cb01fac | 6277 | ret = io_try_cancel_userdata(req, sqe_addr); |
58f99373 PB |
6278 | if (ret != -ENOENT) |
6279 | goto done; | |
58f99373 PB |
6280 | |
6281 | /* slow path, try all io-wq's */ | |
6282 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
6283 | ret = -ENOENT; | |
6284 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
6285 | struct io_uring_task *tctx = node->task->io_uring; | |
fbf23849 | 6286 | |
58f99373 PB |
6287 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
6288 | if (ret != -ENOENT) | |
6289 | break; | |
6290 | } | |
6291 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
58f99373 | 6292 | done: |
58f99373 | 6293 | if (ret < 0) |
93d2bcd2 | 6294 | req_set_fail(req); |
505657bc | 6295 | io_req_complete_post(req, ret, 0); |
5262f567 JA |
6296 | return 0; |
6297 | } | |
6298 | ||
269bbe5f | 6299 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
6300 | const struct io_uring_sqe *sqe) |
6301 | { | |
61710e43 DA |
6302 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6303 | return -EINVAL; | |
26578cda | 6304 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
05f3fb3c JA |
6305 | return -EINVAL; |
6306 | ||
269bbe5f BM |
6307 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
6308 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
6309 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 6310 | return -EINVAL; |
269bbe5f | 6311 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
6312 | return 0; |
6313 | } | |
6314 | ||
889fca73 | 6315 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6316 | { |
6317 | struct io_ring_ctx *ctx = req->ctx; | |
c3bdad02 | 6318 | struct io_uring_rsrc_update2 up; |
05f3fb3c | 6319 | int ret; |
fbf23849 | 6320 | |
269bbe5f BM |
6321 | up.offset = req->rsrc_update.offset; |
6322 | up.data = req->rsrc_update.arg; | |
c3bdad02 PB |
6323 | up.nr = 0; |
6324 | up.tags = 0; | |
615cee49 | 6325 | up.resv = 0; |
c9747fa5 | 6326 | up.resv2 = 0; |
05f3fb3c | 6327 | |
cdb31c29 | 6328 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
fdecb662 | 6329 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
98f0b3b4 | 6330 | &up, req->rsrc_update.nr_args); |
cdb31c29 | 6331 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
05f3fb3c JA |
6332 | |
6333 | if (ret < 0) | |
93d2bcd2 | 6334 | req_set_fail(req); |
889fca73 | 6335 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
6336 | return 0; |
6337 | } | |
6338 | ||
bfe76559 | 6339 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 6340 | { |
d625c6ee | 6341 | switch (req->opcode) { |
e781573e | 6342 | case IORING_OP_NOP: |
bfe76559 | 6343 | return 0; |
f67676d1 JA |
6344 | case IORING_OP_READV: |
6345 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 6346 | case IORING_OP_READ: |
bfe76559 | 6347 | return io_read_prep(req, sqe); |
f67676d1 JA |
6348 | case IORING_OP_WRITEV: |
6349 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 6350 | case IORING_OP_WRITE: |
bfe76559 | 6351 | return io_write_prep(req, sqe); |
0969e783 | 6352 | case IORING_OP_POLL_ADD: |
bfe76559 | 6353 | return io_poll_add_prep(req, sqe); |
0969e783 | 6354 | case IORING_OP_POLL_REMOVE: |
c5de0036 | 6355 | return io_poll_update_prep(req, sqe); |
8ed8d3c3 | 6356 | case IORING_OP_FSYNC: |
1155c76a | 6357 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 6358 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 6359 | return io_sfr_prep(req, sqe); |
03b1230c | 6360 | case IORING_OP_SENDMSG: |
fddaface | 6361 | case IORING_OP_SEND: |
bfe76559 | 6362 | return io_sendmsg_prep(req, sqe); |
03b1230c | 6363 | case IORING_OP_RECVMSG: |
fddaface | 6364 | case IORING_OP_RECV: |
bfe76559 | 6365 | return io_recvmsg_prep(req, sqe); |
f499a021 | 6366 | case IORING_OP_CONNECT: |
bfe76559 | 6367 | return io_connect_prep(req, sqe); |
2d28390a | 6368 | case IORING_OP_TIMEOUT: |
bfe76559 | 6369 | return io_timeout_prep(req, sqe, false); |
b29472ee | 6370 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 6371 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 6372 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 6373 | return io_async_cancel_prep(req, sqe); |
2d28390a | 6374 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 6375 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 6376 | case IORING_OP_ACCEPT: |
bfe76559 | 6377 | return io_accept_prep(req, sqe); |
d63d1b5e | 6378 | case IORING_OP_FALLOCATE: |
bfe76559 | 6379 | return io_fallocate_prep(req, sqe); |
15b71abe | 6380 | case IORING_OP_OPENAT: |
bfe76559 | 6381 | return io_openat_prep(req, sqe); |
b5dba59e | 6382 | case IORING_OP_CLOSE: |
bfe76559 | 6383 | return io_close_prep(req, sqe); |
05f3fb3c | 6384 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 6385 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6386 | case IORING_OP_STATX: |
bfe76559 | 6387 | return io_statx_prep(req, sqe); |
4840e418 | 6388 | case IORING_OP_FADVISE: |
bfe76559 | 6389 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6390 | case IORING_OP_MADVISE: |
bfe76559 | 6391 | return io_madvise_prep(req, sqe); |
cebdb986 | 6392 | case IORING_OP_OPENAT2: |
bfe76559 | 6393 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6394 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6395 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6396 | case IORING_OP_SPLICE: |
bfe76559 | 6397 | return io_splice_prep(req, sqe); |
ddf0322d | 6398 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6399 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6400 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6401 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6402 | case IORING_OP_TEE: |
bfe76559 | 6403 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6404 | case IORING_OP_SHUTDOWN: |
6405 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6406 | case IORING_OP_RENAMEAT: |
6407 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6408 | case IORING_OP_UNLINKAT: |
6409 | return io_unlinkat_prep(req, sqe); | |
e34a02dc DK |
6410 | case IORING_OP_MKDIRAT: |
6411 | return io_mkdirat_prep(req, sqe); | |
7a8721f8 DK |
6412 | case IORING_OP_SYMLINKAT: |
6413 | return io_symlinkat_prep(req, sqe); | |
cf30da90 DK |
6414 | case IORING_OP_LINKAT: |
6415 | return io_linkat_prep(req, sqe); | |
f67676d1 JA |
6416 | } |
6417 | ||
bfe76559 PB |
6418 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6419 | req->opcode); | |
bd54b6fe | 6420 | return -EINVAL; |
bfe76559 PB |
6421 | } |
6422 | ||
93642ef8 | 6423 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 6424 | { |
b7e298d2 PB |
6425 | if (!io_op_defs[req->opcode].needs_async_setup) |
6426 | return 0; | |
6427 | if (WARN_ON_ONCE(req->async_data)) | |
6428 | return -EFAULT; | |
6429 | if (io_alloc_async_data(req)) | |
6430 | return -EAGAIN; | |
6431 | ||
93642ef8 PB |
6432 | switch (req->opcode) { |
6433 | case IORING_OP_READV: | |
93642ef8 PB |
6434 | return io_rw_prep_async(req, READ); |
6435 | case IORING_OP_WRITEV: | |
93642ef8 PB |
6436 | return io_rw_prep_async(req, WRITE); |
6437 | case IORING_OP_SENDMSG: | |
93642ef8 PB |
6438 | return io_sendmsg_prep_async(req); |
6439 | case IORING_OP_RECVMSG: | |
93642ef8 PB |
6440 | return io_recvmsg_prep_async(req); |
6441 | case IORING_OP_CONNECT: | |
6442 | return io_connect_prep_async(req); | |
6443 | } | |
b7e298d2 PB |
6444 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
6445 | req->opcode); | |
6446 | return -EFAULT; | |
f67676d1 JA |
6447 | } |
6448 | ||
9cf7c104 PB |
6449 | static u32 io_get_sequence(struct io_kiocb *req) |
6450 | { | |
a3dbdf54 | 6451 | u32 seq = req->ctx->cached_sq_head; |
9cf7c104 | 6452 | |
a3dbdf54 PB |
6453 | /* need original cached_sq_head, but it was increased for each req */ |
6454 | io_for_each_link(req, req) | |
6455 | seq--; | |
6456 | return seq; | |
9cf7c104 PB |
6457 | } |
6458 | ||
76cc33d7 | 6459 | static bool io_drain_req(struct io_kiocb *req) |
de0617e4 | 6460 | { |
3c19966d | 6461 | struct io_kiocb *pos; |
a197f664 | 6462 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6463 | struct io_defer_entry *de; |
f67676d1 | 6464 | int ret; |
9cf7c104 | 6465 | u32 seq; |
de0617e4 | 6466 | |
b8ce1b9d PB |
6467 | if (req->flags & REQ_F_FAIL) { |
6468 | io_req_complete_fail_submit(req); | |
6469 | return true; | |
6470 | } | |
6471 | ||
3c19966d PB |
6472 | /* |
6473 | * If we need to drain a request in the middle of a link, drain the | |
6474 | * head request and the next request/link after the current link. | |
6475 | * Considering sequential execution of links, IOSQE_IO_DRAIN will be | |
6476 | * maintained for every request of our link. | |
6477 | */ | |
6478 | if (ctx->drain_next) { | |
6479 | req->flags |= REQ_F_IO_DRAIN; | |
6480 | ctx->drain_next = false; | |
6481 | } | |
6482 | /* not interested in head, start from the first linked */ | |
6483 | io_for_each_link(pos, req->link) { | |
6484 | if (pos->flags & REQ_F_IO_DRAIN) { | |
6485 | ctx->drain_next = true; | |
6486 | req->flags |= REQ_F_IO_DRAIN; | |
6487 | break; | |
6488 | } | |
6489 | } | |
6490 | ||
9d858b21 | 6491 | /* Still need defer if there is pending req in defer list. */ |
9632410b | 6492 | spin_lock(&ctx->completion_lock); |
9cf7c104 | 6493 | if (likely(list_empty_careful(&ctx->defer_list) && |
10c66904 | 6494 | !(req->flags & REQ_F_IO_DRAIN))) { |
9632410b | 6495 | spin_unlock(&ctx->completion_lock); |
10c66904 | 6496 | ctx->drain_active = false; |
76cc33d7 | 6497 | return false; |
10c66904 | 6498 | } |
9632410b | 6499 | spin_unlock(&ctx->completion_lock); |
9cf7c104 PB |
6500 | |
6501 | seq = io_get_sequence(req); | |
6502 | /* Still a chance to pass the sequence check */ | |
6503 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
76cc33d7 | 6504 | return false; |
de0617e4 | 6505 | |
b7e298d2 | 6506 | ret = io_req_prep_async(req); |
be7053b7 | 6507 | if (ret) |
1b48773f | 6508 | goto fail; |
cbdcb435 | 6509 | io_prep_async_link(req); |
27dc8338 | 6510 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
76cc33d7 | 6511 | if (!de) { |
1b48773f PB |
6512 | ret = -ENOMEM; |
6513 | fail: | |
6514 | io_req_complete_failed(req, ret); | |
76cc33d7 PB |
6515 | return true; |
6516 | } | |
2d28390a | 6517 | |
79ebeaee | 6518 | spin_lock(&ctx->completion_lock); |
9cf7c104 | 6519 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
79ebeaee | 6520 | spin_unlock(&ctx->completion_lock); |
27dc8338 | 6521 | kfree(de); |
f237c30a | 6522 | io_queue_async_work(req, NULL); |
76cc33d7 | 6523 | return true; |
de0617e4 JA |
6524 | } |
6525 | ||
915967f6 | 6526 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 6527 | de->req = req; |
9cf7c104 | 6528 | de->seq = seq; |
27dc8338 | 6529 | list_add_tail(&de->list, &ctx->defer_list); |
79ebeaee | 6530 | spin_unlock(&ctx->completion_lock); |
76cc33d7 | 6531 | return true; |
de0617e4 JA |
6532 | } |
6533 | ||
68fb8979 | 6534 | static void io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6535 | { |
0e1b6fe3 PB |
6536 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
6537 | switch (req->opcode) { | |
6538 | case IORING_OP_READV: | |
6539 | case IORING_OP_READ_FIXED: | |
6540 | case IORING_OP_READ: | |
bcda7baa | 6541 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
6542 | break; |
6543 | case IORING_OP_RECVMSG: | |
6544 | case IORING_OP_RECV: | |
bcda7baa | 6545 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
6546 | break; |
6547 | } | |
99bc4c38 PB |
6548 | } |
6549 | ||
0e1b6fe3 PB |
6550 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6551 | switch (req->opcode) { | |
6552 | case IORING_OP_READV: | |
6553 | case IORING_OP_READ_FIXED: | |
6554 | case IORING_OP_READ: | |
6555 | case IORING_OP_WRITEV: | |
6556 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6557 | case IORING_OP_WRITE: { |
6558 | struct io_async_rw *io = req->async_data; | |
1dacb4df PB |
6559 | |
6560 | kfree(io->free_iovec); | |
0e1b6fe3 | 6561 | break; |
e8c2bc1f | 6562 | } |
0e1b6fe3 | 6563 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6564 | case IORING_OP_SENDMSG: { |
6565 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
6566 | |
6567 | kfree(io->free_iov); | |
0e1b6fe3 | 6568 | break; |
e8c2bc1f | 6569 | } |
f3cd4850 JA |
6570 | case IORING_OP_OPENAT: |
6571 | case IORING_OP_OPENAT2: | |
6572 | if (req->open.filename) | |
6573 | putname(req->open.filename); | |
6574 | break; | |
80a261fd JA |
6575 | case IORING_OP_RENAMEAT: |
6576 | putname(req->rename.oldpath); | |
6577 | putname(req->rename.newpath); | |
6578 | break; | |
14a1143b JA |
6579 | case IORING_OP_UNLINKAT: |
6580 | putname(req->unlink.filename); | |
6581 | break; | |
e34a02dc DK |
6582 | case IORING_OP_MKDIRAT: |
6583 | putname(req->mkdir.filename); | |
6584 | break; | |
7a8721f8 DK |
6585 | case IORING_OP_SYMLINKAT: |
6586 | putname(req->symlink.oldpath); | |
6587 | putname(req->symlink.newpath); | |
6588 | break; | |
cf30da90 DK |
6589 | case IORING_OP_LINKAT: |
6590 | putname(req->hardlink.oldpath); | |
6591 | putname(req->hardlink.newpath); | |
6592 | break; | |
0e1b6fe3 | 6593 | } |
99bc4c38 | 6594 | } |
75652a30 JA |
6595 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
6596 | kfree(req->apoll->double_poll); | |
6597 | kfree(req->apoll); | |
6598 | req->apoll = NULL; | |
6599 | } | |
3a0a6902 PB |
6600 | if (req->flags & REQ_F_INFLIGHT) { |
6601 | struct io_uring_task *tctx = req->task->io_uring; | |
6602 | ||
6603 | atomic_dec(&tctx->inflight_tracked); | |
3a0a6902 | 6604 | } |
c854357b | 6605 | if (req->flags & REQ_F_CREDS) |
b8e64b53 | 6606 | put_cred(req->creds); |
c854357b PB |
6607 | |
6608 | req->flags &= ~IO_REQ_CLEAN_FLAGS; | |
99bc4c38 PB |
6609 | } |
6610 | ||
889fca73 | 6611 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 6612 | { |
a197f664 | 6613 | struct io_ring_ctx *ctx = req->ctx; |
5730b27e | 6614 | const struct cred *creds = NULL; |
d625c6ee | 6615 | int ret; |
2b188cc1 | 6616 | |
b8e64b53 | 6617 | if ((req->flags & REQ_F_CREDS) && req->creds != current_cred()) |
c10d1f98 | 6618 | creds = override_creds(req->creds); |
5730b27e | 6619 | |
d625c6ee | 6620 | switch (req->opcode) { |
2b188cc1 | 6621 | case IORING_OP_NOP: |
889fca73 | 6622 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
6623 | break; |
6624 | case IORING_OP_READV: | |
edafccee | 6625 | case IORING_OP_READ_FIXED: |
3a6820f2 | 6626 | case IORING_OP_READ: |
889fca73 | 6627 | ret = io_read(req, issue_flags); |
edafccee | 6628 | break; |
3529d8c2 | 6629 | case IORING_OP_WRITEV: |
edafccee | 6630 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 6631 | case IORING_OP_WRITE: |
889fca73 | 6632 | ret = io_write(req, issue_flags); |
2b188cc1 | 6633 | break; |
c992fe29 | 6634 | case IORING_OP_FSYNC: |
45d189c6 | 6635 | ret = io_fsync(req, issue_flags); |
c992fe29 | 6636 | break; |
221c5eb2 | 6637 | case IORING_OP_POLL_ADD: |
61e98203 | 6638 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
6639 | break; |
6640 | case IORING_OP_POLL_REMOVE: | |
c5de0036 | 6641 | ret = io_poll_update(req, issue_flags); |
221c5eb2 | 6642 | break; |
5d17b4a4 | 6643 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 6644 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 6645 | break; |
0fa03c62 | 6646 | case IORING_OP_SENDMSG: |
889fca73 | 6647 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 6648 | break; |
fddaface | 6649 | case IORING_OP_SEND: |
889fca73 | 6650 | ret = io_send(req, issue_flags); |
0fa03c62 | 6651 | break; |
aa1fa28f | 6652 | case IORING_OP_RECVMSG: |
889fca73 | 6653 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 6654 | break; |
fddaface | 6655 | case IORING_OP_RECV: |
889fca73 | 6656 | ret = io_recv(req, issue_flags); |
aa1fa28f | 6657 | break; |
5262f567 | 6658 | case IORING_OP_TIMEOUT: |
61e98203 | 6659 | ret = io_timeout(req, issue_flags); |
5262f567 | 6660 | break; |
11365043 | 6661 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 6662 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 6663 | break; |
17f2fe35 | 6664 | case IORING_OP_ACCEPT: |
889fca73 | 6665 | ret = io_accept(req, issue_flags); |
17f2fe35 | 6666 | break; |
f8e85cf2 | 6667 | case IORING_OP_CONNECT: |
889fca73 | 6668 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 6669 | break; |
62755e35 | 6670 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 6671 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 6672 | break; |
d63d1b5e | 6673 | case IORING_OP_FALLOCATE: |
45d189c6 | 6674 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 6675 | break; |
15b71abe | 6676 | case IORING_OP_OPENAT: |
45d189c6 | 6677 | ret = io_openat(req, issue_flags); |
15b71abe | 6678 | break; |
b5dba59e | 6679 | case IORING_OP_CLOSE: |
889fca73 | 6680 | ret = io_close(req, issue_flags); |
b5dba59e | 6681 | break; |
05f3fb3c | 6682 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6683 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6684 | break; |
eddc7ef5 | 6685 | case IORING_OP_STATX: |
45d189c6 | 6686 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6687 | break; |
4840e418 | 6688 | case IORING_OP_FADVISE: |
45d189c6 | 6689 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6690 | break; |
c1ca757b | 6691 | case IORING_OP_MADVISE: |
45d189c6 | 6692 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6693 | break; |
cebdb986 | 6694 | case IORING_OP_OPENAT2: |
45d189c6 | 6695 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6696 | break; |
3e4827b0 | 6697 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6698 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6699 | break; |
7d67af2c | 6700 | case IORING_OP_SPLICE: |
45d189c6 | 6701 | ret = io_splice(req, issue_flags); |
7d67af2c | 6702 | break; |
ddf0322d | 6703 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6704 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6705 | break; |
067524e9 | 6706 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6707 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6708 | break; |
f2a8d5c7 | 6709 | case IORING_OP_TEE: |
45d189c6 | 6710 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6711 | break; |
36f4fa68 | 6712 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6713 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6714 | break; |
80a261fd | 6715 | case IORING_OP_RENAMEAT: |
45d189c6 | 6716 | ret = io_renameat(req, issue_flags); |
80a261fd | 6717 | break; |
14a1143b | 6718 | case IORING_OP_UNLINKAT: |
45d189c6 | 6719 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6720 | break; |
e34a02dc DK |
6721 | case IORING_OP_MKDIRAT: |
6722 | ret = io_mkdirat(req, issue_flags); | |
6723 | break; | |
7a8721f8 DK |
6724 | case IORING_OP_SYMLINKAT: |
6725 | ret = io_symlinkat(req, issue_flags); | |
6726 | break; | |
cf30da90 DK |
6727 | case IORING_OP_LINKAT: |
6728 | ret = io_linkat(req, issue_flags); | |
6729 | break; | |
2b188cc1 JA |
6730 | default: |
6731 | ret = -EINVAL; | |
6732 | break; | |
6733 | } | |
6734 | ||
5730b27e JA |
6735 | if (creds) |
6736 | revert_creds(creds); | |
def596e9 JA |
6737 | if (ret) |
6738 | return ret; | |
b532576e | 6739 | /* If the op doesn't have a file, we're not polling for it */ |
cb3d8972 PB |
6740 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
6741 | io_iopoll_req_issued(req); | |
def596e9 JA |
6742 | |
6743 | return 0; | |
2b188cc1 JA |
6744 | } |
6745 | ||
ebc11b6c PB |
6746 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
6747 | { | |
6748 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6749 | ||
6750 | req = io_put_req_find_next(req); | |
6751 | return req ? &req->work : NULL; | |
6752 | } | |
6753 | ||
5280f7e5 | 6754 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6755 | { |
6756 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6757 | struct io_kiocb *timeout; |
561fb04a | 6758 | int ret = 0; |
2b188cc1 | 6759 | |
48dcd38d PB |
6760 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
6761 | if (!(req->flags & REQ_F_REFCOUNT)) | |
6762 | __io_req_set_refcount(req, 2); | |
6763 | else | |
6764 | req_ref_get(req); | |
5d5901a3 | 6765 | |
6df1db6b PB |
6766 | timeout = io_prep_linked_timeout(req); |
6767 | if (timeout) | |
6768 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6769 | |
dadebc35 | 6770 | /* either cancelled or io-wq is dying, so don't touch tctx->iowq */ |
4014d943 | 6771 | if (work->flags & IO_WQ_WORK_CANCEL) |
561fb04a | 6772 | ret = -ECANCELED; |
31b51510 | 6773 | |
561fb04a | 6774 | if (!ret) { |
561fb04a | 6775 | do { |
889fca73 | 6776 | ret = io_issue_sqe(req, 0); |
561fb04a JA |
6777 | /* |
6778 | * We can get EAGAIN for polled IO even though we're | |
6779 | * forcing a sync submission from here, since we can't | |
6780 | * wait for request slots on the block side. | |
6781 | */ | |
b5691848 | 6782 | if (ret != -EAGAIN || !(req->ctx->flags & IORING_SETUP_IOPOLL)) |
561fb04a JA |
6783 | break; |
6784 | cond_resched(); | |
6785 | } while (1); | |
6786 | } | |
31b51510 | 6787 | |
a3df7698 | 6788 | /* avoid locking problems by failing it from a clean context */ |
5d5901a3 | 6789 | if (ret) |
a3df7698 | 6790 | io_req_task_queue_fail(req, ret); |
2b188cc1 JA |
6791 | } |
6792 | ||
aeca241b | 6793 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
042b0d85 | 6794 | unsigned i) |
65e19f54 | 6795 | { |
042b0d85 | 6796 | return &table->files[i]; |
dafecf19 PB |
6797 | } |
6798 | ||
65e19f54 JA |
6799 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6800 | int index) | |
6801 | { | |
aeca241b | 6802 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
65e19f54 | 6803 | |
a04b0ac0 | 6804 | return (struct file *) (slot->file_ptr & FFS_MASK); |
65e19f54 JA |
6805 | } |
6806 | ||
a04b0ac0 | 6807 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
9a321c98 PB |
6808 | { |
6809 | unsigned long file_ptr = (unsigned long) file; | |
6810 | ||
b191e2df | 6811 | if (__io_file_supports_nowait(file, READ)) |
9a321c98 | 6812 | file_ptr |= FFS_ASYNC_READ; |
b191e2df | 6813 | if (__io_file_supports_nowait(file, WRITE)) |
9a321c98 PB |
6814 | file_ptr |= FFS_ASYNC_WRITE; |
6815 | if (S_ISREG(file_inode(file)->i_mode)) | |
6816 | file_ptr |= FFS_ISREG; | |
a04b0ac0 | 6817 | file_slot->file_ptr = file_ptr; |
65e19f54 JA |
6818 | } |
6819 | ||
ac177053 PB |
6820 | static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx, |
6821 | struct io_kiocb *req, int fd) | |
09bb8394 | 6822 | { |
8da11c19 | 6823 | struct file *file; |
ac177053 | 6824 | unsigned long file_ptr; |
09bb8394 | 6825 | |
ac177053 PB |
6826 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
6827 | return NULL; | |
6828 | fd = array_index_nospec(fd, ctx->nr_user_files); | |
6829 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; | |
6830 | file = (struct file *) (file_ptr & FFS_MASK); | |
6831 | file_ptr &= ~FFS_MASK; | |
6832 | /* mask in overlapping REQ_F and FFS bits */ | |
b191e2df | 6833 | req->flags |= (file_ptr << REQ_F_NOWAIT_READ_BIT); |
ac177053 PB |
6834 | io_req_set_rsrc_node(req); |
6835 | return file; | |
6836 | } | |
d44f554e | 6837 | |
ac177053 | 6838 | static struct file *io_file_get_normal(struct io_ring_ctx *ctx, |
ac177053 PB |
6839 | struct io_kiocb *req, int fd) |
6840 | { | |
62906e89 | 6841 | struct file *file = fget(fd); |
ac177053 PB |
6842 | |
6843 | trace_io_uring_file_get(ctx, fd); | |
09bb8394 | 6844 | |
ac177053 PB |
6845 | /* we don't allow fixed io_uring files */ |
6846 | if (file && unlikely(file->f_op == &io_uring_fops)) | |
6847 | io_req_track_inflight(req); | |
8371adf5 | 6848 | return file; |
09bb8394 JA |
6849 | } |
6850 | ||
ac177053 | 6851 | static inline struct file *io_file_get(struct io_ring_ctx *ctx, |
ac177053 PB |
6852 | struct io_kiocb *req, int fd, bool fixed) |
6853 | { | |
6854 | if (fixed) | |
6855 | return io_file_get_fixed(ctx, req, fd); | |
6856 | else | |
62906e89 | 6857 | return io_file_get_normal(ctx, req, fd); |
ac177053 PB |
6858 | } |
6859 | ||
f237c30a | 6860 | static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked) |
89b263f6 JA |
6861 | { |
6862 | struct io_kiocb *prev = req->timeout.prev; | |
fc590dd1 | 6863 | int ret = -ENOENT; |
89b263f6 JA |
6864 | |
6865 | if (prev) { | |
fc590dd1 PB |
6866 | if (!(req->task->flags & PF_EXITING)) |
6867 | ret = io_try_cancel_userdata(req, prev->user_data); | |
505657bc | 6868 | io_req_complete_post(req, ret ?: -ETIME, 0); |
89b263f6 | 6869 | io_put_req(prev); |
89b263f6 JA |
6870 | } else { |
6871 | io_req_complete_post(req, -ETIME, 0); | |
6872 | } | |
6873 | } | |
6874 | ||
2665abfd | 6875 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6876 | { |
ad8a48ac JA |
6877 | struct io_timeout_data *data = container_of(timer, |
6878 | struct io_timeout_data, timer); | |
90cd7e42 | 6879 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6880 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6881 | unsigned long flags; |
2665abfd | 6882 | |
89b263f6 | 6883 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
90cd7e42 PB |
6884 | prev = req->timeout.head; |
6885 | req->timeout.head = NULL; | |
2665abfd JA |
6886 | |
6887 | /* | |
6888 | * We don't expect the list to be empty, that will only happen if we | |
6889 | * race with the completion of the linked work. | |
6890 | */ | |
447c19f3 | 6891 | if (prev) { |
f2f87370 | 6892 | io_remove_next_linked(prev); |
447c19f3 PB |
6893 | if (!req_ref_inc_not_zero(prev)) |
6894 | prev = NULL; | |
6895 | } | |
ef9dd637 | 6896 | list_del(&req->timeout.list); |
89b263f6 JA |
6897 | req->timeout.prev = prev; |
6898 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); | |
2665abfd | 6899 | |
89b263f6 JA |
6900 | req->io_task_work.func = io_req_task_link_timeout; |
6901 | io_req_task_work_add(req); | |
2665abfd JA |
6902 | return HRTIMER_NORESTART; |
6903 | } | |
6904 | ||
de968c18 | 6905 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6906 | { |
de968c18 PB |
6907 | struct io_ring_ctx *ctx = req->ctx; |
6908 | ||
89b263f6 | 6909 | spin_lock_irq(&ctx->timeout_lock); |
76a46e06 | 6910 | /* |
f2f87370 PB |
6911 | * If the back reference is NULL, then our linked request finished |
6912 | * before we got a chance to setup the timer | |
76a46e06 | 6913 | */ |
90cd7e42 | 6914 | if (req->timeout.head) { |
e8c2bc1f | 6915 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6916 | |
ad8a48ac JA |
6917 | data->timer.function = io_link_timeout_fn; |
6918 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6919 | data->mode); | |
ef9dd637 | 6920 | list_add_tail(&req->timeout.list, &ctx->ltimeout_list); |
2665abfd | 6921 | } |
89b263f6 | 6922 | spin_unlock_irq(&ctx->timeout_lock); |
2665abfd | 6923 | /* drop submission reference */ |
76a46e06 JA |
6924 | io_put_req(req); |
6925 | } | |
2665abfd | 6926 | |
c5eef2b9 | 6927 | static void __io_queue_sqe(struct io_kiocb *req) |
282cdc86 | 6928 | __must_hold(&req->ctx->uring_lock) |
2b188cc1 | 6929 | { |
906c6caa | 6930 | struct io_kiocb *linked_timeout; |
e0c5c576 | 6931 | int ret; |
2b188cc1 | 6932 | |
59b735ae | 6933 | issue_sqe: |
c5eef2b9 | 6934 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 6935 | |
491381ce JA |
6936 | /* |
6937 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6938 | * doesn't support non-blocking read/write attempts | |
6939 | */ | |
1840038e | 6940 | if (likely(!ret)) { |
e342c807 | 6941 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
c5eef2b9 | 6942 | struct io_ring_ctx *ctx = req->ctx; |
cd0ca2e0 | 6943 | struct io_submit_state *state = &ctx->submit_state; |
e65ef56d | 6944 | |
cd0ca2e0 PB |
6945 | state->compl_reqs[state->compl_nr++] = req; |
6946 | if (state->compl_nr == ARRAY_SIZE(state->compl_reqs)) | |
2a2758f2 | 6947 | io_submit_flush_completions(ctx); |
906c6caa | 6948 | return; |
0d63c148 | 6949 | } |
906c6caa PB |
6950 | |
6951 | linked_timeout = io_prep_linked_timeout(req); | |
6952 | if (linked_timeout) | |
6953 | io_queue_linked_timeout(linked_timeout); | |
1840038e | 6954 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
906c6caa PB |
6955 | linked_timeout = io_prep_linked_timeout(req); |
6956 | ||
59b735ae OL |
6957 | switch (io_arm_poll_handler(req)) { |
6958 | case IO_APOLL_READY: | |
906c6caa | 6959 | if (linked_timeout) |
4ea672ab | 6960 | io_queue_linked_timeout(linked_timeout); |
59b735ae OL |
6961 | goto issue_sqe; |
6962 | case IO_APOLL_ABORTED: | |
1840038e PB |
6963 | /* |
6964 | * Queued up for async execution, worker will release | |
6965 | * submit reference when the iocb is actually submitted. | |
6966 | */ | |
f237c30a | 6967 | io_queue_async_work(req, NULL); |
59b735ae | 6968 | break; |
1840038e | 6969 | } |
906c6caa PB |
6970 | |
6971 | if (linked_timeout) | |
6972 | io_queue_linked_timeout(linked_timeout); | |
0d63c148 | 6973 | } else { |
f41db273 | 6974 | io_req_complete_failed(req, ret); |
9e645e11 | 6975 | } |
2b188cc1 JA |
6976 | } |
6977 | ||
441b8a78 | 6978 | static inline void io_queue_sqe(struct io_kiocb *req) |
282cdc86 | 6979 | __must_hold(&req->ctx->uring_lock) |
4fe2c963 | 6980 | { |
10c66904 | 6981 | if (unlikely(req->ctx->drain_active) && io_drain_req(req)) |
76cc33d7 | 6982 | return; |
4fe2c963 | 6983 | |
a8295b98 | 6984 | if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) { |
c5eef2b9 | 6985 | __io_queue_sqe(req); |
a8295b98 | 6986 | } else if (req->flags & REQ_F_FAIL) { |
c6d3d9cb | 6987 | io_req_complete_fail_submit(req); |
76cc33d7 PB |
6988 | } else { |
6989 | int ret = io_req_prep_async(req); | |
6990 | ||
6991 | if (unlikely(ret)) | |
6992 | io_req_complete_failed(req, ret); | |
6993 | else | |
f237c30a | 6994 | io_queue_async_work(req, NULL); |
ce35a47a | 6995 | } |
4fe2c963 JL |
6996 | } |
6997 | ||
b16fed66 PB |
6998 | /* |
6999 | * Check SQE restrictions (opcode and flags). | |
7000 | * | |
7001 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
7002 | */ | |
7003 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
7004 | struct io_kiocb *req, | |
7005 | unsigned int sqe_flags) | |
4fe2c963 | 7006 | { |
4cfb25bf | 7007 | if (likely(!ctx->restricted)) |
b16fed66 PB |
7008 | return true; |
7009 | ||
7010 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) | |
7011 | return false; | |
7012 | ||
7013 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
7014 | ctx->restrictions.sqe_flags_required) | |
7015 | return false; | |
7016 | ||
7017 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
7018 | ctx->restrictions.sqe_flags_required)) | |
7019 | return false; | |
7020 | ||
7021 | return true; | |
4fe2c963 JL |
7022 | } |
7023 | ||
b16fed66 PB |
7024 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
7025 | const struct io_uring_sqe *sqe) | |
282cdc86 | 7026 | __must_hold(&ctx->uring_lock) |
b16fed66 PB |
7027 | { |
7028 | struct io_submit_state *state; | |
7029 | unsigned int sqe_flags; | |
003e8dcc | 7030 | int personality, ret = 0; |
b16fed66 | 7031 | |
864ea921 | 7032 | /* req is partially pre-initialised, see io_preinit_req() */ |
b16fed66 PB |
7033 | req->opcode = READ_ONCE(sqe->opcode); |
7034 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
7035 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
7036 | req->user_data = READ_ONCE(sqe->user_data); | |
b16fed66 | 7037 | req->file = NULL; |
b16fed66 | 7038 | req->fixed_rsrc_refs = NULL; |
b16fed66 | 7039 | req->task = current; |
b16fed66 PB |
7040 | |
7041 | /* enforce forwards compatibility on users */ | |
dddca226 | 7042 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) |
b16fed66 | 7043 | return -EINVAL; |
b16fed66 PB |
7044 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
7045 | return -EINVAL; | |
4cfb25bf | 7046 | if (!io_check_restriction(ctx, req, sqe_flags)) |
b16fed66 PB |
7047 | return -EACCES; |
7048 | ||
7049 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
7050 | !io_op_defs[req->opcode].buffer_select) | |
7051 | return -EOPNOTSUPP; | |
3c19966d PB |
7052 | if (unlikely(sqe_flags & IOSQE_IO_DRAIN)) |
7053 | ctx->drain_active = true; | |
863e0560 | 7054 | |
003e8dcc JA |
7055 | personality = READ_ONCE(sqe->personality); |
7056 | if (personality) { | |
c10d1f98 PB |
7057 | req->creds = xa_load(&ctx->personalities, personality); |
7058 | if (!req->creds) | |
003e8dcc | 7059 | return -EINVAL; |
c10d1f98 | 7060 | get_cred(req->creds); |
b8e64b53 | 7061 | req->flags |= REQ_F_CREDS; |
003e8dcc | 7062 | } |
b16fed66 PB |
7063 | state = &ctx->submit_state; |
7064 | ||
7065 | /* | |
7066 | * Plug now if we have more than 1 IO left after this, and the target | |
7067 | * is potentially a read/write to block based storage. | |
7068 | */ | |
7069 | if (!state->plug_started && state->ios_left > 1 && | |
7070 | io_op_defs[req->opcode].plug) { | |
7071 | blk_start_plug(&state->plug); | |
7072 | state->plug_started = true; | |
7073 | } | |
7074 | ||
7075 | if (io_op_defs[req->opcode].needs_file) { | |
62906e89 | 7076 | req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd), |
ac177053 | 7077 | (sqe_flags & IOSQE_FIXED_FILE)); |
b16fed66 PB |
7078 | if (unlikely(!req->file)) |
7079 | ret = -EBADF; | |
7080 | } | |
7081 | ||
7082 | state->ios_left--; | |
7083 | return ret; | |
7084 | } | |
7085 | ||
a6b8cadc | 7086 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 7087 | const struct io_uring_sqe *sqe) |
282cdc86 | 7088 | __must_hold(&ctx->uring_lock) |
9e645e11 | 7089 | { |
a1ab7b35 | 7090 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 7091 | int ret; |
9e645e11 | 7092 | |
a6b8cadc PB |
7093 | ret = io_init_req(ctx, req, sqe); |
7094 | if (unlikely(ret)) { | |
7095 | fail_req: | |
a8295b98 | 7096 | /* fail even hard links since we don't submit */ |
de59bc10 | 7097 | if (link->head) { |
a8295b98 HX |
7098 | /* |
7099 | * we can judge a link req is failed or cancelled by if | |
7100 | * REQ_F_FAIL is set, but the head is an exception since | |
7101 | * it may be set REQ_F_FAIL because of other req's failure | |
7102 | * so let's leverage req->result to distinguish if a head | |
7103 | * is set REQ_F_FAIL because of its failure or other req's | |
7104 | * failure so that we can set the correct ret code for it. | |
7105 | * init result here to avoid affecting the normal path. | |
7106 | */ | |
7107 | if (!(link->head->flags & REQ_F_FAIL)) | |
7108 | req_fail_link_node(link->head, -ECANCELED); | |
7109 | } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { | |
7110 | /* | |
7111 | * the current req is a normal req, we should return | |
7112 | * error and thus break the submittion loop. | |
7113 | */ | |
7114 | io_req_complete_failed(req, ret); | |
7115 | return ret; | |
de59bc10 | 7116 | } |
a8295b98 HX |
7117 | req_fail_link_node(req, ret); |
7118 | } else { | |
7119 | ret = io_req_prep(req, sqe); | |
7120 | if (unlikely(ret)) | |
7121 | goto fail_req; | |
a6b8cadc | 7122 | } |
441b8a78 | 7123 | |
be7053b7 | 7124 | /* don't need @sqe from now on */ |
236daeae OL |
7125 | trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data, |
7126 | req->flags, true, | |
7127 | ctx->flags & IORING_SETUP_SQPOLL); | |
a6b8cadc | 7128 | |
9e645e11 JA |
7129 | /* |
7130 | * If we already have a head request, queue this one for async | |
7131 | * submittal once the head completes. If we don't have a head but | |
7132 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
7133 | * submitted sync once the chain is complete. If none of those | |
7134 | * conditions are true (normal request), then just queue it. | |
7135 | */ | |
863e0560 PB |
7136 | if (link->head) { |
7137 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 7138 | |
a8295b98 HX |
7139 | if (!(req->flags & REQ_F_FAIL)) { |
7140 | ret = io_req_prep_async(req); | |
7141 | if (unlikely(ret)) { | |
7142 | req_fail_link_node(req, ret); | |
7143 | if (!(head->flags & REQ_F_FAIL)) | |
7144 | req_fail_link_node(head, -ECANCELED); | |
7145 | } | |
7146 | } | |
9d76377f | 7147 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 7148 | link->last->link = req; |
863e0560 | 7149 | link->last = req; |
32fe525b PB |
7150 | |
7151 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 7152 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
863e0560 | 7153 | link->head = NULL; |
5e159204 | 7154 | io_queue_sqe(head); |
32fe525b | 7155 | } |
9e645e11 | 7156 | } else { |
ef4ff581 | 7157 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
863e0560 PB |
7158 | link->head = req; |
7159 | link->last = req; | |
711be031 | 7160 | } else { |
be7053b7 | 7161 | io_queue_sqe(req); |
711be031 | 7162 | } |
9e645e11 | 7163 | } |
2e6e1fde | 7164 | |
1d4240cc | 7165 | return 0; |
9e645e11 JA |
7166 | } |
7167 | ||
9a56a232 JA |
7168 | /* |
7169 | * Batched submission is done, ensure local IO is flushed out. | |
7170 | */ | |
ba88ff11 PB |
7171 | static void io_submit_state_end(struct io_submit_state *state, |
7172 | struct io_ring_ctx *ctx) | |
9a56a232 | 7173 | { |
a1ab7b35 | 7174 | if (state->link.head) |
de59bc10 | 7175 | io_queue_sqe(state->link.head); |
cd0ca2e0 | 7176 | if (state->compl_nr) |
2a2758f2 | 7177 | io_submit_flush_completions(ctx); |
27926b68 JA |
7178 | if (state->plug_started) |
7179 | blk_finish_plug(&state->plug); | |
9a56a232 JA |
7180 | } |
7181 | ||
7182 | /* | |
7183 | * Start submission side cache. | |
7184 | */ | |
7185 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 7186 | unsigned int max_ios) |
9a56a232 | 7187 | { |
27926b68 | 7188 | state->plug_started = false; |
9a56a232 | 7189 | state->ios_left = max_ios; |
a1ab7b35 PB |
7190 | /* set only head, no need to init link_last in advance */ |
7191 | state->link.head = NULL; | |
9a56a232 JA |
7192 | } |
7193 | ||
2b188cc1 JA |
7194 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
7195 | { | |
75b28aff | 7196 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 7197 | |
caf582c6 PB |
7198 | /* |
7199 | * Ensure any loads from the SQEs are done at this point, | |
7200 | * since once we write the new head, the application could | |
7201 | * write new data to them. | |
7202 | */ | |
7203 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
7204 | } |
7205 | ||
2b188cc1 | 7206 | /* |
dd9ae8a0 | 7207 | * Fetch an sqe, if one is available. Note this returns a pointer to memory |
2b188cc1 JA |
7208 | * that is mapped by userspace. This means that care needs to be taken to |
7209 | * ensure that reads are stable, as we cannot rely on userspace always | |
7210 | * being a good citizen. If members of the sqe are validated and then later | |
7211 | * used, it's important that those reads are done through READ_ONCE() to | |
7212 | * prevent a re-load down the line. | |
7213 | */ | |
709b302f | 7214 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 7215 | { |
ea5ab3b5 | 7216 | unsigned head, mask = ctx->sq_entries - 1; |
17d3aeb3 | 7217 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
2b188cc1 JA |
7218 | |
7219 | /* | |
7220 | * The cached sq head (or cq tail) serves two purposes: | |
7221 | * | |
7222 | * 1) allows us to batch the cost of updating the user visible | |
7223 | * head updates. | |
7224 | * 2) allows the kernel side to track the head on its own, even | |
7225 | * though the application is the one updating it. | |
7226 | */ | |
17d3aeb3 | 7227 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
709b302f PB |
7228 | if (likely(head < ctx->sq_entries)) |
7229 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
7230 | |
7231 | /* drop invalid entries */ | |
15641e42 PB |
7232 | ctx->cq_extra--; |
7233 | WRITE_ONCE(ctx->rings->sq_dropped, | |
7234 | READ_ONCE(ctx->rings->sq_dropped) + 1); | |
709b302f PB |
7235 | return NULL; |
7236 | } | |
7237 | ||
0f212204 | 7238 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
282cdc86 | 7239 | __must_hold(&ctx->uring_lock) |
6c271ce2 | 7240 | { |
46c4e16a | 7241 | int submitted = 0; |
6c271ce2 | 7242 | |
ee7d46d9 PB |
7243 | /* make sure SQ entry isn't read before tail */ |
7244 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
2b85edfc PB |
7245 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
7246 | return -EAGAIN; | |
9a10867a | 7247 | io_get_task_refs(nr); |
6c271ce2 | 7248 | |
ba88ff11 | 7249 | io_submit_state_start(&ctx->submit_state, nr); |
46c4e16a | 7250 | while (submitted < nr) { |
3529d8c2 | 7251 | const struct io_uring_sqe *sqe; |
196be95c | 7252 | struct io_kiocb *req; |
fb5ccc98 | 7253 | |
258b29a9 | 7254 | req = io_alloc_req(ctx); |
196be95c PB |
7255 | if (unlikely(!req)) { |
7256 | if (!submitted) | |
7257 | submitted = -EAGAIN; | |
fb5ccc98 | 7258 | break; |
196be95c | 7259 | } |
4fccfcbb PB |
7260 | sqe = io_get_sqe(ctx); |
7261 | if (unlikely(!sqe)) { | |
0c6e1d7f | 7262 | list_add(&req->inflight_entry, &ctx->submit_state.free_list); |
4fccfcbb PB |
7263 | break; |
7264 | } | |
d3656344 JA |
7265 | /* will complete beyond this point, count as submitted */ |
7266 | submitted++; | |
a1ab7b35 | 7267 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 7268 | break; |
6c271ce2 JA |
7269 | } |
7270 | ||
9466f437 PB |
7271 | if (unlikely(submitted != nr)) { |
7272 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 | 7273 | int unused = nr - ref_used; |
9466f437 | 7274 | |
09899b19 | 7275 | current->io_uring->cached_refs += unused; |
d8a6df10 | 7276 | percpu_ref_put_many(&ctx->refs, unused); |
9466f437 | 7277 | } |
6c271ce2 | 7278 | |
a1ab7b35 | 7279 | io_submit_state_end(&ctx->submit_state, ctx); |
ae9428ca PB |
7280 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
7281 | io_commit_sqring(ctx); | |
7282 | ||
6c271ce2 JA |
7283 | return submitted; |
7284 | } | |
7285 | ||
e4b6d902 PB |
7286 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
7287 | { | |
7288 | return READ_ONCE(sqd->state); | |
7289 | } | |
7290 | ||
23b3628e XW |
7291 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
7292 | { | |
7293 | /* Tell userspace we may need a wakeup call */ | |
79ebeaee | 7294 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7295 | WRITE_ONCE(ctx->rings->sq_flags, |
7296 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7297 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7298 | } |
7299 | ||
7300 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
7301 | { | |
79ebeaee | 7302 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7303 | WRITE_ONCE(ctx->rings->sq_flags, |
7304 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7305 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7306 | } |
7307 | ||
08369246 | 7308 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 7309 | { |
c8d1ba58 | 7310 | unsigned int to_submit; |
bdcd3eab | 7311 | int ret = 0; |
6c271ce2 | 7312 | |
c8d1ba58 | 7313 | to_submit = io_sqring_entries(ctx); |
e95eee2d | 7314 | /* if we're handling multiple rings, cap submit size for fairness */ |
4ce8ad95 OL |
7315 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
7316 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; | |
e95eee2d | 7317 | |
906a3c6f | 7318 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
c8d1ba58 | 7319 | unsigned nr_events = 0; |
948e1947 PB |
7320 | const struct cred *creds = NULL; |
7321 | ||
7322 | if (ctx->sq_creds != current_cred()) | |
7323 | creds = override_creds(ctx->sq_creds); | |
a4c0b3de | 7324 | |
c8d1ba58 | 7325 | mutex_lock(&ctx->uring_lock); |
906a3c6f | 7326 | if (!list_empty(&ctx->iopoll_list)) |
a8576af9 | 7327 | io_do_iopoll(ctx, &nr_events, 0); |
906a3c6f | 7328 | |
3b763ba1 PB |
7329 | /* |
7330 | * Don't submit if refs are dying, good for io_uring_register(), | |
7331 | * but also it is relied upon by io_ring_exit_work() | |
7332 | */ | |
0298ef96 PB |
7333 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
7334 | !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
08369246 | 7335 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 | 7336 | mutex_unlock(&ctx->uring_lock); |
6c271ce2 | 7337 | |
acfb381d PB |
7338 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
7339 | wake_up(&ctx->sqo_sq_wait); | |
948e1947 PB |
7340 | if (creds) |
7341 | revert_creds(creds); | |
acfb381d | 7342 | } |
6c271ce2 | 7343 | |
08369246 XW |
7344 | return ret; |
7345 | } | |
6c271ce2 | 7346 | |
08369246 XW |
7347 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
7348 | { | |
7349 | struct io_ring_ctx *ctx; | |
7350 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 7351 | |
c9dca27d PB |
7352 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7353 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); | |
08369246 | 7354 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 7355 | } |
6c271ce2 | 7356 | |
e4b6d902 PB |
7357 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
7358 | { | |
7359 | bool did_sig = false; | |
7360 | struct ksignal ksig; | |
7361 | ||
7362 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || | |
7363 | signal_pending(current)) { | |
7364 | mutex_unlock(&sqd->lock); | |
7365 | if (signal_pending(current)) | |
7366 | did_sig = get_signal(&ksig); | |
7367 | cond_resched(); | |
7368 | mutex_lock(&sqd->lock); | |
7369 | } | |
e4b6d902 PB |
7370 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
7371 | } | |
7372 | ||
c8d1ba58 JA |
7373 | static int io_sq_thread(void *data) |
7374 | { | |
69fb2131 JA |
7375 | struct io_sq_data *sqd = data; |
7376 | struct io_ring_ctx *ctx; | |
a0d9205f | 7377 | unsigned long timeout = 0; |
37d1e2e3 | 7378 | char buf[TASK_COMM_LEN]; |
08369246 | 7379 | DEFINE_WAIT(wait); |
6c271ce2 | 7380 | |
696ee88a | 7381 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
37d1e2e3 | 7382 | set_task_comm(current, buf); |
37d1e2e3 JA |
7383 | |
7384 | if (sqd->sq_cpu != -1) | |
7385 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
7386 | else | |
7387 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
7388 | current->flags |= PF_NO_SETAFFINITY; | |
7389 | ||
09a6f4ef | 7390 | mutex_lock(&sqd->lock); |
e4b6d902 | 7391 | while (1) { |
1a924a80 | 7392 | bool cap_entries, sqt_spin = false; |
c1edbf5f | 7393 | |
e4b6d902 PB |
7394 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
7395 | if (io_sqd_handle_event(sqd)) | |
c7d95613 | 7396 | break; |
08369246 XW |
7397 | timeout = jiffies + sqd->sq_thread_idle; |
7398 | } | |
e4b6d902 | 7399 | |
e95eee2d | 7400 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 7401 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
948e1947 | 7402 | int ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a | 7403 | |
08369246 XW |
7404 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) |
7405 | sqt_spin = true; | |
69fb2131 | 7406 | } |
dd432ea5 PB |
7407 | if (io_run_task_work()) |
7408 | sqt_spin = true; | |
6c271ce2 | 7409 | |
08369246 | 7410 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 7411 | cond_resched(); |
08369246 XW |
7412 | if (sqt_spin) |
7413 | timeout = jiffies + sqd->sq_thread_idle; | |
7414 | continue; | |
7415 | } | |
7416 | ||
08369246 | 7417 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
dd432ea5 | 7418 | if (!io_sqd_events_pending(sqd) && !current->task_works) { |
1a924a80 PB |
7419 | bool needs_sched = true; |
7420 | ||
724cb4f9 | 7421 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
aaa9f0f4 PB |
7422 | io_ring_set_wakeup_flag(ctx); |
7423 | ||
724cb4f9 HX |
7424 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
7425 | !list_empty_careful(&ctx->iopoll_list)) { | |
7426 | needs_sched = false; | |
7427 | break; | |
7428 | } | |
7429 | if (io_sqring_entries(ctx)) { | |
7430 | needs_sched = false; | |
7431 | break; | |
7432 | } | |
7433 | } | |
7434 | ||
7435 | if (needs_sched) { | |
7436 | mutex_unlock(&sqd->lock); | |
7437 | schedule(); | |
7438 | mutex_lock(&sqd->lock); | |
7439 | } | |
69fb2131 JA |
7440 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7441 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 7442 | } |
08369246 XW |
7443 | |
7444 | finish_wait(&sqd->wait, &wait); | |
7445 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 | 7446 | } |
28cea78a | 7447 | |
78cc687b | 7448 | io_uring_cancel_generic(true, sqd); |
37d1e2e3 | 7449 | sqd->thread = NULL; |
05962f95 | 7450 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
5f3f26f9 | 7451 | io_ring_set_wakeup_flag(ctx); |
521d6a73 | 7452 | io_run_task_work(); |
734551df PB |
7453 | mutex_unlock(&sqd->lock); |
7454 | ||
37d1e2e3 JA |
7455 | complete(&sqd->exited); |
7456 | do_exit(0); | |
6c271ce2 JA |
7457 | } |
7458 | ||
bda52162 JA |
7459 | struct io_wait_queue { |
7460 | struct wait_queue_entry wq; | |
7461 | struct io_ring_ctx *ctx; | |
5fd46178 | 7462 | unsigned cq_tail; |
bda52162 JA |
7463 | unsigned nr_timeouts; |
7464 | }; | |
7465 | ||
6c503150 | 7466 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
7467 | { |
7468 | struct io_ring_ctx *ctx = iowq->ctx; | |
5fd46178 | 7469 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
bda52162 JA |
7470 | |
7471 | /* | |
d195a66e | 7472 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
7473 | * started waiting. For timeouts, we always want to return to userspace, |
7474 | * regardless of event count. | |
7475 | */ | |
5fd46178 | 7476 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
bda52162 JA |
7477 | } |
7478 | ||
7479 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
7480 | int wake_flags, void *key) | |
7481 | { | |
7482 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
7483 | wq); | |
7484 | ||
6c503150 PB |
7485 | /* |
7486 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
7487 | * the task, and the next invocation will do it. | |
7488 | */ | |
5ed7a37d | 7489 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow)) |
6c503150 PB |
7490 | return autoremove_wake_function(curr, mode, wake_flags, key); |
7491 | return -1; | |
bda52162 JA |
7492 | } |
7493 | ||
af9c1a44 JA |
7494 | static int io_run_task_work_sig(void) |
7495 | { | |
7496 | if (io_run_task_work()) | |
7497 | return 1; | |
7498 | if (!signal_pending(current)) | |
7499 | return 0; | |
0b8cfa97 | 7500 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
792ee0f6 | 7501 | return -ERESTARTSYS; |
af9c1a44 JA |
7502 | return -EINTR; |
7503 | } | |
7504 | ||
eeb60b9a PB |
7505 | /* when returns >0, the caller should retry */ |
7506 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
7507 | struct io_wait_queue *iowq, | |
68668891 | 7508 | ktime_t timeout) |
eeb60b9a PB |
7509 | { |
7510 | int ret; | |
7511 | ||
7512 | /* make sure we run task_work before checking for signals */ | |
7513 | ret = io_run_task_work_sig(); | |
7514 | if (ret || io_should_wake(iowq)) | |
7515 | return ret; | |
7516 | /* let the caller flush overflows, retry */ | |
5ed7a37d | 7517 | if (test_bit(0, &ctx->check_cq_overflow)) |
eeb60b9a PB |
7518 | return 1; |
7519 | ||
68668891 JA |
7520 | if (!schedule_hrtimeout(&timeout, HRTIMER_MODE_ABS)) |
7521 | return -ETIME; | |
7522 | return 1; | |
eeb60b9a PB |
7523 | } |
7524 | ||
2b188cc1 JA |
7525 | /* |
7526 | * Wait until events become available, if we don't already have some. The | |
7527 | * application must reap them itself, as they reside on the shared cq ring. | |
7528 | */ | |
7529 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
7530 | const sigset_t __user *sig, size_t sigsz, |
7531 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 7532 | { |
90291099 | 7533 | struct io_wait_queue iowq; |
75b28aff | 7534 | struct io_rings *rings = ctx->rings; |
68668891 | 7535 | ktime_t timeout = KTIME_MAX; |
c1d5a224 | 7536 | int ret; |
2b188cc1 | 7537 | |
b41e9852 | 7538 | do { |
90f67366 | 7539 | io_cqring_overflow_flush(ctx); |
6c503150 | 7540 | if (io_cqring_events(ctx) >= min_events) |
b41e9852 | 7541 | return 0; |
4c6e277c | 7542 | if (!io_run_task_work()) |
b41e9852 | 7543 | break; |
b41e9852 | 7544 | } while (1); |
2b188cc1 | 7545 | |
44df58d4 XW |
7546 | if (uts) { |
7547 | struct timespec64 ts; | |
7548 | ||
7549 | if (get_timespec64(&ts, uts)) | |
7550 | return -EFAULT; | |
68668891 | 7551 | timeout = ktime_add_ns(timespec64_to_ktime(ts), ktime_get_ns()); |
44df58d4 XW |
7552 | } |
7553 | ||
2b188cc1 | 7554 | if (sig) { |
9e75ad5d AB |
7555 | #ifdef CONFIG_COMPAT |
7556 | if (in_compat_syscall()) | |
7557 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 7558 | sigsz); |
9e75ad5d AB |
7559 | else |
7560 | #endif | |
b772434b | 7561 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 7562 | |
2b188cc1 JA |
7563 | if (ret) |
7564 | return ret; | |
7565 | } | |
7566 | ||
90291099 PB |
7567 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
7568 | iowq.wq.private = current; | |
7569 | INIT_LIST_HEAD(&iowq.wq.entry); | |
7570 | iowq.ctx = ctx; | |
bda52162 | 7571 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
5fd46178 | 7572 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
90291099 | 7573 | |
c826bd7a | 7574 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 7575 | do { |
ca0a2651 | 7576 | /* if we can't even flush overflow, don't wait for more */ |
90f67366 | 7577 | if (!io_cqring_overflow_flush(ctx)) { |
ca0a2651 JA |
7578 | ret = -EBUSY; |
7579 | break; | |
7580 | } | |
311997b3 | 7581 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
bda52162 | 7582 | TASK_INTERRUPTIBLE); |
68668891 | 7583 | ret = io_cqring_wait_schedule(ctx, &iowq, timeout); |
311997b3 | 7584 | finish_wait(&ctx->cq_wait, &iowq.wq); |
ca0a2651 | 7585 | cond_resched(); |
eeb60b9a | 7586 | } while (ret > 0); |
bda52162 | 7587 | |
b7db41c9 | 7588 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7589 | |
75b28aff | 7590 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7591 | } |
7592 | ||
9123c8ff | 7593 | static void io_free_page_table(void **table, size_t size) |
05f3fb3c | 7594 | { |
9123c8ff | 7595 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
05f3fb3c | 7596 | |
846a4ef2 | 7597 | for (i = 0; i < nr_tables; i++) |
9123c8ff PB |
7598 | kfree(table[i]); |
7599 | kfree(table); | |
7600 | } | |
7601 | ||
7602 | static void **io_alloc_page_table(size_t size) | |
7603 | { | |
7604 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); | |
7605 | size_t init_size = size; | |
7606 | void **table; | |
7607 | ||
0bea96f5 | 7608 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7609 | if (!table) |
7610 | return NULL; | |
7611 | ||
7612 | for (i = 0; i < nr_tables; i++) { | |
27f6b318 | 7613 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
9123c8ff | 7614 | |
0bea96f5 | 7615 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7616 | if (!table[i]) { |
7617 | io_free_page_table(table, init_size); | |
7618 | return NULL; | |
7619 | } | |
7620 | size -= this_size; | |
7621 | } | |
7622 | return table; | |
05f3fb3c JA |
7623 | } |
7624 | ||
28a9fe25 | 7625 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
1642b445 | 7626 | { |
28a9fe25 PB |
7627 | percpu_ref_exit(&ref_node->refs); |
7628 | kfree(ref_node); | |
1642b445 PB |
7629 | } |
7630 | ||
b9bd2bea PB |
7631 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
7632 | { | |
7633 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); | |
7634 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; | |
7635 | unsigned long flags; | |
7636 | bool first_add = false; | |
817ab1d1 | 7637 | unsigned long delay = HZ; |
b9bd2bea PB |
7638 | |
7639 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); | |
7640 | node->done = true; | |
7641 | ||
817ab1d1 DY |
7642 | /* if we are mid-quiesce then do not delay */ |
7643 | if (node->rsrc_data->quiesce) | |
7644 | delay = 0; | |
7645 | ||
b9bd2bea PB |
7646 | while (!list_empty(&ctx->rsrc_ref_list)) { |
7647 | node = list_first_entry(&ctx->rsrc_ref_list, | |
7648 | struct io_rsrc_node, node); | |
7649 | /* recycle ref nodes in order */ | |
7650 | if (!node->done) | |
7651 | break; | |
7652 | list_del(&node->node); | |
7653 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); | |
7654 | } | |
7655 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); | |
7656 | ||
7657 | if (first_add) | |
817ab1d1 | 7658 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
b9bd2bea PB |
7659 | } |
7660 | ||
7661 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx) | |
7662 | { | |
7663 | struct io_rsrc_node *ref_node; | |
7664 | ||
7665 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); | |
7666 | if (!ref_node) | |
7667 | return NULL; | |
7668 | ||
7669 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, | |
7670 | 0, GFP_KERNEL)) { | |
7671 | kfree(ref_node); | |
7672 | return NULL; | |
7673 | } | |
7674 | INIT_LIST_HEAD(&ref_node->node); | |
7675 | INIT_LIST_HEAD(&ref_node->rsrc_list); | |
7676 | ref_node->done = false; | |
7677 | return ref_node; | |
7678 | } | |
7679 | ||
a7f0ed5a PB |
7680 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
7681 | struct io_rsrc_data *data_to_kill) | |
6b06314c | 7682 | { |
a7f0ed5a PB |
7683 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
7684 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); | |
6b06314c | 7685 | |
a7f0ed5a PB |
7686 | if (data_to_kill) { |
7687 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; | |
82fbcfa9 | 7688 | |
a7f0ed5a | 7689 | rsrc_node->rsrc_data = data_to_kill; |
4956b9ea | 7690 | spin_lock_irq(&ctx->rsrc_ref_lock); |
a7f0ed5a | 7691 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
4956b9ea | 7692 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
82fbcfa9 | 7693 | |
3e942498 | 7694 | atomic_inc(&data_to_kill->refs); |
a7f0ed5a PB |
7695 | percpu_ref_kill(&rsrc_node->refs); |
7696 | ctx->rsrc_node = NULL; | |
7697 | } | |
6b06314c | 7698 | |
a7f0ed5a PB |
7699 | if (!ctx->rsrc_node) { |
7700 | ctx->rsrc_node = ctx->rsrc_backup_node; | |
7701 | ctx->rsrc_backup_node = NULL; | |
7702 | } | |
8bad28d8 HX |
7703 | } |
7704 | ||
a7f0ed5a | 7705 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
8dd03afe PB |
7706 | { |
7707 | if (ctx->rsrc_backup_node) | |
7708 | return 0; | |
b895c9a6 | 7709 | ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx); |
8dd03afe | 7710 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
8bad28d8 HX |
7711 | } |
7712 | ||
40ae0ff7 | 7713 | static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, struct io_ring_ctx *ctx) |
8bad28d8 HX |
7714 | { |
7715 | int ret; | |
05589553 | 7716 | |
215c3902 | 7717 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
8bad28d8 HX |
7718 | if (data->quiesce) |
7719 | return -ENXIO; | |
05589553 | 7720 | |
8bad28d8 | 7721 | data->quiesce = true; |
1ffc5422 | 7722 | do { |
a7f0ed5a | 7723 | ret = io_rsrc_node_switch_start(ctx); |
8dd03afe | 7724 | if (ret) |
f2303b1f | 7725 | break; |
a7f0ed5a | 7726 | io_rsrc_node_switch(ctx, data); |
f2303b1f | 7727 | |
3e942498 PB |
7728 | /* kill initial ref, already quiesced if zero */ |
7729 | if (atomic_dec_and_test(&data->refs)) | |
7730 | break; | |
c018db4a | 7731 | mutex_unlock(&ctx->uring_lock); |
8bad28d8 | 7732 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 | 7733 | ret = wait_for_completion_interruptible(&data->done); |
c018db4a JA |
7734 | if (!ret) { |
7735 | mutex_lock(&ctx->uring_lock); | |
2bcee8e2 DY |
7736 | if (atomic_read(&data->refs) > 0) { |
7737 | /* | |
7738 | * it has been revived by another thread while | |
7739 | * we were unlocked | |
7740 | */ | |
7741 | mutex_unlock(&ctx->uring_lock); | |
7742 | } else { | |
7743 | break; | |
7744 | } | |
c018db4a | 7745 | } |
8bad28d8 | 7746 | |
3e942498 PB |
7747 | atomic_inc(&data->refs); |
7748 | /* wait for all works potentially completing data->done */ | |
7749 | flush_delayed_work(&ctx->rsrc_put_work); | |
cb5e1b81 | 7750 | reinit_completion(&data->done); |
8dd03afe | 7751 | |
1ffc5422 | 7752 | ret = io_run_task_work_sig(); |
8bad28d8 | 7753 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 7754 | } while (ret >= 0); |
8bad28d8 | 7755 | data->quiesce = false; |
05f3fb3c | 7756 | |
8bad28d8 | 7757 | return ret; |
d7954b2b BM |
7758 | } |
7759 | ||
2d091d62 PB |
7760 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
7761 | { | |
7762 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; | |
7763 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; | |
7764 | ||
7765 | return &data->tags[table_idx][off]; | |
7766 | } | |
7767 | ||
44b31f2f | 7768 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
1ad555c6 | 7769 | { |
2d091d62 PB |
7770 | size_t size = data->nr * sizeof(data->tags[0][0]); |
7771 | ||
7772 | if (data->tags) | |
7773 | io_free_page_table((void **)data->tags, size); | |
44b31f2f PB |
7774 | kfree(data); |
7775 | } | |
7776 | ||
d878c816 PB |
7777 | static int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
7778 | u64 __user *utags, unsigned nr, | |
7779 | struct io_rsrc_data **pdata) | |
1ad555c6 | 7780 | { |
b895c9a6 | 7781 | struct io_rsrc_data *data; |
2d091d62 | 7782 | int ret = -ENOMEM; |
d878c816 | 7783 | unsigned i; |
1ad555c6 BM |
7784 | |
7785 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7786 | if (!data) | |
d878c816 | 7787 | return -ENOMEM; |
2d091d62 | 7788 | data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0])); |
b60c8dce | 7789 | if (!data->tags) { |
1ad555c6 | 7790 | kfree(data); |
d878c816 PB |
7791 | return -ENOMEM; |
7792 | } | |
2d091d62 PB |
7793 | |
7794 | data->nr = nr; | |
7795 | data->ctx = ctx; | |
7796 | data->do_put = do_put; | |
d878c816 | 7797 | if (utags) { |
2d091d62 | 7798 | ret = -EFAULT; |
d878c816 | 7799 | for (i = 0; i < nr; i++) { |
fdd1dc31 CIK |
7800 | u64 *tag_slot = io_get_tag_slot(data, i); |
7801 | ||
7802 | if (copy_from_user(tag_slot, &utags[i], | |
7803 | sizeof(*tag_slot))) | |
2d091d62 | 7804 | goto fail; |
d878c816 | 7805 | } |
1ad555c6 | 7806 | } |
b60c8dce | 7807 | |
3e942498 | 7808 | atomic_set(&data->refs, 1); |
1ad555c6 | 7809 | init_completion(&data->done); |
d878c816 PB |
7810 | *pdata = data; |
7811 | return 0; | |
2d091d62 PB |
7812 | fail: |
7813 | io_rsrc_data_free(data); | |
7814 | return ret; | |
1ad555c6 BM |
7815 | } |
7816 | ||
9123c8ff PB |
7817 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
7818 | { | |
0bea96f5 PB |
7819 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
7820 | GFP_KERNEL_ACCOUNT); | |
9123c8ff PB |
7821 | return !!table->files; |
7822 | } | |
7823 | ||
042b0d85 | 7824 | static void io_free_file_tables(struct io_file_table *table) |
9123c8ff | 7825 | { |
042b0d85 | 7826 | kvfree(table->files); |
9123c8ff PB |
7827 | table->files = NULL; |
7828 | } | |
7829 | ||
fff4db76 | 7830 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
1ad555c6 | 7831 | { |
fff4db76 PB |
7832 | #if defined(CONFIG_UNIX) |
7833 | if (ctx->ring_sock) { | |
7834 | struct sock *sock = ctx->ring_sock->sk; | |
7835 | struct sk_buff *skb; | |
7836 | ||
7837 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
7838 | kfree_skb(skb); | |
7839 | } | |
7840 | #else | |
7841 | int i; | |
7842 | ||
7843 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7844 | struct file *file; | |
7845 | ||
7846 | file = io_file_from_index(ctx, i); | |
7847 | if (file) | |
7848 | fput(file); | |
7849 | } | |
7850 | #endif | |
042b0d85 | 7851 | io_free_file_tables(&ctx->file_table); |
44b31f2f | 7852 | io_rsrc_data_free(ctx->file_data); |
fff4db76 PB |
7853 | ctx->file_data = NULL; |
7854 | ctx->nr_user_files = 0; | |
1ad555c6 BM |
7855 | } |
7856 | ||
d7954b2b BM |
7857 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7858 | { | |
75d51314 | 7859 | unsigned nr = ctx->nr_user_files; |
d7954b2b BM |
7860 | int ret; |
7861 | ||
08480400 | 7862 | if (!ctx->file_data) |
d7954b2b | 7863 | return -ENXIO; |
75d51314 PB |
7864 | |
7865 | /* | |
7866 | * Quiesce may unlock ->uring_lock, and while it's not held | |
7867 | * prevent new requests using the table. | |
7868 | */ | |
7869 | ctx->nr_user_files = 0; | |
08480400 | 7870 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
75d51314 | 7871 | ctx->nr_user_files = nr; |
08480400 PB |
7872 | if (!ret) |
7873 | __io_sqe_files_unregister(ctx); | |
7874 | return ret; | |
6b06314c JA |
7875 | } |
7876 | ||
37d1e2e3 | 7877 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
09a6f4ef | 7878 | __releases(&sqd->lock) |
37d1e2e3 | 7879 | { |
521d6a73 PB |
7880 | WARN_ON_ONCE(sqd->thread == current); |
7881 | ||
9e138a48 PB |
7882 | /* |
7883 | * Do the dance but not conditional clear_bit() because it'd race with | |
7884 | * other threads incrementing park_pending and setting the bit. | |
7885 | */ | |
37d1e2e3 | 7886 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
9e138a48 PB |
7887 | if (atomic_dec_return(&sqd->park_pending)) |
7888 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
09a6f4ef | 7889 | mutex_unlock(&sqd->lock); |
37d1e2e3 JA |
7890 | } |
7891 | ||
86e0d676 | 7892 | static void io_sq_thread_park(struct io_sq_data *sqd) |
09a6f4ef | 7893 | __acquires(&sqd->lock) |
37d1e2e3 | 7894 | { |
521d6a73 PB |
7895 | WARN_ON_ONCE(sqd->thread == current); |
7896 | ||
9e138a48 | 7897 | atomic_inc(&sqd->park_pending); |
86e0d676 | 7898 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
09a6f4ef | 7899 | mutex_lock(&sqd->lock); |
05962f95 | 7900 | if (sqd->thread) |
86e0d676 | 7901 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
7902 | } |
7903 | ||
7904 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
7905 | { | |
521d6a73 | 7906 | WARN_ON_ONCE(sqd->thread == current); |
88885f66 | 7907 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
521d6a73 | 7908 | |
05962f95 | 7909 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
88885f66 | 7910 | mutex_lock(&sqd->lock); |
e8f98f24 JA |
7911 | if (sqd->thread) |
7912 | wake_up_process(sqd->thread); | |
09a6f4ef | 7913 | mutex_unlock(&sqd->lock); |
05962f95 | 7914 | wait_for_completion(&sqd->exited); |
37d1e2e3 JA |
7915 | } |
7916 | ||
534ca6d6 | 7917 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7918 | { |
534ca6d6 | 7919 | if (refcount_dec_and_test(&sqd->refs)) { |
9e138a48 PB |
7920 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
7921 | ||
37d1e2e3 JA |
7922 | io_sq_thread_stop(sqd); |
7923 | kfree(sqd); | |
7924 | } | |
7925 | } | |
7926 | ||
7927 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
7928 | { | |
7929 | struct io_sq_data *sqd = ctx->sq_data; | |
7930 | ||
7931 | if (sqd) { | |
05962f95 | 7932 | io_sq_thread_park(sqd); |
521d6a73 | 7933 | list_del_init(&ctx->sqd_list); |
37d1e2e3 | 7934 | io_sqd_update_thread_idle(sqd); |
05962f95 | 7935 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
7936 | |
7937 | io_put_sq_data(sqd); | |
7938 | ctx->sq_data = NULL; | |
534ca6d6 JA |
7939 | } |
7940 | } | |
7941 | ||
aa06165d JA |
7942 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7943 | { | |
7944 | struct io_ring_ctx *ctx_attach; | |
7945 | struct io_sq_data *sqd; | |
7946 | struct fd f; | |
7947 | ||
7948 | f = fdget(p->wq_fd); | |
7949 | if (!f.file) | |
7950 | return ERR_PTR(-ENXIO); | |
7951 | if (f.file->f_op != &io_uring_fops) { | |
7952 | fdput(f); | |
7953 | return ERR_PTR(-EINVAL); | |
7954 | } | |
7955 | ||
7956 | ctx_attach = f.file->private_data; | |
7957 | sqd = ctx_attach->sq_data; | |
7958 | if (!sqd) { | |
7959 | fdput(f); | |
7960 | return ERR_PTR(-EINVAL); | |
7961 | } | |
5c2469e0 JA |
7962 | if (sqd->task_tgid != current->tgid) { |
7963 | fdput(f); | |
7964 | return ERR_PTR(-EPERM); | |
7965 | } | |
aa06165d JA |
7966 | |
7967 | refcount_inc(&sqd->refs); | |
7968 | fdput(f); | |
7969 | return sqd; | |
7970 | } | |
7971 | ||
26984fbf PB |
7972 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
7973 | bool *attached) | |
534ca6d6 JA |
7974 | { |
7975 | struct io_sq_data *sqd; | |
7976 | ||
26984fbf | 7977 | *attached = false; |
5c2469e0 JA |
7978 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
7979 | sqd = io_attach_sq_data(p); | |
26984fbf PB |
7980 | if (!IS_ERR(sqd)) { |
7981 | *attached = true; | |
5c2469e0 | 7982 | return sqd; |
26984fbf | 7983 | } |
5c2469e0 JA |
7984 | /* fall through for EPERM case, setup new sqd/task */ |
7985 | if (PTR_ERR(sqd) != -EPERM) | |
7986 | return sqd; | |
7987 | } | |
aa06165d | 7988 | |
534ca6d6 JA |
7989 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7990 | if (!sqd) | |
7991 | return ERR_PTR(-ENOMEM); | |
7992 | ||
9e138a48 | 7993 | atomic_set(&sqd->park_pending, 0); |
534ca6d6 | 7994 | refcount_set(&sqd->refs, 1); |
69fb2131 | 7995 | INIT_LIST_HEAD(&sqd->ctx_list); |
09a6f4ef | 7996 | mutex_init(&sqd->lock); |
534ca6d6 | 7997 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 7998 | init_completion(&sqd->exited); |
534ca6d6 JA |
7999 | return sqd; |
8000 | } | |
8001 | ||
6b06314c | 8002 | #if defined(CONFIG_UNIX) |
6b06314c JA |
8003 | /* |
8004 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
8005 | * the io_uring can be safely unregistered on process exit, even if we have | |
8006 | * loops in the file referencing. | |
8007 | */ | |
8008 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
8009 | { | |
8010 | struct sock *sk = ctx->ring_sock->sk; | |
8011 | struct scm_fp_list *fpl; | |
8012 | struct sk_buff *skb; | |
08a45173 | 8013 | int i, nr_files; |
6b06314c | 8014 | |
6b06314c JA |
8015 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
8016 | if (!fpl) | |
8017 | return -ENOMEM; | |
8018 | ||
8019 | skb = alloc_skb(0, GFP_KERNEL); | |
8020 | if (!skb) { | |
8021 | kfree(fpl); | |
8022 | return -ENOMEM; | |
8023 | } | |
8024 | ||
8025 | skb->sk = sk; | |
6b06314c | 8026 | |
08a45173 | 8027 | nr_files = 0; |
62e398be | 8028 | fpl->user = get_uid(current_user()); |
6b06314c | 8029 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
8030 | struct file *file = io_file_from_index(ctx, i + offset); |
8031 | ||
8032 | if (!file) | |
08a45173 | 8033 | continue; |
65e19f54 | 8034 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
8035 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
8036 | nr_files++; | |
6b06314c JA |
8037 | } |
8038 | ||
08a45173 JA |
8039 | if (nr_files) { |
8040 | fpl->max = SCM_MAX_FD; | |
8041 | fpl->count = nr_files; | |
8042 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 8043 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
8044 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
8045 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 8046 | |
40d8cb26 PB |
8047 | for (i = 0; i < nr; i++) { |
8048 | struct file *file = io_file_from_index(ctx, i + offset); | |
8049 | ||
8050 | if (file) | |
8051 | fput(file); | |
8052 | } | |
08a45173 JA |
8053 | } else { |
8054 | kfree_skb(skb); | |
54c4d35a | 8055 | free_uid(fpl->user); |
08a45173 JA |
8056 | kfree(fpl); |
8057 | } | |
6b06314c JA |
8058 | |
8059 | return 0; | |
8060 | } | |
8061 | ||
8062 | /* | |
8063 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
8064 | * causes regular reference counting to break down. We rely on the UNIX | |
8065 | * garbage collection to take care of this problem for us. | |
8066 | */ | |
8067 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8068 | { | |
8069 | unsigned left, total; | |
8070 | int ret = 0; | |
8071 | ||
8072 | total = 0; | |
8073 | left = ctx->nr_user_files; | |
8074 | while (left) { | |
8075 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
8076 | |
8077 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
8078 | if (ret) | |
8079 | break; | |
8080 | left -= this_files; | |
8081 | total += this_files; | |
8082 | } | |
8083 | ||
8084 | if (!ret) | |
8085 | return 0; | |
8086 | ||
8087 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
8088 | struct file *file = io_file_from_index(ctx, total); |
8089 | ||
8090 | if (file) | |
8091 | fput(file); | |
6b06314c JA |
8092 | total++; |
8093 | } | |
8094 | ||
8095 | return ret; | |
8096 | } | |
8097 | #else | |
8098 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8099 | { | |
8100 | return 0; | |
8101 | } | |
8102 | #endif | |
8103 | ||
47e90392 | 8104 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 8105 | { |
50238531 | 8106 | struct file *file = prsrc->file; |
05f3fb3c JA |
8107 | #if defined(CONFIG_UNIX) |
8108 | struct sock *sock = ctx->ring_sock->sk; | |
8109 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
8110 | struct sk_buff *skb; | |
8111 | int i; | |
8112 | ||
8113 | __skb_queue_head_init(&list); | |
8114 | ||
8115 | /* | |
8116 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
8117 | * remove this entry and rearrange the file array. | |
8118 | */ | |
8119 | skb = skb_dequeue(head); | |
8120 | while (skb) { | |
8121 | struct scm_fp_list *fp; | |
8122 | ||
8123 | fp = UNIXCB(skb).fp; | |
8124 | for (i = 0; i < fp->count; i++) { | |
8125 | int left; | |
8126 | ||
8127 | if (fp->fp[i] != file) | |
8128 | continue; | |
8129 | ||
8130 | unix_notinflight(fp->user, fp->fp[i]); | |
8131 | left = fp->count - 1 - i; | |
8132 | if (left) { | |
8133 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
8134 | left * sizeof(struct file *)); | |
8135 | } | |
8136 | fp->count--; | |
8137 | if (!fp->count) { | |
8138 | kfree_skb(skb); | |
8139 | skb = NULL; | |
8140 | } else { | |
8141 | __skb_queue_tail(&list, skb); | |
8142 | } | |
8143 | fput(file); | |
8144 | file = NULL; | |
8145 | break; | |
8146 | } | |
8147 | ||
8148 | if (!file) | |
8149 | break; | |
8150 | ||
8151 | __skb_queue_tail(&list, skb); | |
8152 | ||
8153 | skb = skb_dequeue(head); | |
8154 | } | |
8155 | ||
8156 | if (skb_peek(&list)) { | |
8157 | spin_lock_irq(&head->lock); | |
8158 | while ((skb = __skb_dequeue(&list)) != NULL) | |
8159 | __skb_queue_tail(head, skb); | |
8160 | spin_unlock_irq(&head->lock); | |
8161 | } | |
8162 | #else | |
8163 | fput(file); | |
8164 | #endif | |
8165 | } | |
8166 | ||
b895c9a6 | 8167 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
65e19f54 | 8168 | { |
b895c9a6 | 8169 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
269bbe5f BM |
8170 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
8171 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 8172 | |
269bbe5f BM |
8173 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
8174 | list_del(&prsrc->list); | |
b60c8dce PB |
8175 | |
8176 | if (prsrc->tag) { | |
8177 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; | |
b60c8dce PB |
8178 | |
8179 | io_ring_submit_lock(ctx, lock_ring); | |
79ebeaee | 8180 | spin_lock(&ctx->completion_lock); |
142f794e | 8181 | io_fill_cqe_aux(ctx, prsrc->tag, 0, 0); |
b60c8dce | 8182 | io_commit_cqring(ctx); |
79ebeaee | 8183 | spin_unlock(&ctx->completion_lock); |
b60c8dce PB |
8184 | io_cqring_ev_posted(ctx); |
8185 | io_ring_submit_unlock(ctx, lock_ring); | |
8186 | } | |
8187 | ||
40ae0ff7 | 8188 | rsrc_data->do_put(ctx, prsrc); |
269bbe5f | 8189 | kfree(prsrc); |
65e19f54 | 8190 | } |
05589553 | 8191 | |
28a9fe25 | 8192 | io_rsrc_node_destroy(ref_node); |
3e942498 PB |
8193 | if (atomic_dec_and_test(&rsrc_data->refs)) |
8194 | complete(&rsrc_data->done); | |
2faf852d | 8195 | } |
65e19f54 | 8196 | |
269bbe5f | 8197 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
8198 | { |
8199 | struct io_ring_ctx *ctx; | |
8200 | struct llist_node *node; | |
8201 | ||
269bbe5f BM |
8202 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
8203 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
8204 | |
8205 | while (node) { | |
b895c9a6 | 8206 | struct io_rsrc_node *ref_node; |
4a38aed2 JA |
8207 | struct llist_node *next = node->next; |
8208 | ||
b895c9a6 | 8209 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
269bbe5f | 8210 | __io_rsrc_put_work(ref_node); |
4a38aed2 JA |
8211 | node = next; |
8212 | } | |
8213 | } | |
8214 | ||
6b06314c | 8215 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
792e3582 | 8216 | unsigned nr_args, u64 __user *tags) |
6b06314c JA |
8217 | { |
8218 | __s32 __user *fds = (__s32 __user *) arg; | |
05f3fb3c | 8219 | struct file *file; |
f3baed39 | 8220 | int fd, ret; |
846a4ef2 | 8221 | unsigned i; |
6b06314c | 8222 | |
05f3fb3c | 8223 | if (ctx->file_data) |
6b06314c JA |
8224 | return -EBUSY; |
8225 | if (!nr_args) | |
8226 | return -EINVAL; | |
8227 | if (nr_args > IORING_MAX_FIXED_FILES) | |
8228 | return -EMFILE; | |
3a1b8a4e PB |
8229 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
8230 | return -EMFILE; | |
a7f0ed5a | 8231 | ret = io_rsrc_node_switch_start(ctx); |
f3baed39 PB |
8232 | if (ret) |
8233 | return ret; | |
d878c816 PB |
8234 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
8235 | &ctx->file_data); | |
8236 | if (ret) | |
8237 | return ret; | |
6b06314c | 8238 | |
f3baed39 | 8239 | ret = -ENOMEM; |
aeca241b | 8240 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
1ad555c6 | 8241 | goto out_free; |
65e19f54 | 8242 | |
08a45173 | 8243 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
d878c816 | 8244 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
600cf3f8 PB |
8245 | ret = -EFAULT; |
8246 | goto out_fput; | |
8247 | } | |
08a45173 | 8248 | /* allow sparse sets */ |
792e3582 PB |
8249 | if (fd == -1) { |
8250 | ret = -EINVAL; | |
2d091d62 | 8251 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
792e3582 | 8252 | goto out_fput; |
08a45173 | 8253 | continue; |
792e3582 | 8254 | } |
6b06314c | 8255 | |
05f3fb3c | 8256 | file = fget(fd); |
6b06314c | 8257 | ret = -EBADF; |
792e3582 | 8258 | if (unlikely(!file)) |
600cf3f8 | 8259 | goto out_fput; |
05f3fb3c | 8260 | |
6b06314c JA |
8261 | /* |
8262 | * Don't allow io_uring instances to be registered. If UNIX | |
8263 | * isn't enabled, then this causes a reference cycle and this | |
8264 | * instance can never get freed. If UNIX is enabled we'll | |
8265 | * handle it just fine, but there's still no point in allowing | |
8266 | * a ring fd as it doesn't support regular read/write anyway. | |
8267 | */ | |
05f3fb3c JA |
8268 | if (file->f_op == &io_uring_fops) { |
8269 | fput(file); | |
600cf3f8 | 8270 | goto out_fput; |
6b06314c | 8271 | } |
aeca241b | 8272 | io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file); |
6b06314c JA |
8273 | } |
8274 | ||
6b06314c | 8275 | ret = io_sqe_files_scm(ctx); |
05589553 | 8276 | if (ret) { |
08480400 | 8277 | __io_sqe_files_unregister(ctx); |
05589553 XW |
8278 | return ret; |
8279 | } | |
6b06314c | 8280 | |
a7f0ed5a | 8281 | io_rsrc_node_switch(ctx, NULL); |
6b06314c | 8282 | return ret; |
600cf3f8 PB |
8283 | out_fput: |
8284 | for (i = 0; i < ctx->nr_user_files; i++) { | |
8285 | file = io_file_from_index(ctx, i); | |
8286 | if (file) | |
8287 | fput(file); | |
8288 | } | |
042b0d85 | 8289 | io_free_file_tables(&ctx->file_table); |
600cf3f8 | 8290 | ctx->nr_user_files = 0; |
600cf3f8 | 8291 | out_free: |
44b31f2f | 8292 | io_rsrc_data_free(ctx->file_data); |
55cbc256 | 8293 | ctx->file_data = NULL; |
6b06314c JA |
8294 | return ret; |
8295 | } | |
8296 | ||
c3a31e60 JA |
8297 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
8298 | int index) | |
8299 | { | |
8300 | #if defined(CONFIG_UNIX) | |
8301 | struct sock *sock = ctx->ring_sock->sk; | |
8302 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
8303 | struct sk_buff *skb; | |
8304 | ||
8305 | /* | |
8306 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
8307 | * file set. If there's no room, fall back to allocating a new skb | |
8308 | * and filling it in. | |
8309 | */ | |
8310 | spin_lock_irq(&head->lock); | |
8311 | skb = skb_peek(head); | |
8312 | if (skb) { | |
8313 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
8314 | ||
8315 | if (fpl->count < SCM_MAX_FD) { | |
8316 | __skb_unlink(skb, head); | |
8317 | spin_unlock_irq(&head->lock); | |
8318 | fpl->fp[fpl->count] = get_file(file); | |
8319 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
8320 | fpl->count++; | |
8321 | spin_lock_irq(&head->lock); | |
8322 | __skb_queue_head(head, skb); | |
8323 | } else { | |
8324 | skb = NULL; | |
8325 | } | |
8326 | } | |
8327 | spin_unlock_irq(&head->lock); | |
8328 | ||
8329 | if (skb) { | |
8330 | fput(file); | |
8331 | return 0; | |
8332 | } | |
8333 | ||
8334 | return __io_sqe_files_scm(ctx, 1, index); | |
8335 | #else | |
8336 | return 0; | |
8337 | #endif | |
8338 | } | |
8339 | ||
9c7b0ba8 PB |
8340 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
8341 | struct io_rsrc_node *node, void *rsrc) | |
8342 | { | |
c47dfb44 | 8343 | u64 *tag_slot = io_get_tag_slot(data, idx); |
9c7b0ba8 PB |
8344 | struct io_rsrc_put *prsrc; |
8345 | ||
8346 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); | |
8347 | if (!prsrc) | |
8348 | return -ENOMEM; | |
8349 | ||
c47dfb44 PB |
8350 | prsrc->tag = *tag_slot; |
8351 | *tag_slot = 0; | |
9c7b0ba8 PB |
8352 | prsrc->rsrc = rsrc; |
8353 | list_add(&prsrc->list, &node->rsrc_list); | |
8354 | return 0; | |
8355 | } | |
8356 | ||
b9445598 PB |
8357 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
8358 | unsigned int issue_flags, u32 slot_index) | |
8359 | { | |
8360 | struct io_ring_ctx *ctx = req->ctx; | |
8361 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; | |
9c7b0ba8 | 8362 | bool needs_switch = false; |
b9445598 PB |
8363 | struct io_fixed_file *file_slot; |
8364 | int ret = -EBADF; | |
8365 | ||
8366 | io_ring_submit_lock(ctx, !force_nonblock); | |
8367 | if (file->f_op == &io_uring_fops) | |
8368 | goto err; | |
8369 | ret = -ENXIO; | |
8370 | if (!ctx->file_data) | |
8371 | goto err; | |
8372 | ret = -EINVAL; | |
8373 | if (slot_index >= ctx->nr_user_files) | |
8374 | goto err; | |
8375 | ||
8376 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); | |
8377 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); | |
9c7b0ba8 PB |
8378 | |
8379 | if (file_slot->file_ptr) { | |
8380 | struct file *old_file; | |
8381 | ||
8382 | ret = io_rsrc_node_switch_start(ctx); | |
8383 | if (ret) | |
8384 | goto err; | |
8385 | ||
8386 | old_file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8387 | ret = io_queue_rsrc_removal(ctx->file_data, slot_index, | |
8388 | ctx->rsrc_node, old_file); | |
8389 | if (ret) | |
8390 | goto err; | |
8391 | file_slot->file_ptr = 0; | |
8392 | needs_switch = true; | |
8393 | } | |
b9445598 PB |
8394 | |
8395 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; | |
8396 | io_fixed_file_set(file_slot, file); | |
8397 | ret = io_sqe_file_register(ctx, file, slot_index); | |
8398 | if (ret) { | |
8399 | file_slot->file_ptr = 0; | |
8400 | goto err; | |
8401 | } | |
8402 | ||
8403 | ret = 0; | |
8404 | err: | |
9c7b0ba8 PB |
8405 | if (needs_switch) |
8406 | io_rsrc_node_switch(ctx, ctx->file_data); | |
b9445598 PB |
8407 | io_ring_submit_unlock(ctx, !force_nonblock); |
8408 | if (ret) | |
8409 | fput(file); | |
8410 | return ret; | |
8411 | } | |
8412 | ||
7df778be PB |
8413 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) |
8414 | { | |
8415 | unsigned int offset = req->close.file_slot - 1; | |
8416 | struct io_ring_ctx *ctx = req->ctx; | |
8417 | struct io_fixed_file *file_slot; | |
8418 | struct file *file; | |
22a99dcb | 8419 | int ret; |
7df778be PB |
8420 | |
8421 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
8422 | ret = -ENXIO; | |
8423 | if (unlikely(!ctx->file_data)) | |
8424 | goto out; | |
8425 | ret = -EINVAL; | |
8426 | if (offset >= ctx->nr_user_files) | |
8427 | goto out; | |
8428 | ret = io_rsrc_node_switch_start(ctx); | |
8429 | if (ret) | |
8430 | goto out; | |
8431 | ||
22a99dcb PB |
8432 | offset = array_index_nospec(offset, ctx->nr_user_files); |
8433 | file_slot = io_fixed_file_slot(&ctx->file_table, offset); | |
7df778be PB |
8434 | ret = -EBADF; |
8435 | if (!file_slot->file_ptr) | |
8436 | goto out; | |
8437 | ||
8438 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8439 | ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file); | |
8440 | if (ret) | |
8441 | goto out; | |
8442 | ||
8443 | file_slot->file_ptr = 0; | |
8444 | io_rsrc_node_switch(ctx, ctx->file_data); | |
8445 | ret = 0; | |
8446 | out: | |
8447 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
8448 | return ret; | |
8449 | } | |
8450 | ||
05f3fb3c | 8451 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
c3bdad02 | 8452 | struct io_uring_rsrc_update2 *up, |
05f3fb3c JA |
8453 | unsigned nr_args) |
8454 | { | |
c3bdad02 | 8455 | u64 __user *tags = u64_to_user_ptr(up->tags); |
98f0b3b4 | 8456 | __s32 __user *fds = u64_to_user_ptr(up->data); |
b895c9a6 | 8457 | struct io_rsrc_data *data = ctx->file_data; |
a04b0ac0 PB |
8458 | struct io_fixed_file *file_slot; |
8459 | struct file *file; | |
98f0b3b4 PB |
8460 | int fd, i, err = 0; |
8461 | unsigned int done; | |
05589553 | 8462 | bool needs_switch = false; |
c3a31e60 | 8463 | |
98f0b3b4 PB |
8464 | if (!ctx->file_data) |
8465 | return -ENXIO; | |
8466 | if (up->offset + nr_args > ctx->nr_user_files) | |
c3a31e60 JA |
8467 | return -EINVAL; |
8468 | ||
67973b93 | 8469 | for (done = 0; done < nr_args; done++) { |
c3bdad02 PB |
8470 | u64 tag = 0; |
8471 | ||
8472 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || | |
8473 | copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
c3a31e60 JA |
8474 | err = -EFAULT; |
8475 | break; | |
8476 | } | |
c3bdad02 PB |
8477 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
8478 | err = -EINVAL; | |
8479 | break; | |
8480 | } | |
4e0377a1 | 8481 | if (fd == IORING_REGISTER_FILES_SKIP) |
8482 | continue; | |
8483 | ||
67973b93 | 8484 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
aeca241b | 8485 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
ea64ec02 | 8486 | |
a04b0ac0 PB |
8487 | if (file_slot->file_ptr) { |
8488 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
22a99dcb | 8489 | err = io_queue_rsrc_removal(data, i, ctx->rsrc_node, file); |
a5318d3c HD |
8490 | if (err) |
8491 | break; | |
a04b0ac0 | 8492 | file_slot->file_ptr = 0; |
05589553 | 8493 | needs_switch = true; |
c3a31e60 JA |
8494 | } |
8495 | if (fd != -1) { | |
c3a31e60 JA |
8496 | file = fget(fd); |
8497 | if (!file) { | |
8498 | err = -EBADF; | |
8499 | break; | |
8500 | } | |
8501 | /* | |
8502 | * Don't allow io_uring instances to be registered. If | |
8503 | * UNIX isn't enabled, then this causes a reference | |
8504 | * cycle and this instance can never get freed. If UNIX | |
8505 | * is enabled we'll handle it just fine, but there's | |
8506 | * still no point in allowing a ring fd as it doesn't | |
8507 | * support regular read/write anyway. | |
8508 | */ | |
8509 | if (file->f_op == &io_uring_fops) { | |
8510 | fput(file); | |
8511 | err = -EBADF; | |
8512 | break; | |
8513 | } | |
1dc25797 | 8514 | *io_get_tag_slot(data, i) = tag; |
9a321c98 | 8515 | io_fixed_file_set(file_slot, file); |
c3a31e60 | 8516 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 8517 | if (err) { |
a04b0ac0 | 8518 | file_slot->file_ptr = 0; |
f3bd9dae | 8519 | fput(file); |
c3a31e60 | 8520 | break; |
f3bd9dae | 8521 | } |
c3a31e60 | 8522 | } |
05f3fb3c JA |
8523 | } |
8524 | ||
a7f0ed5a PB |
8525 | if (needs_switch) |
8526 | io_rsrc_node_switch(ctx, data); | |
c3a31e60 JA |
8527 | return done ? done : err; |
8528 | } | |
05589553 | 8529 | |
685fe7fe JA |
8530 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
8531 | struct task_struct *task) | |
24369c2e | 8532 | { |
e941894e | 8533 | struct io_wq_hash *hash; |
24369c2e | 8534 | struct io_wq_data data; |
24369c2e | 8535 | unsigned int concurrency; |
24369c2e | 8536 | |
362a9e65 | 8537 | mutex_lock(&ctx->uring_lock); |
e941894e JA |
8538 | hash = ctx->hash_map; |
8539 | if (!hash) { | |
8540 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
362a9e65 YY |
8541 | if (!hash) { |
8542 | mutex_unlock(&ctx->uring_lock); | |
e941894e | 8543 | return ERR_PTR(-ENOMEM); |
362a9e65 | 8544 | } |
e941894e JA |
8545 | refcount_set(&hash->refs, 1); |
8546 | init_waitqueue_head(&hash->wait); | |
8547 | ctx->hash_map = hash; | |
24369c2e | 8548 | } |
362a9e65 | 8549 | mutex_unlock(&ctx->uring_lock); |
24369c2e | 8550 | |
e941894e | 8551 | data.hash = hash; |
685fe7fe | 8552 | data.task = task; |
ebc11b6c | 8553 | data.free_work = io_wq_free_work; |
f5fa38c5 | 8554 | data.do_work = io_wq_submit_work; |
24369c2e | 8555 | |
d25e3a3d JA |
8556 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
8557 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 8558 | |
5aa75ed5 | 8559 | return io_wq_create(concurrency, &data); |
24369c2e PB |
8560 | } |
8561 | ||
5aa75ed5 JA |
8562 | static int io_uring_alloc_task_context(struct task_struct *task, |
8563 | struct io_ring_ctx *ctx) | |
0f212204 JA |
8564 | { |
8565 | struct io_uring_task *tctx; | |
d8a6df10 | 8566 | int ret; |
0f212204 | 8567 | |
09899b19 | 8568 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
0f212204 JA |
8569 | if (unlikely(!tctx)) |
8570 | return -ENOMEM; | |
8571 | ||
d8a6df10 JA |
8572 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
8573 | if (unlikely(ret)) { | |
8574 | kfree(tctx); | |
8575 | return ret; | |
8576 | } | |
8577 | ||
685fe7fe | 8578 | tctx->io_wq = io_init_wq_offload(ctx, task); |
5aa75ed5 JA |
8579 | if (IS_ERR(tctx->io_wq)) { |
8580 | ret = PTR_ERR(tctx->io_wq); | |
8581 | percpu_counter_destroy(&tctx->inflight); | |
8582 | kfree(tctx); | |
8583 | return ret; | |
8584 | } | |
8585 | ||
0f212204 JA |
8586 | xa_init(&tctx->xa); |
8587 | init_waitqueue_head(&tctx->wait); | |
fdaf083c | 8588 | atomic_set(&tctx->in_idle, 0); |
b303fe2e | 8589 | atomic_set(&tctx->inflight_tracked, 0); |
0f212204 | 8590 | task->io_uring = tctx; |
7cbf1722 JA |
8591 | spin_lock_init(&tctx->task_lock); |
8592 | INIT_WQ_LIST(&tctx->task_list); | |
7cbf1722 | 8593 | init_task_work(&tctx->task_work, tctx_task_work); |
0f212204 JA |
8594 | return 0; |
8595 | } | |
8596 | ||
8597 | void __io_uring_free(struct task_struct *tsk) | |
8598 | { | |
8599 | struct io_uring_task *tctx = tsk->io_uring; | |
8600 | ||
8601 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e | 8602 | WARN_ON_ONCE(tctx->io_wq); |
09899b19 | 8603 | WARN_ON_ONCE(tctx->cached_refs); |
ef8eaa4e | 8604 | |
d8a6df10 | 8605 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
8606 | kfree(tctx); |
8607 | tsk->io_uring = NULL; | |
8608 | } | |
8609 | ||
7e84e1c7 SG |
8610 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
8611 | struct io_uring_params *p) | |
2b188cc1 JA |
8612 | { |
8613 | int ret; | |
8614 | ||
d25e3a3d JA |
8615 | /* Retain compatibility with failing for an invalid attach attempt */ |
8616 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
8617 | IORING_SETUP_ATTACH_WQ) { | |
8618 | struct fd f; | |
8619 | ||
8620 | f = fdget(p->wq_fd); | |
8621 | if (!f.file) | |
8622 | return -ENXIO; | |
0cc936f7 JA |
8623 | if (f.file->f_op != &io_uring_fops) { |
8624 | fdput(f); | |
f2a48dd0 | 8625 | return -EINVAL; |
0cc936f7 JA |
8626 | } |
8627 | fdput(f); | |
d25e3a3d | 8628 | } |
6c271ce2 | 8629 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 8630 | struct task_struct *tsk; |
534ca6d6 | 8631 | struct io_sq_data *sqd; |
26984fbf | 8632 | bool attached; |
534ca6d6 | 8633 | |
26984fbf | 8634 | sqd = io_get_sq_data(p, &attached); |
534ca6d6 JA |
8635 | if (IS_ERR(sqd)) { |
8636 | ret = PTR_ERR(sqd); | |
8637 | goto err; | |
8638 | } | |
69fb2131 | 8639 | |
7c30f36a | 8640 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 8641 | ctx->sq_data = sqd; |
917257da JA |
8642 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
8643 | if (!ctx->sq_thread_idle) | |
8644 | ctx->sq_thread_idle = HZ; | |
8645 | ||
78d7f6ba | 8646 | io_sq_thread_park(sqd); |
de75a3d3 PB |
8647 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
8648 | io_sqd_update_thread_idle(sqd); | |
26984fbf | 8649 | /* don't attach to a dying SQPOLL thread, would be racy */ |
f2a48dd0 | 8650 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
78d7f6ba PB |
8651 | io_sq_thread_unpark(sqd); |
8652 | ||
de75a3d3 PB |
8653 | if (ret < 0) |
8654 | goto err; | |
8655 | if (attached) | |
5aa75ed5 | 8656 | return 0; |
aa06165d | 8657 | |
6c271ce2 | 8658 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 8659 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 8660 | |
917257da | 8661 | ret = -EINVAL; |
f2a48dd0 | 8662 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
e8f98f24 | 8663 | goto err_sqpoll; |
37d1e2e3 | 8664 | sqd->sq_cpu = cpu; |
6c271ce2 | 8665 | } else { |
37d1e2e3 | 8666 | sqd->sq_cpu = -1; |
6c271ce2 | 8667 | } |
37d1e2e3 JA |
8668 | |
8669 | sqd->task_pid = current->pid; | |
5c2469e0 | 8670 | sqd->task_tgid = current->tgid; |
46fe18b1 JA |
8671 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
8672 | if (IS_ERR(tsk)) { | |
8673 | ret = PTR_ERR(tsk); | |
e8f98f24 | 8674 | goto err_sqpoll; |
6c271ce2 | 8675 | } |
97a73a0f | 8676 | |
46fe18b1 | 8677 | sqd->thread = tsk; |
97a73a0f | 8678 | ret = io_uring_alloc_task_context(tsk, ctx); |
46fe18b1 | 8679 | wake_up_new_task(tsk); |
0f212204 JA |
8680 | if (ret) |
8681 | goto err; | |
6c271ce2 JA |
8682 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
8683 | /* Can't have SQ_AFF without SQPOLL */ | |
8684 | ret = -EINVAL; | |
8685 | goto err; | |
8686 | } | |
8687 | ||
2b188cc1 | 8688 | return 0; |
f2a48dd0 PB |
8689 | err_sqpoll: |
8690 | complete(&ctx->sq_data->exited); | |
2b188cc1 | 8691 | err: |
37d1e2e3 | 8692 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
8693 | return ret; |
8694 | } | |
8695 | ||
a087e2b5 BM |
8696 | static inline void __io_unaccount_mem(struct user_struct *user, |
8697 | unsigned long nr_pages) | |
2b188cc1 JA |
8698 | { |
8699 | atomic_long_sub(nr_pages, &user->locked_vm); | |
8700 | } | |
8701 | ||
a087e2b5 BM |
8702 | static inline int __io_account_mem(struct user_struct *user, |
8703 | unsigned long nr_pages) | |
2b188cc1 JA |
8704 | { |
8705 | unsigned long page_limit, cur_pages, new_pages; | |
8706 | ||
8707 | /* Don't allow more pages than we can safely lock */ | |
8708 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
8709 | ||
8710 | do { | |
8711 | cur_pages = atomic_long_read(&user->locked_vm); | |
8712 | new_pages = cur_pages + nr_pages; | |
8713 | if (new_pages > page_limit) | |
8714 | return -ENOMEM; | |
8715 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
8716 | new_pages) != cur_pages); | |
8717 | ||
8718 | return 0; | |
8719 | } | |
8720 | ||
26bfa89e | 8721 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8722 | { |
62e398be | 8723 | if (ctx->user) |
a087e2b5 | 8724 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 8725 | |
26bfa89e JA |
8726 | if (ctx->mm_account) |
8727 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8728 | } |
8729 | ||
26bfa89e | 8730 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8731 | { |
30975825 BM |
8732 | int ret; |
8733 | ||
62e398be | 8734 | if (ctx->user) { |
30975825 BM |
8735 | ret = __io_account_mem(ctx->user, nr_pages); |
8736 | if (ret) | |
8737 | return ret; | |
8738 | } | |
8739 | ||
26bfa89e JA |
8740 | if (ctx->mm_account) |
8741 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8742 | |
8743 | return 0; | |
8744 | } | |
8745 | ||
2b188cc1 JA |
8746 | static void io_mem_free(void *ptr) |
8747 | { | |
52e04ef4 MR |
8748 | struct page *page; |
8749 | ||
8750 | if (!ptr) | |
8751 | return; | |
2b188cc1 | 8752 | |
52e04ef4 | 8753 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8754 | if (put_page_testzero(page)) |
8755 | free_compound_page(page); | |
8756 | } | |
8757 | ||
8758 | static void *io_mem_alloc(size_t size) | |
8759 | { | |
44736143 | 8760 | gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP; |
2b188cc1 | 8761 | |
44736143 | 8762 | return (void *) __get_free_pages(gfp, get_order(size)); |
2b188cc1 JA |
8763 | } |
8764 | ||
75b28aff HV |
8765 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8766 | size_t *sq_offset) | |
8767 | { | |
8768 | struct io_rings *rings; | |
8769 | size_t off, sq_array_size; | |
8770 | ||
8771 | off = struct_size(rings, cqes, cq_entries); | |
8772 | if (off == SIZE_MAX) | |
8773 | return SIZE_MAX; | |
8774 | ||
8775 | #ifdef CONFIG_SMP | |
8776 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8777 | if (off == 0) | |
8778 | return SIZE_MAX; | |
8779 | #endif | |
8780 | ||
b36200f5 DV |
8781 | if (sq_offset) |
8782 | *sq_offset = off; | |
8783 | ||
75b28aff HV |
8784 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8785 | if (sq_array_size == SIZE_MAX) | |
8786 | return SIZE_MAX; | |
8787 | ||
8788 | if (check_add_overflow(off, sq_array_size, &off)) | |
8789 | return SIZE_MAX; | |
8790 | ||
75b28aff HV |
8791 | return off; |
8792 | } | |
8793 | ||
41edf1a5 | 8794 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot) |
7f61a1e9 | 8795 | { |
41edf1a5 | 8796 | struct io_mapped_ubuf *imu = *slot; |
7f61a1e9 PB |
8797 | unsigned int i; |
8798 | ||
6224843d PB |
8799 | if (imu != ctx->dummy_ubuf) { |
8800 | for (i = 0; i < imu->nr_bvecs; i++) | |
8801 | unpin_user_page(imu->bvec[i].bv_page); | |
8802 | if (imu->acct_pages) | |
8803 | io_unaccount_mem(ctx, imu->acct_pages); | |
8804 | kvfree(imu); | |
8805 | } | |
41edf1a5 | 8806 | *slot = NULL; |
7f61a1e9 PB |
8807 | } |
8808 | ||
bd54b6fe | 8809 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
edafccee | 8810 | { |
634d00df PB |
8811 | io_buffer_unmap(ctx, &prsrc->buf); |
8812 | prsrc->buf = NULL; | |
bd54b6fe | 8813 | } |
edafccee | 8814 | |
bd54b6fe BM |
8815 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
8816 | { | |
8817 | unsigned int i; | |
edafccee | 8818 | |
7f61a1e9 PB |
8819 | for (i = 0; i < ctx->nr_user_bufs; i++) |
8820 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); | |
edafccee | 8821 | kfree(ctx->user_bufs); |
bb6659cc | 8822 | io_rsrc_data_free(ctx->buf_data); |
edafccee | 8823 | ctx->user_bufs = NULL; |
bd54b6fe | 8824 | ctx->buf_data = NULL; |
edafccee | 8825 | ctx->nr_user_bufs = 0; |
bd54b6fe BM |
8826 | } |
8827 | ||
0a96bbe4 | 8828 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee | 8829 | { |
44aa2b86 | 8830 | unsigned nr = ctx->nr_user_bufs; |
bd54b6fe | 8831 | int ret; |
edafccee | 8832 | |
bd54b6fe | 8833 | if (!ctx->buf_data) |
edafccee JA |
8834 | return -ENXIO; |
8835 | ||
44aa2b86 PB |
8836 | /* |
8837 | * Quiesce may unlock ->uring_lock, and while it's not held | |
8838 | * prevent new requests using the table. | |
8839 | */ | |
8840 | ctx->nr_user_bufs = 0; | |
bd54b6fe | 8841 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
44aa2b86 | 8842 | ctx->nr_user_bufs = nr; |
bd54b6fe BM |
8843 | if (!ret) |
8844 | __io_sqe_buffers_unregister(ctx); | |
8845 | return ret; | |
edafccee JA |
8846 | } |
8847 | ||
8848 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8849 | void __user *arg, unsigned index) | |
8850 | { | |
8851 | struct iovec __user *src; | |
8852 | ||
8853 | #ifdef CONFIG_COMPAT | |
8854 | if (ctx->compat) { | |
8855 | struct compat_iovec __user *ciovs; | |
8856 | struct compat_iovec ciov; | |
8857 | ||
8858 | ciovs = (struct compat_iovec __user *) arg; | |
8859 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8860 | return -EFAULT; | |
8861 | ||
d55e5f5b | 8862 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8863 | dst->iov_len = ciov.iov_len; |
8864 | return 0; | |
8865 | } | |
8866 | #endif | |
8867 | src = (struct iovec __user *) arg; | |
8868 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8869 | return -EFAULT; | |
8870 | return 0; | |
8871 | } | |
8872 | ||
de293938 JA |
8873 | /* |
8874 | * Not super efficient, but this is just a registration time. And we do cache | |
8875 | * the last compound head, so generally we'll only do a full search if we don't | |
8876 | * match that one. | |
8877 | * | |
8878 | * We check if the given compound head page has already been accounted, to | |
8879 | * avoid double accounting it. This allows us to account the full size of the | |
8880 | * page, not just the constituent pages of a huge page. | |
8881 | */ | |
8882 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8883 | int nr_pages, struct page *hpage) | |
8884 | { | |
8885 | int i, j; | |
8886 | ||
8887 | /* check current page array */ | |
8888 | for (i = 0; i < nr_pages; i++) { | |
8889 | if (!PageCompound(pages[i])) | |
8890 | continue; | |
8891 | if (compound_head(pages[i]) == hpage) | |
8892 | return true; | |
8893 | } | |
8894 | ||
8895 | /* check previously registered pages */ | |
8896 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
41edf1a5 | 8897 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
de293938 JA |
8898 | |
8899 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8900 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8901 | continue; | |
8902 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8903 | return true; | |
8904 | } | |
8905 | } | |
8906 | ||
8907 | return false; | |
8908 | } | |
8909 | ||
8910 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8911 | int nr_pages, struct io_mapped_ubuf *imu, | |
8912 | struct page **last_hpage) | |
8913 | { | |
8914 | int i, ret; | |
8915 | ||
216e5835 | 8916 | imu->acct_pages = 0; |
de293938 JA |
8917 | for (i = 0; i < nr_pages; i++) { |
8918 | if (!PageCompound(pages[i])) { | |
8919 | imu->acct_pages++; | |
8920 | } else { | |
8921 | struct page *hpage; | |
8922 | ||
8923 | hpage = compound_head(pages[i]); | |
8924 | if (hpage == *last_hpage) | |
8925 | continue; | |
8926 | *last_hpage = hpage; | |
8927 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8928 | continue; | |
8929 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8930 | } | |
8931 | } | |
8932 | ||
8933 | if (!imu->acct_pages) | |
8934 | return 0; | |
8935 | ||
26bfa89e | 8936 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
8937 | if (ret) |
8938 | imu->acct_pages = 0; | |
8939 | return ret; | |
8940 | } | |
8941 | ||
0a96bbe4 | 8942 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
41edf1a5 | 8943 | struct io_mapped_ubuf **pimu, |
0a96bbe4 | 8944 | struct page **last_hpage) |
edafccee | 8945 | { |
41edf1a5 | 8946 | struct io_mapped_ubuf *imu = NULL; |
edafccee JA |
8947 | struct vm_area_struct **vmas = NULL; |
8948 | struct page **pages = NULL; | |
0a96bbe4 BM |
8949 | unsigned long off, start, end, ubuf; |
8950 | size_t size; | |
8951 | int ret, pret, nr_pages, i; | |
8952 | ||
6224843d PB |
8953 | if (!iov->iov_base) { |
8954 | *pimu = ctx->dummy_ubuf; | |
8955 | return 0; | |
8956 | } | |
8957 | ||
0a96bbe4 BM |
8958 | ubuf = (unsigned long) iov->iov_base; |
8959 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8960 | start = ubuf >> PAGE_SHIFT; | |
8961 | nr_pages = end - start; | |
8962 | ||
41edf1a5 | 8963 | *pimu = NULL; |
0a96bbe4 BM |
8964 | ret = -ENOMEM; |
8965 | ||
8966 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8967 | if (!pages) | |
8968 | goto done; | |
8969 | ||
8970 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8971 | GFP_KERNEL); | |
8972 | if (!vmas) | |
8973 | goto done; | |
edafccee | 8974 | |
41edf1a5 | 8975 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
a2b4198c | 8976 | if (!imu) |
0a96bbe4 BM |
8977 | goto done; |
8978 | ||
8979 | ret = 0; | |
8980 | mmap_read_lock(current->mm); | |
8981 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8982 | pages, vmas); | |
8983 | if (pret == nr_pages) { | |
8984 | /* don't support file backed memory */ | |
8985 | for (i = 0; i < nr_pages; i++) { | |
8986 | struct vm_area_struct *vma = vmas[i]; | |
8987 | ||
40dad765 PB |
8988 | if (vma_is_shmem(vma)) |
8989 | continue; | |
0a96bbe4 BM |
8990 | if (vma->vm_file && |
8991 | !is_file_hugepages(vma->vm_file)) { | |
8992 | ret = -EOPNOTSUPP; | |
8993 | break; | |
8994 | } | |
8995 | } | |
8996 | } else { | |
8997 | ret = pret < 0 ? pret : -EFAULT; | |
8998 | } | |
8999 | mmap_read_unlock(current->mm); | |
9000 | if (ret) { | |
9001 | /* | |
9002 | * if we did partial map, or found file backed vmas, | |
9003 | * release any pages we did get | |
9004 | */ | |
9005 | if (pret > 0) | |
9006 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9007 | goto done; |
9008 | } | |
9009 | ||
9010 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
9011 | if (ret) { | |
9012 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9013 | goto done; |
9014 | } | |
9015 | ||
9016 | off = ubuf & ~PAGE_MASK; | |
9017 | size = iov->iov_len; | |
9018 | for (i = 0; i < nr_pages; i++) { | |
9019 | size_t vec_len; | |
9020 | ||
9021 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
9022 | imu->bvec[i].bv_page = pages[i]; | |
9023 | imu->bvec[i].bv_len = vec_len; | |
9024 | imu->bvec[i].bv_offset = off; | |
9025 | off = 0; | |
9026 | size -= vec_len; | |
9027 | } | |
9028 | /* store original address for later verification */ | |
9029 | imu->ubuf = ubuf; | |
4751f53d | 9030 | imu->ubuf_end = ubuf + iov->iov_len; |
0a96bbe4 | 9031 | imu->nr_bvecs = nr_pages; |
41edf1a5 | 9032 | *pimu = imu; |
0a96bbe4 BM |
9033 | ret = 0; |
9034 | done: | |
41edf1a5 PB |
9035 | if (ret) |
9036 | kvfree(imu); | |
0a96bbe4 BM |
9037 | kvfree(pages); |
9038 | kvfree(vmas); | |
9039 | return ret; | |
9040 | } | |
9041 | ||
2b358604 | 9042 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 9043 | { |
87094465 PB |
9044 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
9045 | return ctx->user_bufs ? 0 : -ENOMEM; | |
2b358604 | 9046 | } |
edafccee | 9047 | |
2b358604 BM |
9048 | static int io_buffer_validate(struct iovec *iov) |
9049 | { | |
50e96989 PB |
9050 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
9051 | ||
2b358604 BM |
9052 | /* |
9053 | * Don't impose further limits on the size and buffer | |
9054 | * constraints here, we'll -EINVAL later when IO is | |
9055 | * submitted if they are wrong. | |
9056 | */ | |
6224843d PB |
9057 | if (!iov->iov_base) |
9058 | return iov->iov_len ? -EFAULT : 0; | |
9059 | if (!iov->iov_len) | |
2b358604 | 9060 | return -EFAULT; |
edafccee | 9061 | |
2b358604 BM |
9062 | /* arbitrary limit, but we need something */ |
9063 | if (iov->iov_len > SZ_1G) | |
9064 | return -EFAULT; | |
edafccee | 9065 | |
50e96989 PB |
9066 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
9067 | return -EOVERFLOW; | |
9068 | ||
2b358604 BM |
9069 | return 0; |
9070 | } | |
edafccee | 9071 | |
2b358604 | 9072 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
634d00df | 9073 | unsigned int nr_args, u64 __user *tags) |
2b358604 | 9074 | { |
bd54b6fe BM |
9075 | struct page *last_hpage = NULL; |
9076 | struct io_rsrc_data *data; | |
2b358604 BM |
9077 | int i, ret; |
9078 | struct iovec iov; | |
edafccee | 9079 | |
87094465 PB |
9080 | if (ctx->user_bufs) |
9081 | return -EBUSY; | |
489809e2 | 9082 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
87094465 | 9083 | return -EINVAL; |
bd54b6fe | 9084 | ret = io_rsrc_node_switch_start(ctx); |
2b358604 BM |
9085 | if (ret) |
9086 | return ret; | |
d878c816 PB |
9087 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
9088 | if (ret) | |
9089 | return ret; | |
bd54b6fe BM |
9090 | ret = io_buffers_map_alloc(ctx, nr_args); |
9091 | if (ret) { | |
bb6659cc | 9092 | io_rsrc_data_free(data); |
bd54b6fe BM |
9093 | return ret; |
9094 | } | |
edafccee | 9095 | |
87094465 | 9096 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
edafccee JA |
9097 | ret = io_copy_iov(ctx, &iov, arg, i); |
9098 | if (ret) | |
0a96bbe4 | 9099 | break; |
2b358604 BM |
9100 | ret = io_buffer_validate(&iov); |
9101 | if (ret) | |
0a96bbe4 | 9102 | break; |
2d091d62 | 9103 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
cf3770e7 CIK |
9104 | ret = -EINVAL; |
9105 | break; | |
9106 | } | |
edafccee | 9107 | |
41edf1a5 PB |
9108 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
9109 | &last_hpage); | |
0a96bbe4 BM |
9110 | if (ret) |
9111 | break; | |
edafccee | 9112 | } |
0a96bbe4 | 9113 | |
bd54b6fe | 9114 | WARN_ON_ONCE(ctx->buf_data); |
0a96bbe4 | 9115 | |
bd54b6fe BM |
9116 | ctx->buf_data = data; |
9117 | if (ret) | |
9118 | __io_sqe_buffers_unregister(ctx); | |
9119 | else | |
9120 | io_rsrc_node_switch(ctx, NULL); | |
edafccee JA |
9121 | return ret; |
9122 | } | |
9123 | ||
634d00df PB |
9124 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
9125 | struct io_uring_rsrc_update2 *up, | |
9126 | unsigned int nr_args) | |
9127 | { | |
9128 | u64 __user *tags = u64_to_user_ptr(up->tags); | |
9129 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); | |
634d00df PB |
9130 | struct page *last_hpage = NULL; |
9131 | bool needs_switch = false; | |
9132 | __u32 done; | |
9133 | int i, err; | |
9134 | ||
9135 | if (!ctx->buf_data) | |
9136 | return -ENXIO; | |
9137 | if (up->offset + nr_args > ctx->nr_user_bufs) | |
9138 | return -EINVAL; | |
9139 | ||
9140 | for (done = 0; done < nr_args; done++) { | |
0b8c0e7c PB |
9141 | struct io_mapped_ubuf *imu; |
9142 | int offset = up->offset + done; | |
634d00df PB |
9143 | u64 tag = 0; |
9144 | ||
9145 | err = io_copy_iov(ctx, &iov, iovs, done); | |
9146 | if (err) | |
9147 | break; | |
9148 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { | |
9149 | err = -EFAULT; | |
9150 | break; | |
9151 | } | |
0b8c0e7c PB |
9152 | err = io_buffer_validate(&iov); |
9153 | if (err) | |
9154 | break; | |
cf3770e7 CIK |
9155 | if (!iov.iov_base && tag) { |
9156 | err = -EINVAL; | |
9157 | break; | |
9158 | } | |
0b8c0e7c PB |
9159 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
9160 | if (err) | |
9161 | break; | |
634d00df | 9162 | |
0b8c0e7c | 9163 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
6224843d | 9164 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
22a99dcb | 9165 | err = io_queue_rsrc_removal(ctx->buf_data, i, |
0b8c0e7c PB |
9166 | ctx->rsrc_node, ctx->user_bufs[i]); |
9167 | if (unlikely(err)) { | |
9168 | io_buffer_unmap(ctx, &imu); | |
634d00df | 9169 | break; |
0b8c0e7c | 9170 | } |
634d00df PB |
9171 | ctx->user_bufs[i] = NULL; |
9172 | needs_switch = true; | |
9173 | } | |
9174 | ||
0b8c0e7c | 9175 | ctx->user_bufs[i] = imu; |
2d091d62 | 9176 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
634d00df PB |
9177 | } |
9178 | ||
9179 | if (needs_switch) | |
9180 | io_rsrc_node_switch(ctx, ctx->buf_data); | |
9181 | return done ? done : err; | |
9182 | } | |
9183 | ||
9b402849 JA |
9184 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
9185 | { | |
9186 | __s32 __user *fds = arg; | |
9187 | int fd; | |
9188 | ||
9189 | if (ctx->cq_ev_fd) | |
9190 | return -EBUSY; | |
9191 | ||
9192 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
9193 | return -EFAULT; | |
9194 | ||
9195 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
9196 | if (IS_ERR(ctx->cq_ev_fd)) { | |
9197 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
fe7e3257 | 9198 | |
9b402849 JA |
9199 | ctx->cq_ev_fd = NULL; |
9200 | return ret; | |
9201 | } | |
9202 | ||
9203 | return 0; | |
9204 | } | |
9205 | ||
9206 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
9207 | { | |
9208 | if (ctx->cq_ev_fd) { | |
9209 | eventfd_ctx_put(ctx->cq_ev_fd); | |
9210 | ctx->cq_ev_fd = NULL; | |
9211 | return 0; | |
9212 | } | |
9213 | ||
9214 | return -ENXIO; | |
9215 | } | |
9216 | ||
5a2e745d JA |
9217 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
9218 | { | |
9e15c3a0 JA |
9219 | struct io_buffer *buf; |
9220 | unsigned long index; | |
9221 | ||
07edfd19 | 9222 | xa_for_each(&ctx->io_buffers, index, buf) |
9e15c3a0 | 9223 | __io_remove_buffers(ctx, buf, index, -1U); |
5a2e745d JA |
9224 | } |
9225 | ||
7255834e | 9226 | static void io_req_cache_free(struct list_head *list) |
1b4c351f | 9227 | { |
68e68ee6 | 9228 | struct io_kiocb *req, *nxt; |
1b4c351f | 9229 | |
bb943b82 PB |
9230 | list_for_each_entry_safe(req, nxt, list, inflight_entry) { |
9231 | list_del(&req->inflight_entry); | |
1b4c351f JA |
9232 | kmem_cache_free(req_cachep, req); |
9233 | } | |
9234 | } | |
9235 | ||
4010fec4 | 9236 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9237 | { |
cd0ca2e0 | 9238 | struct io_submit_state *state = &ctx->submit_state; |
bf019da7 | 9239 | |
9a4fdbd8 JA |
9240 | mutex_lock(&ctx->uring_lock); |
9241 | ||
cd0ca2e0 PB |
9242 | if (state->free_reqs) { |
9243 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); | |
9244 | state->free_reqs = 0; | |
8e5c66c4 | 9245 | } |
9a4fdbd8 | 9246 | |
cd0ca2e0 PB |
9247 | io_flush_cached_locked_reqs(ctx, state); |
9248 | io_req_cache_free(&state->free_list); | |
9a4fdbd8 JA |
9249 | mutex_unlock(&ctx->uring_lock); |
9250 | } | |
9251 | ||
43597aac | 9252 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
2b188cc1 | 9253 | { |
43597aac | 9254 | if (data && !atomic_dec_and_test(&data->refs)) |
bd54b6fe | 9255 | wait_for_completion(&data->done); |
bd54b6fe | 9256 | } |
04fc6c80 | 9257 | |
2b188cc1 JA |
9258 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
9259 | { | |
37d1e2e3 | 9260 | io_sq_thread_finish(ctx); |
2aede0e4 | 9261 | |
37d1e2e3 | 9262 | if (ctx->mm_account) { |
2aede0e4 JA |
9263 | mmdrop(ctx->mm_account); |
9264 | ctx->mm_account = NULL; | |
30975825 | 9265 | } |
def596e9 | 9266 | |
43597aac PB |
9267 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
9268 | io_wait_rsrc_data(ctx->buf_data); | |
9269 | io_wait_rsrc_data(ctx->file_data); | |
9270 | ||
8bad28d8 | 9271 | mutex_lock(&ctx->uring_lock); |
43597aac | 9272 | if (ctx->buf_data) |
bd54b6fe | 9273 | __io_sqe_buffers_unregister(ctx); |
43597aac | 9274 | if (ctx->file_data) |
08480400 | 9275 | __io_sqe_files_unregister(ctx); |
c4ea060e PB |
9276 | if (ctx->rings) |
9277 | __io_cqring_overflow_flush(ctx, true); | |
8bad28d8 | 9278 | mutex_unlock(&ctx->uring_lock); |
9b402849 | 9279 | io_eventfd_unregister(ctx); |
5a2e745d | 9280 | io_destroy_buffers(ctx); |
07db298a PB |
9281 | if (ctx->sq_creds) |
9282 | put_cred(ctx->sq_creds); | |
def596e9 | 9283 | |
a7f0ed5a PB |
9284 | /* there are no registered resources left, nobody uses it */ |
9285 | if (ctx->rsrc_node) | |
9286 | io_rsrc_node_destroy(ctx->rsrc_node); | |
8dd03afe | 9287 | if (ctx->rsrc_backup_node) |
b895c9a6 | 9288 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
a7f0ed5a PB |
9289 | flush_delayed_work(&ctx->rsrc_put_work); |
9290 | ||
9291 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); | |
9292 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); | |
def596e9 | 9293 | |
2b188cc1 | 9294 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
9295 | if (ctx->ring_sock) { |
9296 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 9297 | sock_release(ctx->ring_sock); |
355e8d26 | 9298 | } |
2b188cc1 | 9299 | #endif |
ef9dd637 | 9300 | WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); |
2b188cc1 | 9301 | |
75b28aff | 9302 | io_mem_free(ctx->rings); |
2b188cc1 | 9303 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
9304 | |
9305 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 9306 | free_uid(ctx->user); |
4010fec4 | 9307 | io_req_caches_free(ctx); |
e941894e JA |
9308 | if (ctx->hash_map) |
9309 | io_wq_put_hash(ctx->hash_map); | |
78076bb6 | 9310 | kfree(ctx->cancel_hash); |
6224843d | 9311 | kfree(ctx->dummy_ubuf); |
2b188cc1 JA |
9312 | kfree(ctx); |
9313 | } | |
9314 | ||
9315 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
9316 | { | |
9317 | struct io_ring_ctx *ctx = file->private_data; | |
9318 | __poll_t mask = 0; | |
9319 | ||
311997b3 | 9320 | poll_wait(file, &ctx->poll_wait, wait); |
4f7067c3 SB |
9321 | /* |
9322 | * synchronizes with barrier from wq_has_sleeper call in | |
9323 | * io_commit_cqring | |
9324 | */ | |
2b188cc1 | 9325 | smp_rmb(); |
90554200 | 9326 | if (!io_sqring_full(ctx)) |
2b188cc1 | 9327 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
9328 | |
9329 | /* | |
9330 | * Don't flush cqring overflow list here, just do a simple check. | |
9331 | * Otherwise there could possible be ABBA deadlock: | |
9332 | * CPU0 CPU1 | |
9333 | * ---- ---- | |
9334 | * lock(&ctx->uring_lock); | |
9335 | * lock(&ep->mtx); | |
9336 | * lock(&ctx->uring_lock); | |
9337 | * lock(&ep->mtx); | |
9338 | * | |
9339 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
9340 | * pushs them to do the flush. | |
9341 | */ | |
5ed7a37d | 9342 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
2b188cc1 JA |
9343 | mask |= EPOLLIN | EPOLLRDNORM; |
9344 | ||
9345 | return mask; | |
9346 | } | |
9347 | ||
0bead8cd | 9348 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 9349 | { |
4379bf8b | 9350 | const struct cred *creds; |
071698e1 | 9351 | |
61cf9370 | 9352 | creds = xa_erase(&ctx->personalities, id); |
4379bf8b JA |
9353 | if (creds) { |
9354 | put_cred(creds); | |
0bead8cd | 9355 | return 0; |
1e6fa521 | 9356 | } |
0bead8cd YD |
9357 | |
9358 | return -EINVAL; | |
9359 | } | |
9360 | ||
d56d938b PB |
9361 | struct io_tctx_exit { |
9362 | struct callback_head task_work; | |
9363 | struct completion completion; | |
baf186c4 | 9364 | struct io_ring_ctx *ctx; |
d56d938b PB |
9365 | }; |
9366 | ||
9367 | static void io_tctx_exit_cb(struct callback_head *cb) | |
9368 | { | |
9369 | struct io_uring_task *tctx = current->io_uring; | |
9370 | struct io_tctx_exit *work; | |
9371 | ||
9372 | work = container_of(cb, struct io_tctx_exit, task_work); | |
9373 | /* | |
9374 | * When @in_idle, we're in cancellation and it's racy to remove the | |
9375 | * node. It'll be removed by the end of cancellation, just ignore it. | |
9376 | */ | |
9377 | if (!atomic_read(&tctx->in_idle)) | |
eef51daa | 9378 | io_uring_del_tctx_node((unsigned long)work->ctx); |
d56d938b PB |
9379 | complete(&work->completion); |
9380 | } | |
9381 | ||
28090c13 PB |
9382 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
9383 | { | |
9384 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
9385 | ||
9386 | return req->ctx == data; | |
9387 | } | |
9388 | ||
85faa7b8 JA |
9389 | static void io_ring_exit_work(struct work_struct *work) |
9390 | { | |
d56d938b | 9391 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 9392 | unsigned long timeout = jiffies + HZ * 60 * 5; |
58d3be2c | 9393 | unsigned long interval = HZ / 20; |
d56d938b PB |
9394 | struct io_tctx_exit exit; |
9395 | struct io_tctx_node *node; | |
9396 | int ret; | |
85faa7b8 | 9397 | |
56952e91 JA |
9398 | /* |
9399 | * If we're doing polled IO and end up having requests being | |
9400 | * submitted async (out-of-line), then completions can come in while | |
9401 | * we're waiting for refs to drop. We need to reap these manually, | |
9402 | * as nobody else will be looking for them. | |
9403 | */ | |
b2edc0a7 | 9404 | do { |
3dd0c97a | 9405 | io_uring_try_cancel_requests(ctx, NULL, true); |
28090c13 PB |
9406 | if (ctx->sq_data) { |
9407 | struct io_sq_data *sqd = ctx->sq_data; | |
9408 | struct task_struct *tsk; | |
9409 | ||
9410 | io_sq_thread_park(sqd); | |
9411 | tsk = sqd->thread; | |
9412 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) | |
9413 | io_wq_cancel_cb(tsk->io_uring->io_wq, | |
9414 | io_cancel_ctx_cb, ctx, true); | |
9415 | io_sq_thread_unpark(sqd); | |
9416 | } | |
b5bb3a24 | 9417 | |
58d3be2c PB |
9418 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
9419 | /* there is little hope left, don't run it too often */ | |
9420 | interval = HZ * 60; | |
9421 | } | |
9422 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); | |
d56d938b | 9423 | |
7f00651a PB |
9424 | init_completion(&exit.completion); |
9425 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
9426 | exit.ctx = ctx; | |
89b5066e PB |
9427 | /* |
9428 | * Some may use context even when all refs and requests have been put, | |
9429 | * and they are free to do so while still holding uring_lock or | |
5b0a6acc | 9430 | * completion_lock, see io_req_task_submit(). Apart from other work, |
89b5066e PB |
9431 | * this lock/unlock section also waits them to finish. |
9432 | */ | |
d56d938b PB |
9433 | mutex_lock(&ctx->uring_lock); |
9434 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
9435 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
9436 | ||
d56d938b PB |
9437 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
9438 | ctx_node); | |
7f00651a PB |
9439 | /* don't spin on a single task if cancellation failed */ |
9440 | list_rotate_left(&ctx->tctx_list); | |
d56d938b PB |
9441 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
9442 | if (WARN_ON_ONCE(ret)) | |
9443 | continue; | |
9444 | wake_up_process(node->task); | |
9445 | ||
9446 | mutex_unlock(&ctx->uring_lock); | |
9447 | wait_for_completion(&exit.completion); | |
d56d938b PB |
9448 | mutex_lock(&ctx->uring_lock); |
9449 | } | |
9450 | mutex_unlock(&ctx->uring_lock); | |
79ebeaee JA |
9451 | spin_lock(&ctx->completion_lock); |
9452 | spin_unlock(&ctx->completion_lock); | |
d56d938b | 9453 | |
85faa7b8 JA |
9454 | io_ring_ctx_free(ctx); |
9455 | } | |
9456 | ||
80c4cbdb PB |
9457 | /* Returns true if we found and killed one or more timeouts */ |
9458 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, | |
3dd0c97a | 9459 | bool cancel_all) |
80c4cbdb PB |
9460 | { |
9461 | struct io_kiocb *req, *tmp; | |
9462 | int canceled = 0; | |
9463 | ||
79ebeaee JA |
9464 | spin_lock(&ctx->completion_lock); |
9465 | spin_lock_irq(&ctx->timeout_lock); | |
80c4cbdb | 9466 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
3dd0c97a | 9467 | if (io_match_task(req, tsk, cancel_all)) { |
80c4cbdb PB |
9468 | io_kill_timeout(req, -ECANCELED); |
9469 | canceled++; | |
9470 | } | |
9471 | } | |
79ebeaee | 9472 | spin_unlock_irq(&ctx->timeout_lock); |
51520426 PB |
9473 | if (canceled != 0) |
9474 | io_commit_cqring(ctx); | |
79ebeaee | 9475 | spin_unlock(&ctx->completion_lock); |
80c4cbdb PB |
9476 | if (canceled != 0) |
9477 | io_cqring_ev_posted(ctx); | |
9478 | return canceled != 0; | |
9479 | } | |
9480 | ||
2b188cc1 JA |
9481 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
9482 | { | |
61cf9370 MWO |
9483 | unsigned long index; |
9484 | struct creds *creds; | |
9485 | ||
2b188cc1 JA |
9486 | mutex_lock(&ctx->uring_lock); |
9487 | percpu_ref_kill(&ctx->refs); | |
634578f8 | 9488 | if (ctx->rings) |
6c2450ae | 9489 | __io_cqring_overflow_flush(ctx, true); |
61cf9370 MWO |
9490 | xa_for_each(&ctx->personalities, index, creds) |
9491 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
9492 | mutex_unlock(&ctx->uring_lock); |
9493 | ||
3dd0c97a PB |
9494 | io_kill_timeouts(ctx, NULL, true); |
9495 | io_poll_remove_all(ctx, NULL, true); | |
561fb04a | 9496 | |
15dff286 | 9497 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 9498 | io_iopoll_try_reap_events(ctx); |
309fc03a | 9499 | |
85faa7b8 | 9500 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
9501 | /* |
9502 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
9503 | * if we're exiting a ton of rings at the same time. It just adds | |
9504 | * noise and overhead, there's no discernable change in runtime | |
9505 | * over using system_wq. | |
9506 | */ | |
9507 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
9508 | } |
9509 | ||
9510 | static int io_uring_release(struct inode *inode, struct file *file) | |
9511 | { | |
9512 | struct io_ring_ctx *ctx = file->private_data; | |
9513 | ||
9514 | file->private_data = NULL; | |
9515 | io_ring_ctx_wait_and_kill(ctx); | |
9516 | return 0; | |
9517 | } | |
9518 | ||
f6edbabb PB |
9519 | struct io_task_cancel { |
9520 | struct task_struct *task; | |
3dd0c97a | 9521 | bool all; |
f6edbabb | 9522 | }; |
f254ac04 | 9523 | |
f6edbabb | 9524 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 9525 | { |
9a472ef7 | 9526 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 9527 | struct io_task_cancel *cancel = data; |
9a472ef7 | 9528 | |
f0baed8e | 9529 | return io_match_task_safe(req, cancel->task, cancel->all); |
b711d4ea JA |
9530 | } |
9531 | ||
e1915f76 | 9532 | static bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
3dd0c97a | 9533 | struct task_struct *task, bool cancel_all) |
b7ddce3c | 9534 | { |
e1915f76 | 9535 | struct io_defer_entry *de; |
b7ddce3c PB |
9536 | LIST_HEAD(list); |
9537 | ||
79ebeaee | 9538 | spin_lock(&ctx->completion_lock); |
b7ddce3c | 9539 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
f0baed8e | 9540 | if (io_match_task_safe(de->req, task, cancel_all)) { |
b7ddce3c PB |
9541 | list_cut_position(&list, &ctx->defer_list, &de->list); |
9542 | break; | |
9543 | } | |
9544 | } | |
79ebeaee | 9545 | spin_unlock(&ctx->completion_lock); |
e1915f76 PB |
9546 | if (list_empty(&list)) |
9547 | return false; | |
b7ddce3c PB |
9548 | |
9549 | while (!list_empty(&list)) { | |
9550 | de = list_first_entry(&list, struct io_defer_entry, list); | |
9551 | list_del_init(&de->list); | |
f41db273 | 9552 | io_req_complete_failed(de->req, -ECANCELED); |
b7ddce3c PB |
9553 | kfree(de); |
9554 | } | |
e1915f76 | 9555 | return true; |
b7ddce3c PB |
9556 | } |
9557 | ||
1b00764f PB |
9558 | static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
9559 | { | |
9560 | struct io_tctx_node *node; | |
9561 | enum io_wq_cancel cret; | |
9562 | bool ret = false; | |
9563 | ||
9564 | mutex_lock(&ctx->uring_lock); | |
9565 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
9566 | struct io_uring_task *tctx = node->task->io_uring; | |
9567 | ||
9568 | /* | |
9569 | * io_wq will stay alive while we hold uring_lock, because it's | |
9570 | * killed after ctx nodes, which requires to take the lock. | |
9571 | */ | |
9572 | if (!tctx || !tctx->io_wq) | |
9573 | continue; | |
9574 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
9575 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9576 | } | |
9577 | mutex_unlock(&ctx->uring_lock); | |
9578 | ||
9579 | return ret; | |
9580 | } | |
9581 | ||
9936c7c2 PB |
9582 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
9583 | struct task_struct *task, | |
3dd0c97a | 9584 | bool cancel_all) |
9936c7c2 | 9585 | { |
3dd0c97a | 9586 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
1b00764f | 9587 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 PB |
9588 | |
9589 | while (1) { | |
9590 | enum io_wq_cancel cret; | |
9591 | bool ret = false; | |
9592 | ||
1b00764f PB |
9593 | if (!task) { |
9594 | ret |= io_uring_try_cancel_iowq(ctx); | |
9595 | } else if (tctx && tctx->io_wq) { | |
9596 | /* | |
9597 | * Cancels requests of all rings, not only @ctx, but | |
9598 | * it's fine as the task is in exit/exec. | |
9599 | */ | |
5aa75ed5 | 9600 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
9601 | &cancel, true); |
9602 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9603 | } | |
9604 | ||
9605 | /* SQPOLL thread does its own polling */ | |
3dd0c97a | 9606 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
d052d1d6 | 9607 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
9936c7c2 PB |
9608 | while (!list_empty_careful(&ctx->iopoll_list)) { |
9609 | io_iopoll_try_reap_events(ctx); | |
9610 | ret = true; | |
9611 | } | |
9612 | } | |
9613 | ||
3dd0c97a PB |
9614 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
9615 | ret |= io_poll_remove_all(ctx, task, cancel_all); | |
9616 | ret |= io_kill_timeouts(ctx, task, cancel_all); | |
e5dc480d PB |
9617 | if (task) |
9618 | ret |= io_run_task_work(); | |
9936c7c2 PB |
9619 | if (!ret) |
9620 | break; | |
9621 | cond_resched(); | |
9622 | } | |
9623 | } | |
9624 | ||
eef51daa | 9625 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
0f212204 | 9626 | { |
236434c3 | 9627 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 9628 | struct io_tctx_node *node; |
a528b04e | 9629 | int ret; |
236434c3 MWO |
9630 | |
9631 | if (unlikely(!tctx)) { | |
5aa75ed5 | 9632 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
9633 | if (unlikely(ret)) |
9634 | return ret; | |
e139a1ec | 9635 | |
236434c3 | 9636 | tctx = current->io_uring; |
e139a1ec PB |
9637 | if (ctx->iowq_limits_set) { |
9638 | unsigned int limits[2] = { ctx->iowq_limits[0], | |
9639 | ctx->iowq_limits[1], }; | |
9640 | ||
9641 | ret = io_wq_max_workers(tctx->io_wq, limits); | |
9642 | if (ret) | |
9643 | return ret; | |
9644 | } | |
0f212204 | 9645 | } |
cf27f3b1 PB |
9646 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
9647 | node = kmalloc(sizeof(*node), GFP_KERNEL); | |
9648 | if (!node) | |
9649 | return -ENOMEM; | |
9650 | node->ctx = ctx; | |
9651 | node->task = current; | |
13bf43f5 | 9652 | |
cf27f3b1 PB |
9653 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
9654 | node, GFP_KERNEL)); | |
9655 | if (ret) { | |
9656 | kfree(node); | |
9657 | return ret; | |
0f212204 | 9658 | } |
cf27f3b1 PB |
9659 | |
9660 | mutex_lock(&ctx->uring_lock); | |
9661 | list_add(&node->ctx_node, &ctx->tctx_list); | |
9662 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 9663 | } |
cf27f3b1 | 9664 | tctx->last = ctx; |
0f212204 JA |
9665 | return 0; |
9666 | } | |
9667 | ||
cf27f3b1 PB |
9668 | /* |
9669 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
9670 | */ | |
eef51daa | 9671 | static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
cf27f3b1 PB |
9672 | { |
9673 | struct io_uring_task *tctx = current->io_uring; | |
9674 | ||
9675 | if (likely(tctx && tctx->last == ctx)) | |
9676 | return 0; | |
eef51daa | 9677 | return __io_uring_add_tctx_node(ctx); |
cf27f3b1 PB |
9678 | } |
9679 | ||
0f212204 JA |
9680 | /* |
9681 | * Remove this io_uring_file -> task mapping. | |
9682 | */ | |
eef51daa | 9683 | static void io_uring_del_tctx_node(unsigned long index) |
0f212204 JA |
9684 | { |
9685 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 9686 | struct io_tctx_node *node; |
2941267b | 9687 | |
eebd2e37 PB |
9688 | if (!tctx) |
9689 | return; | |
13bf43f5 PB |
9690 | node = xa_erase(&tctx->xa, index); |
9691 | if (!node) | |
2941267b | 9692 | return; |
0f212204 | 9693 | |
13bf43f5 PB |
9694 | WARN_ON_ONCE(current != node->task); |
9695 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
9696 | ||
9697 | mutex_lock(&node->ctx->uring_lock); | |
9698 | list_del(&node->ctx_node); | |
9699 | mutex_unlock(&node->ctx->uring_lock); | |
9700 | ||
baf186c4 | 9701 | if (tctx->last == node->ctx) |
0f212204 | 9702 | tctx->last = NULL; |
13bf43f5 | 9703 | kfree(node); |
0f212204 JA |
9704 | } |
9705 | ||
8452d4a6 | 9706 | static void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 9707 | { |
ba5ef6dc | 9708 | struct io_wq *wq = tctx->io_wq; |
13bf43f5 | 9709 | struct io_tctx_node *node; |
de7f1d9e PB |
9710 | unsigned long index; |
9711 | ||
8bab4c09 | 9712 | xa_for_each(&tctx->xa, index, node) { |
eef51daa | 9713 | io_uring_del_tctx_node(index); |
8bab4c09 JA |
9714 | cond_resched(); |
9715 | } | |
b16ef427 ME |
9716 | if (wq) { |
9717 | /* | |
9718 | * Must be after io_uring_del_task_file() (removes nodes under | |
9719 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). | |
9720 | */ | |
ba5ef6dc | 9721 | io_wq_put_and_exit(wq); |
dadebc35 | 9722 | tctx->io_wq = NULL; |
b16ef427 | 9723 | } |
de7f1d9e PB |
9724 | } |
9725 | ||
3f48cf18 | 9726 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
521d6a73 | 9727 | { |
3f48cf18 PB |
9728 | if (tracked) |
9729 | return atomic_read(&tctx->inflight_tracked); | |
521d6a73 PB |
9730 | return percpu_counter_sum(&tctx->inflight); |
9731 | } | |
9732 | ||
78cc687b PB |
9733 | /* |
9734 | * Find any io_uring ctx that this task has registered or done IO on, and cancel | |
b16590fd | 9735 | * requests. @sqd should be not-null IFF it's an SQPOLL thread cancellation. |
78cc687b PB |
9736 | */ |
9737 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd) | |
0e9ddb39 | 9738 | { |
521d6a73 | 9739 | struct io_uring_task *tctx = current->io_uring; |
734551df | 9740 | struct io_ring_ctx *ctx; |
0e9ddb39 PB |
9741 | s64 inflight; |
9742 | DEFINE_WAIT(wait); | |
fdaf083c | 9743 | |
78cc687b PB |
9744 | WARN_ON_ONCE(sqd && sqd->thread != current); |
9745 | ||
6d042ffb PO |
9746 | if (!current->io_uring) |
9747 | return; | |
17a91051 PB |
9748 | if (tctx->io_wq) |
9749 | io_wq_exit_start(tctx->io_wq); | |
9750 | ||
0e9ddb39 PB |
9751 | atomic_inc(&tctx->in_idle); |
9752 | do { | |
e9dbe221 | 9753 | io_uring_drop_tctx_refs(current); |
0e9ddb39 | 9754 | /* read completions before cancelations */ |
78cc687b | 9755 | inflight = tctx_inflight(tctx, !cancel_all); |
0e9ddb39 PB |
9756 | if (!inflight) |
9757 | break; | |
fdaf083c | 9758 | |
78cc687b PB |
9759 | if (!sqd) { |
9760 | struct io_tctx_node *node; | |
9761 | unsigned long index; | |
0f212204 | 9762 | |
78cc687b PB |
9763 | xa_for_each(&tctx->xa, index, node) { |
9764 | /* sqpoll task will cancel all its requests */ | |
9765 | if (node->ctx->sq_data) | |
9766 | continue; | |
9767 | io_uring_try_cancel_requests(node->ctx, current, | |
9768 | cancel_all); | |
9769 | } | |
9770 | } else { | |
9771 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
9772 | io_uring_try_cancel_requests(ctx, current, | |
9773 | cancel_all); | |
9774 | } | |
17a91051 | 9775 | |
b16590fd JA |
9776 | prepare_to_wait(&tctx->wait, &wait, TASK_INTERRUPTIBLE); |
9777 | io_run_task_work(); | |
e9dbe221 | 9778 | io_uring_drop_tctx_refs(current); |
b16590fd | 9779 | |
0f212204 | 9780 | /* |
a1bb3cd5 PB |
9781 | * If we've seen completions, retry without waiting. This |
9782 | * avoids a race where a completion comes in before we did | |
9783 | * prepare_to_wait(). | |
0f212204 | 9784 | */ |
3dd0c97a | 9785 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
a1bb3cd5 | 9786 | schedule(); |
f57555ed | 9787 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 9788 | } while (1); |
de7f1d9e | 9789 | |
8452d4a6 | 9790 | io_uring_clean_tctx(tctx); |
3dd0c97a | 9791 | if (cancel_all) { |
abdebba9 PB |
9792 | /* |
9793 | * We shouldn't run task_works after cancel, so just leave | |
9794 | * ->in_idle set for normal exit. | |
9795 | */ | |
9796 | atomic_dec(&tctx->in_idle); | |
3f48cf18 PB |
9797 | /* for exec all current's requests should be gone, kill tctx */ |
9798 | __io_uring_free(current); | |
9799 | } | |
44e728b8 PB |
9800 | } |
9801 | ||
f552a27a | 9802 | void __io_uring_cancel(bool cancel_all) |
78cc687b | 9803 | { |
f552a27a | 9804 | io_uring_cancel_generic(cancel_all, NULL); |
78cc687b PB |
9805 | } |
9806 | ||
6c5c240e RP |
9807 | static void *io_uring_validate_mmap_request(struct file *file, |
9808 | loff_t pgoff, size_t sz) | |
2b188cc1 | 9809 | { |
2b188cc1 | 9810 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 9811 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
9812 | struct page *page; |
9813 | void *ptr; | |
9814 | ||
9815 | switch (offset) { | |
9816 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
9817 | case IORING_OFF_CQ_RING: |
9818 | ptr = ctx->rings; | |
2b188cc1 JA |
9819 | break; |
9820 | case IORING_OFF_SQES: | |
9821 | ptr = ctx->sq_sqes; | |
9822 | break; | |
2b188cc1 | 9823 | default: |
6c5c240e | 9824 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
9825 | } |
9826 | ||
9827 | page = virt_to_head_page(ptr); | |
a50b854e | 9828 | if (sz > page_size(page)) |
6c5c240e RP |
9829 | return ERR_PTR(-EINVAL); |
9830 | ||
9831 | return ptr; | |
9832 | } | |
9833 | ||
9834 | #ifdef CONFIG_MMU | |
9835 | ||
9836 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9837 | { | |
9838 | size_t sz = vma->vm_end - vma->vm_start; | |
9839 | unsigned long pfn; | |
9840 | void *ptr; | |
9841 | ||
9842 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
9843 | if (IS_ERR(ptr)) | |
9844 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9845 | |
9846 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9847 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9848 | } | |
9849 | ||
6c5c240e RP |
9850 | #else /* !CONFIG_MMU */ |
9851 | ||
9852 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9853 | { | |
9854 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9855 | } | |
9856 | ||
9857 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9858 | { | |
9859 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9860 | } | |
9861 | ||
9862 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9863 | unsigned long addr, unsigned long len, | |
9864 | unsigned long pgoff, unsigned long flags) | |
9865 | { | |
9866 | void *ptr; | |
9867 | ||
9868 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9869 | if (IS_ERR(ptr)) | |
9870 | return PTR_ERR(ptr); | |
9871 | ||
9872 | return (unsigned long) ptr; | |
9873 | } | |
9874 | ||
9875 | #endif /* !CONFIG_MMU */ | |
9876 | ||
d9d05217 | 9877 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 JA |
9878 | { |
9879 | DEFINE_WAIT(wait); | |
9880 | ||
9881 | do { | |
9882 | if (!io_sqring_full(ctx)) | |
9883 | break; | |
90554200 JA |
9884 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
9885 | ||
9886 | if (!io_sqring_full(ctx)) | |
9887 | break; | |
90554200 JA |
9888 | schedule(); |
9889 | } while (!signal_pending(current)); | |
9890 | ||
9891 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
5199328a | 9892 | return 0; |
90554200 JA |
9893 | } |
9894 | ||
c73ebb68 HX |
9895 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9896 | struct __kernel_timespec __user **ts, | |
9897 | const sigset_t __user **sig) | |
9898 | { | |
9899 | struct io_uring_getevents_arg arg; | |
9900 | ||
9901 | /* | |
9902 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9903 | * is just a pointer to the sigset_t. | |
9904 | */ | |
9905 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9906 | *sig = (const sigset_t __user *) argp; | |
9907 | *ts = NULL; | |
9908 | return 0; | |
9909 | } | |
9910 | ||
9911 | /* | |
9912 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9913 | * timespec and sigset_t pointers if good. | |
9914 | */ | |
9915 | if (*argsz != sizeof(arg)) | |
9916 | return -EINVAL; | |
9917 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9918 | return -EFAULT; | |
02a84aaf DY |
9919 | if (arg.pad) |
9920 | return -EINVAL; | |
c73ebb68 HX |
9921 | *sig = u64_to_user_ptr(arg.sigmask); |
9922 | *argsz = arg.sigmask_sz; | |
9923 | *ts = u64_to_user_ptr(arg.ts); | |
9924 | return 0; | |
9925 | } | |
9926 | ||
2b188cc1 | 9927 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9928 | u32, min_complete, u32, flags, const void __user *, argp, |
9929 | size_t, argsz) | |
2b188cc1 JA |
9930 | { |
9931 | struct io_ring_ctx *ctx; | |
2b188cc1 JA |
9932 | int submitted = 0; |
9933 | struct fd f; | |
33f993da | 9934 | long ret; |
2b188cc1 | 9935 | |
4c6e277c | 9936 | io_run_task_work(); |
b41e9852 | 9937 | |
33f993da PB |
9938 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
9939 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) | |
2b188cc1 JA |
9940 | return -EINVAL; |
9941 | ||
9942 | f = fdget(fd); | |
33f993da | 9943 | if (unlikely(!f.file)) |
2b188cc1 JA |
9944 | return -EBADF; |
9945 | ||
9946 | ret = -EOPNOTSUPP; | |
33f993da | 9947 | if (unlikely(f.file->f_op != &io_uring_fops)) |
2b188cc1 JA |
9948 | goto out_fput; |
9949 | ||
9950 | ret = -ENXIO; | |
9951 | ctx = f.file->private_data; | |
33f993da | 9952 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
2b188cc1 JA |
9953 | goto out_fput; |
9954 | ||
7e84e1c7 | 9955 | ret = -EBADFD; |
33f993da | 9956 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
9957 | goto out; |
9958 | ||
6c271ce2 JA |
9959 | /* |
9960 | * For SQ polling, the thread will do all submissions and completions. | |
9961 | * Just return the requested submit count, and wake the thread if | |
9962 | * we were asked to. | |
9963 | */ | |
b2a9eada | 9964 | ret = 0; |
6c271ce2 | 9965 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
90f67366 | 9966 | io_cqring_overflow_flush(ctx); |
89448c47 | 9967 | |
21f96522 JA |
9968 | if (unlikely(ctx->sq_data->thread == NULL)) { |
9969 | ret = -EOWNERDEAD; | |
04147488 | 9970 | goto out; |
21f96522 | 9971 | } |
6c271ce2 | 9972 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9973 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9974 | if (flags & IORING_ENTER_SQ_WAIT) { |
9975 | ret = io_sqpoll_wait_sq(ctx); | |
9976 | if (ret) | |
9977 | goto out; | |
9978 | } | |
6c271ce2 | 9979 | submitted = to_submit; |
b2a9eada | 9980 | } else if (to_submit) { |
eef51daa | 9981 | ret = io_uring_add_tctx_node(ctx); |
0f212204 JA |
9982 | if (unlikely(ret)) |
9983 | goto out; | |
2b188cc1 | 9984 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9985 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9986 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9987 | |
9988 | if (submitted != to_submit) | |
9989 | goto out; | |
2b188cc1 JA |
9990 | } |
9991 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9992 | const sigset_t __user *sig; |
9993 | struct __kernel_timespec __user *ts; | |
9994 | ||
9995 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9996 | if (unlikely(ret)) | |
9997 | goto out; | |
9998 | ||
2b188cc1 JA |
9999 | min_complete = min(min_complete, ctx->cq_entries); |
10000 | ||
32b2244a XW |
10001 | /* |
10002 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
10003 | * space applications don't need to do io completion events | |
10004 | * polling again, they can rely on io_sq_thread to do polling | |
10005 | * work, which can reduce cpu usage and uring_lock contention. | |
10006 | */ | |
10007 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
10008 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 10009 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 10010 | } else { |
c73ebb68 | 10011 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 10012 | } |
2b188cc1 JA |
10013 | } |
10014 | ||
7c504e65 | 10015 | out: |
6805b32e | 10016 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
10017 | out_fput: |
10018 | fdput(f); | |
10019 | return submitted ? submitted : ret; | |
10020 | } | |
10021 | ||
bebdb65e | 10022 | #ifdef CONFIG_PROC_FS |
61cf9370 MWO |
10023 | static int io_uring_show_cred(struct seq_file *m, unsigned int id, |
10024 | const struct cred *cred) | |
87ce955b | 10025 | { |
87ce955b JA |
10026 | struct user_namespace *uns = seq_user_ns(m); |
10027 | struct group_info *gi; | |
10028 | kernel_cap_t cap; | |
10029 | unsigned __capi; | |
10030 | int g; | |
10031 | ||
10032 | seq_printf(m, "%5d\n", id); | |
10033 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
10034 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
10035 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
10036 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
10037 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
10038 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
10039 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
10040 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
10041 | seq_puts(m, "\n\tGroups:\t"); | |
10042 | gi = cred->group_info; | |
10043 | for (g = 0; g < gi->ngroups; g++) { | |
10044 | seq_put_decimal_ull(m, g ? " " : "", | |
10045 | from_kgid_munged(uns, gi->gid[g])); | |
10046 | } | |
10047 | seq_puts(m, "\n\tCapEff:\t"); | |
10048 | cap = cred->cap_effective; | |
10049 | CAP_FOR_EACH_U32(__capi) | |
10050 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
10051 | seq_putc(m, '\n'); | |
10052 | return 0; | |
10053 | } | |
10054 | ||
10055 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
10056 | { | |
dbbe9c64 | 10057 | struct io_sq_data *sq = NULL; |
fad8e0de | 10058 | bool has_lock; |
87ce955b JA |
10059 | int i; |
10060 | ||
fad8e0de JA |
10061 | /* |
10062 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
10063 | * since fdinfo case grabs it in the opposite direction of normal use | |
10064 | * cases. If we fail to get the lock, we just don't iterate any | |
10065 | * structures that could be going away outside the io_uring mutex. | |
10066 | */ | |
10067 | has_lock = mutex_trylock(&ctx->uring_lock); | |
10068 | ||
5f3f26f9 | 10069 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 10070 | sq = ctx->sq_data; |
5f3f26f9 JA |
10071 | if (!sq->thread) |
10072 | sq = NULL; | |
10073 | } | |
dbbe9c64 JQ |
10074 | |
10075 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
10076 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 10077 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 10078 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
7b29f92d | 10079 | struct file *f = io_file_from_index(ctx, i); |
87ce955b | 10080 | |
87ce955b JA |
10081 | if (f) |
10082 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
10083 | else | |
10084 | seq_printf(m, "%5u: <none>\n", i); | |
10085 | } | |
10086 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 10087 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
41edf1a5 | 10088 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
4751f53d | 10089 | unsigned int len = buf->ubuf_end - buf->ubuf; |
87ce955b | 10090 | |
4751f53d | 10091 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
87ce955b | 10092 | } |
61cf9370 MWO |
10093 | if (has_lock && !xa_empty(&ctx->personalities)) { |
10094 | unsigned long index; | |
10095 | const struct cred *cred; | |
10096 | ||
87ce955b | 10097 | seq_printf(m, "Personalities:\n"); |
61cf9370 MWO |
10098 | xa_for_each(&ctx->personalities, index, cred) |
10099 | io_uring_show_cred(m, index, cred); | |
87ce955b | 10100 | } |
d7718a9d | 10101 | seq_printf(m, "PollList:\n"); |
79ebeaee | 10102 | spin_lock(&ctx->completion_lock); |
d7718a9d JA |
10103 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
10104 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
10105 | struct io_kiocb *req; | |
10106 | ||
10107 | hlist_for_each_entry(req, list, hash_node) | |
10108 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
10109 | req->task->task_works != NULL); | |
10110 | } | |
79ebeaee | 10111 | spin_unlock(&ctx->completion_lock); |
fad8e0de JA |
10112 | if (has_lock) |
10113 | mutex_unlock(&ctx->uring_lock); | |
87ce955b JA |
10114 | } |
10115 | ||
10116 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
10117 | { | |
10118 | struct io_ring_ctx *ctx = f->private_data; | |
10119 | ||
10120 | if (percpu_ref_tryget(&ctx->refs)) { | |
10121 | __io_uring_show_fdinfo(ctx, m); | |
10122 | percpu_ref_put(&ctx->refs); | |
10123 | } | |
10124 | } | |
bebdb65e | 10125 | #endif |
87ce955b | 10126 | |
2b188cc1 JA |
10127 | static const struct file_operations io_uring_fops = { |
10128 | .release = io_uring_release, | |
10129 | .mmap = io_uring_mmap, | |
6c5c240e RP |
10130 | #ifndef CONFIG_MMU |
10131 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
10132 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
10133 | #endif | |
2b188cc1 | 10134 | .poll = io_uring_poll, |
bebdb65e | 10135 | #ifdef CONFIG_PROC_FS |
87ce955b | 10136 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 10137 | #endif |
2b188cc1 JA |
10138 | }; |
10139 | ||
10140 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
10141 | struct io_uring_params *p) | |
10142 | { | |
75b28aff HV |
10143 | struct io_rings *rings; |
10144 | size_t size, sq_array_offset; | |
2b188cc1 | 10145 | |
bd740481 JA |
10146 | /* make sure these are sane, as we already accounted them */ |
10147 | ctx->sq_entries = p->sq_entries; | |
10148 | ctx->cq_entries = p->cq_entries; | |
10149 | ||
75b28aff HV |
10150 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
10151 | if (size == SIZE_MAX) | |
10152 | return -EOVERFLOW; | |
10153 | ||
10154 | rings = io_mem_alloc(size); | |
10155 | if (!rings) | |
2b188cc1 JA |
10156 | return -ENOMEM; |
10157 | ||
75b28aff HV |
10158 | ctx->rings = rings; |
10159 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
10160 | rings->sq_ring_mask = p->sq_entries - 1; | |
10161 | rings->cq_ring_mask = p->cq_entries - 1; | |
10162 | rings->sq_ring_entries = p->sq_entries; | |
10163 | rings->cq_ring_entries = p->cq_entries; | |
2b188cc1 JA |
10164 | |
10165 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
10166 | if (size == SIZE_MAX) { |
10167 | io_mem_free(ctx->rings); | |
10168 | ctx->rings = NULL; | |
2b188cc1 | 10169 | return -EOVERFLOW; |
eb065d30 | 10170 | } |
2b188cc1 JA |
10171 | |
10172 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
10173 | if (!ctx->sq_sqes) { |
10174 | io_mem_free(ctx->rings); | |
10175 | ctx->rings = NULL; | |
2b188cc1 | 10176 | return -ENOMEM; |
eb065d30 | 10177 | } |
2b188cc1 | 10178 | |
2b188cc1 JA |
10179 | return 0; |
10180 | } | |
10181 | ||
9faadcc8 PB |
10182 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
10183 | { | |
10184 | int ret, fd; | |
10185 | ||
10186 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
10187 | if (fd < 0) | |
10188 | return fd; | |
10189 | ||
eef51daa | 10190 | ret = io_uring_add_tctx_node(ctx); |
9faadcc8 PB |
10191 | if (ret) { |
10192 | put_unused_fd(fd); | |
10193 | return ret; | |
10194 | } | |
10195 | fd_install(fd, file); | |
10196 | return fd; | |
10197 | } | |
10198 | ||
2b188cc1 JA |
10199 | /* |
10200 | * Allocate an anonymous fd, this is what constitutes the application | |
10201 | * visible backing of an io_uring instance. The application mmaps this | |
10202 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
10203 | * we have to tie this fd to a socket for file garbage collection purposes. | |
10204 | */ | |
9faadcc8 | 10205 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
10206 | { |
10207 | struct file *file; | |
9faadcc8 | 10208 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
10209 | int ret; |
10210 | ||
2b188cc1 JA |
10211 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
10212 | &ctx->ring_sock); | |
10213 | if (ret) | |
9faadcc8 | 10214 | return ERR_PTR(ret); |
2b188cc1 JA |
10215 | #endif |
10216 | ||
2b188cc1 JA |
10217 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
10218 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 10219 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
10220 | if (IS_ERR(file)) { |
10221 | sock_release(ctx->ring_sock); | |
10222 | ctx->ring_sock = NULL; | |
10223 | } else { | |
10224 | ctx->ring_sock->file = file; | |
0f212204 | 10225 | } |
2b188cc1 | 10226 | #endif |
9faadcc8 | 10227 | return file; |
2b188cc1 JA |
10228 | } |
10229 | ||
7f13657d XW |
10230 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
10231 | struct io_uring_params __user *params) | |
2b188cc1 | 10232 | { |
2b188cc1 | 10233 | struct io_ring_ctx *ctx; |
9faadcc8 | 10234 | struct file *file; |
2b188cc1 JA |
10235 | int ret; |
10236 | ||
8110c1a6 | 10237 | if (!entries) |
2b188cc1 | 10238 | return -EINVAL; |
8110c1a6 JA |
10239 | if (entries > IORING_MAX_ENTRIES) { |
10240 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
10241 | return -EINVAL; | |
10242 | entries = IORING_MAX_ENTRIES; | |
10243 | } | |
2b188cc1 JA |
10244 | |
10245 | /* | |
10246 | * Use twice as many entries for the CQ ring. It's possible for the | |
10247 | * application to drive a higher depth than the size of the SQ ring, | |
10248 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
10249 | * some flexibility in overcommitting a bit. If the application has |
10250 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
10251 | * of CQ ring entries manually. | |
2b188cc1 JA |
10252 | */ |
10253 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
10254 | if (p->flags & IORING_SETUP_CQSIZE) { |
10255 | /* | |
10256 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
10257 | * to a power-of-two, if it isn't already. We do NOT impose | |
10258 | * any cq vs sq ring sizing. | |
10259 | */ | |
eb2667b3 | 10260 | if (!p->cq_entries) |
33a107f0 | 10261 | return -EINVAL; |
8110c1a6 JA |
10262 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
10263 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
10264 | return -EINVAL; | |
10265 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
10266 | } | |
eb2667b3 JQ |
10267 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
10268 | if (p->cq_entries < p->sq_entries) | |
10269 | return -EINVAL; | |
33a107f0 JA |
10270 | } else { |
10271 | p->cq_entries = 2 * p->sq_entries; | |
10272 | } | |
2b188cc1 | 10273 | |
2b188cc1 | 10274 | ctx = io_ring_ctx_alloc(p); |
62e398be | 10275 | if (!ctx) |
2b188cc1 | 10276 | return -ENOMEM; |
2b188cc1 | 10277 | ctx->compat = in_compat_syscall(); |
62e398be JA |
10278 | if (!capable(CAP_IPC_LOCK)) |
10279 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
10280 | |
10281 | /* | |
10282 | * This is just grabbed for accounting purposes. When a process exits, | |
10283 | * the mm is exited and dropped before the files, hence we need to hang | |
10284 | * on to this mm purely for the purposes of being able to unaccount | |
10285 | * memory (locked/pinned vm). It's not used for anything else. | |
10286 | */ | |
6b7898eb | 10287 | mmgrab(current->mm); |
2aede0e4 | 10288 | ctx->mm_account = current->mm; |
6b7898eb | 10289 | |
2b188cc1 JA |
10290 | ret = io_allocate_scq_urings(ctx, p); |
10291 | if (ret) | |
10292 | goto err; | |
10293 | ||
7e84e1c7 | 10294 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
10295 | if (ret) |
10296 | goto err; | |
eae071c9 | 10297 | /* always set a rsrc node */ |
47b228ce PB |
10298 | ret = io_rsrc_node_switch_start(ctx); |
10299 | if (ret) | |
10300 | goto err; | |
eae071c9 | 10301 | io_rsrc_node_switch(ctx, NULL); |
2b188cc1 | 10302 | |
2b188cc1 | 10303 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
10304 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
10305 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
10306 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
10307 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
10308 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
10309 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
10310 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
10311 | |
10312 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
10313 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
10314 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
10315 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
10316 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
10317 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
10318 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 10319 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 10320 | |
7f13657d XW |
10321 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
10322 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 10323 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 10324 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9690557e PB |
10325 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
10326 | IORING_FEAT_RSRC_TAGS; | |
7f13657d XW |
10327 | |
10328 | if (copy_to_user(params, p, sizeof(*p))) { | |
10329 | ret = -EFAULT; | |
10330 | goto err; | |
10331 | } | |
d1719f70 | 10332 | |
9faadcc8 PB |
10333 | file = io_uring_get_file(ctx); |
10334 | if (IS_ERR(file)) { | |
10335 | ret = PTR_ERR(file); | |
10336 | goto err; | |
10337 | } | |
10338 | ||
044c1ab3 JA |
10339 | /* |
10340 | * Install ring fd as the very last thing, so we don't risk someone | |
10341 | * having closed it before we finish setup | |
10342 | */ | |
9faadcc8 PB |
10343 | ret = io_uring_install_fd(ctx, file); |
10344 | if (ret < 0) { | |
10345 | /* fput will clean it up */ | |
10346 | fput(file); | |
10347 | return ret; | |
10348 | } | |
044c1ab3 | 10349 | |
c826bd7a | 10350 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
10351 | return ret; |
10352 | err: | |
10353 | io_ring_ctx_wait_and_kill(ctx); | |
10354 | return ret; | |
10355 | } | |
10356 | ||
10357 | /* | |
10358 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
10359 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
10360 | * params structure passed in. | |
10361 | */ | |
10362 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
10363 | { | |
10364 | struct io_uring_params p; | |
2b188cc1 JA |
10365 | int i; |
10366 | ||
10367 | if (copy_from_user(&p, params, sizeof(p))) | |
10368 | return -EFAULT; | |
10369 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
10370 | if (p.resv[i]) | |
10371 | return -EINVAL; | |
10372 | } | |
10373 | ||
6c271ce2 | 10374 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 10375 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
10376 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
10377 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
10378 | return -EINVAL; |
10379 | ||
7f13657d | 10380 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
10381 | } |
10382 | ||
10383 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
10384 | struct io_uring_params __user *, params) | |
10385 | { | |
10386 | return io_uring_setup(entries, params); | |
10387 | } | |
10388 | ||
66f4af93 JA |
10389 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
10390 | { | |
10391 | struct io_uring_probe *p; | |
10392 | size_t size; | |
10393 | int i, ret; | |
10394 | ||
10395 | size = struct_size(p, ops, nr_args); | |
10396 | if (size == SIZE_MAX) | |
10397 | return -EOVERFLOW; | |
10398 | p = kzalloc(size, GFP_KERNEL); | |
10399 | if (!p) | |
10400 | return -ENOMEM; | |
10401 | ||
10402 | ret = -EFAULT; | |
10403 | if (copy_from_user(p, arg, size)) | |
10404 | goto out; | |
10405 | ret = -EINVAL; | |
10406 | if (memchr_inv(p, 0, size)) | |
10407 | goto out; | |
10408 | ||
10409 | p->last_op = IORING_OP_LAST - 1; | |
10410 | if (nr_args > IORING_OP_LAST) | |
10411 | nr_args = IORING_OP_LAST; | |
10412 | ||
10413 | for (i = 0; i < nr_args; i++) { | |
10414 | p->ops[i].op = i; | |
10415 | if (!io_op_defs[i].not_supported) | |
10416 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
10417 | } | |
10418 | p->ops_len = i; | |
10419 | ||
10420 | ret = 0; | |
10421 | if (copy_to_user(arg, p, size)) | |
10422 | ret = -EFAULT; | |
10423 | out: | |
10424 | kfree(p); | |
10425 | return ret; | |
10426 | } | |
10427 | ||
071698e1 JA |
10428 | static int io_register_personality(struct io_ring_ctx *ctx) |
10429 | { | |
4379bf8b | 10430 | const struct cred *creds; |
61cf9370 | 10431 | u32 id; |
1e6fa521 | 10432 | int ret; |
071698e1 | 10433 | |
4379bf8b | 10434 | creds = get_current_cred(); |
1e6fa521 | 10435 | |
61cf9370 MWO |
10436 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
10437 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); | |
a30f895a JA |
10438 | if (ret < 0) { |
10439 | put_cred(creds); | |
10440 | return ret; | |
10441 | } | |
10442 | return id; | |
071698e1 JA |
10443 | } |
10444 | ||
21b55dbc SG |
10445 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
10446 | unsigned int nr_args) | |
10447 | { | |
10448 | struct io_uring_restriction *res; | |
10449 | size_t size; | |
10450 | int i, ret; | |
10451 | ||
7e84e1c7 SG |
10452 | /* Restrictions allowed only if rings started disabled */ |
10453 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10454 | return -EBADFD; | |
10455 | ||
21b55dbc | 10456 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 10457 | if (ctx->restrictions.registered) |
21b55dbc SG |
10458 | return -EBUSY; |
10459 | ||
10460 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
10461 | return -EINVAL; | |
10462 | ||
10463 | size = array_size(nr_args, sizeof(*res)); | |
10464 | if (size == SIZE_MAX) | |
10465 | return -EOVERFLOW; | |
10466 | ||
10467 | res = memdup_user(arg, size); | |
10468 | if (IS_ERR(res)) | |
10469 | return PTR_ERR(res); | |
10470 | ||
10471 | ret = 0; | |
10472 | ||
10473 | for (i = 0; i < nr_args; i++) { | |
10474 | switch (res[i].opcode) { | |
10475 | case IORING_RESTRICTION_REGISTER_OP: | |
10476 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
10477 | ret = -EINVAL; | |
10478 | goto out; | |
10479 | } | |
10480 | ||
10481 | __set_bit(res[i].register_op, | |
10482 | ctx->restrictions.register_op); | |
10483 | break; | |
10484 | case IORING_RESTRICTION_SQE_OP: | |
10485 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
10486 | ret = -EINVAL; | |
10487 | goto out; | |
10488 | } | |
10489 | ||
10490 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
10491 | break; | |
10492 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
10493 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
10494 | break; | |
10495 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
10496 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
10497 | break; | |
10498 | default: | |
10499 | ret = -EINVAL; | |
10500 | goto out; | |
10501 | } | |
10502 | } | |
10503 | ||
10504 | out: | |
10505 | /* Reset all restrictions if an error happened */ | |
10506 | if (ret != 0) | |
10507 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
10508 | else | |
7e84e1c7 | 10509 | ctx->restrictions.registered = true; |
21b55dbc SG |
10510 | |
10511 | kfree(res); | |
10512 | return ret; | |
10513 | } | |
10514 | ||
7e84e1c7 SG |
10515 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
10516 | { | |
10517 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10518 | return -EBADFD; | |
10519 | ||
10520 | if (ctx->restrictions.registered) | |
10521 | ctx->restricted = 1; | |
10522 | ||
0298ef96 PB |
10523 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
10524 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) | |
10525 | wake_up(&ctx->sq_data->wait); | |
7e84e1c7 SG |
10526 | return 0; |
10527 | } | |
10528 | ||
fdecb662 | 10529 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 10530 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 PB |
10531 | unsigned nr_args) |
10532 | { | |
10533 | __u32 tmp; | |
10534 | int err; | |
10535 | ||
10536 | if (check_add_overflow(up->offset, nr_args, &tmp)) | |
10537 | return -EOVERFLOW; | |
10538 | err = io_rsrc_node_switch_start(ctx); | |
10539 | if (err) | |
10540 | return err; | |
10541 | ||
fdecb662 PB |
10542 | switch (type) { |
10543 | case IORING_RSRC_FILE: | |
98f0b3b4 | 10544 | return __io_sqe_files_update(ctx, up, nr_args); |
634d00df PB |
10545 | case IORING_RSRC_BUFFER: |
10546 | return __io_sqe_buffers_update(ctx, up, nr_args); | |
98f0b3b4 PB |
10547 | } |
10548 | return -EINVAL; | |
10549 | } | |
10550 | ||
c3bdad02 PB |
10551 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
10552 | unsigned nr_args) | |
98f0b3b4 | 10553 | { |
c3bdad02 | 10554 | struct io_uring_rsrc_update2 up; |
98f0b3b4 PB |
10555 | |
10556 | if (!nr_args) | |
10557 | return -EINVAL; | |
c3bdad02 PB |
10558 | memset(&up, 0, sizeof(up)); |
10559 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) | |
10560 | return -EFAULT; | |
c9747fa5 | 10561 | if (up.resv || up.resv2) |
4dd8f4c2 | 10562 | return -EINVAL; |
c3bdad02 PB |
10563 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); |
10564 | } | |
10565 | ||
10566 | static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
992da01a | 10567 | unsigned size, unsigned type) |
c3bdad02 PB |
10568 | { |
10569 | struct io_uring_rsrc_update2 up; | |
10570 | ||
10571 | if (size != sizeof(up)) | |
10572 | return -EINVAL; | |
98f0b3b4 PB |
10573 | if (copy_from_user(&up, arg, sizeof(up))) |
10574 | return -EFAULT; | |
c9747fa5 | 10575 | if (!up.nr || up.resv || up.resv2) |
98f0b3b4 | 10576 | return -EINVAL; |
992da01a | 10577 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
98f0b3b4 PB |
10578 | } |
10579 | ||
792e3582 | 10580 | static int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
992da01a | 10581 | unsigned int size, unsigned int type) |
792e3582 PB |
10582 | { |
10583 | struct io_uring_rsrc_register rr; | |
10584 | ||
10585 | /* keep it extendible */ | |
10586 | if (size != sizeof(rr)) | |
10587 | return -EINVAL; | |
10588 | ||
10589 | memset(&rr, 0, sizeof(rr)); | |
10590 | if (copy_from_user(&rr, arg, size)) | |
10591 | return -EFAULT; | |
992da01a | 10592 | if (!rr.nr || rr.resv || rr.resv2) |
792e3582 PB |
10593 | return -EINVAL; |
10594 | ||
992da01a | 10595 | switch (type) { |
792e3582 PB |
10596 | case IORING_RSRC_FILE: |
10597 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), | |
10598 | rr.nr, u64_to_user_ptr(rr.tags)); | |
634d00df PB |
10599 | case IORING_RSRC_BUFFER: |
10600 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), | |
10601 | rr.nr, u64_to_user_ptr(rr.tags)); | |
792e3582 PB |
10602 | } |
10603 | return -EINVAL; | |
10604 | } | |
10605 | ||
fe76421d JA |
10606 | static int io_register_iowq_aff(struct io_ring_ctx *ctx, void __user *arg, |
10607 | unsigned len) | |
10608 | { | |
10609 | struct io_uring_task *tctx = current->io_uring; | |
10610 | cpumask_var_t new_mask; | |
10611 | int ret; | |
10612 | ||
10613 | if (!tctx || !tctx->io_wq) | |
10614 | return -EINVAL; | |
10615 | ||
10616 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | |
10617 | return -ENOMEM; | |
10618 | ||
10619 | cpumask_clear(new_mask); | |
10620 | if (len > cpumask_size()) | |
10621 | len = cpumask_size(); | |
10622 | ||
fd213661 ES |
10623 | if (in_compat_syscall()) { |
10624 | ret = compat_get_bitmap(cpumask_bits(new_mask), | |
10625 | (const compat_ulong_t __user *)arg, | |
10626 | len * 8 /* CHAR_BIT */); | |
10627 | } else { | |
10628 | ret = copy_from_user(new_mask, arg, len); | |
10629 | } | |
10630 | ||
10631 | if (ret) { | |
fe76421d JA |
10632 | free_cpumask_var(new_mask); |
10633 | return -EFAULT; | |
10634 | } | |
10635 | ||
10636 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); | |
10637 | free_cpumask_var(new_mask); | |
10638 | return ret; | |
10639 | } | |
10640 | ||
10641 | static int io_unregister_iowq_aff(struct io_ring_ctx *ctx) | |
10642 | { | |
10643 | struct io_uring_task *tctx = current->io_uring; | |
10644 | ||
10645 | if (!tctx || !tctx->io_wq) | |
10646 | return -EINVAL; | |
10647 | ||
10648 | return io_wq_cpu_affinity(tctx->io_wq, NULL); | |
10649 | } | |
10650 | ||
2e480058 JA |
10651 | static int io_register_iowq_max_workers(struct io_ring_ctx *ctx, |
10652 | void __user *arg) | |
b22fa62a | 10653 | __must_hold(&ctx->uring_lock) |
2e480058 | 10654 | { |
b22fa62a | 10655 | struct io_tctx_node *node; |
fa84693b JA |
10656 | struct io_uring_task *tctx = NULL; |
10657 | struct io_sq_data *sqd = NULL; | |
2e480058 JA |
10658 | __u32 new_count[2]; |
10659 | int i, ret; | |
10660 | ||
2e480058 JA |
10661 | if (copy_from_user(new_count, arg, sizeof(new_count))) |
10662 | return -EFAULT; | |
10663 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
10664 | if (new_count[i] > INT_MAX) | |
10665 | return -EINVAL; | |
10666 | ||
fa84693b JA |
10667 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
10668 | sqd = ctx->sq_data; | |
10669 | if (sqd) { | |
009ad9f0 JA |
10670 | /* |
10671 | * Observe the correct sqd->lock -> ctx->uring_lock | |
10672 | * ordering. Fine to drop uring_lock here, we hold | |
10673 | * a ref to the ctx. | |
10674 | */ | |
41d3a6bd | 10675 | refcount_inc(&sqd->refs); |
009ad9f0 | 10676 | mutex_unlock(&ctx->uring_lock); |
fa84693b | 10677 | mutex_lock(&sqd->lock); |
009ad9f0 | 10678 | mutex_lock(&ctx->uring_lock); |
41d3a6bd JA |
10679 | if (sqd->thread) |
10680 | tctx = sqd->thread->io_uring; | |
fa84693b JA |
10681 | } |
10682 | } else { | |
10683 | tctx = current->io_uring; | |
10684 | } | |
10685 | ||
e139a1ec | 10686 | BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits)); |
fa84693b | 10687 | |
c29d963b PB |
10688 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
10689 | if (new_count[i]) | |
10690 | ctx->iowq_limits[i] = new_count[i]; | |
e139a1ec PB |
10691 | ctx->iowq_limits_set = true; |
10692 | ||
10693 | ret = -EINVAL; | |
10694 | if (tctx && tctx->io_wq) { | |
10695 | ret = io_wq_max_workers(tctx->io_wq, new_count); | |
10696 | if (ret) | |
10697 | goto err; | |
10698 | } else { | |
10699 | memset(new_count, 0, sizeof(new_count)); | |
10700 | } | |
fa84693b | 10701 | |
41d3a6bd | 10702 | if (sqd) { |
fa84693b | 10703 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
10704 | io_put_sq_data(sqd); |
10705 | } | |
2e480058 JA |
10706 | |
10707 | if (copy_to_user(arg, new_count, sizeof(new_count))) | |
10708 | return -EFAULT; | |
10709 | ||
b22fa62a PB |
10710 | /* that's it for SQPOLL, only the SQPOLL task creates requests */ |
10711 | if (sqd) | |
10712 | return 0; | |
10713 | ||
10714 | /* now propagate the restriction to all registered users */ | |
10715 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
10716 | struct io_uring_task *tctx = node->task->io_uring; | |
10717 | ||
10718 | if (WARN_ON_ONCE(!tctx->io_wq)) | |
10719 | continue; | |
10720 | ||
10721 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
10722 | new_count[i] = ctx->iowq_limits[i]; | |
10723 | /* ignore errors, it always returns zero anyway */ | |
10724 | (void)io_wq_max_workers(tctx->io_wq, new_count); | |
10725 | } | |
2e480058 | 10726 | return 0; |
fa84693b | 10727 | err: |
41d3a6bd | 10728 | if (sqd) { |
fa84693b | 10729 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
10730 | io_put_sq_data(sqd); |
10731 | } | |
fa84693b | 10732 | return ret; |
2e480058 JA |
10733 | } |
10734 | ||
071698e1 JA |
10735 | static bool io_register_op_must_quiesce(int op) |
10736 | { | |
10737 | switch (op) { | |
bd54b6fe BM |
10738 | case IORING_REGISTER_BUFFERS: |
10739 | case IORING_UNREGISTER_BUFFERS: | |
f4f7d21c | 10740 | case IORING_REGISTER_FILES: |
071698e1 JA |
10741 | case IORING_UNREGISTER_FILES: |
10742 | case IORING_REGISTER_FILES_UPDATE: | |
10743 | case IORING_REGISTER_PROBE: | |
10744 | case IORING_REGISTER_PERSONALITY: | |
10745 | case IORING_UNREGISTER_PERSONALITY: | |
992da01a PB |
10746 | case IORING_REGISTER_FILES2: |
10747 | case IORING_REGISTER_FILES_UPDATE2: | |
10748 | case IORING_REGISTER_BUFFERS2: | |
10749 | case IORING_REGISTER_BUFFERS_UPDATE: | |
fe76421d JA |
10750 | case IORING_REGISTER_IOWQ_AFF: |
10751 | case IORING_UNREGISTER_IOWQ_AFF: | |
2e480058 | 10752 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
071698e1 JA |
10753 | return false; |
10754 | default: | |
10755 | return true; | |
10756 | } | |
10757 | } | |
10758 | ||
e73c5c7c PB |
10759 | static int io_ctx_quiesce(struct io_ring_ctx *ctx) |
10760 | { | |
10761 | long ret; | |
10762 | ||
10763 | percpu_ref_kill(&ctx->refs); | |
10764 | ||
10765 | /* | |
10766 | * Drop uring mutex before waiting for references to exit. If another | |
10767 | * thread is currently inside io_uring_enter() it might need to grab the | |
10768 | * uring_lock to make progress. If we hold it here across the drain | |
10769 | * wait, then we can deadlock. It's safe to drop the mutex here, since | |
10770 | * no new references will come in after we've killed the percpu ref. | |
10771 | */ | |
10772 | mutex_unlock(&ctx->uring_lock); | |
10773 | do { | |
10774 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
10775 | if (!ret) | |
10776 | break; | |
10777 | ret = io_run_task_work_sig(); | |
10778 | } while (ret >= 0); | |
10779 | mutex_lock(&ctx->uring_lock); | |
10780 | ||
10781 | if (ret) | |
10782 | io_refs_resurrect(&ctx->refs, &ctx->ref_comp); | |
10783 | return ret; | |
10784 | } | |
10785 | ||
edafccee JA |
10786 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
10787 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
10788 | __releases(ctx->uring_lock) |
10789 | __acquires(ctx->uring_lock) | |
edafccee JA |
10790 | { |
10791 | int ret; | |
10792 | ||
35fa71a0 JA |
10793 | /* |
10794 | * We're inside the ring mutex, if the ref is already dying, then | |
10795 | * someone else killed the ctx or is already going through | |
10796 | * io_uring_register(). | |
10797 | */ | |
10798 | if (percpu_ref_is_dying(&ctx->refs)) | |
10799 | return -ENXIO; | |
10800 | ||
75c4021a PB |
10801 | if (ctx->restricted) { |
10802 | if (opcode >= IORING_REGISTER_LAST) | |
10803 | return -EINVAL; | |
10804 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); | |
10805 | if (!test_bit(opcode, ctx->restrictions.register_op)) | |
10806 | return -EACCES; | |
10807 | } | |
10808 | ||
071698e1 | 10809 | if (io_register_op_must_quiesce(opcode)) { |
e73c5c7c PB |
10810 | ret = io_ctx_quiesce(ctx); |
10811 | if (ret) | |
f70865db | 10812 | return ret; |
05f3fb3c | 10813 | } |
edafccee JA |
10814 | |
10815 | switch (opcode) { | |
10816 | case IORING_REGISTER_BUFFERS: | |
634d00df | 10817 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
edafccee JA |
10818 | break; |
10819 | case IORING_UNREGISTER_BUFFERS: | |
10820 | ret = -EINVAL; | |
10821 | if (arg || nr_args) | |
10822 | break; | |
0a96bbe4 | 10823 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 10824 | break; |
6b06314c | 10825 | case IORING_REGISTER_FILES: |
792e3582 | 10826 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
6b06314c JA |
10827 | break; |
10828 | case IORING_UNREGISTER_FILES: | |
10829 | ret = -EINVAL; | |
10830 | if (arg || nr_args) | |
10831 | break; | |
10832 | ret = io_sqe_files_unregister(ctx); | |
10833 | break; | |
c3a31e60 | 10834 | case IORING_REGISTER_FILES_UPDATE: |
c3bdad02 | 10835 | ret = io_register_files_update(ctx, arg, nr_args); |
c3a31e60 | 10836 | break; |
9b402849 | 10837 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 10838 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
10839 | ret = -EINVAL; |
10840 | if (nr_args != 1) | |
10841 | break; | |
10842 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
10843 | if (ret) |
10844 | break; | |
10845 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
10846 | ctx->eventfd_async = 1; | |
10847 | else | |
10848 | ctx->eventfd_async = 0; | |
9b402849 JA |
10849 | break; |
10850 | case IORING_UNREGISTER_EVENTFD: | |
10851 | ret = -EINVAL; | |
10852 | if (arg || nr_args) | |
10853 | break; | |
10854 | ret = io_eventfd_unregister(ctx); | |
10855 | break; | |
66f4af93 JA |
10856 | case IORING_REGISTER_PROBE: |
10857 | ret = -EINVAL; | |
10858 | if (!arg || nr_args > 256) | |
10859 | break; | |
10860 | ret = io_probe(ctx, arg, nr_args); | |
10861 | break; | |
071698e1 JA |
10862 | case IORING_REGISTER_PERSONALITY: |
10863 | ret = -EINVAL; | |
10864 | if (arg || nr_args) | |
10865 | break; | |
10866 | ret = io_register_personality(ctx); | |
10867 | break; | |
10868 | case IORING_UNREGISTER_PERSONALITY: | |
10869 | ret = -EINVAL; | |
10870 | if (arg) | |
10871 | break; | |
10872 | ret = io_unregister_personality(ctx, nr_args); | |
10873 | break; | |
7e84e1c7 SG |
10874 | case IORING_REGISTER_ENABLE_RINGS: |
10875 | ret = -EINVAL; | |
10876 | if (arg || nr_args) | |
10877 | break; | |
10878 | ret = io_register_enable_rings(ctx); | |
10879 | break; | |
21b55dbc SG |
10880 | case IORING_REGISTER_RESTRICTIONS: |
10881 | ret = io_register_restrictions(ctx, arg, nr_args); | |
10882 | break; | |
992da01a PB |
10883 | case IORING_REGISTER_FILES2: |
10884 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); | |
10885 | break; | |
10886 | case IORING_REGISTER_FILES_UPDATE2: | |
10887 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
10888 | IORING_RSRC_FILE); | |
10889 | break; | |
10890 | case IORING_REGISTER_BUFFERS2: | |
10891 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); | |
792e3582 | 10892 | break; |
992da01a PB |
10893 | case IORING_REGISTER_BUFFERS_UPDATE: |
10894 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
10895 | IORING_RSRC_BUFFER); | |
c3bdad02 | 10896 | break; |
fe76421d JA |
10897 | case IORING_REGISTER_IOWQ_AFF: |
10898 | ret = -EINVAL; | |
10899 | if (!arg || !nr_args) | |
10900 | break; | |
10901 | ret = io_register_iowq_aff(ctx, arg, nr_args); | |
10902 | break; | |
10903 | case IORING_UNREGISTER_IOWQ_AFF: | |
10904 | ret = -EINVAL; | |
10905 | if (arg || nr_args) | |
10906 | break; | |
10907 | ret = io_unregister_iowq_aff(ctx); | |
10908 | break; | |
2e480058 JA |
10909 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
10910 | ret = -EINVAL; | |
10911 | if (!arg || nr_args != 2) | |
10912 | break; | |
10913 | ret = io_register_iowq_max_workers(ctx, arg); | |
10914 | break; | |
edafccee JA |
10915 | default: |
10916 | ret = -EINVAL; | |
10917 | break; | |
10918 | } | |
10919 | ||
071698e1 | 10920 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 10921 | /* bring the ctx back to life */ |
05f3fb3c | 10922 | percpu_ref_reinit(&ctx->refs); |
0f158b4c | 10923 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 10924 | } |
edafccee JA |
10925 | return ret; |
10926 | } | |
10927 | ||
10928 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
10929 | void __user *, arg, unsigned int, nr_args) | |
10930 | { | |
10931 | struct io_ring_ctx *ctx; | |
10932 | long ret = -EBADF; | |
10933 | struct fd f; | |
10934 | ||
10935 | f = fdget(fd); | |
10936 | if (!f.file) | |
10937 | return -EBADF; | |
10938 | ||
10939 | ret = -EOPNOTSUPP; | |
10940 | if (f.file->f_op != &io_uring_fops) | |
10941 | goto out_fput; | |
10942 | ||
10943 | ctx = f.file->private_data; | |
10944 | ||
b6c23dd5 PB |
10945 | io_run_task_work(); |
10946 | ||
edafccee JA |
10947 | mutex_lock(&ctx->uring_lock); |
10948 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
10949 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
10950 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
10951 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
10952 | out_fput: |
10953 | fdput(f); | |
10954 | return ret; | |
10955 | } | |
10956 | ||
2b188cc1 JA |
10957 | static int __init io_uring_init(void) |
10958 | { | |
d7f62e82 SM |
10959 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
10960 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
10961 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
10962 | } while (0) | |
10963 | ||
10964 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
10965 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
10966 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
10967 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
10968 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
10969 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
10970 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
10971 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
10972 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
10973 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 10974 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
10975 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
10976 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
10977 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
10978 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
10979 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
10980 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
10981 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
10982 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
10983 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
10984 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
10985 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
10986 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
10987 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
10988 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
10989 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 10990 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
10991 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
10992 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
16340eab | 10993 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
d7f62e82 | 10994 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
7d67af2c | 10995 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
b9445598 | 10996 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
d7f62e82 | 10997 | |
b0d658ec PB |
10998 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
10999 | sizeof(struct io_uring_rsrc_update)); | |
11000 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > | |
11001 | sizeof(struct io_uring_rsrc_update2)); | |
90499ad0 PB |
11002 | |
11003 | /* ->buf_index is u16 */ | |
11004 | BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); | |
11005 | ||
b0d658ec PB |
11006 | /* should fit into one byte */ |
11007 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); | |
11008 | ||
d3656344 | 11009 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
32c2d33e | 11010 | BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int)); |
16340eab | 11011 | |
91f245d5 JA |
11012 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
11013 | SLAB_ACCOUNT); | |
2b188cc1 JA |
11014 | return 0; |
11015 | }; | |
11016 | __initcall(io_uring_init); |