]>
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 PB |
5316 | #define IO_POLL_CANCEL_FLAG BIT(31) |
5317 | #define IO_POLL_REF_MASK ((1u << 20)-1) | |
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 */ | |
5630 | if (unlikely(ipt->error || !ipt->nr_entries)) | |
5631 | poll->events |= EPOLLONESHOT; | |
5632 | __io_poll_execute(req, mask); | |
5633 | return 0; | |
d7718a9d JA |
5634 | } |
5635 | ||
350804e3 PB |
5636 | /* |
5637 | * Release ownership. If someone tried to queue a tw while it was | |
5638 | * locked, kick it off for them. | |
5639 | */ | |
5640 | v = atomic_dec_return(&req->poll_refs); | |
5641 | if (unlikely(v & IO_POLL_REF_MASK)) | |
5642 | __io_poll_execute(req, 0); | |
5643 | return 0; | |
5644 | } | |
5645 | ||
5646 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5647 | struct poll_table_struct *p) | |
5648 | { | |
5649 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5650 | struct async_poll *apoll = pt->req->apoll; | |
5651 | ||
5652 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); | |
d7718a9d JA |
5653 | } |
5654 | ||
59b735ae OL |
5655 | enum { |
5656 | IO_APOLL_OK, | |
5657 | IO_APOLL_ABORTED, | |
5658 | IO_APOLL_READY | |
5659 | }; | |
5660 | ||
5661 | static int io_arm_poll_handler(struct io_kiocb *req) | |
d7718a9d JA |
5662 | { |
5663 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5664 | struct io_ring_ctx *ctx = req->ctx; | |
5665 | struct async_poll *apoll; | |
5666 | struct io_poll_table ipt; | |
350804e3 PB |
5667 | __poll_t mask = EPOLLONESHOT | POLLERR | POLLPRI; |
5668 | int ret; | |
d7718a9d JA |
5669 | |
5670 | if (!req->file || !file_can_poll(req->file)) | |
59b735ae | 5671 | return IO_APOLL_ABORTED; |
24c74678 | 5672 | if (req->flags & REQ_F_POLLED) |
59b735ae | 5673 | return IO_APOLL_ABORTED; |
b2d9c3da PB |
5674 | if (!def->pollin && !def->pollout) |
5675 | return IO_APOLL_ABORTED; | |
5676 | ||
5677 | if (def->pollin) { | |
b2d9c3da PB |
5678 | mask |= POLLIN | POLLRDNORM; |
5679 | ||
5680 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5681 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5682 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5683 | mask &= ~POLLIN; | |
5684 | } else { | |
b2d9c3da PB |
5685 | mask |= POLLOUT | POLLWRNORM; |
5686 | } | |
5687 | ||
d7718a9d JA |
5688 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
5689 | if (unlikely(!apoll)) | |
59b735ae | 5690 | return IO_APOLL_ABORTED; |
807abcb0 | 5691 | apoll->double_poll = NULL; |
d7718a9d | 5692 | req->apoll = apoll; |
b2d9c3da | 5693 | req->flags |= REQ_F_POLLED; |
d7718a9d JA |
5694 | ipt.pt._qproc = io_async_queue_proc; |
5695 | ||
350804e3 | 5696 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask); |
41a5169c HX |
5697 | if (ret || ipt.error) |
5698 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; | |
5699 | ||
236daeae OL |
5700 | trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data, |
5701 | mask, apoll->poll.events); | |
59b735ae | 5702 | return IO_APOLL_OK; |
d7718a9d JA |
5703 | } |
5704 | ||
76e1b642 JA |
5705 | /* |
5706 | * Returns true if we found and killed one or more poll requests | |
5707 | */ | |
6b81928d | 5708 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
3dd0c97a | 5709 | bool cancel_all) |
221c5eb2 | 5710 | { |
78076bb6 | 5711 | struct hlist_node *tmp; |
221c5eb2 | 5712 | struct io_kiocb *req; |
350804e3 PB |
5713 | bool found = false; |
5714 | int i; | |
221c5eb2 | 5715 | |
79ebeaee | 5716 | spin_lock(&ctx->completion_lock); |
78076bb6 JA |
5717 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5718 | struct hlist_head *list; | |
5719 | ||
5720 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5721 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
350804e3 PB |
5722 | if (io_match_task_safe(req, tsk, cancel_all)) { |
5723 | io_poll_cancel_req(req); | |
5724 | found = true; | |
5725 | } | |
f3606e3a | 5726 | } |
221c5eb2 | 5727 | } |
79ebeaee | 5728 | spin_unlock(&ctx->completion_lock); |
350804e3 | 5729 | return found; |
221c5eb2 JA |
5730 | } |
5731 | ||
9ba5fac8 PB |
5732 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5733 | bool poll_only) | |
e07785b0 | 5734 | __must_hold(&ctx->completion_lock) |
47f46768 | 5735 | { |
78076bb6 | 5736 | struct hlist_head *list; |
47f46768 JA |
5737 | struct io_kiocb *req; |
5738 | ||
78076bb6 JA |
5739 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5740 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5741 | if (sqe_addr != req->user_data) |
5742 | continue; | |
9ba5fac8 PB |
5743 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
5744 | continue; | |
b2cb805f | 5745 | return req; |
47f46768 | 5746 | } |
b2cb805f JA |
5747 | return NULL; |
5748 | } | |
5749 | ||
350804e3 PB |
5750 | static bool io_poll_disarm(struct io_kiocb *req) |
5751 | __must_hold(&ctx->completion_lock) | |
5752 | { | |
5753 | if (!io_poll_get_ownership(req)) | |
5754 | return false; | |
5755 | io_poll_remove_entries(req); | |
5756 | hash_del(&req->hash_node); | |
5757 | return true; | |
5758 | } | |
5759 | ||
9ba5fac8 PB |
5760 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5761 | bool poll_only) | |
e07785b0 | 5762 | __must_hold(&ctx->completion_lock) |
b2cb805f | 5763 | { |
350804e3 | 5764 | struct io_kiocb *req = io_poll_find(ctx, sqe_addr, poll_only); |
b2cb805f | 5765 | |
b2cb805f JA |
5766 | if (!req) |
5767 | return -ENOENT; | |
350804e3 PB |
5768 | io_poll_cancel_req(req); |
5769 | return 0; | |
47f46768 JA |
5770 | } |
5771 | ||
9096af3e PB |
5772 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
5773 | unsigned int flags) | |
5774 | { | |
5775 | u32 events; | |
47f46768 | 5776 | |
9096af3e PB |
5777 | events = READ_ONCE(sqe->poll32_events); |
5778 | #ifdef __BIG_ENDIAN | |
5779 | events = swahw32(events); | |
5780 | #endif | |
5781 | if (!(flags & IORING_POLL_ADD_MULTI)) | |
5782 | events |= EPOLLONESHOT; | |
5783 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); | |
47f46768 JA |
5784 | } |
5785 | ||
c5de0036 | 5786 | static int io_poll_update_prep(struct io_kiocb *req, |
3529d8c2 | 5787 | const struct io_uring_sqe *sqe) |
0969e783 | 5788 | { |
c5de0036 PB |
5789 | struct io_poll_update *upd = &req->poll_update; |
5790 | u32 flags; | |
5791 | ||
0969e783 JA |
5792 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5793 | return -EINVAL; | |
26578cda | 5794 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
c5de0036 PB |
5795 | return -EINVAL; |
5796 | flags = READ_ONCE(sqe->len); | |
5797 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | | |
5798 | IORING_POLL_ADD_MULTI)) | |
5799 | return -EINVAL; | |
5800 | /* meaningless without update */ | |
5801 | if (flags == IORING_POLL_ADD_MULTI) | |
0969e783 JA |
5802 | return -EINVAL; |
5803 | ||
c5de0036 PB |
5804 | upd->old_user_data = READ_ONCE(sqe->addr); |
5805 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; | |
5806 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; | |
221c5eb2 | 5807 | |
c5de0036 PB |
5808 | upd->new_user_data = READ_ONCE(sqe->off); |
5809 | if (!upd->update_user_data && upd->new_user_data) | |
5810 | return -EINVAL; | |
5811 | if (upd->update_events) | |
5812 | upd->events = io_poll_parse_events(sqe, flags); | |
5813 | else if (sqe->poll32_events) | |
5814 | return -EINVAL; | |
221c5eb2 | 5815 | |
221c5eb2 JA |
5816 | return 0; |
5817 | } | |
5818 | ||
3529d8c2 | 5819 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5820 | { |
5821 | struct io_poll_iocb *poll = &req->poll; | |
c5de0036 | 5822 | u32 flags; |
221c5eb2 JA |
5823 | |
5824 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5825 | return -EINVAL; | |
c5de0036 | 5826 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
88e41cf9 JA |
5827 | return -EINVAL; |
5828 | flags = READ_ONCE(sqe->len); | |
c5de0036 | 5829 | if (flags & ~IORING_POLL_ADD_MULTI) |
221c5eb2 JA |
5830 | return -EINVAL; |
5831 | ||
48dcd38d | 5832 | io_req_set_refcount(req); |
c5de0036 | 5833 | poll->events = io_poll_parse_events(sqe, flags); |
0969e783 JA |
5834 | return 0; |
5835 | } | |
5836 | ||
61e98203 | 5837 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
5838 | { |
5839 | struct io_poll_iocb *poll = &req->poll; | |
0969e783 | 5840 | struct io_poll_table ipt; |
350804e3 | 5841 | int ret; |
0969e783 | 5842 | |
d7718a9d | 5843 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5844 | |
350804e3 PB |
5845 | ret = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events); |
5846 | ret = ret ?: ipt.error; | |
5847 | if (ret) | |
5848 | __io_req_complete(req, issue_flags, ret, 0); | |
5849 | return 0; | |
221c5eb2 JA |
5850 | } |
5851 | ||
c5de0036 | 5852 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
b69de288 JA |
5853 | { |
5854 | struct io_ring_ctx *ctx = req->ctx; | |
5855 | struct io_kiocb *preq; | |
448e6c46 | 5856 | int ret2, ret = 0; |
b69de288 | 5857 | |
79ebeaee | 5858 | spin_lock(&ctx->completion_lock); |
9ba5fac8 | 5859 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
350804e3 | 5860 | if (!preq || !io_poll_disarm(preq)) { |
448e6c46 | 5861 | spin_unlock(&ctx->completion_lock); |
350804e3 | 5862 | ret = preq ? -EALREADY : -ENOENT; |
448e6c46 | 5863 | goto out; |
c5de0036 | 5864 | } |
79ebeaee | 5865 | spin_unlock(&ctx->completion_lock); |
cb3b200e | 5866 | |
448e6c46 PB |
5867 | if (req->poll_update.update_events || req->poll_update.update_user_data) { |
5868 | /* only mask one event flags, keep behavior flags */ | |
5869 | if (req->poll_update.update_events) { | |
5870 | preq->poll.events &= ~0xffff; | |
5871 | preq->poll.events |= req->poll_update.events & 0xffff; | |
5872 | preq->poll.events |= IO_POLL_UNMASK; | |
cb3b200e | 5873 | } |
448e6c46 PB |
5874 | if (req->poll_update.update_user_data) |
5875 | preq->user_data = req->poll_update.new_user_data; | |
5876 | ||
5877 | ret2 = io_poll_add(preq, issue_flags); | |
5878 | /* successfully updated, don't complete poll request */ | |
5879 | if (!ret2) | |
5880 | goto out; | |
b69de288 | 5881 | } |
448e6c46 PB |
5882 | req_set_fail(preq); |
5883 | io_req_complete(preq, -ECANCELED); | |
5884 | out: | |
5885 | if (ret < 0) | |
5886 | req_set_fail(req); | |
5887 | /* complete update request, we're done with it */ | |
5888 | io_req_complete(req, ret); | |
b69de288 JA |
5889 | return 0; |
5890 | } | |
5891 | ||
f237c30a | 5892 | static void io_req_task_timeout(struct io_kiocb *req, bool *locked) |
89850fce | 5893 | { |
89850fce | 5894 | req_set_fail(req); |
505657bc | 5895 | io_req_complete_post(req, -ETIME, 0); |
89850fce JA |
5896 | } |
5897 | ||
5262f567 JA |
5898 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5899 | { | |
ad8a48ac JA |
5900 | struct io_timeout_data *data = container_of(timer, |
5901 | struct io_timeout_data, timer); | |
5902 | struct io_kiocb *req = data->req; | |
5903 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5904 | unsigned long flags; |
5905 | ||
89850fce | 5906 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
a71976f3 | 5907 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5908 | atomic_set(&req->ctx->cq_timeouts, |
5909 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
89850fce | 5910 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
01cec8c1 | 5911 | |
89850fce JA |
5912 | req->io_task_work.func = io_req_task_timeout; |
5913 | io_req_task_work_add(req); | |
5262f567 JA |
5914 | return HRTIMER_NORESTART; |
5915 | } | |
5916 | ||
fbd15848 PB |
5917 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5918 | __u64 user_data) | |
89850fce | 5919 | __must_hold(&ctx->timeout_lock) |
f254ac04 | 5920 | { |
fbd15848 | 5921 | struct io_timeout_data *io; |
47f46768 | 5922 | struct io_kiocb *req; |
fd9c7bc5 | 5923 | bool found = false; |
f254ac04 | 5924 | |
135fcde8 | 5925 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
fd9c7bc5 PB |
5926 | found = user_data == req->user_data; |
5927 | if (found) | |
47f46768 | 5928 | break; |
47f46768 | 5929 | } |
fd9c7bc5 PB |
5930 | if (!found) |
5931 | return ERR_PTR(-ENOENT); | |
fbd15848 PB |
5932 | |
5933 | io = req->async_data; | |
fd9c7bc5 | 5934 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
fbd15848 | 5935 | return ERR_PTR(-EALREADY); |
a71976f3 | 5936 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5937 | return req; |
5938 | } | |
47f46768 | 5939 | |
fbd15848 | 5940 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
ec3c3d0f | 5941 | __must_hold(&ctx->completion_lock) |
89850fce | 5942 | __must_hold(&ctx->timeout_lock) |
fbd15848 PB |
5943 | { |
5944 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5945 | ||
5946 | if (IS_ERR(req)) | |
5947 | return PTR_ERR(req); | |
f254ac04 | 5948 | |
93d2bcd2 | 5949 | req_set_fail(req); |
142f794e | 5950 | io_fill_cqe_req(req, -ECANCELED, 0); |
91c2f697 | 5951 | io_put_req_deferred(req); |
f254ac04 JA |
5952 | return 0; |
5953 | } | |
5954 | ||
50c1df2b JA |
5955 | static clockid_t io_timeout_get_clock(struct io_timeout_data *data) |
5956 | { | |
5957 | switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) { | |
5958 | case IORING_TIMEOUT_BOOTTIME: | |
5959 | return CLOCK_BOOTTIME; | |
5960 | case IORING_TIMEOUT_REALTIME: | |
5961 | return CLOCK_REALTIME; | |
5962 | default: | |
5963 | /* can't happen, vetted at prep time */ | |
5964 | WARN_ON_ONCE(1); | |
5965 | fallthrough; | |
5966 | case 0: | |
5967 | return CLOCK_MONOTONIC; | |
5968 | } | |
5969 | } | |
5970 | ||
f1042b6c PB |
5971 | static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5972 | struct timespec64 *ts, enum hrtimer_mode mode) | |
5973 | __must_hold(&ctx->timeout_lock) | |
5974 | { | |
5975 | struct io_timeout_data *io; | |
5976 | struct io_kiocb *req; | |
5977 | bool found = false; | |
5978 | ||
5979 | list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) { | |
5980 | found = user_data == req->user_data; | |
5981 | if (found) | |
5982 | break; | |
5983 | } | |
5984 | if (!found) | |
5985 | return -ENOENT; | |
5986 | ||
5987 | io = req->async_data; | |
5988 | if (hrtimer_try_to_cancel(&io->timer) == -1) | |
5989 | return -EALREADY; | |
5990 | hrtimer_init(&io->timer, io_timeout_get_clock(io), mode); | |
5991 | io->timer.function = io_link_timeout_fn; | |
5992 | hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode); | |
5993 | return 0; | |
5994 | } | |
5995 | ||
9c8e11b3 PB |
5996 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5997 | struct timespec64 *ts, enum hrtimer_mode mode) | |
89850fce | 5998 | __must_hold(&ctx->timeout_lock) |
47f46768 | 5999 | { |
9c8e11b3 PB |
6000 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
6001 | struct io_timeout_data *data; | |
47f46768 | 6002 | |
9c8e11b3 PB |
6003 | if (IS_ERR(req)) |
6004 | return PTR_ERR(req); | |
47f46768 | 6005 | |
9c8e11b3 PB |
6006 | req->timeout.off = 0; /* noseq */ |
6007 | data = req->async_data; | |
6008 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
50c1df2b | 6009 | hrtimer_init(&data->timer, io_timeout_get_clock(data), mode); |
9c8e11b3 PB |
6010 | data->timer.function = io_timeout_fn; |
6011 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
6012 | return 0; | |
47f46768 JA |
6013 | } |
6014 | ||
3529d8c2 JA |
6015 | static int io_timeout_remove_prep(struct io_kiocb *req, |
6016 | const struct io_uring_sqe *sqe) | |
b29472ee | 6017 | { |
9c8e11b3 PB |
6018 | struct io_timeout_rem *tr = &req->timeout_rem; |
6019 | ||
b29472ee JA |
6020 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
6021 | return -EINVAL; | |
61710e43 DA |
6022 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6023 | return -EINVAL; | |
26578cda | 6024 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
b29472ee JA |
6025 | return -EINVAL; |
6026 | ||
f1042b6c | 6027 | tr->ltimeout = false; |
9c8e11b3 PB |
6028 | tr->addr = READ_ONCE(sqe->addr); |
6029 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
f1042b6c PB |
6030 | if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) { |
6031 | if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
6032 | return -EINVAL; | |
6033 | if (tr->flags & IORING_LINK_TIMEOUT_UPDATE) | |
6034 | tr->ltimeout = true; | |
6035 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS)) | |
9c8e11b3 PB |
6036 | return -EINVAL; |
6037 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
6038 | return -EFAULT; | |
6039 | } else if (tr->flags) { | |
6040 | /* timeout removal doesn't support flags */ | |
b29472ee | 6041 | return -EINVAL; |
9c8e11b3 | 6042 | } |
b29472ee | 6043 | |
b29472ee JA |
6044 | return 0; |
6045 | } | |
6046 | ||
8662daec PB |
6047 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
6048 | { | |
6049 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
6050 | : HRTIMER_MODE_REL; | |
6051 | } | |
6052 | ||
11365043 JA |
6053 | /* |
6054 | * Remove or update an existing timeout command | |
6055 | */ | |
61e98203 | 6056 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 6057 | { |
9c8e11b3 | 6058 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 6059 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 6060 | int ret; |
11365043 | 6061 | |
ec3c3d0f PB |
6062 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
6063 | spin_lock(&ctx->completion_lock); | |
6064 | spin_lock_irq(&ctx->timeout_lock); | |
9c8e11b3 | 6065 | ret = io_timeout_cancel(ctx, tr->addr); |
ec3c3d0f PB |
6066 | spin_unlock_irq(&ctx->timeout_lock); |
6067 | spin_unlock(&ctx->completion_lock); | |
6068 | } else { | |
f1042b6c PB |
6069 | enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags); |
6070 | ||
ec3c3d0f | 6071 | spin_lock_irq(&ctx->timeout_lock); |
f1042b6c PB |
6072 | if (tr->ltimeout) |
6073 | ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
6074 | else | |
6075 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
ec3c3d0f PB |
6076 | spin_unlock_irq(&ctx->timeout_lock); |
6077 | } | |
11365043 | 6078 | |
4e88d6e7 | 6079 | if (ret < 0) |
93d2bcd2 | 6080 | req_set_fail(req); |
505657bc | 6081 | io_req_complete_post(req, ret, 0); |
11365043 | 6082 | return 0; |
5262f567 JA |
6083 | } |
6084 | ||
3529d8c2 | 6085 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 6086 | bool is_timeout_link) |
5262f567 | 6087 | { |
ad8a48ac | 6088 | struct io_timeout_data *data; |
a41525ab | 6089 | unsigned flags; |
56080b02 | 6090 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 6091 | |
ad8a48ac | 6092 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 6093 | return -EINVAL; |
26578cda PB |
6094 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
6095 | sqe->splice_fd_in) | |
a41525ab | 6096 | return -EINVAL; |
56080b02 | 6097 | if (off && is_timeout_link) |
2d28390a | 6098 | return -EINVAL; |
a41525ab | 6099 | flags = READ_ONCE(sqe->timeout_flags); |
50c1df2b JA |
6100 | if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK)) |
6101 | return -EINVAL; | |
6102 | /* more than one clock specified is invalid, obviously */ | |
6103 | if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
5262f567 | 6104 | return -EINVAL; |
bdf20073 | 6105 | |
ef9dd637 | 6106 | INIT_LIST_HEAD(&req->timeout.list); |
bfe68a22 | 6107 | req->timeout.off = off; |
f18ee4cf PB |
6108 | if (unlikely(off && !req->ctx->off_timeout_used)) |
6109 | req->ctx->off_timeout_used = true; | |
26a61679 | 6110 | |
e8c2bc1f | 6111 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
6112 | return -ENOMEM; |
6113 | ||
e8c2bc1f | 6114 | data = req->async_data; |
ad8a48ac | 6115 | data->req = req; |
50c1df2b | 6116 | data->flags = flags; |
ad8a48ac JA |
6117 | |
6118 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
6119 | return -EFAULT; |
6120 | ||
7abdd4c7 | 6121 | INIT_LIST_HEAD(&req->timeout.list); |
8662daec | 6122 | data->mode = io_translate_timeout_mode(flags); |
50c1df2b | 6123 | hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode); |
b97e736a PB |
6124 | |
6125 | if (is_timeout_link) { | |
6126 | struct io_submit_link *link = &req->ctx->submit_state.link; | |
6127 | ||
6128 | if (!link->head) | |
6129 | return -EINVAL; | |
6130 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) | |
6131 | return -EINVAL; | |
4d13d1a4 PB |
6132 | req->timeout.head = link->last; |
6133 | link->last->flags |= REQ_F_ARM_LTIMEOUT; | |
b97e736a | 6134 | } |
ad8a48ac JA |
6135 | return 0; |
6136 | } | |
6137 | ||
61e98203 | 6138 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 6139 | { |
ad8a48ac | 6140 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 6141 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 6142 | struct list_head *entry; |
bfe68a22 | 6143 | u32 tail, off = req->timeout.off; |
ad8a48ac | 6144 | |
89850fce | 6145 | spin_lock_irq(&ctx->timeout_lock); |
93bd25bb | 6146 | |
5262f567 JA |
6147 | /* |
6148 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
6149 | * timeout event to be satisfied. If it isn't set, then this is |
6150 | * a pure timeout request, sequence isn't used. | |
5262f567 | 6151 | */ |
8eb7e2d0 | 6152 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
6153 | entry = ctx->timeout_list.prev; |
6154 | goto add; | |
6155 | } | |
5262f567 | 6156 | |
bfe68a22 PB |
6157 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
6158 | req->timeout.target_seq = tail + off; | |
5262f567 | 6159 | |
f010505b MDG |
6160 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
6161 | * This is safe because ->completion_lock is held, and submissions | |
6162 | * and completions are never mixed in the same ->completion_lock section. | |
6163 | */ | |
6164 | ctx->cq_last_tm_flush = tail; | |
6165 | ||
5262f567 JA |
6166 | /* |
6167 | * Insertion sort, ensuring the first entry in the list is always | |
6168 | * the one we need first. | |
6169 | */ | |
5262f567 | 6170 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
6171 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
6172 | timeout.list); | |
5262f567 | 6173 | |
8eb7e2d0 | 6174 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 6175 | continue; |
bfe68a22 PB |
6176 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
6177 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
6178 | break; |
6179 | } | |
93bd25bb | 6180 | add: |
135fcde8 | 6181 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
6182 | data->timer.function = io_timeout_fn; |
6183 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
89850fce | 6184 | spin_unlock_irq(&ctx->timeout_lock); |
5262f567 JA |
6185 | return 0; |
6186 | } | |
5262f567 | 6187 | |
f458dd84 PB |
6188 | struct io_cancel_data { |
6189 | struct io_ring_ctx *ctx; | |
6190 | u64 user_data; | |
6191 | }; | |
6192 | ||
62755e35 JA |
6193 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
6194 | { | |
6195 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 6196 | struct io_cancel_data *cd = data; |
62755e35 | 6197 | |
f458dd84 | 6198 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
62755e35 JA |
6199 | } |
6200 | ||
f458dd84 PB |
6201 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
6202 | struct io_ring_ctx *ctx) | |
62755e35 | 6203 | { |
f458dd84 | 6204 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
62755e35 | 6205 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
6206 | int ret = 0; |
6207 | ||
f458dd84 | 6208 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
6209 | return -ENOENT; |
6210 | ||
f458dd84 | 6211 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
62755e35 JA |
6212 | switch (cancel_ret) { |
6213 | case IO_WQ_CANCEL_OK: | |
6214 | ret = 0; | |
6215 | break; | |
6216 | case IO_WQ_CANCEL_RUNNING: | |
6217 | ret = -EALREADY; | |
6218 | break; | |
6219 | case IO_WQ_CANCEL_NOTFOUND: | |
6220 | ret = -ENOENT; | |
6221 | break; | |
6222 | } | |
6223 | ||
e977d6d3 JA |
6224 | return ret; |
6225 | } | |
6226 | ||
8cb01fac | 6227 | static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr) |
47f46768 | 6228 | { |
8cb01fac | 6229 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 JA |
6230 | int ret; |
6231 | ||
dadebc35 | 6232 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
8cb01fac | 6233 | |
f458dd84 | 6234 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
df9727af | 6235 | if (ret != -ENOENT) |
8cb01fac | 6236 | return ret; |
505657bc PB |
6237 | |
6238 | spin_lock(&ctx->completion_lock); | |
79ebeaee | 6239 | spin_lock_irq(&ctx->timeout_lock); |
47f46768 | 6240 | ret = io_timeout_cancel(ctx, sqe_addr); |
79ebeaee | 6241 | spin_unlock_irq(&ctx->timeout_lock); |
47f46768 | 6242 | if (ret != -ENOENT) |
505657bc PB |
6243 | goto out; |
6244 | ret = io_poll_cancel(ctx, sqe_addr, false); | |
6245 | out: | |
6246 | spin_unlock(&ctx->completion_lock); | |
6247 | return ret; | |
47f46768 JA |
6248 | } |
6249 | ||
3529d8c2 JA |
6250 | static int io_async_cancel_prep(struct io_kiocb *req, |
6251 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 6252 | { |
fbf23849 | 6253 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 6254 | return -EINVAL; |
61710e43 DA |
6255 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6256 | return -EINVAL; | |
26578cda PB |
6257 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
6258 | sqe->splice_fd_in) | |
e977d6d3 JA |
6259 | return -EINVAL; |
6260 | ||
fbf23849 JA |
6261 | req->cancel.addr = READ_ONCE(sqe->addr); |
6262 | return 0; | |
6263 | } | |
6264 | ||
61e98203 | 6265 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6266 | { |
6267 | struct io_ring_ctx *ctx = req->ctx; | |
58f99373 PB |
6268 | u64 sqe_addr = req->cancel.addr; |
6269 | struct io_tctx_node *node; | |
6270 | int ret; | |
6271 | ||
8cb01fac | 6272 | ret = io_try_cancel_userdata(req, sqe_addr); |
58f99373 PB |
6273 | if (ret != -ENOENT) |
6274 | goto done; | |
58f99373 PB |
6275 | |
6276 | /* slow path, try all io-wq's */ | |
6277 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
6278 | ret = -ENOENT; | |
6279 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
6280 | struct io_uring_task *tctx = node->task->io_uring; | |
fbf23849 | 6281 | |
58f99373 PB |
6282 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
6283 | if (ret != -ENOENT) | |
6284 | break; | |
6285 | } | |
6286 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
58f99373 | 6287 | done: |
58f99373 | 6288 | if (ret < 0) |
93d2bcd2 | 6289 | req_set_fail(req); |
505657bc | 6290 | io_req_complete_post(req, ret, 0); |
5262f567 JA |
6291 | return 0; |
6292 | } | |
6293 | ||
269bbe5f | 6294 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
6295 | const struct io_uring_sqe *sqe) |
6296 | { | |
61710e43 DA |
6297 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6298 | return -EINVAL; | |
26578cda | 6299 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
05f3fb3c JA |
6300 | return -EINVAL; |
6301 | ||
269bbe5f BM |
6302 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
6303 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
6304 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 6305 | return -EINVAL; |
269bbe5f | 6306 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
6307 | return 0; |
6308 | } | |
6309 | ||
889fca73 | 6310 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6311 | { |
6312 | struct io_ring_ctx *ctx = req->ctx; | |
c3bdad02 | 6313 | struct io_uring_rsrc_update2 up; |
05f3fb3c | 6314 | int ret; |
fbf23849 | 6315 | |
269bbe5f BM |
6316 | up.offset = req->rsrc_update.offset; |
6317 | up.data = req->rsrc_update.arg; | |
c3bdad02 PB |
6318 | up.nr = 0; |
6319 | up.tags = 0; | |
615cee49 | 6320 | up.resv = 0; |
c9747fa5 | 6321 | up.resv2 = 0; |
05f3fb3c | 6322 | |
cdb31c29 | 6323 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
fdecb662 | 6324 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
98f0b3b4 | 6325 | &up, req->rsrc_update.nr_args); |
cdb31c29 | 6326 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
05f3fb3c JA |
6327 | |
6328 | if (ret < 0) | |
93d2bcd2 | 6329 | req_set_fail(req); |
889fca73 | 6330 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
6331 | return 0; |
6332 | } | |
6333 | ||
bfe76559 | 6334 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 6335 | { |
d625c6ee | 6336 | switch (req->opcode) { |
e781573e | 6337 | case IORING_OP_NOP: |
bfe76559 | 6338 | return 0; |
f67676d1 JA |
6339 | case IORING_OP_READV: |
6340 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 6341 | case IORING_OP_READ: |
bfe76559 | 6342 | return io_read_prep(req, sqe); |
f67676d1 JA |
6343 | case IORING_OP_WRITEV: |
6344 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 6345 | case IORING_OP_WRITE: |
bfe76559 | 6346 | return io_write_prep(req, sqe); |
0969e783 | 6347 | case IORING_OP_POLL_ADD: |
bfe76559 | 6348 | return io_poll_add_prep(req, sqe); |
0969e783 | 6349 | case IORING_OP_POLL_REMOVE: |
c5de0036 | 6350 | return io_poll_update_prep(req, sqe); |
8ed8d3c3 | 6351 | case IORING_OP_FSYNC: |
1155c76a | 6352 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 6353 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 6354 | return io_sfr_prep(req, sqe); |
03b1230c | 6355 | case IORING_OP_SENDMSG: |
fddaface | 6356 | case IORING_OP_SEND: |
bfe76559 | 6357 | return io_sendmsg_prep(req, sqe); |
03b1230c | 6358 | case IORING_OP_RECVMSG: |
fddaface | 6359 | case IORING_OP_RECV: |
bfe76559 | 6360 | return io_recvmsg_prep(req, sqe); |
f499a021 | 6361 | case IORING_OP_CONNECT: |
bfe76559 | 6362 | return io_connect_prep(req, sqe); |
2d28390a | 6363 | case IORING_OP_TIMEOUT: |
bfe76559 | 6364 | return io_timeout_prep(req, sqe, false); |
b29472ee | 6365 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 6366 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 6367 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 6368 | return io_async_cancel_prep(req, sqe); |
2d28390a | 6369 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 6370 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 6371 | case IORING_OP_ACCEPT: |
bfe76559 | 6372 | return io_accept_prep(req, sqe); |
d63d1b5e | 6373 | case IORING_OP_FALLOCATE: |
bfe76559 | 6374 | return io_fallocate_prep(req, sqe); |
15b71abe | 6375 | case IORING_OP_OPENAT: |
bfe76559 | 6376 | return io_openat_prep(req, sqe); |
b5dba59e | 6377 | case IORING_OP_CLOSE: |
bfe76559 | 6378 | return io_close_prep(req, sqe); |
05f3fb3c | 6379 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 6380 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6381 | case IORING_OP_STATX: |
bfe76559 | 6382 | return io_statx_prep(req, sqe); |
4840e418 | 6383 | case IORING_OP_FADVISE: |
bfe76559 | 6384 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6385 | case IORING_OP_MADVISE: |
bfe76559 | 6386 | return io_madvise_prep(req, sqe); |
cebdb986 | 6387 | case IORING_OP_OPENAT2: |
bfe76559 | 6388 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6389 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6390 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6391 | case IORING_OP_SPLICE: |
bfe76559 | 6392 | return io_splice_prep(req, sqe); |
ddf0322d | 6393 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6394 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6395 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6396 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6397 | case IORING_OP_TEE: |
bfe76559 | 6398 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6399 | case IORING_OP_SHUTDOWN: |
6400 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6401 | case IORING_OP_RENAMEAT: |
6402 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6403 | case IORING_OP_UNLINKAT: |
6404 | return io_unlinkat_prep(req, sqe); | |
e34a02dc DK |
6405 | case IORING_OP_MKDIRAT: |
6406 | return io_mkdirat_prep(req, sqe); | |
7a8721f8 DK |
6407 | case IORING_OP_SYMLINKAT: |
6408 | return io_symlinkat_prep(req, sqe); | |
cf30da90 DK |
6409 | case IORING_OP_LINKAT: |
6410 | return io_linkat_prep(req, sqe); | |
f67676d1 JA |
6411 | } |
6412 | ||
bfe76559 PB |
6413 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6414 | req->opcode); | |
bd54b6fe | 6415 | return -EINVAL; |
bfe76559 PB |
6416 | } |
6417 | ||
93642ef8 | 6418 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 6419 | { |
b7e298d2 PB |
6420 | if (!io_op_defs[req->opcode].needs_async_setup) |
6421 | return 0; | |
6422 | if (WARN_ON_ONCE(req->async_data)) | |
6423 | return -EFAULT; | |
6424 | if (io_alloc_async_data(req)) | |
6425 | return -EAGAIN; | |
6426 | ||
93642ef8 PB |
6427 | switch (req->opcode) { |
6428 | case IORING_OP_READV: | |
93642ef8 PB |
6429 | return io_rw_prep_async(req, READ); |
6430 | case IORING_OP_WRITEV: | |
93642ef8 PB |
6431 | return io_rw_prep_async(req, WRITE); |
6432 | case IORING_OP_SENDMSG: | |
93642ef8 PB |
6433 | return io_sendmsg_prep_async(req); |
6434 | case IORING_OP_RECVMSG: | |
93642ef8 PB |
6435 | return io_recvmsg_prep_async(req); |
6436 | case IORING_OP_CONNECT: | |
6437 | return io_connect_prep_async(req); | |
6438 | } | |
b7e298d2 PB |
6439 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
6440 | req->opcode); | |
6441 | return -EFAULT; | |
f67676d1 JA |
6442 | } |
6443 | ||
9cf7c104 PB |
6444 | static u32 io_get_sequence(struct io_kiocb *req) |
6445 | { | |
a3dbdf54 | 6446 | u32 seq = req->ctx->cached_sq_head; |
9cf7c104 | 6447 | |
a3dbdf54 PB |
6448 | /* need original cached_sq_head, but it was increased for each req */ |
6449 | io_for_each_link(req, req) | |
6450 | seq--; | |
6451 | return seq; | |
9cf7c104 PB |
6452 | } |
6453 | ||
76cc33d7 | 6454 | static bool io_drain_req(struct io_kiocb *req) |
de0617e4 | 6455 | { |
3c19966d | 6456 | struct io_kiocb *pos; |
a197f664 | 6457 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6458 | struct io_defer_entry *de; |
f67676d1 | 6459 | int ret; |
9cf7c104 | 6460 | u32 seq; |
de0617e4 | 6461 | |
b8ce1b9d PB |
6462 | if (req->flags & REQ_F_FAIL) { |
6463 | io_req_complete_fail_submit(req); | |
6464 | return true; | |
6465 | } | |
6466 | ||
3c19966d PB |
6467 | /* |
6468 | * If we need to drain a request in the middle of a link, drain the | |
6469 | * head request and the next request/link after the current link. | |
6470 | * Considering sequential execution of links, IOSQE_IO_DRAIN will be | |
6471 | * maintained for every request of our link. | |
6472 | */ | |
6473 | if (ctx->drain_next) { | |
6474 | req->flags |= REQ_F_IO_DRAIN; | |
6475 | ctx->drain_next = false; | |
6476 | } | |
6477 | /* not interested in head, start from the first linked */ | |
6478 | io_for_each_link(pos, req->link) { | |
6479 | if (pos->flags & REQ_F_IO_DRAIN) { | |
6480 | ctx->drain_next = true; | |
6481 | req->flags |= REQ_F_IO_DRAIN; | |
6482 | break; | |
6483 | } | |
6484 | } | |
6485 | ||
9d858b21 | 6486 | /* Still need defer if there is pending req in defer list. */ |
9632410b | 6487 | spin_lock(&ctx->completion_lock); |
9cf7c104 | 6488 | if (likely(list_empty_careful(&ctx->defer_list) && |
10c66904 | 6489 | !(req->flags & REQ_F_IO_DRAIN))) { |
9632410b | 6490 | spin_unlock(&ctx->completion_lock); |
10c66904 | 6491 | ctx->drain_active = false; |
76cc33d7 | 6492 | return false; |
10c66904 | 6493 | } |
9632410b | 6494 | spin_unlock(&ctx->completion_lock); |
9cf7c104 PB |
6495 | |
6496 | seq = io_get_sequence(req); | |
6497 | /* Still a chance to pass the sequence check */ | |
6498 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
76cc33d7 | 6499 | return false; |
de0617e4 | 6500 | |
b7e298d2 | 6501 | ret = io_req_prep_async(req); |
be7053b7 | 6502 | if (ret) |
1b48773f | 6503 | goto fail; |
cbdcb435 | 6504 | io_prep_async_link(req); |
27dc8338 | 6505 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
76cc33d7 | 6506 | if (!de) { |
1b48773f PB |
6507 | ret = -ENOMEM; |
6508 | fail: | |
6509 | io_req_complete_failed(req, ret); | |
76cc33d7 PB |
6510 | return true; |
6511 | } | |
2d28390a | 6512 | |
79ebeaee | 6513 | spin_lock(&ctx->completion_lock); |
9cf7c104 | 6514 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
79ebeaee | 6515 | spin_unlock(&ctx->completion_lock); |
27dc8338 | 6516 | kfree(de); |
f237c30a | 6517 | io_queue_async_work(req, NULL); |
76cc33d7 | 6518 | return true; |
de0617e4 JA |
6519 | } |
6520 | ||
915967f6 | 6521 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 6522 | de->req = req; |
9cf7c104 | 6523 | de->seq = seq; |
27dc8338 | 6524 | list_add_tail(&de->list, &ctx->defer_list); |
79ebeaee | 6525 | spin_unlock(&ctx->completion_lock); |
76cc33d7 | 6526 | return true; |
de0617e4 JA |
6527 | } |
6528 | ||
68fb8979 | 6529 | static void io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6530 | { |
0e1b6fe3 PB |
6531 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
6532 | switch (req->opcode) { | |
6533 | case IORING_OP_READV: | |
6534 | case IORING_OP_READ_FIXED: | |
6535 | case IORING_OP_READ: | |
bcda7baa | 6536 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
6537 | break; |
6538 | case IORING_OP_RECVMSG: | |
6539 | case IORING_OP_RECV: | |
bcda7baa | 6540 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
6541 | break; |
6542 | } | |
99bc4c38 PB |
6543 | } |
6544 | ||
0e1b6fe3 PB |
6545 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6546 | switch (req->opcode) { | |
6547 | case IORING_OP_READV: | |
6548 | case IORING_OP_READ_FIXED: | |
6549 | case IORING_OP_READ: | |
6550 | case IORING_OP_WRITEV: | |
6551 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6552 | case IORING_OP_WRITE: { |
6553 | struct io_async_rw *io = req->async_data; | |
1dacb4df PB |
6554 | |
6555 | kfree(io->free_iovec); | |
0e1b6fe3 | 6556 | break; |
e8c2bc1f | 6557 | } |
0e1b6fe3 | 6558 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6559 | case IORING_OP_SENDMSG: { |
6560 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
6561 | |
6562 | kfree(io->free_iov); | |
0e1b6fe3 | 6563 | break; |
e8c2bc1f | 6564 | } |
f3cd4850 JA |
6565 | case IORING_OP_OPENAT: |
6566 | case IORING_OP_OPENAT2: | |
6567 | if (req->open.filename) | |
6568 | putname(req->open.filename); | |
6569 | break; | |
80a261fd JA |
6570 | case IORING_OP_RENAMEAT: |
6571 | putname(req->rename.oldpath); | |
6572 | putname(req->rename.newpath); | |
6573 | break; | |
14a1143b JA |
6574 | case IORING_OP_UNLINKAT: |
6575 | putname(req->unlink.filename); | |
6576 | break; | |
e34a02dc DK |
6577 | case IORING_OP_MKDIRAT: |
6578 | putname(req->mkdir.filename); | |
6579 | break; | |
7a8721f8 DK |
6580 | case IORING_OP_SYMLINKAT: |
6581 | putname(req->symlink.oldpath); | |
6582 | putname(req->symlink.newpath); | |
6583 | break; | |
cf30da90 DK |
6584 | case IORING_OP_LINKAT: |
6585 | putname(req->hardlink.oldpath); | |
6586 | putname(req->hardlink.newpath); | |
6587 | break; | |
0e1b6fe3 | 6588 | } |
99bc4c38 | 6589 | } |
75652a30 JA |
6590 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
6591 | kfree(req->apoll->double_poll); | |
6592 | kfree(req->apoll); | |
6593 | req->apoll = NULL; | |
6594 | } | |
3a0a6902 PB |
6595 | if (req->flags & REQ_F_INFLIGHT) { |
6596 | struct io_uring_task *tctx = req->task->io_uring; | |
6597 | ||
6598 | atomic_dec(&tctx->inflight_tracked); | |
3a0a6902 | 6599 | } |
c854357b | 6600 | if (req->flags & REQ_F_CREDS) |
b8e64b53 | 6601 | put_cred(req->creds); |
c854357b PB |
6602 | |
6603 | req->flags &= ~IO_REQ_CLEAN_FLAGS; | |
99bc4c38 PB |
6604 | } |
6605 | ||
889fca73 | 6606 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 6607 | { |
a197f664 | 6608 | struct io_ring_ctx *ctx = req->ctx; |
5730b27e | 6609 | const struct cred *creds = NULL; |
d625c6ee | 6610 | int ret; |
2b188cc1 | 6611 | |
b8e64b53 | 6612 | if ((req->flags & REQ_F_CREDS) && req->creds != current_cred()) |
c10d1f98 | 6613 | creds = override_creds(req->creds); |
5730b27e | 6614 | |
d625c6ee | 6615 | switch (req->opcode) { |
2b188cc1 | 6616 | case IORING_OP_NOP: |
889fca73 | 6617 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
6618 | break; |
6619 | case IORING_OP_READV: | |
edafccee | 6620 | case IORING_OP_READ_FIXED: |
3a6820f2 | 6621 | case IORING_OP_READ: |
889fca73 | 6622 | ret = io_read(req, issue_flags); |
edafccee | 6623 | break; |
3529d8c2 | 6624 | case IORING_OP_WRITEV: |
edafccee | 6625 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 6626 | case IORING_OP_WRITE: |
889fca73 | 6627 | ret = io_write(req, issue_flags); |
2b188cc1 | 6628 | break; |
c992fe29 | 6629 | case IORING_OP_FSYNC: |
45d189c6 | 6630 | ret = io_fsync(req, issue_flags); |
c992fe29 | 6631 | break; |
221c5eb2 | 6632 | case IORING_OP_POLL_ADD: |
61e98203 | 6633 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
6634 | break; |
6635 | case IORING_OP_POLL_REMOVE: | |
c5de0036 | 6636 | ret = io_poll_update(req, issue_flags); |
221c5eb2 | 6637 | break; |
5d17b4a4 | 6638 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 6639 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 6640 | break; |
0fa03c62 | 6641 | case IORING_OP_SENDMSG: |
889fca73 | 6642 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 6643 | break; |
fddaface | 6644 | case IORING_OP_SEND: |
889fca73 | 6645 | ret = io_send(req, issue_flags); |
0fa03c62 | 6646 | break; |
aa1fa28f | 6647 | case IORING_OP_RECVMSG: |
889fca73 | 6648 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 6649 | break; |
fddaface | 6650 | case IORING_OP_RECV: |
889fca73 | 6651 | ret = io_recv(req, issue_flags); |
aa1fa28f | 6652 | break; |
5262f567 | 6653 | case IORING_OP_TIMEOUT: |
61e98203 | 6654 | ret = io_timeout(req, issue_flags); |
5262f567 | 6655 | break; |
11365043 | 6656 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 6657 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 6658 | break; |
17f2fe35 | 6659 | case IORING_OP_ACCEPT: |
889fca73 | 6660 | ret = io_accept(req, issue_flags); |
17f2fe35 | 6661 | break; |
f8e85cf2 | 6662 | case IORING_OP_CONNECT: |
889fca73 | 6663 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 6664 | break; |
62755e35 | 6665 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 6666 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 6667 | break; |
d63d1b5e | 6668 | case IORING_OP_FALLOCATE: |
45d189c6 | 6669 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 6670 | break; |
15b71abe | 6671 | case IORING_OP_OPENAT: |
45d189c6 | 6672 | ret = io_openat(req, issue_flags); |
15b71abe | 6673 | break; |
b5dba59e | 6674 | case IORING_OP_CLOSE: |
889fca73 | 6675 | ret = io_close(req, issue_flags); |
b5dba59e | 6676 | break; |
05f3fb3c | 6677 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6678 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6679 | break; |
eddc7ef5 | 6680 | case IORING_OP_STATX: |
45d189c6 | 6681 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6682 | break; |
4840e418 | 6683 | case IORING_OP_FADVISE: |
45d189c6 | 6684 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6685 | break; |
c1ca757b | 6686 | case IORING_OP_MADVISE: |
45d189c6 | 6687 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6688 | break; |
cebdb986 | 6689 | case IORING_OP_OPENAT2: |
45d189c6 | 6690 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6691 | break; |
3e4827b0 | 6692 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6693 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6694 | break; |
7d67af2c | 6695 | case IORING_OP_SPLICE: |
45d189c6 | 6696 | ret = io_splice(req, issue_flags); |
7d67af2c | 6697 | break; |
ddf0322d | 6698 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6699 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6700 | break; |
067524e9 | 6701 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6702 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6703 | break; |
f2a8d5c7 | 6704 | case IORING_OP_TEE: |
45d189c6 | 6705 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6706 | break; |
36f4fa68 | 6707 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6708 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6709 | break; |
80a261fd | 6710 | case IORING_OP_RENAMEAT: |
45d189c6 | 6711 | ret = io_renameat(req, issue_flags); |
80a261fd | 6712 | break; |
14a1143b | 6713 | case IORING_OP_UNLINKAT: |
45d189c6 | 6714 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6715 | break; |
e34a02dc DK |
6716 | case IORING_OP_MKDIRAT: |
6717 | ret = io_mkdirat(req, issue_flags); | |
6718 | break; | |
7a8721f8 DK |
6719 | case IORING_OP_SYMLINKAT: |
6720 | ret = io_symlinkat(req, issue_flags); | |
6721 | break; | |
cf30da90 DK |
6722 | case IORING_OP_LINKAT: |
6723 | ret = io_linkat(req, issue_flags); | |
6724 | break; | |
2b188cc1 JA |
6725 | default: |
6726 | ret = -EINVAL; | |
6727 | break; | |
6728 | } | |
6729 | ||
5730b27e JA |
6730 | if (creds) |
6731 | revert_creds(creds); | |
def596e9 JA |
6732 | if (ret) |
6733 | return ret; | |
b532576e | 6734 | /* If the op doesn't have a file, we're not polling for it */ |
cb3d8972 PB |
6735 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
6736 | io_iopoll_req_issued(req); | |
def596e9 JA |
6737 | |
6738 | return 0; | |
2b188cc1 JA |
6739 | } |
6740 | ||
ebc11b6c PB |
6741 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
6742 | { | |
6743 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6744 | ||
6745 | req = io_put_req_find_next(req); | |
6746 | return req ? &req->work : NULL; | |
6747 | } | |
6748 | ||
5280f7e5 | 6749 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6750 | { |
6751 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6752 | struct io_kiocb *timeout; |
561fb04a | 6753 | int ret = 0; |
2b188cc1 | 6754 | |
48dcd38d PB |
6755 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
6756 | if (!(req->flags & REQ_F_REFCOUNT)) | |
6757 | __io_req_set_refcount(req, 2); | |
6758 | else | |
6759 | req_ref_get(req); | |
5d5901a3 | 6760 | |
6df1db6b PB |
6761 | timeout = io_prep_linked_timeout(req); |
6762 | if (timeout) | |
6763 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6764 | |
dadebc35 | 6765 | /* either cancelled or io-wq is dying, so don't touch tctx->iowq */ |
4014d943 | 6766 | if (work->flags & IO_WQ_WORK_CANCEL) |
561fb04a | 6767 | ret = -ECANCELED; |
31b51510 | 6768 | |
561fb04a | 6769 | if (!ret) { |
561fb04a | 6770 | do { |
889fca73 | 6771 | ret = io_issue_sqe(req, 0); |
561fb04a JA |
6772 | /* |
6773 | * We can get EAGAIN for polled IO even though we're | |
6774 | * forcing a sync submission from here, since we can't | |
6775 | * wait for request slots on the block side. | |
6776 | */ | |
b5691848 | 6777 | if (ret != -EAGAIN || !(req->ctx->flags & IORING_SETUP_IOPOLL)) |
561fb04a JA |
6778 | break; |
6779 | cond_resched(); | |
6780 | } while (1); | |
6781 | } | |
31b51510 | 6782 | |
a3df7698 | 6783 | /* avoid locking problems by failing it from a clean context */ |
5d5901a3 | 6784 | if (ret) |
a3df7698 | 6785 | io_req_task_queue_fail(req, ret); |
2b188cc1 JA |
6786 | } |
6787 | ||
aeca241b | 6788 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
042b0d85 | 6789 | unsigned i) |
65e19f54 | 6790 | { |
042b0d85 | 6791 | return &table->files[i]; |
dafecf19 PB |
6792 | } |
6793 | ||
65e19f54 JA |
6794 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6795 | int index) | |
6796 | { | |
aeca241b | 6797 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
65e19f54 | 6798 | |
a04b0ac0 | 6799 | return (struct file *) (slot->file_ptr & FFS_MASK); |
65e19f54 JA |
6800 | } |
6801 | ||
a04b0ac0 | 6802 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
9a321c98 PB |
6803 | { |
6804 | unsigned long file_ptr = (unsigned long) file; | |
6805 | ||
b191e2df | 6806 | if (__io_file_supports_nowait(file, READ)) |
9a321c98 | 6807 | file_ptr |= FFS_ASYNC_READ; |
b191e2df | 6808 | if (__io_file_supports_nowait(file, WRITE)) |
9a321c98 PB |
6809 | file_ptr |= FFS_ASYNC_WRITE; |
6810 | if (S_ISREG(file_inode(file)->i_mode)) | |
6811 | file_ptr |= FFS_ISREG; | |
a04b0ac0 | 6812 | file_slot->file_ptr = file_ptr; |
65e19f54 JA |
6813 | } |
6814 | ||
ac177053 PB |
6815 | static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx, |
6816 | struct io_kiocb *req, int fd) | |
09bb8394 | 6817 | { |
8da11c19 | 6818 | struct file *file; |
ac177053 | 6819 | unsigned long file_ptr; |
09bb8394 | 6820 | |
ac177053 PB |
6821 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
6822 | return NULL; | |
6823 | fd = array_index_nospec(fd, ctx->nr_user_files); | |
6824 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; | |
6825 | file = (struct file *) (file_ptr & FFS_MASK); | |
6826 | file_ptr &= ~FFS_MASK; | |
6827 | /* mask in overlapping REQ_F and FFS bits */ | |
b191e2df | 6828 | req->flags |= (file_ptr << REQ_F_NOWAIT_READ_BIT); |
ac177053 PB |
6829 | io_req_set_rsrc_node(req); |
6830 | return file; | |
6831 | } | |
d44f554e | 6832 | |
ac177053 | 6833 | static struct file *io_file_get_normal(struct io_ring_ctx *ctx, |
ac177053 PB |
6834 | struct io_kiocb *req, int fd) |
6835 | { | |
62906e89 | 6836 | struct file *file = fget(fd); |
ac177053 PB |
6837 | |
6838 | trace_io_uring_file_get(ctx, fd); | |
09bb8394 | 6839 | |
ac177053 PB |
6840 | /* we don't allow fixed io_uring files */ |
6841 | if (file && unlikely(file->f_op == &io_uring_fops)) | |
6842 | io_req_track_inflight(req); | |
8371adf5 | 6843 | return file; |
09bb8394 JA |
6844 | } |
6845 | ||
ac177053 | 6846 | static inline struct file *io_file_get(struct io_ring_ctx *ctx, |
ac177053 PB |
6847 | struct io_kiocb *req, int fd, bool fixed) |
6848 | { | |
6849 | if (fixed) | |
6850 | return io_file_get_fixed(ctx, req, fd); | |
6851 | else | |
62906e89 | 6852 | return io_file_get_normal(ctx, req, fd); |
ac177053 PB |
6853 | } |
6854 | ||
f237c30a | 6855 | static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked) |
89b263f6 JA |
6856 | { |
6857 | struct io_kiocb *prev = req->timeout.prev; | |
fc590dd1 | 6858 | int ret = -ENOENT; |
89b263f6 JA |
6859 | |
6860 | if (prev) { | |
fc590dd1 PB |
6861 | if (!(req->task->flags & PF_EXITING)) |
6862 | ret = io_try_cancel_userdata(req, prev->user_data); | |
505657bc | 6863 | io_req_complete_post(req, ret ?: -ETIME, 0); |
89b263f6 | 6864 | io_put_req(prev); |
89b263f6 JA |
6865 | } else { |
6866 | io_req_complete_post(req, -ETIME, 0); | |
6867 | } | |
6868 | } | |
6869 | ||
2665abfd | 6870 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6871 | { |
ad8a48ac JA |
6872 | struct io_timeout_data *data = container_of(timer, |
6873 | struct io_timeout_data, timer); | |
90cd7e42 | 6874 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6875 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6876 | unsigned long flags; |
2665abfd | 6877 | |
89b263f6 | 6878 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
90cd7e42 PB |
6879 | prev = req->timeout.head; |
6880 | req->timeout.head = NULL; | |
2665abfd JA |
6881 | |
6882 | /* | |
6883 | * We don't expect the list to be empty, that will only happen if we | |
6884 | * race with the completion of the linked work. | |
6885 | */ | |
447c19f3 | 6886 | if (prev) { |
f2f87370 | 6887 | io_remove_next_linked(prev); |
447c19f3 PB |
6888 | if (!req_ref_inc_not_zero(prev)) |
6889 | prev = NULL; | |
6890 | } | |
ef9dd637 | 6891 | list_del(&req->timeout.list); |
89b263f6 JA |
6892 | req->timeout.prev = prev; |
6893 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); | |
2665abfd | 6894 | |
89b263f6 JA |
6895 | req->io_task_work.func = io_req_task_link_timeout; |
6896 | io_req_task_work_add(req); | |
2665abfd JA |
6897 | return HRTIMER_NORESTART; |
6898 | } | |
6899 | ||
de968c18 | 6900 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6901 | { |
de968c18 PB |
6902 | struct io_ring_ctx *ctx = req->ctx; |
6903 | ||
89b263f6 | 6904 | spin_lock_irq(&ctx->timeout_lock); |
76a46e06 | 6905 | /* |
f2f87370 PB |
6906 | * If the back reference is NULL, then our linked request finished |
6907 | * before we got a chance to setup the timer | |
76a46e06 | 6908 | */ |
90cd7e42 | 6909 | if (req->timeout.head) { |
e8c2bc1f | 6910 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6911 | |
ad8a48ac JA |
6912 | data->timer.function = io_link_timeout_fn; |
6913 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6914 | data->mode); | |
ef9dd637 | 6915 | list_add_tail(&req->timeout.list, &ctx->ltimeout_list); |
2665abfd | 6916 | } |
89b263f6 | 6917 | spin_unlock_irq(&ctx->timeout_lock); |
2665abfd | 6918 | /* drop submission reference */ |
76a46e06 JA |
6919 | io_put_req(req); |
6920 | } | |
2665abfd | 6921 | |
c5eef2b9 | 6922 | static void __io_queue_sqe(struct io_kiocb *req) |
282cdc86 | 6923 | __must_hold(&req->ctx->uring_lock) |
2b188cc1 | 6924 | { |
906c6caa | 6925 | struct io_kiocb *linked_timeout; |
e0c5c576 | 6926 | int ret; |
2b188cc1 | 6927 | |
59b735ae | 6928 | issue_sqe: |
c5eef2b9 | 6929 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 6930 | |
491381ce JA |
6931 | /* |
6932 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6933 | * doesn't support non-blocking read/write attempts | |
6934 | */ | |
1840038e | 6935 | if (likely(!ret)) { |
e342c807 | 6936 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
c5eef2b9 | 6937 | struct io_ring_ctx *ctx = req->ctx; |
cd0ca2e0 | 6938 | struct io_submit_state *state = &ctx->submit_state; |
e65ef56d | 6939 | |
cd0ca2e0 PB |
6940 | state->compl_reqs[state->compl_nr++] = req; |
6941 | if (state->compl_nr == ARRAY_SIZE(state->compl_reqs)) | |
2a2758f2 | 6942 | io_submit_flush_completions(ctx); |
906c6caa | 6943 | return; |
0d63c148 | 6944 | } |
906c6caa PB |
6945 | |
6946 | linked_timeout = io_prep_linked_timeout(req); | |
6947 | if (linked_timeout) | |
6948 | io_queue_linked_timeout(linked_timeout); | |
1840038e | 6949 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
906c6caa PB |
6950 | linked_timeout = io_prep_linked_timeout(req); |
6951 | ||
59b735ae OL |
6952 | switch (io_arm_poll_handler(req)) { |
6953 | case IO_APOLL_READY: | |
906c6caa | 6954 | if (linked_timeout) |
4ea672ab | 6955 | io_queue_linked_timeout(linked_timeout); |
59b735ae OL |
6956 | goto issue_sqe; |
6957 | case IO_APOLL_ABORTED: | |
1840038e PB |
6958 | /* |
6959 | * Queued up for async execution, worker will release | |
6960 | * submit reference when the iocb is actually submitted. | |
6961 | */ | |
f237c30a | 6962 | io_queue_async_work(req, NULL); |
59b735ae | 6963 | break; |
1840038e | 6964 | } |
906c6caa PB |
6965 | |
6966 | if (linked_timeout) | |
6967 | io_queue_linked_timeout(linked_timeout); | |
0d63c148 | 6968 | } else { |
f41db273 | 6969 | io_req_complete_failed(req, ret); |
9e645e11 | 6970 | } |
2b188cc1 JA |
6971 | } |
6972 | ||
441b8a78 | 6973 | static inline void io_queue_sqe(struct io_kiocb *req) |
282cdc86 | 6974 | __must_hold(&req->ctx->uring_lock) |
4fe2c963 | 6975 | { |
10c66904 | 6976 | if (unlikely(req->ctx->drain_active) && io_drain_req(req)) |
76cc33d7 | 6977 | return; |
4fe2c963 | 6978 | |
a8295b98 | 6979 | if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) { |
c5eef2b9 | 6980 | __io_queue_sqe(req); |
a8295b98 | 6981 | } else if (req->flags & REQ_F_FAIL) { |
c6d3d9cb | 6982 | io_req_complete_fail_submit(req); |
76cc33d7 PB |
6983 | } else { |
6984 | int ret = io_req_prep_async(req); | |
6985 | ||
6986 | if (unlikely(ret)) | |
6987 | io_req_complete_failed(req, ret); | |
6988 | else | |
f237c30a | 6989 | io_queue_async_work(req, NULL); |
ce35a47a | 6990 | } |
4fe2c963 JL |
6991 | } |
6992 | ||
b16fed66 PB |
6993 | /* |
6994 | * Check SQE restrictions (opcode and flags). | |
6995 | * | |
6996 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6997 | */ | |
6998 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6999 | struct io_kiocb *req, | |
7000 | unsigned int sqe_flags) | |
4fe2c963 | 7001 | { |
4cfb25bf | 7002 | if (likely(!ctx->restricted)) |
b16fed66 PB |
7003 | return true; |
7004 | ||
7005 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) | |
7006 | return false; | |
7007 | ||
7008 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
7009 | ctx->restrictions.sqe_flags_required) | |
7010 | return false; | |
7011 | ||
7012 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
7013 | ctx->restrictions.sqe_flags_required)) | |
7014 | return false; | |
7015 | ||
7016 | return true; | |
4fe2c963 JL |
7017 | } |
7018 | ||
b16fed66 PB |
7019 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
7020 | const struct io_uring_sqe *sqe) | |
282cdc86 | 7021 | __must_hold(&ctx->uring_lock) |
b16fed66 PB |
7022 | { |
7023 | struct io_submit_state *state; | |
7024 | unsigned int sqe_flags; | |
003e8dcc | 7025 | int personality, ret = 0; |
b16fed66 | 7026 | |
864ea921 | 7027 | /* req is partially pre-initialised, see io_preinit_req() */ |
b16fed66 PB |
7028 | req->opcode = READ_ONCE(sqe->opcode); |
7029 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
7030 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
7031 | req->user_data = READ_ONCE(sqe->user_data); | |
b16fed66 | 7032 | req->file = NULL; |
b16fed66 | 7033 | req->fixed_rsrc_refs = NULL; |
b16fed66 | 7034 | req->task = current; |
b16fed66 PB |
7035 | |
7036 | /* enforce forwards compatibility on users */ | |
dddca226 | 7037 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) |
b16fed66 | 7038 | return -EINVAL; |
b16fed66 PB |
7039 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
7040 | return -EINVAL; | |
4cfb25bf | 7041 | if (!io_check_restriction(ctx, req, sqe_flags)) |
b16fed66 PB |
7042 | return -EACCES; |
7043 | ||
7044 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
7045 | !io_op_defs[req->opcode].buffer_select) | |
7046 | return -EOPNOTSUPP; | |
3c19966d PB |
7047 | if (unlikely(sqe_flags & IOSQE_IO_DRAIN)) |
7048 | ctx->drain_active = true; | |
863e0560 | 7049 | |
003e8dcc JA |
7050 | personality = READ_ONCE(sqe->personality); |
7051 | if (personality) { | |
c10d1f98 PB |
7052 | req->creds = xa_load(&ctx->personalities, personality); |
7053 | if (!req->creds) | |
003e8dcc | 7054 | return -EINVAL; |
c10d1f98 | 7055 | get_cred(req->creds); |
b8e64b53 | 7056 | req->flags |= REQ_F_CREDS; |
003e8dcc | 7057 | } |
b16fed66 PB |
7058 | state = &ctx->submit_state; |
7059 | ||
7060 | /* | |
7061 | * Plug now if we have more than 1 IO left after this, and the target | |
7062 | * is potentially a read/write to block based storage. | |
7063 | */ | |
7064 | if (!state->plug_started && state->ios_left > 1 && | |
7065 | io_op_defs[req->opcode].plug) { | |
7066 | blk_start_plug(&state->plug); | |
7067 | state->plug_started = true; | |
7068 | } | |
7069 | ||
7070 | if (io_op_defs[req->opcode].needs_file) { | |
62906e89 | 7071 | req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd), |
ac177053 | 7072 | (sqe_flags & IOSQE_FIXED_FILE)); |
b16fed66 PB |
7073 | if (unlikely(!req->file)) |
7074 | ret = -EBADF; | |
7075 | } | |
7076 | ||
7077 | state->ios_left--; | |
7078 | return ret; | |
7079 | } | |
7080 | ||
a6b8cadc | 7081 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 7082 | const struct io_uring_sqe *sqe) |
282cdc86 | 7083 | __must_hold(&ctx->uring_lock) |
9e645e11 | 7084 | { |
a1ab7b35 | 7085 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 7086 | int ret; |
9e645e11 | 7087 | |
a6b8cadc PB |
7088 | ret = io_init_req(ctx, req, sqe); |
7089 | if (unlikely(ret)) { | |
7090 | fail_req: | |
a8295b98 | 7091 | /* fail even hard links since we don't submit */ |
de59bc10 | 7092 | if (link->head) { |
a8295b98 HX |
7093 | /* |
7094 | * we can judge a link req is failed or cancelled by if | |
7095 | * REQ_F_FAIL is set, but the head is an exception since | |
7096 | * it may be set REQ_F_FAIL because of other req's failure | |
7097 | * so let's leverage req->result to distinguish if a head | |
7098 | * is set REQ_F_FAIL because of its failure or other req's | |
7099 | * failure so that we can set the correct ret code for it. | |
7100 | * init result here to avoid affecting the normal path. | |
7101 | */ | |
7102 | if (!(link->head->flags & REQ_F_FAIL)) | |
7103 | req_fail_link_node(link->head, -ECANCELED); | |
7104 | } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { | |
7105 | /* | |
7106 | * the current req is a normal req, we should return | |
7107 | * error and thus break the submittion loop. | |
7108 | */ | |
7109 | io_req_complete_failed(req, ret); | |
7110 | return ret; | |
de59bc10 | 7111 | } |
a8295b98 HX |
7112 | req_fail_link_node(req, ret); |
7113 | } else { | |
7114 | ret = io_req_prep(req, sqe); | |
7115 | if (unlikely(ret)) | |
7116 | goto fail_req; | |
a6b8cadc | 7117 | } |
441b8a78 | 7118 | |
be7053b7 | 7119 | /* don't need @sqe from now on */ |
236daeae OL |
7120 | trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data, |
7121 | req->flags, true, | |
7122 | ctx->flags & IORING_SETUP_SQPOLL); | |
a6b8cadc | 7123 | |
9e645e11 JA |
7124 | /* |
7125 | * If we already have a head request, queue this one for async | |
7126 | * submittal once the head completes. If we don't have a head but | |
7127 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
7128 | * submitted sync once the chain is complete. If none of those | |
7129 | * conditions are true (normal request), then just queue it. | |
7130 | */ | |
863e0560 PB |
7131 | if (link->head) { |
7132 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 7133 | |
a8295b98 HX |
7134 | if (!(req->flags & REQ_F_FAIL)) { |
7135 | ret = io_req_prep_async(req); | |
7136 | if (unlikely(ret)) { | |
7137 | req_fail_link_node(req, ret); | |
7138 | if (!(head->flags & REQ_F_FAIL)) | |
7139 | req_fail_link_node(head, -ECANCELED); | |
7140 | } | |
7141 | } | |
9d76377f | 7142 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 7143 | link->last->link = req; |
863e0560 | 7144 | link->last = req; |
32fe525b PB |
7145 | |
7146 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 7147 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
863e0560 | 7148 | link->head = NULL; |
5e159204 | 7149 | io_queue_sqe(head); |
32fe525b | 7150 | } |
9e645e11 | 7151 | } else { |
ef4ff581 | 7152 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
863e0560 PB |
7153 | link->head = req; |
7154 | link->last = req; | |
711be031 | 7155 | } else { |
be7053b7 | 7156 | io_queue_sqe(req); |
711be031 | 7157 | } |
9e645e11 | 7158 | } |
2e6e1fde | 7159 | |
1d4240cc | 7160 | return 0; |
9e645e11 JA |
7161 | } |
7162 | ||
9a56a232 JA |
7163 | /* |
7164 | * Batched submission is done, ensure local IO is flushed out. | |
7165 | */ | |
ba88ff11 PB |
7166 | static void io_submit_state_end(struct io_submit_state *state, |
7167 | struct io_ring_ctx *ctx) | |
9a56a232 | 7168 | { |
a1ab7b35 | 7169 | if (state->link.head) |
de59bc10 | 7170 | io_queue_sqe(state->link.head); |
cd0ca2e0 | 7171 | if (state->compl_nr) |
2a2758f2 | 7172 | io_submit_flush_completions(ctx); |
27926b68 JA |
7173 | if (state->plug_started) |
7174 | blk_finish_plug(&state->plug); | |
9a56a232 JA |
7175 | } |
7176 | ||
7177 | /* | |
7178 | * Start submission side cache. | |
7179 | */ | |
7180 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 7181 | unsigned int max_ios) |
9a56a232 | 7182 | { |
27926b68 | 7183 | state->plug_started = false; |
9a56a232 | 7184 | state->ios_left = max_ios; |
a1ab7b35 PB |
7185 | /* set only head, no need to init link_last in advance */ |
7186 | state->link.head = NULL; | |
9a56a232 JA |
7187 | } |
7188 | ||
2b188cc1 JA |
7189 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
7190 | { | |
75b28aff | 7191 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 7192 | |
caf582c6 PB |
7193 | /* |
7194 | * Ensure any loads from the SQEs are done at this point, | |
7195 | * since once we write the new head, the application could | |
7196 | * write new data to them. | |
7197 | */ | |
7198 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
7199 | } |
7200 | ||
2b188cc1 | 7201 | /* |
dd9ae8a0 | 7202 | * Fetch an sqe, if one is available. Note this returns a pointer to memory |
2b188cc1 JA |
7203 | * that is mapped by userspace. This means that care needs to be taken to |
7204 | * ensure that reads are stable, as we cannot rely on userspace always | |
7205 | * being a good citizen. If members of the sqe are validated and then later | |
7206 | * used, it's important that those reads are done through READ_ONCE() to | |
7207 | * prevent a re-load down the line. | |
7208 | */ | |
709b302f | 7209 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 7210 | { |
ea5ab3b5 | 7211 | unsigned head, mask = ctx->sq_entries - 1; |
17d3aeb3 | 7212 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
2b188cc1 JA |
7213 | |
7214 | /* | |
7215 | * The cached sq head (or cq tail) serves two purposes: | |
7216 | * | |
7217 | * 1) allows us to batch the cost of updating the user visible | |
7218 | * head updates. | |
7219 | * 2) allows the kernel side to track the head on its own, even | |
7220 | * though the application is the one updating it. | |
7221 | */ | |
17d3aeb3 | 7222 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
709b302f PB |
7223 | if (likely(head < ctx->sq_entries)) |
7224 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
7225 | |
7226 | /* drop invalid entries */ | |
15641e42 PB |
7227 | ctx->cq_extra--; |
7228 | WRITE_ONCE(ctx->rings->sq_dropped, | |
7229 | READ_ONCE(ctx->rings->sq_dropped) + 1); | |
709b302f PB |
7230 | return NULL; |
7231 | } | |
7232 | ||
0f212204 | 7233 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
282cdc86 | 7234 | __must_hold(&ctx->uring_lock) |
6c271ce2 | 7235 | { |
46c4e16a | 7236 | int submitted = 0; |
6c271ce2 | 7237 | |
ee7d46d9 PB |
7238 | /* make sure SQ entry isn't read before tail */ |
7239 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
2b85edfc PB |
7240 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
7241 | return -EAGAIN; | |
9a10867a | 7242 | io_get_task_refs(nr); |
6c271ce2 | 7243 | |
ba88ff11 | 7244 | io_submit_state_start(&ctx->submit_state, nr); |
46c4e16a | 7245 | while (submitted < nr) { |
3529d8c2 | 7246 | const struct io_uring_sqe *sqe; |
196be95c | 7247 | struct io_kiocb *req; |
fb5ccc98 | 7248 | |
258b29a9 | 7249 | req = io_alloc_req(ctx); |
196be95c PB |
7250 | if (unlikely(!req)) { |
7251 | if (!submitted) | |
7252 | submitted = -EAGAIN; | |
fb5ccc98 | 7253 | break; |
196be95c | 7254 | } |
4fccfcbb PB |
7255 | sqe = io_get_sqe(ctx); |
7256 | if (unlikely(!sqe)) { | |
0c6e1d7f | 7257 | list_add(&req->inflight_entry, &ctx->submit_state.free_list); |
4fccfcbb PB |
7258 | break; |
7259 | } | |
d3656344 JA |
7260 | /* will complete beyond this point, count as submitted */ |
7261 | submitted++; | |
a1ab7b35 | 7262 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 7263 | break; |
6c271ce2 JA |
7264 | } |
7265 | ||
9466f437 PB |
7266 | if (unlikely(submitted != nr)) { |
7267 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 | 7268 | int unused = nr - ref_used; |
9466f437 | 7269 | |
09899b19 | 7270 | current->io_uring->cached_refs += unused; |
d8a6df10 | 7271 | percpu_ref_put_many(&ctx->refs, unused); |
9466f437 | 7272 | } |
6c271ce2 | 7273 | |
a1ab7b35 | 7274 | io_submit_state_end(&ctx->submit_state, ctx); |
ae9428ca PB |
7275 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
7276 | io_commit_sqring(ctx); | |
7277 | ||
6c271ce2 JA |
7278 | return submitted; |
7279 | } | |
7280 | ||
e4b6d902 PB |
7281 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
7282 | { | |
7283 | return READ_ONCE(sqd->state); | |
7284 | } | |
7285 | ||
23b3628e XW |
7286 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
7287 | { | |
7288 | /* Tell userspace we may need a wakeup call */ | |
79ebeaee | 7289 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7290 | WRITE_ONCE(ctx->rings->sq_flags, |
7291 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7292 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7293 | } |
7294 | ||
7295 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
7296 | { | |
79ebeaee | 7297 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7298 | WRITE_ONCE(ctx->rings->sq_flags, |
7299 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7300 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7301 | } |
7302 | ||
08369246 | 7303 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 7304 | { |
c8d1ba58 | 7305 | unsigned int to_submit; |
bdcd3eab | 7306 | int ret = 0; |
6c271ce2 | 7307 | |
c8d1ba58 | 7308 | to_submit = io_sqring_entries(ctx); |
e95eee2d | 7309 | /* if we're handling multiple rings, cap submit size for fairness */ |
4ce8ad95 OL |
7310 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
7311 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; | |
e95eee2d | 7312 | |
906a3c6f | 7313 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
c8d1ba58 | 7314 | unsigned nr_events = 0; |
948e1947 PB |
7315 | const struct cred *creds = NULL; |
7316 | ||
7317 | if (ctx->sq_creds != current_cred()) | |
7318 | creds = override_creds(ctx->sq_creds); | |
a4c0b3de | 7319 | |
c8d1ba58 | 7320 | mutex_lock(&ctx->uring_lock); |
906a3c6f | 7321 | if (!list_empty(&ctx->iopoll_list)) |
a8576af9 | 7322 | io_do_iopoll(ctx, &nr_events, 0); |
906a3c6f | 7323 | |
3b763ba1 PB |
7324 | /* |
7325 | * Don't submit if refs are dying, good for io_uring_register(), | |
7326 | * but also it is relied upon by io_ring_exit_work() | |
7327 | */ | |
0298ef96 PB |
7328 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
7329 | !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
08369246 | 7330 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 | 7331 | mutex_unlock(&ctx->uring_lock); |
6c271ce2 | 7332 | |
acfb381d PB |
7333 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
7334 | wake_up(&ctx->sqo_sq_wait); | |
948e1947 PB |
7335 | if (creds) |
7336 | revert_creds(creds); | |
acfb381d | 7337 | } |
6c271ce2 | 7338 | |
08369246 XW |
7339 | return ret; |
7340 | } | |
6c271ce2 | 7341 | |
08369246 XW |
7342 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
7343 | { | |
7344 | struct io_ring_ctx *ctx; | |
7345 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 7346 | |
c9dca27d PB |
7347 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7348 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); | |
08369246 | 7349 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 7350 | } |
6c271ce2 | 7351 | |
e4b6d902 PB |
7352 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
7353 | { | |
7354 | bool did_sig = false; | |
7355 | struct ksignal ksig; | |
7356 | ||
7357 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || | |
7358 | signal_pending(current)) { | |
7359 | mutex_unlock(&sqd->lock); | |
7360 | if (signal_pending(current)) | |
7361 | did_sig = get_signal(&ksig); | |
7362 | cond_resched(); | |
7363 | mutex_lock(&sqd->lock); | |
7364 | } | |
e4b6d902 PB |
7365 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
7366 | } | |
7367 | ||
c8d1ba58 JA |
7368 | static int io_sq_thread(void *data) |
7369 | { | |
69fb2131 JA |
7370 | struct io_sq_data *sqd = data; |
7371 | struct io_ring_ctx *ctx; | |
a0d9205f | 7372 | unsigned long timeout = 0; |
37d1e2e3 | 7373 | char buf[TASK_COMM_LEN]; |
08369246 | 7374 | DEFINE_WAIT(wait); |
6c271ce2 | 7375 | |
696ee88a | 7376 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
37d1e2e3 | 7377 | set_task_comm(current, buf); |
37d1e2e3 JA |
7378 | |
7379 | if (sqd->sq_cpu != -1) | |
7380 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
7381 | else | |
7382 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
7383 | current->flags |= PF_NO_SETAFFINITY; | |
7384 | ||
09a6f4ef | 7385 | mutex_lock(&sqd->lock); |
e4b6d902 | 7386 | while (1) { |
1a924a80 | 7387 | bool cap_entries, sqt_spin = false; |
c1edbf5f | 7388 | |
e4b6d902 PB |
7389 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
7390 | if (io_sqd_handle_event(sqd)) | |
c7d95613 | 7391 | break; |
08369246 XW |
7392 | timeout = jiffies + sqd->sq_thread_idle; |
7393 | } | |
e4b6d902 | 7394 | |
e95eee2d | 7395 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 7396 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
948e1947 | 7397 | int ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a | 7398 | |
08369246 XW |
7399 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) |
7400 | sqt_spin = true; | |
69fb2131 | 7401 | } |
dd432ea5 PB |
7402 | if (io_run_task_work()) |
7403 | sqt_spin = true; | |
6c271ce2 | 7404 | |
08369246 | 7405 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 7406 | cond_resched(); |
08369246 XW |
7407 | if (sqt_spin) |
7408 | timeout = jiffies + sqd->sq_thread_idle; | |
7409 | continue; | |
7410 | } | |
7411 | ||
08369246 | 7412 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
dd432ea5 | 7413 | if (!io_sqd_events_pending(sqd) && !current->task_works) { |
1a924a80 PB |
7414 | bool needs_sched = true; |
7415 | ||
724cb4f9 | 7416 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
aaa9f0f4 PB |
7417 | io_ring_set_wakeup_flag(ctx); |
7418 | ||
724cb4f9 HX |
7419 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
7420 | !list_empty_careful(&ctx->iopoll_list)) { | |
7421 | needs_sched = false; | |
7422 | break; | |
7423 | } | |
7424 | if (io_sqring_entries(ctx)) { | |
7425 | needs_sched = false; | |
7426 | break; | |
7427 | } | |
7428 | } | |
7429 | ||
7430 | if (needs_sched) { | |
7431 | mutex_unlock(&sqd->lock); | |
7432 | schedule(); | |
7433 | mutex_lock(&sqd->lock); | |
7434 | } | |
69fb2131 JA |
7435 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7436 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 7437 | } |
08369246 XW |
7438 | |
7439 | finish_wait(&sqd->wait, &wait); | |
7440 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 | 7441 | } |
28cea78a | 7442 | |
78cc687b | 7443 | io_uring_cancel_generic(true, sqd); |
37d1e2e3 | 7444 | sqd->thread = NULL; |
05962f95 | 7445 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
5f3f26f9 | 7446 | io_ring_set_wakeup_flag(ctx); |
521d6a73 | 7447 | io_run_task_work(); |
734551df PB |
7448 | mutex_unlock(&sqd->lock); |
7449 | ||
37d1e2e3 JA |
7450 | complete(&sqd->exited); |
7451 | do_exit(0); | |
6c271ce2 JA |
7452 | } |
7453 | ||
bda52162 JA |
7454 | struct io_wait_queue { |
7455 | struct wait_queue_entry wq; | |
7456 | struct io_ring_ctx *ctx; | |
5fd46178 | 7457 | unsigned cq_tail; |
bda52162 JA |
7458 | unsigned nr_timeouts; |
7459 | }; | |
7460 | ||
6c503150 | 7461 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
7462 | { |
7463 | struct io_ring_ctx *ctx = iowq->ctx; | |
5fd46178 | 7464 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
bda52162 JA |
7465 | |
7466 | /* | |
d195a66e | 7467 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
7468 | * started waiting. For timeouts, we always want to return to userspace, |
7469 | * regardless of event count. | |
7470 | */ | |
5fd46178 | 7471 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
bda52162 JA |
7472 | } |
7473 | ||
7474 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
7475 | int wake_flags, void *key) | |
7476 | { | |
7477 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
7478 | wq); | |
7479 | ||
6c503150 PB |
7480 | /* |
7481 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
7482 | * the task, and the next invocation will do it. | |
7483 | */ | |
5ed7a37d | 7484 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow)) |
6c503150 PB |
7485 | return autoremove_wake_function(curr, mode, wake_flags, key); |
7486 | return -1; | |
bda52162 JA |
7487 | } |
7488 | ||
af9c1a44 JA |
7489 | static int io_run_task_work_sig(void) |
7490 | { | |
7491 | if (io_run_task_work()) | |
7492 | return 1; | |
7493 | if (!signal_pending(current)) | |
7494 | return 0; | |
0b8cfa97 | 7495 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
792ee0f6 | 7496 | return -ERESTARTSYS; |
af9c1a44 JA |
7497 | return -EINTR; |
7498 | } | |
7499 | ||
eeb60b9a PB |
7500 | /* when returns >0, the caller should retry */ |
7501 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
7502 | struct io_wait_queue *iowq, | |
68668891 | 7503 | ktime_t timeout) |
eeb60b9a PB |
7504 | { |
7505 | int ret; | |
7506 | ||
7507 | /* make sure we run task_work before checking for signals */ | |
7508 | ret = io_run_task_work_sig(); | |
7509 | if (ret || io_should_wake(iowq)) | |
7510 | return ret; | |
7511 | /* let the caller flush overflows, retry */ | |
5ed7a37d | 7512 | if (test_bit(0, &ctx->check_cq_overflow)) |
eeb60b9a PB |
7513 | return 1; |
7514 | ||
68668891 JA |
7515 | if (!schedule_hrtimeout(&timeout, HRTIMER_MODE_ABS)) |
7516 | return -ETIME; | |
7517 | return 1; | |
eeb60b9a PB |
7518 | } |
7519 | ||
2b188cc1 JA |
7520 | /* |
7521 | * Wait until events become available, if we don't already have some. The | |
7522 | * application must reap them itself, as they reside on the shared cq ring. | |
7523 | */ | |
7524 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
7525 | const sigset_t __user *sig, size_t sigsz, |
7526 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 7527 | { |
90291099 | 7528 | struct io_wait_queue iowq; |
75b28aff | 7529 | struct io_rings *rings = ctx->rings; |
68668891 | 7530 | ktime_t timeout = KTIME_MAX; |
c1d5a224 | 7531 | int ret; |
2b188cc1 | 7532 | |
b41e9852 | 7533 | do { |
90f67366 | 7534 | io_cqring_overflow_flush(ctx); |
6c503150 | 7535 | if (io_cqring_events(ctx) >= min_events) |
b41e9852 | 7536 | return 0; |
4c6e277c | 7537 | if (!io_run_task_work()) |
b41e9852 | 7538 | break; |
b41e9852 | 7539 | } while (1); |
2b188cc1 | 7540 | |
44df58d4 XW |
7541 | if (uts) { |
7542 | struct timespec64 ts; | |
7543 | ||
7544 | if (get_timespec64(&ts, uts)) | |
7545 | return -EFAULT; | |
68668891 | 7546 | timeout = ktime_add_ns(timespec64_to_ktime(ts), ktime_get_ns()); |
44df58d4 XW |
7547 | } |
7548 | ||
2b188cc1 | 7549 | if (sig) { |
9e75ad5d AB |
7550 | #ifdef CONFIG_COMPAT |
7551 | if (in_compat_syscall()) | |
7552 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 7553 | sigsz); |
9e75ad5d AB |
7554 | else |
7555 | #endif | |
b772434b | 7556 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 7557 | |
2b188cc1 JA |
7558 | if (ret) |
7559 | return ret; | |
7560 | } | |
7561 | ||
90291099 PB |
7562 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
7563 | iowq.wq.private = current; | |
7564 | INIT_LIST_HEAD(&iowq.wq.entry); | |
7565 | iowq.ctx = ctx; | |
bda52162 | 7566 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
5fd46178 | 7567 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
90291099 | 7568 | |
c826bd7a | 7569 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 7570 | do { |
ca0a2651 | 7571 | /* if we can't even flush overflow, don't wait for more */ |
90f67366 | 7572 | if (!io_cqring_overflow_flush(ctx)) { |
ca0a2651 JA |
7573 | ret = -EBUSY; |
7574 | break; | |
7575 | } | |
311997b3 | 7576 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
bda52162 | 7577 | TASK_INTERRUPTIBLE); |
68668891 | 7578 | ret = io_cqring_wait_schedule(ctx, &iowq, timeout); |
311997b3 | 7579 | finish_wait(&ctx->cq_wait, &iowq.wq); |
ca0a2651 | 7580 | cond_resched(); |
eeb60b9a | 7581 | } while (ret > 0); |
bda52162 | 7582 | |
b7db41c9 | 7583 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7584 | |
75b28aff | 7585 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7586 | } |
7587 | ||
9123c8ff | 7588 | static void io_free_page_table(void **table, size_t size) |
05f3fb3c | 7589 | { |
9123c8ff | 7590 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
05f3fb3c | 7591 | |
846a4ef2 | 7592 | for (i = 0; i < nr_tables; i++) |
9123c8ff PB |
7593 | kfree(table[i]); |
7594 | kfree(table); | |
7595 | } | |
7596 | ||
7597 | static void **io_alloc_page_table(size_t size) | |
7598 | { | |
7599 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); | |
7600 | size_t init_size = size; | |
7601 | void **table; | |
7602 | ||
0bea96f5 | 7603 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7604 | if (!table) |
7605 | return NULL; | |
7606 | ||
7607 | for (i = 0; i < nr_tables; i++) { | |
27f6b318 | 7608 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
9123c8ff | 7609 | |
0bea96f5 | 7610 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7611 | if (!table[i]) { |
7612 | io_free_page_table(table, init_size); | |
7613 | return NULL; | |
7614 | } | |
7615 | size -= this_size; | |
7616 | } | |
7617 | return table; | |
05f3fb3c JA |
7618 | } |
7619 | ||
28a9fe25 | 7620 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
1642b445 | 7621 | { |
28a9fe25 PB |
7622 | percpu_ref_exit(&ref_node->refs); |
7623 | kfree(ref_node); | |
1642b445 PB |
7624 | } |
7625 | ||
b9bd2bea PB |
7626 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
7627 | { | |
7628 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); | |
7629 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; | |
7630 | unsigned long flags; | |
7631 | bool first_add = false; | |
817ab1d1 | 7632 | unsigned long delay = HZ; |
b9bd2bea PB |
7633 | |
7634 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); | |
7635 | node->done = true; | |
7636 | ||
817ab1d1 DY |
7637 | /* if we are mid-quiesce then do not delay */ |
7638 | if (node->rsrc_data->quiesce) | |
7639 | delay = 0; | |
7640 | ||
b9bd2bea PB |
7641 | while (!list_empty(&ctx->rsrc_ref_list)) { |
7642 | node = list_first_entry(&ctx->rsrc_ref_list, | |
7643 | struct io_rsrc_node, node); | |
7644 | /* recycle ref nodes in order */ | |
7645 | if (!node->done) | |
7646 | break; | |
7647 | list_del(&node->node); | |
7648 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); | |
7649 | } | |
7650 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); | |
7651 | ||
7652 | if (first_add) | |
817ab1d1 | 7653 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
b9bd2bea PB |
7654 | } |
7655 | ||
7656 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx) | |
7657 | { | |
7658 | struct io_rsrc_node *ref_node; | |
7659 | ||
7660 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); | |
7661 | if (!ref_node) | |
7662 | return NULL; | |
7663 | ||
7664 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, | |
7665 | 0, GFP_KERNEL)) { | |
7666 | kfree(ref_node); | |
7667 | return NULL; | |
7668 | } | |
7669 | INIT_LIST_HEAD(&ref_node->node); | |
7670 | INIT_LIST_HEAD(&ref_node->rsrc_list); | |
7671 | ref_node->done = false; | |
7672 | return ref_node; | |
7673 | } | |
7674 | ||
a7f0ed5a PB |
7675 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
7676 | struct io_rsrc_data *data_to_kill) | |
6b06314c | 7677 | { |
a7f0ed5a PB |
7678 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
7679 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); | |
6b06314c | 7680 | |
a7f0ed5a PB |
7681 | if (data_to_kill) { |
7682 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; | |
82fbcfa9 | 7683 | |
a7f0ed5a | 7684 | rsrc_node->rsrc_data = data_to_kill; |
4956b9ea | 7685 | spin_lock_irq(&ctx->rsrc_ref_lock); |
a7f0ed5a | 7686 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
4956b9ea | 7687 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
82fbcfa9 | 7688 | |
3e942498 | 7689 | atomic_inc(&data_to_kill->refs); |
a7f0ed5a PB |
7690 | percpu_ref_kill(&rsrc_node->refs); |
7691 | ctx->rsrc_node = NULL; | |
7692 | } | |
6b06314c | 7693 | |
a7f0ed5a PB |
7694 | if (!ctx->rsrc_node) { |
7695 | ctx->rsrc_node = ctx->rsrc_backup_node; | |
7696 | ctx->rsrc_backup_node = NULL; | |
7697 | } | |
8bad28d8 HX |
7698 | } |
7699 | ||
a7f0ed5a | 7700 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
8dd03afe PB |
7701 | { |
7702 | if (ctx->rsrc_backup_node) | |
7703 | return 0; | |
b895c9a6 | 7704 | ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx); |
8dd03afe | 7705 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
8bad28d8 HX |
7706 | } |
7707 | ||
40ae0ff7 | 7708 | static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, struct io_ring_ctx *ctx) |
8bad28d8 HX |
7709 | { |
7710 | int ret; | |
05589553 | 7711 | |
215c3902 | 7712 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
8bad28d8 HX |
7713 | if (data->quiesce) |
7714 | return -ENXIO; | |
05589553 | 7715 | |
8bad28d8 | 7716 | data->quiesce = true; |
1ffc5422 | 7717 | do { |
a7f0ed5a | 7718 | ret = io_rsrc_node_switch_start(ctx); |
8dd03afe | 7719 | if (ret) |
f2303b1f | 7720 | break; |
a7f0ed5a | 7721 | io_rsrc_node_switch(ctx, data); |
f2303b1f | 7722 | |
3e942498 PB |
7723 | /* kill initial ref, already quiesced if zero */ |
7724 | if (atomic_dec_and_test(&data->refs)) | |
7725 | break; | |
c018db4a | 7726 | mutex_unlock(&ctx->uring_lock); |
8bad28d8 | 7727 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 | 7728 | ret = wait_for_completion_interruptible(&data->done); |
c018db4a JA |
7729 | if (!ret) { |
7730 | mutex_lock(&ctx->uring_lock); | |
2bcee8e2 DY |
7731 | if (atomic_read(&data->refs) > 0) { |
7732 | /* | |
7733 | * it has been revived by another thread while | |
7734 | * we were unlocked | |
7735 | */ | |
7736 | mutex_unlock(&ctx->uring_lock); | |
7737 | } else { | |
7738 | break; | |
7739 | } | |
c018db4a | 7740 | } |
8bad28d8 | 7741 | |
3e942498 PB |
7742 | atomic_inc(&data->refs); |
7743 | /* wait for all works potentially completing data->done */ | |
7744 | flush_delayed_work(&ctx->rsrc_put_work); | |
cb5e1b81 | 7745 | reinit_completion(&data->done); |
8dd03afe | 7746 | |
1ffc5422 | 7747 | ret = io_run_task_work_sig(); |
8bad28d8 | 7748 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 7749 | } while (ret >= 0); |
8bad28d8 | 7750 | data->quiesce = false; |
05f3fb3c | 7751 | |
8bad28d8 | 7752 | return ret; |
d7954b2b BM |
7753 | } |
7754 | ||
2d091d62 PB |
7755 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
7756 | { | |
7757 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; | |
7758 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; | |
7759 | ||
7760 | return &data->tags[table_idx][off]; | |
7761 | } | |
7762 | ||
44b31f2f | 7763 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
1ad555c6 | 7764 | { |
2d091d62 PB |
7765 | size_t size = data->nr * sizeof(data->tags[0][0]); |
7766 | ||
7767 | if (data->tags) | |
7768 | io_free_page_table((void **)data->tags, size); | |
44b31f2f PB |
7769 | kfree(data); |
7770 | } | |
7771 | ||
d878c816 PB |
7772 | static int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
7773 | u64 __user *utags, unsigned nr, | |
7774 | struct io_rsrc_data **pdata) | |
1ad555c6 | 7775 | { |
b895c9a6 | 7776 | struct io_rsrc_data *data; |
2d091d62 | 7777 | int ret = -ENOMEM; |
d878c816 | 7778 | unsigned i; |
1ad555c6 BM |
7779 | |
7780 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7781 | if (!data) | |
d878c816 | 7782 | return -ENOMEM; |
2d091d62 | 7783 | data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0])); |
b60c8dce | 7784 | if (!data->tags) { |
1ad555c6 | 7785 | kfree(data); |
d878c816 PB |
7786 | return -ENOMEM; |
7787 | } | |
2d091d62 PB |
7788 | |
7789 | data->nr = nr; | |
7790 | data->ctx = ctx; | |
7791 | data->do_put = do_put; | |
d878c816 | 7792 | if (utags) { |
2d091d62 | 7793 | ret = -EFAULT; |
d878c816 | 7794 | for (i = 0; i < nr; i++) { |
fdd1dc31 CIK |
7795 | u64 *tag_slot = io_get_tag_slot(data, i); |
7796 | ||
7797 | if (copy_from_user(tag_slot, &utags[i], | |
7798 | sizeof(*tag_slot))) | |
2d091d62 | 7799 | goto fail; |
d878c816 | 7800 | } |
1ad555c6 | 7801 | } |
b60c8dce | 7802 | |
3e942498 | 7803 | atomic_set(&data->refs, 1); |
1ad555c6 | 7804 | init_completion(&data->done); |
d878c816 PB |
7805 | *pdata = data; |
7806 | return 0; | |
2d091d62 PB |
7807 | fail: |
7808 | io_rsrc_data_free(data); | |
7809 | return ret; | |
1ad555c6 BM |
7810 | } |
7811 | ||
9123c8ff PB |
7812 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
7813 | { | |
0bea96f5 PB |
7814 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
7815 | GFP_KERNEL_ACCOUNT); | |
9123c8ff PB |
7816 | return !!table->files; |
7817 | } | |
7818 | ||
042b0d85 | 7819 | static void io_free_file_tables(struct io_file_table *table) |
9123c8ff | 7820 | { |
042b0d85 | 7821 | kvfree(table->files); |
9123c8ff PB |
7822 | table->files = NULL; |
7823 | } | |
7824 | ||
fff4db76 | 7825 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
1ad555c6 | 7826 | { |
fff4db76 PB |
7827 | #if defined(CONFIG_UNIX) |
7828 | if (ctx->ring_sock) { | |
7829 | struct sock *sock = ctx->ring_sock->sk; | |
7830 | struct sk_buff *skb; | |
7831 | ||
7832 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
7833 | kfree_skb(skb); | |
7834 | } | |
7835 | #else | |
7836 | int i; | |
7837 | ||
7838 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7839 | struct file *file; | |
7840 | ||
7841 | file = io_file_from_index(ctx, i); | |
7842 | if (file) | |
7843 | fput(file); | |
7844 | } | |
7845 | #endif | |
042b0d85 | 7846 | io_free_file_tables(&ctx->file_table); |
44b31f2f | 7847 | io_rsrc_data_free(ctx->file_data); |
fff4db76 PB |
7848 | ctx->file_data = NULL; |
7849 | ctx->nr_user_files = 0; | |
1ad555c6 BM |
7850 | } |
7851 | ||
d7954b2b BM |
7852 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7853 | { | |
75d51314 | 7854 | unsigned nr = ctx->nr_user_files; |
d7954b2b BM |
7855 | int ret; |
7856 | ||
08480400 | 7857 | if (!ctx->file_data) |
d7954b2b | 7858 | return -ENXIO; |
75d51314 PB |
7859 | |
7860 | /* | |
7861 | * Quiesce may unlock ->uring_lock, and while it's not held | |
7862 | * prevent new requests using the table. | |
7863 | */ | |
7864 | ctx->nr_user_files = 0; | |
08480400 | 7865 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
75d51314 | 7866 | ctx->nr_user_files = nr; |
08480400 PB |
7867 | if (!ret) |
7868 | __io_sqe_files_unregister(ctx); | |
7869 | return ret; | |
6b06314c JA |
7870 | } |
7871 | ||
37d1e2e3 | 7872 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
09a6f4ef | 7873 | __releases(&sqd->lock) |
37d1e2e3 | 7874 | { |
521d6a73 PB |
7875 | WARN_ON_ONCE(sqd->thread == current); |
7876 | ||
9e138a48 PB |
7877 | /* |
7878 | * Do the dance but not conditional clear_bit() because it'd race with | |
7879 | * other threads incrementing park_pending and setting the bit. | |
7880 | */ | |
37d1e2e3 | 7881 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
9e138a48 PB |
7882 | if (atomic_dec_return(&sqd->park_pending)) |
7883 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
09a6f4ef | 7884 | mutex_unlock(&sqd->lock); |
37d1e2e3 JA |
7885 | } |
7886 | ||
86e0d676 | 7887 | static void io_sq_thread_park(struct io_sq_data *sqd) |
09a6f4ef | 7888 | __acquires(&sqd->lock) |
37d1e2e3 | 7889 | { |
521d6a73 PB |
7890 | WARN_ON_ONCE(sqd->thread == current); |
7891 | ||
9e138a48 | 7892 | atomic_inc(&sqd->park_pending); |
86e0d676 | 7893 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
09a6f4ef | 7894 | mutex_lock(&sqd->lock); |
05962f95 | 7895 | if (sqd->thread) |
86e0d676 | 7896 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
7897 | } |
7898 | ||
7899 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
7900 | { | |
521d6a73 | 7901 | WARN_ON_ONCE(sqd->thread == current); |
88885f66 | 7902 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
521d6a73 | 7903 | |
05962f95 | 7904 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
88885f66 | 7905 | mutex_lock(&sqd->lock); |
e8f98f24 JA |
7906 | if (sqd->thread) |
7907 | wake_up_process(sqd->thread); | |
09a6f4ef | 7908 | mutex_unlock(&sqd->lock); |
05962f95 | 7909 | wait_for_completion(&sqd->exited); |
37d1e2e3 JA |
7910 | } |
7911 | ||
534ca6d6 | 7912 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7913 | { |
534ca6d6 | 7914 | if (refcount_dec_and_test(&sqd->refs)) { |
9e138a48 PB |
7915 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
7916 | ||
37d1e2e3 JA |
7917 | io_sq_thread_stop(sqd); |
7918 | kfree(sqd); | |
7919 | } | |
7920 | } | |
7921 | ||
7922 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
7923 | { | |
7924 | struct io_sq_data *sqd = ctx->sq_data; | |
7925 | ||
7926 | if (sqd) { | |
05962f95 | 7927 | io_sq_thread_park(sqd); |
521d6a73 | 7928 | list_del_init(&ctx->sqd_list); |
37d1e2e3 | 7929 | io_sqd_update_thread_idle(sqd); |
05962f95 | 7930 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
7931 | |
7932 | io_put_sq_data(sqd); | |
7933 | ctx->sq_data = NULL; | |
534ca6d6 JA |
7934 | } |
7935 | } | |
7936 | ||
aa06165d JA |
7937 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7938 | { | |
7939 | struct io_ring_ctx *ctx_attach; | |
7940 | struct io_sq_data *sqd; | |
7941 | struct fd f; | |
7942 | ||
7943 | f = fdget(p->wq_fd); | |
7944 | if (!f.file) | |
7945 | return ERR_PTR(-ENXIO); | |
7946 | if (f.file->f_op != &io_uring_fops) { | |
7947 | fdput(f); | |
7948 | return ERR_PTR(-EINVAL); | |
7949 | } | |
7950 | ||
7951 | ctx_attach = f.file->private_data; | |
7952 | sqd = ctx_attach->sq_data; | |
7953 | if (!sqd) { | |
7954 | fdput(f); | |
7955 | return ERR_PTR(-EINVAL); | |
7956 | } | |
5c2469e0 JA |
7957 | if (sqd->task_tgid != current->tgid) { |
7958 | fdput(f); | |
7959 | return ERR_PTR(-EPERM); | |
7960 | } | |
aa06165d JA |
7961 | |
7962 | refcount_inc(&sqd->refs); | |
7963 | fdput(f); | |
7964 | return sqd; | |
7965 | } | |
7966 | ||
26984fbf PB |
7967 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
7968 | bool *attached) | |
534ca6d6 JA |
7969 | { |
7970 | struct io_sq_data *sqd; | |
7971 | ||
26984fbf | 7972 | *attached = false; |
5c2469e0 JA |
7973 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
7974 | sqd = io_attach_sq_data(p); | |
26984fbf PB |
7975 | if (!IS_ERR(sqd)) { |
7976 | *attached = true; | |
5c2469e0 | 7977 | return sqd; |
26984fbf | 7978 | } |
5c2469e0 JA |
7979 | /* fall through for EPERM case, setup new sqd/task */ |
7980 | if (PTR_ERR(sqd) != -EPERM) | |
7981 | return sqd; | |
7982 | } | |
aa06165d | 7983 | |
534ca6d6 JA |
7984 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7985 | if (!sqd) | |
7986 | return ERR_PTR(-ENOMEM); | |
7987 | ||
9e138a48 | 7988 | atomic_set(&sqd->park_pending, 0); |
534ca6d6 | 7989 | refcount_set(&sqd->refs, 1); |
69fb2131 | 7990 | INIT_LIST_HEAD(&sqd->ctx_list); |
09a6f4ef | 7991 | mutex_init(&sqd->lock); |
534ca6d6 | 7992 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 7993 | init_completion(&sqd->exited); |
534ca6d6 JA |
7994 | return sqd; |
7995 | } | |
7996 | ||
6b06314c | 7997 | #if defined(CONFIG_UNIX) |
6b06314c JA |
7998 | /* |
7999 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
8000 | * the io_uring can be safely unregistered on process exit, even if we have | |
8001 | * loops in the file referencing. | |
8002 | */ | |
8003 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
8004 | { | |
8005 | struct sock *sk = ctx->ring_sock->sk; | |
8006 | struct scm_fp_list *fpl; | |
8007 | struct sk_buff *skb; | |
08a45173 | 8008 | int i, nr_files; |
6b06314c | 8009 | |
6b06314c JA |
8010 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
8011 | if (!fpl) | |
8012 | return -ENOMEM; | |
8013 | ||
8014 | skb = alloc_skb(0, GFP_KERNEL); | |
8015 | if (!skb) { | |
8016 | kfree(fpl); | |
8017 | return -ENOMEM; | |
8018 | } | |
8019 | ||
8020 | skb->sk = sk; | |
6b06314c | 8021 | |
08a45173 | 8022 | nr_files = 0; |
62e398be | 8023 | fpl->user = get_uid(current_user()); |
6b06314c | 8024 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
8025 | struct file *file = io_file_from_index(ctx, i + offset); |
8026 | ||
8027 | if (!file) | |
08a45173 | 8028 | continue; |
65e19f54 | 8029 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
8030 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
8031 | nr_files++; | |
6b06314c JA |
8032 | } |
8033 | ||
08a45173 JA |
8034 | if (nr_files) { |
8035 | fpl->max = SCM_MAX_FD; | |
8036 | fpl->count = nr_files; | |
8037 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 8038 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
8039 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
8040 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 8041 | |
40d8cb26 PB |
8042 | for (i = 0; i < nr; i++) { |
8043 | struct file *file = io_file_from_index(ctx, i + offset); | |
8044 | ||
8045 | if (file) | |
8046 | fput(file); | |
8047 | } | |
08a45173 JA |
8048 | } else { |
8049 | kfree_skb(skb); | |
54c4d35a | 8050 | free_uid(fpl->user); |
08a45173 JA |
8051 | kfree(fpl); |
8052 | } | |
6b06314c JA |
8053 | |
8054 | return 0; | |
8055 | } | |
8056 | ||
8057 | /* | |
8058 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
8059 | * causes regular reference counting to break down. We rely on the UNIX | |
8060 | * garbage collection to take care of this problem for us. | |
8061 | */ | |
8062 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8063 | { | |
8064 | unsigned left, total; | |
8065 | int ret = 0; | |
8066 | ||
8067 | total = 0; | |
8068 | left = ctx->nr_user_files; | |
8069 | while (left) { | |
8070 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
8071 | |
8072 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
8073 | if (ret) | |
8074 | break; | |
8075 | left -= this_files; | |
8076 | total += this_files; | |
8077 | } | |
8078 | ||
8079 | if (!ret) | |
8080 | return 0; | |
8081 | ||
8082 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
8083 | struct file *file = io_file_from_index(ctx, total); |
8084 | ||
8085 | if (file) | |
8086 | fput(file); | |
6b06314c JA |
8087 | total++; |
8088 | } | |
8089 | ||
8090 | return ret; | |
8091 | } | |
8092 | #else | |
8093 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8094 | { | |
8095 | return 0; | |
8096 | } | |
8097 | #endif | |
8098 | ||
47e90392 | 8099 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 8100 | { |
50238531 | 8101 | struct file *file = prsrc->file; |
05f3fb3c JA |
8102 | #if defined(CONFIG_UNIX) |
8103 | struct sock *sock = ctx->ring_sock->sk; | |
8104 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
8105 | struct sk_buff *skb; | |
8106 | int i; | |
8107 | ||
8108 | __skb_queue_head_init(&list); | |
8109 | ||
8110 | /* | |
8111 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
8112 | * remove this entry and rearrange the file array. | |
8113 | */ | |
8114 | skb = skb_dequeue(head); | |
8115 | while (skb) { | |
8116 | struct scm_fp_list *fp; | |
8117 | ||
8118 | fp = UNIXCB(skb).fp; | |
8119 | for (i = 0; i < fp->count; i++) { | |
8120 | int left; | |
8121 | ||
8122 | if (fp->fp[i] != file) | |
8123 | continue; | |
8124 | ||
8125 | unix_notinflight(fp->user, fp->fp[i]); | |
8126 | left = fp->count - 1 - i; | |
8127 | if (left) { | |
8128 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
8129 | left * sizeof(struct file *)); | |
8130 | } | |
8131 | fp->count--; | |
8132 | if (!fp->count) { | |
8133 | kfree_skb(skb); | |
8134 | skb = NULL; | |
8135 | } else { | |
8136 | __skb_queue_tail(&list, skb); | |
8137 | } | |
8138 | fput(file); | |
8139 | file = NULL; | |
8140 | break; | |
8141 | } | |
8142 | ||
8143 | if (!file) | |
8144 | break; | |
8145 | ||
8146 | __skb_queue_tail(&list, skb); | |
8147 | ||
8148 | skb = skb_dequeue(head); | |
8149 | } | |
8150 | ||
8151 | if (skb_peek(&list)) { | |
8152 | spin_lock_irq(&head->lock); | |
8153 | while ((skb = __skb_dequeue(&list)) != NULL) | |
8154 | __skb_queue_tail(head, skb); | |
8155 | spin_unlock_irq(&head->lock); | |
8156 | } | |
8157 | #else | |
8158 | fput(file); | |
8159 | #endif | |
8160 | } | |
8161 | ||
b895c9a6 | 8162 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
65e19f54 | 8163 | { |
b895c9a6 | 8164 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
269bbe5f BM |
8165 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
8166 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 8167 | |
269bbe5f BM |
8168 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
8169 | list_del(&prsrc->list); | |
b60c8dce PB |
8170 | |
8171 | if (prsrc->tag) { | |
8172 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; | |
b60c8dce PB |
8173 | |
8174 | io_ring_submit_lock(ctx, lock_ring); | |
79ebeaee | 8175 | spin_lock(&ctx->completion_lock); |
142f794e | 8176 | io_fill_cqe_aux(ctx, prsrc->tag, 0, 0); |
b60c8dce | 8177 | io_commit_cqring(ctx); |
79ebeaee | 8178 | spin_unlock(&ctx->completion_lock); |
b60c8dce PB |
8179 | io_cqring_ev_posted(ctx); |
8180 | io_ring_submit_unlock(ctx, lock_ring); | |
8181 | } | |
8182 | ||
40ae0ff7 | 8183 | rsrc_data->do_put(ctx, prsrc); |
269bbe5f | 8184 | kfree(prsrc); |
65e19f54 | 8185 | } |
05589553 | 8186 | |
28a9fe25 | 8187 | io_rsrc_node_destroy(ref_node); |
3e942498 PB |
8188 | if (atomic_dec_and_test(&rsrc_data->refs)) |
8189 | complete(&rsrc_data->done); | |
2faf852d | 8190 | } |
65e19f54 | 8191 | |
269bbe5f | 8192 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
8193 | { |
8194 | struct io_ring_ctx *ctx; | |
8195 | struct llist_node *node; | |
8196 | ||
269bbe5f BM |
8197 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
8198 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
8199 | |
8200 | while (node) { | |
b895c9a6 | 8201 | struct io_rsrc_node *ref_node; |
4a38aed2 JA |
8202 | struct llist_node *next = node->next; |
8203 | ||
b895c9a6 | 8204 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
269bbe5f | 8205 | __io_rsrc_put_work(ref_node); |
4a38aed2 JA |
8206 | node = next; |
8207 | } | |
8208 | } | |
8209 | ||
6b06314c | 8210 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
792e3582 | 8211 | unsigned nr_args, u64 __user *tags) |
6b06314c JA |
8212 | { |
8213 | __s32 __user *fds = (__s32 __user *) arg; | |
05f3fb3c | 8214 | struct file *file; |
f3baed39 | 8215 | int fd, ret; |
846a4ef2 | 8216 | unsigned i; |
6b06314c | 8217 | |
05f3fb3c | 8218 | if (ctx->file_data) |
6b06314c JA |
8219 | return -EBUSY; |
8220 | if (!nr_args) | |
8221 | return -EINVAL; | |
8222 | if (nr_args > IORING_MAX_FIXED_FILES) | |
8223 | return -EMFILE; | |
3a1b8a4e PB |
8224 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
8225 | return -EMFILE; | |
a7f0ed5a | 8226 | ret = io_rsrc_node_switch_start(ctx); |
f3baed39 PB |
8227 | if (ret) |
8228 | return ret; | |
d878c816 PB |
8229 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
8230 | &ctx->file_data); | |
8231 | if (ret) | |
8232 | return ret; | |
6b06314c | 8233 | |
f3baed39 | 8234 | ret = -ENOMEM; |
aeca241b | 8235 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
1ad555c6 | 8236 | goto out_free; |
65e19f54 | 8237 | |
08a45173 | 8238 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
d878c816 | 8239 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
600cf3f8 PB |
8240 | ret = -EFAULT; |
8241 | goto out_fput; | |
8242 | } | |
08a45173 | 8243 | /* allow sparse sets */ |
792e3582 PB |
8244 | if (fd == -1) { |
8245 | ret = -EINVAL; | |
2d091d62 | 8246 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
792e3582 | 8247 | goto out_fput; |
08a45173 | 8248 | continue; |
792e3582 | 8249 | } |
6b06314c | 8250 | |
05f3fb3c | 8251 | file = fget(fd); |
6b06314c | 8252 | ret = -EBADF; |
792e3582 | 8253 | if (unlikely(!file)) |
600cf3f8 | 8254 | goto out_fput; |
05f3fb3c | 8255 | |
6b06314c JA |
8256 | /* |
8257 | * Don't allow io_uring instances to be registered. If UNIX | |
8258 | * isn't enabled, then this causes a reference cycle and this | |
8259 | * instance can never get freed. If UNIX is enabled we'll | |
8260 | * handle it just fine, but there's still no point in allowing | |
8261 | * a ring fd as it doesn't support regular read/write anyway. | |
8262 | */ | |
05f3fb3c JA |
8263 | if (file->f_op == &io_uring_fops) { |
8264 | fput(file); | |
600cf3f8 | 8265 | goto out_fput; |
6b06314c | 8266 | } |
aeca241b | 8267 | io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file); |
6b06314c JA |
8268 | } |
8269 | ||
6b06314c | 8270 | ret = io_sqe_files_scm(ctx); |
05589553 | 8271 | if (ret) { |
08480400 | 8272 | __io_sqe_files_unregister(ctx); |
05589553 XW |
8273 | return ret; |
8274 | } | |
6b06314c | 8275 | |
a7f0ed5a | 8276 | io_rsrc_node_switch(ctx, NULL); |
6b06314c | 8277 | return ret; |
600cf3f8 PB |
8278 | out_fput: |
8279 | for (i = 0; i < ctx->nr_user_files; i++) { | |
8280 | file = io_file_from_index(ctx, i); | |
8281 | if (file) | |
8282 | fput(file); | |
8283 | } | |
042b0d85 | 8284 | io_free_file_tables(&ctx->file_table); |
600cf3f8 | 8285 | ctx->nr_user_files = 0; |
600cf3f8 | 8286 | out_free: |
44b31f2f | 8287 | io_rsrc_data_free(ctx->file_data); |
55cbc256 | 8288 | ctx->file_data = NULL; |
6b06314c JA |
8289 | return ret; |
8290 | } | |
8291 | ||
c3a31e60 JA |
8292 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
8293 | int index) | |
8294 | { | |
8295 | #if defined(CONFIG_UNIX) | |
8296 | struct sock *sock = ctx->ring_sock->sk; | |
8297 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
8298 | struct sk_buff *skb; | |
8299 | ||
8300 | /* | |
8301 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
8302 | * file set. If there's no room, fall back to allocating a new skb | |
8303 | * and filling it in. | |
8304 | */ | |
8305 | spin_lock_irq(&head->lock); | |
8306 | skb = skb_peek(head); | |
8307 | if (skb) { | |
8308 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
8309 | ||
8310 | if (fpl->count < SCM_MAX_FD) { | |
8311 | __skb_unlink(skb, head); | |
8312 | spin_unlock_irq(&head->lock); | |
8313 | fpl->fp[fpl->count] = get_file(file); | |
8314 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
8315 | fpl->count++; | |
8316 | spin_lock_irq(&head->lock); | |
8317 | __skb_queue_head(head, skb); | |
8318 | } else { | |
8319 | skb = NULL; | |
8320 | } | |
8321 | } | |
8322 | spin_unlock_irq(&head->lock); | |
8323 | ||
8324 | if (skb) { | |
8325 | fput(file); | |
8326 | return 0; | |
8327 | } | |
8328 | ||
8329 | return __io_sqe_files_scm(ctx, 1, index); | |
8330 | #else | |
8331 | return 0; | |
8332 | #endif | |
8333 | } | |
8334 | ||
9c7b0ba8 PB |
8335 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
8336 | struct io_rsrc_node *node, void *rsrc) | |
8337 | { | |
c47dfb44 | 8338 | u64 *tag_slot = io_get_tag_slot(data, idx); |
9c7b0ba8 PB |
8339 | struct io_rsrc_put *prsrc; |
8340 | ||
8341 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); | |
8342 | if (!prsrc) | |
8343 | return -ENOMEM; | |
8344 | ||
c47dfb44 PB |
8345 | prsrc->tag = *tag_slot; |
8346 | *tag_slot = 0; | |
9c7b0ba8 PB |
8347 | prsrc->rsrc = rsrc; |
8348 | list_add(&prsrc->list, &node->rsrc_list); | |
8349 | return 0; | |
8350 | } | |
8351 | ||
b9445598 PB |
8352 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
8353 | unsigned int issue_flags, u32 slot_index) | |
8354 | { | |
8355 | struct io_ring_ctx *ctx = req->ctx; | |
8356 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; | |
9c7b0ba8 | 8357 | bool needs_switch = false; |
b9445598 PB |
8358 | struct io_fixed_file *file_slot; |
8359 | int ret = -EBADF; | |
8360 | ||
8361 | io_ring_submit_lock(ctx, !force_nonblock); | |
8362 | if (file->f_op == &io_uring_fops) | |
8363 | goto err; | |
8364 | ret = -ENXIO; | |
8365 | if (!ctx->file_data) | |
8366 | goto err; | |
8367 | ret = -EINVAL; | |
8368 | if (slot_index >= ctx->nr_user_files) | |
8369 | goto err; | |
8370 | ||
8371 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); | |
8372 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); | |
9c7b0ba8 PB |
8373 | |
8374 | if (file_slot->file_ptr) { | |
8375 | struct file *old_file; | |
8376 | ||
8377 | ret = io_rsrc_node_switch_start(ctx); | |
8378 | if (ret) | |
8379 | goto err; | |
8380 | ||
8381 | old_file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8382 | ret = io_queue_rsrc_removal(ctx->file_data, slot_index, | |
8383 | ctx->rsrc_node, old_file); | |
8384 | if (ret) | |
8385 | goto err; | |
8386 | file_slot->file_ptr = 0; | |
8387 | needs_switch = true; | |
8388 | } | |
b9445598 PB |
8389 | |
8390 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; | |
8391 | io_fixed_file_set(file_slot, file); | |
8392 | ret = io_sqe_file_register(ctx, file, slot_index); | |
8393 | if (ret) { | |
8394 | file_slot->file_ptr = 0; | |
8395 | goto err; | |
8396 | } | |
8397 | ||
8398 | ret = 0; | |
8399 | err: | |
9c7b0ba8 PB |
8400 | if (needs_switch) |
8401 | io_rsrc_node_switch(ctx, ctx->file_data); | |
b9445598 PB |
8402 | io_ring_submit_unlock(ctx, !force_nonblock); |
8403 | if (ret) | |
8404 | fput(file); | |
8405 | return ret; | |
8406 | } | |
8407 | ||
7df778be PB |
8408 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) |
8409 | { | |
8410 | unsigned int offset = req->close.file_slot - 1; | |
8411 | struct io_ring_ctx *ctx = req->ctx; | |
8412 | struct io_fixed_file *file_slot; | |
8413 | struct file *file; | |
22a99dcb | 8414 | int ret; |
7df778be PB |
8415 | |
8416 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
8417 | ret = -ENXIO; | |
8418 | if (unlikely(!ctx->file_data)) | |
8419 | goto out; | |
8420 | ret = -EINVAL; | |
8421 | if (offset >= ctx->nr_user_files) | |
8422 | goto out; | |
8423 | ret = io_rsrc_node_switch_start(ctx); | |
8424 | if (ret) | |
8425 | goto out; | |
8426 | ||
22a99dcb PB |
8427 | offset = array_index_nospec(offset, ctx->nr_user_files); |
8428 | file_slot = io_fixed_file_slot(&ctx->file_table, offset); | |
7df778be PB |
8429 | ret = -EBADF; |
8430 | if (!file_slot->file_ptr) | |
8431 | goto out; | |
8432 | ||
8433 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
8434 | ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file); | |
8435 | if (ret) | |
8436 | goto out; | |
8437 | ||
8438 | file_slot->file_ptr = 0; | |
8439 | io_rsrc_node_switch(ctx, ctx->file_data); | |
8440 | ret = 0; | |
8441 | out: | |
8442 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
8443 | return ret; | |
8444 | } | |
8445 | ||
05f3fb3c | 8446 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
c3bdad02 | 8447 | struct io_uring_rsrc_update2 *up, |
05f3fb3c JA |
8448 | unsigned nr_args) |
8449 | { | |
c3bdad02 | 8450 | u64 __user *tags = u64_to_user_ptr(up->tags); |
98f0b3b4 | 8451 | __s32 __user *fds = u64_to_user_ptr(up->data); |
b895c9a6 | 8452 | struct io_rsrc_data *data = ctx->file_data; |
a04b0ac0 PB |
8453 | struct io_fixed_file *file_slot; |
8454 | struct file *file; | |
98f0b3b4 PB |
8455 | int fd, i, err = 0; |
8456 | unsigned int done; | |
05589553 | 8457 | bool needs_switch = false; |
c3a31e60 | 8458 | |
98f0b3b4 PB |
8459 | if (!ctx->file_data) |
8460 | return -ENXIO; | |
8461 | if (up->offset + nr_args > ctx->nr_user_files) | |
c3a31e60 JA |
8462 | return -EINVAL; |
8463 | ||
67973b93 | 8464 | for (done = 0; done < nr_args; done++) { |
c3bdad02 PB |
8465 | u64 tag = 0; |
8466 | ||
8467 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || | |
8468 | copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
c3a31e60 JA |
8469 | err = -EFAULT; |
8470 | break; | |
8471 | } | |
c3bdad02 PB |
8472 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
8473 | err = -EINVAL; | |
8474 | break; | |
8475 | } | |
4e0377a1 | 8476 | if (fd == IORING_REGISTER_FILES_SKIP) |
8477 | continue; | |
8478 | ||
67973b93 | 8479 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
aeca241b | 8480 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
ea64ec02 | 8481 | |
a04b0ac0 PB |
8482 | if (file_slot->file_ptr) { |
8483 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
22a99dcb | 8484 | err = io_queue_rsrc_removal(data, i, ctx->rsrc_node, file); |
a5318d3c HD |
8485 | if (err) |
8486 | break; | |
a04b0ac0 | 8487 | file_slot->file_ptr = 0; |
05589553 | 8488 | needs_switch = true; |
c3a31e60 JA |
8489 | } |
8490 | if (fd != -1) { | |
c3a31e60 JA |
8491 | file = fget(fd); |
8492 | if (!file) { | |
8493 | err = -EBADF; | |
8494 | break; | |
8495 | } | |
8496 | /* | |
8497 | * Don't allow io_uring instances to be registered. If | |
8498 | * UNIX isn't enabled, then this causes a reference | |
8499 | * cycle and this instance can never get freed. If UNIX | |
8500 | * is enabled we'll handle it just fine, but there's | |
8501 | * still no point in allowing a ring fd as it doesn't | |
8502 | * support regular read/write anyway. | |
8503 | */ | |
8504 | if (file->f_op == &io_uring_fops) { | |
8505 | fput(file); | |
8506 | err = -EBADF; | |
8507 | break; | |
8508 | } | |
1dc25797 | 8509 | *io_get_tag_slot(data, i) = tag; |
9a321c98 | 8510 | io_fixed_file_set(file_slot, file); |
c3a31e60 | 8511 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 8512 | if (err) { |
a04b0ac0 | 8513 | file_slot->file_ptr = 0; |
f3bd9dae | 8514 | fput(file); |
c3a31e60 | 8515 | break; |
f3bd9dae | 8516 | } |
c3a31e60 | 8517 | } |
05f3fb3c JA |
8518 | } |
8519 | ||
a7f0ed5a PB |
8520 | if (needs_switch) |
8521 | io_rsrc_node_switch(ctx, data); | |
c3a31e60 JA |
8522 | return done ? done : err; |
8523 | } | |
05589553 | 8524 | |
685fe7fe JA |
8525 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
8526 | struct task_struct *task) | |
24369c2e | 8527 | { |
e941894e | 8528 | struct io_wq_hash *hash; |
24369c2e | 8529 | struct io_wq_data data; |
24369c2e | 8530 | unsigned int concurrency; |
24369c2e | 8531 | |
362a9e65 | 8532 | mutex_lock(&ctx->uring_lock); |
e941894e JA |
8533 | hash = ctx->hash_map; |
8534 | if (!hash) { | |
8535 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
362a9e65 YY |
8536 | if (!hash) { |
8537 | mutex_unlock(&ctx->uring_lock); | |
e941894e | 8538 | return ERR_PTR(-ENOMEM); |
362a9e65 | 8539 | } |
e941894e JA |
8540 | refcount_set(&hash->refs, 1); |
8541 | init_waitqueue_head(&hash->wait); | |
8542 | ctx->hash_map = hash; | |
24369c2e | 8543 | } |
362a9e65 | 8544 | mutex_unlock(&ctx->uring_lock); |
24369c2e | 8545 | |
e941894e | 8546 | data.hash = hash; |
685fe7fe | 8547 | data.task = task; |
ebc11b6c | 8548 | data.free_work = io_wq_free_work; |
f5fa38c5 | 8549 | data.do_work = io_wq_submit_work; |
24369c2e | 8550 | |
d25e3a3d JA |
8551 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
8552 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 8553 | |
5aa75ed5 | 8554 | return io_wq_create(concurrency, &data); |
24369c2e PB |
8555 | } |
8556 | ||
5aa75ed5 JA |
8557 | static int io_uring_alloc_task_context(struct task_struct *task, |
8558 | struct io_ring_ctx *ctx) | |
0f212204 JA |
8559 | { |
8560 | struct io_uring_task *tctx; | |
d8a6df10 | 8561 | int ret; |
0f212204 | 8562 | |
09899b19 | 8563 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
0f212204 JA |
8564 | if (unlikely(!tctx)) |
8565 | return -ENOMEM; | |
8566 | ||
d8a6df10 JA |
8567 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
8568 | if (unlikely(ret)) { | |
8569 | kfree(tctx); | |
8570 | return ret; | |
8571 | } | |
8572 | ||
685fe7fe | 8573 | tctx->io_wq = io_init_wq_offload(ctx, task); |
5aa75ed5 JA |
8574 | if (IS_ERR(tctx->io_wq)) { |
8575 | ret = PTR_ERR(tctx->io_wq); | |
8576 | percpu_counter_destroy(&tctx->inflight); | |
8577 | kfree(tctx); | |
8578 | return ret; | |
8579 | } | |
8580 | ||
0f212204 JA |
8581 | xa_init(&tctx->xa); |
8582 | init_waitqueue_head(&tctx->wait); | |
fdaf083c | 8583 | atomic_set(&tctx->in_idle, 0); |
b303fe2e | 8584 | atomic_set(&tctx->inflight_tracked, 0); |
0f212204 | 8585 | task->io_uring = tctx; |
7cbf1722 JA |
8586 | spin_lock_init(&tctx->task_lock); |
8587 | INIT_WQ_LIST(&tctx->task_list); | |
7cbf1722 | 8588 | init_task_work(&tctx->task_work, tctx_task_work); |
0f212204 JA |
8589 | return 0; |
8590 | } | |
8591 | ||
8592 | void __io_uring_free(struct task_struct *tsk) | |
8593 | { | |
8594 | struct io_uring_task *tctx = tsk->io_uring; | |
8595 | ||
8596 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e | 8597 | WARN_ON_ONCE(tctx->io_wq); |
09899b19 | 8598 | WARN_ON_ONCE(tctx->cached_refs); |
ef8eaa4e | 8599 | |
d8a6df10 | 8600 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
8601 | kfree(tctx); |
8602 | tsk->io_uring = NULL; | |
8603 | } | |
8604 | ||
7e84e1c7 SG |
8605 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
8606 | struct io_uring_params *p) | |
2b188cc1 JA |
8607 | { |
8608 | int ret; | |
8609 | ||
d25e3a3d JA |
8610 | /* Retain compatibility with failing for an invalid attach attempt */ |
8611 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
8612 | IORING_SETUP_ATTACH_WQ) { | |
8613 | struct fd f; | |
8614 | ||
8615 | f = fdget(p->wq_fd); | |
8616 | if (!f.file) | |
8617 | return -ENXIO; | |
0cc936f7 JA |
8618 | if (f.file->f_op != &io_uring_fops) { |
8619 | fdput(f); | |
f2a48dd0 | 8620 | return -EINVAL; |
0cc936f7 JA |
8621 | } |
8622 | fdput(f); | |
d25e3a3d | 8623 | } |
6c271ce2 | 8624 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 8625 | struct task_struct *tsk; |
534ca6d6 | 8626 | struct io_sq_data *sqd; |
26984fbf | 8627 | bool attached; |
534ca6d6 | 8628 | |
26984fbf | 8629 | sqd = io_get_sq_data(p, &attached); |
534ca6d6 JA |
8630 | if (IS_ERR(sqd)) { |
8631 | ret = PTR_ERR(sqd); | |
8632 | goto err; | |
8633 | } | |
69fb2131 | 8634 | |
7c30f36a | 8635 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 8636 | ctx->sq_data = sqd; |
917257da JA |
8637 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
8638 | if (!ctx->sq_thread_idle) | |
8639 | ctx->sq_thread_idle = HZ; | |
8640 | ||
78d7f6ba | 8641 | io_sq_thread_park(sqd); |
de75a3d3 PB |
8642 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
8643 | io_sqd_update_thread_idle(sqd); | |
26984fbf | 8644 | /* don't attach to a dying SQPOLL thread, would be racy */ |
f2a48dd0 | 8645 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
78d7f6ba PB |
8646 | io_sq_thread_unpark(sqd); |
8647 | ||
de75a3d3 PB |
8648 | if (ret < 0) |
8649 | goto err; | |
8650 | if (attached) | |
5aa75ed5 | 8651 | return 0; |
aa06165d | 8652 | |
6c271ce2 | 8653 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 8654 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 8655 | |
917257da | 8656 | ret = -EINVAL; |
f2a48dd0 | 8657 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
e8f98f24 | 8658 | goto err_sqpoll; |
37d1e2e3 | 8659 | sqd->sq_cpu = cpu; |
6c271ce2 | 8660 | } else { |
37d1e2e3 | 8661 | sqd->sq_cpu = -1; |
6c271ce2 | 8662 | } |
37d1e2e3 JA |
8663 | |
8664 | sqd->task_pid = current->pid; | |
5c2469e0 | 8665 | sqd->task_tgid = current->tgid; |
46fe18b1 JA |
8666 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
8667 | if (IS_ERR(tsk)) { | |
8668 | ret = PTR_ERR(tsk); | |
e8f98f24 | 8669 | goto err_sqpoll; |
6c271ce2 | 8670 | } |
97a73a0f | 8671 | |
46fe18b1 | 8672 | sqd->thread = tsk; |
97a73a0f | 8673 | ret = io_uring_alloc_task_context(tsk, ctx); |
46fe18b1 | 8674 | wake_up_new_task(tsk); |
0f212204 JA |
8675 | if (ret) |
8676 | goto err; | |
6c271ce2 JA |
8677 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
8678 | /* Can't have SQ_AFF without SQPOLL */ | |
8679 | ret = -EINVAL; | |
8680 | goto err; | |
8681 | } | |
8682 | ||
2b188cc1 | 8683 | return 0; |
f2a48dd0 PB |
8684 | err_sqpoll: |
8685 | complete(&ctx->sq_data->exited); | |
2b188cc1 | 8686 | err: |
37d1e2e3 | 8687 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
8688 | return ret; |
8689 | } | |
8690 | ||
a087e2b5 BM |
8691 | static inline void __io_unaccount_mem(struct user_struct *user, |
8692 | unsigned long nr_pages) | |
2b188cc1 JA |
8693 | { |
8694 | atomic_long_sub(nr_pages, &user->locked_vm); | |
8695 | } | |
8696 | ||
a087e2b5 BM |
8697 | static inline int __io_account_mem(struct user_struct *user, |
8698 | unsigned long nr_pages) | |
2b188cc1 JA |
8699 | { |
8700 | unsigned long page_limit, cur_pages, new_pages; | |
8701 | ||
8702 | /* Don't allow more pages than we can safely lock */ | |
8703 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
8704 | ||
8705 | do { | |
8706 | cur_pages = atomic_long_read(&user->locked_vm); | |
8707 | new_pages = cur_pages + nr_pages; | |
8708 | if (new_pages > page_limit) | |
8709 | return -ENOMEM; | |
8710 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
8711 | new_pages) != cur_pages); | |
8712 | ||
8713 | return 0; | |
8714 | } | |
8715 | ||
26bfa89e | 8716 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8717 | { |
62e398be | 8718 | if (ctx->user) |
a087e2b5 | 8719 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 8720 | |
26bfa89e JA |
8721 | if (ctx->mm_account) |
8722 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8723 | } |
8724 | ||
26bfa89e | 8725 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8726 | { |
30975825 BM |
8727 | int ret; |
8728 | ||
62e398be | 8729 | if (ctx->user) { |
30975825 BM |
8730 | ret = __io_account_mem(ctx->user, nr_pages); |
8731 | if (ret) | |
8732 | return ret; | |
8733 | } | |
8734 | ||
26bfa89e JA |
8735 | if (ctx->mm_account) |
8736 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8737 | |
8738 | return 0; | |
8739 | } | |
8740 | ||
2b188cc1 JA |
8741 | static void io_mem_free(void *ptr) |
8742 | { | |
52e04ef4 MR |
8743 | struct page *page; |
8744 | ||
8745 | if (!ptr) | |
8746 | return; | |
2b188cc1 | 8747 | |
52e04ef4 | 8748 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8749 | if (put_page_testzero(page)) |
8750 | free_compound_page(page); | |
8751 | } | |
8752 | ||
8753 | static void *io_mem_alloc(size_t size) | |
8754 | { | |
44736143 | 8755 | gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP; |
2b188cc1 | 8756 | |
44736143 | 8757 | return (void *) __get_free_pages(gfp, get_order(size)); |
2b188cc1 JA |
8758 | } |
8759 | ||
75b28aff HV |
8760 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8761 | size_t *sq_offset) | |
8762 | { | |
8763 | struct io_rings *rings; | |
8764 | size_t off, sq_array_size; | |
8765 | ||
8766 | off = struct_size(rings, cqes, cq_entries); | |
8767 | if (off == SIZE_MAX) | |
8768 | return SIZE_MAX; | |
8769 | ||
8770 | #ifdef CONFIG_SMP | |
8771 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8772 | if (off == 0) | |
8773 | return SIZE_MAX; | |
8774 | #endif | |
8775 | ||
b36200f5 DV |
8776 | if (sq_offset) |
8777 | *sq_offset = off; | |
8778 | ||
75b28aff HV |
8779 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8780 | if (sq_array_size == SIZE_MAX) | |
8781 | return SIZE_MAX; | |
8782 | ||
8783 | if (check_add_overflow(off, sq_array_size, &off)) | |
8784 | return SIZE_MAX; | |
8785 | ||
75b28aff HV |
8786 | return off; |
8787 | } | |
8788 | ||
41edf1a5 | 8789 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot) |
7f61a1e9 | 8790 | { |
41edf1a5 | 8791 | struct io_mapped_ubuf *imu = *slot; |
7f61a1e9 PB |
8792 | unsigned int i; |
8793 | ||
6224843d PB |
8794 | if (imu != ctx->dummy_ubuf) { |
8795 | for (i = 0; i < imu->nr_bvecs; i++) | |
8796 | unpin_user_page(imu->bvec[i].bv_page); | |
8797 | if (imu->acct_pages) | |
8798 | io_unaccount_mem(ctx, imu->acct_pages); | |
8799 | kvfree(imu); | |
8800 | } | |
41edf1a5 | 8801 | *slot = NULL; |
7f61a1e9 PB |
8802 | } |
8803 | ||
bd54b6fe | 8804 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
edafccee | 8805 | { |
634d00df PB |
8806 | io_buffer_unmap(ctx, &prsrc->buf); |
8807 | prsrc->buf = NULL; | |
bd54b6fe | 8808 | } |
edafccee | 8809 | |
bd54b6fe BM |
8810 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
8811 | { | |
8812 | unsigned int i; | |
edafccee | 8813 | |
7f61a1e9 PB |
8814 | for (i = 0; i < ctx->nr_user_bufs; i++) |
8815 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); | |
edafccee | 8816 | kfree(ctx->user_bufs); |
bb6659cc | 8817 | io_rsrc_data_free(ctx->buf_data); |
edafccee | 8818 | ctx->user_bufs = NULL; |
bd54b6fe | 8819 | ctx->buf_data = NULL; |
edafccee | 8820 | ctx->nr_user_bufs = 0; |
bd54b6fe BM |
8821 | } |
8822 | ||
0a96bbe4 | 8823 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee | 8824 | { |
44aa2b86 | 8825 | unsigned nr = ctx->nr_user_bufs; |
bd54b6fe | 8826 | int ret; |
edafccee | 8827 | |
bd54b6fe | 8828 | if (!ctx->buf_data) |
edafccee JA |
8829 | return -ENXIO; |
8830 | ||
44aa2b86 PB |
8831 | /* |
8832 | * Quiesce may unlock ->uring_lock, and while it's not held | |
8833 | * prevent new requests using the table. | |
8834 | */ | |
8835 | ctx->nr_user_bufs = 0; | |
bd54b6fe | 8836 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
44aa2b86 | 8837 | ctx->nr_user_bufs = nr; |
bd54b6fe BM |
8838 | if (!ret) |
8839 | __io_sqe_buffers_unregister(ctx); | |
8840 | return ret; | |
edafccee JA |
8841 | } |
8842 | ||
8843 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8844 | void __user *arg, unsigned index) | |
8845 | { | |
8846 | struct iovec __user *src; | |
8847 | ||
8848 | #ifdef CONFIG_COMPAT | |
8849 | if (ctx->compat) { | |
8850 | struct compat_iovec __user *ciovs; | |
8851 | struct compat_iovec ciov; | |
8852 | ||
8853 | ciovs = (struct compat_iovec __user *) arg; | |
8854 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8855 | return -EFAULT; | |
8856 | ||
d55e5f5b | 8857 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8858 | dst->iov_len = ciov.iov_len; |
8859 | return 0; | |
8860 | } | |
8861 | #endif | |
8862 | src = (struct iovec __user *) arg; | |
8863 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8864 | return -EFAULT; | |
8865 | return 0; | |
8866 | } | |
8867 | ||
de293938 JA |
8868 | /* |
8869 | * Not super efficient, but this is just a registration time. And we do cache | |
8870 | * the last compound head, so generally we'll only do a full search if we don't | |
8871 | * match that one. | |
8872 | * | |
8873 | * We check if the given compound head page has already been accounted, to | |
8874 | * avoid double accounting it. This allows us to account the full size of the | |
8875 | * page, not just the constituent pages of a huge page. | |
8876 | */ | |
8877 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8878 | int nr_pages, struct page *hpage) | |
8879 | { | |
8880 | int i, j; | |
8881 | ||
8882 | /* check current page array */ | |
8883 | for (i = 0; i < nr_pages; i++) { | |
8884 | if (!PageCompound(pages[i])) | |
8885 | continue; | |
8886 | if (compound_head(pages[i]) == hpage) | |
8887 | return true; | |
8888 | } | |
8889 | ||
8890 | /* check previously registered pages */ | |
8891 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
41edf1a5 | 8892 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
de293938 JA |
8893 | |
8894 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8895 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8896 | continue; | |
8897 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8898 | return true; | |
8899 | } | |
8900 | } | |
8901 | ||
8902 | return false; | |
8903 | } | |
8904 | ||
8905 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8906 | int nr_pages, struct io_mapped_ubuf *imu, | |
8907 | struct page **last_hpage) | |
8908 | { | |
8909 | int i, ret; | |
8910 | ||
216e5835 | 8911 | imu->acct_pages = 0; |
de293938 JA |
8912 | for (i = 0; i < nr_pages; i++) { |
8913 | if (!PageCompound(pages[i])) { | |
8914 | imu->acct_pages++; | |
8915 | } else { | |
8916 | struct page *hpage; | |
8917 | ||
8918 | hpage = compound_head(pages[i]); | |
8919 | if (hpage == *last_hpage) | |
8920 | continue; | |
8921 | *last_hpage = hpage; | |
8922 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8923 | continue; | |
8924 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8925 | } | |
8926 | } | |
8927 | ||
8928 | if (!imu->acct_pages) | |
8929 | return 0; | |
8930 | ||
26bfa89e | 8931 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
8932 | if (ret) |
8933 | imu->acct_pages = 0; | |
8934 | return ret; | |
8935 | } | |
8936 | ||
0a96bbe4 | 8937 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
41edf1a5 | 8938 | struct io_mapped_ubuf **pimu, |
0a96bbe4 | 8939 | struct page **last_hpage) |
edafccee | 8940 | { |
41edf1a5 | 8941 | struct io_mapped_ubuf *imu = NULL; |
edafccee JA |
8942 | struct vm_area_struct **vmas = NULL; |
8943 | struct page **pages = NULL; | |
0a96bbe4 BM |
8944 | unsigned long off, start, end, ubuf; |
8945 | size_t size; | |
8946 | int ret, pret, nr_pages, i; | |
8947 | ||
6224843d PB |
8948 | if (!iov->iov_base) { |
8949 | *pimu = ctx->dummy_ubuf; | |
8950 | return 0; | |
8951 | } | |
8952 | ||
0a96bbe4 BM |
8953 | ubuf = (unsigned long) iov->iov_base; |
8954 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8955 | start = ubuf >> PAGE_SHIFT; | |
8956 | nr_pages = end - start; | |
8957 | ||
41edf1a5 | 8958 | *pimu = NULL; |
0a96bbe4 BM |
8959 | ret = -ENOMEM; |
8960 | ||
8961 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8962 | if (!pages) | |
8963 | goto done; | |
8964 | ||
8965 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8966 | GFP_KERNEL); | |
8967 | if (!vmas) | |
8968 | goto done; | |
edafccee | 8969 | |
41edf1a5 | 8970 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
a2b4198c | 8971 | if (!imu) |
0a96bbe4 BM |
8972 | goto done; |
8973 | ||
8974 | ret = 0; | |
8975 | mmap_read_lock(current->mm); | |
8976 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8977 | pages, vmas); | |
8978 | if (pret == nr_pages) { | |
8979 | /* don't support file backed memory */ | |
8980 | for (i = 0; i < nr_pages; i++) { | |
8981 | struct vm_area_struct *vma = vmas[i]; | |
8982 | ||
40dad765 PB |
8983 | if (vma_is_shmem(vma)) |
8984 | continue; | |
0a96bbe4 BM |
8985 | if (vma->vm_file && |
8986 | !is_file_hugepages(vma->vm_file)) { | |
8987 | ret = -EOPNOTSUPP; | |
8988 | break; | |
8989 | } | |
8990 | } | |
8991 | } else { | |
8992 | ret = pret < 0 ? pret : -EFAULT; | |
8993 | } | |
8994 | mmap_read_unlock(current->mm); | |
8995 | if (ret) { | |
8996 | /* | |
8997 | * if we did partial map, or found file backed vmas, | |
8998 | * release any pages we did get | |
8999 | */ | |
9000 | if (pret > 0) | |
9001 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9002 | goto done; |
9003 | } | |
9004 | ||
9005 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
9006 | if (ret) { | |
9007 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9008 | goto done; |
9009 | } | |
9010 | ||
9011 | off = ubuf & ~PAGE_MASK; | |
9012 | size = iov->iov_len; | |
9013 | for (i = 0; i < nr_pages; i++) { | |
9014 | size_t vec_len; | |
9015 | ||
9016 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
9017 | imu->bvec[i].bv_page = pages[i]; | |
9018 | imu->bvec[i].bv_len = vec_len; | |
9019 | imu->bvec[i].bv_offset = off; | |
9020 | off = 0; | |
9021 | size -= vec_len; | |
9022 | } | |
9023 | /* store original address for later verification */ | |
9024 | imu->ubuf = ubuf; | |
4751f53d | 9025 | imu->ubuf_end = ubuf + iov->iov_len; |
0a96bbe4 | 9026 | imu->nr_bvecs = nr_pages; |
41edf1a5 | 9027 | *pimu = imu; |
0a96bbe4 BM |
9028 | ret = 0; |
9029 | done: | |
41edf1a5 PB |
9030 | if (ret) |
9031 | kvfree(imu); | |
0a96bbe4 BM |
9032 | kvfree(pages); |
9033 | kvfree(vmas); | |
9034 | return ret; | |
9035 | } | |
9036 | ||
2b358604 | 9037 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 9038 | { |
87094465 PB |
9039 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
9040 | return ctx->user_bufs ? 0 : -ENOMEM; | |
2b358604 | 9041 | } |
edafccee | 9042 | |
2b358604 BM |
9043 | static int io_buffer_validate(struct iovec *iov) |
9044 | { | |
50e96989 PB |
9045 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
9046 | ||
2b358604 BM |
9047 | /* |
9048 | * Don't impose further limits on the size and buffer | |
9049 | * constraints here, we'll -EINVAL later when IO is | |
9050 | * submitted if they are wrong. | |
9051 | */ | |
6224843d PB |
9052 | if (!iov->iov_base) |
9053 | return iov->iov_len ? -EFAULT : 0; | |
9054 | if (!iov->iov_len) | |
2b358604 | 9055 | return -EFAULT; |
edafccee | 9056 | |
2b358604 BM |
9057 | /* arbitrary limit, but we need something */ |
9058 | if (iov->iov_len > SZ_1G) | |
9059 | return -EFAULT; | |
edafccee | 9060 | |
50e96989 PB |
9061 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
9062 | return -EOVERFLOW; | |
9063 | ||
2b358604 BM |
9064 | return 0; |
9065 | } | |
edafccee | 9066 | |
2b358604 | 9067 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
634d00df | 9068 | unsigned int nr_args, u64 __user *tags) |
2b358604 | 9069 | { |
bd54b6fe BM |
9070 | struct page *last_hpage = NULL; |
9071 | struct io_rsrc_data *data; | |
2b358604 BM |
9072 | int i, ret; |
9073 | struct iovec iov; | |
edafccee | 9074 | |
87094465 PB |
9075 | if (ctx->user_bufs) |
9076 | return -EBUSY; | |
489809e2 | 9077 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
87094465 | 9078 | return -EINVAL; |
bd54b6fe | 9079 | ret = io_rsrc_node_switch_start(ctx); |
2b358604 BM |
9080 | if (ret) |
9081 | return ret; | |
d878c816 PB |
9082 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
9083 | if (ret) | |
9084 | return ret; | |
bd54b6fe BM |
9085 | ret = io_buffers_map_alloc(ctx, nr_args); |
9086 | if (ret) { | |
bb6659cc | 9087 | io_rsrc_data_free(data); |
bd54b6fe BM |
9088 | return ret; |
9089 | } | |
edafccee | 9090 | |
87094465 | 9091 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
edafccee JA |
9092 | ret = io_copy_iov(ctx, &iov, arg, i); |
9093 | if (ret) | |
0a96bbe4 | 9094 | break; |
2b358604 BM |
9095 | ret = io_buffer_validate(&iov); |
9096 | if (ret) | |
0a96bbe4 | 9097 | break; |
2d091d62 | 9098 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
cf3770e7 CIK |
9099 | ret = -EINVAL; |
9100 | break; | |
9101 | } | |
edafccee | 9102 | |
41edf1a5 PB |
9103 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
9104 | &last_hpage); | |
0a96bbe4 BM |
9105 | if (ret) |
9106 | break; | |
edafccee | 9107 | } |
0a96bbe4 | 9108 | |
bd54b6fe | 9109 | WARN_ON_ONCE(ctx->buf_data); |
0a96bbe4 | 9110 | |
bd54b6fe BM |
9111 | ctx->buf_data = data; |
9112 | if (ret) | |
9113 | __io_sqe_buffers_unregister(ctx); | |
9114 | else | |
9115 | io_rsrc_node_switch(ctx, NULL); | |
edafccee JA |
9116 | return ret; |
9117 | } | |
9118 | ||
634d00df PB |
9119 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
9120 | struct io_uring_rsrc_update2 *up, | |
9121 | unsigned int nr_args) | |
9122 | { | |
9123 | u64 __user *tags = u64_to_user_ptr(up->tags); | |
9124 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); | |
634d00df PB |
9125 | struct page *last_hpage = NULL; |
9126 | bool needs_switch = false; | |
9127 | __u32 done; | |
9128 | int i, err; | |
9129 | ||
9130 | if (!ctx->buf_data) | |
9131 | return -ENXIO; | |
9132 | if (up->offset + nr_args > ctx->nr_user_bufs) | |
9133 | return -EINVAL; | |
9134 | ||
9135 | for (done = 0; done < nr_args; done++) { | |
0b8c0e7c PB |
9136 | struct io_mapped_ubuf *imu; |
9137 | int offset = up->offset + done; | |
634d00df PB |
9138 | u64 tag = 0; |
9139 | ||
9140 | err = io_copy_iov(ctx, &iov, iovs, done); | |
9141 | if (err) | |
9142 | break; | |
9143 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { | |
9144 | err = -EFAULT; | |
9145 | break; | |
9146 | } | |
0b8c0e7c PB |
9147 | err = io_buffer_validate(&iov); |
9148 | if (err) | |
9149 | break; | |
cf3770e7 CIK |
9150 | if (!iov.iov_base && tag) { |
9151 | err = -EINVAL; | |
9152 | break; | |
9153 | } | |
0b8c0e7c PB |
9154 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
9155 | if (err) | |
9156 | break; | |
634d00df | 9157 | |
0b8c0e7c | 9158 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
6224843d | 9159 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
22a99dcb | 9160 | err = io_queue_rsrc_removal(ctx->buf_data, i, |
0b8c0e7c PB |
9161 | ctx->rsrc_node, ctx->user_bufs[i]); |
9162 | if (unlikely(err)) { | |
9163 | io_buffer_unmap(ctx, &imu); | |
634d00df | 9164 | break; |
0b8c0e7c | 9165 | } |
634d00df PB |
9166 | ctx->user_bufs[i] = NULL; |
9167 | needs_switch = true; | |
9168 | } | |
9169 | ||
0b8c0e7c | 9170 | ctx->user_bufs[i] = imu; |
2d091d62 | 9171 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
634d00df PB |
9172 | } |
9173 | ||
9174 | if (needs_switch) | |
9175 | io_rsrc_node_switch(ctx, ctx->buf_data); | |
9176 | return done ? done : err; | |
9177 | } | |
9178 | ||
9b402849 JA |
9179 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
9180 | { | |
9181 | __s32 __user *fds = arg; | |
9182 | int fd; | |
9183 | ||
9184 | if (ctx->cq_ev_fd) | |
9185 | return -EBUSY; | |
9186 | ||
9187 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
9188 | return -EFAULT; | |
9189 | ||
9190 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
9191 | if (IS_ERR(ctx->cq_ev_fd)) { | |
9192 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
fe7e3257 | 9193 | |
9b402849 JA |
9194 | ctx->cq_ev_fd = NULL; |
9195 | return ret; | |
9196 | } | |
9197 | ||
9198 | return 0; | |
9199 | } | |
9200 | ||
9201 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
9202 | { | |
9203 | if (ctx->cq_ev_fd) { | |
9204 | eventfd_ctx_put(ctx->cq_ev_fd); | |
9205 | ctx->cq_ev_fd = NULL; | |
9206 | return 0; | |
9207 | } | |
9208 | ||
9209 | return -ENXIO; | |
9210 | } | |
9211 | ||
5a2e745d JA |
9212 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
9213 | { | |
9e15c3a0 JA |
9214 | struct io_buffer *buf; |
9215 | unsigned long index; | |
9216 | ||
07edfd19 | 9217 | xa_for_each(&ctx->io_buffers, index, buf) |
9e15c3a0 | 9218 | __io_remove_buffers(ctx, buf, index, -1U); |
5a2e745d JA |
9219 | } |
9220 | ||
7255834e | 9221 | static void io_req_cache_free(struct list_head *list) |
1b4c351f | 9222 | { |
68e68ee6 | 9223 | struct io_kiocb *req, *nxt; |
1b4c351f | 9224 | |
bb943b82 PB |
9225 | list_for_each_entry_safe(req, nxt, list, inflight_entry) { |
9226 | list_del(&req->inflight_entry); | |
1b4c351f JA |
9227 | kmem_cache_free(req_cachep, req); |
9228 | } | |
9229 | } | |
9230 | ||
4010fec4 | 9231 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 9232 | { |
cd0ca2e0 | 9233 | struct io_submit_state *state = &ctx->submit_state; |
bf019da7 | 9234 | |
9a4fdbd8 JA |
9235 | mutex_lock(&ctx->uring_lock); |
9236 | ||
cd0ca2e0 PB |
9237 | if (state->free_reqs) { |
9238 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); | |
9239 | state->free_reqs = 0; | |
8e5c66c4 | 9240 | } |
9a4fdbd8 | 9241 | |
cd0ca2e0 PB |
9242 | io_flush_cached_locked_reqs(ctx, state); |
9243 | io_req_cache_free(&state->free_list); | |
9a4fdbd8 JA |
9244 | mutex_unlock(&ctx->uring_lock); |
9245 | } | |
9246 | ||
43597aac | 9247 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
2b188cc1 | 9248 | { |
43597aac | 9249 | if (data && !atomic_dec_and_test(&data->refs)) |
bd54b6fe | 9250 | wait_for_completion(&data->done); |
bd54b6fe | 9251 | } |
04fc6c80 | 9252 | |
2b188cc1 JA |
9253 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
9254 | { | |
37d1e2e3 | 9255 | io_sq_thread_finish(ctx); |
2aede0e4 | 9256 | |
37d1e2e3 | 9257 | if (ctx->mm_account) { |
2aede0e4 JA |
9258 | mmdrop(ctx->mm_account); |
9259 | ctx->mm_account = NULL; | |
30975825 | 9260 | } |
def596e9 | 9261 | |
43597aac PB |
9262 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
9263 | io_wait_rsrc_data(ctx->buf_data); | |
9264 | io_wait_rsrc_data(ctx->file_data); | |
9265 | ||
8bad28d8 | 9266 | mutex_lock(&ctx->uring_lock); |
43597aac | 9267 | if (ctx->buf_data) |
bd54b6fe | 9268 | __io_sqe_buffers_unregister(ctx); |
43597aac | 9269 | if (ctx->file_data) |
08480400 | 9270 | __io_sqe_files_unregister(ctx); |
c4ea060e PB |
9271 | if (ctx->rings) |
9272 | __io_cqring_overflow_flush(ctx, true); | |
8bad28d8 | 9273 | mutex_unlock(&ctx->uring_lock); |
9b402849 | 9274 | io_eventfd_unregister(ctx); |
5a2e745d | 9275 | io_destroy_buffers(ctx); |
07db298a PB |
9276 | if (ctx->sq_creds) |
9277 | put_cred(ctx->sq_creds); | |
def596e9 | 9278 | |
a7f0ed5a PB |
9279 | /* there are no registered resources left, nobody uses it */ |
9280 | if (ctx->rsrc_node) | |
9281 | io_rsrc_node_destroy(ctx->rsrc_node); | |
8dd03afe | 9282 | if (ctx->rsrc_backup_node) |
b895c9a6 | 9283 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
a7f0ed5a PB |
9284 | flush_delayed_work(&ctx->rsrc_put_work); |
9285 | ||
9286 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); | |
9287 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); | |
def596e9 | 9288 | |
2b188cc1 | 9289 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
9290 | if (ctx->ring_sock) { |
9291 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 9292 | sock_release(ctx->ring_sock); |
355e8d26 | 9293 | } |
2b188cc1 | 9294 | #endif |
ef9dd637 | 9295 | WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); |
2b188cc1 | 9296 | |
75b28aff | 9297 | io_mem_free(ctx->rings); |
2b188cc1 | 9298 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
9299 | |
9300 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 9301 | free_uid(ctx->user); |
4010fec4 | 9302 | io_req_caches_free(ctx); |
e941894e JA |
9303 | if (ctx->hash_map) |
9304 | io_wq_put_hash(ctx->hash_map); | |
78076bb6 | 9305 | kfree(ctx->cancel_hash); |
6224843d | 9306 | kfree(ctx->dummy_ubuf); |
2b188cc1 JA |
9307 | kfree(ctx); |
9308 | } | |
9309 | ||
9310 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
9311 | { | |
9312 | struct io_ring_ctx *ctx = file->private_data; | |
9313 | __poll_t mask = 0; | |
9314 | ||
311997b3 | 9315 | poll_wait(file, &ctx->poll_wait, wait); |
4f7067c3 SB |
9316 | /* |
9317 | * synchronizes with barrier from wq_has_sleeper call in | |
9318 | * io_commit_cqring | |
9319 | */ | |
2b188cc1 | 9320 | smp_rmb(); |
90554200 | 9321 | if (!io_sqring_full(ctx)) |
2b188cc1 | 9322 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
9323 | |
9324 | /* | |
9325 | * Don't flush cqring overflow list here, just do a simple check. | |
9326 | * Otherwise there could possible be ABBA deadlock: | |
9327 | * CPU0 CPU1 | |
9328 | * ---- ---- | |
9329 | * lock(&ctx->uring_lock); | |
9330 | * lock(&ep->mtx); | |
9331 | * lock(&ctx->uring_lock); | |
9332 | * lock(&ep->mtx); | |
9333 | * | |
9334 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
9335 | * pushs them to do the flush. | |
9336 | */ | |
5ed7a37d | 9337 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
2b188cc1 JA |
9338 | mask |= EPOLLIN | EPOLLRDNORM; |
9339 | ||
9340 | return mask; | |
9341 | } | |
9342 | ||
0bead8cd | 9343 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 9344 | { |
4379bf8b | 9345 | const struct cred *creds; |
071698e1 | 9346 | |
61cf9370 | 9347 | creds = xa_erase(&ctx->personalities, id); |
4379bf8b JA |
9348 | if (creds) { |
9349 | put_cred(creds); | |
0bead8cd | 9350 | return 0; |
1e6fa521 | 9351 | } |
0bead8cd YD |
9352 | |
9353 | return -EINVAL; | |
9354 | } | |
9355 | ||
d56d938b PB |
9356 | struct io_tctx_exit { |
9357 | struct callback_head task_work; | |
9358 | struct completion completion; | |
baf186c4 | 9359 | struct io_ring_ctx *ctx; |
d56d938b PB |
9360 | }; |
9361 | ||
9362 | static void io_tctx_exit_cb(struct callback_head *cb) | |
9363 | { | |
9364 | struct io_uring_task *tctx = current->io_uring; | |
9365 | struct io_tctx_exit *work; | |
9366 | ||
9367 | work = container_of(cb, struct io_tctx_exit, task_work); | |
9368 | /* | |
9369 | * When @in_idle, we're in cancellation and it's racy to remove the | |
9370 | * node. It'll be removed by the end of cancellation, just ignore it. | |
9371 | */ | |
9372 | if (!atomic_read(&tctx->in_idle)) | |
eef51daa | 9373 | io_uring_del_tctx_node((unsigned long)work->ctx); |
d56d938b PB |
9374 | complete(&work->completion); |
9375 | } | |
9376 | ||
28090c13 PB |
9377 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
9378 | { | |
9379 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
9380 | ||
9381 | return req->ctx == data; | |
9382 | } | |
9383 | ||
85faa7b8 JA |
9384 | static void io_ring_exit_work(struct work_struct *work) |
9385 | { | |
d56d938b | 9386 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 9387 | unsigned long timeout = jiffies + HZ * 60 * 5; |
58d3be2c | 9388 | unsigned long interval = HZ / 20; |
d56d938b PB |
9389 | struct io_tctx_exit exit; |
9390 | struct io_tctx_node *node; | |
9391 | int ret; | |
85faa7b8 | 9392 | |
56952e91 JA |
9393 | /* |
9394 | * If we're doing polled IO and end up having requests being | |
9395 | * submitted async (out-of-line), then completions can come in while | |
9396 | * we're waiting for refs to drop. We need to reap these manually, | |
9397 | * as nobody else will be looking for them. | |
9398 | */ | |
b2edc0a7 | 9399 | do { |
3dd0c97a | 9400 | io_uring_try_cancel_requests(ctx, NULL, true); |
28090c13 PB |
9401 | if (ctx->sq_data) { |
9402 | struct io_sq_data *sqd = ctx->sq_data; | |
9403 | struct task_struct *tsk; | |
9404 | ||
9405 | io_sq_thread_park(sqd); | |
9406 | tsk = sqd->thread; | |
9407 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) | |
9408 | io_wq_cancel_cb(tsk->io_uring->io_wq, | |
9409 | io_cancel_ctx_cb, ctx, true); | |
9410 | io_sq_thread_unpark(sqd); | |
9411 | } | |
b5bb3a24 | 9412 | |
58d3be2c PB |
9413 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
9414 | /* there is little hope left, don't run it too often */ | |
9415 | interval = HZ * 60; | |
9416 | } | |
9417 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); | |
d56d938b | 9418 | |
7f00651a PB |
9419 | init_completion(&exit.completion); |
9420 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
9421 | exit.ctx = ctx; | |
89b5066e PB |
9422 | /* |
9423 | * Some may use context even when all refs and requests have been put, | |
9424 | * and they are free to do so while still holding uring_lock or | |
5b0a6acc | 9425 | * completion_lock, see io_req_task_submit(). Apart from other work, |
89b5066e PB |
9426 | * this lock/unlock section also waits them to finish. |
9427 | */ | |
d56d938b PB |
9428 | mutex_lock(&ctx->uring_lock); |
9429 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
9430 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
9431 | ||
d56d938b PB |
9432 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
9433 | ctx_node); | |
7f00651a PB |
9434 | /* don't spin on a single task if cancellation failed */ |
9435 | list_rotate_left(&ctx->tctx_list); | |
d56d938b PB |
9436 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
9437 | if (WARN_ON_ONCE(ret)) | |
9438 | continue; | |
9439 | wake_up_process(node->task); | |
9440 | ||
9441 | mutex_unlock(&ctx->uring_lock); | |
9442 | wait_for_completion(&exit.completion); | |
d56d938b PB |
9443 | mutex_lock(&ctx->uring_lock); |
9444 | } | |
9445 | mutex_unlock(&ctx->uring_lock); | |
79ebeaee JA |
9446 | spin_lock(&ctx->completion_lock); |
9447 | spin_unlock(&ctx->completion_lock); | |
d56d938b | 9448 | |
85faa7b8 JA |
9449 | io_ring_ctx_free(ctx); |
9450 | } | |
9451 | ||
80c4cbdb PB |
9452 | /* Returns true if we found and killed one or more timeouts */ |
9453 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, | |
3dd0c97a | 9454 | bool cancel_all) |
80c4cbdb PB |
9455 | { |
9456 | struct io_kiocb *req, *tmp; | |
9457 | int canceled = 0; | |
9458 | ||
79ebeaee JA |
9459 | spin_lock(&ctx->completion_lock); |
9460 | spin_lock_irq(&ctx->timeout_lock); | |
80c4cbdb | 9461 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
3dd0c97a | 9462 | if (io_match_task(req, tsk, cancel_all)) { |
80c4cbdb PB |
9463 | io_kill_timeout(req, -ECANCELED); |
9464 | canceled++; | |
9465 | } | |
9466 | } | |
79ebeaee | 9467 | spin_unlock_irq(&ctx->timeout_lock); |
51520426 PB |
9468 | if (canceled != 0) |
9469 | io_commit_cqring(ctx); | |
79ebeaee | 9470 | spin_unlock(&ctx->completion_lock); |
80c4cbdb PB |
9471 | if (canceled != 0) |
9472 | io_cqring_ev_posted(ctx); | |
9473 | return canceled != 0; | |
9474 | } | |
9475 | ||
2b188cc1 JA |
9476 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
9477 | { | |
61cf9370 MWO |
9478 | unsigned long index; |
9479 | struct creds *creds; | |
9480 | ||
2b188cc1 JA |
9481 | mutex_lock(&ctx->uring_lock); |
9482 | percpu_ref_kill(&ctx->refs); | |
634578f8 | 9483 | if (ctx->rings) |
6c2450ae | 9484 | __io_cqring_overflow_flush(ctx, true); |
61cf9370 MWO |
9485 | xa_for_each(&ctx->personalities, index, creds) |
9486 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
9487 | mutex_unlock(&ctx->uring_lock); |
9488 | ||
3dd0c97a PB |
9489 | io_kill_timeouts(ctx, NULL, true); |
9490 | io_poll_remove_all(ctx, NULL, true); | |
561fb04a | 9491 | |
15dff286 | 9492 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 9493 | io_iopoll_try_reap_events(ctx); |
309fc03a | 9494 | |
85faa7b8 | 9495 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
9496 | /* |
9497 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
9498 | * if we're exiting a ton of rings at the same time. It just adds | |
9499 | * noise and overhead, there's no discernable change in runtime | |
9500 | * over using system_wq. | |
9501 | */ | |
9502 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
9503 | } |
9504 | ||
9505 | static int io_uring_release(struct inode *inode, struct file *file) | |
9506 | { | |
9507 | struct io_ring_ctx *ctx = file->private_data; | |
9508 | ||
9509 | file->private_data = NULL; | |
9510 | io_ring_ctx_wait_and_kill(ctx); | |
9511 | return 0; | |
9512 | } | |
9513 | ||
f6edbabb PB |
9514 | struct io_task_cancel { |
9515 | struct task_struct *task; | |
3dd0c97a | 9516 | bool all; |
f6edbabb | 9517 | }; |
f254ac04 | 9518 | |
f6edbabb | 9519 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 9520 | { |
9a472ef7 | 9521 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 9522 | struct io_task_cancel *cancel = data; |
9a472ef7 | 9523 | |
f0baed8e | 9524 | return io_match_task_safe(req, cancel->task, cancel->all); |
b711d4ea JA |
9525 | } |
9526 | ||
e1915f76 | 9527 | static bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
3dd0c97a | 9528 | struct task_struct *task, bool cancel_all) |
b7ddce3c | 9529 | { |
e1915f76 | 9530 | struct io_defer_entry *de; |
b7ddce3c PB |
9531 | LIST_HEAD(list); |
9532 | ||
79ebeaee | 9533 | spin_lock(&ctx->completion_lock); |
b7ddce3c | 9534 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
f0baed8e | 9535 | if (io_match_task_safe(de->req, task, cancel_all)) { |
b7ddce3c PB |
9536 | list_cut_position(&list, &ctx->defer_list, &de->list); |
9537 | break; | |
9538 | } | |
9539 | } | |
79ebeaee | 9540 | spin_unlock(&ctx->completion_lock); |
e1915f76 PB |
9541 | if (list_empty(&list)) |
9542 | return false; | |
b7ddce3c PB |
9543 | |
9544 | while (!list_empty(&list)) { | |
9545 | de = list_first_entry(&list, struct io_defer_entry, list); | |
9546 | list_del_init(&de->list); | |
f41db273 | 9547 | io_req_complete_failed(de->req, -ECANCELED); |
b7ddce3c PB |
9548 | kfree(de); |
9549 | } | |
e1915f76 | 9550 | return true; |
b7ddce3c PB |
9551 | } |
9552 | ||
1b00764f PB |
9553 | static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
9554 | { | |
9555 | struct io_tctx_node *node; | |
9556 | enum io_wq_cancel cret; | |
9557 | bool ret = false; | |
9558 | ||
9559 | mutex_lock(&ctx->uring_lock); | |
9560 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
9561 | struct io_uring_task *tctx = node->task->io_uring; | |
9562 | ||
9563 | /* | |
9564 | * io_wq will stay alive while we hold uring_lock, because it's | |
9565 | * killed after ctx nodes, which requires to take the lock. | |
9566 | */ | |
9567 | if (!tctx || !tctx->io_wq) | |
9568 | continue; | |
9569 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
9570 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9571 | } | |
9572 | mutex_unlock(&ctx->uring_lock); | |
9573 | ||
9574 | return ret; | |
9575 | } | |
9576 | ||
9936c7c2 PB |
9577 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
9578 | struct task_struct *task, | |
3dd0c97a | 9579 | bool cancel_all) |
9936c7c2 | 9580 | { |
3dd0c97a | 9581 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
1b00764f | 9582 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 PB |
9583 | |
9584 | while (1) { | |
9585 | enum io_wq_cancel cret; | |
9586 | bool ret = false; | |
9587 | ||
1b00764f PB |
9588 | if (!task) { |
9589 | ret |= io_uring_try_cancel_iowq(ctx); | |
9590 | } else if (tctx && tctx->io_wq) { | |
9591 | /* | |
9592 | * Cancels requests of all rings, not only @ctx, but | |
9593 | * it's fine as the task is in exit/exec. | |
9594 | */ | |
5aa75ed5 | 9595 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
9596 | &cancel, true); |
9597 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9598 | } | |
9599 | ||
9600 | /* SQPOLL thread does its own polling */ | |
3dd0c97a | 9601 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
d052d1d6 | 9602 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
9936c7c2 PB |
9603 | while (!list_empty_careful(&ctx->iopoll_list)) { |
9604 | io_iopoll_try_reap_events(ctx); | |
9605 | ret = true; | |
9606 | } | |
9607 | } | |
9608 | ||
3dd0c97a PB |
9609 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
9610 | ret |= io_poll_remove_all(ctx, task, cancel_all); | |
9611 | ret |= io_kill_timeouts(ctx, task, cancel_all); | |
e5dc480d PB |
9612 | if (task) |
9613 | ret |= io_run_task_work(); | |
9936c7c2 PB |
9614 | if (!ret) |
9615 | break; | |
9616 | cond_resched(); | |
9617 | } | |
9618 | } | |
9619 | ||
eef51daa | 9620 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
0f212204 | 9621 | { |
236434c3 | 9622 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 9623 | struct io_tctx_node *node; |
a528b04e | 9624 | int ret; |
236434c3 MWO |
9625 | |
9626 | if (unlikely(!tctx)) { | |
5aa75ed5 | 9627 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
9628 | if (unlikely(ret)) |
9629 | return ret; | |
e139a1ec | 9630 | |
236434c3 | 9631 | tctx = current->io_uring; |
e139a1ec PB |
9632 | if (ctx->iowq_limits_set) { |
9633 | unsigned int limits[2] = { ctx->iowq_limits[0], | |
9634 | ctx->iowq_limits[1], }; | |
9635 | ||
9636 | ret = io_wq_max_workers(tctx->io_wq, limits); | |
9637 | if (ret) | |
9638 | return ret; | |
9639 | } | |
0f212204 | 9640 | } |
cf27f3b1 PB |
9641 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
9642 | node = kmalloc(sizeof(*node), GFP_KERNEL); | |
9643 | if (!node) | |
9644 | return -ENOMEM; | |
9645 | node->ctx = ctx; | |
9646 | node->task = current; | |
13bf43f5 | 9647 | |
cf27f3b1 PB |
9648 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
9649 | node, GFP_KERNEL)); | |
9650 | if (ret) { | |
9651 | kfree(node); | |
9652 | return ret; | |
0f212204 | 9653 | } |
cf27f3b1 PB |
9654 | |
9655 | mutex_lock(&ctx->uring_lock); | |
9656 | list_add(&node->ctx_node, &ctx->tctx_list); | |
9657 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 9658 | } |
cf27f3b1 | 9659 | tctx->last = ctx; |
0f212204 JA |
9660 | return 0; |
9661 | } | |
9662 | ||
cf27f3b1 PB |
9663 | /* |
9664 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
9665 | */ | |
eef51daa | 9666 | static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
cf27f3b1 PB |
9667 | { |
9668 | struct io_uring_task *tctx = current->io_uring; | |
9669 | ||
9670 | if (likely(tctx && tctx->last == ctx)) | |
9671 | return 0; | |
eef51daa | 9672 | return __io_uring_add_tctx_node(ctx); |
cf27f3b1 PB |
9673 | } |
9674 | ||
0f212204 JA |
9675 | /* |
9676 | * Remove this io_uring_file -> task mapping. | |
9677 | */ | |
eef51daa | 9678 | static void io_uring_del_tctx_node(unsigned long index) |
0f212204 JA |
9679 | { |
9680 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 9681 | struct io_tctx_node *node; |
2941267b | 9682 | |
eebd2e37 PB |
9683 | if (!tctx) |
9684 | return; | |
13bf43f5 PB |
9685 | node = xa_erase(&tctx->xa, index); |
9686 | if (!node) | |
2941267b | 9687 | return; |
0f212204 | 9688 | |
13bf43f5 PB |
9689 | WARN_ON_ONCE(current != node->task); |
9690 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
9691 | ||
9692 | mutex_lock(&node->ctx->uring_lock); | |
9693 | list_del(&node->ctx_node); | |
9694 | mutex_unlock(&node->ctx->uring_lock); | |
9695 | ||
baf186c4 | 9696 | if (tctx->last == node->ctx) |
0f212204 | 9697 | tctx->last = NULL; |
13bf43f5 | 9698 | kfree(node); |
0f212204 JA |
9699 | } |
9700 | ||
8452d4a6 | 9701 | static void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 9702 | { |
ba5ef6dc | 9703 | struct io_wq *wq = tctx->io_wq; |
13bf43f5 | 9704 | struct io_tctx_node *node; |
de7f1d9e PB |
9705 | unsigned long index; |
9706 | ||
8bab4c09 | 9707 | xa_for_each(&tctx->xa, index, node) { |
eef51daa | 9708 | io_uring_del_tctx_node(index); |
8bab4c09 JA |
9709 | cond_resched(); |
9710 | } | |
b16ef427 ME |
9711 | if (wq) { |
9712 | /* | |
9713 | * Must be after io_uring_del_task_file() (removes nodes under | |
9714 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). | |
9715 | */ | |
ba5ef6dc | 9716 | io_wq_put_and_exit(wq); |
dadebc35 | 9717 | tctx->io_wq = NULL; |
b16ef427 | 9718 | } |
de7f1d9e PB |
9719 | } |
9720 | ||
3f48cf18 | 9721 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
521d6a73 | 9722 | { |
3f48cf18 PB |
9723 | if (tracked) |
9724 | return atomic_read(&tctx->inflight_tracked); | |
521d6a73 PB |
9725 | return percpu_counter_sum(&tctx->inflight); |
9726 | } | |
9727 | ||
78cc687b PB |
9728 | /* |
9729 | * Find any io_uring ctx that this task has registered or done IO on, and cancel | |
b16590fd | 9730 | * requests. @sqd should be not-null IFF it's an SQPOLL thread cancellation. |
78cc687b PB |
9731 | */ |
9732 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd) | |
0e9ddb39 | 9733 | { |
521d6a73 | 9734 | struct io_uring_task *tctx = current->io_uring; |
734551df | 9735 | struct io_ring_ctx *ctx; |
0e9ddb39 PB |
9736 | s64 inflight; |
9737 | DEFINE_WAIT(wait); | |
fdaf083c | 9738 | |
78cc687b PB |
9739 | WARN_ON_ONCE(sqd && sqd->thread != current); |
9740 | ||
6d042ffb PO |
9741 | if (!current->io_uring) |
9742 | return; | |
17a91051 PB |
9743 | if (tctx->io_wq) |
9744 | io_wq_exit_start(tctx->io_wq); | |
9745 | ||
0e9ddb39 PB |
9746 | atomic_inc(&tctx->in_idle); |
9747 | do { | |
e9dbe221 | 9748 | io_uring_drop_tctx_refs(current); |
0e9ddb39 | 9749 | /* read completions before cancelations */ |
78cc687b | 9750 | inflight = tctx_inflight(tctx, !cancel_all); |
0e9ddb39 PB |
9751 | if (!inflight) |
9752 | break; | |
fdaf083c | 9753 | |
78cc687b PB |
9754 | if (!sqd) { |
9755 | struct io_tctx_node *node; | |
9756 | unsigned long index; | |
0f212204 | 9757 | |
78cc687b PB |
9758 | xa_for_each(&tctx->xa, index, node) { |
9759 | /* sqpoll task will cancel all its requests */ | |
9760 | if (node->ctx->sq_data) | |
9761 | continue; | |
9762 | io_uring_try_cancel_requests(node->ctx, current, | |
9763 | cancel_all); | |
9764 | } | |
9765 | } else { | |
9766 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
9767 | io_uring_try_cancel_requests(ctx, current, | |
9768 | cancel_all); | |
9769 | } | |
17a91051 | 9770 | |
b16590fd JA |
9771 | prepare_to_wait(&tctx->wait, &wait, TASK_INTERRUPTIBLE); |
9772 | io_run_task_work(); | |
e9dbe221 | 9773 | io_uring_drop_tctx_refs(current); |
b16590fd | 9774 | |
0f212204 | 9775 | /* |
a1bb3cd5 PB |
9776 | * If we've seen completions, retry without waiting. This |
9777 | * avoids a race where a completion comes in before we did | |
9778 | * prepare_to_wait(). | |
0f212204 | 9779 | */ |
3dd0c97a | 9780 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
a1bb3cd5 | 9781 | schedule(); |
f57555ed | 9782 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 9783 | } while (1); |
de7f1d9e | 9784 | |
8452d4a6 | 9785 | io_uring_clean_tctx(tctx); |
3dd0c97a | 9786 | if (cancel_all) { |
abdebba9 PB |
9787 | /* |
9788 | * We shouldn't run task_works after cancel, so just leave | |
9789 | * ->in_idle set for normal exit. | |
9790 | */ | |
9791 | atomic_dec(&tctx->in_idle); | |
3f48cf18 PB |
9792 | /* for exec all current's requests should be gone, kill tctx */ |
9793 | __io_uring_free(current); | |
9794 | } | |
44e728b8 PB |
9795 | } |
9796 | ||
f552a27a | 9797 | void __io_uring_cancel(bool cancel_all) |
78cc687b | 9798 | { |
f552a27a | 9799 | io_uring_cancel_generic(cancel_all, NULL); |
78cc687b PB |
9800 | } |
9801 | ||
6c5c240e RP |
9802 | static void *io_uring_validate_mmap_request(struct file *file, |
9803 | loff_t pgoff, size_t sz) | |
2b188cc1 | 9804 | { |
2b188cc1 | 9805 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 9806 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
9807 | struct page *page; |
9808 | void *ptr; | |
9809 | ||
9810 | switch (offset) { | |
9811 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
9812 | case IORING_OFF_CQ_RING: |
9813 | ptr = ctx->rings; | |
2b188cc1 JA |
9814 | break; |
9815 | case IORING_OFF_SQES: | |
9816 | ptr = ctx->sq_sqes; | |
9817 | break; | |
2b188cc1 | 9818 | default: |
6c5c240e | 9819 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
9820 | } |
9821 | ||
9822 | page = virt_to_head_page(ptr); | |
a50b854e | 9823 | if (sz > page_size(page)) |
6c5c240e RP |
9824 | return ERR_PTR(-EINVAL); |
9825 | ||
9826 | return ptr; | |
9827 | } | |
9828 | ||
9829 | #ifdef CONFIG_MMU | |
9830 | ||
9831 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9832 | { | |
9833 | size_t sz = vma->vm_end - vma->vm_start; | |
9834 | unsigned long pfn; | |
9835 | void *ptr; | |
9836 | ||
9837 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
9838 | if (IS_ERR(ptr)) | |
9839 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9840 | |
9841 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9842 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9843 | } | |
9844 | ||
6c5c240e RP |
9845 | #else /* !CONFIG_MMU */ |
9846 | ||
9847 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9848 | { | |
9849 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9850 | } | |
9851 | ||
9852 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9853 | { | |
9854 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9855 | } | |
9856 | ||
9857 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9858 | unsigned long addr, unsigned long len, | |
9859 | unsigned long pgoff, unsigned long flags) | |
9860 | { | |
9861 | void *ptr; | |
9862 | ||
9863 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9864 | if (IS_ERR(ptr)) | |
9865 | return PTR_ERR(ptr); | |
9866 | ||
9867 | return (unsigned long) ptr; | |
9868 | } | |
9869 | ||
9870 | #endif /* !CONFIG_MMU */ | |
9871 | ||
d9d05217 | 9872 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 JA |
9873 | { |
9874 | DEFINE_WAIT(wait); | |
9875 | ||
9876 | do { | |
9877 | if (!io_sqring_full(ctx)) | |
9878 | break; | |
90554200 JA |
9879 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
9880 | ||
9881 | if (!io_sqring_full(ctx)) | |
9882 | break; | |
90554200 JA |
9883 | schedule(); |
9884 | } while (!signal_pending(current)); | |
9885 | ||
9886 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
5199328a | 9887 | return 0; |
90554200 JA |
9888 | } |
9889 | ||
c73ebb68 HX |
9890 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9891 | struct __kernel_timespec __user **ts, | |
9892 | const sigset_t __user **sig) | |
9893 | { | |
9894 | struct io_uring_getevents_arg arg; | |
9895 | ||
9896 | /* | |
9897 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9898 | * is just a pointer to the sigset_t. | |
9899 | */ | |
9900 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9901 | *sig = (const sigset_t __user *) argp; | |
9902 | *ts = NULL; | |
9903 | return 0; | |
9904 | } | |
9905 | ||
9906 | /* | |
9907 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9908 | * timespec and sigset_t pointers if good. | |
9909 | */ | |
9910 | if (*argsz != sizeof(arg)) | |
9911 | return -EINVAL; | |
9912 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9913 | return -EFAULT; | |
02a84aaf DY |
9914 | if (arg.pad) |
9915 | return -EINVAL; | |
c73ebb68 HX |
9916 | *sig = u64_to_user_ptr(arg.sigmask); |
9917 | *argsz = arg.sigmask_sz; | |
9918 | *ts = u64_to_user_ptr(arg.ts); | |
9919 | return 0; | |
9920 | } | |
9921 | ||
2b188cc1 | 9922 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9923 | u32, min_complete, u32, flags, const void __user *, argp, |
9924 | size_t, argsz) | |
2b188cc1 JA |
9925 | { |
9926 | struct io_ring_ctx *ctx; | |
2b188cc1 JA |
9927 | int submitted = 0; |
9928 | struct fd f; | |
33f993da | 9929 | long ret; |
2b188cc1 | 9930 | |
4c6e277c | 9931 | io_run_task_work(); |
b41e9852 | 9932 | |
33f993da PB |
9933 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
9934 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) | |
2b188cc1 JA |
9935 | return -EINVAL; |
9936 | ||
9937 | f = fdget(fd); | |
33f993da | 9938 | if (unlikely(!f.file)) |
2b188cc1 JA |
9939 | return -EBADF; |
9940 | ||
9941 | ret = -EOPNOTSUPP; | |
33f993da | 9942 | if (unlikely(f.file->f_op != &io_uring_fops)) |
2b188cc1 JA |
9943 | goto out_fput; |
9944 | ||
9945 | ret = -ENXIO; | |
9946 | ctx = f.file->private_data; | |
33f993da | 9947 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
2b188cc1 JA |
9948 | goto out_fput; |
9949 | ||
7e84e1c7 | 9950 | ret = -EBADFD; |
33f993da | 9951 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
9952 | goto out; |
9953 | ||
6c271ce2 JA |
9954 | /* |
9955 | * For SQ polling, the thread will do all submissions and completions. | |
9956 | * Just return the requested submit count, and wake the thread if | |
9957 | * we were asked to. | |
9958 | */ | |
b2a9eada | 9959 | ret = 0; |
6c271ce2 | 9960 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
90f67366 | 9961 | io_cqring_overflow_flush(ctx); |
89448c47 | 9962 | |
21f96522 JA |
9963 | if (unlikely(ctx->sq_data->thread == NULL)) { |
9964 | ret = -EOWNERDEAD; | |
04147488 | 9965 | goto out; |
21f96522 | 9966 | } |
6c271ce2 | 9967 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9968 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9969 | if (flags & IORING_ENTER_SQ_WAIT) { |
9970 | ret = io_sqpoll_wait_sq(ctx); | |
9971 | if (ret) | |
9972 | goto out; | |
9973 | } | |
6c271ce2 | 9974 | submitted = to_submit; |
b2a9eada | 9975 | } else if (to_submit) { |
eef51daa | 9976 | ret = io_uring_add_tctx_node(ctx); |
0f212204 JA |
9977 | if (unlikely(ret)) |
9978 | goto out; | |
2b188cc1 | 9979 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9980 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9981 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9982 | |
9983 | if (submitted != to_submit) | |
9984 | goto out; | |
2b188cc1 JA |
9985 | } |
9986 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9987 | const sigset_t __user *sig; |
9988 | struct __kernel_timespec __user *ts; | |
9989 | ||
9990 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9991 | if (unlikely(ret)) | |
9992 | goto out; | |
9993 | ||
2b188cc1 JA |
9994 | min_complete = min(min_complete, ctx->cq_entries); |
9995 | ||
32b2244a XW |
9996 | /* |
9997 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9998 | * space applications don't need to do io completion events | |
9999 | * polling again, they can rely on io_sq_thread to do polling | |
10000 | * work, which can reduce cpu usage and uring_lock contention. | |
10001 | */ | |
10002 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
10003 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 10004 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 10005 | } else { |
c73ebb68 | 10006 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 10007 | } |
2b188cc1 JA |
10008 | } |
10009 | ||
7c504e65 | 10010 | out: |
6805b32e | 10011 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
10012 | out_fput: |
10013 | fdput(f); | |
10014 | return submitted ? submitted : ret; | |
10015 | } | |
10016 | ||
bebdb65e | 10017 | #ifdef CONFIG_PROC_FS |
61cf9370 MWO |
10018 | static int io_uring_show_cred(struct seq_file *m, unsigned int id, |
10019 | const struct cred *cred) | |
87ce955b | 10020 | { |
87ce955b JA |
10021 | struct user_namespace *uns = seq_user_ns(m); |
10022 | struct group_info *gi; | |
10023 | kernel_cap_t cap; | |
10024 | unsigned __capi; | |
10025 | int g; | |
10026 | ||
10027 | seq_printf(m, "%5d\n", id); | |
10028 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
10029 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
10030 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
10031 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
10032 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
10033 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
10034 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
10035 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
10036 | seq_puts(m, "\n\tGroups:\t"); | |
10037 | gi = cred->group_info; | |
10038 | for (g = 0; g < gi->ngroups; g++) { | |
10039 | seq_put_decimal_ull(m, g ? " " : "", | |
10040 | from_kgid_munged(uns, gi->gid[g])); | |
10041 | } | |
10042 | seq_puts(m, "\n\tCapEff:\t"); | |
10043 | cap = cred->cap_effective; | |
10044 | CAP_FOR_EACH_U32(__capi) | |
10045 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
10046 | seq_putc(m, '\n'); | |
10047 | return 0; | |
10048 | } | |
10049 | ||
10050 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
10051 | { | |
dbbe9c64 | 10052 | struct io_sq_data *sq = NULL; |
fad8e0de | 10053 | bool has_lock; |
87ce955b JA |
10054 | int i; |
10055 | ||
fad8e0de JA |
10056 | /* |
10057 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
10058 | * since fdinfo case grabs it in the opposite direction of normal use | |
10059 | * cases. If we fail to get the lock, we just don't iterate any | |
10060 | * structures that could be going away outside the io_uring mutex. | |
10061 | */ | |
10062 | has_lock = mutex_trylock(&ctx->uring_lock); | |
10063 | ||
5f3f26f9 | 10064 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 10065 | sq = ctx->sq_data; |
5f3f26f9 JA |
10066 | if (!sq->thread) |
10067 | sq = NULL; | |
10068 | } | |
dbbe9c64 JQ |
10069 | |
10070 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
10071 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 10072 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 10073 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
7b29f92d | 10074 | struct file *f = io_file_from_index(ctx, i); |
87ce955b | 10075 | |
87ce955b JA |
10076 | if (f) |
10077 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
10078 | else | |
10079 | seq_printf(m, "%5u: <none>\n", i); | |
10080 | } | |
10081 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 10082 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
41edf1a5 | 10083 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
4751f53d | 10084 | unsigned int len = buf->ubuf_end - buf->ubuf; |
87ce955b | 10085 | |
4751f53d | 10086 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
87ce955b | 10087 | } |
61cf9370 MWO |
10088 | if (has_lock && !xa_empty(&ctx->personalities)) { |
10089 | unsigned long index; | |
10090 | const struct cred *cred; | |
10091 | ||
87ce955b | 10092 | seq_printf(m, "Personalities:\n"); |
61cf9370 MWO |
10093 | xa_for_each(&ctx->personalities, index, cred) |
10094 | io_uring_show_cred(m, index, cred); | |
87ce955b | 10095 | } |
d7718a9d | 10096 | seq_printf(m, "PollList:\n"); |
79ebeaee | 10097 | spin_lock(&ctx->completion_lock); |
d7718a9d JA |
10098 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
10099 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
10100 | struct io_kiocb *req; | |
10101 | ||
10102 | hlist_for_each_entry(req, list, hash_node) | |
10103 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
10104 | req->task->task_works != NULL); | |
10105 | } | |
79ebeaee | 10106 | spin_unlock(&ctx->completion_lock); |
fad8e0de JA |
10107 | if (has_lock) |
10108 | mutex_unlock(&ctx->uring_lock); | |
87ce955b JA |
10109 | } |
10110 | ||
10111 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
10112 | { | |
10113 | struct io_ring_ctx *ctx = f->private_data; | |
10114 | ||
10115 | if (percpu_ref_tryget(&ctx->refs)) { | |
10116 | __io_uring_show_fdinfo(ctx, m); | |
10117 | percpu_ref_put(&ctx->refs); | |
10118 | } | |
10119 | } | |
bebdb65e | 10120 | #endif |
87ce955b | 10121 | |
2b188cc1 JA |
10122 | static const struct file_operations io_uring_fops = { |
10123 | .release = io_uring_release, | |
10124 | .mmap = io_uring_mmap, | |
6c5c240e RP |
10125 | #ifndef CONFIG_MMU |
10126 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
10127 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
10128 | #endif | |
2b188cc1 | 10129 | .poll = io_uring_poll, |
bebdb65e | 10130 | #ifdef CONFIG_PROC_FS |
87ce955b | 10131 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 10132 | #endif |
2b188cc1 JA |
10133 | }; |
10134 | ||
10135 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
10136 | struct io_uring_params *p) | |
10137 | { | |
75b28aff HV |
10138 | struct io_rings *rings; |
10139 | size_t size, sq_array_offset; | |
2b188cc1 | 10140 | |
bd740481 JA |
10141 | /* make sure these are sane, as we already accounted them */ |
10142 | ctx->sq_entries = p->sq_entries; | |
10143 | ctx->cq_entries = p->cq_entries; | |
10144 | ||
75b28aff HV |
10145 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
10146 | if (size == SIZE_MAX) | |
10147 | return -EOVERFLOW; | |
10148 | ||
10149 | rings = io_mem_alloc(size); | |
10150 | if (!rings) | |
2b188cc1 JA |
10151 | return -ENOMEM; |
10152 | ||
75b28aff HV |
10153 | ctx->rings = rings; |
10154 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
10155 | rings->sq_ring_mask = p->sq_entries - 1; | |
10156 | rings->cq_ring_mask = p->cq_entries - 1; | |
10157 | rings->sq_ring_entries = p->sq_entries; | |
10158 | rings->cq_ring_entries = p->cq_entries; | |
2b188cc1 JA |
10159 | |
10160 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
10161 | if (size == SIZE_MAX) { |
10162 | io_mem_free(ctx->rings); | |
10163 | ctx->rings = NULL; | |
2b188cc1 | 10164 | return -EOVERFLOW; |
eb065d30 | 10165 | } |
2b188cc1 JA |
10166 | |
10167 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
10168 | if (!ctx->sq_sqes) { |
10169 | io_mem_free(ctx->rings); | |
10170 | ctx->rings = NULL; | |
2b188cc1 | 10171 | return -ENOMEM; |
eb065d30 | 10172 | } |
2b188cc1 | 10173 | |
2b188cc1 JA |
10174 | return 0; |
10175 | } | |
10176 | ||
9faadcc8 PB |
10177 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
10178 | { | |
10179 | int ret, fd; | |
10180 | ||
10181 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
10182 | if (fd < 0) | |
10183 | return fd; | |
10184 | ||
eef51daa | 10185 | ret = io_uring_add_tctx_node(ctx); |
9faadcc8 PB |
10186 | if (ret) { |
10187 | put_unused_fd(fd); | |
10188 | return ret; | |
10189 | } | |
10190 | fd_install(fd, file); | |
10191 | return fd; | |
10192 | } | |
10193 | ||
2b188cc1 JA |
10194 | /* |
10195 | * Allocate an anonymous fd, this is what constitutes the application | |
10196 | * visible backing of an io_uring instance. The application mmaps this | |
10197 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
10198 | * we have to tie this fd to a socket for file garbage collection purposes. | |
10199 | */ | |
9faadcc8 | 10200 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
10201 | { |
10202 | struct file *file; | |
9faadcc8 | 10203 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
10204 | int ret; |
10205 | ||
2b188cc1 JA |
10206 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
10207 | &ctx->ring_sock); | |
10208 | if (ret) | |
9faadcc8 | 10209 | return ERR_PTR(ret); |
2b188cc1 JA |
10210 | #endif |
10211 | ||
2b188cc1 JA |
10212 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
10213 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 10214 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
10215 | if (IS_ERR(file)) { |
10216 | sock_release(ctx->ring_sock); | |
10217 | ctx->ring_sock = NULL; | |
10218 | } else { | |
10219 | ctx->ring_sock->file = file; | |
0f212204 | 10220 | } |
2b188cc1 | 10221 | #endif |
9faadcc8 | 10222 | return file; |
2b188cc1 JA |
10223 | } |
10224 | ||
7f13657d XW |
10225 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
10226 | struct io_uring_params __user *params) | |
2b188cc1 | 10227 | { |
2b188cc1 | 10228 | struct io_ring_ctx *ctx; |
9faadcc8 | 10229 | struct file *file; |
2b188cc1 JA |
10230 | int ret; |
10231 | ||
8110c1a6 | 10232 | if (!entries) |
2b188cc1 | 10233 | return -EINVAL; |
8110c1a6 JA |
10234 | if (entries > IORING_MAX_ENTRIES) { |
10235 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
10236 | return -EINVAL; | |
10237 | entries = IORING_MAX_ENTRIES; | |
10238 | } | |
2b188cc1 JA |
10239 | |
10240 | /* | |
10241 | * Use twice as many entries for the CQ ring. It's possible for the | |
10242 | * application to drive a higher depth than the size of the SQ ring, | |
10243 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
10244 | * some flexibility in overcommitting a bit. If the application has |
10245 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
10246 | * of CQ ring entries manually. | |
2b188cc1 JA |
10247 | */ |
10248 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
10249 | if (p->flags & IORING_SETUP_CQSIZE) { |
10250 | /* | |
10251 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
10252 | * to a power-of-two, if it isn't already. We do NOT impose | |
10253 | * any cq vs sq ring sizing. | |
10254 | */ | |
eb2667b3 | 10255 | if (!p->cq_entries) |
33a107f0 | 10256 | return -EINVAL; |
8110c1a6 JA |
10257 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
10258 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
10259 | return -EINVAL; | |
10260 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
10261 | } | |
eb2667b3 JQ |
10262 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
10263 | if (p->cq_entries < p->sq_entries) | |
10264 | return -EINVAL; | |
33a107f0 JA |
10265 | } else { |
10266 | p->cq_entries = 2 * p->sq_entries; | |
10267 | } | |
2b188cc1 | 10268 | |
2b188cc1 | 10269 | ctx = io_ring_ctx_alloc(p); |
62e398be | 10270 | if (!ctx) |
2b188cc1 | 10271 | return -ENOMEM; |
2b188cc1 | 10272 | ctx->compat = in_compat_syscall(); |
62e398be JA |
10273 | if (!capable(CAP_IPC_LOCK)) |
10274 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
10275 | |
10276 | /* | |
10277 | * This is just grabbed for accounting purposes. When a process exits, | |
10278 | * the mm is exited and dropped before the files, hence we need to hang | |
10279 | * on to this mm purely for the purposes of being able to unaccount | |
10280 | * memory (locked/pinned vm). It's not used for anything else. | |
10281 | */ | |
6b7898eb | 10282 | mmgrab(current->mm); |
2aede0e4 | 10283 | ctx->mm_account = current->mm; |
6b7898eb | 10284 | |
2b188cc1 JA |
10285 | ret = io_allocate_scq_urings(ctx, p); |
10286 | if (ret) | |
10287 | goto err; | |
10288 | ||
7e84e1c7 | 10289 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
10290 | if (ret) |
10291 | goto err; | |
eae071c9 | 10292 | /* always set a rsrc node */ |
47b228ce PB |
10293 | ret = io_rsrc_node_switch_start(ctx); |
10294 | if (ret) | |
10295 | goto err; | |
eae071c9 | 10296 | io_rsrc_node_switch(ctx, NULL); |
2b188cc1 | 10297 | |
2b188cc1 | 10298 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
10299 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
10300 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
10301 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
10302 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
10303 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
10304 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
10305 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
10306 | |
10307 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
10308 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
10309 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
10310 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
10311 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
10312 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
10313 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 10314 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 10315 | |
7f13657d XW |
10316 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
10317 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 10318 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 10319 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9690557e PB |
10320 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
10321 | IORING_FEAT_RSRC_TAGS; | |
7f13657d XW |
10322 | |
10323 | if (copy_to_user(params, p, sizeof(*p))) { | |
10324 | ret = -EFAULT; | |
10325 | goto err; | |
10326 | } | |
d1719f70 | 10327 | |
9faadcc8 PB |
10328 | file = io_uring_get_file(ctx); |
10329 | if (IS_ERR(file)) { | |
10330 | ret = PTR_ERR(file); | |
10331 | goto err; | |
10332 | } | |
10333 | ||
044c1ab3 JA |
10334 | /* |
10335 | * Install ring fd as the very last thing, so we don't risk someone | |
10336 | * having closed it before we finish setup | |
10337 | */ | |
9faadcc8 PB |
10338 | ret = io_uring_install_fd(ctx, file); |
10339 | if (ret < 0) { | |
10340 | /* fput will clean it up */ | |
10341 | fput(file); | |
10342 | return ret; | |
10343 | } | |
044c1ab3 | 10344 | |
c826bd7a | 10345 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
10346 | return ret; |
10347 | err: | |
10348 | io_ring_ctx_wait_and_kill(ctx); | |
10349 | return ret; | |
10350 | } | |
10351 | ||
10352 | /* | |
10353 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
10354 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
10355 | * params structure passed in. | |
10356 | */ | |
10357 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
10358 | { | |
10359 | struct io_uring_params p; | |
2b188cc1 JA |
10360 | int i; |
10361 | ||
10362 | if (copy_from_user(&p, params, sizeof(p))) | |
10363 | return -EFAULT; | |
10364 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
10365 | if (p.resv[i]) | |
10366 | return -EINVAL; | |
10367 | } | |
10368 | ||
6c271ce2 | 10369 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 10370 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
10371 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
10372 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
10373 | return -EINVAL; |
10374 | ||
7f13657d | 10375 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
10376 | } |
10377 | ||
10378 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
10379 | struct io_uring_params __user *, params) | |
10380 | { | |
10381 | return io_uring_setup(entries, params); | |
10382 | } | |
10383 | ||
66f4af93 JA |
10384 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
10385 | { | |
10386 | struct io_uring_probe *p; | |
10387 | size_t size; | |
10388 | int i, ret; | |
10389 | ||
10390 | size = struct_size(p, ops, nr_args); | |
10391 | if (size == SIZE_MAX) | |
10392 | return -EOVERFLOW; | |
10393 | p = kzalloc(size, GFP_KERNEL); | |
10394 | if (!p) | |
10395 | return -ENOMEM; | |
10396 | ||
10397 | ret = -EFAULT; | |
10398 | if (copy_from_user(p, arg, size)) | |
10399 | goto out; | |
10400 | ret = -EINVAL; | |
10401 | if (memchr_inv(p, 0, size)) | |
10402 | goto out; | |
10403 | ||
10404 | p->last_op = IORING_OP_LAST - 1; | |
10405 | if (nr_args > IORING_OP_LAST) | |
10406 | nr_args = IORING_OP_LAST; | |
10407 | ||
10408 | for (i = 0; i < nr_args; i++) { | |
10409 | p->ops[i].op = i; | |
10410 | if (!io_op_defs[i].not_supported) | |
10411 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
10412 | } | |
10413 | p->ops_len = i; | |
10414 | ||
10415 | ret = 0; | |
10416 | if (copy_to_user(arg, p, size)) | |
10417 | ret = -EFAULT; | |
10418 | out: | |
10419 | kfree(p); | |
10420 | return ret; | |
10421 | } | |
10422 | ||
071698e1 JA |
10423 | static int io_register_personality(struct io_ring_ctx *ctx) |
10424 | { | |
4379bf8b | 10425 | const struct cred *creds; |
61cf9370 | 10426 | u32 id; |
1e6fa521 | 10427 | int ret; |
071698e1 | 10428 | |
4379bf8b | 10429 | creds = get_current_cred(); |
1e6fa521 | 10430 | |
61cf9370 MWO |
10431 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
10432 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); | |
a30f895a JA |
10433 | if (ret < 0) { |
10434 | put_cred(creds); | |
10435 | return ret; | |
10436 | } | |
10437 | return id; | |
071698e1 JA |
10438 | } |
10439 | ||
21b55dbc SG |
10440 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
10441 | unsigned int nr_args) | |
10442 | { | |
10443 | struct io_uring_restriction *res; | |
10444 | size_t size; | |
10445 | int i, ret; | |
10446 | ||
7e84e1c7 SG |
10447 | /* Restrictions allowed only if rings started disabled */ |
10448 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10449 | return -EBADFD; | |
10450 | ||
21b55dbc | 10451 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 10452 | if (ctx->restrictions.registered) |
21b55dbc SG |
10453 | return -EBUSY; |
10454 | ||
10455 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
10456 | return -EINVAL; | |
10457 | ||
10458 | size = array_size(nr_args, sizeof(*res)); | |
10459 | if (size == SIZE_MAX) | |
10460 | return -EOVERFLOW; | |
10461 | ||
10462 | res = memdup_user(arg, size); | |
10463 | if (IS_ERR(res)) | |
10464 | return PTR_ERR(res); | |
10465 | ||
10466 | ret = 0; | |
10467 | ||
10468 | for (i = 0; i < nr_args; i++) { | |
10469 | switch (res[i].opcode) { | |
10470 | case IORING_RESTRICTION_REGISTER_OP: | |
10471 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
10472 | ret = -EINVAL; | |
10473 | goto out; | |
10474 | } | |
10475 | ||
10476 | __set_bit(res[i].register_op, | |
10477 | ctx->restrictions.register_op); | |
10478 | break; | |
10479 | case IORING_RESTRICTION_SQE_OP: | |
10480 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
10481 | ret = -EINVAL; | |
10482 | goto out; | |
10483 | } | |
10484 | ||
10485 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
10486 | break; | |
10487 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
10488 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
10489 | break; | |
10490 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
10491 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
10492 | break; | |
10493 | default: | |
10494 | ret = -EINVAL; | |
10495 | goto out; | |
10496 | } | |
10497 | } | |
10498 | ||
10499 | out: | |
10500 | /* Reset all restrictions if an error happened */ | |
10501 | if (ret != 0) | |
10502 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
10503 | else | |
7e84e1c7 | 10504 | ctx->restrictions.registered = true; |
21b55dbc SG |
10505 | |
10506 | kfree(res); | |
10507 | return ret; | |
10508 | } | |
10509 | ||
7e84e1c7 SG |
10510 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
10511 | { | |
10512 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10513 | return -EBADFD; | |
10514 | ||
10515 | if (ctx->restrictions.registered) | |
10516 | ctx->restricted = 1; | |
10517 | ||
0298ef96 PB |
10518 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
10519 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) | |
10520 | wake_up(&ctx->sq_data->wait); | |
7e84e1c7 SG |
10521 | return 0; |
10522 | } | |
10523 | ||
fdecb662 | 10524 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 10525 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 PB |
10526 | unsigned nr_args) |
10527 | { | |
10528 | __u32 tmp; | |
10529 | int err; | |
10530 | ||
10531 | if (check_add_overflow(up->offset, nr_args, &tmp)) | |
10532 | return -EOVERFLOW; | |
10533 | err = io_rsrc_node_switch_start(ctx); | |
10534 | if (err) | |
10535 | return err; | |
10536 | ||
fdecb662 PB |
10537 | switch (type) { |
10538 | case IORING_RSRC_FILE: | |
98f0b3b4 | 10539 | return __io_sqe_files_update(ctx, up, nr_args); |
634d00df PB |
10540 | case IORING_RSRC_BUFFER: |
10541 | return __io_sqe_buffers_update(ctx, up, nr_args); | |
98f0b3b4 PB |
10542 | } |
10543 | return -EINVAL; | |
10544 | } | |
10545 | ||
c3bdad02 PB |
10546 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
10547 | unsigned nr_args) | |
98f0b3b4 | 10548 | { |
c3bdad02 | 10549 | struct io_uring_rsrc_update2 up; |
98f0b3b4 PB |
10550 | |
10551 | if (!nr_args) | |
10552 | return -EINVAL; | |
c3bdad02 PB |
10553 | memset(&up, 0, sizeof(up)); |
10554 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) | |
10555 | return -EFAULT; | |
c9747fa5 | 10556 | if (up.resv || up.resv2) |
4dd8f4c2 | 10557 | return -EINVAL; |
c3bdad02 PB |
10558 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); |
10559 | } | |
10560 | ||
10561 | static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
992da01a | 10562 | unsigned size, unsigned type) |
c3bdad02 PB |
10563 | { |
10564 | struct io_uring_rsrc_update2 up; | |
10565 | ||
10566 | if (size != sizeof(up)) | |
10567 | return -EINVAL; | |
98f0b3b4 PB |
10568 | if (copy_from_user(&up, arg, sizeof(up))) |
10569 | return -EFAULT; | |
c9747fa5 | 10570 | if (!up.nr || up.resv || up.resv2) |
98f0b3b4 | 10571 | return -EINVAL; |
992da01a | 10572 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
98f0b3b4 PB |
10573 | } |
10574 | ||
792e3582 | 10575 | static int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
992da01a | 10576 | unsigned int size, unsigned int type) |
792e3582 PB |
10577 | { |
10578 | struct io_uring_rsrc_register rr; | |
10579 | ||
10580 | /* keep it extendible */ | |
10581 | if (size != sizeof(rr)) | |
10582 | return -EINVAL; | |
10583 | ||
10584 | memset(&rr, 0, sizeof(rr)); | |
10585 | if (copy_from_user(&rr, arg, size)) | |
10586 | return -EFAULT; | |
992da01a | 10587 | if (!rr.nr || rr.resv || rr.resv2) |
792e3582 PB |
10588 | return -EINVAL; |
10589 | ||
992da01a | 10590 | switch (type) { |
792e3582 PB |
10591 | case IORING_RSRC_FILE: |
10592 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), | |
10593 | rr.nr, u64_to_user_ptr(rr.tags)); | |
634d00df PB |
10594 | case IORING_RSRC_BUFFER: |
10595 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), | |
10596 | rr.nr, u64_to_user_ptr(rr.tags)); | |
792e3582 PB |
10597 | } |
10598 | return -EINVAL; | |
10599 | } | |
10600 | ||
fe76421d JA |
10601 | static int io_register_iowq_aff(struct io_ring_ctx *ctx, void __user *arg, |
10602 | unsigned len) | |
10603 | { | |
10604 | struct io_uring_task *tctx = current->io_uring; | |
10605 | cpumask_var_t new_mask; | |
10606 | int ret; | |
10607 | ||
10608 | if (!tctx || !tctx->io_wq) | |
10609 | return -EINVAL; | |
10610 | ||
10611 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | |
10612 | return -ENOMEM; | |
10613 | ||
10614 | cpumask_clear(new_mask); | |
10615 | if (len > cpumask_size()) | |
10616 | len = cpumask_size(); | |
10617 | ||
fd213661 ES |
10618 | if (in_compat_syscall()) { |
10619 | ret = compat_get_bitmap(cpumask_bits(new_mask), | |
10620 | (const compat_ulong_t __user *)arg, | |
10621 | len * 8 /* CHAR_BIT */); | |
10622 | } else { | |
10623 | ret = copy_from_user(new_mask, arg, len); | |
10624 | } | |
10625 | ||
10626 | if (ret) { | |
fe76421d JA |
10627 | free_cpumask_var(new_mask); |
10628 | return -EFAULT; | |
10629 | } | |
10630 | ||
10631 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); | |
10632 | free_cpumask_var(new_mask); | |
10633 | return ret; | |
10634 | } | |
10635 | ||
10636 | static int io_unregister_iowq_aff(struct io_ring_ctx *ctx) | |
10637 | { | |
10638 | struct io_uring_task *tctx = current->io_uring; | |
10639 | ||
10640 | if (!tctx || !tctx->io_wq) | |
10641 | return -EINVAL; | |
10642 | ||
10643 | return io_wq_cpu_affinity(tctx->io_wq, NULL); | |
10644 | } | |
10645 | ||
2e480058 JA |
10646 | static int io_register_iowq_max_workers(struct io_ring_ctx *ctx, |
10647 | void __user *arg) | |
b22fa62a | 10648 | __must_hold(&ctx->uring_lock) |
2e480058 | 10649 | { |
b22fa62a | 10650 | struct io_tctx_node *node; |
fa84693b JA |
10651 | struct io_uring_task *tctx = NULL; |
10652 | struct io_sq_data *sqd = NULL; | |
2e480058 JA |
10653 | __u32 new_count[2]; |
10654 | int i, ret; | |
10655 | ||
2e480058 JA |
10656 | if (copy_from_user(new_count, arg, sizeof(new_count))) |
10657 | return -EFAULT; | |
10658 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
10659 | if (new_count[i] > INT_MAX) | |
10660 | return -EINVAL; | |
10661 | ||
fa84693b JA |
10662 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
10663 | sqd = ctx->sq_data; | |
10664 | if (sqd) { | |
009ad9f0 JA |
10665 | /* |
10666 | * Observe the correct sqd->lock -> ctx->uring_lock | |
10667 | * ordering. Fine to drop uring_lock here, we hold | |
10668 | * a ref to the ctx. | |
10669 | */ | |
41d3a6bd | 10670 | refcount_inc(&sqd->refs); |
009ad9f0 | 10671 | mutex_unlock(&ctx->uring_lock); |
fa84693b | 10672 | mutex_lock(&sqd->lock); |
009ad9f0 | 10673 | mutex_lock(&ctx->uring_lock); |
41d3a6bd JA |
10674 | if (sqd->thread) |
10675 | tctx = sqd->thread->io_uring; | |
fa84693b JA |
10676 | } |
10677 | } else { | |
10678 | tctx = current->io_uring; | |
10679 | } | |
10680 | ||
e139a1ec | 10681 | BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits)); |
fa84693b | 10682 | |
c29d963b PB |
10683 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
10684 | if (new_count[i]) | |
10685 | ctx->iowq_limits[i] = new_count[i]; | |
e139a1ec PB |
10686 | ctx->iowq_limits_set = true; |
10687 | ||
10688 | ret = -EINVAL; | |
10689 | if (tctx && tctx->io_wq) { | |
10690 | ret = io_wq_max_workers(tctx->io_wq, new_count); | |
10691 | if (ret) | |
10692 | goto err; | |
10693 | } else { | |
10694 | memset(new_count, 0, sizeof(new_count)); | |
10695 | } | |
fa84693b | 10696 | |
41d3a6bd | 10697 | if (sqd) { |
fa84693b | 10698 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
10699 | io_put_sq_data(sqd); |
10700 | } | |
2e480058 JA |
10701 | |
10702 | if (copy_to_user(arg, new_count, sizeof(new_count))) | |
10703 | return -EFAULT; | |
10704 | ||
b22fa62a PB |
10705 | /* that's it for SQPOLL, only the SQPOLL task creates requests */ |
10706 | if (sqd) | |
10707 | return 0; | |
10708 | ||
10709 | /* now propagate the restriction to all registered users */ | |
10710 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
10711 | struct io_uring_task *tctx = node->task->io_uring; | |
10712 | ||
10713 | if (WARN_ON_ONCE(!tctx->io_wq)) | |
10714 | continue; | |
10715 | ||
10716 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
10717 | new_count[i] = ctx->iowq_limits[i]; | |
10718 | /* ignore errors, it always returns zero anyway */ | |
10719 | (void)io_wq_max_workers(tctx->io_wq, new_count); | |
10720 | } | |
2e480058 | 10721 | return 0; |
fa84693b | 10722 | err: |
41d3a6bd | 10723 | if (sqd) { |
fa84693b | 10724 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
10725 | io_put_sq_data(sqd); |
10726 | } | |
fa84693b | 10727 | return ret; |
2e480058 JA |
10728 | } |
10729 | ||
071698e1 JA |
10730 | static bool io_register_op_must_quiesce(int op) |
10731 | { | |
10732 | switch (op) { | |
bd54b6fe BM |
10733 | case IORING_REGISTER_BUFFERS: |
10734 | case IORING_UNREGISTER_BUFFERS: | |
f4f7d21c | 10735 | case IORING_REGISTER_FILES: |
071698e1 JA |
10736 | case IORING_UNREGISTER_FILES: |
10737 | case IORING_REGISTER_FILES_UPDATE: | |
10738 | case IORING_REGISTER_PROBE: | |
10739 | case IORING_REGISTER_PERSONALITY: | |
10740 | case IORING_UNREGISTER_PERSONALITY: | |
992da01a PB |
10741 | case IORING_REGISTER_FILES2: |
10742 | case IORING_REGISTER_FILES_UPDATE2: | |
10743 | case IORING_REGISTER_BUFFERS2: | |
10744 | case IORING_REGISTER_BUFFERS_UPDATE: | |
fe76421d JA |
10745 | case IORING_REGISTER_IOWQ_AFF: |
10746 | case IORING_UNREGISTER_IOWQ_AFF: | |
2e480058 | 10747 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
071698e1 JA |
10748 | return false; |
10749 | default: | |
10750 | return true; | |
10751 | } | |
10752 | } | |
10753 | ||
e73c5c7c PB |
10754 | static int io_ctx_quiesce(struct io_ring_ctx *ctx) |
10755 | { | |
10756 | long ret; | |
10757 | ||
10758 | percpu_ref_kill(&ctx->refs); | |
10759 | ||
10760 | /* | |
10761 | * Drop uring mutex before waiting for references to exit. If another | |
10762 | * thread is currently inside io_uring_enter() it might need to grab the | |
10763 | * uring_lock to make progress. If we hold it here across the drain | |
10764 | * wait, then we can deadlock. It's safe to drop the mutex here, since | |
10765 | * no new references will come in after we've killed the percpu ref. | |
10766 | */ | |
10767 | mutex_unlock(&ctx->uring_lock); | |
10768 | do { | |
10769 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
10770 | if (!ret) | |
10771 | break; | |
10772 | ret = io_run_task_work_sig(); | |
10773 | } while (ret >= 0); | |
10774 | mutex_lock(&ctx->uring_lock); | |
10775 | ||
10776 | if (ret) | |
10777 | io_refs_resurrect(&ctx->refs, &ctx->ref_comp); | |
10778 | return ret; | |
10779 | } | |
10780 | ||
edafccee JA |
10781 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
10782 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
10783 | __releases(ctx->uring_lock) |
10784 | __acquires(ctx->uring_lock) | |
edafccee JA |
10785 | { |
10786 | int ret; | |
10787 | ||
35fa71a0 JA |
10788 | /* |
10789 | * We're inside the ring mutex, if the ref is already dying, then | |
10790 | * someone else killed the ctx or is already going through | |
10791 | * io_uring_register(). | |
10792 | */ | |
10793 | if (percpu_ref_is_dying(&ctx->refs)) | |
10794 | return -ENXIO; | |
10795 | ||
75c4021a PB |
10796 | if (ctx->restricted) { |
10797 | if (opcode >= IORING_REGISTER_LAST) | |
10798 | return -EINVAL; | |
10799 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); | |
10800 | if (!test_bit(opcode, ctx->restrictions.register_op)) | |
10801 | return -EACCES; | |
10802 | } | |
10803 | ||
071698e1 | 10804 | if (io_register_op_must_quiesce(opcode)) { |
e73c5c7c PB |
10805 | ret = io_ctx_quiesce(ctx); |
10806 | if (ret) | |
f70865db | 10807 | return ret; |
05f3fb3c | 10808 | } |
edafccee JA |
10809 | |
10810 | switch (opcode) { | |
10811 | case IORING_REGISTER_BUFFERS: | |
634d00df | 10812 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
edafccee JA |
10813 | break; |
10814 | case IORING_UNREGISTER_BUFFERS: | |
10815 | ret = -EINVAL; | |
10816 | if (arg || nr_args) | |
10817 | break; | |
0a96bbe4 | 10818 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 10819 | break; |
6b06314c | 10820 | case IORING_REGISTER_FILES: |
792e3582 | 10821 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
6b06314c JA |
10822 | break; |
10823 | case IORING_UNREGISTER_FILES: | |
10824 | ret = -EINVAL; | |
10825 | if (arg || nr_args) | |
10826 | break; | |
10827 | ret = io_sqe_files_unregister(ctx); | |
10828 | break; | |
c3a31e60 | 10829 | case IORING_REGISTER_FILES_UPDATE: |
c3bdad02 | 10830 | ret = io_register_files_update(ctx, arg, nr_args); |
c3a31e60 | 10831 | break; |
9b402849 | 10832 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 10833 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
10834 | ret = -EINVAL; |
10835 | if (nr_args != 1) | |
10836 | break; | |
10837 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
10838 | if (ret) |
10839 | break; | |
10840 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
10841 | ctx->eventfd_async = 1; | |
10842 | else | |
10843 | ctx->eventfd_async = 0; | |
9b402849 JA |
10844 | break; |
10845 | case IORING_UNREGISTER_EVENTFD: | |
10846 | ret = -EINVAL; | |
10847 | if (arg || nr_args) | |
10848 | break; | |
10849 | ret = io_eventfd_unregister(ctx); | |
10850 | break; | |
66f4af93 JA |
10851 | case IORING_REGISTER_PROBE: |
10852 | ret = -EINVAL; | |
10853 | if (!arg || nr_args > 256) | |
10854 | break; | |
10855 | ret = io_probe(ctx, arg, nr_args); | |
10856 | break; | |
071698e1 JA |
10857 | case IORING_REGISTER_PERSONALITY: |
10858 | ret = -EINVAL; | |
10859 | if (arg || nr_args) | |
10860 | break; | |
10861 | ret = io_register_personality(ctx); | |
10862 | break; | |
10863 | case IORING_UNREGISTER_PERSONALITY: | |
10864 | ret = -EINVAL; | |
10865 | if (arg) | |
10866 | break; | |
10867 | ret = io_unregister_personality(ctx, nr_args); | |
10868 | break; | |
7e84e1c7 SG |
10869 | case IORING_REGISTER_ENABLE_RINGS: |
10870 | ret = -EINVAL; | |
10871 | if (arg || nr_args) | |
10872 | break; | |
10873 | ret = io_register_enable_rings(ctx); | |
10874 | break; | |
21b55dbc SG |
10875 | case IORING_REGISTER_RESTRICTIONS: |
10876 | ret = io_register_restrictions(ctx, arg, nr_args); | |
10877 | break; | |
992da01a PB |
10878 | case IORING_REGISTER_FILES2: |
10879 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); | |
10880 | break; | |
10881 | case IORING_REGISTER_FILES_UPDATE2: | |
10882 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
10883 | IORING_RSRC_FILE); | |
10884 | break; | |
10885 | case IORING_REGISTER_BUFFERS2: | |
10886 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); | |
792e3582 | 10887 | break; |
992da01a PB |
10888 | case IORING_REGISTER_BUFFERS_UPDATE: |
10889 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
10890 | IORING_RSRC_BUFFER); | |
c3bdad02 | 10891 | break; |
fe76421d JA |
10892 | case IORING_REGISTER_IOWQ_AFF: |
10893 | ret = -EINVAL; | |
10894 | if (!arg || !nr_args) | |
10895 | break; | |
10896 | ret = io_register_iowq_aff(ctx, arg, nr_args); | |
10897 | break; | |
10898 | case IORING_UNREGISTER_IOWQ_AFF: | |
10899 | ret = -EINVAL; | |
10900 | if (arg || nr_args) | |
10901 | break; | |
10902 | ret = io_unregister_iowq_aff(ctx); | |
10903 | break; | |
2e480058 JA |
10904 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
10905 | ret = -EINVAL; | |
10906 | if (!arg || nr_args != 2) | |
10907 | break; | |
10908 | ret = io_register_iowq_max_workers(ctx, arg); | |
10909 | break; | |
edafccee JA |
10910 | default: |
10911 | ret = -EINVAL; | |
10912 | break; | |
10913 | } | |
10914 | ||
071698e1 | 10915 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 10916 | /* bring the ctx back to life */ |
05f3fb3c | 10917 | percpu_ref_reinit(&ctx->refs); |
0f158b4c | 10918 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 10919 | } |
edafccee JA |
10920 | return ret; |
10921 | } | |
10922 | ||
10923 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
10924 | void __user *, arg, unsigned int, nr_args) | |
10925 | { | |
10926 | struct io_ring_ctx *ctx; | |
10927 | long ret = -EBADF; | |
10928 | struct fd f; | |
10929 | ||
10930 | f = fdget(fd); | |
10931 | if (!f.file) | |
10932 | return -EBADF; | |
10933 | ||
10934 | ret = -EOPNOTSUPP; | |
10935 | if (f.file->f_op != &io_uring_fops) | |
10936 | goto out_fput; | |
10937 | ||
10938 | ctx = f.file->private_data; | |
10939 | ||
b6c23dd5 PB |
10940 | io_run_task_work(); |
10941 | ||
edafccee JA |
10942 | mutex_lock(&ctx->uring_lock); |
10943 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
10944 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
10945 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
10946 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
10947 | out_fput: |
10948 | fdput(f); | |
10949 | return ret; | |
10950 | } | |
10951 | ||
2b188cc1 JA |
10952 | static int __init io_uring_init(void) |
10953 | { | |
d7f62e82 SM |
10954 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
10955 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
10956 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
10957 | } while (0) | |
10958 | ||
10959 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
10960 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
10961 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
10962 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
10963 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
10964 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
10965 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
10966 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
10967 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
10968 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 10969 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
10970 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
10971 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
10972 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
10973 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
10974 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
10975 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
10976 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
10977 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
10978 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
10979 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
10980 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
10981 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
10982 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
10983 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
10984 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 10985 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
10986 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
10987 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
16340eab | 10988 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
d7f62e82 | 10989 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
7d67af2c | 10990 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
b9445598 | 10991 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
d7f62e82 | 10992 | |
b0d658ec PB |
10993 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
10994 | sizeof(struct io_uring_rsrc_update)); | |
10995 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > | |
10996 | sizeof(struct io_uring_rsrc_update2)); | |
90499ad0 PB |
10997 | |
10998 | /* ->buf_index is u16 */ | |
10999 | BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); | |
11000 | ||
b0d658ec PB |
11001 | /* should fit into one byte */ |
11002 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); | |
11003 | ||
d3656344 | 11004 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
32c2d33e | 11005 | BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int)); |
16340eab | 11006 | |
91f245d5 JA |
11007 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
11008 | SLAB_ACCOUNT); | |
2b188cc1 JA |
11009 | return 0; |
11010 | }; | |
11011 | __initcall(io_uring_init); |