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 | |
14 | * through a control-dependency in io_get_cqring (smp_store_release to | |
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> |
2b188cc1 | 81 | |
c826bd7a DD |
82 | #define CREATE_TRACE_POINTS |
83 | #include <trace/events/io_uring.h> | |
84 | ||
2b188cc1 JA |
85 | #include <uapi/linux/io_uring.h> |
86 | ||
87 | #include "internal.h" | |
561fb04a | 88 | #include "io-wq.h" |
2b188cc1 | 89 | |
5277deaa | 90 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 91 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
65e19f54 JA |
92 | |
93 | /* | |
94 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs | |
95 | */ | |
96 | #define IORING_FILE_TABLE_SHIFT 9 | |
97 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) | |
98 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) | |
99 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) | |
21b55dbc SG |
100 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
101 | IORING_REGISTER_LAST + IORING_OP_LAST) | |
2b188cc1 | 102 | |
b16fed66 PB |
103 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
104 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ | |
105 | IOSQE_BUFFER_SELECT) | |
106 | ||
2b188cc1 JA |
107 | struct io_uring { |
108 | u32 head ____cacheline_aligned_in_smp; | |
109 | u32 tail ____cacheline_aligned_in_smp; | |
110 | }; | |
111 | ||
1e84b97b | 112 | /* |
75b28aff HV |
113 | * This data is shared with the application through the mmap at offsets |
114 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
115 | * |
116 | * The offsets to the member fields are published through struct | |
117 | * io_sqring_offsets when calling io_uring_setup. | |
118 | */ | |
75b28aff | 119 | struct io_rings { |
1e84b97b SB |
120 | /* |
121 | * Head and tail offsets into the ring; the offsets need to be | |
122 | * masked to get valid indices. | |
123 | * | |
75b28aff HV |
124 | * The kernel controls head of the sq ring and the tail of the cq ring, |
125 | * and the application controls tail of the sq ring and the head of the | |
126 | * cq ring. | |
1e84b97b | 127 | */ |
75b28aff | 128 | struct io_uring sq, cq; |
1e84b97b | 129 | /* |
75b28aff | 130 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
131 | * ring_entries - 1) |
132 | */ | |
75b28aff HV |
133 | u32 sq_ring_mask, cq_ring_mask; |
134 | /* Ring sizes (constant, power of 2) */ | |
135 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
136 | /* |
137 | * Number of invalid entries dropped by the kernel due to | |
138 | * invalid index stored in array | |
139 | * | |
140 | * Written by the kernel, shouldn't be modified by the | |
141 | * application (i.e. get number of "new events" by comparing to | |
142 | * cached value). | |
143 | * | |
144 | * After a new SQ head value was read by the application this | |
145 | * counter includes all submissions that were dropped reaching | |
146 | * the new SQ head (and possibly more). | |
147 | */ | |
75b28aff | 148 | u32 sq_dropped; |
1e84b97b | 149 | /* |
0d9b5b3a | 150 | * Runtime SQ flags |
1e84b97b SB |
151 | * |
152 | * Written by the kernel, shouldn't be modified by the | |
153 | * application. | |
154 | * | |
155 | * The application needs a full memory barrier before checking | |
156 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
157 | */ | |
75b28aff | 158 | u32 sq_flags; |
0d9b5b3a SG |
159 | /* |
160 | * Runtime CQ flags | |
161 | * | |
162 | * Written by the application, shouldn't be modified by the | |
163 | * kernel. | |
164 | */ | |
165 | u32 cq_flags; | |
1e84b97b SB |
166 | /* |
167 | * Number of completion events lost because the queue was full; | |
168 | * this should be avoided by the application by making sure | |
0b4295b5 | 169 | * there are not more requests pending than there is space in |
1e84b97b SB |
170 | * the completion queue. |
171 | * | |
172 | * Written by the kernel, shouldn't be modified by the | |
173 | * application (i.e. get number of "new events" by comparing to | |
174 | * cached value). | |
175 | * | |
176 | * As completion events come in out of order this counter is not | |
177 | * ordered with any other data. | |
178 | */ | |
75b28aff | 179 | u32 cq_overflow; |
1e84b97b SB |
180 | /* |
181 | * Ring buffer of completion events. | |
182 | * | |
183 | * The kernel writes completion events fresh every time they are | |
184 | * produced, so the application is allowed to modify pending | |
185 | * entries. | |
186 | */ | |
75b28aff | 187 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
188 | }; |
189 | ||
45d189c6 PB |
190 | enum io_uring_cmd_flags { |
191 | IO_URING_F_NONBLOCK = 1, | |
889fca73 | 192 | IO_URING_F_COMPLETE_DEFER = 2, |
45d189c6 PB |
193 | }; |
194 | ||
edafccee JA |
195 | struct io_mapped_ubuf { |
196 | u64 ubuf; | |
4751f53d | 197 | u64 ubuf_end; |
edafccee JA |
198 | struct bio_vec *bvec; |
199 | unsigned int nr_bvecs; | |
de293938 | 200 | unsigned long acct_pages; |
edafccee JA |
201 | }; |
202 | ||
50238531 BM |
203 | struct io_ring_ctx; |
204 | ||
6c2450ae PB |
205 | struct io_overflow_cqe { |
206 | struct io_uring_cqe cqe; | |
207 | struct list_head list; | |
208 | }; | |
209 | ||
a04b0ac0 PB |
210 | struct io_fixed_file { |
211 | /* file * with additional FFS_* flags */ | |
212 | unsigned long file_ptr; | |
213 | }; | |
214 | ||
269bbe5f BM |
215 | struct io_rsrc_put { |
216 | struct list_head list; | |
50238531 BM |
217 | union { |
218 | void *rsrc; | |
219 | struct file *file; | |
220 | }; | |
269bbe5f BM |
221 | }; |
222 | ||
aeca241b PB |
223 | struct io_file_table { |
224 | /* two level table */ | |
225 | struct io_fixed_file **files; | |
31b51510 JA |
226 | }; |
227 | ||
b895c9a6 | 228 | struct io_rsrc_node { |
05589553 XW |
229 | struct percpu_ref refs; |
230 | struct list_head node; | |
269bbe5f | 231 | struct list_head rsrc_list; |
b895c9a6 | 232 | struct io_rsrc_data *rsrc_data; |
4a38aed2 | 233 | struct llist_node llist; |
e297822b | 234 | bool done; |
05589553 XW |
235 | }; |
236 | ||
40ae0ff7 PB |
237 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
238 | ||
b895c9a6 | 239 | struct io_rsrc_data { |
05f3fb3c JA |
240 | struct io_ring_ctx *ctx; |
241 | ||
40ae0ff7 | 242 | rsrc_put_fn *do_put; |
3e942498 | 243 | atomic_t refs; |
05f3fb3c | 244 | struct completion done; |
8bad28d8 | 245 | bool quiesce; |
05f3fb3c JA |
246 | }; |
247 | ||
5a2e745d JA |
248 | struct io_buffer { |
249 | struct list_head list; | |
250 | __u64 addr; | |
251 | __s32 len; | |
252 | __u16 bid; | |
253 | }; | |
254 | ||
21b55dbc SG |
255 | struct io_restriction { |
256 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
257 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
258 | u8 sqe_flags_allowed; | |
259 | u8 sqe_flags_required; | |
7e84e1c7 | 260 | bool registered; |
21b55dbc SG |
261 | }; |
262 | ||
37d1e2e3 JA |
263 | enum { |
264 | IO_SQ_THREAD_SHOULD_STOP = 0, | |
265 | IO_SQ_THREAD_SHOULD_PARK, | |
266 | }; | |
267 | ||
534ca6d6 JA |
268 | struct io_sq_data { |
269 | refcount_t refs; | |
9e138a48 | 270 | atomic_t park_pending; |
09a6f4ef | 271 | struct mutex lock; |
69fb2131 JA |
272 | |
273 | /* ctx's that are using this sqd */ | |
274 | struct list_head ctx_list; | |
69fb2131 | 275 | |
534ca6d6 JA |
276 | struct task_struct *thread; |
277 | struct wait_queue_head wait; | |
08369246 XW |
278 | |
279 | unsigned sq_thread_idle; | |
37d1e2e3 JA |
280 | int sq_cpu; |
281 | pid_t task_pid; | |
5c2469e0 | 282 | pid_t task_tgid; |
37d1e2e3 JA |
283 | |
284 | unsigned long state; | |
37d1e2e3 | 285 | struct completion exited; |
b7f5a0bf | 286 | struct callback_head *park_task_work; |
534ca6d6 JA |
287 | }; |
288 | ||
258b29a9 | 289 | #define IO_IOPOLL_BATCH 8 |
6dd0be1e | 290 | #define IO_COMPL_BATCH 32 |
6ff119a6 | 291 | #define IO_REQ_CACHE_SIZE 32 |
bf019da7 | 292 | #define IO_REQ_ALLOC_BATCH 8 |
258b29a9 PB |
293 | |
294 | struct io_comp_state { | |
6dd0be1e | 295 | struct io_kiocb *reqs[IO_COMPL_BATCH]; |
1b4c351f | 296 | unsigned int nr; |
c7dae4ba JA |
297 | unsigned int locked_free_nr; |
298 | /* inline/task_work completion list, under ->uring_lock */ | |
1b4c351f | 299 | struct list_head free_list; |
c7dae4ba JA |
300 | /* IRQ completion list, under ->completion_lock */ |
301 | struct list_head locked_free_list; | |
258b29a9 PB |
302 | }; |
303 | ||
a1ab7b35 PB |
304 | struct io_submit_link { |
305 | struct io_kiocb *head; | |
306 | struct io_kiocb *last; | |
307 | }; | |
308 | ||
258b29a9 PB |
309 | struct io_submit_state { |
310 | struct blk_plug plug; | |
a1ab7b35 | 311 | struct io_submit_link link; |
258b29a9 PB |
312 | |
313 | /* | |
314 | * io_kiocb alloc cache | |
315 | */ | |
bf019da7 | 316 | void *reqs[IO_REQ_CACHE_SIZE]; |
258b29a9 PB |
317 | unsigned int free_reqs; |
318 | ||
319 | bool plug_started; | |
320 | ||
321 | /* | |
322 | * Batch completion logic | |
323 | */ | |
324 | struct io_comp_state comp; | |
325 | ||
326 | /* | |
327 | * File reference cache | |
328 | */ | |
329 | struct file *file; | |
330 | unsigned int fd; | |
331 | unsigned int file_refs; | |
332 | unsigned int ios_left; | |
333 | }; | |
334 | ||
2b188cc1 JA |
335 | struct io_ring_ctx { |
336 | struct { | |
337 | struct percpu_ref refs; | |
338 | } ____cacheline_aligned_in_smp; | |
339 | ||
340 | struct { | |
341 | unsigned int flags; | |
e1d85334 | 342 | unsigned int compat: 1; |
e1d85334 RD |
343 | unsigned int drain_next: 1; |
344 | unsigned int eventfd_async: 1; | |
21b55dbc | 345 | unsigned int restricted: 1; |
2b188cc1 | 346 | |
75b28aff HV |
347 | /* |
348 | * Ring buffer of indices into array of io_uring_sqe, which is | |
349 | * mmapped by the application using the IORING_OFF_SQES offset. | |
350 | * | |
351 | * This indirection could e.g. be used to assign fixed | |
352 | * io_uring_sqe entries to operations and only submit them to | |
353 | * the queue when needed. | |
354 | * | |
355 | * The kernel modifies neither the indices array nor the entries | |
356 | * array. | |
357 | */ | |
358 | u32 *sq_array; | |
2b188cc1 JA |
359 | unsigned cached_sq_head; |
360 | unsigned sq_entries; | |
361 | unsigned sq_mask; | |
6c271ce2 | 362 | unsigned sq_thread_idle; |
498ccd9e | 363 | unsigned cached_sq_dropped; |
2c3bac6d | 364 | unsigned cached_cq_overflow; |
ad3eb2c8 | 365 | unsigned long sq_check_overflow; |
de0617e4 | 366 | |
e941894e JA |
367 | /* hashed buffered write serialization */ |
368 | struct io_wq_hash *hash_map; | |
369 | ||
de0617e4 | 370 | struct list_head defer_list; |
5262f567 | 371 | struct list_head timeout_list; |
1d7bb1d5 | 372 | struct list_head cq_overflow_list; |
fcb323cc | 373 | |
ad3eb2c8 | 374 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
375 | } ____cacheline_aligned_in_smp; |
376 | ||
3c1a2ead JA |
377 | struct { |
378 | struct mutex uring_lock; | |
379 | wait_queue_head_t wait; | |
380 | } ____cacheline_aligned_in_smp; | |
381 | ||
382 | struct io_submit_state submit_state; | |
383 | ||
206aefde JA |
384 | struct io_rings *rings; |
385 | ||
2aede0e4 JA |
386 | /* Only used for accounting purposes */ |
387 | struct mm_struct *mm_account; | |
388 | ||
7c30f36a | 389 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
534ca6d6 JA |
390 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
391 | ||
90554200 | 392 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 393 | struct list_head sqd_list; |
75b28aff | 394 | |
6b06314c JA |
395 | /* |
396 | * If used, fixed file set. Writers must ensure that ->refs is dead, | |
397 | * readers must ensure that ->refs is alive as long as the file* is | |
398 | * used. Only updated through io_uring_register(2). | |
399 | */ | |
b895c9a6 | 400 | struct io_rsrc_data *file_data; |
aeca241b | 401 | struct io_file_table file_table; |
6b06314c JA |
402 | unsigned nr_user_files; |
403 | ||
edafccee JA |
404 | /* if used, fixed mapped user buffers */ |
405 | unsigned nr_user_bufs; | |
406 | struct io_mapped_ubuf *user_bufs; | |
407 | ||
2b188cc1 JA |
408 | struct user_struct *user; |
409 | ||
0f158b4c | 410 | struct completion ref_comp; |
206aefde JA |
411 | |
412 | #if defined(CONFIG_UNIX) | |
413 | struct socket *ring_sock; | |
414 | #endif | |
415 | ||
9e15c3a0 | 416 | struct xarray io_buffers; |
5a2e745d | 417 | |
61cf9370 MWO |
418 | struct xarray personalities; |
419 | u32 pers_next; | |
071698e1 | 420 | |
206aefde JA |
421 | struct { |
422 | unsigned cached_cq_tail; | |
423 | unsigned cq_entries; | |
424 | unsigned cq_mask; | |
425 | atomic_t cq_timeouts; | |
f010505b | 426 | unsigned cq_last_tm_flush; |
ad3eb2c8 | 427 | unsigned long cq_check_overflow; |
206aefde JA |
428 | struct wait_queue_head cq_wait; |
429 | struct fasync_struct *cq_fasync; | |
430 | struct eventfd_ctx *cq_ev_fd; | |
431 | } ____cacheline_aligned_in_smp; | |
2b188cc1 | 432 | |
2b188cc1 JA |
433 | struct { |
434 | spinlock_t completion_lock; | |
e94f141b | 435 | |
def596e9 | 436 | /* |
540e32a0 | 437 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
438 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
439 | * For SQPOLL, only the single threaded io_sq_thread() will | |
440 | * manipulate the list, hence no extra locking is needed there. | |
441 | */ | |
540e32a0 | 442 | struct list_head iopoll_list; |
78076bb6 JA |
443 | struct hlist_head *cancel_hash; |
444 | unsigned cancel_hash_bits; | |
e94f141b | 445 | bool poll_multi_file; |
2b188cc1 | 446 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 447 | |
269bbe5f BM |
448 | struct delayed_work rsrc_put_work; |
449 | struct llist_head rsrc_put_llist; | |
d67d2263 BM |
450 | struct list_head rsrc_ref_list; |
451 | spinlock_t rsrc_ref_lock; | |
a7f0ed5a | 452 | struct io_rsrc_node *rsrc_node; |
b895c9a6 | 453 | struct io_rsrc_node *rsrc_backup_node; |
4a38aed2 | 454 | |
21b55dbc | 455 | struct io_restriction restrictions; |
3c1a2ead | 456 | |
7c25c0d1 JA |
457 | /* exit task_work */ |
458 | struct callback_head *exit_task_work; | |
459 | ||
3c1a2ead JA |
460 | /* Keep this last, we don't need it for the fast path */ |
461 | struct work_struct exit_work; | |
13bf43f5 | 462 | struct list_head tctx_list; |
2b188cc1 JA |
463 | }; |
464 | ||
53e043b2 SM |
465 | struct io_uring_task { |
466 | /* submission side */ | |
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; | |
477 | unsigned long task_state; | |
478 | struct callback_head task_work; | |
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; |
8c838788 | 489 | bool done; |
221c5eb2 | 490 | bool canceled; |
9d805892 PB |
491 | struct wait_queue_entry wait; |
492 | }; | |
493 | ||
494 | struct io_poll_update { | |
495 | struct file *file; | |
496 | u64 old_user_data; | |
497 | u64 new_user_data; | |
498 | __poll_t events; | |
b69de288 JA |
499 | bool update_events; |
500 | bool update_user_data; | |
221c5eb2 JA |
501 | }; |
502 | ||
018043be PB |
503 | struct io_poll_remove { |
504 | struct file *file; | |
505 | u64 addr; | |
506 | }; | |
507 | ||
b5dba59e JA |
508 | struct io_close { |
509 | struct file *file; | |
b5dba59e JA |
510 | int fd; |
511 | }; | |
512 | ||
ad8a48ac JA |
513 | struct io_timeout_data { |
514 | struct io_kiocb *req; | |
515 | struct hrtimer timer; | |
516 | struct timespec64 ts; | |
517 | enum hrtimer_mode mode; | |
518 | }; | |
519 | ||
8ed8d3c3 JA |
520 | struct io_accept { |
521 | struct file *file; | |
522 | struct sockaddr __user *addr; | |
523 | int __user *addr_len; | |
524 | int flags; | |
09952e3e | 525 | unsigned long nofile; |
8ed8d3c3 JA |
526 | }; |
527 | ||
528 | struct io_sync { | |
529 | struct file *file; | |
530 | loff_t len; | |
531 | loff_t off; | |
532 | int flags; | |
d63d1b5e | 533 | int mode; |
8ed8d3c3 JA |
534 | }; |
535 | ||
fbf23849 JA |
536 | struct io_cancel { |
537 | struct file *file; | |
538 | u64 addr; | |
539 | }; | |
540 | ||
b29472ee JA |
541 | struct io_timeout { |
542 | struct file *file; | |
bfe68a22 PB |
543 | u32 off; |
544 | u32 target_seq; | |
135fcde8 | 545 | struct list_head list; |
90cd7e42 PB |
546 | /* head of the link, used by linked timeouts only */ |
547 | struct io_kiocb *head; | |
b29472ee JA |
548 | }; |
549 | ||
0bdf7a2d PB |
550 | struct io_timeout_rem { |
551 | struct file *file; | |
552 | u64 addr; | |
9c8e11b3 PB |
553 | |
554 | /* timeout update */ | |
555 | struct timespec64 ts; | |
556 | u32 flags; | |
0bdf7a2d PB |
557 | }; |
558 | ||
9adbd45d JA |
559 | struct io_rw { |
560 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
561 | struct kiocb kiocb; | |
562 | u64 addr; | |
563 | u64 len; | |
564 | }; | |
565 | ||
3fbb51c1 JA |
566 | struct io_connect { |
567 | struct file *file; | |
568 | struct sockaddr __user *addr; | |
569 | int addr_len; | |
570 | }; | |
571 | ||
e47293fd JA |
572 | struct io_sr_msg { |
573 | struct file *file; | |
fddaface | 574 | union { |
4af3417a PB |
575 | struct compat_msghdr __user *umsg_compat; |
576 | struct user_msghdr __user *umsg; | |
577 | void __user *buf; | |
fddaface | 578 | }; |
e47293fd | 579 | int msg_flags; |
bcda7baa | 580 | int bgid; |
fddaface | 581 | size_t len; |
bcda7baa | 582 | struct io_buffer *kbuf; |
e47293fd JA |
583 | }; |
584 | ||
15b71abe JA |
585 | struct io_open { |
586 | struct file *file; | |
587 | int dfd; | |
15b71abe | 588 | struct filename *filename; |
c12cedf2 | 589 | struct open_how how; |
4022e7af | 590 | unsigned long nofile; |
15b71abe JA |
591 | }; |
592 | ||
269bbe5f | 593 | struct io_rsrc_update { |
05f3fb3c JA |
594 | struct file *file; |
595 | u64 arg; | |
596 | u32 nr_args; | |
597 | u32 offset; | |
598 | }; | |
599 | ||
4840e418 JA |
600 | struct io_fadvise { |
601 | struct file *file; | |
602 | u64 offset; | |
603 | u32 len; | |
604 | u32 advice; | |
605 | }; | |
606 | ||
c1ca757b JA |
607 | struct io_madvise { |
608 | struct file *file; | |
609 | u64 addr; | |
610 | u32 len; | |
611 | u32 advice; | |
612 | }; | |
613 | ||
3e4827b0 JA |
614 | struct io_epoll { |
615 | struct file *file; | |
616 | int epfd; | |
617 | int op; | |
618 | int fd; | |
619 | struct epoll_event event; | |
e47293fd JA |
620 | }; |
621 | ||
7d67af2c PB |
622 | struct io_splice { |
623 | struct file *file_out; | |
624 | struct file *file_in; | |
625 | loff_t off_out; | |
626 | loff_t off_in; | |
627 | u64 len; | |
628 | unsigned int flags; | |
629 | }; | |
630 | ||
ddf0322d JA |
631 | struct io_provide_buf { |
632 | struct file *file; | |
633 | __u64 addr; | |
634 | __s32 len; | |
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 | ||
3ca405eb PB |
670 | struct io_completion { |
671 | struct file *file; | |
672 | struct list_head list; | |
8c3f9cd1 | 673 | u32 cflags; |
3ca405eb PB |
674 | }; |
675 | ||
f499a021 JA |
676 | struct io_async_connect { |
677 | struct sockaddr_storage address; | |
678 | }; | |
679 | ||
03b1230c JA |
680 | struct io_async_msghdr { |
681 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
682 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
683 | struct iovec *free_iov; | |
03b1230c JA |
684 | struct sockaddr __user *uaddr; |
685 | struct msghdr msg; | |
b537916c | 686 | struct sockaddr_storage addr; |
03b1230c JA |
687 | }; |
688 | ||
f67676d1 JA |
689 | struct io_async_rw { |
690 | struct iovec fast_iov[UIO_FASTIOV]; | |
ff6165b2 JA |
691 | const struct iovec *free_iovec; |
692 | struct iov_iter iter; | |
227c0c96 | 693 | size_t bytes_done; |
bcf5a063 | 694 | struct wait_page_queue wpq; |
f67676d1 JA |
695 | }; |
696 | ||
6b47ee6e PB |
697 | enum { |
698 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
699 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
700 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
701 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
702 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 703 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 704 | |
6b47ee6e PB |
705 | REQ_F_FAIL_LINK_BIT, |
706 | REQ_F_INFLIGHT_BIT, | |
707 | REQ_F_CUR_POS_BIT, | |
708 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 709 | REQ_F_LINK_TIMEOUT_BIT, |
99bc4c38 | 710 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 711 | REQ_F_POLLED_BIT, |
bcda7baa | 712 | REQ_F_BUFFER_SELECTED_BIT, |
900fad45 | 713 | REQ_F_LTIMEOUT_ACTIVE_BIT, |
e342c807 | 714 | REQ_F_COMPLETE_INLINE_BIT, |
230d50d4 | 715 | REQ_F_REISSUE_BIT, |
8c130827 | 716 | REQ_F_DONT_REISSUE_BIT, |
9d805892 | 717 | REQ_F_POLL_UPDATE_BIT, |
7b29f92d JA |
718 | /* keep async read/write and isreg together and in order */ |
719 | REQ_F_ASYNC_READ_BIT, | |
720 | REQ_F_ASYNC_WRITE_BIT, | |
721 | REQ_F_ISREG_BIT, | |
84557871 JA |
722 | |
723 | /* not a real bit, just to check we're not overflowing the space */ | |
724 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
725 | }; |
726 | ||
727 | enum { | |
728 | /* ctx owns file */ | |
729 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
730 | /* drain existing IO first */ | |
731 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
732 | /* linked sqes */ | |
733 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
734 | /* doesn't sever on completion < 0 */ | |
735 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
736 | /* IOSQE_ASYNC */ | |
737 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
738 | /* IOSQE_BUFFER_SELECT */ |
739 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 740 | |
6b47ee6e PB |
741 | /* fail rest of links */ |
742 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), | |
b05a1bcd | 743 | /* on inflight list, should be cancelled and waited on exit reliably */ |
6b47ee6e PB |
744 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
745 | /* read/write uses file position */ | |
746 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
747 | /* must not punt to workers */ | |
748 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 749 | /* has or had linked timeout */ |
6b47ee6e | 750 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
99bc4c38 PB |
751 | /* needs cleanup */ |
752 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
753 | /* already went through poll handler */ |
754 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
755 | /* buffer already selected */ |
756 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
900fad45 PB |
757 | /* linked timeout is active, i.e. prepared by link's head */ |
758 | REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT), | |
e342c807 PB |
759 | /* completion is deferred through io_comp_state */ |
760 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
230d50d4 JA |
761 | /* caller should reissue async */ |
762 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), | |
8c130827 PB |
763 | /* don't attempt request reissue, see io_rw_reissue() */ |
764 | REQ_F_DONT_REISSUE = BIT(REQ_F_DONT_REISSUE_BIT), | |
9d805892 PB |
765 | /* switches between poll and poll update */ |
766 | REQ_F_POLL_UPDATE = BIT(REQ_F_POLL_UPDATE_BIT), | |
7b29f92d JA |
767 | /* supports async reads */ |
768 | REQ_F_ASYNC_READ = BIT(REQ_F_ASYNC_READ_BIT), | |
769 | /* supports async writes */ | |
770 | REQ_F_ASYNC_WRITE = BIT(REQ_F_ASYNC_WRITE_BIT), | |
771 | /* regular file */ | |
772 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
d7718a9d JA |
773 | }; |
774 | ||
775 | struct async_poll { | |
776 | struct io_poll_iocb poll; | |
807abcb0 | 777 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
778 | }; |
779 | ||
7cbf1722 JA |
780 | struct io_task_work { |
781 | struct io_wq_work_node node; | |
782 | task_work_func_t func; | |
783 | }; | |
784 | ||
09bb8394 JA |
785 | /* |
786 | * NOTE! Each of the iocb union members has the file pointer | |
787 | * as the first entry in their struct definition. So you can | |
788 | * access the file pointer through any of the sub-structs, | |
789 | * or directly as just 'ki_filp' in this struct. | |
790 | */ | |
2b188cc1 | 791 | struct io_kiocb { |
221c5eb2 | 792 | union { |
09bb8394 | 793 | struct file *file; |
9adbd45d | 794 | struct io_rw rw; |
221c5eb2 | 795 | struct io_poll_iocb poll; |
9d805892 | 796 | struct io_poll_update poll_update; |
018043be | 797 | struct io_poll_remove poll_remove; |
8ed8d3c3 JA |
798 | struct io_accept accept; |
799 | struct io_sync sync; | |
fbf23849 | 800 | struct io_cancel cancel; |
b29472ee | 801 | struct io_timeout timeout; |
0bdf7a2d | 802 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 803 | struct io_connect connect; |
e47293fd | 804 | struct io_sr_msg sr_msg; |
15b71abe | 805 | struct io_open open; |
b5dba59e | 806 | struct io_close close; |
269bbe5f | 807 | struct io_rsrc_update rsrc_update; |
4840e418 | 808 | struct io_fadvise fadvise; |
c1ca757b | 809 | struct io_madvise madvise; |
3e4827b0 | 810 | struct io_epoll epoll; |
7d67af2c | 811 | struct io_splice splice; |
ddf0322d | 812 | struct io_provide_buf pbuf; |
1d9e1288 | 813 | struct io_statx statx; |
36f4fa68 | 814 | struct io_shutdown shutdown; |
80a261fd | 815 | struct io_rename rename; |
14a1143b | 816 | struct io_unlink unlink; |
3ca405eb PB |
817 | /* use only after cleaning per-op data, see io_clean_op() */ |
818 | struct io_completion compl; | |
221c5eb2 | 819 | }; |
2b188cc1 | 820 | |
e8c2bc1f JA |
821 | /* opcode allocated if it needs to store data for async defer */ |
822 | void *async_data; | |
d625c6ee | 823 | u8 opcode; |
65a6543d XW |
824 | /* polled IO has completed */ |
825 | u8 iopoll_completed; | |
2b188cc1 | 826 | |
4f4eeba8 | 827 | u16 buf_index; |
9cf7c104 | 828 | u32 result; |
4f4eeba8 | 829 | |
010e8e6b PB |
830 | struct io_ring_ctx *ctx; |
831 | unsigned int flags; | |
abc54d63 | 832 | atomic_t refs; |
010e8e6b PB |
833 | struct task_struct *task; |
834 | u64 user_data; | |
d7718a9d | 835 | |
f2f87370 | 836 | struct io_kiocb *link; |
269bbe5f | 837 | struct percpu_ref *fixed_rsrc_refs; |
fcb323cc | 838 | |
b303fe2e | 839 | /* used with ctx->iopoll_list with reads/writes */ |
010e8e6b | 840 | struct list_head inflight_entry; |
7cbf1722 JA |
841 | union { |
842 | struct io_task_work io_task_work; | |
843 | struct callback_head task_work; | |
844 | }; | |
010e8e6b PB |
845 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
846 | struct hlist_node hash_node; | |
847 | struct async_poll *apoll; | |
848 | struct io_wq_work work; | |
2b188cc1 | 849 | }; |
05589553 | 850 | |
13bf43f5 PB |
851 | struct io_tctx_node { |
852 | struct list_head ctx_node; | |
853 | struct task_struct *task; | |
13bf43f5 PB |
854 | struct io_ring_ctx *ctx; |
855 | }; | |
856 | ||
27dc8338 PB |
857 | struct io_defer_entry { |
858 | struct list_head list; | |
859 | struct io_kiocb *req; | |
9cf7c104 | 860 | u32 seq; |
2b188cc1 JA |
861 | }; |
862 | ||
d3656344 | 863 | struct io_op_def { |
d3656344 JA |
864 | /* needs req->file assigned */ |
865 | unsigned needs_file : 1; | |
d3656344 JA |
866 | /* hash wq insertion if file is a regular file */ |
867 | unsigned hash_reg_file : 1; | |
868 | /* unbound wq insertion if file is a non-regular file */ | |
869 | unsigned unbound_nonreg_file : 1; | |
66f4af93 JA |
870 | /* opcode is not supported by this kernel */ |
871 | unsigned not_supported : 1; | |
8a72758c JA |
872 | /* set if opcode supports polled "wait" */ |
873 | unsigned pollin : 1; | |
874 | unsigned pollout : 1; | |
bcda7baa JA |
875 | /* op supports buffer selection */ |
876 | unsigned buffer_select : 1; | |
26f0505a PB |
877 | /* do prep async if is going to be punted */ |
878 | unsigned needs_async_setup : 1; | |
27926b68 JA |
879 | /* should block plug */ |
880 | unsigned plug : 1; | |
e8c2bc1f JA |
881 | /* size of async data needed, if any */ |
882 | unsigned short async_size; | |
d3656344 JA |
883 | }; |
884 | ||
0918682b | 885 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
886 | [IORING_OP_NOP] = {}, |
887 | [IORING_OP_READV] = { | |
d3656344 JA |
888 | .needs_file = 1, |
889 | .unbound_nonreg_file = 1, | |
8a72758c | 890 | .pollin = 1, |
4d954c25 | 891 | .buffer_select = 1, |
26f0505a | 892 | .needs_async_setup = 1, |
27926b68 | 893 | .plug = 1, |
e8c2bc1f | 894 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 895 | }, |
0463b6c5 | 896 | [IORING_OP_WRITEV] = { |
d3656344 JA |
897 | .needs_file = 1, |
898 | .hash_reg_file = 1, | |
899 | .unbound_nonreg_file = 1, | |
8a72758c | 900 | .pollout = 1, |
26f0505a | 901 | .needs_async_setup = 1, |
27926b68 | 902 | .plug = 1, |
e8c2bc1f | 903 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 904 | }, |
0463b6c5 | 905 | [IORING_OP_FSYNC] = { |
d3656344 JA |
906 | .needs_file = 1, |
907 | }, | |
0463b6c5 | 908 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
909 | .needs_file = 1, |
910 | .unbound_nonreg_file = 1, | |
8a72758c | 911 | .pollin = 1, |
27926b68 | 912 | .plug = 1, |
e8c2bc1f | 913 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 914 | }, |
0463b6c5 | 915 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
916 | .needs_file = 1, |
917 | .hash_reg_file = 1, | |
918 | .unbound_nonreg_file = 1, | |
8a72758c | 919 | .pollout = 1, |
27926b68 | 920 | .plug = 1, |
e8c2bc1f | 921 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 922 | }, |
0463b6c5 | 923 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
924 | .needs_file = 1, |
925 | .unbound_nonreg_file = 1, | |
926 | }, | |
0463b6c5 PB |
927 | [IORING_OP_POLL_REMOVE] = {}, |
928 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 JA |
929 | .needs_file = 1, |
930 | }, | |
0463b6c5 | 931 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
932 | .needs_file = 1, |
933 | .unbound_nonreg_file = 1, | |
8a72758c | 934 | .pollout = 1, |
26f0505a | 935 | .needs_async_setup = 1, |
e8c2bc1f | 936 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 937 | }, |
0463b6c5 | 938 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
939 | .needs_file = 1, |
940 | .unbound_nonreg_file = 1, | |
8a72758c | 941 | .pollin = 1, |
52de1fe1 | 942 | .buffer_select = 1, |
26f0505a | 943 | .needs_async_setup = 1, |
e8c2bc1f | 944 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 945 | }, |
0463b6c5 | 946 | [IORING_OP_TIMEOUT] = { |
e8c2bc1f | 947 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 948 | }, |
9c8e11b3 PB |
949 | [IORING_OP_TIMEOUT_REMOVE] = { |
950 | /* used by timeout updates' prep() */ | |
9c8e11b3 | 951 | }, |
0463b6c5 | 952 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
953 | .needs_file = 1, |
954 | .unbound_nonreg_file = 1, | |
8a72758c | 955 | .pollin = 1, |
d3656344 | 956 | }, |
0463b6c5 PB |
957 | [IORING_OP_ASYNC_CANCEL] = {}, |
958 | [IORING_OP_LINK_TIMEOUT] = { | |
e8c2bc1f | 959 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 960 | }, |
0463b6c5 | 961 | [IORING_OP_CONNECT] = { |
d3656344 JA |
962 | .needs_file = 1, |
963 | .unbound_nonreg_file = 1, | |
8a72758c | 964 | .pollout = 1, |
26f0505a | 965 | .needs_async_setup = 1, |
e8c2bc1f | 966 | .async_size = sizeof(struct io_async_connect), |
d3656344 | 967 | }, |
0463b6c5 | 968 | [IORING_OP_FALLOCATE] = { |
d3656344 | 969 | .needs_file = 1, |
d3656344 | 970 | }, |
44526bed JA |
971 | [IORING_OP_OPENAT] = {}, |
972 | [IORING_OP_CLOSE] = {}, | |
973 | [IORING_OP_FILES_UPDATE] = {}, | |
974 | [IORING_OP_STATX] = {}, | |
0463b6c5 | 975 | [IORING_OP_READ] = { |
3a6820f2 JA |
976 | .needs_file = 1, |
977 | .unbound_nonreg_file = 1, | |
8a72758c | 978 | .pollin = 1, |
bcda7baa | 979 | .buffer_select = 1, |
27926b68 | 980 | .plug = 1, |
e8c2bc1f | 981 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 982 | }, |
0463b6c5 | 983 | [IORING_OP_WRITE] = { |
3a6820f2 JA |
984 | .needs_file = 1, |
985 | .unbound_nonreg_file = 1, | |
8a72758c | 986 | .pollout = 1, |
27926b68 | 987 | .plug = 1, |
e8c2bc1f | 988 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 989 | }, |
0463b6c5 | 990 | [IORING_OP_FADVISE] = { |
4840e418 | 991 | .needs_file = 1, |
c1ca757b | 992 | }, |
44526bed | 993 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 994 | [IORING_OP_SEND] = { |
fddaface JA |
995 | .needs_file = 1, |
996 | .unbound_nonreg_file = 1, | |
8a72758c | 997 | .pollout = 1, |
fddaface | 998 | }, |
0463b6c5 | 999 | [IORING_OP_RECV] = { |
fddaface JA |
1000 | .needs_file = 1, |
1001 | .unbound_nonreg_file = 1, | |
8a72758c | 1002 | .pollin = 1, |
bcda7baa | 1003 | .buffer_select = 1, |
fddaface | 1004 | }, |
0463b6c5 | 1005 | [IORING_OP_OPENAT2] = { |
cebdb986 | 1006 | }, |
3e4827b0 JA |
1007 | [IORING_OP_EPOLL_CTL] = { |
1008 | .unbound_nonreg_file = 1, | |
3e4827b0 | 1009 | }, |
7d67af2c PB |
1010 | [IORING_OP_SPLICE] = { |
1011 | .needs_file = 1, | |
1012 | .hash_reg_file = 1, | |
1013 | .unbound_nonreg_file = 1, | |
ddf0322d JA |
1014 | }, |
1015 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 1016 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
1017 | [IORING_OP_TEE] = { |
1018 | .needs_file = 1, | |
1019 | .hash_reg_file = 1, | |
1020 | .unbound_nonreg_file = 1, | |
1021 | }, | |
36f4fa68 JA |
1022 | [IORING_OP_SHUTDOWN] = { |
1023 | .needs_file = 1, | |
1024 | }, | |
44526bed JA |
1025 | [IORING_OP_RENAMEAT] = {}, |
1026 | [IORING_OP_UNLINKAT] = {}, | |
d3656344 JA |
1027 | }; |
1028 | ||
7a612350 | 1029 | static bool io_disarm_next(struct io_kiocb *req); |
d56d938b | 1030 | static void io_uring_del_task_file(unsigned long index); |
9936c7c2 PB |
1031 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1032 | struct task_struct *task, | |
1033 | struct files_struct *files); | |
37d1e2e3 | 1034 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx); |
b895c9a6 | 1035 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx); |
1ffc5422 | 1036 | |
ff642164 | 1037 | static bool io_cqring_fill_event(struct io_kiocb *req, long res, unsigned cflags); |
ec9c02ad | 1038 | static void io_put_req(struct io_kiocb *req); |
216578e5 | 1039 | static void io_put_req_deferred(struct io_kiocb *req, int nr); |
c7dae4ba JA |
1040 | static void io_dismantle_req(struct io_kiocb *req); |
1041 | static void io_put_task(struct task_struct *task, int nr); | |
94ae5e77 JA |
1042 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
1043 | static void io_queue_linked_timeout(struct io_kiocb *req); | |
05f3fb3c | 1044 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
269bbe5f | 1045 | struct io_uring_rsrc_update *ip, |
05f3fb3c | 1046 | unsigned nr_args); |
68fb8979 | 1047 | static void io_clean_op(struct io_kiocb *req); |
8371adf5 PB |
1048 | static struct file *io_file_get(struct io_submit_state *state, |
1049 | struct io_kiocb *req, int fd, bool fixed); | |
c5eef2b9 | 1050 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1051 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1052 | |
907d1df3 | 1053 | static void io_req_task_queue(struct io_kiocb *req); |
65453d1e JA |
1054 | static void io_submit_flush_completions(struct io_comp_state *cs, |
1055 | struct io_ring_ctx *ctx); | |
5082620f | 1056 | static bool io_poll_remove_waitqs(struct io_kiocb *req); |
179ae0d1 | 1057 | static int io_req_prep_async(struct io_kiocb *req); |
de0617e4 | 1058 | |
2b188cc1 JA |
1059 | static struct kmem_cache *req_cachep; |
1060 | ||
0918682b | 1061 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1062 | |
1063 | struct sock *io_uring_get_socket(struct file *file) | |
1064 | { | |
1065 | #if defined(CONFIG_UNIX) | |
1066 | if (file->f_op == &io_uring_fops) { | |
1067 | struct io_ring_ctx *ctx = file->private_data; | |
1068 | ||
1069 | return ctx->ring_sock->sk; | |
1070 | } | |
1071 | #endif | |
1072 | return NULL; | |
1073 | } | |
1074 | EXPORT_SYMBOL(io_uring_get_socket); | |
1075 | ||
f2f87370 PB |
1076 | #define io_for_each_link(pos, head) \ |
1077 | for (pos = (head); pos; pos = pos->link) | |
1078 | ||
b895c9a6 | 1079 | static inline void io_req_set_rsrc_node(struct io_kiocb *req) |
36f72fe2 PB |
1080 | { |
1081 | struct io_ring_ctx *ctx = req->ctx; | |
1082 | ||
269bbe5f | 1083 | if (!req->fixed_rsrc_refs) { |
a7f0ed5a | 1084 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
269bbe5f | 1085 | percpu_ref_get(req->fixed_rsrc_refs); |
36f72fe2 PB |
1086 | } |
1087 | } | |
1088 | ||
f70865db PB |
1089 | static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
1090 | { | |
1091 | bool got = percpu_ref_tryget(ref); | |
1092 | ||
1093 | /* already at zero, wait for ->release() */ | |
1094 | if (!got) | |
1095 | wait_for_completion(compl); | |
1096 | percpu_ref_resurrect(ref); | |
1097 | if (got) | |
1098 | percpu_ref_put(ref); | |
1099 | } | |
1100 | ||
08d23634 PB |
1101 | static bool io_match_task(struct io_kiocb *head, |
1102 | struct task_struct *task, | |
1103 | struct files_struct *files) | |
1104 | { | |
1105 | struct io_kiocb *req; | |
1106 | ||
68207680 | 1107 | if (task && head->task != task) |
08d23634 PB |
1108 | return false; |
1109 | if (!files) | |
1110 | return true; | |
1111 | ||
1112 | io_for_each_link(req, head) { | |
b05a1bcd | 1113 | if (req->flags & REQ_F_INFLIGHT) |
02a13674 | 1114 | return true; |
08d23634 PB |
1115 | } |
1116 | return false; | |
1117 | } | |
1118 | ||
c40f6379 JA |
1119 | static inline void req_set_fail_links(struct io_kiocb *req) |
1120 | { | |
e4335ed3 | 1121 | if (req->flags & REQ_F_LINK) |
c40f6379 JA |
1122 | req->flags |= REQ_F_FAIL_LINK; |
1123 | } | |
4a38aed2 | 1124 | |
2b188cc1 JA |
1125 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
1126 | { | |
1127 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1128 | ||
0f158b4c | 1129 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1130 | } |
1131 | ||
8eb7e2d0 PB |
1132 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1133 | { | |
1134 | return !req->timeout.off; | |
1135 | } | |
1136 | ||
2b188cc1 JA |
1137 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
1138 | { | |
1139 | struct io_ring_ctx *ctx; | |
78076bb6 | 1140 | int hash_bits; |
2b188cc1 JA |
1141 | |
1142 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1143 | if (!ctx) | |
1144 | return NULL; | |
1145 | ||
78076bb6 JA |
1146 | /* |
1147 | * Use 5 bits less than the max cq entries, that should give us around | |
1148 | * 32 entries per hash list if totally full and uniformly spread. | |
1149 | */ | |
1150 | hash_bits = ilog2(p->cq_entries); | |
1151 | hash_bits -= 5; | |
1152 | if (hash_bits <= 0) | |
1153 | hash_bits = 1; | |
1154 | ctx->cancel_hash_bits = hash_bits; | |
1155 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1156 | GFP_KERNEL); | |
1157 | if (!ctx->cancel_hash) | |
1158 | goto err; | |
1159 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1160 | ||
21482896 | 1161 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1162 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1163 | goto err; | |
2b188cc1 JA |
1164 | |
1165 | ctx->flags = p->flags; | |
90554200 | 1166 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1167 | INIT_LIST_HEAD(&ctx->sqd_list); |
2b188cc1 | 1168 | init_waitqueue_head(&ctx->cq_wait); |
1d7bb1d5 | 1169 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c | 1170 | init_completion(&ctx->ref_comp); |
9e15c3a0 | 1171 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
61cf9370 | 1172 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
2b188cc1 JA |
1173 | mutex_init(&ctx->uring_lock); |
1174 | init_waitqueue_head(&ctx->wait); | |
1175 | spin_lock_init(&ctx->completion_lock); | |
540e32a0 | 1176 | INIT_LIST_HEAD(&ctx->iopoll_list); |
de0617e4 | 1177 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1178 | INIT_LIST_HEAD(&ctx->timeout_list); |
d67d2263 BM |
1179 | spin_lock_init(&ctx->rsrc_ref_lock); |
1180 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1181 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1182 | init_llist_head(&ctx->rsrc_put_llist); | |
13bf43f5 | 1183 | INIT_LIST_HEAD(&ctx->tctx_list); |
1b4c351f | 1184 | INIT_LIST_HEAD(&ctx->submit_state.comp.free_list); |
c7dae4ba | 1185 | INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list); |
2b188cc1 | 1186 | return ctx; |
206aefde | 1187 | err: |
78076bb6 | 1188 | kfree(ctx->cancel_hash); |
206aefde JA |
1189 | kfree(ctx); |
1190 | return NULL; | |
2b188cc1 JA |
1191 | } |
1192 | ||
9cf7c104 | 1193 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1194 | { |
2bc9930e JA |
1195 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1196 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1197 | |
9cf7c104 | 1198 | return seq != ctx->cached_cq_tail |
2c3bac6d | 1199 | + READ_ONCE(ctx->cached_cq_overflow); |
2bc9930e | 1200 | } |
de0617e4 | 1201 | |
9d858b21 | 1202 | return false; |
de0617e4 JA |
1203 | } |
1204 | ||
ce3d5aae PB |
1205 | static void io_req_track_inflight(struct io_kiocb *req) |
1206 | { | |
ce3d5aae | 1207 | if (!(req->flags & REQ_F_INFLIGHT)) { |
ce3d5aae | 1208 | req->flags |= REQ_F_INFLIGHT; |
b303fe2e | 1209 | atomic_inc(¤t->io_uring->inflight_tracked); |
ce3d5aae PB |
1210 | } |
1211 | } | |
1212 | ||
1e6fa521 JA |
1213 | static void io_prep_async_work(struct io_kiocb *req) |
1214 | { | |
1215 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1216 | struct io_ring_ctx *ctx = req->ctx; |
1217 | ||
003e8dcc JA |
1218 | if (!req->work.creds) |
1219 | req->work.creds = get_current_cred(); | |
1220 | ||
e1d675df PB |
1221 | req->work.list.next = NULL; |
1222 | req->work.flags = 0; | |
feaadc4f PB |
1223 | if (req->flags & REQ_F_FORCE_ASYNC) |
1224 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1225 | ||
1e6fa521 JA |
1226 | if (req->flags & REQ_F_ISREG) { |
1227 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1228 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
4b982bd0 | 1229 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
1e6fa521 JA |
1230 | if (def->unbound_nonreg_file) |
1231 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1232 | } | |
e1d675df PB |
1233 | |
1234 | switch (req->opcode) { | |
1235 | case IORING_OP_SPLICE: | |
1236 | case IORING_OP_TEE: | |
e1d675df PB |
1237 | if (!S_ISREG(file_inode(req->splice.file_in)->i_mode)) |
1238 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1239 | break; | |
1240 | } | |
561fb04a | 1241 | } |
cccf0ee8 | 1242 | |
cbdcb435 | 1243 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1244 | { |
cbdcb435 | 1245 | struct io_kiocb *cur; |
54a91f3b | 1246 | |
f2f87370 PB |
1247 | io_for_each_link(cur, req) |
1248 | io_prep_async_work(cur); | |
561fb04a JA |
1249 | } |
1250 | ||
ebf93667 | 1251 | static void io_queue_async_work(struct io_kiocb *req) |
561fb04a | 1252 | { |
a197f664 | 1253 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1254 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1255 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1256 | |
3bfe6106 JA |
1257 | BUG_ON(!tctx); |
1258 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1259 | |
cbdcb435 PB |
1260 | /* init ->work of the whole link before punting */ |
1261 | io_prep_async_link(req); | |
d07f1e8a PB |
1262 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1263 | &req->work, req->flags); | |
ebf93667 | 1264 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a JA |
1265 | if (link) |
1266 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1267 | } |
1268 | ||
1ee4160c | 1269 | static void io_kill_timeout(struct io_kiocb *req, int status) |
8c855885 | 1270 | __must_hold(&req->ctx->completion_lock) |
5262f567 | 1271 | { |
e8c2bc1f | 1272 | struct io_timeout_data *io = req->async_data; |
5262f567 JA |
1273 | int ret; |
1274 | ||
e8c2bc1f | 1275 | ret = hrtimer_try_to_cancel(&io->timer); |
5262f567 | 1276 | if (ret != -1) { |
01cec8c1 PB |
1277 | atomic_set(&req->ctx->cq_timeouts, |
1278 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1279 | list_del_init(&req->timeout.list); |
ff642164 | 1280 | io_cqring_fill_event(req, status, 0); |
216578e5 | 1281 | io_put_req_deferred(req, 1); |
5262f567 JA |
1282 | } |
1283 | } | |
1284 | ||
04518945 | 1285 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1286 | { |
04518945 | 1287 | do { |
27dc8338 PB |
1288 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1289 | struct io_defer_entry, list); | |
de0617e4 | 1290 | |
9cf7c104 | 1291 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1292 | break; |
27dc8338 | 1293 | list_del_init(&de->list); |
907d1df3 | 1294 | io_req_task_queue(de->req); |
27dc8338 | 1295 | kfree(de); |
04518945 PB |
1296 | } while (!list_empty(&ctx->defer_list)); |
1297 | } | |
1298 | ||
360428f8 | 1299 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
de0617e4 | 1300 | { |
f010505b MDG |
1301 | u32 seq; |
1302 | ||
1303 | if (list_empty(&ctx->timeout_list)) | |
1304 | return; | |
1305 | ||
1306 | seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); | |
1307 | ||
1308 | do { | |
1309 | u32 events_needed, events_got; | |
360428f8 | 1310 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1311 | struct io_kiocb, timeout.list); |
de0617e4 | 1312 | |
8eb7e2d0 | 1313 | if (io_is_timeout_noseq(req)) |
360428f8 | 1314 | break; |
f010505b MDG |
1315 | |
1316 | /* | |
1317 | * Since seq can easily wrap around over time, subtract | |
1318 | * the last seq at which timeouts were flushed before comparing. | |
1319 | * Assuming not more than 2^31-1 events have happened since, | |
1320 | * these subtractions won't have wrapped, so we can check if | |
1321 | * target is in [last_seq, current_seq] by comparing the two. | |
1322 | */ | |
1323 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1324 | events_got = seq - ctx->cq_last_tm_flush; | |
1325 | if (events_got < events_needed) | |
360428f8 | 1326 | break; |
bfe68a22 | 1327 | |
135fcde8 | 1328 | list_del_init(&req->timeout.list); |
1ee4160c | 1329 | io_kill_timeout(req, 0); |
f010505b MDG |
1330 | } while (!list_empty(&ctx->timeout_list)); |
1331 | ||
1332 | ctx->cq_last_tm_flush = seq; | |
360428f8 | 1333 | } |
5262f567 | 1334 | |
360428f8 PB |
1335 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
1336 | { | |
1337 | io_flush_timeouts(ctx); | |
ec30e04b PB |
1338 | |
1339 | /* order cqe stores with ring update */ | |
1340 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 | 1341 | |
04518945 PB |
1342 | if (unlikely(!list_empty(&ctx->defer_list))) |
1343 | __io_queue_deferred(ctx); | |
de0617e4 JA |
1344 | } |
1345 | ||
90554200 JA |
1346 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1347 | { | |
1348 | struct io_rings *r = ctx->rings; | |
1349 | ||
1350 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; | |
1351 | } | |
1352 | ||
888aae2e PB |
1353 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1354 | { | |
1355 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1356 | } | |
1357 | ||
8d13326e | 1358 | static inline struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
2b188cc1 | 1359 | { |
75b28aff | 1360 | struct io_rings *rings = ctx->rings; |
2b188cc1 JA |
1361 | unsigned tail; |
1362 | ||
115e12e5 SB |
1363 | /* |
1364 | * writes to the cq entry need to come after reading head; the | |
1365 | * control dependency is enough as we're using WRITE_ONCE to | |
1366 | * fill the cq entry | |
1367 | */ | |
888aae2e | 1368 | if (__io_cqring_events(ctx) == rings->cq_ring_entries) |
2b188cc1 JA |
1369 | return NULL; |
1370 | ||
888aae2e | 1371 | tail = ctx->cached_cq_tail++; |
75b28aff | 1372 | return &rings->cqes[tail & ctx->cq_mask]; |
2b188cc1 JA |
1373 | } |
1374 | ||
f2842ab5 JA |
1375 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1376 | { | |
44c769de | 1377 | if (likely(!ctx->cq_ev_fd)) |
f0b493e6 | 1378 | return false; |
7e55a19c SG |
1379 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1380 | return false; | |
44c769de | 1381 | return !ctx->eventfd_async || io_wq_current_is_worker(); |
f2842ab5 JA |
1382 | } |
1383 | ||
b41e9852 | 1384 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1385 | { |
b1445e59 PB |
1386 | /* see waitqueue_active() comment */ |
1387 | smp_mb(); | |
1388 | ||
1d7bb1d5 JA |
1389 | if (waitqueue_active(&ctx->wait)) |
1390 | wake_up(&ctx->wait); | |
534ca6d6 JA |
1391 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
1392 | wake_up(&ctx->sq_data->wait); | |
b41e9852 | 1393 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 | 1394 | eventfd_signal(ctx->cq_ev_fd, 1); |
b1445e59 | 1395 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1396 | wake_up_interruptible(&ctx->cq_wait); |
1397 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1398 | } | |
1d7bb1d5 JA |
1399 | } |
1400 | ||
80c18e4a PB |
1401 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1402 | { | |
b1445e59 PB |
1403 | /* see waitqueue_active() comment */ |
1404 | smp_mb(); | |
1405 | ||
80c18e4a PB |
1406 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
1407 | if (waitqueue_active(&ctx->wait)) | |
1408 | wake_up(&ctx->wait); | |
1409 | } | |
1410 | if (io_should_trigger_evfd(ctx)) | |
1411 | eventfd_signal(ctx->cq_ev_fd, 1); | |
b1445e59 | 1412 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1413 | wake_up_interruptible(&ctx->cq_wait); |
1414 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1415 | } | |
80c18e4a PB |
1416 | } |
1417 | ||
c4a2ed72 | 1418 | /* Returns true if there are no backlogged entries after the flush */ |
6c2450ae | 1419 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
1d7bb1d5 JA |
1420 | { |
1421 | struct io_rings *rings = ctx->rings; | |
1d7bb1d5 | 1422 | unsigned long flags; |
b18032bb | 1423 | bool all_flushed, posted; |
1d7bb1d5 | 1424 | |
e23de15f PB |
1425 | if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries) |
1426 | return false; | |
1d7bb1d5 | 1427 | |
b18032bb | 1428 | posted = false; |
1d7bb1d5 | 1429 | spin_lock_irqsave(&ctx->completion_lock, flags); |
6c2450ae PB |
1430 | while (!list_empty(&ctx->cq_overflow_list)) { |
1431 | struct io_uring_cqe *cqe = io_get_cqring(ctx); | |
1432 | struct io_overflow_cqe *ocqe; | |
e6c8aa9a | 1433 | |
1d7bb1d5 JA |
1434 | if (!cqe && !force) |
1435 | break; | |
6c2450ae PB |
1436 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
1437 | struct io_overflow_cqe, list); | |
1438 | if (cqe) | |
1439 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); | |
1440 | else | |
1d7bb1d5 | 1441 | WRITE_ONCE(ctx->rings->cq_overflow, |
6c2450ae | 1442 | ++ctx->cached_cq_overflow); |
b18032bb | 1443 | posted = true; |
6c2450ae PB |
1444 | list_del(&ocqe->list); |
1445 | kfree(ocqe); | |
1d7bb1d5 JA |
1446 | } |
1447 | ||
09e88404 PB |
1448 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1449 | if (all_flushed) { | |
1450 | clear_bit(0, &ctx->sq_check_overflow); | |
1451 | clear_bit(0, &ctx->cq_check_overflow); | |
1452 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; | |
1453 | } | |
46930143 | 1454 | |
b18032bb JA |
1455 | if (posted) |
1456 | io_commit_cqring(ctx); | |
1d7bb1d5 | 1457 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
b18032bb JA |
1458 | if (posted) |
1459 | io_cqring_ev_posted(ctx); | |
09e88404 | 1460 | return all_flushed; |
1d7bb1d5 JA |
1461 | } |
1462 | ||
6c2450ae | 1463 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
6c503150 | 1464 | { |
ca0a2651 JA |
1465 | bool ret = true; |
1466 | ||
6c503150 PB |
1467 | if (test_bit(0, &ctx->cq_check_overflow)) { |
1468 | /* iopoll syncs against uring_lock, not completion_lock */ | |
1469 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1470 | mutex_lock(&ctx->uring_lock); | |
6c2450ae | 1471 | ret = __io_cqring_overflow_flush(ctx, force); |
6c503150 PB |
1472 | if (ctx->flags & IORING_SETUP_IOPOLL) |
1473 | mutex_unlock(&ctx->uring_lock); | |
1474 | } | |
ca0a2651 JA |
1475 | |
1476 | return ret; | |
6c503150 PB |
1477 | } |
1478 | ||
abc54d63 JA |
1479 | /* |
1480 | * Shamelessly stolen from the mm implementation of page reference checking, | |
1481 | * see commit f958d7b528b1 for details. | |
1482 | */ | |
1483 | #define req_ref_zero_or_close_to_overflow(req) \ | |
1484 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) | |
1485 | ||
de9b4cca JA |
1486 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) |
1487 | { | |
abc54d63 | 1488 | return atomic_inc_not_zero(&req->refs); |
de9b4cca JA |
1489 | } |
1490 | ||
1491 | static inline bool req_ref_sub_and_test(struct io_kiocb *req, int refs) | |
1492 | { | |
abc54d63 JA |
1493 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1494 | return atomic_sub_and_test(refs, &req->refs); | |
de9b4cca JA |
1495 | } |
1496 | ||
1497 | static inline bool req_ref_put_and_test(struct io_kiocb *req) | |
1498 | { | |
abc54d63 JA |
1499 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1500 | return atomic_dec_and_test(&req->refs); | |
de9b4cca JA |
1501 | } |
1502 | ||
1503 | static inline void req_ref_put(struct io_kiocb *req) | |
1504 | { | |
abc54d63 | 1505 | WARN_ON_ONCE(req_ref_put_and_test(req)); |
de9b4cca JA |
1506 | } |
1507 | ||
1508 | static inline void req_ref_get(struct io_kiocb *req) | |
1509 | { | |
abc54d63 JA |
1510 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1511 | atomic_inc(&req->refs); | |
de9b4cca JA |
1512 | } |
1513 | ||
8d13326e PB |
1514 | static bool io_cqring_event_overflow(struct io_kiocb *req, long res, |
1515 | unsigned int cflags) | |
2b188cc1 | 1516 | { |
78e19bbe | 1517 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 | 1518 | |
c4ea060e | 1519 | if (!atomic_read(&req->task->io_uring->in_idle)) { |
6c2450ae PB |
1520 | struct io_overflow_cqe *ocqe; |
1521 | ||
1522 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); | |
1523 | if (!ocqe) | |
1524 | goto overflow; | |
ad3eb2c8 JA |
1525 | if (list_empty(&ctx->cq_overflow_list)) { |
1526 | set_bit(0, &ctx->sq_check_overflow); | |
1527 | set_bit(0, &ctx->cq_check_overflow); | |
6d5f9049 | 1528 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
ad3eb2c8 | 1529 | } |
6c2450ae PB |
1530 | ocqe->cqe.user_data = req->user_data; |
1531 | ocqe->cqe.res = res; | |
1532 | ocqe->cqe.flags = cflags; | |
1533 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); | |
5082620f | 1534 | return true; |
2b188cc1 | 1535 | } |
6c2450ae PB |
1536 | overflow: |
1537 | /* | |
1538 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1539 | * or cannot allocate an overflow entry, then we need to drop it | |
1540 | * on the floor. | |
1541 | */ | |
1542 | WRITE_ONCE(ctx->rings->cq_overflow, ++ctx->cached_cq_overflow); | |
5082620f | 1543 | return false; |
2b188cc1 JA |
1544 | } |
1545 | ||
8d13326e PB |
1546 | static inline bool __io_cqring_fill_event(struct io_kiocb *req, long res, |
1547 | unsigned int cflags) | |
1548 | { | |
1549 | struct io_ring_ctx *ctx = req->ctx; | |
1550 | struct io_uring_cqe *cqe; | |
1551 | ||
1552 | trace_io_uring_complete(ctx, req->user_data, res, cflags); | |
1553 | ||
1554 | /* | |
1555 | * If we can't get a cq entry, userspace overflowed the | |
1556 | * submission (by quite a lot). Increment the overflow count in | |
1557 | * the ring. | |
1558 | */ | |
1559 | cqe = io_get_cqring(ctx); | |
1560 | if (likely(cqe)) { | |
1561 | WRITE_ONCE(cqe->user_data, req->user_data); | |
1562 | WRITE_ONCE(cqe->res, res); | |
1563 | WRITE_ONCE(cqe->flags, cflags); | |
1564 | return true; | |
1565 | } | |
1566 | return io_cqring_event_overflow(req, res, cflags); | |
1567 | } | |
1568 | ||
1569 | /* not as hot to bloat with inlining */ | |
1570 | static noinline bool io_cqring_fill_event(struct io_kiocb *req, long res, | |
1571 | unsigned int cflags) | |
1572 | { | |
1573 | return __io_cqring_fill_event(req, res, cflags); | |
1574 | } | |
1575 | ||
7a612350 PB |
1576 | static void io_req_complete_post(struct io_kiocb *req, long res, |
1577 | unsigned int cflags) | |
2b188cc1 | 1578 | { |
78e19bbe | 1579 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1580 | unsigned long flags; |
1581 | ||
1582 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
8d13326e | 1583 | __io_cqring_fill_event(req, res, cflags); |
c7dae4ba JA |
1584 | /* |
1585 | * If we're the last reference to this request, add to our locked | |
1586 | * free_list cache. | |
1587 | */ | |
de9b4cca | 1588 | if (req_ref_put_and_test(req)) { |
c7dae4ba JA |
1589 | struct io_comp_state *cs = &ctx->submit_state.comp; |
1590 | ||
7a612350 PB |
1591 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
1592 | if (req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_FAIL_LINK)) | |
1593 | io_disarm_next(req); | |
1594 | if (req->link) { | |
1595 | io_req_task_queue(req->link); | |
1596 | req->link = NULL; | |
1597 | } | |
1598 | } | |
c7dae4ba JA |
1599 | io_dismantle_req(req); |
1600 | io_put_task(req->task, 1); | |
1601 | list_add(&req->compl.list, &cs->locked_free_list); | |
1602 | cs->locked_free_nr++; | |
180f829f PB |
1603 | } else { |
1604 | if (!percpu_ref_tryget(&ctx->refs)) | |
1605 | req = NULL; | |
1606 | } | |
7a612350 | 1607 | io_commit_cqring(ctx); |
2b188cc1 | 1608 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
7a612350 | 1609 | |
180f829f PB |
1610 | if (req) { |
1611 | io_cqring_ev_posted(ctx); | |
c7dae4ba | 1612 | percpu_ref_put(&ctx->refs); |
180f829f | 1613 | } |
229a7b63 JA |
1614 | } |
1615 | ||
a38d68db | 1616 | static void io_req_complete_state(struct io_kiocb *req, long res, |
889fca73 | 1617 | unsigned int cflags) |
229a7b63 | 1618 | { |
68fb8979 PB |
1619 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED)) |
1620 | io_clean_op(req); | |
a38d68db PB |
1621 | req->result = res; |
1622 | req->compl.cflags = cflags; | |
e342c807 | 1623 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
1624 | } |
1625 | ||
889fca73 PB |
1626 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
1627 | long res, unsigned cflags) | |
bcda7baa | 1628 | { |
889fca73 PB |
1629 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
1630 | io_req_complete_state(req, res, cflags); | |
a38d68db | 1631 | else |
c7dae4ba | 1632 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
1633 | } |
1634 | ||
a38d68db | 1635 | static inline void io_req_complete(struct io_kiocb *req, long res) |
0ddf92e8 | 1636 | { |
889fca73 | 1637 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
1638 | } |
1639 | ||
f41db273 PB |
1640 | static void io_req_complete_failed(struct io_kiocb *req, long res) |
1641 | { | |
1642 | req_set_fail_links(req); | |
1643 | io_put_req(req); | |
1644 | io_req_complete_post(req, res, 0); | |
1645 | } | |
1646 | ||
dac7a098 PB |
1647 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
1648 | struct io_comp_state *cs) | |
1649 | { | |
1650 | spin_lock_irq(&ctx->completion_lock); | |
1651 | list_splice_init(&cs->locked_free_list, &cs->free_list); | |
1652 | cs->locked_free_nr = 0; | |
1653 | spin_unlock_irq(&ctx->completion_lock); | |
1654 | } | |
1655 | ||
dd78f492 | 1656 | /* Returns true IFF there are requests in the cache */ |
c7dae4ba | 1657 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 1658 | { |
c7dae4ba JA |
1659 | struct io_submit_state *state = &ctx->submit_state; |
1660 | struct io_comp_state *cs = &state->comp; | |
dd78f492 | 1661 | int nr; |
0ddf92e8 | 1662 | |
c7dae4ba JA |
1663 | /* |
1664 | * If we have more than a batch's worth of requests in our IRQ side | |
1665 | * locked cache, grab the lock and move them over to our submission | |
1666 | * side cache. | |
1667 | */ | |
dac7a098 PB |
1668 | if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) |
1669 | io_flush_cached_locked_reqs(ctx, cs); | |
0ddf92e8 | 1670 | |
dd78f492 | 1671 | nr = state->free_reqs; |
c7dae4ba | 1672 | while (!list_empty(&cs->free_list)) { |
dd78f492 PB |
1673 | struct io_kiocb *req = list_first_entry(&cs->free_list, |
1674 | struct io_kiocb, compl.list); | |
1675 | ||
1b4c351f | 1676 | list_del(&req->compl.list); |
dd78f492 PB |
1677 | state->reqs[nr++] = req; |
1678 | if (nr == ARRAY_SIZE(state->reqs)) | |
e5d1bc0a | 1679 | break; |
1b4c351f JA |
1680 | } |
1681 | ||
dd78f492 PB |
1682 | state->free_reqs = nr; |
1683 | return nr != 0; | |
0ddf92e8 JA |
1684 | } |
1685 | ||
e5d1bc0a | 1686 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
2b188cc1 | 1687 | { |
e5d1bc0a PB |
1688 | struct io_submit_state *state = &ctx->submit_state; |
1689 | ||
1690 | BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs)); | |
1691 | ||
f6b6c7d6 | 1692 | if (!state->free_reqs) { |
291b2821 | 1693 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
2579f913 JA |
1694 | int ret; |
1695 | ||
c7dae4ba | 1696 | if (io_flush_cached_reqs(ctx)) |
e5d1bc0a PB |
1697 | goto got_req; |
1698 | ||
bf019da7 PB |
1699 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
1700 | state->reqs); | |
fd6fab2c JA |
1701 | |
1702 | /* | |
1703 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1704 | * retry single alloc to be on the safe side. | |
1705 | */ | |
1706 | if (unlikely(ret <= 0)) { | |
1707 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
1708 | if (!state->reqs[0]) | |
3893f39f | 1709 | return NULL; |
fd6fab2c JA |
1710 | ret = 1; |
1711 | } | |
291b2821 | 1712 | state->free_reqs = ret; |
2b188cc1 | 1713 | } |
e5d1bc0a | 1714 | got_req: |
291b2821 PB |
1715 | state->free_reqs--; |
1716 | return state->reqs[state->free_reqs]; | |
2b188cc1 JA |
1717 | } |
1718 | ||
e1d767f0 | 1719 | static inline void io_put_file(struct file *file) |
8da11c19 | 1720 | { |
e1d767f0 | 1721 | if (file) |
8da11c19 PB |
1722 | fput(file); |
1723 | } | |
1724 | ||
4edf20f9 | 1725 | static void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 1726 | { |
094bae49 PB |
1727 | unsigned int flags = req->flags; |
1728 | ||
e1d767f0 PB |
1729 | if (!(flags & REQ_F_FIXED_FILE)) |
1730 | io_put_file(req->file); | |
094bae49 PB |
1731 | if (flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED | |
1732 | REQ_F_INFLIGHT)) { | |
1733 | io_clean_op(req); | |
1734 | ||
1735 | if (req->flags & REQ_F_INFLIGHT) { | |
b303fe2e | 1736 | struct io_uring_task *tctx = req->task->io_uring; |
094bae49 | 1737 | |
b303fe2e | 1738 | atomic_dec(&tctx->inflight_tracked); |
094bae49 PB |
1739 | req->flags &= ~REQ_F_INFLIGHT; |
1740 | } | |
1741 | } | |
269bbe5f BM |
1742 | if (req->fixed_rsrc_refs) |
1743 | percpu_ref_put(req->fixed_rsrc_refs); | |
094bae49 PB |
1744 | if (req->async_data) |
1745 | kfree(req->async_data); | |
003e8dcc JA |
1746 | if (req->work.creds) { |
1747 | put_cred(req->work.creds); | |
1748 | req->work.creds = NULL; | |
1749 | } | |
e65ef56d JA |
1750 | } |
1751 | ||
b23fcf47 | 1752 | /* must to be called somewhat shortly after putting a request */ |
7c660731 PB |
1753 | static inline void io_put_task(struct task_struct *task, int nr) |
1754 | { | |
1755 | struct io_uring_task *tctx = task->io_uring; | |
1756 | ||
1757 | percpu_counter_sub(&tctx->inflight, nr); | |
1758 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1759 | wake_up(&tctx->wait); | |
1760 | put_task_struct_many(task, nr); | |
1761 | } | |
1762 | ||
216578e5 | 1763 | static void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 1764 | { |
51a4cc11 | 1765 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 1766 | |
216578e5 | 1767 | io_dismantle_req(req); |
7c660731 | 1768 | io_put_task(req->task, 1); |
c6ca97b3 | 1769 | |
3893f39f | 1770 | kmem_cache_free(req_cachep, req); |
ecfc5177 | 1771 | percpu_ref_put(&ctx->refs); |
e65ef56d JA |
1772 | } |
1773 | ||
f2f87370 PB |
1774 | static inline void io_remove_next_linked(struct io_kiocb *req) |
1775 | { | |
1776 | struct io_kiocb *nxt = req->link; | |
1777 | ||
1778 | req->link = nxt->link; | |
1779 | nxt->link = NULL; | |
1780 | } | |
1781 | ||
33cc89a9 PB |
1782 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
1783 | __must_hold(&req->ctx->completion_lock) | |
2665abfd | 1784 | { |
33cc89a9 | 1785 | struct io_kiocb *link = req->link; |
f2f87370 | 1786 | |
900fad45 PB |
1787 | /* |
1788 | * Can happen if a linked timeout fired and link had been like | |
1789 | * req -> link t-out -> link t-out [-> ...] | |
1790 | */ | |
c9abd7ad PB |
1791 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
1792 | struct io_timeout_data *io = link->async_data; | |
1793 | int ret; | |
7c86ffee | 1794 | |
f2f87370 | 1795 | io_remove_next_linked(req); |
90cd7e42 | 1796 | link->timeout.head = NULL; |
c9abd7ad PB |
1797 | ret = hrtimer_try_to_cancel(&io->timer); |
1798 | if (ret != -1) { | |
ff642164 | 1799 | io_cqring_fill_event(link, -ECANCELED, 0); |
33cc89a9 | 1800 | io_put_req_deferred(link, 1); |
d4729fbd | 1801 | return true; |
c9abd7ad PB |
1802 | } |
1803 | } | |
d4729fbd | 1804 | return false; |
7c86ffee PB |
1805 | } |
1806 | ||
d148ca4b | 1807 | static void io_fail_links(struct io_kiocb *req) |
33cc89a9 | 1808 | __must_hold(&req->ctx->completion_lock) |
9e645e11 | 1809 | { |
33cc89a9 | 1810 | struct io_kiocb *nxt, *link = req->link; |
9e645e11 | 1811 | |
f2f87370 | 1812 | req->link = NULL; |
f2f87370 PB |
1813 | while (link) { |
1814 | nxt = link->link; | |
1815 | link->link = NULL; | |
2665abfd | 1816 | |
f2f87370 | 1817 | trace_io_uring_fail_link(req, link); |
ff642164 | 1818 | io_cqring_fill_event(link, -ECANCELED, 0); |
1575f21a | 1819 | io_put_req_deferred(link, 2); |
f2f87370 | 1820 | link = nxt; |
9e645e11 | 1821 | } |
33cc89a9 | 1822 | } |
9e645e11 | 1823 | |
33cc89a9 PB |
1824 | static bool io_disarm_next(struct io_kiocb *req) |
1825 | __must_hold(&req->ctx->completion_lock) | |
1826 | { | |
1827 | bool posted = false; | |
1828 | ||
1829 | if (likely(req->flags & REQ_F_LINK_TIMEOUT)) | |
1830 | posted = io_kill_linked_timeout(req); | |
e4335ed3 PB |
1831 | if (unlikely((req->flags & REQ_F_FAIL_LINK) && |
1832 | !(req->flags & REQ_F_HARDLINK))) { | |
33cc89a9 PB |
1833 | posted |= (req->link != NULL); |
1834 | io_fail_links(req); | |
1835 | } | |
1836 | return posted; | |
9e645e11 JA |
1837 | } |
1838 | ||
3fa5e0f3 | 1839 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
c69f8dbe | 1840 | { |
33cc89a9 | 1841 | struct io_kiocb *nxt; |
944e58bf | 1842 | |
9e645e11 JA |
1843 | /* |
1844 | * If LINK is set, we have dependent requests in this chain. If we | |
1845 | * didn't fail this request, queue the first one up, moving any other | |
1846 | * dependencies to the next request. In case of failure, fail the rest | |
1847 | * of the chain. | |
1848 | */ | |
33cc89a9 PB |
1849 | if (req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_FAIL_LINK)) { |
1850 | struct io_ring_ctx *ctx = req->ctx; | |
1851 | unsigned long flags; | |
1852 | bool posted; | |
1853 | ||
1854 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
1855 | posted = io_disarm_next(req); | |
1856 | if (posted) | |
1857 | io_commit_cqring(req->ctx); | |
1858 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
1859 | if (posted) | |
1860 | io_cqring_ev_posted(ctx); | |
f2f87370 | 1861 | } |
33cc89a9 PB |
1862 | nxt = req->link; |
1863 | req->link = NULL; | |
1864 | return nxt; | |
4d7dd462 | 1865 | } |
9e645e11 | 1866 | |
f2f87370 | 1867 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
3fa5e0f3 | 1868 | { |
cdbff982 | 1869 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
3fa5e0f3 PB |
1870 | return NULL; |
1871 | return __io_req_find_next(req); | |
1872 | } | |
1873 | ||
2c32395d PB |
1874 | static void ctx_flush_and_put(struct io_ring_ctx *ctx) |
1875 | { | |
1876 | if (!ctx) | |
1877 | return; | |
1878 | if (ctx->submit_state.comp.nr) { | |
1879 | mutex_lock(&ctx->uring_lock); | |
1880 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); | |
1881 | mutex_unlock(&ctx->uring_lock); | |
1882 | } | |
1883 | percpu_ref_put(&ctx->refs); | |
1884 | } | |
1885 | ||
7cbf1722 | 1886 | static bool __tctx_task_work(struct io_uring_task *tctx) |
c2c4c83c | 1887 | { |
65453d1e | 1888 | struct io_ring_ctx *ctx = NULL; |
7cbf1722 JA |
1889 | struct io_wq_work_list list; |
1890 | struct io_wq_work_node *node; | |
c2c4c83c | 1891 | |
7cbf1722 JA |
1892 | if (wq_list_empty(&tctx->task_list)) |
1893 | return false; | |
6200b0ae | 1894 | |
0b81e80c | 1895 | spin_lock_irq(&tctx->task_lock); |
7cbf1722 JA |
1896 | list = tctx->task_list; |
1897 | INIT_WQ_LIST(&tctx->task_list); | |
0b81e80c | 1898 | spin_unlock_irq(&tctx->task_lock); |
c2c4c83c | 1899 | |
7cbf1722 JA |
1900 | node = list.first; |
1901 | while (node) { | |
1902 | struct io_wq_work_node *next = node->next; | |
1903 | struct io_kiocb *req; | |
0ba9c9ed | 1904 | |
7cbf1722 | 1905 | req = container_of(node, struct io_kiocb, io_task_work.node); |
2c32395d PB |
1906 | if (req->ctx != ctx) { |
1907 | ctx_flush_and_put(ctx); | |
1908 | ctx = req->ctx; | |
1909 | percpu_ref_get(&ctx->refs); | |
65453d1e | 1910 | } |
65453d1e | 1911 | |
2c32395d PB |
1912 | req->task_work.func(&req->task_work); |
1913 | node = next; | |
7cbf1722 JA |
1914 | } |
1915 | ||
2c32395d | 1916 | ctx_flush_and_put(ctx); |
7cbf1722 | 1917 | return list.first != NULL; |
c2c4c83c JA |
1918 | } |
1919 | ||
7cbf1722 | 1920 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 1921 | { |
7cbf1722 | 1922 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work); |
c40f6379 | 1923 | |
1d5f360d JA |
1924 | clear_bit(0, &tctx->task_state); |
1925 | ||
7cbf1722 JA |
1926 | while (__tctx_task_work(tctx)) |
1927 | cond_resched(); | |
7cbf1722 JA |
1928 | } |
1929 | ||
c15b79de | 1930 | static int io_req_task_work_add(struct io_kiocb *req) |
7cbf1722 | 1931 | { |
c15b79de | 1932 | struct task_struct *tsk = req->task; |
7cbf1722 | 1933 | struct io_uring_task *tctx = tsk->io_uring; |
c15b79de | 1934 | enum task_work_notify_mode notify; |
7cbf1722 | 1935 | struct io_wq_work_node *node, *prev; |
0b81e80c | 1936 | unsigned long flags; |
c15b79de PB |
1937 | int ret = 0; |
1938 | ||
1939 | if (unlikely(tsk->flags & PF_EXITING)) | |
1940 | return -ESRCH; | |
7cbf1722 JA |
1941 | |
1942 | WARN_ON_ONCE(!tctx); | |
1943 | ||
0b81e80c | 1944 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 | 1945 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
0b81e80c | 1946 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
1947 | |
1948 | /* task_work already pending, we're done */ | |
1949 | if (test_bit(0, &tctx->task_state) || | |
1950 | test_and_set_bit(0, &tctx->task_state)) | |
1951 | return 0; | |
1952 | ||
c15b79de PB |
1953 | /* |
1954 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For | |
1955 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
1956 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
1957 | * will do the job. | |
1958 | */ | |
1959 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; | |
1960 | ||
1961 | if (!task_work_add(tsk, &tctx->task_work, notify)) { | |
1962 | wake_up_process(tsk); | |
7cbf1722 | 1963 | return 0; |
c15b79de | 1964 | } |
7cbf1722 JA |
1965 | |
1966 | /* | |
1967 | * Slow path - we failed, find and delete work. if the work is not | |
1968 | * in the list, it got run and we're fine. | |
1969 | */ | |
0b81e80c | 1970 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 JA |
1971 | wq_list_for_each(node, prev, &tctx->task_list) { |
1972 | if (&req->io_task_work.node == node) { | |
1973 | wq_list_del(&tctx->task_list, node, prev); | |
1974 | ret = 1; | |
1975 | break; | |
1976 | } | |
1977 | } | |
0b81e80c | 1978 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
1979 | clear_bit(0, &tctx->task_state); |
1980 | return ret; | |
1981 | } | |
1982 | ||
9b465711 PB |
1983 | static bool io_run_task_work_head(struct callback_head **work_head) |
1984 | { | |
1985 | struct callback_head *work, *next; | |
1986 | bool executed = false; | |
1987 | ||
1988 | do { | |
1989 | work = xchg(work_head, NULL); | |
1990 | if (!work) | |
1991 | break; | |
1992 | ||
1993 | do { | |
1994 | next = work->next; | |
1995 | work->func(work); | |
1996 | work = next; | |
1997 | cond_resched(); | |
1998 | } while (work); | |
1999 | executed = true; | |
2000 | } while (1); | |
2001 | ||
2002 | return executed; | |
2003 | } | |
2004 | ||
2005 | static void io_task_work_add_head(struct callback_head **work_head, | |
2006 | struct callback_head *task_work) | |
eab30c4d | 2007 | { |
7c25c0d1 | 2008 | struct callback_head *head; |
eab30c4d | 2009 | |
7c25c0d1 | 2010 | do { |
9b465711 PB |
2011 | head = READ_ONCE(*work_head); |
2012 | task_work->next = head; | |
2013 | } while (cmpxchg(work_head, head, task_work) != head); | |
2014 | } | |
2015 | ||
2016 | static void io_req_task_work_add_fallback(struct io_kiocb *req, | |
2017 | task_work_func_t cb) | |
2018 | { | |
2019 | init_task_work(&req->task_work, cb); | |
2020 | io_task_work_add_head(&req->ctx->exit_task_work, &req->task_work); | |
eab30c4d PB |
2021 | } |
2022 | ||
c40f6379 JA |
2023 | static void io_req_task_cancel(struct callback_head *cb) |
2024 | { | |
2025 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
87ceb6a6 | 2026 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2027 | |
e83acd7d | 2028 | /* ctx is guaranteed to stay alive while we hold uring_lock */ |
792bb6eb | 2029 | mutex_lock(&ctx->uring_lock); |
2593553a | 2030 | io_req_complete_failed(req, req->result); |
792bb6eb | 2031 | mutex_unlock(&ctx->uring_lock); |
c40f6379 JA |
2032 | } |
2033 | ||
2034 | static void __io_req_task_submit(struct io_kiocb *req) | |
2035 | { | |
2036 | struct io_ring_ctx *ctx = req->ctx; | |
2037 | ||
04fc6c80 | 2038 | /* ctx stays valid until unlock, even if we drop all ours ctx->refs */ |
81b6d05c | 2039 | mutex_lock(&ctx->uring_lock); |
70aacfe6 | 2040 | if (!(current->flags & PF_EXITING) && !current->in_execve) |
c5eef2b9 | 2041 | __io_queue_sqe(req); |
81b6d05c | 2042 | else |
2593553a | 2043 | io_req_complete_failed(req, -EFAULT); |
81b6d05c | 2044 | mutex_unlock(&ctx->uring_lock); |
c40f6379 JA |
2045 | } |
2046 | ||
2047 | static void io_req_task_submit(struct callback_head *cb) | |
2048 | { | |
2049 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
2050 | ||
2051 | __io_req_task_submit(req); | |
2052 | } | |
2053 | ||
2c4b8eb6 | 2054 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
c40f6379 | 2055 | { |
2c4b8eb6 PB |
2056 | req->result = ret; |
2057 | req->task_work.func = io_req_task_cancel; | |
c40f6379 | 2058 | |
2c4b8eb6 | 2059 | if (unlikely(io_req_task_work_add(req))) |
eab30c4d | 2060 | io_req_task_work_add_fallback(req, io_req_task_cancel); |
c40f6379 JA |
2061 | } |
2062 | ||
2c4b8eb6 | 2063 | static void io_req_task_queue(struct io_kiocb *req) |
a3df7698 | 2064 | { |
2c4b8eb6 | 2065 | req->task_work.func = io_req_task_submit; |
a3df7698 PB |
2066 | |
2067 | if (unlikely(io_req_task_work_add(req))) | |
2c4b8eb6 | 2068 | io_req_task_queue_fail(req, -ECANCELED); |
a3df7698 PB |
2069 | } |
2070 | ||
f2f87370 | 2071 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2072 | { |
9b5f7bd9 | 2073 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2074 | |
2075 | if (nxt) | |
906a8c3f | 2076 | io_req_task_queue(nxt); |
c69f8dbe JL |
2077 | } |
2078 | ||
c3524383 | 2079 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2080 | { |
c3524383 PB |
2081 | io_queue_next(req); |
2082 | __io_free_req(req); | |
2083 | } | |
8766dd51 | 2084 | |
2d6500d4 | 2085 | struct req_batch { |
5af1d13e PB |
2086 | struct task_struct *task; |
2087 | int task_refs; | |
1b4c351f | 2088 | int ctx_refs; |
2d6500d4 PB |
2089 | }; |
2090 | ||
5af1d13e PB |
2091 | static inline void io_init_req_batch(struct req_batch *rb) |
2092 | { | |
5af1d13e | 2093 | rb->task_refs = 0; |
9ae72463 | 2094 | rb->ctx_refs = 0; |
5af1d13e PB |
2095 | rb->task = NULL; |
2096 | } | |
2097 | ||
2d6500d4 PB |
2098 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
2099 | struct req_batch *rb) | |
2100 | { | |
6e833d53 | 2101 | if (rb->task) |
7c660731 | 2102 | io_put_task(rb->task, rb->task_refs); |
9ae72463 PB |
2103 | if (rb->ctx_refs) |
2104 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); | |
2d6500d4 PB |
2105 | } |
2106 | ||
6ff119a6 PB |
2107 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
2108 | struct io_submit_state *state) | |
2d6500d4 | 2109 | { |
f2f87370 | 2110 | io_queue_next(req); |
96670657 | 2111 | io_dismantle_req(req); |
2d6500d4 | 2112 | |
e3bc8e9d | 2113 | if (req->task != rb->task) { |
7c660731 PB |
2114 | if (rb->task) |
2115 | io_put_task(rb->task, rb->task_refs); | |
e3bc8e9d JA |
2116 | rb->task = req->task; |
2117 | rb->task_refs = 0; | |
5af1d13e | 2118 | } |
e3bc8e9d | 2119 | rb->task_refs++; |
9ae72463 | 2120 | rb->ctx_refs++; |
5af1d13e | 2121 | |
bd759045 | 2122 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
6ff119a6 | 2123 | state->reqs[state->free_reqs++] = req; |
bd759045 PB |
2124 | else |
2125 | list_add(&req->compl.list, &state->comp.free_list); | |
7a743e22 PB |
2126 | } |
2127 | ||
905c172f PB |
2128 | static void io_submit_flush_completions(struct io_comp_state *cs, |
2129 | struct io_ring_ctx *ctx) | |
2130 | { | |
2131 | int i, nr = cs->nr; | |
2132 | struct io_kiocb *req; | |
2133 | struct req_batch rb; | |
2134 | ||
2135 | io_init_req_batch(&rb); | |
2136 | spin_lock_irq(&ctx->completion_lock); | |
2137 | for (i = 0; i < nr; i++) { | |
2138 | req = cs->reqs[i]; | |
8d13326e | 2139 | __io_cqring_fill_event(req, req->result, req->compl.cflags); |
905c172f PB |
2140 | } |
2141 | io_commit_cqring(ctx); | |
2142 | spin_unlock_irq(&ctx->completion_lock); | |
2143 | ||
2144 | io_cqring_ev_posted(ctx); | |
2145 | for (i = 0; i < nr; i++) { | |
2146 | req = cs->reqs[i]; | |
2147 | ||
2148 | /* submission and completion refs */ | |
de9b4cca | 2149 | if (req_ref_sub_and_test(req, 2)) |
6ff119a6 | 2150 | io_req_free_batch(&rb, req, &ctx->submit_state); |
905c172f PB |
2151 | } |
2152 | ||
2153 | io_req_free_batch_finish(ctx, &rb); | |
2154 | cs->nr = 0; | |
7a743e22 PB |
2155 | } |
2156 | ||
ba816ad6 JA |
2157 | /* |
2158 | * Drop reference to request, return next in chain (if there is one) if this | |
2159 | * was the last reference to this request. | |
2160 | */ | |
0d85035a | 2161 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2162 | { |
9b5f7bd9 PB |
2163 | struct io_kiocb *nxt = NULL; |
2164 | ||
de9b4cca | 2165 | if (req_ref_put_and_test(req)) { |
9b5f7bd9 | 2166 | nxt = io_req_find_next(req); |
4d7dd462 | 2167 | __io_free_req(req); |
2a44f467 | 2168 | } |
9b5f7bd9 | 2169 | return nxt; |
2b188cc1 JA |
2170 | } |
2171 | ||
0d85035a | 2172 | static inline void io_put_req(struct io_kiocb *req) |
e65ef56d | 2173 | { |
de9b4cca | 2174 | if (req_ref_put_and_test(req)) |
e65ef56d | 2175 | io_free_req(req); |
2b188cc1 JA |
2176 | } |
2177 | ||
216578e5 PB |
2178 | static void io_put_req_deferred_cb(struct callback_head *cb) |
2179 | { | |
2180 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
2181 | ||
2182 | io_free_req(req); | |
2183 | } | |
2184 | ||
2185 | static void io_free_req_deferred(struct io_kiocb *req) | |
2186 | { | |
7cbf1722 | 2187 | req->task_work.func = io_put_req_deferred_cb; |
a05432fb | 2188 | if (unlikely(io_req_task_work_add(req))) |
eab30c4d | 2189 | io_req_task_work_add_fallback(req, io_put_req_deferred_cb); |
216578e5 PB |
2190 | } |
2191 | ||
2192 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) | |
2193 | { | |
de9b4cca | 2194 | if (req_ref_sub_and_test(req, refs)) |
216578e5 PB |
2195 | io_free_req_deferred(req); |
2196 | } | |
2197 | ||
6c503150 | 2198 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2199 | { |
2200 | /* See comment at the top of this file */ | |
2201 | smp_rmb(); | |
e23de15f | 2202 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2203 | } |
2204 | ||
fb5ccc98 PB |
2205 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2206 | { | |
2207 | struct io_rings *rings = ctx->rings; | |
2208 | ||
2209 | /* make sure SQ entry isn't read before tail */ | |
2210 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2211 | } | |
2212 | ||
8ff069bf | 2213 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2214 | { |
8ff069bf | 2215 | unsigned int cflags; |
e94f141b | 2216 | |
bcda7baa JA |
2217 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2218 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2219 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2220 | kfree(kbuf); |
2221 | return cflags; | |
e94f141b JA |
2222 | } |
2223 | ||
8ff069bf | 2224 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2225 | { |
4d954c25 | 2226 | struct io_buffer *kbuf; |
bcda7baa | 2227 | |
4d954c25 | 2228 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
8ff069bf PB |
2229 | return io_put_kbuf(req, kbuf); |
2230 | } | |
2231 | ||
4c6e277c JA |
2232 | static inline bool io_run_task_work(void) |
2233 | { | |
6200b0ae JA |
2234 | /* |
2235 | * Not safe to run on exiting task, and the task_work handling will | |
2236 | * not add work to such a task. | |
2237 | */ | |
2238 | if (unlikely(current->flags & PF_EXITING)) | |
2239 | return false; | |
4c6e277c JA |
2240 | if (current->task_works) { |
2241 | __set_current_state(TASK_RUNNING); | |
2242 | task_work_run(); | |
2243 | return true; | |
2244 | } | |
2245 | ||
2246 | return false; | |
bcda7baa JA |
2247 | } |
2248 | ||
def596e9 JA |
2249 | /* |
2250 | * Find and free completed poll iocbs | |
2251 | */ | |
2252 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2253 | struct list_head *done) | |
2254 | { | |
8237e045 | 2255 | struct req_batch rb; |
def596e9 | 2256 | struct io_kiocb *req; |
bbde017a XW |
2257 | |
2258 | /* order with ->result store in io_complete_rw_iopoll() */ | |
2259 | smp_rmb(); | |
def596e9 | 2260 | |
5af1d13e | 2261 | io_init_req_batch(&rb); |
def596e9 | 2262 | while (!list_empty(done)) { |
bcda7baa JA |
2263 | int cflags = 0; |
2264 | ||
d21ffe7e | 2265 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
f161340d PB |
2266 | list_del(&req->inflight_entry); |
2267 | ||
8c130827 PB |
2268 | if (READ_ONCE(req->result) == -EAGAIN && |
2269 | !(req->flags & REQ_F_DONT_REISSUE)) { | |
bbde017a | 2270 | req->iopoll_completed = 0; |
8c130827 PB |
2271 | req_ref_get(req); |
2272 | io_queue_async_work(req); | |
2273 | continue; | |
bbde017a | 2274 | } |
def596e9 | 2275 | |
bcda7baa | 2276 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2277 | cflags = io_put_rw_kbuf(req); |
bcda7baa | 2278 | |
8d13326e | 2279 | __io_cqring_fill_event(req, req->result, cflags); |
def596e9 JA |
2280 | (*nr_events)++; |
2281 | ||
de9b4cca | 2282 | if (req_ref_put_and_test(req)) |
6ff119a6 | 2283 | io_req_free_batch(&rb, req, &ctx->submit_state); |
def596e9 | 2284 | } |
def596e9 | 2285 | |
09bb8394 | 2286 | io_commit_cqring(ctx); |
80c18e4a | 2287 | io_cqring_ev_posted_iopoll(ctx); |
2d6500d4 | 2288 | io_req_free_batch_finish(ctx, &rb); |
581f9810 BM |
2289 | } |
2290 | ||
def596e9 JA |
2291 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
2292 | long min) | |
2293 | { | |
2294 | struct io_kiocb *req, *tmp; | |
2295 | LIST_HEAD(done); | |
2296 | bool spin; | |
2297 | int ret; | |
2298 | ||
2299 | /* | |
2300 | * Only spin for completions if we don't have multiple devices hanging | |
2301 | * off our complete list, and we're under the requested amount. | |
2302 | */ | |
2303 | spin = !ctx->poll_multi_file && *nr_events < min; | |
2304 | ||
2305 | ret = 0; | |
d21ffe7e | 2306 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
9adbd45d | 2307 | struct kiocb *kiocb = &req->rw.kiocb; |
def596e9 JA |
2308 | |
2309 | /* | |
581f9810 BM |
2310 | * Move completed and retryable entries to our local lists. |
2311 | * If we find a request that requires polling, break out | |
2312 | * and complete those lists first, if we have entries there. | |
def596e9 | 2313 | */ |
65a6543d | 2314 | if (READ_ONCE(req->iopoll_completed)) { |
d21ffe7e | 2315 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2316 | continue; |
2317 | } | |
2318 | if (!list_empty(&done)) | |
2319 | break; | |
2320 | ||
2321 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
2322 | if (ret < 0) | |
2323 | break; | |
2324 | ||
3aadc23e PB |
2325 | /* iopoll may have completed current req */ |
2326 | if (READ_ONCE(req->iopoll_completed)) | |
d21ffe7e | 2327 | list_move_tail(&req->inflight_entry, &done); |
3aadc23e | 2328 | |
def596e9 JA |
2329 | if (ret && spin) |
2330 | spin = false; | |
2331 | ret = 0; | |
2332 | } | |
2333 | ||
2334 | if (!list_empty(&done)) | |
2335 | io_iopoll_complete(ctx, nr_events, &done); | |
2336 | ||
2337 | return ret; | |
2338 | } | |
2339 | ||
2340 | /* | |
d195a66e | 2341 | * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a |
def596e9 JA |
2342 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
2343 | * as a non-spinning completion check. | |
2344 | */ | |
2345 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2346 | long min) | |
2347 | { | |
540e32a0 | 2348 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
def596e9 JA |
2349 | int ret; |
2350 | ||
2351 | ret = io_do_iopoll(ctx, nr_events, min); | |
2352 | if (ret < 0) | |
2353 | return ret; | |
eba0a4dd | 2354 | if (*nr_events >= min) |
def596e9 JA |
2355 | return 0; |
2356 | } | |
2357 | ||
2358 | return 1; | |
2359 | } | |
2360 | ||
2361 | /* | |
2362 | * We can't just wait for polled events to come to us, we have to actively | |
2363 | * find and complete them. | |
2364 | */ | |
b2edc0a7 | 2365 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2366 | { |
2367 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2368 | return; | |
2369 | ||
2370 | mutex_lock(&ctx->uring_lock); | |
540e32a0 | 2371 | while (!list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2372 | unsigned int nr_events = 0; |
2373 | ||
b2edc0a7 | 2374 | io_do_iopoll(ctx, &nr_events, 0); |
08f5439f | 2375 | |
b2edc0a7 PB |
2376 | /* let it sleep and repeat later if can't complete a request */ |
2377 | if (nr_events == 0) | |
2378 | break; | |
08f5439f JA |
2379 | /* |
2380 | * Ensure we allow local-to-the-cpu processing to take place, | |
2381 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2382 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2383 | */ |
3fcee5a6 PB |
2384 | if (need_resched()) { |
2385 | mutex_unlock(&ctx->uring_lock); | |
2386 | cond_resched(); | |
2387 | mutex_lock(&ctx->uring_lock); | |
2388 | } | |
def596e9 JA |
2389 | } |
2390 | mutex_unlock(&ctx->uring_lock); | |
2391 | } | |
2392 | ||
7668b92a | 2393 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2394 | { |
7668b92a | 2395 | unsigned int nr_events = 0; |
2b2ed975 | 2396 | int iters = 0, ret = 0; |
500f9fba | 2397 | |
c7849be9 XW |
2398 | /* |
2399 | * We disallow the app entering submit/complete with polling, but we | |
2400 | * still need to lock the ring to prevent racing with polled issue | |
2401 | * that got punted to a workqueue. | |
2402 | */ | |
2403 | mutex_lock(&ctx->uring_lock); | |
def596e9 | 2404 | do { |
a3a0e43f JA |
2405 | /* |
2406 | * Don't enter poll loop if we already have events pending. | |
2407 | * If we do, we can potentially be spinning for commands that | |
2408 | * already triggered a CQE (eg in error). | |
2409 | */ | |
6c503150 | 2410 | if (test_bit(0, &ctx->cq_check_overflow)) |
6c2450ae | 2411 | __io_cqring_overflow_flush(ctx, false); |
6c503150 | 2412 | if (io_cqring_events(ctx)) |
a3a0e43f JA |
2413 | break; |
2414 | ||
500f9fba JA |
2415 | /* |
2416 | * If a submit got punted to a workqueue, we can have the | |
2417 | * application entering polling for a command before it gets | |
2418 | * issued. That app will hold the uring_lock for the duration | |
2419 | * of the poll right here, so we need to take a breather every | |
2420 | * now and then to ensure that the issue has a chance to add | |
2421 | * the poll to the issued list. Otherwise we can spin here | |
2422 | * forever, while the workqueue is stuck trying to acquire the | |
2423 | * very same mutex. | |
2424 | */ | |
2425 | if (!(++iters & 7)) { | |
2426 | mutex_unlock(&ctx->uring_lock); | |
4c6e277c | 2427 | io_run_task_work(); |
500f9fba JA |
2428 | mutex_lock(&ctx->uring_lock); |
2429 | } | |
2430 | ||
7668b92a | 2431 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
def596e9 JA |
2432 | if (ret <= 0) |
2433 | break; | |
2434 | ret = 0; | |
7668b92a | 2435 | } while (min && !nr_events && !need_resched()); |
def596e9 | 2436 | |
500f9fba | 2437 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2438 | return ret; |
2439 | } | |
2440 | ||
491381ce | 2441 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2442 | { |
491381ce JA |
2443 | /* |
2444 | * Tell lockdep we inherited freeze protection from submission | |
2445 | * thread. | |
2446 | */ | |
2447 | if (req->flags & REQ_F_ISREG) { | |
1c98679d | 2448 | struct super_block *sb = file_inode(req->file)->i_sb; |
2b188cc1 | 2449 | |
1c98679d PB |
2450 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
2451 | sb_end_write(sb); | |
2b188cc1 JA |
2452 | } |
2453 | } | |
2454 | ||
b63534c4 | 2455 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2456 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 | 2457 | { |
ab454438 PB |
2458 | struct io_async_rw *rw = req->async_data; |
2459 | ||
2460 | if (!rw) | |
2461 | return !io_req_prep_async(req); | |
2462 | /* may have left rw->iter inconsistent on -EIOCBQUEUED */ | |
2463 | iov_iter_revert(&rw->iter, req->result - iov_iter_count(&rw->iter)); | |
2464 | return true; | |
b63534c4 | 2465 | } |
b63534c4 | 2466 | |
3e6a0d3c | 2467 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 2468 | { |
355afaeb | 2469 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 2470 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 2471 | |
355afaeb JA |
2472 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2473 | return false; | |
3e6a0d3c JA |
2474 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
2475 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 2476 | return false; |
7c977a58 JA |
2477 | /* |
2478 | * If ref is dying, we might be running poll reap from the exit work. | |
2479 | * Don't attempt to reissue from that path, just let it fail with | |
2480 | * -EAGAIN. | |
2481 | */ | |
3e6a0d3c JA |
2482 | if (percpu_ref_is_dying(&ctx->refs)) |
2483 | return false; | |
2484 | return true; | |
2485 | } | |
e82ad485 | 2486 | #else |
a1ff1e3f JA |
2487 | static bool io_resubmit_prep(struct io_kiocb *req) |
2488 | { | |
2489 | return false; | |
2490 | } | |
e82ad485 JA |
2491 | static bool io_rw_should_reissue(struct io_kiocb *req) |
2492 | { | |
2493 | return false; | |
2494 | } | |
3e6a0d3c JA |
2495 | #endif |
2496 | ||
a1d7c393 | 2497 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
889fca73 | 2498 | unsigned int issue_flags) |
a1d7c393 | 2499 | { |
2f8e45f1 PB |
2500 | int cflags = 0; |
2501 | ||
b65c128f PB |
2502 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
2503 | kiocb_end_write(req); | |
9532b99b PB |
2504 | if (res != req->result) { |
2505 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && | |
2506 | io_rw_should_reissue(req)) { | |
2507 | req->flags |= REQ_F_REISSUE; | |
2508 | return; | |
2509 | } | |
2f8e45f1 | 2510 | req_set_fail_links(req); |
9532b99b | 2511 | } |
2f8e45f1 PB |
2512 | if (req->flags & REQ_F_BUFFER_SELECTED) |
2513 | cflags = io_put_rw_kbuf(req); | |
2514 | __io_req_complete(req, issue_flags, res, cflags); | |
ba816ad6 JA |
2515 | } |
2516 | ||
2517 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2518 | { | |
9adbd45d | 2519 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2520 | |
889fca73 | 2521 | __io_complete_rw(req, res, res2, 0); |
2b188cc1 JA |
2522 | } |
2523 | ||
def596e9 JA |
2524 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2525 | { | |
9adbd45d | 2526 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2527 | |
491381ce JA |
2528 | if (kiocb->ki_flags & IOCB_WRITE) |
2529 | kiocb_end_write(req); | |
9532b99b | 2530 | if (unlikely(res != req->result)) { |
a1ff1e3f JA |
2531 | if (!(res == -EAGAIN && io_rw_should_reissue(req) && |
2532 | io_resubmit_prep(req))) { | |
9532b99b PB |
2533 | req_set_fail_links(req); |
2534 | req->flags |= REQ_F_DONT_REISSUE; | |
2535 | } | |
8c130827 | 2536 | } |
bbde017a XW |
2537 | |
2538 | WRITE_ONCE(req->result, res); | |
b9b0e0d3 | 2539 | /* order with io_iopoll_complete() checking ->result */ |
cd664b0e PB |
2540 | smp_wmb(); |
2541 | WRITE_ONCE(req->iopoll_completed, 1); | |
def596e9 JA |
2542 | } |
2543 | ||
2544 | /* | |
2545 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2546 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
2547 | * find it from a io_iopoll_getevents() thread before the issuer is done | |
2548 | * accessing the kiocb cookie. | |
2549 | */ | |
2e9dbe90 | 2550 | static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async) |
def596e9 JA |
2551 | { |
2552 | struct io_ring_ctx *ctx = req->ctx; | |
2553 | ||
2554 | /* | |
2555 | * Track whether we have multiple files in our lists. This will impact | |
2556 | * how we do polling eventually, not spinning if we're on potentially | |
2557 | * different devices. | |
2558 | */ | |
540e32a0 | 2559 | if (list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2560 | ctx->poll_multi_file = false; |
2561 | } else if (!ctx->poll_multi_file) { | |
2562 | struct io_kiocb *list_req; | |
2563 | ||
540e32a0 | 2564 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
d21ffe7e | 2565 | inflight_entry); |
9adbd45d | 2566 | if (list_req->file != req->file) |
def596e9 JA |
2567 | ctx->poll_multi_file = true; |
2568 | } | |
2569 | ||
2570 | /* | |
2571 | * For fast devices, IO may have already completed. If it has, add | |
2572 | * it to the front so we find it first. | |
2573 | */ | |
65a6543d | 2574 | if (READ_ONCE(req->iopoll_completed)) |
d21ffe7e | 2575 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
def596e9 | 2576 | else |
d21ffe7e | 2577 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
bdcd3eab | 2578 | |
2e9dbe90 XW |
2579 | /* |
2580 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread | |
2581 | * task context or in io worker task context. If current task context is | |
2582 | * sq thread, we don't need to check whether should wake up sq thread. | |
2583 | */ | |
2584 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && | |
534ca6d6 JA |
2585 | wq_has_sleeper(&ctx->sq_data->wait)) |
2586 | wake_up(&ctx->sq_data->wait); | |
def596e9 JA |
2587 | } |
2588 | ||
9f13c35b PB |
2589 | static inline void io_state_file_put(struct io_submit_state *state) |
2590 | { | |
02b23a9a PB |
2591 | if (state->file_refs) { |
2592 | fput_many(state->file, state->file_refs); | |
2593 | state->file_refs = 0; | |
2594 | } | |
9a56a232 JA |
2595 | } |
2596 | ||
2597 | /* | |
2598 | * Get as many references to a file as we have IOs left in this submission, | |
2599 | * assuming most submissions are for one file, or at least that each file | |
2600 | * has more than one submission. | |
2601 | */ | |
8da11c19 | 2602 | static struct file *__io_file_get(struct io_submit_state *state, int fd) |
9a56a232 JA |
2603 | { |
2604 | if (!state) | |
2605 | return fget(fd); | |
2606 | ||
6e1271e6 | 2607 | if (state->file_refs) { |
9a56a232 | 2608 | if (state->fd == fd) { |
6e1271e6 | 2609 | state->file_refs--; |
9a56a232 JA |
2610 | return state->file; |
2611 | } | |
02b23a9a | 2612 | io_state_file_put(state); |
9a56a232 JA |
2613 | } |
2614 | state->file = fget_many(fd, state->ios_left); | |
6e1271e6 | 2615 | if (unlikely(!state->file)) |
9a56a232 JA |
2616 | return NULL; |
2617 | ||
2618 | state->fd = fd; | |
6e1271e6 | 2619 | state->file_refs = state->ios_left - 1; |
9a56a232 JA |
2620 | return state->file; |
2621 | } | |
2622 | ||
4503b767 JA |
2623 | static bool io_bdev_nowait(struct block_device *bdev) |
2624 | { | |
9ba0d0c8 | 2625 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2626 | } |
2627 | ||
2b188cc1 JA |
2628 | /* |
2629 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2630 | * any file. For now, just ensure that anything potentially problematic is done | |
2631 | * inline. | |
2632 | */ | |
7b29f92d | 2633 | static bool __io_file_supports_async(struct file *file, int rw) |
2b188cc1 JA |
2634 | { |
2635 | umode_t mode = file_inode(file)->i_mode; | |
2636 | ||
4503b767 | 2637 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2638 | if (IS_ENABLED(CONFIG_BLOCK) && |
2639 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2640 | return true; |
2641 | return false; | |
2642 | } | |
2643 | if (S_ISCHR(mode) || S_ISSOCK(mode)) | |
2b188cc1 | 2644 | return true; |
4503b767 | 2645 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2646 | if (IS_ENABLED(CONFIG_BLOCK) && |
2647 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2648 | file->f_op != &io_uring_fops) |
2649 | return true; | |
2650 | return false; | |
2651 | } | |
2b188cc1 | 2652 | |
c5b85625 JA |
2653 | /* any ->read/write should understand O_NONBLOCK */ |
2654 | if (file->f_flags & O_NONBLOCK) | |
2655 | return true; | |
2656 | ||
af197f50 JA |
2657 | if (!(file->f_mode & FMODE_NOWAIT)) |
2658 | return false; | |
2659 | ||
2660 | if (rw == READ) | |
2661 | return file->f_op->read_iter != NULL; | |
2662 | ||
2663 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
2664 | } |
2665 | ||
7b29f92d JA |
2666 | static bool io_file_supports_async(struct io_kiocb *req, int rw) |
2667 | { | |
2668 | if (rw == READ && (req->flags & REQ_F_ASYNC_READ)) | |
2669 | return true; | |
2670 | else if (rw == WRITE && (req->flags & REQ_F_ASYNC_WRITE)) | |
2671 | return true; | |
2672 | ||
2673 | return __io_file_supports_async(req->file, rw); | |
2674 | } | |
2675 | ||
a88fc400 | 2676 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 2677 | { |
def596e9 | 2678 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2679 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 2680 | struct file *file = req->file; |
09bb8394 JA |
2681 | unsigned ioprio; |
2682 | int ret; | |
2b188cc1 | 2683 | |
7b29f92d | 2684 | if (!(req->flags & REQ_F_ISREG) && S_ISREG(file_inode(file)->i_mode)) |
491381ce JA |
2685 | req->flags |= REQ_F_ISREG; |
2686 | ||
2b188cc1 | 2687 | kiocb->ki_pos = READ_ONCE(sqe->off); |
75c668cd | 2688 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
ba04291e | 2689 | req->flags |= REQ_F_CUR_POS; |
75c668cd | 2690 | kiocb->ki_pos = file->f_pos; |
ba04291e | 2691 | } |
2b188cc1 | 2692 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
2693 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
2694 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
2695 | if (unlikely(ret)) | |
2696 | return ret; | |
2b188cc1 | 2697 | |
75c668cd PB |
2698 | /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */ |
2699 | if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK)) | |
2700 | req->flags |= REQ_F_NOWAIT; | |
2701 | ||
2b188cc1 JA |
2702 | ioprio = READ_ONCE(sqe->ioprio); |
2703 | if (ioprio) { | |
2704 | ret = ioprio_check_cap(ioprio); | |
2705 | if (ret) | |
09bb8394 | 2706 | return ret; |
2b188cc1 JA |
2707 | |
2708 | kiocb->ki_ioprio = ioprio; | |
2709 | } else | |
2710 | kiocb->ki_ioprio = get_current_ioprio(); | |
2711 | ||
def596e9 | 2712 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
2713 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
2714 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 2715 | return -EOPNOTSUPP; |
2b188cc1 | 2716 | |
def596e9 JA |
2717 | kiocb->ki_flags |= IOCB_HIPRI; |
2718 | kiocb->ki_complete = io_complete_rw_iopoll; | |
65a6543d | 2719 | req->iopoll_completed = 0; |
def596e9 | 2720 | } else { |
09bb8394 JA |
2721 | if (kiocb->ki_flags & IOCB_HIPRI) |
2722 | return -EINVAL; | |
def596e9 JA |
2723 | kiocb->ki_complete = io_complete_rw; |
2724 | } | |
9adbd45d | 2725 | |
3529d8c2 JA |
2726 | req->rw.addr = READ_ONCE(sqe->addr); |
2727 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2728 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2729 | return 0; |
2b188cc1 JA |
2730 | } |
2731 | ||
2732 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2733 | { | |
2734 | switch (ret) { | |
2735 | case -EIOCBQUEUED: | |
2736 | break; | |
2737 | case -ERESTARTSYS: | |
2738 | case -ERESTARTNOINTR: | |
2739 | case -ERESTARTNOHAND: | |
2740 | case -ERESTART_RESTARTBLOCK: | |
2741 | /* | |
2742 | * We can't just restart the syscall, since previously | |
2743 | * submitted sqes may already be in progress. Just fail this | |
2744 | * IO with EINTR. | |
2745 | */ | |
2746 | ret = -EINTR; | |
df561f66 | 2747 | fallthrough; |
2b188cc1 JA |
2748 | default: |
2749 | kiocb->ki_complete(kiocb, ret, 0); | |
2750 | } | |
2751 | } | |
2752 | ||
a1d7c393 | 2753 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
889fca73 | 2754 | unsigned int issue_flags) |
ba816ad6 | 2755 | { |
ba04291e | 2756 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2757 | struct io_async_rw *io = req->async_data; |
97284637 | 2758 | bool check_reissue = kiocb->ki_complete == io_complete_rw; |
ba04291e | 2759 | |
227c0c96 | 2760 | /* add previously done IO, if any */ |
e8c2bc1f | 2761 | if (io && io->bytes_done > 0) { |
227c0c96 | 2762 | if (ret < 0) |
e8c2bc1f | 2763 | ret = io->bytes_done; |
227c0c96 | 2764 | else |
e8c2bc1f | 2765 | ret += io->bytes_done; |
227c0c96 JA |
2766 | } |
2767 | ||
ba04291e JA |
2768 | if (req->flags & REQ_F_CUR_POS) |
2769 | req->file->f_pos = kiocb->ki_pos; | |
bcaec089 | 2770 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
889fca73 | 2771 | __io_complete_rw(req, ret, 0, issue_flags); |
ba816ad6 JA |
2772 | else |
2773 | io_rw_done(kiocb, ret); | |
97284637 PB |
2774 | |
2775 | if (check_reissue && req->flags & REQ_F_REISSUE) { | |
2776 | req->flags &= ~REQ_F_REISSUE; | |
8c130827 PB |
2777 | if (!io_resubmit_prep(req)) { |
2778 | req_ref_get(req); | |
2779 | io_queue_async_work(req); | |
2780 | } else { | |
97284637 PB |
2781 | int cflags = 0; |
2782 | ||
2783 | req_set_fail_links(req); | |
2784 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
2785 | cflags = io_put_rw_kbuf(req); | |
2786 | __io_req_complete(req, issue_flags, ret, cflags); | |
2787 | } | |
2788 | } | |
ba816ad6 JA |
2789 | } |
2790 | ||
847595de | 2791 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
edafccee | 2792 | { |
9adbd45d JA |
2793 | struct io_ring_ctx *ctx = req->ctx; |
2794 | size_t len = req->rw.len; | |
edafccee | 2795 | struct io_mapped_ubuf *imu; |
4be1c615 | 2796 | u16 index, buf_index = req->buf_index; |
75769e3f | 2797 | u64 buf_end, buf_addr = req->rw.addr; |
edafccee | 2798 | size_t offset; |
edafccee | 2799 | |
edafccee JA |
2800 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
2801 | return -EFAULT; | |
edafccee JA |
2802 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
2803 | imu = &ctx->user_bufs[index]; | |
9adbd45d | 2804 | buf_addr = req->rw.addr; |
edafccee | 2805 | |
75769e3f | 2806 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
edafccee JA |
2807 | return -EFAULT; |
2808 | /* not inside the mapped region */ | |
4751f53d | 2809 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
edafccee JA |
2810 | return -EFAULT; |
2811 | ||
2812 | /* | |
2813 | * May not be a start of buffer, set size appropriately | |
2814 | * and advance us to the beginning. | |
2815 | */ | |
2816 | offset = buf_addr - imu->ubuf; | |
2817 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
2818 | |
2819 | if (offset) { | |
2820 | /* | |
2821 | * Don't use iov_iter_advance() here, as it's really slow for | |
2822 | * using the latter parts of a big fixed buffer - it iterates | |
2823 | * over each segment manually. We can cheat a bit here, because | |
2824 | * we know that: | |
2825 | * | |
2826 | * 1) it's a BVEC iter, we set it up | |
2827 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
2828 | * first and last bvec | |
2829 | * | |
2830 | * So just find our index, and adjust the iterator afterwards. | |
2831 | * If the offset is within the first bvec (or the whole first | |
2832 | * bvec, just use iov_iter_advance(). This makes it easier | |
2833 | * since we can just skip the first segment, which may not | |
2834 | * be PAGE_SIZE aligned. | |
2835 | */ | |
2836 | const struct bio_vec *bvec = imu->bvec; | |
2837 | ||
2838 | if (offset <= bvec->bv_len) { | |
2839 | iov_iter_advance(iter, offset); | |
2840 | } else { | |
2841 | unsigned long seg_skip; | |
2842 | ||
2843 | /* skip first vec */ | |
2844 | offset -= bvec->bv_len; | |
2845 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
2846 | ||
2847 | iter->bvec = bvec + seg_skip; | |
2848 | iter->nr_segs -= seg_skip; | |
99c79f66 | 2849 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 2850 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
2851 | } |
2852 | } | |
2853 | ||
847595de | 2854 | return 0; |
edafccee JA |
2855 | } |
2856 | ||
bcda7baa JA |
2857 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
2858 | { | |
2859 | if (needs_lock) | |
2860 | mutex_unlock(&ctx->uring_lock); | |
2861 | } | |
2862 | ||
2863 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
2864 | { | |
2865 | /* | |
2866 | * "Normal" inline submissions always hold the uring_lock, since we | |
2867 | * grab it from the system call. Same is true for the SQPOLL offload. | |
2868 | * The only exception is when we've detached the request and issue it | |
2869 | * from an async worker thread, grab the lock for that case. | |
2870 | */ | |
2871 | if (needs_lock) | |
2872 | mutex_lock(&ctx->uring_lock); | |
2873 | } | |
2874 | ||
2875 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
2876 | int bgid, struct io_buffer *kbuf, | |
2877 | bool needs_lock) | |
2878 | { | |
2879 | struct io_buffer *head; | |
2880 | ||
2881 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
2882 | return kbuf; | |
2883 | ||
2884 | io_ring_submit_lock(req->ctx, needs_lock); | |
2885 | ||
2886 | lockdep_assert_held(&req->ctx->uring_lock); | |
2887 | ||
9e15c3a0 | 2888 | head = xa_load(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
2889 | if (head) { |
2890 | if (!list_empty(&head->list)) { | |
2891 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
2892 | list); | |
2893 | list_del(&kbuf->list); | |
2894 | } else { | |
2895 | kbuf = head; | |
9e15c3a0 | 2896 | xa_erase(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
2897 | } |
2898 | if (*len > kbuf->len) | |
2899 | *len = kbuf->len; | |
2900 | } else { | |
2901 | kbuf = ERR_PTR(-ENOBUFS); | |
2902 | } | |
2903 | ||
2904 | io_ring_submit_unlock(req->ctx, needs_lock); | |
2905 | ||
2906 | return kbuf; | |
2907 | } | |
2908 | ||
4d954c25 JA |
2909 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
2910 | bool needs_lock) | |
2911 | { | |
2912 | struct io_buffer *kbuf; | |
4f4eeba8 | 2913 | u16 bgid; |
4d954c25 JA |
2914 | |
2915 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
4f4eeba8 | 2916 | bgid = req->buf_index; |
4d954c25 JA |
2917 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
2918 | if (IS_ERR(kbuf)) | |
2919 | return kbuf; | |
2920 | req->rw.addr = (u64) (unsigned long) kbuf; | |
2921 | req->flags |= REQ_F_BUFFER_SELECTED; | |
2922 | return u64_to_user_ptr(kbuf->addr); | |
2923 | } | |
2924 | ||
2925 | #ifdef CONFIG_COMPAT | |
2926 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
2927 | bool needs_lock) | |
2928 | { | |
2929 | struct compat_iovec __user *uiov; | |
2930 | compat_ssize_t clen; | |
2931 | void __user *buf; | |
2932 | ssize_t len; | |
2933 | ||
2934 | uiov = u64_to_user_ptr(req->rw.addr); | |
2935 | if (!access_ok(uiov, sizeof(*uiov))) | |
2936 | return -EFAULT; | |
2937 | if (__get_user(clen, &uiov->iov_len)) | |
2938 | return -EFAULT; | |
2939 | if (clen < 0) | |
2940 | return -EINVAL; | |
2941 | ||
2942 | len = clen; | |
2943 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
2944 | if (IS_ERR(buf)) | |
2945 | return PTR_ERR(buf); | |
2946 | iov[0].iov_base = buf; | |
2947 | iov[0].iov_len = (compat_size_t) len; | |
2948 | return 0; | |
2949 | } | |
2950 | #endif | |
2951 | ||
2952 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
2953 | bool needs_lock) | |
2954 | { | |
2955 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
2956 | void __user *buf; | |
2957 | ssize_t len; | |
2958 | ||
2959 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
2960 | return -EFAULT; | |
2961 | ||
2962 | len = iov[0].iov_len; | |
2963 | if (len < 0) | |
2964 | return -EINVAL; | |
2965 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
2966 | if (IS_ERR(buf)) | |
2967 | return PTR_ERR(buf); | |
2968 | iov[0].iov_base = buf; | |
2969 | iov[0].iov_len = len; | |
2970 | return 0; | |
2971 | } | |
2972 | ||
2973 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
2974 | bool needs_lock) | |
2975 | { | |
dddb3e26 JA |
2976 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
2977 | struct io_buffer *kbuf; | |
2978 | ||
2979 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
2980 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); | |
2981 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 2982 | return 0; |
dddb3e26 | 2983 | } |
dd201662 | 2984 | if (req->rw.len != 1) |
4d954c25 JA |
2985 | return -EINVAL; |
2986 | ||
2987 | #ifdef CONFIG_COMPAT | |
2988 | if (req->ctx->compat) | |
2989 | return io_compat_import(req, iov, needs_lock); | |
2990 | #endif | |
2991 | ||
2992 | return __io_iov_buffer_select(req, iov, needs_lock); | |
2993 | } | |
2994 | ||
847595de PB |
2995 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
2996 | struct iov_iter *iter, bool needs_lock) | |
2b188cc1 | 2997 | { |
9adbd45d JA |
2998 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
2999 | size_t sqe_len = req->rw.len; | |
847595de | 3000 | u8 opcode = req->opcode; |
4d954c25 | 3001 | ssize_t ret; |
edafccee | 3002 | |
7d009165 | 3003 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 3004 | *iovec = NULL; |
9adbd45d | 3005 | return io_import_fixed(req, rw, iter); |
edafccee | 3006 | } |
2b188cc1 | 3007 | |
bcda7baa | 3008 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 3009 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
3010 | return -EINVAL; |
3011 | ||
3a6820f2 | 3012 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3013 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 3014 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 3015 | if (IS_ERR(buf)) |
4d954c25 | 3016 | return PTR_ERR(buf); |
3f9d6441 | 3017 | req->rw.len = sqe_len; |
bcda7baa JA |
3018 | } |
3019 | ||
3a6820f2 JA |
3020 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
3021 | *iovec = NULL; | |
10fc72e4 | 3022 | return ret; |
3a6820f2 JA |
3023 | } |
3024 | ||
4d954c25 JA |
3025 | if (req->flags & REQ_F_BUFFER_SELECT) { |
3026 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
847595de PB |
3027 | if (!ret) |
3028 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); | |
4d954c25 JA |
3029 | *iovec = NULL; |
3030 | return ret; | |
3031 | } | |
3032 | ||
89cd35c5 CH |
3033 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
3034 | req->ctx->compat); | |
2b188cc1 JA |
3035 | } |
3036 | ||
0fef9483 JA |
3037 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3038 | { | |
5b09e37e | 3039 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3040 | } |
3041 | ||
31b51510 | 3042 | /* |
32960613 JA |
3043 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3044 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3045 | */ |
4017eb91 | 3046 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3047 | { |
4017eb91 JA |
3048 | struct kiocb *kiocb = &req->rw.kiocb; |
3049 | struct file *file = req->file; | |
32960613 JA |
3050 | ssize_t ret = 0; |
3051 | ||
3052 | /* | |
3053 | * Don't support polled IO through this interface, and we can't | |
3054 | * support non-blocking either. For the latter, this just causes | |
3055 | * the kiocb to be handled from an async context. | |
3056 | */ | |
3057 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3058 | return -EOPNOTSUPP; | |
3059 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3060 | return -EAGAIN; | |
3061 | ||
3062 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3063 | struct iovec iovec; |
32960613 JA |
3064 | ssize_t nr; |
3065 | ||
311ae9e1 PB |
3066 | if (!iov_iter_is_bvec(iter)) { |
3067 | iovec = iov_iter_iovec(iter); | |
3068 | } else { | |
4017eb91 JA |
3069 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3070 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3071 | } |
3072 | ||
32960613 JA |
3073 | if (rw == READ) { |
3074 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3075 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3076 | } else { |
3077 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3078 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3079 | } |
3080 | ||
3081 | if (nr < 0) { | |
3082 | if (!ret) | |
3083 | ret = nr; | |
3084 | break; | |
3085 | } | |
3086 | ret += nr; | |
3087 | if (nr != iovec.iov_len) | |
3088 | break; | |
4017eb91 JA |
3089 | req->rw.len -= nr; |
3090 | req->rw.addr += nr; | |
32960613 JA |
3091 | iov_iter_advance(iter, nr); |
3092 | } | |
3093 | ||
3094 | return ret; | |
3095 | } | |
3096 | ||
ff6165b2 JA |
3097 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3098 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3099 | { |
e8c2bc1f | 3100 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3101 | |
ff6165b2 | 3102 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3103 | rw->free_iovec = iovec; |
227c0c96 | 3104 | rw->bytes_done = 0; |
ff6165b2 | 3105 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3106 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3107 | return; |
b64e3444 | 3108 | if (!iovec) { |
ff6165b2 JA |
3109 | unsigned iov_off = 0; |
3110 | ||
3111 | rw->iter.iov = rw->fast_iov; | |
3112 | if (iter->iov != fast_iov) { | |
3113 | iov_off = iter->iov - fast_iov; | |
3114 | rw->iter.iov += iov_off; | |
3115 | } | |
3116 | if (rw->fast_iov != fast_iov) | |
3117 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3118 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3119 | } else { |
3120 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3121 | } |
3122 | } | |
3123 | ||
6cb78689 | 3124 | static inline int io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3125 | { |
e8c2bc1f JA |
3126 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3127 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3128 | return req->async_data == NULL; | |
3d9932a8 XW |
3129 | } |
3130 | ||
ff6165b2 JA |
3131 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3132 | const struct iovec *fast_iov, | |
227c0c96 | 3133 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3134 | { |
26f0505a | 3135 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
74566df3 | 3136 | return 0; |
e8c2bc1f | 3137 | if (!req->async_data) { |
6cb78689 | 3138 | if (io_alloc_async_data(req)) { |
6bf985dc | 3139 | kfree(iovec); |
5d204bcf | 3140 | return -ENOMEM; |
6bf985dc | 3141 | } |
b7bb4f7d | 3142 | |
ff6165b2 | 3143 | io_req_map_rw(req, iovec, fast_iov, iter); |
5d204bcf | 3144 | } |
b7bb4f7d | 3145 | return 0; |
f67676d1 JA |
3146 | } |
3147 | ||
73debe68 | 3148 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3149 | { |
e8c2bc1f | 3150 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3151 | struct iovec *iov = iorw->fast_iov; |
847595de | 3152 | int ret; |
c3e330a4 | 3153 | |
2846c481 | 3154 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
c3e330a4 PB |
3155 | if (unlikely(ret < 0)) |
3156 | return ret; | |
3157 | ||
ab0b196c PB |
3158 | iorw->bytes_done = 0; |
3159 | iorw->free_iovec = iov; | |
3160 | if (iov) | |
3161 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3162 | return 0; |
3163 | } | |
3164 | ||
73debe68 | 3165 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3166 | { |
3529d8c2 JA |
3167 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3168 | return -EBADF; | |
93642ef8 | 3169 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3170 | } |
3171 | ||
c1dd91d1 JA |
3172 | /* |
3173 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3174 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3175 | * This gets called when the page is unlocked, and we generally expect that to | |
3176 | * happen when the page IO is completed and the page is now uptodate. This will | |
3177 | * queue a task_work based retry of the operation, attempting to copy the data | |
3178 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3179 | * do a thread based blocking retry of the operation. That's the unexpected | |
3180 | * slow path. | |
3181 | */ | |
bcf5a063 JA |
3182 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3183 | int sync, void *arg) | |
3184 | { | |
3185 | struct wait_page_queue *wpq; | |
3186 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3187 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3188 | |
3189 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3190 | ||
cdc8fcb4 LT |
3191 | if (!wake_page_match(wpq, key)) |
3192 | return 0; | |
3193 | ||
c8d317aa | 3194 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 JA |
3195 | list_del_init(&wait->entry); |
3196 | ||
bcf5a063 | 3197 | /* submit ref gets dropped, acquire a new one */ |
de9b4cca | 3198 | req_ref_get(req); |
921b9054 | 3199 | io_req_task_queue(req); |
bcf5a063 JA |
3200 | return 1; |
3201 | } | |
3202 | ||
c1dd91d1 JA |
3203 | /* |
3204 | * This controls whether a given IO request should be armed for async page | |
3205 | * based retry. If we return false here, the request is handed to the async | |
3206 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3207 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3208 | * will either succeed because the page is now uptodate and unlocked, or it | |
3209 | * will register a callback when the page is unlocked at IO completion. Through | |
3210 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3211 | * That retry will attempt the buffered read again. The retry will generally | |
3212 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3213 | * async worker threads for a blocking retry. | |
3214 | */ | |
227c0c96 | 3215 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3216 | { |
e8c2bc1f JA |
3217 | struct io_async_rw *rw = req->async_data; |
3218 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3219 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3220 | |
bcf5a063 JA |
3221 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3222 | if (req->flags & REQ_F_NOWAIT) | |
3223 | return false; | |
f67676d1 | 3224 | |
227c0c96 | 3225 | /* Only for buffered IO */ |
3b2a4439 | 3226 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3227 | return false; |
3b2a4439 | 3228 | |
bcf5a063 JA |
3229 | /* |
3230 | * just use poll if we can, and don't attempt if the fs doesn't | |
3231 | * support callback based unlocks | |
3232 | */ | |
3233 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3234 | return false; | |
f67676d1 | 3235 | |
3b2a4439 JA |
3236 | wait->wait.func = io_async_buf_func; |
3237 | wait->wait.private = req; | |
3238 | wait->wait.flags = 0; | |
3239 | INIT_LIST_HEAD(&wait->wait.entry); | |
3240 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3241 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3242 | kiocb->ki_waitq = wait; |
3b2a4439 | 3243 | return true; |
bcf5a063 JA |
3244 | } |
3245 | ||
3246 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) | |
3247 | { | |
3248 | if (req->file->f_op->read_iter) | |
3249 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d | 3250 | else if (req->file->f_op->read) |
4017eb91 | 3251 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3252 | else |
3253 | return -EINVAL; | |
f67676d1 JA |
3254 | } |
3255 | ||
889fca73 | 3256 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3257 | { |
3258 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3259 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3260 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3261 | struct io_async_rw *rw = req->async_data; |
227c0c96 | 3262 | ssize_t io_size, ret, ret2; |
45d189c6 | 3263 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ff6165b2 | 3264 | |
2846c481 | 3265 | if (rw) { |
e8c2bc1f | 3266 | iter = &rw->iter; |
2846c481 PB |
3267 | iovec = NULL; |
3268 | } else { | |
3269 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); | |
3270 | if (ret < 0) | |
3271 | return ret; | |
3272 | } | |
632546c4 | 3273 | io_size = iov_iter_count(iter); |
fa15bafb | 3274 | req->result = io_size; |
2b188cc1 | 3275 | |
fd6c2e4c JA |
3276 | /* Ensure we clear previously set non-block flag */ |
3277 | if (!force_nonblock) | |
29de5f6a | 3278 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
a88fc400 PB |
3279 | else |
3280 | kiocb->ki_flags |= IOCB_NOWAIT; | |
3281 | ||
24c74678 | 3282 | /* If the file doesn't support async, just async punt */ |
7b29f92d | 3283 | if (force_nonblock && !io_file_supports_async(req, READ)) { |
6713e7a6 | 3284 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc | 3285 | return ret ?: -EAGAIN; |
6713e7a6 | 3286 | } |
9e645e11 | 3287 | |
632546c4 | 3288 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size); |
5ea5dd45 PB |
3289 | if (unlikely(ret)) { |
3290 | kfree(iovec); | |
3291 | return ret; | |
3292 | } | |
2b188cc1 | 3293 | |
227c0c96 | 3294 | ret = io_iter_do_read(req, iter); |
32960613 | 3295 | |
230d50d4 | 3296 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
6ad7f233 | 3297 | req->flags &= ~REQ_F_REISSUE; |
eefdf30f JA |
3298 | /* IOPOLL retry should happen for io-wq threads */ |
3299 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3300 | goto done; |
75c668cd PB |
3301 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3302 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3303 | goto done; |
84216315 | 3304 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3305 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
f38c7e3a | 3306 | ret = 0; |
230d50d4 JA |
3307 | } else if (ret == -EIOCBQUEUED) { |
3308 | goto out_free; | |
7335e3bf | 3309 | } else if (ret <= 0 || ret == io_size || !force_nonblock || |
75c668cd | 3310 | (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) { |
7335e3bf | 3311 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3312 | goto done; |
227c0c96 JA |
3313 | } |
3314 | ||
227c0c96 | 3315 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc PB |
3316 | if (ret2) |
3317 | return ret2; | |
3318 | ||
fe1cdd55 | 3319 | iovec = NULL; |
e8c2bc1f | 3320 | rw = req->async_data; |
227c0c96 | 3321 | /* now use our persistent iterator, if we aren't already */ |
e8c2bc1f | 3322 | iter = &rw->iter; |
227c0c96 | 3323 | |
b23df91b PB |
3324 | do { |
3325 | io_size -= ret; | |
3326 | rw->bytes_done += ret; | |
3327 | /* if we can retry, do so with the callbacks armed */ | |
3328 | if (!io_rw_should_retry(req)) { | |
3329 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3330 | return -EAGAIN; | |
3331 | } | |
3332 | ||
3333 | /* | |
3334 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3335 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3336 | * desired page gets unlocked. We can also get a partial read | |
3337 | * here, and if we do, then just retry at the new offset. | |
3338 | */ | |
3339 | ret = io_iter_do_read(req, iter); | |
3340 | if (ret == -EIOCBQUEUED) | |
3341 | return 0; | |
227c0c96 | 3342 | /* we got some bytes, but not all. retry. */ |
b5b0ecb7 | 3343 | kiocb->ki_flags &= ~IOCB_WAITQ; |
b23df91b | 3344 | } while (ret > 0 && ret < io_size); |
227c0c96 | 3345 | done: |
889fca73 | 3346 | kiocb_done(kiocb, ret, issue_flags); |
fe1cdd55 PB |
3347 | out_free: |
3348 | /* it's faster to check here then delegate to kfree */ | |
3349 | if (iovec) | |
3350 | kfree(iovec); | |
5ea5dd45 | 3351 | return 0; |
2b188cc1 JA |
3352 | } |
3353 | ||
73debe68 | 3354 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3355 | { |
3529d8c2 JA |
3356 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3357 | return -EBADF; | |
93642ef8 | 3358 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3359 | } |
3360 | ||
889fca73 | 3361 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3362 | { |
3363 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3364 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3365 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3366 | struct io_async_rw *rw = req->async_data; |
fa15bafb | 3367 | ssize_t ret, ret2, io_size; |
45d189c6 | 3368 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
2b188cc1 | 3369 | |
2846c481 | 3370 | if (rw) { |
e8c2bc1f | 3371 | iter = &rw->iter; |
2846c481 PB |
3372 | iovec = NULL; |
3373 | } else { | |
3374 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
3375 | if (ret < 0) | |
3376 | return ret; | |
3377 | } | |
632546c4 | 3378 | io_size = iov_iter_count(iter); |
fa15bafb | 3379 | req->result = io_size; |
2b188cc1 | 3380 | |
fd6c2e4c JA |
3381 | /* Ensure we clear previously set non-block flag */ |
3382 | if (!force_nonblock) | |
a88fc400 PB |
3383 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3384 | else | |
3385 | kiocb->ki_flags |= IOCB_NOWAIT; | |
fd6c2e4c | 3386 | |
24c74678 | 3387 | /* If the file doesn't support async, just async punt */ |
7b29f92d | 3388 | if (force_nonblock && !io_file_supports_async(req, WRITE)) |
f67676d1 | 3389 | goto copy_iov; |
31b51510 | 3390 | |
10d59345 JA |
3391 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3392 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3393 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3394 | goto copy_iov; |
31b51510 | 3395 | |
632546c4 | 3396 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size); |
fa15bafb PB |
3397 | if (unlikely(ret)) |
3398 | goto out_free; | |
4ed734b0 | 3399 | |
fa15bafb PB |
3400 | /* |
3401 | * Open-code file_start_write here to grab freeze protection, | |
3402 | * which will be released by another thread in | |
3403 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3404 | * released so that it doesn't complain about the held lock when | |
3405 | * we return to userspace. | |
3406 | */ | |
3407 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3408 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3409 | __sb_writers_release(file_inode(req->file)->i_sb, |
3410 | SB_FREEZE_WRITE); | |
3411 | } | |
3412 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3413 | |
fa15bafb | 3414 | if (req->file->f_op->write_iter) |
ff6165b2 | 3415 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3416 | else if (req->file->f_op->write) |
4017eb91 | 3417 | ret2 = loop_rw_iter(WRITE, req, iter); |
2dd2111d GH |
3418 | else |
3419 | ret2 = -EINVAL; | |
4ed734b0 | 3420 | |
6ad7f233 PB |
3421 | if (req->flags & REQ_F_REISSUE) { |
3422 | req->flags &= ~REQ_F_REISSUE; | |
230d50d4 | 3423 | ret2 = -EAGAIN; |
6ad7f233 | 3424 | } |
230d50d4 | 3425 | |
fa15bafb PB |
3426 | /* |
3427 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3428 | * retry them without IOCB_NOWAIT. | |
3429 | */ | |
3430 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3431 | ret2 = -EAGAIN; | |
75c668cd PB |
3432 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3433 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3434 | goto done; |
fa15bafb | 3435 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3436 | /* IOPOLL retry should happen for io-wq threads */ |
3437 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3438 | goto copy_iov; | |
355afaeb | 3439 | done: |
889fca73 | 3440 | kiocb_done(kiocb, ret2, issue_flags); |
fa15bafb | 3441 | } else { |
f67676d1 | 3442 | copy_iov: |
84216315 | 3443 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3444 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
227c0c96 | 3445 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
6bf985dc | 3446 | return ret ?: -EAGAIN; |
2b188cc1 | 3447 | } |
31b51510 | 3448 | out_free: |
f261c168 | 3449 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3450 | if (iovec) |
6f2cc166 | 3451 | kfree(iovec); |
2b188cc1 JA |
3452 | return ret; |
3453 | } | |
3454 | ||
80a261fd JA |
3455 | static int io_renameat_prep(struct io_kiocb *req, |
3456 | const struct io_uring_sqe *sqe) | |
3457 | { | |
3458 | struct io_rename *ren = &req->rename; | |
3459 | const char __user *oldf, *newf; | |
3460 | ||
3461 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3462 | return -EBADF; | |
3463 | ||
3464 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3465 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3466 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3467 | ren->new_dfd = READ_ONCE(sqe->len); | |
3468 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3469 | ||
3470 | ren->oldpath = getname(oldf); | |
3471 | if (IS_ERR(ren->oldpath)) | |
3472 | return PTR_ERR(ren->oldpath); | |
3473 | ||
3474 | ren->newpath = getname(newf); | |
3475 | if (IS_ERR(ren->newpath)) { | |
3476 | putname(ren->oldpath); | |
3477 | return PTR_ERR(ren->newpath); | |
3478 | } | |
3479 | ||
3480 | req->flags |= REQ_F_NEED_CLEANUP; | |
3481 | return 0; | |
3482 | } | |
3483 | ||
45d189c6 | 3484 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
3485 | { |
3486 | struct io_rename *ren = &req->rename; | |
3487 | int ret; | |
3488 | ||
45d189c6 | 3489 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
3490 | return -EAGAIN; |
3491 | ||
3492 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3493 | ren->newpath, ren->flags); | |
3494 | ||
3495 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3496 | if (ret < 0) | |
3497 | req_set_fail_links(req); | |
3498 | io_req_complete(req, ret); | |
3499 | return 0; | |
3500 | } | |
3501 | ||
14a1143b JA |
3502 | static int io_unlinkat_prep(struct io_kiocb *req, |
3503 | const struct io_uring_sqe *sqe) | |
3504 | { | |
3505 | struct io_unlink *un = &req->unlink; | |
3506 | const char __user *fname; | |
3507 | ||
3508 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3509 | return -EBADF; | |
3510 | ||
3511 | un->dfd = READ_ONCE(sqe->fd); | |
3512 | ||
3513 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3514 | if (un->flags & ~AT_REMOVEDIR) | |
3515 | return -EINVAL; | |
3516 | ||
3517 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3518 | un->filename = getname(fname); | |
3519 | if (IS_ERR(un->filename)) | |
3520 | return PTR_ERR(un->filename); | |
3521 | ||
3522 | req->flags |= REQ_F_NEED_CLEANUP; | |
3523 | return 0; | |
3524 | } | |
3525 | ||
45d189c6 | 3526 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
3527 | { |
3528 | struct io_unlink *un = &req->unlink; | |
3529 | int ret; | |
3530 | ||
45d189c6 | 3531 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
3532 | return -EAGAIN; |
3533 | ||
3534 | if (un->flags & AT_REMOVEDIR) | |
3535 | ret = do_rmdir(un->dfd, un->filename); | |
3536 | else | |
3537 | ret = do_unlinkat(un->dfd, un->filename); | |
3538 | ||
3539 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3540 | if (ret < 0) | |
3541 | req_set_fail_links(req); | |
3542 | io_req_complete(req, ret); | |
3543 | return 0; | |
3544 | } | |
3545 | ||
36f4fa68 JA |
3546 | static int io_shutdown_prep(struct io_kiocb *req, |
3547 | const struct io_uring_sqe *sqe) | |
3548 | { | |
3549 | #if defined(CONFIG_NET) | |
3550 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3551 | return -EINVAL; | |
3552 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || | |
3553 | sqe->buf_index) | |
3554 | return -EINVAL; | |
3555 | ||
3556 | req->shutdown.how = READ_ONCE(sqe->len); | |
3557 | return 0; | |
3558 | #else | |
3559 | return -EOPNOTSUPP; | |
3560 | #endif | |
3561 | } | |
3562 | ||
45d189c6 | 3563 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
3564 | { |
3565 | #if defined(CONFIG_NET) | |
3566 | struct socket *sock; | |
3567 | int ret; | |
3568 | ||
45d189c6 | 3569 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
3570 | return -EAGAIN; |
3571 | ||
48aba79b | 3572 | sock = sock_from_file(req->file); |
36f4fa68 | 3573 | if (unlikely(!sock)) |
48aba79b | 3574 | return -ENOTSOCK; |
36f4fa68 JA |
3575 | |
3576 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d JA |
3577 | if (ret < 0) |
3578 | req_set_fail_links(req); | |
36f4fa68 JA |
3579 | io_req_complete(req, ret); |
3580 | return 0; | |
3581 | #else | |
3582 | return -EOPNOTSUPP; | |
3583 | #endif | |
3584 | } | |
3585 | ||
f2a8d5c7 PB |
3586 | static int __io_splice_prep(struct io_kiocb *req, |
3587 | const struct io_uring_sqe *sqe) | |
7d67af2c PB |
3588 | { |
3589 | struct io_splice* sp = &req->splice; | |
3590 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; | |
7d67af2c | 3591 | |
3232dd02 PB |
3592 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3593 | return -EINVAL; | |
7d67af2c PB |
3594 | |
3595 | sp->file_in = NULL; | |
7d67af2c PB |
3596 | sp->len = READ_ONCE(sqe->len); |
3597 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3598 | ||
3599 | if (unlikely(sp->flags & ~valid_flags)) | |
3600 | return -EINVAL; | |
3601 | ||
8371adf5 PB |
3602 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
3603 | (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3604 | if (!sp->file_in) | |
3605 | return -EBADF; | |
7d67af2c | 3606 | req->flags |= REQ_F_NEED_CLEANUP; |
7d67af2c PB |
3607 | return 0; |
3608 | } | |
3609 | ||
f2a8d5c7 PB |
3610 | static int io_tee_prep(struct io_kiocb *req, |
3611 | const struct io_uring_sqe *sqe) | |
3612 | { | |
3613 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3614 | return -EINVAL; | |
3615 | return __io_splice_prep(req, sqe); | |
3616 | } | |
3617 | ||
45d189c6 | 3618 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
3619 | { |
3620 | struct io_splice *sp = &req->splice; | |
3621 | struct file *in = sp->file_in; | |
3622 | struct file *out = sp->file_out; | |
3623 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3624 | long ret = 0; | |
3625 | ||
45d189c6 | 3626 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
3627 | return -EAGAIN; |
3628 | if (sp->len) | |
3629 | ret = do_tee(in, out, sp->len, flags); | |
3630 | ||
e1d767f0 PB |
3631 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
3632 | io_put_file(in); | |
f2a8d5c7 PB |
3633 | req->flags &= ~REQ_F_NEED_CLEANUP; |
3634 | ||
f2a8d5c7 PB |
3635 | if (ret != sp->len) |
3636 | req_set_fail_links(req); | |
e1e16097 | 3637 | io_req_complete(req, ret); |
f2a8d5c7 PB |
3638 | return 0; |
3639 | } | |
3640 | ||
3641 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
3642 | { | |
3643 | struct io_splice* sp = &req->splice; | |
3644 | ||
3645 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
3646 | sp->off_out = READ_ONCE(sqe->off); | |
3647 | return __io_splice_prep(req, sqe); | |
3648 | } | |
3649 | ||
45d189c6 | 3650 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
3651 | { |
3652 | struct io_splice *sp = &req->splice; | |
3653 | struct file *in = sp->file_in; | |
3654 | struct file *out = sp->file_out; | |
3655 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3656 | loff_t *poff_in, *poff_out; | |
c9687426 | 3657 | long ret = 0; |
7d67af2c | 3658 | |
45d189c6 | 3659 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 3660 | return -EAGAIN; |
7d67af2c PB |
3661 | |
3662 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
3663 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 3664 | |
948a7749 | 3665 | if (sp->len) |
c9687426 | 3666 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c | 3667 | |
e1d767f0 PB |
3668 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
3669 | io_put_file(in); | |
7d67af2c PB |
3670 | req->flags &= ~REQ_F_NEED_CLEANUP; |
3671 | ||
7d67af2c PB |
3672 | if (ret != sp->len) |
3673 | req_set_fail_links(req); | |
e1e16097 | 3674 | io_req_complete(req, ret); |
7d67af2c PB |
3675 | return 0; |
3676 | } | |
3677 | ||
2b188cc1 JA |
3678 | /* |
3679 | * IORING_OP_NOP just posts a completion event, nothing else. | |
3680 | */ | |
889fca73 | 3681 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3682 | { |
3683 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 3684 | |
def596e9 JA |
3685 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
3686 | return -EINVAL; | |
3687 | ||
889fca73 | 3688 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
3689 | return 0; |
3690 | } | |
3691 | ||
1155c76a | 3692 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 3693 | { |
6b06314c | 3694 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 3695 | |
09bb8394 JA |
3696 | if (!req->file) |
3697 | return -EBADF; | |
c992fe29 | 3698 | |
6b06314c | 3699 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 3700 | return -EINVAL; |
edafccee | 3701 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
c992fe29 CH |
3702 | return -EINVAL; |
3703 | ||
8ed8d3c3 JA |
3704 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
3705 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
3706 | return -EINVAL; | |
3707 | ||
3708 | req->sync.off = READ_ONCE(sqe->off); | |
3709 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
3710 | return 0; |
3711 | } | |
3712 | ||
45d189c6 | 3713 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 3714 | { |
8ed8d3c3 | 3715 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
3716 | int ret; |
3717 | ||
ac45abc0 | 3718 | /* fsync always requires a blocking context */ |
45d189c6 | 3719 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
3720 | return -EAGAIN; |
3721 | ||
9adbd45d | 3722 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
3723 | end > 0 ? end : LLONG_MAX, |
3724 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
3725 | if (ret < 0) | |
3726 | req_set_fail_links(req); | |
e1e16097 | 3727 | io_req_complete(req, ret); |
c992fe29 CH |
3728 | return 0; |
3729 | } | |
3730 | ||
d63d1b5e JA |
3731 | static int io_fallocate_prep(struct io_kiocb *req, |
3732 | const struct io_uring_sqe *sqe) | |
3733 | { | |
3734 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) | |
3735 | return -EINVAL; | |
3232dd02 PB |
3736 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3737 | return -EINVAL; | |
d63d1b5e JA |
3738 | |
3739 | req->sync.off = READ_ONCE(sqe->off); | |
3740 | req->sync.len = READ_ONCE(sqe->addr); | |
3741 | req->sync.mode = READ_ONCE(sqe->len); | |
3742 | return 0; | |
3743 | } | |
3744 | ||
45d189c6 | 3745 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 3746 | { |
ac45abc0 PB |
3747 | int ret; |
3748 | ||
d63d1b5e | 3749 | /* fallocate always requiring blocking context */ |
45d189c6 | 3750 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 3751 | return -EAGAIN; |
ac45abc0 PB |
3752 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
3753 | req->sync.len); | |
ac45abc0 PB |
3754 | if (ret < 0) |
3755 | req_set_fail_links(req); | |
e1e16097 | 3756 | io_req_complete(req, ret); |
5d17b4a4 JA |
3757 | return 0; |
3758 | } | |
3759 | ||
ec65fea5 | 3760 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 3761 | { |
f8748881 | 3762 | const char __user *fname; |
15b71abe | 3763 | int ret; |
b7bb4f7d | 3764 | |
ec65fea5 | 3765 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 3766 | return -EINVAL; |
ec65fea5 | 3767 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 3768 | return -EBADF; |
03b1230c | 3769 | |
ec65fea5 PB |
3770 | /* open.how should be already initialised */ |
3771 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 3772 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 3773 | |
25e72d10 PB |
3774 | req->open.dfd = READ_ONCE(sqe->fd); |
3775 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 3776 | req->open.filename = getname(fname); |
15b71abe JA |
3777 | if (IS_ERR(req->open.filename)) { |
3778 | ret = PTR_ERR(req->open.filename); | |
3779 | req->open.filename = NULL; | |
3780 | return ret; | |
3781 | } | |
4022e7af | 3782 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 3783 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 3784 | return 0; |
03b1230c JA |
3785 | } |
3786 | ||
ec65fea5 PB |
3787 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
3788 | { | |
3789 | u64 flags, mode; | |
3790 | ||
14587a46 | 3791 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 3792 | return -EINVAL; |
ec65fea5 PB |
3793 | mode = READ_ONCE(sqe->len); |
3794 | flags = READ_ONCE(sqe->open_flags); | |
3795 | req->open.how = build_open_how(flags, mode); | |
3796 | return __io_openat_prep(req, sqe); | |
3797 | } | |
3798 | ||
cebdb986 | 3799 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 3800 | { |
cebdb986 | 3801 | struct open_how __user *how; |
cebdb986 | 3802 | size_t len; |
0fa03c62 JA |
3803 | int ret; |
3804 | ||
14587a46 | 3805 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 3806 | return -EINVAL; |
cebdb986 JA |
3807 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
3808 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
3809 | if (len < OPEN_HOW_SIZE_VER0) |
3810 | return -EINVAL; | |
3529d8c2 | 3811 | |
cebdb986 JA |
3812 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
3813 | len); | |
3814 | if (ret) | |
3815 | return ret; | |
3529d8c2 | 3816 | |
ec65fea5 | 3817 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
3818 | } |
3819 | ||
45d189c6 | 3820 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
3821 | { |
3822 | struct open_flags op; | |
15b71abe | 3823 | struct file *file; |
3a81fd02 JA |
3824 | bool nonblock_set; |
3825 | bool resolve_nonblock; | |
15b71abe JA |
3826 | int ret; |
3827 | ||
cebdb986 | 3828 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
3829 | if (ret) |
3830 | goto err; | |
3a81fd02 JA |
3831 | nonblock_set = op.open_flag & O_NONBLOCK; |
3832 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 3833 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
3834 | /* |
3835 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
3836 | * it'll always -EAGAIN | |
3837 | */ | |
3838 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
3839 | return -EAGAIN; | |
3840 | op.lookup_flags |= LOOKUP_CACHED; | |
3841 | op.open_flag |= O_NONBLOCK; | |
3842 | } | |
15b71abe | 3843 | |
4022e7af | 3844 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
15b71abe JA |
3845 | if (ret < 0) |
3846 | goto err; | |
3847 | ||
3848 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
3a81fd02 | 3849 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
45d189c6 PB |
3850 | if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) && |
3851 | file == ERR_PTR(-EAGAIN)) { | |
944d1444 | 3852 | /* |
3a81fd02 JA |
3853 | * We could hang on to this 'fd', but seems like marginal |
3854 | * gain for something that is now known to be a slower path. | |
3855 | * So just put it, and we'll get a new one when we retry. | |
944d1444 | 3856 | */ |
3a81fd02 JA |
3857 | put_unused_fd(ret); |
3858 | return -EAGAIN; | |
3859 | } | |
3860 | ||
15b71abe JA |
3861 | if (IS_ERR(file)) { |
3862 | put_unused_fd(ret); | |
3863 | ret = PTR_ERR(file); | |
3864 | } else { | |
45d189c6 | 3865 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
3a81fd02 | 3866 | file->f_flags &= ~O_NONBLOCK; |
15b71abe JA |
3867 | fsnotify_open(file); |
3868 | fd_install(ret, file); | |
3869 | } | |
3870 | err: | |
3871 | putname(req->open.filename); | |
8fef80bf | 3872 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe JA |
3873 | if (ret < 0) |
3874 | req_set_fail_links(req); | |
0bdf3398 | 3875 | __io_req_complete(req, issue_flags, ret, 0); |
15b71abe JA |
3876 | return 0; |
3877 | } | |
3878 | ||
45d189c6 | 3879 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 3880 | { |
e45cff58 | 3881 | return io_openat2(req, issue_flags); |
cebdb986 JA |
3882 | } |
3883 | ||
067524e9 JA |
3884 | static int io_remove_buffers_prep(struct io_kiocb *req, |
3885 | const struct io_uring_sqe *sqe) | |
3886 | { | |
3887 | struct io_provide_buf *p = &req->pbuf; | |
3888 | u64 tmp; | |
3889 | ||
3890 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) | |
3891 | return -EINVAL; | |
3892 | ||
3893 | tmp = READ_ONCE(sqe->fd); | |
3894 | if (!tmp || tmp > USHRT_MAX) | |
3895 | return -EINVAL; | |
3896 | ||
3897 | memset(p, 0, sizeof(*p)); | |
3898 | p->nbufs = tmp; | |
3899 | p->bgid = READ_ONCE(sqe->buf_group); | |
3900 | return 0; | |
3901 | } | |
3902 | ||
3903 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
3904 | int bgid, unsigned nbufs) | |
3905 | { | |
3906 | unsigned i = 0; | |
3907 | ||
3908 | /* shouldn't happen */ | |
3909 | if (!nbufs) | |
3910 | return 0; | |
3911 | ||
3912 | /* the head kbuf is the list itself */ | |
3913 | while (!list_empty(&buf->list)) { | |
3914 | struct io_buffer *nxt; | |
3915 | ||
3916 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
3917 | list_del(&nxt->list); | |
3918 | kfree(nxt); | |
3919 | if (++i == nbufs) | |
3920 | return i; | |
3921 | } | |
3922 | i++; | |
3923 | kfree(buf); | |
9e15c3a0 | 3924 | xa_erase(&ctx->io_buffers, bgid); |
067524e9 JA |
3925 | |
3926 | return i; | |
3927 | } | |
3928 | ||
889fca73 | 3929 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
3930 | { |
3931 | struct io_provide_buf *p = &req->pbuf; | |
3932 | struct io_ring_ctx *ctx = req->ctx; | |
3933 | struct io_buffer *head; | |
3934 | int ret = 0; | |
45d189c6 | 3935 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
067524e9 JA |
3936 | |
3937 | io_ring_submit_lock(ctx, !force_nonblock); | |
3938 | ||
3939 | lockdep_assert_held(&ctx->uring_lock); | |
3940 | ||
3941 | ret = -ENOENT; | |
9e15c3a0 | 3942 | head = xa_load(&ctx->io_buffers, p->bgid); |
067524e9 JA |
3943 | if (head) |
3944 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 JA |
3945 | if (ret < 0) |
3946 | req_set_fail_links(req); | |
067524e9 | 3947 | |
9fb8cb49 PB |
3948 | /* complete before unlock, IOPOLL may need the lock */ |
3949 | __io_req_complete(req, issue_flags, ret, 0); | |
3950 | io_ring_submit_unlock(ctx, !force_nonblock); | |
067524e9 JA |
3951 | return 0; |
3952 | } | |
3953 | ||
ddf0322d JA |
3954 | static int io_provide_buffers_prep(struct io_kiocb *req, |
3955 | const struct io_uring_sqe *sqe) | |
3956 | { | |
d81269fe | 3957 | unsigned long size; |
ddf0322d JA |
3958 | struct io_provide_buf *p = &req->pbuf; |
3959 | u64 tmp; | |
3960 | ||
3961 | if (sqe->ioprio || sqe->rw_flags) | |
3962 | return -EINVAL; | |
3963 | ||
3964 | tmp = READ_ONCE(sqe->fd); | |
3965 | if (!tmp || tmp > USHRT_MAX) | |
3966 | return -E2BIG; | |
3967 | p->nbufs = tmp; | |
3968 | p->addr = READ_ONCE(sqe->addr); | |
3969 | p->len = READ_ONCE(sqe->len); | |
3970 | ||
d81269fe PB |
3971 | size = (unsigned long)p->len * p->nbufs; |
3972 | if (!access_ok(u64_to_user_ptr(p->addr), size)) | |
ddf0322d JA |
3973 | return -EFAULT; |
3974 | ||
3975 | p->bgid = READ_ONCE(sqe->buf_group); | |
3976 | tmp = READ_ONCE(sqe->off); | |
3977 | if (tmp > USHRT_MAX) | |
3978 | return -E2BIG; | |
3979 | p->bid = tmp; | |
3980 | return 0; | |
3981 | } | |
3982 | ||
3983 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
3984 | { | |
3985 | struct io_buffer *buf; | |
3986 | u64 addr = pbuf->addr; | |
3987 | int i, bid = pbuf->bid; | |
3988 | ||
3989 | for (i = 0; i < pbuf->nbufs; i++) { | |
3990 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); | |
3991 | if (!buf) | |
3992 | break; | |
3993 | ||
3994 | buf->addr = addr; | |
3995 | buf->len = pbuf->len; | |
3996 | buf->bid = bid; | |
3997 | addr += pbuf->len; | |
3998 | bid++; | |
3999 | if (!*head) { | |
4000 | INIT_LIST_HEAD(&buf->list); | |
4001 | *head = buf; | |
4002 | } else { | |
4003 | list_add_tail(&buf->list, &(*head)->list); | |
4004 | } | |
4005 | } | |
4006 | ||
4007 | return i ? i : -ENOMEM; | |
4008 | } | |
4009 | ||
889fca73 | 4010 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4011 | { |
4012 | struct io_provide_buf *p = &req->pbuf; | |
4013 | struct io_ring_ctx *ctx = req->ctx; | |
4014 | struct io_buffer *head, *list; | |
4015 | int ret = 0; | |
45d189c6 | 4016 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ddf0322d JA |
4017 | |
4018 | io_ring_submit_lock(ctx, !force_nonblock); | |
4019 | ||
4020 | lockdep_assert_held(&ctx->uring_lock); | |
4021 | ||
9e15c3a0 | 4022 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
ddf0322d JA |
4023 | |
4024 | ret = io_add_buffers(p, &head); | |
9e15c3a0 JA |
4025 | if (ret >= 0 && !list) { |
4026 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); | |
4027 | if (ret < 0) | |
067524e9 | 4028 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d | 4029 | } |
ddf0322d JA |
4030 | if (ret < 0) |
4031 | req_set_fail_links(req); | |
9fb8cb49 PB |
4032 | /* complete before unlock, IOPOLL may need the lock */ |
4033 | __io_req_complete(req, issue_flags, ret, 0); | |
4034 | io_ring_submit_unlock(ctx, !force_nonblock); | |
ddf0322d | 4035 | return 0; |
cebdb986 JA |
4036 | } |
4037 | ||
3e4827b0 JA |
4038 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4039 | const struct io_uring_sqe *sqe) | |
4040 | { | |
4041 | #if defined(CONFIG_EPOLL) | |
4042 | if (sqe->ioprio || sqe->buf_index) | |
4043 | return -EINVAL; | |
6ca56f84 | 4044 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4045 | return -EINVAL; |
3e4827b0 JA |
4046 | |
4047 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4048 | req->epoll.op = READ_ONCE(sqe->len); | |
4049 | req->epoll.fd = READ_ONCE(sqe->off); | |
4050 | ||
4051 | if (ep_op_has_event(req->epoll.op)) { | |
4052 | struct epoll_event __user *ev; | |
4053 | ||
4054 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4055 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4056 | return -EFAULT; | |
4057 | } | |
4058 | ||
4059 | return 0; | |
4060 | #else | |
4061 | return -EOPNOTSUPP; | |
4062 | #endif | |
4063 | } | |
4064 | ||
889fca73 | 4065 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4066 | { |
4067 | #if defined(CONFIG_EPOLL) | |
4068 | struct io_epoll *ie = &req->epoll; | |
4069 | int ret; | |
45d189c6 | 4070 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4071 | |
4072 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4073 | if (force_nonblock && ret == -EAGAIN) | |
4074 | return -EAGAIN; | |
4075 | ||
4076 | if (ret < 0) | |
4077 | req_set_fail_links(req); | |
889fca73 | 4078 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4079 | return 0; |
4080 | #else | |
4081 | return -EOPNOTSUPP; | |
4082 | #endif | |
4083 | } | |
4084 | ||
c1ca757b JA |
4085 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4086 | { | |
4087 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4088 | if (sqe->ioprio || sqe->buf_index || sqe->off) | |
4089 | return -EINVAL; | |
3232dd02 PB |
4090 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4091 | return -EINVAL; | |
c1ca757b JA |
4092 | |
4093 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4094 | req->madvise.len = READ_ONCE(sqe->len); | |
4095 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4096 | return 0; | |
4097 | #else | |
4098 | return -EOPNOTSUPP; | |
4099 | #endif | |
4100 | } | |
4101 | ||
45d189c6 | 4102 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4103 | { |
4104 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4105 | struct io_madvise *ma = &req->madvise; | |
4106 | int ret; | |
4107 | ||
45d189c6 | 4108 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4109 | return -EAGAIN; |
4110 | ||
0726b01e | 4111 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b JA |
4112 | if (ret < 0) |
4113 | req_set_fail_links(req); | |
e1e16097 | 4114 | io_req_complete(req, ret); |
c1ca757b JA |
4115 | return 0; |
4116 | #else | |
4117 | return -EOPNOTSUPP; | |
4118 | #endif | |
4119 | } | |
4120 | ||
4840e418 JA |
4121 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4122 | { | |
4123 | if (sqe->ioprio || sqe->buf_index || sqe->addr) | |
4124 | return -EINVAL; | |
3232dd02 PB |
4125 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4126 | return -EINVAL; | |
4840e418 JA |
4127 | |
4128 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4129 | req->fadvise.len = READ_ONCE(sqe->len); | |
4130 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4131 | return 0; | |
4132 | } | |
4133 | ||
45d189c6 | 4134 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4135 | { |
4136 | struct io_fadvise *fa = &req->fadvise; | |
4137 | int ret; | |
4138 | ||
45d189c6 | 4139 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4140 | switch (fa->advice) { |
4141 | case POSIX_FADV_NORMAL: | |
4142 | case POSIX_FADV_RANDOM: | |
4143 | case POSIX_FADV_SEQUENTIAL: | |
4144 | break; | |
4145 | default: | |
4146 | return -EAGAIN; | |
4147 | } | |
4148 | } | |
4840e418 JA |
4149 | |
4150 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4151 | if (ret < 0) | |
4152 | req_set_fail_links(req); | |
0bdf3398 | 4153 | __io_req_complete(req, issue_flags, ret, 0); |
4840e418 JA |
4154 | return 0; |
4155 | } | |
4156 | ||
eddc7ef5 JA |
4157 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4158 | { | |
6ca56f84 | 4159 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4160 | return -EINVAL; |
eddc7ef5 JA |
4161 | if (sqe->ioprio || sqe->buf_index) |
4162 | return -EINVAL; | |
9c280f90 | 4163 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4164 | return -EBADF; |
eddc7ef5 | 4165 | |
1d9e1288 BM |
4166 | req->statx.dfd = READ_ONCE(sqe->fd); |
4167 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4168 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4169 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4170 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4171 | |
4172 | return 0; | |
4173 | } | |
4174 | ||
45d189c6 | 4175 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4176 | { |
1d9e1288 | 4177 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4178 | int ret; |
4179 | ||
59d70013 | 4180 | if (issue_flags & IO_URING_F_NONBLOCK) |
eddc7ef5 JA |
4181 | return -EAGAIN; |
4182 | ||
e62753e4 BM |
4183 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4184 | ctx->buffer); | |
eddc7ef5 | 4185 | |
eddc7ef5 JA |
4186 | if (ret < 0) |
4187 | req_set_fail_links(req); | |
e1e16097 | 4188 | io_req_complete(req, ret); |
eddc7ef5 JA |
4189 | return 0; |
4190 | } | |
4191 | ||
b5dba59e JA |
4192 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4193 | { | |
14587a46 | 4194 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4195 | return -EINVAL; |
b5dba59e JA |
4196 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
4197 | sqe->rw_flags || sqe->buf_index) | |
4198 | return -EINVAL; | |
9c280f90 | 4199 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4200 | return -EBADF; |
b5dba59e JA |
4201 | |
4202 | req->close.fd = READ_ONCE(sqe->fd); | |
b5dba59e | 4203 | return 0; |
b5dba59e JA |
4204 | } |
4205 | ||
889fca73 | 4206 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4207 | { |
9eac1904 | 4208 | struct files_struct *files = current->files; |
3af73b28 | 4209 | struct io_close *close = &req->close; |
9eac1904 | 4210 | struct fdtable *fdt; |
a1fde923 PB |
4211 | struct file *file = NULL; |
4212 | int ret = -EBADF; | |
b5dba59e | 4213 | |
9eac1904 JA |
4214 | spin_lock(&files->file_lock); |
4215 | fdt = files_fdtable(files); | |
4216 | if (close->fd >= fdt->max_fds) { | |
4217 | spin_unlock(&files->file_lock); | |
4218 | goto err; | |
4219 | } | |
4220 | file = fdt->fd[close->fd]; | |
a1fde923 | 4221 | if (!file || file->f_op == &io_uring_fops) { |
9eac1904 JA |
4222 | spin_unlock(&files->file_lock); |
4223 | file = NULL; | |
4224 | goto err; | |
3af73b28 | 4225 | } |
b5dba59e JA |
4226 | |
4227 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 4228 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 4229 | spin_unlock(&files->file_lock); |
0bf0eefd | 4230 | return -EAGAIN; |
a2100672 | 4231 | } |
b5dba59e | 4232 | |
9eac1904 JA |
4233 | ret = __close_fd_get_file(close->fd, &file); |
4234 | spin_unlock(&files->file_lock); | |
4235 | if (ret < 0) { | |
4236 | if (ret == -ENOENT) | |
4237 | ret = -EBADF; | |
4238 | goto err; | |
4239 | } | |
4240 | ||
3af73b28 | 4241 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4242 | ret = filp_close(file, current->files); |
4243 | err: | |
3af73b28 PB |
4244 | if (ret < 0) |
4245 | req_set_fail_links(req); | |
9eac1904 JA |
4246 | if (file) |
4247 | fput(file); | |
889fca73 | 4248 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 4249 | return 0; |
b5dba59e JA |
4250 | } |
4251 | ||
1155c76a | 4252 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4253 | { |
4254 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 4255 | |
5d17b4a4 JA |
4256 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4257 | return -EINVAL; | |
4258 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) | |
4259 | return -EINVAL; | |
4260 | ||
8ed8d3c3 JA |
4261 | req->sync.off = READ_ONCE(sqe->off); |
4262 | req->sync.len = READ_ONCE(sqe->len); | |
4263 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4264 | return 0; |
4265 | } | |
4266 | ||
45d189c6 | 4267 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4268 | { |
8ed8d3c3 JA |
4269 | int ret; |
4270 | ||
ac45abc0 | 4271 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 4272 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4273 | return -EAGAIN; |
4274 | ||
9adbd45d | 4275 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4276 | req->sync.flags); |
4277 | if (ret < 0) | |
4278 | req_set_fail_links(req); | |
e1e16097 | 4279 | io_req_complete(req, ret); |
5d17b4a4 JA |
4280 | return 0; |
4281 | } | |
4282 | ||
469956e8 | 4283 | #if defined(CONFIG_NET) |
02d27d89 PB |
4284 | static int io_setup_async_msg(struct io_kiocb *req, |
4285 | struct io_async_msghdr *kmsg) | |
4286 | { | |
e8c2bc1f JA |
4287 | struct io_async_msghdr *async_msg = req->async_data; |
4288 | ||
4289 | if (async_msg) | |
02d27d89 | 4290 | return -EAGAIN; |
e8c2bc1f | 4291 | if (io_alloc_async_data(req)) { |
257e84a5 | 4292 | kfree(kmsg->free_iov); |
02d27d89 PB |
4293 | return -ENOMEM; |
4294 | } | |
e8c2bc1f | 4295 | async_msg = req->async_data; |
02d27d89 | 4296 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4297 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 4298 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
4299 | /* if were using fast_iov, set it to the new one */ |
4300 | if (!async_msg->free_iov) | |
4301 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
4302 | ||
02d27d89 PB |
4303 | return -EAGAIN; |
4304 | } | |
4305 | ||
2ae523ed PB |
4306 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4307 | struct io_async_msghdr *iomsg) | |
4308 | { | |
2ae523ed | 4309 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 4310 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 4311 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 4312 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
4313 | } |
4314 | ||
93642ef8 PB |
4315 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
4316 | { | |
4317 | int ret; | |
4318 | ||
93642ef8 PB |
4319 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
4320 | if (!ret) | |
4321 | req->flags |= REQ_F_NEED_CLEANUP; | |
4322 | return ret; | |
4323 | } | |
4324 | ||
3529d8c2 | 4325 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4326 | { |
e47293fd | 4327 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 4328 | |
d2b6f48b PB |
4329 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4330 | return -EINVAL; | |
4331 | ||
270a5940 | 4332 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4333 | sr->len = READ_ONCE(sqe->len); |
04411806 PB |
4334 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4335 | if (sr->msg_flags & MSG_DONTWAIT) | |
4336 | req->flags |= REQ_F_NOWAIT; | |
3529d8c2 | 4337 | |
d8768362 JA |
4338 | #ifdef CONFIG_COMPAT |
4339 | if (req->ctx->compat) | |
4340 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4341 | #endif | |
93642ef8 | 4342 | return 0; |
03b1230c JA |
4343 | } |
4344 | ||
889fca73 | 4345 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4346 | { |
6b754c8b | 4347 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4348 | struct socket *sock; |
7a7cacba | 4349 | unsigned flags; |
0031275d | 4350 | int min_ret = 0; |
0fa03c62 JA |
4351 | int ret; |
4352 | ||
dba4a925 | 4353 | sock = sock_from_file(req->file); |
7a7cacba | 4354 | if (unlikely(!sock)) |
dba4a925 | 4355 | return -ENOTSOCK; |
3529d8c2 | 4356 | |
257e84a5 PB |
4357 | kmsg = req->async_data; |
4358 | if (!kmsg) { | |
7a7cacba PB |
4359 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
4360 | if (ret) | |
4361 | return ret; | |
4362 | kmsg = &iomsg; | |
0fa03c62 | 4363 | } |
0fa03c62 | 4364 | |
04411806 PB |
4365 | flags = req->sr_msg.msg_flags; |
4366 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4367 | flags |= MSG_DONTWAIT; |
0031275d SM |
4368 | if (flags & MSG_WAITALL) |
4369 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4370 | ||
7a7cacba | 4371 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
45d189c6 | 4372 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4373 | return io_setup_async_msg(req, kmsg); |
4374 | if (ret == -ERESTARTSYS) | |
4375 | ret = -EINTR; | |
0fa03c62 | 4376 | |
257e84a5 PB |
4377 | /* fast path, check for non-NULL to avoid function call */ |
4378 | if (kmsg->free_iov) | |
4379 | kfree(kmsg->free_iov); | |
99bc4c38 | 4380 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 4381 | if (ret < min_ret) |
4e88d6e7 | 4382 | req_set_fail_links(req); |
889fca73 | 4383 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 4384 | return 0; |
03b1230c | 4385 | } |
aa1fa28f | 4386 | |
889fca73 | 4387 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4388 | { |
7a7cacba PB |
4389 | struct io_sr_msg *sr = &req->sr_msg; |
4390 | struct msghdr msg; | |
4391 | struct iovec iov; | |
fddaface | 4392 | struct socket *sock; |
7a7cacba | 4393 | unsigned flags; |
0031275d | 4394 | int min_ret = 0; |
fddaface JA |
4395 | int ret; |
4396 | ||
dba4a925 | 4397 | sock = sock_from_file(req->file); |
7a7cacba | 4398 | if (unlikely(!sock)) |
dba4a925 | 4399 | return -ENOTSOCK; |
fddaface | 4400 | |
7a7cacba PB |
4401 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4402 | if (unlikely(ret)) | |
14db8411 | 4403 | return ret; |
fddaface | 4404 | |
7a7cacba PB |
4405 | msg.msg_name = NULL; |
4406 | msg.msg_control = NULL; | |
4407 | msg.msg_controllen = 0; | |
4408 | msg.msg_namelen = 0; | |
fddaface | 4409 | |
04411806 PB |
4410 | flags = req->sr_msg.msg_flags; |
4411 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4412 | flags |= MSG_DONTWAIT; |
0031275d SM |
4413 | if (flags & MSG_WAITALL) |
4414 | min_ret = iov_iter_count(&msg.msg_iter); | |
4415 | ||
7a7cacba PB |
4416 | msg.msg_flags = flags; |
4417 | ret = sock_sendmsg(sock, &msg); | |
45d189c6 | 4418 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4419 | return -EAGAIN; |
4420 | if (ret == -ERESTARTSYS) | |
4421 | ret = -EINTR; | |
fddaface | 4422 | |
0031275d | 4423 | if (ret < min_ret) |
fddaface | 4424 | req_set_fail_links(req); |
889fca73 | 4425 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 4426 | return 0; |
fddaface JA |
4427 | } |
4428 | ||
1400e697 PB |
4429 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4430 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4431 | { |
4432 | struct io_sr_msg *sr = &req->sr_msg; | |
4433 | struct iovec __user *uiov; | |
4434 | size_t iov_len; | |
4435 | int ret; | |
4436 | ||
1400e697 PB |
4437 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4438 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4439 | if (ret) |
4440 | return ret; | |
4441 | ||
4442 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4443 | if (iov_len > 1) | |
4444 | return -EINVAL; | |
5476dfed | 4445 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4446 | return -EFAULT; |
5476dfed | 4447 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 4448 | iomsg->free_iov = NULL; |
52de1fe1 | 4449 | } else { |
257e84a5 | 4450 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4451 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 4452 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 4453 | false); |
52de1fe1 JA |
4454 | if (ret > 0) |
4455 | ret = 0; | |
4456 | } | |
4457 | ||
4458 | return ret; | |
4459 | } | |
4460 | ||
4461 | #ifdef CONFIG_COMPAT | |
4462 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4463 | struct io_async_msghdr *iomsg) |
52de1fe1 | 4464 | { |
52de1fe1 JA |
4465 | struct io_sr_msg *sr = &req->sr_msg; |
4466 | struct compat_iovec __user *uiov; | |
4467 | compat_uptr_t ptr; | |
4468 | compat_size_t len; | |
4469 | int ret; | |
4470 | ||
4af3417a PB |
4471 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
4472 | &ptr, &len); | |
52de1fe1 JA |
4473 | if (ret) |
4474 | return ret; | |
4475 | ||
4476 | uiov = compat_ptr(ptr); | |
4477 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4478 | compat_ssize_t clen; | |
4479 | ||
4480 | if (len > 1) | |
4481 | return -EINVAL; | |
4482 | if (!access_ok(uiov, sizeof(*uiov))) | |
4483 | return -EFAULT; | |
4484 | if (__get_user(clen, &uiov->iov_len)) | |
4485 | return -EFAULT; | |
4486 | if (clen < 0) | |
4487 | return -EINVAL; | |
2d280bc8 | 4488 | sr->len = clen; |
257e84a5 | 4489 | iomsg->free_iov = NULL; |
52de1fe1 | 4490 | } else { |
257e84a5 | 4491 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4492 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 4493 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 4494 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
4495 | if (ret < 0) |
4496 | return ret; | |
4497 | } | |
4498 | ||
4499 | return 0; | |
4500 | } | |
4501 | #endif | |
4502 | ||
1400e697 PB |
4503 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4504 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4505 | { |
1400e697 | 4506 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
4507 | |
4508 | #ifdef CONFIG_COMPAT | |
4509 | if (req->ctx->compat) | |
1400e697 | 4510 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4511 | #endif |
52de1fe1 | 4512 | |
1400e697 | 4513 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4514 | } |
4515 | ||
bcda7baa | 4516 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4517 | bool needs_lock) |
bcda7baa JA |
4518 | { |
4519 | struct io_sr_msg *sr = &req->sr_msg; | |
4520 | struct io_buffer *kbuf; | |
4521 | ||
bcda7baa JA |
4522 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
4523 | if (IS_ERR(kbuf)) | |
4524 | return kbuf; | |
4525 | ||
4526 | sr->kbuf = kbuf; | |
4527 | req->flags |= REQ_F_BUFFER_SELECTED; | |
bcda7baa | 4528 | return kbuf; |
fddaface JA |
4529 | } |
4530 | ||
7fbb1b54 PB |
4531 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4532 | { | |
4533 | return io_put_kbuf(req, req->sr_msg.kbuf); | |
4534 | } | |
4535 | ||
93642ef8 | 4536 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 4537 | { |
99bc4c38 | 4538 | int ret; |
3529d8c2 | 4539 | |
93642ef8 PB |
4540 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
4541 | if (!ret) | |
4542 | req->flags |= REQ_F_NEED_CLEANUP; | |
4543 | return ret; | |
4544 | } | |
4545 | ||
4546 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4547 | { | |
4548 | struct io_sr_msg *sr = &req->sr_msg; | |
4549 | ||
d2b6f48b PB |
4550 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4551 | return -EINVAL; | |
4552 | ||
270a5940 | 4553 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4554 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4555 | sr->bgid = READ_ONCE(sqe->buf_group); |
04411806 PB |
4556 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4557 | if (sr->msg_flags & MSG_DONTWAIT) | |
4558 | req->flags |= REQ_F_NOWAIT; | |
06b76d44 | 4559 | |
d8768362 JA |
4560 | #ifdef CONFIG_COMPAT |
4561 | if (req->ctx->compat) | |
4562 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4563 | #endif | |
93642ef8 | 4564 | return 0; |
aa1fa28f JA |
4565 | } |
4566 | ||
889fca73 | 4567 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4568 | { |
6b754c8b | 4569 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4570 | struct socket *sock; |
7fbb1b54 | 4571 | struct io_buffer *kbuf; |
7a7cacba | 4572 | unsigned flags; |
0031275d | 4573 | int min_ret = 0; |
52de1fe1 | 4574 | int ret, cflags = 0; |
45d189c6 | 4575 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 4576 | |
dba4a925 | 4577 | sock = sock_from_file(req->file); |
7a7cacba | 4578 | if (unlikely(!sock)) |
dba4a925 | 4579 | return -ENOTSOCK; |
3529d8c2 | 4580 | |
257e84a5 PB |
4581 | kmsg = req->async_data; |
4582 | if (!kmsg) { | |
7a7cacba PB |
4583 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
4584 | if (ret) | |
681fda8d | 4585 | return ret; |
7a7cacba PB |
4586 | kmsg = &iomsg; |
4587 | } | |
03b1230c | 4588 | |
bc02ef33 | 4589 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4590 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4591 | if (IS_ERR(kbuf)) |
52de1fe1 | 4592 | return PTR_ERR(kbuf); |
7a7cacba | 4593 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
4594 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
4595 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
4596 | 1, req->sr_msg.len); |
4597 | } | |
52de1fe1 | 4598 | |
04411806 PB |
4599 | flags = req->sr_msg.msg_flags; |
4600 | if (force_nonblock) | |
7a7cacba | 4601 | flags |= MSG_DONTWAIT; |
0031275d SM |
4602 | if (flags & MSG_WAITALL) |
4603 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4604 | ||
7a7cacba PB |
4605 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4606 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4607 | if (force_nonblock && ret == -EAGAIN) |
4608 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4609 | if (ret == -ERESTARTSYS) |
4610 | ret = -EINTR; | |
03b1230c | 4611 | |
7fbb1b54 PB |
4612 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4613 | cflags = io_put_recv_kbuf(req); | |
257e84a5 PB |
4614 | /* fast path, check for non-NULL to avoid function call */ |
4615 | if (kmsg->free_iov) | |
4616 | kfree(kmsg->free_iov); | |
99bc4c38 | 4617 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 4618 | if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
4e88d6e7 | 4619 | req_set_fail_links(req); |
889fca73 | 4620 | __io_req_complete(req, issue_flags, ret, cflags); |
03b1230c | 4621 | return 0; |
0fa03c62 | 4622 | } |
5d17b4a4 | 4623 | |
889fca73 | 4624 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4625 | { |
6b754c8b | 4626 | struct io_buffer *kbuf; |
7a7cacba PB |
4627 | struct io_sr_msg *sr = &req->sr_msg; |
4628 | struct msghdr msg; | |
4629 | void __user *buf = sr->buf; | |
fddaface | 4630 | struct socket *sock; |
7a7cacba PB |
4631 | struct iovec iov; |
4632 | unsigned flags; | |
0031275d | 4633 | int min_ret = 0; |
bcda7baa | 4634 | int ret, cflags = 0; |
45d189c6 | 4635 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 4636 | |
dba4a925 | 4637 | sock = sock_from_file(req->file); |
7a7cacba | 4638 | if (unlikely(!sock)) |
dba4a925 | 4639 | return -ENOTSOCK; |
fddaface | 4640 | |
bc02ef33 | 4641 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4642 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
4643 | if (IS_ERR(kbuf)) |
4644 | return PTR_ERR(kbuf); | |
7a7cacba | 4645 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 4646 | } |
bcda7baa | 4647 | |
7a7cacba | 4648 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
4649 | if (unlikely(ret)) |
4650 | goto out_free; | |
fddaface | 4651 | |
7a7cacba PB |
4652 | msg.msg_name = NULL; |
4653 | msg.msg_control = NULL; | |
4654 | msg.msg_controllen = 0; | |
4655 | msg.msg_namelen = 0; | |
4656 | msg.msg_iocb = NULL; | |
4657 | msg.msg_flags = 0; | |
fddaface | 4658 | |
04411806 PB |
4659 | flags = req->sr_msg.msg_flags; |
4660 | if (force_nonblock) | |
7a7cacba | 4661 | flags |= MSG_DONTWAIT; |
0031275d SM |
4662 | if (flags & MSG_WAITALL) |
4663 | min_ret = iov_iter_count(&msg.msg_iter); | |
4664 | ||
7a7cacba PB |
4665 | ret = sock_recvmsg(sock, &msg, flags); |
4666 | if (force_nonblock && ret == -EAGAIN) | |
4667 | return -EAGAIN; | |
4668 | if (ret == -ERESTARTSYS) | |
4669 | ret = -EINTR; | |
14c32eee | 4670 | out_free: |
7fbb1b54 PB |
4671 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4672 | cflags = io_put_recv_kbuf(req); | |
0031275d | 4673 | if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
fddaface | 4674 | req_set_fail_links(req); |
889fca73 | 4675 | __io_req_complete(req, issue_flags, ret, cflags); |
fddaface | 4676 | return 0; |
fddaface JA |
4677 | } |
4678 | ||
3529d8c2 | 4679 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 4680 | { |
8ed8d3c3 JA |
4681 | struct io_accept *accept = &req->accept; |
4682 | ||
14587a46 | 4683 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 4684 | return -EINVAL; |
8042d6ce | 4685 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
4686 | return -EINVAL; |
4687 | ||
d55e5f5b JA |
4688 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4689 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 4690 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 4691 | accept->nofile = rlimit(RLIMIT_NOFILE); |
8ed8d3c3 | 4692 | return 0; |
8ed8d3c3 | 4693 | } |
17f2fe35 | 4694 | |
889fca73 | 4695 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
4696 | { |
4697 | struct io_accept *accept = &req->accept; | |
45d189c6 | 4698 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 4699 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
8ed8d3c3 JA |
4700 | int ret; |
4701 | ||
e697deed JX |
4702 | if (req->file->f_flags & O_NONBLOCK) |
4703 | req->flags |= REQ_F_NOWAIT; | |
4704 | ||
8ed8d3c3 | 4705 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
09952e3e JA |
4706 | accept->addr_len, accept->flags, |
4707 | accept->nofile); | |
8ed8d3c3 | 4708 | if (ret == -EAGAIN && force_nonblock) |
17f2fe35 | 4709 | return -EAGAIN; |
ac45abc0 PB |
4710 | if (ret < 0) { |
4711 | if (ret == -ERESTARTSYS) | |
4712 | ret = -EINTR; | |
4e88d6e7 | 4713 | req_set_fail_links(req); |
ac45abc0 | 4714 | } |
889fca73 | 4715 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 4716 | return 0; |
8ed8d3c3 JA |
4717 | } |
4718 | ||
93642ef8 PB |
4719 | static int io_connect_prep_async(struct io_kiocb *req) |
4720 | { | |
4721 | struct io_async_connect *io = req->async_data; | |
4722 | struct io_connect *conn = &req->connect; | |
4723 | ||
4724 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
4725 | } | |
4726 | ||
3529d8c2 | 4727 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 4728 | { |
3529d8c2 | 4729 | struct io_connect *conn = &req->connect; |
f499a021 | 4730 | |
14587a46 | 4731 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 JA |
4732 | return -EINVAL; |
4733 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) | |
4734 | return -EINVAL; | |
4735 | ||
3529d8c2 JA |
4736 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4737 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 4738 | return 0; |
f499a021 JA |
4739 | } |
4740 | ||
889fca73 | 4741 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 4742 | { |
e8c2bc1f | 4743 | struct io_async_connect __io, *io; |
f8e85cf2 | 4744 | unsigned file_flags; |
3fbb51c1 | 4745 | int ret; |
45d189c6 | 4746 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 4747 | |
e8c2bc1f JA |
4748 | if (req->async_data) { |
4749 | io = req->async_data; | |
f499a021 | 4750 | } else { |
3529d8c2 JA |
4751 | ret = move_addr_to_kernel(req->connect.addr, |
4752 | req->connect.addr_len, | |
e8c2bc1f | 4753 | &__io.address); |
f499a021 JA |
4754 | if (ret) |
4755 | goto out; | |
4756 | io = &__io; | |
4757 | } | |
4758 | ||
3fbb51c1 JA |
4759 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
4760 | ||
e8c2bc1f | 4761 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 4762 | req->connect.addr_len, file_flags); |
87f80d62 | 4763 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 4764 | if (req->async_data) |
b7bb4f7d | 4765 | return -EAGAIN; |
e8c2bc1f | 4766 | if (io_alloc_async_data(req)) { |
f499a021 JA |
4767 | ret = -ENOMEM; |
4768 | goto out; | |
4769 | } | |
e8c2bc1f | 4770 | memcpy(req->async_data, &__io, sizeof(__io)); |
f8e85cf2 | 4771 | return -EAGAIN; |
f499a021 | 4772 | } |
f8e85cf2 JA |
4773 | if (ret == -ERESTARTSYS) |
4774 | ret = -EINTR; | |
f499a021 | 4775 | out: |
4e88d6e7 JA |
4776 | if (ret < 0) |
4777 | req_set_fail_links(req); | |
889fca73 | 4778 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 4779 | return 0; |
469956e8 Y |
4780 | } |
4781 | #else /* !CONFIG_NET */ | |
99a10081 JA |
4782 | #define IO_NETOP_FN(op) \ |
4783 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
4784 | { \ | |
4785 | return -EOPNOTSUPP; \ | |
4786 | } | |
4787 | ||
4788 | #define IO_NETOP_PREP(op) \ | |
4789 | IO_NETOP_FN(op) \ | |
4790 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
4791 | { \ | |
4792 | return -EOPNOTSUPP; \ | |
4793 | } \ | |
4794 | ||
4795 | #define IO_NETOP_PREP_ASYNC(op) \ | |
4796 | IO_NETOP_PREP(op) \ | |
4797 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
4798 | { \ | |
4799 | return -EOPNOTSUPP; \ | |
4800 | } | |
4801 | ||
4802 | IO_NETOP_PREP_ASYNC(sendmsg); | |
4803 | IO_NETOP_PREP_ASYNC(recvmsg); | |
4804 | IO_NETOP_PREP_ASYNC(connect); | |
4805 | IO_NETOP_PREP(accept); | |
4806 | IO_NETOP_FN(send); | |
4807 | IO_NETOP_FN(recv); | |
469956e8 | 4808 | #endif /* CONFIG_NET */ |
f8e85cf2 | 4809 | |
d7718a9d JA |
4810 | struct io_poll_table { |
4811 | struct poll_table_struct pt; | |
4812 | struct io_kiocb *req; | |
4813 | int error; | |
4814 | }; | |
ce593a6c | 4815 | |
d7718a9d JA |
4816 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
4817 | __poll_t mask, task_work_func_t func) | |
4818 | { | |
aa96bf8a | 4819 | int ret; |
d7718a9d JA |
4820 | |
4821 | /* for instances that support it check for an event match first: */ | |
4822 | if (mask && !(mask & poll->events)) | |
4823 | return 0; | |
4824 | ||
4825 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
4826 | ||
4827 | list_del_init(&poll->wait.entry); | |
4828 | ||
d7718a9d | 4829 | req->result = mask; |
7cbf1722 | 4830 | req->task_work.func = func; |
6d816e08 | 4831 | |
d7718a9d | 4832 | /* |
e3aabf95 JA |
4833 | * If this fails, then the task is exiting. When a task exits, the |
4834 | * work gets canceled, so just cancel this request as well instead | |
4835 | * of executing it. We can't safely execute it anyway, as we may not | |
4836 | * have the needed state needed for it anyway. | |
d7718a9d | 4837 | */ |
355fb9e2 | 4838 | ret = io_req_task_work_add(req); |
aa96bf8a | 4839 | if (unlikely(ret)) { |
e3aabf95 | 4840 | WRITE_ONCE(poll->canceled, true); |
eab30c4d | 4841 | io_req_task_work_add_fallback(req, func); |
aa96bf8a | 4842 | } |
d7718a9d JA |
4843 | return 1; |
4844 | } | |
4845 | ||
74ce6ce4 JA |
4846 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
4847 | __acquires(&req->ctx->completion_lock) | |
4848 | { | |
4849 | struct io_ring_ctx *ctx = req->ctx; | |
4850 | ||
4851 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
4852 | struct poll_table_struct pt = { ._key = poll->events }; | |
4853 | ||
4854 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
4855 | } | |
4856 | ||
4857 | spin_lock_irq(&ctx->completion_lock); | |
4858 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
4859 | add_wait_queue(poll->head, &poll->wait); | |
4860 | return true; | |
4861 | } | |
4862 | ||
4863 | return false; | |
4864 | } | |
4865 | ||
d4e7cd36 | 4866 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 4867 | { |
e8c2bc1f | 4868 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 4869 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 4870 | return req->async_data; |
d4e7cd36 JA |
4871 | return req->apoll->double_poll; |
4872 | } | |
4873 | ||
4874 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
4875 | { | |
4876 | if (req->opcode == IORING_OP_POLL_ADD) | |
4877 | return &req->poll; | |
4878 | return &req->apoll->poll; | |
4879 | } | |
4880 | ||
4881 | static void io_poll_remove_double(struct io_kiocb *req) | |
e07785b0 | 4882 | __must_hold(&req->ctx->completion_lock) |
d4e7cd36 JA |
4883 | { |
4884 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
4885 | |
4886 | lockdep_assert_held(&req->ctx->completion_lock); | |
4887 | ||
4888 | if (poll && poll->head) { | |
4889 | struct wait_queue_head *head = poll->head; | |
4890 | ||
4891 | spin_lock(&head->lock); | |
4892 | list_del_init(&poll->wait.entry); | |
4893 | if (poll->wait.private) | |
de9b4cca | 4894 | req_ref_put(req); |
18bceab1 JA |
4895 | poll->head = NULL; |
4896 | spin_unlock(&head->lock); | |
4897 | } | |
4898 | } | |
4899 | ||
e27414be | 4900 | static bool io_poll_complete(struct io_kiocb *req, __poll_t mask) |
e07785b0 | 4901 | __must_hold(&req->ctx->completion_lock) |
18bceab1 JA |
4902 | { |
4903 | struct io_ring_ctx *ctx = req->ctx; | |
88e41cf9 | 4904 | unsigned flags = IORING_CQE_F_MORE; |
e27414be | 4905 | int error; |
18bceab1 | 4906 | |
e27414be | 4907 | if (READ_ONCE(req->poll.canceled)) { |
45ab03b1 | 4908 | error = -ECANCELED; |
88e41cf9 | 4909 | req->poll.events |= EPOLLONESHOT; |
e27414be | 4910 | } else { |
5082620f | 4911 | error = mangle_poll(mask); |
e27414be | 4912 | } |
b69de288 JA |
4913 | if (req->poll.events & EPOLLONESHOT) |
4914 | flags = 0; | |
ff642164 | 4915 | if (!io_cqring_fill_event(req, error, flags)) { |
5082620f | 4916 | io_poll_remove_waitqs(req); |
88e41cf9 JA |
4917 | req->poll.done = true; |
4918 | flags = 0; | |
4919 | } | |
18bceab1 | 4920 | io_commit_cqring(ctx); |
88e41cf9 | 4921 | return !(flags & IORING_CQE_F_MORE); |
18bceab1 JA |
4922 | } |
4923 | ||
dd221f46 | 4924 | static void io_poll_task_func(struct callback_head *cb) |
18bceab1 | 4925 | { |
dd221f46 | 4926 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
18bceab1 | 4927 | struct io_ring_ctx *ctx = req->ctx; |
dd221f46 | 4928 | struct io_kiocb *nxt; |
18bceab1 JA |
4929 | |
4930 | if (io_poll_rewait(req, &req->poll)) { | |
4931 | spin_unlock_irq(&ctx->completion_lock); | |
dd221f46 | 4932 | } else { |
f40b964a | 4933 | bool done; |
88e41cf9 | 4934 | |
e27414be | 4935 | done = io_poll_complete(req, req->result); |
88e41cf9 JA |
4936 | if (done) { |
4937 | hash_del(&req->hash_node); | |
f40b964a | 4938 | } else { |
88e41cf9 JA |
4939 | req->result = 0; |
4940 | add_wait_queue(req->poll.head, &req->poll.wait); | |
4941 | } | |
dd221f46 | 4942 | spin_unlock_irq(&ctx->completion_lock); |
f40b964a | 4943 | io_cqring_ev_posted(ctx); |
18bceab1 | 4944 | |
88e41cf9 JA |
4945 | if (done) { |
4946 | nxt = io_put_req_find_next(req); | |
4947 | if (nxt) | |
4948 | __io_req_task_submit(nxt); | |
4949 | } | |
dd221f46 | 4950 | } |
18bceab1 JA |
4951 | } |
4952 | ||
4953 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
4954 | int sync, void *key) | |
4955 | { | |
4956 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 4957 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 JA |
4958 | __poll_t mask = key_to_poll(key); |
4959 | ||
4960 | /* for instances that support it check for an event match first: */ | |
4961 | if (mask && !(mask & poll->events)) | |
4962 | return 0; | |
88e41cf9 JA |
4963 | if (!(poll->events & EPOLLONESHOT)) |
4964 | return poll->wait.func(&poll->wait, mode, sync, key); | |
18bceab1 | 4965 | |
8706e04e JA |
4966 | list_del_init(&wait->entry); |
4967 | ||
807abcb0 | 4968 | if (poll && poll->head) { |
18bceab1 JA |
4969 | bool done; |
4970 | ||
807abcb0 JA |
4971 | spin_lock(&poll->head->lock); |
4972 | done = list_empty(&poll->wait.entry); | |
18bceab1 | 4973 | if (!done) |
807abcb0 | 4974 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
4975 | /* make sure double remove sees this as being gone */ |
4976 | wait->private = NULL; | |
807abcb0 | 4977 | spin_unlock(&poll->head->lock); |
c8b5e260 JA |
4978 | if (!done) { |
4979 | /* use wait func handler, so it matches the rq type */ | |
4980 | poll->wait.func(&poll->wait, mode, sync, key); | |
4981 | } | |
18bceab1 | 4982 | } |
de9b4cca | 4983 | req_ref_put(req); |
18bceab1 JA |
4984 | return 1; |
4985 | } | |
4986 | ||
4987 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
4988 | wait_queue_func_t wake_func) | |
4989 | { | |
4990 | poll->head = NULL; | |
4991 | poll->done = false; | |
4992 | poll->canceled = false; | |
464dca61 JA |
4993 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
4994 | /* mask in events that we always want/need */ | |
4995 | poll->events = events | IO_POLL_UNMASK; | |
18bceab1 JA |
4996 | INIT_LIST_HEAD(&poll->wait.entry); |
4997 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
4998 | } | |
4999 | ||
5000 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
5001 | struct wait_queue_head *head, |
5002 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
5003 | { |
5004 | struct io_kiocb *req = pt->req; | |
5005 | ||
5006 | /* | |
5007 | * If poll->head is already set, it's because the file being polled | |
5008 | * uses multiple waitqueues for poll handling (eg one for read, one | |
5009 | * for write). Setup a separate io_poll_iocb if this happens. | |
5010 | */ | |
5011 | if (unlikely(poll->head)) { | |
58852d4d PB |
5012 | struct io_poll_iocb *poll_one = poll; |
5013 | ||
18bceab1 | 5014 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5015 | if (*poll_ptr) { |
18bceab1 JA |
5016 | pt->error = -EINVAL; |
5017 | return; | |
5018 | } | |
1c3b3e65 JA |
5019 | /* double add on the same waitqueue head, ignore */ |
5020 | if (poll->head == head) | |
5021 | return; | |
18bceab1 JA |
5022 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
5023 | if (!poll) { | |
5024 | pt->error = -ENOMEM; | |
5025 | return; | |
5026 | } | |
58852d4d | 5027 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
de9b4cca | 5028 | req_ref_get(req); |
18bceab1 | 5029 | poll->wait.private = req; |
807abcb0 | 5030 | *poll_ptr = poll; |
18bceab1 JA |
5031 | } |
5032 | ||
5033 | pt->error = 0; | |
5034 | poll->head = head; | |
a31eb4a2 JX |
5035 | |
5036 | if (poll->events & EPOLLEXCLUSIVE) | |
5037 | add_wait_queue_exclusive(head, &poll->wait); | |
5038 | else | |
5039 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5040 | } |
5041 | ||
5042 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5043 | struct poll_table_struct *p) | |
5044 | { | |
5045 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5046 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5047 | |
807abcb0 | 5048 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5049 | } |
5050 | ||
d7718a9d JA |
5051 | static void io_async_task_func(struct callback_head *cb) |
5052 | { | |
5053 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
5054 | struct async_poll *apoll = req->apoll; | |
5055 | struct io_ring_ctx *ctx = req->ctx; | |
5056 | ||
5057 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); | |
5058 | ||
74ce6ce4 | 5059 | if (io_poll_rewait(req, &apoll->poll)) { |
d7718a9d | 5060 | spin_unlock_irq(&ctx->completion_lock); |
74ce6ce4 | 5061 | return; |
d7718a9d JA |
5062 | } |
5063 | ||
0ea13b44 | 5064 | hash_del(&req->hash_node); |
d4e7cd36 | 5065 | io_poll_remove_double(req); |
74ce6ce4 JA |
5066 | spin_unlock_irq(&ctx->completion_lock); |
5067 | ||
0be0b0e3 PB |
5068 | if (!READ_ONCE(apoll->poll.canceled)) |
5069 | __io_req_task_submit(req); | |
5070 | else | |
2593553a | 5071 | io_req_complete_failed(req, -ECANCELED); |
aa340845 | 5072 | |
807abcb0 | 5073 | kfree(apoll->double_poll); |
31067255 | 5074 | kfree(apoll); |
d7718a9d JA |
5075 | } |
5076 | ||
5077 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5078 | void *key) | |
5079 | { | |
5080 | struct io_kiocb *req = wait->private; | |
5081 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5082 | ||
5083 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5084 | key_to_poll(key)); | |
5085 | ||
5086 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5087 | } | |
5088 | ||
5089 | static void io_poll_req_insert(struct io_kiocb *req) | |
5090 | { | |
5091 | struct io_ring_ctx *ctx = req->ctx; | |
5092 | struct hlist_head *list; | |
5093 | ||
5094 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5095 | hlist_add_head(&req->hash_node, list); | |
5096 | } | |
5097 | ||
5098 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5099 | struct io_poll_iocb *poll, | |
5100 | struct io_poll_table *ipt, __poll_t mask, | |
5101 | wait_queue_func_t wake_func) | |
5102 | __acquires(&ctx->completion_lock) | |
5103 | { | |
5104 | struct io_ring_ctx *ctx = req->ctx; | |
5105 | bool cancel = false; | |
5106 | ||
4d52f338 | 5107 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5108 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5109 | poll->file = req->file; |
18bceab1 | 5110 | poll->wait.private = req; |
d7718a9d JA |
5111 | |
5112 | ipt->pt._key = mask; | |
5113 | ipt->req = req; | |
5114 | ipt->error = -EINVAL; | |
5115 | ||
d7718a9d JA |
5116 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
5117 | ||
5118 | spin_lock_irq(&ctx->completion_lock); | |
5119 | if (likely(poll->head)) { | |
5120 | spin_lock(&poll->head->lock); | |
5121 | if (unlikely(list_empty(&poll->wait.entry))) { | |
5122 | if (ipt->error) | |
5123 | cancel = true; | |
5124 | ipt->error = 0; | |
5125 | mask = 0; | |
5126 | } | |
88e41cf9 | 5127 | if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error) |
d7718a9d JA |
5128 | list_del_init(&poll->wait.entry); |
5129 | else if (cancel) | |
5130 | WRITE_ONCE(poll->canceled, true); | |
5131 | else if (!poll->done) /* actually waiting for an event */ | |
5132 | io_poll_req_insert(req); | |
5133 | spin_unlock(&poll->head->lock); | |
5134 | } | |
5135 | ||
5136 | return mask; | |
5137 | } | |
5138 | ||
5139 | static bool io_arm_poll_handler(struct io_kiocb *req) | |
5140 | { | |
5141 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5142 | struct io_ring_ctx *ctx = req->ctx; | |
5143 | struct async_poll *apoll; | |
5144 | struct io_poll_table ipt; | |
5145 | __poll_t mask, ret; | |
9dab14b8 | 5146 | int rw; |
d7718a9d JA |
5147 | |
5148 | if (!req->file || !file_can_poll(req->file)) | |
5149 | return false; | |
24c74678 | 5150 | if (req->flags & REQ_F_POLLED) |
d7718a9d | 5151 | return false; |
9dab14b8 JA |
5152 | if (def->pollin) |
5153 | rw = READ; | |
5154 | else if (def->pollout) | |
5155 | rw = WRITE; | |
5156 | else | |
5157 | return false; | |
5158 | /* if we can't nonblock try, then no point in arming a poll handler */ | |
7b29f92d | 5159 | if (!io_file_supports_async(req, rw)) |
d7718a9d JA |
5160 | return false; |
5161 | ||
5162 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
5163 | if (unlikely(!apoll)) | |
5164 | return false; | |
807abcb0 | 5165 | apoll->double_poll = NULL; |
d7718a9d JA |
5166 | |
5167 | req->flags |= REQ_F_POLLED; | |
d7718a9d | 5168 | req->apoll = apoll; |
d7718a9d | 5169 | |
88e41cf9 | 5170 | mask = EPOLLONESHOT; |
d7718a9d | 5171 | if (def->pollin) |
8755d97a | 5172 | mask |= POLLIN | POLLRDNORM; |
d7718a9d JA |
5173 | if (def->pollout) |
5174 | mask |= POLLOUT | POLLWRNORM; | |
901341bb LH |
5175 | |
5176 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5177 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5178 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5179 | mask &= ~POLLIN; | |
5180 | ||
d7718a9d JA |
5181 | mask |= POLLERR | POLLPRI; |
5182 | ||
5183 | ipt.pt._qproc = io_async_queue_proc; | |
5184 | ||
5185 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5186 | io_async_wake); | |
a36da65c | 5187 | if (ret || ipt.error) { |
d4e7cd36 | 5188 | io_poll_remove_double(req); |
d7718a9d | 5189 | spin_unlock_irq(&ctx->completion_lock); |
807abcb0 | 5190 | kfree(apoll->double_poll); |
d7718a9d JA |
5191 | kfree(apoll); |
5192 | return false; | |
5193 | } | |
5194 | spin_unlock_irq(&ctx->completion_lock); | |
5195 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, | |
5196 | apoll->poll.events); | |
5197 | return true; | |
5198 | } | |
5199 | ||
5200 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
b2e720ac | 5201 | struct io_poll_iocb *poll, bool do_cancel) |
e07785b0 | 5202 | __must_hold(&req->ctx->completion_lock) |
221c5eb2 | 5203 | { |
b41e9852 | 5204 | bool do_complete = false; |
221c5eb2 | 5205 | |
5082620f JA |
5206 | if (!poll->head) |
5207 | return false; | |
221c5eb2 | 5208 | spin_lock(&poll->head->lock); |
b2e720ac JA |
5209 | if (do_cancel) |
5210 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5211 | if (!list_empty(&poll->wait.entry)) { |
5212 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5213 | do_complete = true; |
221c5eb2 JA |
5214 | } |
5215 | spin_unlock(&poll->head->lock); | |
3bfa5bcb | 5216 | hash_del(&req->hash_node); |
d7718a9d JA |
5217 | return do_complete; |
5218 | } | |
5219 | ||
b2c3f7e1 | 5220 | static bool io_poll_remove_waitqs(struct io_kiocb *req) |
e07785b0 | 5221 | __must_hold(&req->ctx->completion_lock) |
d7718a9d JA |
5222 | { |
5223 | bool do_complete; | |
5224 | ||
d4e7cd36 JA |
5225 | io_poll_remove_double(req); |
5226 | ||
d7718a9d | 5227 | if (req->opcode == IORING_OP_POLL_ADD) { |
b2e720ac | 5228 | do_complete = __io_poll_remove_one(req, &req->poll, true); |
d7718a9d | 5229 | } else { |
3bfa5bcb JA |
5230 | struct async_poll *apoll = req->apoll; |
5231 | ||
d7718a9d | 5232 | /* non-poll requests have submit ref still */ |
b2e720ac | 5233 | do_complete = __io_poll_remove_one(req, &apoll->poll, true); |
3bfa5bcb | 5234 | if (do_complete) { |
7394161c | 5235 | req_ref_put(req); |
807abcb0 | 5236 | kfree(apoll->double_poll); |
3bfa5bcb JA |
5237 | kfree(apoll); |
5238 | } | |
b1f573bd XW |
5239 | } |
5240 | ||
b2c3f7e1 JA |
5241 | return do_complete; |
5242 | } | |
5243 | ||
5244 | static bool io_poll_remove_one(struct io_kiocb *req) | |
e07785b0 | 5245 | __must_hold(&req->ctx->completion_lock) |
b2c3f7e1 JA |
5246 | { |
5247 | bool do_complete; | |
5248 | ||
5249 | do_complete = io_poll_remove_waitqs(req); | |
b41e9852 | 5250 | if (do_complete) { |
ff642164 | 5251 | io_cqring_fill_event(req, -ECANCELED, 0); |
b41e9852 | 5252 | io_commit_cqring(req->ctx); |
f254ac04 | 5253 | req_set_fail_links(req); |
216578e5 | 5254 | io_put_req_deferred(req, 1); |
b41e9852 JA |
5255 | } |
5256 | ||
5257 | return do_complete; | |
221c5eb2 JA |
5258 | } |
5259 | ||
76e1b642 JA |
5260 | /* |
5261 | * Returns true if we found and killed one or more poll requests | |
5262 | */ | |
6b81928d PB |
5263 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
5264 | struct files_struct *files) | |
221c5eb2 | 5265 | { |
78076bb6 | 5266 | struct hlist_node *tmp; |
221c5eb2 | 5267 | struct io_kiocb *req; |
8e2e1faf | 5268 | int posted = 0, i; |
221c5eb2 JA |
5269 | |
5270 | spin_lock_irq(&ctx->completion_lock); | |
78076bb6 JA |
5271 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5272 | struct hlist_head *list; | |
5273 | ||
5274 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5275 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
6b81928d | 5276 | if (io_match_task(req, tsk, files)) |
f3606e3a JA |
5277 | posted += io_poll_remove_one(req); |
5278 | } | |
221c5eb2 JA |
5279 | } |
5280 | spin_unlock_irq(&ctx->completion_lock); | |
b41e9852 | 5281 | |
8e2e1faf JA |
5282 | if (posted) |
5283 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5284 | |
5285 | return posted != 0; | |
221c5eb2 JA |
5286 | } |
5287 | ||
b2cb805f | 5288 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr) |
e07785b0 | 5289 | __must_hold(&ctx->completion_lock) |
47f46768 | 5290 | { |
78076bb6 | 5291 | struct hlist_head *list; |
47f46768 JA |
5292 | struct io_kiocb *req; |
5293 | ||
78076bb6 JA |
5294 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5295 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5296 | if (sqe_addr != req->user_data) |
5297 | continue; | |
b2cb805f | 5298 | return req; |
47f46768 JA |
5299 | } |
5300 | ||
b2cb805f JA |
5301 | return NULL; |
5302 | } | |
5303 | ||
5304 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) | |
e07785b0 | 5305 | __must_hold(&ctx->completion_lock) |
b2cb805f JA |
5306 | { |
5307 | struct io_kiocb *req; | |
5308 | ||
5309 | req = io_poll_find(ctx, sqe_addr); | |
5310 | if (!req) | |
5311 | return -ENOENT; | |
5312 | if (io_poll_remove_one(req)) | |
5313 | return 0; | |
5314 | ||
5315 | return -EALREADY; | |
47f46768 JA |
5316 | } |
5317 | ||
3529d8c2 JA |
5318 | static int io_poll_remove_prep(struct io_kiocb *req, |
5319 | const struct io_uring_sqe *sqe) | |
0969e783 | 5320 | { |
0969e783 JA |
5321 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5322 | return -EINVAL; | |
5323 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || | |
5324 | sqe->poll_events) | |
5325 | return -EINVAL; | |
5326 | ||
018043be | 5327 | req->poll_remove.addr = READ_ONCE(sqe->addr); |
0969e783 JA |
5328 | return 0; |
5329 | } | |
5330 | ||
221c5eb2 JA |
5331 | /* |
5332 | * Find a running poll command that matches one specified in sqe->addr, | |
5333 | * and remove it if found. | |
5334 | */ | |
61e98203 | 5335 | static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags) |
221c5eb2 JA |
5336 | { |
5337 | struct io_ring_ctx *ctx = req->ctx; | |
47f46768 | 5338 | int ret; |
221c5eb2 | 5339 | |
221c5eb2 | 5340 | spin_lock_irq(&ctx->completion_lock); |
018043be | 5341 | ret = io_poll_cancel(ctx, req->poll_remove.addr); |
221c5eb2 JA |
5342 | spin_unlock_irq(&ctx->completion_lock); |
5343 | ||
4e88d6e7 JA |
5344 | if (ret < 0) |
5345 | req_set_fail_links(req); | |
0bdf3398 | 5346 | __io_req_complete(req, issue_flags, ret, 0); |
221c5eb2 JA |
5347 | return 0; |
5348 | } | |
5349 | ||
221c5eb2 JA |
5350 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5351 | void *key) | |
5352 | { | |
c2f2eb7d JA |
5353 | struct io_kiocb *req = wait->private; |
5354 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5355 | |
d7718a9d | 5356 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5357 | } |
5358 | ||
221c5eb2 JA |
5359 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5360 | struct poll_table_struct *p) | |
5361 | { | |
5362 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5363 | ||
e8c2bc1f | 5364 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5365 | } |
5366 | ||
3529d8c2 | 5367 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 | 5368 | { |
88e41cf9 | 5369 | u32 events, flags; |
221c5eb2 JA |
5370 | |
5371 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5372 | return -EINVAL; | |
b69de288 | 5373 | if (sqe->ioprio || sqe->buf_index) |
88e41cf9 JA |
5374 | return -EINVAL; |
5375 | flags = READ_ONCE(sqe->len); | |
b69de288 JA |
5376 | if (flags & ~(IORING_POLL_ADD_MULTI | IORING_POLL_UPDATE_EVENTS | |
5377 | IORING_POLL_UPDATE_USER_DATA)) | |
221c5eb2 | 5378 | return -EINVAL; |
5769a351 JX |
5379 | events = READ_ONCE(sqe->poll32_events); |
5380 | #ifdef __BIG_ENDIAN | |
5381 | events = swahw32(events); | |
5382 | #endif | |
b69de288 | 5383 | if (!(flags & IORING_POLL_ADD_MULTI)) |
88e41cf9 | 5384 | events |= EPOLLONESHOT; |
9d805892 PB |
5385 | events = demangle_poll(events) | |
5386 | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); | |
66d2d00d PB |
5387 | |
5388 | if (flags & (IORING_POLL_UPDATE_EVENTS|IORING_POLL_UPDATE_USER_DATA)) { | |
9d805892 PB |
5389 | struct io_poll_update *poll_upd = &req->poll_update; |
5390 | ||
5391 | req->flags |= REQ_F_POLL_UPDATE; | |
5392 | poll_upd->events = events; | |
5393 | poll_upd->old_user_data = READ_ONCE(sqe->addr); | |
5394 | poll_upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; | |
5395 | poll_upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; | |
5396 | if (poll_upd->update_user_data) | |
5397 | poll_upd->new_user_data = READ_ONCE(sqe->off); | |
66d2d00d | 5398 | } else { |
9d805892 PB |
5399 | struct io_poll_iocb *poll = &req->poll; |
5400 | ||
5401 | poll->events = events; | |
66d2d00d PB |
5402 | if (sqe->off || sqe->addr) |
5403 | return -EINVAL; | |
b69de288 | 5404 | } |
0969e783 JA |
5405 | return 0; |
5406 | } | |
5407 | ||
b69de288 | 5408 | static int __io_poll_add(struct io_kiocb *req) |
0969e783 JA |
5409 | { |
5410 | struct io_poll_iocb *poll = &req->poll; | |
5411 | struct io_ring_ctx *ctx = req->ctx; | |
5412 | struct io_poll_table ipt; | |
0969e783 | 5413 | __poll_t mask; |
0969e783 | 5414 | |
d7718a9d | 5415 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5416 | |
d7718a9d JA |
5417 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5418 | io_poll_wake); | |
221c5eb2 | 5419 | |
8c838788 | 5420 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5421 | ipt.error = 0; |
e27414be | 5422 | io_poll_complete(req, mask); |
221c5eb2 | 5423 | } |
221c5eb2 JA |
5424 | spin_unlock_irq(&ctx->completion_lock); |
5425 | ||
8c838788 JA |
5426 | if (mask) { |
5427 | io_cqring_ev_posted(ctx); | |
88e41cf9 JA |
5428 | if (poll->events & EPOLLONESHOT) |
5429 | io_put_req(req); | |
221c5eb2 | 5430 | } |
8c838788 | 5431 | return ipt.error; |
221c5eb2 JA |
5432 | } |
5433 | ||
b69de288 JA |
5434 | static int io_poll_update(struct io_kiocb *req) |
5435 | { | |
5436 | struct io_ring_ctx *ctx = req->ctx; | |
5437 | struct io_kiocb *preq; | |
cb3b200e | 5438 | bool completing; |
b69de288 JA |
5439 | int ret; |
5440 | ||
5441 | spin_lock_irq(&ctx->completion_lock); | |
9d805892 | 5442 | preq = io_poll_find(ctx, req->poll_update.old_user_data); |
b69de288 JA |
5443 | if (!preq) { |
5444 | ret = -ENOENT; | |
5445 | goto err; | |
5446 | } else if (preq->opcode != IORING_OP_POLL_ADD) { | |
5447 | /* don't allow internal poll updates */ | |
5448 | ret = -EACCES; | |
5449 | goto err; | |
5450 | } | |
cb3b200e JA |
5451 | |
5452 | /* | |
5453 | * Don't allow racy completion with singleshot, as we cannot safely | |
5454 | * update those. For multishot, if we're racing with completion, just | |
5455 | * let completion re-add it. | |
5456 | */ | |
5457 | completing = !__io_poll_remove_one(preq, &preq->poll, false); | |
5458 | if (completing && (preq->poll.events & EPOLLONESHOT)) { | |
5459 | ret = -EALREADY; | |
5460 | goto err; | |
b69de288 JA |
5461 | } |
5462 | /* we now have a detached poll request. reissue. */ | |
5463 | ret = 0; | |
5464 | err: | |
b69de288 | 5465 | if (ret < 0) { |
cb3b200e | 5466 | spin_unlock_irq(&ctx->completion_lock); |
b69de288 JA |
5467 | req_set_fail_links(req); |
5468 | io_req_complete(req, ret); | |
5469 | return 0; | |
5470 | } | |
5471 | /* only mask one event flags, keep behavior flags */ | |
9d805892 | 5472 | if (req->poll_update.update_events) { |
b69de288 | 5473 | preq->poll.events &= ~0xffff; |
9d805892 | 5474 | preq->poll.events |= req->poll_update.events & 0xffff; |
b69de288 JA |
5475 | preq->poll.events |= IO_POLL_UNMASK; |
5476 | } | |
9d805892 PB |
5477 | if (req->poll_update.update_user_data) |
5478 | preq->user_data = req->poll_update.new_user_data; | |
b69de288 | 5479 | |
cb3b200e JA |
5480 | spin_unlock_irq(&ctx->completion_lock); |
5481 | ||
b69de288 JA |
5482 | /* complete update request, we're done with it */ |
5483 | io_req_complete(req, ret); | |
5484 | ||
cb3b200e JA |
5485 | if (!completing) { |
5486 | ret = __io_poll_add(preq); | |
5487 | if (ret < 0) { | |
5488 | req_set_fail_links(preq); | |
5489 | io_req_complete(preq, ret); | |
5490 | } | |
b69de288 JA |
5491 | } |
5492 | return 0; | |
5493 | } | |
5494 | ||
5495 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) | |
5496 | { | |
9d805892 | 5497 | if (!(req->flags & REQ_F_POLL_UPDATE)) |
b69de288 JA |
5498 | return __io_poll_add(req); |
5499 | return io_poll_update(req); | |
5500 | } | |
5501 | ||
5262f567 JA |
5502 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5503 | { | |
ad8a48ac JA |
5504 | struct io_timeout_data *data = container_of(timer, |
5505 | struct io_timeout_data, timer); | |
5506 | struct io_kiocb *req = data->req; | |
5507 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5508 | unsigned long flags; |
5509 | ||
5262f567 | 5510 | spin_lock_irqsave(&ctx->completion_lock, flags); |
a71976f3 | 5511 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5512 | atomic_set(&req->ctx->cq_timeouts, |
5513 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
5514 | ||
ff642164 | 5515 | io_cqring_fill_event(req, -ETIME, 0); |
5262f567 JA |
5516 | io_commit_cqring(ctx); |
5517 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5518 | ||
5519 | io_cqring_ev_posted(ctx); | |
4e88d6e7 | 5520 | req_set_fail_links(req); |
5262f567 JA |
5521 | io_put_req(req); |
5522 | return HRTIMER_NORESTART; | |
5523 | } | |
5524 | ||
fbd15848 PB |
5525 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5526 | __u64 user_data) | |
e07785b0 | 5527 | __must_hold(&ctx->completion_lock) |
f254ac04 | 5528 | { |
fbd15848 | 5529 | struct io_timeout_data *io; |
47f46768 JA |
5530 | struct io_kiocb *req; |
5531 | int ret = -ENOENT; | |
f254ac04 | 5532 | |
135fcde8 | 5533 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
47f46768 | 5534 | if (user_data == req->user_data) { |
47f46768 JA |
5535 | ret = 0; |
5536 | break; | |
5537 | } | |
5538 | } | |
5539 | ||
5540 | if (ret == -ENOENT) | |
fbd15848 PB |
5541 | return ERR_PTR(ret); |
5542 | ||
5543 | io = req->async_data; | |
e8c2bc1f | 5544 | ret = hrtimer_try_to_cancel(&io->timer); |
f254ac04 | 5545 | if (ret == -1) |
fbd15848 | 5546 | return ERR_PTR(-EALREADY); |
a71976f3 | 5547 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5548 | return req; |
5549 | } | |
47f46768 | 5550 | |
fbd15848 | 5551 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
e07785b0 | 5552 | __must_hold(&ctx->completion_lock) |
fbd15848 PB |
5553 | { |
5554 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5555 | ||
5556 | if (IS_ERR(req)) | |
5557 | return PTR_ERR(req); | |
f254ac04 JA |
5558 | |
5559 | req_set_fail_links(req); | |
ff642164 | 5560 | io_cqring_fill_event(req, -ECANCELED, 0); |
216578e5 | 5561 | io_put_req_deferred(req, 1); |
f254ac04 JA |
5562 | return 0; |
5563 | } | |
5564 | ||
9c8e11b3 PB |
5565 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5566 | struct timespec64 *ts, enum hrtimer_mode mode) | |
e07785b0 | 5567 | __must_hold(&ctx->completion_lock) |
47f46768 | 5568 | { |
9c8e11b3 PB |
5569 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
5570 | struct io_timeout_data *data; | |
47f46768 | 5571 | |
9c8e11b3 PB |
5572 | if (IS_ERR(req)) |
5573 | return PTR_ERR(req); | |
47f46768 | 5574 | |
9c8e11b3 PB |
5575 | req->timeout.off = 0; /* noseq */ |
5576 | data = req->async_data; | |
5577 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
5578 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); | |
5579 | data->timer.function = io_timeout_fn; | |
5580 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
5581 | return 0; | |
47f46768 JA |
5582 | } |
5583 | ||
3529d8c2 JA |
5584 | static int io_timeout_remove_prep(struct io_kiocb *req, |
5585 | const struct io_uring_sqe *sqe) | |
b29472ee | 5586 | { |
9c8e11b3 PB |
5587 | struct io_timeout_rem *tr = &req->timeout_rem; |
5588 | ||
b29472ee JA |
5589 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5590 | return -EINVAL; | |
61710e43 DA |
5591 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5592 | return -EINVAL; | |
9c8e11b3 | 5593 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
b29472ee JA |
5594 | return -EINVAL; |
5595 | ||
9c8e11b3 PB |
5596 | tr->addr = READ_ONCE(sqe->addr); |
5597 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
5598 | if (tr->flags & IORING_TIMEOUT_UPDATE) { | |
5599 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) | |
5600 | return -EINVAL; | |
5601 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
5602 | return -EFAULT; | |
5603 | } else if (tr->flags) { | |
5604 | /* timeout removal doesn't support flags */ | |
b29472ee | 5605 | return -EINVAL; |
9c8e11b3 | 5606 | } |
b29472ee | 5607 | |
b29472ee JA |
5608 | return 0; |
5609 | } | |
5610 | ||
8662daec PB |
5611 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
5612 | { | |
5613 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
5614 | : HRTIMER_MODE_REL; | |
5615 | } | |
5616 | ||
11365043 JA |
5617 | /* |
5618 | * Remove or update an existing timeout command | |
5619 | */ | |
61e98203 | 5620 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 5621 | { |
9c8e11b3 | 5622 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 5623 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 5624 | int ret; |
11365043 | 5625 | |
11365043 | 5626 | spin_lock_irq(&ctx->completion_lock); |
8662daec | 5627 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) |
9c8e11b3 | 5628 | ret = io_timeout_cancel(ctx, tr->addr); |
8662daec PB |
5629 | else |
5630 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, | |
5631 | io_translate_timeout_mode(tr->flags)); | |
11365043 | 5632 | |
ff642164 | 5633 | io_cqring_fill_event(req, ret, 0); |
11365043 JA |
5634 | io_commit_cqring(ctx); |
5635 | spin_unlock_irq(&ctx->completion_lock); | |
5262f567 | 5636 | io_cqring_ev_posted(ctx); |
4e88d6e7 JA |
5637 | if (ret < 0) |
5638 | req_set_fail_links(req); | |
ec9c02ad | 5639 | io_put_req(req); |
11365043 | 5640 | return 0; |
5262f567 JA |
5641 | } |
5642 | ||
3529d8c2 | 5643 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 5644 | bool is_timeout_link) |
5262f567 | 5645 | { |
ad8a48ac | 5646 | struct io_timeout_data *data; |
a41525ab | 5647 | unsigned flags; |
56080b02 | 5648 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 5649 | |
ad8a48ac | 5650 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 5651 | return -EINVAL; |
ad8a48ac | 5652 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
a41525ab | 5653 | return -EINVAL; |
56080b02 | 5654 | if (off && is_timeout_link) |
2d28390a | 5655 | return -EINVAL; |
a41525ab JA |
5656 | flags = READ_ONCE(sqe->timeout_flags); |
5657 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 5658 | return -EINVAL; |
bdf20073 | 5659 | |
bfe68a22 | 5660 | req->timeout.off = off; |
26a61679 | 5661 | |
e8c2bc1f | 5662 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
5663 | return -ENOMEM; |
5664 | ||
e8c2bc1f | 5665 | data = req->async_data; |
ad8a48ac | 5666 | data->req = req; |
ad8a48ac JA |
5667 | |
5668 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
5669 | return -EFAULT; |
5670 | ||
8662daec | 5671 | data->mode = io_translate_timeout_mode(flags); |
ad8a48ac | 5672 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
2482b58f PB |
5673 | if (is_timeout_link) |
5674 | io_req_track_inflight(req); | |
ad8a48ac JA |
5675 | return 0; |
5676 | } | |
5677 | ||
61e98203 | 5678 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 5679 | { |
ad8a48ac | 5680 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 5681 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 5682 | struct list_head *entry; |
bfe68a22 | 5683 | u32 tail, off = req->timeout.off; |
ad8a48ac | 5684 | |
733f5c95 | 5685 | spin_lock_irq(&ctx->completion_lock); |
93bd25bb | 5686 | |
5262f567 JA |
5687 | /* |
5688 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
5689 | * timeout event to be satisfied. If it isn't set, then this is |
5690 | * a pure timeout request, sequence isn't used. | |
5262f567 | 5691 | */ |
8eb7e2d0 | 5692 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
5693 | entry = ctx->timeout_list.prev; |
5694 | goto add; | |
5695 | } | |
5262f567 | 5696 | |
bfe68a22 PB |
5697 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
5698 | req->timeout.target_seq = tail + off; | |
5262f567 | 5699 | |
f010505b MDG |
5700 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
5701 | * This is safe because ->completion_lock is held, and submissions | |
5702 | * and completions are never mixed in the same ->completion_lock section. | |
5703 | */ | |
5704 | ctx->cq_last_tm_flush = tail; | |
5705 | ||
5262f567 JA |
5706 | /* |
5707 | * Insertion sort, ensuring the first entry in the list is always | |
5708 | * the one we need first. | |
5709 | */ | |
5262f567 | 5710 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
5711 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
5712 | timeout.list); | |
5262f567 | 5713 | |
8eb7e2d0 | 5714 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 5715 | continue; |
bfe68a22 PB |
5716 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
5717 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
5718 | break; |
5719 | } | |
93bd25bb | 5720 | add: |
135fcde8 | 5721 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
5722 | data->timer.function = io_timeout_fn; |
5723 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
5262f567 | 5724 | spin_unlock_irq(&ctx->completion_lock); |
5262f567 JA |
5725 | return 0; |
5726 | } | |
5262f567 | 5727 | |
f458dd84 PB |
5728 | struct io_cancel_data { |
5729 | struct io_ring_ctx *ctx; | |
5730 | u64 user_data; | |
5731 | }; | |
5732 | ||
62755e35 JA |
5733 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
5734 | { | |
5735 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 5736 | struct io_cancel_data *cd = data; |
62755e35 | 5737 | |
f458dd84 | 5738 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
62755e35 JA |
5739 | } |
5740 | ||
f458dd84 PB |
5741 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
5742 | struct io_ring_ctx *ctx) | |
62755e35 | 5743 | { |
f458dd84 | 5744 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
62755e35 | 5745 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
5746 | int ret = 0; |
5747 | ||
f458dd84 | 5748 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
5749 | return -ENOENT; |
5750 | ||
f458dd84 | 5751 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
62755e35 JA |
5752 | switch (cancel_ret) { |
5753 | case IO_WQ_CANCEL_OK: | |
5754 | ret = 0; | |
5755 | break; | |
5756 | case IO_WQ_CANCEL_RUNNING: | |
5757 | ret = -EALREADY; | |
5758 | break; | |
5759 | case IO_WQ_CANCEL_NOTFOUND: | |
5760 | ret = -ENOENT; | |
5761 | break; | |
5762 | } | |
5763 | ||
e977d6d3 JA |
5764 | return ret; |
5765 | } | |
5766 | ||
47f46768 JA |
5767 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
5768 | struct io_kiocb *req, __u64 sqe_addr, | |
014db007 | 5769 | int success_ret) |
47f46768 JA |
5770 | { |
5771 | unsigned long flags; | |
5772 | int ret; | |
5773 | ||
f458dd84 | 5774 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
47f46768 | 5775 | spin_lock_irqsave(&ctx->completion_lock, flags); |
df9727af PB |
5776 | if (ret != -ENOENT) |
5777 | goto done; | |
47f46768 JA |
5778 | ret = io_timeout_cancel(ctx, sqe_addr); |
5779 | if (ret != -ENOENT) | |
5780 | goto done; | |
5781 | ret = io_poll_cancel(ctx, sqe_addr); | |
5782 | done: | |
b0dd8a41 JA |
5783 | if (!ret) |
5784 | ret = success_ret; | |
ff642164 | 5785 | io_cqring_fill_event(req, ret, 0); |
47f46768 JA |
5786 | io_commit_cqring(ctx); |
5787 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5788 | io_cqring_ev_posted(ctx); | |
5789 | ||
4e88d6e7 JA |
5790 | if (ret < 0) |
5791 | req_set_fail_links(req); | |
47f46768 JA |
5792 | } |
5793 | ||
3529d8c2 JA |
5794 | static int io_async_cancel_prep(struct io_kiocb *req, |
5795 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 5796 | { |
fbf23849 | 5797 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 5798 | return -EINVAL; |
61710e43 DA |
5799 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5800 | return -EINVAL; | |
5801 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) | |
e977d6d3 JA |
5802 | return -EINVAL; |
5803 | ||
fbf23849 JA |
5804 | req->cancel.addr = READ_ONCE(sqe->addr); |
5805 | return 0; | |
5806 | } | |
5807 | ||
61e98203 | 5808 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
5809 | { |
5810 | struct io_ring_ctx *ctx = req->ctx; | |
58f99373 PB |
5811 | u64 sqe_addr = req->cancel.addr; |
5812 | struct io_tctx_node *node; | |
5813 | int ret; | |
5814 | ||
5815 | /* tasks should wait for their io-wq threads, so safe w/o sync */ | |
5816 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); | |
5817 | spin_lock_irq(&ctx->completion_lock); | |
5818 | if (ret != -ENOENT) | |
5819 | goto done; | |
5820 | ret = io_timeout_cancel(ctx, sqe_addr); | |
5821 | if (ret != -ENOENT) | |
5822 | goto done; | |
5823 | ret = io_poll_cancel(ctx, sqe_addr); | |
5824 | if (ret != -ENOENT) | |
5825 | goto done; | |
5826 | spin_unlock_irq(&ctx->completion_lock); | |
5827 | ||
5828 | /* slow path, try all io-wq's */ | |
5829 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
5830 | ret = -ENOENT; | |
5831 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
5832 | struct io_uring_task *tctx = node->task->io_uring; | |
fbf23849 | 5833 | |
58f99373 PB |
5834 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
5835 | if (ret != -ENOENT) | |
5836 | break; | |
5837 | } | |
5838 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
5839 | ||
5840 | spin_lock_irq(&ctx->completion_lock); | |
5841 | done: | |
ff642164 | 5842 | io_cqring_fill_event(req, ret, 0); |
58f99373 PB |
5843 | io_commit_cqring(ctx); |
5844 | spin_unlock_irq(&ctx->completion_lock); | |
5845 | io_cqring_ev_posted(ctx); | |
5846 | ||
5847 | if (ret < 0) | |
5848 | req_set_fail_links(req); | |
5849 | io_put_req(req); | |
5262f567 JA |
5850 | return 0; |
5851 | } | |
5852 | ||
269bbe5f | 5853 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
5854 | const struct io_uring_sqe *sqe) |
5855 | { | |
6ca56f84 JA |
5856 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
5857 | return -EINVAL; | |
61710e43 DA |
5858 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5859 | return -EINVAL; | |
5860 | if (sqe->ioprio || sqe->rw_flags) | |
05f3fb3c JA |
5861 | return -EINVAL; |
5862 | ||
269bbe5f BM |
5863 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
5864 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
5865 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 5866 | return -EINVAL; |
269bbe5f | 5867 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
5868 | return 0; |
5869 | } | |
5870 | ||
889fca73 | 5871 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
5872 | { |
5873 | struct io_ring_ctx *ctx = req->ctx; | |
269bbe5f | 5874 | struct io_uring_rsrc_update up; |
05f3fb3c | 5875 | int ret; |
fbf23849 | 5876 | |
45d189c6 | 5877 | if (issue_flags & IO_URING_F_NONBLOCK) |
05f3fb3c | 5878 | return -EAGAIN; |
05f3fb3c | 5879 | |
269bbe5f BM |
5880 | up.offset = req->rsrc_update.offset; |
5881 | up.data = req->rsrc_update.arg; | |
05f3fb3c JA |
5882 | |
5883 | mutex_lock(&ctx->uring_lock); | |
269bbe5f | 5884 | ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args); |
05f3fb3c JA |
5885 | mutex_unlock(&ctx->uring_lock); |
5886 | ||
5887 | if (ret < 0) | |
5888 | req_set_fail_links(req); | |
889fca73 | 5889 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
5890 | return 0; |
5891 | } | |
5892 | ||
bfe76559 | 5893 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 5894 | { |
d625c6ee | 5895 | switch (req->opcode) { |
e781573e | 5896 | case IORING_OP_NOP: |
bfe76559 | 5897 | return 0; |
f67676d1 JA |
5898 | case IORING_OP_READV: |
5899 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 5900 | case IORING_OP_READ: |
bfe76559 | 5901 | return io_read_prep(req, sqe); |
f67676d1 JA |
5902 | case IORING_OP_WRITEV: |
5903 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 5904 | case IORING_OP_WRITE: |
bfe76559 | 5905 | return io_write_prep(req, sqe); |
0969e783 | 5906 | case IORING_OP_POLL_ADD: |
bfe76559 | 5907 | return io_poll_add_prep(req, sqe); |
0969e783 | 5908 | case IORING_OP_POLL_REMOVE: |
bfe76559 | 5909 | return io_poll_remove_prep(req, sqe); |
8ed8d3c3 | 5910 | case IORING_OP_FSYNC: |
1155c76a | 5911 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 5912 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 5913 | return io_sfr_prep(req, sqe); |
03b1230c | 5914 | case IORING_OP_SENDMSG: |
fddaface | 5915 | case IORING_OP_SEND: |
bfe76559 | 5916 | return io_sendmsg_prep(req, sqe); |
03b1230c | 5917 | case IORING_OP_RECVMSG: |
fddaface | 5918 | case IORING_OP_RECV: |
bfe76559 | 5919 | return io_recvmsg_prep(req, sqe); |
f499a021 | 5920 | case IORING_OP_CONNECT: |
bfe76559 | 5921 | return io_connect_prep(req, sqe); |
2d28390a | 5922 | case IORING_OP_TIMEOUT: |
bfe76559 | 5923 | return io_timeout_prep(req, sqe, false); |
b29472ee | 5924 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 5925 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 5926 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 5927 | return io_async_cancel_prep(req, sqe); |
2d28390a | 5928 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 5929 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 5930 | case IORING_OP_ACCEPT: |
bfe76559 | 5931 | return io_accept_prep(req, sqe); |
d63d1b5e | 5932 | case IORING_OP_FALLOCATE: |
bfe76559 | 5933 | return io_fallocate_prep(req, sqe); |
15b71abe | 5934 | case IORING_OP_OPENAT: |
bfe76559 | 5935 | return io_openat_prep(req, sqe); |
b5dba59e | 5936 | case IORING_OP_CLOSE: |
bfe76559 | 5937 | return io_close_prep(req, sqe); |
05f3fb3c | 5938 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 5939 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 5940 | case IORING_OP_STATX: |
bfe76559 | 5941 | return io_statx_prep(req, sqe); |
4840e418 | 5942 | case IORING_OP_FADVISE: |
bfe76559 | 5943 | return io_fadvise_prep(req, sqe); |
c1ca757b | 5944 | case IORING_OP_MADVISE: |
bfe76559 | 5945 | return io_madvise_prep(req, sqe); |
cebdb986 | 5946 | case IORING_OP_OPENAT2: |
bfe76559 | 5947 | return io_openat2_prep(req, sqe); |
3e4827b0 | 5948 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 5949 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 5950 | case IORING_OP_SPLICE: |
bfe76559 | 5951 | return io_splice_prep(req, sqe); |
ddf0322d | 5952 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 5953 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 5954 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 5955 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 5956 | case IORING_OP_TEE: |
bfe76559 | 5957 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
5958 | case IORING_OP_SHUTDOWN: |
5959 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
5960 | case IORING_OP_RENAMEAT: |
5961 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
5962 | case IORING_OP_UNLINKAT: |
5963 | return io_unlinkat_prep(req, sqe); | |
f67676d1 JA |
5964 | } |
5965 | ||
bfe76559 PB |
5966 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
5967 | req->opcode); | |
5968 | return-EINVAL; | |
5969 | } | |
5970 | ||
93642ef8 | 5971 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 5972 | { |
b7e298d2 PB |
5973 | if (!io_op_defs[req->opcode].needs_async_setup) |
5974 | return 0; | |
5975 | if (WARN_ON_ONCE(req->async_data)) | |
5976 | return -EFAULT; | |
5977 | if (io_alloc_async_data(req)) | |
5978 | return -EAGAIN; | |
5979 | ||
93642ef8 PB |
5980 | switch (req->opcode) { |
5981 | case IORING_OP_READV: | |
93642ef8 PB |
5982 | return io_rw_prep_async(req, READ); |
5983 | case IORING_OP_WRITEV: | |
93642ef8 PB |
5984 | return io_rw_prep_async(req, WRITE); |
5985 | case IORING_OP_SENDMSG: | |
93642ef8 PB |
5986 | return io_sendmsg_prep_async(req); |
5987 | case IORING_OP_RECVMSG: | |
93642ef8 PB |
5988 | return io_recvmsg_prep_async(req); |
5989 | case IORING_OP_CONNECT: | |
5990 | return io_connect_prep_async(req); | |
5991 | } | |
b7e298d2 PB |
5992 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
5993 | req->opcode); | |
5994 | return -EFAULT; | |
f67676d1 JA |
5995 | } |
5996 | ||
9cf7c104 PB |
5997 | static u32 io_get_sequence(struct io_kiocb *req) |
5998 | { | |
5999 | struct io_kiocb *pos; | |
6000 | struct io_ring_ctx *ctx = req->ctx; | |
f2f87370 | 6001 | u32 total_submitted, nr_reqs = 0; |
9cf7c104 | 6002 | |
f2f87370 PB |
6003 | io_for_each_link(pos, req) |
6004 | nr_reqs++; | |
9cf7c104 PB |
6005 | |
6006 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; | |
6007 | return total_submitted - nr_reqs; | |
6008 | } | |
6009 | ||
be7053b7 | 6010 | static int io_req_defer(struct io_kiocb *req) |
de0617e4 | 6011 | { |
a197f664 | 6012 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6013 | struct io_defer_entry *de; |
f67676d1 | 6014 | int ret; |
9cf7c104 | 6015 | u32 seq; |
de0617e4 | 6016 | |
9d858b21 | 6017 | /* Still need defer if there is pending req in defer list. */ |
9cf7c104 PB |
6018 | if (likely(list_empty_careful(&ctx->defer_list) && |
6019 | !(req->flags & REQ_F_IO_DRAIN))) | |
6020 | return 0; | |
6021 | ||
6022 | seq = io_get_sequence(req); | |
6023 | /* Still a chance to pass the sequence check */ | |
6024 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
de0617e4 JA |
6025 | return 0; |
6026 | ||
b7e298d2 | 6027 | ret = io_req_prep_async(req); |
be7053b7 PB |
6028 | if (ret) |
6029 | return ret; | |
cbdcb435 | 6030 | io_prep_async_link(req); |
27dc8338 PB |
6031 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
6032 | if (!de) | |
6033 | return -ENOMEM; | |
2d28390a | 6034 | |
de0617e4 | 6035 | spin_lock_irq(&ctx->completion_lock); |
9cf7c104 | 6036 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
de0617e4 | 6037 | spin_unlock_irq(&ctx->completion_lock); |
27dc8338 | 6038 | kfree(de); |
ae34817b PB |
6039 | io_queue_async_work(req); |
6040 | return -EIOCBQUEUED; | |
de0617e4 JA |
6041 | } |
6042 | ||
915967f6 | 6043 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 6044 | de->req = req; |
9cf7c104 | 6045 | de->seq = seq; |
27dc8338 | 6046 | list_add_tail(&de->list, &ctx->defer_list); |
de0617e4 JA |
6047 | spin_unlock_irq(&ctx->completion_lock); |
6048 | return -EIOCBQUEUED; | |
6049 | } | |
6050 | ||
68fb8979 | 6051 | static void io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6052 | { |
0e1b6fe3 PB |
6053 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
6054 | switch (req->opcode) { | |
6055 | case IORING_OP_READV: | |
6056 | case IORING_OP_READ_FIXED: | |
6057 | case IORING_OP_READ: | |
bcda7baa | 6058 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
6059 | break; |
6060 | case IORING_OP_RECVMSG: | |
6061 | case IORING_OP_RECV: | |
bcda7baa | 6062 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
6063 | break; |
6064 | } | |
6065 | req->flags &= ~REQ_F_BUFFER_SELECTED; | |
99bc4c38 PB |
6066 | } |
6067 | ||
0e1b6fe3 PB |
6068 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6069 | switch (req->opcode) { | |
6070 | case IORING_OP_READV: | |
6071 | case IORING_OP_READ_FIXED: | |
6072 | case IORING_OP_READ: | |
6073 | case IORING_OP_WRITEV: | |
6074 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6075 | case IORING_OP_WRITE: { |
6076 | struct io_async_rw *io = req->async_data; | |
6077 | if (io->free_iovec) | |
6078 | kfree(io->free_iovec); | |
0e1b6fe3 | 6079 | break; |
e8c2bc1f | 6080 | } |
0e1b6fe3 | 6081 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6082 | case IORING_OP_SENDMSG: { |
6083 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
6084 | |
6085 | kfree(io->free_iov); | |
0e1b6fe3 | 6086 | break; |
e8c2bc1f | 6087 | } |
0e1b6fe3 PB |
6088 | case IORING_OP_SPLICE: |
6089 | case IORING_OP_TEE: | |
e1d767f0 PB |
6090 | if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED)) |
6091 | io_put_file(req->splice.file_in); | |
0e1b6fe3 | 6092 | break; |
f3cd4850 JA |
6093 | case IORING_OP_OPENAT: |
6094 | case IORING_OP_OPENAT2: | |
6095 | if (req->open.filename) | |
6096 | putname(req->open.filename); | |
6097 | break; | |
80a261fd JA |
6098 | case IORING_OP_RENAMEAT: |
6099 | putname(req->rename.oldpath); | |
6100 | putname(req->rename.newpath); | |
6101 | break; | |
14a1143b JA |
6102 | case IORING_OP_UNLINKAT: |
6103 | putname(req->unlink.filename); | |
6104 | break; | |
0e1b6fe3 PB |
6105 | } |
6106 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
99bc4c38 | 6107 | } |
99bc4c38 PB |
6108 | } |
6109 | ||
889fca73 | 6110 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 6111 | { |
a197f664 | 6112 | struct io_ring_ctx *ctx = req->ctx; |
5730b27e | 6113 | const struct cred *creds = NULL; |
d625c6ee | 6114 | int ret; |
2b188cc1 | 6115 | |
003e8dcc JA |
6116 | if (req->work.creds && req->work.creds != current_cred()) |
6117 | creds = override_creds(req->work.creds); | |
5730b27e | 6118 | |
d625c6ee | 6119 | switch (req->opcode) { |
2b188cc1 | 6120 | case IORING_OP_NOP: |
889fca73 | 6121 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
6122 | break; |
6123 | case IORING_OP_READV: | |
edafccee | 6124 | case IORING_OP_READ_FIXED: |
3a6820f2 | 6125 | case IORING_OP_READ: |
889fca73 | 6126 | ret = io_read(req, issue_flags); |
edafccee | 6127 | break; |
3529d8c2 | 6128 | case IORING_OP_WRITEV: |
edafccee | 6129 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 6130 | case IORING_OP_WRITE: |
889fca73 | 6131 | ret = io_write(req, issue_flags); |
2b188cc1 | 6132 | break; |
c992fe29 | 6133 | case IORING_OP_FSYNC: |
45d189c6 | 6134 | ret = io_fsync(req, issue_flags); |
c992fe29 | 6135 | break; |
221c5eb2 | 6136 | case IORING_OP_POLL_ADD: |
61e98203 | 6137 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
6138 | break; |
6139 | case IORING_OP_POLL_REMOVE: | |
61e98203 | 6140 | ret = io_poll_remove(req, issue_flags); |
221c5eb2 | 6141 | break; |
5d17b4a4 | 6142 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 6143 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 6144 | break; |
0fa03c62 | 6145 | case IORING_OP_SENDMSG: |
889fca73 | 6146 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 6147 | break; |
fddaface | 6148 | case IORING_OP_SEND: |
889fca73 | 6149 | ret = io_send(req, issue_flags); |
0fa03c62 | 6150 | break; |
aa1fa28f | 6151 | case IORING_OP_RECVMSG: |
889fca73 | 6152 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 6153 | break; |
fddaface | 6154 | case IORING_OP_RECV: |
889fca73 | 6155 | ret = io_recv(req, issue_flags); |
aa1fa28f | 6156 | break; |
5262f567 | 6157 | case IORING_OP_TIMEOUT: |
61e98203 | 6158 | ret = io_timeout(req, issue_flags); |
5262f567 | 6159 | break; |
11365043 | 6160 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 6161 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 6162 | break; |
17f2fe35 | 6163 | case IORING_OP_ACCEPT: |
889fca73 | 6164 | ret = io_accept(req, issue_flags); |
17f2fe35 | 6165 | break; |
f8e85cf2 | 6166 | case IORING_OP_CONNECT: |
889fca73 | 6167 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 6168 | break; |
62755e35 | 6169 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 6170 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 6171 | break; |
d63d1b5e | 6172 | case IORING_OP_FALLOCATE: |
45d189c6 | 6173 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 6174 | break; |
15b71abe | 6175 | case IORING_OP_OPENAT: |
45d189c6 | 6176 | ret = io_openat(req, issue_flags); |
15b71abe | 6177 | break; |
b5dba59e | 6178 | case IORING_OP_CLOSE: |
889fca73 | 6179 | ret = io_close(req, issue_flags); |
b5dba59e | 6180 | break; |
05f3fb3c | 6181 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6182 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6183 | break; |
eddc7ef5 | 6184 | case IORING_OP_STATX: |
45d189c6 | 6185 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6186 | break; |
4840e418 | 6187 | case IORING_OP_FADVISE: |
45d189c6 | 6188 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6189 | break; |
c1ca757b | 6190 | case IORING_OP_MADVISE: |
45d189c6 | 6191 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6192 | break; |
cebdb986 | 6193 | case IORING_OP_OPENAT2: |
45d189c6 | 6194 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6195 | break; |
3e4827b0 | 6196 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6197 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6198 | break; |
7d67af2c | 6199 | case IORING_OP_SPLICE: |
45d189c6 | 6200 | ret = io_splice(req, issue_flags); |
7d67af2c | 6201 | break; |
ddf0322d | 6202 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6203 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6204 | break; |
067524e9 | 6205 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6206 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6207 | break; |
f2a8d5c7 | 6208 | case IORING_OP_TEE: |
45d189c6 | 6209 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6210 | break; |
36f4fa68 | 6211 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6212 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6213 | break; |
80a261fd | 6214 | case IORING_OP_RENAMEAT: |
45d189c6 | 6215 | ret = io_renameat(req, issue_flags); |
80a261fd | 6216 | break; |
14a1143b | 6217 | case IORING_OP_UNLINKAT: |
45d189c6 | 6218 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6219 | break; |
2b188cc1 JA |
6220 | default: |
6221 | ret = -EINVAL; | |
6222 | break; | |
6223 | } | |
6224 | ||
5730b27e JA |
6225 | if (creds) |
6226 | revert_creds(creds); | |
6227 | ||
def596e9 JA |
6228 | if (ret) |
6229 | return ret; | |
6230 | ||
b532576e JA |
6231 | /* If the op doesn't have a file, we're not polling for it */ |
6232 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { | |
11ba820b JA |
6233 | const bool in_async = io_wq_current_is_worker(); |
6234 | ||
11ba820b JA |
6235 | /* workqueue context doesn't hold uring_lock, grab it now */ |
6236 | if (in_async) | |
6237 | mutex_lock(&ctx->uring_lock); | |
6238 | ||
2e9dbe90 | 6239 | io_iopoll_req_issued(req, in_async); |
11ba820b JA |
6240 | |
6241 | if (in_async) | |
6242 | mutex_unlock(&ctx->uring_lock); | |
def596e9 JA |
6243 | } |
6244 | ||
6245 | return 0; | |
2b188cc1 JA |
6246 | } |
6247 | ||
5280f7e5 | 6248 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6249 | { |
6250 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6251 | struct io_kiocb *timeout; |
561fb04a | 6252 | int ret = 0; |
2b188cc1 | 6253 | |
6df1db6b PB |
6254 | timeout = io_prep_linked_timeout(req); |
6255 | if (timeout) | |
6256 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6257 | |
4014d943 | 6258 | if (work->flags & IO_WQ_WORK_CANCEL) |
561fb04a | 6259 | ret = -ECANCELED; |
31b51510 | 6260 | |
561fb04a | 6261 | if (!ret) { |
561fb04a | 6262 | do { |
889fca73 | 6263 | ret = io_issue_sqe(req, 0); |
561fb04a JA |
6264 | /* |
6265 | * We can get EAGAIN for polled IO even though we're | |
6266 | * forcing a sync submission from here, since we can't | |
6267 | * wait for request slots on the block side. | |
6268 | */ | |
6269 | if (ret != -EAGAIN) | |
6270 | break; | |
6271 | cond_resched(); | |
6272 | } while (1); | |
6273 | } | |
31b51510 | 6274 | |
a3df7698 | 6275 | /* avoid locking problems by failing it from a clean context */ |
561fb04a | 6276 | if (ret) { |
a3df7698 | 6277 | /* io-wq is going to take one down */ |
de9b4cca | 6278 | req_ref_get(req); |
a3df7698 | 6279 | io_req_task_queue_fail(req, ret); |
edafccee | 6280 | } |
2b188cc1 JA |
6281 | } |
6282 | ||
7b29f92d JA |
6283 | #define FFS_ASYNC_READ 0x1UL |
6284 | #define FFS_ASYNC_WRITE 0x2UL | |
6285 | #ifdef CONFIG_64BIT | |
6286 | #define FFS_ISREG 0x4UL | |
6287 | #else | |
6288 | #define FFS_ISREG 0x0UL | |
6289 | #endif | |
6290 | #define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG) | |
6291 | ||
aeca241b | 6292 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
a04b0ac0 | 6293 | unsigned i) |
65e19f54 | 6294 | { |
aeca241b | 6295 | struct io_fixed_file *table_l2; |
65e19f54 | 6296 | |
aeca241b PB |
6297 | table_l2 = table->files[i >> IORING_FILE_TABLE_SHIFT]; |
6298 | return &table_l2[i & IORING_FILE_TABLE_MASK]; | |
dafecf19 PB |
6299 | } |
6300 | ||
6301 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, | |
6302 | int index) | |
6303 | { | |
aeca241b | 6304 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
7b29f92d | 6305 | |
a04b0ac0 | 6306 | return (struct file *) (slot->file_ptr & FFS_MASK); |
65e19f54 JA |
6307 | } |
6308 | ||
a04b0ac0 | 6309 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
9a321c98 PB |
6310 | { |
6311 | unsigned long file_ptr = (unsigned long) file; | |
6312 | ||
6313 | if (__io_file_supports_async(file, READ)) | |
6314 | file_ptr |= FFS_ASYNC_READ; | |
6315 | if (__io_file_supports_async(file, WRITE)) | |
6316 | file_ptr |= FFS_ASYNC_WRITE; | |
6317 | if (S_ISREG(file_inode(file)->i_mode)) | |
6318 | file_ptr |= FFS_ISREG; | |
a04b0ac0 | 6319 | file_slot->file_ptr = file_ptr; |
9a321c98 PB |
6320 | } |
6321 | ||
8371adf5 PB |
6322 | static struct file *io_file_get(struct io_submit_state *state, |
6323 | struct io_kiocb *req, int fd, bool fixed) | |
09bb8394 | 6324 | { |
a197f664 | 6325 | struct io_ring_ctx *ctx = req->ctx; |
8da11c19 | 6326 | struct file *file; |
09bb8394 | 6327 | |
8da11c19 | 6328 | if (fixed) { |
7b29f92d JA |
6329 | unsigned long file_ptr; |
6330 | ||
479f517b | 6331 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
8371adf5 | 6332 | return NULL; |
b7620121 | 6333 | fd = array_index_nospec(fd, ctx->nr_user_files); |
aeca241b | 6334 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; |
7b29f92d JA |
6335 | file = (struct file *) (file_ptr & FFS_MASK); |
6336 | file_ptr &= ~FFS_MASK; | |
6337 | /* mask in overlapping REQ_F and FFS bits */ | |
6338 | req->flags |= (file_ptr << REQ_F_ASYNC_READ_BIT); | |
b895c9a6 | 6339 | io_req_set_rsrc_node(req); |
09bb8394 | 6340 | } else { |
c826bd7a | 6341 | trace_io_uring_file_get(ctx, fd); |
8da11c19 | 6342 | file = __io_file_get(state, fd); |
d44f554e JA |
6343 | |
6344 | /* we don't allow fixed io_uring files */ | |
6345 | if (file && unlikely(file->f_op == &io_uring_fops)) | |
6346 | io_req_track_inflight(req); | |
09bb8394 JA |
6347 | } |
6348 | ||
8371adf5 | 6349 | return file; |
09bb8394 JA |
6350 | } |
6351 | ||
2665abfd | 6352 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6353 | { |
ad8a48ac JA |
6354 | struct io_timeout_data *data = container_of(timer, |
6355 | struct io_timeout_data, timer); | |
90cd7e42 | 6356 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6357 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6358 | unsigned long flags; |
2665abfd JA |
6359 | |
6360 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
90cd7e42 PB |
6361 | prev = req->timeout.head; |
6362 | req->timeout.head = NULL; | |
2665abfd JA |
6363 | |
6364 | /* | |
6365 | * We don't expect the list to be empty, that will only happen if we | |
6366 | * race with the completion of the linked work. | |
6367 | */ | |
de9b4cca | 6368 | if (prev && req_ref_inc_not_zero(prev)) |
f2f87370 | 6369 | io_remove_next_linked(prev); |
90cd7e42 PB |
6370 | else |
6371 | prev = NULL; | |
2665abfd JA |
6372 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
6373 | ||
6374 | if (prev) { | |
014db007 | 6375 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
9ae1f8dd | 6376 | io_put_req_deferred(prev, 1); |
47f46768 | 6377 | } else { |
9ae1f8dd | 6378 | io_req_complete_post(req, -ETIME, 0); |
2665abfd | 6379 | } |
df9727af | 6380 | io_put_req_deferred(req, 1); |
2665abfd JA |
6381 | return HRTIMER_NORESTART; |
6382 | } | |
6383 | ||
de968c18 | 6384 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6385 | { |
de968c18 PB |
6386 | struct io_ring_ctx *ctx = req->ctx; |
6387 | ||
6388 | spin_lock_irq(&ctx->completion_lock); | |
76a46e06 | 6389 | /* |
f2f87370 PB |
6390 | * If the back reference is NULL, then our linked request finished |
6391 | * before we got a chance to setup the timer | |
76a46e06 | 6392 | */ |
90cd7e42 | 6393 | if (req->timeout.head) { |
e8c2bc1f | 6394 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6395 | |
ad8a48ac JA |
6396 | data->timer.function = io_link_timeout_fn; |
6397 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6398 | data->mode); | |
2665abfd | 6399 | } |
76a46e06 | 6400 | spin_unlock_irq(&ctx->completion_lock); |
2665abfd | 6401 | /* drop submission reference */ |
76a46e06 JA |
6402 | io_put_req(req); |
6403 | } | |
2665abfd | 6404 | |
ad8a48ac | 6405 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
2665abfd | 6406 | { |
f2f87370 | 6407 | struct io_kiocb *nxt = req->link; |
2665abfd | 6408 | |
f2f87370 PB |
6409 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
6410 | nxt->opcode != IORING_OP_LINK_TIMEOUT) | |
76a46e06 | 6411 | return NULL; |
2665abfd | 6412 | |
90cd7e42 | 6413 | nxt->timeout.head = req; |
900fad45 | 6414 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
76a46e06 | 6415 | req->flags |= REQ_F_LINK_TIMEOUT; |
76a46e06 | 6416 | return nxt; |
2665abfd JA |
6417 | } |
6418 | ||
c5eef2b9 | 6419 | static void __io_queue_sqe(struct io_kiocb *req) |
2b188cc1 | 6420 | { |
d3d7298d | 6421 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
e0c5c576 | 6422 | int ret; |
2b188cc1 | 6423 | |
c5eef2b9 | 6424 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 6425 | |
491381ce JA |
6426 | /* |
6427 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6428 | * doesn't support non-blocking read/write attempts | |
6429 | */ | |
1840038e | 6430 | if (likely(!ret)) { |
0d63c148 | 6431 | /* drop submission reference */ |
e342c807 | 6432 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
c5eef2b9 PB |
6433 | struct io_ring_ctx *ctx = req->ctx; |
6434 | struct io_comp_state *cs = &ctx->submit_state.comp; | |
e65ef56d | 6435 | |
6dd0be1e | 6436 | cs->reqs[cs->nr++] = req; |
d3d7298d | 6437 | if (cs->nr == ARRAY_SIZE(cs->reqs)) |
c5eef2b9 | 6438 | io_submit_flush_completions(cs, ctx); |
9affd664 | 6439 | } else { |
d3d7298d | 6440 | io_put_req(req); |
0d63c148 | 6441 | } |
1840038e PB |
6442 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
6443 | if (!io_arm_poll_handler(req)) { | |
6444 | /* | |
6445 | * Queued up for async execution, worker will release | |
6446 | * submit reference when the iocb is actually submitted. | |
6447 | */ | |
6448 | io_queue_async_work(req); | |
6449 | } | |
0d63c148 | 6450 | } else { |
f41db273 | 6451 | io_req_complete_failed(req, ret); |
9e645e11 | 6452 | } |
d3d7298d PB |
6453 | if (linked_timeout) |
6454 | io_queue_linked_timeout(linked_timeout); | |
2b188cc1 JA |
6455 | } |
6456 | ||
be7053b7 | 6457 | static void io_queue_sqe(struct io_kiocb *req) |
4fe2c963 JL |
6458 | { |
6459 | int ret; | |
6460 | ||
be7053b7 | 6461 | ret = io_req_defer(req); |
4fe2c963 JL |
6462 | if (ret) { |
6463 | if (ret != -EIOCBQUEUED) { | |
1118591a | 6464 | fail_req: |
f41db273 | 6465 | io_req_complete_failed(req, ret); |
4fe2c963 | 6466 | } |
2550878f | 6467 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
b7e298d2 | 6468 | ret = io_req_prep_async(req); |
be7053b7 PB |
6469 | if (unlikely(ret)) |
6470 | goto fail_req; | |
ce35a47a JA |
6471 | io_queue_async_work(req); |
6472 | } else { | |
c5eef2b9 | 6473 | __io_queue_sqe(req); |
ce35a47a | 6474 | } |
4fe2c963 JL |
6475 | } |
6476 | ||
b16fed66 PB |
6477 | /* |
6478 | * Check SQE restrictions (opcode and flags). | |
6479 | * | |
6480 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6481 | */ | |
6482 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6483 | struct io_kiocb *req, | |
6484 | unsigned int sqe_flags) | |
4fe2c963 | 6485 | { |
b16fed66 PB |
6486 | if (!ctx->restricted) |
6487 | return true; | |
6488 | ||
6489 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) | |
6490 | return false; | |
6491 | ||
6492 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6493 | ctx->restrictions.sqe_flags_required) | |
6494 | return false; | |
6495 | ||
6496 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
6497 | ctx->restrictions.sqe_flags_required)) | |
6498 | return false; | |
6499 | ||
6500 | return true; | |
4fe2c963 JL |
6501 | } |
6502 | ||
b16fed66 PB |
6503 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
6504 | const struct io_uring_sqe *sqe) | |
6505 | { | |
6506 | struct io_submit_state *state; | |
6507 | unsigned int sqe_flags; | |
003e8dcc | 6508 | int personality, ret = 0; |
b16fed66 PB |
6509 | |
6510 | req->opcode = READ_ONCE(sqe->opcode); | |
6511 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
6512 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
6513 | req->user_data = READ_ONCE(sqe->user_data); | |
6514 | req->async_data = NULL; | |
6515 | req->file = NULL; | |
6516 | req->ctx = ctx; | |
6517 | req->link = NULL; | |
6518 | req->fixed_rsrc_refs = NULL; | |
6519 | /* one is dropped after submission, the other at completion */ | |
abc54d63 | 6520 | atomic_set(&req->refs, 2); |
b16fed66 PB |
6521 | req->task = current; |
6522 | req->result = 0; | |
93e68e03 | 6523 | req->work.creds = NULL; |
b16fed66 PB |
6524 | |
6525 | /* enforce forwards compatibility on users */ | |
ebf4a5db PB |
6526 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
6527 | req->flags = 0; | |
b16fed66 | 6528 | return -EINVAL; |
ebf4a5db | 6529 | } |
b16fed66 PB |
6530 | |
6531 | if (unlikely(req->opcode >= IORING_OP_LAST)) | |
6532 | return -EINVAL; | |
6533 | ||
b16fed66 PB |
6534 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
6535 | return -EACCES; | |
6536 | ||
6537 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
6538 | !io_op_defs[req->opcode].buffer_select) | |
6539 | return -EOPNOTSUPP; | |
863e0560 | 6540 | |
003e8dcc JA |
6541 | personality = READ_ONCE(sqe->personality); |
6542 | if (personality) { | |
61cf9370 | 6543 | req->work.creds = xa_load(&ctx->personalities, personality); |
003e8dcc JA |
6544 | if (!req->work.creds) |
6545 | return -EINVAL; | |
6546 | get_cred(req->work.creds); | |
003e8dcc | 6547 | } |
b16fed66 PB |
6548 | state = &ctx->submit_state; |
6549 | ||
6550 | /* | |
6551 | * Plug now if we have more than 1 IO left after this, and the target | |
6552 | * is potentially a read/write to block based storage. | |
6553 | */ | |
6554 | if (!state->plug_started && state->ios_left > 1 && | |
6555 | io_op_defs[req->opcode].plug) { | |
6556 | blk_start_plug(&state->plug); | |
6557 | state->plug_started = true; | |
6558 | } | |
6559 | ||
6560 | if (io_op_defs[req->opcode].needs_file) { | |
6561 | bool fixed = req->flags & REQ_F_FIXED_FILE; | |
6562 | ||
6563 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); | |
6564 | if (unlikely(!req->file)) | |
6565 | ret = -EBADF; | |
6566 | } | |
6567 | ||
6568 | state->ios_left--; | |
6569 | return ret; | |
6570 | } | |
6571 | ||
a6b8cadc | 6572 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 6573 | const struct io_uring_sqe *sqe) |
9e645e11 | 6574 | { |
a1ab7b35 | 6575 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 6576 | int ret; |
9e645e11 | 6577 | |
a6b8cadc PB |
6578 | ret = io_init_req(ctx, req, sqe); |
6579 | if (unlikely(ret)) { | |
6580 | fail_req: | |
de59bc10 PB |
6581 | if (link->head) { |
6582 | /* fail even hard links since we don't submit */ | |
cf109604 | 6583 | link->head->flags |= REQ_F_FAIL_LINK; |
f41db273 | 6584 | io_req_complete_failed(link->head, -ECANCELED); |
de59bc10 PB |
6585 | link->head = NULL; |
6586 | } | |
f41db273 | 6587 | io_req_complete_failed(req, ret); |
a6b8cadc PB |
6588 | return ret; |
6589 | } | |
be7053b7 PB |
6590 | ret = io_req_prep(req, sqe); |
6591 | if (unlikely(ret)) | |
6592 | goto fail_req; | |
a6b8cadc | 6593 | |
be7053b7 | 6594 | /* don't need @sqe from now on */ |
a6b8cadc PB |
6595 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
6596 | true, ctx->flags & IORING_SETUP_SQPOLL); | |
6597 | ||
9e645e11 JA |
6598 | /* |
6599 | * If we already have a head request, queue this one for async | |
6600 | * submittal once the head completes. If we don't have a head but | |
6601 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
6602 | * submitted sync once the chain is complete. If none of those | |
6603 | * conditions are true (normal request), then just queue it. | |
6604 | */ | |
863e0560 PB |
6605 | if (link->head) { |
6606 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 6607 | |
8cdf2193 PB |
6608 | /* |
6609 | * Taking sequential execution of a link, draining both sides | |
6610 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all | |
6611 | * requests in the link. So, it drains the head and the | |
6612 | * next after the link request. The last one is done via | |
6613 | * drain_next flag to persist the effect across calls. | |
6614 | */ | |
ef4ff581 | 6615 | if (req->flags & REQ_F_IO_DRAIN) { |
711be031 PB |
6616 | head->flags |= REQ_F_IO_DRAIN; |
6617 | ctx->drain_next = 1; | |
6618 | } | |
b7e298d2 | 6619 | ret = io_req_prep_async(req); |
cf109604 | 6620 | if (unlikely(ret)) |
a6b8cadc | 6621 | goto fail_req; |
9d76377f | 6622 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 6623 | link->last->link = req; |
863e0560 | 6624 | link->last = req; |
32fe525b PB |
6625 | |
6626 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 6627 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
de59bc10 | 6628 | io_queue_sqe(head); |
863e0560 | 6629 | link->head = NULL; |
32fe525b | 6630 | } |
9e645e11 | 6631 | } else { |
711be031 PB |
6632 | if (unlikely(ctx->drain_next)) { |
6633 | req->flags |= REQ_F_IO_DRAIN; | |
ef4ff581 | 6634 | ctx->drain_next = 0; |
711be031 | 6635 | } |
ef4ff581 | 6636 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
863e0560 PB |
6637 | link->head = req; |
6638 | link->last = req; | |
711be031 | 6639 | } else { |
be7053b7 | 6640 | io_queue_sqe(req); |
711be031 | 6641 | } |
9e645e11 | 6642 | } |
2e6e1fde | 6643 | |
1d4240cc | 6644 | return 0; |
9e645e11 JA |
6645 | } |
6646 | ||
9a56a232 JA |
6647 | /* |
6648 | * Batched submission is done, ensure local IO is flushed out. | |
6649 | */ | |
ba88ff11 PB |
6650 | static void io_submit_state_end(struct io_submit_state *state, |
6651 | struct io_ring_ctx *ctx) | |
9a56a232 | 6652 | { |
a1ab7b35 | 6653 | if (state->link.head) |
de59bc10 | 6654 | io_queue_sqe(state->link.head); |
6dd0be1e | 6655 | if (state->comp.nr) |
ba88ff11 | 6656 | io_submit_flush_completions(&state->comp, ctx); |
27926b68 JA |
6657 | if (state->plug_started) |
6658 | blk_finish_plug(&state->plug); | |
9f13c35b | 6659 | io_state_file_put(state); |
9a56a232 JA |
6660 | } |
6661 | ||
6662 | /* | |
6663 | * Start submission side cache. | |
6664 | */ | |
6665 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 6666 | unsigned int max_ios) |
9a56a232 | 6667 | { |
27926b68 | 6668 | state->plug_started = false; |
9a56a232 | 6669 | state->ios_left = max_ios; |
a1ab7b35 PB |
6670 | /* set only head, no need to init link_last in advance */ |
6671 | state->link.head = NULL; | |
9a56a232 JA |
6672 | } |
6673 | ||
2b188cc1 JA |
6674 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
6675 | { | |
75b28aff | 6676 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 6677 | |
caf582c6 PB |
6678 | /* |
6679 | * Ensure any loads from the SQEs are done at this point, | |
6680 | * since once we write the new head, the application could | |
6681 | * write new data to them. | |
6682 | */ | |
6683 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
6684 | } |
6685 | ||
2b188cc1 | 6686 | /* |
3529d8c2 | 6687 | * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory |
2b188cc1 JA |
6688 | * that is mapped by userspace. This means that care needs to be taken to |
6689 | * ensure that reads are stable, as we cannot rely on userspace always | |
6690 | * being a good citizen. If members of the sqe are validated and then later | |
6691 | * used, it's important that those reads are done through READ_ONCE() to | |
6692 | * prevent a re-load down the line. | |
6693 | */ | |
709b302f | 6694 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 6695 | { |
75b28aff | 6696 | u32 *sq_array = ctx->sq_array; |
2b188cc1 JA |
6697 | unsigned head; |
6698 | ||
6699 | /* | |
6700 | * The cached sq head (or cq tail) serves two purposes: | |
6701 | * | |
6702 | * 1) allows us to batch the cost of updating the user visible | |
6703 | * head updates. | |
6704 | * 2) allows the kernel side to track the head on its own, even | |
6705 | * though the application is the one updating it. | |
6706 | */ | |
4fccfcbb | 6707 | head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]); |
709b302f PB |
6708 | if (likely(head < ctx->sq_entries)) |
6709 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
6710 | |
6711 | /* drop invalid entries */ | |
498ccd9e | 6712 | ctx->cached_sq_dropped++; |
ee7d46d9 | 6713 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
709b302f PB |
6714 | return NULL; |
6715 | } | |
6716 | ||
0f212204 | 6717 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
6c271ce2 | 6718 | { |
46c4e16a | 6719 | int submitted = 0; |
6c271ce2 | 6720 | |
c4a2ed72 | 6721 | /* if we have a backlog and couldn't flush it all, return BUSY */ |
ad3eb2c8 | 6722 | if (test_bit(0, &ctx->sq_check_overflow)) { |
6c2450ae | 6723 | if (!__io_cqring_overflow_flush(ctx, false)) |
ad3eb2c8 JA |
6724 | return -EBUSY; |
6725 | } | |
6c271ce2 | 6726 | |
ee7d46d9 PB |
6727 | /* make sure SQ entry isn't read before tail */ |
6728 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
9ef4f124 | 6729 | |
2b85edfc PB |
6730 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
6731 | return -EAGAIN; | |
6c271ce2 | 6732 | |
d8a6df10 | 6733 | percpu_counter_add(¤t->io_uring->inflight, nr); |
faf7b51c | 6734 | refcount_add(nr, ¤t->usage); |
ba88ff11 | 6735 | io_submit_state_start(&ctx->submit_state, nr); |
b14cca0c | 6736 | |
46c4e16a | 6737 | while (submitted < nr) { |
3529d8c2 | 6738 | const struct io_uring_sqe *sqe; |
196be95c | 6739 | struct io_kiocb *req; |
fb5ccc98 | 6740 | |
258b29a9 | 6741 | req = io_alloc_req(ctx); |
196be95c PB |
6742 | if (unlikely(!req)) { |
6743 | if (!submitted) | |
6744 | submitted = -EAGAIN; | |
fb5ccc98 | 6745 | break; |
196be95c | 6746 | } |
4fccfcbb PB |
6747 | sqe = io_get_sqe(ctx); |
6748 | if (unlikely(!sqe)) { | |
6749 | kmem_cache_free(req_cachep, req); | |
6750 | break; | |
6751 | } | |
d3656344 JA |
6752 | /* will complete beyond this point, count as submitted */ |
6753 | submitted++; | |
a1ab7b35 | 6754 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 6755 | break; |
6c271ce2 JA |
6756 | } |
6757 | ||
9466f437 PB |
6758 | if (unlikely(submitted != nr)) { |
6759 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 JA |
6760 | struct io_uring_task *tctx = current->io_uring; |
6761 | int unused = nr - ref_used; | |
9466f437 | 6762 | |
d8a6df10 JA |
6763 | percpu_ref_put_many(&ctx->refs, unused); |
6764 | percpu_counter_sub(&tctx->inflight, unused); | |
6765 | put_task_struct_many(current, unused); | |
9466f437 | 6766 | } |
6c271ce2 | 6767 | |
a1ab7b35 | 6768 | io_submit_state_end(&ctx->submit_state, ctx); |
ae9428ca PB |
6769 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
6770 | io_commit_sqring(ctx); | |
6771 | ||
6c271ce2 JA |
6772 | return submitted; |
6773 | } | |
6774 | ||
23b3628e XW |
6775 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
6776 | { | |
6777 | /* Tell userspace we may need a wakeup call */ | |
6778 | spin_lock_irq(&ctx->completion_lock); | |
6779 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; | |
6780 | spin_unlock_irq(&ctx->completion_lock); | |
6781 | } | |
6782 | ||
6783 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
6784 | { | |
6785 | spin_lock_irq(&ctx->completion_lock); | |
6786 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; | |
6787 | spin_unlock_irq(&ctx->completion_lock); | |
6788 | } | |
6789 | ||
08369246 | 6790 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 6791 | { |
c8d1ba58 | 6792 | unsigned int to_submit; |
bdcd3eab | 6793 | int ret = 0; |
6c271ce2 | 6794 | |
c8d1ba58 | 6795 | to_submit = io_sqring_entries(ctx); |
e95eee2d JA |
6796 | /* if we're handling multiple rings, cap submit size for fairness */ |
6797 | if (cap_entries && to_submit > 8) | |
6798 | to_submit = 8; | |
6799 | ||
906a3c6f | 6800 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
c8d1ba58 | 6801 | unsigned nr_events = 0; |
a4c0b3de | 6802 | |
c8d1ba58 | 6803 | mutex_lock(&ctx->uring_lock); |
906a3c6f | 6804 | if (!list_empty(&ctx->iopoll_list)) |
c8d1ba58 | 6805 | io_do_iopoll(ctx, &nr_events, 0); |
906a3c6f | 6806 | |
0298ef96 PB |
6807 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
6808 | !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
08369246 | 6809 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 JA |
6810 | mutex_unlock(&ctx->uring_lock); |
6811 | } | |
6c271ce2 | 6812 | |
90554200 JA |
6813 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) |
6814 | wake_up(&ctx->sqo_sq_wait); | |
6c271ce2 | 6815 | |
08369246 XW |
6816 | return ret; |
6817 | } | |
6c271ce2 | 6818 | |
08369246 XW |
6819 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
6820 | { | |
6821 | struct io_ring_ctx *ctx; | |
6822 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 6823 | |
c9dca27d PB |
6824 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6825 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); | |
08369246 | 6826 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 6827 | } |
6c271ce2 | 6828 | |
c8d1ba58 JA |
6829 | static int io_sq_thread(void *data) |
6830 | { | |
69fb2131 JA |
6831 | struct io_sq_data *sqd = data; |
6832 | struct io_ring_ctx *ctx; | |
a0d9205f | 6833 | unsigned long timeout = 0; |
37d1e2e3 | 6834 | char buf[TASK_COMM_LEN]; |
08369246 | 6835 | DEFINE_WAIT(wait); |
6c271ce2 | 6836 | |
696ee88a | 6837 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
37d1e2e3 | 6838 | set_task_comm(current, buf); |
37d1e2e3 JA |
6839 | current->pf_io_worker = NULL; |
6840 | ||
6841 | if (sqd->sq_cpu != -1) | |
6842 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
6843 | else | |
6844 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
6845 | current->flags |= PF_NO_SETAFFINITY; | |
6846 | ||
09a6f4ef | 6847 | mutex_lock(&sqd->lock); |
05962f95 | 6848 | while (!test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)) { |
08369246 XW |
6849 | int ret; |
6850 | bool cap_entries, sqt_spin, needs_sched; | |
c1edbf5f | 6851 | |
82734c5b JA |
6852 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || |
6853 | signal_pending(current)) { | |
6854 | bool did_sig = false; | |
6855 | ||
09a6f4ef | 6856 | mutex_unlock(&sqd->lock); |
82734c5b JA |
6857 | if (signal_pending(current)) { |
6858 | struct ksignal ksig; | |
6859 | ||
6860 | did_sig = get_signal(&ksig); | |
6861 | } | |
05962f95 | 6862 | cond_resched(); |
09a6f4ef | 6863 | mutex_lock(&sqd->lock); |
82734c5b JA |
6864 | if (did_sig) |
6865 | break; | |
521d6a73 | 6866 | io_run_task_work(); |
b7f5a0bf | 6867 | io_run_task_work_head(&sqd->park_task_work); |
08369246 | 6868 | timeout = jiffies + sqd->sq_thread_idle; |
7d41e854 | 6869 | continue; |
08369246 | 6870 | } |
08369246 | 6871 | sqt_spin = false; |
e95eee2d | 6872 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 6873 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
7c30f36a SM |
6874 | const struct cred *creds = NULL; |
6875 | ||
6876 | if (ctx->sq_creds != current_cred()) | |
6877 | creds = override_creds(ctx->sq_creds); | |
08369246 | 6878 | ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a SM |
6879 | if (creds) |
6880 | revert_creds(creds); | |
08369246 XW |
6881 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) |
6882 | sqt_spin = true; | |
69fb2131 | 6883 | } |
6c271ce2 | 6884 | |
08369246 | 6885 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 JA |
6886 | io_run_task_work(); |
6887 | cond_resched(); | |
08369246 XW |
6888 | if (sqt_spin) |
6889 | timeout = jiffies + sqd->sq_thread_idle; | |
6890 | continue; | |
6891 | } | |
6892 | ||
08369246 XW |
6893 | needs_sched = true; |
6894 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); | |
6895 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { | |
6896 | if ((ctx->flags & IORING_SETUP_IOPOLL) && | |
6897 | !list_empty_careful(&ctx->iopoll_list)) { | |
6898 | needs_sched = false; | |
6899 | break; | |
6900 | } | |
6901 | if (io_sqring_entries(ctx)) { | |
6902 | needs_sched = false; | |
6903 | break; | |
6904 | } | |
6905 | } | |
6906 | ||
05962f95 | 6907 | if (needs_sched && !test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state)) { |
69fb2131 JA |
6908 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6909 | io_ring_set_wakeup_flag(ctx); | |
08369246 | 6910 | |
09a6f4ef | 6911 | mutex_unlock(&sqd->lock); |
69fb2131 | 6912 | schedule(); |
09a6f4ef | 6913 | mutex_lock(&sqd->lock); |
69fb2131 JA |
6914 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6915 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 6916 | } |
08369246 XW |
6917 | |
6918 | finish_wait(&sqd->wait, &wait); | |
b7f5a0bf | 6919 | io_run_task_work_head(&sqd->park_task_work); |
08369246 | 6920 | timeout = jiffies + sqd->sq_thread_idle; |
6c271ce2 | 6921 | } |
28cea78a | 6922 | |
521d6a73 PB |
6923 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6924 | io_uring_cancel_sqpoll(ctx); | |
37d1e2e3 | 6925 | sqd->thread = NULL; |
05962f95 | 6926 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
5f3f26f9 | 6927 | io_ring_set_wakeup_flag(ctx); |
09a6f4ef | 6928 | mutex_unlock(&sqd->lock); |
521d6a73 PB |
6929 | |
6930 | io_run_task_work(); | |
b7f5a0bf | 6931 | io_run_task_work_head(&sqd->park_task_work); |
37d1e2e3 JA |
6932 | complete(&sqd->exited); |
6933 | do_exit(0); | |
6c271ce2 JA |
6934 | } |
6935 | ||
bda52162 JA |
6936 | struct io_wait_queue { |
6937 | struct wait_queue_entry wq; | |
6938 | struct io_ring_ctx *ctx; | |
6939 | unsigned to_wait; | |
6940 | unsigned nr_timeouts; | |
6941 | }; | |
6942 | ||
6c503150 | 6943 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
6944 | { |
6945 | struct io_ring_ctx *ctx = iowq->ctx; | |
6946 | ||
6947 | /* | |
d195a66e | 6948 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
6949 | * started waiting. For timeouts, we always want to return to userspace, |
6950 | * regardless of event count. | |
6951 | */ | |
6c503150 | 6952 | return io_cqring_events(ctx) >= iowq->to_wait || |
bda52162 JA |
6953 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
6954 | } | |
6955 | ||
6956 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
6957 | int wake_flags, void *key) | |
6958 | { | |
6959 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
6960 | wq); | |
6961 | ||
6c503150 PB |
6962 | /* |
6963 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
6964 | * the task, and the next invocation will do it. | |
6965 | */ | |
6966 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) | |
6967 | return autoremove_wake_function(curr, mode, wake_flags, key); | |
6968 | return -1; | |
bda52162 JA |
6969 | } |
6970 | ||
af9c1a44 JA |
6971 | static int io_run_task_work_sig(void) |
6972 | { | |
6973 | if (io_run_task_work()) | |
6974 | return 1; | |
6975 | if (!signal_pending(current)) | |
6976 | return 0; | |
0b8cfa97 | 6977 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
792ee0f6 | 6978 | return -ERESTARTSYS; |
af9c1a44 JA |
6979 | return -EINTR; |
6980 | } | |
6981 | ||
eeb60b9a PB |
6982 | /* when returns >0, the caller should retry */ |
6983 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
6984 | struct io_wait_queue *iowq, | |
6985 | signed long *timeout) | |
6986 | { | |
6987 | int ret; | |
6988 | ||
6989 | /* make sure we run task_work before checking for signals */ | |
6990 | ret = io_run_task_work_sig(); | |
6991 | if (ret || io_should_wake(iowq)) | |
6992 | return ret; | |
6993 | /* let the caller flush overflows, retry */ | |
6994 | if (test_bit(0, &ctx->cq_check_overflow)) | |
6995 | return 1; | |
6996 | ||
6997 | *timeout = schedule_timeout(*timeout); | |
6998 | return !*timeout ? -ETIME : 1; | |
6999 | } | |
7000 | ||
2b188cc1 JA |
7001 | /* |
7002 | * Wait until events become available, if we don't already have some. The | |
7003 | * application must reap them itself, as they reside on the shared cq ring. | |
7004 | */ | |
7005 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
7006 | const sigset_t __user *sig, size_t sigsz, |
7007 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 7008 | { |
bda52162 JA |
7009 | struct io_wait_queue iowq = { |
7010 | .wq = { | |
7011 | .private = current, | |
7012 | .func = io_wake_function, | |
7013 | .entry = LIST_HEAD_INIT(iowq.wq.entry), | |
7014 | }, | |
7015 | .ctx = ctx, | |
7016 | .to_wait = min_events, | |
7017 | }; | |
75b28aff | 7018 | struct io_rings *rings = ctx->rings; |
c1d5a224 PB |
7019 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
7020 | int ret; | |
2b188cc1 | 7021 | |
b41e9852 | 7022 | do { |
6c2450ae | 7023 | io_cqring_overflow_flush(ctx, false); |
6c503150 | 7024 | if (io_cqring_events(ctx) >= min_events) |
b41e9852 | 7025 | return 0; |
4c6e277c | 7026 | if (!io_run_task_work()) |
b41e9852 | 7027 | break; |
b41e9852 | 7028 | } while (1); |
2b188cc1 JA |
7029 | |
7030 | if (sig) { | |
9e75ad5d AB |
7031 | #ifdef CONFIG_COMPAT |
7032 | if (in_compat_syscall()) | |
7033 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 7034 | sigsz); |
9e75ad5d AB |
7035 | else |
7036 | #endif | |
b772434b | 7037 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 7038 | |
2b188cc1 JA |
7039 | if (ret) |
7040 | return ret; | |
7041 | } | |
7042 | ||
c73ebb68 | 7043 | if (uts) { |
c1d5a224 PB |
7044 | struct timespec64 ts; |
7045 | ||
c73ebb68 HX |
7046 | if (get_timespec64(&ts, uts)) |
7047 | return -EFAULT; | |
7048 | timeout = timespec64_to_jiffies(&ts); | |
7049 | } | |
7050 | ||
bda52162 | 7051 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
c826bd7a | 7052 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 7053 | do { |
ca0a2651 | 7054 | /* if we can't even flush overflow, don't wait for more */ |
6c2450ae | 7055 | if (!io_cqring_overflow_flush(ctx, false)) { |
ca0a2651 JA |
7056 | ret = -EBUSY; |
7057 | break; | |
7058 | } | |
bda52162 JA |
7059 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
7060 | TASK_INTERRUPTIBLE); | |
eeb60b9a PB |
7061 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
7062 | finish_wait(&ctx->wait, &iowq.wq); | |
ca0a2651 | 7063 | cond_resched(); |
eeb60b9a | 7064 | } while (ret > 0); |
bda52162 | 7065 | |
b7db41c9 | 7066 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7067 | |
75b28aff | 7068 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7069 | } |
7070 | ||
aeca241b | 7071 | static void io_free_file_tables(struct io_file_table *table, unsigned nr_files) |
846a4ef2 PB |
7072 | { |
7073 | unsigned i, nr_tables = DIV_ROUND_UP(nr_files, IORING_MAX_FILES_TABLE); | |
7074 | ||
7075 | for (i = 0; i < nr_tables; i++) | |
aeca241b PB |
7076 | kfree(table->files[i]); |
7077 | kfree(table->files); | |
7078 | table->files = NULL; | |
846a4ef2 PB |
7079 | } |
7080 | ||
6b06314c JA |
7081 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7082 | { | |
7083 | #if defined(CONFIG_UNIX) | |
7084 | if (ctx->ring_sock) { | |
7085 | struct sock *sock = ctx->ring_sock->sk; | |
7086 | struct sk_buff *skb; | |
7087 | ||
7088 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
7089 | kfree_skb(skb); | |
7090 | } | |
7091 | #else | |
7092 | int i; | |
7093 | ||
65e19f54 JA |
7094 | for (i = 0; i < ctx->nr_user_files; i++) { |
7095 | struct file *file; | |
7096 | ||
7097 | file = io_file_from_index(ctx, i); | |
7098 | if (file) | |
7099 | fput(file); | |
7100 | } | |
6b06314c | 7101 | #endif |
08480400 PB |
7102 | io_free_file_tables(&ctx->file_table, ctx->nr_user_files); |
7103 | kfree(ctx->file_data); | |
7104 | ctx->file_data = NULL; | |
7105 | ctx->nr_user_files = 0; | |
6b06314c JA |
7106 | } |
7107 | ||
2a63b2d9 | 7108 | static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx) |
1642b445 | 7109 | { |
2a63b2d9 | 7110 | spin_lock_bh(&ctx->rsrc_ref_lock); |
1642b445 PB |
7111 | } |
7112 | ||
2a63b2d9 | 7113 | static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx) |
6b06314c | 7114 | { |
2a63b2d9 BM |
7115 | spin_unlock_bh(&ctx->rsrc_ref_lock); |
7116 | } | |
65e19f54 | 7117 | |
28a9fe25 PB |
7118 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
7119 | { | |
7120 | percpu_ref_exit(&ref_node->refs); | |
7121 | kfree(ref_node); | |
7122 | } | |
7123 | ||
a7f0ed5a PB |
7124 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
7125 | struct io_rsrc_data *data_to_kill) | |
1642b445 | 7126 | { |
a7f0ed5a PB |
7127 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
7128 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); | |
82fbcfa9 | 7129 | |
a7f0ed5a PB |
7130 | if (data_to_kill) { |
7131 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; | |
82fbcfa9 | 7132 | |
a7f0ed5a PB |
7133 | rsrc_node->rsrc_data = data_to_kill; |
7134 | io_rsrc_ref_lock(ctx); | |
7135 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); | |
7136 | io_rsrc_ref_unlock(ctx); | |
82fbcfa9 | 7137 | |
3e942498 | 7138 | atomic_inc(&data_to_kill->refs); |
a7f0ed5a PB |
7139 | percpu_ref_kill(&rsrc_node->refs); |
7140 | ctx->rsrc_node = NULL; | |
7141 | } | |
6b06314c | 7142 | |
a7f0ed5a PB |
7143 | if (!ctx->rsrc_node) { |
7144 | ctx->rsrc_node = ctx->rsrc_backup_node; | |
7145 | ctx->rsrc_backup_node = NULL; | |
7146 | } | |
8bad28d8 HX |
7147 | } |
7148 | ||
a7f0ed5a | 7149 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
8dd03afe PB |
7150 | { |
7151 | if (ctx->rsrc_backup_node) | |
7152 | return 0; | |
b895c9a6 | 7153 | ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx); |
8dd03afe PB |
7154 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
7155 | } | |
7156 | ||
40ae0ff7 | 7157 | static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, struct io_ring_ctx *ctx) |
8bad28d8 HX |
7158 | { |
7159 | int ret; | |
05589553 | 7160 | |
215c3902 | 7161 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
8bad28d8 HX |
7162 | if (data->quiesce) |
7163 | return -ENXIO; | |
05589553 | 7164 | |
8bad28d8 | 7165 | data->quiesce = true; |
1ffc5422 | 7166 | do { |
a7f0ed5a | 7167 | ret = io_rsrc_node_switch_start(ctx); |
8dd03afe | 7168 | if (ret) |
f2303b1f | 7169 | break; |
a7f0ed5a PB |
7170 | io_rsrc_node_switch(ctx, data); |
7171 | ||
3e942498 PB |
7172 | /* kill initial ref, already quiesced if zero */ |
7173 | if (atomic_dec_and_test(&data->refs)) | |
7174 | break; | |
8bad28d8 | 7175 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 PB |
7176 | ret = wait_for_completion_interruptible(&data->done); |
7177 | if (!ret) | |
7178 | break; | |
8bad28d8 | 7179 | |
3e942498 PB |
7180 | atomic_inc(&data->refs); |
7181 | /* wait for all works potentially completing data->done */ | |
7182 | flush_delayed_work(&ctx->rsrc_put_work); | |
cb5e1b81 | 7183 | reinit_completion(&data->done); |
8dd03afe | 7184 | |
8bad28d8 | 7185 | mutex_unlock(&ctx->uring_lock); |
1ffc5422 | 7186 | ret = io_run_task_work_sig(); |
8bad28d8 | 7187 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 7188 | } while (ret >= 0); |
8bad28d8 | 7189 | data->quiesce = false; |
05f3fb3c | 7190 | |
8bad28d8 | 7191 | return ret; |
d7954b2b BM |
7192 | } |
7193 | ||
40ae0ff7 PB |
7194 | static struct io_rsrc_data *io_rsrc_data_alloc(struct io_ring_ctx *ctx, |
7195 | rsrc_put_fn *do_put) | |
1ad555c6 | 7196 | { |
b895c9a6 | 7197 | struct io_rsrc_data *data; |
1ad555c6 BM |
7198 | |
7199 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7200 | if (!data) | |
7201 | return NULL; | |
7202 | ||
3e942498 | 7203 | atomic_set(&data->refs, 1); |
1ad555c6 | 7204 | data->ctx = ctx; |
40ae0ff7 | 7205 | data->do_put = do_put; |
1ad555c6 BM |
7206 | init_completion(&data->done); |
7207 | return data; | |
7208 | } | |
7209 | ||
d7954b2b BM |
7210 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7211 | { | |
d7954b2b BM |
7212 | int ret; |
7213 | ||
08480400 | 7214 | if (!ctx->file_data) |
d7954b2b | 7215 | return -ENXIO; |
08480400 PB |
7216 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
7217 | if (!ret) | |
7218 | __io_sqe_files_unregister(ctx); | |
7219 | return ret; | |
6b06314c JA |
7220 | } |
7221 | ||
37d1e2e3 | 7222 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
09a6f4ef | 7223 | __releases(&sqd->lock) |
37d1e2e3 | 7224 | { |
521d6a73 PB |
7225 | WARN_ON_ONCE(sqd->thread == current); |
7226 | ||
9e138a48 PB |
7227 | /* |
7228 | * Do the dance but not conditional clear_bit() because it'd race with | |
7229 | * other threads incrementing park_pending and setting the bit. | |
7230 | */ | |
37d1e2e3 | 7231 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
9e138a48 PB |
7232 | if (atomic_dec_return(&sqd->park_pending)) |
7233 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
09a6f4ef | 7234 | mutex_unlock(&sqd->lock); |
37d1e2e3 JA |
7235 | } |
7236 | ||
86e0d676 | 7237 | static void io_sq_thread_park(struct io_sq_data *sqd) |
09a6f4ef | 7238 | __acquires(&sqd->lock) |
37d1e2e3 | 7239 | { |
521d6a73 PB |
7240 | WARN_ON_ONCE(sqd->thread == current); |
7241 | ||
9e138a48 | 7242 | atomic_inc(&sqd->park_pending); |
86e0d676 | 7243 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
09a6f4ef | 7244 | mutex_lock(&sqd->lock); |
05962f95 | 7245 | if (sqd->thread) |
86e0d676 | 7246 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
7247 | } |
7248 | ||
7249 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
7250 | { | |
521d6a73 | 7251 | WARN_ON_ONCE(sqd->thread == current); |
88885f66 | 7252 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
521d6a73 | 7253 | |
05962f95 | 7254 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
88885f66 | 7255 | mutex_lock(&sqd->lock); |
e8f98f24 JA |
7256 | if (sqd->thread) |
7257 | wake_up_process(sqd->thread); | |
09a6f4ef | 7258 | mutex_unlock(&sqd->lock); |
05962f95 | 7259 | wait_for_completion(&sqd->exited); |
37d1e2e3 JA |
7260 | } |
7261 | ||
534ca6d6 | 7262 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7263 | { |
534ca6d6 | 7264 | if (refcount_dec_and_test(&sqd->refs)) { |
9e138a48 PB |
7265 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
7266 | ||
37d1e2e3 JA |
7267 | io_sq_thread_stop(sqd); |
7268 | kfree(sqd); | |
7269 | } | |
7270 | } | |
7271 | ||
7272 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
7273 | { | |
7274 | struct io_sq_data *sqd = ctx->sq_data; | |
7275 | ||
7276 | if (sqd) { | |
05962f95 | 7277 | io_sq_thread_park(sqd); |
521d6a73 | 7278 | list_del_init(&ctx->sqd_list); |
37d1e2e3 | 7279 | io_sqd_update_thread_idle(sqd); |
05962f95 | 7280 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
7281 | |
7282 | io_put_sq_data(sqd); | |
7283 | ctx->sq_data = NULL; | |
7c30f36a SM |
7284 | if (ctx->sq_creds) |
7285 | put_cred(ctx->sq_creds); | |
534ca6d6 JA |
7286 | } |
7287 | } | |
7288 | ||
aa06165d JA |
7289 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7290 | { | |
7291 | struct io_ring_ctx *ctx_attach; | |
7292 | struct io_sq_data *sqd; | |
7293 | struct fd f; | |
7294 | ||
7295 | f = fdget(p->wq_fd); | |
7296 | if (!f.file) | |
7297 | return ERR_PTR(-ENXIO); | |
7298 | if (f.file->f_op != &io_uring_fops) { | |
7299 | fdput(f); | |
7300 | return ERR_PTR(-EINVAL); | |
7301 | } | |
7302 | ||
7303 | ctx_attach = f.file->private_data; | |
7304 | sqd = ctx_attach->sq_data; | |
7305 | if (!sqd) { | |
7306 | fdput(f); | |
7307 | return ERR_PTR(-EINVAL); | |
7308 | } | |
5c2469e0 JA |
7309 | if (sqd->task_tgid != current->tgid) { |
7310 | fdput(f); | |
7311 | return ERR_PTR(-EPERM); | |
7312 | } | |
aa06165d JA |
7313 | |
7314 | refcount_inc(&sqd->refs); | |
7315 | fdput(f); | |
7316 | return sqd; | |
7317 | } | |
7318 | ||
26984fbf PB |
7319 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
7320 | bool *attached) | |
534ca6d6 JA |
7321 | { |
7322 | struct io_sq_data *sqd; | |
7323 | ||
26984fbf | 7324 | *attached = false; |
5c2469e0 JA |
7325 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
7326 | sqd = io_attach_sq_data(p); | |
26984fbf PB |
7327 | if (!IS_ERR(sqd)) { |
7328 | *attached = true; | |
5c2469e0 | 7329 | return sqd; |
26984fbf | 7330 | } |
5c2469e0 JA |
7331 | /* fall through for EPERM case, setup new sqd/task */ |
7332 | if (PTR_ERR(sqd) != -EPERM) | |
7333 | return sqd; | |
7334 | } | |
aa06165d | 7335 | |
534ca6d6 JA |
7336 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7337 | if (!sqd) | |
7338 | return ERR_PTR(-ENOMEM); | |
7339 | ||
9e138a48 | 7340 | atomic_set(&sqd->park_pending, 0); |
534ca6d6 | 7341 | refcount_set(&sqd->refs, 1); |
69fb2131 | 7342 | INIT_LIST_HEAD(&sqd->ctx_list); |
09a6f4ef | 7343 | mutex_init(&sqd->lock); |
534ca6d6 | 7344 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 7345 | init_completion(&sqd->exited); |
534ca6d6 JA |
7346 | return sqd; |
7347 | } | |
7348 | ||
6b06314c | 7349 | #if defined(CONFIG_UNIX) |
6b06314c JA |
7350 | /* |
7351 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
7352 | * the io_uring can be safely unregistered on process exit, even if we have | |
7353 | * loops in the file referencing. | |
7354 | */ | |
7355 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
7356 | { | |
7357 | struct sock *sk = ctx->ring_sock->sk; | |
7358 | struct scm_fp_list *fpl; | |
7359 | struct sk_buff *skb; | |
08a45173 | 7360 | int i, nr_files; |
6b06314c | 7361 | |
6b06314c JA |
7362 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
7363 | if (!fpl) | |
7364 | return -ENOMEM; | |
7365 | ||
7366 | skb = alloc_skb(0, GFP_KERNEL); | |
7367 | if (!skb) { | |
7368 | kfree(fpl); | |
7369 | return -ENOMEM; | |
7370 | } | |
7371 | ||
7372 | skb->sk = sk; | |
6b06314c | 7373 | |
08a45173 | 7374 | nr_files = 0; |
62e398be | 7375 | fpl->user = get_uid(current_user()); |
6b06314c | 7376 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
7377 | struct file *file = io_file_from_index(ctx, i + offset); |
7378 | ||
7379 | if (!file) | |
08a45173 | 7380 | continue; |
65e19f54 | 7381 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
7382 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
7383 | nr_files++; | |
6b06314c JA |
7384 | } |
7385 | ||
08a45173 JA |
7386 | if (nr_files) { |
7387 | fpl->max = SCM_MAX_FD; | |
7388 | fpl->count = nr_files; | |
7389 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 7390 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
7391 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
7392 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 7393 | |
08a45173 JA |
7394 | for (i = 0; i < nr_files; i++) |
7395 | fput(fpl->fp[i]); | |
7396 | } else { | |
7397 | kfree_skb(skb); | |
7398 | kfree(fpl); | |
7399 | } | |
6b06314c JA |
7400 | |
7401 | return 0; | |
7402 | } | |
7403 | ||
7404 | /* | |
7405 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
7406 | * causes regular reference counting to break down. We rely on the UNIX | |
7407 | * garbage collection to take care of this problem for us. | |
7408 | */ | |
7409 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7410 | { | |
7411 | unsigned left, total; | |
7412 | int ret = 0; | |
7413 | ||
7414 | total = 0; | |
7415 | left = ctx->nr_user_files; | |
7416 | while (left) { | |
7417 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
7418 | |
7419 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
7420 | if (ret) | |
7421 | break; | |
7422 | left -= this_files; | |
7423 | total += this_files; | |
7424 | } | |
7425 | ||
7426 | if (!ret) | |
7427 | return 0; | |
7428 | ||
7429 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
7430 | struct file *file = io_file_from_index(ctx, total); |
7431 | ||
7432 | if (file) | |
7433 | fput(file); | |
6b06314c JA |
7434 | total++; |
7435 | } | |
7436 | ||
7437 | return ret; | |
7438 | } | |
7439 | #else | |
7440 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7441 | { | |
7442 | return 0; | |
7443 | } | |
7444 | #endif | |
7445 | ||
aeca241b | 7446 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
65e19f54 | 7447 | { |
846a4ef2 PB |
7448 | unsigned i, nr_tables = DIV_ROUND_UP(nr_files, IORING_MAX_FILES_TABLE); |
7449 | ||
aeca241b PB |
7450 | table->files = kcalloc(nr_tables, sizeof(*table->files), GFP_KERNEL); |
7451 | if (!table->files) | |
846a4ef2 | 7452 | return false; |
65e19f54 JA |
7453 | |
7454 | for (i = 0; i < nr_tables; i++) { | |
846a4ef2 | 7455 | unsigned int this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
65e19f54 | 7456 | |
aeca241b | 7457 | table->files[i] = kcalloc(this_files, sizeof(*table->files[i]), |
65e19f54 | 7458 | GFP_KERNEL); |
aeca241b | 7459 | if (!table->files[i]) |
65e19f54 JA |
7460 | break; |
7461 | nr_files -= this_files; | |
7462 | } | |
7463 | ||
7464 | if (i == nr_tables) | |
846a4ef2 | 7465 | return true; |
65e19f54 | 7466 | |
aeca241b | 7467 | io_free_file_tables(table, nr_tables * IORING_MAX_FILES_TABLE); |
846a4ef2 | 7468 | return false; |
65e19f54 JA |
7469 | } |
7470 | ||
47e90392 | 7471 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 7472 | { |
50238531 | 7473 | struct file *file = prsrc->file; |
05f3fb3c JA |
7474 | #if defined(CONFIG_UNIX) |
7475 | struct sock *sock = ctx->ring_sock->sk; | |
7476 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
7477 | struct sk_buff *skb; | |
7478 | int i; | |
7479 | ||
7480 | __skb_queue_head_init(&list); | |
7481 | ||
7482 | /* | |
7483 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
7484 | * remove this entry and rearrange the file array. | |
7485 | */ | |
7486 | skb = skb_dequeue(head); | |
7487 | while (skb) { | |
7488 | struct scm_fp_list *fp; | |
7489 | ||
7490 | fp = UNIXCB(skb).fp; | |
7491 | for (i = 0; i < fp->count; i++) { | |
7492 | int left; | |
7493 | ||
7494 | if (fp->fp[i] != file) | |
7495 | continue; | |
7496 | ||
7497 | unix_notinflight(fp->user, fp->fp[i]); | |
7498 | left = fp->count - 1 - i; | |
7499 | if (left) { | |
7500 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
7501 | left * sizeof(struct file *)); | |
7502 | } | |
7503 | fp->count--; | |
7504 | if (!fp->count) { | |
7505 | kfree_skb(skb); | |
7506 | skb = NULL; | |
7507 | } else { | |
7508 | __skb_queue_tail(&list, skb); | |
7509 | } | |
7510 | fput(file); | |
7511 | file = NULL; | |
7512 | break; | |
7513 | } | |
7514 | ||
7515 | if (!file) | |
7516 | break; | |
7517 | ||
7518 | __skb_queue_tail(&list, skb); | |
7519 | ||
7520 | skb = skb_dequeue(head); | |
7521 | } | |
7522 | ||
7523 | if (skb_peek(&list)) { | |
7524 | spin_lock_irq(&head->lock); | |
7525 | while ((skb = __skb_dequeue(&list)) != NULL) | |
7526 | __skb_queue_tail(head, skb); | |
7527 | spin_unlock_irq(&head->lock); | |
7528 | } | |
7529 | #else | |
7530 | fput(file); | |
7531 | #endif | |
7532 | } | |
7533 | ||
b895c9a6 | 7534 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
65e19f54 | 7535 | { |
b895c9a6 | 7536 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
269bbe5f BM |
7537 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
7538 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 7539 | |
269bbe5f BM |
7540 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
7541 | list_del(&prsrc->list); | |
40ae0ff7 | 7542 | rsrc_data->do_put(ctx, prsrc); |
269bbe5f | 7543 | kfree(prsrc); |
65e19f54 | 7544 | } |
05589553 | 7545 | |
28a9fe25 | 7546 | io_rsrc_node_destroy(ref_node); |
3e942498 PB |
7547 | if (atomic_dec_and_test(&rsrc_data->refs)) |
7548 | complete(&rsrc_data->done); | |
2faf852d | 7549 | } |
65e19f54 | 7550 | |
269bbe5f | 7551 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
7552 | { |
7553 | struct io_ring_ctx *ctx; | |
7554 | struct llist_node *node; | |
7555 | ||
269bbe5f BM |
7556 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
7557 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
7558 | |
7559 | while (node) { | |
b895c9a6 | 7560 | struct io_rsrc_node *ref_node; |
4a38aed2 JA |
7561 | struct llist_node *next = node->next; |
7562 | ||
b895c9a6 | 7563 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
269bbe5f | 7564 | __io_rsrc_put_work(ref_node); |
4a38aed2 JA |
7565 | node = next; |
7566 | } | |
7567 | } | |
7568 | ||
00835dce | 7569 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
2faf852d | 7570 | { |
b895c9a6 | 7571 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); |
3e942498 | 7572 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; |
e297822b | 7573 | bool first_add = false; |
65e19f54 | 7574 | |
2a63b2d9 | 7575 | io_rsrc_ref_lock(ctx); |
b895c9a6 | 7576 | node->done = true; |
e297822b | 7577 | |
d67d2263 | 7578 | while (!list_empty(&ctx->rsrc_ref_list)) { |
b895c9a6 PB |
7579 | node = list_first_entry(&ctx->rsrc_ref_list, |
7580 | struct io_rsrc_node, node); | |
e297822b | 7581 | /* recycle ref nodes in order */ |
b895c9a6 | 7582 | if (!node->done) |
e297822b | 7583 | break; |
b895c9a6 PB |
7584 | list_del(&node->node); |
7585 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); | |
e297822b | 7586 | } |
2a63b2d9 | 7587 | io_rsrc_ref_unlock(ctx); |
05589553 | 7588 | |
3e942498 PB |
7589 | if (first_add) |
7590 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ); | |
05f3fb3c | 7591 | } |
65e19f54 | 7592 | |
b895c9a6 | 7593 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx) |
05f3fb3c | 7594 | { |
b895c9a6 | 7595 | struct io_rsrc_node *ref_node; |
05f3fb3c | 7596 | |
05589553 XW |
7597 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
7598 | if (!ref_node) | |
3e2224c5 | 7599 | return NULL; |
05f3fb3c | 7600 | |
00835dce | 7601 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
05589553 XW |
7602 | 0, GFP_KERNEL)) { |
7603 | kfree(ref_node); | |
3e2224c5 | 7604 | return NULL; |
05589553 XW |
7605 | } |
7606 | INIT_LIST_HEAD(&ref_node->node); | |
269bbe5f | 7607 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
e297822b | 7608 | ref_node->done = false; |
05589553 | 7609 | return ref_node; |
05589553 XW |
7610 | } |
7611 | ||
6b06314c JA |
7612 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
7613 | unsigned nr_args) | |
7614 | { | |
7615 | __s32 __user *fds = (__s32 __user *) arg; | |
05f3fb3c | 7616 | struct file *file; |
f3baed39 | 7617 | int fd, ret; |
846a4ef2 | 7618 | unsigned i; |
b895c9a6 | 7619 | struct io_rsrc_data *file_data; |
6b06314c | 7620 | |
05f3fb3c | 7621 | if (ctx->file_data) |
6b06314c JA |
7622 | return -EBUSY; |
7623 | if (!nr_args) | |
7624 | return -EINVAL; | |
7625 | if (nr_args > IORING_MAX_FIXED_FILES) | |
7626 | return -EMFILE; | |
a7f0ed5a | 7627 | ret = io_rsrc_node_switch_start(ctx); |
f3baed39 PB |
7628 | if (ret) |
7629 | return ret; | |
6b06314c | 7630 | |
47e90392 | 7631 | file_data = io_rsrc_data_alloc(ctx, io_rsrc_file_put); |
5398ae69 | 7632 | if (!file_data) |
05f3fb3c | 7633 | return -ENOMEM; |
13770a71 | 7634 | ctx->file_data = file_data; |
f3baed39 | 7635 | ret = -ENOMEM; |
aeca241b | 7636 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
1ad555c6 | 7637 | goto out_free; |
65e19f54 | 7638 | |
08a45173 | 7639 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
600cf3f8 PB |
7640 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
7641 | ret = -EFAULT; | |
7642 | goto out_fput; | |
7643 | } | |
08a45173 | 7644 | /* allow sparse sets */ |
600cf3f8 | 7645 | if (fd == -1) |
08a45173 | 7646 | continue; |
6b06314c | 7647 | |
05f3fb3c | 7648 | file = fget(fd); |
6b06314c | 7649 | ret = -EBADF; |
05f3fb3c | 7650 | if (!file) |
600cf3f8 | 7651 | goto out_fput; |
05f3fb3c | 7652 | |
6b06314c JA |
7653 | /* |
7654 | * Don't allow io_uring instances to be registered. If UNIX | |
7655 | * isn't enabled, then this causes a reference cycle and this | |
7656 | * instance can never get freed. If UNIX is enabled we'll | |
7657 | * handle it just fine, but there's still no point in allowing | |
7658 | * a ring fd as it doesn't support regular read/write anyway. | |
7659 | */ | |
05f3fb3c JA |
7660 | if (file->f_op == &io_uring_fops) { |
7661 | fput(file); | |
600cf3f8 | 7662 | goto out_fput; |
6b06314c | 7663 | } |
aeca241b | 7664 | io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file); |
6b06314c JA |
7665 | } |
7666 | ||
6b06314c | 7667 | ret = io_sqe_files_scm(ctx); |
05589553 | 7668 | if (ret) { |
08480400 | 7669 | __io_sqe_files_unregister(ctx); |
05589553 XW |
7670 | return ret; |
7671 | } | |
6b06314c | 7672 | |
a7f0ed5a | 7673 | io_rsrc_node_switch(ctx, NULL); |
6b06314c | 7674 | return ret; |
600cf3f8 PB |
7675 | out_fput: |
7676 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7677 | file = io_file_from_index(ctx, i); | |
7678 | if (file) | |
7679 | fput(file); | |
7680 | } | |
aeca241b | 7681 | io_free_file_tables(&ctx->file_table, nr_args); |
600cf3f8 | 7682 | ctx->nr_user_files = 0; |
600cf3f8 | 7683 | out_free: |
3e942498 | 7684 | kfree(ctx->file_data); |
55cbc256 | 7685 | ctx->file_data = NULL; |
6b06314c JA |
7686 | return ret; |
7687 | } | |
7688 | ||
c3a31e60 JA |
7689 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
7690 | int index) | |
7691 | { | |
7692 | #if defined(CONFIG_UNIX) | |
7693 | struct sock *sock = ctx->ring_sock->sk; | |
7694 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
7695 | struct sk_buff *skb; | |
7696 | ||
7697 | /* | |
7698 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
7699 | * file set. If there's no room, fall back to allocating a new skb | |
7700 | * and filling it in. | |
7701 | */ | |
7702 | spin_lock_irq(&head->lock); | |
7703 | skb = skb_peek(head); | |
7704 | if (skb) { | |
7705 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
7706 | ||
7707 | if (fpl->count < SCM_MAX_FD) { | |
7708 | __skb_unlink(skb, head); | |
7709 | spin_unlock_irq(&head->lock); | |
7710 | fpl->fp[fpl->count] = get_file(file); | |
7711 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
7712 | fpl->count++; | |
7713 | spin_lock_irq(&head->lock); | |
7714 | __skb_queue_head(head, skb); | |
7715 | } else { | |
7716 | skb = NULL; | |
7717 | } | |
7718 | } | |
7719 | spin_unlock_irq(&head->lock); | |
7720 | ||
7721 | if (skb) { | |
7722 | fput(file); | |
7723 | return 0; | |
7724 | } | |
7725 | ||
7726 | return __io_sqe_files_scm(ctx, 1, index); | |
7727 | #else | |
7728 | return 0; | |
7729 | #endif | |
7730 | } | |
7731 | ||
e7c78371 PB |
7732 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, |
7733 | struct io_rsrc_node *node, void *rsrc) | |
05f3fb3c | 7734 | { |
269bbe5f | 7735 | struct io_rsrc_put *prsrc; |
05f3fb3c | 7736 | |
269bbe5f BM |
7737 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
7738 | if (!prsrc) | |
a5318d3c | 7739 | return -ENOMEM; |
05f3fb3c | 7740 | |
50238531 | 7741 | prsrc->rsrc = rsrc; |
e7c78371 | 7742 | list_add(&prsrc->list, &node->rsrc_list); |
a5318d3c | 7743 | return 0; |
05f3fb3c JA |
7744 | } |
7745 | ||
7746 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, | |
269bbe5f | 7747 | struct io_uring_rsrc_update *up, |
05f3fb3c JA |
7748 | unsigned nr_args) |
7749 | { | |
b895c9a6 | 7750 | struct io_rsrc_data *data = ctx->file_data; |
a04b0ac0 PB |
7751 | struct io_fixed_file *file_slot; |
7752 | struct file *file; | |
c3a31e60 JA |
7753 | __s32 __user *fds; |
7754 | int fd, i, err; | |
7755 | __u32 done; | |
05589553 | 7756 | bool needs_switch = false; |
c3a31e60 | 7757 | |
05f3fb3c | 7758 | if (check_add_overflow(up->offset, nr_args, &done)) |
c3a31e60 JA |
7759 | return -EOVERFLOW; |
7760 | if (done > ctx->nr_user_files) | |
7761 | return -EINVAL; | |
a7f0ed5a | 7762 | err = io_rsrc_node_switch_start(ctx); |
8dd03afe PB |
7763 | if (err) |
7764 | return err; | |
05589553 | 7765 | |
269bbe5f | 7766 | fds = u64_to_user_ptr(up->data); |
67973b93 | 7767 | for (done = 0; done < nr_args; done++) { |
c3a31e60 JA |
7768 | err = 0; |
7769 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
7770 | err = -EFAULT; | |
7771 | break; | |
7772 | } | |
4e0377a1 | 7773 | if (fd == IORING_REGISTER_FILES_SKIP) |
7774 | continue; | |
7775 | ||
67973b93 | 7776 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
aeca241b | 7777 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
ea64ec02 | 7778 | |
a04b0ac0 PB |
7779 | if (file_slot->file_ptr) { |
7780 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
a7f0ed5a | 7781 | err = io_queue_rsrc_removal(data, ctx->rsrc_node, file); |
a5318d3c HD |
7782 | if (err) |
7783 | break; | |
a04b0ac0 | 7784 | file_slot->file_ptr = 0; |
05589553 | 7785 | needs_switch = true; |
c3a31e60 JA |
7786 | } |
7787 | if (fd != -1) { | |
c3a31e60 JA |
7788 | file = fget(fd); |
7789 | if (!file) { | |
7790 | err = -EBADF; | |
7791 | break; | |
7792 | } | |
7793 | /* | |
7794 | * Don't allow io_uring instances to be registered. If | |
7795 | * UNIX isn't enabled, then this causes a reference | |
7796 | * cycle and this instance can never get freed. If UNIX | |
7797 | * is enabled we'll handle it just fine, but there's | |
7798 | * still no point in allowing a ring fd as it doesn't | |
7799 | * support regular read/write anyway. | |
7800 | */ | |
7801 | if (file->f_op == &io_uring_fops) { | |
7802 | fput(file); | |
7803 | err = -EBADF; | |
7804 | break; | |
7805 | } | |
9a321c98 | 7806 | io_fixed_file_set(file_slot, file); |
c3a31e60 | 7807 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 7808 | if (err) { |
a04b0ac0 | 7809 | file_slot->file_ptr = 0; |
f3bd9dae | 7810 | fput(file); |
c3a31e60 | 7811 | break; |
f3bd9dae | 7812 | } |
c3a31e60 | 7813 | } |
05f3fb3c JA |
7814 | } |
7815 | ||
a7f0ed5a PB |
7816 | if (needs_switch) |
7817 | io_rsrc_node_switch(ctx, data); | |
c3a31e60 JA |
7818 | return done ? done : err; |
7819 | } | |
05589553 | 7820 | |
05f3fb3c JA |
7821 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
7822 | unsigned nr_args) | |
7823 | { | |
269bbe5f | 7824 | struct io_uring_rsrc_update up; |
05f3fb3c JA |
7825 | |
7826 | if (!ctx->file_data) | |
7827 | return -ENXIO; | |
7828 | if (!nr_args) | |
7829 | return -EINVAL; | |
7830 | if (copy_from_user(&up, arg, sizeof(up))) | |
7831 | return -EFAULT; | |
7832 | if (up.resv) | |
7833 | return -EINVAL; | |
7834 | ||
7835 | return __io_sqe_files_update(ctx, &up, nr_args); | |
7836 | } | |
c3a31e60 | 7837 | |
5280f7e5 | 7838 | static struct io_wq_work *io_free_work(struct io_wq_work *work) |
7d723065 JA |
7839 | { |
7840 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
7841 | ||
5280f7e5 PB |
7842 | req = io_put_req_find_next(req); |
7843 | return req ? &req->work : NULL; | |
7d723065 JA |
7844 | } |
7845 | ||
685fe7fe JA |
7846 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
7847 | struct task_struct *task) | |
24369c2e | 7848 | { |
e941894e | 7849 | struct io_wq_hash *hash; |
24369c2e | 7850 | struct io_wq_data data; |
24369c2e | 7851 | unsigned int concurrency; |
24369c2e | 7852 | |
e941894e JA |
7853 | hash = ctx->hash_map; |
7854 | if (!hash) { | |
7855 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
7856 | if (!hash) | |
7857 | return ERR_PTR(-ENOMEM); | |
7858 | refcount_set(&hash->refs, 1); | |
7859 | init_waitqueue_head(&hash->wait); | |
7860 | ctx->hash_map = hash; | |
24369c2e PB |
7861 | } |
7862 | ||
e941894e | 7863 | data.hash = hash; |
685fe7fe | 7864 | data.task = task; |
e9fd9396 | 7865 | data.free_work = io_free_work; |
f5fa38c5 | 7866 | data.do_work = io_wq_submit_work; |
24369c2e | 7867 | |
d25e3a3d JA |
7868 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
7869 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 7870 | |
5aa75ed5 | 7871 | return io_wq_create(concurrency, &data); |
24369c2e PB |
7872 | } |
7873 | ||
5aa75ed5 JA |
7874 | static int io_uring_alloc_task_context(struct task_struct *task, |
7875 | struct io_ring_ctx *ctx) | |
0f212204 JA |
7876 | { |
7877 | struct io_uring_task *tctx; | |
d8a6df10 | 7878 | int ret; |
0f212204 JA |
7879 | |
7880 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); | |
7881 | if (unlikely(!tctx)) | |
7882 | return -ENOMEM; | |
7883 | ||
d8a6df10 JA |
7884 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
7885 | if (unlikely(ret)) { | |
7886 | kfree(tctx); | |
7887 | return ret; | |
7888 | } | |
7889 | ||
685fe7fe | 7890 | tctx->io_wq = io_init_wq_offload(ctx, task); |
5aa75ed5 JA |
7891 | if (IS_ERR(tctx->io_wq)) { |
7892 | ret = PTR_ERR(tctx->io_wq); | |
7893 | percpu_counter_destroy(&tctx->inflight); | |
7894 | kfree(tctx); | |
7895 | return ret; | |
7896 | } | |
7897 | ||
0f212204 JA |
7898 | xa_init(&tctx->xa); |
7899 | init_waitqueue_head(&tctx->wait); | |
7900 | tctx->last = NULL; | |
fdaf083c | 7901 | atomic_set(&tctx->in_idle, 0); |
b303fe2e | 7902 | atomic_set(&tctx->inflight_tracked, 0); |
0f212204 | 7903 | task->io_uring = tctx; |
7cbf1722 JA |
7904 | spin_lock_init(&tctx->task_lock); |
7905 | INIT_WQ_LIST(&tctx->task_list); | |
7906 | tctx->task_state = 0; | |
7907 | init_task_work(&tctx->task_work, tctx_task_work); | |
0f212204 JA |
7908 | return 0; |
7909 | } | |
7910 | ||
7911 | void __io_uring_free(struct task_struct *tsk) | |
7912 | { | |
7913 | struct io_uring_task *tctx = tsk->io_uring; | |
7914 | ||
7915 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e PB |
7916 | WARN_ON_ONCE(tctx->io_wq); |
7917 | ||
d8a6df10 | 7918 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
7919 | kfree(tctx); |
7920 | tsk->io_uring = NULL; | |
7921 | } | |
7922 | ||
7e84e1c7 SG |
7923 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
7924 | struct io_uring_params *p) | |
2b188cc1 JA |
7925 | { |
7926 | int ret; | |
7927 | ||
d25e3a3d JA |
7928 | /* Retain compatibility with failing for an invalid attach attempt */ |
7929 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
7930 | IORING_SETUP_ATTACH_WQ) { | |
7931 | struct fd f; | |
7932 | ||
7933 | f = fdget(p->wq_fd); | |
7934 | if (!f.file) | |
7935 | return -ENXIO; | |
7936 | if (f.file->f_op != &io_uring_fops) { | |
7937 | fdput(f); | |
7938 | return -EINVAL; | |
7939 | } | |
7940 | fdput(f); | |
7941 | } | |
6c271ce2 | 7942 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 7943 | struct task_struct *tsk; |
534ca6d6 | 7944 | struct io_sq_data *sqd; |
26984fbf | 7945 | bool attached; |
534ca6d6 | 7946 | |
26984fbf | 7947 | sqd = io_get_sq_data(p, &attached); |
534ca6d6 JA |
7948 | if (IS_ERR(sqd)) { |
7949 | ret = PTR_ERR(sqd); | |
7950 | goto err; | |
7951 | } | |
69fb2131 | 7952 | |
7c30f36a | 7953 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 7954 | ctx->sq_data = sqd; |
917257da JA |
7955 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
7956 | if (!ctx->sq_thread_idle) | |
7957 | ctx->sq_thread_idle = HZ; | |
7958 | ||
26984fbf | 7959 | ret = 0; |
78d7f6ba | 7960 | io_sq_thread_park(sqd); |
de75a3d3 PB |
7961 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
7962 | io_sqd_update_thread_idle(sqd); | |
26984fbf | 7963 | /* don't attach to a dying SQPOLL thread, would be racy */ |
de75a3d3 | 7964 | if (attached && !sqd->thread) |
26984fbf | 7965 | ret = -ENXIO; |
78d7f6ba PB |
7966 | io_sq_thread_unpark(sqd); |
7967 | ||
de75a3d3 PB |
7968 | if (ret < 0) |
7969 | goto err; | |
7970 | if (attached) | |
5aa75ed5 | 7971 | return 0; |
aa06165d | 7972 | |
6c271ce2 | 7973 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 7974 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 7975 | |
917257da | 7976 | ret = -EINVAL; |
44a9bd18 | 7977 | if (cpu >= nr_cpu_ids) |
e8f98f24 | 7978 | goto err_sqpoll; |
7889f44d | 7979 | if (!cpu_online(cpu)) |
e8f98f24 | 7980 | goto err_sqpoll; |
917257da | 7981 | |
37d1e2e3 | 7982 | sqd->sq_cpu = cpu; |
6c271ce2 | 7983 | } else { |
37d1e2e3 | 7984 | sqd->sq_cpu = -1; |
6c271ce2 | 7985 | } |
37d1e2e3 JA |
7986 | |
7987 | sqd->task_pid = current->pid; | |
5c2469e0 | 7988 | sqd->task_tgid = current->tgid; |
46fe18b1 JA |
7989 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
7990 | if (IS_ERR(tsk)) { | |
7991 | ret = PTR_ERR(tsk); | |
e8f98f24 | 7992 | goto err_sqpoll; |
6c271ce2 | 7993 | } |
97a73a0f | 7994 | |
46fe18b1 | 7995 | sqd->thread = tsk; |
97a73a0f | 7996 | ret = io_uring_alloc_task_context(tsk, ctx); |
46fe18b1 | 7997 | wake_up_new_task(tsk); |
0f212204 JA |
7998 | if (ret) |
7999 | goto err; | |
6c271ce2 JA |
8000 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
8001 | /* Can't have SQ_AFF without SQPOLL */ | |
8002 | ret = -EINVAL; | |
8003 | goto err; | |
8004 | } | |
8005 | ||
2b188cc1 JA |
8006 | return 0; |
8007 | err: | |
37d1e2e3 | 8008 | io_sq_thread_finish(ctx); |
2b188cc1 | 8009 | return ret; |
e8f98f24 JA |
8010 | err_sqpoll: |
8011 | complete(&ctx->sq_data->exited); | |
8012 | goto err; | |
2b188cc1 JA |
8013 | } |
8014 | ||
a087e2b5 BM |
8015 | static inline void __io_unaccount_mem(struct user_struct *user, |
8016 | unsigned long nr_pages) | |
2b188cc1 JA |
8017 | { |
8018 | atomic_long_sub(nr_pages, &user->locked_vm); | |
8019 | } | |
8020 | ||
a087e2b5 BM |
8021 | static inline int __io_account_mem(struct user_struct *user, |
8022 | unsigned long nr_pages) | |
2b188cc1 JA |
8023 | { |
8024 | unsigned long page_limit, cur_pages, new_pages; | |
8025 | ||
8026 | /* Don't allow more pages than we can safely lock */ | |
8027 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
8028 | ||
8029 | do { | |
8030 | cur_pages = atomic_long_read(&user->locked_vm); | |
8031 | new_pages = cur_pages + nr_pages; | |
8032 | if (new_pages > page_limit) | |
8033 | return -ENOMEM; | |
8034 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
8035 | new_pages) != cur_pages); | |
8036 | ||
8037 | return 0; | |
8038 | } | |
8039 | ||
26bfa89e | 8040 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8041 | { |
62e398be | 8042 | if (ctx->user) |
a087e2b5 | 8043 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 8044 | |
26bfa89e JA |
8045 | if (ctx->mm_account) |
8046 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8047 | } |
8048 | ||
26bfa89e | 8049 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8050 | { |
30975825 BM |
8051 | int ret; |
8052 | ||
62e398be | 8053 | if (ctx->user) { |
30975825 BM |
8054 | ret = __io_account_mem(ctx->user, nr_pages); |
8055 | if (ret) | |
8056 | return ret; | |
8057 | } | |
8058 | ||
26bfa89e JA |
8059 | if (ctx->mm_account) |
8060 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8061 | |
8062 | return 0; | |
8063 | } | |
8064 | ||
2b188cc1 JA |
8065 | static void io_mem_free(void *ptr) |
8066 | { | |
52e04ef4 MR |
8067 | struct page *page; |
8068 | ||
8069 | if (!ptr) | |
8070 | return; | |
2b188cc1 | 8071 | |
52e04ef4 | 8072 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8073 | if (put_page_testzero(page)) |
8074 | free_compound_page(page); | |
8075 | } | |
8076 | ||
8077 | static void *io_mem_alloc(size_t size) | |
8078 | { | |
8079 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
26bfa89e | 8080 | __GFP_NORETRY | __GFP_ACCOUNT; |
2b188cc1 JA |
8081 | |
8082 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8083 | } | |
8084 | ||
75b28aff HV |
8085 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8086 | size_t *sq_offset) | |
8087 | { | |
8088 | struct io_rings *rings; | |
8089 | size_t off, sq_array_size; | |
8090 | ||
8091 | off = struct_size(rings, cqes, cq_entries); | |
8092 | if (off == SIZE_MAX) | |
8093 | return SIZE_MAX; | |
8094 | ||
8095 | #ifdef CONFIG_SMP | |
8096 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8097 | if (off == 0) | |
8098 | return SIZE_MAX; | |
8099 | #endif | |
8100 | ||
b36200f5 DV |
8101 | if (sq_offset) |
8102 | *sq_offset = off; | |
8103 | ||
75b28aff HV |
8104 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8105 | if (sq_array_size == SIZE_MAX) | |
8106 | return SIZE_MAX; | |
8107 | ||
8108 | if (check_add_overflow(off, sq_array_size, &off)) | |
8109 | return SIZE_MAX; | |
8110 | ||
75b28aff HV |
8111 | return off; |
8112 | } | |
8113 | ||
7f61a1e9 PB |
8114 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf *imu) |
8115 | { | |
8116 | unsigned int i; | |
8117 | ||
8118 | for (i = 0; i < imu->nr_bvecs; i++) | |
8119 | unpin_user_page(imu->bvec[i].bv_page); | |
8120 | if (imu->acct_pages) | |
8121 | io_unaccount_mem(ctx, imu->acct_pages); | |
8122 | kvfree(imu->bvec); | |
8123 | imu->nr_bvecs = 0; | |
8124 | } | |
8125 | ||
0a96bbe4 | 8126 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee | 8127 | { |
7f61a1e9 | 8128 | unsigned int i; |
edafccee JA |
8129 | |
8130 | if (!ctx->user_bufs) | |
8131 | return -ENXIO; | |
8132 | ||
7f61a1e9 PB |
8133 | for (i = 0; i < ctx->nr_user_bufs; i++) |
8134 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); | |
edafccee JA |
8135 | kfree(ctx->user_bufs); |
8136 | ctx->user_bufs = NULL; | |
8137 | ctx->nr_user_bufs = 0; | |
8138 | return 0; | |
8139 | } | |
8140 | ||
8141 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8142 | void __user *arg, unsigned index) | |
8143 | { | |
8144 | struct iovec __user *src; | |
8145 | ||
8146 | #ifdef CONFIG_COMPAT | |
8147 | if (ctx->compat) { | |
8148 | struct compat_iovec __user *ciovs; | |
8149 | struct compat_iovec ciov; | |
8150 | ||
8151 | ciovs = (struct compat_iovec __user *) arg; | |
8152 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8153 | return -EFAULT; | |
8154 | ||
d55e5f5b | 8155 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8156 | dst->iov_len = ciov.iov_len; |
8157 | return 0; | |
8158 | } | |
8159 | #endif | |
8160 | src = (struct iovec __user *) arg; | |
8161 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8162 | return -EFAULT; | |
8163 | return 0; | |
8164 | } | |
8165 | ||
de293938 JA |
8166 | /* |
8167 | * Not super efficient, but this is just a registration time. And we do cache | |
8168 | * the last compound head, so generally we'll only do a full search if we don't | |
8169 | * match that one. | |
8170 | * | |
8171 | * We check if the given compound head page has already been accounted, to | |
8172 | * avoid double accounting it. This allows us to account the full size of the | |
8173 | * page, not just the constituent pages of a huge page. | |
8174 | */ | |
8175 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8176 | int nr_pages, struct page *hpage) | |
8177 | { | |
8178 | int i, j; | |
8179 | ||
8180 | /* check current page array */ | |
8181 | for (i = 0; i < nr_pages; i++) { | |
8182 | if (!PageCompound(pages[i])) | |
8183 | continue; | |
8184 | if (compound_head(pages[i]) == hpage) | |
8185 | return true; | |
8186 | } | |
8187 | ||
8188 | /* check previously registered pages */ | |
8189 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8190 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8191 | ||
8192 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8193 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8194 | continue; | |
8195 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8196 | return true; | |
8197 | } | |
8198 | } | |
8199 | ||
8200 | return false; | |
8201 | } | |
8202 | ||
8203 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8204 | int nr_pages, struct io_mapped_ubuf *imu, | |
8205 | struct page **last_hpage) | |
8206 | { | |
8207 | int i, ret; | |
8208 | ||
8209 | for (i = 0; i < nr_pages; i++) { | |
8210 | if (!PageCompound(pages[i])) { | |
8211 | imu->acct_pages++; | |
8212 | } else { | |
8213 | struct page *hpage; | |
8214 | ||
8215 | hpage = compound_head(pages[i]); | |
8216 | if (hpage == *last_hpage) | |
8217 | continue; | |
8218 | *last_hpage = hpage; | |
8219 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8220 | continue; | |
8221 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8222 | } | |
8223 | } | |
8224 | ||
8225 | if (!imu->acct_pages) | |
8226 | return 0; | |
8227 | ||
26bfa89e | 8228 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
8229 | if (ret) |
8230 | imu->acct_pages = 0; | |
8231 | return ret; | |
8232 | } | |
8233 | ||
0a96bbe4 BM |
8234 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
8235 | struct io_mapped_ubuf *imu, | |
8236 | struct page **last_hpage) | |
edafccee JA |
8237 | { |
8238 | struct vm_area_struct **vmas = NULL; | |
8239 | struct page **pages = NULL; | |
0a96bbe4 BM |
8240 | unsigned long off, start, end, ubuf; |
8241 | size_t size; | |
8242 | int ret, pret, nr_pages, i; | |
8243 | ||
8244 | ubuf = (unsigned long) iov->iov_base; | |
8245 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8246 | start = ubuf >> PAGE_SHIFT; | |
8247 | nr_pages = end - start; | |
8248 | ||
8249 | ret = -ENOMEM; | |
8250 | ||
8251 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8252 | if (!pages) | |
8253 | goto done; | |
8254 | ||
8255 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8256 | GFP_KERNEL); | |
8257 | if (!vmas) | |
8258 | goto done; | |
edafccee | 8259 | |
0a96bbe4 BM |
8260 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
8261 | GFP_KERNEL); | |
8262 | if (!imu->bvec) | |
8263 | goto done; | |
8264 | ||
8265 | ret = 0; | |
8266 | mmap_read_lock(current->mm); | |
8267 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8268 | pages, vmas); | |
8269 | if (pret == nr_pages) { | |
8270 | /* don't support file backed memory */ | |
8271 | for (i = 0; i < nr_pages; i++) { | |
8272 | struct vm_area_struct *vma = vmas[i]; | |
8273 | ||
8274 | if (vma->vm_file && | |
8275 | !is_file_hugepages(vma->vm_file)) { | |
8276 | ret = -EOPNOTSUPP; | |
8277 | break; | |
8278 | } | |
8279 | } | |
8280 | } else { | |
8281 | ret = pret < 0 ? pret : -EFAULT; | |
8282 | } | |
8283 | mmap_read_unlock(current->mm); | |
8284 | if (ret) { | |
8285 | /* | |
8286 | * if we did partial map, or found file backed vmas, | |
8287 | * release any pages we did get | |
8288 | */ | |
8289 | if (pret > 0) | |
8290 | unpin_user_pages(pages, pret); | |
8291 | kvfree(imu->bvec); | |
8292 | goto done; | |
8293 | } | |
8294 | ||
8295 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
8296 | if (ret) { | |
8297 | unpin_user_pages(pages, pret); | |
8298 | kvfree(imu->bvec); | |
8299 | goto done; | |
8300 | } | |
8301 | ||
8302 | off = ubuf & ~PAGE_MASK; | |
8303 | size = iov->iov_len; | |
8304 | for (i = 0; i < nr_pages; i++) { | |
8305 | size_t vec_len; | |
8306 | ||
8307 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
8308 | imu->bvec[i].bv_page = pages[i]; | |
8309 | imu->bvec[i].bv_len = vec_len; | |
8310 | imu->bvec[i].bv_offset = off; | |
8311 | off = 0; | |
8312 | size -= vec_len; | |
8313 | } | |
8314 | /* store original address for later verification */ | |
8315 | imu->ubuf = ubuf; | |
4751f53d | 8316 | imu->ubuf_end = ubuf + iov->iov_len; |
0a96bbe4 BM |
8317 | imu->nr_bvecs = nr_pages; |
8318 | ret = 0; | |
8319 | done: | |
8320 | kvfree(pages); | |
8321 | kvfree(vmas); | |
8322 | return ret; | |
8323 | } | |
8324 | ||
2b358604 | 8325 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 8326 | { |
87094465 PB |
8327 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
8328 | return ctx->user_bufs ? 0 : -ENOMEM; | |
2b358604 | 8329 | } |
edafccee | 8330 | |
2b358604 BM |
8331 | static int io_buffer_validate(struct iovec *iov) |
8332 | { | |
50e96989 PB |
8333 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
8334 | ||
2b358604 BM |
8335 | /* |
8336 | * Don't impose further limits on the size and buffer | |
8337 | * constraints here, we'll -EINVAL later when IO is | |
8338 | * submitted if they are wrong. | |
8339 | */ | |
8340 | if (!iov->iov_base || !iov->iov_len) | |
8341 | return -EFAULT; | |
edafccee | 8342 | |
2b358604 BM |
8343 | /* arbitrary limit, but we need something */ |
8344 | if (iov->iov_len > SZ_1G) | |
8345 | return -EFAULT; | |
edafccee | 8346 | |
50e96989 PB |
8347 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
8348 | return -EOVERFLOW; | |
8349 | ||
2b358604 BM |
8350 | return 0; |
8351 | } | |
edafccee | 8352 | |
2b358604 BM |
8353 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
8354 | unsigned int nr_args) | |
8355 | { | |
8356 | int i, ret; | |
8357 | struct iovec iov; | |
8358 | struct page *last_hpage = NULL; | |
edafccee | 8359 | |
87094465 PB |
8360 | if (ctx->user_bufs) |
8361 | return -EBUSY; | |
8362 | if (!nr_args || nr_args > UIO_MAXIOV) | |
8363 | return -EINVAL; | |
2b358604 BM |
8364 | ret = io_buffers_map_alloc(ctx, nr_args); |
8365 | if (ret) | |
8366 | return ret; | |
edafccee | 8367 | |
87094465 | 8368 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
edafccee | 8369 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
edafccee | 8370 | |
edafccee JA |
8371 | ret = io_copy_iov(ctx, &iov, arg, i); |
8372 | if (ret) | |
0a96bbe4 | 8373 | break; |
2b358604 BM |
8374 | ret = io_buffer_validate(&iov); |
8375 | if (ret) | |
0a96bbe4 | 8376 | break; |
0a96bbe4 BM |
8377 | ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage); |
8378 | if (ret) | |
8379 | break; | |
edafccee | 8380 | } |
0a96bbe4 BM |
8381 | |
8382 | if (ret) | |
8383 | io_sqe_buffers_unregister(ctx); | |
8384 | ||
edafccee JA |
8385 | return ret; |
8386 | } | |
8387 | ||
9b402849 JA |
8388 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
8389 | { | |
8390 | __s32 __user *fds = arg; | |
8391 | int fd; | |
8392 | ||
8393 | if (ctx->cq_ev_fd) | |
8394 | return -EBUSY; | |
8395 | ||
8396 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
8397 | return -EFAULT; | |
8398 | ||
8399 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
8400 | if (IS_ERR(ctx->cq_ev_fd)) { | |
8401 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
8402 | ctx->cq_ev_fd = NULL; | |
8403 | return ret; | |
8404 | } | |
8405 | ||
8406 | return 0; | |
8407 | } | |
8408 | ||
8409 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
8410 | { | |
8411 | if (ctx->cq_ev_fd) { | |
8412 | eventfd_ctx_put(ctx->cq_ev_fd); | |
8413 | ctx->cq_ev_fd = NULL; | |
8414 | return 0; | |
8415 | } | |
8416 | ||
8417 | return -ENXIO; | |
8418 | } | |
8419 | ||
5a2e745d JA |
8420 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
8421 | { | |
9e15c3a0 JA |
8422 | struct io_buffer *buf; |
8423 | unsigned long index; | |
8424 | ||
8425 | xa_for_each(&ctx->io_buffers, index, buf) | |
8426 | __io_remove_buffers(ctx, buf, index, -1U); | |
5a2e745d JA |
8427 | } |
8428 | ||
68e68ee6 | 8429 | static void io_req_cache_free(struct list_head *list, struct task_struct *tsk) |
1b4c351f | 8430 | { |
68e68ee6 | 8431 | struct io_kiocb *req, *nxt; |
1b4c351f | 8432 | |
68e68ee6 JA |
8433 | list_for_each_entry_safe(req, nxt, list, compl.list) { |
8434 | if (tsk && req->task != tsk) | |
8435 | continue; | |
1b4c351f JA |
8436 | list_del(&req->compl.list); |
8437 | kmem_cache_free(req_cachep, req); | |
8438 | } | |
8439 | } | |
8440 | ||
4010fec4 | 8441 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 8442 | { |
bf019da7 | 8443 | struct io_submit_state *submit_state = &ctx->submit_state; |
e5547d2c | 8444 | struct io_comp_state *cs = &ctx->submit_state.comp; |
bf019da7 | 8445 | |
9a4fdbd8 JA |
8446 | mutex_lock(&ctx->uring_lock); |
8447 | ||
8e5c66c4 | 8448 | if (submit_state->free_reqs) { |
9a4fdbd8 JA |
8449 | kmem_cache_free_bulk(req_cachep, submit_state->free_reqs, |
8450 | submit_state->reqs); | |
8e5c66c4 PB |
8451 | submit_state->free_reqs = 0; |
8452 | } | |
9a4fdbd8 | 8453 | |
dac7a098 | 8454 | io_flush_cached_locked_reqs(ctx, cs); |
e5547d2c | 8455 | io_req_cache_free(&cs->free_list, NULL); |
9a4fdbd8 JA |
8456 | mutex_unlock(&ctx->uring_lock); |
8457 | } | |
8458 | ||
2b188cc1 JA |
8459 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
8460 | { | |
37d1e2e3 | 8461 | io_sq_thread_finish(ctx); |
0a96bbe4 | 8462 | io_sqe_buffers_unregister(ctx); |
2aede0e4 | 8463 | |
37d1e2e3 | 8464 | if (ctx->mm_account) { |
2aede0e4 JA |
8465 | mmdrop(ctx->mm_account); |
8466 | ctx->mm_account = NULL; | |
30975825 | 8467 | } |
def596e9 | 8468 | |
8bad28d8 | 8469 | mutex_lock(&ctx->uring_lock); |
08480400 PB |
8470 | if (ctx->file_data) { |
8471 | if (!atomic_dec_and_test(&ctx->file_data->refs)) | |
8472 | wait_for_completion(&ctx->file_data->done); | |
8473 | __io_sqe_files_unregister(ctx); | |
8474 | } | |
c4ea060e PB |
8475 | if (ctx->rings) |
8476 | __io_cqring_overflow_flush(ctx, true); | |
8bad28d8 | 8477 | mutex_unlock(&ctx->uring_lock); |
9b402849 | 8478 | io_eventfd_unregister(ctx); |
5a2e745d | 8479 | io_destroy_buffers(ctx); |
def596e9 | 8480 | |
a7f0ed5a PB |
8481 | /* there are no registered resources left, nobody uses it */ |
8482 | if (ctx->rsrc_node) | |
8483 | io_rsrc_node_destroy(ctx->rsrc_node); | |
8dd03afe | 8484 | if (ctx->rsrc_backup_node) |
b895c9a6 | 8485 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
a7f0ed5a PB |
8486 | flush_delayed_work(&ctx->rsrc_put_work); |
8487 | ||
8488 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); | |
8489 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); | |
8dd03afe | 8490 | |
2b188cc1 | 8491 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
8492 | if (ctx->ring_sock) { |
8493 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 8494 | sock_release(ctx->ring_sock); |
355e8d26 | 8495 | } |
2b188cc1 JA |
8496 | #endif |
8497 | ||
75b28aff | 8498 | io_mem_free(ctx->rings); |
2b188cc1 | 8499 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
8500 | |
8501 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 8502 | free_uid(ctx->user); |
4010fec4 | 8503 | io_req_caches_free(ctx); |
e941894e JA |
8504 | if (ctx->hash_map) |
8505 | io_wq_put_hash(ctx->hash_map); | |
78076bb6 | 8506 | kfree(ctx->cancel_hash); |
2b188cc1 JA |
8507 | kfree(ctx); |
8508 | } | |
8509 | ||
8510 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
8511 | { | |
8512 | struct io_ring_ctx *ctx = file->private_data; | |
8513 | __poll_t mask = 0; | |
8514 | ||
8515 | poll_wait(file, &ctx->cq_wait, wait); | |
4f7067c3 SB |
8516 | /* |
8517 | * synchronizes with barrier from wq_has_sleeper call in | |
8518 | * io_commit_cqring | |
8519 | */ | |
2b188cc1 | 8520 | smp_rmb(); |
90554200 | 8521 | if (!io_sqring_full(ctx)) |
2b188cc1 | 8522 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
8523 | |
8524 | /* | |
8525 | * Don't flush cqring overflow list here, just do a simple check. | |
8526 | * Otherwise there could possible be ABBA deadlock: | |
8527 | * CPU0 CPU1 | |
8528 | * ---- ---- | |
8529 | * lock(&ctx->uring_lock); | |
8530 | * lock(&ep->mtx); | |
8531 | * lock(&ctx->uring_lock); | |
8532 | * lock(&ep->mtx); | |
8533 | * | |
8534 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
8535 | * pushs them to do the flush. | |
8536 | */ | |
8537 | if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow)) | |
2b188cc1 JA |
8538 | mask |= EPOLLIN | EPOLLRDNORM; |
8539 | ||
8540 | return mask; | |
8541 | } | |
8542 | ||
8543 | static int io_uring_fasync(int fd, struct file *file, int on) | |
8544 | { | |
8545 | struct io_ring_ctx *ctx = file->private_data; | |
8546 | ||
8547 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
8548 | } | |
8549 | ||
0bead8cd | 8550 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 8551 | { |
4379bf8b | 8552 | const struct cred *creds; |
071698e1 | 8553 | |
61cf9370 | 8554 | creds = xa_erase(&ctx->personalities, id); |
4379bf8b JA |
8555 | if (creds) { |
8556 | put_cred(creds); | |
0bead8cd | 8557 | return 0; |
1e6fa521 | 8558 | } |
0bead8cd YD |
8559 | |
8560 | return -EINVAL; | |
8561 | } | |
8562 | ||
9b465711 | 8563 | static inline bool io_run_ctx_fallback(struct io_ring_ctx *ctx) |
7c25c0d1 | 8564 | { |
9b465711 | 8565 | return io_run_task_work_head(&ctx->exit_task_work); |
7c25c0d1 JA |
8566 | } |
8567 | ||
d56d938b PB |
8568 | struct io_tctx_exit { |
8569 | struct callback_head task_work; | |
8570 | struct completion completion; | |
baf186c4 | 8571 | struct io_ring_ctx *ctx; |
d56d938b PB |
8572 | }; |
8573 | ||
8574 | static void io_tctx_exit_cb(struct callback_head *cb) | |
8575 | { | |
8576 | struct io_uring_task *tctx = current->io_uring; | |
8577 | struct io_tctx_exit *work; | |
8578 | ||
8579 | work = container_of(cb, struct io_tctx_exit, task_work); | |
8580 | /* | |
8581 | * When @in_idle, we're in cancellation and it's racy to remove the | |
8582 | * node. It'll be removed by the end of cancellation, just ignore it. | |
8583 | */ | |
8584 | if (!atomic_read(&tctx->in_idle)) | |
baf186c4 | 8585 | io_uring_del_task_file((unsigned long)work->ctx); |
d56d938b PB |
8586 | complete(&work->completion); |
8587 | } | |
8588 | ||
85faa7b8 JA |
8589 | static void io_ring_exit_work(struct work_struct *work) |
8590 | { | |
d56d938b | 8591 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 8592 | unsigned long timeout = jiffies + HZ * 60 * 5; |
d56d938b PB |
8593 | struct io_tctx_exit exit; |
8594 | struct io_tctx_node *node; | |
8595 | int ret; | |
85faa7b8 | 8596 | |
a185f1db PB |
8597 | /* prevent SQPOLL from submitting new requests */ |
8598 | if (ctx->sq_data) { | |
8599 | io_sq_thread_park(ctx->sq_data); | |
8600 | list_del_init(&ctx->sqd_list); | |
8601 | io_sqd_update_thread_idle(ctx->sq_data); | |
8602 | io_sq_thread_unpark(ctx->sq_data); | |
8603 | } | |
8604 | ||
56952e91 JA |
8605 | /* |
8606 | * If we're doing polled IO and end up having requests being | |
8607 | * submitted async (out-of-line), then completions can come in while | |
8608 | * we're waiting for refs to drop. We need to reap these manually, | |
8609 | * as nobody else will be looking for them. | |
8610 | */ | |
b2edc0a7 | 8611 | do { |
9936c7c2 | 8612 | io_uring_try_cancel_requests(ctx, NULL, NULL); |
b5bb3a24 PB |
8613 | |
8614 | WARN_ON_ONCE(time_after(jiffies, timeout)); | |
b2edc0a7 | 8615 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
d56d938b | 8616 | |
89b5066e PB |
8617 | /* |
8618 | * Some may use context even when all refs and requests have been put, | |
8619 | * and they are free to do so while still holding uring_lock or | |
8620 | * completion_lock, see __io_req_task_submit(). Apart from other work, | |
8621 | * this lock/unlock section also waits them to finish. | |
8622 | */ | |
d56d938b PB |
8623 | mutex_lock(&ctx->uring_lock); |
8624 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
8625 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
8626 | ||
d56d938b PB |
8627 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
8628 | ctx_node); | |
baf186c4 | 8629 | exit.ctx = ctx; |
d56d938b PB |
8630 | init_completion(&exit.completion); |
8631 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
8632 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); | |
8633 | if (WARN_ON_ONCE(ret)) | |
8634 | continue; | |
8635 | wake_up_process(node->task); | |
8636 | ||
8637 | mutex_unlock(&ctx->uring_lock); | |
8638 | wait_for_completion(&exit.completion); | |
8639 | cond_resched(); | |
8640 | mutex_lock(&ctx->uring_lock); | |
8641 | } | |
8642 | mutex_unlock(&ctx->uring_lock); | |
89b5066e PB |
8643 | spin_lock_irq(&ctx->completion_lock); |
8644 | spin_unlock_irq(&ctx->completion_lock); | |
d56d938b | 8645 | |
85faa7b8 JA |
8646 | io_ring_ctx_free(ctx); |
8647 | } | |
8648 | ||
80c4cbdb PB |
8649 | /* Returns true if we found and killed one or more timeouts */ |
8650 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, | |
8651 | struct files_struct *files) | |
8652 | { | |
8653 | struct io_kiocb *req, *tmp; | |
8654 | int canceled = 0; | |
8655 | ||
8656 | spin_lock_irq(&ctx->completion_lock); | |
8657 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { | |
8658 | if (io_match_task(req, tsk, files)) { | |
8659 | io_kill_timeout(req, -ECANCELED); | |
8660 | canceled++; | |
8661 | } | |
8662 | } | |
51520426 PB |
8663 | if (canceled != 0) |
8664 | io_commit_cqring(ctx); | |
80c4cbdb | 8665 | spin_unlock_irq(&ctx->completion_lock); |
80c4cbdb PB |
8666 | if (canceled != 0) |
8667 | io_cqring_ev_posted(ctx); | |
8668 | return canceled != 0; | |
8669 | } | |
8670 | ||
2b188cc1 JA |
8671 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
8672 | { | |
61cf9370 MWO |
8673 | unsigned long index; |
8674 | struct creds *creds; | |
8675 | ||
2b188cc1 JA |
8676 | mutex_lock(&ctx->uring_lock); |
8677 | percpu_ref_kill(&ctx->refs); | |
634578f8 | 8678 | if (ctx->rings) |
6c2450ae | 8679 | __io_cqring_overflow_flush(ctx, true); |
61cf9370 MWO |
8680 | xa_for_each(&ctx->personalities, index, creds) |
8681 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
8682 | mutex_unlock(&ctx->uring_lock); |
8683 | ||
6b81928d PB |
8684 | io_kill_timeouts(ctx, NULL, NULL); |
8685 | io_poll_remove_all(ctx, NULL, NULL); | |
561fb04a | 8686 | |
15dff286 | 8687 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 8688 | io_iopoll_try_reap_events(ctx); |
309fc03a | 8689 | |
85faa7b8 | 8690 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
8691 | /* |
8692 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
8693 | * if we're exiting a ton of rings at the same time. It just adds | |
8694 | * noise and overhead, there's no discernable change in runtime | |
8695 | * over using system_wq. | |
8696 | */ | |
8697 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
8698 | } |
8699 | ||
8700 | static int io_uring_release(struct inode *inode, struct file *file) | |
8701 | { | |
8702 | struct io_ring_ctx *ctx = file->private_data; | |
8703 | ||
8704 | file->private_data = NULL; | |
8705 | io_ring_ctx_wait_and_kill(ctx); | |
8706 | return 0; | |
8707 | } | |
8708 | ||
f6edbabb PB |
8709 | struct io_task_cancel { |
8710 | struct task_struct *task; | |
8711 | struct files_struct *files; | |
8712 | }; | |
f254ac04 | 8713 | |
f6edbabb | 8714 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 8715 | { |
9a472ef7 | 8716 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 8717 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
8718 | bool ret; |
8719 | ||
f6edbabb | 8720 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
8721 | unsigned long flags; |
8722 | struct io_ring_ctx *ctx = req->ctx; | |
8723 | ||
8724 | /* protect against races with linked timeouts */ | |
8725 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
f6edbabb | 8726 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8727 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
8728 | } else { | |
f6edbabb | 8729 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8730 | } |
8731 | return ret; | |
b711d4ea JA |
8732 | } |
8733 | ||
e1915f76 | 8734 | static bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
ef9865a4 | 8735 | struct task_struct *task, |
b7ddce3c PB |
8736 | struct files_struct *files) |
8737 | { | |
e1915f76 | 8738 | struct io_defer_entry *de; |
b7ddce3c PB |
8739 | LIST_HEAD(list); |
8740 | ||
8741 | spin_lock_irq(&ctx->completion_lock); | |
8742 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { | |
08d23634 | 8743 | if (io_match_task(de->req, task, files)) { |
b7ddce3c PB |
8744 | list_cut_position(&list, &ctx->defer_list, &de->list); |
8745 | break; | |
8746 | } | |
8747 | } | |
8748 | spin_unlock_irq(&ctx->completion_lock); | |
e1915f76 PB |
8749 | if (list_empty(&list)) |
8750 | return false; | |
b7ddce3c PB |
8751 | |
8752 | while (!list_empty(&list)) { | |
8753 | de = list_first_entry(&list, struct io_defer_entry, list); | |
8754 | list_del_init(&de->list); | |
f41db273 | 8755 | io_req_complete_failed(de->req, -ECANCELED); |
b7ddce3c PB |
8756 | kfree(de); |
8757 | } | |
e1915f76 | 8758 | return true; |
b7ddce3c PB |
8759 | } |
8760 | ||
1b00764f PB |
8761 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
8762 | { | |
8763 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
8764 | ||
8765 | return req->ctx == data; | |
8766 | } | |
8767 | ||
8768 | static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) | |
8769 | { | |
8770 | struct io_tctx_node *node; | |
8771 | enum io_wq_cancel cret; | |
8772 | bool ret = false; | |
8773 | ||
8774 | mutex_lock(&ctx->uring_lock); | |
8775 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
8776 | struct io_uring_task *tctx = node->task->io_uring; | |
8777 | ||
8778 | /* | |
8779 | * io_wq will stay alive while we hold uring_lock, because it's | |
8780 | * killed after ctx nodes, which requires to take the lock. | |
8781 | */ | |
8782 | if (!tctx || !tctx->io_wq) | |
8783 | continue; | |
8784 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
8785 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
8786 | } | |
8787 | mutex_unlock(&ctx->uring_lock); | |
8788 | ||
8789 | return ret; | |
8790 | } | |
8791 | ||
9936c7c2 PB |
8792 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
8793 | struct task_struct *task, | |
8794 | struct files_struct *files) | |
8795 | { | |
8796 | struct io_task_cancel cancel = { .task = task, .files = files, }; | |
1b00764f | 8797 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 PB |
8798 | |
8799 | while (1) { | |
8800 | enum io_wq_cancel cret; | |
8801 | bool ret = false; | |
8802 | ||
1b00764f PB |
8803 | if (!task) { |
8804 | ret |= io_uring_try_cancel_iowq(ctx); | |
8805 | } else if (tctx && tctx->io_wq) { | |
8806 | /* | |
8807 | * Cancels requests of all rings, not only @ctx, but | |
8808 | * it's fine as the task is in exit/exec. | |
8809 | */ | |
5aa75ed5 | 8810 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
8811 | &cancel, true); |
8812 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
8813 | } | |
8814 | ||
8815 | /* SQPOLL thread does its own polling */ | |
d052d1d6 JA |
8816 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && !files) || |
8817 | (ctx->sq_data && ctx->sq_data->thread == current)) { | |
9936c7c2 PB |
8818 | while (!list_empty_careful(&ctx->iopoll_list)) { |
8819 | io_iopoll_try_reap_events(ctx); | |
8820 | ret = true; | |
8821 | } | |
8822 | } | |
8823 | ||
e1915f76 | 8824 | ret |= io_cancel_defer_files(ctx, task, files); |
9936c7c2 PB |
8825 | ret |= io_poll_remove_all(ctx, task, files); |
8826 | ret |= io_kill_timeouts(ctx, task, files); | |
8827 | ret |= io_run_task_work(); | |
ba50a036 | 8828 | ret |= io_run_ctx_fallback(ctx); |
9936c7c2 PB |
8829 | if (!ret) |
8830 | break; | |
8831 | cond_resched(); | |
8832 | } | |
8833 | } | |
8834 | ||
cf27f3b1 | 8835 | static int __io_uring_add_task_file(struct io_ring_ctx *ctx) |
0f212204 | 8836 | { |
236434c3 | 8837 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 8838 | struct io_tctx_node *node; |
a528b04e | 8839 | int ret; |
236434c3 MWO |
8840 | |
8841 | if (unlikely(!tctx)) { | |
5aa75ed5 | 8842 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
8843 | if (unlikely(ret)) |
8844 | return ret; | |
236434c3 | 8845 | tctx = current->io_uring; |
0f212204 | 8846 | } |
cf27f3b1 PB |
8847 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
8848 | node = kmalloc(sizeof(*node), GFP_KERNEL); | |
8849 | if (!node) | |
8850 | return -ENOMEM; | |
8851 | node->ctx = ctx; | |
8852 | node->task = current; | |
13bf43f5 | 8853 | |
cf27f3b1 PB |
8854 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
8855 | node, GFP_KERNEL)); | |
8856 | if (ret) { | |
8857 | kfree(node); | |
8858 | return ret; | |
0f212204 | 8859 | } |
cf27f3b1 PB |
8860 | |
8861 | mutex_lock(&ctx->uring_lock); | |
8862 | list_add(&node->ctx_node, &ctx->tctx_list); | |
8863 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 8864 | } |
cf27f3b1 | 8865 | tctx->last = ctx; |
0f212204 JA |
8866 | return 0; |
8867 | } | |
8868 | ||
cf27f3b1 PB |
8869 | /* |
8870 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
8871 | */ | |
8872 | static inline int io_uring_add_task_file(struct io_ring_ctx *ctx) | |
8873 | { | |
8874 | struct io_uring_task *tctx = current->io_uring; | |
8875 | ||
8876 | if (likely(tctx && tctx->last == ctx)) | |
8877 | return 0; | |
8878 | return __io_uring_add_task_file(ctx); | |
8879 | } | |
8880 | ||
0f212204 JA |
8881 | /* |
8882 | * Remove this io_uring_file -> task mapping. | |
8883 | */ | |
2941267b | 8884 | static void io_uring_del_task_file(unsigned long index) |
0f212204 JA |
8885 | { |
8886 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 8887 | struct io_tctx_node *node; |
2941267b | 8888 | |
eebd2e37 PB |
8889 | if (!tctx) |
8890 | return; | |
13bf43f5 PB |
8891 | node = xa_erase(&tctx->xa, index); |
8892 | if (!node) | |
2941267b | 8893 | return; |
0f212204 | 8894 | |
13bf43f5 PB |
8895 | WARN_ON_ONCE(current != node->task); |
8896 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
8897 | ||
8898 | mutex_lock(&node->ctx->uring_lock); | |
8899 | list_del(&node->ctx_node); | |
8900 | mutex_unlock(&node->ctx->uring_lock); | |
8901 | ||
baf186c4 | 8902 | if (tctx->last == node->ctx) |
0f212204 | 8903 | tctx->last = NULL; |
13bf43f5 | 8904 | kfree(node); |
0f212204 JA |
8905 | } |
8906 | ||
8452d4a6 | 8907 | static void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 8908 | { |
13bf43f5 | 8909 | struct io_tctx_node *node; |
de7f1d9e PB |
8910 | unsigned long index; |
8911 | ||
13bf43f5 | 8912 | xa_for_each(&tctx->xa, index, node) |
2941267b | 8913 | io_uring_del_task_file(index); |
8452d4a6 PB |
8914 | if (tctx->io_wq) { |
8915 | io_wq_put_and_exit(tctx->io_wq); | |
8916 | tctx->io_wq = NULL; | |
8917 | } | |
de7f1d9e PB |
8918 | } |
8919 | ||
3f48cf18 | 8920 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
521d6a73 | 8921 | { |
3f48cf18 PB |
8922 | if (tracked) |
8923 | return atomic_read(&tctx->inflight_tracked); | |
521d6a73 PB |
8924 | return percpu_counter_sum(&tctx->inflight); |
8925 | } | |
8926 | ||
8927 | static void io_sqpoll_cancel_cb(struct callback_head *cb) | |
8928 | { | |
8929 | struct io_tctx_exit *work = container_of(cb, struct io_tctx_exit, task_work); | |
8930 | struct io_ring_ctx *ctx = work->ctx; | |
8931 | struct io_sq_data *sqd = ctx->sq_data; | |
8932 | ||
8933 | if (sqd->thread) | |
8934 | io_uring_cancel_sqpoll(ctx); | |
8935 | complete(&work->completion); | |
8936 | } | |
8937 | ||
8938 | static void io_sqpoll_cancel_sync(struct io_ring_ctx *ctx) | |
8939 | { | |
8940 | struct io_sq_data *sqd = ctx->sq_data; | |
8941 | struct io_tctx_exit work = { .ctx = ctx, }; | |
8942 | struct task_struct *task; | |
8943 | ||
8944 | io_sq_thread_park(sqd); | |
8945 | list_del_init(&ctx->sqd_list); | |
8946 | io_sqd_update_thread_idle(sqd); | |
8947 | task = sqd->thread; | |
8948 | if (task) { | |
8949 | init_completion(&work.completion); | |
8950 | init_task_work(&work.task_work, io_sqpoll_cancel_cb); | |
b7f5a0bf | 8951 | io_task_work_add_head(&sqd->park_task_work, &work.task_work); |
521d6a73 PB |
8952 | wake_up_process(task); |
8953 | } | |
8954 | io_sq_thread_unpark(sqd); | |
8955 | ||
8956 | if (task) | |
8957 | wait_for_completion(&work.completion); | |
8958 | } | |
8959 | ||
368b2080 | 8960 | static void io_uring_try_cancel(struct files_struct *files) |
0f212204 JA |
8961 | { |
8962 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 8963 | struct io_tctx_node *node; |
ce765372 | 8964 | unsigned long index; |
0f212204 | 8965 | |
521d6a73 PB |
8966 | xa_for_each(&tctx->xa, index, node) { |
8967 | struct io_ring_ctx *ctx = node->ctx; | |
8968 | ||
8969 | if (ctx->sq_data) { | |
8970 | io_sqpoll_cancel_sync(ctx); | |
8971 | continue; | |
8972 | } | |
368b2080 | 8973 | io_uring_try_cancel_requests(ctx, current, files); |
521d6a73 | 8974 | } |
fdaf083c JA |
8975 | } |
8976 | ||
521d6a73 | 8977 | /* should only be called by SQPOLL task */ |
0e9ddb39 PB |
8978 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx) |
8979 | { | |
37d1e2e3 | 8980 | struct io_sq_data *sqd = ctx->sq_data; |
521d6a73 | 8981 | struct io_uring_task *tctx = current->io_uring; |
0e9ddb39 PB |
8982 | s64 inflight; |
8983 | DEFINE_WAIT(wait); | |
fdaf083c | 8984 | |
521d6a73 PB |
8985 | WARN_ON_ONCE(!sqd || ctx->sq_data->thread != current); |
8986 | ||
0e9ddb39 PB |
8987 | atomic_inc(&tctx->in_idle); |
8988 | do { | |
8989 | /* read completions before cancelations */ | |
3f48cf18 | 8990 | inflight = tctx_inflight(tctx, false); |
0e9ddb39 PB |
8991 | if (!inflight) |
8992 | break; | |
521d6a73 | 8993 | io_uring_try_cancel_requests(ctx, current, NULL); |
fdaf083c | 8994 | |
0e9ddb39 PB |
8995 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
8996 | /* | |
8997 | * If we've seen completions, retry without waiting. This | |
8998 | * avoids a race where a completion comes in before we did | |
8999 | * prepare_to_wait(). | |
9000 | */ | |
3f48cf18 | 9001 | if (inflight == tctx_inflight(tctx, false)) |
0e9ddb39 PB |
9002 | schedule(); |
9003 | finish_wait(&tctx->wait, &wait); | |
9004 | } while (1); | |
9005 | atomic_dec(&tctx->in_idle); | |
0f212204 JA |
9006 | } |
9007 | ||
0f212204 JA |
9008 | /* |
9009 | * Find any io_uring fd that this task has registered or done IO on, and cancel | |
9010 | * requests. | |
9011 | */ | |
3f48cf18 | 9012 | void __io_uring_cancel(struct files_struct *files) |
0f212204 JA |
9013 | { |
9014 | struct io_uring_task *tctx = current->io_uring; | |
9015 | DEFINE_WAIT(wait); | |
d8a6df10 | 9016 | s64 inflight; |
0f212204 JA |
9017 | |
9018 | /* make sure overflow events are dropped */ | |
fdaf083c | 9019 | atomic_inc(&tctx->in_idle); |
3f48cf18 | 9020 | io_uring_try_cancel(files); |
5a978dcf | 9021 | |
d8a6df10 | 9022 | do { |
0f212204 | 9023 | /* read completions before cancelations */ |
3f48cf18 | 9024 | inflight = tctx_inflight(tctx, !!files); |
d8a6df10 JA |
9025 | if (!inflight) |
9026 | break; | |
3f48cf18 | 9027 | io_uring_try_cancel(files); |
0f212204 JA |
9028 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
9029 | ||
9030 | /* | |
a1bb3cd5 PB |
9031 | * If we've seen completions, retry without waiting. This |
9032 | * avoids a race where a completion comes in before we did | |
9033 | * prepare_to_wait(). | |
0f212204 | 9034 | */ |
3f48cf18 | 9035 | if (inflight == tctx_inflight(tctx, !!files)) |
a1bb3cd5 | 9036 | schedule(); |
f57555ed | 9037 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 9038 | } while (1); |
fdaf083c | 9039 | atomic_dec(&tctx->in_idle); |
de7f1d9e | 9040 | |
8452d4a6 | 9041 | io_uring_clean_tctx(tctx); |
3f48cf18 PB |
9042 | if (!files) { |
9043 | /* for exec all current's requests should be gone, kill tctx */ | |
9044 | __io_uring_free(current); | |
9045 | } | |
44e728b8 PB |
9046 | } |
9047 | ||
6c5c240e RP |
9048 | static void *io_uring_validate_mmap_request(struct file *file, |
9049 | loff_t pgoff, size_t sz) | |
2b188cc1 | 9050 | { |
2b188cc1 | 9051 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 9052 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
9053 | struct page *page; |
9054 | void *ptr; | |
9055 | ||
9056 | switch (offset) { | |
9057 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
9058 | case IORING_OFF_CQ_RING: |
9059 | ptr = ctx->rings; | |
2b188cc1 JA |
9060 | break; |
9061 | case IORING_OFF_SQES: | |
9062 | ptr = ctx->sq_sqes; | |
9063 | break; | |
2b188cc1 | 9064 | default: |
6c5c240e | 9065 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
9066 | } |
9067 | ||
9068 | page = virt_to_head_page(ptr); | |
a50b854e | 9069 | if (sz > page_size(page)) |
6c5c240e RP |
9070 | return ERR_PTR(-EINVAL); |
9071 | ||
9072 | return ptr; | |
9073 | } | |
9074 | ||
9075 | #ifdef CONFIG_MMU | |
9076 | ||
9077 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9078 | { | |
9079 | size_t sz = vma->vm_end - vma->vm_start; | |
9080 | unsigned long pfn; | |
9081 | void *ptr; | |
9082 | ||
9083 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
9084 | if (IS_ERR(ptr)) | |
9085 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9086 | |
9087 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9088 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9089 | } | |
9090 | ||
6c5c240e RP |
9091 | #else /* !CONFIG_MMU */ |
9092 | ||
9093 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9094 | { | |
9095 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9096 | } | |
9097 | ||
9098 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9099 | { | |
9100 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9101 | } | |
9102 | ||
9103 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9104 | unsigned long addr, unsigned long len, | |
9105 | unsigned long pgoff, unsigned long flags) | |
9106 | { | |
9107 | void *ptr; | |
9108 | ||
9109 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9110 | if (IS_ERR(ptr)) | |
9111 | return PTR_ERR(ptr); | |
9112 | ||
9113 | return (unsigned long) ptr; | |
9114 | } | |
9115 | ||
9116 | #endif /* !CONFIG_MMU */ | |
9117 | ||
d9d05217 | 9118 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 JA |
9119 | { |
9120 | DEFINE_WAIT(wait); | |
9121 | ||
9122 | do { | |
9123 | if (!io_sqring_full(ctx)) | |
9124 | break; | |
90554200 JA |
9125 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
9126 | ||
9127 | if (!io_sqring_full(ctx)) | |
9128 | break; | |
90554200 JA |
9129 | schedule(); |
9130 | } while (!signal_pending(current)); | |
9131 | ||
9132 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
5199328a | 9133 | return 0; |
90554200 JA |
9134 | } |
9135 | ||
c73ebb68 HX |
9136 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9137 | struct __kernel_timespec __user **ts, | |
9138 | const sigset_t __user **sig) | |
9139 | { | |
9140 | struct io_uring_getevents_arg arg; | |
9141 | ||
9142 | /* | |
9143 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9144 | * is just a pointer to the sigset_t. | |
9145 | */ | |
9146 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9147 | *sig = (const sigset_t __user *) argp; | |
9148 | *ts = NULL; | |
9149 | return 0; | |
9150 | } | |
9151 | ||
9152 | /* | |
9153 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9154 | * timespec and sigset_t pointers if good. | |
9155 | */ | |
9156 | if (*argsz != sizeof(arg)) | |
9157 | return -EINVAL; | |
9158 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9159 | return -EFAULT; | |
9160 | *sig = u64_to_user_ptr(arg.sigmask); | |
9161 | *argsz = arg.sigmask_sz; | |
9162 | *ts = u64_to_user_ptr(arg.ts); | |
9163 | return 0; | |
9164 | } | |
9165 | ||
2b188cc1 | 9166 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9167 | u32, min_complete, u32, flags, const void __user *, argp, |
9168 | size_t, argsz) | |
2b188cc1 JA |
9169 | { |
9170 | struct io_ring_ctx *ctx; | |
2b188cc1 JA |
9171 | int submitted = 0; |
9172 | struct fd f; | |
33f993da | 9173 | long ret; |
2b188cc1 | 9174 | |
4c6e277c | 9175 | io_run_task_work(); |
b41e9852 | 9176 | |
33f993da PB |
9177 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
9178 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) | |
2b188cc1 JA |
9179 | return -EINVAL; |
9180 | ||
9181 | f = fdget(fd); | |
33f993da | 9182 | if (unlikely(!f.file)) |
2b188cc1 JA |
9183 | return -EBADF; |
9184 | ||
9185 | ret = -EOPNOTSUPP; | |
33f993da | 9186 | if (unlikely(f.file->f_op != &io_uring_fops)) |
2b188cc1 JA |
9187 | goto out_fput; |
9188 | ||
9189 | ret = -ENXIO; | |
9190 | ctx = f.file->private_data; | |
33f993da | 9191 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
2b188cc1 JA |
9192 | goto out_fput; |
9193 | ||
7e84e1c7 | 9194 | ret = -EBADFD; |
33f993da | 9195 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
9196 | goto out; |
9197 | ||
6c271ce2 JA |
9198 | /* |
9199 | * For SQ polling, the thread will do all submissions and completions. | |
9200 | * Just return the requested submit count, and wake the thread if | |
9201 | * we were asked to. | |
9202 | */ | |
b2a9eada | 9203 | ret = 0; |
6c271ce2 | 9204 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
6c2450ae | 9205 | io_cqring_overflow_flush(ctx, false); |
89448c47 | 9206 | |
d9d05217 | 9207 | ret = -EOWNERDEAD; |
04147488 SM |
9208 | if (unlikely(ctx->sq_data->thread == NULL)) { |
9209 | goto out; | |
9210 | } | |
6c271ce2 | 9211 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9212 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9213 | if (flags & IORING_ENTER_SQ_WAIT) { |
9214 | ret = io_sqpoll_wait_sq(ctx); | |
9215 | if (ret) | |
9216 | goto out; | |
9217 | } | |
6c271ce2 | 9218 | submitted = to_submit; |
b2a9eada | 9219 | } else if (to_submit) { |
baf186c4 | 9220 | ret = io_uring_add_task_file(ctx); |
0f212204 JA |
9221 | if (unlikely(ret)) |
9222 | goto out; | |
2b188cc1 | 9223 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9224 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9225 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9226 | |
9227 | if (submitted != to_submit) | |
9228 | goto out; | |
2b188cc1 JA |
9229 | } |
9230 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9231 | const sigset_t __user *sig; |
9232 | struct __kernel_timespec __user *ts; | |
9233 | ||
9234 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9235 | if (unlikely(ret)) | |
9236 | goto out; | |
9237 | ||
2b188cc1 JA |
9238 | min_complete = min(min_complete, ctx->cq_entries); |
9239 | ||
32b2244a XW |
9240 | /* |
9241 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9242 | * space applications don't need to do io completion events | |
9243 | * polling again, they can rely on io_sq_thread to do polling | |
9244 | * work, which can reduce cpu usage and uring_lock contention. | |
9245 | */ | |
9246 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
9247 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 9248 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 9249 | } else { |
c73ebb68 | 9250 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 9251 | } |
2b188cc1 JA |
9252 | } |
9253 | ||
7c504e65 | 9254 | out: |
6805b32e | 9255 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
9256 | out_fput: |
9257 | fdput(f); | |
9258 | return submitted ? submitted : ret; | |
9259 | } | |
9260 | ||
bebdb65e | 9261 | #ifdef CONFIG_PROC_FS |
61cf9370 MWO |
9262 | static int io_uring_show_cred(struct seq_file *m, unsigned int id, |
9263 | const struct cred *cred) | |
87ce955b | 9264 | { |
87ce955b JA |
9265 | struct user_namespace *uns = seq_user_ns(m); |
9266 | struct group_info *gi; | |
9267 | kernel_cap_t cap; | |
9268 | unsigned __capi; | |
9269 | int g; | |
9270 | ||
9271 | seq_printf(m, "%5d\n", id); | |
9272 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
9273 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
9274 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
9275 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
9276 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
9277 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
9278 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
9279 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
9280 | seq_puts(m, "\n\tGroups:\t"); | |
9281 | gi = cred->group_info; | |
9282 | for (g = 0; g < gi->ngroups; g++) { | |
9283 | seq_put_decimal_ull(m, g ? " " : "", | |
9284 | from_kgid_munged(uns, gi->gid[g])); | |
9285 | } | |
9286 | seq_puts(m, "\n\tCapEff:\t"); | |
9287 | cap = cred->cap_effective; | |
9288 | CAP_FOR_EACH_U32(__capi) | |
9289 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
9290 | seq_putc(m, '\n'); | |
9291 | return 0; | |
9292 | } | |
9293 | ||
9294 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
9295 | { | |
dbbe9c64 | 9296 | struct io_sq_data *sq = NULL; |
fad8e0de | 9297 | bool has_lock; |
87ce955b JA |
9298 | int i; |
9299 | ||
fad8e0de JA |
9300 | /* |
9301 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
9302 | * since fdinfo case grabs it in the opposite direction of normal use | |
9303 | * cases. If we fail to get the lock, we just don't iterate any | |
9304 | * structures that could be going away outside the io_uring mutex. | |
9305 | */ | |
9306 | has_lock = mutex_trylock(&ctx->uring_lock); | |
9307 | ||
5f3f26f9 | 9308 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 9309 | sq = ctx->sq_data; |
5f3f26f9 JA |
9310 | if (!sq->thread) |
9311 | sq = NULL; | |
9312 | } | |
dbbe9c64 JQ |
9313 | |
9314 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
9315 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 9316 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 9317 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
7b29f92d | 9318 | struct file *f = io_file_from_index(ctx, i); |
87ce955b | 9319 | |
87ce955b JA |
9320 | if (f) |
9321 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
9322 | else | |
9323 | seq_printf(m, "%5u: <none>\n", i); | |
9324 | } | |
9325 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 9326 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
87ce955b | 9327 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
4751f53d | 9328 | unsigned int len = buf->ubuf_end - buf->ubuf; |
87ce955b | 9329 | |
4751f53d | 9330 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
87ce955b | 9331 | } |
61cf9370 MWO |
9332 | if (has_lock && !xa_empty(&ctx->personalities)) { |
9333 | unsigned long index; | |
9334 | const struct cred *cred; | |
9335 | ||
87ce955b | 9336 | seq_printf(m, "Personalities:\n"); |
61cf9370 MWO |
9337 | xa_for_each(&ctx->personalities, index, cred) |
9338 | io_uring_show_cred(m, index, cred); | |
87ce955b | 9339 | } |
d7718a9d JA |
9340 | seq_printf(m, "PollList:\n"); |
9341 | spin_lock_irq(&ctx->completion_lock); | |
9342 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { | |
9343 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
9344 | struct io_kiocb *req; | |
9345 | ||
9346 | hlist_for_each_entry(req, list, hash_node) | |
9347 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
9348 | req->task->task_works != NULL); | |
9349 | } | |
9350 | spin_unlock_irq(&ctx->completion_lock); | |
fad8e0de JA |
9351 | if (has_lock) |
9352 | mutex_unlock(&ctx->uring_lock); | |
87ce955b JA |
9353 | } |
9354 | ||
9355 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
9356 | { | |
9357 | struct io_ring_ctx *ctx = f->private_data; | |
9358 | ||
9359 | if (percpu_ref_tryget(&ctx->refs)) { | |
9360 | __io_uring_show_fdinfo(ctx, m); | |
9361 | percpu_ref_put(&ctx->refs); | |
9362 | } | |
9363 | } | |
bebdb65e | 9364 | #endif |
87ce955b | 9365 | |
2b188cc1 JA |
9366 | static const struct file_operations io_uring_fops = { |
9367 | .release = io_uring_release, | |
9368 | .mmap = io_uring_mmap, | |
6c5c240e RP |
9369 | #ifndef CONFIG_MMU |
9370 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
9371 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
9372 | #endif | |
2b188cc1 JA |
9373 | .poll = io_uring_poll, |
9374 | .fasync = io_uring_fasync, | |
bebdb65e | 9375 | #ifdef CONFIG_PROC_FS |
87ce955b | 9376 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 9377 | #endif |
2b188cc1 JA |
9378 | }; |
9379 | ||
9380 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
9381 | struct io_uring_params *p) | |
9382 | { | |
75b28aff HV |
9383 | struct io_rings *rings; |
9384 | size_t size, sq_array_offset; | |
2b188cc1 | 9385 | |
bd740481 JA |
9386 | /* make sure these are sane, as we already accounted them */ |
9387 | ctx->sq_entries = p->sq_entries; | |
9388 | ctx->cq_entries = p->cq_entries; | |
9389 | ||
75b28aff HV |
9390 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
9391 | if (size == SIZE_MAX) | |
9392 | return -EOVERFLOW; | |
9393 | ||
9394 | rings = io_mem_alloc(size); | |
9395 | if (!rings) | |
2b188cc1 JA |
9396 | return -ENOMEM; |
9397 | ||
75b28aff HV |
9398 | ctx->rings = rings; |
9399 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
9400 | rings->sq_ring_mask = p->sq_entries - 1; | |
9401 | rings->cq_ring_mask = p->cq_entries - 1; | |
9402 | rings->sq_ring_entries = p->sq_entries; | |
9403 | rings->cq_ring_entries = p->cq_entries; | |
9404 | ctx->sq_mask = rings->sq_ring_mask; | |
9405 | ctx->cq_mask = rings->cq_ring_mask; | |
2b188cc1 JA |
9406 | |
9407 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
9408 | if (size == SIZE_MAX) { |
9409 | io_mem_free(ctx->rings); | |
9410 | ctx->rings = NULL; | |
2b188cc1 | 9411 | return -EOVERFLOW; |
eb065d30 | 9412 | } |
2b188cc1 JA |
9413 | |
9414 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
9415 | if (!ctx->sq_sqes) { |
9416 | io_mem_free(ctx->rings); | |
9417 | ctx->rings = NULL; | |
2b188cc1 | 9418 | return -ENOMEM; |
eb065d30 | 9419 | } |
2b188cc1 | 9420 | |
2b188cc1 JA |
9421 | return 0; |
9422 | } | |
9423 | ||
9faadcc8 PB |
9424 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
9425 | { | |
9426 | int ret, fd; | |
9427 | ||
9428 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
9429 | if (fd < 0) | |
9430 | return fd; | |
9431 | ||
baf186c4 | 9432 | ret = io_uring_add_task_file(ctx); |
9faadcc8 PB |
9433 | if (ret) { |
9434 | put_unused_fd(fd); | |
9435 | return ret; | |
9436 | } | |
9437 | fd_install(fd, file); | |
9438 | return fd; | |
9439 | } | |
9440 | ||
2b188cc1 JA |
9441 | /* |
9442 | * Allocate an anonymous fd, this is what constitutes the application | |
9443 | * visible backing of an io_uring instance. The application mmaps this | |
9444 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
9445 | * we have to tie this fd to a socket for file garbage collection purposes. | |
9446 | */ | |
9faadcc8 | 9447 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
9448 | { |
9449 | struct file *file; | |
9faadcc8 | 9450 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
9451 | int ret; |
9452 | ||
2b188cc1 JA |
9453 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
9454 | &ctx->ring_sock); | |
9455 | if (ret) | |
9faadcc8 | 9456 | return ERR_PTR(ret); |
2b188cc1 JA |
9457 | #endif |
9458 | ||
2b188cc1 JA |
9459 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
9460 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 9461 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
9462 | if (IS_ERR(file)) { |
9463 | sock_release(ctx->ring_sock); | |
9464 | ctx->ring_sock = NULL; | |
9465 | } else { | |
9466 | ctx->ring_sock->file = file; | |
0f212204 | 9467 | } |
2b188cc1 | 9468 | #endif |
9faadcc8 | 9469 | return file; |
2b188cc1 JA |
9470 | } |
9471 | ||
7f13657d XW |
9472 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
9473 | struct io_uring_params __user *params) | |
2b188cc1 | 9474 | { |
2b188cc1 | 9475 | struct io_ring_ctx *ctx; |
9faadcc8 | 9476 | struct file *file; |
2b188cc1 JA |
9477 | int ret; |
9478 | ||
8110c1a6 | 9479 | if (!entries) |
2b188cc1 | 9480 | return -EINVAL; |
8110c1a6 JA |
9481 | if (entries > IORING_MAX_ENTRIES) { |
9482 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9483 | return -EINVAL; | |
9484 | entries = IORING_MAX_ENTRIES; | |
9485 | } | |
2b188cc1 JA |
9486 | |
9487 | /* | |
9488 | * Use twice as many entries for the CQ ring. It's possible for the | |
9489 | * application to drive a higher depth than the size of the SQ ring, | |
9490 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
9491 | * some flexibility in overcommitting a bit. If the application has |
9492 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
9493 | * of CQ ring entries manually. | |
2b188cc1 JA |
9494 | */ |
9495 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
9496 | if (p->flags & IORING_SETUP_CQSIZE) { |
9497 | /* | |
9498 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
9499 | * to a power-of-two, if it isn't already. We do NOT impose | |
9500 | * any cq vs sq ring sizing. | |
9501 | */ | |
eb2667b3 | 9502 | if (!p->cq_entries) |
33a107f0 | 9503 | return -EINVAL; |
8110c1a6 JA |
9504 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
9505 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9506 | return -EINVAL; | |
9507 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
9508 | } | |
eb2667b3 JQ |
9509 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
9510 | if (p->cq_entries < p->sq_entries) | |
9511 | return -EINVAL; | |
33a107f0 JA |
9512 | } else { |
9513 | p->cq_entries = 2 * p->sq_entries; | |
9514 | } | |
2b188cc1 | 9515 | |
2b188cc1 | 9516 | ctx = io_ring_ctx_alloc(p); |
62e398be | 9517 | if (!ctx) |
2b188cc1 | 9518 | return -ENOMEM; |
2b188cc1 | 9519 | ctx->compat = in_compat_syscall(); |
62e398be JA |
9520 | if (!capable(CAP_IPC_LOCK)) |
9521 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
9522 | |
9523 | /* | |
9524 | * This is just grabbed for accounting purposes. When a process exits, | |
9525 | * the mm is exited and dropped before the files, hence we need to hang | |
9526 | * on to this mm purely for the purposes of being able to unaccount | |
9527 | * memory (locked/pinned vm). It's not used for anything else. | |
9528 | */ | |
6b7898eb | 9529 | mmgrab(current->mm); |
2aede0e4 | 9530 | ctx->mm_account = current->mm; |
6b7898eb | 9531 | |
2b188cc1 JA |
9532 | ret = io_allocate_scq_urings(ctx, p); |
9533 | if (ret) | |
9534 | goto err; | |
9535 | ||
7e84e1c7 | 9536 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
9537 | if (ret) |
9538 | goto err; | |
9539 | ||
2b188cc1 | 9540 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
9541 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
9542 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
9543 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
9544 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
9545 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
9546 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
9547 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
9548 | |
9549 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
9550 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
9551 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
9552 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
9553 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
9554 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
9555 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 9556 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 9557 | |
7f13657d XW |
9558 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
9559 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 9560 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 9561 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
1c0aa1fa | 9562 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS; |
7f13657d XW |
9563 | |
9564 | if (copy_to_user(params, p, sizeof(*p))) { | |
9565 | ret = -EFAULT; | |
9566 | goto err; | |
9567 | } | |
d1719f70 | 9568 | |
9faadcc8 PB |
9569 | file = io_uring_get_file(ctx); |
9570 | if (IS_ERR(file)) { | |
9571 | ret = PTR_ERR(file); | |
9572 | goto err; | |
9573 | } | |
9574 | ||
044c1ab3 JA |
9575 | /* |
9576 | * Install ring fd as the very last thing, so we don't risk someone | |
9577 | * having closed it before we finish setup | |
9578 | */ | |
9faadcc8 PB |
9579 | ret = io_uring_install_fd(ctx, file); |
9580 | if (ret < 0) { | |
9581 | /* fput will clean it up */ | |
9582 | fput(file); | |
9583 | return ret; | |
9584 | } | |
044c1ab3 | 9585 | |
c826bd7a | 9586 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
9587 | return ret; |
9588 | err: | |
9589 | io_ring_ctx_wait_and_kill(ctx); | |
9590 | return ret; | |
9591 | } | |
9592 | ||
9593 | /* | |
9594 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
9595 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
9596 | * params structure passed in. | |
9597 | */ | |
9598 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
9599 | { | |
9600 | struct io_uring_params p; | |
2b188cc1 JA |
9601 | int i; |
9602 | ||
9603 | if (copy_from_user(&p, params, sizeof(p))) | |
9604 | return -EFAULT; | |
9605 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
9606 | if (p.resv[i]) | |
9607 | return -EINVAL; | |
9608 | } | |
9609 | ||
6c271ce2 | 9610 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 9611 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
9612 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
9613 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
9614 | return -EINVAL; |
9615 | ||
7f13657d | 9616 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
9617 | } |
9618 | ||
9619 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
9620 | struct io_uring_params __user *, params) | |
9621 | { | |
9622 | return io_uring_setup(entries, params); | |
9623 | } | |
9624 | ||
66f4af93 JA |
9625 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
9626 | { | |
9627 | struct io_uring_probe *p; | |
9628 | size_t size; | |
9629 | int i, ret; | |
9630 | ||
9631 | size = struct_size(p, ops, nr_args); | |
9632 | if (size == SIZE_MAX) | |
9633 | return -EOVERFLOW; | |
9634 | p = kzalloc(size, GFP_KERNEL); | |
9635 | if (!p) | |
9636 | return -ENOMEM; | |
9637 | ||
9638 | ret = -EFAULT; | |
9639 | if (copy_from_user(p, arg, size)) | |
9640 | goto out; | |
9641 | ret = -EINVAL; | |
9642 | if (memchr_inv(p, 0, size)) | |
9643 | goto out; | |
9644 | ||
9645 | p->last_op = IORING_OP_LAST - 1; | |
9646 | if (nr_args > IORING_OP_LAST) | |
9647 | nr_args = IORING_OP_LAST; | |
9648 | ||
9649 | for (i = 0; i < nr_args; i++) { | |
9650 | p->ops[i].op = i; | |
9651 | if (!io_op_defs[i].not_supported) | |
9652 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
9653 | } | |
9654 | p->ops_len = i; | |
9655 | ||
9656 | ret = 0; | |
9657 | if (copy_to_user(arg, p, size)) | |
9658 | ret = -EFAULT; | |
9659 | out: | |
9660 | kfree(p); | |
9661 | return ret; | |
9662 | } | |
9663 | ||
071698e1 JA |
9664 | static int io_register_personality(struct io_ring_ctx *ctx) |
9665 | { | |
4379bf8b | 9666 | const struct cred *creds; |
61cf9370 | 9667 | u32 id; |
1e6fa521 | 9668 | int ret; |
071698e1 | 9669 | |
4379bf8b | 9670 | creds = get_current_cred(); |
1e6fa521 | 9671 | |
61cf9370 MWO |
9672 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
9673 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); | |
9674 | if (!ret) | |
9675 | return id; | |
9676 | put_cred(creds); | |
1e6fa521 | 9677 | return ret; |
071698e1 JA |
9678 | } |
9679 | ||
21b55dbc SG |
9680 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
9681 | unsigned int nr_args) | |
9682 | { | |
9683 | struct io_uring_restriction *res; | |
9684 | size_t size; | |
9685 | int i, ret; | |
9686 | ||
7e84e1c7 SG |
9687 | /* Restrictions allowed only if rings started disabled */ |
9688 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9689 | return -EBADFD; | |
9690 | ||
21b55dbc | 9691 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 9692 | if (ctx->restrictions.registered) |
21b55dbc SG |
9693 | return -EBUSY; |
9694 | ||
9695 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
9696 | return -EINVAL; | |
9697 | ||
9698 | size = array_size(nr_args, sizeof(*res)); | |
9699 | if (size == SIZE_MAX) | |
9700 | return -EOVERFLOW; | |
9701 | ||
9702 | res = memdup_user(arg, size); | |
9703 | if (IS_ERR(res)) | |
9704 | return PTR_ERR(res); | |
9705 | ||
9706 | ret = 0; | |
9707 | ||
9708 | for (i = 0; i < nr_args; i++) { | |
9709 | switch (res[i].opcode) { | |
9710 | case IORING_RESTRICTION_REGISTER_OP: | |
9711 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
9712 | ret = -EINVAL; | |
9713 | goto out; | |
9714 | } | |
9715 | ||
9716 | __set_bit(res[i].register_op, | |
9717 | ctx->restrictions.register_op); | |
9718 | break; | |
9719 | case IORING_RESTRICTION_SQE_OP: | |
9720 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
9721 | ret = -EINVAL; | |
9722 | goto out; | |
9723 | } | |
9724 | ||
9725 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
9726 | break; | |
9727 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
9728 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
9729 | break; | |
9730 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
9731 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
9732 | break; | |
9733 | default: | |
9734 | ret = -EINVAL; | |
9735 | goto out; | |
9736 | } | |
9737 | } | |
9738 | ||
9739 | out: | |
9740 | /* Reset all restrictions if an error happened */ | |
9741 | if (ret != 0) | |
9742 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
9743 | else | |
7e84e1c7 | 9744 | ctx->restrictions.registered = true; |
21b55dbc SG |
9745 | |
9746 | kfree(res); | |
9747 | return ret; | |
9748 | } | |
9749 | ||
7e84e1c7 SG |
9750 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
9751 | { | |
9752 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9753 | return -EBADFD; | |
9754 | ||
9755 | if (ctx->restrictions.registered) | |
9756 | ctx->restricted = 1; | |
9757 | ||
0298ef96 PB |
9758 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
9759 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) | |
9760 | wake_up(&ctx->sq_data->wait); | |
7e84e1c7 SG |
9761 | return 0; |
9762 | } | |
9763 | ||
071698e1 JA |
9764 | static bool io_register_op_must_quiesce(int op) |
9765 | { | |
9766 | switch (op) { | |
f4f7d21c | 9767 | case IORING_REGISTER_FILES: |
071698e1 JA |
9768 | case IORING_UNREGISTER_FILES: |
9769 | case IORING_REGISTER_FILES_UPDATE: | |
9770 | case IORING_REGISTER_PROBE: | |
9771 | case IORING_REGISTER_PERSONALITY: | |
9772 | case IORING_UNREGISTER_PERSONALITY: | |
9773 | return false; | |
9774 | default: | |
9775 | return true; | |
9776 | } | |
9777 | } | |
9778 | ||
edafccee JA |
9779 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
9780 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
9781 | __releases(ctx->uring_lock) |
9782 | __acquires(ctx->uring_lock) | |
edafccee JA |
9783 | { |
9784 | int ret; | |
9785 | ||
35fa71a0 JA |
9786 | /* |
9787 | * We're inside the ring mutex, if the ref is already dying, then | |
9788 | * someone else killed the ctx or is already going through | |
9789 | * io_uring_register(). | |
9790 | */ | |
9791 | if (percpu_ref_is_dying(&ctx->refs)) | |
9792 | return -ENXIO; | |
9793 | ||
071698e1 | 9794 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 9795 | percpu_ref_kill(&ctx->refs); |
b19062a5 | 9796 | |
05f3fb3c JA |
9797 | /* |
9798 | * Drop uring mutex before waiting for references to exit. If | |
9799 | * another thread is currently inside io_uring_enter() it might | |
9800 | * need to grab the uring_lock to make progress. If we hold it | |
9801 | * here across the drain wait, then we can deadlock. It's safe | |
9802 | * to drop the mutex here, since no new references will come in | |
9803 | * after we've killed the percpu ref. | |
9804 | */ | |
9805 | mutex_unlock(&ctx->uring_lock); | |
af9c1a44 JA |
9806 | do { |
9807 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
9808 | if (!ret) | |
9809 | break; | |
ed6930c9 JA |
9810 | ret = io_run_task_work_sig(); |
9811 | if (ret < 0) | |
9812 | break; | |
af9c1a44 | 9813 | } while (1); |
05f3fb3c | 9814 | mutex_lock(&ctx->uring_lock); |
af9c1a44 | 9815 | |
c150368b | 9816 | if (ret) { |
f70865db PB |
9817 | io_refs_resurrect(&ctx->refs, &ctx->ref_comp); |
9818 | return ret; | |
21b55dbc SG |
9819 | } |
9820 | } | |
9821 | ||
9822 | if (ctx->restricted) { | |
9823 | if (opcode >= IORING_REGISTER_LAST) { | |
9824 | ret = -EINVAL; | |
9825 | goto out; | |
9826 | } | |
9827 | ||
9828 | if (!test_bit(opcode, ctx->restrictions.register_op)) { | |
9829 | ret = -EACCES; | |
c150368b JA |
9830 | goto out; |
9831 | } | |
05f3fb3c | 9832 | } |
edafccee JA |
9833 | |
9834 | switch (opcode) { | |
9835 | case IORING_REGISTER_BUFFERS: | |
0a96bbe4 | 9836 | ret = io_sqe_buffers_register(ctx, arg, nr_args); |
edafccee JA |
9837 | break; |
9838 | case IORING_UNREGISTER_BUFFERS: | |
9839 | ret = -EINVAL; | |
9840 | if (arg || nr_args) | |
9841 | break; | |
0a96bbe4 | 9842 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 9843 | break; |
6b06314c JA |
9844 | case IORING_REGISTER_FILES: |
9845 | ret = io_sqe_files_register(ctx, arg, nr_args); | |
9846 | break; | |
9847 | case IORING_UNREGISTER_FILES: | |
9848 | ret = -EINVAL; | |
9849 | if (arg || nr_args) | |
9850 | break; | |
9851 | ret = io_sqe_files_unregister(ctx); | |
9852 | break; | |
c3a31e60 JA |
9853 | case IORING_REGISTER_FILES_UPDATE: |
9854 | ret = io_sqe_files_update(ctx, arg, nr_args); | |
9855 | break; | |
9b402849 | 9856 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 9857 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
9858 | ret = -EINVAL; |
9859 | if (nr_args != 1) | |
9860 | break; | |
9861 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
9862 | if (ret) |
9863 | break; | |
9864 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
9865 | ctx->eventfd_async = 1; | |
9866 | else | |
9867 | ctx->eventfd_async = 0; | |
9b402849 JA |
9868 | break; |
9869 | case IORING_UNREGISTER_EVENTFD: | |
9870 | ret = -EINVAL; | |
9871 | if (arg || nr_args) | |
9872 | break; | |
9873 | ret = io_eventfd_unregister(ctx); | |
9874 | break; | |
66f4af93 JA |
9875 | case IORING_REGISTER_PROBE: |
9876 | ret = -EINVAL; | |
9877 | if (!arg || nr_args > 256) | |
9878 | break; | |
9879 | ret = io_probe(ctx, arg, nr_args); | |
9880 | break; | |
071698e1 JA |
9881 | case IORING_REGISTER_PERSONALITY: |
9882 | ret = -EINVAL; | |
9883 | if (arg || nr_args) | |
9884 | break; | |
9885 | ret = io_register_personality(ctx); | |
9886 | break; | |
9887 | case IORING_UNREGISTER_PERSONALITY: | |
9888 | ret = -EINVAL; | |
9889 | if (arg) | |
9890 | break; | |
9891 | ret = io_unregister_personality(ctx, nr_args); | |
9892 | break; | |
7e84e1c7 SG |
9893 | case IORING_REGISTER_ENABLE_RINGS: |
9894 | ret = -EINVAL; | |
9895 | if (arg || nr_args) | |
9896 | break; | |
9897 | ret = io_register_enable_rings(ctx); | |
9898 | break; | |
21b55dbc SG |
9899 | case IORING_REGISTER_RESTRICTIONS: |
9900 | ret = io_register_restrictions(ctx, arg, nr_args); | |
9901 | break; | |
edafccee JA |
9902 | default: |
9903 | ret = -EINVAL; | |
9904 | break; | |
9905 | } | |
9906 | ||
21b55dbc | 9907 | out: |
071698e1 | 9908 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 9909 | /* bring the ctx back to life */ |
05f3fb3c | 9910 | percpu_ref_reinit(&ctx->refs); |
0f158b4c | 9911 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 9912 | } |
edafccee JA |
9913 | return ret; |
9914 | } | |
9915 | ||
9916 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
9917 | void __user *, arg, unsigned int, nr_args) | |
9918 | { | |
9919 | struct io_ring_ctx *ctx; | |
9920 | long ret = -EBADF; | |
9921 | struct fd f; | |
9922 | ||
9923 | f = fdget(fd); | |
9924 | if (!f.file) | |
9925 | return -EBADF; | |
9926 | ||
9927 | ret = -EOPNOTSUPP; | |
9928 | if (f.file->f_op != &io_uring_fops) | |
9929 | goto out_fput; | |
9930 | ||
9931 | ctx = f.file->private_data; | |
9932 | ||
b6c23dd5 PB |
9933 | io_run_task_work(); |
9934 | ||
edafccee JA |
9935 | mutex_lock(&ctx->uring_lock); |
9936 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
9937 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
9938 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
9939 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
9940 | out_fput: |
9941 | fdput(f); | |
9942 | return ret; | |
9943 | } | |
9944 | ||
2b188cc1 JA |
9945 | static int __init io_uring_init(void) |
9946 | { | |
d7f62e82 SM |
9947 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
9948 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
9949 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
9950 | } while (0) | |
9951 | ||
9952 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
9953 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
9954 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
9955 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
9956 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
9957 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
9958 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
9959 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
9960 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
9961 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 9962 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
9963 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
9964 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
9965 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
9966 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
9967 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
9968 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
9969 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
9970 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
9971 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
9972 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
9973 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
9974 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
9975 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
9976 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
9977 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 9978 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
9979 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
9980 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
9981 | BUILD_BUG_SQE_ELEM(42, __u16, personality); | |
7d67af2c | 9982 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
d7f62e82 | 9983 | |
d3656344 | 9984 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
84557871 | 9985 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
91f245d5 JA |
9986 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
9987 | SLAB_ACCOUNT); | |
2b188cc1 JA |
9988 | return 0; |
9989 | }; | |
9990 | __initcall(io_uring_init); |